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.icc;
018
019import java.io.IOException;
020import java.io.PrintWriter;
021import java.io.StringWriter;
022import java.util.logging.Logger;
023
024import org.apache.commons.imaging.ImagingException;
025
026public class IccProfileInfo {
027
028    private static final Logger LOGGER = Logger.getLogger(IccProfileInfo.class.getName());
029
030    private final byte[] data;
031    public final int profileSize;
032    public final int cmmTypeSignature;
033    public final int profileVersion;
034    public final int profileDeviceClassSignature;
035    public final int colorSpace;
036    public final int profileConnectionSpace;
037    public final int profileFileSignature;
038    public final int primaryPlatformSignature;
039    public final int variousFlags;
040    public final int deviceManufacturer;
041    public final int deviceModel;
042    public final int renderingIntent;
043    public final int profileCreatorSignature;
044    private final byte[] profileId;
045    private final IccTag[] tags;
046
047    public IccProfileInfo(final byte[] data, final int profileSize, final int cmmTypeSignature, final int profileVersion, final int profileDeviceClassSignature,
048            final int colorSpace, final int profileConnectionSpace, final int profileFileSignature, final int primaryPlatformSignature, final int variousFlags,
049            final int deviceManufacturer, final int deviceModel, final int renderingIntent, final int profileCreatorSignature, final byte[] profileId,
050            final IccTag[] tags) {
051        this.data = data;
052
053        this.profileSize = profileSize;
054        this.cmmTypeSignature = cmmTypeSignature;
055        this.profileVersion = profileVersion;
056        this.profileDeviceClassSignature = profileDeviceClassSignature;
057        this.colorSpace = colorSpace;
058        this.profileConnectionSpace = profileConnectionSpace;
059        this.profileFileSignature = profileFileSignature;
060        this.primaryPlatformSignature = primaryPlatformSignature;
061        this.variousFlags = variousFlags;
062        this.deviceManufacturer = deviceManufacturer;
063        this.deviceModel = deviceModel;
064        this.renderingIntent = renderingIntent;
065        this.profileCreatorSignature = profileCreatorSignature;
066        this.profileId = profileId;
067
068        this.tags = tags;
069    }
070
071    public void dump(final String prefix) {
072        LOGGER.fine(toString());
073    }
074
075    public byte[] getData() {
076        return data.clone();
077    }
078
079    public byte[] getProfileId() {
080        return profileId.clone();
081    }
082
083    public IccTag[] getTags() {
084        return tags;
085    }
086
087    public boolean isSrgb() {
088        return deviceManufacturer == IccConstants.IEC && deviceModel == IccConstants.sRGB;
089    }
090
091    private void printCharQuad(final PrintWriter pw, final String msg, final int i) {
092        pw.println(msg + ": '" + (char) (0xff & i >> 24) + (char) (0xff & i >> 16) + (char) (0xff & i >> 8) + (char) (0xff & i >> 0) + "'");
093    }
094
095    @Override
096    public String toString() {
097        try {
098            return toString("");
099        } catch (final Exception e) {
100            return "IccProfileInfo: Error";
101        }
102    }
103
104    public String toString(final String prefix) throws ImagingException, IOException {
105        final StringWriter sw = new StringWriter();
106        final PrintWriter pw = new PrintWriter(sw);
107
108        pw.println(prefix + ": " + "data length: " + data.length);
109
110        printCharQuad(pw, prefix + ": " + "ProfileDeviceClassSignature", profileDeviceClassSignature);
111        printCharQuad(pw, prefix + ": " + "CMMTypeSignature", cmmTypeSignature);
112        printCharQuad(pw, prefix + ": " + "ProfileDeviceClassSignature", profileDeviceClassSignature);
113        printCharQuad(pw, prefix + ": " + "ColorSpace", colorSpace);
114        printCharQuad(pw, prefix + ": " + "ProfileConnectionSpace", profileConnectionSpace);
115        printCharQuad(pw, prefix + ": " + "ProfileFileSignature", profileFileSignature);
116        printCharQuad(pw, prefix + ": " + "PrimaryPlatformSignature", primaryPlatformSignature);
117        printCharQuad(pw, prefix + ": " + "ProfileFileSignature", profileFileSignature);
118        printCharQuad(pw, prefix + ": " + "DeviceManufacturer", deviceManufacturer);
119        printCharQuad(pw, prefix + ": " + "DeviceModel", deviceModel);
120        printCharQuad(pw, prefix + ": " + "RenderingIntent", renderingIntent);
121        printCharQuad(pw, prefix + ": " + "ProfileCreatorSignature", profileCreatorSignature);
122
123        for (int i = 0; i < tags.length; i++) {
124            final IccTag tag = tags[i];
125            tag.dump(pw, "\t" + i + ": ");
126        }
127
128        pw.println(prefix + ": " + "isSrgb: " + isSrgb());
129        pw.flush();
130
131        return sw.getBuffer().toString();
132    }
133
134}