0

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.

Daniel
  • 7,357
  • 7
  • 32
  • 84
  • What is `Image`? We need to know what library that is coming from. Also, why no external libs? You are clearly using one already. If you want to do it yourself: [Portable Network Graphics (PNG) Specification (Second Edition)](https://www.w3.org/TR/2003/REC-PNG-20031110/) – Ted Lyngmo Jan 04 '22 at 13:44
  • I'm not using external libs, I'm just writing an example code that doesn't even exist but would fit my needs if it did. `Image` doesn't even exist. – Daniel Jan 04 '22 at 13:46
  • 2
    Ok, well, then the only option is to read the spec and take it from there. If you need to be able to load images with other formats, you need to read the specifications for those too. – Ted Lyngmo Jan 04 '22 at 13:46
  • 2
    You will have to use an OS API or an external library. It's not a trivial task to write your own library to decompress and read png files. – drescherjm Jan 04 '22 at 13:47
  • I was unclear with my question, so I edited to clarify. – Daniel Jan 04 '22 at 13:50
  • Depending on which OS you are using you may already have support to read such files. – drescherjm Jan 04 '22 at 13:50
  • Its been a long time I wrote my own graphics file loaders so I wonder, is there a reason why you don't want to use a lib? – Pepijn Kramer Jan 04 '22 at 13:51
  • 1
    If you want to make something rather portable and have it done Today rather than in a Year, I'd recommend a library. OpenCV is popular and makes it easy. Just follow a short tutorial and you'll be reading pixels before you know it. – Ted Lyngmo Jan 04 '22 at 13:52
  • I prefer not to use external libs if I can, but I will if I need to, I just thought this should be a task simple enough that external libs wouldn't be necessary. I just need to read pixels really, nothing else. – Daniel Jan 04 '22 at 13:54
  • To not use any library, I would consider using PPM images (https://en.wikipedia.org/wiki/Netpbm#File_formats). That's an easy image implementation you should be able to read & write easily from scratch. – BNilsou Jan 04 '22 at 13:54
  • If you are using MSWindows you can use this OS API: [https://learn.microsoft.com/en-us/windows/win32/api/gdiplusheaders/nf-gdiplusheaders-image-fromfile](https://learn.microsoft.com/en-us/windows/win32/api/gdiplusheaders/nf-gdiplusheaders-image-fromfile) – drescherjm Jan 04 '22 at 13:54
  • @BNilsou ppm images are fine. – Daniel Jan 04 '22 at 13:56
  • 1
    @Daniel Loading an uncompressed bmp is kind of easy, but compressed graphics are not. In any case, unless it is for my own educational purposes I'd rather use already tested documented libraries then writing my own. They also tend to have a community you can ask for support. – Pepijn Kramer Jan 04 '22 at 13:56

1 Answers1

0

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