0

My program result is giving 24 bit RGB data. It is very difficult verify my result by looking into the array of 24 bit data.

If there is any easy method to convert this array to an image, I can easily verify my result. Expecting your help for the same. Please.

Example: 4x3 Resolution Image, I have following data as text file.

110000001100000011000000 110000001100000011111111 110000001100000011111111 110000001100000011111111 110000001100000011111111 110000001100000011111111 110000001100000011000000 110000001100000011111111 110000001100000011111111 110000001100000011111111 110000001100000011111111 110000001100000011111111

tollin jose
  • 845
  • 4
  • 12
  • 23
  • 2
    What language? What platform? How are you retrieving that result? What format does the result have? What image size? You really need to clarify your question – Jcl Feb 02 '15 at 08:28
  • My project is in VHDL. But as result I have 24bit RGB array as txt file. – tollin jose Feb 02 '15 at 08:30
  • As far as I know (I could be wrong), VHDL is used as a pseudo-language to describe circuit boards. How'd you want that image to be shown to you? What are the contents of that txt file? Numbers written in ASCII? Binary data with the txt extension? When asking questions, you should put yourself in the feet of those who are reading your question. We know absolutely nothing about your project, and taken what you have written, with no context, there's **ABSOLUTELY** no way anyone who doesn't know your specific project can help you. Please edit your question and give it a context. – Jcl Feb 02 '15 at 08:32
  • The question is not with respect to my project. So there is not point in explaining about my project here. You asked, that is why I told you about my project. My question is how to convert 24bit pixel array to an image. – tollin jose Feb 02 '15 at 08:40
  • 1
    in Windows? in Unix? using C#? Using VHDL? Using C++? Outputting a JPG file? Displaying to an Oculus VR? The resulting txt file stored in a external SDCard formatted with a propietary filesystem? How are we supposed to know? Programming questions need a *context*. There are almost infinite ways of "converting a 24bit pixel array to an image" using electronical devices and you need to narrow. – Jcl Feb 02 '15 at 08:41
  • @tollin - Choose whichever programming language you want. Write a program to parse the text file to separate each pixel into their respective RGB planes then write the image to file. In C you can use `libjpeg`, Java you can use the `ImageIO` class, C++ you can use [`jpeg-compressor`](https://code.google.com/p/jpeg-compressor/) - There are so many ways to do it. Looking at your previous questions, you seem to have familiarity with MATLAB, so parse the data with MATLAB and use `imwrite` to save your image. If you desire a MATLAB solution, specify that and a MATLAB user (like me) will help. – rayryeng Feb 02 '15 at 08:44
  • 2
    Recommended read: http://tinyurl.com/stack-hints – Jcl Feb 02 '15 at 08:46

1 Answers1

3

I had a look at this and it is pretty ugly but works - I think. I use awk to convert the ones and zeroes into straight numbers, and lay them out in a PPM format file which is about the simplest file format you can get from the NetPBM suite documentation here.

So, for a 4x3 image with RGB and 255 as the maximum pixel intensity, your file will need to look like this:

P3     # header saying PPM format
4 3    # 4x3 pixels
255    # max value per pixel is 255
192    # Red value of top left pixel
192    # Green value of top left pixel
192    # Blue vaue of top left pixel
...
...

So, I convert your file like this

#!/bin/bash
tr ' ' '\n' < file | awk '
   function bintxt2num(str){
      result=0;
      this=1;
      for(i=8;i>0;i--){
         if(substr(str,i,1)=="1")result += this
         this*=2
      }
      print result
   }
   BEGIN{ printf "P3\n4 3\n255\n"}
   {
      R=substr($0,1,8);  bintxt2num(R);
      G=substr($0,9,8);  bintxt2num(G);
      B=substr($0,17,8); bintxt2num(B);
   }' | convert ppm:- -scale 5000% result.png

And at the end, I use the ImageMagick tool convert to convert the PPM file output from awk into a PNG file called result.png and scale it up to a decent size while I am at it.

It looks like this:

enter image description here

In case I have made a silly mistake somewhere in my awk, your PPM file comes out looking like this:

P3
4 3
255
192
192
192
192
192
255
192
192
255
192
192
255
192
192
255
192
192
255
192
192
192
192
192
255
192
192
255
192
192
255
192
192
255
192
192
255

If you object to, or cannot install ImageMagick for some reason, you can always use the original NetPBM binaries and run ppm2tiff or ppm2jpeg to do the conversion from PPM to TIFF or JPEG. See here.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432