Solved

Load an image into a bitmap

Posted on 1998-06-08
7
374 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
[X]
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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 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