home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch13 / quake / BMP.java < prev    next >
Text File  |  1996-11-19  |  2KB  |  45 lines

  1. // BMP file writer
  2.  
  3. // Written by Bernie Roehl, November 1996
  4.  
  5. package quake;
  6.  
  7. import java.io.*;
  8.  
  9. public class BMP {
  10.         static public void dump(String filename, byte[] picture, int width, int height, byte[] palette)
  11.                 throws IOException {
  12.                 DataOutputStream out = new DataOutputStream(new FileOutputStream(filename));
  13.                 // write out file header
  14.                 out.writeByte('B');
  15.                 out.writeByte('M');
  16.                 // total size is sizeof file header + sizeof bmp header + sizeof palette + bits
  17.                 out.writeInt(swap32(14 + 40 + 1024 + width * height));  // total size
  18.                 out.writeInt(0);
  19.                 out.writeInt(swap32(14 + 40 + 1024));  // offset to bits
  20.                 // write out BMP header
  21.                 out.writeInt(swap32(40));      // size of bitmap header
  22.                 out.writeInt(swap32(width));   // width
  23.                 out.writeInt(swap32(height));  // height
  24.                 out.writeInt(0x01000800);
  25.                                                // next few are zero for defaults
  26.                 out.writeInt(0);               // compression type
  27.                 out.writeInt(0);               // size of the bitmap in bytes
  28.                 out.writeInt(0);               // horizontal resolution
  29.                 out.writeInt(0);               // vertical resolution
  30.                 out.writeInt(0);               // number of colors in image
  31.                 out.writeInt(0);               // number of important colors
  32.                 out.write(palette);            // write out palette
  33.                 out.write(picture);            // write out bits
  34.         }
  35.  
  36.         static int swap32(int value) {
  37.                 return
  38.                         ((value >> 24) & 0x000000FF) +
  39.                         ((value >> 8)  & 0x0000FF00) +
  40.                         ((value << 8)  & 0x00FF0000) +
  41.                         ((value << 24) & 0xFF000000);
  42.         }
  43. }
  44.  
  45.