C++ Converting image to integer array
-
27-09-2019 - |
Question
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.
OTHER TIPS
Unless you are doing it for interest and learning, I would recommend you do use a third party library, for example ImageMagick.
This will save you time (and avoid bugs)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow