001: package org.libtiff.jai.codecimpl;
002:
003: import org.libtiff.jai.codec.XTIFFTileCodecImpl;
004: import org.libtiff.jai.codec.XTIFFTileCodec;
005: import org.libtiff.jai.codec.XTIFFEncodeParam;
006: import org.libtiff.jai.codec.XTIFFField;
007: import org.libtiff.jai.codec.XTIFF;
008: import java.awt.Rectangle;
009:
010: /**
011: * This codec encapsulates all the logic for the default TIFF
012: * "uncompressed" bit-packing codec algorithm.
013: */
014: public class XTIFFUncompTileCodec extends XTIFFTileCodecImpl {
015:
016: public XTIFFUncompTileCodec() {
017: }
018:
019: public XTIFFTileCodec create() {
020: return new XTIFFUncompTileCodec();
021: }
022:
023: public boolean canEncode() {
024: return true;
025: }
026:
027: public void register() {
028: register(XTIFF.COMPRESSION_NONE);
029: }
030:
031: /**
032: * encode the tile into bpixels and return the byte size
033: * (uncompressed packing algorithm). The padding has already
034: * been done, so we may safely assume that pixels is exactly
035: * rows by cols by numBands ints.
036: */
037:
038: public int encodeTilePixels(int[] pixels, Rectangle rect,
039: byte[] bpixels) {
040:
041: int rows = (int) rect.getHeight();
042: int cols = (int) rect.getWidth();
043: int index, remainder;
044: int pixel = 0;
045: int k = 0;
046: int rowBytes = 0;
047:
048: switch (sampleSize[0]) {
049:
050: case 1:
051:
052: index = 0;
053: rowBytes = (cols + 7) / 8;
054:
055: // For each of the rows in a tile
056: for (int i = 0; i < rows; i++) {
057:
058: // Write out the number of pixels exactly
059: // divisible by 8 (bits per byte)
060: for (int j = 0; j < cols / 8; j++) {
061:
062: pixel = (pixels[index++] << 7)
063: | (pixels[index++] << 6)
064: | (pixels[index++] << 5)
065: | (pixels[index++] << 4)
066: | (pixels[index++] << 3)
067: | (pixels[index++] << 2)
068: | (pixels[index++] << 1) | pixels[index++];
069: bpixels[k++] = (byte) pixel;
070: }
071:
072: // Write out the pixels remaining after division by 8
073: if (cols % 8 > 0) {
074: pixel = 0;
075: for (int j = 0; j < cols % 8; j++) {
076: pixel |= (pixels[index++] << (7 - j));
077: }
078: bpixels[k++] = (byte) pixel;
079: }
080: } //row loop
081: break;
082:
083: case 4:
084:
085: index = 0;
086: rowBytes = (cols + 3) / 4;
087:
088: // For each of the rows in a strip
089: for (int i = 0; i < rows; i++) {
090:
091: // Write out the number of pixels that will fit into an
092: // even number of nibbles.
093: for (int j = 0; j < cols / 2; j++) {
094: pixel = (pixels[index++] << 4) | pixels[index++];
095: bpixels[k++] = (byte) pixel;
096: }
097:
098: // Last pixel for odd-length lines
099: if ((cols % 2) == 1) {
100: pixel = pixels[index++] << 4;
101: bpixels[k++] = (byte) pixel;
102: }
103: }
104: break;
105:
106: case 8:
107:
108: index = 0;
109: rowBytes = cols * numBands;
110: for (int i = 0; i < rows; i++) {
111: for (int j = 0; j < cols * numBands; j++) {
112: bpixels[k++] = (byte) pixels[index++];
113: }
114: }
115: break;
116:
117: case 16:
118:
119: index = 0;
120: rowBytes = cols * 2;
121: int l = 0;
122: for (int i = 0; i < rows; i++) {
123: for (int j = 0; j < cols; j++) {
124: short value = (short) pixels[index++];
125: bpixels[l++] = (byte) ((value & 0xff00) >> 8);
126: bpixels[l++] = (byte) (value & 0x00ff);
127: }
128: }
129: break;
130:
131: }
132: return rows * rowBytes;
133: }
134:
135: /**
136: * Decompress data packed bytes into packed bytes
137: */
138: public void decodeTilePixels(byte[] input, Rectangle rect,
139: byte[] bpixels) {
140: for (int i = 0; i < unitsInThisTile; i++) {
141: bpixels[i] = input[i];
142: }
143: }
144:
145: /**
146: * Decompress data packed bytes into short
147: */
148: public void decodeTilePixels(byte[] input, Rectangle rect,
149: short[] spixels) {
150: unpackShorts(input, spixels, unitsInThisTile);
151: }
152:
153: }
|