/
image.c
1014 lines (884 loc) · 28.7 KB
/
image.c
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
#include "minisphere.h"
#include "api.h"
#include "color.h"
#include "surface.h"
#include "image.h"
struct image
{
int refcount;
unsigned int id;
ALLEGRO_BITMAP* bitmap;
unsigned int cache_hits;
image_lock_t lock;
unsigned int lock_count;
color_t* pixel_cache;
int width;
int height;
image_t* parent;
};
static duk_ret_t js_GetSystemArrow (duk_context* ctx);
static duk_ret_t js_GetSystemDownArrow (duk_context* ctx);
static duk_ret_t js_GetSystemUpArrow (duk_context* ctx);
static duk_ret_t js_LoadImage (duk_context* ctx);
static duk_ret_t js_GrabImage (duk_context* ctx);
static duk_ret_t js_new_Image (duk_context* ctx);
static duk_ret_t js_Image_finalize (duk_context* ctx);
static duk_ret_t js_Image_toString (duk_context* ctx);
static duk_ret_t js_Image_get_height (duk_context* ctx);
static duk_ret_t js_Image_get_width (duk_context* ctx);
static duk_ret_t js_Image_blit (duk_context* ctx);
static duk_ret_t js_Image_blitMask (duk_context* ctx);
static duk_ret_t js_Image_createSurface (duk_context* ctx);
static duk_ret_t js_Image_rotateBlit (duk_context* ctx);
static duk_ret_t js_Image_rotateBlitMask (duk_context* ctx);
static duk_ret_t js_Image_transformBlit (duk_context* ctx);
static duk_ret_t js_Image_transformBlitMask (duk_context* ctx);
static duk_ret_t js_Image_zoomBlit (duk_context* ctx);
static duk_ret_t js_Image_zoomBlitMask (duk_context* ctx);
static void cache_pixels (image_t* image);
static void uncache_pixels (image_t* image);
static unsigned int s_next_image_id = 0;
static image_t* s_sys_arrow = NULL;
static image_t* s_sys_dn_arrow = NULL;
static image_t* s_sys_up_arrow = NULL;
image_t*
create_image(int width, int height)
{
image_t* image;
if ((image = calloc(1, sizeof(image_t))) == NULL)
goto on_error;
if ((image->bitmap = al_create_bitmap(width, height)) == NULL)
goto on_error;
image->id = s_next_image_id++;
image->width = al_get_bitmap_width(image->bitmap);
image->height = al_get_bitmap_height(image->bitmap);
console_log(3, "engine: Created new %i*%i image [%u]\n", image->width, image->height, image->id);
return ref_image(image);
on_error:
free(image);
return NULL;
}
image_t*
create_subimage(image_t* parent, int x, int y, int width, int height)
{
image_t* image;
if ((image = calloc(1, sizeof(image_t))) == NULL) goto on_error;
if ((image->bitmap = al_create_sub_bitmap(parent->bitmap, x, y, width, height)) == NULL)
goto on_error;
image->id = s_next_image_id++;
image->width = al_get_bitmap_width(image->bitmap);
image->height = al_get_bitmap_height(image->bitmap);
image->parent = ref_image(parent);
console_log(3, "engine: Created %i*%i subimage [%u -> %u]\n", image->width, image->height, parent->id, image->id);
return ref_image(image);
on_error:
free(image);
return NULL;
}
image_t*
create_surface(int width, int height)
{
image_t* image;
int bitmap_flags;
bitmap_flags = al_get_new_bitmap_flags();
al_set_new_bitmap_flags(bitmap_flags & ALLEGRO_MEMORY_BITMAP);
image = create_image(width, height);
al_set_new_bitmap_flags(bitmap_flags);
return image;
}
image_t*
clone_image(const image_t* src_image)
{
image_t* image;
if ((image = calloc(1, sizeof(image_t))) == NULL)
goto on_error;
if ((image->bitmap = al_clone_bitmap(src_image->bitmap)) == NULL)
goto on_error;
image->id = s_next_image_id++;
image->width = al_get_bitmap_width(image->bitmap);
image->height = al_get_bitmap_height(image->bitmap);
console_log(3, "engine: Created %i*%i image via clone [%u -> %u]\n", image->width, image->height, src_image->id, image->id);
return ref_image(image);
on_error:
free(image);
return NULL;
}
image_t*
clone_surface(const image_t* src_image)
{
int bitmap_flags;
image_t* image;
bitmap_flags = al_get_new_bitmap_flags();
if ((image = calloc(1, sizeof(image_t))) == NULL)
goto on_error;
al_set_new_bitmap_flags(bitmap_flags & ALLEGRO_MEMORY_BITMAP);
if ((image->bitmap = al_clone_bitmap(src_image->bitmap)) == NULL)
goto on_error;
al_set_new_bitmap_flags(bitmap_flags);
image->id = s_next_image_id++;
image->width = al_get_bitmap_width(image->bitmap);
image->height = al_get_bitmap_height(image->bitmap);
console_log(3, "engine: Created %i*%i surface via clone [%u -> %u]\n", image->width, image->height, src_image->id, image->id);
return ref_image(image);
on_error:
al_set_new_bitmap_flags(bitmap_flags);
free(image);
return NULL;
}
image_t*
load_image(const char* path)
{
image_t* image;
if ((image = calloc(1, sizeof(image_t))) == NULL)
goto on_error;
if ((image->bitmap = al_load_bitmap(path)) == NULL)
goto on_error;
image->id = s_next_image_id++;
image->width = al_get_bitmap_width(image->bitmap);
image->height = al_get_bitmap_height(image->bitmap);
console_log(2, "engine: Loaded %i*%i image %s [%u]\n", image->width, image->height, path, image->id);
return ref_image(image);
on_error:
free(image);
return NULL;
}
image_t*
read_image(FILE* file, int width, int height)
{
long file_pos;
image_t* image;
uint8_t* line_ptr;
size_t line_size;
ALLEGRO_LOCKED_REGION* lock = NULL;
int i_y;
file_pos = ftell(file);
if ((image = calloc(1, sizeof(image_t))) == NULL) goto on_error;
if ((image->bitmap = al_create_bitmap(width, height)) == NULL) goto on_error;
if ((lock = al_lock_bitmap(image->bitmap, ALLEGRO_PIXEL_FORMAT_ABGR_8888, ALLEGRO_LOCK_WRITEONLY)) == NULL)
goto on_error;
line_size = width * 4;
for (i_y = 0; i_y < height; ++i_y) {
line_ptr = (uint8_t*)lock->data + i_y * lock->pitch;
if (fread(line_ptr, line_size, 1, file) != 1)
goto on_error;
}
al_unlock_bitmap(image->bitmap);
image->id = s_next_image_id++;
image->width = al_get_bitmap_width(image->bitmap);
image->height = al_get_bitmap_height(image->bitmap);
console_log(3, "engine: Read %i*%i image from open file [%u]\n", image->width, image->height, image->id);
return ref_image(image);
on_error:
fseek(file, file_pos, SEEK_SET);
if (lock != NULL) al_unlock_bitmap(image->bitmap);
if (image != NULL) {
if (image->bitmap != NULL) al_destroy_bitmap(image->bitmap);
free(image);
}
return NULL;
}
image_t*
read_subimage(FILE* file, image_t* parent, int x, int y, int width, int height)
{
long file_pos;
image_t* image;
image_lock_t* lock = NULL;
color_t *pline;
int i_y;
file_pos = ftell(file);
if (!(image = create_subimage(parent, x, y, width, height))) goto on_error;
if (!(lock = lock_image(parent))) goto on_error;
for (i_y = 0; i_y < height; ++i_y) {
pline = lock->pixels + x + (i_y + y) * lock->pitch;
if (fread(pline, width * 4, 1, file) != 1)
goto on_error;
}
unlock_image(parent, lock);
return image;
on_error:
fseek(file, file_pos, SEEK_SET);
if (lock != NULL)
unlock_image(parent, lock);
free_image(image);
return NULL;
}
image_t*
ref_image(image_t* image)
{
if (image != NULL)
++image->refcount;
return image;
}
void
free_image(image_t* image)
{
if (image == NULL || --image->refcount > 0)
return;
console_log(3, "image %u: No more references, freeing image\n", image->id);
uncache_pixels(image);
al_destroy_bitmap(image->bitmap);
free_image(image->parent);
free(image);
}
ALLEGRO_BITMAP*
get_image_bitmap(image_t* image)
{
uncache_pixels(image);
return image->bitmap;
}
int
get_image_height(const image_t* image)
{
return image->height;
}
color_t
get_image_pixel(image_t* image, int x, int y)
{
if (image->pixel_cache == NULL) {
console_log(4, "image %u: get_image_pixel() cache miss!\n", image->id);
cache_pixels(image);
}
else
++image->cache_hits;
return image->pixel_cache[x + y * image->width];
}
int
get_image_width(const image_t* image)
{
return image->width;
}
void
set_image_pixel(image_t* image, int x, int y, color_t color)
{
ALLEGRO_BITMAP* old_target;
uncache_pixels(image);
old_target = al_get_target_bitmap();
al_set_target_bitmap(image->bitmap);
al_put_pixel(x, y, nativecolor(color));
al_set_target_bitmap(old_target);
}
bool
apply_color_matrix(image_t* image, colormatrix_t matrix, int x, int y, int width, int height)
{
image_lock_t* lock;
color_t* pixel;
int i_x, i_y;
if (!(lock = lock_image(image)))
return false;
uncache_pixels(image);
for (i_x = x; i_x < x + width; ++i_x) for (i_y = y; i_y < y + height; ++i_y) {
pixel = &lock->pixels[i_x + i_y * lock->pitch];
*pixel = transform_pixel(*pixel, matrix);
}
unlock_image(image, lock);
return true;
}
bool
apply_color_matrix_4(image_t* image, colormatrix_t ul_mat, colormatrix_t ur_mat, colormatrix_t ll_mat, colormatrix_t lr_mat, int x, int y, int w, int h)
{
int i1, i2, i3;
image_lock_t* lock;
colormatrix_t mat_1, mat_2, mat_3;
color_t* pixel;
int i_x, i_y;
if (!(lock = lock_image(image)))
return false;
uncache_pixels(image);
// process the transformation
#define INTERP(mat, cell, src1, src2) \
mat.cell = (src1.cell * i1 + src2.cell * i2) / i3
for (i_y = y; i_y < y + h; ++i_y) {
// phase 1: calculate the matrices for the current line.
// this gives us two color matrices which will be interpolated to transform the
// pixels on this line. essentially what we're doing is just bilinear interpolation,
// but with matrices. it looks more complicated than it is.
i1 = y + h - 1 - i_y;
i2 = i_y - y;
i3 = h - 1;
INTERP(mat_1, rn, ul_mat, ll_mat); INTERP(mat_2, rn, ur_mat, lr_mat);
INTERP(mat_1, rr, ul_mat, ll_mat); INTERP(mat_2, rr, ur_mat, lr_mat);
INTERP(mat_1, rg, ul_mat, ll_mat); INTERP(mat_2, rg, ur_mat, lr_mat);
INTERP(mat_1, rb, ul_mat, ll_mat); INTERP(mat_2, rb, ur_mat, lr_mat);
INTERP(mat_1, gn, ul_mat, ll_mat); INTERP(mat_2, gn, ur_mat, lr_mat);
INTERP(mat_1, gr, ul_mat, ll_mat); INTERP(mat_2, gr, ur_mat, lr_mat);
INTERP(mat_1, gg, ul_mat, ll_mat); INTERP(mat_2, gg, ur_mat, lr_mat);
INTERP(mat_1, gb, ul_mat, ll_mat); INTERP(mat_2, gb, ur_mat, lr_mat);
INTERP(mat_1, bn, ul_mat, ll_mat); INTERP(mat_2, bn, ur_mat, lr_mat);
INTERP(mat_1, br, ul_mat, ll_mat); INTERP(mat_2, br, ur_mat, lr_mat);
INTERP(mat_1, bg, ul_mat, ll_mat); INTERP(mat_2, bg, ur_mat, lr_mat);
INTERP(mat_1, bb, ul_mat, ll_mat); INTERP(mat_2, bb, ur_mat, lr_mat);
for (i_x = x; i_x < x + w; ++i_x) {
// phase 2: apply the pixel transformation.
// the two matrices calculated above are interpolated as we move from
// left to right. this gives us a final matrix we can use to transform
// the pixel.
i1 = x + w - 1 - i_x;
i2 = i_x - x;
i3 = w - 1;
INTERP(mat_3, rn, mat_1, mat_2); INTERP(mat_3, gn, mat_1, mat_2); INTERP(mat_3, bn, mat_1, mat_2);
INTERP(mat_3, rr, mat_1, mat_2); INTERP(mat_3, gr, mat_1, mat_2); INTERP(mat_3, br, mat_1, mat_2);
INTERP(mat_3, rg, mat_1, mat_2); INTERP(mat_3, gg, mat_1, mat_2); INTERP(mat_3, bg, mat_1, mat_2);
INTERP(mat_3, rb, mat_1, mat_2); INTERP(mat_3, gb, mat_1, mat_2); INTERP(mat_3, bb, mat_1, mat_2);
// finally, we can transform the pixel!
pixel = &lock->pixels[i_x + i_y * lock->pitch];
*pixel = transform_pixel(*pixel, mat_3);
}
}
#undef INTERP
unlock_image(image, lock);
return true;
}
bool
apply_image_lookup(image_t* image, int x, int y, int width, int height, uint8_t red_lu[256], uint8_t green_lu[256], uint8_t blue_lu[256], uint8_t alpha_lu[256])
{
ALLEGRO_BITMAP* bitmap = get_image_bitmap(image);
uint8_t* pixel;
ALLEGRO_LOCKED_REGION* lock;
int i_x, i_y;
if ((lock = al_lock_bitmap(bitmap, ALLEGRO_PIXEL_FORMAT_ABGR_8888, ALLEGRO_LOCK_READWRITE)) == NULL)
return false;
uncache_pixels(image);
for (i_x = x; i_x < x + width; ++i_x) for (i_y = y; i_y < y + height; ++i_y) {
pixel = (uint8_t*)lock->data + i_x * 4 + i_y * lock->pitch;
pixel[0] = red_lu[pixel[0]];
pixel[1] = green_lu[pixel[1]];
pixel[2] = blue_lu[pixel[2]];
pixel[3] = alpha_lu[pixel[3]];
}
al_unlock_bitmap(bitmap);
return true;
}
void
draw_image(image_t* image, int x, int y)
{
al_draw_bitmap(image->bitmap, x, y, 0x0);
}
void
draw_image_masked(image_t* image, color_t mask, int x, int y)
{
al_draw_tinted_bitmap(image->bitmap, al_map_rgba(mask.r, mask.g, mask.b, mask.alpha), x, y, 0x0);
}
void
draw_image_scaled(image_t* image, int x, int y, int width, int height)
{
al_draw_scaled_bitmap(image->bitmap,
0, 0, al_get_bitmap_width(image->bitmap), al_get_bitmap_height(image->bitmap),
x, y, width, height, 0x0);
}
void
draw_image_scaled_masked(image_t* image, color_t mask, int x, int y, int width, int height)
{
al_draw_tinted_scaled_bitmap(image->bitmap, nativecolor(mask),
0, 0, al_get_bitmap_width(image->bitmap), al_get_bitmap_height(image->bitmap),
x, y, width, height, 0x0);
}
void
draw_image_tiled(image_t* image, int x, int y, int width, int height)
{
draw_image_tiled_masked(image, rgba(255, 255, 255, 255), x, y, width, height);
}
void
draw_image_tiled_masked(image_t* image, color_t mask, int x, int y, int width, int height)
{
ALLEGRO_COLOR native_mask = nativecolor(mask);
int img_w, img_h;
bool is_drawing_held;
int tile_w, tile_h;
int i_x, i_y;
img_w = image->width; img_h = image->height;
if (img_w >= 16 && img_h >= 16) {
// tile in hardware whenever possible
ALLEGRO_VERTEX vbuf[] = {
{ x, y, 0, 0, 0, native_mask },
{ x + width, y, 0, width, 0, native_mask },
{ x, y + height, 0, 0, height, native_mask },
{ x + width, y + height, 0, width, height, native_mask }
};
al_draw_prim(vbuf, NULL, image->bitmap, 0, 4, ALLEGRO_PRIM_TRIANGLE_STRIP);
}
else {
// texture smaller than 16x16, tile it in software (Allegro pads it)
is_drawing_held = al_is_bitmap_drawing_held();
al_hold_bitmap_drawing(true);
for (i_x = width / img_w; i_x >= 0; --i_x) for (i_y = height / img_h; i_y >= 0; --i_y) {
tile_w = i_x == width / img_w ? width % img_w : img_w;
tile_h = i_y == height / img_h ? height % img_h : img_h;
al_draw_tinted_bitmap_region(image->bitmap, native_mask,
0, 0, tile_w, tile_h,
x + i_x * img_w, y + i_y * img_h, 0x0);
}
al_hold_bitmap_drawing(is_drawing_held);
}
}
void
fill_image(image_t* image, color_t color)
{
int clip_x, clip_y, clip_w, clip_h;
ALLEGRO_BITMAP* last_target;
uncache_pixels(image);
al_get_clipping_rectangle(&clip_x, &clip_y, &clip_w, &clip_h);
al_reset_clipping_rectangle();
last_target = al_get_target_bitmap();
al_set_target_bitmap(image->bitmap);
al_clear_to_color(al_map_rgba(color.r, color.g, color.b, color.alpha));
al_set_target_bitmap(last_target);
al_set_clipping_rectangle(clip_x, clip_y, clip_w, clip_h);
}
bool
flip_image(image_t* image, bool is_h_flip, bool is_v_flip)
{
int draw_flags = 0x0;
ALLEGRO_BITMAP* new_bitmap;
ALLEGRO_BITMAP* old_target;
if (!is_h_flip && !is_v_flip) // this really shouldn't happen...
return true;
uncache_pixels(image);
if (!(new_bitmap = al_create_bitmap(image->width, image->height))) return false;
old_target = al_get_target_bitmap();
al_set_target_bitmap(new_bitmap);
if (is_h_flip) draw_flags &= ALLEGRO_FLIP_HORIZONTAL;
if (is_v_flip) draw_flags &= ALLEGRO_FLIP_VERTICAL;
al_draw_bitmap(image->bitmap, 0, 0, draw_flags);
al_set_target_bitmap(old_target);
al_destroy_bitmap(image->bitmap);
image->bitmap = new_bitmap;
return true;
}
image_lock_t*
lock_image(image_t* image)
{
ALLEGRO_LOCKED_REGION* ll_lock;
if (image->lock_count == 0) {
if (!(ll_lock = al_lock_bitmap(image->bitmap, ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE, ALLEGRO_LOCK_READWRITE)))
return NULL;
ref_image(image);
image->lock.pixels = ll_lock->data;
image->lock.pitch = ll_lock->pitch / 4;
}
++image->lock_count;
return &image->lock;
}
bool
replace_image_color(image_t* image, color_t color, color_t new_color)
{
ALLEGRO_BITMAP* bitmap = get_image_bitmap(image);
uint8_t* pixel;
ALLEGRO_LOCKED_REGION* lock;
int w, h;
int i_x, i_y;
if ((lock = al_lock_bitmap(bitmap, ALLEGRO_PIXEL_FORMAT_ABGR_8888, ALLEGRO_LOCK_READWRITE)) == NULL)
return false;
uncache_pixels(image);
w = al_get_bitmap_width(bitmap);
h = al_get_bitmap_height(bitmap);
for (i_x = 0; i_x < w; ++i_x) for (i_y = 0; i_y < h; ++i_y) {
pixel = (uint8_t*)lock->data + i_x * 4 + i_y * lock->pitch;
if (pixel[0] == color.r &&
pixel[1] == color.g &&
pixel[2] == color.b &&
pixel[3] == color.alpha)
{
pixel[0] = new_color.r;
pixel[1] = new_color.g;
pixel[2] = new_color.b;
pixel[3] = new_color.alpha;
}
}
al_unlock_bitmap(bitmap);
return true;
}
bool
rescale_image(image_t* image, int width, int height)
{
ALLEGRO_BITMAP* new_bitmap;
ALLEGRO_BITMAP* old_target;
if (width == image->width && height == image->height)
return true;
if (!(new_bitmap = al_create_bitmap(width, height))) return false;
uncache_pixels(image);
old_target = al_get_target_bitmap();
al_set_target_bitmap(new_bitmap);
al_draw_scaled_bitmap(image->bitmap, 0, 0, image->width, image->height, 0, 0, width, height, 0x0);
al_set_target_bitmap(old_target);
al_destroy_bitmap(image->bitmap);
image->bitmap = new_bitmap;
image->width = al_get_bitmap_width(image->bitmap);
image->height = al_get_bitmap_height(image->bitmap);
return true;
}
void
unlock_image(image_t* image, image_lock_t* lock)
{
// if the caller provides the wrong lock pointer, the image
// won't be unlocked. this prevents accidental unlocking.
if (lock != &image->lock) return;
if (image->lock_count == 0 || --image->lock_count > 0)
return;
al_unlock_bitmap(image->bitmap);
free_image(image);
}
static void
cache_pixels(image_t* image)
{
color_t* cache;
image_lock_t* lock;
void *psrc, *pdest;
int i;
free(image->pixel_cache); image->pixel_cache = NULL;
if (!(lock = lock_image(image)))
goto on_error;
if (!(cache = malloc(image->width * image->height * 4)))
goto on_error;
console_log(4, "image %u: Creating new pixel cache\n", image->id);
for (i = 0; i < image->height; ++i) {
psrc = lock->pixels + i * lock->pitch;
pdest = cache + i * image->width;
memcpy(pdest, psrc, image->width * 4);
}
unlock_image(image, lock);
image->pixel_cache = cache;
image->cache_hits = 0;
return;
on_error:
if (lock != NULL)
al_unlock_bitmap(image->bitmap);
}
static void
uncache_pixels(image_t* image)
{
if (image->pixel_cache == NULL)
return;
console_log(4, "image %u: Pixel cache invalidated, hits: %u\n", image->id, image->cache_hits);
free(image->pixel_cache);
image->pixel_cache = NULL;
}
void
init_image_api(duk_context* ctx)
{
const char* filename;
char* path;
// load system-provided images
if (g_sys_conf != NULL) {
filename = al_get_config_value(g_sys_conf, NULL, "Arrow");
path = get_sys_asset_path(filename, "system");
s_sys_arrow = load_image(path);
free(path);
filename = al_get_config_value(g_sys_conf, NULL, "UpArrow");
path = get_sys_asset_path(filename, "system");
s_sys_up_arrow = load_image(path);
free(path);
filename = al_get_config_value(g_sys_conf, NULL, "DownArrow");
path = get_sys_asset_path(filename, "system");
s_sys_dn_arrow = load_image(path);
free(path);
}
// register image API functions
register_api_function(ctx, NULL, "GetSystemArrow", js_GetSystemArrow);
register_api_function(ctx, NULL, "GetSystemDownArrow", js_GetSystemDownArrow);
register_api_function(ctx, NULL, "GetSystemUpArrow", js_GetSystemUpArrow);
register_api_function(ctx, NULL, "LoadImage", js_LoadImage);
register_api_function(ctx, NULL, "GrabImage", js_GrabImage);
// register Image properties and methods
register_api_ctor(ctx, "Image", js_new_Image, js_Image_finalize);
register_api_function(ctx, "Image", "toString", js_Image_toString);
register_api_prop(ctx, "Image", "height", js_Image_get_height, NULL);
register_api_prop(ctx, "Image", "width", js_Image_get_width, NULL);
register_api_function(ctx, "Image", "blit", js_Image_blit);
register_api_function(ctx, "Image", "blitMask", js_Image_blitMask);
register_api_function(ctx, "Image", "createSurface", js_Image_createSurface);
register_api_function(ctx, "Image", "rotateBlit", js_Image_rotateBlit);
register_api_function(ctx, "Image", "rotateBlitMask", js_Image_rotateBlitMask);
register_api_function(ctx, "Image", "transformBlit", js_Image_transformBlit);
register_api_function(ctx, "Image", "transformBlitMask", js_Image_transformBlitMask);
register_api_function(ctx, "Image", "zoomBlit", js_Image_zoomBlit);
register_api_function(ctx, "Image", "zoomBlitMask", js_Image_zoomBlitMask);
}
void
duk_push_sphere_image(duk_context* ctx, image_t* image)
{
duk_push_sphere_obj(ctx, "Image", ref_image(image));
}
image_t*
duk_require_sphere_image(duk_context* ctx, duk_idx_t index)
{
return duk_require_sphere_obj(ctx, index, "Image");
}
static duk_ret_t
js_GetSystemArrow(duk_context* ctx)
{
if (s_sys_arrow == NULL)
duk_error_ni(ctx, -1, DUK_ERR_REFERENCE_ERROR, "GetSystemArrow(): No system arrow image available");
duk_push_sphere_image(ctx, s_sys_arrow);
return 1;
}
static duk_ret_t
js_GetSystemDownArrow(duk_context* ctx)
{
if (s_sys_dn_arrow == NULL)
duk_error_ni(ctx, -1, DUK_ERR_REFERENCE_ERROR, "GetSystemDownArrow(): No system down arrow image available");
duk_push_sphere_image(ctx, s_sys_dn_arrow);
return 1;
}
static duk_ret_t
js_GetSystemUpArrow(duk_context* ctx)
{
if (s_sys_up_arrow == NULL)
duk_error_ni(ctx, -1, DUK_ERR_REFERENCE_ERROR, "GetSystemUpArrow(): No system up arrow image available");
duk_push_sphere_image(ctx, s_sys_up_arrow);
return 1;
}
static duk_ret_t
js_LoadImage(duk_context* ctx)
{
duk_require_string(ctx, 0);
if (duk_safe_call(ctx, js_new_Image, 0, 1) != 0)
duk_throw(ctx);
return 1;
}
static duk_ret_t
js_GrabImage(duk_context* ctx)
{
int x = duk_require_int(ctx, 0) * g_scale_x;
int y = duk_require_int(ctx, 1) * g_scale_y;
int w = duk_require_int(ctx, 2) * g_scale_x;
int h = duk_require_int(ctx, 3) * g_scale_y;
ALLEGRO_BITMAP* backbuffer;
image_t* image;
backbuffer = al_get_backbuffer(g_display);
if ((image = create_image(w, h)) == NULL)
duk_error_ni(ctx, -1, DUK_ERR_ERROR, "GrabImage(): Failed to create new image");
al_set_target_bitmap(get_image_bitmap(image));
al_draw_bitmap_region(backbuffer, x, y, w, h, 0, 0, 0x0);
al_set_target_backbuffer(g_display);
if (!rescale_image(image, g_res_x, g_res_y))
duk_error_ni(ctx, -1, DUK_ERR_ERROR, "GrabImage(): Failed to rescale grabbed image");
duk_push_sphere_image(ctx, image);
free_image(image);
return 1;
}
static duk_ret_t
js_new_Image(duk_context* ctx)
{
int n_args = duk_get_top(ctx);
const char* filename;
color_t fill_color;
image_t* image;
image_t* src_image;
char* path;
int width, height;
if (n_args >= 3) {
width = duk_require_int(ctx, 0);
height = duk_require_int(ctx, 1);
fill_color = duk_require_sphere_color(ctx, 2);
if (!(image = create_image(width, height)))
duk_error_ni(ctx, -1, DUK_ERR_ERROR, "Image(): Failed to create new image");
fill_image(image, fill_color);
}
else if (duk_is_sphere_obj(ctx, 0, "Surface")) {
src_image = duk_require_sphere_surface(ctx, 0);
if (!(image = clone_image(src_image)))
duk_error_ni(ctx, -1, DUK_ERR_ERROR, "Image(): Failed to create image from surface");
}
else {
filename = duk_require_string(ctx, 0);
path = get_asset_path(filename, "images", false);
image = load_image(path);
free(path);
if (image == NULL)
duk_error_ni(ctx, -1, DUK_ERR_ERROR, "Image(): Failed to load image file '%s'", filename);
}
duk_push_sphere_image(ctx, image);
free_image(image);
return 1;
}
static duk_ret_t
js_Image_finalize(duk_context* ctx)
{
image_t* image;
image = duk_require_sphere_image(ctx, 0);
free_image(image);
return 0;
}
static duk_ret_t
js_Image_get_height(duk_context* ctx)
{
image_t* image;
duk_push_this(ctx);
image = duk_require_sphere_image(ctx, -1);
duk_pop(ctx);
duk_push_int(ctx, get_image_height(image));
return 1;
}
static duk_ret_t
js_Image_get_width(duk_context* ctx)
{
image_t* image;
duk_push_this(ctx);
image = duk_require_sphere_image(ctx, -1);
duk_pop(ctx);
duk_push_int(ctx, get_image_width(image));
return 1;
}
static duk_ret_t
js_Image_toString(duk_context* ctx)
{
duk_push_string(ctx, "[object image]");
return 1;
}
static duk_ret_t
js_Image_blit(duk_context* ctx)
{
int x = duk_require_int(ctx, 0);
int y = duk_require_int(ctx, 1);
image_t* image;
duk_push_this(ctx);
image = duk_require_sphere_image(ctx, -1);
duk_pop(ctx);
if (!is_skipped_frame()) al_draw_bitmap(get_image_bitmap(image), x, y, 0x0);
return 0;
}
static duk_ret_t
js_Image_blitMask(duk_context* ctx)
{
int x = duk_require_int(ctx, 0);
int y = duk_require_int(ctx, 1);
color_t mask = duk_require_sphere_color(ctx, 2);
image_t* image;
duk_push_this(ctx);
image = duk_require_sphere_image(ctx, -1);
duk_pop(ctx);
if (!is_skipped_frame()) al_draw_tinted_bitmap(get_image_bitmap(image), al_map_rgba(mask.r, mask.g, mask.b, mask.alpha), x, y, 0x0);
return 0;
}
static duk_ret_t
js_Image_createSurface(duk_context* ctx)
{
image_t* image;
image_t* new_image;
duk_push_this(ctx);
image = duk_require_sphere_image(ctx, -1);
duk_pop(ctx);
if ((new_image = clone_surface(image)) == NULL)
duk_error_ni(ctx, -1, DUK_ERR_ERROR, "Image:createSurface(): Failed to create new surface image");
duk_push_sphere_surface(ctx, new_image);
free_image(new_image);
return 1;
}
static duk_ret_t
js_Image_rotateBlit(duk_context* ctx)
{
int x = duk_require_int(ctx, 0);
int y = duk_require_int(ctx, 1);
float angle = duk_require_number(ctx, 2);
image_t* image;
duk_push_this(ctx);
image = duk_require_sphere_image(ctx, -1);
duk_pop(ctx);
if (!is_skipped_frame())
al_draw_rotated_bitmap(get_image_bitmap(image),
image->width / 2, image->height / 2, x + image->width / 2, y + image->height / 2,
angle, 0x0);
return 0;
}
static duk_ret_t
js_Image_rotateBlitMask(duk_context* ctx)
{
int x = duk_require_int(ctx, 0);
int y = duk_require_int(ctx, 1);
float angle = duk_require_number(ctx, 2);
color_t mask = duk_require_sphere_color(ctx, 3);
image_t* image;
duk_push_this(ctx);
image = duk_require_sphere_image(ctx, -1);
duk_pop(ctx);
if (!is_skipped_frame())
al_draw_tinted_rotated_bitmap(get_image_bitmap(image), al_map_rgba(mask.r, mask.g, mask.b, mask.alpha),
image->width / 2, image->height / 2, x + image->width / 2, y + image->height / 2,
angle, 0x0);
return 0;
}
static duk_ret_t
js_Image_transformBlit(duk_context* ctx)
{
int x1 = duk_require_int(ctx, 0);
int y1 = duk_require_int(ctx, 1);
int x2 = duk_require_int(ctx, 2);
int y2 = duk_require_int(ctx, 3);
int x3 = duk_require_int(ctx, 4);
int y3 = duk_require_int(ctx, 5);
int x4 = duk_require_int(ctx, 6);
int y4 = duk_require_int(ctx, 7);
image_t* image;
ALLEGRO_COLOR vertex_color;
duk_push_this(ctx);
image = duk_require_sphere_image(ctx, -1);
duk_pop(ctx);
vertex_color = al_map_rgba(255, 255, 255, 255);
ALLEGRO_VERTEX v[] = {
{ x1, y1, 0, 0, 0, vertex_color },
{ x2, y2, 0, image->width, 0, vertex_color },
{ x4, y4, 0, 0, image->height, vertex_color },
{ x3, y3, 0, image->width, image->height, vertex_color }
};
if (!is_skipped_frame())
al_draw_prim(v, NULL, get_image_bitmap(image), 0, 4, ALLEGRO_PRIM_TRIANGLE_STRIP);
return 0;
}
static duk_ret_t
js_Image_transformBlitMask(duk_context* ctx)
{
int x1 = duk_require_int(ctx, 0);
int y1 = duk_require_int(ctx, 1);
int x2 = duk_require_int(ctx, 2);
int y2 = duk_require_int(ctx, 3);
int x3 = duk_require_int(ctx, 4);
int y3 = duk_require_int(ctx, 5);
int x4 = duk_require_int(ctx, 6);
int y4 = duk_require_int(ctx, 7);
color_t mask = duk_require_sphere_color(ctx, 8);
ALLEGRO_COLOR vtx_color;
image_t* image;
duk_push_this(ctx);
image = duk_require_sphere_image(ctx, -1);
duk_pop(ctx);
vtx_color = al_map_rgba(mask.r, mask.g, mask.b, mask.alpha);
ALLEGRO_VERTEX v[] = {
{ x1, y1, 0, 0, 0, vtx_color },
{ x2, y2, 0, image->width, 0, vtx_color },
{ x4, y4, 0, 0, image->height, vtx_color },
{ x3, y3, 0, image->width, image->height, vtx_color }
};
if (!is_skipped_frame())
al_draw_prim(v, NULL, get_image_bitmap(image), 0, 4, ALLEGRO_PRIM_TRIANGLE_STRIP);
return 0;
}
static duk_ret_t
js_Image_zoomBlit(duk_context* ctx)
{
int x = duk_require_int(ctx, 0);
int y = duk_require_int(ctx, 1);
float scale = duk_require_number(ctx, 2);
image_t* image;
duk_push_this(ctx);
image = duk_require_sphere_image(ctx, -1);
duk_pop(ctx);
if (!is_skipped_frame())
al_draw_scaled_bitmap(get_image_bitmap(image), 0, 0, image->width, image->height, x, y, image->width * scale, image->height * scale, 0x0);
return 0;
}
static duk_ret_t
js_Image_zoomBlitMask(duk_context* ctx)
{
int x = duk_require_int(ctx, 0);