Solved

How to force a bitmap to be 8 bit

Posted on 1998-02-05
11
545 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
Comment Utility
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
Comment Utility
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
Comment Utility
maybe interesting too, if you use D3 :

http://www.borland.com/devsupport/delphi/qanda/new/787.html
0
 

Author Comment

by:toreot
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:toreot
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

9 Experts available now in Live!

Get 1:1 Help Now