Coder Social home page Coder Social logo

bmp-sharp's Introduction

BMP# (bmp-sharp)

Simple .NET fully managed .NetStandard C# library for handling Bitmap (BMP) files.

Description

This is simple Bitmap library that helps you wrap binary data (pixels) into BMP header for saving into file and vice versa.

It supports only 24 bits BGR and 32 bits BGRA byte arrays.

Library supports x86 (little-endian) and ARM (big-endian).

Documentation on wiki pages.

Why this library

I'm working on a library that will help me parse CIFAR 10 dataset and I wanted to simply display binary image data. Problem was that I couldn't find any .NET library that will take byte[] and with few parameters display it as image (without conversion into float or use Windows.System.Forms/WPF) so I wrote this simple library to help me out.

Also usefull for exporting [SkiaSharp] SKBitmap data (SkiaSharp doesn't supprot this Check issue #320).

var outputImage = SkiaSharp.SKBitmap.Decode( imageFile );
if (!(outputImage.BytesPerPixel == 3 || outputImage.BytesPerPixel == 4))
	throw new Exception( $"Unsupported Bits per image ({outputImage.BytesPerPixel}) for BmpSharp" );

var bitsPerPixel = outputImage.BytesPerPixel == 4 ? BmpSharp.BitsPerPixelEnum.RGBA32 : BmpSharp.BitsPerPixelEnum.RGB24;
var bmp = new BmpSharp.Bitmap( outputImage.Width, outputImage.Height, outputImage.Bytes, bitsPerPixel );
return bmp.GetBmpBytes(flipped:true);	// returns BMP File as byte[] array

Samples

In sample folder.

public static byte[] GenerateRedGreen24BitGradient() {
	var width = 256;
	var height = 256;
	var bytesPerPixel = (int) BmpSharp.BytesPerPixelEnum.RGB24;
	var framebuffer = new byte[width * height * bytesPerPixel];

	for (var row = 0; row < height; row++) {
		for (var column = 0; column < width; column++) {
			long offset = ( row * height * bytesPerPixel ) + ( column * bytesPerPixel );
			framebuffer[offset] = 0; // blue
			framebuffer[offset + 1] = (byte) column; // green
			framebuffer[offset + 2] = (byte) row; // red
		}
	}
	return framebuffer;
}
var redGreenGradient = GenerateRedGreen24BitGradient();
var redGreenGradientBitmap = new Bitmap( 256, 256, redGreenGradient, BitsPerPixelEnum.RGB24 );
System.IO.File.WriteAllBytes( $"{nameof( redGreenGradient )}.bmp", redGreenGradientBitmap.GetBmpBytes() );

Result:

Exported image

Out of scope

  • All different headers formats
  • Image filtering
  • Compression support (use PNG)
  • 1/4/8 bits per pixel support (i'm open for pull requests)

History

0.2.0 Bugfixes & RGBA32 Support

  • Bug fixes and Code refactoring (there are code breaking changes from version 0.1.0).
  • Added RGBA (alpha channel) support (32 bits)

0.1.0 Initial release

  • Added support of saving byte[] to BMP
  • Should support reading/saving of little/big endian platforms (x86 / ARM)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.