You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2010/09/10 18:33:42 UTC
svn commit: r995859 [6/30] - in
/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan: ./ color/
common/ common/byteSources/ common/mylzw/ formats/bmp/
formats/bmp/pixelparsers/ formats/bmp/writers/ formats/gif/ formats/ico/
formats/jpeg/ fo...
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/BinaryInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/BinaryInputStream.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/BinaryInputStream.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/BinaryInputStream.java Fri Sep 10 16:33:35 2010
@@ -25,664 +25,664 @@ import org.apache.sanselan.ImageReadExce
public class BinaryInputStream extends InputStream implements BinaryConstants
{
- protected boolean debug = false;
+ protected boolean debug = false;
- public final void setDebug(boolean b)
- {
- debug = b;
- }
-
- public final boolean getDebug()
- {
- return debug;
- }
-
- private final InputStream is;
-
- public BinaryInputStream(byte bytes[], int byteOrder)
- {
- this.byteOrder = byteOrder;
- this.is = new ByteArrayInputStream(bytes);
- }
-
- public BinaryInputStream(InputStream is, int byteOrder)
- {
- this.byteOrder = byteOrder;
- this.is = is;
- }
-
- public BinaryInputStream(InputStream is)
- {
- this.is = is;
- }
-
- // default byte order for Java, many file formats.
- private int byteOrder = BYTE_ORDER_NETWORK;
-
- protected void setByteOrder(int a, int b) throws ImageReadException,
- IOException
- {
- if (a != b)
- throw new ImageReadException("Byte Order bytes don't match (" + a
- + ", " + b + ").");
-
- if (a == BYTE_ORDER_MOTOROLA)
- byteOrder = a;
- else if (a == BYTE_ORDER_INTEL)
- byteOrder = a;
- else
- throw new ImageReadException("Unknown Byte Order hint: " + a);
- }
-
- protected void setByteOrder(int byteOrder)
- {
- this.byteOrder = byteOrder;
- }
-
- protected int getByteOrder()
- {
- return byteOrder;
- }
-
- public int read() throws IOException
- {
- return is.read();
- }
-
- protected final int convertByteArrayToInt(String name, byte bytes[])
- {
- return convertByteArrayToInt(name, bytes, byteOrder);
- }
-
- public final int convertByteArrayToShort(String name, byte bytes[])
- {
- return convertByteArrayToShort(name, bytes, byteOrder);
- }
-
- public final int convertByteArrayToShort(String name, int start,
- byte bytes[])
- {
- return convertByteArrayToShort(name, start, bytes, byteOrder);
- }
-
- public final int read4Bytes(String name, String exception)
- throws ImageReadException, IOException
- {
- return read4Bytes(name, exception, byteOrder);
- }
-
- public final int read3Bytes(String name, String exception)
- throws ImageReadException, IOException
- {
- return read3Bytes(name, exception, byteOrder);
- }
-
- public final int read2Bytes(String name, String exception)
- throws ImageReadException, IOException
- {
- return read2Bytes(name, exception, byteOrder);
- }
-
- protected final void readRandomBytes() throws ImageReadException,
- IOException
- {
-
- for (int counter = 0; counter < 100; counter++)
- {
- readByte("" + counter, "Random Data");
- }
- }
-
- public final void debugNumber(String msg, int data)
- {
- debugNumber(msg, data, 1);
- }
-
- public final void debugNumber(String msg, int data, int bytes)
- {
- System.out.print(msg + ": " + data + " (");
- int byteData = data;
- for (int i = 0; i < bytes; i++)
- {
- if (i > 0)
- System.out.print(",");
- int singleByte = 0xff & byteData;
- System.out.print((char) singleByte + " [" + singleByte + "]");
- byteData >>= 8;
- }
- System.out.println(") [0x" + Integer.toHexString(data) + ", "
- + Integer.toBinaryString(data) + "]");
- }
-
- public final void readAndVerifyBytes(byte expected[], String exception)
- throws ImageReadException, IOException
- {
- for (int i = 0; i < expected.length; i++)
- {
- int data = is.read();
- byte b = (byte) (0xff & data);
-
- if ((data < 0) || (b != expected[i]))
- {
- System.out.println("i" + ": " + i);
-
- this.debugByteArray("expected", expected);
- debugNumber("data[" + i + "]", b);
- // debugNumber("expected[" + i + "]", expected[i]);
-
- throw new ImageReadException(exception);
- }
- }
- }
-
- protected final void readAndVerifyBytes(String name, byte expected[],
- String exception) throws ImageReadException, IOException
- {
- byte bytes[] = readByteArray(name, expected.length, exception);
-
- for (int i = 0; i < expected.length; i++)
- {
- if (bytes[i] != expected[i])
- {
- System.out.println("i" + ": " + i);
- debugNumber("bytes[" + i + "]", bytes[i]);
- debugNumber("expected[" + i + "]", expected[i]);
-
- throw new ImageReadException(exception);
- }
- }
- }
-
- public final void skipBytes(int length, String exception)
- throws IOException
- {
- long total = 0;
- while (length != total)
- {
- long skipped = is.skip(length - total);
- if (skipped < 1)
- throw new IOException(exception + " (" + skipped + ")");
- total += skipped;
- }
- }
-
- protected final void scanForByte(byte value) throws IOException
- {
- int count = 0;
- for (int i = 0; count < 3; i++)
- // while(count<3)
- {
- int b = is.read();
- if (b < 0)
- return;
- if ((0xff & b) == value)
- {
- System.out.println("\t" + i + ": match.");
- count++;
- }
- }
- }
-
- public final byte readByte(String name, String exception)
- throws IOException
- {
- int result = is.read();
-
- if ((result < 0))
- {
- System.out.println(name + ": " + result);
- throw new IOException(exception);
- }
-
- if (debug)
- debugNumber(name, result);
-
- return (byte) (0xff & result);
- }
-
- protected final RationalNumber[] convertByteArrayToRationalArray(
- String name, byte bytes[], int start, int length, int byteOrder)
- {
- int expectedLength = start + length * 8;
-
- if (bytes.length < expectedLength)
- {
- System.out.println(name + ": expected length: " + expectedLength
- + ", actual length: " + bytes.length);
- return null;
- }
-
- RationalNumber result[] = new RationalNumber[length];
-
- for (int i = 0; i < length; i++)
- {
- result[i] = convertByteArrayToRational(name, bytes, start + i * 8,
- byteOrder);
- }
-
- return result;
- }
-
- protected final RationalNumber convertByteArrayToRational(String name,
- byte bytes[], int byteOrder)
- {
- return convertByteArrayToRational(name, bytes, 0, byteOrder);
- }
-
- protected final RationalNumber convertByteArrayToRational(String name,
- byte bytes[], int start, int byteOrder)
- {
- int numerator = convertByteArrayToInt(name, bytes, start + 0, 4,
- byteOrder);
- int divisor = convertByteArrayToInt(name, bytes, start + 4, 4,
- byteOrder);
-
- return new RationalNumber(numerator, divisor);
- }
-
- protected final int convertByteArrayToInt(String name, byte bytes[],
- int byteOrder)
- {
- return convertByteArrayToInt(name, bytes, 0, 4, byteOrder);
- }
-
- protected final int convertByteArrayToInt(String name, byte bytes[],
- int start, int length, int byteOrder)
- {
- byte byte0 = bytes[start + 0];
- byte byte1 = bytes[start + 1];
- byte byte2 = bytes[start + 2];
- byte byte3 = 0;
- if (length == 4)
- byte3 = bytes[start + 3];
-
- // return convert4BytesToInt(name, byte0, byte1, byte2, byte3,
- // byteOrder);
-
- int result;
-
- if (byteOrder == BYTE_ORDER_MOTOROLA) // motorola, big endian
- result = ((0xff & byte0) << 24) + ((0xff & byte1) << 16)
- + ((0xff & byte2) << 8) + ((0xff & byte3) << 0);
- // result = (( byte0) << 24) + ((byte1) << 16)
- // + (( byte2) << 8) + (( byte3) << 0);
- else
- // intel, little endian
- result = ((0xff & byte3) << 24) + ((0xff & byte2) << 16)
- + ((0xff & byte1) << 8) + ((0xff & byte0) << 0);
- // result = (( byte3) << 24) + (( byte2) << 16)
- // + (( byte1) << 8) + (( byte0) << 0);
-
- if (debug)
- debugNumber(name, result, 4);
-
- return result;
- }
-
- protected final int[] convertByteArrayToIntArray(String name, byte bytes[],
- int start, int length, int byteOrder)
- {
- int expectedLength = start + length * 4;
-
- if (bytes.length < expectedLength)
- {
- System.out.println(name + ": expected length: " + expectedLength
- + ", actual length: " + bytes.length);
- return null;
- }
-
- int result[] = new int[length];
-
- for (int i = 0; i < length; i++)
- {
- result[i] = convertByteArrayToInt(name, bytes, start + i * 4, 4,
- byteOrder);
- }
-
- return result;
- }
-
- protected final int convertByteArrayToShort(String name, byte bytes[],
- int byteOrder)
- {
- return convertByteArrayToShort(name, 0, bytes, byteOrder);
- }
-
- protected final int convertByteArrayToShort(String name, int start,
- byte bytes[], int byteOrder)
- {
- byte byte0 = bytes[start + 0];
- byte byte1 = bytes[start + 1];
-
- // return convert2BytesToShort(name, byte0, byte1, byteOrder);
-
- int result;
-
- if (byteOrder == BYTE_ORDER_MOTOROLA) // motorola, big endian
- result = ((0xff & byte0) << 8) + ((0xff & byte1) << 0);
- else
- // intel, little endian
- result = ((0xff & byte1) << 8) + ((0xff & byte0) << 0);
-
- if (debug)
- debugNumber(name, result, 2);
-
- return result;
- }
-
- protected final int[] convertByteArrayToShortArray(String name,
- byte bytes[], int start, int length, int byteOrder)
- {
- int expectedLength = start + length * 2;
-
- if (bytes.length < expectedLength)
- {
- System.out.println(name + ": expected length: " + expectedLength
- + ", actual length: " + bytes.length);
- return null;
- }
-
- int result[] = new int[length];
-
- for (int i = 0; i < length; i++)
- {
- result[i] = convertByteArrayToShort(name, start + i * 2, bytes,
- byteOrder);
-
- // byte byte0 = bytes[start + i * 2];
- // byte byte1 = bytes[start + i * 2 + 1];
- // result[i] = convertBytesToShort(name, byte0, byte1, byteOrder);
- }
-
- return result;
- }
-
- public final byte[] readByteArray(String name, int length, String exception)
- throws ImageReadException, IOException
- {
- byte result[] = new byte[length];
-
- int read = 0;
- while (read < length)
- {
- int count = is.read(result, read, length - read);
- if (count < 1)
- throw new IOException(exception);
-
- read += count;
- }
-
- if (debug)
- {
- for (int i = 0; ((i < length) && (i < 150)); i++)
- {
- debugNumber(name + " (" + i + ")", 0xff & result[i]);
- }
- }
- return result;
- }
-
- protected final void debugByteArray(String name, byte bytes[])
- {
- System.out.println(name + ": " + bytes.length);
-
- for (int i = 0; ((i < bytes.length) && (i < 50)); i++)
- {
- debugNumber(name + " (" + i + ")", bytes[i]);
- }
- }
-
- protected final void debugNumberArray(String name, int numbers[], int length)
- {
- System.out.println(name + ": " + numbers.length);
-
- for (int i = 0; ((i < numbers.length) && (i < 50)); i++)
- {
- debugNumber(name + " (" + i + ")", numbers[i], length);
- }
- }
-
- public final byte[] readBytearray(String name, byte bytes[], int start,
- int count)
- {
- if (bytes.length < (start + count))
- return null;
-
- byte result[] = new byte[count];
- System.arraycopy(bytes, start, result, 0, count);
-
- if (debug)
- debugByteArray(name, result);
-
- return result;
- }
-
- public final byte[] readByteArray(int length, String error)
- throws ImageReadException, IOException
- {
- boolean verbose = false;
- boolean strict = true;
- return readByteArray(length, error, verbose, strict);
- }
-
- public final byte[] readByteArray(int length, String error,
- boolean verbose, boolean strict) throws ImageReadException,
- IOException
- {
- byte bytes[] = new byte[length];
- int total = 0;
- int read;
- while ((read = read(bytes, total, length - total)) > 0)
- total += read;
- if (total < length)
- {
- if (strict)
- throw new ImageReadException(error);
- else if(verbose)
- System.out.println(error);
- return null;
- }
- return bytes;
- }
-
- protected final byte[] getBytearrayTail(String name, byte bytes[], int count)
- {
- return readBytearray(name, bytes, count, bytes.length - count);
- }
-
- protected final byte[] getBytearrayHead(String name, byte bytes[], int count)
- {
- return readBytearray(name, bytes, 0, bytes.length - count);
- }
-
- public final boolean compareByteArrays(byte a[], int aStart, byte b[],
- int bStart, int length)
- {
- if (a.length < (aStart + length))
- return false;
- if (b.length < (bStart + length))
- return false;
-
- for (int i = 0; i < length; i++)
- {
- if (a[aStart + i] != b[bStart + i])
- {
- debugNumber("a[" + (aStart + i) + "]", a[aStart + i]);
- debugNumber("b[" + (bStart + i) + "]", b[bStart + i]);
-
- return false;
- }
- }
-
- return true;
- }
-
- protected final int read4Bytes(String name, String exception, int byteOrder)
- throws ImageReadException, IOException
- {
- int size = 4;
- byte bytes[] = new byte[size];
-
- int read = 0;
- while (read < size)
- {
- int count = is.read(bytes, read, size - read);
- if (count < 1)
- throw new IOException(exception);
-
- read += count;
- }
-
- return convertByteArrayToInt(name, bytes, byteOrder);
- }
-
- protected final int read3Bytes(String name, String exception, int byteOrder)
- throws ImageReadException, IOException
- {
- int size = 3;
- byte bytes[] = new byte[size];
-
- int read = 0;
- while (read < size)
- {
- int count = is.read(bytes, read, size - read);
- if (count < 1)
- throw new IOException(exception);
-
- read += count;
- }
-
- return convertByteArrayToInt(name, bytes, 0, 3, byteOrder);
-
- }
-
- protected final int read2Bytes(String name, String exception, int byteOrder)
- throws ImageReadException, IOException
- {
- int size = 2;
- byte bytes[] = new byte[size];
-
- int read = 0;
- while (read < size)
- {
- int count = is.read(bytes, read, size - read);
- if (count < 1)
- throw new IOException(exception);
-
- read += count;
- }
-
- return convertByteArrayToShort(name, bytes, byteOrder);
- }
-
- public final int read1ByteInteger(String exception)
- throws ImageReadException, IOException
- {
- int byte0 = is.read();
- if (byte0 < 0)
- throw new ImageReadException(exception);
-
- return 0xff & byte0;
- }
-
- public final int read2ByteInteger(String exception)
- throws ImageReadException, IOException
- {
- int byte0 = is.read();
- int byte1 = is.read();
- if (byte0 < 0 || byte1 < 0)
- throw new ImageReadException(exception);
-
- if (byteOrder == BYTE_ORDER_MOTOROLA) // motorola, big endian
- return ((0xff & byte0) << 8) + ((0xff & byte1) << 0);
- else
- // intel, little endian
- return ((0xff & byte1) << 8) + ((0xff & byte0) << 0);
- }
-
- public final int read4ByteInteger(String exception)
- throws ImageReadException, IOException
- {
- int byte0 = is.read();
- int byte1 = is.read();
- int byte2 = is.read();
- int byte3 = is.read();
- if (byte0 < 0 || byte1 < 0 || byte2 < 0 || byte3 < 0)
- throw new ImageReadException(exception);
-
- if (byteOrder == BYTE_ORDER_MOTOROLA) // motorola, big endian
- return ((0xff & byte0) << 24) + ((0xff & byte1) << 16)
- + ((0xff & byte2) << 8) + ((0xff & byte3) << 0);
- else
- // intel, little endian
- return ((0xff & byte3) << 24) + ((0xff & byte2) << 16)
- + ((0xff & byte1) << 8) + ((0xff & byte0) << 0);
- }
-
- protected final void printCharQuad(String msg, int i)
- {
- System.out.println(msg + ": '" + (char) (0xff & (i >> 24))
- + (char) (0xff & (i >> 16)) + (char) (0xff & (i >> 8))
- + (char) (0xff & (i >> 0)) + "'");
-
- }
-
- protected final void printByteBits(String msg, byte i)
- {
- System.out.println(msg + ": '" + Integer.toBinaryString(0xff & i));
- }
-
- protected final static int CharsToQuad(char c1, char c2, char c3, char c4)
- {
- return (((0xff & c1) << 24) | ((0xff & c2) << 16) | ((0xff & c3) << 8) | ((0xff & c4) << 0));
- }
-
- public final int findNull(byte src[])
- {
- return findNull(src, 0);
- }
-
- public final int findNull(byte src[], int start)
- {
- for (int i = start; i < src.length; i++)
- {
- if (src[i] == 0)
- return i;
-
- }
- return -1;
- }
-
- protected final byte[] getRAFBytes(RandomAccessFile raf, long pos,
- int length, String exception) throws IOException
- {
- byte result[] = new byte[length];
-
- if (debug)
- {
- System.out.println("getRAFBytes pos" + ": " + pos);
- System.out.println("getRAFBytes length" + ": " + length);
- }
-
- raf.seek(pos);
-
- int read = 0;
- while (read < length)
- {
- int count = raf.read(result, read, length - read);
- if (count < 1)
- throw new IOException(exception);
-
- read += count;
- }
-
- return result;
-
- }
-
- protected void skipBytes(int length) throws IOException
- {
- skipBytes(length, "Couldn't skip bytes");
- }
+ public final void setDebug(boolean b)
+ {
+ debug = b;
+ }
+
+ public final boolean getDebug()
+ {
+ return debug;
+ }
+
+ private final InputStream is;
+
+ public BinaryInputStream(byte bytes[], int byteOrder)
+ {
+ this.byteOrder = byteOrder;
+ this.is = new ByteArrayInputStream(bytes);
+ }
+
+ public BinaryInputStream(InputStream is, int byteOrder)
+ {
+ this.byteOrder = byteOrder;
+ this.is = is;
+ }
+
+ public BinaryInputStream(InputStream is)
+ {
+ this.is = is;
+ }
+
+ // default byte order for Java, many file formats.
+ private int byteOrder = BYTE_ORDER_NETWORK;
+
+ protected void setByteOrder(int a, int b) throws ImageReadException,
+ IOException
+ {
+ if (a != b)
+ throw new ImageReadException("Byte Order bytes don't match (" + a
+ + ", " + b + ").");
+
+ if (a == BYTE_ORDER_MOTOROLA)
+ byteOrder = a;
+ else if (a == BYTE_ORDER_INTEL)
+ byteOrder = a;
+ else
+ throw new ImageReadException("Unknown Byte Order hint: " + a);
+ }
+
+ protected void setByteOrder(int byteOrder)
+ {
+ this.byteOrder = byteOrder;
+ }
+
+ protected int getByteOrder()
+ {
+ return byteOrder;
+ }
+
+ public int read() throws IOException
+ {
+ return is.read();
+ }
+
+ protected final int convertByteArrayToInt(String name, byte bytes[])
+ {
+ return convertByteArrayToInt(name, bytes, byteOrder);
+ }
+
+ public final int convertByteArrayToShort(String name, byte bytes[])
+ {
+ return convertByteArrayToShort(name, bytes, byteOrder);
+ }
+
+ public final int convertByteArrayToShort(String name, int start,
+ byte bytes[])
+ {
+ return convertByteArrayToShort(name, start, bytes, byteOrder);
+ }
+
+ public final int read4Bytes(String name, String exception)
+ throws ImageReadException, IOException
+ {
+ return read4Bytes(name, exception, byteOrder);
+ }
+
+ public final int read3Bytes(String name, String exception)
+ throws ImageReadException, IOException
+ {
+ return read3Bytes(name, exception, byteOrder);
+ }
+
+ public final int read2Bytes(String name, String exception)
+ throws ImageReadException, IOException
+ {
+ return read2Bytes(name, exception, byteOrder);
+ }
+
+ protected final void readRandomBytes() throws ImageReadException,
+ IOException
+ {
+
+ for (int counter = 0; counter < 100; counter++)
+ {
+ readByte("" + counter, "Random Data");
+ }
+ }
+
+ public final void debugNumber(String msg, int data)
+ {
+ debugNumber(msg, data, 1);
+ }
+
+ public final void debugNumber(String msg, int data, int bytes)
+ {
+ System.out.print(msg + ": " + data + " (");
+ int byteData = data;
+ for (int i = 0; i < bytes; i++)
+ {
+ if (i > 0)
+ System.out.print(",");
+ int singleByte = 0xff & byteData;
+ System.out.print((char) singleByte + " [" + singleByte + "]");
+ byteData >>= 8;
+ }
+ System.out.println(") [0x" + Integer.toHexString(data) + ", "
+ + Integer.toBinaryString(data) + "]");
+ }
+
+ public final void readAndVerifyBytes(byte expected[], String exception)
+ throws ImageReadException, IOException
+ {
+ for (int i = 0; i < expected.length; i++)
+ {
+ int data = is.read();
+ byte b = (byte) (0xff & data);
+
+ if ((data < 0) || (b != expected[i]))
+ {
+ System.out.println("i" + ": " + i);
+
+ this.debugByteArray("expected", expected);
+ debugNumber("data[" + i + "]", b);
+ // debugNumber("expected[" + i + "]", expected[i]);
+
+ throw new ImageReadException(exception);
+ }
+ }
+ }
+
+ protected final void readAndVerifyBytes(String name, byte expected[],
+ String exception) throws ImageReadException, IOException
+ {
+ byte bytes[] = readByteArray(name, expected.length, exception);
+
+ for (int i = 0; i < expected.length; i++)
+ {
+ if (bytes[i] != expected[i])
+ {
+ System.out.println("i" + ": " + i);
+ debugNumber("bytes[" + i + "]", bytes[i]);
+ debugNumber("expected[" + i + "]", expected[i]);
+
+ throw new ImageReadException(exception);
+ }
+ }
+ }
+
+ public final void skipBytes(int length, String exception)
+ throws IOException
+ {
+ long total = 0;
+ while (length != total)
+ {
+ long skipped = is.skip(length - total);
+ if (skipped < 1)
+ throw new IOException(exception + " (" + skipped + ")");
+ total += skipped;
+ }
+ }
+
+ protected final void scanForByte(byte value) throws IOException
+ {
+ int count = 0;
+ for (int i = 0; count < 3; i++)
+ // while(count<3)
+ {
+ int b = is.read();
+ if (b < 0)
+ return;
+ if ((0xff & b) == value)
+ {
+ System.out.println("\t" + i + ": match.");
+ count++;
+ }
+ }
+ }
+
+ public final byte readByte(String name, String exception)
+ throws IOException
+ {
+ int result = is.read();
+
+ if ((result < 0))
+ {
+ System.out.println(name + ": " + result);
+ throw new IOException(exception);
+ }
+
+ if (debug)
+ debugNumber(name, result);
+
+ return (byte) (0xff & result);
+ }
+
+ protected final RationalNumber[] convertByteArrayToRationalArray(
+ String name, byte bytes[], int start, int length, int byteOrder)
+ {
+ int expectedLength = start + length * 8;
+
+ if (bytes.length < expectedLength)
+ {
+ System.out.println(name + ": expected length: " + expectedLength
+ + ", actual length: " + bytes.length);
+ return null;
+ }
+
+ RationalNumber result[] = new RationalNumber[length];
+
+ for (int i = 0; i < length; i++)
+ {
+ result[i] = convertByteArrayToRational(name, bytes, start + i * 8,
+ byteOrder);
+ }
+
+ return result;
+ }
+
+ protected final RationalNumber convertByteArrayToRational(String name,
+ byte bytes[], int byteOrder)
+ {
+ return convertByteArrayToRational(name, bytes, 0, byteOrder);
+ }
+
+ protected final RationalNumber convertByteArrayToRational(String name,
+ byte bytes[], int start, int byteOrder)
+ {
+ int numerator = convertByteArrayToInt(name, bytes, start + 0, 4,
+ byteOrder);
+ int divisor = convertByteArrayToInt(name, bytes, start + 4, 4,
+ byteOrder);
+
+ return new RationalNumber(numerator, divisor);
+ }
+
+ protected final int convertByteArrayToInt(String name, byte bytes[],
+ int byteOrder)
+ {
+ return convertByteArrayToInt(name, bytes, 0, 4, byteOrder);
+ }
+
+ protected final int convertByteArrayToInt(String name, byte bytes[],
+ int start, int length, int byteOrder)
+ {
+ byte byte0 = bytes[start + 0];
+ byte byte1 = bytes[start + 1];
+ byte byte2 = bytes[start + 2];
+ byte byte3 = 0;
+ if (length == 4)
+ byte3 = bytes[start + 3];
+
+ // return convert4BytesToInt(name, byte0, byte1, byte2, byte3,
+ // byteOrder);
+
+ int result;
+
+ if (byteOrder == BYTE_ORDER_MOTOROLA) // motorola, big endian
+ result = ((0xff & byte0) << 24) + ((0xff & byte1) << 16)
+ + ((0xff & byte2) << 8) + ((0xff & byte3) << 0);
+ // result = (( byte0) << 24) + ((byte1) << 16)
+ // + (( byte2) << 8) + (( byte3) << 0);
+ else
+ // intel, little endian
+ result = ((0xff & byte3) << 24) + ((0xff & byte2) << 16)
+ + ((0xff & byte1) << 8) + ((0xff & byte0) << 0);
+ // result = (( byte3) << 24) + (( byte2) << 16)
+ // + (( byte1) << 8) + (( byte0) << 0);
+
+ if (debug)
+ debugNumber(name, result, 4);
+
+ return result;
+ }
+
+ protected final int[] convertByteArrayToIntArray(String name, byte bytes[],
+ int start, int length, int byteOrder)
+ {
+ int expectedLength = start + length * 4;
+
+ if (bytes.length < expectedLength)
+ {
+ System.out.println(name + ": expected length: " + expectedLength
+ + ", actual length: " + bytes.length);
+ return null;
+ }
+
+ int result[] = new int[length];
+
+ for (int i = 0; i < length; i++)
+ {
+ result[i] = convertByteArrayToInt(name, bytes, start + i * 4, 4,
+ byteOrder);
+ }
+
+ return result;
+ }
+
+ protected final int convertByteArrayToShort(String name, byte bytes[],
+ int byteOrder)
+ {
+ return convertByteArrayToShort(name, 0, bytes, byteOrder);
+ }
+
+ protected final int convertByteArrayToShort(String name, int start,
+ byte bytes[], int byteOrder)
+ {
+ byte byte0 = bytes[start + 0];
+ byte byte1 = bytes[start + 1];
+
+ // return convert2BytesToShort(name, byte0, byte1, byteOrder);
+
+ int result;
+
+ if (byteOrder == BYTE_ORDER_MOTOROLA) // motorola, big endian
+ result = ((0xff & byte0) << 8) + ((0xff & byte1) << 0);
+ else
+ // intel, little endian
+ result = ((0xff & byte1) << 8) + ((0xff & byte0) << 0);
+
+ if (debug)
+ debugNumber(name, result, 2);
+
+ return result;
+ }
+
+ protected final int[] convertByteArrayToShortArray(String name,
+ byte bytes[], int start, int length, int byteOrder)
+ {
+ int expectedLength = start + length * 2;
+
+ if (bytes.length < expectedLength)
+ {
+ System.out.println(name + ": expected length: " + expectedLength
+ + ", actual length: " + bytes.length);
+ return null;
+ }
+
+ int result[] = new int[length];
+
+ for (int i = 0; i < length; i++)
+ {
+ result[i] = convertByteArrayToShort(name, start + i * 2, bytes,
+ byteOrder);
+
+ // byte byte0 = bytes[start + i * 2];
+ // byte byte1 = bytes[start + i * 2 + 1];
+ // result[i] = convertBytesToShort(name, byte0, byte1, byteOrder);
+ }
+
+ return result;
+ }
+
+ public final byte[] readByteArray(String name, int length, String exception)
+ throws ImageReadException, IOException
+ {
+ byte result[] = new byte[length];
+
+ int read = 0;
+ while (read < length)
+ {
+ int count = is.read(result, read, length - read);
+ if (count < 1)
+ throw new IOException(exception);
+
+ read += count;
+ }
+
+ if (debug)
+ {
+ for (int i = 0; ((i < length) && (i < 150)); i++)
+ {
+ debugNumber(name + " (" + i + ")", 0xff & result[i]);
+ }
+ }
+ return result;
+ }
+
+ protected final void debugByteArray(String name, byte bytes[])
+ {
+ System.out.println(name + ": " + bytes.length);
+
+ for (int i = 0; ((i < bytes.length) && (i < 50)); i++)
+ {
+ debugNumber(name + " (" + i + ")", bytes[i]);
+ }
+ }
+
+ protected final void debugNumberArray(String name, int numbers[], int length)
+ {
+ System.out.println(name + ": " + numbers.length);
+
+ for (int i = 0; ((i < numbers.length) && (i < 50)); i++)
+ {
+ debugNumber(name + " (" + i + ")", numbers[i], length);
+ }
+ }
+
+ public final byte[] readBytearray(String name, byte bytes[], int start,
+ int count)
+ {
+ if (bytes.length < (start + count))
+ return null;
+
+ byte result[] = new byte[count];
+ System.arraycopy(bytes, start, result, 0, count);
+
+ if (debug)
+ debugByteArray(name, result);
+
+ return result;
+ }
+
+ public final byte[] readByteArray(int length, String error)
+ throws ImageReadException, IOException
+ {
+ boolean verbose = false;
+ boolean strict = true;
+ return readByteArray(length, error, verbose, strict);
+ }
+
+ public final byte[] readByteArray(int length, String error,
+ boolean verbose, boolean strict) throws ImageReadException,
+ IOException
+ {
+ byte bytes[] = new byte[length];
+ int total = 0;
+ int read;
+ while ((read = read(bytes, total, length - total)) > 0)
+ total += read;
+ if (total < length)
+ {
+ if (strict)
+ throw new ImageReadException(error);
+ else if(verbose)
+ System.out.println(error);
+ return null;
+ }
+ return bytes;
+ }
+
+ protected final byte[] getBytearrayTail(String name, byte bytes[], int count)
+ {
+ return readBytearray(name, bytes, count, bytes.length - count);
+ }
+
+ protected final byte[] getBytearrayHead(String name, byte bytes[], int count)
+ {
+ return readBytearray(name, bytes, 0, bytes.length - count);
+ }
+
+ public final boolean compareByteArrays(byte a[], int aStart, byte b[],
+ int bStart, int length)
+ {
+ if (a.length < (aStart + length))
+ return false;
+ if (b.length < (bStart + length))
+ return false;
+
+ for (int i = 0; i < length; i++)
+ {
+ if (a[aStart + i] != b[bStart + i])
+ {
+ debugNumber("a[" + (aStart + i) + "]", a[aStart + i]);
+ debugNumber("b[" + (bStart + i) + "]", b[bStart + i]);
+
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ protected final int read4Bytes(String name, String exception, int byteOrder)
+ throws ImageReadException, IOException
+ {
+ int size = 4;
+ byte bytes[] = new byte[size];
+
+ int read = 0;
+ while (read < size)
+ {
+ int count = is.read(bytes, read, size - read);
+ if (count < 1)
+ throw new IOException(exception);
+
+ read += count;
+ }
+
+ return convertByteArrayToInt(name, bytes, byteOrder);
+ }
+
+ protected final int read3Bytes(String name, String exception, int byteOrder)
+ throws ImageReadException, IOException
+ {
+ int size = 3;
+ byte bytes[] = new byte[size];
+
+ int read = 0;
+ while (read < size)
+ {
+ int count = is.read(bytes, read, size - read);
+ if (count < 1)
+ throw new IOException(exception);
+
+ read += count;
+ }
+
+ return convertByteArrayToInt(name, bytes, 0, 3, byteOrder);
+
+ }
+
+ protected final int read2Bytes(String name, String exception, int byteOrder)
+ throws ImageReadException, IOException
+ {
+ int size = 2;
+ byte bytes[] = new byte[size];
+
+ int read = 0;
+ while (read < size)
+ {
+ int count = is.read(bytes, read, size - read);
+ if (count < 1)
+ throw new IOException(exception);
+
+ read += count;
+ }
+
+ return convertByteArrayToShort(name, bytes, byteOrder);
+ }
+
+ public final int read1ByteInteger(String exception)
+ throws ImageReadException, IOException
+ {
+ int byte0 = is.read();
+ if (byte0 < 0)
+ throw new ImageReadException(exception);
+
+ return 0xff & byte0;
+ }
+
+ public final int read2ByteInteger(String exception)
+ throws ImageReadException, IOException
+ {
+ int byte0 = is.read();
+ int byte1 = is.read();
+ if (byte0 < 0 || byte1 < 0)
+ throw new ImageReadException(exception);
+
+ if (byteOrder == BYTE_ORDER_MOTOROLA) // motorola, big endian
+ return ((0xff & byte0) << 8) + ((0xff & byte1) << 0);
+ else
+ // intel, little endian
+ return ((0xff & byte1) << 8) + ((0xff & byte0) << 0);
+ }
+
+ public final int read4ByteInteger(String exception)
+ throws ImageReadException, IOException
+ {
+ int byte0 = is.read();
+ int byte1 = is.read();
+ int byte2 = is.read();
+ int byte3 = is.read();
+ if (byte0 < 0 || byte1 < 0 || byte2 < 0 || byte3 < 0)
+ throw new ImageReadException(exception);
+
+ if (byteOrder == BYTE_ORDER_MOTOROLA) // motorola, big endian
+ return ((0xff & byte0) << 24) + ((0xff & byte1) << 16)
+ + ((0xff & byte2) << 8) + ((0xff & byte3) << 0);
+ else
+ // intel, little endian
+ return ((0xff & byte3) << 24) + ((0xff & byte2) << 16)
+ + ((0xff & byte1) << 8) + ((0xff & byte0) << 0);
+ }
+
+ protected final void printCharQuad(String msg, int i)
+ {
+ System.out.println(msg + ": '" + (char) (0xff & (i >> 24))
+ + (char) (0xff & (i >> 16)) + (char) (0xff & (i >> 8))
+ + (char) (0xff & (i >> 0)) + "'");
+
+ }
+
+ protected final void printByteBits(String msg, byte i)
+ {
+ System.out.println(msg + ": '" + Integer.toBinaryString(0xff & i));
+ }
+
+ protected final static int CharsToQuad(char c1, char c2, char c3, char c4)
+ {
+ return (((0xff & c1) << 24) | ((0xff & c2) << 16) | ((0xff & c3) << 8) | ((0xff & c4) << 0));
+ }
+
+ public final int findNull(byte src[])
+ {
+ return findNull(src, 0);
+ }
+
+ public final int findNull(byte src[], int start)
+ {
+ for (int i = start; i < src.length; i++)
+ {
+ if (src[i] == 0)
+ return i;
+
+ }
+ return -1;
+ }
+
+ protected final byte[] getRAFBytes(RandomAccessFile raf, long pos,
+ int length, String exception) throws IOException
+ {
+ byte result[] = new byte[length];
+
+ if (debug)
+ {
+ System.out.println("getRAFBytes pos" + ": " + pos);
+ System.out.println("getRAFBytes length" + ": " + length);
+ }
+
+ raf.seek(pos);
+
+ int read = 0;
+ while (read < length)
+ {
+ int count = raf.read(result, read, length - read);
+ if (count < 1)
+ throw new IOException(exception);
+
+ read += count;
+ }
+
+ return result;
+
+ }
+
+ protected void skipBytes(int length) throws IOException
+ {
+ skipBytes(length, "Couldn't skip bytes");
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/BinaryOutputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/BinaryOutputStream.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/BinaryOutputStream.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/BinaryOutputStream.java Fri Sep 10 16:33:35 2010
@@ -23,154 +23,154 @@ import org.apache.sanselan.ImageWriteExc
public class BinaryOutputStream extends OutputStream implements BinaryConstants
{
- protected boolean debug = false;
- private int count = 0;
+ protected boolean debug = false;
+ private int count = 0;
- public final void setDebug(boolean b)
- {
- debug = b;
- }
-
- public final boolean getDebug()
- {
- return debug;
- }
-
- private final OutputStream os;
-
- public BinaryOutputStream(OutputStream os, int byteOrder)
- {
- this.byteOrder = byteOrder;
- this.os = os;
- }
-
- public BinaryOutputStream(OutputStream os)
- {
- this.os = os;
- }
-
- // default byte order for Java, many file formats.
- private int byteOrder = BYTE_ORDER_NETWORK;
-
- protected void setByteOrder(int a, int b) throws ImageWriteException,
- IOException
- {
- if (a != b)
- throw new ImageWriteException("Byte Order bytes don't match (" + a
- + ", " + b + ").");
-
- if (a == BYTE_ORDER_MOTOROLA)
- byteOrder = a;
- else if (a == BYTE_ORDER_INTEL)
- byteOrder = a;
- else
- throw new ImageWriteException("Unknown Byte Order hint: " + a);
- }
-
- protected void setByteOrder(int byteOrder)
- {
- this.byteOrder = byteOrder;
- }
-
- public int getByteOrder()
- {
- return byteOrder;
- }
-
- public void write(int i) throws IOException
- {
- os.write(i);
- count++;
- }
-
- public int getByteCount()
- {
- return count;
- }
-
- public final void write4Bytes(int value) throws ImageWriteException,
- IOException
- {
- writeNBytes(value, 4);
- }
-
- public final void write3Bytes(int value) throws ImageWriteException,
- IOException
- {
- writeNBytes(value, 3);
- }
-
- public final void write2Bytes(int value) throws ImageWriteException,
- IOException
- {
- writeNBytes(value, 2);
- }
-
- public final void write4ByteInteger(int value) throws ImageWriteException,
- IOException
- {
- if (byteOrder == BYTE_ORDER_MOTOROLA)
- {
- write(0xff & (value >> 24));
- write(0xff & (value >> 16));
- write(0xff & (value >> 8));
- write(0xff & value);
- } else
- {
- write(0xff & value);
- write(0xff & (value >> 8));
- write(0xff & (value >> 16));
- write(0xff & (value >> 24));
- }
- }
-
- public final void write2ByteInteger(int value) throws ImageWriteException,
- IOException
- {
- if (byteOrder == BYTE_ORDER_MOTOROLA)
- {
- write(0xff & (value >> 8));
- write(0xff & value);
- } else
- {
- write(0xff & value);
- write(0xff & (value >> 8));
- }
- }
-
- public final void writeByteArray(byte bytes[]) throws IOException
- {
- os.write(bytes, 0, bytes.length);
- count += bytes.length;
- }
-
- private byte[] convertValueToByteArray(int value, int n)
- {
- byte result[] = new byte[n];
-
- if (byteOrder == BYTE_ORDER_MOTOROLA)
- {
- for (int i = 0; i < n; i++)
- {
- int b = 0xff & (value >> (8 * (n - i - 1)));
- result[i] = (byte) b;
- }
- } else
- {
- for (int i = 0; i < n; i++)
- {
- int b = 0xff & (value >> (8 * i));
- result[i] = (byte) b;
- }
- }
-
- return result;
- }
-
- private final void writeNBytes(int value, int n)
- throws ImageWriteException, IOException
- {
- write(convertValueToByteArray(value, n));
- }
+ public final void setDebug(boolean b)
+ {
+ debug = b;
+ }
+
+ public final boolean getDebug()
+ {
+ return debug;
+ }
+
+ private final OutputStream os;
+
+ public BinaryOutputStream(OutputStream os, int byteOrder)
+ {
+ this.byteOrder = byteOrder;
+ this.os = os;
+ }
+
+ public BinaryOutputStream(OutputStream os)
+ {
+ this.os = os;
+ }
+
+ // default byte order for Java, many file formats.
+ private int byteOrder = BYTE_ORDER_NETWORK;
+
+ protected void setByteOrder(int a, int b) throws ImageWriteException,
+ IOException
+ {
+ if (a != b)
+ throw new ImageWriteException("Byte Order bytes don't match (" + a
+ + ", " + b + ").");
+
+ if (a == BYTE_ORDER_MOTOROLA)
+ byteOrder = a;
+ else if (a == BYTE_ORDER_INTEL)
+ byteOrder = a;
+ else
+ throw new ImageWriteException("Unknown Byte Order hint: " + a);
+ }
+
+ protected void setByteOrder(int byteOrder)
+ {
+ this.byteOrder = byteOrder;
+ }
+
+ public int getByteOrder()
+ {
+ return byteOrder;
+ }
+
+ public void write(int i) throws IOException
+ {
+ os.write(i);
+ count++;
+ }
+
+ public int getByteCount()
+ {
+ return count;
+ }
+
+ public final void write4Bytes(int value) throws ImageWriteException,
+ IOException
+ {
+ writeNBytes(value, 4);
+ }
+
+ public final void write3Bytes(int value) throws ImageWriteException,
+ IOException
+ {
+ writeNBytes(value, 3);
+ }
+
+ public final void write2Bytes(int value) throws ImageWriteException,
+ IOException
+ {
+ writeNBytes(value, 2);
+ }
+
+ public final void write4ByteInteger(int value) throws ImageWriteException,
+ IOException
+ {
+ if (byteOrder == BYTE_ORDER_MOTOROLA)
+ {
+ write(0xff & (value >> 24));
+ write(0xff & (value >> 16));
+ write(0xff & (value >> 8));
+ write(0xff & value);
+ } else
+ {
+ write(0xff & value);
+ write(0xff & (value >> 8));
+ write(0xff & (value >> 16));
+ write(0xff & (value >> 24));
+ }
+ }
+
+ public final void write2ByteInteger(int value) throws ImageWriteException,
+ IOException
+ {
+ if (byteOrder == BYTE_ORDER_MOTOROLA)
+ {
+ write(0xff & (value >> 8));
+ write(0xff & value);
+ } else
+ {
+ write(0xff & value);
+ write(0xff & (value >> 8));
+ }
+ }
+
+ public final void writeByteArray(byte bytes[]) throws IOException
+ {
+ os.write(bytes, 0, bytes.length);
+ count += bytes.length;
+ }
+
+ private byte[] convertValueToByteArray(int value, int n)
+ {
+ byte result[] = new byte[n];
+
+ if (byteOrder == BYTE_ORDER_MOTOROLA)
+ {
+ for (int i = 0; i < n; i++)
+ {
+ int b = 0xff & (value >> (8 * (n - i - 1)));
+ result[i] = (byte) b;
+ }
+ } else
+ {
+ for (int i = 0; i < n; i++)
+ {
+ int b = 0xff & (value >> (8 * i));
+ result[i] = (byte) b;
+ }
+ }
+
+ return result;
+ }
+
+ private final void writeNBytes(int value, int n)
+ throws ImageWriteException, IOException
+ {
+ write(convertValueToByteArray(value, n));
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/BitInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/BitInputStream.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/BitInputStream.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/BitInputStream.java Fri Sep 10 16:33:35 2010
@@ -21,103 +21,103 @@ import java.io.InputStream;
public class BitInputStream extends InputStream implements BinaryConstants
{
- // TODO should be byte order conscious, ie TIFF for reading
- // samples size<8 - shuoldn't that effect their order within byte?
- private final InputStream is;
-
- public BitInputStream(InputStream is)
- {
- this.is = is;
- // super(is);
- }
-
- public int read() throws IOException
- {
- if (cacheBitsRemaining > 0)
- throw new IOException("BitInputStream: incomplete bit read");
- return is.read();
- }
-
- private int cache;
- private int cacheBitsRemaining = 0;
- private long bytes_read = 0;
-
- public final int readBits(int count) throws IOException
- {
- if (count < 8)
- {
- if (cacheBitsRemaining == 0)
- {
- // fill cache
- cache = is.read();
- cacheBitsRemaining = 8;
- bytes_read++;
- }
- if (count > cacheBitsRemaining)
- throw new IOException(
- "BitInputStream: can't read bit fields across bytes");
-
- // int bits_to_shift = cache_bits_remaining - count;
- cacheBitsRemaining -= count;
- int bits = cache >> cacheBitsRemaining;
-
- switch (count)
- {
- case 1 :
- return bits & 1;
- case 2 :
- return bits & 3;
- case 3 :
- return bits & 7;
- case 4 :
- return bits & 15;
- case 5 :
- return bits & 31;
- case 6 :
- return bits & 63;
- case 7 :
- return bits & 127;
- }
-
- }
- if (cacheBitsRemaining > 0)
- throw new IOException("BitInputStream: incomplete bit read");
-
- if (count == 8)
- {
- bytes_read++;
- return is.read();
- }
-
- if (count == 16)
- {
- bytes_read += 2;
- return (is.read() << 8) | (is.read() << 0);
- }
-
- if (count == 24)
- {
- bytes_read += 3;
- return (is.read() << 16) | (is.read() << 8) | (is.read() << 0);
- }
-
- if (count == 32)
- {
- bytes_read += 4;
- return (is.read() << 24) | (is.read() << 16) | (is.read() << 8)
- | (is.read() << 0);
- }
-
- throw new IOException("BitInputStream: unknown error");
- }
-
- public void flushCache()
- {
- cacheBitsRemaining = 0;
- }
-
- public long getBytesRead()
- {
- return bytes_read;
- }
+ // TODO should be byte order conscious, ie TIFF for reading
+ // samples size<8 - shuoldn't that effect their order within byte?
+ private final InputStream is;
+
+ public BitInputStream(InputStream is)
+ {
+ this.is = is;
+ // super(is);
+ }
+
+ public int read() throws IOException
+ {
+ if (cacheBitsRemaining > 0)
+ throw new IOException("BitInputStream: incomplete bit read");
+ return is.read();
+ }
+
+ private int cache;
+ private int cacheBitsRemaining = 0;
+ private long bytes_read = 0;
+
+ public final int readBits(int count) throws IOException
+ {
+ if (count < 8)
+ {
+ if (cacheBitsRemaining == 0)
+ {
+ // fill cache
+ cache = is.read();
+ cacheBitsRemaining = 8;
+ bytes_read++;
+ }
+ if (count > cacheBitsRemaining)
+ throw new IOException(
+ "BitInputStream: can't read bit fields across bytes");
+
+ // int bits_to_shift = cache_bits_remaining - count;
+ cacheBitsRemaining -= count;
+ int bits = cache >> cacheBitsRemaining;
+
+ switch (count)
+ {
+ case 1 :
+ return bits & 1;
+ case 2 :
+ return bits & 3;
+ case 3 :
+ return bits & 7;
+ case 4 :
+ return bits & 15;
+ case 5 :
+ return bits & 31;
+ case 6 :
+ return bits & 63;
+ case 7 :
+ return bits & 127;
+ }
+
+ }
+ if (cacheBitsRemaining > 0)
+ throw new IOException("BitInputStream: incomplete bit read");
+
+ if (count == 8)
+ {
+ bytes_read++;
+ return is.read();
+ }
+
+ if (count == 16)
+ {
+ bytes_read += 2;
+ return (is.read() << 8) | (is.read() << 0);
+ }
+
+ if (count == 24)
+ {
+ bytes_read += 3;
+ return (is.read() << 16) | (is.read() << 8) | (is.read() << 0);
+ }
+
+ if (count == 32)
+ {
+ bytes_read += 4;
+ return (is.read() << 24) | (is.read() << 16) | (is.read() << 8)
+ | (is.read() << 0);
+ }
+
+ throw new IOException("BitInputStream: unknown error");
+ }
+
+ public void flushCache()
+ {
+ cacheBitsRemaining = 0;
+ }
+
+ public long getBytesRead()
+ {
+ return bytes_read;
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/BitInputStreamFlexible.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/BitInputStreamFlexible.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/BitInputStreamFlexible.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/BitInputStreamFlexible.java Fri Sep 10 16:33:35 2010
@@ -20,95 +20,95 @@ import java.io.IOException;
import java.io.InputStream;
public class BitInputStreamFlexible extends InputStream
- implements
- BinaryConstants
+ implements
+ BinaryConstants
{
- // TODO should be byte order conscious, ie TIFF for reading
- // samples size<8 - shuoldn't that effect their order within byte?
- private final InputStream is;
-
- public BitInputStreamFlexible(InputStream is)
- {
- this.is = is;
- // super(is);
- }
-
- public int read() throws IOException
- {
- if (cacheBitsRemaining > 0)
- throw new IOException("BitInputStream: incomplete bit read");
- return is.read();
- }
-
- private int cache;
- private int cacheBitsRemaining = 0;
- private long bytesRead = 0;
-
- public final int readBits(int count) throws IOException
- {
-
- if (count <= 32) // catch-all
- {
- int result = 0;
- // int done = 0;
-
- if (cacheBitsRemaining > 0)
- {
- if (count >= cacheBitsRemaining)
- {
- result = ((1 << cacheBitsRemaining) - 1) & cache;
- count -= cacheBitsRemaining;
- cacheBitsRemaining = 0;
- }
- else
- {
- // cache >>= count;
- cacheBitsRemaining -= count;
- result = ((1 << count) - 1) & (cache >> cacheBitsRemaining);
- count = 0;
- }
- }
- while (count >= 8)
- {
- cache = is.read();
- if (cache < 0)
- throw new IOException("couldn't read bits");
- System.out.println("cache 1: " + cache + " ("
- + Integer.toHexString(cache) + ", "
- + Integer.toBinaryString(cache) + ")");
- bytesRead++;
- result = (result << 8) | (0xff & cache);
- count -= 8;
- }
- if (count > 0)
- {
- cache = is.read();
- if (cache < 0)
- throw new IOException("couldn't read bits");
- System.out.println("cache 2: " + cache + " ("
- + Integer.toHexString(cache) + ", "
- + Integer.toBinaryString(cache) + ")");
- bytesRead++;
- cacheBitsRemaining = 8 - count;
- result = (result << count)
- | (((1 << count) - 1) & (cache >> cacheBitsRemaining));
- count = 0;
- }
-
- return result;
- }
-
- throw new IOException("BitInputStream: unknown error");
-
- }
-
- public void flushCache()
- {
- cacheBitsRemaining = 0;
- }
-
- public long getBytesRead()
- {
- return bytesRead;
- }
+ // TODO should be byte order conscious, ie TIFF for reading
+ // samples size<8 - shuoldn't that effect their order within byte?
+ private final InputStream is;
+
+ public BitInputStreamFlexible(InputStream is)
+ {
+ this.is = is;
+ // super(is);
+ }
+
+ public int read() throws IOException
+ {
+ if (cacheBitsRemaining > 0)
+ throw new IOException("BitInputStream: incomplete bit read");
+ return is.read();
+ }
+
+ private int cache;
+ private int cacheBitsRemaining = 0;
+ private long bytesRead = 0;
+
+ public final int readBits(int count) throws IOException
+ {
+
+ if (count <= 32) // catch-all
+ {
+ int result = 0;
+ // int done = 0;
+
+ if (cacheBitsRemaining > 0)
+ {
+ if (count >= cacheBitsRemaining)
+ {
+ result = ((1 << cacheBitsRemaining) - 1) & cache;
+ count -= cacheBitsRemaining;
+ cacheBitsRemaining = 0;
+ }
+ else
+ {
+ // cache >>= count;
+ cacheBitsRemaining -= count;
+ result = ((1 << count) - 1) & (cache >> cacheBitsRemaining);
+ count = 0;
+ }
+ }
+ while (count >= 8)
+ {
+ cache = is.read();
+ if (cache < 0)
+ throw new IOException("couldn't read bits");
+ System.out.println("cache 1: " + cache + " ("
+ + Integer.toHexString(cache) + ", "
+ + Integer.toBinaryString(cache) + ")");
+ bytesRead++;
+ result = (result << 8) | (0xff & cache);
+ count -= 8;
+ }
+ if (count > 0)
+ {
+ cache = is.read();
+ if (cache < 0)
+ throw new IOException("couldn't read bits");
+ System.out.println("cache 2: " + cache + " ("
+ + Integer.toHexString(cache) + ", "
+ + Integer.toBinaryString(cache) + ")");
+ bytesRead++;
+ cacheBitsRemaining = 8 - count;
+ result = (result << count)
+ | (((1 << count) - 1) & (cache >> cacheBitsRemaining));
+ count = 0;
+ }
+
+ return result;
+ }
+
+ throw new IOException("BitInputStream: unknown error");
+
+ }
+
+ public void flushCache()
+ {
+ cacheBitsRemaining = 0;
+ }
+
+ public long getBytesRead()
+ {
+ return bytesRead;
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/Compression.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/Compression.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/Compression.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/Compression.java Fri Sep 10 16:33:35 2010
@@ -27,35 +27,35 @@ import org.apache.sanselan.common.mylzw.
public class Compression
{
- public byte[] decompressLZW(byte compressed[], int LZWMinimumCodeSize,
- int expectedSize, int byteOrder) throws IOException
- {
- InputStream is = new ByteArrayInputStream(compressed);
-
- MyLZWDecompressor decompressor = new MyLZWDecompressor(
- LZWMinimumCodeSize, byteOrder);
- byte[] result = decompressor.decompress(is, expectedSize);
-
- return result;
- }
-
- public byte[] decompressPackBits(byte compressed[], int expectedSize,
- int byteOrder) throws ImageReadException, IOException
- {
- byte unpacked[] = new PackBits().decompress(compressed, expectedSize);
- return unpacked;
- }
-
- public byte[] compressLZW(byte src[], int LZWMinimumCodeSize,
- int byteOrder, boolean earlyLimit) throws IOException
-
- {
- MyLZWCompressor compressor = new MyLZWCompressor(LZWMinimumCodeSize,
- byteOrder, earlyLimit);
+ public byte[] decompressLZW(byte compressed[], int LZWMinimumCodeSize,
+ int expectedSize, int byteOrder) throws IOException
+ {
+ InputStream is = new ByteArrayInputStream(compressed);
+
+ MyLZWDecompressor decompressor = new MyLZWDecompressor(
+ LZWMinimumCodeSize, byteOrder);
+ byte[] result = decompressor.decompress(is, expectedSize);
+
+ return result;
+ }
+
+ public byte[] decompressPackBits(byte compressed[], int expectedSize,
+ int byteOrder) throws ImageReadException, IOException
+ {
+ byte unpacked[] = new PackBits().decompress(compressed, expectedSize);
+ return unpacked;
+ }
+
+ public byte[] compressLZW(byte src[], int LZWMinimumCodeSize,
+ int byteOrder, boolean earlyLimit) throws IOException
+
+ {
+ MyLZWCompressor compressor = new MyLZWCompressor(LZWMinimumCodeSize,
+ byteOrder, earlyLimit);
- byte compressed[] = compressor.compress(src);
+ byte compressed[] = compressor.compress(src);
- return compressed;
- }
+ return compressed;
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/IBufferedImageFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/IBufferedImageFactory.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/IBufferedImageFactory.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/IBufferedImageFactory.java Fri Sep 10 16:33:35 2010
@@ -21,9 +21,9 @@ import java.awt.image.BufferedImage;
public interface IBufferedImageFactory
{
- public BufferedImage getColorBufferedImage(int width, int height,
- boolean hasAlpha);
+ public BufferedImage getColorBufferedImage(int width, int height,
+ boolean hasAlpha);
- public BufferedImage getGrayscaleBufferedImage(int width, int height,
- boolean hasAlpha);
+ public BufferedImage getGrayscaleBufferedImage(int width, int height,
+ boolean hasAlpha);
}
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/IImageMetadata.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/IImageMetadata.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/IImageMetadata.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/IImageMetadata.java Fri Sep 10 16:33:35 2010
@@ -20,14 +20,14 @@ import java.util.ArrayList;
public interface IImageMetadata
{
- public String toString(String prefix);
+ public String toString(String prefix);
- public ArrayList getItems();
+ public ArrayList getItems();
- public interface IImageMetadataItem
- {
- public String toString(String prefix);
+ public interface IImageMetadataItem
+ {
+ public String toString(String prefix);
- public String toString();
- }
+ public String toString();
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/ImageMetadata.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/ImageMetadata.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/ImageMetadata.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/ImageMetadata.java Fri Sep 10 16:33:35 2010
@@ -21,87 +21,87 @@ import java.util.ArrayList;
public class ImageMetadata implements IImageMetadata
{
- private final ArrayList items = new ArrayList();
+ private final ArrayList items = new ArrayList();
- public void add(String keyword, String text)
- {
- add(new Item(keyword, text));
- }
-
- public void add(IImageMetadataItem item)
- {
- items.add(item);
- }
-
- public ArrayList getItems()
- {
- return new ArrayList(items);
- }
-
- protected static final String newline = System
- .getProperty("line.separator");
-
- public String toString()
- {
- return toString(null);
- }
-
- public String toString(String prefix)
- {
- if (null == prefix)
- prefix = "";
-
- StringBuffer result = new StringBuffer();
- for (int i = 0; i < items.size(); i++)
- {
- if (i > 0)
- result.append(newline);
- // if (null != prefix)
- // result.append(prefix);
-
- ImageMetadata.IImageMetadataItem item = (ImageMetadata.IImageMetadataItem) items
- .get(i);
- result.append(item.toString(prefix + "\t"));
-
- // Debug.debug("prefix", prefix);
- // Debug.debug("item", items.get(i));
- // Debug.debug();
- }
- return result.toString();
- }
-
- public static class Item implements IImageMetadataItem
- {
- private final String keyword, text;
-
- public Item(String keyword, String text)
- {
- this.keyword = keyword;
- this.text = text;
- }
-
- public String getKeyword()
- {
- return keyword;
- }
-
- public String getText()
- {
- return text;
- }
-
- public String toString()
- {
- return toString(null);
- }
-
- public String toString(String prefix)
- {
- String result = keyword + ": " + text;
- if (null != prefix)
- result = prefix + result;
- return result;
- }
- }
+ public void add(String keyword, String text)
+ {
+ add(new Item(keyword, text));
+ }
+
+ public void add(IImageMetadataItem item)
+ {
+ items.add(item);
+ }
+
+ public ArrayList getItems()
+ {
+ return new ArrayList(items);
+ }
+
+ protected static final String newline = System
+ .getProperty("line.separator");
+
+ public String toString()
+ {
+ return toString(null);
+ }
+
+ public String toString(String prefix)
+ {
+ if (null == prefix)
+ prefix = "";
+
+ StringBuffer result = new StringBuffer();
+ for (int i = 0; i < items.size(); i++)
+ {
+ if (i > 0)
+ result.append(newline);
+ // if (null != prefix)
+ // result.append(prefix);
+
+ ImageMetadata.IImageMetadataItem item = (ImageMetadata.IImageMetadataItem) items
+ .get(i);
+ result.append(item.toString(prefix + "\t"));
+
+ // Debug.debug("prefix", prefix);
+ // Debug.debug("item", items.get(i));
+ // Debug.debug();
+ }
+ return result.toString();
+ }
+
+ public static class Item implements IImageMetadataItem
+ {
+ private final String keyword, text;
+
+ public Item(String keyword, String text)
+ {
+ this.keyword = keyword;
+ this.text = text;
+ }
+
+ public String getKeyword()
+ {
+ return keyword;
+ }
+
+ public String getText()
+ {
+ return text;
+ }
+
+ public String toString()
+ {
+ return toString(null);
+ }
+
+ public String toString(String prefix)
+ {
+ String result = keyword + ": " + text;
+ if (null != prefix)
+ result = prefix + result;
+ return result;
+ }
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/MyByteArrayOutputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/MyByteArrayOutputStream.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/MyByteArrayOutputStream.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/MyByteArrayOutputStream.java Fri Sep 10 16:33:35 2010
@@ -22,38 +22,38 @@ import java.io.OutputStream;
public class MyByteArrayOutputStream extends OutputStream
// some performace benefit, because not thread safe.
{
- private final byte bytes[];
+ private final byte bytes[];
- public MyByteArrayOutputStream(int length)
- {
- bytes = new byte[length];
- }
-
- private int count = 0;
-
- public void write(int value) throws IOException
- {
- if (count >= bytes.length)
- throw new IOException("Write exceeded expected length (" + count
- + ", " + bytes.length + ")");
-
- bytes[count] = (byte) value;
- count++;
- }
-
- public byte[] toByteArray()
- {
- if (count < bytes.length)
- {
- byte result[] = new byte[count];
- System.arraycopy(bytes, 0, result, 0, count);
- return result;
- }
- return bytes;
- }
-
- public int getBytesWritten()
- {
- return count;
- }
+ public MyByteArrayOutputStream(int length)
+ {
+ bytes = new byte[length];
+ }
+
+ private int count = 0;
+
+ public void write(int value) throws IOException
+ {
+ if (count >= bytes.length)
+ throw new IOException("Write exceeded expected length (" + count
+ + ", " + bytes.length + ")");
+
+ bytes[count] = (byte) value;
+ count++;
+ }
+
+ public byte[] toByteArray()
+ {
+ if (count < bytes.length)
+ {
+ byte result[] = new byte[count];
+ System.arraycopy(bytes, 0, result, 0, count);
+ return result;
+ }
+ return bytes;
+ }
+
+ public int getBytesWritten()
+ {
+ return count;
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/RationalNumber.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/RationalNumber.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/RationalNumber.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/RationalNumber.java Fri Sep 10 16:33:35 2010
@@ -21,107 +21,107 @@ import java.text.NumberFormat;
public class RationalNumber extends Number
{
- private static final long serialVersionUID = -1;
+ private static final long serialVersionUID = -1;
- public final int numerator;
- public final int divisor;
+ public final int numerator;
+ public final int divisor;
- public RationalNumber(int numerator, int divisor)
- {
- this.numerator = numerator;
- this.divisor = divisor;
- }
-
- public static final RationalNumber factoryMethod(long n, long d)
- {
- // safer than constructor - handles values outside min/max range.
- // also does some simple finding of common denominators.
-
- if (n > Integer.MAX_VALUE || n < Integer.MIN_VALUE
- || d > Integer.MAX_VALUE || d < Integer.MIN_VALUE)
- {
- while ((n > Integer.MAX_VALUE || n < Integer.MIN_VALUE
- || d > Integer.MAX_VALUE || d < Integer.MIN_VALUE)
- && (Math.abs(n) > 1) && (Math.abs(d) > 1))
- {
- // brutal, inprecise truncation =(
- // use the sign-preserving right shift operator.
- n >>= 1;
- d >>= 1;
- }
-
- if (d == 0)
- throw new NumberFormatException("Invalid value, numerator: "
- + n + ", divisor: " + d);
- }
-
- long gcd = gcd(n, d);
- d = d / gcd;
- n = n / gcd;
-
- return new RationalNumber((int) n, (int) d);
- }
-
- /**
- * Return the greatest common divisor
- */
- private static long gcd(long a, long b)
- {
-
- if (b == 0)
- return a;
- else
- return gcd(b, a % b);
- }
-
- public RationalNumber negate()
- {
- return new RationalNumber(-numerator, divisor);
- }
-
- public double doubleValue()
- {
- return (double) numerator / (double) divisor;
- }
-
- public float floatValue()
- {
- return (float) numerator / (float) divisor;
- }
-
- public int intValue()
- {
- return numerator / divisor;
- }
-
- public long longValue()
- {
- return (long) numerator / (long) divisor;
- }
-
- public boolean isValid()
- {
- return divisor != 0;
- }
-
- private static final NumberFormat nf = DecimalFormat.getInstance();
-
- public String toString()
- {
- if (divisor == 0)
- return "Invalid rational (" + numerator + "/" + divisor + ")";
- if ((numerator % divisor) == 0)
- return nf.format(numerator / divisor);
- return numerator + "/" + divisor + " ("
- + nf.format((double) numerator / divisor) + ")";
- }
-
- public String toDisplayString()
- {
- if ((numerator % divisor) == 0)
- return "" + (numerator / divisor);
- NumberFormat nf = DecimalFormat.getInstance();
- nf.setMaximumFractionDigits(3);
- return nf.format((double) numerator / (double) divisor);
- }
+ public RationalNumber(int numerator, int divisor)
+ {
+ this.numerator = numerator;
+ this.divisor = divisor;
+ }
+
+ public static final RationalNumber factoryMethod(long n, long d)
+ {
+ // safer than constructor - handles values outside min/max range.
+ // also does some simple finding of common denominators.
+
+ if (n > Integer.MAX_VALUE || n < Integer.MIN_VALUE
+ || d > Integer.MAX_VALUE || d < Integer.MIN_VALUE)
+ {
+ while ((n > Integer.MAX_VALUE || n < Integer.MIN_VALUE
+ || d > Integer.MAX_VALUE || d < Integer.MIN_VALUE)
+ && (Math.abs(n) > 1) && (Math.abs(d) > 1))
+ {
+ // brutal, inprecise truncation =(
+ // use the sign-preserving right shift operator.
+ n >>= 1;
+ d >>= 1;
+ }
+
+ if (d == 0)
+ throw new NumberFormatException("Invalid value, numerator: "
+ + n + ", divisor: " + d);
+ }
+
+ long gcd = gcd(n, d);
+ d = d / gcd;
+ n = n / gcd;
+
+ return new RationalNumber((int) n, (int) d);
+ }
+
+ /**
+ * Return the greatest common divisor
+ */
+ private static long gcd(long a, long b)
+ {
+
+ if (b == 0)
+ return a;
+ else
+ return gcd(b, a % b);
+ }
+
+ public RationalNumber negate()
+ {
+ return new RationalNumber(-numerator, divisor);
+ }
+
+ public double doubleValue()
+ {
+ return (double) numerator / (double) divisor;
+ }
+
+ public float floatValue()
+ {
+ return (float) numerator / (float) divisor;
+ }
+
+ public int intValue()
+ {
+ return numerator / divisor;
+ }
+
+ public long longValue()
+ {
+ return (long) numerator / (long) divisor;
+ }
+
+ public boolean isValid()
+ {
+ return divisor != 0;
+ }
+
+ private static final NumberFormat nf = DecimalFormat.getInstance();
+
+ public String toString()
+ {
+ if (divisor == 0)
+ return "Invalid rational (" + numerator + "/" + divisor + ")";
+ if ((numerator % divisor) == 0)
+ return nf.format(numerator / divisor);
+ return numerator + "/" + divisor + " ("
+ + nf.format((double) numerator / divisor) + ")";
+ }
+
+ public String toDisplayString()
+ {
+ if ((numerator % divisor) == 0)
+ return "" + (numerator / divisor);
+ NumberFormat nf = DecimalFormat.getInstance();
+ nf.setMaximumFractionDigits(3);
+ return nf.format((double) numerator / (double) divisor);
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/RationalNumberUtilities.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/RationalNumberUtilities.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/RationalNumberUtilities.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/RationalNumberUtilities.java Fri Sep 10 16:33:35 2010
@@ -19,129 +19,129 @@ package org.apache.sanselan.common;
public abstract class RationalNumberUtilities extends Number
{
- private static class Option
- {
- public final RationalNumber rationalNumber;
- public final double error;
-
- private Option(final RationalNumber rationalNumber, final double error)
- {
- this.rationalNumber = rationalNumber;
- this.error = error;
- }
-
- public static final Option factory(final RationalNumber rationalNumber,
- final double value)
- {
- return new Option(rationalNumber, Math.abs(rationalNumber
- .doubleValue()
- - value));
- }
-
- public String toString()
- {
- return rationalNumber.toString();
- }
- }
-
- // int-precision tolerance
- private static final double TOLERANCE = 1E-8;
-
- //
- // calculate rational number using successive approximations.
- //
- public static final RationalNumber getRationalNumber(double value)
- {
- if (value >= Integer.MAX_VALUE)
- return new RationalNumber(Integer.MAX_VALUE, 1);
- else if (value <= -Integer.MAX_VALUE)
- return new RationalNumber(-Integer.MAX_VALUE, 1);
-
- boolean negative = false;
- if (value < 0)
- {
- negative = true;
- value = Math.abs(value);
- }
-
- Option low;
- Option high;
- {
- RationalNumber l, h;
-
- if (value == 0)
- return new RationalNumber(0, 1);
- else if (value >= 1)
- {
- int approx = (int) value;
- if (approx < value)
- {
- l = new RationalNumber(approx, 1);
- h = new RationalNumber(approx + 1, 1);
- }
- else
- {
- l = new RationalNumber(approx - 1, 1);
- h = new RationalNumber(approx, 1);
- }
- }
- else
- {
- int approx = (int) (1.0 / value);
- if ((1.0 / approx) < value)
- {
- l = new RationalNumber(1, approx);
- h = new RationalNumber(1, approx - 1);
- }
- else
- {
- l = new RationalNumber(1, approx + 1);
- h = new RationalNumber(1, approx);
- }
- }
- low = Option.factory(l, value);
- high = Option.factory(h, value);
- }
-
- Option bestOption = (low.error < high.error) ? low : high;
-
- final int MAX_ITERATIONS = 100; // value is quite high, actually. shouldn't matter.
- for (int count = 0; bestOption.error > TOLERANCE
- && count < MAX_ITERATIONS; count++)
- {
- // Debug.debug("bestOption: " + bestOption + ", left: " + low
- // + ", right: " + high + ", value: " + value + ", error: "
- // + bestOption.error);
-
- RationalNumber mediant = RationalNumber.factoryMethod(
- (long) low.rationalNumber.numerator
- + (long) high.rationalNumber.numerator,
- (long) low.rationalNumber.divisor
- + (long) high.rationalNumber.divisor);
- Option mediantOption = Option.factory(mediant, value);
-
- if (value < mediant.doubleValue())
- {
- if (high.error <= mediantOption.error)
- break;
-
- high = mediantOption;
- }
- else
- {
- if (low.error <= mediantOption.error)
- break;
-
- low = mediantOption;
- }
-
- if (mediantOption.error < bestOption.error)
- bestOption = mediantOption;
- }
-
- return negative
- ? bestOption.rationalNumber.negate()
- : bestOption.rationalNumber;
- }
+ private static class Option
+ {
+ public final RationalNumber rationalNumber;
+ public final double error;
+
+ private Option(final RationalNumber rationalNumber, final double error)
+ {
+ this.rationalNumber = rationalNumber;
+ this.error = error;
+ }
+
+ public static final Option factory(final RationalNumber rationalNumber,
+ final double value)
+ {
+ return new Option(rationalNumber, Math.abs(rationalNumber
+ .doubleValue()
+ - value));
+ }
+
+ public String toString()
+ {
+ return rationalNumber.toString();
+ }
+ }
+
+ // int-precision tolerance
+ private static final double TOLERANCE = 1E-8;
+
+ //
+ // calculate rational number using successive approximations.
+ //
+ public static final RationalNumber getRationalNumber(double value)
+ {
+ if (value >= Integer.MAX_VALUE)
+ return new RationalNumber(Integer.MAX_VALUE, 1);
+ else if (value <= -Integer.MAX_VALUE)
+ return new RationalNumber(-Integer.MAX_VALUE, 1);
+
+ boolean negative = false;
+ if (value < 0)
+ {
+ negative = true;
+ value = Math.abs(value);
+ }
+
+ Option low;
+ Option high;
+ {
+ RationalNumber l, h;
+
+ if (value == 0)
+ return new RationalNumber(0, 1);
+ else if (value >= 1)
+ {
+ int approx = (int) value;
+ if (approx < value)
+ {
+ l = new RationalNumber(approx, 1);
+ h = new RationalNumber(approx + 1, 1);
+ }
+ else
+ {
+ l = new RationalNumber(approx - 1, 1);
+ h = new RationalNumber(approx, 1);
+ }
+ }
+ else
+ {
+ int approx = (int) (1.0 / value);
+ if ((1.0 / approx) < value)
+ {
+ l = new RationalNumber(1, approx);
+ h = new RationalNumber(1, approx - 1);
+ }
+ else
+ {
+ l = new RationalNumber(1, approx + 1);
+ h = new RationalNumber(1, approx);
+ }
+ }
+ low = Option.factory(l, value);
+ high = Option.factory(h, value);
+ }
+
+ Option bestOption = (low.error < high.error) ? low : high;
+
+ final int MAX_ITERATIONS = 100; // value is quite high, actually. shouldn't matter.
+ for (int count = 0; bestOption.error > TOLERANCE
+ && count < MAX_ITERATIONS; count++)
+ {
+ // Debug.debug("bestOption: " + bestOption + ", left: " + low
+ // + ", right: " + high + ", value: " + value + ", error: "
+ // + bestOption.error);
+
+ RationalNumber mediant = RationalNumber.factoryMethod(
+ (long) low.rationalNumber.numerator
+ + (long) high.rationalNumber.numerator,
+ (long) low.rationalNumber.divisor
+ + (long) high.rationalNumber.divisor);
+ Option mediantOption = Option.factory(mediant, value);
+
+ if (value < mediant.doubleValue())
+ {
+ if (high.error <= mediantOption.error)
+ break;
+
+ high = mediantOption;
+ }
+ else
+ {
+ if (low.error <= mediantOption.error)
+ break;
+
+ low = mediantOption;
+ }
+
+ if (mediantOption.error < bestOption.error)
+ bestOption = mediantOption;
+ }
+
+ return negative
+ ? bestOption.rationalNumber.negate()
+ : bestOption.rationalNumber;
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/RgbBufferedImageFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/RgbBufferedImageFactory.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/RgbBufferedImageFactory.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/RgbBufferedImageFactory.java Fri Sep 10 16:33:35 2010
@@ -21,18 +21,18 @@ import java.awt.image.BufferedImage;
public class RgbBufferedImageFactory implements IBufferedImageFactory
{
- public BufferedImage getColorBufferedImage(int width, int height,
- boolean hasAlpha)
- {
- if (hasAlpha)
- return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
- return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- }
+ public BufferedImage getColorBufferedImage(int width, int height,
+ boolean hasAlpha)
+ {
+ if (hasAlpha)
+ return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
+ return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+ }
- public BufferedImage getGrayscaleBufferedImage(int width, int height,
- boolean hasAlpha)
- {
- // always use color.
- return getColorBufferedImage(width, height, hasAlpha);
- }
+ public BufferedImage getGrayscaleBufferedImage(int width, int height,
+ boolean hasAlpha)
+ {
+ // always use color.
+ return getColorBufferedImage(width, height, hasAlpha);
+ }
}
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/SimpleBufferedImageFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/SimpleBufferedImageFactory.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/SimpleBufferedImageFactory.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/SimpleBufferedImageFactory.java Fri Sep 10 16:33:35 2010
@@ -21,20 +21,20 @@ import java.awt.image.BufferedImage;
public class SimpleBufferedImageFactory implements IBufferedImageFactory
{
- public BufferedImage getColorBufferedImage(int width, int height,
- boolean hasAlpha)
- {
- if (hasAlpha)
- return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
- return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- }
+ public BufferedImage getColorBufferedImage(int width, int height,
+ boolean hasAlpha)
+ {
+ if (hasAlpha)
+ return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
+ return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+ }
- public BufferedImage getGrayscaleBufferedImage(int width, int height,
- boolean hasAlpha)
- {
- if (hasAlpha)
- return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
+ public BufferedImage getGrayscaleBufferedImage(int width, int height,
+ boolean hasAlpha)
+ {
+ if (hasAlpha)
+ return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
- return new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
- }
+ return new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
+ }
}
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/ZLibUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/ZLibUtils.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/ZLibUtils.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/ZLibUtils.java Fri Sep 10 16:33:35 2010
@@ -24,21 +24,21 @@ import java.util.zip.InflaterInputStream
public class ZLibUtils extends BinaryFileFunctions
{
- public final byte[] inflate(byte bytes[]) throws IOException
- // slow, probably.
- {
- ByteArrayInputStream in = new ByteArrayInputStream(bytes);
- InflaterInputStream zIn = new InflaterInputStream(in);
- return getStreamBytes(zIn);
- }
+ public final byte[] inflate(byte bytes[]) throws IOException
+ // slow, probably.
+ {
+ ByteArrayInputStream in = new ByteArrayInputStream(bytes);
+ InflaterInputStream zIn = new InflaterInputStream(in);
+ return getStreamBytes(zIn);
+ }
- public final byte[] deflate(byte bytes[]) throws IOException
- {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- DeflaterOutputStream dos = new DeflaterOutputStream(baos);
- dos.write(bytes);
- dos.close();
- return baos.toByteArray();
- }
+ public final byte[] deflate(byte bytes[]) throws IOException
+ {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ DeflaterOutputStream dos = new DeflaterOutputStream(baos);
+ dos.write(bytes);
+ dos.close();
+ return baos.toByteArray();
+ }
}
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSource.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSource.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSource.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSource.java Fri Sep 10 16:33:35 2010
@@ -23,45 +23,45 @@ import org.apache.sanselan.common.Binary
public abstract class ByteSource extends BinaryFileFunctions
{
- protected final String filename;
+ protected final String filename;
- public ByteSource(final String filename)
- {
- this.filename = filename;
- }
+ public ByteSource(final String filename)
+ {
+ this.filename = filename;
+ }
- public final InputStream getInputStream(int start) throws IOException
- {
- InputStream is = getInputStream();
+ public final InputStream getInputStream(int start) throws IOException
+ {
+ InputStream is = getInputStream();
- skipBytes(is, start);
+ skipBytes(is, start);
- return is;
- }
+ return is;
+ }
- public abstract InputStream getInputStream() throws IOException;
+ public abstract InputStream getInputStream() throws IOException;
- public abstract byte[] getBlock(int start, int length) throws IOException;
+ public abstract byte[] getBlock(int start, int length) throws IOException;
- public abstract byte[] getAll() throws IOException;
+ public abstract byte[] getAll() throws IOException;
- /*
- * This operation can be VERY expensive; for inputstream
- * byte sources, the entire stream must be drained to
- * determine its length.
- */
- public abstract long getLength() throws IOException;
+ /*
+ * This operation can be VERY expensive; for inputstream
+ * byte sources, the entire stream must be drained to
+ * determine its length.
+ */
+ public abstract long getLength() throws IOException;
- //
- // public byte[] getAll() throws IOException
- // {
- // return getBlock(0, (int) getLength());
- // }
+ //
+ // public byte[] getAll() throws IOException
+ // {
+ // return getBlock(0, (int) getLength());
+ // }
- public abstract String getDescription();
+ public abstract String getDescription();
- public final String getFilename()
- {
- return filename;
- }
+ public final String getFilename()
+ {
+ return filename;
+ }
}
\ No newline at end of file