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