forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJFImage.java
More file actions
executable file
·1204 lines (1107 loc) · 29.7 KB
/
Copy pathJFImage.java
File metadata and controls
executable file
·1204 lines (1107 loc) · 29.7 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
package javaforce;
import java.awt.*;
import java.awt.image.*;
import java.awt.font.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
/**
* Encapsules BufferedImage to provide more functions.
* Implements javax.swing.Icon so it can also be used with JLabel.
*
* @author Peter Quiring
*/
public class JFImage extends JComponent implements Icon {
private BufferedImage bi;
private Graphics2D g2d;
private int buffer[];
private ResizeOperation resizeOperation = ResizeOperation.CLEAR;
private int imageType; //BufferedImage.TYPE_INT_...
public enum ResizeOperation {
CLEAR, //reset image
CHOP, //copy/chop image
SCALE //scale image
};
public JFImage() {
imageType = BufferedImage.TYPE_INT_ARGB;
}
public JFImage(boolean alpha) {
if (alpha)
imageType = BufferedImage.TYPE_INT_ARGB;
else
imageType = BufferedImage.TYPE_INT_RGB;
}
public JFImage(int x, int y) {
imageType = BufferedImage.TYPE_INT_ARGB;
setImageSize(x, y);
}
public JFImage(int x, int y, boolean alpha) {
if (alpha)
imageType = BufferedImage.TYPE_INT_ARGB;
else
imageType = BufferedImage.TYPE_INT_RGB;
setImageSize(x, y);
}
private void init() {
g2d = bi.createGraphics();
buffer = ((DataBufferInt) bi.getRaster().getDataBuffer()).getData();
fill(0,0,getWidth(),getHeight(), 0); //fill with black opaque (the default varies by platform)
}
private void initImage(int x, int y) {
// System.out.println("JFImage.initImage:" + x + "," + y);
bi = new BufferedImage(x, y, imageType);
init();
setPreferredSize(new Dimension(x, y));
}
public void setImageSize(int x, int y) {
setSize(x, y);
initImage(x, y);
}
public void setBounds(int x, int y, int w, int h) {
//usually called from LayoutManager / ScrollPane
// System.out.println("JFImage.setBounds:" + x + "," + y + "," + w + "," + h);
super.setBounds(x, y, w, h);
if (bi != null) {
BufferedImage oldbi = bi;
int oldw = getWidth();
int oldh = getHeight();
Composite org;
switch (resizeOperation) {
case CLEAR:
initImage(w, h);
break;
case CHOP:
initImage(w, h);
org = g2d.getComposite();
g2d.setComposite(AlphaComposite.Src);
g2d.drawImage(oldbi, 0, 0, null);
g2d.setComposite(org);
break;
case SCALE:
initImage(w, h);
org = g2d.getComposite();
g2d.setComposite(AlphaComposite.Src);
g2d.drawImage(oldbi, 0, 0, w, h, 0, 0, oldw, oldh, null);
g2d.setComposite(org);
break;
}
} else {
initImage(w, h);
}
}
public void setSize(int x, int y) {
// System.out.println("JFImage.setSize:" + x + "," + y);
super.setSize(x, y); //just calls setBounds(getLocation(),x,y);
}
public void setSize(Dimension d) {
// System.out.println("JFImage.setSize:" + d);
super.setSize(d);
}
/**
* Controls how this image is resized by layout managers.
*/
public void setResizeOperation(ResizeOperation ro) {
resizeOperation = ro;
}
public Image getImage() {
return bi;
}
public BufferedImage getBufferedImage() {
return bi;
}
/**
* Replaced BufferedImage.
* Must be TYPE_INT_ARGB or TYPE_INT_RGB type.
* @param bi - new BufferedImage
*/
public void setBufferedImage(BufferedImage bi) {
int newType = bi.getType();
if (newType != BufferedImage.TYPE_INT_ARGB || newType != BufferedImage.TYPE_INT_RGB) {
JFLog.log("JFImage.setBufferedImage() : Input image is not INT_RGB/INT_ARGB type");
return;
}
this.bi = bi;
imageType = bi.getType();
init();
}
public Graphics getGraphics() {
return g2d;
}
public Graphics2D getGraphics2D() {
return g2d;
}
/** Paint this image onto Graphics. */
public void paint(Graphics g) {
//System.out.println("paint");
if (bi == null) {
return;
}
g.drawImage(getImage(), 0, 0, null);
}
public int getWidth() {
if (bi == null) {
return -1;
}
return bi.getWidth();
}
public int getHeight() {
if (bi == null) {
return -1;
}
return bi.getHeight();
}
public boolean load(InputStream in) {
//use ImageIO (the returned Image is type TYPE_CUSTOM which is slow so copy pixels to an TYPE_INT_ARGB)
BufferedImage tmp;
try {
tmp = ImageIO.read(in);
in.close();
} catch (Exception e) {
JFLog.log(e);
return false;
}
if (tmp == null) {
return false;
}
int x = tmp.getWidth();
int y = tmp.getHeight();
int px[] = tmp.getRGB(0, 0, x, y, null, 0, x); //tmp may not be int[] buffer
setImageSize(x, y);
putPixels(px, 0, 0, x, y, 0);
return true;
}
public boolean save(OutputStream out, String fmt) {
boolean ret;
if (fmt.equals("jpg")) ret = saveJPG(out); //Must convert image type
else if (fmt.equals("ico")) ret = saveICO(out); //ImageIO doesn't support ICO
else if (fmt.equals("icns")) ret = saveICNS(out); //ImageIO doesn't support ICNS
else if (fmt.equals("bmp")) ret = saveBMP(out); //ImageIO doesn't support BMP (except Windows)
else {
try {
ret = ImageIO.write(bi, fmt, out);
} catch (Exception e) {
JFLog.log(e);
ret = false;
}
}
try {out.close();} catch (Exception e) {}
return ret;
}
public boolean load(String filename) {
try {
return load(new FileInputStream(filename));
} catch (Exception e) {
return false;
}
}
public boolean save(String filename, String fmt) {
try {
return save(new FileOutputStream(filename), fmt);
} catch (Exception e) {
return false;
}
}
public boolean loadJPG(InputStream in) {
return load(in);
}
public boolean saveJPG(OutputStream out) {
//Do NOT use a BufferedImage of type TYPE_INT_ARGB
//On Windows it creates a 4 channel image which is mistake in CMYK format (which gives it a pinkish tint)
//On Linux it throws an exception!!!
//See http://stackoverflow.com/questions/3432388/imageio-not-able-to-write-a-jpeg-file
//So convert BufferedImage to type TYPE_3BYTE_BGR instead and it works on all platforms
try {
int w = bi.getWidth();
int h = bi.getHeight();
int px[] = new int[w * h];
BufferedImage tmp = new BufferedImage(bi.getWidth(),bi.getHeight(),BufferedImage.TYPE_3BYTE_BGR);
bi.getRGB(0,0,w,h,px,0,w);
tmp.setRGB(0,0,w,h,px,0,w);
return ImageIO.write(tmp, "jpg", out);
} catch (Exception e) {
JFLog.log(e);
}
return false;
}
public boolean loadJPG(String filename) {
try {
return loadJPG(new FileInputStream(filename));
} catch (Exception e) {
return false;
}
}
public boolean saveJPG(String filename) {
try {
return saveJPG(new FileOutputStream(filename));
} catch (Exception e) {
return false;
}
}
public boolean loadPNG(InputStream in) {
return load(in);
}
public boolean savePNG(OutputStream out) {
return save(out, "png");
}
public boolean loadPNG(String filename) {
try {
return loadPNG(new FileInputStream(filename));
} catch (Exception e) {
return false;
}
}
public boolean savePNG(String filename) {
try {
return savePNG(new FileOutputStream(filename));
} catch (Exception e) {
return false;
}
}
//NOTE : Not all JREs support "bmp" so I use custom code
public boolean loadBMP(String filename) {
try {
return loadBMP(new FileInputStream(filename));
} catch (Exception e) {
return false;
}
}
public boolean saveBMP(String filename) {
try {
return saveBMP(new FileOutputStream(filename));
} catch (Exception e) {
return false;
}
}
public boolean loadBMP(InputStream in) {
int buf[];
Dimension size = new Dimension(0, 0);
buf = bmp.load(in, size);
try {in.close();} catch (Exception e) {}
if (buf == null) {
return false;
}
if (size.width == 0 || size.height == 0) {
return false;
}
setImageSize(size.width, size.height);
putPixels(buf, 0, 0, size.width, size.height, 0);
return true;
}
public boolean saveBMP(OutputStream out) {
int pixels[];
pixels = getPixels(0, 0, getWidth(), getHeight());
Dimension size = new Dimension(getWidth(), getHeight());
return bmp.save24(out, pixels, size, false);
}
//save ICO (no loading supported)
public boolean saveICO(String filename) {
try {
return saveICO(new FileOutputStream(filename));
} catch (Exception e) {
return false;
}
}
public boolean saveICO(OutputStream out) {
int pixels[];
pixels = getPixels(0, 0, getWidth(), getHeight());
Dimension size = new Dimension(getWidth(), getHeight());
return bmp.save32(out, pixels, size, false, true);
}
/** save icns (Mac icon) */
public boolean saveICNS(String filename) {
try {
return saveICNS(new FileOutputStream(filename));
} catch (Exception e) {
return false;
}
}
/** save icns (Mac icon) */
public boolean saveICNS(OutputStream out) {
//see http://en.wikipedia.org/wiki/Apple_Icon_Image_format
//use one of the PNG formats
byte header[] = new byte[4*4];
//scale image to one of supported sizes
int w = getWidth();
int h = getHeight();
if (w > h) h = w;
if (h > w) w = h;
int newSize = 16;
String OSType = "icp4";
/*
if (w > 512) {
newSize = 1024; //also retina 512x512@2x ???
OSType = "ic10";
}
else
*/
if (w > 256) {
newSize = 512;
OSType = "ic09";
}
else if (w > 128) {
newSize = 256;
OSType = "ic08";
}
else if (w > 64) {
newSize = 128;
OSType = "ic07";
}
else if (w > 32) {
newSize = 64;
OSType = "icp6";
}
else if (w > 16) {
newSize = 32;
OSType = "icp5";
}
if (w != newSize || h != newSize) {
// System.out.println("Scaling icns to:" + newSize + "x" + newSize);
setResizeOperation(ResizeOperation.SCALE);
setSize(newSize, newSize);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
savePNG(baos);
// System.out.println("png.size=" + baos.size());
System.arraycopy("icns".getBytes(), 0, header, 0, 4); //file header "icns"
BE.setuint32(header, 4, 4*4 + baos.size()); //file size
System.arraycopy(OSType.getBytes(), 0, header, 8, 4); //image type
BE.setuint32(header, 12, 4*2 + baos.size()); //image size
try {
out.write(header);
out.write(baos.toByteArray());
} catch (Exception e) {
return false;
}
return true;
}
public boolean loadSVG(String filename) {
try {
return loadSVG(new FileInputStream(filename));
} catch (Exception e) {
return false;
}
}
public boolean saveSVG(String filename) {
try {
return saveSVG(new FileOutputStream(filename));
} catch (Exception e) {
return false;
}
}
public boolean loadSVG(InputStream in) {
int buf[];
Dimension size = new Dimension(0, 0);
buf = svg.load(in, size);
try {in.close();} catch (Exception e) {}
if (buf == null) {
return false;
}
if (size.width == 0 || size.height == 0) {
return false;
}
setImageSize(size.width, size.height);
putPixels(buf, 0, 0, size.width, size.height, 0);
return true;
}
public boolean saveSVG(OutputStream out) {
ByteArrayOutputStream png_data = new ByteArrayOutputStream();
savePNG(png_data);
Dimension size = new Dimension(getWidth(), getHeight());
boolean ret = svg.save(out, png_data.toByteArray(), size);
try {out.close();} catch (Exception e) {}
return ret;
}
public boolean loadXPM(String filename) {
try {
return loadXPM(new FileInputStream(filename));
} catch (Exception e) {
return false;
}
}
public boolean loadXPM(InputStream in) {
int buf[];
Dimension size = new Dimension(0, 0);
buf = new xpm().load(in, size);
if (buf == null) {
return false;
}
if (size.width == 0 || size.height == 0) {
return false;
}
setImageSize(size.width, size.height);
putPixels(buf, 0, 0, size.width, size.height, 0);
return true;
}
/** Puts pixels . */
public void putJFImage(JFImage img, int x, int y) {
int px[] = img.getPixels();
putPixels(px, x, y, img.getWidth(), img.getHeight(), 0);
}
/** Puts pixels unless src pixel == keyclr */
public void putJFImageKeyClr(JFImage img, int x, int y, int keyClr) {
int px[] = img.getPixels();
putPixelsKeyClr(px, x, y, img.getWidth(), img.getHeight(), 0, keyClr);
}
/** Puts pixels blending using img alpha (dest alpha is ignored) */
public void putJFImageBlend(JFImage img, int x, int y, boolean keepAlpha) {
int px[] = img.getPixels(0, 0, img.getWidth(), img.getHeight());
putPixelsBlend(px, x, y, img.getWidth(), img.getHeight(), 0, keepAlpha);
}
/** Puts pixels scaling image to fit */
public void putJFImageScale(JFImage img, int x, int y, int width, int height) {
g2d.drawImage(img.getImage(), x, y, x+width-1, y+height-1, 0, 0, img.getWidth()-1, img.getHeight()-1, null);
}
/** Returns an area of this image as a new JFImage. */
public JFImage getJFImage(int x, int y, int w, int h) {
JFImage ret = new JFImage();
ret.bi = bi.getSubimage(x, y, w, h);
ret.init();
return ret;
}
public static final int ALPHA_MASK = 0xff000000; //Alpha
public static final int OPAQUE = 0xff000000;
public static final int TRANSPARENT = 0x00000000;
public static final int RED_MASK = 0x00ff0000;
public static final int GREEN_MASK = 0x0000ff00;
public static final int BLUE_MASK = 0x000000ff;
public static final int RGB_MASK = 0x00ffffff;
/** Draws one pixel using r,g,b values (alpha assumed opaque) */
public void putPixel(int x, int y, int r, int g, int b) {
buffer[y * getWidth() + x] = OPAQUE | r << 16 | g << 8 | b;
}
/** Draws one pixel using rgb value (alpha assumed opaque) */
public void putPixel(int x, int y, int c) {
buffer[y * getWidth() + x] = OPAQUE | c;
}
/** Returns rgb value of pixel at x,y */
public int getPixel(int x, int y) {
return buffer[y * getWidth() + x] & RGB_MASK;
}
/** Returns alpha value of pixel at x,y */
public int getAlpha(int x, int y) {
return (buffer[y * getWidth() + x] & ALPHA_MASK) >>> 24;
}
/** Sets alpha value at x,y */
public void putAlpha(int x, int y, int lvl) {
int offset = y * getWidth() + x;
int px = buffer[offset] & RGB_MASK;
px |= (lvl << 24);
buffer[offset] = px;
}
/** Puts pixels */
public void putPixels(int[] px, int x, int y, int w, int h, int offset) {
putPixels(px, x, y, w, h, offset, w);
}
/** Puts pixels (supports padding at end of each scan line) */
public void putPixels(int[] px, int x, int y, int w, int h, int offset, int scansize) {
//do clipping
int ow = w; //org width
int bw = getWidth();
int bh = getHeight();
if (y < 0) {
y *= -1;
h -= y;
if (h <= 0) {
return;
}
offset += y * scansize;
y = 0;
}
if (x < 0) {
x *= -1;
w -= x;
if (w <= 0) {
return;
}
offset += x;
x = 0;
}
if (x + w > bw) {
w = bw - x;
if (w <= 0) {
return;
}
}
if (y + h > bh) {
h = bh - y;
if (h <= 0) {
return;
}
}
int dst = y * bw + x;
if (w == bw && scansize == w) {
System.arraycopy(px, offset, buffer, dst, w * h);
} else {
for(int i=0;i<h;i++) {
System.arraycopy(px, offset, buffer, dst, w);
offset += scansize;
dst += bw;
}
}
}
/** Put Pixels unless src pixel == keyclr */
public void putPixelsKeyClr(int[] px, int x, int y, int w, int h, int offset, int keyclr) {
//do clipping
int scansize = w;
int bw = getWidth();
int bh = getHeight();
if (y < 0) {
y *= -1;
h -= y;
if (h <= 0) {
return;
}
offset += y * scansize;
y = 0;
}
if (x < 0) {
x *= -1;
w -= x;
if (w <= 0) {
return;
}
offset += x;
x = 0;
}
if (x + w > bw) {
w = bw - x;
if (w <= 0) {
return;
}
}
if (y + h > bh) {
h = bh - y;
if (h <= 0) {
return;
}
}
int dst = y * bw + x;
for(int i=0;i<h;i++) {
for(int j=0;j<w;j++) {
if ((px[offset + j] & RGB_MASK) != keyclr) {
buffer[dst + j] = px[offset + j];
}
}
offset += scansize;
dst += bw;
}
}
/** Puts pixels blending using src alpha (dest alpha is ignored).
* if keepAlpha is true then dest alpha is preserved.
* if keepAlpha is false then src alpha is copied to dest.
*/
public void putPixelsBlend(int[] px, int x, int y, int w, int h, int offset, boolean keepAlpha) {
//do clipping
int scansize = w;
int bw = getWidth();
int bh = getHeight();
if (y < 0) {
y *= -1;
h -= y;
if (h <= 0) {
return;
}
offset += y * scansize;
y = 0;
}
if (x < 0) {
x *= -1;
w -= x;
if (w <= 0) {
return;
}
offset += x;
x = 0;
}
if (x + w > bw) {
w = bw - x;
if (w <= 0) {
return;
}
}
if (y + h > bh) {
h = bh - y;
if (h <= 0) {
return;
}
}
int dst = y * bw + x;
int sp, slvl, dp, dlvl, sa, da;
for(int i=0;i<h;i++) {
for(int j=0;j<w;j++) {
sp = px[offset + j];
sa = sp & ALPHA_MASK;
sp &= RGB_MASK;
slvl = sa >>> 24;
if (slvl > 0) {
slvl++;
}
dlvl = 0x100 - slvl;
dp = buffer[dst + j];
da = dp & ALPHA_MASK;
dp &= RGB_MASK;
buffer[dst + j] = (keepAlpha ? da : sa)
+ ((((dp & RED_MASK) * dlvl) >> 8) & RED_MASK)
+ ((((dp & GREEN_MASK) * dlvl) >> 8) & GREEN_MASK)
+ ((((dp & BLUE_MASK) * dlvl) >> 8) & BLUE_MASK)
+ ((((sp & RED_MASK) * slvl) >> 8) & RED_MASK)
+ ((((sp & GREEN_MASK) * slvl) >> 8) & GREEN_MASK)
+ ((((sp & BLUE_MASK) * slvl) >> 8) & BLUE_MASK);
}
offset += scansize;
dst += bw;
}
}
/** Puts pixels blending using src alpha (dest alpha is ignored) unless src pixel == keyclr.
* if keepAlpha is true then dest alpha is preserved.
* if keepAlpha is false then src alpha is copied to dest.
*/
public void putPixelsBlendKeyClr(int[] px, int x, int y, int w, int h, int offset, boolean keepAlpha, int keyclr) {
//do clipping
int scansize = w;
int bw = getWidth();
int bh = getHeight();
if (y < 0) {
y *= -1;
h -= y;
if (h <= 0) {
return;
}
offset += y * scansize;
y = 0;
}
if (x < 0) {
x *= -1;
w -= x;
if (w <= 0) {
return;
}
offset += x;
x = 0;
}
if (x + w > bw) {
w = bw - x;
if (w <= 0) {
return;
}
}
if (y + h > bh) {
h = bh - y;
if (h <= 0) {
return;
}
}
int dst = y * bw + x;
int sp, slvl, dp, dlvl, sa, da;
for(int i=0;i<h;i++) {
for(int j=0;j<w;j++) {
sp = px[offset + j];
sa = sp & ALPHA_MASK;
sp &= RGB_MASK;
if (sp == keyclr) continue;
slvl = sa >>> 24;
if (slvl > 0) {
slvl++;
}
dlvl = 0x100 - slvl;
dp = buffer[dst + j];
da = dp & ALPHA_MASK;
dp &= RGB_MASK;
buffer[dst + j] = (keepAlpha ? da : sa)
+ ((((dp & RED_MASK) * dlvl) >> 8) & RED_MASK)
+ ((((dp & GREEN_MASK) * dlvl) >> 8) & GREEN_MASK)
+ ((((dp & BLUE_MASK) * dlvl) >> 8) & BLUE_MASK)
+ ((((sp & RED_MASK) * slvl) >> 8) & RED_MASK)
+ ((((sp & GREEN_MASK) * slvl) >> 8) & GREEN_MASK)
+ ((((sp & BLUE_MASK) * slvl) >> 8) & BLUE_MASK);
}
offset += scansize;
dst += bw;
}
}
/** Gets a rectangle of pixels (including alpha) */
public int[] getPixels(int x, int y, int w, int h) {
int ret[] = new int[w * h];
int scansize = w;
int offset = 0;
int bw = getWidth();
int bh = getHeight();
if (y < 0) {
y *= -1;
h -= y;
if (h <= 0) {
return null;
}
offset += y * scansize;
y = 0;
}
if (x < 0) {
x *= -1;
w -= x;
if (w <= 0) {
return null;
}
offset += x;
x = 0;
}
if (x + w > bw) {
w = bw - x;
if (w <= 0) {
return null;
}
}
if (y + h > bh) {
h = bh - y;
if (h <= 0) {
return null;
}
}
int src = y * bw + x;
if (w == bw && scansize == w) {
System.arraycopy(buffer, src, ret, offset, w * h);
} else {
for(int i=0;i<h;i++) {
System.arraycopy(buffer, src, ret, offset, w);
offset += scansize;
src += bw;
}
}
return ret;
}
/** Returns a copy of the buffer */
public int[] getPixels() {
int px[] = new int[buffer.length];
System.arraycopy(buffer, 0, px, 0, buffer.length);
return px;
}
/** Returns the data buffer */
public int[] getBuffer() {
return buffer;
}
/** Clears the image to black with opaque alpha. */
public void clear() {
fill(0,0,getWidth(),getHeight(),0);
}
/** Fills a rectangle with clr (assumes OPAQUE alpha) */
public void fill(int x, int y, int w, int h, int clr) {
fill(x,y,w,h,clr,false);
}
/** Fills a rectangle with clr */
public void fill(int x, int y, int w, int h, int clr, boolean hasAlpha) {
if (!hasAlpha) clr |= OPAQUE;
int scansize = w;
int bw = getWidth();
int bh = getHeight();
if (y < 0) {
y *= -1;
h -= y;
if (h <= 0) {
return;
}
y = 0;
}
if (x < 0) {
x *= -1;
w -= x;
if (w <= 0) {
return;
}
x = 0;
}
if (x + w > bw) {
w = bw - x;
if (w <= 0) {
return;
}
}
if (y + h > bh) {
h = bh - y;
if (h <= 0) {
return;
}
}
int dst = y * bw + x;
if (w == bw && scansize == w) {
Arrays.fill(buffer, dst, dst + w * h, clr);
} else {
for(int i=0;i<h;i++) {
Arrays.fill(buffer, dst, dst + w, clr);
dst += bw;
}
}
}
/** Fills the alpha channel with lvl (doesn't touch rgb values) */
public void fillAlpha(int x, int y, int w, int h, int lvl) {
lvl <<= 24;
int scansize = w;
int bw = getWidth();
int bh = getHeight();
if (y < 0) {
y *= -1;
h -= y;
if (h <= 0) {
return;
}
y = 0;
}
if (x < 0) {
x *= -1;
w -= x;
if (w <= 0) {
return;
}
x = 0;
}
if (x + w > bw) {
w = bw - x;
if (w <= 0) {
return;
}
}
if (y + h > bh) {
h = bh - y;
if (h <= 0) {
return;
}
}
int dst = y * bw + x;
if (w == bw && scansize == w) {
int cnt = w * h;
for(int i=0;i<cnt;i++) {
buffer[dst] = (buffer[dst] & RGB_MASK) | lvl;
dst++;
}
} else {
int odst;
for(int i=0;i<h;i++) {
odst = dst;
for(int j=0;j<w;j++) {
buffer[dst] = (buffer[dst] & RGB_MASK) | lvl;
dst++;
}
dst = odst + bw;
}
}
}
/** Fills alpha channel with lvl ONLY where dest pixel == keyClr */
public void fillAlphaKeyClr(int x, int y, int w, int h, int lvl, int keyClr) {
lvl <<= 24;
//do clipping
int scansize = w;
int bw = getWidth();
int bh = getHeight();
if (y < 0) {
y *= -1;
h -= y;
if (h <= 0) {
return;
}
y = 0;
}
if (x < 0) {
x *= -1;
w -= x;
if (w <= 0) {
return;
}
x = 0;
}
if (x + w > bw) {
w = bw - x;
if (w <= 0) {
return;
}
}
if (y + h > bh) {
h = bh - y;
if (h <= 0) {
return;
}
}
int dst = y * bw + x;
if (w == bw && scansize == w) {