Solved

changing pixel depth in bitmaps

Posted on 1998-04-08
3
606 Views
Last Modified: 2013-11-20
The problem is that when I'm in 16-bit+ color modes and I want to create a bitmap that has a color format of 8 bits per pixel, the bitmap created is drawn with colors that I do not want.  The way i create an 8 bit per pixel bitmap is to first create a bitmap in the current color mode i am in, then I use GetDibits to transform the bitmap into an 8 bits per pixel bitmap.  However, the documentation for GetDibits says that when it does this transformation, it will synthesize its own color table from a general mix of 256 colors including the 20 static colors defined in the default logical palette.  This is clearly NOT the color table i want.  I want the 8 bits per pixel bitmap to use the same colors as the original bitmap was drawn in.  (The original bitmap is definitely not drawn with more than 256 colors - it's drawn with just like 20 different colors.) The bitmap is a picture of the view within my application.

I've also thought of BitBltting from a 16+ bit bitmap to an 8-bit bitmap, but that means I have to create an 8-bit bitmap and select it into my destination device context.  The documentation says that when i do a SelectObject for a bitmap, the bitmap color format must be the same as the device context color format.  And, I am not sure how to create a device context that is in an 8-bit color format when my system is  in a 16-bit+ color mode.  How do i do this?
 
In short, when I am in 16-bit+ color resolution modes, I want to create an 8 bits per pixel bitmap (of my current view) that uses a 256 color palette that i specify.  How do i do this?  Thanks for your response(s).
   
0
Comment
Question by:Sulaco
3 Comments
 
LVL 3

Expert Comment

by:byang
ID: 1317557
Setup a BITMAPINFO structure, make it 8-bit, and put your palette RGB values in bmiColors field. Then use
CreateDIBSection(NULL,(BITMAPINFO*)pbmi,DIB_RGB_COLORS, &pvBits, NULL,0) to create the bitmap, it returns you an HBITMAP handle. Then load your bitmap into buffer pointed by pvBits. Use SetDIBitsToDevice() to draw it.

0
 

Author Comment

by:Sulaco
ID: 1317558
Well, first, i don't want to draw the 8 BPP bitmap.  I want to save it out to a file.  (So, I don't need to use SetDibitsToDevice, right?)  Second, I still don't understand how loading my 16+ BPP bitmap data into the buffer pointed to by pvBits will create a 8 BPP bitmap for me.  The original bitmap i have created is in 16+ BPP.  If i simply loaded this data into the buffer pointed to by pvBits, it won't change the color format from 16+ BPP to 8 BPP, will it?

0
 
LVL 1

Accepted Solution

by:
JamieR earned 100 total points
ID: 1317559
First off, DIBs of more than 256 colours do not have palettes, they simply store the actual RGB data for each pixel, rather than an index. To dither to 8bits, create an 8bit DIB and then simply step through each pixel in the 16bit image, read the RGB value and then step though each entry in your 8bit palette until you find the nearest colour. Store this index in your corresponding 8bit image.

Windows has a (very poor) attempt at doing this automatically if you try to display a 16bit image under an 8bit driver, which is what you are seeing at the moment.

Jamie

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

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‚Ķ
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

11 Experts available now in Live!

Get 1:1 Help Now