-
Notifications
You must be signed in to change notification settings - Fork 31
/
BufferedImage.java
2046 lines (1900 loc) · 69.6 KB
/
BufferedImage.java
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
/*
* Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017
* for use in SwingJS via transpilation into JavaScript using Java2Script.
*
* Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.awt.image;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.util.Hashtable;
import java.util.Vector;
import sun.awt.image.ByteComponentRaster;
import sun.awt.image.BytePackedRaster;
import sun.awt.image.IntegerComponentRaster;
import sun.awt.image.OffScreenImageSource;
import sun.awt.image.PixelConverter;
import sun.awt.image.ShortComponentRaster;
import sun.awt.image.SunWritableRaster;
import swingjs.JSGraphics2D;
import swingjs.JSGraphicsCompositor;
import swingjs.JSUtil;
import swingjs.api.JSUtilI;
import swingjs.api.js.DOMNode;
import swingjs.api.js.HTML5Canvas;
/**
* SwingJS note:
*
* Besides a Raster and ColorModel, a BufferedImage in SwingJS will also have associated
* with it one or more of:
*
* a) A JSGraphics2D object
*
* b) An HTML5 canvas element associated with that graphics object
*
* c) An ARGB int[] or [B,G,R,A] byte[] pixel array that is not necessarily in the form of the raster
* associated with this image.
*
* Thus, fast general purpose images can be created by:
*
* 1) loading an image from disk into an HTML5 canvas directly. Note that one clock tick is required for
* reading pixels from this image;
*
* 2) starting with a blank image and using createGraphics() to generate an HTML5 canvas and draw to it;
*
* 3) creating the image from scratch, with no associated graphics object, directly accessing its raster;
*
* 4) starting with an image and then from that utilizing its raster to read or write to.
*
* 5) creating the image from scratch, providing the raw raster in the constructor;
*
*
* So the states are:
*
* IMAGE_HAS_CANVAS (from 1 and 2 directly, 4 if not written to)
* IMAGE_LOCAL_RASTER (from 3 and 4)
* IMAGE_FOREIGN_RASTER (from 5)
*
* CANVAS_TAINTED
* RASTER_TAINTED
*
*
*
* The <code>BufferedImage</code> subclass describes an {@link java.awt.Image
* Image} with an accessible buffer of image data. A <code>BufferedImage</code>
* is comprised of a {@link ColorModel} and a {@link Raster} of image data. The
* number and types of bands in the {@link SampleModel} of the
* <code>Raster</code> must match the number and types required by the
* <code>ColorModel</code> to represent its color and alpha components. All
* <code>BufferedImage</code> objects have an upper left corner coordinate of
* (0, 0). Any <code>Raster</code> used to construct a
* <code>BufferedImage</code> must therefore have minX=0 and minY=0.
*
* <p>
* This class relies on the data fetching and setting methods of
* <code>Raster</code>, and on the color characterization methods of
* <code>ColorModel</code>.
*
* @see ColorModel
* @see Raster
* @see WritableRaster
*/
public class BufferedImage extends Image implements RenderedImage, Transparency // , WritableRenderedImage
{
int imageType = TYPE_CUSTOM;
ColorModel colorModel;
protected WritableRaster raster;
OffScreenImageSource osis;
@SuppressWarnings("rawtypes")
Hashtable properties;
boolean isAlphaPremultiplied;// If true, alpha has been premultiplied in
// color channels in standard r g b a format
protected int width, height;
/**
* the JSGrpahics2D object associated with this image, if it has been requested
*/
public JSGraphics2D 秘g; // a JSGraphics2D instance
/**
* if an image is used just for graphics that the HTML5 canvas can draw, we back
* the BufferedImage with an HTML5 canvas and draw to it, never using the
* raster associated with this image.
*/
public Object 秘imgNode; // used by JSGraphics2D directly
/**
* pixels associated with this image
*
* int_ARGB or int_(FF)RGB or byte_interleaved_ARGB
*
*/
protected int[] 秘pix;
/**
* set to true if pixels have been generated from an HTML5 canvas
*
*/
private boolean 秘haveFilePixels;
/**
* the HTML5 canvas that originated 秘pix or that was created from them.
*
*/
public Object 秘canvas; // created in setRGB
/**
* the Component associated with this image;
* used to set font, background, and foreground color
*/
public Component 秘component; // for context from component.createImage()
/**
* cached pixels created by setRGB(int,int,int,int,int[],int,int) and used by
* setRGB(int,int,int) and the getRGB and getRangeRGB methods for faster processing
*
* it is not clear at all that this works -- it is not being cleared ever
*
*/
private int[] 秘pixSaved;
// private static int rangeIndex;
/**
* a flag to indicate that the user provided a raw raster in the constructor,
* so we have no idea if it has been changed or not.
*
*/
private boolean 秘hasRasterData;
private int 秘wxh;
/**
* Image Type Constants
*/
/**
* Image type is not recognized so it must be a customized image. This type is
* only used as a return value for the getType() method.
*/
public static final int TYPE_CUSTOM = 0;
/**
* Represents an image with 8-bit RGB color components packed into integer
* pixels. The image has a {@link DirectColorModel} without alpha. When data
* with non-opaque alpha is stored in an image of this type, the color data must
* be adjusted to a non-premultiplied form and the alpha discarded, as described
* in the {@link java.awt.AlphaComposite} documentation.
*/
public static final int TYPE_INT_RGB = 1;
/**
* Represents an image with 8-bit RGBA color components packed into integer
* pixels. The image has a <code>DirectColorModel</code> with alpha. The color
* data in this image is considered not to be premultiplied with alpha. When
* this type is used as the <code>imageType</code> argument to a
* <code>BufferedImage</code> constructor, the created image is consistent with
* images created in the JDK1.1 and earlier releases.
*/
public static final int TYPE_INT_ARGB = 2;
/**
* Represents an image with 8-bit RGBA color components packed into integer
* pixels. The image has a <code>DirectColorModel</code> with alpha. The color
* data in this image is considered to be premultiplied with alpha.
*/
public static final int TYPE_INT_ARGB_PRE = 3;
//
/**
* Represents an image with 8-bit RGB color components, corresponding to a
* Windows- or Solaris- style BGR color model, with the colors Blue, Green, and
* Red packed into integer pixels. There is no alpha. The image has a
* {@link DirectColorModel}. When data with non-opaque alpha is stored in an
* image of this type, the color data must be adjusted to a non-premultiplied
* form and the alpha discarded, as described in the
* {@link java.awt.AlphaComposite} documentation.
*/
public static final int TYPE_INT_BGR = 4;
/**
* Represents an image with 8-bit RGB color components, corresponding to a
* Windows-style BGR color model) with the colors Blue, Green, and Red stored in
* 3 bytes. There is no alpha. The image has a <code>ComponentColorModel</code>.
* When data with non-opaque alpha is stored in an image of this type, the color
* data must be adjusted to a non-premultiplied form and the alpha discarded, as
* described in the {@link java.awt.AlphaComposite} documentation.
*/
public static final int TYPE_3BYTE_BGR = 5;
/**
* Represents an image with 8-bit RGBA color components with the colors Blue,
* Green, and Red stored in 3 bytes and 1 byte of alpha. The image has a
* <code>ComponentColorModel</code> with alpha. The color data in this image is
* considered not to be premultiplied with alpha. The byte data is interleaved
* in a single byte array in the order B, G, R, A from lower to higher byte
* addresses within each pixel.
*/
public static final int TYPE_4BYTE_HTML5 = JSUtilI.TYPE_4BYTE_HTML5;
/**
* Represents an image with 8-bit RGBA color components with the colors Blue,
* Green, and Red stored in 3 bytes and 1 byte of alpha. The image has a
* <code>ComponentColorModel</code> with alpha. The color data in this image is
* considered not to be premultiplied with alpha. The byte data is interleaved
* in a single byte array in the order A, B, G, R from lower to higher byte
* addresses within each pixel.
*/
public static final int TYPE_4BYTE_ABGR = 6;
/**
* Represents an image with 8-bit RGBA color components with the colors Blue,
* Green, and Red stored in 3 bytes and 1 byte of alpha. The image has a
* <code>ComponentColorModel</code> with alpha. The color data in this image is
* considered to be premultiplied with alpha. The byte data is interleaved in a
* single byte array in the order A, B, G, R from lower to higher byte addresses
* within each pixel.
*/
public static final int TYPE_4BYTE_ABGR_PRE = 7;
/**
* Represents an image with 5-6-5 RGB color components (5-bits red, 6-bits
* green, 5-bits blue) with no alpha. This image has a
* <code>DirectColorModel</code>. When data with non-opaque alpha is stored in
* an image of this type, the color data must be adjusted to a non-premultiplied
* form and the alpha discarded, as described in the
* {@link java.awt.AlphaComposite} documentation.
*/
public static final int TYPE_USHORT_565_RGB = 8;
/**
* Represents an image with 5-5-5 RGB color components (5-bits red, 5-bits
* green, 5-bits blue) with no alpha. This image has a
* <code>DirectColorModel</code>. When data with non-opaque alpha is stored in
* an image of this type, the color data must be adjusted to a non-premultiplied
* form and the alpha discarded, as described in the
* {@link java.awt.AlphaComposite} documentation.
*/
public static final int TYPE_USHORT_555_RGB = 9;
/**
* Represents a unsigned byte grayscale image, non-indexed. This image has a
* <code>ComponentColorModel</code> with a CS_GRAY {@link ColorSpace}. When data
* with non-opaque alpha is stored in an image of this type, the color data must
* be adjusted to a non-premultiplied form and the alpha discarded, as described
* in the {@link java.awt.AlphaComposite} documentation.
*/
public static final int TYPE_BYTE_GRAY = 10;
/**
* Represents an unsigned short grayscale image, non-indexed). This image has a
* <code>ComponentColorModel</code> with a CS_GRAY <code>ColorSpace</code>. When
* data with non-opaque alpha is stored in an image of this type, the color data
* must be adjusted to a non-premultiplied form and the alpha discarded, as
* described in the {@link java.awt.AlphaComposite} documentation.
*/
public static final int TYPE_USHORT_GRAY = 11;
/**
* Represents an opaque byte-packed 1, 2, or 4 bit image. The image has an
* {@link IndexColorModel} without alpha. When this type is used as the
* <code>imageType</code> argument to the <code>BufferedImage</code> constructor
* that takes an <code>imageType</code> argument but no <code>ColorModel</code>
* argument, a 1-bit image is created with an <code>IndexColorModel</code> with
* two colors in the default sRGB <code>ColorSpace</code>: {0, 0, 0}
* and {255, 255, 255}.
*
* <p>
* Images with 2 or 4 bits per pixel may be constructed via the
* <code>BufferedImage</code> constructor that takes a <code>ColorModel</code>
* argument by supplying a <code>ColorModel</code> with an appropriate map size.
*
* <p>
* Images with 8 bits per pixel should use the image types
* <code>TYPE_BYTE_INDEXED</code> or <code>TYPE_BYTE_GRAY</code> depending on
* their <code>ColorModel</code>.
*
* <p>
* When color data is stored in an image of this type, the closest color in the
* colormap is determined by the <code>IndexColorModel</code> and the resulting
* index is stored. Approximation and loss of alpha or color components can
* result, depending on the colors in the <code>IndexColorModel</code> colormap.
*/
public static final int TYPE_BYTE_BINARY = 12;
/**
* Represents an indexed byte image. When this type is used as the
* <code>imageType</code> argument to the <code>BufferedImage</code> constructor
* that takes an <code>imageType</code> argument but no <code>ColorModel</code>
* argument, an <code>IndexColorModel</code> is created with a 256-color 6/6/6
* color cube palette with the rest of the colors from 216-255 populated by
* grayscale values in the default sRGB ColorSpace.
*
* <p>
* When color data is stored in an image of this type, the closest color in the
* colormap is determined by the <code>IndexColorModel</code> and the resulting
* index is stored. Approximation and loss of alpha or color components can
* result, depending on the colors in the <code>IndexColorModel</code> colormap.
*/
public static final int TYPE_BYTE_INDEXED = 13;
private static final int DCM_RED_MASK = 0x00ff0000;
private static final int DCM_GREEN_MASK = 0x0000ff00;
private static final int DCM_BLUE_MASK = 0x000000ff;
private static final int DCM_ALPHA_MASK = 0xff000000;
private static final int DCM_565_RED_MASK = 0xf800;
private static final int DCM_565_GRN_MASK = 0x07E0;
private static final int DCM_565_BLU_MASK = 0x001F;
private static final int DCM_555_RED_MASK = 0x7C00;
private static final int DCM_555_GRN_MASK = 0x03E0;
private static final int DCM_555_BLU_MASK = 0x001F;
private static final int DCM_BGR_RED_MASK = 0x0000ff;
private static final int DCM_BGR_GRN_MASK = 0x00ff00;
private static final int DCM_BGR_BLU_MASK = 0xff0000;
/**
* Constructs a <code>BufferedImage</code> of one of the predefined image types.
* The <code>ColorSpace</code> for the image is the default sRGB space.
*
* @param width width of the created image
* @param height height of the created image
* @param imageType type of the created image
* @see ColorSpace
* @see #TYPE_INT_RGB
* @see #TYPE_INT_ARGB
* @see #TYPE_INT_ARGB_PRE // SwingJS not implemented
* @see #TYPE_INT_BGR
* @see #TYPE_3BYTE_BGR
* @see #TYPE_4BYTE_ABGR
* @see #TYPE_4BYTE_ABGR_PRE
* @see #TYPE_BYTE_GRAY
* @see #TYPE_USHORT_GRAY
* @see #TYPE_BYTE_BINARY
* @see #TYPE_BYTE_INDEXED
* @see #TYPE_USHORT_565_RGB
* @see #TYPE_USHORT_555_RGB
*/
public BufferedImage(int width, int height, int imageType) {
秘init(width, height, imageType);
}
protected void 秘init(int width, int height, int imageType) {
this.width = width;
this.height = height;
秘wxh = width * height;
switch (imageType) {
case TYPE_INT_RGB:
colorModel = new DirectColorModel(24, 0x00ff0000, // Red
0x0000ff00, // Green
0x000000ff, // Blue
0x0 // Alpha
);
raster = colorModel.createCompatibleWritableRaster(width, height);
raster.setImage(this);
秘pix = ((DataBufferInt) raster.getDataBuffer()).data;
break;
case TYPE_INT_ARGB:
colorModel = ColorModel.getRGBdefault();
raster = colorModel.createCompatibleWritableRaster(width, height);
raster.setImage(this);
秘pix = ((DataBufferInt) raster.getDataBuffer()).data;
break;
case TYPE_INT_ARGB_PRE:
colorModel = new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), 32, 0x00ff0000, // Red
0x0000ff00, // Green
0x000000ff, // Blue
0xff000000, // Alpha
true, // Alpha Premultiplied
DataBuffer.TYPE_INT);
raster = colorModel.createCompatibleWritableRaster(width, height);
break;
case TYPE_INT_BGR:
colorModel = new DirectColorModel(24, 0x000000ff, // Red
0x0000ff00, // Green
0x00ff0000 // Blue
);
raster = colorModel.createCompatibleWritableRaster(width, height);
break;
case TYPE_3BYTE_BGR: {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = { 8, 8, 8 };
int[] bOffs = { 2, 1, 0 };
colorModel = new ComponentColorModel(cs, nBits, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, width * 3, 3, bOffs, null);
}
break;
case TYPE_4BYTE_ABGR: {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = { 8, 8, 8, 8 };
int[] bOffs = { 3, 2, 1, 0 };
colorModel = new ComponentColorModel(cs, nBits, true, false, Transparency.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, width * 4, 4, bOffs, null);
}
break;
case TYPE_4BYTE_HTML5: { // [r g b a...]
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = { 8, 8, 8, 8 };
int[] bOffs = { 0, 1, 2, 3 };
colorModel = new ComponentColorModel(cs, nBits, true, false, Transparency.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, width * 4, 4, bOffs, null);
秘pix = ((DataBufferInt) raster.getDataBuffer()).data;
秘haveFilePixels = 秘hasRasterData = true;
}
break;
case TYPE_4BYTE_ABGR_PRE: {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = { 8, 8, 8, 8 };
int[] bOffs = { 3, 2, 1, 0 };
colorModel = new ComponentColorModel(cs, nBits, true, true, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, width * 4, 4, bOffs, null);
}
break;
case TYPE_BYTE_GRAY: {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
int[] nBits = { 8 };
colorModel = new ComponentColorModel(cs, nBits, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
raster = colorModel.createCompatibleWritableRaster(width, height);
}
break;
case TYPE_USHORT_GRAY: {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
int[] nBits = { 16 };
colorModel = new ComponentColorModel(cs, nBits, false, true, Transparency.OPAQUE, DataBuffer.TYPE_USHORT);
raster = colorModel.createCompatibleWritableRaster(width, height);
}
break;
case TYPE_BYTE_BINARY: // B&W
byte[] arr = { (byte) 0, (byte) 0xff };
colorModel = new IndexColorModel(1, 2, arr, arr, arr);
raster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, width, height, 1, 1, null);
break;
case TYPE_BYTE_INDEXED:
// Create a 6x6x6 color cube
int[] cmap = new int[256];
int i = 0;
for (int r = 0; r < 256; r += 51) {
for (int g = 0; g < 256; g += 51) {
for (int b = 0; b < 256; b += 51) {
cmap[i++] = (r << 16) | (g << 8) | b;
}
}
}
// And populate the rest of the cmap with gray values
int grayIncr = 256 / (256 - i);
// The gray ramp will be between 18 and 252
int gray = grayIncr * 3;
for (; i < 256; i++) {
cmap[i] = (gray << 16) | (gray << 8) | gray;
gray += grayIncr;
}
colorModel = new IndexColorModel(8, 256, cmap, 0, false, -1, DataBuffer.TYPE_BYTE);
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, 1, null);
break;
case TYPE_USHORT_565_RGB: {
colorModel = new DirectColorModel(16, DCM_565_RED_MASK, DCM_565_GRN_MASK, DCM_565_BLU_MASK);
raster = colorModel.createCompatibleWritableRaster(width, height);
}
break;
case TYPE_USHORT_555_RGB: {
colorModel = new DirectColorModel(15, DCM_555_RED_MASK, DCM_555_GRN_MASK, DCM_555_BLU_MASK);
raster = colorModel.createCompatibleWritableRaster(width, height);
}
break;
default:
throw new IllegalArgumentException("Unknown image type " + imageType);
}
this.imageType = imageType;
}
/**
* Constructs a <code>BufferedImage</code> of one of the predefined image types:
* TYPE_BYTE_BINARY or TYPE_BYTE_INDEXED.
*
* <p>
* If the image type is TYPE_BYTE_BINARY, the number of entries in the color
* model is used to determine whether the image should have 1, 2, or 4 bits per
* pixel. If the color model has 1 or 2 entries, the image will have 1 bit per
* pixel. If it has 3 or 4 entries, the image with have 2 bits per pixel. If it
* has between 5 and 16 entries, the image will have 4 bits per pixel.
* Otherwise, an IllegalArgumentException will be thrown.
*
* @param width width of the created image
* @param height height of the created image
* @param imageType type of the created image
* @param cm <code>IndexColorModel</code> of the created image
* @throws IllegalArgumentException if the imageType is not TYPE_BYTE_BINARY or
* TYPE_BYTE_INDEXED or if the imageType is
* TYPE_BYTE_BINARY and the color map has more
* than 16 entries.
* @see #TYPE_BYTE_BINARY
* @see #TYPE_BYTE_INDEXED
*/
public BufferedImage(int width, int height, int imageType, IndexColorModel cm) {
if (cm.hasAlpha() && cm.isAlphaPremultiplied()) {
throw new IllegalArgumentException("This image types do not have " + "premultiplied alpha.");
}
this.width = width;
this.height = height;
秘wxh = width * height;
switch (imageType) {
case TYPE_BYTE_BINARY:
int bits; // Will be set below
int mapSize = cm.getMapSize();
if (mapSize <= 2) {
bits = 1;
} else if (mapSize <= 4) {
bits = 2;
} else if (mapSize <= 16) {
bits = 4;
} else {
throw new IllegalArgumentException(
"Color map for TYPE_BYTE_BINARY " + "must have no more than 16 entries");
}
raster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, width, height, 1, bits, null);
break;
case TYPE_BYTE_INDEXED:
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, 1, null);
break;
default:
throw new IllegalArgumentException("Invalid image type (" + imageType + "). Image type must"
+ " be either TYPE_BYTE_BINARY or " + " TYPE_BYTE_INDEXED");
}
if (!cm.isCompatibleRaster(raster)) {
throw new IllegalArgumentException("Incompatible image type and IndexColorModel");
}
colorModel = cm;
this.imageType = imageType;
}
/**
* Constructs a new <code>BufferedImage</code> with a specified
* <code>ColorModel</code> and <code>Raster</code>. If the number and types of
* bands in the <code>SampleModel</code> of the <code>Raster</code> do not match
* the number and types required by the <code>ColorModel</code> to represent its
* color and alpha components, a {@link RasterFormatException} is thrown. This
* method can multiply or divide the color <code>Raster</code> data by alpha to
* match the <code>alphaPremultiplied</code> state in the
* <code>ColorModel</code>. Properties for this <code>BufferedImage</code> can
* be established by passing in a {@link Hashtable} of <code>String</code>/
* <code>Object</code> pairs.
*
* @param cm <code>ColorModel</code> for the new image
* @param raster <code>Raster</code> for the image data
* @param isRasterPremultiplied if <code>true</code>, the data in the raster has
* been premultiplied with alpha.
* @param properties <code>Hashtable</code> of
* <code>String</code>/<code>Object</code> pairs.
* @exception <code>RasterFormatException</code> if the number and types of
* bands in the <code>SampleModel</code> of the <code>Raster</code>
* do not match the number and types required by the
* <code>ColorModel</code> to represent its color and alpha
* components.
* @exception <code>IllegalArgumentException</code> if <code>raster</code> is
* incompatible with <code>cm</code>
* @see ColorModel
* @see Raster
* @see WritableRaster
*/
/*
*
* FOR NOW THE CODE WHICH DEFINES THE RASTER TYPE IS DUPLICATED BY DVF SEE THE
* METHOD DEFINERASTERTYPE @ RASTEROUTPUTMANAGER
*/
public BufferedImage(ColorModel cm, WritableRaster raster, boolean isRasterPremultiplied,
Hashtable<?, ?> properties) {
if (!cm.isCompatibleRaster(raster)) {
throw new IllegalArgumentException("Raster " + raster + " is incompatible with ColorModel " + cm);
}
if ((raster.minX != 0) || (raster.minY != 0)) {
throw new IllegalArgumentException(
"Raster " + raster + " has minX or minY not equal to zero: " + raster.minX + " " + raster.minY);
}
colorModel = cm;
this.raster = raster;
this.width = raster.getWidth();
this.height = raster.getHeight();
秘wxh = width * height;
raster.setImage(this);
if (getColorModel() == ColorModel.秘RGBdefault)
秘pix = ((DataBufferInt) raster.getDataBuffer()).data;
else
秘hasRasterData = true;
this.properties = properties;
int numBands = raster.getNumBands();
boolean isAlphaPre = cm.isAlphaPremultiplied();
ColorSpace cs;
// Force the raster data alpha state to match the premultiplied
// state in the color model
// coerceData(isRasterPremultiplied);
SampleModel sm = raster.getSampleModel();
cs = cm.getColorSpace();
int csType = cs.getType();
if (csType != ColorSpace.TYPE_RGB) {
if (csType == ColorSpace.TYPE_GRAY && ComponentColorModel.class.equals(cm.getClass())) {
// Check if this might be a child raster (fix for bug 4240596)
if (sm instanceof ComponentSampleModel && ((ComponentSampleModel) sm).getPixelStride() != numBands) {
imageType = TYPE_CUSTOM;
} else if (raster instanceof ByteComponentRaster
&& PixelInterleavedSampleModel.class.equals(sm.getClass()) && raster.getNumBands() == 1
&& cm.getComponentSize(0) == 8 && ((ByteComponentRaster) raster).getPixelStride() == 1) {
imageType = TYPE_BYTE_GRAY;
} else if (raster instanceof ShortComponentRaster
&& PixelInterleavedSampleModel.class.equals(sm.getClass()) && raster.getNumBands() == 1
&& cm.getComponentSize(0) == 16 && ((ShortComponentRaster) raster).getPixelStride() == 1) {
imageType = TYPE_USHORT_GRAY;
}
} else {
imageType = TYPE_CUSTOM;
}
return;
}
if ((raster instanceof IntegerComponentRaster) && (numBands == 3 || numBands == 4)) {
IntegerComponentRaster iraster = (IntegerComponentRaster) raster;
// Check if the raster params and the color model
// are correct
int pixSize = cm.getPixelSize();
if (iraster.getPixelStride() == 1 && DirectColorModel.class.equals(cm.getClass())
&& SinglePixelPackedSampleModel.class.equals(sm.getClass()) && (pixSize == 32 || pixSize == 24)) {
// Now check on the DirectColorModel params
DirectColorModel dcm = (DirectColorModel) cm;
int rmask = dcm.getRedMask();
int gmask = dcm.getGreenMask();
int bmask = dcm.getBlueMask();
if (rmask == DCM_RED_MASK && gmask == DCM_GREEN_MASK && bmask == DCM_BLUE_MASK) {
if (dcm.getAlphaMask() == DCM_ALPHA_MASK) {
imageType = (isAlphaPre ? TYPE_INT_ARGB_PRE : TYPE_INT_ARGB);
} else {
// No Alpha
if (!dcm.hasAlpha()) {
imageType = TYPE_INT_RGB;
}
}
} // if (dcm.getRedMask() == DCM_RED_MASK &&
else if (rmask == DCM_BGR_RED_MASK && gmask == DCM_BGR_GRN_MASK && bmask == DCM_BGR_BLU_MASK) {
if (!dcm.hasAlpha()) {
imageType = TYPE_INT_BGR;
}
} // if (rmask == DCM_BGR_RED_MASK &&
} // if (iraster.getPixelStride() == 1
} // ((raster instanceof IntegerComponentRaster) &&
else if ((IndexColorModel.class.equals(cm.getClass())) && (numBands == 1) && (!cm.hasAlpha() || !isAlphaPre)) {
IndexColorModel icm = (IndexColorModel) cm;
int pixSize = icm.getPixelSize();
if (raster instanceof BytePackedRaster && MultiPixelPackedSampleModel.class.equals(sm.getClass())) {
imageType = TYPE_BYTE_BINARY;
} // if (raster instanceof BytePackedRaster)
else if (raster instanceof ByteComponentRaster && PixelInterleavedSampleModel.class.equals(sm.getClass())) {
ByteComponentRaster braster = (ByteComponentRaster) raster;
if (braster.getPixelStride() == 1 && pixSize <= 8) {
imageType = TYPE_BYTE_INDEXED;
}
}
} // else if (cm instanceof IndexColorModel) && (numBands == 1))
else if ((raster instanceof ShortComponentRaster) && (DirectColorModel.class.equals(cm.getClass()))
&& (SinglePixelPackedSampleModel.class.equals(sm.getClass())) && (numBands == 3) && !cm.hasAlpha()) {
DirectColorModel dcm = (DirectColorModel) cm;
if (dcm.getRedMask() == DCM_565_RED_MASK) {
if (dcm.getGreenMask() == DCM_565_GRN_MASK && dcm.getBlueMask() == DCM_565_BLU_MASK) {
imageType = TYPE_USHORT_565_RGB;
}
} else if (dcm.getRedMask() == DCM_555_RED_MASK) {
if (dcm.getGreenMask() == DCM_555_GRN_MASK && dcm.getBlueMask() == DCM_555_BLU_MASK) {
imageType = TYPE_USHORT_555_RGB;
}
}
} // else if ((cm instanceof IndexColorModel) && (numBands == 1))
else if ((raster instanceof ByteComponentRaster) && (cm instanceof ComponentColorModel)
&& (raster.getSampleModel() instanceof PixelInterleavedSampleModel)
&& (numBands == 3 || numBands == 4)) {
ComponentColorModel ccm = (ComponentColorModel) cm;
PixelInterleavedSampleModel csm = (PixelInterleavedSampleModel) raster.getSampleModel();
ByteComponentRaster braster = (ByteComponentRaster) raster;
int[] offs = csm.getBandOffsets();
if (ccm.getNumComponents() != numBands) {
throw new RasterFormatException("Number of components in " + "ColorModel (" + ccm.getNumComponents()
+ ") does not match # in " + " Raster (" + numBands + ")");
}
int[] nBits = ccm.getComponentSize();
boolean is8bit = true;
for (int i = 0; i < numBands; i++) {
if (nBits[i] != 8) {
is8bit = false;
break;
}
}
if (is8bit && braster.getPixelStride() == numBands && offs[0] == numBands - 1 && offs[1] == numBands - 2
&& offs[2] == numBands - 3 && ComponentColorModel.class.equals(ccm.getClass())
&& PixelInterleavedSampleModel.class.equals(csm.getClass())) {
if (numBands == 3 && !ccm.hasAlpha()) {
imageType = TYPE_3BYTE_BGR;
} else if (offs[3] == 0 && ccm.hasAlpha()) {
imageType = (isAlphaPre ? TYPE_4BYTE_ABGR_PRE : TYPE_4BYTE_ABGR);
}
}
} // else if ((raster instanceof ByteComponentRaster) &&
}
/**
* Returns the image type. If it is not one of the known types, TYPE_CUSTOM is
* returned.
*
* @return the image type of this <code>BufferedImage</code>.
* @see #TYPE_INT_RGB
* @see #TYPE_INT_ARGB
* @see #TYPE_INT_ARGB_PRE
* @see #TYPE_INT_BGR
* @see #TYPE_3BYTE_BGR
* @see #TYPE_4BYTE_ABGR
* @see #TYPE_4BYTE_ABGR_PRE
* @see #TYPE_BYTE_GRAY
* @see #TYPE_BYTE_BINARY
* @see #TYPE_BYTE_INDEXED
* @see #TYPE_USHORT_GRAY
* @see #TYPE_USHORT_565_RGB
* @see #TYPE_USHORT_555_RGB
* @see #TYPE_CUSTOM
*/
public int getType() {
return imageType;
}
/**
* Returns the <code>ColorModel</code>.
*
* @return the <code>ColorModel</code> of this <code>BufferedImage</code>.
*/
@Override
public ColorModel getColorModel() {
// one of Component, Index, or Packed(Direct)
return colorModel;
}
/**
* Returns the {@link WritableRaster}.
*
* @return the <code>WriteableRaster</code> of this <code>BufferedImage</code> .
*/
public WritableRaster getRaster() {
秘hasRasterData = true;
// NO!!! checkHavePixels();
return raster;
}
/**
* Returns a <code>WritableRaster</code> representing the alpha channel for
* <code>BufferedImage</code> objects with <code>ColorModel</code> objects that
* support a separate spatial alpha channel, such as
* <code>ComponentColorModel</code> and <code>DirectColorModel</code>. Returns
* <code>null</code> if there is no alpha channel associated with the
* <code>ColorModel</code> in this image. This method assumes that for all
* <code>ColorModel</code> objects other than <code>IndexColorModel</code>, if
* the <code>ColorModel</code> supports alpha, there is a separate alpha channel
* which is stored as the last band of image data. If the image uses an
* <code>IndexColorModel</code> that has alpha in the lookup table, this method
* returns <code>null</code> since there is no spatially discrete alpha channel.
* This method creates a new <code>WritableRaster</code>, but shares the data
* array.
*
* @return a <code>WritableRaster</code> or <code>null</code> if this
* <code>BufferedImage</code> has no alpha channel associated with its
* <code>ColorModel</code>.
*/
public WritableRaster getAlphaRaster() {
return colorModel.getAlphaRaster(raster);
}
/**
* Returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and
* default sRGB colorspace. Color conversion takes place if this default model
* does not match the image <code>ColorModel</code>. There are only 8-bits of
* precision for each color component in the returned data when using this
* method.
*
* <p>
*
* An <code>ArrayOutOfBoundsException</code> may be thrown if the coordinates
* are not in bounds. However, explicit bounds checking is not guaranteed.
*
* @param x the X coordinate of the pixel from which to get the pixel in the
* default RGB color model and sRGB color space
* @param y the Y coordinate of the pixel from which to get the pixel in the
* default RGB color model and sRGB color space
* @return an integer pixel in the default RGB color model and default sRGB
* colorspace.
* @see #setRGB(int, int, int)
* @see #setRGB(int, int, int, int, int[], int, int)
*/
public int getRGB(int x, int y) {
秘ensureHavePixels(true);
if (秘pix == null)
秘pix = 秘pixSaved;
return 秘pix[y * this.width + x];
}
/**
* Returns an array of integer pixels in the default RGB color model
* (TYPE_INT_ARGB) and default sRGB color space, from a portion of the image
* data. Color conversion takes place if the default model does not match the
* image <code>ColorModel</code>. There are only 8-bits of precision for each
* color component in the returned data when using this method. With a specified
* coordinate (x, y) in the image, the ARGB pixel can be accessed in this
* way:
* </p>
*
* <pre>
* pixel = rgbArray[offset + (y - startY) * scansize + (x - startX)];
* </pre>
*
* <p>
*
* An <code>ArrayOutOfBoundsException</code> may be thrown if the region is not
* in bounds. However, explicit bounds checking is not guaranteed.
*
* @param startX the starting X coordinate
* @param startY the starting Y coordinate
* @param w width of region
* @param h height of region
* @param rgbArray if not <code>null</code>, the rgb pixels are written here
* @param offset offset into the <code>rgbArray</code>
* @param scansize scanline stride for the <code>rgbArray</code>
* @return array of RGB pixels.
* @see #setRGB(int, int, int)
* @see #setRGB(int, int, int, int, int[], int, int)
*/
public int[] getRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) {
秘ensureHavePixels(true);
if (秘pix == null)
秘pix = 秘pixSaved;
return getRangeRGB(startX, startY, w, h, rgbArray, offset, scansize);
}
/**
* Ensure that we do have pixels, either because they were there already, or by
* generating them from the associated HTML5 canvas.
*
* @param andSetImageNode to set 秘imgNode to the graphics canvas.
*
* @return true if pixels had to be set
*/
public boolean 秘ensureHavePixels(boolean andSetImageNode) {
if (!秘haveFilePixels && (秘imgNode != null || 秘g != null)) {
秘setPixelsFromHTML5Canavas(andSetImageNode);
return true;
}
if (秘hasRasterData) {
秘getPixelsFromRaster();
return true;
}
return false;
}
/**
* Read a range of pixels as int_ARGB[].
*
* @param startX
* @param startY
* @param w
* @param h
* @param rgbArray
* @param offset
* @param scansize
* @return
*/
public int[] getRangeRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) {
if (秘pix == null && 秘pixSaved == null)
秘ensureHavePixels(false);
int[] pixels = (秘pix == null ? 秘pixSaved : 秘pix);
if (pixels == null)
return rgbArray;
if (pixels.length == 秘wxh) {
for (int y = startY, yoff = offset; y < startY + h; y++, yoff += scansize)
for (int off = yoff, x = startX; x < startX + w; x++)
rgbArray[off++] = pixels[y * width + x];
} else {
// already in HTML5 format
for (int y = startY, yoff = offset; y < startY + h; y++, yoff += scansize) {
for (int off = yoff, x = startX, pt = (y * width)<<2 ; x < startX + w; x++) {
// r g b a to argb
rgbArray[off++] = (pixels[pt++] << 16) | (pixels[pt++] << 8) | pixels[pt++] | (pixels[pt++] << 24);
}
}
}
return rgbArray;
}
/**
* Sets a pixel in this <code>BufferedImage</code> to the specified RGB value.
* The pixel is assumed to be in the default RGB color model, TYPE_INT_ARGB, and
* default sRGB color space. For images with an <code>IndexColorModel</code>,
* the index with the nearest color is chosen.
*
* <p>
*
* An <code>ArrayOutOfBoundsException</code> may be thrown if the coordinates
* are not in bounds. However, explicit bounds checking is not guaranteed.
*
* @param x the X coordinate of the pixel to set
* @param y the Y coordinate of the pixel to set
* @param rgb the RGB value
* @see #getRGB(int, int)
* @see #getRGB(int, int, int, int, int[], int, int)
*/
public synchronized void setRGB(int x, int y, int rgb) {
if (秘ensureHavePixels(false));
int[] pixels = (秘pix == null ? 秘pixSaved : 秘pix);
pixels[y * this.width + x] = rgb;
}
/**
*
* Sets an array of integer pixels in the default RGB color model
* (TYPE_INT_ARGB) and default sRGB color space, into a portion of the image
* data. Color conversion takes place if the default model does not match the
* image <code>ColorModel</code>. There are only 8-bits of precision for each
* color component in the returned data when using this method. With a specified
* coordinate (x, y) in the this image, the ARGB pixel can be accessed in
* this way:
*
* <pre>
* pixel = rgbArray[offset + (y - startY) * scansize + (x - startX)];
* </pre>
*
* WARNING: No dithering takes place.
*
* <p>