Solved

Load an image into a bitmap

Posted on 1998-06-08
7
369 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now