0

I am looking to convert an image file to an array of 24-bit RGB values. To simplify things, I will only concentrate on uncompressed formats like PNG or bitmap.

The idea is to the use that array in code to print an image to screen. This can be 32-bit RGB but I will only use RRGGBB values and discount the alpha channel.

I've searched around and there's an online converter to do this (https://littlevgl.com/image-to-c-array) but I am looking to do this on the fly.

Any help appreciated.

Captain Jack
  • 144
  • 13
  • `PNG` is both filtered on a line-by-line basis and compressed. I would recommend CImg http://cimg.eu to load and process almost any format easily. – Mark Setchell Jan 09 '20 at 17:09
  • @MarkSetchell, it seems cimg is C++ only. Is it usable from C? – Paul Ogilvie Jan 09 '20 at 17:11
  • Ah - right. I suspect bitmaps will be uncompressed. I am happy to stick with a truly uncompressed format. CImg is C++ - I would need something in C and preferably lightweight. I will just be loading the file once on program start and overlaying the image on screen. – Captain Jack Jan 09 '20 at 17:13
  • @PaulOgilvie Well spotted, no, it is C++ only. Maybe OP's requirement is not too firm... – Mark Setchell Jan 09 '20 at 17:14
  • The only difference between a bitmap and an array is that the scanlines of a bitmap are padded. Can't you use the padded scanlines? – Paul Ogilvie Jan 09 '20 at 17:15
  • 1
    See https://stackoverflow.com/questions/47768094/pointer-file-randomly-changes-value-in-middle-of-reading-raw-bitmap-data for an example of how to read (and use) bitmaps. – Paul Ogilvie Jan 09 '20 at 17:16
  • My knowledge of image formats is a little hazy. Is there a piece of code that would help me understand this better. – Captain Jack Jan 09 '20 at 17:17
  • @PaulOgilvie oh interesting link. That might work for me! Thanks! – Captain Jack Jan 09 '20 at 17:17
  • I would recommend NetPBM https://stackoverflow.com/a/46618886/2836621 – Mark Setchell Jan 09 '20 at 17:19
  • The "image-to-c-array" link you posted is for converting the image into an array of data to be compiled as part of the executable by the C compiler. Is that what you are actually trying to do as part of your build process? Or do you want your program to load an image file into a buffer at run-time? – Ian Abbott Jan 09 '20 at 17:21
  • @IanAbbott preferably the latter. This will allow me to change the image file that will be loaded into buffer at run time. Compiling it directly into code would be OK I guess but would be a pain to change it. – Captain Jack Jan 10 '20 at 12:32

0 Answers0