-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitmapImage.java
More file actions
2888 lines (2701 loc) · 121 KB
/
BitmapImage.java
File metadata and controls
2888 lines (2701 loc) · 121 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
/*
* @(#)BitmapImage.java 1.5 2011-01-05
*
* Copyright (c) 2004-2011 Werner Randelshofer, Immensee, Switzerland.
* All rights reserved.
*
* You may not use, copy or modify this file, except in compliance with the
* license agreement you entered into with Werner Randelshofer.
* For details see accompanying license terms.
*/
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBufferByte;
import java.awt.image.IndexColorModel;
import java.awt.image.DirectColorModel;
import java.awt.image.Raster;
import java.util.zip.Adler32;
import javax.swing.JFrame;
/**
* A BitmapImage is comprised of a ColorModel and an accessible byte array of
* image data.
* <p>
* The image data is expressed in several layers of rectangular regions
* called bit-planes. To determine the bits that form a single pixel one
* must combine all data-bits at the same x,y position in each bit-plane.
* This is known as a "planar" storage layout as it was used on Commodore
* Amiga Computers.
* <p>
* The bit-planes can be stored contiguously or can be interleaved at each
* scanline of the image.
* <p>
* <p>
* Fig 1. A sample image:
* <p><pre>
* .+++..@...@.+..###...+++. This sample uses 4 colors:
* +...+.@@.@@.+.#.....+...+ . = color 0 (all bits clear)
* +++++:@.@.@.+.#..##.+++++ + = color 1 (bit 0 set, bit 1 clear)
* +...+.@...@.+.#...#.+...+ @ = color 2 (bit 0 clear, bit 1 set)
* +...+.@...@.+..####.+...+ # = color 3 (all bits set)
* </pre><p>
* Fig 2. Contiguous bit-plane storage layout.
* <p><pre>
* 01110000 00001001 11000111 0....... This is the first bit-plane.
* 10001000 00001010 00001000 1....... Each number represents a bit
* 11111000 00001010 01101111 1....... in the storage layout. Eight
* 10001000 00001010 00101000 1....... bits are grouped into one byte.
* 10001000 00001001 11101000 1....... Dots indicate unused bits.
* <p>
* 00000010 00100001 11000000 0....... This is the second bit-plane.
* 00000011 01100010 00000000 0.......
* 00000010 10100010 01100000 0.......
* 00000010 00100010 00100000 0.......
* 00000010 00100001 11100000 0.......
* <p></pre>
* Fig 3. Interleaved bit-plane storage layout.
* <p><pre>
* 01110000 00001001 11000111 0....... This is the first bit-plane.
* 00000010 00100001 11000000 0....... This is the second bit-plane.
* <p>
* 10001000 00001010 00001000 1....... The bit-planes are interleaved
* 00000011 01100010 00000000 0....... at every scanline of the image.
* <p>
* 11111000 00001010 01101111 1.......
* 00000010 10100010 01100000 0.......
* <p>
* 10001000 00001010 00101000 1.......
* 00000010 00100010 00100000 0.......
* <p>
* 10001000 00001001 11101000 1.......
* 00000010 00100001 11100000 0.......
* <p></pre>
* For more details refer to "Amiga ROM Kernel Reference Manual: Libraries,
* Addison Wesley"
* <p>
* <b>Responsibility</b>
* <p>
* Gives clients direct access to the image data of the bitmap.
* Knows how to convert the bitmap into chunky image data according
* to the current color model.
* Supports indexed color model, direct color model, 6 and 8 bit HAM color model.
*
* @author Werner Randelshofer, Hausmatt 10, CH-6405 Immensee, Switzerland
* @version 1.5 2011-01-05 Adds support for RGB555.
* <br>1.4 2011-01-03 Adds method setIntPixels().
* <br>1.3 2010-10-25 Removed suffixes in instance variable names.
* <br>1.2.1 2005-07-16 Setting a preferredColorModel is now better
* honoured.
* <br>1.2 2004-05-26 Improved performance of planar to chunky conversion
* routines.
* <br>1.1.1 2004-05-18 Fixed a bug, which caused an image to be all
* transparent, when it was of bitmap type indexed color, and when the desired
* bitmap type was true color, and the bitmap had a transparent color.
* <br>1.1 2003-04-01 BitmapImage can now convert bitmaps with IndexColorModel's
* into chunky pixels with DirectColorModel.
* <br>1.0 1999-10-19
*/
public class BitmapImage
implements Cloneable {
/** The bitmap data array. */
private byte[] bitmap;
/** The width of the image. */
private int width;
/** The height of the image. */
private int height;
/** The number of bits that form a single pixel. */
private int depth;
/** BitmapStride is the number of data array elements
* between two bits of the same image pixel. */
private int bitplaneStride;
/** ScanlineStride is the number of data array elements
* between a given pixel and the pixel in the same column of
* the next scanline. */
private int scanlineStride;
/** This ColorModel is used for the next conversion from planar
* bitmap data into chunky pixel data.
*/
private ColorModel planarColorModel;
/** This ColorModel represents the preferred color model for chunky pixel.
* If this value is null, then convertToChunky uses the planarColorModel_.
*/
private ColorModel preferredChunkyColorModel_;
/** This ColorModel represents the current color model for chunky pixel.
*/
private ColorModel currentChunkyColorModel_;
/** This ColorModel was used at the previous conversion from
* planar bitmap into chunky pixel data.
*/
private ColorModel lastPixelColorModel_;
/** Indicates availability of chunky pixel data. */
private int pixelType;
/** Tag for byte pixel data. */
public final static int BYTE_PIXEL = 1;
/** Tag for integer pixel data. */
public final static int INT_PIXEL = 2;
/** Tag for short pixel data. */
public final static int SHORT_PIXEL = 2;
/** Tag indicating that no pixel data is available. */
public final static int NO_PIXEL = 0;
/** Output array for byte pixel data. */
private byte[] bytePixels;
/** Output array for integer pixel data. */
private int[] intPixels;
/** Output array for short pixel data. */
private short[] shortPixels;
/**
* If this boolean is set to true, then convertToChunky always generates
* chunky pixels using a DirectColorModel.
*/
private boolean enforceDirectColors_ = false;
/**
* If you set this to true, then convertToChunky always generates
* chunky pixels using a DirectColorModel.
*/
public void setEnforceDirectColors(boolean b) {
enforceDirectColors_ = b;
}
/**
* If this returns true, then convertToChunky always generates
* chunky pixels using a DirectColorModel.
*/
public boolean isEnforceDirectColors() {
return enforceDirectColors_;
}
/**
* Construct an interleaved bitmap with the specified size,
* depth and color model.
* BitplaneStride and ScanlineStride are rounded up to the next
* even number of bytes.
* <p>
* Pre condition:
* -
* <p>
* Post condition:
* Interleaved bitmap constructed.
* <p>
* Obligation:
* -
*
* @param width Width in pixels.
* @param height Height in pixels.
* @param depth Number of bits per pixel.
* @param colorModel Color model to be used for conversions from/to chunky pixels.
*/
public BitmapImage(int width, int height, int depth, ColorModel colorModel) {
this(width, height, depth, colorModel, true);
}
/**
* Construct a bitmap with the specified size, depth and color model
* and with optional interleave.
* BitplaneStride and ScanlineStride are rounded up to the next
* even number of bytes.
* <p>
* Pre condition:
* -
* <p>
* Post condition:
* BitmapImage constructed.
* <p>
* Obligation:
* -
*
* @param width Width in pixels.
* @param height Height in pixels.
* @param depth Number of bits per pixel.
* @param colorModel Color model to be used for conversions from/to chunky pixels.
* @param isInterleaved Indicator for contiguous or interleaved bit-planes.
*/
public BitmapImage(int width, int height, int depth, ColorModel colorModel, boolean isInterleaved) {
this.width = width;
this.height = height;
this.depth = depth;
this.planarColorModel = colorModel;
if (isInterleaved) {
bitplaneStride = (width + 15) / 16 * 2;
scanlineStride = bitplaneStride * depth;
bitmap = new byte[scanlineStride * height];
} else {
scanlineStride = (width + 15) / 16 * 2;
bitplaneStride = scanlineStride * depth;
bitmap = new byte[bitplaneStride * height];
}
pixelType = NO_PIXEL;
}
/**
* Construct a bitmap with the specified size, depth, color model and
* interleave.
* <p>
* Pre condition:
* ScanlineStride must be a multiple of BitplaneStride or vice versa.
* <p>
* Post condition:
* BitmapImage constructed.
* <p>
* Obligation:
* -
*
* @param width Width in pixels.
* @param height Height in pixels.
* @param depth Number of bits per pixel.
* @param colorModel Color model to be used for conversions from/to chunky pixels.
* @param bitStride Number of data array elements between two bits of the same image pixel.
* @param scanlineStride Number of data array elements between a given pixel and the pixel in the same column of
* the next scanline.
*/
public BitmapImage(int width, int height, int depth, ColorModel colorModel, int bitStride, int scanlineStride) {
this.width = width;
this.height = height;
this.depth = depth;
this.planarColorModel = colorModel;
this.bitplaneStride = bitStride;
this.scanlineStride = scanlineStride;
if (bitplaneStride < scanlineStride) {
bitmap = new byte[scanlineStride * height];
} else {
bitmap = new byte[bitplaneStride * height];
}
pixelType = NO_PIXEL;
}
/**
* Returns the width of the image.
* <p>
* Pre condition: -
* <p>
* Post condition: -
* <p>
* Obligation: -
*
* @return The width in pixels.
*/
public int getWidth() {
return width;
}
/**
* Returns the height of the image.
* <p>
* Pre condition: -
* <p>
* Post condition: -
* <p>
* Obligation: -
*
* @return The height in pixels.
*/
public int getHeight() {
return height;
}
/**
* Returns the depth of the image.
* <p>
* The depth indicates how many bits are used to form a single pixel.
* <p>
* Pre condition: -
* <p>
* Post condition: -
* <p>
* Obligation: -
*
* @return The number of bitplanes used to form a single pixel.
*/
public int getDepth() {
return depth;
}
/**
* Returns the numer of bytes you must add to a given address
* in the bitmap to advance to the next scanline of the image.
* <p>
* Pre condition: -
* <p>
* Post condition: -
* <p>
* Obligation: -
*
* @return The scansize.
*/
public int getScanlineStride() {
return scanlineStride;
}
/**
* Returns the number of bytes that you must add to a bitmap address
* to advance to the next bit of a scanline.
* <p>
* Pre condition: -
* <p>
* Post condition: -
* <p>
* Obligation: -
*
* @return The interleave of the bitmap.
*/
public int getBitplaneStride() {
return bitplaneStride;
}
/**
* Replaces the color model used for conversions from/to chunky pixels.
* <p>
* Pre condition: The new color model must correspond with the depth of the bitmap.
* <p>
* Post condition: Color model changed.
* <p>
* Obligation: -
*
* @param colorModel The new color model.
*/
public void setPlanarColorModel(ColorModel colorModel) {
planarColorModel = colorModel;
}
/**
* Returns the current color model of the planar image in this bitmap.
* <p>
* Pre condition: -
* <p>
* Post condition: -
* <p>
* Obligation: -
*
* @return The color model.
*/
public ColorModel getPlanarColorModel() {
return planarColorModel;
}
/**
* Sets the preferred color model used for to chunky pixels.
* <p>
* Pre condition: -
* <p>
* Post condition: Color model changed.
* <p>
* Obligation: -
*
* @param colorModel The new color model.
*/
public void setPreferredChunkyColorModel(ColorModel colorModel) {
preferredChunkyColorModel_ = colorModel;
}
/**
* Returns the current color model of the chunky image in this bitmap.
* <p>
* Pre condition: -
* <p>
* Post condition: -
* <p>
* Obligation: -
*
* @return The color model.
*/
public ColorModel getChunkyColorModel() {
if (currentChunkyColorModel_ == null) {
convertToChunky(0, 0, 0, 0);
}
return currentChunkyColorModel_;
}
/**
* Gives you direct access to the bitmap data array.
* <p>
* Pre condition: -.
* <p>
* Post condition: -
* <p>
* Obligation: The bitmap data array remains property
* of the BitmapImage and will be used at the next
* conversion to chunky. You can access it as you
* like (even during conversion) since this class
* does never change the contents of the bitmap.
*
* @return A reference to the bitmap data.
*/
public byte[] getBitmap() {
return bitmap;
}
/**
* Returns a reference to the byte pixel data that has been
* generated by a previous call to #converToChunky.
* <p>
* Pre condition: -
* <p>
* Post condition: -
* <p>
* Obligation: You may modify the contents of the array
* as you like to get some nice effects for the
* next call to #convertToChunky. Note whovewer that
* #convertToChunky will not reuse this array when
* the colorModel has been changed to a color format
* that requires pixels in integer format.
*
* @return byte array or NULL when no byte pixels have been
* generated by #convertToChunky.
*/
public byte[] getBytePixels() {
if (pixelType == BYTE_PIXEL) {
return bytePixels;
} else {
return null;
}
}
/**
* Returns a reference to the byte pixel data that has been
* generated by a previous call to #converToChunky.
* <p>
* Pre condition: -
* <p>
* Post condition: -
* <p>
* Obligation: You may modify the contents of the array
* as you like to get some nice effects for the
* next call to #convertToChunky. Note whovewer that
* #convertToChunky will not reuse this array when
* the colorModel has been changed to a color format
* that requires pixels in integer format.
*
* @return byte array or NULL when no byte pixels have been
* generated by #convertToChunky.
*/
public short[] getShortPixels() {
if (pixelType == BYTE_PIXEL) {
return shortPixels;
} else {
return null;
}
}
/**
* Returns a reference to the integer pixel data that has been
* generated by a previous call to #converToChunky.
* <p>
* Pre condition: -
* <p>
* Post condition: -
* <p>
* Obligation: You may modify the contents of the array
* as you like to get some nice effects for the
* next call to #convertToChunky. Note however that
* #convertToChunky will not reuse this array when
* the colorModel has been changed to a color format
* that requires pixels in byte format.
*
* @return byte array or NULL when no int pixels have been
* generated by #convertToChunky.
*/
public int[] getIntPixels() {
if (pixelType == INT_PIXEL) {
return intPixels;
} else {
return null;
}
}
/**
* Returns the available type of pixel data.
* <p>
* Pre condition: -
* <p>
* Post condition: -
* <p>
* Obligation: -
*
* @return A constant that specifies the current type of pixel data.
*/
public int getPixelType() {
return pixelType;
}
/**
* Creates a clone.
* <p>
* Pre condition: -
* <p>
* Post condition: Clone created.
*
* @return A clone.
*/
@Override
public BitmapImage clone() {
try {
BitmapImage theClone = (BitmapImage) super.clone();
theClone.bitmap = (byte[]) bitmap.clone();
if (getPixelType() == BYTE_PIXEL) {
theClone.bytePixels = (byte[]) bytePixels.clone();
}
if (getPixelType() == INT_PIXEL) {
theClone.intPixels = (int[]) intPixels.clone();
}
return theClone;
} catch (CloneNotSupportedException e) {
throw new InternalError(e.toString());
}
}
/**
* Converts the planar image data into chunky pixel data.
* <p>
* This method will either generate byte pixel data or integer
* pixel data (depending on the color model).
* <p>
* The pixel array that resulted to a prior call to this
* method will be reused when the image dimension and the color
* model allows for it.
* <p>
* Pre condition: -
* <p>
* Post condition: Chunky pixels generated.
* <p>
* Obligation: -
*
* @return The type of generated pixel data.
*/
public int convertToChunky() {
return convertToChunky(0, 0, getHeight() - 1, getWidth() - 1);
}
/**
* Converts the indicated area of the bitmap data into pixel data.
* <p>
* This method will either generate byte pixel data or integer
* pixel data (depending on the color model).
* <p>
* Note that the size of the generated pixel data always corresponds
* to the size of the complete image. You do only specify a subset
* of the image to be <i>converted</i> not a subset to be extracted.
* Note also that the pixel data that resulted from prior calls to
* this method will be reused when the generated pixel array was
* of the same size and type.
* <p>
* Pre condition: -
* <p>
* Post condition: The indicated part of the bitmap has been
* converted into chunky pixels.
* <p>
* Obligation: -
*
* @return The type of generated pixel data.
*/
public int convertToChunky(int top, int left, int bottom, int right) {
pixelType = NO_PIXEL;
/* Ensure pre conditions are met. */
if (top < 0) {
top = 0;
}
if (left < 0) {
left = 0;
}
if (bottom > getHeight() - 1) {
bottom = getHeight() - 1;
}
if (right > getWidth() - 1) {
right = getWidth() - 1;
}
/* */
if (planarColorModel instanceof HAMColorModel) {
if (intPixels == null || intPixels.length != getWidth() * getHeight()) {
bytePixels = null;
shortPixels = null;
intPixels = new int[getWidth() * getHeight()];
}
currentChunkyColorModel_ = planarColorModel;
if (((HAMColorModel) planarColorModel).getHAMType() == HAMColorModel.HAM6) {
ham6PlanesToDirectPixels(top, left, bottom, right);
} else if (((HAMColorModel) planarColorModel).getHAMType() == HAMColorModel.HAM8) {
ham8PlanesToDirectPixels(top, left, bottom, right);
} else {
throw new InternalError("unsupported ham model:" + planarColorModel);
}
pixelType = INT_PIXEL;
} else {
if (planarColorModel instanceof IndexColorModel) {
if (enforceDirectColors_ || preferredChunkyColorModel_ instanceof DirectColorModel) {
if (preferredChunkyColorModel_ != null && ((DirectColorModel) preferredChunkyColorModel_).getPixelSize() == 16) {
if (shortPixels == null || shortPixels.length != getWidth() * getHeight()) {
bytePixels = null;
intPixels = null;
shortPixels = null;
shortPixels = new short[getWidth() * getHeight()];
}
currentChunkyColorModel_ =
(preferredChunkyColorModel_ != null && (preferredChunkyColorModel_ instanceof DirectColorModel))
? preferredChunkyColorModel_
: new DirectColorModel(16, 0x7c00, 0x3e0, 0x1f);
indexPlanesTo555(top, left, bottom, right);
pixelType = SHORT_PIXEL;
} else {
if (intPixels == null || intPixels.length != getWidth() * getHeight()) {
bytePixels = null;
shortPixels = null;
intPixels = new int[getWidth() * getHeight()];
}
currentChunkyColorModel_ =
(preferredChunkyColorModel_ != null && (preferredChunkyColorModel_ instanceof DirectColorModel))
? preferredChunkyColorModel_
: ColorModel.getRGBdefault();
currentChunkyColorModel_ = new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
indexPlanesToDirectPixels(top, left, bottom, right);
pixelType = INT_PIXEL;
}
} else {
if (bytePixels == null || bytePixels.length != getWidth() * getHeight()) {
intPixels = null;
shortPixels = null;
bytePixels = new byte[getWidth() * getHeight()];
}
currentChunkyColorModel_ = planarColorModel;
indexPlanesToIndexPixels(top, left, bottom, right);
pixelType = BYTE_PIXEL;
}
} else if (planarColorModel instanceof DirectColorModel) {
if (((DirectColorModel) planarColorModel).getPixelSize() == 16) {
if (shortPixels == null || shortPixels.length != getWidth() * getHeight()) {
bytePixels = null;
intPixels = null;
shortPixels = null;
shortPixels = new short[getWidth() * getHeight()];
}
currentChunkyColorModel_ = planarColorModel;
directPlanesTo555(top, left, bottom, right);
pixelType = SHORT_PIXEL;
} else {
if (intPixels == null || intPixels.length != getWidth() * getHeight()) {
bytePixels = null;
shortPixels = null;
shortPixels = null;
intPixels = new int[getWidth() * getHeight()];
}
currentChunkyColorModel_ = planarColorModel;
directPlanesToDirectPixels(top, left, bottom, right);
pixelType = INT_PIXEL;
}
} else {
throw new InternalError("unsupported color model:" + planarColorModel);
}
}
return pixelType;
}
/**
* Converts the indicated area of the bitmap data into pixel data.
* <p>
* This method will either generate byte pixel data or integer
* pixel data (depending on the color model).
* <p>
* Note that the size of the generated pixel data always corresponds
* to the size of the complete image. You do only specify a subset
* of the image to be <i>converted</i> not a subset to be extracted.
* Note also that the pixel data that resulted from prior calls to
* this method will be reused when the generated pixel array was
* of the same size and type.
* <p>
* Pre condition: -
* <p>
* Post condition: The indicated part of the bitmap has been
* converted into chunky pixels.
* <p>
* Obligation: -
*/
public void convertFromChunky(BufferedImage image) {
/* */
if (planarColorModel instanceof HAMColorModel) {
throw new UnsupportedOperationException("HAM mode not implemented:"+ planarColorModel);
} else {
if (planarColorModel instanceof IndexColorModel) {
if (image.getType() == BufferedImage.TYPE_BYTE_INDEXED) {
planarColorModel=image.getColorModel();
Raster raster=image.getRaster();
int dx=0,dy=0;
while (raster.getParent()!=null) {
dx+=raster.getMinX();
dy+=raster.getMinY();
raster=raster.getParent();
}
DataBufferByte dbuf= ((DataBufferByte)image.getRaster().getDataBuffer());
int inScanlineStride=raster.getWidth();
byte[] inb=dbuf.getData();
if (bytePixels==null||bytePixels.length!=width*height) {
bytePixels=new byte[width*height];
}
for (int y=0;y<height;y++) {
System.arraycopy(inb,dx+(y+dy)*inScanlineStride,bytePixels,y*width,width);
}
indexPixelsToIndexPlanes(0, 0, getHeight() - 1, getWidth() - 1);
} else {
throw new UnsupportedOperationException("index color model not implemented:" + planarColorModel);
}
} else if (planarColorModel instanceof DirectColorModel) {
throw new UnsupportedOperationException("index color model not implemented:" + planarColorModel);
} else {
throw new UnsupportedOperationException("unsupported color model:" + planarColorModel);
}
}
}
/**
* Frees the memory allocated for the pixel data.
*
* <p>
* Pre condition: -
* <p>
* Post condition: The bitmap has given up all its
* references to the pixel data.
* <p>
* Obligation: The pixel data will not be reused at the
* next call to #convertToChunky.
*/
public void flushPixels() {
pixelType = NO_PIXEL;
intPixels = null;
shortPixels = null;
bytePixels = null;
}
/**
* Converts the planar image data into chunky pixels.
*
* After successful completion the chunky pixels can by used
* in conjunction with the IndexColorModel associated to
* this instance.
*
* Pre condition
* The color model must be an instance of java.awt.IndexColorModel.
* 0 <= topBound <= bottomBound <= height.
* 0 <= leftBound <= rightBound <= width.
* Post condition
* -
* Obligation
* -
*
* @author Werner Randelshofer, Hausmatt 10, CH-6405 Immensee, Switzerland
* @version 1997-10-16 Created.
*/
private void indexPlanesToIndexPixels(int top, int left, int bottom, int right) {
/* Add one to bottom and right to facilitate computations. */
bottom++;
right++;
final int scanlineStride = getScanlineStride();
final int bitplaneStride = getBitplaneStride();
final int depth = getDepth();
final int width = getWidth();
final int pixelLineStride = width - right + left;
final int bottomScanline = bottom * scanlineStride;
//final int bitCorrection = depth - 8;
//final int bitCorrection = 8 - depth;
int x;
int iPixel = top * width + left;
int pixel = 0;
//int bitShift;
int iBitmap;
int iScanline;
int iDepth;
int b0, b1, b2, b3, b4, b5, b6, b7;
b0 = b1 = b2 = b3 = b4 = b5 = b6 = b7 = 0;
final int bitplaneStride1 = bitplaneStride;
final int bitplaneStride2 = bitplaneStride * 2;
final int bitplaneStride3 = bitplaneStride * 3;
final int bitplaneStride4 = bitplaneStride * 4;
final int bitplaneStride5 = bitplaneStride * 5;
final int bitplaneStride6 = bitplaneStride * 6;
final int bitplaneStride7 = bitplaneStride * 7;
int iBit; // the index of the bit inside the byte at the current x-position
int bitMask; // the mask for the bit inside the byte at the current x-position
switch (depth) {
case 1:
/*
for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
for (x = left; x < right; x++) {
bitShift = x % 8;
iBitmap = iScanline + x / 8;
bytePixels_[iPixel++] = (byte) (((bitmap_[iBitmap] << bitShift) & 128) >>> 7);
}
iPixel += pixelLineStride;
}
*/
for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
for (x = left; x < right; x++) {
bytePixels[iPixel++] = (byte) (((bitmap[iScanline + (x >>> 3)] << (x & 7)) & 128) >>> 7);
}
iPixel += pixelLineStride;
}
break;
case 2:
/*
for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
for (x = left; x < right; x++) {
bitShift = x & 7;
iBitmap = iScanline + x >>> 3;
bytePixels_[iPixel++] = (byte) (
((bitmap_[iBitmap] << bitShift) & 128) >>> 7
| ((bitmap_[iBitmap+bitplaneStride1] << bitShift) & 128) >>> 6
);
}
iPixel += pixelLineStride;
}*/
for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
for (x = left; x < right; x++) {
iBit = x & 7;
bitMask = 128 >>> (iBit);
iBitmap = iScanline + (x >>> 3);
bytePixels[iPixel++] = (byte) (((bitmap[iBitmap] & bitMask)
| (bitmap[iBitmap + bitplaneStride1] & bitMask) << 1) >>> (7 - iBit));
}
iPixel += pixelLineStride;
}
break;
case 3:
/*
for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
for (x = left; x < right; x++) {
bitShift = x & 7;
iBitmap = iScanline + x >>> 3;
bytePixels_[iPixel++] = (byte) (
((bitmap_[iBitmap] << bitShift) & 128) >>> 7
| ((bitmap_[iBitmap+bitplaneStride1] << bitShift) & 128) >>> 6
| ((bitmap_[iBitmap+bitplaneStride2] << bitShift) & 128) >>> 5
);
}
iPixel += pixelLineStride;
}*/
for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
for (x = left; x < right; x++) {
iBit = x & 7;
bitMask = 128 >>> (iBit);
iBitmap = iScanline + (x >>> 3);
bytePixels[iPixel++] = (byte) (((bitmap[iBitmap] & bitMask)
| (bitmap[iBitmap + bitplaneStride1] & bitMask) << 1
| (bitmap[iBitmap + bitplaneStride2] & bitMask) << 2) >>> (7 - iBit));
}
iPixel += pixelLineStride;
}
break;
case 4:
/*
for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
for (x = left; x < right; x++) {
int bitShift = x % 8;
iBitmap = iScanline + x / 8;
bytePixels_[iPixel++] = (byte) (
((bitmap[iBitmap] << bitShift) & 128) >>> 7
| ((bitmap[iBitmap+bitplaneStride1] << bitShift) & 128) >>> 6
| ((bitmap[iBitmap+bitplaneStride2] << bitShift) & 128) >>> 5
| ((bitmap[iBitmap+bitplaneStride3] << bitShift) & 128) >>> 4
);
}
iPixel += pixelLineStride;
}*/
for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
for (x = left; x < right; x++) {
iBit = x & 7;
bitMask = 128 >>> (iBit);
iBitmap = iScanline + (x >>> 3);
bytePixels[iPixel++] = (byte) (((bitmap[iBitmap] & bitMask)
| (bitmap[iBitmap + bitplaneStride1] & bitMask) << 1
| (bitmap[iBitmap + bitplaneStride2] & bitMask) << 2
| (bitmap[iBitmap + bitplaneStride3] & bitMask) << 3) >>> (7 - iBit));
}
iPixel += pixelLineStride;
}
break;
case 5:
/*
for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
for (x = left; x < right; x++) {
bitShift = x % 8;
iBitmap = iScanline + x / 8;
bytePixels_[iPixel++] = (byte) (
((bitmap_[iBitmap] << bitShift) & 128) >>> 7
| ((bitmap_[iBitmap+bitplaneStride1] << bitShift) & 128) >>> 6
| ((bitmap_[iBitmap+bitplaneStride2] << bitShift) & 128) >>> 5
| ((bitmap_[iBitmap+bitplaneStride3] << bitShift) & 128) >>> 4
| ((bitmap_[iBitmap+bitplaneStride4] << bitShift) & 128) >>> 3
);
}
iPixel += pixelLineStride;
}*/
/*
for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
for (x = left; x < right; x++) {
iBit = x & 7;
bitMask = 128 >>> (iBit);
iBitmap = iScanline + (x >>> 3);
bytePixels_[iPixel++] = (byte) ((
(bitmap_[iBitmap] & bitMask)
| (bitmap_[iBitmap+bitplaneStride1] & bitMask) << 1
| (bitmap_[iBitmap+bitplaneStride2] & bitMask) << 2
| (bitmap_[iBitmap+bitplaneStride3] & bitMask) << 3
| (bitmap_[iBitmap+bitplaneStride4] & bitMask) << 4
) >>> (7 - iBit));
}
iPixel += pixelLineStride;
}
iPixel=0;
*/
for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
for (x = left; x < right; x++) {
iBit = x & 7;
bitMask = 128 >>> (iBit);
iBitmap = iScanline + (x >>> 3);
if (iBit == 0) {
b0 = bitmap[iBitmap];
b1 = bitmap[iBitmap + bitplaneStride];
b2 = bitmap[iBitmap + bitplaneStride2];
b3 = bitmap[iBitmap + bitplaneStride3];
b4 = bitmap[iBitmap + bitplaneStride4];
}
bytePixels[iPixel++] = (byte) (((b0 & bitMask)
| (b1 & bitMask) << 1
| (b2 & bitMask) << 2
| (b3 & bitMask) << 3
| (b4 & bitMask) << 4) >>> (7 - iBit));
}
iPixel += pixelLineStride;
}
break;
case 6:
/*
for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
for (x = left; x < right; x++) {
bitShift = x % 8;
iBitmap = iScanline + x / 8;
bytePixels_[iPixel++] = (byte) (