forked from twig33/ynoclient
-
Notifications
You must be signed in to change notification settings - Fork 8
/
bitmap.cpp
1215 lines (995 loc) · 37.1 KB
/
bitmap.cpp
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
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EasyRPG Player 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/
// Headers
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include "utils.h"
#include "cache.h"
#include "bitmap.h"
#include "filefinder.h"
#include "options.h"
#include <lcf/data.h>
#include "output.h"
#include "image_xyz.h"
#include "image_bmp.h"
#include "image_png.h"
#include "transform.h"
#include "font.h"
#include "output.h"
#include "util_macro.h"
#include "bitmap_hslrgb.h"
#include <iostream>
BitmapRef Bitmap::Create(int width, int height, const Color& color) {
BitmapRef surface = Bitmap::Create(width, height, true);
surface->Fill(color);
return surface;
}
BitmapRef Bitmap::Create(Filesystem_Stream::InputStream stream, bool transparent, uint32_t flags) {
BitmapRef bmp = std::make_shared<Bitmap>(std::move(stream), transparent, flags);
if (!bmp->pixels()) {
return BitmapRef();
}
return bmp;
}
BitmapRef Bitmap::Create(const uint8_t* data, unsigned bytes, bool transparent, uint32_t flags) {
BitmapRef bmp = std::make_shared<Bitmap>(data, bytes, transparent, flags);
if (!bmp->pixels()) {
return BitmapRef();
}
return bmp;
}
BitmapRef Bitmap::Create(Bitmap const& source, Rect const& src_rect, bool transparent) {
return std::make_shared<Bitmap>(source, src_rect, transparent);
}
BitmapRef Bitmap::Create(int width, int height, bool transparent, int /* bpp */) {
return std::make_shared<Bitmap>(width, height, transparent);
}
BitmapRef Bitmap::Create(void *pixels, int width, int height, int pitch, const DynamicFormat& format) {
return std::make_shared<Bitmap>(pixels, width, height, pitch, format);
}
Bitmap::Bitmap(int width, int height, bool transparent) {
format = (transparent ? pixel_format : opaque_pixel_format);
pixman_format = find_format(format);
Init(width, height, (void *) NULL);
}
Bitmap::Bitmap(void *pixels, int width, int height, int pitch, const DynamicFormat& _format) {
format = _format;
pixman_format = find_format(format);
Init(width, height, pixels, pitch, false);
}
Bitmap::Bitmap(Filesystem_Stream::InputStream stream, bool transparent, uint32_t flags) {
format = (transparent ? pixel_format : opaque_pixel_format);
pixman_format = find_format(format);
if (!stream) {
Output::Error("Couldn't read image file {}", stream.GetName());
return;
}
int w = 0;
int h = 0;
void* pixels = nullptr;
uint8_t data[4] = {};
size_t bytes = stream.read(reinterpret_cast<char*>(data), 4).gcount();
stream.seekg(0, std::ios::ios_base::beg);
bool img_okay = false;
if (bytes >= 4 && strncmp((char*)data, "XYZ1", 4) == 0)
img_okay = ImageXYZ::ReadXYZ(stream, transparent, w, h, pixels);
else if (bytes > 2 && strncmp((char*)data, "BM", 2) == 0)
img_okay = ImageBMP::ReadBMP(stream, transparent, w, h, pixels);
else if (bytes >= 4 && strncmp((char*)(data + 1), "PNG", 3) == 0)
img_okay = ImagePNG::ReadPNG(stream, transparent, w, h, pixels);
else
Output::Warning("Unsupported image file {} (Magic: {:02X})", stream.GetName(), *reinterpret_cast<uint32_t*>(data));
if (!img_okay) {
free(pixels);
pixels = nullptr;
return;
}
Init(w, h, nullptr);
ConvertImage(w, h, pixels, transparent);
CheckPixels(flags);
filename = ToString(stream.GetName());
}
Bitmap::Bitmap(const uint8_t* data, unsigned bytes, bool transparent, uint32_t flags) {
format = (transparent ? pixel_format : opaque_pixel_format);
pixman_format = find_format(format);
int w = 0, h = 0;
void* pixels = nullptr;
bool img_okay = false;
if (bytes > 4 && strncmp((char*) data, "XYZ1", 4) == 0)
img_okay = ImageXYZ::ReadXYZ(data, bytes, transparent, w, h, pixels);
else if (bytes > 2 && strncmp((char*) data, "BM", 2) == 0)
img_okay = ImageBMP::ReadBMP(data, bytes, transparent, w, h, pixels);
else if (bytes > 4 && strncmp((char*)(data + 1), "PNG", 3) == 0)
img_okay = ImagePNG::ReadPNG((const void*) data, transparent, w, h, pixels);
else
Output::Warning("Unsupported image (Magic: {:02X})", bytes >= 4 ? *reinterpret_cast<const uint32_t*>(data) : 0);
if (!img_okay) {
free(pixels);
pixels = nullptr;
return;
}
Init(w, h, nullptr);
ConvertImage(w, h, pixels, transparent);
CheckPixels(flags);
}
Bitmap::Bitmap(Bitmap const& source, Rect const& src_rect, bool transparent) {
format = (transparent ? pixel_format : opaque_pixel_format);
pixman_format = find_format(format);
Init(src_rect.width, src_rect.height, (void *) NULL);
Blit(0, 0, source, src_rect, Opacity::Opaque());
}
bool Bitmap::WritePNG(Filesystem_Stream::OutputStream& os) const {
size_t const width = GetWidth(), height = GetHeight();
size_t const stride = width * 4;
std::vector<uint32_t> data(width * height);
auto dst = PixmanImagePtr{pixman_image_create_bits(PIXMAN_b8g8r8, width, height, &data.front(), stride)};
pixman_image_composite32(PIXMAN_OP_SRC, bitmap.get(), NULL, dst.get(),
0, 0, 0, 0, 0, 0, width, height);
return ImagePNG::WritePNG(os, width, height, &data.front());
}
size_t Bitmap::GetSize() const {
if (!bitmap) {
return 0;
}
return pitch() * height();
}
ImageOpacity Bitmap::ComputeImageOpacity() const {
bool all_opaque = true;
bool all_transp = true;
auto* p = reinterpret_cast<const uint32_t*>(pixels());
const auto mask = pixel_format.rgba_to_uint32_t(0, 0, 0, 0xFF);
int n = GetSize() / sizeof(uint32_t);
for (int i = 0; i < n; ++i ) {
auto px = p[i] & mask;
all_opaque &= (px == mask);
all_transp &= (px == 0);
}
return
all_transp ? ImageOpacity::Transparent :
all_opaque ? ImageOpacity::Opaque :
ImageOpacity::Partial;
}
ImageOpacity Bitmap::ComputeImageOpacity(Rect rect) const {
bool all_opaque = true;
bool all_transp = true;
const auto full_rect = GetRect();
rect = full_rect.GetSubRect(rect);
auto* p = reinterpret_cast<const uint32_t*>(pixels());
const int stride = pitch() / sizeof(uint32_t);
const auto mask = pixel_format.rgba_to_uint32_t(0, 0, 0, 0xFF);
int xend = (rect.x + rect.width);
int yend = (rect.y + rect.height);
for (int y = rect.y * stride; y < yend * stride; y += stride) {
for (int x = rect.x; x < xend; ++x) {
auto px = p[x + y] & mask;
all_transp &= (px == 0);
all_opaque &= (px == mask);
}
}
return
all_transp ? ImageOpacity::Transparent :
all_opaque ? ImageOpacity::Opaque :
ImageOpacity::Partial;
}
void Bitmap::CheckPixels(uint32_t flags) {
if (flags & Flag_System) {
DynamicFormat format(32,8,24,8,16,8,8,8,0,PF::Alpha);
uint32_t pixel;
Bitmap bmp(reinterpret_cast<void*>(&pixel), 1, 1, 4, format);
pixman_image_composite32(PIXMAN_OP_SRC, bitmap.get(), (pixman_image_t*) NULL, bmp.bitmap.get(),
0, 32, 0, 0, 0, 0, 1, 1);
bg_color = Color((int)(pixel>>24)&0xFF, (int)(pixel>>16)&0xFF, (int)(pixel>>8)&0xFF, (int)pixel&0xFF);
pixman_image_composite32(PIXMAN_OP_SRC, bitmap.get(), (pixman_image_t*) NULL, bmp.bitmap.get(),
16, 32, 0, 0, 0, 0, 1, 1);
sh_color = Color((int)(pixel>>24)&0xFF, (int)(pixel>>16)&0xFF, (int)(pixel>>8)&0xFF, (int)pixel&0xFF);
}
if (flags & Flag_Chipset) {
const int h = height() / TILE_SIZE;
const int w = width() / TILE_SIZE;
tile_opacity = TileOpacity(w, h);
for (int ty = 0; ty < h; ++ty) {
for (int tx = 0; tx < w; ++tx) {
Rect rect(tx * TILE_SIZE, ty * TILE_SIZE, TILE_SIZE, TILE_SIZE);
auto op = ComputeImageOpacity(rect);
tile_opacity.Set(tx, ty, op);
}
}
}
if (flags & Flag_ReadOnly) {
read_only = true;
image_opacity = ComputeImageOpacity();
}
}
void Bitmap::HueChangeBlit(int x, int y, Bitmap const& src, Rect const& src_rect_, double hue_) {
Rect dst_rect(x, y, 0, 0), src_rect = src_rect_;
if (!Rect::AdjustRectangles(src_rect, dst_rect, src.GetRect()))
return;
if (!Rect::AdjustRectangles(dst_rect, src_rect, GetRect()))
return;
int hue = (int) (hue_ / 60.0 * 0x100);
if (hue < 0)
hue += ((-hue + 0x5FF) / 0x600) * 0x600;
else if (hue > 0x600)
hue -= (hue / 0x600) * 0x600;
DynamicFormat format(32,8,24,8,16,8,8,8,0,PF::Alpha);
std::vector<uint32_t> pixels;
pixels.resize(src_rect.width * src_rect.height);
Bitmap bmp(reinterpret_cast<void*>(&pixels.front()), src_rect.width, src_rect.height, src_rect.width * 4, format);
bmp.Blit(0, 0, src, src_rect, Opacity::Opaque());
for (std::vector<uint32_t>::iterator p = pixels.begin(); p != pixels.end(); ++p) {
uint32_t pixel = *p;
uint8_t r = (pixel>>24) & 0xFF;
uint8_t g = (pixel>>16) & 0xFF;
uint8_t b = (pixel>> 8) & 0xFF;
uint8_t a = pixel & 0xFF;
if (a > 0)
RGB_adjust_HSL(r, g, b, hue);
*p = ((uint32_t) r << 24) | ((uint32_t) g << 16) | ((uint32_t) b << 8) | (uint32_t) a;
}
Blit(dst_rect.x, dst_rect.y, bmp, bmp.GetRect(), Opacity::Opaque());
}
void Bitmap::TextDraw(Rect const& rect, int color, StringView text, Text::Alignment align) {
FontRef font = Font::Default();
Rect text_rect = font->GetSize(text);
int dx = text_rect.width - rect.width;
switch (align) {
case Text::AlignLeft:
TextDraw(rect.x, rect.y, color, text);
break;
case Text::AlignCenter:
TextDraw(rect.x + dx / 2, rect.y, color, text);
break;
case Text::AlignRight:
TextDraw(rect.x + dx, rect.y, color, text);
break;
default: assert(false);
}
}
void Bitmap::TextDraw(int x, int y, int color, StringView text, Text::Alignment align) {
auto font = Font::Default();
auto system = Cache::SystemOrBlack();
Text::Draw(*this, x, y, *font, *system, color, text, align);
}
void Bitmap::TextDraw(Rect const& rect, Color color, StringView text, Text::Alignment align) {
FontRef font = Font::Default();
Rect text_rect = font->GetSize(text);
int dx = text_rect.width - rect.width;
switch (align) {
case Text::AlignLeft:
TextDraw(rect.x, rect.y, color, text);
break;
case Text::AlignCenter:
TextDraw(rect.x + dx / 2, rect.y, color, text);
break;
case Text::AlignRight:
TextDraw(rect.x + dx, rect.y, color, text);
break;
default: assert(false);
}
}
void Bitmap::TextDraw(int x, int y, Color color, StringView text) {
auto font = Font::Default();
Text::Draw(*this, x, y, *font, color, text);
}
Rect Bitmap::TransformRectangle(const Transform& xform, const Rect& rect) {
pixman_box16 bounds = {
static_cast<int16_t>(rect.x),
static_cast<int16_t>(rect.y),
static_cast<int16_t>(rect.x + rect.width),
static_cast<int16_t>(rect.y + rect.height)
};
pixman_transform_bounds(&xform.matrix, &bounds);
return Rect(bounds.x1, bounds.y1, bounds.x2 - bounds.x1, bounds.y2 - bounds.y1);
}
static constexpr std::array<std::pair<int,pixman_format_code_t>, 27> formats_map = {{
{ DynamicFormat(32,8,24,8,16,8,8,8,0,PF::Alpha).code_alpha(), PIXMAN_r8g8b8a8 },
{ DynamicFormat(32,8,24,8,16,8,8,8,0,PF::NoAlpha).code_alpha(), PIXMAN_r8g8b8x8 },
{ DynamicFormat(32,8,16,8,8,8,0,8,24,PF::Alpha).code_alpha(), PIXMAN_a8r8g8b8 },
{ DynamicFormat(32,8,16,8,8,8,0,0,0,PF::NoAlpha).code_alpha(), PIXMAN_x8r8g8b8 },
{ DynamicFormat(32,8,0,8,8,8,16,8,24,PF::Alpha).code_alpha(), PIXMAN_a8b8g8r8 },
{ DynamicFormat(32,8,0,8,8,8,16,0,0,PF::NoAlpha).code_alpha(), PIXMAN_x8b8g8r8 },
{ DynamicFormat(32,8,8,8,16,8,24,8,0,PF::Alpha).code_alpha(), PIXMAN_b8g8r8a8 },
{ DynamicFormat(32,8,8,8,16,8,24,0,0,PF::NoAlpha).code_alpha(), PIXMAN_b8g8r8x8 },
{ DynamicFormat(32,6,12,6,6,6,0,0,0,PF::NoAlpha).code_alpha(), PIXMAN_x14r6g6b6 },
{ DynamicFormat(32,10,20,10,10,10,0,0,0,PF::NoAlpha).code_alpha(), PIXMAN_x2r10g10b10 },
{ DynamicFormat(32,10,20,10,10,10,0,2,30,PF::Alpha).code_alpha(), PIXMAN_a2r10g10b10 },
{ DynamicFormat(32,10,0,10,10,10,20,0,0,PF::NoAlpha).code_alpha(), PIXMAN_x2b10g10r10 },
{ DynamicFormat(32,10,0,10,10,10,20,2,30,PF::Alpha).code_alpha(), PIXMAN_a2b10g10r10 },
{ DynamicFormat(24,8,16,8,8,8,0,0,0,PF::NoAlpha).code_alpha(), PIXMAN_r8g8b8 },
{ DynamicFormat(24,8,0,8,8,8,16,0,0,PF::NoAlpha).code_alpha(), PIXMAN_b8g8r8 },
{ DynamicFormat(16,5,11,6,5,5,0,0,0,PF::NoAlpha).code_alpha(), PIXMAN_r5g6b5 },
{ DynamicFormat(16,5,0,6,5,5,11,0,0,PF::NoAlpha).code_alpha(), PIXMAN_b5g6r5 },
{ DynamicFormat(16,5,10,5,5,5,0,1,15,PF::Alpha).code_alpha(), PIXMAN_a1r5g5b5 },
{ DynamicFormat(16,5,10,5,5,5,0,0,0,PF::NoAlpha).code_alpha(), PIXMAN_x1r5g5b5 },
{ DynamicFormat(16,5,0,5,5,5,10,1,15,PF::Alpha).code_alpha(), PIXMAN_a1b5g5r5 },
{ DynamicFormat(16,5,0,5,5,5,10,0,0,PF::NoAlpha).code_alpha(), PIXMAN_x1b5g5r5 },
{ DynamicFormat(16,4,8,4,4,4,0,4,12,PF::Alpha).code_alpha(), PIXMAN_a4r4g4b4 },
{ DynamicFormat(16,4,8,4,4,4,0,0,0,PF::NoAlpha).code_alpha(), PIXMAN_x4r4g4b4 },
{ DynamicFormat(16,4,0,4,4,4,8,4,12,PF::Alpha).code_alpha(), PIXMAN_a4b4g4r4 },
{ DynamicFormat(16,4,0,4,4,4,8,0,0,PF::NoAlpha).code_alpha(), PIXMAN_x4b4g4r4 },
{ DynamicFormat(8,8,0,8,0,8,0,8,0,PF::Alpha).code_alpha(), PIXMAN_g8 },
{ DynamicFormat(8,8,0,8,0,8,0,0,0,PF::NoAlpha).code_alpha(), PIXMAN_g8 }
}};
pixman_format_code_t Bitmap::find_format(const DynamicFormat& format) {
auto dcode = format.code_alpha();
auto iter = std::find_if(formats_map.begin(), formats_map.end(), [dcode](const auto& p) { return p.first == dcode; });
if (iter == formats_map.end()) {
// To fix add a pair to initialize_formats that maps the outputted
// DynamicFormat to a pixman format
Output::Error("{}\nDynamicFormat({}, {}, {}, {}, {}, {}, {}, {}, {}, {})",
"Couldn't find Pixman format for",
format.bits,
format.r.bits, format.r.shift,
format.g.bits, format.g.shift,
format.b.bits, format.b.shift,
format.a.bits, format.a.shift,
format.alpha_type == PF::Alpha ? "PF::Alpha" : "PF::NoAlpha");
}
return iter->second;
}
DynamicFormat Bitmap::pixel_format;
DynamicFormat Bitmap::opaque_pixel_format;
DynamicFormat Bitmap::image_format;
DynamicFormat Bitmap::opaque_image_format;
void Bitmap::SetFormat(const DynamicFormat& format) {
pixel_format = format;
opaque_pixel_format = format;
opaque_pixel_format.alpha_type = PF::NoAlpha;
image_format = format_R8G8B8A8_a().format();
opaque_image_format = format_R8G8B8A8_n().format();
}
DynamicFormat Bitmap::ChooseFormat(const DynamicFormat& format) {
uint32_t amask;
amask = (format.a.mask == 0)
? ((~0U >> (32 - format.bits)) ^ (format.r.mask | format.g.mask | format.b.mask))
: format.a.mask;
if (amask != 0)
return DynamicFormat(format.bits,
format.r.mask, format.g.mask, format.b.mask,
amask, PF::Alpha);
switch (format.bits) {
case 16:
return (format.r.shift > format.b.shift)
? DynamicFormat(16,5,10,5,5,5,0,1,15,PF::Alpha)
: DynamicFormat(16,5,0,5,5,5,10,1,15,PF::Alpha);
case 24:
return (format.r.shift > format.b.shift)
? DynamicFormat(32,8,16,8,8,8,0,8,24,PF::Alpha)
: DynamicFormat(32,8,0,8,8,8,16,8,24,PF::Alpha);
default:
return format_B8G8R8A8_a().format();
}
}
static void destroy_func(pixman_image_t * /* image */, void *data) {
free(data);
}
static pixman_indexed_t palette;
static bool palette_initialized = false;
static void initialize_palette() {
if (palette_initialized)
return;
palette.color = false;
palette.rgba[0] = 0U;
for (int i = 1; i < PIXMAN_MAX_INDEXED; i++)
palette.rgba[i] = ~0U;
palette_initialized = true;
}
void Bitmap::Init(int width, int height, void* data, int pitch, bool destroy) {
if (!pitch)
pitch = width * format.bytes;
bitmap.reset(pixman_image_create_bits(pixman_format, width, height, (uint32_t*) data, pitch));
if (bitmap == NULL) {
Output::Error("Couldn't create {}x{} image.", width, height);
}
if (format.bits == 8) {
initialize_palette();
pixman_image_set_indexed(bitmap.get(), &palette);
}
if (data != NULL && destroy)
pixman_image_set_destroy_function(bitmap.get(), destroy_func, data);
}
void Bitmap::ConvertImage(int& width, int& height, void*& pixels, bool transparent) {
const DynamicFormat& img_format = transparent ? image_format : opaque_image_format;
// premultiply alpha
for (int y = 0; y < height; y++) {
uint8_t* dst = (uint8_t*) pixels + y * width * 4;
for (int x = 0; x < width; x++) {
uint8_t &r = *dst++;
uint8_t &g = *dst++;
uint8_t &b = *dst++;
uint8_t &a = *dst++;
MultiplyAlpha(r, g, b, a);
}
}
Bitmap src(pixels, width, height, 0, img_format);
Clear();
Blit(0, 0, src, src.GetRect(), Opacity::Opaque());
free(pixels);
}
void* Bitmap::pixels() {
if (!bitmap) {
return nullptr;
}
return (void*) pixman_image_get_data(bitmap.get());
}
void const* Bitmap::pixels() const {
return (void const*) pixman_image_get_data(bitmap.get());
}
int Bitmap::bpp() const {
return (pixman_image_get_depth(bitmap.get()) + 7) / 8;
}
int Bitmap::width() const {
return pixman_image_get_width(bitmap.get());
}
int Bitmap::height() const {
return pixman_image_get_height(bitmap.get());
}
int Bitmap::pitch() const {
return pixman_image_get_stride(bitmap.get());
}
namespace {
PixmanImagePtr CreateMask(Opacity const& opacity, Rect const& src_rect, Transform const* pxform = nullptr) {
if (opacity.IsOpaque()) {
return nullptr;
}
if (!opacity.IsSplit()) {
pixman_color_t tcolor = {0, 0, 0, static_cast<uint16_t>(opacity.Value() << 8)};
return PixmanImagePtr{ pixman_image_create_solid_fill(&tcolor) };
}
auto mask = PixmanImagePtr{pixman_image_create_bits(PIXMAN_a8, 1, 2, (uint32_t*) NULL, 4)};
uint32_t* pixels = pixman_image_get_data(mask.get());
*reinterpret_cast<uint8_t*>(&pixels[0]) = (opacity.top & 0xFF);
*reinterpret_cast<uint8_t*>(&pixels[1]) = (opacity.bottom & 0xFF);
Transform xform = Transform::Scale(1.0 / src_rect.width, 1.0 / src_rect.height);
xform *= Transform::Translation(0, opacity.split);
if (pxform)
xform *= *pxform;
pixman_image_set_transform(mask.get(), &xform.matrix);
return mask;
}
} // anonymous namespace
void Bitmap::Blit(int x, int y, Bitmap const& src, Rect const& src_rect, Opacity const& opacity, Bitmap::BlendMode blend_mode) {
if (opacity.IsTransparent()) {
return;
}
auto mask = CreateMask(opacity, src_rect);
pixman_image_composite32(src.GetOperator(mask.get(), blend_mode),
src.bitmap.get(),
mask.get(), bitmap.get(),
src_rect.x, src_rect.y,
0, 0,
x, y,
src_rect.width, src_rect.height);
}
void Bitmap::BlitFast(int x, int y, Bitmap const & src, Rect const & src_rect, Opacity const & opacity) {
if (opacity.IsTransparent()) {
return;
}
pixman_image_composite32(PIXMAN_OP_SRC,
src.bitmap.get(),
nullptr, bitmap.get(),
src_rect.x, src_rect.y,
0, 0,
x, y,
src_rect.width, src_rect.height);
}
PixmanImagePtr Bitmap::GetSubimage(Bitmap const& src, const Rect& src_rect) {
uint8_t* pixels = (uint8_t*) src.pixels() + src_rect.x * src.bpp() + src_rect.y * src.pitch();
return PixmanImagePtr{ pixman_image_create_bits(src.pixman_format, src_rect.width, src_rect.height,
(uint32_t*) pixels, src.pitch()) };
}
void Bitmap::TiledBlit(Rect const& src_rect, Bitmap const& src, Rect const& dst_rect, Opacity const& opacity, Bitmap::BlendMode blend_mode) {
TiledBlit(0, 0, src_rect, src, dst_rect, opacity, blend_mode);
}
void Bitmap::TiledBlit(int ox, int oy, Rect const& src_rect, Bitmap const& src, Rect const& dst_rect, Opacity const& opacity, Bitmap::BlendMode blend_mode) {
if (opacity.IsTransparent()) {
return;
}
if (ox >= src_rect.width) ox %= src_rect.width;
if (oy >= src_rect.height) oy %= src_rect.height;
if (ox < 0) ox += src_rect.width * ((-ox + src_rect.width - 1) / src_rect.width);
if (oy < 0) oy += src_rect.height * ((-oy + src_rect.height - 1) / src_rect.height);
auto src_bm = GetSubimage(src, src_rect);
pixman_image_set_repeat(src_bm.get(), PIXMAN_REPEAT_NORMAL);
auto mask = CreateMask(opacity, src_rect);
pixman_image_composite32(src.GetOperator(mask.get(), blend_mode),
src_bm.get(), mask.get(), bitmap.get(),
ox, oy,
0, 0,
dst_rect.x, dst_rect.y,
dst_rect.width, dst_rect.height);
}
void Bitmap::StretchBlit(Bitmap const& src, Rect const& src_rect, Opacity const& opacity, Bitmap::BlendMode blend_mode) {
StretchBlit(GetRect(), src, src_rect, opacity, blend_mode);
}
void Bitmap::StretchBlit(Rect const& dst_rect, Bitmap const& src, Rect const& src_rect, Opacity const& opacity, Bitmap::BlendMode blend_mode) {
if (opacity.IsTransparent()) {
return;
}
double zoom_x = (double)src_rect.width / dst_rect.width;
double zoom_y = (double)src_rect.height / dst_rect.height;
Transform xform = Transform::Scale(zoom_x, zoom_y);
pixman_image_set_transform(src.bitmap.get(), &xform.matrix);
auto mask = CreateMask(opacity, src_rect, &xform);
pixman_image_composite32(src.GetOperator(mask.get(), blend_mode),
src.bitmap.get(), mask.get(), bitmap.get(),
src_rect.x / zoom_x, src_rect.y / zoom_y,
0, 0,
dst_rect.x, dst_rect.y,
dst_rect.width, dst_rect.height);
pixman_image_set_transform(src.bitmap.get(), nullptr);
}
void Bitmap::WaverBlit(int x, int y, double zoom_x, double zoom_y, Bitmap const& src, Rect const& src_rect, int depth, double phase, Opacity const& opacity, Bitmap::BlendMode blend_mode) {
if (opacity.IsTransparent()) {
return;
}
Transform xform = Transform::Scale(1.0 / zoom_x, 1.0 / zoom_y);
pixman_image_set_transform(src.bitmap.get(), &xform.matrix);
auto mask = CreateMask(opacity, src_rect, &xform);
int height = static_cast<int>(std::floor(src_rect.height * zoom_y));
int width = static_cast<int>(std::floor(src_rect.width * zoom_x));
const auto xoff = src_rect.x * zoom_x;
const auto yoff = src_rect.y * zoom_y;
const auto yclip = y < 0 ? -y : 0;
const auto yend = std::min(height, this->height() - y);
for (int i = yclip; i < yend; i++) {
int dy = y + i;
// RPG_RT starts the effect from the top of the screen even if the image is clipped. The result
// is that moving images which cross the top of the screen can appear to go too fast or too slow
// in RPT_RT. The (i - yclip) is RPG_RT compatible behavior. Just (i) would be more correct.
const double sy = (i - yclip) * (2 * M_PI) / (32.0 * zoom_y);
const int offset = 2 * zoom_x * depth * std::sin(phase + sy);
pixman_image_composite32(src.GetOperator(mask.get(), blend_mode),
src.bitmap.get(), mask.get(), bitmap.get(),
xoff, yoff + i,
0, i,
x + offset, dy,
width, 1);
}
pixman_image_set_transform(src.bitmap.get(), nullptr);
}
static pixman_color_t PixmanColor(const Color &color) {
pixman_color_t pcolor;
pcolor.red = color.red * color.alpha;
pcolor.green = color.green * color.alpha;
pcolor.blue = color.blue * color.alpha;
pcolor.alpha = color.alpha << 8;
return pcolor;
}
void Bitmap::Fill(const Color &color) {
pixman_color_t pcolor = PixmanColor(color);
pixman_box32_t box = { 0, 0, width(), height() };
pixman_image_fill_boxes(PIXMAN_OP_SRC, bitmap.get(), &pcolor, 1, &box);
}
void Bitmap::FillRect(Rect const& dst_rect, const Color &color) {
pixman_color_t pcolor = PixmanColor(color);
auto timage = PixmanImagePtr{pixman_image_create_solid_fill(&pcolor)};
pixman_image_composite32(PIXMAN_OP_OVER,
timage.get(), nullptr, bitmap.get(),
0, 0,
0, 0,
dst_rect.x, dst_rect.y,
dst_rect.width, dst_rect.height);
}
void Bitmap::Clear() {
if (!pixels()) {
// Happens when height or width of bitmap are 0
return;
}
memset(pixels(), '\0', height() * pitch());
}
void Bitmap::ClearRect(Rect const& dst_rect) {
pixman_color_t pcolor = {};
pixman_box32_t box = {
dst_rect.x,
dst_rect.y,
dst_rect.x + dst_rect.width,
dst_rect.y + dst_rect.height
};
pixman_image_fill_boxes(PIXMAN_OP_CLEAR, bitmap.get(), &pcolor, 1, &box);
}
// Hard light lookup table mapping source color to destination color
// FIXME: Replace this with std::array<std::array<uint8_t,256>,256> when we have C++17
struct HardLightTable {
uint8_t table[256][256] = {};
};
static constexpr HardLightTable make_hard_light_lookup() {
HardLightTable hl;
for (int i = 0; i < 256; ++i) {
for (int j = 0; j < 256; ++j) {
int res = 0;
if (i <= 128)
res = (2 * i * j) / 255;
else
res = 255 - 2 * (255 - i) * (255 - j) / 255;
hl.table[i][j] = res > 255 ? 255 : res < 0 ? 0 : res;
}
}
return hl;
}
constexpr auto hard_light = make_hard_light_lookup();
// Saturation Tone Inline: Changes a pixel saturation
static inline void saturation_tone(uint32_t &src_pixel, int saturation, int rs, int gs, int bs, int as) {
// Algorithm from OpenPDN (MIT license)
// Transformation in Y'CbCr color space
uint8_t r = (src_pixel >> rs) & 0xFF;
uint8_t g = (src_pixel >> gs) & 0xFF;
uint8_t b = (src_pixel >> bs) & 0xFF;
uint8_t a = (src_pixel >> as) & 0xFF;
// Y' = 0.299 R' + 0.587 G' + 0.114 B'
uint8_t lum = (7471 * b + 38470 * g + 19595 * r) >> 16;
// Scale Cb/Cr by scale factor "sat"
int red = ((lum * 1024 + (r - lum) * saturation) >> 10);
red = red > 255 ? 255 : red < 0 ? 0 : red;
int green = ((lum * 1024 + (g - lum) * saturation) >> 10);
green = green > 255 ? 255 : green < 0 ? 0 : green;
int blue = ((lum * 1024 + (b - lum) * saturation) >> 10);
blue = blue > 255 ? 255 : blue < 0 ? 0 : blue;
src_pixel = ((uint32_t)red << rs) | ((uint32_t)green << gs) | ((uint32_t)blue << bs) | ((uint32_t)a << as);
}
// Color Tone Inline: Changes color of a pixel by hard light table
static inline void color_tone(uint32_t &src_pixel, Tone tone, int rs, int gs, int bs, int as) {
src_pixel = ((uint32_t)hard_light.table[tone.red][(src_pixel >> rs) & 0xFF] << rs)
| ((uint32_t)hard_light.table[tone.green][(src_pixel >> gs) & 0xFF] << gs)
| ((uint32_t)hard_light.table[tone.blue][(src_pixel >> bs) & 0xFF] << bs)
| ((uint32_t)((src_pixel >> as) & 0xFF) << as);
}
void Bitmap::ToneBlit(int x, int y, Bitmap const& src, Rect const& src_rect, const Tone &tone, Opacity const& opacity, bool check_alpha) {
if (opacity.IsTransparent()) {
return;
}
if (tone == Tone(128,128,128,128)) {
if (&src != this) {
Blit(x, y, src, src_rect, opacity);
}
return;
}
// Only needed here, other codepaths are sanity checked by pixman
if (x < 0 || y < 0 || x >= width() || y >= height()) {
return;
}
if (&src != this)
pixman_image_composite32(src.GetOperator(),
src.bitmap.get(), nullptr, bitmap.get(),
src_rect.x, src_rect.y,
0, 0,
x, y,
src_rect.width, src_rect.height);
int as = pixel_format.a.shift;
int rs = pixel_format.r.shift;
int gs = pixel_format.g.shift;
int bs = pixel_format.b.shift;
int next_row = pitch() / sizeof(uint32_t);
uint32_t* pixels = (uint32_t*)this->pixels();
pixels = pixels + (y - 1) * next_row + x;
uint16_t limit_height = std::min<uint16_t>(src_rect.height, height());
uint16_t limit_width = std::min<uint16_t>(src_rect.width, width());
// If Saturation + Color:
if (tone.gray != 128 && (tone.red != 128 || tone.green != 128 || tone.blue != 128)) {
int sat = tone.gray > 128 ? 1024 + (tone.gray - 128) * 16 : tone.gray * 8;
if (&src != this || check_alpha) {
for (uint16_t i = 0; i < limit_height; ++i) {
pixels += next_row;
for (uint16_t j = 0; j < limit_width; ++j) {
if ((uint8_t)((pixels[j] >> as) & 0xFF) == 0)
continue;
saturation_tone(pixels[j], sat, rs, gs, bs, as);
color_tone(pixels[j], tone, rs, gs, bs, as);
}
}
}
else {
for (uint16_t i = 0; i < limit_height; ++i) {
pixels += next_row;
for (uint16_t j = 0; j < limit_width; ++j) {
saturation_tone(pixels[j], sat, rs, gs, bs, as);
color_tone(pixels[j], tone, rs, gs, bs, as);
}
}
}
}
// If Only Saturation:
else if (tone.gray != 128) {
int sat = tone.gray > 128 ? 1024 + (tone.gray - 128) * 16 : tone.gray * 8;
if (&src != this || check_alpha) {
for (uint16_t i = 0; i < limit_height; ++i) {
pixels += next_row;
for (uint16_t j = 0; j < limit_width; ++j) {
if ((uint8_t)((pixels[j] >> as) & 0xFF) == 0)
continue;
saturation_tone(pixels[j], sat, rs, gs, bs, as);
}
}
}
else {
for (uint16_t i = 0; i < limit_height; ++i) {
pixels += next_row;
for (uint16_t j = 0; j < limit_width; ++j) {
saturation_tone(pixels[j], sat, rs, gs, bs, as);
}
}
}
}
// If Only Color:
else if (tone.red != 128 || tone.green != 128 || tone.blue != 128) {
if (&src != this || check_alpha) {
for (uint16_t i = 0; i < limit_height; ++i) {
pixels += next_row;
for (uint16_t j = 0; j < limit_width; ++j) {
if ((uint8_t)((pixels[j] >> as) & 0xFF) == 0)
continue;
color_tone(pixels[j], tone, rs, gs, bs, as);
}
}
}
else {
for (uint16_t i = 0; i < limit_height; ++i) {
pixels += next_row;
for (uint16_t j = 0; j < limit_width; ++j) {
color_tone(pixels[j], tone, rs, gs, bs, as);
}
}
}
}
}
void Bitmap::BlendBlit(int x, int y, Bitmap const& src, Rect const& src_rect, const Color& color, Opacity const& opacity) {
if (opacity.IsTransparent()) {
return;
}
if (color.alpha == 0) {
if (&src != this)
Blit(x, y, src, src_rect, opacity);
return;
}
if (&src != this)
pixman_image_composite32(src.GetOperator(),
src.bitmap.get(), nullptr, bitmap.get(),
src_rect.x, src_rect.y,
0, 0,
x, y,
src_rect.width, src_rect.height);
pixman_color_t tcolor = PixmanColor(color);
auto timage = PixmanImagePtr{ pixman_image_create_solid_fill(&tcolor) };
pixman_image_composite32(PIXMAN_OP_OVER,
timage.get(), src.bitmap.get(), bitmap.get(),
0, 0,
src_rect.x, src_rect.y,
x, y,
src_rect.width, src_rect.height);
}
void Bitmap::FlipBlit(int x, int y, Bitmap const& src, Rect const& src_rect, bool horizontal, bool vertical, Opacity const& opacity, Bitmap::BlendMode blend_mode) {
if (opacity.IsTransparent()) {
return;
}
bool has_xform = (horizontal || vertical);
const auto img_w = src.GetWidth();
const auto img_h = src.GetHeight();
auto rect = src_rect;
if (has_xform) {
Transform xform = Transform::Scale(horizontal ? -1 : 1, vertical ? -1 : 1);
xform *= Transform::Translation(horizontal ? -img_w : 0, vertical ? -img_h : 0);
pixman_image_set_transform(src.bitmap.get(), &xform.matrix);
const auto src_x = horizontal ? img_w - src_rect.x - src_rect.width : src_rect.x;
const auto src_y = vertical ? img_h - src_rect.y - src_rect.height : src_rect.y;
rect = Rect{ src_x, src_y, src_rect.width, src_rect.height };
}
Blit(x, y, src, rect, opacity, blend_mode);
if (has_xform) {
pixman_image_set_transform(src.bitmap.get(), nullptr);
}
}
void Bitmap::Flip(bool horizontal, bool vertical) {
if (!horizontal && !vertical) {
return;
}
const auto w = GetWidth();
const auto h = GetHeight();
const auto p = pitch();
auto temp = PixmanImagePtr{ pixman_image_create_bits(pixman_format, w, h, nullptr, p) };
std::memcpy(pixman_image_get_data(temp.get()),
pixman_image_get_data(bitmap.get()),
p * h);
Transform xform = Transform::Scale(horizontal ? -1 : 1, vertical ? -1 : 1);
xform *= Transform::Translation(horizontal ? -w : 0, vertical ? -h : 0);
pixman_image_set_transform(temp.get(), &xform.matrix);
pixman_image_composite32(PIXMAN_OP_SRC,
temp.get(), nullptr, bitmap.get(),
0, 0, 0, 0, 0, 0, w, h);