Read/Write portion of image from/to file

Magick++ is an object-oriented C++ interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick++.
Post Reply
jsanterre
Posts: 31
Joined: 2014-05-09T10:39:21-07:00
Authentication code: 6789

Read/Write portion of image from/to file

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

jsanterre
Posts: 31
Joined: 2014-05-09T10:39:21-07:00
Authentication code: 6789

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

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

Yes, I understand, but do not know Magick++. Perhaps some one who uses Magick++ can advise you on that.
jsanterre
Posts: 31
Joined: 2014-05-09T10:39:21-07:00
Authentication code: 6789

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

Post 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
jsanterre
Posts: 31
Joined: 2014-05-09T10:39:21-07:00
Authentication code: 6789

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

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
jsanterre
Posts: 31
Joined: 2014-05-09T10:39:21-07:00
Authentication code: 6789

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

Post by jsanterre »

Thanks to both of you! Your answers helped me a lot!

Julie
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

Let us know what you finally did that worked for you.
jsanterre
Posts: 31
Joined: 2014-05-09T10:39:21-07:00
Authentication code: 6789

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

Post 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
jsanterre
Posts: 31
Joined: 2014-05-09T10:39:21-07:00
Authentication code: 6789

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

Post 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
Post Reply