pixelColor() is slow
Posted: 2008-07-02T16:19:25-07:00
I've noticed that the first call to Magick::Image::pixelColor(...) is painfully slow.
Image i(Geometry(12000, 10000), Color(0, QuantumRange, 0)); // allocate image = slow
i.pixelColor(1, 1, Color(QuantumRange, 0, 0)); // very, very slow
i.pixelColor(9000, 7000, Color(QuantumRange, 0, 0)); // fast
To be fair, I am working with a large images. The first call must be creating some kind of pixel cache. I see no reason for this. Worse, the pixel cache must overflow memory because this allocation takes far longer than the original.
Interestingly, the following achieves the same result very quickly.
Image i(Geometry(12000, 10000), Color(0, QuantumRange, 0)); // create image = slow
PixelPacket * i_cache = i.getPixels(0, 0, i.columns(), i.rows()); // all pixels, fast
*(i_cache + 1 * i.rows() + 1) = Color(QuantumRange, 0, 0); // fast
*(i_cache + 7000 * i.rows() + 9000) = Color(QuantumRange, 0, 0); // fast
i.syncPixels(); // fast
No question here as I found my solution, just an observation that may help others.
ImageMagick 6.4.2
Magick++
Windows XP
-Chris
Image i(Geometry(12000, 10000), Color(0, QuantumRange, 0)); // allocate image = slow
i.pixelColor(1, 1, Color(QuantumRange, 0, 0)); // very, very slow
i.pixelColor(9000, 7000, Color(QuantumRange, 0, 0)); // fast
To be fair, I am working with a large images. The first call must be creating some kind of pixel cache. I see no reason for this. Worse, the pixel cache must overflow memory because this allocation takes far longer than the original.
Interestingly, the following achieves the same result very quickly.
Image i(Geometry(12000, 10000), Color(0, QuantumRange, 0)); // create image = slow
PixelPacket * i_cache = i.getPixels(0, 0, i.columns(), i.rows()); // all pixels, fast
*(i_cache + 1 * i.rows() + 1) = Color(QuantumRange, 0, 0); // fast
*(i_cache + 7000 * i.rows() + 9000) = Color(QuantumRange, 0, 0); // fast
i.syncPixels(); // fast
No question here as I found my solution, just an observation that may help others.
ImageMagick 6.4.2
Magick++
Windows XP
-Chris