[Solved] Convert a PNG image to a C++ Array

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?".
Yaron
Posts: 17
Joined: 2016-09-05T19:02:33-07:00
Authentication code: 1151

[Solved] Convert a PNG image to a C++ Array

Post by Yaron »

Hello,

Notepad++ contains a source file rgba_icons.h.
I'd like to replace the image "bookmark14" with my own PNG. IOW: convert my PNG to the array used in that file.
How can I do that with ImageMagick?

I'd appreciate your help.
Last edited by Yaron on 2016-09-06T12:14:57-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert a PNG image to a C++ Array

Post by snibgo »

Use the following command:

Code: Select all

convert yourping.png out.h
Or, with IM v7, use "magick" instead of "convert".

I don't think there is a way to tell IM what name to use for the array, so use sed or whatever.

You could do it from inside a C++ program if you wanted.
snibgo's IM pages: im.snibgo.com
Yaron
Posts: 17
Joined: 2016-09-05T19:02:33-07:00
Authentication code: 1151

Re: Convert a PNG image to a C++ Array

Post by Yaron »

Hello snibgo,

Thanks for replying. I appreciate it.

I've used the convert command.
Building Notepad++ with the new array, I get a wrong image (this is the original).

In rgba_icons.h, the array starts with

Code: Select all

static const unsigned char bookmark14[784] = {
In the converted file it starts with

Code: Select all

static unsigned char bookmark14[] = {
I've added "const". How do I add the No. (e.g. 784) to the output file?
Can you thing of any other reason for the wrong image?

Best regards.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert a PNG image to a C++ Array

Post by snibgo »

"const" just tells the compiler the array won't be changed. It won't affect what the image looks like.

C doesn't need a count between brackets [ and ].

What does your program do with the data? It will work only if your program understands the PNM format. Your original data ...

Code: Select all

static const unsigned char bookmark18[1296] = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5B, 0x77, 0xD0, 0x4B,
0x5A, 0x74, 0xD0, 0xA3, 0x58, 0x72, 0xD0, 0xDB, 0x53, 0x6F, 0xCE, 0xFE,
... seems to be a different format.
snibgo's IM pages: im.snibgo.com
Yaron
Posts: 17
Joined: 2016-09-05T19:02:33-07:00
Authentication code: 1151

Re: Convert a PNG image to a C++ Array

Post by Yaron »

Notepad++ uses the data in ScintillaEditView.cpp:

Code: Select all

execute(SCI_MARKERDEFINERGBAIMAGE, MARK_BOOKMARK, reinterpret_cast<LPARAM>(bookmark14));
So, how do I convert my PNG to the

Code: Select all

static const unsigned char bookmark14[784] = {
format?

Thanks again. I appreciate your help.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert a PNG image to a C++ Array

Post by snibgo »

Okay, so you want to swap out the image data from Notepad++ with your own image, yes?

You'll have to find what format the Notepad++ software expects the data to be in. Maybe it's very simple, like 28*28 = 784 grayscale pixels. If so, then just create your 28x28 image, convert it to something.h, remove the first 13 bytes from something.h, and that's it.

But it might be more complex.
snibgo's IM pages: im.snibgo.com
Yaron
Posts: 17
Joined: 2016-09-05T19:02:33-07:00
Authentication code: 1151

Re: Convert a PNG image to a C++ Array

Post by Yaron »

Okay, so you want to swap out the image data from Notepad++ with your own image, yes?
Correct.
like 28*28 = 784 grayscale pixels.
Do you mean setting my PNG canvas size to 28*28?

Thanks.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Convert a PNG image to a C++ Array

Post by glennrp »

Assuming you have a 28x28 grayscale file.png,

Code: Select all

convert file.png -define h:format=gray -depth 8 -size 28x28  grey28x28.h
Yaron
Posts: 17
Joined: 2016-09-05T19:02:33-07:00
Authentication code: 1151

Re: Convert a PNG image to a C++ Array

Post by Yaron »

Hello glennrp,

Thanks for replying. I appreciate it.
Assuming you have a 28x28 grayscale file.png,
I'm not a pro. :)
My image is 14*14. Should I change the canvas size to 28*28? Or do I completely misunderstand it?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert a PNG image to a C++ Array

Post by snibgo »

I don't know Notepad++. I can only guess. If an image has 784 bytes, with no header, then it might be 28x28 pixels square. But that's only a guess. Perhaps it is 14x14 pixels, with 4 channels per pixel. I don't know.
snibgo's IM pages: im.snibgo.com
Yaron
Posts: 17
Joined: 2016-09-05T19:02:33-07:00
Authentication code: 1151

Re: Convert a PNG image to a C++ Array

Post by Yaron »

The image displayed in Notepad++ is 14*14.
Thanks again.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Convert a PNG image to a C++ Array

Post by glennrp »

Yaron wrote: My image is 14*14. Should I change the canvas size to 28*28? Or do I completely misunderstand it?
OK, then, assuming you have a 14x14 RGBA file.png,

Code: Select all

convert file.png -define h:format=rgba -depth 8 -size 14x14  rgba14x14.h
I don't know what orientation Notepad wants. If your result is upside-down
or right-to-left, add "-flop" and/or "-flip" options. If the colors are swapped around, try
the "-separate" and "-combine" options to put them in the correct order within pixels.
Last edited by glennrp on 2016-09-06T17:24:49-07:00, edited 1 time in total.
Yaron
Posts: 17
Joined: 2016-09-05T19:02:33-07:00
Authentication code: 1151

Re: Convert a PNG image to a C++ Array

Post by Yaron »

Hello glennrp,

That works like a charm! Thank you very much indeed.

Allow me one more question:
Are you sure I can't use a 32bpp PNG for the conversion?
I've installed ImageMagick-7.0.3-0-Q8-x86-dll.exe.

Best regards.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: [Solved] Convert a PNG image to a C++ Array

Post by glennrp »

"-depth 8" means 8 bits per sample, so indeed the command will give you a 32-bit RGBA (8-8-8-8). I should have changed the name of the output file to "rgba14x14.h" though, just to clarify that. I'll do that now.
Yaron
Posts: 17
Joined: 2016-09-05T19:02:33-07:00
Authentication code: 1151

Re: [Solved] Convert a PNG image to a C++ Array

Post by Yaron »

Hello glennrp,

Thanks again for your kind help. I do appreciate it.

Best regards.
Post Reply