'BMP_EXAMPLE.BAS 'Copyleft 2012, M. Eric Carr / Paleotechnologist.Net 'eric (atsign) paleotechnologist (dot) net 'This code is placed in the public domain. 'Output of graphical data to a .BMP file, serving as 'an example of how the BMP header format works. '(FreeBASIC has this functionality built-in, using the BSAVE function.) 'Output is a sample 3x3, 24-bit bitmap (colors are in BBGGRR order): ' 000000 800000 FF0000 ' (black) (dk blue) (blue) ' 008000 808000 FF8000 '(dk grn) (dk cyan) (blue-cyan) ' 00FF00 80FF00 FFFF00 ' (green) (grn-cyan) (cyan) dim as ubyte fs1, fs2, fs3, fs4 dim as ubyte xs1, xs2 dim as ubyte ys1, ys2 dim as ulongint filesize, xsize,ysize,bmpsize,linesize dim as ulongint fstemp, bstemp, xstemp, ystemp dim as ulongint lineadj, x, y, pad open "test1.bmp" for output as #1 xsize = 3 ysize = 3 'Compute the correct line size (padded to a multiple of four bytes) linesize = xsize * 3 lineadj = 4-(linesize mod 4) if lineadj=4 then lineadj=0 linesize = linesize + lineadj 'Compute bitmap size and file size bmpsize = linesize*ysize filesize = 26 + bmpsize 'Split filesize into four bytes. This would be easier in C, but 'the approach used here works. fstemp = filesize fs1 = fstemp mod 256 fstemp = (fstemp-fs1)/256 fs2 = fstemp mod 256 fstemp = (fstemp-fs2)/256 fs3 = fstemp mod 256 fstemp = (fstemp-fs3)/256 fs4 = fstemp mod 256 'Split xsize (image width in pixels) into twp bytes xstemp = xsize xs1 = xstemp mod 256 xstemp = (xstemp-xs1)/256 xs2 = xstemp mod 256 'Split ysize (image height in pixels) into two bytes ystemp = ysize ys1 = ystemp mod 256 ystemp = (ystemp-ys1)/256 ys2 = ystemp mod 256 'BMP file type header print #1, "BM"; 'File size, in bytes (Little-Endian) print #1, chr$(fs1);chr$(fs2);chr$(fs3);chr$(fs4); 'Reserved (2 bytes) print #1, chr$(0);chr$(0); 'Reserved (another 2 bytes) print #1, chr$(0);chr$(0); 'Offset for start of bitmap data print #1,chr$(26);chr$(0);chr$(0);chr$(0); 'Bytes in the rest of the header (from this point on) (Little-Endian) print #1,chr$(12);chr$(0);chr$(0);chr$(0); 'Width of bitmap, in pixels (Little-Endian) print #1, chr$(xs1);chr$(xs2); 'Height of bitmap, in pixels (Little-Endian) print #1, chr$(ys1);chr$(ys2); 'Number of color planes (Little-Endian) print #1, chr$(1);chr$(0); 'Bits per pixel (Little-Endian) print #1, chr$(24);chr$(0); 'Start of bitmap data. Normally, this would execute two nested loops 'over the rows and columns. For now, we will simply output the data 'row by row and pixel by pixel: ' 000000 800000 FF0000 Top row ' 008000 808000 FF8000 Middle row ' 00FF00 80FF00 FFFF00 Bottom row 'The rows are stored bottom-to-top, so they will be output 'in that order. '================================================================== 'Bottom left pixel print #1, chr$(00);chr$(255);chr$(00); 'Bottom center pixel print #1, chr$(128);chr$(255);chr$(00); 'Bottom right pixel print #1, chr$(255);chr$(255);chr$(00); 'Three more bytes to pad the row from 9 to 12 bytes (a multiple of four) print #1, chr$(00);chr$(00);chr$(00); 'Middle left pixel print #1, chr$(00);chr$(128);chr$(00); 'Middle center pixel print #1, chr$(128);chr$(128);chr$(00); 'Middle right pixel print #1, chr$(255);chr$(128);chr$(00); 'Three more bytes to pad the row from 9 to 12 bytes (a multiple of four) print #1, chr$(00);chr$(00);chr$(00); 'Top left pixel print #1, chr$(00);chr$(00);chr$(00); 'Top center pixel print #1, chr$(128);chr$(00);chr$(00); 'Top right pixel print #1, chr$(255);chr$(00);chr$(00); 'Three more bytes to pad the row from 9 to 12 bytes (a multiple of four) print #1, chr$(00);chr$(00);chr$(00); close #1