-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathPImage.java
More file actions
3438 lines (2971 loc) · 106 KB
/
PImage.java
File metadata and controls
3438 lines (2971 loc) · 106 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
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2004-14 Ben Fry and Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
package processing.core;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.util.Iterator;
import javax.imageio.*;
import javax.imageio.metadata.*;
/**
* ( begin auto-generated from PImage.xml )
*
* Datatype for storing images. Processing can display <b>.gif</b>,
* <b>.jpg</b>, <b>.tga</b>, and <b>.png</b> images. Images may be
* displayed in 2D and 3D space. Before an image is used, it must be loaded
* with the <b>loadImage()</b> function. The <b>PImage</b> class contains
* fields for the <b>width</b> and <b>height</b> of the image, as well as
* an array called <b>pixels[]</b> that contains the values for every pixel
* in the image. The methods described below allow easy access to the
* image's pixels and alpha channel and simplify the process of compositing.<br/>
* <br/> using the <b>pixels[]</b> array, be sure to use the
* <b>loadPixels()</b> method on the image to make sure that the pixel data
* is properly loaded.<br/>
* <br/> create a new image, use the <b>createImage()</b> function. Do not
* use the syntax <b>new PImage()</b>.
*
* ( end auto-generated )
*
* @webref image
* @usage Web & Application
* @instanceName pimg any object of type PImage
* @see PApplet#loadImage(String)
* @see PApplet#imageMode(int)
* @see PApplet#createImage(int, int, int)
*/
public class PImage implements PConstants, Cloneable {
/**
* Format for this image, one of RGB, ARGB or ALPHA.
* note that RGB images still require 0xff in the high byte
* because of how they'll be manipulated by other functions
*/
public int format;
/**
* ( begin auto-generated from pixels.xml )
*
* Array containing the values for all the pixels in the display window.
* These values are of the color datatype. This array is the size of the
* display window. For example, if the image is 100x100 pixels, there will
* be 10000 values and if the window is 200x300 pixels, there will be 60000
* values. The <b>index</b> value defines the position of a value within
* the array. For example, the statement <b>color b = pixels[230]</b> will
* set the variable <b>b</b> to be equal to the value at that location in
* the array.<br />
* <br />
* Before accessing this array, the data must loaded with the
* <b>loadPixels()</b> function. After the array data has been modified,
* the <b>updatePixels()</b> function must be run to update the changes.
* Without <b>loadPixels()</b>, running the code may (or will in future
* releases) result in a NullPointerException.
*
* ( end auto-generated )
*
* @webref image:pixels
* @usage web_application
* @brief Array containing the color of every pixel in the image
*/
public int[] pixels;
/** 1 for most images, 2 for hi-dpi/retina */
public int pixelDensity = 1;
/** Actual dimensions of pixels array, taking into account the 2x setting. */
public int pixelWidth;
public int pixelHeight;
/**
* ( begin auto-generated from PImage_width.xml )
*
* The width of the image in units of pixels.
*
* ( end auto-generated )
* @webref pimage:field
* @usage web_application
* @brief Image width
*/
public int width;
/**
* ( begin auto-generated from PImage_height.xml )
*
* The height of the image in units of pixels.
*
* ( end auto-generated )
* @webref pimage:field
* @usage web_application
* @brief Image height
*/
public int height;
/**
* Path to parent object that will be used with save().
* This prevents users from needing savePath() to use PImage.save().
*/
public PApplet parent;
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
/** modified portion of the image */
protected boolean modified;
protected int mx1, my1, mx2, my2;
/** Loaded pixels flag */
public boolean loaded = false;
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
// private fields
private int fracU, ifU, fracV, ifV, u1, u2, v1, v2, sX, sY, iw, iw1, ih1;
private int ul, ll, ur, lr, cUL, cLL, cUR, cLR;
private int srcXOffset, srcYOffset;
private int r, g, b, a;
private int[] srcBuffer;
// fixed point precision is limited to 15 bits!!
static final int PRECISIONB = 15;
static final int PRECISIONF = 1 << PRECISIONB;
static final int PREC_MAXVAL = PRECISIONF-1;
static final int PREC_ALPHA_SHIFT = 24-PRECISIONB;
static final int PREC_RED_SHIFT = 16-PRECISIONB;
// internal kernel stuff for the gaussian blur filter
private int blurRadius;
private int blurKernelSize;
private int[] blurKernel;
private int[][] blurMult;
// colour component bitmasks (moved from PConstants in 2.0b7)
public static final int ALPHA_MASK = 0xff000000;
public static final int RED_MASK = 0x00ff0000;
public static final int GREEN_MASK = 0x0000ff00;
public static final int BLUE_MASK = 0x000000ff;
//////////////////////////////////////////////////////////////
/**
* ( begin auto-generated from PImage.xml )
*
* Datatype for storing images. Processing can display <b>.gif</b>,
* <b>.jpg</b>, <b>.tga</b>, and <b>.png</b> images. Images may be
* displayed in 2D and 3D space. Before an image is used, it must be loaded
* with the <b>loadImage()</b> function. The <b>PImage</b> object contains
* fields for the <b>width</b> and <b>height</b> of the image, as well as
* an array called <b>pixels[]</b> which contains the values for every
* pixel in the image. A group of methods, described below, allow easy
* access to the image's pixels and alpha channel and simplify the process
* of compositing.
* <br/> <br/>
* Before using the <b>pixels[]</b> array, be sure to use the
* <b>loadPixels()</b> method on the image to make sure that the pixel data
* is properly loaded.
* <br/> <br/>
* To create a new image, use the <b>createImage()</b> function (do not use
* <b>new PImage()</b>).
* ( end auto-generated )
* @nowebref
* @usage web_application
* @see PApplet#loadImage(String, String)
* @see PApplet#imageMode(int)
* @see PApplet#createImage(int, int, int)
*/
public PImage() {
format = ARGB; // default to ARGB images for release 0116
pixelDensity = 1;
}
/**
* @nowebref
* @param width image width
* @param height image height
*/
public PImage(int width, int height) {
init(width, height, RGB, 1);
// toxi: is it maybe better to init the image with max alpha enabled?
//for(int i=0; i<pixels.length; i++) pixels[i]=0xffffffff;
// fry: i'm opting for the full transparent image, which is how
// photoshop works, and our audience oughta be familiar with.
// also, i want to avoid having to set all those pixels since
// in java it's super slow, and most using this fxn will be
// setting all the pixels anyway.
// toxi: agreed and same reasons why i left it out ;)
}
/**
* @nowebref
* @param format Either RGB, ARGB, ALPHA (grayscale alpha channel)
*/
public PImage(int width, int height, int format) {
init(width, height, format, 1);
}
public PImage(int width, int height, int format, int factor) {
init(width, height, format, factor);
}
/**
* Do not remove, see notes in the other variant.
*/
public void init(int width, int height, int format) { // ignore
init(width, height, format, 1);
}
/**
* Function to be used by subclasses of PImage to init later than
* at the constructor, or re-init later when things changes.
* Used by Capture and Movie classes (and perhaps others),
* because the width/height will not be known when super() is called.
* (Leave this public so that other libraries can do the same.)
*/
public void init(int width, int height, int format, int factor) { // ignore
this.width = width;
this.height = height;
this.format = format;
this.pixelDensity = factor;
pixelWidth = width * pixelDensity;
pixelHeight = height * pixelDensity;
this.pixels = new int[pixelWidth * pixelHeight];
}
/**
* Check the alpha on an image, using a really primitive loop.
*/
protected void checkAlpha() {
if (pixels == null) return;
for (int i = 0; i < pixels.length; i++) {
// since transparency is often at corners, hopefully this
// will find a non-transparent pixel quickly and exit
if ((pixels[i] & 0xff000000) != 0xff000000) {
format = ARGB;
break;
}
}
}
//////////////////////////////////////////////////////////////
/**
* Construct a new PImage from a java.awt.Image. This constructor assumes
* that you've done the work of making sure a MediaTracker has been used
* to fully download the data and that the img is valid.
*
* @nowebref
* @param img assumes a MediaTracker has been used to fully download
* the data and the img is valid
*/
public PImage(Image img) {
format = RGB;
if (img instanceof BufferedImage) {
BufferedImage bi = (BufferedImage) img;
width = bi.getWidth();
height = bi.getHeight();
int type = bi.getType();
if (type == BufferedImage.TYPE_3BYTE_BGR ||
type == BufferedImage.TYPE_4BYTE_ABGR) {
pixels = new int[width * height];
bi.getRGB(0, 0, width, height, pixels, 0, width);
if (type == BufferedImage.TYPE_4BYTE_ABGR) {
format = ARGB;
} else {
opaque();
}
} else {
DataBuffer db = bi.getRaster().getDataBuffer();
if (db instanceof DataBufferInt) {
pixels = ((DataBufferInt) db).getData();
if (type == BufferedImage.TYPE_INT_ARGB) {
format = ARGB;
} else if (type == BufferedImage.TYPE_INT_RGB) {
opaque();
}
}
}
}
// Implements fall-through if not DataBufferInt above, or not a
// known type, or not DataBufferInt for the data itself.
if (pixels == null) { // go the old school Java 1.0 route
width = img.getWidth(null);
height = img.getHeight(null);
pixels = new int[width * height];
PixelGrabber pg =
new PixelGrabber(img, 0, 0, width, height, pixels, 0, width);
try {
pg.grabPixels();
} catch (InterruptedException e) { }
}
pixelDensity = 1;
pixelWidth = width;
pixelHeight = height;
}
/**
* Use the getNative() method instead, which allows library interfaces to be
* written in a cross-platform fashion for desktop, Android, and others.
* This is still included for PGraphics objects, which may need the image.
*/
public Image getImage() { // ignore
return (Image) getNative();
}
/**
* Returns a native BufferedImage from this PImage.
*/
public Object getNative() { // ignore
loadPixels();
int type = (format == RGB) ?
BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage image = new BufferedImage(pixelWidth, pixelHeight, type);
WritableRaster wr = image.getRaster();
wr.setDataElements(0, 0, pixelWidth, pixelHeight, pixels);
return image;
}
//////////////////////////////////////////////////////////////
// MARKING IMAGE AS MODIFIED / FOR USE w/ GET/SET
public boolean isModified() { // ignore
return modified;
}
public void setModified() { // ignore
modified = true;
mx1 = 0;
my1 = 0;
mx2 = pixelWidth;
my2 = pixelHeight;
}
public void setModified(boolean m) { // ignore
modified = m;
}
public int getModifiedX1() { // ignore
return mx1;
}
public int getModifiedX2() { // ignore
return mx2;
}
public int getModifiedY1() { // ignore
return my1;
}
public int getModifiedY2() { // ignore
return my2;
}
/**
* ( begin auto-generated from PImage_loadPixels.xml )
*
* Loads the pixel data for the image into its <b>pixels[]</b> array. This
* function must always be called before reading from or writing to <b>pixels[]</b>.
* <br/><br/> renderers may or may not seem to require <b>loadPixels()</b>
* or <b>updatePixels()</b>. However, the rule is that any time you want to
* manipulate the <b>pixels[]</b> array, you must first call
* <b>loadPixels()</b>, and after changes have been made, call
* <b>updatePixels()</b>. Even if the renderer may not seem to use this
* function in the current Processing release, this will always be subject
* to change.
*
* ( end auto-generated )
*
* <h3>Advanced</h3>
* Call this when you want to mess with the pixels[] array.
* <p/>
* For subclasses where the pixels[] buffer isn't set by default,
* this should copy all data into the pixels[] array
*
* @webref pimage:pixels
* @brief Loads the pixel data for the image into its pixels[] array
* @usage web_application
*/
public void loadPixels() { // ignore
if (pixels == null || pixels.length != pixelWidth*pixelHeight) {
pixels = new int[pixelWidth*pixelHeight];
}
setLoaded();
}
public void updatePixels() { // ignore
updatePixels(0, 0, pixelWidth, pixelHeight);
}
/**
* ( begin auto-generated from PImage_updatePixels.xml )
*
* Updates the image with the data in its <b>pixels[]</b> array. Use in
* conjunction with <b>loadPixels()</b>. If you're only reading pixels from
* the array, there's no need to call <b>updatePixels()</b>.
* <br/><br/> renderers may or may not seem to require <b>loadPixels()</b>
* or <b>updatePixels()</b>. However, the rule is that any time you want to
* manipulate the <b>pixels[]</b> array, you must first call
* <b>loadPixels()</b>, and after changes have been made, call
* <b>updatePixels()</b>. Even if the renderer may not seem to use this
* function in the current Processing release, this will always be subject
* to change.
* <br/> <br/>
* Currently, none of the renderers use the additional parameters to
* <b>updatePixels()</b>, however this may be implemented in the future.
*
* ( end auto-generated )
* <h3>Advanced</h3>
* Mark the pixels in this region as needing an update.
* This is not currently used by any of the renderers, however the api
* is structured this way in the hope of being able to use this to
* speed things up in the future.
* @webref pimage:pixels
* @brief Updates the image with the data in its pixels[] array
* @usage web_application
* @param x x-coordinate of the upper-left corner
* @param y y-coordinate of the upper-left corner
* @param w width
* @param h height
*/
public void updatePixels(int x, int y, int w, int h) { // ignore
int x2 = x + w;
int y2 = y + h;
if (!modified) {
mx1 = PApplet.max(0, x);
mx2 = PApplet.min(pixelWidth, x2);
my1 = PApplet.max(0, y);
my2 = PApplet.min(pixelHeight, y2);
modified = true;
} else {
if (x < mx1) mx1 = PApplet.max(0, x);
if (x > mx2) mx2 = PApplet.min(pixelWidth, x);
if (y < my1) my1 = PApplet.max(0, y);
if (y > my2) my2 = PApplet.min(pixelHeight, y);
if (x2 < mx1) mx1 = PApplet.max(0, x2);
if (x2 > mx2) mx2 = PApplet.min(pixelWidth, x2);
if (y2 < my1) my1 = PApplet.max(0, y2);
if (y2 > my2) my2 = PApplet.min(pixelHeight, y2);
}
}
//////////////////////////////////////////////////////////////
// COPYING IMAGE DATA
/**
* Duplicate an image, returns new PImage object.
* The pixels[] array for the new object will be unique
* and recopied from the source image. This is implemented as an
* override of Object.clone(). We recommend using get() instead,
* because it prevents you from needing to catch the
* CloneNotSupportedException, and from doing a cast from the result.
*/
@Override
public Object clone() throws CloneNotSupportedException { // ignore
return get();
}
/**
* ( begin auto-generated from PImage_resize.xml )
*
* Resize the image to a new width and height. To make the image scale
* proportionally, use 0 as the value for the <b>wide</b> or <b>high</b>
* parameter. For instance, to make the width of an image 150 pixels, and
* change the height using the same proportion, use resize(150, 0).<br />
* <br />
* Even though a PGraphics is technically a PImage, it is not possible to
* rescale the image data found in a PGraphics. (It's simply not possible
* to do this consistently across renderers: technically infeasible with
* P3D, or what would it even do with PDF?) If you want to resize PGraphics
* content, first get a copy of its image data using the <b>get()</b>
* method, and call <b>resize()</b> on the PImage that is returned.
*
* ( end auto-generated )
* @webref pimage:method
* @brief Changes the size of an image to a new width and height
* @usage web_application
* @param w the resized image width
* @param h the resized image height
* @see PImage#get(int, int, int, int)
*/
public void resize(int w, int h) { // ignore
if (w <= 0 && h <= 0) {
throw new IllegalArgumentException("width or height must be > 0 for resize");
}
if (w == 0) { // Use height to determine relative size
float diff = (float) h / (float) height;
w = (int) (width * diff);
} else if (h == 0) { // Use the width to determine relative size
float diff = (float) w / (float) width;
h = (int) (height * diff);
}
BufferedImage img =
shrinkImage((BufferedImage) getNative(), w*pixelDensity, h*pixelDensity);
PImage temp = new PImage(img);
this.pixelWidth = temp.width;
this.pixelHeight = temp.height;
// Get the resized pixel array
this.pixels = temp.pixels;
this.width = pixelWidth / pixelDensity;
this.height = pixelHeight / pixelDensity;
// Mark the pixels array as altered
updatePixels();
}
// Adapted from getFasterScaledInstance() method from page 111 of
// "Filthy Rich Clients" by Chet Haase and Romain Guy
// Additional modifications and simplifications have been added,
// plus a fix to deal with an infinite loop if images are expanded.
// http://code.google.com/p/processing/issues/detail?id=1463
static private BufferedImage shrinkImage(BufferedImage img,
int targetWidth, int targetHeight) {
int type = (img.getTransparency() == Transparency.OPAQUE) ?
BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage outgoing = img;
BufferedImage scratchImage = null;
Graphics2D g2 = null;
int prevW = outgoing.getWidth();
int prevH = outgoing.getHeight();
boolean isTranslucent = img.getTransparency() != Transparency.OPAQUE;
// Use multi-step technique: start with original size, then scale down in
// multiple passes with drawImage() until the target size is reached
int w = img.getWidth();
int h = img.getHeight();
do {
if (w > targetWidth) {
w /= 2;
// if this is the last step, do the exact size
if (w < targetWidth) {
w = targetWidth;
}
} else if (targetWidth >= w) {
w = targetWidth;
}
if (h > targetHeight) {
h /= 2;
if (h < targetHeight) {
h = targetHeight;
}
} else if (targetHeight >= h) {
h = targetHeight;
}
if (scratchImage == null || isTranslucent) {
// Use a single scratch buffer for all iterations and then copy
// to the final, correctly-sized image before returning
scratchImage = new BufferedImage(w, h, type);
g2 = scratchImage.createGraphics();
}
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(outgoing, 0, 0, w, h, 0, 0, prevW, prevH, null);
prevW = w;
prevH = h;
outgoing = scratchImage;
} while (w != targetWidth || h != targetHeight);
if (g2 != null) {
g2.dispose();
}
// If we used a scratch buffer that is larger than our target size,
// create an image of the right size and copy the results into it
if (targetWidth != outgoing.getWidth() ||
targetHeight != outgoing.getHeight()) {
scratchImage = new BufferedImage(targetWidth, targetHeight, type);
g2 = scratchImage.createGraphics();
g2.drawImage(outgoing, 0, 0, null);
g2.dispose();
outgoing = scratchImage;
}
return outgoing;
}
//////////////////////////////////////////////////////////////
// MARKING IMAGE AS LOADED / FOR USE IN RENDERERS
public boolean isLoaded() { // ignore
return loaded;
}
public void setLoaded() { // ignore
loaded = true;
}
public void setLoaded(boolean l) { // ignore
loaded = l;
}
//////////////////////////////////////////////////////////////
// GET/SET PIXELS
/**
* ( begin auto-generated from PImage_get.xml )
*
* Reads the color of any pixel or grabs a section of an image. If no
* parameters are specified, the entire image is returned. Use the <b>x</b>
* and <b>y</b> parameters to get the value of one pixel. Get a section of
* the display window by specifying an additional <b>width</b> and
* <b>height</b> parameter. When getting an image, the <b>x</b> and
* <b>y</b> parameters define the coordinates for the upper-left corner of
* the image, regardless of the current <b>imageMode()</b>.<br />
* <br />
* If the pixel requested is outside of the image window, black is
* returned. The numbers returned are scaled according to the current color
* ranges, but only RGB values are returned by this function. For example,
* even though you may have drawn a shape with <b>colorMode(HSB)</b>, the
* numbers returned will be in RGB format.<br />
* <br />
* Getting the color of a single pixel with <b>get(x, y)</b> is easy, but
* not as fast as grabbing the data directly from <b>pixels[]</b>. The
* equivalent statement to <b>get(x, y)</b> using <b>pixels[]</b> is
* <b>pixels[y*width+x]</b>. See the reference for <b>pixels[]</b> for more information.
*
* ( end auto-generated )
*
* <h3>Advanced</h3>
* Returns an ARGB "color" type (a packed 32 bit int with the color.
* If the coordinate is outside the image, zero is returned
* (black, but completely transparent).
* <P>
* If the image is in RGB format (i.e. on a PVideo object),
* the value will get its high bits set, just to avoid cases where
* they haven't been set already.
* <P>
* If the image is in ALPHA format, this returns a white with its
* alpha value set.
* <P>
* This function is included primarily for beginners. It is quite
* slow because it has to check to see if the x, y that was provided
* is inside the bounds, and then has to check to see what image
* type it is. If you want things to be more efficient, access the
* pixels[] array directly.
*
* @webref image:pixels
* @brief Reads the color of any pixel or grabs a rectangle of pixels
* @usage web_application
* @param x x-coordinate of the pixel
* @param y y-coordinate of the pixel
* @see PApplet#set(int, int, int)
* @see PApplet#pixels
* @see PApplet#copy(PImage, int, int, int, int, int, int, int, int)
*/
public int get(int x, int y) {
if ((x < 0) || (y < 0) || (x >= pixelWidth) || (y >= pixelHeight)) return 0;
switch (format) {
case RGB:
return pixels[y*pixelWidth + x] | 0xff000000;
case ARGB:
return pixels[y*pixelWidth + x];
case ALPHA:
return (pixels[y*pixelWidth + x] << 24) | 0xffffff;
}
return 0;
}
/**
* @param w width of pixel rectangle to get
* @param h height of pixel rectangle to get
*/
public PImage get(int x, int y, int w, int h) {
int targetX = 0;
int targetY = 0;
int targetWidth = w;
int targetHeight = h;
boolean cropped = false;
if (x < 0) {
w += x; // x is negative, removes the left edge from the width
targetX = -x;
cropped = true;
x = 0;
}
if (y < 0) {
h += y; // y is negative, clip the number of rows
targetY = -y;
cropped = true;
y = 0;
}
if (x + w > pixelWidth) {
w = pixelWidth - x;
cropped = true;
}
if (y + h > pixelHeight) {
h = pixelHeight - y;
cropped = true;
}
if (w < 0) {
w = 0;
}
if (h < 0) {
h = 0;
}
int targetFormat = format;
if (cropped && format == RGB) {
targetFormat = ARGB;
}
PImage target = new PImage(targetWidth / pixelDensity,
targetHeight / pixelDensity,
targetFormat, pixelDensity);
target.parent = parent; // parent may be null so can't use createImage()
if (w > 0 && h > 0) {
getImpl(x, y, w, h, target, targetX, targetY);
}
return target;
}
/**
* Returns a copy of this PImage. Equivalent to get(0, 0, width, height).
* Deprecated, just use copy() instead.
*/
public PImage get() {
// Formerly this used clone(), which caused memory problems.
// http://code.google.com/p/processing/issues/detail?id=42
return get(0, 0, pixelWidth, pixelHeight);
}
public PImage copy() {
return get(0, 0, pixelWidth, pixelHeight);
}
/**
* Internal function to actually handle getting a block of pixels that
* has already been properly cropped to a valid region. That is, x/y/w/h
* are guaranteed to be inside the image space, so the implementation can
* use the fastest possible pixel copying method.
*/
protected void getImpl(int sourceX, int sourceY,
int sourceWidth, int sourceHeight,
PImage target, int targetX, int targetY) {
int sourceIndex = sourceY*pixelWidth + sourceX;
int targetIndex = targetY*target.pixelWidth + targetX;
for (int row = 0; row < sourceHeight; row++) {
System.arraycopy(pixels, sourceIndex, target.pixels, targetIndex, sourceWidth);
sourceIndex += pixelWidth;
targetIndex += target.pixelWidth;
}
}
/**
* ( begin auto-generated from PImage_set.xml )
*
* Changes the color of any pixel or writes an image directly into the
* display window.<br />
* <br />
* The <b>x</b> and <b>y</b> parameters specify the pixel to change and the
* <b>color</b> parameter specifies the color value. The color parameter is
* affected by the current color mode (the default is RGB values from 0 to
* 255). When setting an image, the <b>x</b> and <b>y</b> parameters define
* the coordinates for the upper-left corner of the image, regardless of
* the current <b>imageMode()</b>.
* <br /><br />
* Setting the color of a single pixel with <b>set(x, y)</b> is easy, but
* not as fast as putting the data directly into <b>pixels[]</b>. The
* equivalent statement to <b>set(x, y, #000000)</b> using <b>pixels[]</b>
* is <b>pixels[y*width+x] = #000000</b>. See the reference for
* <b>pixels[]</b> for more information.
*
* ( end auto-generated )
*
* @webref image:pixels
* @brief writes a color to any pixel or writes an image into another
* @usage web_application
* @param x x-coordinate of the pixel
* @param y y-coordinate of the pixel
* @param c any value of the color datatype
* @see PImage#get(int, int, int, int)
* @see PImage#pixels
* @see PImage#copy(PImage, int, int, int, int, int, int, int, int)
*/
public void set(int x, int y, int c) {
if ((x < 0) || (y < 0) || (x >= pixelWidth) || (y >= pixelHeight)) return;
pixels[y*pixelWidth + x] = c;
updatePixels(x, y, 1, 1); // slow...
}
/**
* <h3>Advanced</h3>
* Efficient method of drawing an image's pixels directly to this surface.
* No variations are employed, meaning that any scale, tint, or imageMode
* settings will be ignored.
*
* @param img image to copy into the original image
*/
public void set(int x, int y, PImage img) {
int sx = 0;
int sy = 0;
int sw = img.pixelWidth;
int sh = img.pixelHeight;
if (x < 0) { // off left edge
sx -= x;
sw += x;
x = 0;
}
if (y < 0) { // off top edge
sy -= y;
sh += y;
y = 0;
}
if (x + sw > pixelWidth) { // off right edge
sw = pixelWidth - x;
}
if (y + sh > pixelHeight) { // off bottom edge
sh = pixelHeight - y;
}
// this could be nonexistent
if ((sw <= 0) || (sh <= 0)) return;
setImpl(img, sx, sy, sw, sh, x, y);
}
/**
* Internal function to actually handle setting a block of pixels that
* has already been properly cropped from the image to a valid region.
*/
protected void setImpl(PImage sourceImage,
int sourceX, int sourceY,
int sourceWidth, int sourceHeight,
int targetX, int targetY) {
int sourceOffset = sourceY * sourceImage.pixelWidth + sourceX;
int targetOffset = targetY * pixelWidth + targetX;
for (int y = sourceY; y < sourceY + sourceHeight; y++) {
System.arraycopy(sourceImage.pixels, sourceOffset, pixels, targetOffset, sourceWidth);
sourceOffset += sourceImage.pixelWidth;
targetOffset += pixelWidth;
}
//updatePixelsImpl(targetX, targetY, sourceWidth, sourceHeight);
updatePixels(targetX, targetY, sourceWidth, sourceHeight);
}
//////////////////////////////////////////////////////////////
// ALPHA CHANNEL
/**
* @param maskArray array of integers used as the alpha channel, needs to be
* the same length as the image's pixel array.
*/
public void mask(int[] maskArray) { // ignore
loadPixels();
// don't execute if mask image is different size
if (maskArray.length != pixels.length) {
throw new IllegalArgumentException("mask() can only be used with an image that's the same size.");
}
for (int i = 0; i < pixels.length; i++) {
pixels[i] = ((maskArray[i] & 0xff) << 24) | (pixels[i] & 0xffffff);
}
format = ARGB;
updatePixels();
}
/**
* ( begin auto-generated from PImage_mask.xml )
*
* Masks part of an image from displaying by loading another image and
* using it as an alpha channel. This mask image should only contain
* grayscale data, but only the blue color channel is used. The mask image
* needs to be the same size as the image to which it is applied.<br />
* <br />
* In addition to using a mask image, an integer array containing the alpha
* channel data can be specified directly. This method is useful for
* creating dynamically generated alpha masks. This array must be of the
* same length as the target image's pixels array and should contain only
* grayscale data of values between 0-255.
*
* ( end auto-generated )
*
* <h3>Advanced</h3>
*
* Set alpha channel for an image. Black colors in the source
* image will make the destination image completely transparent,
* and white will make things fully opaque. Gray values will
* be in-between steps.
* <P>
* Strictly speaking the "blue" value from the source image is
* used as the alpha color. For a fully grayscale image, this
* is correct, but for a color image it's not 100% accurate.
* For a more accurate conversion, first use filter(GRAY)
* which will make the image into a "correct" grayscale by
* performing a proper luminance-based conversion.
*
* @webref pimage:method
* @usage web_application
* @param img image to use as the mask
* @brief Masks part of an image with another image as an alpha channel
*/
public void mask(PImage img) {
img.loadPixels();
mask(img.pixels);
}
//////////////////////////////////////////////////////////////