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;
18  
19  import java.awt.color.ColorSpace;
20  import java.awt.color.ICC_ColorSpace;
21  import java.awt.color.ICC_Profile;
22  import java.awt.image.BufferedImage;
23  import java.io.IOException;
24  import java.util.logging.Logger;
25  
26  import org.apache.commons.imaging.icc.IccProfileInfo;
27  import org.apache.commons.imaging.icc.IccProfileParser;
28  
29  /**
30   * Used to store metadata and descriptive information extracted from image files.
31   */
32  public class ImageDump {
33  
34      private static final Logger LOGGER = Logger.getLogger(ImageDump.class.getName());
35  
36      private String colorSpaceTypeToName(final ColorSpace cs) {
37          // System.out.println(prefix + ": " + "type: "
38          // + cs.getType() );
39          switch (cs.getType()) {
40          case ColorSpace.TYPE_CMYK:
41              return "TYPE_CMYK";
42          case ColorSpace.TYPE_RGB:
43              return "TYPE_RGB";
44          case ColorSpace.CS_sRGB:
45              return "CS_sRGB";
46          case ColorSpace.CS_GRAY:
47              return "CS_GRAY";
48          case ColorSpace.CS_CIEXYZ:
49              return "CS_CIEXYZ";
50          case ColorSpace.CS_LINEAR_RGB:
51              return "CS_LINEAR_RGB";
52          case ColorSpace.CS_PYCC:
53              return "CS_PYCC";
54          default:
55              return "unknown";
56          }
57      }
58  
59      public void dump(final BufferedImage src) throws IOException {
60          dump("", src);
61      }
62  
63      public void dump(final String prefix, final BufferedImage src) throws IOException {
64          LOGGER.fine(prefix + ": " + "dump");
65          dumpColorSpace(prefix, src.getColorModel().getColorSpace());
66          dumpBiProps(prefix, src);
67      }
68  
69      public void dumpBiProps(final String prefix, final BufferedImage src) {
70          final String[] keys = src.getPropertyNames();
71          if (keys == null) {
72              LOGGER.fine(prefix + ": no props");
73              return;
74          }
75          for (final String key : keys) {
76              LOGGER.fine(prefix + ": " + key + ": " + src.getProperty(key));
77          }
78      }
79  
80      public void dumpColorSpace(final String prefix, final ColorSpace cs) throws IOException {
81          LOGGER.fine(prefix + ": " + "type: " + cs.getType() + " (" + colorSpaceTypeToName(cs) + ")");
82  
83          if (!(cs instanceof ICC_ColorSpace)) {
84              LOGGER.fine(prefix + ": " + "Unknown ColorSpace: " + cs.getClass().getName());
85              return;
86          }
87  
88          final ICC_ColorSpace iccColorSpace = (ICC_ColorSpace) cs;
89          final ICC_Profile iccProfile = iccColorSpace.getProfile();
90  
91          final byte[] bytes = iccProfile.getData();
92  
93          final IccProfileParser parser = new IccProfileParser();
94  
95          final IccProfileInfo info = parser.getIccProfileInfo(bytes);
96          info.dump(prefix);
97      }
98  
99  }