Solved

Load an image into a bitmap

Posted on 1998-06-08
7
372 Views
Last Modified: 2013-12-03
I have some raw 8-bit grayscale data that I want to load into a bitmap.  Can anyone give me directions
   on how to load this data and display it in a CFormView.  I'm using VC5.0 and MFC. Thanx

   zeekial@yahoo.
0
Comment
Question by:zeekial
  • 4
  • 2
7 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 1316195
You need to convert the raw 8-bit grayscale data into a DIB.

1. Read the data into memory.
2. Create a grayscale palette.
3. Form the data and the palette to a DIB.
4. Display it.

Look into the MFC sample "DIBLOOK: Illustrates the Use of DIBs and Color Palettes" and the Win32 sample "Mypal: Palette Management Example".
0
 

Author Comment

by:zeekial
ID: 1316196
Here's some code to show you where I stand:

      int fh;
      char buffer[60000];
      unsigned int nbytes = 60000;

      fh = open("imagefile",O_RDONLY)
      read(fh, buffer, nbytes);

      now all my data is in character buffer "buffer".  How do I make a grayscale palette and how do I put this buffer into a DIB and display?  Which functions do I use?  
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 200 total points
ID: 1316197
You can simply create a compatible bitmap of the appropriate size, create a DC for it, and then use a loop that does a SetPixel for each pixel in the drawing setting its RGB values to be the same.

You don't _need_ to create a palette .. only if your dispaly has a limited number of colours (<16bit colour say) and you want to see all your grey scale levels.  Without a palette, windows will map your colours to the nearest colour available in the current palette.

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 10

Expert Comment

by:RONSLOW
ID: 1316198
SetPixelV is quicker, BTW than SetPixel

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1316199
try something like this ...

  CBitmap bitmap;
  CDC dcMem;
  dcMem.CreateCompatibleDC(NULL);
  bitmap.CreateCompatibleBitmap (hdc, width, height);
  CBitmap* pbitmapOld = dcMem.SelectObject(&bitmap);
  int i = 0;
  for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) {
      char grey = buffer[i++];
      dcMem.SetPixelV (x,y,RGB(grey,grey,grey));
    }
  }
  dcMem.SelectObject(pbitmapOld);

0
 
LVL 23

Expert Comment

by:chensu
ID: 1316200
>You don't _need_ to create a palette .. only if your dispaly has a limited number of colours (<16bit colour say) and you want to see all your grey scale levels.

Nowadays, there are still a lot of systems working on 256-color mode.

>Without a palette, windows will map your colours to the nearest colour available in the current palette.

Yes. In this case, the display will be likely looking ugly since you cannot guarantee the current palette contains enough grayscale entries (RGB(x, x, x)).

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1316201
The code above can be changed to create a true colour DIB (no palette required CreateDIBSection) and the set the pixels.  The palette manipulation can then be done at display time.

Alternatively, create a compatible bitmap (as above) and a use palette.

NOTE: If using CreateDIBSection, you can get back a points to the array of pixel values, and set them diretcly without using SetPixelV.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Login Script to Copy Folders 12 42
sameEnds challenge 3 180
font type, point size, readability questions 7 109
Fibonacci challenge 11 133
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

831 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