private void writeHeader( String version, int logicalScreenWidth, int logicalScreenHeight, int colorResolution, int pixelAspectRatio, int backgroundColorIndex, boolean sortFlag, int bitsPerPixel, byte[] globalColorTable) throws IOException { try { // Signature stream.writeBytes("GIF" + version); // Screen Descriptor // Width stream.writeShort((short) logicalScreenWidth); // Height stream.writeShort((short) logicalScreenHeight); // Global Color Table // Packed fields int packedFields = globalColorTable != null ? 0x80 : 0x00; packedFields |= ((colorResolution - 1) & 0x7) << 4; if (sortFlag) { packedFields |= 0x8; } packedFields |= (bitsPerPixel - 1); stream.write(packedFields); // Background color index stream.write(backgroundColorIndex); // Pixel aspect ratio stream.write(pixelAspectRatio); // Global Color Table if (globalColorTable != null) { stream.write(globalColorTable); } } catch (IOException e) { throw new IIOException("I/O error writing header!", e); } }