tFDIB


Quick Data

Header file:tfdib.h
Object name:tFDIB
Object index:1 of 1 Object

General Description

The tFDIB class is a class for handling bitmaps - not actually loading them from resources, but creating a bitmap from scratch and then drawing on it. You can load ordinary bitmap files and save ordinary bitmap files. It does consume a lot of memory though, so be careful.

This class was not actually written by myself. theForger <forger@winprog.org> actually wrote this class, and put it on the net. I came across it, liked it, and with his permission, modified it to work with my foundation classes. He has also given me permission to redistribute it - and, of course, his legal info is included, intact, at the top.

Now this class has some distict limiatations. It can not load, save or create certain bitdepths - one bit and four bit is out of the question; I think you might be able to do 8 bit, possibly 16 bit, and definitely 24 bit. Please keep this in mind when you are programming with this class.

This component is derived from _GRAPHICSBASE, and inherits the properties from that. That's right, one of my modifications was to add the capability of drawing on the bitmap just like you would any other F3C graphics object - so drawing will be very familiar.

The only other thing of note would be the file handling routines. If you don't want to use the file routines (load and save) then just #define tFDIB_NOUSEFILEFUNC before you include the file. If you wish to use the file routines via the fFILESTREAM interface (you'll understand why later) then you will need to include it (filestream.h) before you include this file, or otherwise those routines will not be compiled in. If you are not using file routines, don't include them - it will make your executable enlarge by 50-60 kilobytes!


Methods


Prototype Description

[Constructors]
tFDIB();
tFDIB(UINT Width, UINT Height, int BitDepth);
tFDIB(const char* Filename);
All of these are constructors for the tFDIB class. The first constructor does nothing; it just initialises the class, ready for you to do something with it. The second constructor creates a bitmap with the specified Width, Height and BitDepth. The third constructor automatically loads the specified filename from file.
bool New(UINT Width, UINT Height, int BitDepth); This function creates a new bitmap from scratch. The new bitmap will be Width wide and Height high, and have BitDepth depth.
bool LoadFromFile(char* FileName); bool SaveToFile(char* FileName); These functions either load or save a bitmap. The functions deal with bitmaps only in the bitmap form - JPEG's or GIF's or anything else is not supported.
HBITMAP GetBitmap(void); This function returns the handle of the bitmap. This is useful if you would want to draw the bitmap onto something else.
int GetWidth(void);
int GetHeight(void);
int GetWidthBytes(void);
int GetBitDepth(void);
These functions all return information about the bitmap that this class currently looks after. The only item here which probably needs explaining is the GetWidthBytes() function. This function returns the number of bytes that makes up one row of the image - effectively the number of bytes of a bitmap that is Width wide and 1 pixel high, and has the same bit-depth.
void SetPalette(HDC DeviceContext);
void SetupPalette();
These functions deal with the palette. The first function creates a palette based on the palette for a certain device context. The second function creates a generic palette for any device. You will only need to create or set a palette if you are going to create an 8 bit bitmap (as 8 bit bitmaps require a table of colour values, otherwise known as a palette).
bool Write(ostream & Output);
bool Read(istream & Input);
These function either read or write a bitmap from the specified filestream. How does this work? Let's say that you create a file which is merely two bitmaps following each other in the same file. It is possible to then extract both bitmaps individually. For the first bitmap, you would open the appropriate file, and just seek to the beginning. Then, let the appropriate function read the bitmap - it will stop at the end of the first bitmap. You could then pass that stream straight back to the function to make the function read the next bitmap - because it is already at the correct place to begin reading the next. In other words, you can read or write a bitmap in the middle of a file, without having to have one individual file to store just one bitmap. If you didn't understand any of that, don't worry - neither did I. Just see the examples section to see what I mean.
bool WriteF(fFILESTREAM & Output);
bool ReadF(fFILESTREAM & Input);
These functions are almost exactly like the above functions, except that they can use the fFILESTREAM interface instead of the underlying fstream interface.

Sample Code

The following snippet gives a basic idea of how to use this component.


//Create an instance of the tFDIB...
tFDIB Bitmap;

//Create a nice big bitmap...
Bitmap.New(800, 600, 24); //800x600, 24 bits.

//Draw on it like any other object...
Bitmap.Line(0,0, 799, 599);

//Use it elsewhere...
ScreenGraphics.Draw(0,0, Bitmap.GetHandle());
// (The above line draws the bitmap on the screen at
//  0,0 (provided that you set up the ScreenGraphics
//  component correctly)

//Now save a bitmap.
Bitmap.SaveToFile("simpleline.bmp");

//Now try something tricky. Find two bitmaps and give them
//short names (less than 8 characters, no spaces). Now,
//type this in on the command line:
// copy bitmap1.bmp+bitmap2.bmp combined.bmp
//(this will combine the files togther). Now try this:
tFDIB Bitmap1;
tFDIB Bitmap2;
fFILESTREAM File;

File.OpenNormalFile("combined.bmp", fmOpenRead);

Bitmap1.ReadF(File);
Bitmap2.ReadF(File);

//Now Bitmap1 contains the first bitmap, and Bitmap2
//contains the second bitmap! Used creatively, this
//can be quite useful...

Back to indexThe FreeFoote Foundation Classes Documentation