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.io.IOException;
024import java.util.logging.Logger;
025
026import org.apache.commons.imaging.icc.IccProfileInfo;
027import org.apache.commons.imaging.icc.IccProfileParser;
028
029/**
030 * Used to store metadata and descriptive information extracted from 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        case ColorSpace.CS_sRGB:
045            return "CS_sRGB";
046        case ColorSpace.CS_GRAY:
047            return "CS_GRAY";
048        case ColorSpace.CS_CIEXYZ:
049            return "CS_CIEXYZ";
050        case ColorSpace.CS_LINEAR_RGB:
051            return "CS_LINEAR_RGB";
052        case ColorSpace.CS_PYCC:
053            return "CS_PYCC";
054        default:
055            return "unknown";
056        }
057    }
058
059    public void dump(final BufferedImage src) throws IOException {
060        dump("", src);
061    }
062
063    public void dump(final String prefix, final BufferedImage src) throws IOException {
064        LOGGER.fine(prefix + ": " + "dump");
065        dumpColorSpace(prefix, src.getColorModel().getColorSpace());
066        dumpBiProps(prefix, src);
067    }
068
069    public void dumpBiProps(final String prefix, final BufferedImage src) {
070        final String[] keys = src.getPropertyNames();
071        if (keys == null) {
072            LOGGER.fine(prefix + ": no props");
073            return;
074        }
075        for (final String key : keys) {
076            LOGGER.fine(prefix + ": " + key + ": " + src.getProperty(key));
077        }
078    }
079
080    public void dumpColorSpace(final String prefix, final ColorSpace cs) throws IOException {
081        LOGGER.fine(prefix + ": " + "type: " + cs.getType() + " (" + colorSpaceTypeToName(cs) + ")");
082
083        if (!(cs instanceof ICC_ColorSpace)) {
084            LOGGER.fine(prefix + ": " + "Unknown ColorSpace: " + cs.getClass().getName());
085            return;
086        }
087
088        final ICC_ColorSpace iccColorSpace = (ICC_ColorSpace) cs;
089        final ICC_Profile iccProfile = iccColorSpace.getProfile();
090
091        final byte[] bytes = iccProfile.getData();
092
093        final IccProfileParser parser = new IccProfileParser();
094
095        final IccProfileInfo info = parser.getIccProfileInfo(bytes);
096        info.dump(prefix);
097    }
098
099}