|
1 /* |
|
2 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 package com.sun.imageio.plugins.tiff; |
|
26 |
|
27 import javax.imageio.plugins.tiff.BaselineTIFFTagSet; |
|
28 import java.io.IOException; |
|
29 import java.util.zip.Deflater; |
|
30 import javax.imageio.ImageWriteParam; |
|
31 |
|
32 /** |
|
33 * Compressor superclass for Deflate and ZLib compression. |
|
34 */ |
|
35 public class TIFFDeflater extends TIFFCompressor { |
|
36 |
|
37 Deflater deflater; |
|
38 int predictor; |
|
39 |
|
40 public TIFFDeflater(String compressionType, |
|
41 int compressionTagValue, |
|
42 ImageWriteParam param, |
|
43 int predictorValue) { |
|
44 super(compressionType, compressionTagValue, true); |
|
45 |
|
46 this.predictor = predictorValue; |
|
47 |
|
48 // Set the deflate level. |
|
49 int deflateLevel; |
|
50 if(param != null && |
|
51 param.getCompressionMode() == ImageWriteParam.MODE_EXPLICIT) { |
|
52 float quality = param.getCompressionQuality(); |
|
53 deflateLevel = (int)(1 + 8*quality); |
|
54 } else { |
|
55 deflateLevel = Deflater.DEFAULT_COMPRESSION; |
|
56 } |
|
57 |
|
58 this.deflater = new Deflater(deflateLevel); |
|
59 } |
|
60 |
|
61 public int encode(byte[] b, int off, |
|
62 int width, int height, |
|
63 int[] bitsPerSample, |
|
64 int scanlineStride) throws IOException { |
|
65 |
|
66 int inputSize = height*scanlineStride; |
|
67 int blocks = (inputSize + 32767)/32768; |
|
68 |
|
69 // Worst case for Zlib deflate is input size + 5 bytes per 32k |
|
70 // block, plus 6 header bytes |
|
71 byte[] compData = new byte[inputSize + 5*blocks + 6]; |
|
72 |
|
73 int numCompressedBytes = 0; |
|
74 if(predictor == BaselineTIFFTagSet.PREDICTOR_HORIZONTAL_DIFFERENCING) { |
|
75 int samplesPerPixel = bitsPerSample.length; |
|
76 int bitsPerPixel = 0; |
|
77 for (int i = 0; i < samplesPerPixel; i++) { |
|
78 bitsPerPixel += bitsPerSample[i]; |
|
79 } |
|
80 int bytesPerRow = (bitsPerPixel*width + 7)/8; |
|
81 byte[] rowBuf = new byte[bytesPerRow]; |
|
82 |
|
83 int maxRow = height - 1; |
|
84 for(int i = 0; i < height; i++) { |
|
85 // Cannot modify b[] in place as it might be a data |
|
86 // array from the image being written so make a copy. |
|
87 System.arraycopy(b, off, rowBuf, 0, bytesPerRow); |
|
88 for(int j = bytesPerRow - 1; j >= samplesPerPixel; j--) { |
|
89 rowBuf[j] -= rowBuf[j - samplesPerPixel]; |
|
90 } |
|
91 |
|
92 deflater.setInput(rowBuf); |
|
93 if(i == maxRow) { |
|
94 deflater.finish(); |
|
95 } |
|
96 |
|
97 int numBytes = 0; |
|
98 while((numBytes = deflater.deflate(compData, |
|
99 numCompressedBytes, |
|
100 compData.length - |
|
101 numCompressedBytes)) != 0) { |
|
102 numCompressedBytes += numBytes; |
|
103 } |
|
104 |
|
105 off += scanlineStride; |
|
106 } |
|
107 } else { |
|
108 deflater.setInput(b, off, height*scanlineStride); |
|
109 deflater.finish(); |
|
110 |
|
111 numCompressedBytes = deflater.deflate(compData); |
|
112 } |
|
113 |
|
114 deflater.reset(); |
|
115 |
|
116 stream.write(compData, 0, numCompressedBytes); |
|
117 |
|
118 return numCompressedBytes; |
|
119 } |
|
120 } |