core: Rename GP_Context.c -> GP_Pixmap.c
[gfxprim.git] / tests / loaders / PNG.c
blob65328839bcf9885f1ca096d90fb18d679d1d8988
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2013 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
23 #include <string.h>
24 #include <errno.h>
25 #include <sys/stat.h>
27 #include <core/GP_Pixmap.h>
28 #include <core/GP_GetPutPixel.h>
29 #include <loaders/GP_Loaders.h>
31 #include "tst_test.h"
33 static int test_load_PNG(const char *path)
35 GP_Pixmap *img;
37 errno = 0;
39 img = GP_LoadPNG(path, NULL);
41 if (img == NULL) {
42 switch (errno) {
43 case ENOSYS:
44 tst_msg("Not Implemented");
45 return TST_SKIPPED;
46 default:
47 tst_msg("Got %s", strerror(errno));
48 return TST_FAILED;
53 * TODO: check correct data.
56 GP_PixmapFree(img);
58 return TST_SUCCESS;
61 struct check_color_test {
62 const char *path;
63 GP_Pixel pixel;
66 static int test_load_PNG_check_color(struct check_color_test *test)
68 GP_Pixmap *img;
70 errno = 0;
72 img = GP_LoadPNG(test->path, NULL);
74 if (img == NULL) {
75 switch (errno) {
76 case ENOSYS:
77 tst_msg("Not Implemented");
78 return TST_SKIPPED;
79 default:
80 tst_msg("Got %s", strerror(errno));
81 return TST_FAILED;
85 unsigned int x, y, fail = 0;
87 for (x = 0; x < img->w; x++) {
88 for (y = 0; y < img->w; y++) {
89 GP_Pixel p = GP_GetPixel(img, x, y);
91 if (p != test->pixel) {
92 if (!fail)
93 tst_msg("First failed at %u,%u %x %x",
94 x, y, p, test->pixel);
95 fail = 1;
100 if (!fail)
101 tst_msg("Pixmap pixels are correct");
103 GP_PixmapFree(img);
105 if (fail)
106 return TST_FAILED;
108 return TST_SUCCESS;
111 static struct check_color_test white_adam7 = {
112 .path = "100x100-white-adam7.png",
113 .pixel = 0xffffff,
116 static struct check_color_test black_grayscale = {
117 .path = "100x100-black-grayscale.png",
118 .pixel = 0x000000,
121 static struct check_color_test red = {
122 .path = "100x100-red.png",
123 .pixel = 0xff0000,
126 static int test_save_PNG(GP_PixelType pixel_type)
128 GP_Pixmap *pixmap;
129 int ret;
131 pixmap = GP_PixmapAlloc(100, 100, pixel_type);
133 if (pixmap == NULL) {
134 tst_msg("Failed to allocate pixmap");
135 return TST_UNTESTED;
138 errno = 0;
140 ret = GP_SavePNG(pixmap, "/dev/null", NULL);
142 if (ret == 0) {
143 tst_msg("Saved successfully");
144 GP_PixmapFree(pixmap);
145 return TST_SUCCESS;
148 switch (errno) {
149 case ENOSYS:
150 tst_msg("Not Implemented");
151 GP_PixmapFree(pixmap);
152 return TST_SKIPPED;
153 default:
154 tst_msg("Failed and errno is not ENOSYS (%i)", errno);
155 GP_PixmapFree(pixmap);
156 return TST_FAILED;
160 const struct tst_suite tst_suite = {
161 .suite_name = "PNG",
162 .tests = {
163 /* PNG loader tests */
164 {.name = "PNG Load 100x100 RGB",
165 .tst_fn = test_load_PNG_check_color,
166 .res_path = "data/png/valid/100x100-red.png",
167 .data = &red,
168 .flags = TST_TMPDIR | TST_CHECK_MALLOC},
170 {.name = "PNG Load 100x100 RGB 50\% alpha",
171 .tst_fn = test_load_PNG,
172 .res_path = "data/png/valid/100x100-red-alpha.png",
173 .data = "100x100-red-alpha.png",
174 .flags = TST_TMPDIR | TST_CHECK_MALLOC},
176 {.name = "PNG Load 100x100 8 bit Grayscale",
177 .tst_fn = test_load_PNG_check_color,
178 .res_path = "data/png/valid/100x100-black-grayscale.png",
179 .data = &black_grayscale,
180 .flags = TST_TMPDIR | TST_CHECK_MALLOC},
182 {.name = "PNG Load 100x100 8 bit Grayscale + alpha",
183 .tst_fn = test_load_PNG,
184 .res_path = "data/png/valid/100x100-black-grayscale-alpha.png",
185 .data = "100x100-black-grayscale-alpha.png",
186 .flags = TST_TMPDIR | TST_CHECK_MALLOC},
188 {.name = "PNG Load 100x100 Palette + alpha",
189 .tst_fn = test_load_PNG,
190 .res_path = "data/png/valid/100x100-palette-alpha.png",
191 .data = "100x100-palette-alpha.png",
192 .flags = TST_TMPDIR | TST_CHECK_MALLOC},
194 {.name = "PNG Load 100x100 Palette",
195 .tst_fn = test_load_PNG,
196 .res_path = "data/png/valid/100x100-red-palette.png",
197 .data = "100x100-red-palette.png",
198 .flags = TST_TMPDIR | TST_CHECK_MALLOC},
200 {.name = "PNG Load 100x100 RGB Adam7",
201 .tst_fn = test_load_PNG_check_color,
202 .res_path = "data/png/valid/100x100-white-adam7.png",
203 .data = &white_adam7,
204 .flags = TST_TMPDIR | TST_CHECK_MALLOC},
206 {.name = "PNG Save 100x100 G1",
207 .tst_fn = test_save_PNG,
208 .data = (void*)GP_PIXEL_G1,
209 .flags = TST_CHECK_MALLOC},
211 {.name = "PNG Save 100x100 G2",
212 .tst_fn = test_save_PNG,
213 .data = (void*)GP_PIXEL_G2,
214 .flags = TST_CHECK_MALLOC},
216 {.name = "PNG Save 100x100 G4",
217 .tst_fn = test_save_PNG,
218 .data = (void*)GP_PIXEL_G4,
219 .flags = TST_CHECK_MALLOC},
221 {.name = "PNG Save 100x100 G8",
222 .tst_fn = test_save_PNG,
223 .data = (void*)GP_PIXEL_G8,
224 .flags = TST_CHECK_MALLOC},
226 {.name = "PNG Save 100x100 RGB888",
227 .tst_fn = test_save_PNG,
228 .data = (void*)GP_PIXEL_RGB888,
229 .flags = TST_CHECK_MALLOC},
231 {.name = "PNG Save 100x100 RGBA8888",
232 .tst_fn = test_save_PNG,
233 .data = (void*)GP_PIXEL_RGBA8888,
234 .flags = TST_CHECK_MALLOC},
236 {.name = "PNG Save 100x100 BGR888",
237 .tst_fn = test_save_PNG,
238 .data = (void*)GP_PIXEL_BGR888,
239 .flags = TST_CHECK_MALLOC},
241 {.name = "PNG Save 100x100 G16",
242 .tst_fn = test_save_PNG,
243 .data = (void*)GP_PIXEL_G16,
244 .flags = TST_CHECK_MALLOC},
245 {.name = NULL},