~hjd/ubuntu/wily/xmlgraphics-commons/debian-merged

« back to all changes in this revision

Viewing changes to src/java/org/apache/xmlgraphics/image/rendered/Any2sRGBRed.java

  • Committer: Hans Joachim Desserud
  • Date: 2015-11-11 18:22:53 UTC
  • mfrom: (9.1.5 sid)
  • Revision ID: hans_joachim_desserud-20151111182253-zwi0frfm97j0wddn
  * Merge from Debian unstable.  Remaining changes:
    - d/control: Drop dependencies required for unit testing as they
      include libmockito-java which would pull maven into main, disable unit
      test execution.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 * limitations under the License.
16
16
 */
17
17
 
18
 
/* $Id: Any2sRGBRed.java 1345683 2012-06-03 14:50:33Z gadams $ */
 
18
/* $Id: Any2sRGBRed.java 1681698 2015-05-26 07:49:35Z ssteiner $ */
19
19
 
20
20
package org.apache.xmlgraphics.image.rendered;
21
21
 
46
46
 * luminance image.  The alpha channel if any will be copied to the
47
47
 * new image.
48
48
 *
49
 
 * @version $Id: Any2sRGBRed.java 1345683 2012-06-03 14:50:33Z gadams $
 
49
 * @version $Id: Any2sRGBRed.java 1681698 2015-05-26 07:49:35Z ssteiner $
50
50
 *
51
51
 * Originally authored by Thomas DeWeese.
52
52
 */
53
53
public class Any2sRGBRed extends AbstractRed {
54
54
 
55
 
    boolean srcIsLsRGB = false;
 
55
    boolean srcIsLsRGB;
56
56
 
57
57
    /**
58
58
     * Construct a luminace image from src.
60
60
     * @param src The image to convert to a luminance image
61
61
     */
62
62
    public Any2sRGBRed(CachableRed src) {
63
 
        super(src,src.getBounds(),
 
63
        super(src, src.getBounds(),
64
64
              fixColorModel(src),
65
65
              fixSampleModel(src),
66
66
              src.getTileGridXOffset(),
68
68
              null);
69
69
 
70
70
        ColorModel srcCM = src.getColorModel();
71
 
        if (srcCM == null) return;
 
71
        if (srcCM == null) {
 
72
            return;
 
73
        }
72
74
        ColorSpace srcCS = srcCM.getColorSpace();
73
 
        if (srcCS == ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB))
 
75
        if (srcCS == ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB)) {
74
76
            srcIsLsRGB = true;
 
77
        }
75
78
    }
76
79
 
77
80
    public static boolean is_INT_PACK_COMP(SampleModel sm) {
78
 
        if(!(sm instanceof SinglePixelPackedSampleModel)) return false;
 
81
        if (!(sm instanceof SinglePixelPackedSampleModel)) {
 
82
            return false;
 
83
        }
79
84
 
80
85
        // Check transfer types
81
 
        if(sm.getDataType() != DataBuffer.TYPE_INT)       return false;
 
86
        if (sm.getDataType() != DataBuffer.TYPE_INT) {
 
87
            return false;
 
88
        }
82
89
 
83
90
        SinglePixelPackedSampleModel sppsm;
84
91
        sppsm = (SinglePixelPackedSampleModel)sm;
85
92
 
86
93
        int [] masks = sppsm.getBitMasks();
87
 
        if ((masks.length != 3) && (masks.length != 4)) return false;
88
 
        if(masks[0] != 0x00ff0000) return false;
89
 
        if(masks[1] != 0x0000ff00) return false;
90
 
        if(masks[2] != 0x000000ff) return false;
91
 
        if ((masks.length == 4) &&
92
 
            (masks[3] != 0xff000000)) return false;
 
94
        if ((masks.length != 3) && (masks.length != 4)) {
 
95
            return false;
 
96
        }
 
97
        if (masks[0] != 0x00ff0000) {
 
98
            return false;
 
99
        }
 
100
        if (masks[1] != 0x0000ff00) {
 
101
            return false;
 
102
        }
 
103
        if (masks[2] != 0x000000ff) {
 
104
            return false;
 
105
        }
 
106
        if ((masks.length == 4)
 
107
            && (masks[3] != 0xff000000)) {
 
108
            return false;
 
109
        }
93
110
 
94
111
        return true;
95
112
   }
107
124
     */
108
125
    private static final int[] linearToSRGBLut = new int[256];
109
126
    static {
110
 
        final double scale = 1.0/255;
111
 
        final double exp   = 1.0/GAMMA;
 
127
        final double scale = 1.0 / 255;
 
128
        final double exp   = 1.0 / GAMMA;
112
129
        // System.out.print("L2S: ");
113
 
        for(int i=0; i<256; i++){
114
 
            double value = i*scale;
115
 
            if(value <= 0.0031308)
 
130
        for (int i = 0; i < 256; i++) {
 
131
            double value = i * scale;
 
132
            if (value <= 0.0031308) {
116
133
                value *= 12.92;
117
 
            else
 
134
            } else {
118
135
                value = 1.055 * Math.pow(value, exp) - 0.055;
 
136
            }
119
137
 
120
 
            linearToSRGBLut[i] = (int)Math.round(value*255.);
 
138
            linearToSRGBLut[i] = (int)Math.round(value * 255.);
121
139
            // System.out.print(linearToSRGBLut[i] + ",");
122
140
        }
123
141
        // System.out.println("");
130
148
        DataBufferInt db = (DataBufferInt)wr.getDataBuffer();
131
149
 
132
150
        final int     srcBase
133
 
            = (db.getOffset() +
134
 
               sm.getOffset(wr.getMinX()-wr.getSampleModelTranslateX(),
135
 
                            wr.getMinY()-wr.getSampleModelTranslateY()));
 
151
            = (db.getOffset()
 
152
               + sm.getOffset(wr.getMinX() - wr.getSampleModelTranslateX(),
 
153
                            wr.getMinY() - wr.getSampleModelTranslateY()));
136
154
        // Access the pixel data array
137
155
        final int[] pixels   = db.getBankData()[0];
138
156
        final int width      = wr.getWidth();
139
157
        final int height     = wr.getHeight();
140
158
        final int scanStride = sm.getScanlineStride();
141
159
 
142
 
        int end, pix;
 
160
        int end;
 
161
        int pix;
143
162
 
144
163
        // For alpha premult we need to multiply all comps.
145
 
        for (int y=0; y<height; y++) {
146
 
            int sp  = srcBase + y*scanStride;
 
164
        for (int y = 0; y < height; y++) {
 
165
            int sp  = srcBase + y * scanStride;
147
166
            end = sp + width;
148
167
 
149
 
            while (sp<end) {
 
168
            while (sp < end) {
150
169
                pix = pixels[sp];
151
170
                pixels[sp] =
152
 
                    ((     pix      &0xFF000000)|
153
 
                     (lut[(pix>>>16)&0xFF]<<16) |
154
 
                     (lut[(pix>>> 8)&0xFF]<< 8) |
155
 
                     (lut[(pix     )&0xFF]    ));
 
171
                    ((pix & 0xFF000000)
 
172
                     | (lut[(pix >>> 16) & 0xFF] << 16)
 
173
                     | (lut[(pix >>> 8) & 0xFF] << 8)
 
174
                     | (lut[pix & 0xFF]));
156
175
                sp++;
157
176
            }
158
177
        }
169
188
 
170
189
 
171
190
        // Fast case, Linear SRGB source, INT Pack writable raster...
172
 
        if (srcIsLsRGB &&
173
 
            is_INT_PACK_COMP(wr.getSampleModel())) {
 
191
        if (srcIsLsRGB
 
192
            && is_INT_PACK_COMP(wr.getSampleModel())) {
174
193
            src.copyData(wr);
175
 
            if (srcCM.hasAlpha())
 
194
            if (srcCM.hasAlpha()) {
176
195
                GraphicsUtil.coerceData(wr, srcCM, false);
 
196
            }
177
197
            applyLut_INT(wr, linearToSRGBLut);
178
198
            return wr;
179
199
        }
217
237
            return wr;
218
238
        }
219
239
 
220
 
        if (srcCM.getColorSpace() ==
221
 
            ColorSpace.getInstance(ColorSpace.CS_GRAY)) {
 
240
        if (srcCM.getColorSpace()
 
241
            == ColorSpace.getInstance(ColorSpace.CS_GRAY)) {
222
242
 
223
243
            // This is a little bit of a hack.  There is only
224
244
            // a linear grayscale ICC profile in the JDK so
256
276
            // No transform needed, just reformat data...
257
277
            // System.out.println("Bypassing");
258
278
 
259
 
            if (is_INT_PACK_COMP(srcSM))
 
279
            if (is_INT_PACK_COMP(srcSM)) {
260
280
                src.copyData(wr);
261
 
            else
 
281
            } else {
262
282
                GraphicsUtil.copyData(src.getData(wr.getBounds()), wr);
 
283
            }
263
284
 
264
285
            return wr;
265
286
        }
266
287
 
267
288
        Raster srcRas = src.getData(wr.getBounds());
 
289
        assert srcRas instanceof WritableRaster;
268
290
        WritableRaster srcWr  = (WritableRaster)srcRas;
269
291
 
270
292
        // Divide out alpha if we have it.  We need to do this since
271
293
        // the color convert may not be a linear operation which may
272
294
        // lead to out of range values.
273
295
        ColorModel srcBICM = srcCM;
274
 
        if (srcCM.hasAlpha())
 
296
        if (srcCM.hasAlpha()) {
275
297
            srcBICM = GraphicsUtil.coerceData(srcWr, srcCM, false);
 
298
        }
276
299
 
277
 
        BufferedImage srcBI, dstBI;
 
300
        BufferedImage srcBI;
 
301
        BufferedImage dstBI;
278
302
        srcBI = new BufferedImage(srcBICM,
279
 
                                  srcWr.createWritableTranslatedChild(0,0),
 
303
                                  srcWr.createWritableTranslatedChild(0, 0),
280
304
                                  false,
281
305
                                  null);
282
306
 
289
313
 
290
314
        // System.out.println("After filter:");
291
315
 
292
 
        WritableRaster wr00 = wr.createWritableTranslatedChild(0,0);
293
 
        for (int i=0; i<dstCM.getColorSpace().getNumComponents(); i++)
 
316
        WritableRaster wr00 = wr.createWritableTranslatedChild(0, 0);
 
317
        for (int i = 0; i < dstCM.getColorSpace().getNumComponents(); i++) {
294
318
            copyBand(dstBI.getRaster(), i, wr00,    i);
 
319
        }
295
320
 
296
 
        if (dstCM.hasAlpha())
297
 
            copyBand(srcWr, srcSM.getNumBands()-1,
298
 
                     wr,    getSampleModel().getNumBands()-1);
 
321
        if (dstCM.hasAlpha()) {
 
322
            copyBand(srcWr, srcSM.getNumBands() - 1,
 
323
                     wr,    getSampleModel().getNumBands() - 1);
 
324
        }
299
325
        return wr;
300
326
    }
301
327
 
307
333
    protected static ColorModel fixColorModel(CachableRed src) {
308
334
        ColorModel  cm = src.getColorModel();
309
335
        if (cm != null) {
310
 
            if (cm.hasAlpha())
 
336
            if (cm.hasAlpha()) {
311
337
                return GraphicsUtil.sRGB_Unpre;
 
338
            }
312
339
 
313
340
            return GraphicsUtil.sRGB;
314
341
        } else {
344
371
 
345
372
        boolean alpha = false;
346
373
 
347
 
        if (cm != null)
 
374
        if (cm != null) {
348
375
            alpha = cm.hasAlpha();
349
 
        else {
 
376
        } else {
350
377
            switch (sm.getNumBands()) {
351
378
            case 1: case 3:
352
379
                alpha = false;
356
383
                break;
357
384
            }
358
385
        }
359
 
        if (alpha)
360
 
            return new SinglePixelPackedSampleModel
361
 
                (DataBuffer.TYPE_INT,
 
386
        if (alpha) {
 
387
            return new SinglePixelPackedSampleModel(
 
388
                DataBuffer.TYPE_INT,
362
389
                 sm.getWidth(),
363
390
                 sm.getHeight(),
364
391
                 new int [] {0xFF0000, 0xFF00, 0xFF, 0xFF000000});
365
 
        else
366
 
            return new SinglePixelPackedSampleModel
367
 
                (DataBuffer.TYPE_INT,
 
392
        } else {
 
393
            return new SinglePixelPackedSampleModel(
 
394
                DataBuffer.TYPE_INT,
368
395
                 sm.getWidth(),
369
396
                 sm.getHeight(),
370
397
                 new int [] {0xFF0000, 0xFF00, 0xFF});
 
398
        }
371
399
    }
372
400
}