简体   繁体   中英

How can I load an image and iterate through its pixels?

My question is pretty simple, suppose I have an image and I want to load it into my program so that I can get the color of each pixel. How could I do that? In a nutshell I want a method that gives me the color of a pixel.

struct color{
    double r, g, b;
    color(){}
    color(float red, float green, float blue){r = red; g = green; b = blue;}
};

color GetPixel(string imageName, int x, int y){
    //if(x < 0 || x >= width of image) return color(0,0,0);
    //if(y < 0 || y >= height of image) return color(0,0,0);
    //do stuff
    return colorForPixelXY;
}

Preferably I would like to do it natively, without using any external libs.

The STB image library is a single file, header only "library" that you can include and use in your project scott free. It's sort of an industry standard for cases like yours. Highly recommended.

https://github.com/nothings/stb

That said, reading a PNM/PPM file is very easy thing to do in C++ as it is basically a text file. Any image package can take your BMP,JPG and save it to PNM that you can even include in your executable binary.

read PPM file and store it in an array; coded with C

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM