Class WriteBMP32


  • public class WriteBMP32
    extends java.lang.Object
    For writing 32 bit BMP (imageio won't), credit to Ian McDonagh
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static WriteBMP32.InfoHeader createInfoHeader​(java.awt.image.BufferedImage img)
      Creates an InfoHeader from the source image.
      static int getBitmapSize​(int w, int h, int bpp)
      Calculates the size in bytes of a bitmap with the specified size and colour depth.
      static int getBytesPerLine1​(int width)
      Calculates the number of bytes per line required for the given width in pixels, for a 1-bit bitmap.
      static int getBytesPerLine24​(int width)
      Calculates the number of bytes per line required for the given with in pixels, for a 24-bit bitmap.
      static int getBytesPerLine4​(int width)
      Calculates the number of bytes per line required for the given with in pixels, for a 4-bit bitmap.
      static int getBytesPerLine8​(int width)
      Calculates the number of bytes per line required for the given with in pixels, for a 8-bit bitmap.
      static int getColorMapSize​(short sBitCount)
      Calculates the size in bytes for a colour map with the specified bit count.
      static void write​(java.awt.image.BufferedImage img, java.io.File file)
      Encodes and writes BMP data the output file
      static void write​(java.awt.image.BufferedImage img, java.io.OutputStream os)
      Encodes and writes BMP data to the output
      static void write1​(java.awt.image.Raster raster, WriteBMP32.LittleEndianOutputStream out)
      Encodes and writes raster data as a 1-bit bitmap.
      static void write24​(java.awt.image.Raster raster, WriteBMP32.LittleEndianOutputStream out)
      Encodes and writes raster data as a 24-bit bitmap.
      static void write32​(java.awt.image.Raster raster, java.awt.image.Raster alpha, WriteBMP32.LittleEndianOutputStream out)
      Encodes and writes raster data, together with alpha (transparency) data, as a 32-bit bitmap.
      static void write4​(java.awt.image.Raster raster, WriteBMP32.LittleEndianOutputStream out)
      Encodes and writes raster data as a 4-bit bitmap.
      static void write8​(java.awt.image.Raster raster, WriteBMP32.LittleEndianOutputStream out)
      Encodes and writes raster data as an 8-bit bitmap.
      static void writeColorMap​(java.awt.image.IndexColorModel icm, WriteBMP32.LittleEndianOutputStream out)
      Writes the colour map resulting from the source IndexColorModel.
      static void writeFileHeader​(int fileSize, int dataOffset, WriteBMP32.LittleEndianOutputStream out)
      Writes the file header.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • write

        public static void write​(java.awt.image.BufferedImage img,
                                 java.io.File file)
                          throws java.io.IOException
        Encodes and writes BMP data the output file
        Parameters:
        img - the image to encode
        file - the file to which encoded data will be written
        Throws:
        java.io.IOException - if an error occurs
      • write

        public static void write​(java.awt.image.BufferedImage img,
                                 java.io.OutputStream os)
                          throws java.io.IOException
        Encodes and writes BMP data to the output
        Parameters:
        img - the image to encode
        os - the output to which encoded data will be written
        Throws:
        java.io.IOException - if an error occurs
      • createInfoHeader

        public static WriteBMP32.InfoHeader createInfoHeader​(java.awt.image.BufferedImage img)
        Creates an InfoHeader from the source image.
        Parameters:
        img - the source image
        Returns:
        the resultant InfoHeader structure
      • writeFileHeader

        public static void writeFileHeader​(int fileSize,
                                           int dataOffset,
                                           WriteBMP32.LittleEndianOutputStream out)
                                    throws java.io.IOException
        Writes the file header.
        Parameters:
        fileSize - the calculated file size for the BMP data being written
        dataOffset - the calculated offset within the BMP data where the actual bitmap begins
        out - the output to which the file header will be written
        Throws:
        java.io.IOException - if an error occurs
      • writeColorMap

        public static void writeColorMap​(java.awt.image.IndexColorModel icm,
                                         WriteBMP32.LittleEndianOutputStream out)
                                  throws java.io.IOException
        Writes the colour map resulting from the source IndexColorModel.
        Parameters:
        icm - the source IndexColorModel
        out - the output to which the colour map will be written
        Throws:
        java.io.IOException - if an error occurs
      • getBytesPerLine1

        public static int getBytesPerLine1​(int width)
        Calculates the number of bytes per line required for the given width in pixels, for a 1-bit bitmap. Lines are always padded to the next 4-byte boundary.
        Parameters:
        width - the width in pixels
        Returns:
        the number of bytes per line
      • getBytesPerLine4

        public static int getBytesPerLine4​(int width)
        Calculates the number of bytes per line required for the given with in pixels, for a 4-bit bitmap. Lines are always padded to the next 4-byte boundary.
        Parameters:
        width - the width in pixels
        Returns:
        the number of bytes per line
      • getBytesPerLine8

        public static int getBytesPerLine8​(int width)
        Calculates the number of bytes per line required for the given with in pixels, for a 8-bit bitmap. Lines are always padded to the next 4-byte boundary.
        Parameters:
        width - the width in pixels
        Returns:
        the number of bytes per line
      • getBytesPerLine24

        public static int getBytesPerLine24​(int width)
        Calculates the number of bytes per line required for the given with in pixels, for a 24-bit bitmap. Lines are always padded to the next 4-byte boundary.
        Parameters:
        width - the width in pixels
        Returns:
        the number of bytes per line
      • getBitmapSize

        public static int getBitmapSize​(int w,
                                        int h,
                                        int bpp)
        Calculates the size in bytes of a bitmap with the specified size and colour depth.
        Parameters:
        w - the width in pixels
        h - the height in pixels
        bpp - the colour depth (bits per pixel)
        Returns:
        the size of the bitmap in bytes
      • write1

        public static void write1​(java.awt.image.Raster raster,
                                  WriteBMP32.LittleEndianOutputStream out)
                           throws java.io.IOException
        Encodes and writes raster data as a 1-bit bitmap.
        Parameters:
        raster - the source raster data
        out - the output to which the bitmap will be written
        Throws:
        java.io.IOException - if an error occurs
      • write4

        public static void write4​(java.awt.image.Raster raster,
                                  WriteBMP32.LittleEndianOutputStream out)
                           throws java.io.IOException
        Encodes and writes raster data as a 4-bit bitmap.
        Parameters:
        raster - the source raster data
        out - the output to which the bitmap will be written
        Throws:
        java.io.IOException - if an error occurs
      • write8

        public static void write8​(java.awt.image.Raster raster,
                                  WriteBMP32.LittleEndianOutputStream out)
                           throws java.io.IOException
        Encodes and writes raster data as an 8-bit bitmap.
        Parameters:
        raster - the source raster data
        out - the output to which the bitmap will be written
        Throws:
        java.io.IOException - if an error occurs
      • write24

        public static void write24​(java.awt.image.Raster raster,
                                   WriteBMP32.LittleEndianOutputStream out)
                            throws java.io.IOException
        Encodes and writes raster data as a 24-bit bitmap.
        Parameters:
        raster - the source raster data
        out - the output to which the bitmap will be written
        Throws:
        java.io.IOException - if an error occurs
      • write32

        public static void write32​(java.awt.image.Raster raster,
                                   java.awt.image.Raster alpha,
                                   WriteBMP32.LittleEndianOutputStream out)
                            throws java.io.IOException
        Encodes and writes raster data, together with alpha (transparency) data, as a 32-bit bitmap.
        Parameters:
        raster - the source raster data
        alpha - the source alpha data
        out - the output to which the bitmap will be written
        Throws:
        java.io.IOException - if an error occurs
      • getColorMapSize

        public static int getColorMapSize​(short sBitCount)
        Calculates the size in bytes for a colour map with the specified bit count.
        Parameters:
        sBitCount - the bit count, which represents the colour depth
        Returns:
        the size of the colour map, in bytes if sBitCount is less than or equal to 8, otherwise 0 as colour maps are only used for bitmaps with a colour depth of 8 bits or less.