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

Convert a cursor to C++ Array

Post by Yaron »

Hello,

The following code is a cursor converted C++ Array.

Code: Select all

static BYTE XORMask[128] =
{
  0xff, 0xff, 0xff, 0xff,
  0xf9, 0xff, 0xff, 0xff,
  0xf0, 0xff, 0xff, 0xff,
  0xf0, 0xff, 0xff, 0xff,
  0xf0, 0xff, 0xff, 0xff,
  0xf0, 0xff, 0xff, 0xff,
  0xf0, 0x24, 0xff, 0xff,
  0xf0, 0x00, 0x7f, 0xff,
  0xc0, 0x00, 0x7f, 0xff,
  0x80, 0x00, 0x7f, 0xff,
  0x80, 0x00, 0x7f, 0xff,
  0x80, 0x00, 0x7f, 0xff,
  0x80, 0x00, 0x7f, 0xff,
  0x80, 0x00, 0x7f, 0xff,
  0xc0, 0x00, 0x7f, 0xff,
  0xe0, 0x00, 0x7f, 0xff,
  0xf0, 0x00, 0xff, 0xff,
  0xf0, 0x00, 0xff, 0xff,
  0xf0, 0x00, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff,
};

/* AND mask for hand cursor */
/* Generated by HexEdit */
static BYTE ANDMask[128] =
{
  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
  0x06, 0x00, 0x00, 0x00,
  0x06, 0x00, 0x00, 0x00,
  0x06, 0x00, 0x00, 0x00,
  0x06, 0x00, 0x00, 0x00,
  0x06, 0x00, 0x00, 0x00,
  0x06, 0xdb, 0x00, 0x00,
  0x06, 0xdb, 0x00, 0x00,
  0x36, 0xdb, 0x00, 0x00,
  0x36, 0xdb, 0x00, 0x00,
  0x37, 0xff, 0x00, 0x00,
  0x3f, 0xff, 0x00, 0x00,
  0x3f, 0xff, 0x00, 0x00,
  0x1f, 0xff, 0x00, 0x00,
  0x0f, 0xff, 0x00, 0x00,
  0x07, 0xfe, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
};
Can I use ImageMagick for that conversion?

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

Re: Convert a cursor to C++ Array

Post by snibgo »

Suppose your cursor is in a file named cursor.png. Then:

Code: Select all

magick cursor.png out.h
out.h will be something like:

Code: Select all

/*
  out.h (PNM).
*/
static const unsigned char
  MagickImage[] =
  {
    0x50, 0x35, 0x0A, 0x33, 0x20, 0x33, 0x0A, 0x32, 0x35, 0x35, 0x0A, 0xB9, 
    0xB9, 0xB9, 0xB9, 0xFF, 0xB9, 0xB9, 0xB9, 0xB9, 
  };

This is PNM format. The pixels start after the third newline (0x0A). If you want 16 bits per channel, use "-depth 16". If you want colour, ie three values per pixel, use "-type truecolor".

Note that the defaults seem to have changed between IM versions 6 and 7, so it may be better to always specify the depth and type you need.
snibgo's IM pages: im.snibgo.com
Yaron
Posts: 17
Joined: 2016-09-05T19:02:33-07:00
Authentication code: 1151

Re: Convert a cursor to C++ Array

Post by Yaron »

Thanks for replying. I appreciate it.

I've tried various combination but none works properly. I get a corrupt image.

I have 2 arrays:

Code: Select all

static BYTE XORMask[128]
and

Code: Select all

static BYTE ANDMask[128]
I have a .cur file. I suppose I should convert it to XOR Mask and AND Mask.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert a cursor to C++ Array

Post by snibgo »

IM can read Windows cursor formats. (But there may be other ".cur" formats.) For example:

Code: Select all

f:\web\im>%IM%identify up_m.cur

up_m.cur[0] CUR 48x48 48x48+0+0 1-bit sRGB 1.6KB 0.000u 0:00.000
up_m.cur[1] CUR 32x32 32x32+0+0 4-bit sRGB 1.6KB 0.000u 0:00.016
IM sees two images in the file. I suppose these are the "and" and "xor" masks. Converting them gives two ".h" files.
snibgo's IM pages: im.snibgo.com
Yaron
Posts: 17
Joined: 2016-09-05T19:02:33-07:00
Authentication code: 1151

Re: Convert a cursor to C++ Array

Post by Yaron »

Those 2 arrays are sizes.

If you take aero_link.cur from C:\Windows\Cursors, you get:

Code: Select all

aero_link.cur[0] CUR 48x48 48x48+0+0 8-bit sRGB 13942B 0.016u 0:00.014
aero_link.cur[1] CUR 32x32 32x32+0+0 8-bit sRGB 13942B 0.000u 0:00.000
but if you delete the 48x48, you get:

Code: Select all

aero_link.cur CUR 32x32 32x32+0+0 8-bit sRGB 4286B 0.000u 0:00.000
Anyway, the question is how you convert them.

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

Re: Convert a cursor to C++ Array

Post by snibgo »

On my Windows 8.1, that file gives me three images:

Code: Select all

f:\web\im>%IMG7%magick C:\Windows\Cursors\aero_link.cur info:

C:\Windows\Cursors\aero_link.cur[0] CUR 64x64 64x64+0+0 8-bit sRGB 30894B 0.016u 0:00.047
C:\Windows\Cursors\aero_link.cur[1] CUR 48x48 48x48+0+0 8-bit sRGB 30894B 0.000u 0:00.046
C:\Windows\Cursors\aero_link.cur[2] CUR 32x32 32x32+0+0 8-bit sRGB 30894B 0.000u 0:00.062
We can convert them all to PNG images:

Code: Select all

magick C:\Windows\Cursors\aero_link.cur aero_cur_%d.png
This is a hand with pointing finger, on a transparent background, at three different sizes. Any or all of these could be converted to .h files.
snibgo's IM pages: im.snibgo.com
Yaron
Posts: 17
Joined: 2016-09-05T19:02:33-07:00
Authentication code: 1151

Re: Convert a cursor to C++ Array

Post by Yaron »

Any or all of these could be converted to .h files.
I've tried converting to PNM and RGBA formats. I get a proper .h file but it doesn't work in my code.

Thanks.
Post Reply