base/common/src/main/com/thesett/common/swing/surface/Surface.java
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);
    }
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.

@param  w         The image width.
@param  h         The image height.
@param  pixelBits The number of grey-scale bits to use. Must be 1, 2 or 4.

@return A buffered image of the specifed size and type.