@@ -129,10 +129,7 @@ public FastBitmap(FastBitmap fastBitmap){
129
129
*/
130
130
public FastBitmap (BufferedImage bufferedImage ) {
131
131
this .bufferedImage = bufferedImage ;
132
- if (getType () == BufferedImage .TYPE_3BYTE_BGR ) {
133
- toRGB ();
134
- }
135
- setCoordinateSystem (CoordinateSystem .Matrix );
132
+ prepare ();
136
133
refresh ();
137
134
}
138
135
@@ -142,10 +139,7 @@ public FastBitmap(BufferedImage bufferedImage) {
142
139
*/
143
140
public FastBitmap (Image image ) {
144
141
bufferedImage = (BufferedImage )image ;
145
- if (getType () == BufferedImage .TYPE_3BYTE_BGR ) {
146
- toRGB ();
147
- }
148
- setCoordinateSystem (CoordinateSystem .Matrix );
142
+ prepare ();
149
143
refresh ();
150
144
}
151
145
@@ -155,10 +149,7 @@ public FastBitmap(Image image) {
155
149
*/
156
150
public FastBitmap (ImageIcon ico ){
157
151
bufferedImage = (BufferedImage )ico .getImage ();
158
- if (getType () == BufferedImage .TYPE_3BYTE_BGR ) {
159
- toRGB ();
160
- }
161
- setCoordinateSystem (CoordinateSystem .Matrix );
152
+ prepare ();
162
153
refresh ();
163
154
}
164
155
@@ -169,16 +160,7 @@ public FastBitmap(ImageIcon ico){
169
160
public FastBitmap (String pathname ){
170
161
try {
171
162
this .bufferedImage = ImageIO .read (new File (pathname ));
172
- setCoordinateSystem (CoordinateSystem .Matrix );
173
- if (getType () == BufferedImage .TYPE_BYTE_GRAY ) {
174
- refresh ();
175
- }
176
- else if (getType () == BufferedImage .TYPE_INT_ARGB || getType () == BufferedImage .TYPE_4BYTE_ABGR ){
177
- toARGB ();
178
- }
179
- else {
180
- toRGB ();
181
- }
163
+ prepare ();
182
164
} catch (IOException ex ) {
183
165
ex .printStackTrace ();
184
166
}
@@ -208,8 +190,8 @@ public FastBitmap(int width, int height, ColorSpace colorSpace){
208
190
else if (colorSpace == ColorSpace .Grayscale ){
209
191
bufferedImage = new BufferedImage (width , height , BufferedImage .TYPE_BYTE_GRAY );
210
192
}
211
- else if (colorSpace == ColorSpace .RGB ){
212
- bufferedImage = new BufferedImage (width , height , BufferedImage .TYPE_INT_RGB );
193
+ else if (colorSpace == ColorSpace .ARGB ){
194
+ bufferedImage = new BufferedImage (width , height , BufferedImage .TYPE_INT_ARGB );
213
195
}
214
196
215
197
this .setCoordinateSystem (CoordinateSystem .Matrix );
@@ -232,12 +214,32 @@ public FastBitmap(int[][] image){
232
214
* @param image Array.
233
215
*/
234
216
public FastBitmap (int [][][] image ){
235
- bufferedImage = new BufferedImage (image [0 ].length , image .length , BufferedImage .TYPE_INT_RGB );
217
+ if (image [0 ][0 ].length == 3 )
218
+ bufferedImage = new BufferedImage (image [0 ].length , image .length , BufferedImage .TYPE_INT_RGB );
219
+ else {
220
+ bufferedImage = new BufferedImage (image [0 ].length , image .length , BufferedImage .TYPE_INT_ARGB );
221
+ }
236
222
this .setCoordinateSystem (CoordinateSystem .Matrix );
237
223
refresh ();
238
224
arrayToImage (image );
239
225
}
240
226
227
+ /**
228
+ * Prepare the Fast Bitmap;
229
+ */
230
+ private void prepare (){
231
+ if (getType () == BufferedImage .TYPE_BYTE_GRAY ) {
232
+ refresh ();
233
+ }
234
+ else if (getType () == BufferedImage .TYPE_INT_ARGB || getType () == BufferedImage .TYPE_4BYTE_ABGR ){
235
+ toARGB ();
236
+ }
237
+ else {
238
+ toRGB ();
239
+ }
240
+ setCoordinateSystem (CoordinateSystem .Matrix );
241
+ }
242
+
241
243
/**
242
244
* Refresh raster and get data buffer from raster.
243
245
*/
@@ -544,9 +546,17 @@ public void arrayToImage(double image[][]){
544
546
* @param image Array.
545
547
*/
546
548
public void arrayToImage (int image [][][]){
547
- for (int x = 0 ; x < image .length ; x ++) {
548
- for (int y = 0 ; y < image [0 ].length ; y ++) {
549
- setRGB (x , y , image [x ][y ][0 ], image [x ][y ][1 ], image [x ][y ][2 ]);
549
+ if (image [0 ][0 ].length == 3 )
550
+ for (int x = 0 ; x < image .length ; x ++) {
551
+ for (int y = 0 ; y < image [0 ].length ; y ++) {
552
+ setRGB (x , y , image [x ][y ][0 ], image [x ][y ][1 ], image [x ][y ][2 ]);
553
+ }
554
+ }
555
+ else {
556
+ for (int x = 0 ; x < image .length ; x ++) {
557
+ for (int y = 0 ; y < image [0 ].length ; y ++) {
558
+ setARGB (x , y , image [x ][y ][0 ], image [x ][y ][1 ], image [x ][y ][2 ], image [x ][y ][3 ]);
559
+ }
550
560
}
551
561
}
552
562
}
@@ -556,9 +566,17 @@ public void arrayToImage(int image[][][]){
556
566
* @param image Array.
557
567
*/
558
568
public void arrayToImage (float image [][][]){
559
- for (int x = 0 ; x < image .length ; x ++) {
560
- for (int y = 0 ; y < image [0 ].length ; y ++) {
561
- setRGB (x , y , (int )image [x ][y ][0 ], (int )image [x ][y ][1 ], (int )image [x ][y ][2 ]);
569
+ if (image [0 ][0 ].length == 3 )
570
+ for (int x = 0 ; x < image .length ; x ++) {
571
+ for (int y = 0 ; y < image [0 ].length ; y ++) {
572
+ setRGB (x , y , (int )image [x ][y ][0 ], (int )image [x ][y ][1 ], (int )image [x ][y ][2 ]);
573
+ }
574
+ }
575
+ else {
576
+ for (int x = 0 ; x < image .length ; x ++) {
577
+ for (int y = 0 ; y < image [0 ].length ; y ++) {
578
+ setARGB (x , y , (int )image [x ][y ][0 ], (int )image [x ][y ][1 ], (int )image [x ][y ][2 ], (int )image [x ][y ][3 ]);
579
+ }
562
580
}
563
581
}
564
582
}
@@ -568,9 +586,17 @@ public void arrayToImage(float image[][][]){
568
586
* @param image Array.
569
587
*/
570
588
public void arrayToImage (double image [][][]){
571
- for (int x = 0 ; x < image .length ; x ++) {
572
- for (int y = 0 ; y < image [0 ].length ; y ++) {
573
- setRGB (x , y , (int )image [x ][y ][0 ], (int )image [x ][y ][1 ], (int )image [x ][y ][2 ]);
589
+ if (image [0 ][0 ].length == 3 )
590
+ for (int x = 0 ; x < image .length ; x ++) {
591
+ for (int y = 0 ; y < image [0 ].length ; y ++) {
592
+ setRGB (x , y , (int )image [x ][y ][0 ], (int )image [x ][y ][1 ], (int )image [x ][y ][2 ]);
593
+ }
594
+ }
595
+ else {
596
+ for (int x = 0 ; x < image .length ; x ++) {
597
+ for (int y = 0 ; y < image [0 ].length ; y ++) {
598
+ setARGB (x , y , (int )image [x ][y ][0 ], (int )image [x ][y ][1 ], (int )image [x ][y ][2 ], (int )image [x ][y ][3 ]);
599
+ }
574
600
}
575
601
}
576
602
}
0 commit comments