Does the Image constructor copy or reference my pixel 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
User avatar
jonoomph
Posts: 27
Joined: 2011-03-27T14:25:09-07:00
Authentication code: 8675308

Does the Image constructor copy or reference my pixel array?

Post by jonoomph »

Greetings! When using Magick++, I am passing in my array of pixels to the Image constructor. It works fine, except I noticed that the memory footprint of my program doubles when I do this. Is it possible to pass in an array without ImageMagick doubling the memory of my program? I imagine the Image constructor is somehow making a copy of my array. It would be great if it could just use my array pointer, instead of making a copy. Any thoughts of suggestions?

EXAMPLE:
// Create an Image from a C++ pixel array
Image my_image(width, height, "RGB", CharPixel, my_array);

Thanks!
-Jonathan
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Does the Image constructor copy or reference my pixel ar

Post by magick »

A couple of users have asked for this feature-- it does not exist just yet. Magick++ copies the pixels to prevent inadvertent memory faults in the event the user deletes the pixels while Magick++ is operating on them. Another problem is that the user supplied pixel array may not match the schema required by ImageMagick. For example, the default ImageMagick installation is 16-bits per pixel, whereas the user supplied array is typically 8-bits. The default ImageMagick schema is RGBO whereas the the typical user supplied array may be RGBA or perhaps AGBR. Where O is 255-A (or 65535-A for 16-bit color components).
User avatar
jonoomph
Posts: 27
Joined: 2011-03-27T14:25:09-07:00
Authentication code: 8675308

Re: Does the Image constructor copy or reference my pixel ar

Post by jonoomph »

Thanks for the info! I am processing frames of video from FFmpeg just as fast as I can, and was hoping I could pass the pixel array of each frame into ImageMagick, process the image in some way, and then return the pixel array to my program to either be sent to SDL (i.e. to the screen) or back to FFmpeg to be re-encoded. Everything works fine, but it's pretty slow. The Image constructor seems to be the big slow down, but I'm not sure what can be done about it (if anything). I thought maybe it was due to the array being copied, but that was just my first thought.

Is this the most efficient way to pass a pixel array to ImageMagick?
Image my_image(width, height, "RGB", CharPixel, my_array);

Also, it's worth mentioning that the pixel array contains a 1920 x 1080 sized image, which is pretty big, and maybe it's not realistic to process 30 of those a second, etc... I am going to try resizing the image down to a much smaller size, like 640x480, or maybe even smaller and see how that affects the performance of ImageMagick. =)

Thanks again!
-Jonathan
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Does the Image constructor copy or reference my pixel ar

Post by anthony »

Is ther a way of accessing the actual pixel array? EG just get pixel X,Y?

Of course that would not be 'thread compliment' as another thread could be changing the image while you are reading it!
But then perhaps you can lock the whole image to the current thread.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Does the Image constructor copy or reference my pixel ar

Post by anthony »

jonoomph wrote:Also, it's worth mentioning that the pixel array contains a 1920 x 1080 sized image, which is pretty big, and maybe it's not realistic to process 30 of those a second, etc... I am going to try resizing the image down to a much smaller size, like 640x480, or maybe even smaller and see how that affects the performance of ImageMagick
You could also compile a specialised 8-bit ImageMagick for your needs. That in itself will speed up memory performance.

HOWEVER: many video formats use a bit depth of 10, rather than 8 ot 16. Weird but true.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply