private BufferedImage createBinaryImage ( int w , int h , int pixelBits ) { int bytesPerRow = w * pixelBits / 8 ; if ( ( w * pixelBits % 8 ) != 0 ) { bytesPerRow ++ ; } byte [ ] imageData = new byte [ h * bytesPerRow ] ; IndexColorModel cm = null ; switch ( pixelBits ) { case 1 : { cm = new IndexColorModel ( pixelBits , lut1Arr . length , lut1Arr , lut1Arr , lut1Arr ) ; break ; } case 2 : { cm = new IndexColorModel ( pixelBits , lut2Arr . length , lut2Arr , lut2Arr , lut2Arr ) ; break ; } case 4 : { cm = new IndexColorModel ( pixelBits , lut4Arr . length , lut4Arr , lut4Arr , lut4Arr ) ; break ; } default : { new Exception ( "Invalid # of bit per pixel" ) . printStackTrace ( ) ; } } DataBuffer db = new DataBufferByte ( imageData , imageData . length ) ; WritableRaster r = Raster . createPackedRaster ( db , w , h , pixelBits , null ) ; return new BufferedImage ( cm , r , false , null ) ; }
Creates a custom grey - scale binary image format .