Windows BMP 3.x File Specifications Version 1.04 ----------------------------------------------------------------------------- Created in 4/27/1997 - Last update 05/16/1998 Written by FJTC for e-Software GP E-Mail: chino@icmsc.sc.usp.br ----------------------------------------------------------------------------- Distribution Note: This file is a freeware. ----------------------------------------------------------------------------- Use it by your own risk. I'll not assume any damage that the information in this file could do in your computer, brain or anything related you. ----------------------------------------------------------------------------- Introduction This file contains the description of the Microsoft Windows BMP version 3.x. Enjoy it. ----------------------------------------------------------------------------- Conventions of this file Variable sizes: BYTE: Unsigned 8-bit integer WORD: Unsigned 16-bit integer (IBM-PC) DWORD: Unsigned 32-bit integer (IBM-PC) Language equivalents: This file C/C++ Pascal QBasic/VB 3- BYTE unsigned char byte STRING * 1(**) WORD unsigned int word INTEGER(*) DWORD unsigned long longint(*) LONG(*) (*) Note: Unfortunately it's signed integer. (**) Note: It's not an integer data type, you must use asc(x) function. ----------------------------------------------------------------------------- File Organization The BMP file is divided in 3 or 4 parts: The file information Header, the Image information Header, Palette (optional, it's not used in True Color images), and Bitmap data. You must read all parts to rebuild the stored image. ----------------------------------------------------------------------------- File Information Header The file information header is organized as follow: Record BMPInfoHeader FileId: WORD - File Id. data. Always "BM" or WORD 19778 FileSize: DWORD - Fisical file size in bytes. The Windows programs rarely see this field. Reserved1: WORD - Always 0 Reserved2: WORD - Always 0 ImgOffset: DWORD - Offset of the image bitmap. Windows programs use it to read the bitmap data. End Record Using this header's informations, you will be able to know if the file is a BMP file. For example: Function IsBMPFile(BMPInfoHeader, FileSize): Boolean Begin Return ((BMPInfoHeader.FileId = "BM" and (BMPInfoHeader.FileSize = FileSize) and (BMPInfoHeader.Reserved1 = 0) and (BMPInfoHeader.Reserved2 = 0)) End. Note: You can use the Image Information Header to verify it too. ----------------------------------------------------------------------------- Image Information Header The image information header is organized as follow: Record ImgInfoHeader HeaderSize: DWORD - Size of this header (always 40 bytes) ImgWidth: DWORD - Image Width in pixels ImgHeight: DWORD - Image Height in pixels NumPlanes: WORD - Number of planes (Always 1) PixelDepth: WORD - Number of bits per pixel. It could be: 1 = two colors 4 = 16 colors (Use palette) 8 = 256 colors (Use palette) 24= True color Compression: DWORD - Compression method used in Bitmap 0 = no compression (default) 1 = 8-bit RLE compression 2 = 4-bit RLE compression BitmapSize: DWORD - Size of bitmat data in file. Typically sets to 0 HResolution: DWORD - Horizontal resolution in pixel/m. VResolution: DWORD - Vertical resolution in pixel/m UsedColors: DWORD - Number of colors in image. If it's = 0 then, the image uses the maximum number of colors determined by PixelDepth. This value is 2^PixelDepth SignificantColors: DWORD - Number of significant colors used in the bitmap. This information is very usefull if you will put an image in a display that supports fewer colors. End Record Using this information, you will be able to read the palette data (if it exists) and the bitmap data. The palette always exists in 1, 4 and 8 bits/pixel images and never exists on 24 bits per pixel. ----------------------------------------------------------------------------- Palette (Optional Data) Palette only exists in bitmaps tha uses 256 colors or fewer. The palette is a vector palette[ColorsUsed] of BMPRGB. The BMPRGB uses this structure: Record BMPRGB Blue: BYTE - Blue Value (0-255) Green: BYTE - Green Value (0-255) Red: BYTE - Red Value (0-255) Reserved: This value can be ignored End Record Each attribute is stored in order. For example: palette[0] contains the information about color attribute 0 palette[1] contains the information about color attribute 1 . . . palette[255] contains the information about color attribute 255 Important Note: The BMPREG is not in standard RGB model. Each 4 bytes data is represented using tihs format: BGRA where B is blue byte, G s Green byte, R is the red byte and A is not used yet but I think it'll be the ALPHA byte in future uses. ----------------------------------------------------------------------------- Bitmap Data The bitmap data is always byte-oriented. The data is stored in sequential order but BMP pixel order is diffeent of the hardware pixel order. In BMP bitmap, the first pixel is the down-left corner pixel and the last is the top-right corner pixel. Example: Image 320x200 1st pixel = 0, 200 2nd pixel = 1, 200 Last pixel = 319, 0 (using hardware that uses 1st pixel at Top-Left corner) In BMP file, each line MUST have n bytes, that n is divisible by 4. If the image have n not divisible by 4, the BMP file will fill with non-valid information. For example if an image have 1 pixel per line in 8-bit color depth, the BMP will save 3 additional pixel that must be ignored when you read the bitmap. Take care with this. This file will not tell about the compression method 1 and 2 because they are rarelly used (Only .RLE files use these methods). ------------------------------------------------------------------------------ Hints to save a BMP File Do not forget: - All Headers have some constants values into them. Don't forget them or other applications will not be able to open your file. - The palette ranges are 0-1 (1-bit), 0-15 (4-bit) and 0-255 (8-bits) Remember, for each value of R, G or B, Reserved may assume any value. 24 bits images DOES NOT have palette. - Each line of the BMP MUST have n * 4 bytes. - The first pixel in the bitmap is the botton-left and the last is the top-right pixel. DON'T forget it. - Do not use this file format if you don't want to spend disk space. The real advantage is the large range of applications that support it. ------------------------------------------------------------------------------ More information: Microsoft Corp. at http://www.microsoft.com