git subrepo clone https://github.com/libretro/libretro-common.git deps/libretro-common
[pcsx_rearmed.git] / deps / libretro-common / formats / image_transfer.c
1 /* Copyright  (C) 2010-2020 The RetroArch team
2  *
3  * ---------------------------------------------------------------------------------------
4  * The following license statement only applies to this file (image_transfer.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 <stdint.h>
24 #include <string.h>
25
26 #include <boolean.h>
27
28 #ifdef HAVE_RPNG
29 #include <formats/rpng.h>
30 #endif
31 #ifdef HAVE_RJPEG
32 #include <formats/rjpeg.h>
33 #endif
34 #ifdef HAVE_RTGA
35 #include <formats/rtga.h>
36 #endif
37 #ifdef HAVE_RBMP
38 #include <formats/rbmp.h>
39 #endif
40
41 #include <formats/image.h>
42
43 void image_transfer_free(void *data, enum image_type_enum type)
44 {
45    switch (type)
46    {
47       case IMAGE_TYPE_TGA:
48 #ifdef HAVE_RTGA
49          rtga_free((rtga_t*)data);
50 #endif
51          break;
52       case IMAGE_TYPE_PNG:
53          {
54 #ifdef HAVE_RPNG
55             rpng_t *rpng = (rpng_t*)data;
56             if (rpng)
57                rpng_free(rpng);
58 #endif
59          }
60          break;
61       case IMAGE_TYPE_JPEG:
62 #ifdef HAVE_RJPEG
63          rjpeg_free((rjpeg_t*)data);
64 #endif
65          break;
66       case IMAGE_TYPE_BMP:
67 #ifdef HAVE_RBMP
68          rbmp_free((rbmp_t*)data);
69 #endif
70          break;
71       case IMAGE_TYPE_NONE:
72          break;
73    }
74 }
75
76 void *image_transfer_new(enum image_type_enum type)
77 {
78    switch (type)
79    {
80       case IMAGE_TYPE_PNG:
81 #ifdef HAVE_RPNG
82          return rpng_alloc();
83 #else
84          break;
85 #endif
86       case IMAGE_TYPE_JPEG:
87 #ifdef HAVE_RJPEG
88          return rjpeg_alloc();
89 #else
90          break;
91 #endif
92       case IMAGE_TYPE_TGA:
93 #ifdef HAVE_RTGA
94          return rtga_alloc();
95 #else
96          break;
97 #endif
98       case IMAGE_TYPE_BMP:
99 #ifdef HAVE_RBMP
100          return rbmp_alloc();
101 #else
102          break;
103 #endif
104       default:
105          break;
106    }
107
108    return NULL;
109 }
110
111 bool image_transfer_start(void *data, enum image_type_enum type)
112 {
113
114    switch (type)
115    {
116       case IMAGE_TYPE_PNG:
117 #ifdef HAVE_RPNG
118          if (!rpng_start((rpng_t*)data))
119             break;
120          return true;
121 #else
122          break;
123 #endif
124       case IMAGE_TYPE_JPEG:
125 #ifdef HAVE_RJPEG
126          return true;
127 #else
128          break;
129 #endif
130       case IMAGE_TYPE_TGA:
131 #ifdef HAVE_RTGA
132          return true;
133 #else
134          break;
135 #endif
136       case IMAGE_TYPE_BMP:
137          return true;
138       case IMAGE_TYPE_NONE:
139          break;
140    }
141
142    return false;
143 }
144
145 bool image_transfer_is_valid(
146       void *data,
147       enum image_type_enum type)
148 {
149    switch (type)
150    {
151       case IMAGE_TYPE_PNG:
152 #ifdef HAVE_RPNG
153          return rpng_is_valid((rpng_t*)data);
154 #else
155          break;
156 #endif
157       case IMAGE_TYPE_JPEG:
158 #ifdef HAVE_RJPEG
159          return true;
160 #else
161          break;
162 #endif
163       case IMAGE_TYPE_TGA:
164 #ifdef HAVE_RTGA
165          return true;
166 #else
167          break;
168 #endif
169       case IMAGE_TYPE_BMP:
170          return true;
171       case IMAGE_TYPE_NONE:
172          break;
173    }
174
175    return false;
176 }
177
178 void image_transfer_set_buffer_ptr(
179       void *data,
180       enum image_type_enum type,
181       void *ptr,
182       size_t len)
183 {
184    switch (type)
185    {
186       case IMAGE_TYPE_PNG:
187 #ifdef HAVE_RPNG
188          rpng_set_buf_ptr((rpng_t*)data, (uint8_t*)ptr, len);
189 #endif
190          break;
191       case IMAGE_TYPE_JPEG:
192 #ifdef HAVE_RJPEG
193          rjpeg_set_buf_ptr((rjpeg_t*)data, (uint8_t*)ptr);
194 #endif
195          break;
196       case IMAGE_TYPE_TGA:
197 #ifdef HAVE_RTGA
198          rtga_set_buf_ptr((rtga_t*)data, (uint8_t*)ptr);
199 #endif
200          break;
201       case IMAGE_TYPE_BMP:
202 #ifdef HAVE_RBMP
203          rbmp_set_buf_ptr((rbmp_t*)data, (uint8_t*)ptr);
204 #endif
205          break;
206       case IMAGE_TYPE_NONE:
207          break;
208    }
209 }
210
211 int image_transfer_process(
212       void *data,
213       enum image_type_enum type,
214       uint32_t **buf, size_t len,
215       unsigned *width, unsigned *height)
216 {
217    switch (type)
218    {
219       case IMAGE_TYPE_PNG:
220 #ifdef HAVE_RPNG
221          return rpng_process_image(
222                (rpng_t*)data,
223                (void**)buf, len, width, height);
224 #else
225          break;
226 #endif
227       case IMAGE_TYPE_JPEG:
228 #ifdef HAVE_RJPEG
229          return rjpeg_process_image((rjpeg_t*)data,
230                (void**)buf, len, width, height);
231 #else
232          break;
233 #endif
234       case IMAGE_TYPE_TGA:
235 #ifdef HAVE_RTGA
236          return rtga_process_image((rtga_t*)data,
237                (void**)buf, len, width, height);
238 #else
239          break;
240 #endif
241       case IMAGE_TYPE_BMP:
242 #ifdef HAVE_RBMP
243          return rbmp_process_image((rbmp_t*)data,
244                (void**)buf, len, width, height);
245 #else
246          break;
247 #endif
248       case IMAGE_TYPE_NONE:
249          break;
250    }
251
252    return 0;
253 }
254
255 bool image_transfer_iterate(void *data, enum image_type_enum type)
256 {
257
258    switch (type)
259    {
260       case IMAGE_TYPE_PNG:
261 #ifdef HAVE_RPNG
262          if (!rpng_iterate_image((rpng_t*)data))
263             return false;
264 #endif
265          break;
266       case IMAGE_TYPE_JPEG:
267 #ifdef HAVE_RJPEG
268          return false;
269 #else
270          break;
271 #endif
272       case IMAGE_TYPE_TGA:
273 #ifdef HAVE_RTGA
274          return false;
275 #else
276          break;
277 #endif
278       case IMAGE_TYPE_BMP:
279          return false;
280       case IMAGE_TYPE_NONE:
281          return false;
282    }
283
284    return true;
285 }