loaders: PNG: Handle gamma on 16bpp conversion
[gfxprim.git] / tests / core / Convert.gen.c.t
blob362d765aa1856f7cc6f704f48c70de8aa55e8e60
1 @ include source.t
2 /*
3  * Pixel conversions tests.
4  *
5  * Copyright (C) 2009-2014 Cyril Hrubis <metan@ucw.cz>
6  */
7 #include <stdio.h>
8 #include <core/GP_Convert.h>
10 #include "tst_test.h"
13  * Returns black color for particular pixel type.
14  */
15 static GP_Pixel get_black(GP_PixelType pixel_type)
17         switch (pixel_type) {
18 @ for pt in pixeltypes:
19         case {{ pt.C_type }}:
20 @     if pt.is_cmyk():
21 @         K = pt.chans['K']
22                 /* Black in CMYK is full K rest zero */
23                 return {{ K.C_mask }};
24 @     elif pt.is_alpha():
25 @         A = pt.chans['A']
26                 /* Black with Alpha channel is full A rest zero */
27                 return {{ A.C_mask }};
28 @     else:
29                 return 0;
30 @ end
31         default:
32                 tst_msg("Invalid pixel type %i", pixel_type);
33                 exit(TST_INTERR);
34         }
38  * Returns white color for particular pixel type.
39  */
40 static GP_Pixel get_white(GP_PixelType pixel_type)
42         switch (pixel_type) {
43 @ for pt in pixeltypes:
44         case {{ pt.C_type }}:
45 @     if pt.is_cmyk():
46                 /* White in CMYK is zero */
47                 return 0x0;
48 @     elif pt.is_rgb():
49 @         R = pt.chans['R']
50 @         G = pt.chans['G']
51 @         B = pt.chans['B']
52 @         if pt.is_alpha():
53 @             A = pt.chans['A']
54                 /* White in RGBA */
55                 return {{ A.C_mask }} | {{ R.C_mask }} | {{ G.C_mask }} | {{ B.C_mask }};
56 @         else:
57                 /* Plain old RGB */
58                 return {{ R.C_mask }} | {{ G.C_mask }} | {{ B.C_mask }};
59 @         end
60 @     elif pt.is_gray():
61 @         V = pt.chans['V']
62 @         if pt.is_alpha():
63 @             A = pt.chans['A']
64                 /* Grayscale with Alpha */
65                 return {{ V.C_mask }} | {{ A.C_mask }};
66 @         else:
67                 /* Grayscale */
68                 return {{ V.C_mask }};
69 @         end
70 @     else:
71                 tst_msg("FIXME: Unsupported conversion to %s",
72                         GP_PixelTypeName(pixel_type));
73                 exit(TST_INTERR);
74 @ end
75         default:
76                 tst_msg("Invalid pixel type %i", pixel_type);
77                 exit(TST_INTERR);
78         }
82  * Returns red color for particular pixel type.
83  */
84 static GP_Pixel get_red(GP_PixelType pixel_type)
86         switch (pixel_type) {
87 @ for pt in pixeltypes:
88         case {{ pt.C_type }}:
89 @     if pt.is_cmyk():
90 @         M = pt.chans['M']
91 @         Y = pt.chans['Y']
92                 /* Red in CMYK is full M and Y rest zero */
93                 return {{ M.C_mask }} | {{ Y.C_mask }};
94 @     elif pt.is_rgb():
95 @         R = pt.chans['R']
96 @         if pt.is_alpha():
97 @             A = pt.chans['A']
98                 /* Red with Alpha channel is full Alpha and R rest zero */
99                 return {{ A.C_mask }} | {{ R.C_mask }};
100 @         else:
101                 /* Plain old RGB */
102                 return {{ R.C_mask }};
103 @         end
104 @     elif pt.is_gray():
105 @         V = pt.chans['V']
106 @         if pt.is_alpha():
107 @             A = pt.chans['A']
108                 /* Grayscale with Alpha channel is full Alpha + 1/3 Gray */
109                 return ({{ hex(V.max // 3)}}{{ V.C_shift }}) | {{ A.C_mask }};
110 @         else:
111                 /* Grayscale is 1/3 Gray */
112                 return {{ hex(V.max // 3) }}{{ V.C_shift }};
113 @         end
114 @     else:
115                 tst_msg("FIXME: Unsupported conversion to %s",
116                         GP_PixelTypeName(pixel_type));
117                 exit(TST_INTERR);
118 @ end
119         default:
120                 tst_msg("Invalid pixel type %i", pixel_type);
121                 exit(TST_INTERR);
122         }
125 @ def gen_convert_and_check(test_name, in_name, out_name):
126 static int convert_and_check_{{ test_name }}_{{ in_name }}_to_{{ out_name }}(void)
128         GP_Pixel out = 0;
129         GP_Pixel in = get_{{ test_name }}(GP_PIXEL_{{ in_name }});
130         GP_Pixel out_exp = get_{{ test_name }}(GP_PIXEL_{{ out_name }});
132         tst_msg("{{ in_name }} %08x -> {{ out_name }} %08x", in, out_exp);
134         GP_Pixel_{{ in_name }}_TO_{{ out_name }}(in, out);
136         if (out_exp != out) {
137                 tst_msg("Pixels are different have %08x, expected %08x",
138                         out, out_exp);
139                 return TST_FAILED;
140         }
142         return TST_SUCCESS;
145 @ end
147 @ def gen_converts():
148 @     for pt1 in pixeltypes:
149 @         if not pt1.is_unknown() and not pt1.is_palette():
150 @             if pt1.name not in ['RGB888', 'RGBA8888']:
151 @ # White
152 {@ gen_convert_and_check('white', pt1.name, 'RGB888') @}
153 {@ gen_convert_and_check('white', pt1.name, 'RGBA8888') @}
154 {@ gen_convert_and_check('white', 'RGB888', pt1.name) @}
155 {@ gen_convert_and_check('white', 'RGBA8888', pt1.name) @}
156 @ # Black
157 {@ gen_convert_and_check('black', pt1.name, 'RGB888') @}
158 {@ gen_convert_and_check('black', pt1.name, 'RGBA8888') @}
159 {@ gen_convert_and_check('black', 'RGB888', pt1.name) @}
160 {@ gen_convert_and_check('black', 'RGBA8888', pt1.name) @}
161 @ # Red
162 @                 if not pt1.is_gray():
163 {@ gen_convert_and_check('red', pt1.name, 'RGB888') @}
164 {@ gen_convert_and_check('red', pt1.name, 'RGBA8888') @}
165 @                 end
166 {@ gen_convert_and_check('red', 'RGB888', pt1.name) @}
167 {@ gen_convert_and_check('red', 'RGBA8888', pt1.name) @}
168 @ end
170 {@ gen_converts() @}
172 @ def gen_suite_entry(name, p_from, p_to):
173                 {.name = "Convert {{ name }} {{ p_from }} -> {{ p_to }}",
174                  .tst_fn = convert_and_check_{{ name }}_{{ p_from }}_to_{{ p_to }}},
175 @ end
177 const struct tst_suite tst_suite = {
178         .suite_name = "Pixel Conversions Testsuite",
179         .tests = {
180 @ for pt1 in pixeltypes:
181 @     if not pt1.is_unknown() and not pt1.is_palette():
182 @         if pt1.name not in ['RGB888', 'RGBA8888']:
183 @ # White
184 {@ gen_suite_entry('white', pt1.name, 'RGB888') @}
185 {@ gen_suite_entry('white', pt1.name, 'RGBA8888') @}
186 {@ gen_suite_entry('white', 'RGB888', pt1.name) @}
187 {@ gen_suite_entry('white', 'RGBA8888', pt1.name) @}
188 @ # Black
189 {@ gen_suite_entry('black', pt1.name, 'RGB888') @}
190 {@ gen_suite_entry('black', pt1.name, 'RGBA8888') @}
191 {@ gen_suite_entry('black', 'RGB888', pt1.name) @}
192 {@ gen_suite_entry('black', 'RGBA8888', pt1.name) @}
193 @ # Red
194 @             if not pt1.is_gray():
195 {@ gen_suite_entry('red', pt1.name, 'RGB888') @}
196 {@ gen_suite_entry('red', pt1.name, 'RGBA8888') @}
197 @             end
198 {@ gen_suite_entry('red', 'RGB888', pt1.name) @}
199 {@ gen_suite_entry('red', 'RGBA8888', pt1.name) @}
200 @ end
201                 {.name = NULL}
202         }