Modify opacity channel via low level pixel access in C++

Magick++ is an object-oriented C++ interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick++.
Post Reply
drunkensapo
Posts: 23
Joined: 2013-08-14T15:40:56-07:00
Authentication code: 6789

Modify opacity channel via low level pixel access in C++

Post by drunkensapo » 2015-06-16T10:30:12-07:00

Dear all,
In a previous post I tried many ways to modify locally (i.e. different values for every pixel) the alpha channel of an image. See the original post here: viewtopic.php?t=23915
I seem to be hitting the same problem as my original post in point 2)
However using SetPixelColor is really slow and im programming a very time consuming filter. I managed to accelerate my code accessing directly the pixel array. However this seems to work for color channels but not for opacity. For example, if I try to multiply the opacity by a certain value in the range [0,1], opacity gets unchanged.
however, using magick's quantumOperator it works perfectly. See second example.
Thanks for your time.

Code: Select all

void quantumOperator(Image_t *img, double alpha)
{

	Geometry size=img->size();
	int h = size.height();
	int w = size.width();
	xml_out("w: %d h: %d\n",w,h);

	int byteIndex;
	int channels=1;
	int bytesPerPixel=channels;
	int bytesPerRow = bytesPerPixel * w;
	//printf("bytesPerPixel: %d depth: %zd\n",bytesPerPixel,img->depth());

	img->modifyImage();
	Pixels imgview(*img);
	PixelPacket *imgpix = imgview.get(0,0,w,h);

	printf("alpha: %g\n", alpha);
	for(int yy=0; yy<h; yy++)
	{
		for(int xx=0;xx<w;xx++)
		{
			byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel;

			double op=imgpix->opacity;
			if(xx==w/2) printf("op before: %g\n", op);
			//op=(1.0-alpha)*QuantumRange+op*alpha;
			op=op*alpha;
			imgpix->opacity=(Quantum)rint(op);
			if(xx==w/2) printf("op after: %g\n", op);
			imgpix++;

		}
	}

	imgview.sync();
	img->syncPixels();
}

Code: Select all

	//this works
	mask.modifyImage();
	mask.quantumOperator(Magick::OpacityChannel, Magick::MultiplyEvaluateOperator, m_alpha);
	//this does not work
	quantumOperator(&mask,m_alpha);

Post Reply