[Solved] Convert a PNG image to a C++ Array
[Solved] Convert a PNG image to a C++ Array
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.
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.
-
- 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
Use the following command:
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.
Code: Select all
convert yourping.png out.h
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
Re: Convert a PNG image to a C++ Array
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
In the converted file it starts with
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.
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] = {
Code: Select all
static unsigned char bookmark14[] = {
Can you thing of any other reason for the wrong image?
Best regards.
-
- 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
"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 ...
... seems to be a different format.
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,
snibgo's IM pages: im.snibgo.com
Re: Convert a PNG image to a C++ Array
Notepad++ uses the data in ScintillaEditView.cpp:
So, how do I convert my PNG to the format?
Thanks again. I appreciate your help.
Code: Select all
execute(SCI_MARKERDEFINERGBAIMAGE, MARK_BOOKMARK, reinterpret_cast<LPARAM>(bookmark14));
Code: Select all
static const unsigned char bookmark14[784] = {
Thanks again. I appreciate your help.
-
- 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
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.
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
Re: Convert a PNG image to a C++ Array
Correct.Okay, so you want to swap out the image data from Notepad++ with your own image, yes?
Do you mean setting my PNG canvas size to 28*28?like 28*28 = 784 grayscale pixels.
Thanks.
Re: Convert a PNG image to a C++ Array
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
Hello glennrp,
Thanks for replying. I appreciate it.
My image is 14*14. Should I change the canvas size to 28*28? Or do I completely misunderstand it?
Thanks for replying. I appreciate it.
I'm not a pro.Assuming you have a 28x28 grayscale file.png,
My image is 14*14. Should I change the canvas size to 28*28? Or do I completely misunderstand it?
-
- 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
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
Re: Convert a PNG image to a C++ Array
The image displayed in Notepad++ is 14*14.
Thanks again.
Thanks again.
Re: Convert a PNG image to a C++ Array
OK, then, assuming you have a 14x14 RGBA file.png,Yaron wrote: My image is 14*14. Should I change the canvas size to 28*28? Or do I completely misunderstand it?
Code: Select all
convert file.png -define h:format=rgba -depth 8 -size 14x14 rgba14x14.h
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.
Re: Convert a PNG image to a C++ Array
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.
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
"-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
Hello glennrp,
Thanks again for your kind help. I do appreciate it.
Best regards.
Thanks again for your kind help. I do appreciate it.
Best regards.