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.png.chunks;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.IOException;
21  
22  import org.apache.commons.imaging.ImageReadException;
23  import org.apache.commons.imaging.formats.png.GammaCorrection;
24  
25  import static org.apache.commons.imaging.common.BinaryFunctions.*;
26  
27  public class PngChunkPlte extends PngChunk {
28      private final int[] rgb;
29  
30      public PngChunkPlte(final int length, final int chunkType, final int crc, final byte[] bytes)
31              throws ImageReadException, IOException {
32          super(length, chunkType, crc, bytes);
33  
34          final ByteArrayInputStream is = new ByteArrayInputStream(bytes);
35  
36          if ((length % 3) != 0) {
37              throw new ImageReadException("PLTE: wrong length: " + length);
38          }
39  
40          final int count = length / 3;
41  
42          rgb = new int[count];
43  
44          for (int i = 0; i < count; i++) {
45              final int red = readByte("red[" + i + "]", is,
46                      "Not a Valid Png File: PLTE Corrupt");
47              final int green = readByte("green[" + i + "]", is,
48                      "Not a Valid Png File: PLTE Corrupt");
49              final int blue = readByte("blue[" + i + "]", is,
50                      "Not a Valid Png File: PLTE Corrupt");
51              rgb[i] = 0xff000000 | ((0xff & red) << 16) | ((0xff & green) << 8)
52                      | ((0xff & blue) << 0);
53          }
54      }
55  
56      public int[] getRgb() {
57          return rgb; // TODO clone? Is the method used?
58      }
59  
60      public int getRGB(final int index) throws ImageReadException {
61          if ((index < 0) || (index >= rgb.length)) {
62              throw new ImageReadException("PNG: unknown Palette reference: "
63                      + index);
64          }
65          return rgb[index];
66      }
67  
68      // public void printPalette() {
69      // Debug.debug();
70      // Debug.debug("palette");
71      // for (int i = 0; i < rgb.length; i++) {
72      // Debug.debug("\t" + "palette[" + i + "];", rgb[i] + " (0x"
73      // + Integer.toHexString(rgb[i]) + ")");
74      //
75      // }
76      // Debug.debug();
77      // }
78  
79      public void correct(final GammaCorrection gammaCorrection) {
80          for (int i = 0; i < rgb.length; i++) {
81              rgb[i] = gammaCorrection.correctARGB(rgb[i]);
82          }
83      }
84  
85  }