    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.ImageReadException;
    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,
    048            final int profileVersion, final int profileDeviceClassSignature,
    049            final int colorSpace, final int profileConnectionSpace,
    050            final int profileFileSignature, final int primaryPlatformSignature,
    051            final int variousFlags, final int deviceManufacturer, final int deviceModel,
    052            final int renderingIntent, final int profileCreatorSignature, final byte[] profileId,
    053            final IccTag[] tags) {
    054        this.data = data;
    055
    056        this.profileSize = profileSize;
    057        this.cmmTypeSignature = cmmTypeSignature;
    058        this.profileVersion = profileVersion;
    059        this.profileDeviceClassSignature = profileDeviceClassSignature;
    060        this.colorSpace = colorSpace;
    061        this.profileConnectionSpace = profileConnectionSpace;
    062        this.profileFileSignature = profileFileSignature;
    063        this.primaryPlatformSignature = primaryPlatformSignature;
    064        this.variousFlags = variousFlags;
    065        this.deviceManufacturer = deviceManufacturer;
    066        this.deviceModel = deviceModel;
    067        this.renderingIntent = renderingIntent;
    068        this.profileCreatorSignature = profileCreatorSignature;
    069        this.profileId = profileId;
    070
    071        this.tags = tags;
    072    }
    073
    074    public byte[] getData() {
    075        return data.clone();
    076    }
    077
    078    public byte[] getProfileId() {
    079        return profileId.clone();
    080    }
    081
    082    public IccTag[] getTags() {
    083        return tags;
    084    }
    085
    086    public boolean issRGB() {
    087        return deviceManufacturer == IccConstants.IEC
    088                && 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))
    093                + (char) (0xff & (i >> 16)) + (char) (0xff & (i >> 8))
    094                + (char) (0xff & (i >> 0)) + "'");
    095    }
    096
    097    public void dump(final String prefix) {
    098        LOGGER.fine(toString());
    099    }
    100
    101    @Override
    102    public String toString() {
    103        try {
    104            return toString("");
    105        } catch (final Exception e) {
    106            return "IccProfileInfo: Error";
    107        }
    108    }
    109
    110    public String toString(final String prefix) throws ImageReadException,
    111            IOException {
    112        final StringWriter sw = new StringWriter();
    113        final PrintWriter pw = new PrintWriter(sw);
    114
    115        pw.println(prefix + ": " + "data length: " + data.length);
    116
    117        printCharQuad(pw, prefix + ": " + "ProfileDeviceClassSignature", profileDeviceClassSignature);
    118        printCharQuad(pw, prefix + ": " + "CMMTypeSignature", cmmTypeSignature);
    119        printCharQuad(pw, prefix + ": " + "ProfileDeviceClassSignature", profileDeviceClassSignature);
    120        printCharQuad(pw, prefix + ": " + "ColorSpace", colorSpace);
    121        printCharQuad(pw, prefix + ": " + "ProfileConnectionSpace", profileConnectionSpace);
    122        printCharQuad(pw, prefix + ": " + "ProfileFileSignature", profileFileSignature);
    123        printCharQuad(pw, prefix + ": " + "PrimaryPlatformSignature", primaryPlatformSignature);
    124        printCharQuad(pw, prefix + ": " + "ProfileFileSignature", profileFileSignature);
    125        printCharQuad(pw, prefix + ": " + "DeviceManufacturer", deviceManufacturer);
    126        printCharQuad(pw, prefix + ": " + "DeviceModel", deviceModel);
    127        printCharQuad(pw, prefix + ": " + "RenderingIntent", renderingIntent);
    128        printCharQuad(pw, prefix + ": " + "ProfileCreatorSignature", profileCreatorSignature);
    129
    130        for (int i = 0; i < tags.length; i++) {
    131            final IccTag tag = tags[i];
    132            tag.dump(pw, "\t" + i + ": ");
    133        }
    134
    135        pw.println(prefix + ": " + "issRGB: " + issRGB());
    136        pw.flush();
    137
    138        return sw.getBuffer().toString();
    139    }
    140
    141}