It depends on the image file format you’re using: If it’s one of common formats, like PNG or JPEG, you better use corresponding library (like libpng, or jpeglib, respectively). If it’s in some kind of simpler format, then you could write the code by yourself; for example, If the image is in textual format, where each pixel/component value is an integer, then something along the lines of the code you pasted in your message will do. Note however that usually at least image size has to be supplied in the image file, before pixel values, so you’d probably read the image size first, then allocate memory for storing pixel values, and then proceed with the reading loop (where loop index/indices range will be determined by the image size again).