Page 1 of 1

SetPixelRed problem?!

Posted: 2012-11-26T04:15:31-07:00
by Pantera_Neagra
I want to develop an encryption/decryption algorithm using last bits of a photo.
Everything it's seems ok (bit mask all that stuff), i obtain the desired modified pixels (RGB) from the image.

The only problem is when I try to write them. It's very frustrating and I don't know what I do worng.
Let's say I have a pixel with R=100 G=80 B=120.

I use
SetPixelRed(pixel,100);
SetPixelGreen(pixel,80);
SetPixelBlue(pixel,120);

When I will read the modified pixel from the new image, i will have almost the same value like R=102 G=80 B=118.
It's not a big difference, but for my algorithm it's a disaster.

Please help me with some advices to accurate the pixels...
I tried to increase the precision , but nothing happend ...

Re: SetPixelRed problem?!

Posted: 2012-11-26T05:33:57-07:00
by magick
You need to scale your pixel values. Here is a classic pixel loop in ImageMagick:

Code: Select all

    p=pixels;
    for (y=0; y < (ssize_t) image->rows; y++)
    {
      q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
      if (q == (PixelPacket *) NULL)
        break;
      for (x=0; x < (ssize_t) image->columns; x++)
      {
        SetPixelAlpha(q,ScaleCharToQuantum(*p++));
        SetPixelRed(q,ScaleCharToQuantum(*p++));
        SetPixelGreen(q,ScaleCharToQuantum(*p++));
        SetPixelBlue(q,ScaleCharToQuantum(*p++));
        q++;
      }
      if (SyncAuthenticPixels(image,exception) == MagickFalse)
        break;
  }

Re: SetPixelRed problem?!

Posted: 2012-11-26T08:01:21-07:00
by el_supremo
If you save the image as a JPG, the lossy compression will change the colours of the pixels slightly.

Pete

Re: SetPixelRed problem?!

Posted: 2012-11-26T08:53:44-07:00
by Pantera_Neagra
Thank you very much for the replays.

It seems like my code but if I use GetAuthenticPixels it return always NULL ...
It work just with GetCacheViewVirtualPixels.

About jpg indeed I try to use JPG , how can I fix it? (also i have problem in png, in png works better but after around 30 pixels one is corrupt ... ).

Re: SetPixelRed problem?!

Posted: 2012-11-26T09:47:48-07:00
by Pantera_Neagra
even if i use ScaleCharToQuantum, i still have the same problems..