Solved

How to force a bitmap to be 8 bit

Posted on 1998-02-05
11
560 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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 300 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
add combobox item based on numbers 9 141
Delphi selector screen 2 78
Need Help Delphi 2010 CheckBox1 Stored value in memo 13 66
How to build JSON File in Delphi 6 3 32
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

808 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