Accessing Pixel
-
- Posts: 43
- Joined: 2011-10-01T14:34:04-07:00
- Authentication code: 8675308
Accessing Pixel
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.
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.
-
- Posts: 1015
- Joined: 2005-03-21T21:16:57-07:00
Re: Accessing Pixel
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
[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();
}
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
See my message in this topic for a link to a zip of all the files.
-
- Posts: 43
- Joined: 2011-10-01T14:34:04-07:00
- Authentication code: 8675308
Re: Accessing Pixel
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(); }
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Accessing Pixel
I second the motion!Thank you very much el_supremo
I did not really want to go though all the effort of working out the surrounding 'fluff'. It makes for a good example.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
-
- Posts: 1015
- Joined: 2005-03-21T21:16:57-07:00
Re: Accessing Pixel
I'm glad it helped. I'll add it to my MagickWand examples page later today.
Pete
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
See my message in this topic for a link to a zip of all the files.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Accessing Pixel
Perhaps it should be included in IM sources as a basic example demo.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
-
- Posts: 43
- Joined: 2011-10-01T14:34:04-07:00
- Authentication code: 8675308
Re: Accessing Pixel
I think you should....it definetly helps a lot of people...anthony wrote:Perhaps it should be included in IM sources as a basic example demo.
-
- Posts: 43
- Joined: 2011-10-01T14:34:04-07:00
- Authentication code: 8675308
Re: Accessing Pixel
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.
-
- Posts: 1015
- Joined: 2005-03-21T21:16:57-07:00
Re: Accessing Pixel
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)
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)
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
See my message in this topic for a link to a zip of all the files.
-
- Posts: 43
- Joined: 2011-10-01T14:34:04-07:00
- Authentication code: 8675308
Re: Accessing Pixel
Thank you...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)