Convert binary files in black and white to JPEG

I have C source code for an embedded system containing data arrays for an 8-bit image in pixels in shades of gray. I am responsible for documenting the software, and I would like to convert this source code to a JPEG file (image).

Here is a sample code:

const unsigned char grayscale_image[] = { 0, 0, 0, 0, 0, 0, 0, 74, 106, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 146, 93, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //... }; const unsigned int height = 41; const unsigned int width = 20; 

Here are my questions: (yes, plural)

  • Which application do you recommend for converting this source file to JPEG?
  • Can GIMP or Paint import a CSV data file?
  • If I am writing this custom application, what Java libraries exist for JPEG?
  • What libraries exist in C # to accomplish this task?

I have the following resources at my disposal: MS Visio 2010, Gimp, Paint, Java, Eclipse, MS Visual Studio 2010 Professional, wxWidgets, wxFrameBuilder, Cygwin.
I can write my own application in C #, Java, C or C ++.

Thank you for your advice.

+6
source share
3 answers

The problem with using java will be to get bytes as int. When reading, you will need to convert to int in order to capture values> 127, because java has no unsigned bytes.

 int height=41; int width=20; int[] data = {...}; BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); for ( int x = 0; x < width; x++ ) { for ( int y = 0; y < height; y++ ) { // fix this based on your rastering order final int c = data[ y * width + x ]; // all of the components set to the same will be gray bi.setRGB(x,y,new Color(c,c,c).getRGB() ); } } File out = new File("image.jpg"); ImageIO.write(bi, "jpg", out); 
+1
source

I can answer question 4, and I can give you the code for this in C #. It is very simple ...

 int width = 20, height = 41; byte[] grayscale_image = {0, 0, 0, ...}; System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height); int x = 0; int y = 0; foreach (int i in grayscale_image) { bitmap.SetPixel(x, y, System.Drawing.Color.FromArgb(i, i, i)); x++; if (x >= 41) { x = 0; y++; } } bitmap.Save("output.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); 

You can also optimize this code if you look for methods to optimize the bitmap (for example, block the memory of a bitmap image)

EDIT : alternative with bit locking (should be much faster) ...

NOTE. I'm not 100% sure about the PixelFormat used when creating the Bitmap object - I was the best guess about the options available.

 int width = 20, height = 41; byte[] grayscale_image = {0, 0, 0, ...}; System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height, PixelFormat.Format8bppIndexed); System.Drawing.Imaging.BitmapData bmpData = bitmap.LockBits( new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, bitmap.PixelFormat); System.Runtime.InteropServices.Marshal.Copy(bytes, 0, bmpData.Scan0, bytes.Length); bitmap.UnlockBits(bmpData); return bitmap; 
+1
source

You can just use the ImageIO class in java.

 BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GREY); Graphics2D g2 = bi.createGraphics(); //loop through and draw the pixels here File out = new File("Myimage.jpg"); ImageIO.write(bi, "jpg", out); 
0
source

Source: https://habr.com/ru/post/895163/


All Articles