 
LOADBMP() is a replacement for GFABasic for Windows' LOADBMP() function.

Developed by:
	Sjouke Hamstra
	Biotoop 2
	NL-3893 BK Zeewolde
	Netherlands
        CIS: 100101,1112  Forum: WINAPC 3rd Party  Library: GFA

Why this replacement?

Simply put: the LOADBMP() function implemented in GFABasic for Windows does not do a valid job. The color information from some bitmaps is not processed. It beats me why. The LOADBMP() function I present is a stripped version from a larger program that loads, stores and prints multiple DIBs. In this program a bitmap is kept with a seperate palette, where GFA stores the color information in the window palette. 

June 8 1995, Version 1.1
------------------------
LOADBMP doesn't run compiled. Until now. 

- LOADBMP used a DIM on a byte array to allocate memory for the DIB. To compile correctly $ABIG should be added. In version 1.1 DIM is replaced by MALLOC().

- The TYPE-assignment BITMAPINFOHEADER:{lpbi%}=*bi. didn't move the contents of the TYPE (compiled). The assignment is replaced by a BMOVE command.

- The routine to load a huge memory block is changed. When a memory block is stored over a 64k border the part that should be stored in the next 64k block ended up at the beginning of the current. In LOADBMP the the first byte is not stored at the beginning of a 64k block (offset=0) but at offset position $428. The next 32k block is tored in the current block, but the next block passes the 64k border. The $428 bytes that should be put in the next block are stored at the beginning of the current block. Now the first block to read is 64k-offset.

- Some minor changes are made, too.

----------------------------------------------------------------------------------

June 4th 1995, Version 1.0
--------------------------
Some background information

GFA stores the color information from a bitmap in the current palette of the active window. You can retrieve GFA's active window with the function WIN(). The current palette can then be obtained with _PAL(WIN()). Every window has its own palette with a maximum of 256 entries. The exact amount of entries depends on the video mode of your PC.

The LOADBMP() function simply replaces GFA's LOADBMP() and will store the colors in the current window palette.

Now for the program.

The LOADBMP() function uses some TYPEs that should be defined earlier. So when you merge the FUNCTION into your own program don't forget to add a call to the DEFDIB() procedure.

In the function LOADBMP() you will find a routine that loads large block of information (>64K) from disk to memory. Also you find information on how to use the API function RealizePalette() and GFA's REALIZE command which are poorly documented. RealizePalette calculates and sets the palette selected in the given DC to the systempalette. In fact RealizePalette calculates a translation table in which every color of the palette is mapped to a color of the systempalette. When no color match is found and an entrie of the system palette is free, GDI will fill it in with the requested color. Otherwise the color is mapped to the nearest color available in the systempalette. Once GDI has calculated the translation table it won't do it again!! Any subsequent RealizePalette() call simply resets the calculated translation table. 
The LOADBMP() function sets the palette entries of the current GFA window palette using the API function SetPaletteEntries(). To force GDI to recalculate the translation table you must first select the logical palette again in the current DC. GDI then knows to recalculate the mapping at the next RealizePalette().

WM_PAINT

Before The bitmap is put to the screen (allways as a result of a WM_PAINT message!) first the calculated palette is activated. No calculation is done at that moment, the palette is just activated, so it won't take any precious computertime. 

WM_PALETTECHANGED & WM_QUERYNEWPALETTE

Under Windows an application is never alone, so any program can and may change the systempalette. When your bitmap has to redrawn, either in the background or in the foreground, it should be drawn with the right colors. Windows sends two messages to inform the application about a palette change. (Windows only sends these messages to the application that used SelectPalette())
As a bonus I added a callback procedure which handles these messages. WM_PALETTECHANGED is send whenever the systempalette is changed by another application and the WM_QUERYNEWPALETTE message when your appliction receives the input focus again. The source is well documented and can be understood when you take the notes about RealizePalette into account.

Caveat.

- I developed and tested all this on a 256 color SpeedStar PRO video adapter. No problems noticed

- LOADBMP() does not load PCX files as GFA's LOADBMP() does.


June, 4th 1995
Sjouke Hamstra