下面列出了javax.imageio.plugins.tiff.BaselineTIFFTagSet#PREDICTOR_HORIZONTAL_DIFFERENCING 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public TIFFDeflateDecompressor(int predictor) throws IIOException {
inflater = new Inflater();
if (predictor != BaselineTIFFTagSet.PREDICTOR_NONE &&
predictor !=
BaselineTIFFTagSet.PREDICTOR_HORIZONTAL_DIFFERENCING) {
throw new IIOException("Illegal value for Predictor in " +
"TIFF file");
}
this.predictor = predictor;
}
public TIFFLZWDecompressor(int predictor, int fillOrder)
throws IIOException {
super();
if (predictor != BaselineTIFFTagSet.PREDICTOR_NONE &&
predictor !=
BaselineTIFFTagSet.PREDICTOR_HORIZONTAL_DIFFERENCING) {
throw new IIOException("Illegal value for Predictor in " +
"TIFF file");
}
this.predictor = predictor;
flipBits = fillOrder == BaselineTIFFTagSet.FILL_ORDER_RIGHT_TO_LEFT;
}
public TIFFDeflateDecompressor(int predictor) throws IIOException {
inflater = new Inflater();
if (predictor != BaselineTIFFTagSet.PREDICTOR_NONE &&
predictor !=
BaselineTIFFTagSet.PREDICTOR_HORIZONTAL_DIFFERENCING) {
throw new IIOException("Illegal value for Predictor in " +
"TIFF file");
}
this.predictor = predictor;
}
public TIFFLZWDecompressor(int predictor, int fillOrder)
throws IIOException {
super();
if (predictor != BaselineTIFFTagSet.PREDICTOR_NONE &&
predictor !=
BaselineTIFFTagSet.PREDICTOR_HORIZONTAL_DIFFERENCING) {
throw new IIOException("Illegal value for Predictor in " +
"TIFF file");
}
this.predictor = predictor;
flipBits = fillOrder == BaselineTIFFTagSet.FILL_ORDER_RIGHT_TO_LEFT;
}
public int encode(byte[] b, int off,
int width, int height,
int[] bitsPerSample,
int scanlineStride) throws IOException {
LZWCompressor lzwCompressor = new LZWCompressor(stream, 8, true);
int samplesPerPixel = bitsPerSample.length;
int bitsPerPixel = 0;
for (int i = 0; i < samplesPerPixel; i++) {
bitsPerPixel += bitsPerSample[i];
}
int bytesPerRow = (bitsPerPixel*width + 7)/8;
long initialStreamPosition = stream.getStreamPosition();
boolean usePredictor =
predictor == BaselineTIFFTagSet.PREDICTOR_HORIZONTAL_DIFFERENCING;
if(bytesPerRow == scanlineStride && !usePredictor) {
lzwCompressor.compress(b, off, bytesPerRow*height);
} else {
byte[] rowBuf = usePredictor ? new byte[bytesPerRow] : null;
for(int i = 0; i < height; i++) {
if(usePredictor) {
// Cannot modify b[] in place as it might be a data
// array from the image being written so make a copy.
System.arraycopy(b, off, rowBuf, 0, bytesPerRow);
for(int j = bytesPerRow - 1; j >= samplesPerPixel; j--) {
rowBuf[j] -= rowBuf[j - samplesPerPixel];
}
lzwCompressor.compress(rowBuf, 0, bytesPerRow);
} else {
lzwCompressor.compress(b, off, bytesPerRow);
}
off += scanlineStride;
}
}
lzwCompressor.flush();
int bytesWritten =
(int)(stream.getStreamPosition() - initialStreamPosition);
return bytesWritten;
}
public void decodeRaw(byte[] b,
int dstOffset,
int bitsPerPixel,
int scanlineStride) throws IOException {
// Check bitsPerSample.
if (predictor ==
BaselineTIFFTagSet.PREDICTOR_HORIZONTAL_DIFFERENCING) {
int len = bitsPerSample.length;
for(int i = 0; i < len; i++) {
if(bitsPerSample[i] != 8) {
throw new IIOException
(bitsPerSample[i] + "-bit samples "+
"are not supported for Horizontal "+
"differencing Predictor");
}
}
}
stream.seek(offset);
byte[] sdata = new byte[byteCount];
stream.readFully(sdata);
if (flipBits) {
for (int i = 0; i < byteCount; i++) {
sdata[i] = TIFFFaxDecompressor.flipTable[sdata[i] & 0xff];
}
}
int bytesPerRow = (srcWidth*bitsPerPixel + 7)/8;
byte[] buf;
int bufOffset;
if(bytesPerRow == scanlineStride) {
buf = b;
bufOffset = dstOffset;
} else {
buf = new byte[bytesPerRow*srcHeight];
bufOffset = 0;
}
int numBytesDecoded = decode(sdata, 0, buf, bufOffset);
if(bytesPerRow != scanlineStride) {
int off = 0;
for (int y = 0; y < srcHeight; y++) {
System.arraycopy(buf, off, b, dstOffset, bytesPerRow);
off += bytesPerRow;
dstOffset += scanlineStride;
}
}
}
public int decode(byte[] sdata, int srcOffset,
byte[] ddata, int dstOffset)
throws IOException {
if (sdata[0] == (byte)0x00 && sdata[1] == (byte)0x01) {
throw new IIOException
("TIFF 5.0-style LZW compression is not supported!");
}
this.srcData = sdata;
this.dstData = ddata;
this.srcIndex = srcOffset;
this.dstIndex = dstOffset;
this.nextData = 0;
this.nextBits = 0;
initializeStringTable();
int code, oldCode = 0;
byte[] string;
while ((code = getNextCode()) != EOI_CODE) {
if (code == CLEAR_CODE) {
initializeStringTable();
code = getNextCode();
if (code == EOI_CODE) {
break;
}
writeString(stringTable[code]);
oldCode = code;
} else {
if (code < tableIndex) {
string = stringTable[code];
writeString(string);
addStringToTable(stringTable[oldCode], string[0]);
oldCode = code;
} else {
string = stringTable[oldCode];
string = composeString(string, string[0]);
writeString(string);
addStringToTable(string);
oldCode = code;
}
}
}
if (predictor ==
BaselineTIFFTagSet.PREDICTOR_HORIZONTAL_DIFFERENCING) {
int step = planar || samplesPerPixel == 1 ? 1 : samplesPerPixel;
int samplesPerRow = step * srcWidth;
int off = dstOffset + step;
for (int j = 0; j < srcHeight; j++) {
int count = off;
for (int i = step; i < samplesPerRow; i++) {
dstData[count] += dstData[count - step];
count++;
}
off += samplesPerRow;
}
}
return dstIndex - dstOffset;
}
public int encode(byte[] b, int off,
int width, int height,
int[] bitsPerSample,
int scanlineStride) throws IOException {
int inputSize = height*scanlineStride;
int blocks = (inputSize + 32767)/32768;
// Worst case for Zlib deflate is input size + 5 bytes per 32k
// block, plus 6 header bytes
byte[] compData = new byte[inputSize + 5*blocks + 6];
int numCompressedBytes = 0;
if(predictor == BaselineTIFFTagSet.PREDICTOR_HORIZONTAL_DIFFERENCING) {
int samplesPerPixel = bitsPerSample.length;
int bitsPerPixel = 0;
for (int i = 0; i < samplesPerPixel; i++) {
bitsPerPixel += bitsPerSample[i];
}
int bytesPerRow = (bitsPerPixel*width + 7)/8;
byte[] rowBuf = new byte[bytesPerRow];
int maxRow = height - 1;
for(int i = 0; i < height; i++) {
// Cannot modify b[] in place as it might be a data
// array from the image being written so make a copy.
System.arraycopy(b, off, rowBuf, 0, bytesPerRow);
for(int j = bytesPerRow - 1; j >= samplesPerPixel; j--) {
rowBuf[j] -= rowBuf[j - samplesPerPixel];
}
deflater.setInput(rowBuf);
if(i == maxRow) {
deflater.finish();
}
int numBytes = 0;
while((numBytes = deflater.deflate(compData,
numCompressedBytes,
compData.length -
numCompressedBytes)) != 0) {
numCompressedBytes += numBytes;
}
off += scanlineStride;
}
} else {
deflater.setInput(b, off, height*scanlineStride);
deflater.finish();
numCompressedBytes = deflater.deflate(compData);
}
deflater.reset();
stream.write(compData, 0, numCompressedBytes);
return numCompressedBytes;
}
public int encode(byte[] b, int off,
int width, int height,
int[] bitsPerSample,
int scanlineStride) throws IOException {
LZWCompressor lzwCompressor = new LZWCompressor(stream, 8, true);
int samplesPerPixel = bitsPerSample.length;
int bitsPerPixel = 0;
for (int i = 0; i < samplesPerPixel; i++) {
bitsPerPixel += bitsPerSample[i];
}
int bytesPerRow = (bitsPerPixel*width + 7)/8;
long initialStreamPosition = stream.getStreamPosition();
boolean usePredictor =
predictor == BaselineTIFFTagSet.PREDICTOR_HORIZONTAL_DIFFERENCING;
if(bytesPerRow == scanlineStride && !usePredictor) {
lzwCompressor.compress(b, off, bytesPerRow*height);
} else {
byte[] rowBuf = usePredictor ? new byte[bytesPerRow] : null;
for(int i = 0; i < height; i++) {
if(usePredictor) {
// Cannot modify b[] in place as it might be a data
// array from the image being written so make a copy.
System.arraycopy(b, off, rowBuf, 0, bytesPerRow);
for(int j = bytesPerRow - 1; j >= samplesPerPixel; j--) {
rowBuf[j] -= rowBuf[j - samplesPerPixel];
}
lzwCompressor.compress(rowBuf, 0, bytesPerRow);
} else {
lzwCompressor.compress(b, off, bytesPerRow);
}
off += scanlineStride;
}
}
lzwCompressor.flush();
int bytesWritten =
(int)(stream.getStreamPosition() - initialStreamPosition);
return bytesWritten;
}
public void decodeRaw(byte[] b,
int dstOffset,
int bitsPerPixel,
int scanlineStride) throws IOException {
// Check bitsPerSample.
if (predictor ==
BaselineTIFFTagSet.PREDICTOR_HORIZONTAL_DIFFERENCING) {
int len = bitsPerSample.length;
for(int i = 0; i < len; i++) {
if(bitsPerSample[i] != 8) {
throw new IIOException
(bitsPerSample[i] + "-bit samples "+
"are not supported for Horizontal "+
"differencing Predictor");
}
}
}
stream.seek(offset);
byte[] sdata = new byte[byteCount];
stream.readFully(sdata);
if (flipBits) {
for (int i = 0; i < byteCount; i++) {
sdata[i] = TIFFFaxDecompressor.flipTable[sdata[i] & 0xff];
}
}
int bytesPerRow = (srcWidth*bitsPerPixel + 7)/8;
byte[] buf;
int bufOffset;
if(bytesPerRow == scanlineStride) {
buf = b;
bufOffset = dstOffset;
} else {
buf = new byte[bytesPerRow*srcHeight];
bufOffset = 0;
}
int numBytesDecoded = decode(sdata, 0, buf, bufOffset);
if(bytesPerRow != scanlineStride) {
int off = 0;
for (int y = 0; y < srcHeight; y++) {
System.arraycopy(buf, off, b, dstOffset, bytesPerRow);
off += bytesPerRow;
dstOffset += scanlineStride;
}
}
}
public int decode(byte[] sdata, int srcOffset,
byte[] ddata, int dstOffset)
throws IOException {
if (sdata[0] == (byte)0x00 && sdata[1] == (byte)0x01) {
throw new IIOException
("TIFF 5.0-style LZW compression is not supported!");
}
this.srcData = sdata;
this.dstData = ddata;
this.srcIndex = srcOffset;
this.dstIndex = dstOffset;
this.nextData = 0;
this.nextBits = 0;
initializeStringTable();
int code, oldCode = 0;
byte[] string;
while ((code = getNextCode()) != EOI_CODE) {
if (code == CLEAR_CODE) {
initializeStringTable();
code = getNextCode();
if (code == EOI_CODE) {
break;
}
writeString(stringTable[code]);
oldCode = code;
} else {
if (code < tableIndex) {
string = stringTable[code];
writeString(string);
addStringToTable(stringTable[oldCode], string[0]);
oldCode = code;
} else {
string = stringTable[oldCode];
string = composeString(string, string[0]);
writeString(string);
addStringToTable(string);
oldCode = code;
}
}
}
if (predictor ==
BaselineTIFFTagSet.PREDICTOR_HORIZONTAL_DIFFERENCING) {
int step = planar || samplesPerPixel == 1 ? 1 : samplesPerPixel;
int samplesPerRow = step * srcWidth;
int off = dstOffset + step;
for (int j = 0; j < srcHeight; j++) {
int count = off;
for (int i = step; i < samplesPerRow; i++) {
dstData[count] += dstData[count - step];
count++;
}
off += samplesPerRow;
}
}
return dstIndex - dstOffset;
}
public int encode(byte[] b, int off,
int width, int height,
int[] bitsPerSample,
int scanlineStride) throws IOException {
int inputSize = height*scanlineStride;
int blocks = (inputSize + 32767)/32768;
// Worst case for Zlib deflate is input size + 5 bytes per 32k
// block, plus 6 header bytes
byte[] compData = new byte[inputSize + 5*blocks + 6];
int numCompressedBytes = 0;
if(predictor == BaselineTIFFTagSet.PREDICTOR_HORIZONTAL_DIFFERENCING) {
int samplesPerPixel = bitsPerSample.length;
int bitsPerPixel = 0;
for (int i = 0; i < samplesPerPixel; i++) {
bitsPerPixel += bitsPerSample[i];
}
int bytesPerRow = (bitsPerPixel*width + 7)/8;
byte[] rowBuf = new byte[bytesPerRow];
int maxRow = height - 1;
for(int i = 0; i < height; i++) {
// Cannot modify b[] in place as it might be a data
// array from the image being written so make a copy.
System.arraycopy(b, off, rowBuf, 0, bytesPerRow);
for(int j = bytesPerRow - 1; j >= samplesPerPixel; j--) {
rowBuf[j] -= rowBuf[j - samplesPerPixel];
}
deflater.setInput(rowBuf);
if(i == maxRow) {
deflater.finish();
}
int numBytes = 0;
while((numBytes = deflater.deflate(compData,
numCompressedBytes,
compData.length -
numCompressedBytes)) != 0) {
numCompressedBytes += numBytes;
}
off += scanlineStride;
}
} else {
deflater.setInput(b, off, height*scanlineStride);
deflater.finish();
numCompressedBytes = deflater.deflate(compData);
}
deflater.reset();
stream.write(compData, 0, numCompressedBytes);
return numCompressedBytes;
}