Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert raw grayscale binary to JPEG

I have C language source code, for an embedded system, containing arrays of data for an 8-bit per pixel grayscale image. I'm in charge of documenting the software and I'd like to convert this source code to a JPEG (image) file.

Here is a code sample:

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)

  1. What application(s) do you recommend for converting this source file to JPEG?
  2. Can GIMP or Paint import a CSV file of data?
  3. If I write this custom application, what Java libraries exist for JPEG?
  4. What libraries exist in C# for accomplishing 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 the custom application in C#, Java, C or C++.

Thanks for your advice.

like image 980
Thomas Matthews Avatar asked Aug 16 '11 15:08

Thomas Matthews


People also ask

How to convert an image to grayscale online?

Free online image to grayscale converter. Just drag and drop your image and it will be automatically grayscaled. There are no ads, popups or nonsense, just an awesome image grayscaler. Load an image, grayscale an image. Created by engineers from team Browserling . We just created Online GIF Tools with dozens of utilities for editing GIFs.

How to convert binary to image for free?

Free online binary to image converter. Just load your binary number and it will automatically get converted to an image. There are no ads, popups or nonsense, just a binary to image converter. Load a binary value, get an image. Created for developers by developers from team Browserling .

How to convert raw files to JPG?

How to Convert RAW to JPG? 1 Click the “Choose Files” button to select your RAW files. 2 Click the “Convert to JPG” button to start the conversion. 3 When the status change to “Done” click the “Download JPG” button.

How to convert a multicolor JPG to two-color binary image?

As soon as you paste a JPG/JPEG picture in the input area, the utility will convert a multicolor JPG into a two-color binary picture. You can specify the two JPG colors in the options or let the program select the colors automatically. Also, you can enable color dithering and choose the dithering method.


2 Answers

The issue with using java will be to get the bytes as ints. While reading in you will need to convert to an int in order to capture values > 127 because java does not have 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);
like image 102
Clint Avatar answered Sep 30 '22 09:09

Clint


I can answers question 4 and I can give you the code to do 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 may also be able to optimise this code if you look around for bitmap optimization techniques (such as locking the bitmap memory)

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

NOTE: I am not 100% sure about the PixelFormat used when creating the Bitmap object - was my best guess at 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;
like image 32
musefan Avatar answered Sep 30 '22 10:09

musefan