001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.imaging;
018
019import java.awt.color.ColorSpace;
020import java.awt.color.ICC_ColorSpace;
021import java.awt.color.ICC_Profile;
022import java.awt.image.BufferedImage;
023import java.util.logging.Logger;
024
025import org.apache.commons.imaging.icc.IccProfileInfo;
026import org.apache.commons.imaging.icc.IccProfileParser;
027
028/**
029 * Used to store metadata and descriptive information extracted from
030 * image files.
031 */
032public class ImageDump {
033
034    private static final Logger LOGGER = Logger.getLogger(ImageDump.class.getName());
035
036    private String colorSpaceTypeToName(final ColorSpace cs) {
037        // System.out.println(prefix + ": " + "type: "
038        // + cs.getType() );
039        switch (cs.getType()) {
040        case ColorSpace.TYPE_CMYK:
041            return "TYPE_CMYK";
042        case ColorSpace.TYPE_RGB:
043            return "TYPE_RGB";
044
045        case ColorSpace.CS_sRGB:
046            return "CS_sRGB";
047        case ColorSpace.CS_GRAY:
048            return "CS_GRAY";
049        case ColorSpace.CS_CIEXYZ:
050            return "CS_CIEXYZ";
051        case ColorSpace.CS_LINEAR_RGB:
052            return "CS_LINEAR_RGB";
053        case ColorSpace.CS_PYCC:
054            return "CS_PYCC";
055        default:
056            return "unknown";
057        }
058    }
059
060    public void dumpColorSpace(final String prefix, final ColorSpace cs) {
061        LOGGER.fine(prefix + ": " + "type: " + cs.getType() + " ("
062                + colorSpaceTypeToName(cs) + ")");
063
064        if (!(cs instanceof ICC_ColorSpace)) {
065            LOGGER.fine(prefix + ": " + "Unknown ColorSpace: "
066                    + cs.getClass().getName());
067            return;
068        }
069
070        final ICC_ColorSpace iccColorSpace = (ICC_ColorSpace) cs;
071        final ICC_Profile iccProfile = iccColorSpace.getProfile();
072
073        final byte[] bytes = iccProfile.getData();
074
075        final IccProfileParser parser = new IccProfileParser();
076
077        final IccProfileInfo info = parser.getICCProfileInfo(bytes);
078        info.dump(prefix);
079    }
080
081    public void dump(final BufferedImage src) {
082        dump("", src);
083    }
084
085    public void dump(final String prefix, final BufferedImage src) {
086        LOGGER.fine(prefix + ": " + "dump");
087        dumpColorSpace(prefix, src.getColorModel().getColorSpace());
088        dumpBIProps(prefix, src);
089    }
090
091    public void dumpBIProps(final String prefix, final BufferedImage src) {
092        final String[] keys = src.getPropertyNames();
093        if (keys == null) {
094            LOGGER.fine(prefix + ": no props");
095            return;
096        }
097        for (final String key : keys) {
098            LOGGER.fine(prefix + ": " + key + ": "
099                    + src.getProperty(key));
100        }
101    }
102
103}