001: /*
002: * $RCSfile: FPXUtils.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:55:41 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.codecimpl.fpx;
013:
014: import java.io.IOException;
015: import java.text.DecimalFormat;
016: import com.sun.media.jai.codec.SeekableStream;
017:
018: public class FPXUtils {
019:
020: // /** Reads a 2-byte little-endian value. */
021: // public static final int readInt2(SeekableStream file, int offset)
022: // throws IOException {
023: // file.seek(offset);
024: // return file.readShortLE();
025: // }
026:
027: // /** Reads a 4-byte little-endian value. */
028: // public static final int readInt4(SeekableStream file, int offset)
029: // throws IOException {
030: // file.seek(offset);
031: // return file.readIntLE();
032: // }
033:
034: // /** Reads a 4-byte little-endian IEEE float. */
035: // public static final float readFloat(SeekableStream file, int offset)
036: // throws IOException {
037: // file.seek(offset);
038: // return file.readFloatLE();
039: // }
040:
041: // /** Reads an 8-byte little-endian IEEE double. */
042: // public static final double readDouble(SeekableStream file,
043: // int offset)
044: // throws IOException {
045: // file.seek(offset);
046: // return file.readDoubleLE();
047: // }
048:
049: public static final short getShortLE(byte[] data, int offset) {
050: int b0 = data[offset] & 0xff;
051: int b1 = data[offset + 1] & 0xff;
052:
053: return (short) ((b1 << 8) | b0);
054: }
055:
056: public static final int getUnsignedShortLE(byte[] data, int offset) {
057: int b0 = data[offset] & 0xff;
058: int b1 = data[offset + 1] & 0xff;
059:
060: return (b1 << 8) | b0;
061: }
062:
063: public static final int getIntLE(byte[] data, int offset) {
064: int b0 = data[offset] & 0xff;
065: int b1 = data[offset + 1] & 0xff;
066: int b2 = data[offset + 2] & 0xff;
067: int b3 = data[offset + 3] & 0xff;
068:
069: return (b3 << 24) | (b2 << 16) | (b1 << 8) | b0;
070: }
071:
072: public static final long getUnsignedIntLE(byte[] data, int offset) {
073: long b0 = data[offset] & 0xff;
074: long b1 = data[offset + 1] & 0xff;
075: long b2 = data[offset + 2] & 0xff;
076: long b3 = data[offset + 3] & 0xff;
077:
078: return (b3 << 24) | (b2 << 16) | (b1 << 8) | b0;
079: }
080:
081: public static final String getString(byte[] data, int offset,
082: int length) {
083: if (length == 0) {
084: return "<none>";
085: } else {
086: length = length / 2 - 1; // workaround for Kodak bug
087: }
088: StringBuffer b = new StringBuffer(length);
089: for (int i = 0; i < length; i++) {
090: int c = getUnsignedShortLE(data, offset);
091: b.append((char) c);
092: offset += 2;
093: }
094:
095: return b.toString();
096: }
097:
098: private static void printDecimal(int i) {
099: DecimalFormat d = new DecimalFormat("00000");
100: System.out.print(d.format(i));
101: }
102:
103: private static void printHex(byte b) {
104: int i = b & 0xff;
105: int hi = i / 16;
106: int lo = i % 16;
107:
108: if (hi < 10) {
109: System.out.print((char) ('0' + hi));
110: } else {
111: System.out.print((char) ('a' + hi - 10));
112: }
113:
114: if (lo < 10) {
115: System.out.print((char) ('0' + lo));
116: } else {
117: System.out.print((char) ('a' + lo - 10));
118: }
119: }
120:
121: private static void printChar(byte b) {
122: char c = (char) (b & 0xff);
123:
124: if (c >= '!' && c <= '~') {
125: System.out.print(' ');
126: System.out.print(c);
127: } else if (c == 0) {
128: System.out.print("^@");
129: } else if (c < ' ') {
130: System.out.print('^');
131: System.out.print((char) ('A' + c - 1));
132: } else if (c == ' ') {
133: System.out.print("__");
134: } else {
135: System.out.print("??");
136: }
137: }
138:
139: public static void dumpBuffer(byte[] buf, int offset, int length,
140: int printOffset) {
141: int lines = length / 8;
142:
143: for (int j = 0; j < lines; j++) {
144: printDecimal(printOffset);
145: System.out.print(": ");
146:
147: for (int i = 0; i < 8; i++) {
148: printHex(buf[offset + i]);
149: System.out.print(" ");
150: }
151: for (int i = 0; i < 8; i++) {
152: printChar(buf[offset + i]);
153: System.out.print(" ");
154: }
155:
156: offset += 8;
157: printOffset += 8;
158: System.out.println();
159: }
160: }
161: }
|