Convert PNG to C Source .h file

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
ktfoley
Posts: 2
Joined: 2014-07-09T19:09:39-07:00
Authentication code: 6789

Convert PNG to C Source .h file

Post by ktfoley »

New to this.
Need to convert a PNG image (of a mouse cursor, 32x32 pixels) into two C arrays, one with just the Alpha, and one with the RGB8.

How do I get just the Alpha into one .h file, and just the RGB into the other .h file?

I have tried: convert foo.png -channel RGB foo_rgb.h
I have tried: convert foo.png -channel ALPHA foo_a.h


Thanks!
Using: Centos 6.5, Code::Blocks 98xx, wxWidgets 2.8.12, ImageMagick 6.5.4-7
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert PNG to C Source .h file

Post by fmw42 »

Note: I am not a software engineer so I am not sure if you want ASCII or binary data. If ASCII, then I think you will need to convert the image to txt: format and then extract the channels as you need them as ASCII. Or separate the alpha channel from the image and write them NetPBM format -- the alpha to ASCII pgm and the rgb to ASCII ppm.

To separate the alpha channel use

convert image -alpha extract P2:image.pgm
convert image -alpha off P3:image.ppm

see
http://netpbm.sourceforge.net/doc/ppm.html
http://netpbm.sourceforge.net/doc/pgm.html
http://www.imagemagick.org/Usage/files/#txt

EDIT: the P2: and P3: prefix to these outputs do not seem to work. I was sure there was a way to force the PBM images to ascii format.


If you want just binary data (no headers), I believe you can use .gray and .rgb

convert image -alpha extract GRAY:alpha.gray
convert image -alpha off RGB:rgb.rgb

see http://www.imagemagick.org/script/formats.php (the prefixes may not be necessary).
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert PNG to C Source .h file

Post by snibgo »

You'll need to check this, but I think:

Code: Select all

convert in.png out_rgb.h

Code: Select all

convert in.png -alpha extract out_alpha.h
The second gives one byte per pixel, ie PNM "P5".

(Your version of IM is very old. Very very old.)

fmw42 wrote:I was sure there was a way to force the PBM images to ascii format.
"-compress none".
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert PNG to C Source .h file

Post by fmw42 »

snibgo wrote:"-compress none".
Thanks. That was it. I will modify the IM formats page to add this note.

Is there documentation on using .h suffix to get PBM format images?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert PNG to C Source .h file

Post by snibgo »

I'm fairly sure .h is documented somewhere, but I can't find where. Maybe it isn't.

It isn't on the pages you linked to, nor "convert -list format". It should be.
snibgo's IM pages: im.snibgo.com
ktfoley
Posts: 2
Joined: 2014-07-09T19:09:39-07:00
Authentication code: 6789

Re: Convert PNG to C Source .h file

Post by ktfoley »

Thanks folks. I appreciate the help. As a follow up, how would I mogrify multiple png files into a single .h? And did anyone find the documentation support for .h output?

mogrify -format h *.png

... creates one .h for each png. This is time consuming.

Thanks again!]]



centos 6.5 (which is where IM is built into command line, which is why it's such an old build)
Code::Blocks
wxWidgetpalooza
PB&J.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert PNG to C Source .h file

Post by fmw42 »

mogrify only works by reading one image at a time and outputting one image for each input image.

What do you mean by reading multiple images and writing out only one? How do you want to combine the output images from each input image? Do you want layers/frames/pages in one file or do you want to append all the images side-by-side?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert PNG to C Source .h file

Post by snibgo »

I don't know about montage[*]. With convert, you can:

Code: Select all

convert bw*.png h:- >abcd.h
to get two images inside one H file, but then you have a name clash within abcd.h.

[*]EDIT: I meant mogrify. Sorry.
snibgo's IM pages: im.snibgo.com
Post Reply