Printing Pixels

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
rohitkrishna
Posts: 43
Joined: 2011-10-01T14:34:04-07:00
Authentication code: 8675308

Printing Pixels

Post by rohitkrishna »

hi...

Can i print the values of pixels in the type pixelwand **pixels..?

My code is

Code: Select all

void subimages_pxls(MagickWand *wand,size_t sbimg_wdt,size_t sbimg_hgt)
{
        size_t width=MagickGetImageWidth(wand);
        size_t height=MagickGetImageHeight(wand);
        int n_sbimg=(width/sbimg_wdt)*(height/sbimg_hgt);
        printf("\nNo of Sub Images : %d\n",n_sbimg);
	int i,j,k=0;
	size_t row[n_sbimg],col[n_sbimg];
	for(i=0;i<height;i=i+sbimg_hgt)
	{
		for(j=0;j<width;j=j+sbimg_wdt)
		{
			row[k]=i;
			col[k]=j;
			k++;
		}
	}

PixelIterator * rg_itr;
	PixelWand ** pixels;
	
	int n,
	t=(sbimg_wdt*sbimg_hgt)*n_sbimg;
	int * p;
	
	p=(int*)malloc(t*sizeof(int));
	i=0;
	for(n=0;n<n_sbimg;n++)
	{
		rg_itr=NewPixelRegionIterator(wand,col[n],row[n],sbimg_wdt,sbimg_hgt);	
		for (y=0; y<sbimg_hgt; y++)
		{	
                pixels = PixelGetNextIteratorRow(rg_itr, &sbimg_wdt);
        	for (x=0;x<sbimg_wdt; x++,i++)          
                { 
                	p[i]=(int)*pixels;              
                }       
                }
        }
        for(n=0;n<(sbimg_wdt*sbimg_hgt)*n_sbimg;n++)
        {
        	printf("pixels %d : %d\n",n,p[n]);
	}
}
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Printing Pixels

Post by el_supremo »

There are three ways to obtain and print out the colours from a Pixelwand.
You can use PixelGetRed,PixelGetGreen,PixelGetBlue which return a normalized double.
Alternatively, PixelGetRedQuantum,PixelGetGreenQuantum,PixelGetBlueQuantum return the colours as integers
e.g.

Code: Select all

printf("pixels %d : %d,%d,%d\n",n,PixelGetRedQuantum(p[n]),PixelGetgGreenQuantum(p[n]),PixelGetBlueQuantum(p[n]));
Finally, there's PixelGetColorAsNormalizedString which returns a string such as 254,123,27
e.g.

Code: Select all

printf("pixels %d : %s\n",PixelGetColorAsNormalizedString(p[n]);
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.
rohitkrishna
Posts: 43
Joined: 2011-10-01T14:34:04-07:00
Authentication code: 8675308

Re: Printing Pixels

Post by rohitkrishna »

el_supremo wrote:There are three ways to obtain and print out the colours from a Pixelwand.
You can use PixelGetRed,PixelGetGreen,PixelGetBlue which return a normalized double.
Alternatively, PixelGetRedQuantum,PixelGetGreenQuantum,PixelGetBlueQuantum return the colours as integers
e.g.

Code: Select all

printf("pixels %d : %d,%d,%d\n",n,PixelGetRedQuantum(p[n]),PixelGetgGreenQuantum(p[n]),PixelGetBlueQuantum(p[n]));
Finally, there's PixelGetColorAsNormalizedString which returns a string such as 254,123,27
e.g.

Code: Select all

printf("pixels %d : %s\n",PixelGetColorAsNormalizedString(p[n]);
Pete
When i try to use the PixelGetColorAsNormalizedString its givin me an error lik this.....
error: argument of type "int" is incompatible with parameter of type "const PixelWand *"
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Printing Pixels

Post by el_supremo »

I forgot the "n" argument and there's a missing parenthesis:

Code: Select all

printf("pixels %d : %s\n",n,PixelGetColorAsNormalizedString(p[n]));
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.
rohitkrishna
Posts: 43
Joined: 2011-10-01T14:34:04-07:00
Authentication code: 8675308

Re: Printing Pixels

Post by rohitkrishna »

el_supremo wrote:I forgot the "n" argument and there's a missing parenthesis:

Code: Select all

printf("pixels %d : %s\n",n,PixelGetColorAsNormalizedString(p[n]));
Pete

Still gives the same error..

i even tried changing the data type to size_t instead of int when type casting
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Printing Pixels

Post by el_supremo »

Change the second half of your code to this:

Code: Select all

PixelIterator * rg_itr;
PixelWand ** pixels;

int n;
for (n=0;n<n_sbimg;n++) {
    rg_itr=NewPixelRegionIterator(wand,col[n],row[n],sbimg_wdt,sbimg_hgt);
    printf("\nsubimage %d\n",n);
    for (y=0; y<sbimg_hgt; y++) {
    	printf("row %d\n",y);
        pixels = PixelGetNextIteratorRow(rg_itr, &sbimg_wdt);
        for (x=0;x<sbimg_wdt; x++) {
            printf("pixels %d : %s\n",n,PixelGetColorAsNormalizedString(pixels[x]));
        }
    }
}
I don't know why you cast the PixelWand pointers as int * but it isn't a good idea. If you want to save those pointers you should malloc enough space for n_subimg*sbimg_hgt (PixelWand **) and save the values returned by PixelGetNextIteratorRow. I think you have to free them eventually anyway.

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.
Post Reply