-
Notifications
You must be signed in to change notification settings - Fork 191
/
main.c
1101 lines (995 loc) · 27.2 KB
/
main.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
/*
Copyright 1997-2024 Sam Lantinga
Copyright 2022 Collabora Ltd.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
#include <SDL3_image/SDL_image.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_test.h>
#include <stdlib.h>
#if defined(SDL_FILESYSTEM_OS2) || defined(SDL_FILESYSTEM_WINDOWS)
static const char pathsep[] = "\\";
#elif defined(SDL_FILESYSTEM_RISCOS)
static const char pathsep[] = ".";
#else
static const char pathsep[] = "/";
#endif
#if defined(__APPLE__) && !defined(SDL_IMAGE_USE_COMMON_BACKEND)
# define USING_IMAGEIO 1
#else
# define USING_IMAGEIO 0
#endif
typedef enum
{
TEST_FILE_DIST,
TEST_FILE_BUILT
} TestFileType;
static SDL_bool
GetStringBoolean(const char *value, SDL_bool default_value)
{
if (!value || !*value) {
return default_value;
}
if (*value == '0' || SDL_strcasecmp(value, "false") == 0) {
return SDL_FALSE;
}
return SDL_TRUE;
}
/*
* Return the absolute path to a resource file, similar to GLib's
* g_test_build_filename().
*
* If type is TEST_FILE_DIST, look for it in $SDL_TEST_SRCDIR or next
* to the executable.
*
* If type is TEST_FILE_BUILT, look for it in $SDL_TEST_BUILDDIR or next
* to the executable.
*
* Fails and returns NULL if out of memory.
*/
static char *
GetTestFilename(TestFileType type, const char *file)
{
const char *env;
char *base = NULL;
char *path = NULL;
SDL_bool needPathSep = SDL_TRUE;
if (type == TEST_FILE_DIST) {
env = SDL_getenv("SDL_TEST_SRCDIR");
} else {
env = SDL_getenv("SDL_TEST_BUILDDIR");
}
if (env != NULL) {
base = SDL_strdup(env);
if (base == NULL) {
SDL_OutOfMemory();
return NULL;
}
}
if (base == NULL) {
base = SDL_GetBasePath();
/* SDL_GetBasePath() guarantees a trailing path separator */
needPathSep = SDL_FALSE;
}
if (base != NULL) {
size_t len = SDL_strlen(base) + SDL_strlen(pathsep) + SDL_strlen(file) + 1;
path = SDL_malloc(len);
if (path == NULL) {
SDL_OutOfMemory();
return NULL;
}
if (needPathSep) {
SDL_snprintf(path, len, "%s%s%s", base, pathsep, file);
} else {
SDL_snprintf(path, len, "%s%s", base, file);
}
SDL_free(base);
} else {
path = SDL_strdup(file);
if (path == NULL) {
SDL_OutOfMemory();
return NULL;
}
}
return path;
}
static SDLTest_CommonState *state;
typedef struct
{
const char *name;
const char *sample;
const char *reference;
int w;
int h;
int tolerance;
int initFlag;
SDL_bool canLoad;
SDL_bool canSave;
int (SDLCALL * checkFunction)(SDL_RWops *src);
SDL_Surface *(SDLCALL * loadFunction)(SDL_RWops *src);
} Format;
static const Format formats[] =
{
{
"AVIF",
"sample.avif",
"sample.bmp",
23,
42,
300,
IMG_INIT_AVIF,
#ifdef LOAD_AVIF
SDL_TRUE,
#else
SDL_FALSE,
#endif
SDL_FALSE, /* can save */
IMG_isAVIF,
IMG_LoadAVIF_RW,
},
{
"BMP",
"sample.bmp",
"sample.png",
23,
42,
0, /* lossless */
0, /* no initialization */
#ifdef LOAD_BMP
SDL_TRUE,
#else
SDL_FALSE,
#endif
SDL_FALSE, /* can save */
IMG_isBMP,
IMG_LoadBMP_RW,
},
{
"CUR",
"sample.cur",
"sample.bmp",
23,
42,
0, /* lossless */
0, /* no initialization */
#ifdef LOAD_BMP
SDL_TRUE,
#else
SDL_FALSE,
#endif
SDL_FALSE, /* can save */
IMG_isCUR,
IMG_LoadCUR_RW,
},
{
"GIF",
"palette.gif",
"palette.bmp",
23,
42,
0, /* lossless */
0, /* no initialization */
#if USING_IMAGEIO || defined(LOAD_GIF)
SDL_TRUE,
#else
SDL_FALSE,
#endif
SDL_FALSE, /* can save */
IMG_isGIF,
IMG_LoadGIF_RW,
},
{
"ICO",
"sample.ico",
"sample.bmp",
23,
42,
0, /* lossless */
0, /* no initialization */
#ifdef LOAD_BMP
SDL_TRUE,
#else
SDL_FALSE,
#endif
SDL_FALSE, /* can save */
IMG_isICO,
IMG_LoadICO_RW,
},
{
"JPG",
"sample.jpg",
"sample.bmp",
23,
42,
100,
IMG_INIT_JPG,
#if (USING_IMAGEIO && defined(JPG_USES_IMAGEIO)) || defined(SDL_IMAGE_USE_WIC_BACKEND) || defined(LOAD_JPG)
SDL_TRUE,
#else
SDL_FALSE,
#endif
SDL_IMAGE_SAVE_JPG,
IMG_isJPG,
IMG_LoadJPG_RW,
},
{
"JXL",
"sample.jxl",
"sample.bmp",
23,
42,
300,
IMG_INIT_JXL,
#ifdef LOAD_JXL
SDL_TRUE,
#else
SDL_FALSE,
#endif
SDL_FALSE, /* can save */
IMG_isJXL,
IMG_LoadJXL_RW,
},
#if 0
{
"LBM",
"sample.lbm",
"sample.bmp",
23,
42,
0, /* lossless? */
0, /* no initialization */
#ifdef LOAD_LBM
SDL_TRUE,
#else
SDL_FALSE,
#endif
SDL_FALSE, /* can save */
IMG_isLBM,
IMG_LoadLBM_RW,
},
#endif
{
"PCX",
"sample.pcx",
"sample.bmp",
23,
42,
0, /* lossless? */
0, /* no initialization */
#ifdef LOAD_PCX
SDL_TRUE,
#else
SDL_FALSE,
#endif
SDL_FALSE, /* can save */
IMG_isPCX,
IMG_LoadPCX_RW,
},
{
"PNG",
"sample.png",
"sample.bmp",
23,
42,
0, /* lossless */
IMG_INIT_PNG,
#if (USING_IMAGEIO && defined(PNG_USES_IMAGEIO)) || defined(SDL_IMAGE_USE_WIC_BACKEND) || defined(LOAD_PNG)
SDL_TRUE,
#else
SDL_FALSE,
#endif
#ifdef SDL_IMAGE_SAVE_PNG
SDL_IMAGE_SAVE_PNG ? SDL_TRUE : SDL_FALSE,
#else
SDL_FALSE,
#endif
IMG_isPNG,
IMG_LoadPNG_RW,
},
{
"PNM",
"sample.pnm",
"sample.bmp",
23,
42,
0, /* lossless */
0, /* no initialization */
#ifdef LOAD_PNM
SDL_TRUE,
#else
SDL_FALSE,
#endif
SDL_FALSE, /* can save */
IMG_isPNM,
IMG_LoadPNM_RW,
},
{
"QOI",
"sample.qoi",
"sample.bmp",
23,
42,
0, /* lossless */
0, /* no initialization */
#ifdef LOAD_QOI
SDL_TRUE,
#else
SDL_FALSE,
#endif
SDL_FALSE, /* can save */
IMG_isQOI,
IMG_LoadQOI_RW,
},
{
"SVG",
"svg.svg",
"svg.bmp",
32,
32,
100,
0, /* no initialization */
#ifdef LOAD_SVG
SDL_TRUE,
#else
SDL_FALSE,
#endif
SDL_FALSE, /* can save */
IMG_isSVG,
IMG_LoadSVG_RW,
},
{
"SVG-sized",
"svg.svg",
"svg64.bmp",
64,
64,
100,
0, /* no initialization */
#ifdef LOAD_SVG
SDL_TRUE,
#else
SDL_FALSE,
#endif
SDL_FALSE, /* can save */
IMG_isSVG,
IMG_LoadSVG_RW,
},
{
"SVG-class",
"svg-class.svg",
"svg-class.bmp",
82,
82,
0, /* lossless? */
0, /* no initialization */
#ifdef LOAD_SVG
SDL_TRUE,
#else
SDL_FALSE,
#endif
SDL_FALSE, /* can save */
IMG_isSVG,
IMG_LoadSVG_RW,
},
{
"TGA",
"sample.tga",
"sample.bmp",
23,
42,
0, /* lossless? */
0, /* no initialization */
#if USING_IMAGEIO || defined(LOAD_TGA)
SDL_TRUE,
#else
SDL_FALSE,
#endif
SDL_FALSE, /* can save */
NULL,
IMG_LoadTGA_RW,
},
{
"TIF",
"sample.tif",
"sample.bmp",
23,
42,
0, /* lossless */
IMG_INIT_TIF,
#if USING_IMAGEIO || defined(SDL_IMAGE_USE_WIC_BACKEND) || defined(LOAD_TIF)
SDL_TRUE,
#else
SDL_FALSE,
#endif
SDL_FALSE, /* can save */
IMG_isTIF,
IMG_LoadTIF_RW,
},
{
"WEBP",
"sample.webp",
"sample.bmp",
23,
42,
0, /* lossless */
IMG_INIT_WEBP,
#ifdef LOAD_WEBP
SDL_TRUE,
#else
SDL_FALSE,
#endif
SDL_FALSE, /* can save */
IMG_isWEBP,
IMG_LoadWEBP_RW,
},
{
"XCF",
"sample.xcf",
"sample.bmp",
23,
42,
0, /* lossless */
0, /* no initialization */
#ifdef LOAD_XCF
SDL_TRUE,
#else
SDL_FALSE,
#endif
SDL_FALSE, /* can save */
IMG_isXCF,
IMG_LoadXCF_RW,
},
{
"XPM",
"sample.xpm",
"sample.bmp",
23,
42,
0, /* lossless */
0, /* no initialization */
#ifdef LOAD_XPM
SDL_TRUE,
#else
SDL_FALSE,
#endif
SDL_FALSE, /* can save */
IMG_isXPM,
IMG_LoadXPM_RW,
},
#if 0
{
"XV",
"sample.xv",
"sample.bmp",
23,
42,
0, /* lossless? */
0, /* no initialization */
#ifdef LOAD_XV
SDL_TRUE,
#else
SDL_FALSE,
#endif
SDL_FALSE, /* can save */
IMG_isXV,
IMG_LoadXV_RW,
},
#endif
};
static SDL_bool
StrHasSuffix(const char *str, const char *suffix)
{
size_t str_len = SDL_strlen(str);
size_t suffix_len = SDL_strlen(suffix);
return (str_len >= suffix_len
&& SDL_strcmp(str + (str_len - suffix_len), suffix) == 0);
}
typedef enum
{
LOAD_CONVENIENCE = 0,
LOAD_RW,
LOAD_TYPED_RW,
LOAD_FORMAT_SPECIFIC,
LOAD_SIZED
} LoadMode;
/* Convert to RGBA for comparison, if necessary */
static SDL_bool
ConvertToRgba32(SDL_Surface **surface_p)
{
if ((*surface_p)->format->format != SDL_PIXELFORMAT_RGBA32) {
SDL_Surface *temp;
temp = SDL_ConvertSurfaceFormat(*surface_p, SDL_PIXELFORMAT_RGBA32);
SDLTest_AssertCheck(temp != NULL,
"Converting to RGBA should succeed (%s)",
SDL_GetError());
if (temp == NULL) {
return SDL_FALSE;
}
SDL_DestroySurface(*surface_p);
*surface_p = temp;
}
return SDL_TRUE;
}
static void
DumpPixels(const char *filename, SDL_Surface *surface)
{
const unsigned char *pixels = surface->pixels;
const unsigned char *p;
size_t w, h, pitch;
size_t i, j;
SDL_Log("%s:\n", filename);
if (surface->format->palette) {
size_t n = 0;
if (surface->format->palette->ncolors >= 0) {
n = (size_t) surface->format->palette->ncolors;
}
SDL_Log(" Palette:\n");
for (i = 0; i < n; i++) {
SDL_Log(" RGBA[0x%02x] = %02x%02x%02x%02x\n",
(unsigned) i,
surface->format->palette->colors[i].r,
surface->format->palette->colors[i].g,
surface->format->palette->colors[i].b,
surface->format->palette->colors[i].a);
}
}
if (surface->w < 0) {
SDL_Log(" Invalid width %d\n", surface->w);
return;
}
if (surface->h < 0) {
SDL_Log(" Invalid height %d\n", surface->h);
return;
}
if (surface->pitch < 0) {
SDL_Log(" Invalid pitch %d\n", surface->pitch);
return;
}
w = (size_t) surface->w;
h = (size_t) surface->h;
pitch = (size_t) surface->pitch;
SDL_Log(" Pixels:\n");
for (j = 0; j < h; j++) {
SDL_Log(" ");
for (i = 0; i < w; i++) {
p = pixels + (j * pitch) + (i * surface->format->BytesPerPixel);
switch (surface->format->BitsPerPixel) {
case 1:
case 4:
case 8:
SDL_Log("%02x ", *p);
break;
case 12:
case 15:
case 16:
SDL_Log("%02x", *p++);
SDL_Log("%02x ", *p);
break;
case 24:
SDL_Log("%02x", *p++);
SDL_Log("%02x", *p++);
SDL_Log("%02x ", *p);
break;
case 32:
SDL_Log("%02x", *p++);
SDL_Log("%02x", *p++);
SDL_Log("%02x", *p++);
SDL_Log("%02x ", *p);
break;
}
}
SDL_Log("\n");
}
}
static void
FormatLoadTest(const Format *format,
LoadMode mode)
{
SDL_Surface *reference = NULL;
SDL_Surface *surface = NULL;
SDL_RWops *src = NULL;
char *filename = GetTestFilename(TEST_FILE_DIST, format->sample);
char *refFilename = GetTestFilename(TEST_FILE_DIST, format->reference);
int initResult = 0;
int diff;
if (!SDLTest_AssertCheck(filename != NULL,
"Building filename should succeed (%s)",
SDL_GetError())) {
goto out;
}
if (!SDLTest_AssertCheck(refFilename != NULL,
"Building ref filename should succeed (%s)",
SDL_GetError())) {
goto out;
}
if (StrHasSuffix(format->reference, ".bmp")) {
reference = SDL_LoadBMP(refFilename);
if (!SDLTest_AssertCheck(reference != NULL,
"Loading reference should succeed (%s)",
SDL_GetError())) {
goto out;
}
}
else if (StrHasSuffix (format->reference, ".png")) {
#ifdef LOAD_PNG
reference = IMG_Load(refFilename);
if (!SDLTest_AssertCheck(reference != NULL,
"Loading reference should succeed (%s)",
SDL_GetError())) {
goto out;
}
#endif
}
if (format->initFlag) {
initResult = IMG_Init(format->initFlag);
if (!SDLTest_AssertCheck(initResult != 0,
"Initialization should succeed (%s)",
SDL_GetError())) {
goto out;
}
SDLTest_AssertCheck(initResult & format->initFlag,
"Expected at least bit 0x%x set, got 0x%x",
format->initFlag, initResult);
}
if (mode != LOAD_CONVENIENCE) {
src = SDL_RWFromFile(filename, "rb");
SDLTest_AssertCheck(src != NULL,
"Opening %s should succeed (%s)",
filename, SDL_GetError());
if (src == NULL)
goto out;
}
switch (mode) {
case LOAD_CONVENIENCE:
surface = IMG_Load(filename);
break;
case LOAD_RW:
if (format->checkFunction != NULL) {
SDL_RWops *ref_src;
int check;
ref_src = SDL_RWFromFile(refFilename, "rb");
SDLTest_AssertCheck(ref_src != NULL,
"Opening %s should succeed (%s)",
refFilename, SDL_GetError());
if (ref_src != NULL) {
check = format->checkFunction(ref_src);
SDLTest_AssertCheck(!check,
"Should not detect %s as %s -> %d",
refFilename, format->name, check);
SDL_RWclose(ref_src);
}
}
if (format->checkFunction != NULL) {
int check = format->checkFunction(src);
SDLTest_AssertCheck(check,
"Should detect %s as %s -> %d",
filename, format->name, check);
}
surface = IMG_Load_RW(src, SDL_TRUE);
src = NULL; /* ownership taken */
break;
case LOAD_TYPED_RW:
surface = IMG_LoadTyped_RW(src, SDL_TRUE, format->name);
src = NULL; /* ownership taken */
break;
case LOAD_FORMAT_SPECIFIC:
surface = format->loadFunction(src);
break;
case LOAD_SIZED:
if (SDL_strcmp(format->name, "SVG-sized") == 0) {
surface = IMG_LoadSizedSVG_RW(src, 64, 64);
}
break;
}
if (!SDLTest_AssertCheck(surface != NULL,
"Load %s (%s)", filename, SDL_GetError())) {
goto out;
}
SDLTest_AssertCheck(surface->w == format->w,
"Expected width %d px, got %d",
format->w, surface->w);
SDLTest_AssertCheck(surface->h == format->h,
"Expected height %d px, got %d",
format->h, surface->h);
if (GetStringBoolean(SDL_getenv("SDL_IMAGE_TEST_DEBUG"), SDL_FALSE)) {
DumpPixels(filename, surface);
}
if (reference != NULL) {
ConvertToRgba32(&reference);
ConvertToRgba32(&surface);
diff = SDLTest_CompareSurfaces(surface, reference, format->tolerance);
SDLTest_AssertCheck(diff == 0,
"Surface differed from reference by at most %d in %d pixels",
format->tolerance, diff);
if (diff != 0 || GetStringBoolean(SDL_getenv("SDL_IMAGE_TEST_DEBUG"), SDL_FALSE)) {
DumpPixels(filename, surface);
DumpPixels(refFilename, reference);
}
}
out:
if (surface != NULL) {
SDL_DestroySurface(surface);
}
if (reference != NULL) {
SDL_DestroySurface(reference);
}
if (src != NULL) {
SDL_RWclose(src);
}
if (refFilename != NULL) {
SDL_free(refFilename);
}
if (filename != NULL) {
SDL_free(filename);
}
if (initResult) {
IMG_Quit();
}
}
static void
FormatSaveTest(const Format *format,
SDL_bool rw)
{
char *refFilename = GetTestFilename(TEST_FILE_DIST, "sample.bmp");
char filename[64] = { 0 };
SDL_Surface *reference = NULL;
SDL_Surface *surface = NULL;
SDL_RWops *dest = NULL;
int initResult = 0;
int diff;
int result;
SDL_snprintf(filename, sizeof(filename),
"save%s.%s",
rw ? "Rwops" : "",
format->name);
if (!SDLTest_AssertCheck(refFilename != NULL,
"Building ref filename should succeed (%s)",
SDL_GetError())) {
goto out;
}
reference = SDL_LoadBMP(refFilename);
if (!SDLTest_AssertCheck(reference != NULL,
"Loading reference should succeed (%s)",
SDL_GetError())) {
goto out;
}
if (format->initFlag) {
initResult = IMG_Init(format->initFlag);
if (!SDLTest_AssertCheck(initResult != 0,
"Initialization should succeed (%s)",
SDL_GetError())) {
goto out;
}
SDLTest_AssertCheck(initResult & format->initFlag,
"Expected at least bit 0x%x set, got 0x%x",
format->initFlag, initResult);
}
if (SDL_strcmp (format->name, "PNG") == 0) {
if (rw) {
dest = SDL_RWFromFile(filename, "wb");
result = IMG_SavePNG_RW(reference, dest, SDL_FALSE);
SDL_RWclose(dest);
} else {
result = IMG_SavePNG(reference, filename);
}
} else if (SDL_strcmp(format->name, "JPG") == 0) {
if (rw) {
dest = SDL_RWFromFile(filename, "wb");
result = IMG_SaveJPG_RW(reference, dest, SDL_FALSE, 90);
SDL_RWclose(dest);
} else {
result = IMG_SaveJPG(reference, filename, 90);
}
} else {
SDLTest_AssertCheck(SDL_FALSE, "How do I save %s?", format->name);
goto out;
}
SDLTest_AssertCheck(result == 0, "Save %s (%s)", filename, SDL_GetError());
if (format->canLoad) {
surface = IMG_Load(filename);
if (!SDLTest_AssertCheck(surface != NULL,
"Load %s (%s)", "saved file", SDL_GetError())) {
goto out;
}
ConvertToRgba32(&reference);
ConvertToRgba32(&surface);
SDLTest_AssertCheck(surface->w == format->w,
"Expected width %d px, got %d",
format->w, surface->w);
SDLTest_AssertCheck(surface->h == format->h,
"Expected height %d px, got %d",
format->h, surface->h);
diff = SDLTest_CompareSurfaces(surface, reference, format->tolerance);
SDLTest_AssertCheck(diff == 0,
"Surface differed from reference by at most %d in %d pixels",
format->tolerance, diff);
if (diff != 0 || GetStringBoolean(SDL_getenv("SDL_IMAGE_TEST_DEBUG"), SDL_FALSE)) {
DumpPixels(filename, surface);
DumpPixels(refFilename, reference);
}
}
out:
if (surface != NULL) {
SDL_DestroySurface(surface);
}
if (reference != NULL) {
SDL_DestroySurface(reference);
}
if (refFilename != NULL) {
SDL_free(refFilename);
}
if (initResult) {
IMG_Quit();
}
}
static void
FormatTest(const Format *format)
{
SDL_bool forced;
char envVar[64] = { 0 };
SDL_snprintf(envVar, sizeof(envVar), "SDL_IMAGE_TEST_REQUIRE_LOAD_%s",
format->name);
forced = GetStringBoolean(SDL_getenv(envVar), SDL_FALSE);
if (forced) {
SDLTest_AssertCheck(format->canLoad,
"%s loading should be enabled", format->name);
}
if (format->canLoad || forced) {
SDLTest_Log("Testing ability to load format %s", format->name);
if (SDL_strcmp(format->name, "SVG-sized") == 0) {
FormatLoadTest(format, LOAD_SIZED);
} else {
FormatLoadTest(format, LOAD_CONVENIENCE);
if (SDL_strcmp(format->name, "TGA") == 0) {
SDLTest_Log("SKIP: Recognising %s by magic number is not supported", format->name);
} else {
FormatLoadTest(format, LOAD_RW);
}
FormatLoadTest(format, LOAD_TYPED_RW);
if (format->loadFunction != NULL) {
FormatLoadTest(format, LOAD_FORMAT_SPECIFIC);
}
}
} else {
SDLTest_Log("Format %s is not supported", format->name);
}
SDL_snprintf(envVar, sizeof(envVar), "SDL_IMAGE_TEST_REQUIRE_SAVE_%s",
format->name);
forced = GetStringBoolean(SDL_getenv(envVar), SDL_FALSE);
if (forced) {
SDLTest_AssertCheck(format->canSave,
"%s saving should be enabled", format->name);
}
if (format->canSave || forced) {
SDLTest_Log("Testing ability to save format %s", format->name);
FormatSaveTest(format, SDL_FALSE);
FormatSaveTest(format, SDL_TRUE);
} else {
SDLTest_Log("Saving format %s is not supported", format->name);
}
}
static int
TestFormats(void *arg)
{
size_t i;
(void)arg;
for (i = 0; i < SDL_arraysize(formats); i++) {
FormatTest(&formats[i]);
}
return TEST_COMPLETED;
}
static const SDLTest_TestCaseReference formatsTestCase = {
TestFormats, "Images", "Load and save various image formats", TEST_ENABLED
};
static const SDLTest_TestCaseReference *testCases[] = {
&formatsTestCase,
NULL
};
static SDLTest_TestSuiteReference testSuite = {
"img",
NULL,
testCases,
NULL
};
static SDLTest_TestSuiteReference *testSuites[] = {
&testSuite,
NULL
};
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void
quit(int rc)
{
SDLTest_CommonQuit(state);