summaryrefslogtreecommitdiffstats
path: root/awt/org/apache/harmony/awt/gl/color/ColorScaler.java
blob: a1cc169bb9a3a6671a90fcb76510ab2459872d45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
/**
 * @author Oleg V. Khaschansky
 * @version $Revision$
 */
package org.apache.harmony.awt.gl.color;

import java.awt.color.ColorSpace;
import java.awt.color.ICC_Profile;
import java.awt.image.DataBuffer;
import java.awt.image.Raster;
import java.awt.image.SampleModel;
import java.awt.image.WritableRaster;

/**
 * This class provides functionality for scaling color data when
 * ranges of the source and destination color values differs. 
 */
public class ColorScaler {
    private static final float MAX_SHORT = 0xFFFF;
    private static final float MAX_SIGNED_SHORT = 0x7FFF;

    private static final float MAX_XYZ = 1f + (32767f/32768f);

    // Cached values for scaling color data
    private float[] channelMinValues = null;
    private float[] channelMulipliers = null; // for scale
    private float[] invChannelMulipliers = null; // for unscale

    int nColorChannels = 0;

    // For scaling rasters, false if transfer type is double or float
    boolean isTTypeIntegral = false;

    /**
     * Loads scaling data for raster. Note, if profile pf is null,
     * for non-integral data types multipliers are not initialized.
     * @param r - raster
     * @param pf - profile which helps to determine the ranges of the color data
     */
    public void loadScalingData(Raster r, ICC_Profile pf) {
        boolean isSrcTTypeIntegral =
            r.getTransferType() != DataBuffer.TYPE_FLOAT &&
            r.getTransferType() != DataBuffer.TYPE_DOUBLE;
        if (isSrcTTypeIntegral)
            loadScalingData(r.getSampleModel());
        else if (pf != null)
            loadScalingData(pf);
    }

    /**
     * Use this method only for integral transfer types.
     * Extracts min/max values from the sample model
     * @param sm - sample model
     */
    public void loadScalingData(SampleModel sm) {
        // Supposing integral transfer type
        isTTypeIntegral = true;

        nColorChannels = sm.getNumBands();

        channelMinValues = new float[nColorChannels];
        channelMulipliers = new float[nColorChannels];
        invChannelMulipliers = new float[nColorChannels];

        boolean isSignedShort =
            (sm.getTransferType() == DataBuffer.TYPE_SHORT);

        float maxVal;
        for (int i=0; i<nColorChannels; i++) {
            channelMinValues[i] = 0;
            if (isSignedShort) {
                channelMulipliers[i] = MAX_SHORT / MAX_SIGNED_SHORT;
                invChannelMulipliers[i] = MAX_SIGNED_SHORT / MAX_SHORT;
            } else {
                maxVal = ((1 << sm.getSampleSize(i)) - 1);
                channelMulipliers[i] = MAX_SHORT / maxVal;
                invChannelMulipliers[i] = maxVal / MAX_SHORT;
            }
        }
    }

    /**
     * Use this method only for double of float transfer types.
     * Extracts scaling data from the color space signature
     * and other tags, stored in the profile
     * @param pf - ICC profile
     */
    public void loadScalingData(ICC_Profile pf) {
        // Supposing double or float transfer type
        isTTypeIntegral = false;

        nColorChannels = pf.getNumComponents();

        // Get min/max values directly from the profile
        // Very much like fillMinMaxValues in ICC_ColorSpace
        float maxValues[] = new float[nColorChannels];
        float minValues[] = new float[nColorChannels];

        switch (pf.getColorSpaceType()) {
            case ColorSpace.TYPE_XYZ:
                minValues[0] = 0;
                minValues[1] = 0;
                minValues[2] = 0;
                maxValues[0] = MAX_XYZ;
                maxValues[1] = MAX_XYZ;
                maxValues[2] = MAX_XYZ;
                break;
            case ColorSpace.TYPE_Lab:
                minValues[0] = 0;
                minValues[1] = -128;
                minValues[2] = -128;
                maxValues[0] = 100;
                maxValues[1] = 127;
                maxValues[2] = 127;
                break;
            default:
                for (int i=0; i<nColorChannels; i++) {
                    minValues[i] = 0;
                    maxValues[i] = 1;
                }
        }

        channelMinValues = minValues;
        channelMulipliers = new float[nColorChannels];
        invChannelMulipliers = new float[nColorChannels];

        for (int i = 0; i < nColorChannels; i++) {
            channelMulipliers[i] =
                MAX_SHORT / (maxValues[i] - channelMinValues[i]);

            invChannelMulipliers[i] =
                (maxValues[i] - channelMinValues[i]) / MAX_SHORT;
        }
    }

    /**
     * Extracts scaling data from the color space
     * @param cs - color space
     */
    public void loadScalingData(ColorSpace cs) {
        nColorChannels = cs.getNumComponents();

        channelMinValues = new float[nColorChannels];
        channelMulipliers = new float[nColorChannels];
        invChannelMulipliers = new float[nColorChannels];

        for (int i = 0; i < nColorChannels; i++) {
            channelMinValues[i] = cs.getMinValue(i);

            channelMulipliers[i] =
                MAX_SHORT / (cs.getMaxValue(i) - channelMinValues[i]);

            invChannelMulipliers[i] =
                (cs.getMaxValue(i) - channelMinValues[i]) / MAX_SHORT;
        }
    }

    /**
     * Scales and normalizes the whole raster and returns the result
     * in the float array
     * @param r - source raster
     * @return scaled and normalized raster data
     */
    public float[][] scaleNormalize(Raster r) {
        int width = r.getWidth();
        int height = r.getHeight();
        float result[][] = new float[width*height][nColorChannels];
        float normMultipliers[] = new float[nColorChannels];

        int pos = 0;
        if (isTTypeIntegral) {
            // Change max value from MAX_SHORT to 1f
            for (int i=0; i<nColorChannels; i++) {
                normMultipliers[i] = channelMulipliers[i] / MAX_SHORT;
            }

            int sample;
            for (int row=r.getMinX(); row<width; row++) {
                for (int col=r.getMinY(); col<height; col++) {
                    for (int chan = 0; chan < nColorChannels; chan++) {
                        sample = r.getSample(row, col, chan);
                        result[pos][chan] = (sample * normMultipliers[chan]);
                    }
                    pos++;
                }
            }
        } else { // Just get the samples...
            for (int row=r.getMinX(); row<width; row++) {
                for (int col=r.getMinY(); col<height; col++) {
                    for (int chan = 0; chan < nColorChannels; chan++) {
                        result[pos][chan] = r.getSampleFloat(row, col, chan);
                    }
                    pos++;
                }
            }
        }
        return result;
    }

    /**
     * Unscale the whole float array and put the result
     * in the raster
     * @param r - destination raster
     * @param data - input pixels
     */
    public void unscaleNormalized(WritableRaster r, float data[][]) {
        int width = r.getWidth();
        int height = r.getHeight();
        float normMultipliers[] = new float[nColorChannels];

        int pos = 0;
        if (isTTypeIntegral) {
            // Change max value from MAX_SHORT to 1f
            for (int i=0; i<nColorChannels; i++) {
                normMultipliers[i] = invChannelMulipliers[i] * MAX_SHORT;
            }

            int sample;
            for (int row=r.getMinX(); row<width; row++) {
                for (int col=r.getMinY(); col<height; col++) {
                    for (int chan = 0; chan < nColorChannels; chan++) {
                        sample = (int) (data[pos][chan] * normMultipliers[chan] + 0.5f);
                        r.setSample(row, col, chan, sample);
                    }
                    pos++;
                }
            }
        } else { // Just set the samples...
            for (int row=r.getMinX(); row<width; row++) {
                for (int col=r.getMinY(); col<height; col++) {
                    for (int chan = 0; chan < nColorChannels; chan++) {
                        r.setSample(row, col, chan, data[pos][chan]);
                    }
                    pos++;
                }
            }
        }
    }

    /**
     * Scales the whole raster to short and returns the result
     * in the array
     * @param r - source raster
     * @return scaled and normalized raster data
     */
    public short[] scale(Raster r) {
        int width = r.getWidth();
        int height = r.getHeight();
        short result[] = new short[width*height*nColorChannels];

        int pos = 0;
        if (isTTypeIntegral) {
            int sample;
            for (int row=r.getMinX(); row<width; row++) {
                for (int col=r.getMinY(); col<height; col++) {
                    for (int chan = 0; chan < nColorChannels; chan++) {
                        sample = r.getSample(row, col, chan);
                        result[pos++] =
                            (short) (sample * channelMulipliers[chan] + 0.5f);
                    }
                }
            }
        } else {
            float sample;
            for (int row=r.getMinX(); row<width; row++) {
                for (int col=r.getMinY(); col<height; col++) {
                    for (int chan = 0; chan < nColorChannels; chan++) {
                        sample = r.getSampleFloat(row, col, chan);
                        result[pos++] = (short) ((sample - channelMinValues[chan])
                            * channelMulipliers[chan] + 0.5f);
                    }
                }
            }
        }
        return result;
    }

    /**
     * Unscales the whole data array and puts obtained values to the raster
     * @param data - input data
     * @param wr - destination raster
     */
    public void unscale(short[] data, WritableRaster wr) {
        int width = wr.getWidth();
        int height = wr.getHeight();

        int pos = 0;
        if (isTTypeIntegral) {
            int sample;
            for (int row=wr.getMinX(); row<width; row++) {
                for (int col=wr.getMinY(); col<height; col++) {
                    for (int chan = 0; chan < nColorChannels; chan++) {
                         sample = (int) ((data[pos++] & 0xFFFF) *
                                invChannelMulipliers[chan] + 0.5f);
                         wr.setSample(row, col, chan, sample);
                    }
                }
            }
        } else {
            float sample;
            for (int row=wr.getMinX(); row<width; row++) {
                for (int col=wr.getMinY(); col<height; col++) {
                    for (int chan = 0; chan < nColorChannels; chan++) {
                         sample = (data[pos++] & 0xFFFF) *
                            invChannelMulipliers[chan] + channelMinValues[chan];
                         wr.setSample(row, col, chan, sample);
                    }
                }
            }
        }
    }

    /**
     * Scales one pixel and puts obtained values to the chanData
     * @param pixelData - input pixel
     * @param chanData - output buffer
     * @param chanDataOffset - output buffer offset
     */
    public void scale(float[] pixelData, short[] chanData, int chanDataOffset) {
        for (int chan = 0; chan < nColorChannels; chan++) {
            chanData[chanDataOffset + chan] =
                    (short) ((pixelData[chan] - channelMinValues[chan]) *
                        channelMulipliers[chan] + 0.5f);
        }
    }

    /**
     * Unscales one pixel and puts obtained values to the pixelData
     * @param pixelData - output pixel
     * @param chanData - input buffer
     * @param chanDataOffset - input buffer offset
     */
    public void unscale(float[] pixelData, short[] chanData, int chanDataOffset) {
        for (int chan = 0; chan < nColorChannels; chan++) {
            pixelData[chan] = (chanData[chanDataOffset + chan] & 0xFFFF)
                * invChannelMulipliers[chan] + channelMinValues[chan];
        }
    }
}