1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (rpng_test.c).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #include <file/nbio.h>
32 #include <formats/rpng.h>
33 #include <formats/image.h>
35 static bool rpng_load_image_argb(const char *path, uint32_t **data,
36 unsigned *width, unsigned *height)
43 struct nbio_t* handle = (struct nbio_t*)nbio_open(path, NBIO_READ);
48 nbio_begin_read(handle);
50 while (!nbio_iterate(handle));
52 ptr = nbio_get_ptr(handle, &file_len);
68 if (!rpng_set_buf_ptr(rpng, (uint8_t*)ptr, file_len))
74 if (!rpng_start(rpng))
80 while (rpng_iterate_image(rpng));
82 if (!rpng_is_valid(rpng))
90 retval = rpng_process_image(rpng,
91 (void**)data, file_len, width, height);
92 }while(retval == IMAGE_PROCESS_NEXT);
94 if (retval == IMAGE_PROCESS_ERROR || retval == IMAGE_PROCESS_ERROR_END)
108 static int test_rpng(const char *in_path)
112 const uint32_t *imlib_data = NULL;
114 const uint32_t test_data[] = {
115 0xff000000 | 0x50, 0xff000000 | 0x80,
116 0xff000000 | 0x40, 0xff000000 | 0x88,
117 0xff000000 | 0x50, 0xff000000 | 0x80,
118 0xff000000 | 0x40, 0xff000000 | 0x88,
119 0xff000000 | 0xc3, 0xff000000 | 0xd3,
120 0xff000000 | 0xc3, 0xff000000 | 0xd3,
121 0xff000000 | 0xc3, 0xff000000 | 0xd3,
122 0xff000000 | 0xc3, 0xff000000 | 0xd3,
124 uint32_t *data = NULL;
128 if (!rpng_save_image_argb("/tmp/test.png", test_data, 4, 4, 16))
131 if (!rpng_load_image_argb(in_path, &data, &width, &height))
134 fprintf(stderr, "Path: %s.\n", in_path);
135 fprintf(stderr, "Got image: %u x %u.\n", width, height);
138 fprintf(stderr, "\nRPNG:\n");
139 for (unsigned h = 0; h < height; h++)
142 for (w = 0; w < width; w++)
143 fprintf(stderr, "[%08x] ", data[h * width + w]);
144 fprintf(stderr, "\n");
149 /* Validate with imlib2 as well. */
150 img = imlib_load_image(in_path);
154 imlib_context_set_image(img);
156 width = imlib_image_get_width();
157 height = imlib_image_get_width();
158 imlib_data = imlib_image_get_data_for_reading_only();
161 fprintf(stderr, "\nImlib:\n");
162 for (unsigned h = 0; h < height; h++)
164 for (unsigned w = 0; w < width; w++)
165 fprintf(stderr, "[%08x] ", imlib_data[h * width + w]);
166 fprintf(stderr, "\n");
170 if (memcmp(imlib_data, data, width * height * sizeof(uint32_t)) != 0)
172 fprintf(stderr, "Imlib and RPNG differs!\n");
176 fprintf(stderr, "Imlib and RPNG are equivalent!\n");
185 int main(int argc, char *argv[])
187 const char *in_path = "/tmp/test.png";
191 fprintf(stderr, "Usage: %s <png file>\n", argv[0]);
198 fprintf(stderr, "Doing tests...\n");
200 if (test_rpng(in_path) != 0)
202 fprintf(stderr, "Test failed.\n");