CImg: What is meant by a "pixel value"?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jakebruce86
    New Member
    • Feb 2010
    • 3

    CImg: What is meant by a "pixel value"?

    In the CImg library and documentation there are many functions that return "pixel values". I have noticed through experimentation that some pixel values are negative. If I set a pixel to a specific color, is there any way that i can return the exact RGB value of that color? What is a "pixel value" and how is it related to the pixel's actual color?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Well that rather depends on how you declared your image. Since the class is actually CImg<T>, i.e. a template class, and that template parameter T represents the type of the pixel the pixel value is whatever type you chose to use when you declared your CImg and how that type holds the pixels colour is dependent on what that type is.

    Normally pixels are not signed types so the fact you are getting negative values suggests you are declaring it incorrectly. Is just the values used to represent the colour combined together using the bitwise operators to produce a single value.

    For example in a Windows bitmap using a RGB colour scheme the pixel is a DWORD (unsigned 32bit value). You can use the RGB macro to combine the a red, green and blue value, each unsigned 8bit values into a single unsigned 32bit pixel COLORREF. The are inverse macros to get the red, green and blue components from a COLORREF.

    However you have not said what pixel type you are using so this that example is probably just that, an example that doesn't apply to your situation.

    Comment

    • jakebruce86
      New Member
      • Feb 2010
      • 3

      #3
      Here's a simple experimental program I have been messing with to try and understand what pixel values are. First I set all the pixels in an image called "img.png" to some random color. Then I read the pixel value using atXY. It seems to always be returning just the red component of the image.
      ----------------------------------------------------------------------------------------------------
      #include <CImg.h>
      #include <iostream>

      using namespace cimg_library;

      using namespace std;

      const char colWhite[] = { 255, 255, 255 };

      const char colBlack[] = { 0, 0, 0 };
      const unsigned char colRand[] = {150, 100, 50};

      int main() {
      CImg<unsigned int> *image = new CImg<unsigned int>("img.png") ;
      for(int x = 0; x < image->dimx(); ++x) {
      for(int y = 0; y < image->dimy(); ++y) {
      image->draw_point(x,y ,colRand);
      }
      }

      clog << image->atXY(0,0) << "\n";
      image->save_png("img" );
      return 0;
      }
      ----------------------------------------------------------------------------
      I've tried changing the rand color values, and from my experience, the atXY function seems to always just return whatever the red value is. Is this actually what is happening here?

      Thank you!

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        According to the documentation CImg can not load PNGs

        Comment

        • 69huff
          New Member
          • Feb 2010
          • 1

          #5
          CImg *can* handle png's, using libpng (or ImageMagick, etc).

          I don't know if there's a better method to access the pixels values, but, maybe this helps you:

          image->atXY(x, y, 0) // this gives you the red channel
          image->atXY(x, y, 1) // this gives you the blue channel
          image->atXY(x, y, 2) // this gives you the green channel

          If someone knows how to get a whole UINT with the ARGB set, please post it. It would be more practical than calling thrice the function, I guess.

          Salud!

          Comment

          • jakebruce86
            New Member
            • Feb 2010
            • 3

            #6
            Whoa! I had no idea about that! That will most definitely help! Thank you so much.

            Comment

            Working...