?
Solved

How to force a bitmap to be 8 bit

Posted on 1998-02-05
11
Medium Priority
?
604 Views
Last Modified: 2012-05-04
My program makes bitmaps which use a custom 256 color palette. I use the bitmap routines in Delphi for creating and saving the bitmaps. When Windows is set up in 24 bit color mode, the bitmaps are saved as 24 bits, making them 3 times bigger than necessary. How can force them to be 8 bit?
0
Comment
Question by:toreot
[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
  • 2
  • +2
11 Comments
 
LVL 3

Expert Comment

by:Matvey
ID: 1358311
What I did, is open a truely 256 color bitmap(check an example that comes with Delphi), and on it you draw yours, so it's palete is set to 256 colors.
I'm sure you can also play with the Palete property, but I had no patience to do it... Anyway, you can check the sources.

Good luck!
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1358312
Hi

If you are using Delphi 3, then you can use the PixelFormat property of a bitmap:
  MyBitmap.PixelFormat := pf8Bit;

JB
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1358313
maybe interesting too, if you use D3 :

http://www.borland.com/devsupport/delphi/qanda/new/787.html
0
Industry Leaders: 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!

 

Author Comment

by:toreot
ID: 1358314
Matvey: By opening, do you mean loading a bitmap file? That is not useful in my case. I can't find the example you are refering to. Do you have the file name?

JimBob: I have bought D3, but not used it yet. Perhaps it is as easy as using the PixelFormat property. I will try as soon as possible. However, my program also ought to run under Win 3.11 so I would appreciate if anyone could tell me how to do it in D1. I have a feeling that it is necessary to use Windows API.

ZifNab: An interesting item, but I can't see how it might apply to my problem.
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1358315
Well,you can find some files in the images\splash or smth like it.

Do you have D1? I think it also has some pictures...
Anyway, if you want, I can send some to you...my email:
bosism@netvision.net.il
0
 

Author Comment

by:toreot
ID: 1358316
I misunderstood you. I thought you meant a coding example. But my program needs many bitmaps. You suggest I load a 256 color bitmap, erase its contents, change its palette, width and height and then draw on it each time I need a bitmap? It might work, but seems rather inelegant.
0
 
LVL 5

Expert Comment

by:inter
ID: 1358317
Toreot,

The cause and solution is as follows:

CAUSE
-----
The Delphi, when creating a new bitmap, creates a COMPATIBLE bitmap image with the Screen (i.e. desktop image). Since Desktop in 24 bit is a 24 bit image, delphi auto create a 24 bit image. So we need to alter this after creating the TBitmap or program our TBitmapX which dono create compatible bitmap with screen.

CURE
----
Since, we cannot change the VCL, we should convert the bitmap to 8 bits uppon writting ONLY IF it is a 24 bit bitmap due to the screen. To overcome with minimum trouble we should guarantee that the actual colors in the image are < 256. Upon saving the image we do the following:

1 - We prepare bitmap file header by computing the BITMAPINFOHEADER with pallette table + Bitmap size
2 - We prepare the BITMAPINFOHEADER such that biBitCount = 8 even if the image is 24 bit
3 - We prepare a color pallete(or may be you have already one).
4 - We convert all pixels RGB values(24 bit images have R G B components for each pixel as you know) to pallete indexes and prepare a 8 bit image buffer.
5 - We write BITMAPFILEHEADER, BITMAPINFO HEADER, PALETTE, and BitmapBits to file?

Before doing so, what do you think about it?
(I am quite sure that, there is no other way if we want to use TBitmap, but there exists if we write our own TBitmap class.)
Smile, not that hard!
Igor
0
 
LVL 8

Accepted Solution

by:
ZifNab earned 600 total points
ID: 1358318
Look at GenPal v1.00 (freeware) :

http://www.geocities.com/Athens/Delphi/9072/download.htm

you can use this source code to change to get it like you want.

And something very interesting, but not for 24 to 8 bit :

http://www.melander.dk/delphi/resampler/index.html

(also links to a very good info site, with ISDN's about 24 to 8 bits)
0
 

Author Comment

by:toreot
ID: 1358319
Sorry for the delay. I will award Inter the points if he can supply more details.
  The following may complicate things: My bitmaps are filled with a mathematical pattern which is copied from a form canvas using CopyRect. The canvas is 24 bits, but uses a 256 color palette. Some trials with PixelFormat pf8Bit in D3 suggests that the copying mangles the bitmap palette. Any comments on this?
0
 
LVL 5

Expert Comment

by:inter
ID: 1358320
Sorry I was out,

1 - Does speed important for you?
2 - In any case what are the dimensions of the region you are copying?

Igor
0
 

Author Comment

by:toreot
ID: 1358321
No, speed is not important. The dimension of the region varies, it may be the whole screen.
 I restate my problem: I draw a 256 color picture on an MDI form using my own palette. I wish to save this pattern as a 8-bit bitmap, regardless of the Windows color depth, without destroying the palette. I will be satisfied with a Delphi 3 solution, but it would be nice if it would work in D1 too.
0

Featured Post

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!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month12 days, 6 hours left to enroll

752 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