SetPixelRed problem?!

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
Pantera_Neagra
Posts: 3
Joined: 2012-11-26T03:55:01-07:00
Authentication code: 6789

SetPixelRed problem?!

Post 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 ...
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: SetPixelRed problem?!

Post 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;
  }
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: SetPixelRed problem?!

Post by el_supremo »

If you save the image as a JPG, the lossy compression will change the colours of the pixels slightly.

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.
Pantera_Neagra
Posts: 3
Joined: 2012-11-26T03:55:01-07:00
Authentication code: 6789

Re: SetPixelRed problem?!

Post 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 ... ).
Pantera_Neagra
Posts: 3
Joined: 2012-11-26T03:55:01-07:00
Authentication code: 6789

Re: SetPixelRed problem?!

Post by Pantera_Neagra »

even if i use ScaleCharToQuantum, i still have the same problems..
Post Reply