lightrec: implement clock cache clear on cycle_multiplier change
[pcsx_rearmed.git] / deps / libretro-common / samples / formats / png / rpng_test.c
CommitLineData
3719602c
PC
1/* Copyright (C) 2010-2020 The RetroArch team
2 *
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (rpng_test.c).
5 * ---------------------------------------------------------------------------------------
6 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14 *
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.
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <stdint.h>
26#include <string.h>
27#ifdef HAVE_IMLIB2
28#include <Imlib2.h>
29#endif
30
31#include <file/nbio.h>
32#include <formats/rpng.h>
33#include <formats/image.h>
34
35static bool rpng_load_image_argb(const char *path, uint32_t **data,
36 unsigned *width, unsigned *height)
37{
38 int retval;
39 size_t file_len;
40 bool ret = true;
41 rpng_t *rpng = NULL;
42 void *ptr = NULL;
43 struct nbio_t* handle = (struct nbio_t*)nbio_open(path, NBIO_READ);
44
45 if (!handle)
46 goto end;
47
48 nbio_begin_read(handle);
49
50 while (!nbio_iterate(handle));
51
52 ptr = nbio_get_ptr(handle, &file_len);
53
54 if (!ptr)
55 {
56 ret = false;
57 goto end;
58 }
59
60 rpng = rpng_alloc();
61
62 if (!rpng)
63 {
64 ret = false;
65 goto end;
66 }
67
68 if (!rpng_set_buf_ptr(rpng, (uint8_t*)ptr, file_len))
69 {
70 ret = false;
71 goto end;
72 }
73
74 if (!rpng_start(rpng))
75 {
76 ret = false;
77 goto end;
78 }
79
80 while (rpng_iterate_image(rpng));
81
82 if (!rpng_is_valid(rpng))
83 {
84 ret = false;
85 goto end;
86 }
87
88 do
89 {
90 retval = rpng_process_image(rpng,
91 (void**)data, file_len, width, height);
92 }while(retval == IMAGE_PROCESS_NEXT);
93
94 if (retval == IMAGE_PROCESS_ERROR || retval == IMAGE_PROCESS_ERROR_END)
95 ret = false;
96
97end:
98 if (handle)
99 nbio_free(handle);
100 if (rpng)
101 rpng_free(rpng);
102 rpng = NULL;
103 if (!ret)
104 free(*data);
105 return ret;
106}
107
108static int test_rpng(const char *in_path)
109{
110#ifdef HAVE_IMLIB2
111 Imlib_Image img;
112 const uint32_t *imlib_data = NULL;
113#endif
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,
123 };
124 uint32_t *data = NULL;
125 unsigned width = 0;
126 unsigned height = 0;
127
128 if (!rpng_save_image_argb("/tmp/test.png", test_data, 4, 4, 16))
129 return 1;
130
131 if (!rpng_load_image_argb(in_path, &data, &width, &height))
132 return 2;
133
134 fprintf(stderr, "Path: %s.\n", in_path);
135 fprintf(stderr, "Got image: %u x %u.\n", width, height);
136
137#if 0
138 fprintf(stderr, "\nRPNG:\n");
139 for (unsigned h = 0; h < height; h++)
140 {
141 unsigned w;
142 for (w = 0; w < width; w++)
143 fprintf(stderr, "[%08x] ", data[h * width + w]);
144 fprintf(stderr, "\n");
145 }
146#endif
147
148#ifdef HAVE_IMLIB2
149 /* Validate with imlib2 as well. */
150 img = imlib_load_image(in_path);
151 if (!img)
152 return 4;
153
154 imlib_context_set_image(img);
155
156 width = imlib_image_get_width();
157 height = imlib_image_get_width();
158 imlib_data = imlib_image_get_data_for_reading_only();
159
160#if 0
161 fprintf(stderr, "\nImlib:\n");
162 for (unsigned h = 0; h < height; h++)
163 {
164 for (unsigned w = 0; w < width; w++)
165 fprintf(stderr, "[%08x] ", imlib_data[h * width + w]);
166 fprintf(stderr, "\n");
167 }
168#endif
169
170 if (memcmp(imlib_data, data, width * height * sizeof(uint32_t)) != 0)
171 {
172 fprintf(stderr, "Imlib and RPNG differs!\n");
173 return 5;
174 }
175 else
176 fprintf(stderr, "Imlib and RPNG are equivalent!\n");
177
178 imlib_free_image();
179#endif
180 free(data);
181
182 return 0;
183}
184
185int main(int argc, char *argv[])
186{
187 const char *in_path = "/tmp/test.png";
188
189 if (argc > 2)
190 {
191 fprintf(stderr, "Usage: %s <png file>\n", argv[0]);
192 return 1;
193 }
194
195 if (argc == 2)
196 in_path = argv[1];
197
198 fprintf(stderr, "Doing tests...\n");
199
200 if (test_rpng(in_path) != 0)
201 {
202 fprintf(stderr, "Test failed.\n");
203 return -1;
204 }
205
206 return 0;
207}