Java ByteBuffer Write writeImageDescriptor(ByteBuffer out, int imageLeft, int imageTop, int imageWidth, int imageHeight, boolean hasLct, int numColors)

Here you can find the source of writeImageDescriptor(ByteBuffer out, int imageLeft, int imageTop, int imageWidth, int imageHeight, boolean hasLct, int numColors)

Description

write Image Descriptor

License

Open Source License

Declaration

public static void writeImageDescriptor(ByteBuffer out, int imageLeft,
            int imageTop, int imageWidth, int imageHeight, boolean hasLct,
            int numColors) 

Method Source Code

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

import java.nio.ByteBuffer;

public class Main {
    public static final int IMAGE_DESCRIPTOR_LENGTH = 10;

    public static void writeImageDescriptor(ByteBuffer out, int imageLeft,
            int imageTop, int imageWidth, int imageHeight, boolean hasLct,
            int numColors) {
        verifyRemaining(out, IMAGE_DESCRIPTOR_LENGTH);
        verifyShortValues(imageLeft, imageTop, imageWidth, imageHeight);

        final byte packed;
        if (hasLct) {
            int size = log2(numColors) - 1;
            packed = (byte) (0x80 | size);
        } else {/*w ww . j  a v  a2  s  . c o  m*/
            packed = 0x00;
        }

        // Image separator
        out.put((byte) 0x2C);
        out.putShort((short) imageLeft).putShort((short) imageTop)
                .putShort((short) imageWidth).putShort((short) imageHeight)
                .put(packed);
    }

    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);
            }
        }
    }

    private static int log2(int num) {
        return (int) Math.round(Math.log(num) / Math.log(2));
    }
}

Related

  1. writeHalf(final ByteBuffer buf, final int value)
  2. writeHeader(int type, ByteBuffer lengthBuffer, ByteBuffer buffer)
  3. writeHeaderAndLsd(ByteBuffer out, int width, int height, boolean hasGct, int gctSize)
  4. writeHexString(ByteBuffer buffer, String hex)
  5. writeHexString(ByteBuffer buffer, String hexStr)
  6. writeInt(ByteBuffer buf, int pos, int v)
  7. writeInt(ByteBuffer logBuf, int i)
  8. writeInt24(int v, ByteBuffer buffer)
  9. writeInt4(final ByteBuffer bb, int value, int highValue, final boolean flush)