View | Details | Raw Unified | Return to bug 53408
Collapse All | Expand All

(-)test/java/org/apache/xmlgraphics/java2d/color/profile/ColorProfileUtilTestCase.java (+44 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
/* $Id: ColorProfileUtil.java 1345683 2012-06-03 14:50:33Z gadams $ */
19
20
package org.apache.xmlgraphics.java2d.color.profile;
21
22
import java.awt.color.ColorSpace;
23
import java.awt.color.ICC_Profile;
24
import java.io.ByteArrayInputStream;
25
import java.io.ByteArrayOutputStream;
26
import java.io.IOException;
27
28
import org.junit.Test;
29
30
import static org.junit.Assert.assertTrue;
31
32
public class ColorProfileUtilTestCase {
33
34
    @Test
35
    public void testIsDefaultsRGB() throws IOException {
36
        ICC_Profile profile = ICC_Profile.getInstance(ColorSpace.CS_sRGB);
37
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
38
        profile.write(baos);
39
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
40
        ICC_Profile profileFromFile = ICC_Profile.getInstance(bais);
41
        assertTrue(ColorProfileUtil.isDefaultsRGB(profileFromFile));
42
    }
43
44
}
(-)src/java/org/apache/xmlgraphics/java2d/color/profile/ColorProfileUtil.java (-5 / +19 lines)
Lines 22-30 Link Here
22
import java.awt.color.ColorSpace;
22
import java.awt.color.ColorSpace;
23
import java.awt.color.ICC_ColorSpace;
23
import java.awt.color.ICC_ColorSpace;
24
import java.awt.color.ICC_Profile;
24
import java.awt.color.ICC_Profile;
25
import java.awt.color.ICC_ProfileRGB;
25
import java.io.IOException;
26
import java.io.IOException;
26
import java.io.InputStream;
27
import java.io.InputStream;
27
import java.io.UnsupportedEncodingException;
28
import java.io.UnsupportedEncodingException;
29
import java.util.Arrays;
28
30
29
// CSOFF: MethodName
31
// CSOFF: MethodName
30
32
Lines 64-75 Link Here
64
     * @return true if it is the default sRGB profile
66
     * @return true if it is the default sRGB profile
65
     */
67
     */
66
    public static boolean isDefaultsRGB(ICC_Profile profile) {
68
    public static boolean isDefaultsRGB(ICC_Profile profile) {
67
        ColorSpace sRGB = ColorSpace.getInstance(ColorSpace.CS_sRGB);
69
        if (!(profile instanceof ICC_ProfileRGB)) {
68
        ICC_Profile sRGBProfile = null;
70
          return false;
69
        if (sRGB instanceof ICC_ColorSpace) {
71
        }
70
            sRGBProfile = ((ICC_ColorSpace)sRGB).getProfile();
72
        // not sure what the best way to compare two profiles is, but comparing instances is not the right way
73
        ICC_Profile sRGBProfile = ICC_Profile.getInstance(ColorSpace.CS_sRGB);
74
        if (profile.getProfileClass() != sRGBProfile.getProfileClass()) {
75
          return false;
76
        }
77
        if (profile.getMajorVersion() != sRGBProfile.getMajorVersion()) {
78
          return false;
79
        }
80
        if (profile.getMinorVersion() != sRGBProfile.getMinorVersion()) {
81
          return false;
71
        }
82
        }
72
        return profile == sRGBProfile;
83
        if (!Arrays.equals(profile.getData(), sRGBProfile.getData())) {
84
          return false;
85
        }
86
        return true;
73
    }
87
    }
74
88
75
    /**
89
    /**
(-)src/java/org/apache/xmlgraphics/image/loader/impl/ImageRawPNG.java (+17 lines)
Lines 40-45 Link Here
40
    private int redTransparentAlpha;
40
    private int redTransparentAlpha;
41
    private int greenTransparentAlpha;
41
    private int greenTransparentAlpha;
42
    private int blueTransparentAlpha;
42
    private int blueTransparentAlpha;
43
    private int renderingIntent = -1;
43
44
44
    /**
45
    /**
45
     * Main constructor.
46
     * Main constructor.
Lines 141-144 Link Here
141
        return color;
142
        return color;
142
    }
143
    }
143
144
145
    /**
146
     * Used to set the rendering intent when the color space is sRGB.
147
     * @param intent the rendering intent of the sRGB color space
148
     */
149
    public void setRenderingIntent(int intent) {
150
        renderingIntent = intent;
151
    }
152
153
    /**
154
     * Returns the rendering intent of the sRGB color space.
155
     * @return the rendering intent
156
     */
157
    public int getRenderingIntent() {
158
      return this.renderingIntent;
159
    }
160
144
}
161
}
(-)src/java/org/apache/xmlgraphics/image/loader/impl/PNGFile.java (-6 / +55 lines)
Lines 20-25 Link Here
20
package org.apache.xmlgraphics.image.loader.impl;
20
package org.apache.xmlgraphics.image.loader.impl;
21
21
22
import java.awt.color.ColorSpace;
22
import java.awt.color.ColorSpace;
23
import java.awt.color.ICC_ColorSpace;
23
import java.awt.color.ICC_Profile;
24
import java.awt.color.ICC_Profile;
24
import java.awt.image.ColorModel;
25
import java.awt.image.ColorModel;
25
import java.awt.image.ComponentColorModel;
26
import java.awt.image.ComponentColorModel;
Lines 34-39 Link Here
34
import java.util.ArrayList;
35
import java.util.ArrayList;
35
import java.util.Collections;
36
import java.util.Collections;
36
import java.util.List;
37
import java.util.List;
38
import java.util.zip.Inflater;
39
import java.util.zip.InflaterInputStream;
37
40
38
import org.apache.xmlgraphics.image.codec.png.PNGChunk;
41
import org.apache.xmlgraphics.image.codec.png.PNGChunk;
39
import org.apache.xmlgraphics.image.codec.util.PropertyUtil;
42
import org.apache.xmlgraphics.image.codec.util.PropertyUtil;
Lines 49-54 Link Here
49
52
50
    private ColorModel colorModel;
53
    private ColorModel colorModel;
51
    private ICC_Profile iccProfile;
54
    private ICC_Profile iccProfile;
55
    private int sRGBRenderingIntent = -1;
52
    private int bitDepth;
56
    private int bitDepth;
53
    private int colorType;
57
    private int colorType;
54
    private boolean isTransparent;
58
    private boolean isTransparent;
Lines 96-101 Link Here
96
                } else if (chunkType.equals(PNGChunk.ChunkType.tRNS.name())) {
100
                } else if (chunkType.equals(PNGChunk.ChunkType.tRNS.name())) {
97
                    chunk = PNGChunk.readChunk(distream);
101
                    chunk = PNGChunk.readChunk(distream);
98
                    parse_tRNS_chunk(chunk);
102
                    parse_tRNS_chunk(chunk);
103
                } else if (chunkType.equals(PNGChunk.ChunkType.iCCP.name())) {
104
                    chunk = PNGChunk.readChunk(distream);
105
                    parse_iCCP_chunk(chunk);
106
                } else if (chunkType.equals(PNGChunk.ChunkType.sRGB.name())) {
107
                  chunk = PNGChunk.readChunk(distream);
108
                  parse_sRGB_chunk(chunk);
99
                } else {
109
                } else {
100
                    // chunk = PNGChunk.readChunk(distream);
110
                    // chunk = PNGChunk.readChunk(distream);
101
                    PNGChunk.skipChunk(distream);
111
                    PNGChunk.skipChunk(distream);
Lines 110-115 Link Here
110
120
111
    public ImageRawPNG getImageRawPNG(ImageInfo info) throws ImageException {
121
    public ImageRawPNG getImageRawPNG(ImageInfo info) throws ImageException {
112
        InputStream seqStream = new SequenceInputStream(Collections.enumeration(streamVec));
122
        InputStream seqStream = new SequenceInputStream(Collections.enumeration(streamVec));
123
        ColorSpace rgbCS = null;
113
        switch (colorType) {
124
        switch (colorType) {
114
        case PNG_COLOR_GRAY:
125
        case PNG_COLOR_GRAY:
115
            if (hasPalette) {
126
            if (hasPalette) {
Lines 119-127 Link Here
119
                    ColorModel.OPAQUE, DataBuffer.TYPE_BYTE);
130
                    ColorModel.OPAQUE, DataBuffer.TYPE_BYTE);
120
            break;
131
            break;
121
        case PNG_COLOR_RGB:
132
        case PNG_COLOR_RGB:
122
            // actually a check of the sRGB chunk would be necessary to confirm if it's really sRGB
133
            if (iccProfile != null) {
123
            colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false,
134
                rgbCS = new ICC_ColorSpace(iccProfile);
124
                    ColorModel.OPAQUE, DataBuffer.TYPE_BYTE);
135
            } else if (sRGBRenderingIntent != -1) {
136
                rgbCS = ColorSpace.getInstance(ColorSpace.CS_sRGB);
137
            } else {
138
                rgbCS = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB);
139
            }
140
            colorModel = new ComponentColorModel(rgbCS, false, false, ColorModel.OPAQUE, DataBuffer.TYPE_BYTE);
125
            break;
141
            break;
126
        case PNG_COLOR_PALETTE:
142
        case PNG_COLOR_PALETTE:
127
            if (hasAlphaPalette) {
143
            if (hasAlphaPalette) {
Lines 140-148 Link Here
140
                    ColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE);
156
                    ColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE);
141
            break;
157
            break;
142
        case PNG_COLOR_RGB_ALPHA:
158
        case PNG_COLOR_RGB_ALPHA:
143
            // actually a check of the sRGB chunk would be necessary to confirm if it's really sRGB
159
            if (iccProfile != null) {
144
            colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), true, false,
160
                rgbCS = new ICC_ColorSpace(iccProfile);
145
                    ColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE);
161
            } else if (sRGBRenderingIntent != -1) {
162
                rgbCS = ColorSpace.getInstance(ColorSpace.CS_sRGB);
163
            } else {
164
                rgbCS = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB);
165
            }
166
            colorModel = new ComponentColorModel(rgbCS, true, false, ColorModel.TRANSLUCENT,
167
                    DataBuffer.TYPE_BYTE);
146
            break;
168
            break;
147
        default:
169
        default:
148
            throw new ImageException("Unsupported color type: " + colorType);
170
            throw new ImageException("Unsupported color type: " + colorType);
Lines 161-166 Link Here
161
                //
183
                //
162
            }
184
            }
163
        }
185
        }
186
        if (sRGBRenderingIntent != -1) {
187
          rawImage.setRenderingIntent(sRGBRenderingIntent);
188
        }
164
        return rawImage;
189
        return rawImage;
165
    }
190
    }
166
191
Lines 235-238 Link Here
235
        isTransparent = true;
260
        isTransparent = true;
236
    }
261
    }
237
262
263
    private void parse_iCCP_chunk(PNGChunk chunk) {
264
        int length = chunk.getLength();
265
        String name = ""; // todo simplify this
266
        byte b;
267
        int textIndex = 0;
268
        while ((b = chunk.getByte(textIndex++)) != 0) {
269
            name += (char) b;
270
        }
271
        byte compression = chunk.getByte(textIndex++);
272
        byte[] profile = new byte[length - textIndex];
273
        System.arraycopy(chunk.getData(), textIndex, profile, 0, length - textIndex);
274
        ByteArrayInputStream bais = new ByteArrayInputStream(profile);
275
        InflaterInputStream iis = new InflaterInputStream(bais, new Inflater());
276
        try {
277
            iccProfile = ICC_Profile.getInstance(iis);
278
        } catch (IOException ioe) {
279
            // this is OK; the profile will be null
280
        }
281
    }
282
283
    private void parse_sRGB_chunk(PNGChunk chunk) {
284
      sRGBRenderingIntent = chunk.getByte(0);
285
    }
286
238
}
287
}

Return to bug 53408