jdk/src/java.desktop/share/classes/sun/java2d/cmm/lcms/LCMSTransform.java
author pkbalakr
Thu, 19 May 2016 16:34:36 +0530
changeset 38984 bbded3eab454
parent 25859 3317bb8137f4
permissions -rw-r--r--
6587251: Import declaration not used in sun.java2d.* Reviewed-by: prr, psadhukhan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
38984
bbded3eab454 6587251: Import declaration not used in sun.java2d.*
pkbalakr
parents: 25859
diff changeset
     2
 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/**********************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 **********************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 **********************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *** COPYRIGHT (c) Eastman Kodak Company, 1997                      ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *** As  an unpublished  work pursuant to Title 17 of the United    ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *** States Code.  All rights reserved.                             ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 **********************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 **********************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 **********************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package sun.java2d.cmm.lcms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.color.ICC_Profile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.color.CMMException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.color.ColorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.image.Raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.image.WritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.awt.image.SampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.awt.image.DataBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.java2d.cmm.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.java2d.cmm.lcms.*;
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
    49
import static sun.java2d.cmm.lcms.LCMSImageLayout.ImageLayoutException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class LCMSTransform implements ColorTransform {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    long ID;
14884
74d1acdb7ee4 7124245: [lcms] ColorConvertOp to color space CS_GRAY apparently converts orange to 244,244,0
bae
parents: 7668
diff changeset
    54
    private int inFormatter = 0;
74d1acdb7ee4 7124245: [lcms] ColorConvertOp to color space CS_GRAY apparently converts orange to 244,244,0
bae
parents: 7668
diff changeset
    55
    private boolean isInIntPacked = false;
74d1acdb7ee4 7124245: [lcms] ColorConvertOp to color space CS_GRAY apparently converts orange to 244,244,0
bae
parents: 7668
diff changeset
    56
    private int outFormatter = 0;
74d1acdb7ee4 7124245: [lcms] ColorConvertOp to color space CS_GRAY apparently converts orange to 244,244,0
bae
parents: 7668
diff changeset
    57
    private boolean isOutIntPacked = false;
6482
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
    58
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    ICC_Profile[] profiles;
19783
88ce9a76c992 7043064: sun/java2d/cmm/ tests failed against RI b141 & b138-nightly
bae
parents: 16930
diff changeset
    60
    LCMSProfile[] lcmsProfiles;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    int renderType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    int transformType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
6482
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
    64
    private int numInComponents = -1;
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
    65
    private int numOutComponents = -1;
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
    66
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private Object disposerReferent = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /* the class initializer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (ProfileDeferralMgr.deferring) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            ProfileDeferralMgr.activateProfiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public LCMSTransform(ICC_Profile profile, int renderType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                         int transformType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        /* Actually, it is not a complete transform but just part of it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        profiles = new ICC_Profile[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        profiles[0] = profile;
19783
88ce9a76c992 7043064: sun/java2d/cmm/ tests failed against RI b141 & b138-nightly
bae
parents: 16930
diff changeset
    82
        lcmsProfiles = new LCMSProfile[1];
88ce9a76c992 7043064: sun/java2d/cmm/ tests failed against RI b141 & b138-nightly
bae
parents: 16930
diff changeset
    83
        lcmsProfiles[0] = LCMS.getProfileID(profile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        this.renderType = (renderType == ColorTransform.Any)?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                              ICC_Profile.icPerceptual : renderType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        this.transformType = transformType;
6482
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
    87
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
    88
        /* Note that ICC_Profile.getNumComponents() is quite expensive
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
    89
         * (it may results in a reading of the profile header).
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
    90
         * So, here we cache the number of components of input and
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
    91
         * output profiles for further usage.
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
    92
         */
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
    93
        numInComponents = profiles[0].getNumComponents();
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
    94
        numOutComponents = profiles[profiles.length - 1].getNumComponents();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public LCMSTransform (ColorTransform[] transforms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        int size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        for (int i=0; i < transforms.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            size+=((LCMSTransform)transforms[i]).profiles.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        profiles = new ICC_Profile[size];
19783
88ce9a76c992 7043064: sun/java2d/cmm/ tests failed against RI b141 & b138-nightly
bae
parents: 16930
diff changeset
   103
        lcmsProfiles = new LCMSProfile[size];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        for (int i=0; i < transforms.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            LCMSTransform curTrans = (LCMSTransform)transforms[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            System.arraycopy(curTrans.profiles, 0, profiles, j,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                             curTrans.profiles.length);
19783
88ce9a76c992 7043064: sun/java2d/cmm/ tests failed against RI b141 & b138-nightly
bae
parents: 16930
diff changeset
   109
            System.arraycopy(curTrans.lcmsProfiles, 0, lcmsProfiles, j,
88ce9a76c992 7043064: sun/java2d/cmm/ tests failed against RI b141 & b138-nightly
bae
parents: 16930
diff changeset
   110
                             curTrans.lcmsProfiles.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            j += curTrans.profiles.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        renderType = ((LCMSTransform)transforms[0]).renderType;
6482
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   114
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   115
        /* Note that ICC_Profile.getNumComponents() is quite expensive
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   116
         * (it may results in a reading of the profile header).
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   117
         * So, here we cache the number of components of input and
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   118
         * output profiles for further usage.
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   119
         */
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   120
        numInComponents = profiles[0].getNumComponents();
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   121
        numOutComponents = profiles[profiles.length - 1].getNumComponents();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public int getNumInComponents() {
6482
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   125
        return numInComponents;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public int getNumOutComponents() {
6482
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   129
        return numOutComponents;
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   130
    }
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   131
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   132
    private synchronized void doTransform(LCMSImageLayout in,
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   133
                                          LCMSImageLayout out) {
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   134
        // update native transfrom if needed
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   135
        if (ID == 0L ||
14884
74d1acdb7ee4 7124245: [lcms] ColorConvertOp to color space CS_GRAY apparently converts orange to 244,244,0
bae
parents: 7668
diff changeset
   136
            inFormatter != in.pixelType || isInIntPacked != in.isIntPacked ||
74d1acdb7ee4 7124245: [lcms] ColorConvertOp to color space CS_GRAY apparently converts orange to 244,244,0
bae
parents: 7668
diff changeset
   137
            outFormatter != out.pixelType || isOutIntPacked != out.isIntPacked)
74d1acdb7ee4 7124245: [lcms] ColorConvertOp to color space CS_GRAY apparently converts orange to 244,244,0
bae
parents: 7668
diff changeset
   138
        {
6482
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   139
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   140
            if (ID != 0L) {
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   141
                // Disposer will destroy forgotten transform
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   142
                disposerReferent = new Object();
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   143
            }
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   144
            inFormatter = in.pixelType;
14884
74d1acdb7ee4 7124245: [lcms] ColorConvertOp to color space CS_GRAY apparently converts orange to 244,244,0
bae
parents: 7668
diff changeset
   145
            isInIntPacked = in.isIntPacked;
74d1acdb7ee4 7124245: [lcms] ColorConvertOp to color space CS_GRAY apparently converts orange to 244,244,0
bae
parents: 7668
diff changeset
   146
6482
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   147
            outFormatter = out.pixelType;
14884
74d1acdb7ee4 7124245: [lcms] ColorConvertOp to color space CS_GRAY apparently converts orange to 244,244,0
bae
parents: 7668
diff changeset
   148
            isOutIntPacked = out.isIntPacked;
6482
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   149
19783
88ce9a76c992 7043064: sun/java2d/cmm/ tests failed against RI b141 & b138-nightly
bae
parents: 16930
diff changeset
   150
            ID = LCMS.createTransform(lcmsProfiles, renderType,
14884
74d1acdb7ee4 7124245: [lcms] ColorConvertOp to color space CS_GRAY apparently converts orange to 244,244,0
bae
parents: 7668
diff changeset
   151
                                            inFormatter, isInIntPacked,
74d1acdb7ee4 7124245: [lcms] ColorConvertOp to color space CS_GRAY apparently converts orange to 244,244,0
bae
parents: 7668
diff changeset
   152
                                            outFormatter, isOutIntPacked,
6482
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   153
                                            disposerReferent);
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   154
        }
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   155
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   156
        LCMS.colorConvert(this, in, out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public void colorConvert(BufferedImage src, BufferedImage dst) {
15974
91b0e63e6e83 8005530: [lcms] Improve performance of ColorConverOp for default destinations
bae
parents: 14884
diff changeset
   160
        LCMSImageLayout srcIL, dstIL;
16900
chegar
parents: 15974 16899
diff changeset
   161
        try {
16930
lana
parents: 16833 16900
diff changeset
   162
            if (!dst.getColorModel().hasAlpha()) {
lana
parents: 16833 16900
diff changeset
   163
                dstIL = LCMSImageLayout.createImageLayout(dst);
15974
91b0e63e6e83 8005530: [lcms] Improve performance of ColorConverOp for default destinations
bae
parents: 14884
diff changeset
   164
16930
lana
parents: 16833 16900
diff changeset
   165
                if (dstIL != null) {
lana
parents: 16833 16900
diff changeset
   166
                    srcIL = LCMSImageLayout.createImageLayout(src);
lana
parents: 16833 16900
diff changeset
   167
                    if (srcIL != null) {
lana
parents: 16833 16900
diff changeset
   168
                        doTransform(srcIL, dstIL);
lana
parents: 16833 16900
diff changeset
   169
                        return;
lana
parents: 16833 16900
diff changeset
   170
                    }
16900
chegar
parents: 15974 16899
diff changeset
   171
                }
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   172
            }
16900
chegar
parents: 15974 16899
diff changeset
   173
        }  catch (ImageLayoutException e) {
chegar
parents: 15974 16899
diff changeset
   174
            throw new CMMException("Unable to convert images");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
15974
91b0e63e6e83 8005530: [lcms] Improve performance of ColorConverOp for default destinations
bae
parents: 14884
diff changeset
   176
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        Raster srcRas = src.getRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        WritableRaster dstRas = dst.getRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        ColorModel srcCM = src.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        ColorModel dstCM = dst.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        int w = src.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        int h = src.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        int srcNumComp = srcCM.getNumColorComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        int dstNumComp = dstCM.getNumColorComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        int precision = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        float maxNum = 255.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        for (int i = 0; i < srcNumComp; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (srcCM.getComponentSize(i) > 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                 precision = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                 maxNum = 65535.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        for (int i = 0; i < dstNumComp; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (dstCM.getComponentSize(i) > 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                 precision = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                 maxNum = 65535.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        float[] srcMinVal = new float[srcNumComp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        float[] srcInvDiffMinMax = new float[srcNumComp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        ColorSpace cs = srcCM.getColorSpace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        for (int i = 0; i < srcNumComp; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            srcMinVal[i] = cs.getMinValue(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            srcInvDiffMinMax[i] = maxNum / (cs.getMaxValue(i) - srcMinVal[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        cs = dstCM.getColorSpace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        float[] dstMinVal = new float[dstNumComp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        float[] dstDiffMinMax = new float[dstNumComp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        for (int i = 0; i < dstNumComp; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            dstMinVal[i] = cs.getMinValue(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            dstDiffMinMax[i] = (cs.getMaxValue(i) - dstMinVal[i]) / maxNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        boolean dstHasAlpha = dstCM.hasAlpha();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        boolean needSrcAlpha = srcCM.hasAlpha() && dstHasAlpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        float[] dstColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (dstHasAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            dstColor = new float[dstNumComp + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            dstColor = new float[dstNumComp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (precision == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            byte[] srcLine = new byte[w * srcNumComp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            byte[] dstLine = new byte[w * dstNumComp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            Object pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            float[] color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            float[] alpha = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (needSrcAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                alpha = new float[w];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            int idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            // TODO check for src npixels = dst npixels
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   232
            try {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   233
                srcIL = new LCMSImageLayout(
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   234
                        srcLine, srcLine.length/getNumInComponents(),
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   235
                        LCMSImageLayout.CHANNELS_SH(getNumInComponents()) |
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   236
                        LCMSImageLayout.BYTES_SH(1), getNumInComponents());
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   237
                dstIL = new LCMSImageLayout(
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   238
                        dstLine, dstLine.length/getNumOutComponents(),
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   239
                        LCMSImageLayout.CHANNELS_SH(getNumOutComponents()) |
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   240
                        LCMSImageLayout.BYTES_SH(1), getNumOutComponents());
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   241
            } catch (ImageLayoutException e) {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   242
                throw new CMMException("Unable to convert images");
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   243
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            // process each scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            for (int y = 0; y < h; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                // convert src scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                pixel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                color = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                idx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                for (int x = 0; x < w; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    pixel = srcRas.getDataElements(x, y, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    color = srcCM.getNormalizedComponents(pixel, color, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    for (int i = 0; i < srcNumComp; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                        srcLine[idx++] = (byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                            ((color[i] - srcMinVal[i]) * srcInvDiffMinMax[i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                             0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    if (needSrcAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        alpha[x] = color[srcNumComp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                // color convert srcLine to dstLine
6482
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   263
                doTransform(srcIL, dstIL);
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   264
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                // convert dst scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                pixel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                idx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                for (int x = 0; x < w; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    for (int i = 0; i < dstNumComp; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                        dstColor[i] = ((float) (dstLine[idx++] & 0xff)) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                                      dstDiffMinMax[i] + dstMinVal[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    if (needSrcAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                        dstColor[dstNumComp] = alpha[x];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    } else if (dstHasAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                        dstColor[dstNumComp] = 1.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    pixel = dstCM.getDataElements(dstColor, 0, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    dstRas.setDataElements(x, y, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            short[] srcLine = new short[w * srcNumComp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            short[] dstLine = new short[w * dstNumComp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            Object pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            float[] color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            float[] alpha = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if (needSrcAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                alpha = new float[w];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            int idx;
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   292
            try {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   293
                srcIL = new LCMSImageLayout(
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   294
                    srcLine, srcLine.length/getNumInComponents(),
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   295
                    LCMSImageLayout.CHANNELS_SH(getNumInComponents()) |
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   296
                    LCMSImageLayout.BYTES_SH(2), getNumInComponents()*2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   298
                dstIL = new LCMSImageLayout(
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   299
                    dstLine, dstLine.length/getNumOutComponents(),
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   300
                    LCMSImageLayout.CHANNELS_SH(getNumOutComponents()) |
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   301
                    LCMSImageLayout.BYTES_SH(2), getNumOutComponents()*2);
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   302
            } catch (ImageLayoutException e) {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   303
                throw new CMMException("Unable to convert images");
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   304
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            // process each scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            for (int y = 0; y < h; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                // convert src scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                pixel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                color = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                idx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                for (int x = 0; x < w; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    pixel = srcRas.getDataElements(x, y, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    color = srcCM.getNormalizedComponents(pixel, color, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    for (int i = 0; i < srcNumComp; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                        srcLine[idx++] = (short)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                            ((color[i] - srcMinVal[i]) * srcInvDiffMinMax[i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                             0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    if (needSrcAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                        alpha[x] = color[srcNumComp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                // color convert srcLine to dstLine
6482
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   324
                doTransform(srcIL, dstIL);
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   325
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                // convert dst scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                pixel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                idx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                for (int x = 0; x < w; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    for (int i = 0; i < dstNumComp; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        dstColor[i] = ((float) (dstLine[idx++] & 0xffff)) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                      dstDiffMinMax[i] + dstMinVal[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    if (needSrcAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        dstColor[dstNumComp] = alpha[x];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    } else if (dstHasAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                        dstColor[dstNumComp] = 1.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    pixel = dstCM.getDataElements(dstColor, 0, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    dstRas.setDataElements(x, y, pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    public void colorConvert(Raster src, WritableRaster dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                             float[] srcMinVal, float[]srcMaxVal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                             float[] dstMinVal, float[]dstMaxVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        LCMSImageLayout srcIL, dstIL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        // Can't pass src and dst directly to CMM, so process per scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        SampleModel srcSM = src.getSampleModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        SampleModel dstSM = dst.getSampleModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        int srcTransferType = src.getTransferType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        int dstTransferType = dst.getTransferType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        boolean srcIsFloat, dstIsFloat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if ((srcTransferType == DataBuffer.TYPE_FLOAT) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            (srcTransferType == DataBuffer.TYPE_DOUBLE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            srcIsFloat = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            srcIsFloat = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if ((dstTransferType == DataBuffer.TYPE_FLOAT) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            (dstTransferType == DataBuffer.TYPE_DOUBLE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            dstIsFloat = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            dstIsFloat = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        int w = src.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        int h = src.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        int srcNumBands = src.getNumBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        int dstNumBands = dst.getNumBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        float[] srcScaleFactor = new float[srcNumBands];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        float[] dstScaleFactor = new float[dstNumBands];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        float[] srcUseMinVal = new float[srcNumBands];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        float[] dstUseMinVal = new float[dstNumBands];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        for (int i = 0; i < srcNumBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            if (srcIsFloat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                srcScaleFactor[i] =  65535.0f / (srcMaxVal[i] - srcMinVal[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                srcUseMinVal[i] = srcMinVal[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                if (srcTransferType == DataBuffer.TYPE_SHORT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    srcScaleFactor[i] = 65535.0f / 32767.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    srcScaleFactor[i] = 65535.0f /
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                        ((float) ((1 << srcSM.getSampleSize(i)) - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                srcUseMinVal[i] = 0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        for (int i = 0; i < dstNumBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (dstIsFloat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                dstScaleFactor[i] = (dstMaxVal[i] - dstMinVal[i]) / 65535.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                dstUseMinVal[i] = dstMinVal[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                if (dstTransferType == DataBuffer.TYPE_SHORT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    dstScaleFactor[i] = 32767.0f / 65535.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    dstScaleFactor[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                        ((float) ((1 << dstSM.getSampleSize(i)) - 1)) /
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                        65535.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                dstUseMinVal[i] = 0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        int ys = src.getMinY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        int yd = dst.getMinY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        int xs, xd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        float sample;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        short[] srcLine = new short[w * srcNumBands];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        short[] dstLine = new short[w * dstNumBands];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        int idx;
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   413
        try {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   414
            srcIL = new LCMSImageLayout(
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   415
                    srcLine, srcLine.length/getNumInComponents(),
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   416
                    LCMSImageLayout.CHANNELS_SH(getNumInComponents()) |
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   417
                    LCMSImageLayout.BYTES_SH(2), getNumInComponents()*2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   419
            dstIL = new LCMSImageLayout(
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   420
                    dstLine, dstLine.length/getNumOutComponents(),
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   421
                    LCMSImageLayout.CHANNELS_SH(getNumOutComponents()) |
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   422
                    LCMSImageLayout.BYTES_SH(2), getNumOutComponents()*2);
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   423
        } catch (ImageLayoutException e) {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   424
            throw new CMMException("Unable to convert rasters");
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   425
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        // process each scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        for (int y = 0; y < h; y++, ys++, yd++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            // get src scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            xs = src.getMinX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            idx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            for (int x = 0; x < w; x++, xs++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                for (int i = 0; i < srcNumBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    sample = src.getSampleFloat(xs, ys, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    srcLine[idx++] = (short)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                        ((sample - srcUseMinVal[i]) * srcScaleFactor[i] + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            // color convert srcLine to dstLine
6482
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   440
            doTransform(srcIL, dstIL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            // store dst scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            xd = dst.getMinX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            idx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            for (int x = 0; x < w; x++, xd++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                for (int i = 0; i < dstNumBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    sample = ((dstLine[idx++] & 0xffff) * dstScaleFactor[i]) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                             dstUseMinVal[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    dst.setSample(xd, yd, i, sample);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    public void colorConvert(Raster src, WritableRaster dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        LCMSImageLayout srcIL, dstIL;
15974
91b0e63e6e83 8005530: [lcms] Improve performance of ColorConverOp for default destinations
bae
parents: 14884
diff changeset
   458
        dstIL = LCMSImageLayout.createImageLayout(dst);
91b0e63e6e83 8005530: [lcms] Improve performance of ColorConverOp for default destinations
bae
parents: 14884
diff changeset
   459
        if (dstIL != null) {
91b0e63e6e83 8005530: [lcms] Improve performance of ColorConverOp for default destinations
bae
parents: 14884
diff changeset
   460
            srcIL = LCMSImageLayout.createImageLayout(src);
91b0e63e6e83 8005530: [lcms] Improve performance of ColorConverOp for default destinations
bae
parents: 14884
diff changeset
   461
            if (srcIL != null) {
91b0e63e6e83 8005530: [lcms] Improve performance of ColorConverOp for default destinations
bae
parents: 14884
diff changeset
   462
                doTransform(srcIL, dstIL);
91b0e63e6e83 8005530: [lcms] Improve performance of ColorConverOp for default destinations
bae
parents: 14884
diff changeset
   463
                return;
91b0e63e6e83 8005530: [lcms] Improve performance of ColorConverOp for default destinations
bae
parents: 14884
diff changeset
   464
            }
91b0e63e6e83 8005530: [lcms] Improve performance of ColorConverOp for default destinations
bae
parents: 14884
diff changeset
   465
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        // Can't pass src and dst directly to CMM, so process per scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        SampleModel srcSM = src.getSampleModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        SampleModel dstSM = dst.getSampleModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        int srcTransferType = src.getTransferType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        int dstTransferType = dst.getTransferType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        int w = src.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        int h = src.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        int srcNumBands = src.getNumBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        int dstNumBands = dst.getNumBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        int precision = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        float maxNum = 255.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        for (int i = 0; i < srcNumBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            if (srcSM.getSampleSize(i) > 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                 precision = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                 maxNum = 65535.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        for (int i = 0; i < dstNumBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            if (dstSM.getSampleSize(i) > 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                 precision = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                 maxNum = 65535.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        float[] srcScaleFactor = new float[srcNumBands];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        float[] dstScaleFactor = new float[dstNumBands];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        for (int i = 0; i < srcNumBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            if (srcTransferType == DataBuffer.TYPE_SHORT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                srcScaleFactor[i] = maxNum / 32767.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                srcScaleFactor[i] = maxNum /
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    ((float) ((1 << srcSM.getSampleSize(i)) - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        for (int i = 0; i < dstNumBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            if (dstTransferType == DataBuffer.TYPE_SHORT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                dstScaleFactor[i] = 32767.0f / maxNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                dstScaleFactor[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    ((float) ((1 << dstSM.getSampleSize(i)) - 1)) / maxNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        int ys = src.getMinY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        int yd = dst.getMinY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        int xs, xd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        int sample;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        if (precision == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            byte[] srcLine = new byte[w * srcNumBands];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            byte[] dstLine = new byte[w * dstNumBands];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            int idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            // TODO check for src npixels = dst npixels
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   516
            try {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   517
                srcIL = new LCMSImageLayout(
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   518
                        srcLine, srcLine.length/getNumInComponents(),
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   519
                        LCMSImageLayout.CHANNELS_SH(getNumInComponents()) |
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   520
                        LCMSImageLayout.BYTES_SH(1), getNumInComponents());
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   521
                dstIL = new LCMSImageLayout(
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   522
                        dstLine, dstLine.length/getNumOutComponents(),
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   523
                        LCMSImageLayout.CHANNELS_SH(getNumOutComponents()) |
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   524
                        LCMSImageLayout.BYTES_SH(1), getNumOutComponents());
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   525
            } catch (ImageLayoutException e) {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   526
                throw new CMMException("Unable to convert rasters");
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   527
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            // process each scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            for (int y = 0; y < h; y++, ys++, yd++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                // get src scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                xs = src.getMinX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                idx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                for (int x = 0; x < w; x++, xs++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    for (int i = 0; i < srcNumBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                        sample = src.getSample(xs, ys, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                        srcLine[idx++] = (byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                            ((sample * srcScaleFactor[i]) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                // color convert srcLine to dstLine
6482
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   542
                doTransform(srcIL, dstIL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                // store dst scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                xd = dst.getMinX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                idx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                for (int x = 0; x < w; x++, xd++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    for (int i = 0; i < dstNumBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                        sample = (int) (((dstLine[idx++] & 0xff) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                                         dstScaleFactor[i]) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                        dst.setSample(xd, yd, i, sample);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            short[] srcLine = new short[w * srcNumBands];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            short[] dstLine = new short[w * dstNumBands];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            int idx;
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   559
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   560
            try {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   561
                srcIL = new LCMSImageLayout(
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   562
                        srcLine, srcLine.length/getNumInComponents(),
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   563
                        LCMSImageLayout.CHANNELS_SH(getNumInComponents()) |
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   564
                        LCMSImageLayout.BYTES_SH(2), getNumInComponents()*2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   566
                dstIL = new LCMSImageLayout(
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   567
                        dstLine, dstLine.length/getNumOutComponents(),
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   568
                        LCMSImageLayout.CHANNELS_SH(getNumOutComponents()) |
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   569
                        LCMSImageLayout.BYTES_SH(2), getNumOutComponents()*2);
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   570
            } catch (ImageLayoutException e) {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   571
                throw new CMMException("Unable to convert rasters");
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   572
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            // process each scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            for (int y = 0; y < h; y++, ys++, yd++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                // get src scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                xs = src.getMinX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                idx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                for (int x = 0; x < w; x++, xs++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    for (int i = 0; i < srcNumBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                        sample = src.getSample(xs, ys, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                        srcLine[idx++] = (short)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                            ((sample * srcScaleFactor[i]) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                // color convert srcLine to dstLine
6482
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   587
                doTransform(srcIL, dstIL);
0f6a4442b29e 6523398: OSS CMM: Need to implement writing ICC profile tags in new lcms library
bae
parents: 5506
diff changeset
   588
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                // store dst scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                xd = dst.getMinX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                idx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                for (int x = 0; x < w; x++, xd++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                    for (int i = 0; i < dstNumBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                        sample = (int) (((dstLine[idx++] & 0xffff) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                                         dstScaleFactor[i]) + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                        dst.setSample(xd, yd, i, sample);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    /* convert an array of colors in short format */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    /* each color is a contiguous set of array elements */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    /* the number of colors is (size of the array) / (number of input/output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
       components */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    public short[] colorConvert(short[] src, short[] dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        if (dst == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            dst = new short [(src.length/getNumInComponents())*getNumOutComponents()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   613
        try {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   614
            LCMSImageLayout srcIL = new LCMSImageLayout(
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   615
                    src, src.length/getNumInComponents(),
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   616
                    LCMSImageLayout.CHANNELS_SH(getNumInComponents()) |
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   617
                    LCMSImageLayout.BYTES_SH(2), getNumInComponents()*2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   619
            LCMSImageLayout dstIL = new LCMSImageLayout(
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   620
                    dst, dst.length/getNumOutComponents(),
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   621
                    LCMSImageLayout.CHANNELS_SH(getNumOutComponents()) |
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   622
                    LCMSImageLayout.BYTES_SH(2), getNumOutComponents()*2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   624
            doTransform(srcIL, dstIL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   626
            return dst;
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   627
        } catch (ImageLayoutException e) {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   628
            throw new CMMException("Unable to convert data");
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   629
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    public byte[] colorConvert(byte[] src, byte[] dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        if (dst == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            dst = new byte [(src.length/getNumInComponents())*getNumOutComponents()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   637
        try {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   638
            LCMSImageLayout srcIL = new LCMSImageLayout(
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   639
                    src, src.length/getNumInComponents(),
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   640
                    LCMSImageLayout.CHANNELS_SH(getNumInComponents()) |
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   641
                    LCMSImageLayout.BYTES_SH(1), getNumInComponents());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   643
            LCMSImageLayout dstIL = new LCMSImageLayout(
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   644
                    dst, dst.length/getNumOutComponents(),
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   645
                    LCMSImageLayout.CHANNELS_SH(getNumOutComponents()) |
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   646
                    LCMSImageLayout.BYTES_SH(1), getNumOutComponents());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   648
            doTransform(srcIL, dstIL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   650
            return dst;
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   651
        } catch (ImageLayoutException e) {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   652
            throw new CMMException("Unable to convert data");
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   653
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
}