Incorrect result by reading pixels in magick++

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
haggi

Incorrect result by reading pixels in magick++

Post by haggi »

Hi.

I'm quite new to Magic++. I try to create some geometry depending on a color/luminance value of an image file at a certain position.

To test my code I simply built a loop to read out pixels at a certain uv position.
My testimage is a simple checker image with 10 tiles (5 black 5 white) in one row and 10 rows.
The image is 1000 pixels in square. This means the first black square goes from 0,0 ot 100,100.

Okay if I now check the pixel color for 19,59 I get a full white what is definitly not correct.
At position 20, 60 I get the correct value of black. Again at 21, 61 the result is wrong.

I suppose my code is somehow wrong. Maybe anyone can give me a hint? Thanks a lot.


Code: Select all

#include <Magick++.h>
#include <string>
#include <iostream>

using namespace std;

using namespace Magick;

int main( int /*argc*/, char ** argv)
{

  // Initialize ImageMagick install location for Windows
  InitializeMagick(*argv);

  try {

	Image image("C:/daten/Test/sourceimages/checker.tif");
	// Ensure that there are no other references to this image.
	image.modifyImage();
	// Set the image type to TrueColor DirectClass representation.
	image.type(TrueColorType);
	
	int rows = image.rows() - 1;
	int columns = image.columns() - 1;
	fprintf(stdout, "col %d row %d\n", image.rows(), image.columns());
	const MagickLib::PixelPacket *pixel_cache = image.getConstPixels(0,0,columns,rows);

	int column = columns / 2;
	int row = rows / 2;
	int x = 19;
	int y = 59;
	const MagickLib::PixelPacket *pixel = pixel_cache+x*y+x;
	float value =  (pixel->red + pixel->green + pixel->blue) / 3.0 / 65535;
	fprintf(stdout, "Final Value: %f at coord: %d %d red: %d green: %d, blue: %d \n", value, x, y, pixel->red, pixel->green, pixel->blue );
	x = 20;
	y = 60;
	pixel = pixel_cache+x*y+x;
	value =  (pixel->red + pixel->green + pixel->blue) / 3.0 / 65535;
	fprintf(stdout, "Final Value: %f at coord: %d %d red: %d green: %d, blue: %d \n", value, x, y, pixel->red, pixel->green, pixel->blue );

  }
  catch( exception &error_ )
    {
      cout << "Caught exception: " << error_.what() << endl;
      return 1;
    }

  return 0;
}
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Incorrect result by reading pixels in magick++

Post by magick »

It's recommended to get a few scanlines at a time or a few pixels at a time rather than the whole image.

You did not compute the pixel offset correctly. Instead, use
  • const MagickLib::PixelPacket *pixel = pixel_cache+columns*y+x;
haggi

Re: Incorrect result by reading pixels in magick++

Post by haggi »

Thanks, of course you are right, stupid me.

I thought it would be useful to read the image once and then access pixels.
There can be about 1 million requests during the calculation of my tool. And the access will be in very different parts of the image. I thought it could save a lot of overhead if I read the whole image once insted of reading only a small part a lot of times.
Post Reply