-2

I've been reading image files in double pointer arrays. ie:

    BMP(char *fn, int no )
{
    ifstream in(fn,ios::binary);
    in.read((char*)&header,sizeof(header));
    width=(int)(header[21]<<24|header[20]<<16|header[19]<<8|header[18]);
    height=(int)(header[25]<<24|header[24]<<16|header[23]<<8|header[22]);
    cout<<"Height:"<<height<<" Width:"<<width<<'\n';
    in.read((char*)&plt,sizeof(plt));
    clrs=new unsigned char[width*height];
    in.read((char*)clrs,width*height);
    in.close();

    rollNo = no;
}

The problem I'm facing is that, it reads color values from 0 to 255 as per grey-scale readings.

    void showOne ()
{
    for ( int i = 0 ; i < width*height ; i++ )
    {
        int val1 = clrs[i];

        cout << "\t\t index : " << i << " :\t\t" << val1 << endl;
    }
}

Can anyone tell me how can I convert that reading into a Hexadecimal value, or yet, any other way so that RGB values could be distinguished?

Tacet
  • 1,411
  • 2
  • 17
  • 32
  • 1
    I would recommend you don't roll it all yourself and make the same mistakes that countless other programmers have made - use a library. Good engineers can code - great engineers re-use building blocks created by others to work on more interesting problems than decoding Microsoft's proprietary formats from the 90s when hardware was slow and limited. The benefits will be that your code can then work with all formats, not just BMPs. Look at ImageMagick and other libraries. – Mark Setchell Dec 22 '14 at 10:40
  • can you suggest me any library in c++ that could be used to work with images. - Regards – Shahzada Fahad Ashraf Dec 22 '14 at 10:48
  • 2
    @MarkSetchell: true but (1) being *able* to write code that reads images is a nice skill to have, and (2) the BMP file format, for all its quirks, is by far not the hardest in the world to read. You could better have pointed the OP to a good BMP specification. – Jongware Dec 22 '14 at 10:52
  • It depends what area you want to work in - the most general purpose is probably Magick++, if you are doing high-performance machine vision then maybe OpenCV. Have a look here http://stackoverflow.com/questions/796364/fast-cross-platform-c-c-image-processing-libraries and http://stackoverflow.com/questions/9095043/good-library-for-image-processing-especially-for-morphological-operations-in – Mark Setchell Dec 22 '14 at 10:54
  • @Jongware That's why I only commented rather than answering - as I was just expressing a general opinion rather than giving a concrete answer. If you want to recommend a good BMP specification that could also help. – Mark Setchell Dec 22 '14 at 10:55
  • 1
    @MarkSetchell: the OP's code contains too many low-level errors to fix. And *because* the BMP format is ancient, and well-documented to boot, it's only a matter of using Google and picking *any* of the millions of hits. Downvoted for "lack of proper research", mentally noted for VTC because of Too Broad/Unclear What Is Asked. – Jongware Dec 22 '14 at 11:05
  • I've been striving to make a Retina Recognition software, I know it's been made before but I just want to understand the physics behind the whole thing. I've found a library called "CImg", hopefully it'll work. – Shahzada Fahad Ashraf Dec 22 '14 at 11:10
  • For clarity: (1) you don't parse the entire header, so you don't know if it's a 24-bit color image, or 8 bit, or something else; (2) as it seems you are expecting a 24-bit color image: you do not reserve space for and read all bytes; (3) "into a Hexadecimal value" makes no sense; (4) a single byte does not mean it's "grayscale". – Jongware Dec 22 '14 at 12:55
  • @MarkSetchell: although I can understand your means, I find incorrect and unpolite to "escape" from the problem without any knowledge of the context the OP is working in. The code to read a simple BMP is no more than 100 lines of plain C, the library you are suggesting are megabytes wide and have so many dependencies with code extraneous to the native OS environment (most likely windows), that -for a simple problem- are an overkill. Spending two days understanding the BMP format of two weeks in learning a foreign API not being that your primary task or job, isn't a "clean choice" at all. – Emilio Garavaglia Dec 22 '14 at 20:39
  • @MarkSetchell: " Good engineers can code - great engineers re-use building blocks created by others to work on more interesting problems". I'm sorry burt as a 50 y.o electronic engineer I have to strongly disagree for that. We need people that are able to THINK, not monkey cut&paster. Reusing other software is good only if you can understand it. Otherwise your brain will never have "content", but only "pointer tables" – Emilio Garavaglia Dec 22 '14 at 20:43
  • @EmilioGaravaglia I believe software reuse is a central tenet of modern software design - especially with C++ and its inheritance and polymorphism - what else is STL? Or any other widely used library? No need to believe me though, I was actually paraphrasing from "The Cathedral and the Bazaar" http://www.catb.org/~esr/writings/cathedral-bazaar/cathedral-bazaar/ar01s02.html – Mark Setchell Dec 22 '14 at 22:22

1 Answers1

0

When the image encodes 24 bits per pixel data (true color), the field biBitCount of the header is set to 24 (you must check that).

Then the pixel data is made of triples of BGR bytes. Depending on what you want to do with the color values, you will keep the raw bytes or transform them to other representations, three by three.