1 /*
   2  * Copyright (c) 2005, 2016, 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 java.io.IOException;
  28 import java.util.zip.DataFormatException;
  29 import java.util.zip.Inflater;
  30 import javax.imageio.IIOException;
  31 import javax.imageio.plugins.tiff.BaselineTIFFTagSet;
  32 
  33 public class TIFFDeflateDecompressor extends TIFFDecompressor {
  34 
  35     Inflater inflater = null;
  36     int predictor;
  37 
  38     public TIFFDeflateDecompressor(int predictor) throws IIOException {
  39         inflater = new Inflater();
  40 
  41         if (predictor != BaselineTIFFTagSet.PREDICTOR_NONE &&
  42             predictor !=
  43             BaselineTIFFTagSet.PREDICTOR_HORIZONTAL_DIFFERENCING) {
  44             throw new IIOException("Illegal value for Predictor in " +
  45                                    "TIFF file");
  46         }
  47 
  48         this.predictor = predictor;
  49     }
  50 
  51     public synchronized void decodeRaw(byte[] b,
  52                                        int dstOffset,
  53                                        int bitsPerPixel,
  54                                        int scanlineStride) throws IOException {
  55 
  56         // Check bitsPerSample.
  57         if (predictor ==
  58             BaselineTIFFTagSet.PREDICTOR_HORIZONTAL_DIFFERENCING) {
  59             int len = bitsPerSample.length;
  60             for(int i = 0; i < len; i++) {
  61                 if(bitsPerSample[i] != 8) {
  62                     throw new IIOException
  63                         (bitsPerSample[i] + "-bit samples "+
  64                          "are not supported for Horizontal "+
  65                          "differencing Predictor");
  66                 }
  67             }
  68         }
  69 
  70         // Seek to current tile data offset.
  71         stream.seek(offset);
  72 
  73         // Read the deflated data.
  74         byte[] srcData = new byte[byteCount];
  75         stream.readFully(srcData);
  76 
  77         int bytesPerRow = (srcWidth*bitsPerPixel + 7)/8;
  78         byte[] buf;
  79         int bufOffset;
  80         if(bytesPerRow == scanlineStride) {
  81             buf = b;
  82             bufOffset = dstOffset;
  83         } else {
  84             buf = new byte[bytesPerRow*srcHeight];
  85             bufOffset = 0;
  86         }
  87 
  88         // Set the input to the Inflater.
  89         inflater.setInput(srcData);
  90 
  91         // Inflate the data.
  92         try {
  93             inflater.inflate(buf, bufOffset, bytesPerRow*srcHeight);
  94         } catch(DataFormatException dfe) {
  95             throw new IIOException("Error inflating data",
  96                                    dfe);
  97         }
  98 
  99         // Reset the Inflater.
 100         inflater.reset();
 101 
 102         if (predictor ==
 103             BaselineTIFFTagSet.PREDICTOR_HORIZONTAL_DIFFERENCING) {
 104             int step = planar || samplesPerPixel == 1 ? 1 : samplesPerPixel;
 105             int samplesPerRow = step * srcWidth;
 106 
 107             int off = bufOffset + step;
 108             for (int j = 0; j < srcHeight; j++) {
 109                 int count = off;
 110                 for (int i = step; i < samplesPerRow; i++) {
 111                     buf[count] += buf[count - step];
 112                     count++;
 113                 }
 114                 off += samplesPerRow;
 115             }
 116         }
 117 
 118         if(bytesPerRow != scanlineStride) {
 119             int off = 0;
 120             for (int y = 0; y < srcHeight; y++) {
 121                 System.arraycopy(buf, off, b, dstOffset, bytesPerRow);
 122                 off += bytesPerRow;
 123                 dstOffset += scanlineStride;
 124             }
 125         }
 126     }
 127 }