 '===============
 'bmp file loader
 '===============

 'Charles Pegge
 '19:25 19/11/2011

 'details of BMP format:
 'wikipedia.org/wiki/BMP_file_format

 packed type BmpMapHeader
   short bfType
   long  bfSize
   short bfReserved1
   short bfReserved2
   long  bfOffBits
   '
   'BITMAPINFOHEADER (40 bytes)
   '
   long  biSize
   long  biWidth
   long  biHeight
   short biPlanes
   short biBitCount
   long  biCompression
   long  biSizeImage
   long  biXPelsPerMeter
   long  biYPelsPerMeter
   long  biClrUsed
   long  biClrImportant
 end type


  packed type pixel24
    byte b,g,r
  end type


  packed type pixel32
    byte b,g,r,a
  end type

   

  string bmpfile="t.bmp" 'choose your file
  string bmpdata
  getfile bmpfile,bmpdata
  sys le=len bmpdata
  '
  BmpMapHeader *d : @d = *bmpdata

  'DISPLAY HEADER VALUES
  '
  '-----------------------------------
  function DispBmpInfo(BmpMapHeader*d)
  '===================================
  '
  'capture signature to string
  '
  string n="    "
  **n=?d
  n=left n,2
  '
  string pr="BMP HEADER DATA FOR " bmpfile cr cr

  def    p pr=pr+ %1 tab %2 cr
   '
 '------------------------------------------
  p  "Signature:        "  n
  p  "File Size:        "  d.bfSize
  p  "Bit data offset : "  d.bfOffBits
  p  "Header size:      "  d.bisize
  p  "Image Width:      "  d.biWidth
  p  "Image Height:     "  d.biHeight
  p  "Bit planes:       "  d.biPlanes
  p  "Bits per pixel:   "  d.biBitCount
  p  "Compression:      "  d.biCompression
  p  "Size of Image:    "  d.biSizeImage
  p  "Xpixels per metre:"  d.biXPelsPerMeter
  p  "Ypixels per metre:"  d.biYPelsPerMeter
  p  "Colours used:     "  d.biClrUsed
  p  "Colours important:"  d.biClrImportant
  '------------------------------------------ 

  print pr
  '
  end function


  if bmpdata then DispBmpInfo d


  'TEST PIXEL READING
  '==================

  'pixel24 *pix : @pix = *BmpData + d.bfOffBits
  'print pix(1).r


  'CREATE A BMP FILE FROM SCRATCH
  '==============================

  pixel24 *pix '3 color pixel
  sys offs=64, w=16, h=16, bits=8*sizeof pix
  BmpMapHeader *hdr
  sys le=offs+w*h*sizeof pix
  BmpData=nuls le
  @hdr = *BmpData   'map header overlay
  @pix = @hdr+offs  'map pixel overlay


  'ENTER METRICS INTO THE HEADER
  '=============================

  hdr<=0x4d42,le,0,0,offs,40,w,h,1,bits,0,w*h*sizeof pix


  'FILL IN PIXELS
  '==============

  scope
 
  indexbase 0
  int x,y
  for y=1 to 14
     for x=1 to 14
       pix(y*w+x)<=0,160,128 ' b g r
    next
  next

  end scope   
  

  'SAVE BMP FILE
  '=============

  string fname="s.bmp"
  putfile fname,BmpData
  print "Created: " fname


  '====
  done:
  '====

  BmpData=""


'=====
'NOTES
'=====

'----------------------------------------------------------------------------------------------------------
' BMP file formats
'
' The BITMAPFILEHEADER:
'----------------------------------------------------------------------------------------------------------
'start size name           description
'----------------------------------------------------------------------------------------------------------
'0     2    bfType         must always be set to 'BM' to declare that this is a .bmp-file.
'2     4    bfSize         the size of the file in bytes.
'4     2    bfReserved1    must always be set to zero.
'8     2    bfReserved2    must always be set to zero.
'10    4    bfOffBits      the offset from the beginning of the file to the bitmap data.
'----------------------------------------------------------------------------------------------------------
'
'
' The BITMAPINFOHEADER:
'----------------------------------------------------------------------------------------------------------
'start size name            description
'----------------------------------------------------------------------------------------------------------
'14    4    biSize          size of the BITMAPINFOHEADER structure, in bytes.
'18    4    biWidth         width of the image, in pixels.
'22    4    biHeight        height of the image, in pixels.
'26    2    biPlanes        number of planes of the target device, must be set to zero.
'28    2    biBitCount      number of bits per pixel.
'30    4    biCompression   type of compression, usually set to zero (no compression).
'34    4    biSizeImage     size of the image data, in bytes. If there is no compression, it is valid to set this member to zero.
'38    4    biXPelsPerMeter horizontal pixels per meter on the designated targer device, usually set to zero.
'42    4    biYPelsPerMeter vertical pixels per meter on the designated targer device, usually set to zero.
'46    4    biClrUsed       number of colors used in the bitmap, if set to zero the number of colors is calculated using the biBitCount member.
'50    4    biClrImportant  number of color that are 'important' for the bitmap, if set to zero, all colors are important.
'----------------------------------------------------------------------------------------------------------
 
