Solved

Load an image into a bitmap

Posted on 1998-06-08
7
370 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi 2010 Export to pdf 2 285
Process filename extension 3 172
Doc'in system (example?) BA 7 86
firstChar challenge 13 106
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 …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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…

914 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

20 Experts available now in Live!

Get 1:1 Help Now