1

I have taken the follow code from this question and I am trYing to visualize the colors of an bmp image.

    int row_padded = (width*3 + 3) & (~3);
    char* data = new  char[row_padded];
    char tmp;

    for(int i = 0; i < height; i++) {
        fread(data, sizeof( char), row_padded, f);
        for(int j = 0; j < width*3; j += 3) {
            cout << "B: "<< (double)data[j] << " G: " << (double)data[j+1]<< " R: " << (double)data[j+2]<< endl;
        }
    }


This is the image:
This is the image
However most of the results I get are B:0 G:0 R:0, despite the fact the picture has nothing black on it. Is this the right way to get the colors?

  • 1
    I suggest using a library (such as [stb_image](https://github.com/nothings/stb/blob/master/stb_image.h)) instead of parsing the file manually. – HolyBlackCat Jan 12 '20 at 15:19
  • I want to do it without any libraries. – Христо Тодоров Jan 12 '20 at 15:35
  • Then you can look at how stb_image does it, and do something similar. – HolyBlackCat Jan 12 '20 at 15:38
  • 2
    It is a PNG image. Your operating system knows how to read them. https://learn.microsoft.com/en-us/windows/win32/gdiplus/-gdiplus-using-images-bitmaps-and-metafiles-use – Hans Passant Jan 12 '20 at 15:41
  • See may answer at https://stackoverflow.com/questions/47768094/pointer-file-randomly-changes-value-in-middle-of-reading-raw-bitmap-data which shows how to read a bitmap and how to get the color value of each pixel. – Paul Ogilvie Jan 12 '20 at 15:46
  • 1
    If you want to do it without using any libraries, then you'll just end up re-implementing the code in those libraries you don't want to use. What's the point? Except for a lot of extra work.. – Jesper Juhl Jan 12 '20 at 16:13
  • Which language, C or C++? They are distinct languages. For example, C++ has iterators, algorithms, and `std::vector`. The C language doesn't. – Thomas Matthews Jan 12 '20 at 21:26

0 Answers0