-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentColorModel.java
More file actions
2948 lines (2869 loc) · 131 KB
/
ComponentColorModel.java
File metadata and controls
2948 lines (2869 loc) · 131 KB
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package java.awt.image;
import java.awt.color.ColorSpace;
import java.awt.color.ICC_ColorSpace;
/**
* A <CODE>ColorModel</CODE> class that works with pixel values that
* represent color and alpha information as separate samples and that
* store each sample in a separate data element. This class can be
* used with an arbitrary <CODE>ColorSpace</CODE>. The number of
* color samples in the pixel values must be same as the number of
* color components in the <CODE>ColorSpace</CODE>. There may be a
* single alpha sample.
* <p>
* For those methods that use
* a primitive array pixel representation of type <CODE>transferType</CODE>,
* the array length is the same as the number of color and alpha samples.
* Color samples are stored first in the array followed by the alpha
* sample, if present. The order of the color samples is specified
* by the <CODE>ColorSpace</CODE>. Typically, this order reflects the
* name of the color space type. For example, for <CODE>TYPE_RGB</CODE>,
* index 0 corresponds to red, index 1 to green, and index 2 to blue.
* <p>
* The translation from pixel sample values to color/alpha components for
* display or processing purposes is based on a one-to-one correspondence of
* samples to components.
* Depending on the transfer type used to create an instance of
* <code>ComponentColorModel</code>, the pixel sample values
* represented by that instance may be signed or unsigned and may
* be of integral type or float or double (see below for details).
* The translation from sample values to normalized color/alpha components
* must follow certain rules. For float and double samples, the translation
* is an identity, i.e. normalized component values are equal to the
* corresponding sample values. For integral samples, the translation
* should be only a simple scale and offset, where the scale and offset
* constants may be different for each component. The result of
* applying the scale and offset constants is a set of color/alpha
* component values, which are guaranteed to fall within a certain
* range. Typically, the range for a color component will be the range
* defined by the <code>getMinValue</code> and <code>getMaxValue</code>
* methods of the <code>ColorSpace</code> class. The range for an
* alpha component should be 0.0 to 1.0.
* <p>
* Instances of <code>ComponentColorModel</code> created with transfer types
* <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
* and <CODE>DataBuffer.TYPE_INT</CODE> have pixel sample values which
* are treated as unsigned integral values.
* The number of bits in a color or alpha sample of a pixel value might not
* be the same as the number of bits for the corresponding color or alpha
* sample passed to the
* <code>ComponentColorModel(ColorSpace, int[], boolean, boolean, int, int)</code>
* constructor. In
* that case, this class assumes that the least significant n bits of a sample
* value hold the component value, where n is the number of significant bits
* for the component passed to the constructor. It also assumes that
* any higher-order bits in a sample value are zero. Thus, sample values
* range from 0 to 2<sup>n</sup> - 1. This class maps these sample values
* to normalized color component values such that 0 maps to the value
* obtained from the <code>ColorSpace's</code> <code>getMinValue</code>
* method for each component and 2<sup>n</sup> - 1 maps to the value
* obtained from <code>getMaxValue</code>. To create a
* <code>ComponentColorModel</code> with a different color sample mapping
* requires subclassing this class and overriding the
* <code>getNormalizedComponents(Object, float[], int)</code> method.
* The mapping for an alpha sample always maps 0 to 0.0 and
* 2<sup>n</sup> - 1 to 1.0.
* <p>
* For instances with unsigned sample values,
* the unnormalized color/alpha component representation is only
* supported if two conditions hold. First, sample value value 0 must
* map to normalized component value 0.0 and sample value 2<sup>n</sup> - 1
* to 1.0. Second the min/max range of all color components of the
* <code>ColorSpace</code> must be 0.0 to 1.0. In this case, the
* component representation is the n least
* significant bits of the corresponding sample. Thus each component is
* an unsigned integral value between 0 and 2<sup>n</sup> - 1, where
* n is the number of significant bits for a particular component.
* If these conditions are not met, any method taking an unnormalized
* component argument will throw an <code>IllegalArgumentException</code>.
* <p>
* Instances of <code>ComponentColorModel</code> created with transfer types
* <CODE>DataBuffer.TYPE_SHORT</CODE>, <CODE>DataBuffer.TYPE_FLOAT</CODE>, and
* <CODE>DataBuffer.TYPE_DOUBLE</CODE> have pixel sample values which
* are treated as signed short, float, or double values.
* Such instances do not support the unnormalized color/alpha component
* representation, so any methods taking such a representation as an argument
* will throw an <code>IllegalArgumentException</code> when called on one
* of these instances. The normalized component values of instances
* of this class have a range which depends on the transfer
* type as follows: for float samples, the full range of the float data
* type; for double samples, the full range of the float data type
* (resulting from casting double to float); for short samples,
* from approximately -maxVal to +maxVal, where maxVal is the per
* component maximum value for the <code>ColorSpace</code>
* (-32767 maps to -maxVal, 0 maps to 0.0, and 32767 maps
* to +maxVal). A subclass may override the scaling for short sample
* values to normalized component values by overriding the
* <code>getNormalizedComponents(Object, float[], int)</code> method.
* For float and double samples, the normalized component values are
* taken to be equal to the corresponding sample values, and subclasses
* should not attempt to add any non-identity scaling for these transfer
* types.
* <p>
* Instances of <code>ComponentColorModel</code> created with transfer types
* <CODE>DataBuffer.TYPE_SHORT</CODE>, <CODE>DataBuffer.TYPE_FLOAT</CODE>, and
* <CODE>DataBuffer.TYPE_DOUBLE</CODE>
* use all the bits of all sample values. Thus all color/alpha components
* have 16 bits when using <CODE>DataBuffer.TYPE_SHORT</CODE>, 32 bits when
* using <CODE>DataBuffer.TYPE_FLOAT</CODE>, and 64 bits when using
* <CODE>DataBuffer.TYPE_DOUBLE</CODE>. When the
* <code>ComponentColorModel(ColorSpace, int[], boolean, boolean, int, int)</code>
* form of constructor is used with one of these transfer types, the
* bits array argument is ignored.
* <p>
* It is possible to have color/alpha sample values
* which cannot be reasonably interpreted as component values for rendering.
* This can happen when <code>ComponentColorModel</code> is subclassed to
* override the mapping of unsigned sample values to normalized color
* component values or when signed sample values outside a certain range
* are used. (As an example, specifying an alpha component as a signed
* short value outside the range 0 to 32767, normalized range 0.0 to 1.0, can
* lead to unexpected results.) It is the
* responsibility of applications to appropriately scale pixel data before
* rendering such that color components fall within the normalized range
* of the <code>ColorSpace</code> (obtained using the <code>getMinValue</code>
* and <code>getMaxValue</code> methods of the <code>ColorSpace</code> class)
* and the alpha component is between 0.0 and 1.0. If color or alpha
* component values fall outside these ranges, rendering results are
* indeterminate.
* <p>
* Methods that use a single int pixel representation throw
* an <CODE>IllegalArgumentException</CODE>, unless the number of components
* for the <CODE>ComponentColorModel</CODE> is one and the component
* value is unsigned -- in other words, a single color component using
* a transfer type of <CODE>DataBuffer.TYPE_BYTE</CODE>,
* <CODE>DataBuffer.TYPE_USHORT</CODE>, or <CODE>DataBuffer.TYPE_INT</CODE>
* and no alpha.
* <p>
* A <CODE>ComponentColorModel</CODE> can be used in conjunction with a
* <CODE>ComponentSampleModel</CODE>, a <CODE>BandedSampleModel</CODE>,
* or a <CODE>PixelInterleavedSampleModel</CODE> to construct a
* <CODE>BufferedImage</CODE>.
*
* @see ColorModel
* @see ColorSpace
* @see ComponentSampleModel
* @see BandedSampleModel
* @see PixelInterleavedSampleModel
* @see BufferedImage
*
*/
public class ComponentColorModel extends ColorModel {
/**
* <code>signed</code> is <code>true</code> for <code>short</code>,
* <code>float</code>, and <code>double</code> transfer types; it
* is <code>false</code> for <code>byte</code>, <code>ushort</code>,
* and <code>int</code> transfer types.
*/
private boolean signed; // true for transfer types short, float, double
// false for byte, ushort, int
private boolean is_sRGB_stdScale;
private boolean is_LinearRGB_stdScale;
private boolean is_LinearGray_stdScale;
private boolean is_ICCGray_stdScale;
private byte[] tosRGB8LUT;
private byte[] fromsRGB8LUT8;
private short[] fromsRGB8LUT16;
private byte[] fromLinearGray16ToOtherGray8LUT;
private short[] fromLinearGray16ToOtherGray16LUT;
private boolean needScaleInit;
private boolean noUnnorm;
private boolean nonStdScale;
private float[] min;
private float[] diffMinMax;
private float[] compOffset;
private float[] compScale;
/**
* Constructs a <CODE>ComponentColorModel</CODE> from the specified
* parameters. Color components will be in the specified
* <CODE>ColorSpace</CODE>. The supported transfer types are
* <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
* <CODE>DataBuffer.TYPE_INT</CODE>,
* <CODE>DataBuffer.TYPE_SHORT</CODE>, <CODE>DataBuffer.TYPE_FLOAT</CODE>,
* and <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
* If not null, the <CODE>bits</CODE> array specifies the
* number of significant bits per color and alpha component and its
* length should be at least the number of components in the
* <CODE>ColorSpace</CODE> if there is no alpha
* information in the pixel values, or one more than this number if
* there is alpha information. When the <CODE>transferType</CODE> is
* <CODE>DataBuffer.TYPE_SHORT</CODE>, <CODE>DataBuffer.TYPE_FLOAT</CODE>,
* or <CODE>DataBuffer.TYPE_DOUBLE</CODE> the <CODE>bits</CODE> array
* argument is ignored. <CODE>hasAlpha</CODE> indicates whether alpha
* information is present. If <CODE>hasAlpha</CODE> is true, then
* the boolean <CODE>isAlphaPremultiplied</CODE>
* specifies how to interpret color and alpha samples in pixel values.
* If the boolean is true, color samples are assumed to have been
* multiplied by the alpha sample. The <CODE>transparency</CODE>
* specifies what alpha values can be represented by this color model.
* The acceptable <code>transparency</code> values are
* <CODE>OPAQUE</CODE>, <CODE>BITMASK</CODE> or <CODE>TRANSLUCENT</CODE>.
* The <CODE>transferType</CODE> is the type of primitive array used
* to represent pixel values.
*
* @param colorSpace The <CODE>ColorSpace</CODE> associated
* with this color model.
* @param bits The number of significant bits per component.
* May be null, in which case all bits of all
* component samples will be significant.
* Ignored if transferType is one of
* <CODE>DataBuffer.TYPE_SHORT</CODE>,
* <CODE>DataBuffer.TYPE_FLOAT</CODE>, or
* <CODE>DataBuffer.TYPE_DOUBLE</CODE>,
* in which case all bits of all component
* samples will be significant.
* @param hasAlpha If true, this color model supports alpha.
* @param isAlphaPremultiplied If true, alpha is premultiplied.
* @param transparency Specifies what alpha values can be represented
* by this color model.
* @param transferType Specifies the type of primitive array used to
* represent pixel values.
*
* @throws IllegalArgumentException If the <CODE>bits</CODE> array
* argument is not null, its length is less than the number of
* color and alpha components, and transferType is one of
* <CODE>DataBuffer.TYPE_BYTE</CODE>,
* <CODE>DataBuffer.TYPE_USHORT</CODE>, or
* <CODE>DataBuffer.TYPE_INT</CODE>.
* @throws IllegalArgumentException If transferType is not one of
* <CODE>DataBuffer.TYPE_BYTE</CODE>,
* <CODE>DataBuffer.TYPE_USHORT</CODE>,
* <CODE>DataBuffer.TYPE_INT</CODE>,
* <CODE>DataBuffer.TYPE_SHORT</CODE>,
* <CODE>DataBuffer.TYPE_FLOAT</CODE>, or
* <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
*
* @see ColorSpace
* @see java.awt.Transparency
*/
public ComponentColorModel (ColorSpace colorSpace,
int[] bits,
boolean hasAlpha,
boolean isAlphaPremultiplied,
int transparency,
int transferType) {
super (bitsHelper(transferType, colorSpace, hasAlpha),
bitsArrayHelper(bits, transferType, colorSpace, hasAlpha),
colorSpace, hasAlpha, isAlphaPremultiplied, transparency,
transferType);
switch(transferType) {
case DataBuffer.TYPE_BYTE:
case DataBuffer.TYPE_USHORT:
case DataBuffer.TYPE_INT:
signed = false;
needScaleInit = true;
break;
case DataBuffer.TYPE_SHORT:
signed = true;
needScaleInit = true;
break;
case DataBuffer.TYPE_FLOAT:
case DataBuffer.TYPE_DOUBLE:
signed = true;
needScaleInit = false;
noUnnorm = true;
nonStdScale = false;
break;
default:
throw new IllegalArgumentException("This constructor is not "+
"compatible with transferType " + transferType);
}
setupLUTs();
}
/**
* Constructs a <CODE>ComponentColorModel</CODE> from the specified
* parameters. Color components will be in the specified
* <CODE>ColorSpace</CODE>. The supported transfer types are
* <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
* <CODE>DataBuffer.TYPE_INT</CODE>,
* <CODE>DataBuffer.TYPE_SHORT</CODE>, <CODE>DataBuffer.TYPE_FLOAT</CODE>,
* and <CODE>DataBuffer.TYPE_DOUBLE</CODE>. The number of significant
* bits per color and alpha component will be 8, 16, 32, 16, 32, or 64,
* respectively. The number of color components will be the
* number of components in the <CODE>ColorSpace</CODE>. There will be
* an alpha component if <CODE>hasAlpha</CODE> is <CODE>true</CODE>.
* If <CODE>hasAlpha</CODE> is true, then
* the boolean <CODE>isAlphaPremultiplied</CODE>
* specifies how to interpret color and alpha samples in pixel values.
* If the boolean is true, color samples are assumed to have been
* multiplied by the alpha sample. The <CODE>transparency</CODE>
* specifies what alpha values can be represented by this color model.
* The acceptable <code>transparency</code> values are
* <CODE>OPAQUE</CODE>, <CODE>BITMASK</CODE> or <CODE>TRANSLUCENT</CODE>.
* The <CODE>transferType</CODE> is the type of primitive array used
* to represent pixel values.
*
* @param colorSpace The <CODE>ColorSpace</CODE> associated
* with this color model.
* @param hasAlpha If true, this color model supports alpha.
* @param isAlphaPremultiplied If true, alpha is premultiplied.
* @param transparency Specifies what alpha values can be represented
* by this color model.
* @param transferType Specifies the type of primitive array used to
* represent pixel values.
*
* @throws IllegalArgumentException If transferType is not one of
* <CODE>DataBuffer.TYPE_BYTE</CODE>,
* <CODE>DataBuffer.TYPE_USHORT</CODE>,
* <CODE>DataBuffer.TYPE_INT</CODE>,
* <CODE>DataBuffer.TYPE_SHORT</CODE>,
* <CODE>DataBuffer.TYPE_FLOAT</CODE>, or
* <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
*
* @see ColorSpace
* @see java.awt.Transparency
* @since 1.4
*/
public ComponentColorModel (ColorSpace colorSpace,
boolean hasAlpha,
boolean isAlphaPremultiplied,
int transparency,
int transferType) {
this(colorSpace, null, hasAlpha, isAlphaPremultiplied,
transparency, transferType);
}
private static int bitsHelper(int transferType,
ColorSpace colorSpace,
boolean hasAlpha) {
int numBits = DataBuffer.getDataTypeSize(transferType);
int numComponents = colorSpace.getNumComponents();
if (hasAlpha) {
++numComponents;
}
return numBits * numComponents;
}
private static int[] bitsArrayHelper(int[] origBits,
int transferType,
ColorSpace colorSpace,
boolean hasAlpha) {
switch(transferType) {
case DataBuffer.TYPE_BYTE:
case DataBuffer.TYPE_USHORT:
case DataBuffer.TYPE_INT:
if (origBits != null) {
return origBits;
}
break;
default:
break;
}
int numBits = DataBuffer.getDataTypeSize(transferType);
int numComponents = colorSpace.getNumComponents();
if (hasAlpha) {
++numComponents;
}
int[] bits = new int[numComponents];
for (int i = 0; i < numComponents; i++) {
bits[i] = numBits;
}
return bits;
}
private void setupLUTs() {
// REMIND: there is potential to accelerate sRGB, LinearRGB,
// LinearGray, ICCGray, and non-ICC Gray spaces with non-standard
// scaling, if that becomes important
//
// NOTE: The is_xxx_stdScale and nonStdScale booleans are provisionally
// set here when this method is called at construction time. These
// variables may be set again when initScale is called later.
// When setupLUTs returns, nonStdScale is true if (the transferType
// is not float or double) AND (some minimum ColorSpace component
// value is not 0.0 OR some maximum ColorSpace component value
// is not 1.0). This is correct for the calls to
// getNormalizedComponents(Object, float[], int) from initScale().
// initScale() may change the value nonStdScale based on the
// return value of getNormalizedComponents() - this will only
// happen if getNormalizedComponents() has been overridden by a
// subclass to make the mapping of min/max pixel sample values
// something different from min/max color component values.
if (is_sRGB) {
is_sRGB_stdScale = true;
nonStdScale = false;
} else if (ColorModel.isLinearRGBspace(colorSpace)) {
// Note that the built-in Linear RGB space has a normalized
// range of 0.0 - 1.0 for each coordinate. Usage of these
// LUTs makes that assumption.
is_LinearRGB_stdScale = true;
nonStdScale = false;
if (transferType == DataBuffer.TYPE_BYTE) {
tosRGB8LUT = ColorModel.getLinearRGB8TosRGB8LUT();
fromsRGB8LUT8 = ColorModel.getsRGB8ToLinearRGB8LUT();
} else {
tosRGB8LUT = ColorModel.getLinearRGB16TosRGB8LUT();
fromsRGB8LUT16 = ColorModel.getsRGB8ToLinearRGB16LUT();
}
} else if ((colorSpaceType == ColorSpace.TYPE_GRAY) &&
(colorSpace instanceof ICC_ColorSpace) &&
(colorSpace.getMinValue(0) == 0.0f) &&
(colorSpace.getMaxValue(0) == 1.0f)) {
// Note that a normalized range of 0.0 - 1.0 for the gray
// component is required, because usage of these LUTs makes
// that assumption.
ICC_ColorSpace ics = (ICC_ColorSpace) colorSpace;
is_ICCGray_stdScale = true;
nonStdScale = false;
fromsRGB8LUT16 = ColorModel.getsRGB8ToLinearRGB16LUT();
if (ColorModel.isLinearGRAYspace(ics)) {
is_LinearGray_stdScale = true;
if (transferType == DataBuffer.TYPE_BYTE) {
tosRGB8LUT = ColorModel.getGray8TosRGB8LUT(ics);
} else {
tosRGB8LUT = ColorModel.getGray16TosRGB8LUT(ics);
}
} else {
if (transferType == DataBuffer.TYPE_BYTE) {
tosRGB8LUT = ColorModel.getGray8TosRGB8LUT(ics);
fromLinearGray16ToOtherGray8LUT =
ColorModel.getLinearGray16ToOtherGray8LUT(ics);
} else {
tosRGB8LUT = ColorModel.getGray16TosRGB8LUT(ics);
fromLinearGray16ToOtherGray16LUT =
ColorModel.getLinearGray16ToOtherGray16LUT(ics);
}
}
} else if (needScaleInit) {
// if transferType is byte, ushort, int, or short and we
// don't already know the ColorSpace has minVlaue == 0.0f and
// maxValue == 1.0f for all components, we need to check that
// now and setup the min[] and diffMinMax[] arrays if necessary.
nonStdScale = false;
for (int i = 0; i < numColorComponents; i++) {
if ((colorSpace.getMinValue(i) != 0.0f) ||
(colorSpace.getMaxValue(i) != 1.0f)) {
nonStdScale = true;
break;
}
}
if (nonStdScale) {
min = new float[numColorComponents];
diffMinMax = new float[numColorComponents];
for (int i = 0; i < numColorComponents; i++) {
min[i] = colorSpace.getMinValue(i);
diffMinMax[i] = colorSpace.getMaxValue(i) - min[i];
}
}
}
}
private void initScale() {
// This method is called the first time any method which uses
// pixel sample value to color component value scaling information
// is called if the transferType supports non-standard scaling
// as defined above (byte, ushort, int, and short), unless the
// method is getNormalizedComponents(Object, float[], int) (that
// method must be overridden to use non-standard scaling). This
// method also sets up the noUnnorm boolean variable for these
// transferTypes. After this method is called, the nonStdScale
// variable will be true if getNormalizedComponents() maps a
// sample value of 0 to anything other than 0.0f OR maps a
// sample value of 2^^n - 1 (2^^15 - 1 for short transferType)
// to anything other than 1.0f. Note that this can be independent
// of the colorSpace min/max component values, if the
// getNormalizedComponents() method has been overridden for some
// reason, e.g. to provide greater dynamic range in the sample
// values than in the color component values. Unfortunately,
// this method can't be called at construction time, since a
// subclass may still have uninitialized state that would cause
// getNormalizedComponents() to return an incorrect result.
needScaleInit = false; // only needs to called once
if (nonStdScale || signed) {
// The unnormalized form is only supported for unsigned
// transferTypes and when the ColorSpace min/max values
// are 0.0/1.0. When this method is called nonStdScale is
// true if the latter condition does not hold. In addition,
// the unnormalized form requires that the full range of
// the pixel sample values map to the full 0.0 - 1.0 range
// of color component values. That condition is checked
// later in this method.
noUnnorm = true;
} else {
noUnnorm = false;
}
float[] lowVal, highVal;
switch (transferType) {
case DataBuffer.TYPE_BYTE:
{
byte[] bpixel = new byte[numComponents];
for (int i = 0; i < numColorComponents; i++) {
bpixel[i] = 0;
}
if (supportsAlpha) {
bpixel[numColorComponents] =
(byte) ((1 << nBits[numColorComponents]) - 1);
}
lowVal = getNormalizedComponents(bpixel, null, 0);
for (int i = 0; i < numColorComponents; i++) {
bpixel[i] = (byte) ((1 << nBits[i]) - 1);
}
highVal = getNormalizedComponents(bpixel, null, 0);
}
break;
case DataBuffer.TYPE_USHORT:
{
short[] uspixel = new short[numComponents];
for (int i = 0; i < numColorComponents; i++) {
uspixel[i] = 0;
}
if (supportsAlpha) {
uspixel[numColorComponents] =
(short) ((1 << nBits[numColorComponents]) - 1);
}
lowVal = getNormalizedComponents(uspixel, null, 0);
for (int i = 0; i < numColorComponents; i++) {
uspixel[i] = (short) ((1 << nBits[i]) - 1);
}
highVal = getNormalizedComponents(uspixel, null, 0);
}
break;
case DataBuffer.TYPE_INT:
{
int[] ipixel = new int[numComponents];
for (int i = 0; i < numColorComponents; i++) {
ipixel[i] = 0;
}
if (supportsAlpha) {
ipixel[numColorComponents] =
((1 << nBits[numColorComponents]) - 1);
}
lowVal = getNormalizedComponents(ipixel, null, 0);
for (int i = 0; i < numColorComponents; i++) {
ipixel[i] = ((1 << nBits[i]) - 1);
}
highVal = getNormalizedComponents(ipixel, null, 0);
}
break;
case DataBuffer.TYPE_SHORT:
{
short[] spixel = new short[numComponents];
for (int i = 0; i < numColorComponents; i++) {
spixel[i] = 0;
}
if (supportsAlpha) {
spixel[numColorComponents] = 32767;
}
lowVal = getNormalizedComponents(spixel, null, 0);
for (int i = 0; i < numColorComponents; i++) {
spixel[i] = 32767;
}
highVal = getNormalizedComponents(spixel, null, 0);
}
break;
default:
lowVal = highVal = null; // to keep the compiler from complaining
break;
}
nonStdScale = false;
for (int i = 0; i < numColorComponents; i++) {
if ((lowVal[i] != 0.0f) || (highVal[i] != 1.0f)) {
nonStdScale = true;
break;
}
}
if (nonStdScale) {
noUnnorm = true;
is_sRGB_stdScale = false;
is_LinearRGB_stdScale = false;
is_LinearGray_stdScale = false;
is_ICCGray_stdScale = false;
compOffset = new float[numColorComponents];
compScale = new float[numColorComponents];
for (int i = 0; i < numColorComponents; i++) {
compOffset[i] = lowVal[i];
compScale[i] = 1.0f / (highVal[i] - lowVal[i]);
}
}
}
private int getRGBComponent(int pixel, int idx) {
if (numComponents > 1) {
throw new
IllegalArgumentException("More than one component per pixel");
}
if (signed) {
throw new
IllegalArgumentException("Component value is signed");
}
if (needScaleInit) {
initScale();
}
// Since there is only 1 component, there is no alpha
// Normalize the pixel in order to convert it
Object opixel = null;
switch (transferType) {
case DataBuffer.TYPE_BYTE:
{
byte[] bpixel = { (byte) pixel };
opixel = bpixel;
}
break;
case DataBuffer.TYPE_USHORT:
{
short[] spixel = { (short) pixel };
opixel = spixel;
}
break;
case DataBuffer.TYPE_INT:
{
int[] ipixel = { pixel };
opixel = ipixel;
}
break;
}
float[] norm = getNormalizedComponents(opixel, null, 0);
float[] rgb = colorSpace.toRGB(norm);
return (int) (rgb[idx] * 255.0f + 0.5f);
}
/**
* Returns the red color component for the specified pixel, scaled
* from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion
* is done if necessary. The pixel value is specified as an int.
* The returned value will be a non pre-multiplied value.
* If the alpha is premultiplied, this method divides
* it out before returning the value (if the alpha value is 0,
* the red value will be 0).
*
* @param pixel The pixel from which you want to get the red color component.
*
* @return The red color component for the specified pixel, as an int.
*
* @throws IllegalArgumentException If there is more than
* one component in this <CODE>ColorModel</CODE>.
* @throws IllegalArgumentException If the component value for this
* <CODE>ColorModel</CODE> is signed
*/
public int getRed(int pixel) {
return getRGBComponent(pixel, 0);
}
/**
* Returns the green color component for the specified pixel, scaled
* from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion
* is done if necessary. The pixel value is specified as an int.
* The returned value will be a non
* pre-multiplied value. If the alpha is premultiplied, this method
* divides it out before returning the value (if the alpha value is 0,
* the green value will be 0).
*
* @param pixel The pixel from which you want to get the green color component.
*
* @return The green color component for the specified pixel, as an int.
*
* @throws IllegalArgumentException If there is more than
* one component in this <CODE>ColorModel</CODE>.
* @throws IllegalArgumentException If the component value for this
* <CODE>ColorModel</CODE> is signed
*/
public int getGreen(int pixel) {
return getRGBComponent(pixel, 1);
}
/**
* Returns the blue color component for the specified pixel, scaled
* from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion
* is done if necessary. The pixel value is specified as an int.
* The returned value will be a non
* pre-multiplied value. If the alpha is premultiplied, this method
* divides it out before returning the value (if the alpha value is 0,
* the blue value will be 0).
*
* @param pixel The pixel from which you want to get the blue color component.
*
* @return The blue color component for the specified pixel, as an int.
*
* @throws IllegalArgumentException If there is more than
* one component in this <CODE>ColorModel</CODE>.
* @throws IllegalArgumentException If the component value for this
* <CODE>ColorModel</CODE> is signed
*/
public int getBlue(int pixel) {
return getRGBComponent(pixel, 2);
}
/**
* Returns the alpha component for the specified pixel, scaled
* from 0 to 255. The pixel value is specified as an int.
*
* @param pixel The pixel from which you want to get the alpha component.
*
* @return The alpha component for the specified pixel, as an int.
*
* @throws IllegalArgumentException If there is more than
* one component in this <CODE>ColorModel</CODE>.
* @throws IllegalArgumentException If the component value for this
* <CODE>ColorModel</CODE> is signed
*/
public int getAlpha(int pixel) {
if (supportsAlpha == false) {
return 255;
}
if (numComponents > 1) {
throw new
IllegalArgumentException("More than one component per pixel");
}
if (signed) {
throw new
IllegalArgumentException("Component value is signed");
}
return (int) ((((float) pixel) / ((1<<nBits[0])-1)) * 255.0f + 0.5f);
}
/**
* Returns the color/alpha components of the pixel in the default
* RGB color model format. A color conversion is done if necessary.
* The returned value will be in a non pre-multiplied format. If
* the alpha is premultiplied, this method divides it out of the
* color components (if the alpha value is 0, the color values will be 0).
*
* @param pixel The pixel from which you want to get the color/alpha components.
*
* @return The color/alpha components for the specified pixel, as an int.
*
* @throws IllegalArgumentException If there is more than
* one component in this <CODE>ColorModel</CODE>.
* @throws IllegalArgumentException If the component value for this
* <CODE>ColorModel</CODE> is signed
*/
public int getRGB(int pixel) {
if (numComponents > 1) {
throw new
IllegalArgumentException("More than one component per pixel");
}
if (signed) {
throw new
IllegalArgumentException("Component value is signed");
}
return (getAlpha(pixel) << 24)
| (getRed(pixel) << 16)
| (getGreen(pixel) << 8)
| (getBlue(pixel) << 0);
}
private int extractComponent(Object inData, int idx, int precision) {
// Extract component idx from inData. The precision argument
// should be either 8 or 16. If it's 8, this method will return
// an 8-bit value. If it's 16, this method will return a 16-bit
// value for transferTypes other than TYPE_BYTE. For TYPE_BYTE,
// an 8-bit value will be returned.
// This method maps the input value corresponding to a
// normalized ColorSpace component value of 0.0 to 0, and the
// input value corresponding to a normalized ColorSpace
// component value of 1.0 to 2^n - 1 (where n is 8 or 16), so
// it is appropriate only for ColorSpaces with min/max component
// values of 0.0/1.0. This will be true for sRGB, the built-in
// Linear RGB and Linear Gray spaces, and any other ICC grayscale
// spaces for which we have precomputed LUTs.
boolean needAlpha = (supportsAlpha && isAlphaPremultiplied);
int alp = 0;
int comp;
int mask = (1 << nBits[idx]) - 1;
switch (transferType) {
// Note: we do no clamping of the pixel data here - we
// assume that the data is scaled properly
case DataBuffer.TYPE_SHORT: {
short sdata[] = (short[]) inData;
float scalefactor = (float) ((1 << precision) - 1);
if (needAlpha) {
short s = sdata[numColorComponents];
if (s != (short) 0) {
return (int) ((((float) sdata[idx]) /
((float) s)) * scalefactor + 0.5f);
} else {
return 0;
}
} else {
return (int) ((sdata[idx] / 32767.0f) * scalefactor + 0.5f);
}
}
case DataBuffer.TYPE_FLOAT: {
float fdata[] = (float[]) inData;
float scalefactor = (float) ((1 << precision) - 1);
if (needAlpha) {
float f = fdata[numColorComponents];
if (f != 0.0f) {
return (int) (((fdata[idx] / f) * scalefactor) + 0.5f);
} else {
return 0;
}
} else {
return (int) (fdata[idx] * scalefactor + 0.5f);
}
}
case DataBuffer.TYPE_DOUBLE: {
double ddata[] = (double[]) inData;
double scalefactor = (double) ((1 << precision) - 1);
if (needAlpha) {
double d = ddata[numColorComponents];
if (d != 0.0) {
return (int) (((ddata[idx] / d) * scalefactor) + 0.5);
} else {
return 0;
}
} else {
return (int) (ddata[idx] * scalefactor + 0.5);
}
}
case DataBuffer.TYPE_BYTE:
byte bdata[] = (byte[])inData;
comp = bdata[idx] & mask;
precision = 8;
if (needAlpha) {
alp = bdata[numColorComponents] & mask;
}
break;
case DataBuffer.TYPE_USHORT:
short usdata[] = (short[])inData;
comp = usdata[idx] & mask;
if (needAlpha) {
alp = usdata[numColorComponents] & mask;
}
break;
case DataBuffer.TYPE_INT:
int idata[] = (int[])inData;
comp = idata[idx];
if (needAlpha) {
alp = idata[numColorComponents];
}
break;
default:
throw new
UnsupportedOperationException("This method has not "+
"been implemented for transferType " + transferType);
}
if (needAlpha) {
if (alp != 0) {
float scalefactor = (float) ((1 << precision) - 1);
float fcomp = ((float) comp) / ((float)mask);
float invalp = ((float) ((1<<nBits[numColorComponents]) - 1)) /
((float) alp);
return (int) (fcomp * invalp * scalefactor + 0.5f);
} else {
return 0;
}
} else {
if (nBits[idx] != precision) {
float scalefactor = (float) ((1 << precision) - 1);
float fcomp = ((float) comp) / ((float)mask);
return (int) (fcomp * scalefactor + 0.5f);
}
return comp;
}
}
private int getRGBComponent(Object inData, int idx) {
if (needScaleInit) {
initScale();
}
if (is_sRGB_stdScale) {
return extractComponent(inData, idx, 8);
} else if (is_LinearRGB_stdScale) {
int lutidx = extractComponent(inData, idx, 16);
return tosRGB8LUT[lutidx] & 0xff;
} else if (is_ICCGray_stdScale) {
int lutidx = extractComponent(inData, 0, 16);
return tosRGB8LUT[lutidx] & 0xff;
}
// Not CS_sRGB, CS_LINEAR_RGB, or any TYPE_GRAY ICC_ColorSpace
float[] norm = getNormalizedComponents(inData, null, 0);
// Note that getNormalizedComponents returns non-premultiplied values
float[] rgb = colorSpace.toRGB(norm);
return (int) (rgb[idx] * 255.0f + 0.5f);
}
/**
* Returns the red color component for the specified pixel, scaled
* from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion
* is done if necessary. The <CODE>pixel</CODE> value is specified by an array
* of data elements of type <CODE>transferType</CODE> passed in as an object
* reference. The returned value will be a non pre-multiplied value. If the
* alpha is premultiplied, this method divides it out before returning
* the value (if the alpha value is 0, the red value will be 0). Since
* <code>ComponentColorModel</code> can be subclassed, subclasses
* inherit the implementation of this method and if they don't override
* it then they throw an exception if they use an unsupported
* <code>transferType</code>.
*
* @param inData The pixel from which you want to get the red color component,
* specified by an array of data elements of type <CODE>transferType</CODE>.
*
* @return The red color component for the specified pixel, as an int.
*
* @throws ClassCastException If <CODE>inData</CODE> is not a primitive array
* of type <CODE>transferType</CODE>.
* @throws ArrayIndexOutOfBoundsException if <CODE>inData</CODE> is not
* large enough to hold a pixel value for this
* <CODE>ColorModel</CODE>.
* @throws UnsupportedOperationException If the transfer type of
* this <CODE>ComponentColorModel</CODE>
* is not one of the supported transfer types:
* <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
* <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
* <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
*/
public int getRed(Object inData) {
return getRGBComponent(inData, 0);
}
/**
* Returns the green color component for the specified pixel, scaled
* from 0 to 255 in the default RGB <CODE>ColorSpace</CODE>, sRGB.
* A color conversion is done if necessary. The <CODE>pixel</CODE> value
* is specified by an array of data elements of type <CODE>transferType</CODE>
* passed in as an object reference. The returned value is a non pre-multiplied
* value. If the alpha is premultiplied, this method divides it out before
* returning the value (if the alpha value is 0, the green value will be 0).
* Since <code>ComponentColorModel</code> can be subclassed,
* subclasses inherit the implementation of this method and if they
* don't override it then they throw an exception if they use an
* unsupported <code>transferType</code>.
*
* @param inData The pixel from which you want to get the green color component,
* specified by an array of data elements of type <CODE>transferType</CODE>.
*
* @return The green color component for the specified pixel, as an int.
*
* @throws ClassCastException If <CODE>inData</CODE> is not a primitive array
* of type <CODE>transferType</CODE>.
* @throws ArrayIndexOutOfBoundsException if <CODE>inData</CODE> is not
* large enough to hold a pixel value for this
* <CODE>ColorModel</CODE>.
* @throws UnsupportedOperationException If the transfer type of
* this <CODE>ComponentColorModel</CODE>
* is not one of the supported transfer types:
* <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
* <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
* <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
*/
public int getGreen(Object inData) {
return getRGBComponent(inData, 1);
}
/**
* Returns the blue color component for the specified pixel, scaled
* from 0 to 255 in the default RGB <CODE>ColorSpace</CODE>, sRGB.
* A color conversion is done if necessary. The <CODE>pixel</CODE> value is
* specified by an array of data elements of type <CODE>transferType</CODE>
* passed in as an object reference. The returned value is a non pre-multiplied
* value. If the alpha is premultiplied, this method divides it out before
* returning the value (if the alpha value is 0, the blue value will be 0).
* Since <code>ComponentColorModel</code> can be subclassed,
* subclasses inherit the implementation of this method and if they
* don't override it then they throw an exception if they use an
* unsupported <code>transferType</code>.
*
* @param inData The pixel from which you want to get the blue color component,
* specified by an array of data elements of type <CODE>transferType</CODE>.
*
* @return The blue color component for the specified pixel, as an int.
*