View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.imaging.formats.tiff.photometricinterpreters;
18  
19  import java.io.IOException;
20  import java.util.Arrays;
21  
22  import org.apache.commons.imaging.ImagingException;
23  import org.apache.commons.imaging.ImagingFormatException;
24  import org.apache.commons.imaging.common.AllocationRequestException;
25  import org.apache.commons.imaging.common.Allocator;
26  import org.apache.commons.imaging.common.ImageBuilder;
27  
28  public class PhotometricInterpreterPalette extends PhotometricInterpreter {
29  
30      /**
31       * The color map of integer ARGB values tied to the pixel index of the palette.
32       */
33      private final int[] indexColorMap;
34      private final int bitsPerPixelMask;
35  
36      /**
37       * Constructs a new instance.
38       *
39       * @param samplesPerPixel Samples per pixel.
40       * @param bitsPerSample   Bits per sample.
41       * @param predictor       TODO
42       * @param width           TODO
43       * @param height          TODO
44       * @param colorMap        TODO
45       *
46       * @throws ImagingFormatException     if an index into the {@code colorMap} is out of bounds.
47       * @throws AllocationRequestException Thrown when an allocation request exceeds the {@link Allocator} limit.
48       */
49      public PhotometricInterpreterPalette(final int samplesPerPixel, final int[] bitsPerSample, final int predictor, final int width, final int height,
50              final int[] colorMap) {
51          super(samplesPerPixel, bitsPerSample, predictor, width, height);
52  
53          final int bitsPerPixel = getBitsPerSample(0);
54          final int colorMapScale = 1 << bitsPerPixel;
55          int colorMapScaleX2;
56          try {
57              colorMapScaleX2 = Math.multiplyExact(2, colorMapScale);
58          } catch (final ArithmeticException e) {
59              throw new ImagingFormatException("bitsPerPixel is too large or colorMap is too small", e);
60          }
61          // Validate colorMap[i], colorMap[i + colorMapScale], and colorMap[i + colorMapScaleX2] where max(i) is
62          // colorMapScale - 1.
63          int maxI;
64          try {
65              maxI = Math.addExact(colorMapScaleX2, colorMapScale - 1);
66          } catch (final ArithmeticException e) {
67              throw new ImagingFormatException("bitsPerPixel is too large or colorMap is too small", e);
68          }
69          if (maxI >= colorMap.length) {
70              throw new ImagingFormatException("bitsPerPixel %,d (maxI = %,d) is too large or colorMap is too small %,d", bitsPerPixel, maxI, colorMap.length);
71          }
72          indexColorMap = Allocator.intArray(colorMapScale);
73          Arrays.setAll(indexColorMap, i -> {
74              final int red = colorMap[i] >> 8 & 0xff;
75              final int green = colorMap[i + colorMapScale] >> 8 & 0xff;
76              final int blue = colorMap[i + colorMapScaleX2] >> 8 & 0xff;
77              return 0xff000000 | red << 16 | green << 8 | blue;
78          });
79  
80          // Fix for IMAGING-247 5/17/2020
81          // This interpreter is used with TIFF_COMPRESSION_PACKBITS (32773).
82          // which unpacks to 8 bits per sample. But if the bits-per-pixel
83          // is less than 8 bits, some authoring tools do not zero-out the
84          // unused bits. This results in cases where the decoded by index
85          // exceeds the size of the palette. So we set up a mask to protect
86          // the code from an array bounds exception.
87          int temp = 0;
88          for (int i = 0; i < bitsPerPixel; i++) {
89              temp = temp << 1 | 1;
90          }
91          bitsPerPixelMask = temp;
92  
93      }
94  
95      @Override
96      public void interpretPixel(final ImageBuilder imageBuilder, final int[] samples, final int x, final int y) throws ImagingException, IOException {
97          imageBuilder.setRgb(x, y, indexColorMap[samples[0] & bitsPerPixelMask]);
98      }
99  }