Page 1 of 2

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

Posted: 2016-09-05T19:09:19-07:00
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.

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

Posted: 2016-09-05T21:28:10-07:00
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.

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

Posted: 2016-09-06T04:49:02-07:00
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.

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

Posted: 2016-09-06T05:16:47-07:00
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.

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

Posted: 2016-09-06T07:42:17-07:00
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.

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

Posted: 2016-09-06T08:04:11-07:00
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.

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

Posted: 2016-09-06T08:13:09-07:00
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.

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

Posted: 2016-09-06T08:19:59-07:00
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

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

Posted: 2016-09-06T08:35:51-07:00
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?

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

Posted: 2016-09-06T08:56:58-07:00
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.

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

Posted: 2016-09-06T09:03:41-07:00
by Yaron
The image displayed in Notepad++ is 14*14.
Thanks again.

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

Posted: 2016-09-06T11:07:06-07:00
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.

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

Posted: 2016-09-06T12:13:38-07:00
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.

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

Posted: 2016-09-06T17:23:55-07:00
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.

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

Posted: 2016-09-06T17:42:33-07:00
by Yaron
Hello glennrp,

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

Best regards.