How to display a greyscale raw image (*.raw) in vc++?

Posted on 2003-02-22
Medium Priority
Last Modified: 2011-09-20
I am doing a project that needs to display image of raw format (*.raw) in VC++. It seems it is necessary for creating image header for display in Bitmap format.

I am just a student and the problem has not yet been solved for a long time. Could anyone help me to solve it? Thanks.
Question by:wesker
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 12

Accepted Solution

Salte earned 150 total points
ID: 7998342
greyscale isn't that difficult.

I assume it is (at most) 8 bit per pixel and thus maximum 256 different greyscale tones.

If so you can easily make a bitmap file.

Open a file as binary file:

write the bitmap file header and the bitmap header (both of them). specify 8 bit per pixel and specify as many colors as you have greyscale tones.

write a palette.

THe lowest index (index 0) of the palette should be the color RGB = (0,0,0);

The highest index of the palette should correspond to white color: RGP (255,255,255);

Then do something like this:

if you use 64 grey scale tones then you have values in the range 0-63 with 0 meaning black and 63 meaning white.
Index 0 and 63 has already been placed, insert all the greyscale tones in between, RGB = (x,x,x) where each x is a one byte value that is computed based on your n-bit value (n <= 8). This is probably best done by extended the missing bits if any with all zeroes. Note that the white color will have all bit set instead and so the distance between 62 and 63 is larger than between 48 and 49 in the tones. That shouldn't be noticable though.

Just make sure that the highest greyscale tone is 255,255,255.

Grey scale values are all values with RGB = (x,x,x);

Once you have made the palette you can build the image in such a way that it will fit in a bit map.

Just copy pixel by pixel. Use one byte per pixel in the bitmap. Not sure how many bits per pixel in the .raw file and not sure about the format there but you probably know that.

.bmp files have their image stored as an array of lines. There are H lines where H is the height of the image.

Each line have room for W pixels where W is the width. However, a line is always a multiple of 4 so if the width is not a multiple of 4 there are some extra bytes at the end of the line which is ignored as far as displaying the image but they are used to align the next line on a 4 byte boundary. For example if width is 41 you get 3 bytes extra so the line length is 44 bytes.

line_length = (width + 3) & ~3;

can be used to compute the length of a line given the width. This assumes 8 bits per pixel. If you use 3 bytes per pixel it would be (width * 3 + 3) & ~3; instead.

All the pixels are stored left to right in a line. Byte by byte. Place the palette indices for that color. The way the palette was made, the palette index is the same as the greyscale tone and so that pixel value should be easy.

Then save the palette and image after the headers and the file is complete.


Author Comment

ID: 7998935
Hi Alf,

Thank you very much for your detailed explanation!
I can get some idea how to do now. But, could you mind explaining more on how to code the data from *.raw format to RGB(x,x,x) and the palette? I am a VC++ beginner and don't know how to build such program from zero. Thanks.

LVL 11

Expert Comment

ID: 9784164
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to Salte

Please leave any comments here within the next seven days. Experts: Silence
means you don't care.


-bcl (bcladd)
EE Cleanup Volunteer


Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question