-
Notifications
You must be signed in to change notification settings - Fork 128
/
pixmappaint.d
3248 lines (2717 loc) · 69.1 KB
/
pixmappaint.d
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
/+
== pixmappaint ==
Copyright Elias Batek (0xEAB) 2024.
Distributed under the Boost Software License, Version 1.0.
+/
/++
Pixmap image manipulation
$(WARNING
$(B Early Technology Preview.)
)
$(PITFALL
This module is $(B work in progress).
API is subject to changes until further notice.
)
Pixmap refers to raster graphics, a subset of “bitmap” graphics.
A pixmap is an array of pixels and the corresponding meta data to describe
how an image if formed from those pixels.
In the case of this library, a “width” field is used to map a specified
number of pixels to a row of an image.
```
pixels := [ 0, 1, 2, 3 ]
width := 2
pixmap(pixels, width)
=> [
[ 0, 1 ]
[ 2, 3 ]
]
```
```
pixels := [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
width := 3
pixmap(pixels, width)
=> [
[ 0, 1, 2 ]
[ 3, 4, 5 ]
[ 6, 7, 8 ]
[ 9, 10, 11 ]
]
```
```
pixels := [ 0, 1, 2, 3, 4, 5, 6, 7 ]
width := 4
pixmap(pixels, width)
=> [
[ 0, 1, 2, 3 ]
[ 4, 5, 6, 7 ]
]
```
### Colors
Colors are stored in an RGBA format with 8 bit per channel.
See [arsd.color.Color|Pixel] for details.
### The coordinate system
The top left corner of a pixmap is its $(B origin) `(0,0)`.
The $(horizontal axis) is called `x`.
Its corresponding length/dimension is known as `width`.
The letter `y` is used to describe the $(B vertical axis).
Its corresponding length/dimension is known as `height`.
```
0 → x
↓
y
```
Furthermore, $(B length) refers to the areal size of a pixmap.
It represents the total number of pixels in a pixmap.
It follows from the foregoing that the term $(I long) usually refers to
the length (not the width).
### Pixmaps
A [Pixmap] consist of two fields:
$(LIST
* a slice (of an array of [Pixel|Pixels])
* a width
)
This design comes with many advantages.
First and foremost it brings simplicity.
Pixel data buffers can be reused across pixmaps,
even when those have different sizes.
Simply slice the buffer to fit just enough pixels for the new pixmap.
Memory management can also happen outside of the pixmap.
It is possible to use a buffer allocated elsewhere. (Such a one shouldn’t
be mixed with the built-in memory management facilities of the pixmap type.
Otherwise one will end up with GC-allocated copies.)
The most important downside is that it makes pixmaps basically a partial
reference type.
Copying a pixmap creates a shallow copy still poiting to the same pixel
data that is also used by the source pixmap.
This implies that manipulating the source pixels also manipulates the
pixels of the copy – and vice versa.
The issues implied by this become an apparent when one of the references
modifies the pixel data in a way that also affects the dimensions of the
image; such as cropping.
Pixmaps describe how pixel data stored in a 1-dimensional memory space is
meant to be interpreted as a 2-dimensional image.
A notable implication of this 1D ↔ 2D mapping is, that slicing the 1D data
leads to non-sensical results in the 2D space when the 1D-slice is
reinterpreted as 2D-image.
Especially slicing across scanlines (→ horizontal rows of an image) is
prone to such errors.
(Slicing of the 1D array data can actually be utilized to cut off the
bottom part of an image. Any other naiv cropping operations will run into
the aforementioned issues.)
### Image manipulation
The term “image manipulation function” here refers to functions that
manipulate (e.g. transform) an image as a whole.
Image manipulation functions in this library are provided in up to three
flavors:
$(LIST
* a “source to target” function
* a “source to newly allocated target” wrapper
* $(I optionally) an “in-place” adaption
)
Additionally, a “compute dimensions of target” function is provided.
#### Source to Target
The regular “source to target” function takes (at least) two parameters:
A source [Pixmap] and a target [Pixmap].
(Additional operation-specific arguments may be required as well.)
The target pixmap usually needs to be able to fit at least the same number
of pixels as the source holds.
Use the corresponding “compute size of target function” to calculate the
required size when needed.
(A notable exception would be cropping, where to target pixmap must be only
at least long enough to hold the area of the size to crop to.)
The data stored in the buffer of the target pixmap is overwritten by the
operation.
A modified Pixmap structure with adjusted dimensions is returned.
These functions are named plain and simple after the respective operation
they perform; e.g. [flipHorizontally] or [crop].
---
// Allocate a new target Pixmap.
Pixmap target = Pixmap.makeNew(
flipHorizontallyCalcDims(sourceImage)
);
// Flip the image horizontally and store the updated structure.
// (Note: As a horizontal flip does not affect the dimensions of a Pixmap,
// storing the updated structure would not be necessary
// in this specific scenario.)
target = sourceImage.flipHorizontally(target);
---
---
const cropOffset = Point(0, 0);
const cropSize = Size(100, 100);
// Allocate a new target Pixmap.
Pixmap target = Pixmap.makeNew(
cropCalcDims(sourceImage, cropSize, cropOffset)
);
// Crop the Pixmap.
target = sourceImage.crop(target, cropSize, cropOffset);
---
$(PITFALL
“Source to target” functions do not work in place.
Do not attempt to pass Pixmaps sharing the same buffer for both source
and target. Such would lead to bad results with heavy artifacts.
Use the “in-place” variant of the operation instead.
Moreover:
Do not use the artifacts produced by this as a creative effect.
Those are an implementation detail (and may change at any point).
)
#### Source to New Target
The “source to newly allocated target” wrapper allocates a new buffer to
hold the manipulated target.
These wrappers are provided for user convenience.
They are identified by the suffix `-New` that is appended to the name of
the corresponding “source to target” function;
e.g. [flipHorizontallyNew] or [cropNew].
---
// Create a new flipped Pixmap.
Pixmap target = sourceImage.flipHorizontallyNew();
---
---
const cropOffset = Point(0, 0);
const cropSize = Size(100, 100);
// Create a new cropped Pixmap.
Pixmap target = sourceImage.cropNew(cropSize, cropOffset);
---
#### In-Place
For selected image manipulation functions a special adaption is provided
that stores the result in the source pixel data buffer.
Depending on the operation, implementing in-place transformations can be
either straightforward or a major undertaking (and topic of research).
This library focuses and the former and leaves out cases where the latter
applies.
In particular, algorithms that require allocating further buffers to store
temporary results or auxiliary data will probably not get implemented.
Furthermore, operations where to result is longer than the source cannot
be performed in-place.
Certain in-place manipulation functions return a shallow-copy of the
source structure with dimensions adjusted accordingly.
This is behavior is not streamlined consistently as the lack of an
in-place option for certain operations makes them a special case anyway.
These function are suffixed with `-InPlace`;
e.g. [flipHorizontallyInPlace] or [cropInPlace].
$(TIP
Manipulating the source image directly can lead to unexpected results
when the source image is used in multiple places.
)
$(NOTE
Users are usually better off to utilize the regular “source to target”
functions with a reused pixel data buffer.
These functions do not serve as a performance optimization.
Some of them might perform significantly worse than their regular
variant. Always benchmark and profile.
)
---
image.flipHorizontallyInPlace();
---
---
const cropOffset = Point(0, 0);
const cropSize = Size(100, 100);
image = image.cropInPlace(cropSize, cropOffset);
---
#### Compute size of target
Functions to “compute (the) dimensions of (a) target” are primarily meant
to be utilized to calculate the size for allocating new pixmaps to be used
as a target for manipulation functions.
They are provided for all manipulation functions even in cases where they
are provide little to no benefit. This is for consistency and to ease
development.
Such functions are identified by a `-CalcDims` suffix;
e.g. [flipHorizontallyCalcDims] or [cropCalcDims].
They usually take the same parameters as their corresponding
“source to new target” function. This does not apply in cases where
certain parameters are irrelevant for the computation of the target size.
+/
module arsd.pixmappaint;
import arsd.color;
import arsd.core;
private float roundImpl(float f) {
import std.math : round;
return round(f);
}
// `pure` rounding function.
// std.math.round() isn’t pure on all targets.
// → <https://issues.dlang.org/show_bug.cgi?id=11320>
private float round(float f) pure @nogc nothrow @trusted {
return (castTo!(float function(float) pure @nogc nothrow)(&roundImpl))(f);
}
/*
## TODO:
- Refactoring the template-mess of blendPixel() & co.
- Scaling
- Rotating
- Skewing
- HSL
- Advanced blend modes (maybe)
*/
///
alias Color = arsd.color.Color;
///
alias ColorF = arsd.color.ColorF;
///
alias Pixel = Color;
///
alias Point = arsd.color.Point;
///
alias Rectangle = arsd.color.Rectangle;
///
alias Size = arsd.color.Size;
// verify assumption(s)
static assert(Pixel.sizeof == uint.sizeof);
@safe pure nothrow @nogc {
///
Pixel rgba(ubyte r, ubyte g, ubyte b, ubyte a = 0xFF) {
return Pixel(r, g, b, a);
}
///
Pixel rgba(ubyte r, ubyte g, ubyte b, float aPct) {
return Pixel(r, g, b, percentageDecimalToUInt8(aPct));
}
///
Pixel rgb(ubyte r, ubyte g, ubyte b) {
return rgba(r, g, b, 0xFF);
}
}
/++
$(I Advanced functionality.)
Meta data for the construction of a Pixmap.
+/
struct PixmapBlueprint {
/++
Total number of pixels stored in a Pixmap.
+/
size_t length;
/++
Width of a Pixmap.
+/
int width;
@safe pure nothrow @nogc:
///
public static PixmapBlueprint fromSize(const Size size) {
return PixmapBlueprint(
size.area,
size.width,
);
}
///
public static PixmapBlueprint fromPixmap(const Pixmap pixmap) {
return PixmapBlueprint(
pixmap.length,
pixmap.width,
);
}
/++
Determines whether the blueprint is plausible.
+/
bool isValid() const {
return ((length % width) == 0);
}
/++
Height of a Pixmap.
See_also:
This is the counterpart to the dimension known as [width].
+/
int height() const {
return castTo!int(length / width);
}
///
Size size() const {
return Size(width, height);
}
}
/++
Pixel data container
+/
struct Pixmap {
/// Pixel data
Pixel[] data;
/// Pixel per row
int width;
@safe pure nothrow:
///
deprecated("Use `Pixmap.makeNew(size)` instead.")
this(Size size) {
this.size = size;
}
///
deprecated("Use `Pixmap.makeNew(Size(width, height))` instead.")
this(int width, int height)
in (width > 0)
in (height > 0) {
this(Size(width, height));
}
///
this(inout(Pixel)[] data, int width) inout @nogc
in (data.length % width == 0) {
this.data = data;
this.width = width;
}
///
static Pixmap makeNew(PixmapBlueprint blueprint) {
auto data = new Pixel[](blueprint.length);
return Pixmap(data, blueprint.width);
}
///
static Pixmap makeNew(Size size) {
return Pixmap.makeNew(PixmapBlueprint.fromSize(size));
}
/++
Creates a $(I deep copy) of the Pixmap
+/
Pixmap clone() const {
return Pixmap(
this.data.dup,
this.width,
);
}
/++
Copies the pixel data to the target Pixmap.
Returns:
A size-adjusted shallow copy of the input Pixmap overwritten
with the image data of the SubPixmap.
$(PITFALL
While the returned Pixmap utilizes the buffer provided by the input,
the returned Pixmap might not exactly match the input.
Always use the returned Pixmap structure.
---
// Same buffer, but new structure:
auto pixmap2 = source.copyTo(pixmap);
// Alternatively, replace the old structure:
pixmap = source.copyTo(pixmap);
---
)
+/
Pixmap copyTo(Pixmap target) @nogc const {
// Length adjustment
const l = this.length;
if (target.data.length < l) {
assert(false, "The target Pixmap is too small.");
} else if (target.data.length > l) {
target.data = target.data[0 .. l];
}
copyToImpl(target);
return target;
}
private void copyToImpl(Pixmap target) @nogc const {
target.data[] = this.data[];
}
// undocumented: really shouldn’t be used.
// carries the risks of `length` and `width` getting out of sync accidentally.
deprecated("Use `size` instead.")
void length(int value) {
data.length = value;
}
/++
Changes the size of the buffer
Reallocates the underlying pixel array.
+/
void size(Size value) {
data.length = value.area;
width = value.width;
}
/// ditto
void size(int totalPixels, int width)
in (totalPixels % width == 0) {
data.length = totalPixels;
this.width = width;
}
static {
/++
Creates a Pixmap wrapping the pixel data from the provided `TrueColorImage`.
Interoperability function: `arsd.color`
+/
Pixmap fromTrueColorImage(TrueColorImage source) @nogc {
return Pixmap(source.imageData.colors, source.width);
}
/++
Creates a Pixmap wrapping the pixel data from the provided `MemoryImage`.
Interoperability function: `arsd.color`
+/
Pixmap fromMemoryImage(MemoryImage source) {
return fromTrueColorImage(source.getAsTrueColorImage());
}
}
@safe pure nothrow @nogc:
/// Height of the buffer, i.e. the number of lines
int height() inout {
if (width == 0) {
return 0;
}
return castTo!int(data.length / width);
}
/// Rectangular size of the buffer
Size size() inout {
return Size(width, height);
}
/// Length of the buffer, i.e. the number of pixels
int length() inout {
return castTo!int(data.length);
}
/++
Number of bytes per line
Returns:
width × Pixel.sizeof
+/
int pitch() inout {
return (width * int(Pixel.sizeof));
}
/++
Adjusts the Pixmap according to the provided blueprint.
The blueprint must not be larger than the data buffer of the pixmap.
This function does not reallocate the pixel data buffer.
If the blueprint is larger than the data buffer of the pixmap,
this will result in a bounds-check error if applicable.
+/
void adjustTo(PixmapBlueprint blueprint) {
debug assert(this.data.length >= blueprint.length);
debug assert(blueprint.isValid);
this.data = this.data[0 .. blueprint.length];
this.width = blueprint.width;
}
/++
Calculates the index (linear offset) of the requested position
within the pixmap data.
+/
int scanTo(Point pos) inout {
return linearOffset(width, pos);
}
/++
Accesses the pixel at the requested position within the pixmap data.
+/
ref inout(Pixel) scan(Point pos) inout {
return data[scanTo(pos)];
}
/++
Retrieves a linear slice of the pixmap.
Returns:
`n` pixels starting at the top-left position `pos`.
+/
inout(Pixel)[] scan(Point pos, int n) inout {
immutable size_t offset = linearOffset(width, pos);
immutable size_t end = (offset + n);
return data[offset .. end];
}
/// ditto
inout(Pixel)[] sliceAt(Point pos, int n) inout {
return scan(pos, n);
}
/++
Retrieves a rectangular subimage of the pixmap.
+/
inout(SubPixmap) scanArea(Point pos, Size size) inout {
return inout(SubPixmap)(this, size, pos);
}
/// TODO: remove
deprecated alias scanSubPixmap = scanArea;
/// TODO: remove
deprecated alias scan2D = scanArea;
/++
Retrieves the first line of the Pixmap.
See_also:
Check out [PixmapScanner] for more useful scanning functionality.
+/
inout(Pixel)[] scanLine() inout {
return data[0 .. width];
}
public {
/++
Provides access to a single pixel at the requested 2D-position.
See_also:
Accessing pixels through the [data] array will be more useful,
usually.
+/
ref inout(Pixel) accessPixel(Point pos) inout @system {
const idx = linearOffset(pos, this.width);
return this.data[idx];
}
/// ditto
Pixel getPixel(Point pos) const {
const idx = linearOffset(pos, this.width);
return this.data[idx];
}
/// ditto
Pixel getPixel(int x, int y) const {
return this.getPixel(Point(x, y));
}
/// ditto
void setPixel(Point pos, Pixel value) {
const idx = linearOffset(pos, this.width);
this.data[idx] = value;
}
/// ditto
void setPixel(int x, int y, Pixel value) {
return this.setPixel(Point(x, y), value);
}
}
/// Clears the buffer’s contents (by setting each pixel to the same color)
void clear(Pixel value) {
data[] = value;
}
}
/++
A subpixmap represents a subimage of a [Pixmap].
This wrapper provides convenient access to a rectangular slice of a Pixmap.
```
╔═════════════╗
║ Pixmap ║
║ ║
║ ┌───┐ ║
║ │Sub│ ║
║ └───┘ ║
╚═════════════╝
```
+/
struct SubPixmap {
/++
Source image referenced by the subimage
+/
Pixmap source;
/++
Size of the subimage
+/
Size size;
/++
2D offset of the subimage
+/
Point offset;
public @safe pure nothrow @nogc {
///
this(inout Pixmap source, Size size = Size(0, 0), Point offset = Point(0, 0)) inout {
this.source = source;
this.size = size;
this.offset = offset;
}
///
this(inout Pixmap source, Point offset, Size size = Size(0, 0)) inout {
this(source, size, offset);
}
}
@safe pure nothrow:
public {
/++
Allocates a new Pixmap cropped to the pixel data of the subimage.
See_also:
Use [extractToPixmap] for a non-allocating variant with an .
+/
Pixmap extractToNewPixmap() const {
auto pm = Pixmap.makeNew(size);
this.extractToPixmap(pm);
return pm;
}
/++
Copies the pixel data – cropped to the subimage region –
into the target Pixmap.
$(PITFALL
Do not attempt to extract a subimage back into the source pixmap.
This will fail in cases where source and target regions overlap
and potentially crash the program.
)
Returns:
A size-adjusted shallow copy of the input Pixmap overwritten
with the image data of the SubPixmap.
$(PITFALL
While the returned Pixmap utilizes the buffer provided by the input,
the returned Pixmap might not exactly match the input.
The dimensions (width and height) and the length might have changed.
Always use the returned Pixmap structure.
---
// Same buffer, but new structure:
auto pixmap2 = subPixmap.extractToPixmap(pixmap);
// Alternatively, replace the old structure:
pixmap = subPixmap.extractToPixmap(pixmap);
---
)
+/
Pixmap extractToPixmap(Pixmap target) @nogc const {
// Length adjustment
const l = this.length;
if (target.data.length < l) {
assert(false, "The target Pixmap is too small.");
} else if (target.data.length > l) {
target.data = target.data[0 .. l];
}
target.width = this.width;
extractToPixmapCopyImpl(target);
return target;
}
private void extractToPixmapCopyImpl(Pixmap target) @nogc const {
auto src = SubPixmapScanner(this);
auto dst = PixmapScannerRW(target);
foreach (dstLine; dst) {
dstLine[] = src.front[];
src.popFront();
}
}
private void extractToPixmapCopyPixelByPixelImpl(Pixmap target) @nogc const {
auto src = SubPixmapScanner(this);
auto dst = PixmapScannerRW(target);
foreach (dstLine; dst) {
const srcLine = src.front;
foreach (idx, ref px; dstLine) {
px = srcLine[idx];
}
src.popFront();
}
}
}
@safe pure nothrow @nogc:
public {
/++
Width of the subimage.
+/
int width() const {
return size.width;
}
/// ditto
void width(int value) {
size.width = value;
}
/++
Height of the subimage.
+/
int height() const {
return size.height;
}
/// ditto
void height(int value) {
size.height = value;
}
/++
Number of pixels in the subimage.
+/
int length() const {
return size.area;
}
}
public {
/++
Linear offset of the subimage within the source image.
Calculates the index of the “first pixel of the subimage”
in the “pixel data of the source image”.
+/
int sourceOffsetLinear() const {
return linearOffset(offset, source.width);
}
/// ditto
void sourceOffsetLinear(int value) {
this.offset = Point.fromLinearOffset(value, source.width);
}
/++
$(I Advanced functionality.)
Offset of the pixel following the bottom right corner of the subimage.
(`Point(O, 0)` is the top left corner of the source image.)
+/
Point sourceOffsetEnd() const {
auto vec = Point(size.width, (size.height - 1));
return (offset + vec);
}
/++
Linear offset of the subimage within the source image.
Calculates the index of the “first pixel of the subimage”
in the “pixel data of the source image”.
+/
int sourceOffsetLinearEnd() const {
return linearOffset(sourceOffsetEnd, source.width);
}
}
/++
Determines whether the area of the subimage
lies within the source image
and does not overflow its lines.
$(TIP
If the offset and/or size of a subimage are off, two issues can occur:
$(LIST
* The resulting subimage will look displaced.
(As if the lines were shifted.)
This indicates that one scanline of the subimage spans over
two ore more lines of the source image.
(Happens when `(subimage.offset.x + subimage.size.width) > source.size.width`.)
* When accessing the pixel data, bounds checks will fail.
This suggests that the area of the subimage extends beyond
the bottom end (and optionally also beyond the right end) of
the source.
)
Both defects could indicate an invalid subimage.
Use this function to verify the SubPixmap.
)
$(WARNING
Do not use invalid SubPixmaps.
The library assumes that the SubPixmaps it receives are always valid.
Non-valid SubPixmaps are not meant to be used for creative effects
or similar either. Such uses might lead to unexpected quirks or
crashes eventually.
)
+/
bool isValid() const {
return (
(sourceMarginLeft >= 0)
&& (sourceMarginTop >= 0)
&& (sourceMarginBottom >= 0)
&& (sourceMarginRight >= 0)
);
}
public inout {
/++
Retrieves the pixel at the requested position of the subimage.
+/
ref inout(Pixel) scan(Point pos) {
return source.scan(offset + pos);
}
/++
Retrieves the first line of the subimage.
+/
inout(Pixel)[] scanLine() {
const lo = linearOffset(offset, size.width);
return source.data[lo .. size.width];
}
}
/++
Copies the pixels of this subimage to a target image.
The target MUST have the same size.
See_also:
Usually you’ll want to use [extractToPixmap] or [drawPixmap] instead.
+/
public void xferTo(SubPixmap target) const {
debug assert(target.size == this.size);
auto src = SubPixmapScanner(this);
auto dst = SubPixmapScannerRW(target);
foreach (dstLine; dst) {
dstLine[] = src.front[];
src.popFront();
}
}