Page 1 of 1

Read/Write portion of image from/to file

Posted: 2014-12-01T12:04:37-07:00
by jsanterre
Hi everyone!

I need to process really big images in C++. So I would like to read only a portion of an image from file using Magick++ API, process it, and write that portion back to file. I would process each chunk in turn until the entire image has been processed.

Is there a way to read and write only portions of file using Magick++ API?

Many thanks!

Julie

Re: Read/Write portion of image from/to file

Posted: 2014-12-01T13:05:07-07:00
by fmw42

Re: Read/Write portion of image from/to file

Posted: 2014-12-01T13:45:40-07:00
by jsanterre
Thank you very much for your answer. It was very interesting.

However, I don't think this approach would work for me. The problem is that I'm developing a software written in C++ and I can't assume the users of my software will have imagemagick installed on their own machine... That's why I would need a way to do the exact same thing that is described in those links but using Magick++.

Thank you anyway!

Julie

Re: Read/Write portion of image from/to file

Posted: 2014-12-01T13:58:06-07:00
by fmw42
Yes, I understand, but do not know Magick++. Perhaps some one who uses Magick++ can advise you on that.

Re: Read/Write portion of image from/to file

Posted: 2014-12-02T08:18:41-07:00
by jsanterre
Thank you very much. :)

So I tried to make this work with the Image Cache Methods (see the docs at http://www.imagemagick.org/Magick++/Ima ... l%20Access) but did not succeed yet. In particular, I have been trying to use the functions setPixels() and readPixels(). If I understand things correctly, after a call to setPixels(), I should be able to read only a portion of a file using readPixels(). That function takes "unsigned char *source_" as an argument. What exactly should be that source if I want to read directly from file (and not from a buffer)?

Many thanks for your help!

Julie

Re: Read/Write portion of image from/to file

Posted: 2014-12-02T10:53:05-07:00
by jsanterre
Here's what we are planning to do:

Read the entire big image using Image img = read( filename ) and then have access to the desired portion of the file using img.getConstPixels(). (The way we understand things is that ImageMagick itself should take care of swapping to disk when it's out of memory.)

Julie

Re: Read/Write portion of image from/to file

Posted: 2014-12-02T11:50:19-07:00
by snibgo
I use MagickCore, the lower-level interface. As I understand it, IM reads the entire image into memory, swapping it as required. Then I access whatever rectangles I want. Typically, a rectangle is one entire line of the image. IM swaps data to and from disk as required, but I try to avoid swapping.

The exception is large MPC files. These are memory-mapped, so IM directly accesses the disk for each rectangle, so there is no swapping, and everything is much faster. When I am playing with large images (greater than about 1 G pixels), I first convert to an MPC file.

Re: Read/Write portion of image from/to file

Posted: 2014-12-02T13:49:06-07:00
by fmw42
I do not know if this will help, but see http://www.imagemagick.org/script/archi ... php#stream

see also Best Practices under this topic http://www.imagemagick.org/script/archi ... .php#cache for notes about MPC format and IM environment variables for -limit

Re: Read/Write portion of image from/to file

Posted: 2014-12-03T11:56:29-07:00
by jsanterre
Thanks to both of you! Your answers helped me a lot!

Julie

Re: Read/Write portion of image from/to file

Posted: 2014-12-03T12:54:56-07:00
by fmw42
Let us know what you finally did that worked for you.

Re: Read/Write portion of image from/to file

Posted: 2014-12-03T14:03:39-07:00
by jsanterre
For now, we are planning on letting IM handle the swapping. Then, if we have performance issue, we will look into more fancy stuff.

I will keep you posted!

Again, thanks for your help!

Julie

Re: Read/Write portion of image from/to file

Posted: 2015-05-08T10:20:23-07:00
by jsanterre
Hi there,

Here are news about how things are going... As described above, we decided to let IM handle the swapping. We had a round of beta releases and now, the product is finally out. It seems that all of our users are happy with the way IM handle big images!

Thanks!

Julie