Search code examples
c++imageargbpixel-manipulation

C++ Converting image to integer array


How would I go about converting the pixels in an image (.png file) to an integer array, where each pixel is converted to its ARGB integer equivalent? Not a 2D integer array by the way, a 1D one (where access is through array[row*width+col]).

Thanks.


Solution

  • Once you have read image data to some buffer, ordinary cast should do the trick:

    GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file ("image.png", NULL);
    unsigned char *pixels = gdk_pixbuf_get_pixels (pixbuf);
    int *array = reinterpret_cast<int*>(pixels);
    

    Example uses GdkPixbuf library, but other libraries should be similar.