Zorian
Zorian

Reputation: 175

Java extract colorSpace and PCS from ICC_Profile to verify the integrety of an image

First (I forgot to say I'm new to java and it's my first post here, so if I make some mistakes, pls correct me ;) ) I got the problem, that I got a lot of images. Between these images are some which are corruped (e.g. a picture with ICC-Profile CMYK and Lab has the pixel-scheme RGB or other example ICC: RGB and XYZ and pixel-scheme CMYK). I need to programm a Java application to test these files for this specific category.

FileTypes are: JPG,TIFF,GIF,PNG and BMP.

I installed already ImageIO and ImageIO from twelvemonkeys to accept CMYK-Files. I tried to read out the Meta/exif-data and also the ICC-data but it didn't show me the information I need.

I tried to read out the Data with PhotoMe but therefor I have to look up all files one by one and compare it by hand. So I knew the data is in the files and not empty.

So the goal is to read out the metadata and ICC-Profil and compare the specific values.

Thanks Zorian

Edit: I managed to get the Pixel-Scheme, I think, but I can't acces the other informations. Here's the code to acces the Pixel-Scheme (or ColorSpace). So Have u got an idea of getting the other things?

int cst = 0;
ColorSpace cs= image.getColorModel().getColorSpace();
System.out.println("--> "+cs.getType());
cst = cs.getType();
PixelScheme pisc = new PixelScheme();
pisc.showPixelSchema(cst);
log.append("Pixel-Scheme = " + pisc.showPixelScheme(cst) + "\n");



//Class Pixel-Scheme
//the Case values taken from a table from java.awt.color.ColorSpace;

package components;

import java.awt.color.ColorSpace;


public class PixelSchema {

    public String showPixelSchema (int cst)
    {

            switch (cst)
            {
                case 0: ptype = "XYZ";
                    break;
                case 1: ptype = "Lab";
                    break;
                case 2: ptype = "Luv";
                    break;
                case 3: ptype = "YCbCr";
                    break;
                case 4: ptype = "Yxy";
                    break;
                case 5: ptype = "RGB";
                    break;
                case 6: ptype = "GRAY";
                    break;
                case 7: ptype = "HSV";
                    break;
                case 8: ptype = "HLS";
                    break;
                case 9: ptype = "CMYK";
                    break;
                case 11: ptype = "CMY";
                    break;
                case 12: ptype = "2CLR";
                    break;
                case 13: ptype = "3CLR";
                    break;
                case 14: ptype = "4CLR";
                    break;
                case 15: ptype = "5CLR";
                    break;
                case 16: ptype = "6CLR";
                    break;
                case 17: ptype = "7CLR";
                    break;
                case 18: ptype = "8CLR";
                    break;
                case 19: ptype = "9CLR";
                    break;
                case 20: ptype = "ACLR";
                    break;
                case 21: ptype = "BCLR";
                    break;
                case 22: ptype = "CCLR";
                    break;
                case 23: ptype = "DCLR";
                    break;
                case 24: ptype = "ECLR";
                    break;
                case 25: ptype = "FCLR";
                    break;
                case 1000: ptype = "sRGB";
                    break;
                case 1001: ptype = "CIEXYZ";
                    break;
                case 1002: ptype = "PYCC";
                    break;
                case 1003: ptype = "GRAY";
                    break; 
                case 1004: ptype = "LINEAR_RGB";
                    break; 
                default: ptype = "ERROR!";
                    break;
            }

        return ptype;
        }
}

Update: I implemented the snippet but got still an error in Netbeans but somehow it work and display me the needed data.

public void ICC(File file) throws IOException
    {
      //Error indicated cause of file (but I don't know an other way to import my image (tried to convert it to some kinds of arrays but it won't work))
      Map<MetadataType, Metadata> metadataMap = Metadata.readMetadata(file));
            ICCProfile icc_profile = (ICCProfile)metadataMap.get(MetadataType.ICC_PROFILE);

            if(icc_profile != null) {
                icc_profile.showMetadata();
                log.append("PCS = ");
                log.append("Color Space = ");
            }   
    }

But now my question is: Is there the possibility to get only this two pieces of data [Color Space: CMYK + PCS: Lab] out of the header and write them to a variable? (The two ones in the brackets) I need it because I have to display the results of the Color Space and PCS and compare it to the PixelScheme.


Update 2: I finally got to all data I need, but still got the problem from Update 1! Here's my code snippet for this Error!

public void ICC(File file) throws IOException
{          

Map<MetadataType, Metadata> metadataMap = Metadata.readMetadata(file);
ICCProfile icc_profile = (ICCProfile)metadataMap.get(MetadataType.ICC_PROFILE);

                 if(icc_profile != null) 
                 {
                    icc_profile.showMetadata();
                    String iccs =  icc_profile.getReader().getColorSpace();
                    String icpcs = icc_profile.getReader().getPCS();
                    log.append("Color Space = " + iccs + "\n");
                    log.append("PCS = " + icpcs + "\n");
                  }   
}

Update 3: Error found ;) it was 1 ) too much

Thanks a lot for this lib!

Upvotes: 1

Views: 1411

Answers (1)

dragon66
dragon66

Reputation: 2715

You may want to try icafe. It support extracting ICC_Profile from TIFF, JPEG, PNG. The following is some information from a specific run:

*** Start of ICC_Profile Header ***
Profile Size: 3144
CMM Type: Lino
Version: 2.10
Profile/Device Class: 'mntr': display devices - CRTs and LCDs
Color Space: RGB 
PCS: XYZ 
Date Created: 1998/2/9, 6:49:0
Profile File Signature: acsp
Primary Platform Signature: MSFT
Flags: not embedded, used independently
Device Manufacturer: IEC 
Device Model: sRGB
Device Attributes: reflective, glossy, positive, color
Rendering Intent: media-relative colorimetric
PCS Illuminant: X = 0.9642029, Y = 1.0, Z = 0.8249054
Profile Creator: HP  
Profile ID: [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]
*** End of ICC_Profile Header ***
*** Start of ICC_Profile Tag Table ***
Tag Count: 17
Tag# 0, Tag Name: bTRC (blueTRCTag), Data Offset: 1084, Data Length: 2060
Tag# 1, Tag Name: bXYZ (blueMatrixColumnTag), Data Offset: 576, Data Length: 20
Tag# 2, Tag Name: BKPT (mediaBlackPointTag), Data Offset: 516, Data Length: 20
Tag# 3, Tag Name: cprt (copyrightTag), Data Offset: 336, Data Length: 51
Tag# 4, Tag Name: desc (profileDescriptionTag), Data Offset: 388, Data Length: 108
Tag# 5, Tag Name: dmdd (deviceModelDescTag), Data Offset: 708, Data Length: 136
Tag# 6, Tag Name: dmnd (deviceMfgDescTag), Data Offset: 596, Data Length: 112
Tag# 7, Tag Name: gTRC (greenTRCTag), Data Offset: 1084, Data Length: 2060
Tag# 8, Tag Name: gXYZ (greenMatrixColumnTag), Data Offset: 556, Data Length: 20
Tag# 9, Tag Name: lumi (luminanceTag), Data Offset: 1016, Data Length: 20
Tag# 10, Tag Name: meas (measurementTag), Data Offset: 1036, Data Length: 36
Tag# 11, Tag Name: rTRC (redTRCTag), Data Offset: 1084, Data Length: 2060
Tag# 12, Tag Name: rXYZ (redMatrixColumnTag), Data Offset: 536, Data Length: 20
Tag# 13, Tag Name: tech (technologyTag), Data Offset: 1072, Data Length: 12
Tag# 14, Tag Name: view (viewingConditionsTag), Data Offset: 980, Data Length: 36
Tag# 15, Tag Name: vued (viewingCondDescTag), Data Offset: 844, Data Length: 134
Tag# 16, Tag Name: wtpt (mediaWhitePointTag), Data Offset: 496, Data Length: 20
*** End of ICC_Profile Tag Table ***

Here is a code sample to start with:

    import java.io.IOException;
    import java.util.Collection;
    import java.util.Iterator;

    import com.icafe4j.image.meta.Metadata;
    import com.icafe4j.image.meta.MetadataEntry;
    import com.icafe4j.image.meta.MetadataType;
    import com.icafe4j.image.meta.icc.ICCProfile;

    public class ExtractICCProfile {

        public static void main(String[] args) throws IOException {
            Map<MetadataType, Metadata> metadataMap = Metadata.readMetadata(args[0]);
            ICCProfile icc_profile = (ICCProfile)metadataMap.get(MetadataType.ICC_PROFILE);

            if(icc_profile != null) {
                Iterator<MetadataEntry> iterator = icc_profile.iterator();

                while(iterator.hasNext()) {
                    MetadataEntry item = iterator.next();
                    printMetadata(item, "", "     ");
                }
            }   
        }
        private void printMetadata(MetadataEntry entry, String indent, String increment) {
            logger.info(indent + entry.getKey() (StringUtils.isNullOrEmpty(entry.getValue())? "" : ": " + entry.getValue()));
            if(entry.isMetadataEntryGroup()) {
                 indent += increment;
                 Collection<MetadataEntry> entries = entry.getMetadataEntries();
                 for(MetadataEntry e : entries) {
                    printMetadata(e, indent, increment);
                 }          
            }
        }   
    }

Grab "icafe.jar" from the "lib" directory of the above link and you are good to go test with the code sample.

Update: your can show only colorSpace and PCS like this:

if(icc_profile != null) 
{  
   String colorSpace = icc_profile.getColorSpace();
   String pcs = icc_profile.getPCS();
}

Update 2:

If you need to read further data from a specific tag, you can grab the tag table first and then get the tag entry you are interested in. Here is a snippet:

    public void readTag(ProfileTag tag) {
        ProfileTagTable tagTable = reader.getTagTable();
        TagEntry tag = tagTable.getTagEntry(tag);
        // Do your stuff with tag entry
    }

And here is the nested TagEntry class:

    public static class TagEntry implements Comparable<TagEntry> {
        private int profileTag;
        private int dataOffset;
        private int dataLength;
        private byte[] data;

        public TagEntry(int profileTag, int dataOffset, int dataLength, byte[] data) {
            this.profileTag  = profileTag;
            this.dataOffset = dataOffset;
            this.dataLength = dataLength;
            this.data = data;
        }

        @Override
        public int compareTo(TagEntry o) {
            return (int)((this.profileTag&0xffffffffL) - (o.profileTag&0x0ffffffffL));
        }

        public int getProfileTag() {
            return profileTag;
        }

        public int getDataOffset() {
            return dataOffset;
        }

        public int getDataLength() {
            return dataLength;
        }

        public byte[] getData() {
            return data;
        }       
    }

Upvotes: 2

Related Questions