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.util.logging.Logger;
24  
25  import org.apache.commons.imaging.icc.IccProfileInfo;
26  import org.apache.commons.imaging.icc.IccProfileParser;
27  
28  /**
29   * Used to store metadata and descriptive information extracted from
30   * 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  
45          case ColorSpace.CS_sRGB:
46              return "CS_sRGB";
47          case ColorSpace.CS_GRAY:
48              return "CS_GRAY";
49          case ColorSpace.CS_CIEXYZ:
50              return "CS_CIEXYZ";
51          case ColorSpace.CS_LINEAR_RGB:
52              return "CS_LINEAR_RGB";
53          case ColorSpace.CS_PYCC:
54              return "CS_PYCC";
55          default:
56              return "unknown";
57          }
58      }
59  
60      public void dumpColorSpace(final String prefix, final ColorSpace cs) {
61          LOGGER.fine(prefix + ": " + "type: " + cs.getType() + " ("
62                  + colorSpaceTypeToName(cs) + ")");
63  
64          if (!(cs instanceof ICC_ColorSpace)) {
65              LOGGER.fine(prefix + ": " + "Unknown ColorSpace: "
66                      + cs.getClass().getName());
67              return;
68          }
69  
70          final ICC_ColorSpace iccColorSpace = (ICC_ColorSpace) cs;
71          final ICC_Profile iccProfile = iccColorSpace.getProfile();
72  
73          final byte[] bytes = iccProfile.getData();
74  
75          final IccProfileParserileParser.html#IccProfileParser">IccProfileParser parser = new IccProfileParser();
76  
77          final IccProfileInfo info = parser.getICCProfileInfo(bytes);
78          info.dump(prefix);
79      }
80  
81      public void dump(final BufferedImage src) {
82          dump("", src);
83      }
84  
85      public void dump(final String prefix, final BufferedImage src) {
86          LOGGER.fine(prefix + ": " + "dump");
87          dumpColorSpace(prefix, src.getColorModel().getColorSpace());
88          dumpBIProps(prefix, src);
89      }
90  
91      public void dumpBIProps(final String prefix, final BufferedImage src) {
92          final String[] keys = src.getPropertyNames();
93          if (keys == null) {
94              LOGGER.fine(prefix + ": no props");
95              return;
96          }
97          for (final String key : keys) {
98              LOGGER.fine(prefix + ": " + key + ": "
99                      + src.getProperty(key));
100         }
101     }
102 
103 }