-
-
Notifications
You must be signed in to change notification settings - Fork 417
/
BitmapData.hx
1663 lines (1259 loc) · 60.2 KB
/
BitmapData.hx
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 openfl.display; #if !flash #if (display || openfl_next || js)
import lime.graphics.opengl.GLBuffer;
import lime.graphics.opengl.GLTexture;
import lime.graphics.GLRenderContext;
import lime.graphics.Image;
import lime.graphics.ImageBuffer;
import lime.graphics.utils.ImageCanvasUtil;
import lime.math.ColorMatrix;
import lime.utils.Float32Array;
import lime.utils.UInt8Array;
import openfl._internal.renderer.RenderSession;
import openfl.errors.IOError;
import openfl.filters.BitmapFilter;
import openfl.geom.ColorTransform;
import openfl.geom.Matrix;
import openfl.geom.Point;
import openfl.geom.Rectangle;
import openfl.utils.ByteArray;
import openfl.Vector;
#if js
import js.html.CanvasElement;
import js.html.CanvasRenderingContext2D;
import js.html.ImageData;
import js.html.ImageElement;
import js.html.Uint8ClampedArray;
import js.Browser;
#end
@:access(lime.graphics.Image)
@:access(lime.graphics.ImageBuffer)
@:access(lime.math.Rectangle)
@:access(openfl.geom.ColorTransform)
@:access(openfl.geom.Point)
@:access(openfl.geom.Rectangle)
@:autoBuild(openfl.Assets.embedBitmap())
/**
* The BitmapData class lets you work with the data(pixels) of a Bitmap
* object. You can use the methods of the BitmapData class to create
* arbitrarily sized transparent or opaque bitmap images and manipulate them
* in various ways at runtime. You can also access the BitmapData for a bitmap
* image that you load with the <code>openfl.Assets</code> or
* <code>openfl.display.Loader</code> classes.
*
* <p>This class lets you separate bitmap rendering operations from the
* internal display updating routines of OpenFL. By manipulating a
* BitmapData object directly, you can create complex images without incurring
* the per-frame overhead of constantly redrawing the content from vector
* data.</p>
*
* <p>The methods of the BitmapData class support effects that are not
* available through the filters available to non-bitmap display objects.</p>
*
* <p>A BitmapData object contains an array of pixel data. This data can
* represent either a fully opaque bitmap or a transparent bitmap that
* contains alpha channel data. Either type of BitmapData object is stored as
* a buffer of 32-bit integers. Each 32-bit integer determines the properties
* of a single pixel in the bitmap.</p>
*
* <p>Each 32-bit integer is a combination of four 8-bit channel values(from
* 0 to 255) that describe the alpha transparency and the red, green, and blue
* (ARGB) values of the pixel.(For ARGB values, the most significant byte
* represents the alpha channel value, followed by red, green, and blue.)</p>
*
* <p>The four channels(alpha, red, green, and blue) are represented as
* numbers when you use them with the <code>BitmapData.copyChannel()</code>
* method or the <code>DisplacementMapFilter.componentX</code> and
* <code>DisplacementMapFilter.componentY</code> properties, and these numbers
* are represented by the following constants in the BitmapDataChannel
* class:</p>
*
* <ul>
* <li><code>BitmapDataChannel.ALPHA</code></li>
* <li><code>BitmapDataChannel.RED</code></li>
* <li><code>BitmapDataChannel.GREEN</code></li>
* <li><code>BitmapDataChannel.BLUE</code></li>
* </ul>
*
* <p>You can attach BitmapData objects to a Bitmap object by using the
* <code>bitmapData</code> property of the Bitmap object.</p>
*
* <p>You can use a BitmapData object to fill a Graphics object by using the
* <code>Graphics.beginBitmapFill()</code> method.</p>
*
* <p>You can also use a BitmapData object to perform batch tile rendering
* using the <code>openfl.display.Tilesheet</code> class.</p>
*
* <p>In Flash Player 10, the maximum size for a BitmapData object
* is 8,191 pixels in width or height, and the total number of pixels cannot
* exceed 16,777,215 pixels.(So, if a BitmapData object is 8,191 pixels wide,
* it can only be 2,048 pixels high.) In Flash Player 9 and earlier, the limitation
* is 2,880 pixels in height and 2,880 in width.</p>
*/
class BitmapData implements IBitmapDrawable {
/**
* The height of the bitmap image in pixels.
*/
public var height (default, null):Int;
/**
* The rectangle that defines the size and location of the bitmap image. The
* top and left of the rectangle are 0; the width and height are equal to the
* width and height in pixels of the BitmapData object.
*/
public var rect (default, null):Rectangle;
/**
* Defines whether the bitmap image supports per-pixel transparency. You can
* set this value only when you construct a BitmapData object by passing in
* <code>true</code> for the <code>transparent</code> parameter of the
* constructor. Then, after you create a BitmapData object, you can check
* whether it supports per-pixel transparency by determining if the value of
* the <code>transparent</code> property is <code>true</code>.
*/
public var transparent (default, null):Bool;
/**
* The width of the bitmap image in pixels.
*/
public var width (default, null):Int;
@:noCompletion @:dox(hide) public var __worldTransform:Matrix;
@:noCompletion private var __buffer:GLBuffer;
@:noCompletion private var __image:Image;
@:noCompletion private var __isValid:Bool;
@:noCompletion private var __texture:GLTexture;
@:noCompletion private var __textureImage:Image;
@:noCompletion private var __uvData:TextureUvs;
/**
* Creates a BitmapData object with a specified width and height. If you specify a value for
* the <code>fillColor</code> parameter, every pixel in the bitmap is set to that color.
*
* By default, the bitmap is created as transparent, unless you pass the value <code>false</code>
* for the transparent parameter. After you create an opaque bitmap, you cannot change it
* to a transparent bitmap. Every pixel in an opaque bitmap uses only 24 bits of color channel
* information. If you define the bitmap as transparent, every pixel uses 32 bits of color
* channel information, including an alpha transparency channel.
*
* @param width The width of the bitmap image in pixels.
* @param height The height of the bitmap image in pixels.
* @param transparent Specifies whether the bitmap image supports per-pixel transparency. The default value is <code>true</code>(transparent). To create a fully transparent bitmap, set the value of the <code>transparent</code> parameter to <code>true</code> and the value of the <code>fillColor</code> parameter to 0x00000000(or to 0). Setting the <code>transparent</code> property to <code>false</code> can result in minor improvements in rendering performance.
* @param fillColor A 32-bit ARGB color value that you use to fill the bitmap image area. The default value is 0xFFFFFFFF(solid white).
*/
public function new (width:Int, height:Int, transparent:Bool = true, fillColor:UInt = 0xFFFFFFFF) {
this.transparent = transparent;
if (width > 0 && height > 0) {
this.width = width;
this.height = height;
rect = new Rectangle (0, 0, width, height);
if (!transparent) {
fillColor = (0xFF << 24) | (fillColor & 0xFFFFFF);
}
__image = new Image (null, 0, 0, width, height, fillColor);
__image.transparent = transparent;
__isValid = true;
}
__createUVs ();
}
/**
* Takes a source image and a filter object and generates the filtered image.
*
* This method relies on the behavior of built-in filter objects, which determine the
* destination rectangle that is affected by an input source rectangle.
*
* After a filter is applied, the resulting image can be larger than the input image.
* For example, if you use a BlurFilter class to blur a source rectangle of(50,50,100,100)
* and a destination point of(10,10), the area that changes in the destination image is
* larger than(10,10,60,60) because of the blurring. This happens internally during the
* applyFilter() call.
*
* If the <code>sourceRect</code> parameter of the sourceBitmapData parameter is an
* interior region, such as(50,50,100,100) in a 200 x 200 image, the filter uses the source
* pixels outside the <code>sourceRect</code> parameter to generate the destination rectangle.
*
* If the BitmapData object and the object specified as the <code>sourceBitmapData</code>
* parameter are the same object, the application uses a temporary copy of the object to
* perform the filter. For best performance, avoid this situation.
*
* @param sourceBitmapData The input bitmap image to use. The source image can be a different BitmapData object or it can refer to the current BitmapData instance.
* @param sourceRect A rectangle that defines the area of the source image to use as input.
* @param destPoint The point within the destination image(the current BitmapData instance) that corresponds to the upper-left corner of the source rectangle.
* @param filter The filter object that you use to perform the filtering operation.
*/
public function applyFilter (sourceBitmapData:BitmapData, sourceRect:Rectangle, destPoint:Point, filter:BitmapFilter):Void {
if (!__isValid || sourceBitmapData == null || !sourceBitmapData.__isValid) return;
#if js
ImageCanvasUtil.convertToCanvas (__image);
ImageCanvasUtil.createImageData (__image);
ImageCanvasUtil.convertToCanvas (sourceBitmapData.__image);
ImageCanvasUtil.createImageData (sourceBitmapData.__image);
#end
#if js
filter.__applyFilter (__image.buffer.__srcImageData, sourceBitmapData.__image.buffer.__srcImageData, sourceRect, destPoint);
#end
__image.dirty = true;
}
/**
* Returns a new BitmapData object that is a clone of the original instance with an exact copy of the contained bitmap.
* @return A new BitmapData object that is identical to the original.
*/
public function clone ():BitmapData {
if (!__isValid) {
return new BitmapData (width, height, transparent);
} else {
return BitmapData.fromImage (__image.clone (), transparent);
}
}
/**
* Adjusts the color values in a specified area of a bitmap image by using a <code>ColorTransform</code>
* object. If the rectangle matches the boundaries of the bitmap image, this method transforms the color
* values of the entire image.
* @param rect A Rectangle object that defines the area of the image in which the ColorTransform object is applied.
* @param colorTransform A ColorTransform object that describes the color transformation values to apply.
*/
public function colorTransform (rect:Rectangle, colorTransform:ColorTransform):Void {
__image.colorTransform (rect.__toLimeRectangle (), colorTransform.__toLimeColorMatrix ());
}
/**
* Transfers data from one channel of another BitmapData object or the
* current BitmapData object into a channel of the current BitmapData object.
* All of the data in the other channels in the destination BitmapData object
* are preserved.
*
* <p>The source channel value and destination channel value can be one of
* following values: </p>
*
* <ul>
* <li><code>BitmapDataChannel.RED</code></li>
* <li><code>BitmapDataChannel.GREEN</code></li>
* <li><code>BitmapDataChannel.BLUE</code></li>
* <li><code>BitmapDataChannel.ALPHA</code></li>
* </ul>
*
* @param sourceBitmapData The input bitmap image to use. The source image
* can be a different BitmapData object or it can
* refer to the current BitmapData object.
* @param sourceRect The source Rectangle object. To copy only channel
* data from a smaller area within the bitmap,
* specify a source rectangle that is smaller than
* the overall size of the BitmapData object.
* @param destPoint The destination Point object that represents the
* upper-left corner of the rectangular area where
* the new channel data is placed. To copy only
* channel data from one area to a different area in
* the destination image, specify a point other than
* (0,0).
* @param sourceChannel The source channel. Use a value from the
* BitmapDataChannel class
* (<code>BitmapDataChannel.RED</code>,
* <code>BitmapDataChannel.BLUE</code>,
* <code>BitmapDataChannel.GREEN</code>,
* <code>BitmapDataChannel.ALPHA</code>).
* @param destChannel The destination channel. Use a value from the
* BitmapDataChannel class
* (<code>BitmapDataChannel.RED</code>,
* <code>BitmapDataChannel.BLUE</code>,
* <code>BitmapDataChannel.GREEN</code>,
* <code>BitmapDataChannel.ALPHA</code>).
* @throws TypeError The sourceBitmapData, sourceRect or destPoint are null.
*/
public function copyChannel (sourceBitmapData:BitmapData, sourceRect:Rectangle, destPoint:Point, sourceChannel:Int, destChannel:Int):Void {
if (!__isValid) return;
var sourceChannel = switch (sourceChannel) {
case 1: ImageChannel.RED;
case 2: ImageChannel.GREEN;
case 4: ImageChannel.BLUE;
case 8: ImageChannel.ALPHA;
default: return;
}
var destChannel = switch (destChannel) {
case 1: ImageChannel.RED;
case 2: ImageChannel.GREEN;
case 4: ImageChannel.BLUE;
case 8: ImageChannel.ALPHA;
default: return;
}
__image.copyChannel (sourceBitmapData.__image, sourceRect.__toLimeRectangle (), destPoint.__toLimeVector2 (), sourceChannel, destChannel);
}
/**
* Provides a fast routine to perform pixel manipulation between images with
* no stretching, rotation, or color effects. This method copies a
* rectangular area of a source image to a rectangular area of the same size
* at the destination point of the destination BitmapData object.
*
* <p>If you include the <code>alphaBitmap</code> and <code>alphaPoint</code>
* parameters, you can use a secondary image as an alpha source for the
* source image. If the source image has alpha data, both sets of alpha data
* are used to composite pixels from the source image to the destination
* image. The <code>alphaPoint</code> parameter is the point in the alpha
* image that corresponds to the upper-left corner of the source rectangle.
* Any pixels outside the intersection of the source image and alpha image
* are not copied to the destination image.</p>
*
* <p>The <code>mergeAlpha</code> property controls whether or not the alpha
* channel is used when a transparent image is copied onto another
* transparent image. To copy pixels with the alpha channel data, set the
* <code>mergeAlpha</code> property to <code>true</code>. By default, the
* <code>mergeAlpha</code> property is <code>false</code>.</p>
*
* @param sourceBitmapData The input bitmap image from which to copy pixels.
* The source image can be a different BitmapData
* instance, or it can refer to the current
* BitmapData instance.
* @param sourceRect A rectangle that defines the area of the source
* image to use as input.
* @param destPoint The destination point that represents the
* upper-left corner of the rectangular area where
* the new pixels are placed.
* @param alphaBitmapData A secondary, alpha BitmapData object source.
* @param alphaPoint The point in the alpha BitmapData object source
* that corresponds to the upper-left corner of the
* <code>sourceRect</code> parameter.
* @param mergeAlpha To use the alpha channel, set the value to
* <code>true</code>. To copy pixels with no alpha
* channel, set the value to <code>false</code>.
* @throws TypeError The sourceBitmapData, sourceRect, destPoint are null.
*/
public function copyPixels (sourceBitmapData:BitmapData, sourceRect:Rectangle, destPoint:Point, alphaBitmapData:BitmapData = null, alphaPoint:Point = null, mergeAlpha:Bool = false):Void {
if (!__isValid || sourceBitmapData == null) return;
__image.copyPixels (sourceBitmapData.__image, sourceRect.__toLimeRectangle (), destPoint.__toLimeVector2 (), alphaBitmapData != null ? alphaBitmapData.__image : null, alphaPoint != null ? alphaPoint.__toLimeVector2 () : null, mergeAlpha);
}
/**
* Frees memory that is used to store the BitmapData object.
*
* <p>When the <code>dispose()</code> method is called on an image, the width
* and height of the image are set to 0. All subsequent calls to methods or
* properties of this BitmapData instance fail, and an exception is thrown.
* </p>
*
* <p><code>BitmapData.dispose()</code> releases the memory occupied by the
* actual bitmap data, immediately(a bitmap can consume up to 64 MB of
* memory). After using <code>BitmapData.dispose()</code>, the BitmapData
* object is no longer usable and an exception may be thrown if
* you call functions on the BitmapData object. However,
* <code>BitmapData.dispose()</code> does not garbage collect the BitmapData
* object(approximately 128 bytes); the memory occupied by the actual
* BitmapData object is released at the time the BitmapData object is
* collected by the garbage collector.</p>
*
*/
public function dispose ():Void {
__image = null;
width = 0;
height = 0;
rect = null;
__isValid = false;
}
/**
* Draws the <code>source</code> display object onto the bitmap image, using
* the NME software renderer. You can specify <code>matrix</code>,
* <code>colorTransform</code>, <code>blendMode</code>, and a destination
* <code>clipRect</code> parameter to control how the rendering performs.
* Optionally, you can specify whether the bitmap should be smoothed when
* scaled(this works only if the source object is a BitmapData object).
*
* <p>The source display object does not use any of its applied
* transformations for this call. It is treated as it exists in the library
* or file, with no matrix transform, no color transform, and no blend mode.
* To draw a display object(such as a movie clip) by using its own transform
* properties, you can copy its <code>transform</code> property object to the
* <code>transform</code> property of the Bitmap object that uses the
* BitmapData object.</p>
*
* @param source The display object or BitmapData object to draw to
* the BitmapData object.(The DisplayObject and
* BitmapData classes implement the IBitmapDrawable
* interface.)
* @param matrix A Matrix object used to scale, rotate, or translate
* the coordinates of the bitmap. If you do not want to
* apply a matrix transformation to the image, set this
* parameter to an identity matrix, created with the
* default <code>new Matrix()</code> constructor, or
* pass a <code>null</code> value.
* @param colorTransform A ColorTransform object that you use to adjust the
* color values of the bitmap. If no object is
* supplied, the bitmap image's colors are not
* transformed. If you must pass this parameter but you
* do not want to transform the image, set this
* parameter to a ColorTransform object created with
* the default <code>new ColorTransform()</code>
* constructor.
* @param blendMode A string value, from the openfl.display.BlendMode
* class, specifying the blend mode to be applied to
* the resulting bitmap.
* @param clipRect A Rectangle object that defines the area of the
* source object to draw. If you do not supply this
* value, no clipping occurs and the entire source
* object is drawn.
* @param smoothing A Boolean value that determines whether a BitmapData
* object is smoothed when scaled or rotated, due to a
* scaling or rotation in the <code>matrix</code>
* parameter. The <code>smoothing</code> parameter only
* applies if the <code>source</code> parameter is a
* BitmapData object. With <code>smoothing</code> set
* to <code>false</code>, the rotated or scaled
* BitmapData image can appear pixelated or jagged. For
* example, the following two images use the same
* BitmapData object for the <code>source</code>
* parameter, but the <code>smoothing</code> parameter
* is set to <code>true</code> on the left and
* <code>false</code> on the right:
*
* <p>Drawing a bitmap with <code>smoothing</code> set
* to <code>true</code> takes longer than doing so with
* <code>smoothing</code> set to
* <code>false</code>.</p>
* @throws ArgumentError The <code>source</code> parameter is not a
* BitmapData or DisplayObject object.
* @throws ArgumentError The source is null or not a valid IBitmapDrawable
* object.
* @throws SecurityError The <code>source</code> object and(in the case of a
* Sprite or MovieClip object) all of its child objects
* do not come from the same domain as the caller, or
* are not in a content that is accessible to the
* caller by having called the
* <code>Security.allowDomain()</code> method. This
* restriction does not apply to AIR content in the
* application security sandbox.
*/
public function draw (source:IBitmapDrawable, matrix:Matrix = null, colorTransform:ColorTransform = null, blendMode:BlendMode = null, clipRect:Rectangle = null, smoothing:Bool = false):Void {
if (!__isValid) return;
switch (__image.type) {
case CANVAS:
ImageCanvasUtil.convertToCanvas (__image);
ImageCanvasUtil.sync (__image);
#if js
var buffer = __image.buffer;
var renderSession = new RenderSession ();
renderSession.context = buffer.__srcContext;
renderSession.roundPixels = true;
if (!smoothing) {
untyped (buffer.__srcContext).mozImageSmoothingEnabled = false;
untyped (buffer.__srcContext).webkitImageSmoothingEnabled = false;
buffer.__srcContext.imageSmoothingEnabled = false;
}
var matrixCache = source.__worldTransform;
source.__worldTransform = matrix != null ? matrix : new Matrix ();
source.__updateChildren (false);
source.__renderCanvas (renderSession);
source.__worldTransform = matrixCache;
source.__updateChildren (true);
if (!smoothing) {
untyped (buffer.__srcContext).mozImageSmoothingEnabled = true;
untyped (buffer.__srcContext).webkitImageSmoothingEnabled = true;
buffer.__srcContext.imageSmoothingEnabled = true;
}
buffer.__srcContext.setTransform (1, 0, 0, 1, 0, 0);
#end
default:
// TODO
}
}
/**
* Encodes the current image as a JPG or PNG format ByteArray.
*
* This method is not available to the HTML5 and Flash targets.
*
* @param format The encoding format, either "png" or "jpg".
* @param quality The encoding quality, when encoding with the JPG format.
* @return A ByteArray in the specified encoding format
*/
public function encode (rect:Rectangle, compressor:Dynamic, byteArray:ByteArray = null):ByteArray {
openfl.Lib.notImplemented ("BitmapData.encode");
return null;
}
/**
* Fills a rectangular area of pixels with a specified ARGB color.
*
* @param rect The rectangular area to fill.
* @param color The ARGB color value that fills the area. ARGB colors are
* often specified in hexadecimal format; for example,
* 0xFF336699.
* @throws TypeError The rect is null.
*/
public function fillRect (rect:Rectangle, color:Int):Void {
if (!__isValid || rect == null) return;
__image.fillRect (rect.__toLimeRectangle (), color);
}
/**
* Performs a flood fill operation on an image starting at an(<i>x</i>,
* <i>y</i>) coordinate and filling with a certain color. The
* <code>floodFill()</code> method is similar to the paint bucket tool in
* various paint programs. The color is an ARGB color that contains alpha
* information and color information.
*
* @param x The <i>x</i> coordinate of the image.
* @param y The <i>y</i> coordinate of the image.
* @param color The ARGB color to use as a fill.
*/
public function floodFill (x:Int, y:Int, color:Int):Void {
if (!__isValid) return;
__image.floodFill (x, y, color);
}
public static function fromBase64 (base64:String, type:String, onload:BitmapData -> Void = null):BitmapData {
var bitmapData = new BitmapData (0, 0, true);
bitmapData.__loadFromBase64 (base64, type, onload);
return bitmapData;
}
public static function fromBytes (bytes:ByteArray, rawAlpha:ByteArray = null, onload:BitmapData -> Void = null):BitmapData {
var bitmapData = new BitmapData (0, 0, true);
bitmapData.__loadFromBytes (bytes, rawAlpha, onload);
return bitmapData;
}
#if js
public static function fromCanvas (canvas:CanvasElement, transparent:Bool = true):BitmapData {
var bitmapData = new BitmapData (0, 0, transparent);
bitmapData.__loadFromImage (Image.fromCanvas (canvas));
bitmapData.__image.transparent = transparent;
return bitmapData;
}
#end
public static function fromFile (path:String, onload:BitmapData -> Void = null, onerror:Void -> Void = null):BitmapData {
var bitmapData = new BitmapData (0, 0, true);
bitmapData.__loadFromFile (path, onload, onerror);
return bitmapData;
}
public static function fromImage (image:Image, transparent:Bool = true):BitmapData {
var bitmapData = new BitmapData (0, 0, transparent);
bitmapData.__loadFromImage (image);
bitmapData.__image.transparent = transparent;
return bitmapData;
}
/**
* Determines the destination rectangle that the <code>applyFilter()</code>
* method call affects, given a BitmapData object, a source rectangle, and a
* filter object.
*
* <p>For example, a blur filter normally affects an area larger than the
* size of the original image. A 100 x 200 pixel image that is being filtered
* by a default BlurFilter instance, where <code>blurX = blurY = 4</code>
* generates a destination rectangle of <code>(-2,-2,104,204)</code>. The
* <code>generateFilterRect()</code> method lets you find out the size of
* this destination rectangle in advance so that you can size the destination
* image appropriately before you perform a filter operation.</p>
*
* <p>Some filters clip their destination rectangle based on the source image
* size. For example, an inner <code>DropShadow</code> does not generate a
* larger result than its source image. In this API, the BitmapData object is
* used as the source bounds and not the source <code>rect</code>
* parameter.</p>
*
* @param sourceRect A rectangle defining the area of the source image to use
* as input.
* @param filter A filter object that you use to calculate the
* destination rectangle.
* @return A destination rectangle computed by using an image, the
* <code>sourceRect</code> parameter, and a filter.
* @throws TypeError The sourceRect or filter are null.
*/
public function generateFilterRect (sourceRect:Rectangle, filter:BitmapFilter):Rectangle {
return sourceRect.clone ();
}
public function getBuffer (gl:GLRenderContext):GLBuffer {
if (__buffer == null) {
var data = [
width, height, 0, 1, 1,
0, height, 0, 0, 1,
width, 0, 0, 1, 0,
0, 0, 0, 0, 0
];
__buffer = gl.createBuffer ();
gl.bindBuffer (gl.ARRAY_BUFFER, __buffer);
gl.bufferData (gl.ARRAY_BUFFER, new Float32Array (cast data), gl.STATIC_DRAW);
gl.bindBuffer (gl.ARRAY_BUFFER, null);
}
return __buffer;
}
/**
* Determines a rectangular region that either fully encloses all pixels of a
* specified color within the bitmap image(if the <code>findColor</code>
* parameter is set to <code>true</code>) or fully encloses all pixels that
* do not include the specified color(if the <code>findColor</code>
* parameter is set to <code>false</code>).
*
* <p>For example, if you have a source image and you want to determine the
* rectangle of the image that contains a nonzero alpha channel, pass
* <code>{mask: 0xFF000000, color: 0x00000000}</code> as parameters. If the
* <code>findColor</code> parameter is set to <code>true</code>, the entire
* image is searched for the bounds of pixels for which <code>(value & mask)
* == color</code>(where <code>value</code> is the color value of the
* pixel). If the <code>findColor</code> parameter is set to
* <code>false</code>, the entire image is searched for the bounds of pixels
* for which <code>(value & mask) != color</code>(where <code>value</code>
* is the color value of the pixel). To determine white space around an
* image, pass <code>{mask: 0xFFFFFFFF, color: 0xFFFFFFFF}</code> to find the
* bounds of nonwhite pixels.</p>
*
* @param mask A hexadecimal value, specifying the bits of the ARGB
* color to consider. The color value is combined with this
* hexadecimal value, by using the <code>&</code>(bitwise
* AND) operator.
* @param color A hexadecimal value, specifying the ARGB color to match
* (if <code>findColor</code> is set to <code>true</code>)
* or <i>not</i> to match(if <code>findColor</code> is set
* to <code>false</code>).
* @param findColor If the value is set to <code>true</code>, returns the
* bounds of a color value in an image. If the value is set
* to <code>false</code>, returns the bounds of where this
* color doesn't exist in an image.
* @return The region of the image that is the specified color.
*/
public function getColorBoundsRect (mask:Int, color:Int, findColor:Bool = true):Rectangle {
return __image.rect.__toFlashRectangle ();
}
/**
* Returns an integer that represents an RGB pixel value from a BitmapData
* object at a specific point(<i>x</i>, <i>y</i>). The
* <code>getPixel()</code> method returns an unmultiplied pixel value. No
* alpha information is returned.
*
* <p>All pixels in a BitmapData object are stored as premultiplied color
* values. A premultiplied image pixel has the red, green, and blue color
* channel values already multiplied by the alpha data. For example, if the
* alpha value is 0, the values for the RGB channels are also 0, independent
* of their unmultiplied values. This loss of data can cause some problems
* when you perform operations. All BitmapData methods take and return
* unmultiplied values. The internal pixel representation is converted from
* premultiplied to unmultiplied before it is returned as a value. During a
* set operation, the pixel value is premultiplied before the raw image pixel
* is set.</p>
*
* @param x The <i>x</i> position of the pixel.
* @param y The <i>y</i> position of the pixel.
* @return A number that represents an RGB pixel value. If the(<i>x</i>,
* <i>y</i>) coordinates are outside the bounds of the image, the
* method returns 0.
*/
public function getPixel (x:Int, y:Int):Int {
if (!__isValid) return 0;
return __image.getPixel (x, y);
}
/**
* Returns an ARGB color value that contains alpha channel data and RGB data.
* This method is similar to the <code>getPixel()</code> method, which
* returns an RGB color without alpha channel data.
*
* <p>All pixels in a BitmapData object are stored as premultiplied color
* values. A premultiplied image pixel has the red, green, and blue color
* channel values already multiplied by the alpha data. For example, if the
* alpha value is 0, the values for the RGB channels are also 0, independent
* of their unmultiplied values. This loss of data can cause some problems
* when you perform operations. All BitmapData methods take and return
* unmultiplied values. The internal pixel representation is converted from
* premultiplied to unmultiplied before it is returned as a value. During a
* set operation, the pixel value is premultiplied before the raw image pixel
* is set.</p>
*
* @param x The <i>x</i> position of the pixel.
* @param y The <i>y</i> position of the pixel.
* @return A number representing an ARGB pixel value. If the(<i>x</i>,
* <i>y</i>) coordinates are outside the bounds of the image, 0 is
* returned.
*/
public function getPixel32 (x:Int, y:Int):Int {
if (!__isValid) return 0;
return __image.getPixel32 (x, y);
}
/**
* Generates a byte array from a rectangular region of pixel data. Writes an
* unsigned integer(a 32-bit unmultiplied pixel value) for each pixel into
* the byte array.
*
* @param rect A rectangular area in the current BitmapData object.
* @return A ByteArray representing the pixels in the given Rectangle.
* @throws TypeError The rect is null.
*/
public function getPixels (rect:Rectangle):ByteArray {
if (!__isValid) return null;
if (rect == null) rect = this.rect;
return __image.getPixels (rect.__toLimeRectangle ());
}
public function getTexture (gl:GLRenderContext):GLTexture {
if (__texture == null) {
__texture = gl.createTexture ();
gl.bindTexture (gl.TEXTURE_2D, __texture);
gl.texParameteri (gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri (gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri (gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri (gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
__image.dirty = true;
}
if (__image.dirty) {
gl.bindTexture (gl.TEXTURE_2D, __texture);
var textureImage = __image.clone ();
textureImage.premultiplied = true;
gl.texImage2D (gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, textureImage.data);
gl.bindTexture (gl.TEXTURE_2D, null);
__image.dirty = false;
}
return __texture;
}
/**
* Generates a vector array from a rectangular region of pixel data. Returns
* a Vector object of unsigned integers(a 32-bit unmultiplied pixel value)
* for the specified rectangle.
*
* @param rect A rectangular area in the current BitmapData object.
* @return A Vector representing the given Rectangle.
* @throws TypeError The rect is null.
*/
public function getVector (rect:Rectangle) {
var pixels = getPixels (rect);
var result = new Vector<UInt> ();
for (i in 0...Std.int (pixels.length / 4)) {
result.push (pixels.readUnsignedInt ());
}
return result;
}
public function histogram (hRect:Rectangle = null) {
var rect = hRect != null ? hRect : new Rectangle (0, 0, width, height);
var pixels = getPixels (rect);
var result = [for (i in 0...4) [for (j in 0...256) 0]];
for (i in 0...pixels.length) {
++result[i % 4][pixels.readUnsignedByte ()];
}
return result;
}
public function hitTest (firstPoint:Point, firstAlphaThreshold:Int, secondObject:Dynamic, secondBitmapDataPoint:Point = null, secondAlphaThreshold:Int = 1):Bool {
if (!__isValid) return false;
openfl.Lib.notImplemented ("BitmapData.hitTest");
return false;
}
/**
* Locks an image so that any objects that reference the BitmapData object,
* such as Bitmap objects, are not updated when this BitmapData object
* changes. To improve performance, use this method along with the
* <code>unlock()</code> method before and after numerous calls to the
* <code>setPixel()</code> or <code>setPixel32()</code> method.
*
*/
public function lock ():Void {
}
/**
* Fills an image with pixels representing random noise.
*
* @param randomSeed The random seed number to use. If you keep all other
* parameters the same, you can generate different
* pseudo-random results by varying the random seed
* value. The noise function is a mapping function, not
* a true random-number generation function, so it
* creates the same results each time from the same
* random seed.
* @param low The lowest value to generate for each channel(0 to
* 255).
* @param high The highest value to generate for each channel(0 to
* 255).
* @param channelOptions A number that can be a combination of any of the
* four color channel values
* (<code>BitmapDataChannel.RED</code>,
* <code>BitmapDataChannel.BLUE</code>,
* <code>BitmapDataChannel.GREEN</code>, and
* <code>BitmapDataChannel.ALPHA</code>). You can use
* the logical OR operator(<code>|</code>) to combine
* channel values.
* @param grayScale A Boolean value. If the value is <code>true</code>,
* a grayscale image is created by setting all of the
* color channels to the same value. The alpha channel
* selection is not affected by setting this parameter
* to <code>true</code>.
*/
public function noise (randomSeed:Int, low:Int = 0, high:Int = 255, channelOptions:Int = 7, grayScale:Bool = false):Void {
if (!__isValid) return;
openfl.Lib.notImplemented ("BitmapData.noise");
}
public function paletteMap (sourceBitmapData:BitmapData, sourceRect:Rectangle, destPoint:Point, ?redArray:Array<Int>, ?greenArray:Array<Int>, ?blueArray:Array<Int>, ?alphaArray:Array<Int>):Void {
var memory = new ByteArray ();
var sw:Int = Std.int (sourceRect.width);
var sh:Int = Std.int (sourceRect.height);
#if js
memory.length = ((sw * sh) * 4);
#end
memory = getPixels (sourceRect);
memory.position = 0;
Memory.select (memory);
var position:Int, pixelValue:Int, r:Int, g:Int, b:Int, color:Int;
for (i in 0...(sh * sw)) {
position = i * 4;
pixelValue = cast Memory.getI32 (position);
r = (pixelValue >> 8) & 0xFF;
g = (pixelValue >> 16) & 0xFF;
b = (pixelValue >> 24) & 0xFF;
color = __flipPixel ((0xff << 24) |
redArray[r] |
greenArray[g] |
blueArray[b]);
Memory.setI32 (position, color);
}
memory.position = 0;
var destRect = new Rectangle (destPoint.x, destPoint.y, sw, sh);
setPixels (destRect, memory);
Memory.select (null);
}
/**
* Generates a Perlin noise image.
*
* <p>The Perlin noise generation algorithm interpolates and combines
* individual random noise functions(called octaves) into a single function
* that generates more natural-seeming random noise. Like musical octaves,
* each octave function is twice the frequency of the one before it. Perlin
* noise has been described as a "fractal sum of noise" because it combines
* multiple sets of noise data with different levels of detail.</p>