Page 1 of 1

Getting a pixel of an image

Posted: 2017-03-31T18:46:54-07:00
by Airon65
I want to get some pixels from my image and print it out to the screen. My attempt is like this:

Code: Select all

const MagickCore::Quantum *pixelsArr = image1->getConstPixels( 0, 0, 800, 600 );
unsigned char r = pixelsArr[ 0 ].red * 255 / QuantumRange;
unsigned char g = pixelsArr[ 0 ].greenQuantum * 255 / QuantumRange;
but I get error:

Code: Select all

first.cpp:726:38: error: member reference base type 'const MagickCore::Quantum' (aka 'const float') is not a structure or union
                                        unsigned char r = pixelsArr[ i ].red * 255 / QuantumRange;
                                                          ~~~~~~~~~~~~~~^~~~
first.cpp:727:38: error: member reference base type 'const MagickCore::Quantum' (aka 'const float') is not a structure or union
                                        unsigned char g = pixelsArr[ i ].redQuantum * 255 / QuantumRange;
How to convert this float (which called Quantum) to R/G/B components?

IM: is up to date, OS X 10.12.3.

Re: Getting a pixel of an image

Posted: 2017-03-31T18:52:30-07:00
by Airon65
I've tried to read some docs on what's the Quantum is but the link to Quantum docs: http://www.imagemagick.org/Magick++/Quantum.html is broken.

Re: Getting a pixel of an image

Posted: 2017-03-31T19:04:40-07:00
by snibgo
"Quantum" is a scalar type, a simple number with no elements. Depending on your build, it is defined as an int, float, double or whatever. From your error message, it is a float.

Re: Getting a pixel of an image

Posted: 2017-03-31T19:06:27-07:00
by Airon65
Is the RGB order just goes like this in my pixelsArr?

Code: Select all

R G B R G B R G B R G B....
and every R, G, B is of a quantum type

Re: Getting a pixel of an image

Posted: 2017-03-31T19:14:22-07:00
by Airon65
In other words for RGB image I should go this way:

Code: Select all

const MagickCore::Quantum *pixelsArr = image->getConstPixels( 0, 0, _imgWidth, _imgHeight );
for ( unsigned int i = 0; i < _imgWidth * _imgHeight * 3; i += 3 ) {

	for ( unsigned int i1 = 0; i1 < 2; i1++ ) {
	
		unsigned char r = pixelsArr[ i + 0 ] * 255 / QuantumRange;
		unsigned char g = pixelsArr[ i + 1 ] * 255 / QuantumRange;
		unsigned char b = pixelsArr[ i + 2 ] * 255 / QuantumRange;

	}

}
Am I right or not? :)

Re: Getting a pixel of an image

Posted: 2017-03-31T19:15:26-07:00
by snibgo
getConstPixels() is declared in Image.h. It returns "PixelPacket *", which is not the same thing as "Quantum *".

Re: Getting a pixel of an image

Posted: 2017-03-31T19:34:03-07:00
by Airon65
snibgo wrote: 2017-03-31T19:15:26-07:00 getConstPixels() is declared in Image.h. It returns "PixelPacket *", which is not the same thing as "Quantum *".
It doesn't look that it's the same thing:

Code: Select all

const MagickCore::PixelPacket *pixelsArr = image->getConstPixels( 0, 0, _imageWidth, _imageHeight );

Code: Select all

first.cpp:723:36: error: cannot initialize a variable of type 'const MagickCore::PixelPacket *' (aka 'const MagickCore::_PixelPacket *') with an rvalue of type
      'const Quantum *' (aka 'const float *')
                                const MagickCore::PixelPacket *pixelsArr = image->getConstPixels( 0, 0, _imageWidth, _imageHeight );
                                                               ^           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
What's wrong with my code?

Code: Select all

$ magick -version
Version: ImageMagick 7.0.5-2 Q16 x86_64 2017-03-11 http://www.imagemagick.org
Copyright: © 1999-2017 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules 
Delegates (built-in): bzlib freetype jng jpeg ltdl lzma png tiff xml zlib

Re: Getting a pixel of an image

Posted: 2017-04-01T04:27:38-07:00
by snibgo
Ah, you are using v7? getConstPixels() changed from v6. In v6, it returns PixelPacket*. In v7, it returns Quantum*.

When asking questions, please always state the version of IM you are using.

Sorry, I don't use Magick++ or MacickWand in v7. But you can see how getConstPixels is used, by an example in Image.cpp. You can call GetPixelInfoPixel to convert the returned Quantum* to an object of type PixelInfo.

The type PixelInfo is defined in MagickCore\pixel.h. This has element red, green, blue and many others.

Re: Getting a pixel of an image

Posted: 2017-04-01T09:33:22-07:00
by Airon65
Instead of that I use this approach:

Code: Select all

for ( unsigned int j = 0; j < _height; j++ ) {
	for ( unsigned int i = 0; i < _width; i++ ) {
	
		Magick::Color c = image->pixelColor( i, j );
		unsigned char r = c.quantumRed() * 255 / QuantumRange;
		unsigned char g = c.quantumGreen() * 255 / QuantumRange;
		unsigned char b = c.quantumBlue() * 255 / QuantumRange;
		
	}	
}
it's very usable way of getting a pixel color for me!