Java ByteBuffer Write writeHeaderAndLsd(ByteBuffer out, int width, int height, boolean hasGct, int gctSize)

Here you can find the source of writeHeaderAndLsd(ByteBuffer out, int width, int height, boolean hasGct, int gctSize)

Description

write Header And Lsd

License

Open Source License

Declaration

public static void writeHeaderAndLsd(ByteBuffer out, int width,
            int height, boolean hasGct, int gctSize) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.nio.ByteBuffer;

public class Main {
    public static final int HEADER_LENGTH = 13;

    public static void writeHeaderAndLsd(ByteBuffer out, int width,
            int height, boolean hasGct, int gctSize) {
        verifyRemaining(out, HEADER_LENGTH);
        verifyShortValues(width, height);

        // GIF/*from   w  w  w. j  a  va2 s . com*/
        out.put((byte) 0x47).put((byte) 0x49).put((byte) 0x46);
        // Version - 89a.
        out.put((byte) 0x38).put((byte) 0x39).put((byte) 0x61);

        /** LSD (Logical Screen Descriptor) **/
        // Width.
        out.putShort((short) width);
        // Height.
        out.putShort((short) height);
        // Packed GCT (Global Color Table) flag + color resolution + sort flag + size of GCT.
        // GCT flag (false) - most significant bit.
        byte gctFlag = (byte) ((hasGct ? 1 : 0) << 7);
        // Color resolution - next three bits.
        byte colorResolution = 1 << 5;
        // Sort flag - next bit;
        byte sortFlag = 0 << 4;
        // exponent of size of color table, size = 2^(1 + exponent) - least significant 3 bits.
        byte size = (byte) gctSize;

        byte packed = (byte) (gctFlag | colorResolution | sortFlag | size);
        out.put(packed);

        // Background color index.
        out.put((byte) 0);

        // Pixel aspect ratio.
        out.put((byte) 0);
    }

    private static void verifyRemaining(ByteBuffer buffer, int expected) {
        if (buffer.remaining() < expected) {
            throw new IllegalArgumentException("Must have at least "
                    + expected + " bytes to write");
        }
    }

    private static void verifyShortValues(int... shortValues) {
        for (int dimen : shortValues) {
            if (dimen > Short.MAX_VALUE || dimen < 0) {
                throw new IllegalArgumentException(
                        "Must pass in non-negative short dimensions, not: "
                                + dimen);
            }
        }
    }
}

Related

  1. writeFully(final WritableByteChannel channel, final ByteBuffer buf)
  2. writeFully(WritableByteChannel bc, ByteBuffer buf)
  3. writeFully(WritableByteChannel channel, ByteBuffer[] srcs)
  4. writeHalf(final ByteBuffer buf, final int value)
  5. writeHeader(int type, ByteBuffer lengthBuffer, ByteBuffer buffer)
  6. writeHexString(ByteBuffer buffer, String hex)
  7. writeHexString(ByteBuffer buffer, String hexStr)
  8. writeImageDescriptor(ByteBuffer out, int imageLeft, int imageTop, int imageWidth, int imageHeight, boolean hasLct, int numColors)
  9. writeInt(ByteBuffer buf, int pos, int v)