Page 1 of 1

Accessing Pixel

Posted: 2011-11-29T10:16:50-07:00
by rohitkrishna
Hi everyone,

Is there any way with which i can directly access a pixel at a given x,y position in magickwand and change the color of that particular pixel to red.

Re: Accessing Pixel

Posted: 2011-11-29T13:00:19-07:00
by el_supremo
This code reads the built-in logo: image and changes the pixel at 200,100 to red.

[EDIT] I've modified the code to use Region Iterator, as in my original version, or to use a DrawingWand as suggested by Anthony.

Pete

Code: Select all

// Change the colour of one pixel in the logo: image
// using either DrawPoint or a RegionIterator

#include <windows.h>
#include <wand/magick_wand.h>

void test_wand(void)
{
	MagickWand *mw = NULL;
// Comment out this define to use the region iterator instead of the Draw
#define USE_DRAW
#ifndef USE_DRAW
	PixelIterator *iterator = NULL;
	PixelWand **pixels = NULL;
	size_t x;
#else
	DrawingWand *dw = NULL;
	PixelWand *fill = NULL;
#endif
	MagickWandGenesis();

	/* Create a wand */
	mw = NewMagickWand();

	/* Read the input image */
	MagickReadImage(mw,"logo:");
#ifndef USE_DRAW
	// Get a one-pixel region at coordinate 200,100
	iterator = NewPixelRegionIterator(mw,200,100,1,1);
	pixels=PixelGetNextIteratorRow(iterator,&x);
	// Modify the pixel
	PixelSetColor(pixels[0],"red");
	// then sync it back into the wand
	PixelSyncIterator(iterator);
#else
	fill = NewPixelWand();
	dw = NewDrawingWand();
	PixelSetColor(fill,"red");
    DrawSetFillColor(dw,fill);
	// Uses the current Fill as the colour of the point
	DrawPoint(dw,200,100);
    MagickDrawImage(mw,dw);

#endif
	/* write it */
	MagickWriteImage(mw,"logo_pix.gif");

	/* Tidy up */
#ifndef USE_DRAW
	iterator=DestroyPixelIterator(iterator);
#else
	if(dw)dw = DestroyDrawingWand(dw);
	if(fill)fill = DestroyPixelWand(fill);
#endif
	if(mw) mw = DestroyMagickWand(mw);

	MagickWandTerminus();
}

Re: Accessing Pixel

Posted: 2011-11-29T16:28:15-07:00
by rohitkrishna
Thank you very much el_supremo
el_supremo wrote:This code reads the built-in logo: image and changes the pixel at 200,100 to red.

[EDIT] I've modified the code to use Region Iterator, as in my original version, or to use a DrawingWand as suggested by Anthony.

Pete

Code: Select all

// Change the colour of one pixel in the logo: image
// using either DrawPoint or a RegionIterator

#include <windows.h>
#include <wand/magick_wand.h>

void test_wand(void)
{
	MagickWand *mw = NULL;
// Comment out this define to use the region iterator instead of the Draw
#define USE_DRAW
#ifndef USE_DRAW
	PixelIterator *iterator = NULL;
	PixelWand **pixels = NULL;
	size_t x;
#else
	DrawingWand *dw = NULL;
	PixelWand *fill = NULL;
#endif
	MagickWandGenesis();

	/* Create a wand */
	mw = NewMagickWand();

	/* Read the input image */
	MagickReadImage(mw,"logo:");
#ifndef USE_DRAW
	// Get a one-pixel region at coordinate 200,100
	iterator = NewPixelRegionIterator(mw,200,100,1,1);
	pixels=PixelGetNextIteratorRow(iterator,&x);
	// Modify the pixel
	PixelSetColor(pixels[0],"red");
	// then sync it back into the wand
	PixelSyncIterator(iterator);
#else
	fill = NewPixelWand();
	dw = NewDrawingWand();
	PixelSetColor(fill,"red");
    DrawSetFillColor(dw,fill);
	// Uses the current Fill as the colour of the point
	DrawPoint(dw,200,100);
    MagickDrawImage(mw,dw);

#endif
	/* write it */
	MagickWriteImage(mw,"logo_pix.gif");

	/* Tidy up */
#ifndef USE_DRAW
	iterator=DestroyPixelIterator(iterator);
#else
	if(dw)dw = DestroyDrawingWand(dw);
	if(fill)fill = DestroyPixelWand(fill);
#endif
	if(mw) mw = DestroyMagickWand(mw);

	MagickWandTerminus();
}

Re: Accessing Pixel

Posted: 2011-11-30T20:50:37-07:00
by anthony
Thank you very much el_supremo
I second the motion!

I did not really want to go though all the effort of working out the surrounding 'fluff'. It makes for a good example.

Re: Accessing Pixel

Posted: 2011-12-01T07:53:55-07:00
by el_supremo
I'm glad it helped. I'll add it to my MagickWand examples page later today.

Pete

Re: Accessing Pixel

Posted: 2011-12-01T23:50:35-07:00
by anthony
Perhaps it should be included in IM sources as a basic example demo.

Re: Accessing Pixel

Posted: 2011-12-03T12:58:23-07:00
by rohitkrishna
anthony wrote:Perhaps it should be included in IM sources as a basic example demo.
I think you should....it definetly helps a lot of people...

Re: Accessing Pixel

Posted: 2011-12-03T13:15:25-07:00
by rohitkrishna
el_supremo wrote:This code reads the built-in logo: image and changes the pixel at 200,100 to red.

[EDIT] I've modified the code to use Region Iterator, as in my original version, or to use a DrawingWand as suggested by Anthony.

Pete

Code: Select all

// Change the colour of one pixel in the logo: image
// using either DrawPoint or a RegionIterator

#include <windows.h>
#include <wand/magick_wand.h>

void test_wand(void)
{
	MagickWand *mw = NULL;
// Comment out this define to use the region iterator instead of the Draw
#define USE_DRAW
#ifndef USE_DRAW
	PixelIterator *iterator = NULL;
	PixelWand **pixels = NULL;
	size_t x;
#else
	DrawingWand *dw = NULL;
	PixelWand *fill = NULL;
#endif
	MagickWandGenesis();

	/* Create a wand */
	mw = NewMagickWand();

	/* Read the input image */
	MagickReadImage(mw,"logo:");
#ifndef USE_DRAW
	// Get a one-pixel region at coordinate 200,100
	iterator = NewPixelRegionIterator(mw,200,100,1,1);
	pixels=PixelGetNextIteratorRow(iterator,&x);
	// Modify the pixel
	PixelSetColor(pixels[0],"red");
	// then sync it back into the wand
	PixelSyncIterator(iterator);
#else
	fill = NewPixelWand();
	dw = NewDrawingWand();
	PixelSetColor(fill,"red");
    DrawSetFillColor(dw,fill);
	// Uses the current Fill as the colour of the point
	DrawPoint(dw,200,100);
    MagickDrawImage(mw,dw);

#endif
	/* write it */
	MagickWriteImage(mw,"logo_pix.gif");

	/* Tidy up */
#ifndef USE_DRAW
	iterator=DestroyPixelIterator(iterator);
#else
	if(dw)dw = DestroyDrawingWand(dw);
	if(fill)fill = DestroyPixelWand(fill);
#endif
	if(mw) mw = DestroyMagickWand(mw);

	MagickWandTerminus();
}

I got a doubt...what is value of x ... because in region iterator we generally give the region width as second argument.

Re: Accessing Pixel

Posted: 2011-12-03T14:04:19-07:00
by el_supremo
PixelGetNextIteratorRow sets that variable to the number of pixelwands that it returns. In this case I only asked for one pixel and it's in the middle of the image so it can't return zero.
In the general case though you would have to check the value of x after calling PixelGetNextIteratorRow to make sure that it has the number of wands that you are expecting.

Pete
P.S. I've added the example to my MagickWand examples (see the link below)

Re: Accessing Pixel

Posted: 2011-12-06T18:26:29-07:00
by rohitkrishna
el_supremo wrote:PixelGetNextIteratorRow sets that variable to the number of pixelwands that it returns. In this case I only asked for one pixel and it's in the middle of the image so it can't return zero.
In the general case though you would have to check the value of x after calling PixelGetNextIteratorRow to make sure that it has the number of wands that you are expecting.

Pete
P.S. I've added the example to my MagickWand examples (see the link below)
Thank you...