Hi folks I know this is not the best place to ask, Ive already asked in the mailling list and imagemagicks board, but maybe someone here had this very same problem, i'm trying to implement a simple posterization function, but I get strange values for the pixel vales and the image (it is supposed to work only with grayscale ) is outputed unchanged I have no idea about what do to anymore. But I will post the code, maybe someone have an idea:
Code:
int quantizateImage( Picture* input, Picture* output, int quantfactor )
{
long x, y;
const PixelPacket *p;
PixelPacket* q;
PixelPacket white = {MaxRGB, MaxRGB, MaxRGB, OpaqueOpacity};
output->data = CloneImageInfo( input->data );
output->exception = &(input->image)->exception;
output->image = CloneImage( input->image,0,0,MagickTrue, output->exception );
if (output->image == (Image *) NULL)
{ return 0; } //@TODO: Provide a better exception handler
q = GetImagePixels( output->image,0,y,(output->image)->columns,1 );
if ((p == ( const PixelPacket* ) NULL) || (q == ( PixelPacket * ) NULL))
return 0;
int temp = MaxRGB/2;
for (x=0; x < (long) (input->image)->columns; x++)
{
q->red = q->red/temp;
q->red = q->red*temp;
q->green = q->green/temp;
q->green = q->green*temp;
q->blue = q->blue/temp;
q->green = q->green*temp;
q++;
}
if (SyncImagePixels( output->image ) == MagickFalse)
return 0;
if (y < (long) (input->image)->rows)
{ return 0; } //@TODO: provide a better exception handler
}
A guy mentioned I could be overflowing float representation so Iv re implemented the for as
Code:
for (x=0; x < (long) (input->image)->columns; x++)
{
q->red = floorf(q->red/quantfactor);
q->red = q->red*quantfactor;
q->green = floorf(q->green/quantfactor);
q->green = q->green*quantfactor;
q->blue = floorf(q->blue/quantfactor);
q->blue = q->green*quantfactor;
q++;
}
but I still have the same output =/
Oh Ive forgotten to provide the definition of Picture :P
Here it goes:
Code:
typedef struct Picture Picture;
struct Picture{
Image* image;
ImageInfo* data;
MagickWand* wand;
PixelPacket* pixels;
PixelIterator* image_iterator;
StringInfo* profile;
ExceptionInfo* exception;
Image* canvas;
};
Thanks in advance