private void WriteXorBitmap(BufferedImage img)
 {
     Raster raster = img.getRaster();
     switch (img.getColorModel().getPixelSize())
     {
         case 1:
             Write1(raster);
             break;
         case 4:
             Write4(raster);
             break;
         case 8:
             Write8(raster);
             break;
         case 24:
             Write24(raster);
             break;
         case 32:
             Raster alpha = img.getAlphaRaster();
             Write32(raster, alpha);
             break;
     }
 }
        /// <summary>
        /// Encodes the <em>AND</em> bitmap for the given image according the its
        /// alpha channel (transparency) and writes it to the given output.
        /// </summary>
        /// <param name="img">
        ///            the image to encode as the <em>AND</em> bitmap. </param>
        private void WriteAndBitmap(BufferedImage img)
        {
            WritableRaster alpha = img.getAlphaRaster();

            // indexed transparency (eg. GIF files)
            if (img.getColorModel() is IndexColorModel && img.getColorModel().hasAlpha())
            {
                int w = img.getWidth();
                int h = img.getHeight();

                int bytesPerLine = GetBytesPerLine1(w);

                byte[] line = new byte[bytesPerLine];

                IndexColorModel icm = (IndexColorModel)img.getColorModel();
                Raster raster = img.getRaster();

                for (int y = h - 1; y >= 0; y--)
                {
                    for (int x = 0; x < w; x++)
                    {
                        int bi = x / 8;
                        int i = x % 8;
                        // int a = alpha.getSample(x, y, 0);
                        int p = raster.getSample(x, y, 0);
                        int a = icm.getAlpha(p);
                        // invert bit since and mask is applied to xor mask
                        int b = ~a & 1;
                        line[bi] = SetBit(line[bi], i, b);
                    }

                    writer.Write(line);
                }
            }
            // no transparency
            else if (alpha == null)
            {
                int h = img.getHeight();
                int w = img.getWidth();
                // calculate number of bytes per line, including 32-bit padding
                int bytesPerLine = GetBytesPerLine1(w);

                byte[] line = new byte[bytesPerLine];
                for (int i = 0; i < bytesPerLine; i++)
                {
                    line[i] = (byte)0;
                }

                for (int y = h - 1; y >= 0; y--)
                {
                    writer.Write(line);
                }
            }
            // transparency (ARGB, etc. eg. PNG)
            else
            {
                int w = img.getWidth();
                int h = img.getHeight();

                int bytesPerLine = GetBytesPerLine1(w);

                byte[] line = new byte[bytesPerLine];

                for (int y = h - 1; y >= 0; y--)
                {
                    for (int x = 0; x < w; x++)
                    {
                        int bi = x / 8;
                        int i = x % 8;
                        int a = alpha.getSample(x, y, 0);
                        // invert bit since and mask is applied to xor mask
                        int b = ~a & 1;
                        line[bi] = SetBit(line[bi], i, b);
                    }

                    writer.Write(line);
                }
            }
        }