/src/FreeRDP/winpr/libwinpr/utils/image.c
Line | Count | Source |
1 | | /** |
2 | | * WinPR: Windows Portable Runtime |
3 | | * Image Utils |
4 | | * |
5 | | * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * Copyright 2016 Inuvika Inc. |
7 | | * Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com> |
8 | | * |
9 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | * you may not use this file except in compliance with the License. |
11 | | * You may obtain a copy of the License at |
12 | | * |
13 | | * http://www.apache.org/licenses/LICENSE-2.0 |
14 | | * |
15 | | * Unless required by applicable law or agreed to in writing, software |
16 | | * distributed under the License is distributed on an "AS IS" BASIS, |
17 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | * See the License for the specific language governing permissions and |
19 | | * limitations under the License. |
20 | | */ |
21 | | |
22 | | #include <stdlib.h> |
23 | | |
24 | | #include <winpr/config.h> |
25 | | |
26 | | #include <winpr/wtypes.h> |
27 | | #include <winpr/crt.h> |
28 | | #include <winpr/file.h> |
29 | | #include <winpr/cast.h> |
30 | | |
31 | | #include <winpr/image.h> |
32 | | |
33 | | #if defined(WINPR_UTILS_IMAGE_PNG) |
34 | | #include <png.h> |
35 | | #endif |
36 | | |
37 | | #if defined(WINPR_UTILS_IMAGE_JPEG) |
38 | | #define INT32 INT32_WINPR |
39 | | #include <jpeglib.h> |
40 | | #undef INT32 |
41 | | #endif |
42 | | |
43 | | #if defined(WINPR_UTILS_IMAGE_WEBP) |
44 | | #include <webp/encode.h> |
45 | | #include <webp/decode.h> |
46 | | #endif |
47 | | |
48 | | #if defined(WITH_LODEPNG) |
49 | | #include <lodepng.h> |
50 | | #endif |
51 | | #include <winpr/stream.h> |
52 | | |
53 | | #include "image.h" |
54 | | #include "../log.h" |
55 | 0 | #define TAG WINPR_TAG("utils.image") |
56 | | |
57 | | static SSIZE_T winpr_convert_from_jpeg(const BYTE* comp_data, size_t comp_data_bytes, UINT32* width, |
58 | | UINT32* height, UINT32* bpp, BYTE** ppdecomp_data); |
59 | | static SSIZE_T winpr_convert_from_png(const BYTE* comp_data, size_t comp_data_bytes, UINT32* width, |
60 | | UINT32* height, UINT32* bpp, BYTE** ppdecomp_data); |
61 | | static SSIZE_T winpr_convert_from_webp(const BYTE* comp_data, size_t comp_data_bytes, UINT32* width, |
62 | | UINT32* height, UINT32* bpp, BYTE** ppdecomp_data); |
63 | | |
64 | | BOOL writeBitmapFileHeader(wStream* s, const WINPR_BITMAP_FILE_HEADER* bf) |
65 | 0 | { |
66 | 0 | if (!Stream_EnsureRemainingCapacity(s, sizeof(WINPR_BITMAP_FILE_HEADER))) |
67 | 0 | return FALSE; |
68 | | |
69 | 0 | Stream_Write_UINT8(s, bf->bfType[0]); |
70 | 0 | Stream_Write_UINT8(s, bf->bfType[1]); |
71 | 0 | Stream_Write_UINT32(s, bf->bfSize); |
72 | 0 | Stream_Write_UINT16(s, bf->bfReserved1); |
73 | 0 | Stream_Write_UINT16(s, bf->bfReserved2); |
74 | 0 | Stream_Write_UINT32(s, bf->bfOffBits); |
75 | 0 | return TRUE; |
76 | 0 | } |
77 | | |
78 | | BOOL readBitmapFileHeader(wStream* s, WINPR_BITMAP_FILE_HEADER* bf) |
79 | 0 | { |
80 | 0 | static wLog* log = NULL; |
81 | 0 | if (!log) |
82 | 0 | log = WLog_Get(TAG); |
83 | |
|
84 | 0 | if (!s || !bf || |
85 | 0 | (!Stream_CheckAndLogRequiredLengthWLog(log, s, sizeof(WINPR_BITMAP_FILE_HEADER)))) |
86 | 0 | return FALSE; |
87 | | |
88 | 0 | Stream_Read_UINT8(s, bf->bfType[0]); |
89 | 0 | Stream_Read_UINT8(s, bf->bfType[1]); |
90 | 0 | Stream_Read_UINT32(s, bf->bfSize); |
91 | 0 | Stream_Read_UINT16(s, bf->bfReserved1); |
92 | 0 | Stream_Read_UINT16(s, bf->bfReserved2); |
93 | 0 | Stream_Read_UINT32(s, bf->bfOffBits); |
94 | |
|
95 | 0 | if (bf->bfSize < sizeof(WINPR_BITMAP_FILE_HEADER)) |
96 | 0 | { |
97 | 0 | WLog_Print(log, WLOG_ERROR, "Invalid bitmap::bfSize=%" PRIu32 ", require at least %" PRIuz, |
98 | 0 | bf->bfSize, sizeof(WINPR_BITMAP_FILE_HEADER)); |
99 | 0 | return FALSE; |
100 | 0 | } |
101 | | |
102 | 0 | if ((bf->bfType[0] != 'B') || (bf->bfType[1] != 'M')) |
103 | 0 | { |
104 | 0 | WLog_Print(log, WLOG_ERROR, "Invalid bitmap header [%c%c], expected [BM]", bf->bfType[0], |
105 | 0 | bf->bfType[1]); |
106 | 0 | return FALSE; |
107 | 0 | } |
108 | 0 | return Stream_CheckAndLogRequiredCapacityWLog(log, s, |
109 | 0 | bf->bfSize - sizeof(WINPR_BITMAP_FILE_HEADER)); |
110 | 0 | } |
111 | | |
112 | | BOOL writeBitmapInfoHeader(wStream* s, const WINPR_BITMAP_INFO_HEADER* bi) |
113 | 0 | { |
114 | 0 | if (!Stream_EnsureRemainingCapacity(s, sizeof(WINPR_BITMAP_INFO_HEADER))) |
115 | 0 | return FALSE; |
116 | | |
117 | 0 | Stream_Write_UINT32(s, bi->biSize); |
118 | 0 | Stream_Write_INT32(s, bi->biWidth); |
119 | 0 | Stream_Write_INT32(s, bi->biHeight); |
120 | 0 | Stream_Write_UINT16(s, bi->biPlanes); |
121 | 0 | Stream_Write_UINT16(s, bi->biBitCount); |
122 | 0 | Stream_Write_UINT32(s, bi->biCompression); |
123 | 0 | Stream_Write_UINT32(s, bi->biSizeImage); |
124 | 0 | Stream_Write_INT32(s, bi->biXPelsPerMeter); |
125 | 0 | Stream_Write_INT32(s, bi->biYPelsPerMeter); |
126 | 0 | Stream_Write_UINT32(s, bi->biClrUsed); |
127 | 0 | Stream_Write_UINT32(s, bi->biClrImportant); |
128 | 0 | return TRUE; |
129 | 0 | } |
130 | | |
131 | | BOOL readBitmapInfoHeader(wStream* s, WINPR_BITMAP_INFO_HEADER* bi, size_t* poffset) |
132 | 0 | { |
133 | 0 | if (!s || !bi || (!Stream_CheckAndLogRequiredLength(TAG, s, sizeof(WINPR_BITMAP_INFO_HEADER)))) |
134 | 0 | return FALSE; |
135 | | |
136 | 0 | const size_t start = Stream_GetPosition(s); |
137 | 0 | Stream_Read_UINT32(s, bi->biSize); |
138 | 0 | Stream_Read_INT32(s, bi->biWidth); |
139 | 0 | Stream_Read_INT32(s, bi->biHeight); |
140 | 0 | Stream_Read_UINT16(s, bi->biPlanes); |
141 | 0 | Stream_Read_UINT16(s, bi->biBitCount); |
142 | 0 | Stream_Read_UINT32(s, bi->biCompression); |
143 | 0 | Stream_Read_UINT32(s, bi->biSizeImage); |
144 | 0 | Stream_Read_INT32(s, bi->biXPelsPerMeter); |
145 | 0 | Stream_Read_INT32(s, bi->biYPelsPerMeter); |
146 | 0 | Stream_Read_UINT32(s, bi->biClrUsed); |
147 | 0 | Stream_Read_UINT32(s, bi->biClrImportant); |
148 | |
|
149 | 0 | if ((bi->biBitCount < 1) || (bi->biBitCount > 32)) |
150 | 0 | { |
151 | 0 | WLog_WARN(TAG, "invalid biBitCount=%" PRIu32, bi->biBitCount); |
152 | 0 | return FALSE; |
153 | 0 | } |
154 | | |
155 | | /* https://learn.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-bitmapinfoheader */ |
156 | 0 | size_t offset = 0; |
157 | 0 | switch (bi->biCompression) |
158 | 0 | { |
159 | 0 | case BI_RGB: |
160 | 0 | if (bi->biBitCount <= 8) |
161 | 0 | { |
162 | 0 | DWORD used = bi->biClrUsed; |
163 | 0 | if (used == 0) |
164 | 0 | used = (1 << bi->biBitCount) / 8; |
165 | 0 | offset += sizeof(RGBQUAD) * used; |
166 | 0 | } |
167 | 0 | if (bi->biSizeImage == 0) |
168 | 0 | { |
169 | 0 | UINT32 stride = WINPR_ASSERTING_INT_CAST( |
170 | 0 | uint32_t, ((((bi->biWidth * bi->biBitCount) + 31) & ~31) >> 3)); |
171 | 0 | bi->biSizeImage = WINPR_ASSERTING_INT_CAST(uint32_t, abs(bi->biHeight)) * stride; |
172 | 0 | } |
173 | 0 | break; |
174 | 0 | case BI_BITFIELDS: |
175 | 0 | offset += sizeof(DWORD) * 3; // 3 DWORD color masks |
176 | 0 | break; |
177 | 0 | default: |
178 | 0 | WLog_ERR(TAG, "unsupported biCompression %" PRIu32, bi->biCompression); |
179 | 0 | return FALSE; |
180 | 0 | } |
181 | | |
182 | 0 | if (bi->biSizeImage == 0) |
183 | 0 | { |
184 | 0 | WLog_ERR(TAG, "invalid biSizeImage %" PRIu32, bi->biSizeImage); |
185 | 0 | return FALSE; |
186 | 0 | } |
187 | | |
188 | 0 | const size_t pos = Stream_GetPosition(s) - start; |
189 | 0 | if (bi->biSize < pos) |
190 | 0 | { |
191 | 0 | WLog_ERR(TAG, "invalid biSize %" PRIu32 " < (actual) offset %" PRIuz, bi->biSize, pos); |
192 | 0 | return FALSE; |
193 | 0 | } |
194 | | |
195 | 0 | *poffset = offset; |
196 | 0 | return Stream_SafeSeek(s, bi->biSize - pos); |
197 | 0 | } |
198 | | |
199 | | BYTE* winpr_bitmap_construct_header(size_t width, size_t height, size_t bpp) |
200 | 0 | { |
201 | 0 | BYTE* result = NULL; |
202 | 0 | WINPR_BITMAP_FILE_HEADER bf = WINPR_C_ARRAY_INIT; |
203 | 0 | WINPR_BITMAP_INFO_HEADER bi = WINPR_C_ARRAY_INIT; |
204 | |
|
205 | 0 | size_t stride = (width * bpp + 7) / 8; |
206 | 0 | if ((stride % 4) != 0) |
207 | 0 | stride += 4 - (stride % 4); |
208 | |
|
209 | 0 | size_t imgSize = stride * height; |
210 | 0 | if ((width > INT32_MAX) || (height > INT32_MAX) || (bpp > UINT16_MAX) || (imgSize > UINT32_MAX)) |
211 | 0 | return NULL; |
212 | | |
213 | 0 | wStream* s = Stream_New(NULL, WINPR_IMAGE_BMP_HEADER_LEN); |
214 | 0 | if (!s) |
215 | 0 | return NULL; |
216 | | |
217 | 0 | bf.bfType[0] = 'B'; |
218 | 0 | bf.bfType[1] = 'M'; |
219 | 0 | bf.bfReserved1 = 0; |
220 | 0 | bf.bfReserved2 = 0; |
221 | 0 | bi.biSize = (UINT32)sizeof(WINPR_BITMAP_INFO_HEADER); |
222 | 0 | bf.bfOffBits = (UINT32)sizeof(WINPR_BITMAP_FILE_HEADER) + bi.biSize; |
223 | 0 | bi.biSizeImage = (UINT32)imgSize; |
224 | 0 | bf.bfSize = bf.bfOffBits + bi.biSizeImage; |
225 | 0 | bi.biWidth = (INT32)width; |
226 | 0 | bi.biHeight = -1 * (INT32)height; |
227 | 0 | bi.biPlanes = 1; |
228 | 0 | bi.biBitCount = (UINT16)bpp; |
229 | 0 | bi.biCompression = BI_RGB; |
230 | 0 | bi.biXPelsPerMeter = (INT32)width; |
231 | 0 | bi.biYPelsPerMeter = (INT32)height; |
232 | 0 | bi.biClrUsed = 0; |
233 | 0 | bi.biClrImportant = 0; |
234 | |
|
235 | 0 | size_t offset = 0; |
236 | 0 | switch (bi.biCompression) |
237 | 0 | { |
238 | 0 | case BI_RGB: |
239 | 0 | if (bi.biBitCount <= 8) |
240 | 0 | { |
241 | 0 | DWORD used = bi.biClrUsed; |
242 | 0 | if (used == 0) |
243 | 0 | used = (1 << bi.biBitCount) / 8; |
244 | 0 | offset += sizeof(RGBQUAD) * used; |
245 | 0 | } |
246 | 0 | break; |
247 | 0 | case BI_BITFIELDS: |
248 | 0 | offset += sizeof(DWORD) * 3; // 3 DWORD color masks |
249 | 0 | break; |
250 | 0 | default: |
251 | 0 | return NULL; |
252 | 0 | } |
253 | | |
254 | 0 | if (!writeBitmapFileHeader(s, &bf)) |
255 | 0 | goto fail; |
256 | | |
257 | 0 | if (!writeBitmapInfoHeader(s, &bi)) |
258 | 0 | goto fail; |
259 | | |
260 | 0 | if (!Stream_EnsureRemainingCapacity(s, offset)) |
261 | 0 | goto fail; |
262 | | |
263 | 0 | Stream_Zero(s, offset); |
264 | 0 | result = Stream_Buffer(s); |
265 | 0 | fail: |
266 | 0 | Stream_Free(s, result == 0); |
267 | 0 | return result; |
268 | 0 | } |
269 | | |
270 | | /** |
271 | | * Refer to "Compressed Image File Formats: JPEG, PNG, GIF, XBM, BMP" book |
272 | | */ |
273 | | |
274 | | WINPR_ATTR_MALLOC(free, 1) |
275 | | WINPR_ATTR_NODISCARD |
276 | | static void* winpr_bitmap_write_buffer(const BYTE* data, WINPR_ATTR_UNUSED size_t size, |
277 | | UINT32 width, UINT32 height, UINT32 stride, UINT32 bpp, |
278 | | UINT32* pSize) |
279 | 0 | { |
280 | 0 | WINPR_ASSERT(data || (size == 0)); |
281 | |
|
282 | 0 | void* result = NULL; |
283 | 0 | size_t bpp_stride = 1ull * width * (bpp / 8); |
284 | 0 | if ((bpp_stride % 4) != 0) |
285 | 0 | bpp_stride += 4 - (bpp_stride % 4); |
286 | |
|
287 | 0 | if (bpp_stride > UINT32_MAX) |
288 | 0 | return NULL; |
289 | | |
290 | 0 | wStream* s = Stream_New(NULL, 1024); |
291 | |
|
292 | 0 | if (stride == 0) |
293 | 0 | stride = (UINT32)bpp_stride; |
294 | |
|
295 | 0 | BYTE* bmp_header = winpr_bitmap_construct_header(width, height, bpp); |
296 | 0 | if (!bmp_header) |
297 | 0 | goto fail; |
298 | 0 | if (!Stream_EnsureRemainingCapacity(s, WINPR_IMAGE_BMP_HEADER_LEN)) |
299 | 0 | goto fail; |
300 | 0 | Stream_Write(s, bmp_header, WINPR_IMAGE_BMP_HEADER_LEN); |
301 | |
|
302 | 0 | if (!Stream_EnsureRemainingCapacity(s, 1ULL * bpp_stride * height)) |
303 | 0 | goto fail; |
304 | | |
305 | 0 | for (size_t y = 0; y < height; y++) |
306 | 0 | { |
307 | 0 | const BYTE* line = &data[stride * y]; |
308 | |
|
309 | 0 | Stream_Write(s, line, stride); |
310 | 0 | Stream_Zero(s, bpp_stride - stride); |
311 | 0 | } |
312 | |
|
313 | 0 | result = Stream_Buffer(s); |
314 | 0 | { |
315 | 0 | const size_t pos = Stream_GetPosition(s); |
316 | 0 | if (pos > UINT32_MAX) |
317 | 0 | goto fail; |
318 | 0 | *pSize = (UINT32)pos; |
319 | 0 | } |
320 | 0 | fail: |
321 | 0 | Stream_Free(s, result == NULL); |
322 | 0 | free(bmp_header); |
323 | 0 | return result; |
324 | 0 | } |
325 | | |
326 | | int winpr_bitmap_write(const char* filename, const BYTE* data, size_t width, size_t height, |
327 | | size_t bpp) |
328 | 0 | { |
329 | 0 | return winpr_bitmap_write_ex(filename, data, 0, width, height, bpp); |
330 | 0 | } |
331 | | |
332 | | int winpr_bitmap_write_ex(const char* filename, const BYTE* data, size_t stride, size_t width, |
333 | | size_t height, size_t bpp) |
334 | 0 | { |
335 | 0 | FILE* fp = NULL; |
336 | 0 | int ret = -1; |
337 | 0 | void* bmpdata = NULL; |
338 | 0 | const size_t bpp_stride = ((((width * bpp) + 31) & (size_t)~31) >> 3); |
339 | |
|
340 | 0 | if ((stride > UINT32_MAX) || (width > UINT32_MAX) || (height > UINT32_MAX) || |
341 | 0 | (bpp > UINT32_MAX)) |
342 | 0 | goto fail; |
343 | | |
344 | 0 | if (stride == 0) |
345 | 0 | stride = bpp_stride; |
346 | |
|
347 | 0 | { |
348 | 0 | UINT32 bmpsize = 0; |
349 | 0 | { |
350 | 0 | const size_t size = stride * 1ull * height; |
351 | 0 | bmpdata = winpr_bitmap_write_buffer(data, size, (UINT32)width, (UINT32)height, |
352 | 0 | (UINT32)stride, (UINT32)bpp, &bmpsize); |
353 | 0 | } |
354 | 0 | if (!bmpdata) |
355 | 0 | goto fail; |
356 | | |
357 | 0 | fp = winpr_fopen(filename, "w+b"); |
358 | 0 | if (!fp) |
359 | 0 | { |
360 | 0 | WLog_ERR(TAG, "failed to open file %s", filename); |
361 | 0 | goto fail; |
362 | 0 | } |
363 | | |
364 | 0 | if (fwrite(bmpdata, bmpsize, 1, fp) != 1) |
365 | 0 | goto fail; |
366 | 0 | } |
367 | | |
368 | 0 | ret = 0; |
369 | 0 | fail: |
370 | 0 | if (fp) |
371 | 0 | (void)fclose(fp); |
372 | 0 | free(bmpdata); |
373 | 0 | return ret; |
374 | 0 | } |
375 | | |
376 | | static int write_and_free(const char* filename, void* data, size_t size) |
377 | 0 | { |
378 | 0 | int status = -1; |
379 | 0 | if (!data) |
380 | 0 | goto fail; |
381 | | |
382 | 0 | { |
383 | 0 | FILE* fp = winpr_fopen(filename, "w+b"); |
384 | 0 | if (!fp) |
385 | 0 | goto fail; |
386 | | |
387 | 0 | { |
388 | 0 | const size_t w = fwrite(data, 1, size, fp); |
389 | 0 | (void)fclose(fp); |
390 | 0 | status = (w == size) ? 1 : -1; |
391 | 0 | } |
392 | 0 | } |
393 | 0 | fail: |
394 | 0 | free(data); |
395 | 0 | return status; |
396 | 0 | } |
397 | | |
398 | | int winpr_image_write(wImage* image, const char* filename) |
399 | 0 | { |
400 | 0 | WINPR_ASSERT(image); |
401 | 0 | return winpr_image_write_ex(image, WINPR_ASSERTING_INT_CAST(uint32_t, image->type), filename); |
402 | 0 | } |
403 | | |
404 | | int winpr_image_write_ex(wImage* image, UINT32 format, const char* filename) |
405 | 0 | { |
406 | 0 | WINPR_ASSERT(image); |
407 | |
|
408 | 0 | size_t size = 0; |
409 | 0 | void* data = winpr_image_write_buffer(image, format, &size); |
410 | 0 | if (!data) |
411 | 0 | return -1; |
412 | 0 | return write_and_free(filename, data, size); |
413 | 0 | } |
414 | | |
415 | | static int winpr_image_bitmap_read_buffer(wImage* image, const BYTE* buffer, size_t size) |
416 | 0 | { |
417 | 0 | int rc = -1; |
418 | 0 | BOOL vFlip = 0; |
419 | 0 | WINPR_BITMAP_FILE_HEADER bf = WINPR_C_ARRAY_INIT; |
420 | 0 | WINPR_BITMAP_INFO_HEADER bi = WINPR_C_ARRAY_INIT; |
421 | 0 | wStream sbuffer = WINPR_C_ARRAY_INIT; |
422 | 0 | wStream* s = Stream_StaticConstInit(&sbuffer, buffer, size); |
423 | |
|
424 | 0 | if (!s) |
425 | 0 | return -1; |
426 | | |
427 | 0 | size_t bmpoffset = 0; |
428 | 0 | if (!readBitmapFileHeader(s, &bf) || !readBitmapInfoHeader(s, &bi, &bmpoffset)) |
429 | 0 | goto fail; |
430 | | |
431 | 0 | if ((bf.bfType[0] != 'B') || (bf.bfType[1] != 'M')) |
432 | 0 | { |
433 | 0 | WLog_WARN(TAG, "Invalid bitmap header %c%c", bf.bfType[0], bf.bfType[1]); |
434 | 0 | goto fail; |
435 | 0 | } |
436 | | |
437 | 0 | image->type = WINPR_IMAGE_BITMAP; |
438 | |
|
439 | 0 | { |
440 | 0 | const size_t pos = Stream_GetPosition(s); |
441 | 0 | const size_t expect = bf.bfOffBits; |
442 | 0 | if (pos != expect) |
443 | 0 | { |
444 | 0 | WLog_WARN(TAG, "pos=%" PRIuz ", expected %" PRIuz ", offset=%" PRIuz, pos, expect, |
445 | 0 | bmpoffset); |
446 | 0 | goto fail; |
447 | 0 | } |
448 | 0 | } |
449 | | |
450 | 0 | if (!Stream_CheckAndLogRequiredCapacity(TAG, s, bi.biSizeImage)) |
451 | 0 | goto fail; |
452 | | |
453 | 0 | if (bi.biWidth <= 0) |
454 | 0 | { |
455 | 0 | WLog_WARN(TAG, "bi.biWidth=%" PRId32, bi.biWidth); |
456 | 0 | goto fail; |
457 | 0 | } |
458 | | |
459 | 0 | image->width = (UINT32)bi.biWidth; |
460 | |
|
461 | 0 | if (bi.biHeight < 0) |
462 | 0 | { |
463 | 0 | vFlip = FALSE; |
464 | 0 | image->height = (UINT32)(-1 * bi.biHeight); |
465 | 0 | } |
466 | 0 | else |
467 | 0 | { |
468 | 0 | vFlip = TRUE; |
469 | 0 | image->height = (UINT32)bi.biHeight; |
470 | 0 | } |
471 | |
|
472 | 0 | if (image->height <= 0) |
473 | 0 | { |
474 | 0 | WLog_WARN(TAG, "image->height=%" PRIu32, image->height); |
475 | 0 | goto fail; |
476 | 0 | } |
477 | | |
478 | 0 | image->bitsPerPixel = bi.biBitCount; |
479 | 0 | { |
480 | 0 | const size_t bpp = (bi.biBitCount + 7UL) / 8UL; |
481 | 0 | image->bytesPerPixel = WINPR_ASSERTING_INT_CAST(uint32_t, bpp); |
482 | |
|
483 | 0 | image->scanline = WINPR_ASSERTING_INT_CAST(uint32_t, bi.biWidth) * image->bytesPerPixel; |
484 | 0 | if ((image->scanline % 4) != 0) |
485 | 0 | image->scanline += 4 - image->scanline % 4; |
486 | |
|
487 | 0 | { |
488 | 0 | const size_t bmpsize = 1ULL * image->scanline * image->height; |
489 | 0 | if (bmpsize != bi.biSizeImage) |
490 | 0 | WLog_WARN(TAG, "bmpsize=%" PRIuz " != bi.biSizeImage=%" PRIu32, bmpsize, |
491 | 0 | bi.biSizeImage); |
492 | |
|
493 | 0 | { |
494 | 0 | size_t scanline = image->scanline; |
495 | 0 | if (bi.biSizeImage < bmpsize) |
496 | 0 | { |
497 | | /* Workaround for unaligned bitmaps */ |
498 | 0 | const size_t uscanline = image->width * bpp; |
499 | 0 | const size_t unaligned = image->height * uscanline; |
500 | 0 | if (bi.biSizeImage != unaligned) |
501 | 0 | goto fail; |
502 | 0 | scanline = uscanline; |
503 | 0 | } |
504 | | |
505 | 0 | image->data = NULL; |
506 | 0 | { |
507 | 0 | const size_t asize = 1ULL * image->scanline * image->height; |
508 | 0 | if (asize > 0) |
509 | 0 | image->data = (BYTE*)malloc(asize); |
510 | 0 | } |
511 | |
|
512 | 0 | if (!image->data) |
513 | 0 | goto fail; |
514 | | |
515 | 0 | if (!vFlip) |
516 | 0 | { |
517 | 0 | BYTE* pDstData = image->data; |
518 | |
|
519 | 0 | for (size_t index = 0; index < image->height; index++) |
520 | 0 | { |
521 | 0 | Stream_Read(s, pDstData, scanline); |
522 | 0 | Stream_Seek(s, image->scanline - scanline); |
523 | 0 | pDstData += scanline; |
524 | 0 | } |
525 | 0 | } |
526 | 0 | else |
527 | 0 | { |
528 | 0 | BYTE* pDstData = &(image->data[(image->height - 1ull) * image->scanline]); |
529 | |
|
530 | 0 | for (size_t index = 0; index < image->height; index++) |
531 | 0 | { |
532 | 0 | Stream_Read(s, pDstData, scanline); |
533 | 0 | Stream_Seek(s, image->scanline - scanline); |
534 | 0 | pDstData -= scanline; |
535 | 0 | } |
536 | 0 | } |
537 | 0 | } |
538 | 0 | } |
539 | 0 | } |
540 | | |
541 | 0 | rc = 1; |
542 | 0 | fail: |
543 | |
|
544 | 0 | if (rc < 0) |
545 | 0 | { |
546 | 0 | free(image->data); |
547 | 0 | image->data = NULL; |
548 | 0 | } |
549 | |
|
550 | 0 | return rc; |
551 | 0 | } |
552 | | |
553 | | int winpr_image_read(wImage* image, const char* filename) |
554 | 0 | { |
555 | 0 | int status = -1; |
556 | |
|
557 | 0 | FILE* fp = winpr_fopen(filename, "rb"); |
558 | 0 | if (!fp) |
559 | 0 | { |
560 | 0 | WLog_ERR(TAG, "failed to open file %s", filename); |
561 | 0 | return -1; |
562 | 0 | } |
563 | | |
564 | 0 | (void)fseek(fp, 0, SEEK_END); |
565 | 0 | INT64 pos = _ftelli64(fp); |
566 | 0 | (void)fseek(fp, 0, SEEK_SET); |
567 | |
|
568 | 0 | if (pos > 0) |
569 | 0 | { |
570 | 0 | BYTE* buffer = malloc((size_t)pos); |
571 | 0 | if (buffer) |
572 | 0 | { |
573 | 0 | size_t r = fread(buffer, 1, (size_t)pos, fp); |
574 | 0 | if (r == (size_t)pos) |
575 | 0 | { |
576 | 0 | status = winpr_image_read_buffer(image, buffer, (size_t)pos); |
577 | 0 | } |
578 | 0 | } |
579 | 0 | free(buffer); |
580 | 0 | } |
581 | 0 | (void)fclose(fp); |
582 | 0 | return status; |
583 | 0 | } |
584 | | |
585 | | int winpr_image_read_buffer(wImage* image, const BYTE* buffer, size_t size) |
586 | 0 | { |
587 | 0 | BYTE sig[12] = WINPR_C_ARRAY_INIT; |
588 | 0 | int status = -1; |
589 | |
|
590 | 0 | if (size < sizeof(sig)) |
591 | 0 | return -1; |
592 | | |
593 | 0 | CopyMemory(sig, buffer, sizeof(sig)); |
594 | |
|
595 | 0 | if ((sig[0] == 'B') && (sig[1] == 'M')) |
596 | 0 | { |
597 | 0 | image->type = WINPR_IMAGE_BITMAP; |
598 | 0 | status = winpr_image_bitmap_read_buffer(image, buffer, size); |
599 | 0 | } |
600 | 0 | else if ((sig[0] == 'R') && (sig[1] == 'I') && (sig[2] == 'F') && (sig[3] == 'F') && |
601 | 0 | (sig[8] == 'W') && (sig[9] == 'E') && (sig[10] == 'B') && (sig[11] == 'P')) |
602 | 0 | { |
603 | 0 | image->type = WINPR_IMAGE_WEBP; |
604 | 0 | const SSIZE_T rc = winpr_convert_from_webp(buffer, size, &image->width, &image->height, |
605 | 0 | &image->bitsPerPixel, &image->data); |
606 | 0 | if (rc >= 0) |
607 | 0 | { |
608 | 0 | image->bytesPerPixel = (image->bitsPerPixel + 7) / 8; |
609 | 0 | image->scanline = image->width * image->bytesPerPixel; |
610 | 0 | status = 1; |
611 | 0 | } |
612 | 0 | } |
613 | 0 | else if ((sig[0] == 0xFF) && (sig[1] == 0xD8) && (sig[2] == 0xFF) && (sig[3] == 0xE0) && |
614 | 0 | (sig[6] == 0x4A) && (sig[7] == 0x46) && (sig[8] == 0x49) && (sig[9] == 0x46) && |
615 | 0 | (sig[10] == 0x00)) |
616 | 0 | { |
617 | 0 | image->type = WINPR_IMAGE_JPEG; |
618 | 0 | const SSIZE_T rc = winpr_convert_from_jpeg(buffer, size, &image->width, &image->height, |
619 | 0 | &image->bitsPerPixel, &image->data); |
620 | 0 | if (rc >= 0) |
621 | 0 | { |
622 | 0 | image->bytesPerPixel = (image->bitsPerPixel + 7) / 8; |
623 | 0 | image->scanline = image->width * image->bytesPerPixel; |
624 | 0 | status = 1; |
625 | 0 | } |
626 | 0 | } |
627 | 0 | else if ((sig[0] == 0x89) && (sig[1] == 'P') && (sig[2] == 'N') && (sig[3] == 'G') && |
628 | 0 | (sig[4] == '\r') && (sig[5] == '\n') && (sig[6] == 0x1A) && (sig[7] == '\n')) |
629 | 0 | { |
630 | 0 | image->type = WINPR_IMAGE_PNG; |
631 | 0 | const SSIZE_T rc = winpr_convert_from_png(buffer, size, &image->width, &image->height, |
632 | 0 | &image->bitsPerPixel, &image->data); |
633 | 0 | if (rc >= 0) |
634 | 0 | { |
635 | 0 | image->bytesPerPixel = (image->bitsPerPixel + 7) / 8; |
636 | 0 | image->scanline = image->width * image->bytesPerPixel; |
637 | 0 | status = 1; |
638 | 0 | } |
639 | 0 | } |
640 | |
|
641 | 0 | return status; |
642 | 0 | } |
643 | | |
644 | | wImage* winpr_image_new(void) |
645 | 0 | { |
646 | 0 | wImage* image = (wImage*)calloc(1, sizeof(wImage)); |
647 | |
|
648 | 0 | if (!image) |
649 | 0 | return NULL; |
650 | | |
651 | 0 | return image; |
652 | 0 | } |
653 | | |
654 | | void winpr_image_free(wImage* image, BOOL bFreeBuffer) |
655 | 0 | { |
656 | 0 | if (!image) |
657 | 0 | return; |
658 | | |
659 | 0 | if (bFreeBuffer) |
660 | 0 | free(image->data); |
661 | |
|
662 | 0 | free(image); |
663 | 0 | } |
664 | | |
665 | | static void* winpr_convert_to_jpeg(WINPR_ATTR_UNUSED const void* data, |
666 | | WINPR_ATTR_UNUSED size_t size, WINPR_ATTR_UNUSED UINT32 width, |
667 | | WINPR_ATTR_UNUSED UINT32 height, WINPR_ATTR_UNUSED UINT32 stride, |
668 | | WINPR_ATTR_UNUSED UINT32 bpp, WINPR_ATTR_UNUSED UINT32* pSize) |
669 | 0 | { |
670 | 0 | WINPR_ASSERT(data || (size == 0)); |
671 | 0 | WINPR_ASSERT(pSize); |
672 | |
|
673 | 0 | *pSize = 0; |
674 | |
|
675 | 0 | #if !defined(WINPR_UTILS_IMAGE_JPEG) |
676 | 0 | WLog_WARN(TAG, "JPEG not supported in this build"); |
677 | 0 | return NULL; |
678 | | #else |
679 | | BYTE* outbuffer = NULL; |
680 | | unsigned long outsize = 0; |
681 | | struct jpeg_compress_struct cinfo = WINPR_C_ARRAY_INIT; |
682 | | |
683 | | const size_t expect1 = 1ull * stride * height; |
684 | | const size_t bytes = (bpp + 7) / 8; |
685 | | const size_t expect2 = 1ull * width * height * bytes; |
686 | | if (expect1 < expect2) |
687 | | return NULL; |
688 | | if (expect1 > size) |
689 | | return NULL; |
690 | | |
691 | | /* Set up the error handler. */ |
692 | | struct jpeg_error_mgr jerr = WINPR_C_ARRAY_INIT; |
693 | | cinfo.err = jpeg_std_error(&jerr); |
694 | | |
695 | | jpeg_create_compress(&cinfo); |
696 | | jpeg_mem_dest(&cinfo, &outbuffer, &outsize); |
697 | | |
698 | | cinfo.image_width = width; |
699 | | cinfo.image_height = height; |
700 | | WINPR_ASSERT(bpp <= INT32_MAX / 8); |
701 | | cinfo.input_components = (int)(bpp + 7) / 8; |
702 | | cinfo.in_color_space = (bpp > 24) ? JCS_EXT_BGRA : JCS_EXT_BGR; |
703 | | cinfo.data_precision = 8; |
704 | | |
705 | | jpeg_set_defaults(&cinfo); |
706 | | jpeg_set_quality(&cinfo, 100, TRUE); |
707 | | /* Use 4:4:4 subsampling (default is 4:2:0) */ |
708 | | cinfo.comp_info[0].h_samp_factor = cinfo.comp_info[0].v_samp_factor = 1; |
709 | | |
710 | | jpeg_start_compress(&cinfo, TRUE); |
711 | | |
712 | | const JSAMPLE* cdata = data; |
713 | | for (size_t x = 0; x < height; x++) |
714 | | { |
715 | | WINPR_ASSERT(x * stride <= UINT32_MAX); |
716 | | const JDIMENSION offset = (JDIMENSION)x * stride; |
717 | | |
718 | | /* libjpeg is not const correct, we must cast here to avoid issues |
719 | | * with newer C compilers type check errors */ |
720 | | JSAMPLE* coffset = WINPR_CAST_CONST_PTR_AWAY(&cdata[offset], JSAMPLE*); |
721 | | if (jpeg_write_scanlines(&cinfo, &coffset, 1) != 1) |
722 | | goto fail; |
723 | | } |
724 | | |
725 | | fail: |
726 | | jpeg_finish_compress(&cinfo); |
727 | | jpeg_destroy_compress(&cinfo); |
728 | | |
729 | | WINPR_ASSERT(outsize <= UINT32_MAX); |
730 | | *pSize = (UINT32)outsize; |
731 | | return outbuffer; |
732 | | #endif |
733 | 0 | } |
734 | | |
735 | | // NOLINTBEGIN(readability-non-const-parameter) |
736 | | SSIZE_T winpr_convert_from_jpeg(WINPR_ATTR_UNUSED const BYTE* comp_data, |
737 | | WINPR_ATTR_UNUSED size_t comp_data_bytes, |
738 | | WINPR_ATTR_UNUSED UINT32* width, WINPR_ATTR_UNUSED UINT32* height, |
739 | | WINPR_ATTR_UNUSED UINT32* bpp, |
740 | | WINPR_ATTR_UNUSED BYTE** ppdecomp_data) |
741 | | // NOLINTEND(readability-non-const-parameter) |
742 | 0 | { |
743 | 0 | WINPR_ASSERT(comp_data || (comp_data_bytes == 0)); |
744 | 0 | WINPR_ASSERT(width); |
745 | 0 | WINPR_ASSERT(height); |
746 | 0 | WINPR_ASSERT(bpp); |
747 | 0 | WINPR_ASSERT(ppdecomp_data); |
748 | |
|
749 | 0 | #if !defined(WINPR_UTILS_IMAGE_JPEG) |
750 | 0 | WLog_WARN(TAG, "JPEG not supported in this build"); |
751 | 0 | return -1; |
752 | | #else |
753 | | struct jpeg_decompress_struct cinfo = WINPR_C_ARRAY_INIT; |
754 | | struct jpeg_error_mgr jerr; |
755 | | SSIZE_T size = -1; |
756 | | BYTE* decomp_data = NULL; |
757 | | |
758 | | cinfo.err = jpeg_std_error(&jerr); |
759 | | jpeg_create_decompress(&cinfo); |
760 | | jpeg_mem_src(&cinfo, comp_data, comp_data_bytes); |
761 | | |
762 | | if (jpeg_read_header(&cinfo, 1) != JPEG_HEADER_OK) |
763 | | goto fail; |
764 | | |
765 | | cinfo.out_color_space = cinfo.num_components > 3 ? JCS_EXT_RGBA : JCS_EXT_BGR; |
766 | | |
767 | | *width = WINPR_ASSERTING_INT_CAST(uint32_t, cinfo.image_width); |
768 | | *height = WINPR_ASSERTING_INT_CAST(uint32_t, cinfo.image_height); |
769 | | *bpp = WINPR_ASSERTING_INT_CAST(uint32_t, cinfo.num_components * 8); |
770 | | |
771 | | if (!jpeg_start_decompress(&cinfo)) |
772 | | goto fail; |
773 | | |
774 | | size_t stride = |
775 | | 1ULL * cinfo.image_width * WINPR_ASSERTING_INT_CAST(uint32_t, cinfo.num_components); |
776 | | |
777 | | if ((stride == 0) || (cinfo.image_height == 0)) |
778 | | goto fail; |
779 | | |
780 | | decomp_data = calloc(stride, cinfo.image_height); |
781 | | if (decomp_data) |
782 | | { |
783 | | while (cinfo.output_scanline < cinfo.image_height) |
784 | | { |
785 | | JSAMPROW row = &decomp_data[cinfo.output_scanline * stride]; |
786 | | if (jpeg_read_scanlines(&cinfo, &row, 1) != 1) |
787 | | goto fail; |
788 | | } |
789 | | const size_t ssize = stride * cinfo.image_height; |
790 | | WINPR_ASSERT(ssize < SSIZE_MAX); |
791 | | size = (SSIZE_T)ssize; |
792 | | } |
793 | | jpeg_finish_decompress(&cinfo); |
794 | | |
795 | | fail: |
796 | | jpeg_destroy_decompress(&cinfo); |
797 | | *ppdecomp_data = decomp_data; |
798 | | return size; |
799 | | #endif |
800 | 0 | } |
801 | | |
802 | | static void* winpr_convert_to_webp(WINPR_ATTR_UNUSED const void* data, |
803 | | WINPR_ATTR_UNUSED size_t size, WINPR_ATTR_UNUSED UINT32 width, |
804 | | WINPR_ATTR_UNUSED UINT32 height, WINPR_ATTR_UNUSED UINT32 stride, |
805 | | WINPR_ATTR_UNUSED UINT32 bpp, UINT32* pSize) |
806 | 0 | { |
807 | 0 | WINPR_ASSERT(data || (size == 0)); |
808 | 0 | WINPR_ASSERT(pSize); |
809 | |
|
810 | 0 | *pSize = 0; |
811 | |
|
812 | 0 | #if !defined(WINPR_UTILS_IMAGE_WEBP) |
813 | 0 | WLog_WARN(TAG, "WEBP not supported in this build"); |
814 | 0 | return NULL; |
815 | | #else |
816 | | size_t dstSize = 0; |
817 | | uint8_t* pDstData = NULL; |
818 | | WINPR_ASSERT(width <= INT32_MAX); |
819 | | WINPR_ASSERT(height <= INT32_MAX); |
820 | | WINPR_ASSERT(stride <= INT32_MAX); |
821 | | switch (bpp) |
822 | | { |
823 | | case 32: |
824 | | dstSize = WebPEncodeLosslessBGRA(data, (int)width, (int)height, (int)stride, &pDstData); |
825 | | break; |
826 | | case 24: |
827 | | dstSize = WebPEncodeLosslessBGR(data, (int)width, (int)height, (int)stride, &pDstData); |
828 | | break; |
829 | | default: |
830 | | return NULL; |
831 | | } |
832 | | |
833 | | void* rc = malloc(dstSize); |
834 | | if (rc) |
835 | | { |
836 | | memcpy(rc, pDstData, dstSize); |
837 | | |
838 | | WINPR_ASSERT(dstSize <= UINT32_MAX); |
839 | | *pSize = (UINT32)dstSize; |
840 | | } |
841 | | WebPFree(pDstData); |
842 | | return rc; |
843 | | #endif |
844 | 0 | } |
845 | | |
846 | | SSIZE_T winpr_convert_from_webp(WINPR_ATTR_UNUSED const BYTE* comp_data, |
847 | | WINPR_ATTR_UNUSED size_t comp_data_bytes, UINT32* width, |
848 | | UINT32* height, UINT32* bpp, BYTE** ppdecomp_data) |
849 | 0 | { |
850 | 0 | WINPR_ASSERT(comp_data || (comp_data_bytes == 0)); |
851 | 0 | WINPR_ASSERT(width); |
852 | 0 | WINPR_ASSERT(height); |
853 | 0 | WINPR_ASSERT(bpp); |
854 | 0 | WINPR_ASSERT(ppdecomp_data); |
855 | |
|
856 | 0 | *width = 0; |
857 | 0 | *height = 0; |
858 | 0 | *bpp = 0; |
859 | 0 | *ppdecomp_data = NULL; |
860 | 0 | #if !defined(WINPR_UTILS_IMAGE_WEBP) |
861 | 0 | WLog_WARN(TAG, "WEBP not supported in this build"); |
862 | 0 | return -1; |
863 | | #else |
864 | | |
865 | | int w = 0; |
866 | | int h = 0; |
867 | | uint8_t* dst = WebPDecodeBGRA(comp_data, comp_data_bytes, &w, &h); |
868 | | if (!dst || (w < 0) || (h < 0)) |
869 | | { |
870 | | free(dst); |
871 | | return -1; |
872 | | } |
873 | | |
874 | | *width = WINPR_ASSERTING_INT_CAST(uint32_t, w); |
875 | | *height = WINPR_ASSERTING_INT_CAST(uint32_t, h); |
876 | | *bpp = 32; |
877 | | *ppdecomp_data = dst; |
878 | | return 4ll * w * h; |
879 | | #endif |
880 | 0 | } |
881 | | |
882 | | #if defined(WINPR_UTILS_IMAGE_PNG) |
883 | | struct png_mem_encode |
884 | | { |
885 | | char* buffer; |
886 | | size_t size; |
887 | | }; |
888 | | |
889 | | static void png_write_data(png_structp png_ptr, png_bytep data, png_size_t length) |
890 | | { |
891 | | /* with libpng15 next line causes pointer deference error; use libpng12 */ |
892 | | struct png_mem_encode* p = |
893 | | (struct png_mem_encode*)png_get_io_ptr(png_ptr); /* was png_ptr->io_ptr */ |
894 | | size_t nsize = p->size + length; |
895 | | |
896 | | /* allocate or grow buffer */ |
897 | | if (p->buffer) |
898 | | { |
899 | | char* tmp = realloc(p->buffer, nsize); |
900 | | if (tmp) |
901 | | p->buffer = tmp; |
902 | | } |
903 | | else |
904 | | p->buffer = malloc(nsize); |
905 | | |
906 | | if (!p->buffer) |
907 | | png_error(png_ptr, "Write Error"); |
908 | | |
909 | | /* copy new bytes to end of buffer */ |
910 | | memcpy(p->buffer + p->size, data, length); |
911 | | p->size += length; |
912 | | } |
913 | | |
914 | | /* This is optional but included to show how png_set_write_fn() is called */ |
915 | | static void png_flush(WINPR_ATTR_UNUSED png_structp png_ptr) |
916 | | { |
917 | | } |
918 | | |
919 | | static SSIZE_T save_png_to_buffer(UINT32 bpp, UINT32 width, uint32_t stride, UINT32 height, |
920 | | const uint8_t* data, size_t size, void** pDstData) |
921 | | { |
922 | | SSIZE_T rc = -1; |
923 | | png_structp png_ptr = NULL; |
924 | | png_infop info_ptr = NULL; |
925 | | png_byte** row_pointers = NULL; |
926 | | struct png_mem_encode state = WINPR_C_ARRAY_INIT; |
927 | | |
928 | | *pDstData = NULL; |
929 | | |
930 | | if (!data || (size == 0)) |
931 | | return 0; |
932 | | |
933 | | WINPR_ASSERT(pDstData); |
934 | | |
935 | | if (size < (1ULL * stride * height)) |
936 | | goto fail; |
937 | | |
938 | | /* Initialize the write struct. */ |
939 | | png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
940 | | if (png_ptr == NULL) |
941 | | goto fail; |
942 | | |
943 | | /* Initialize the info struct. */ |
944 | | info_ptr = png_create_info_struct(png_ptr); |
945 | | if (info_ptr == NULL) |
946 | | goto fail; |
947 | | |
948 | | /* Set up error handling. */ |
949 | | if (setjmp(png_jmpbuf(png_ptr))) |
950 | | goto fail; |
951 | | |
952 | | /* Set image attributes. */ |
953 | | int colorType = PNG_COLOR_TYPE_PALETTE; |
954 | | if (bpp > 8) |
955 | | colorType = PNG_COLOR_TYPE_RGB; |
956 | | if (bpp > 16) |
957 | | colorType = PNG_COLOR_TYPE_RGB; |
958 | | if (bpp > 24) |
959 | | colorType = PNG_COLOR_TYPE_RGBA; |
960 | | |
961 | | png_set_IHDR(png_ptr, info_ptr, width, height, 8, colorType, PNG_INTERLACE_NONE, |
962 | | PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
963 | | |
964 | | /* Initialize rows of PNG. */ |
965 | | row_pointers = (png_byte**)png_malloc(png_ptr, height * sizeof(png_byte*)); |
966 | | for (size_t y = 0; y < height; ++y) |
967 | | { |
968 | | const uint8_t* line = &data[y * stride]; |
969 | | uint8_t* row = png_malloc(png_ptr, sizeof(uint8_t) * stride); |
970 | | row_pointers[y] = (png_byte*)row; |
971 | | for (size_t x = 0; x < width; ++x) |
972 | | { |
973 | | |
974 | | *row++ = *line++; |
975 | | if (bpp > 8) |
976 | | *row++ = *line++; |
977 | | if (bpp > 16) |
978 | | *row++ = *line++; |
979 | | if (bpp > 24) |
980 | | *row++ = *line++; |
981 | | } |
982 | | } |
983 | | |
984 | | /* Actually write the image data. */ |
985 | | png_set_write_fn(png_ptr, &state, png_write_data, png_flush); |
986 | | png_set_rows(png_ptr, info_ptr, row_pointers); |
987 | | png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_BGR, NULL); |
988 | | |
989 | | /* Cleanup. */ |
990 | | for (size_t y = 0; y < height; y++) |
991 | | png_free(png_ptr, row_pointers[y]); |
992 | | png_free(png_ptr, (void*)row_pointers); |
993 | | |
994 | | /* Finish writing. */ |
995 | | if (state.size > SSIZE_MAX) |
996 | | goto fail; |
997 | | rc = (SSIZE_T)state.size; |
998 | | *pDstData = state.buffer; |
999 | | fail: |
1000 | | png_destroy_write_struct(&png_ptr, &info_ptr); |
1001 | | if (rc < 0) |
1002 | | free(state.buffer); |
1003 | | return rc; |
1004 | | } |
1005 | | |
1006 | | typedef struct |
1007 | | { |
1008 | | png_bytep buffer; |
1009 | | png_uint_32 bufsize; |
1010 | | png_uint_32 current_pos; |
1011 | | } MEMORY_READER_STATE; |
1012 | | |
1013 | | static void read_data_memory(png_structp png_ptr, png_bytep data, size_t length) |
1014 | | { |
1015 | | MEMORY_READER_STATE* f = png_get_io_ptr(png_ptr); |
1016 | | if (length > (f->bufsize - f->current_pos)) |
1017 | | png_error(png_ptr, "read error in read_data_memory (loadpng)"); |
1018 | | else |
1019 | | { |
1020 | | memcpy(data, f->buffer + f->current_pos, length); |
1021 | | f->current_pos += length; |
1022 | | } |
1023 | | } |
1024 | | |
1025 | | static void* winpr_read_png_from_buffer(const void* data, size_t SrcSize, size_t* pSize, |
1026 | | UINT32* pWidth, UINT32* pHeight, UINT32* pBpp) |
1027 | | { |
1028 | | void* rc = NULL; |
1029 | | png_uint_32 width = 0; |
1030 | | png_uint_32 height = 0; |
1031 | | int bit_depth = 0; |
1032 | | int color_type = 0; |
1033 | | int interlace_type = 0; |
1034 | | int transforms = PNG_TRANSFORM_IDENTITY; |
1035 | | MEMORY_READER_STATE memory_reader_state = WINPR_C_ARRAY_INIT; |
1036 | | png_bytepp row_pointers = NULL; |
1037 | | png_infop info_ptr = NULL; |
1038 | | if (SrcSize > UINT32_MAX) |
1039 | | return NULL; |
1040 | | |
1041 | | png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
1042 | | if (!png_ptr) |
1043 | | goto fail; |
1044 | | info_ptr = png_create_info_struct(png_ptr); |
1045 | | if (!info_ptr) |
1046 | | goto fail; |
1047 | | |
1048 | | memory_reader_state.buffer = WINPR_CAST_CONST_PTR_AWAY(data, png_bytep); |
1049 | | memory_reader_state.bufsize = (UINT32)SrcSize; |
1050 | | memory_reader_state.current_pos = 0; |
1051 | | |
1052 | | png_set_read_fn(png_ptr, &memory_reader_state, read_data_memory); |
1053 | | |
1054 | | transforms |= PNG_TRANSFORM_BGR; |
1055 | | png_read_png(png_ptr, info_ptr, transforms, NULL); |
1056 | | |
1057 | | if (png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, |
1058 | | NULL, NULL) != 1) |
1059 | | goto fail; |
1060 | | |
1061 | | WINPR_ASSERT(bit_depth >= 0); |
1062 | | const png_byte channelcount = png_get_channels(png_ptr, info_ptr); |
1063 | | const size_t bpp = channelcount * (size_t)bit_depth; |
1064 | | |
1065 | | row_pointers = png_get_rows(png_ptr, info_ptr); |
1066 | | if (row_pointers) |
1067 | | { |
1068 | | const size_t stride = 1ULL * width * bpp / 8ull; |
1069 | | const size_t png_stride = png_get_rowbytes(png_ptr, info_ptr); |
1070 | | const size_t size = 1ULL * width * height * bpp / 8ull; |
1071 | | const size_t copybytes = stride > png_stride ? png_stride : stride; |
1072 | | |
1073 | | rc = malloc(size); |
1074 | | if (rc) |
1075 | | { |
1076 | | char* cur = rc; |
1077 | | for (png_uint_32 i = 0; i < height; i++) |
1078 | | { |
1079 | | memcpy(cur, row_pointers[i], copybytes); |
1080 | | cur += stride; |
1081 | | } |
1082 | | *pSize = size; |
1083 | | *pWidth = width; |
1084 | | *pHeight = height; |
1085 | | WINPR_ASSERT(bpp <= UINT32_MAX); |
1086 | | *pBpp = (UINT32)bpp; |
1087 | | } |
1088 | | } |
1089 | | fail: |
1090 | | |
1091 | | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
1092 | | return rc; |
1093 | | } |
1094 | | #endif |
1095 | | |
1096 | | static void* winpr_convert_to_png(WINPR_ATTR_UNUSED const void* data, WINPR_ATTR_UNUSED size_t size, |
1097 | | WINPR_ATTR_UNUSED UINT32 width, WINPR_ATTR_UNUSED UINT32 height, |
1098 | | WINPR_ATTR_UNUSED UINT32 stride, WINPR_ATTR_UNUSED UINT32 bpp, |
1099 | | UINT32* pSize) |
1100 | 0 | { |
1101 | 0 | WINPR_ASSERT(data || (size == 0)); |
1102 | 0 | WINPR_ASSERT(pSize); |
1103 | |
|
1104 | 0 | *pSize = 0; |
1105 | |
|
1106 | | #if defined(WINPR_UTILS_IMAGE_PNG) |
1107 | | void* dst = NULL; |
1108 | | SSIZE_T rc = save_png_to_buffer(bpp, width, stride, height, data, size, &dst); |
1109 | | if (rc <= 0) |
1110 | | return NULL; |
1111 | | *pSize = (UINT32)rc; |
1112 | | return dst; |
1113 | | #elif defined(WITH_LODEPNG) |
1114 | | { |
1115 | | BYTE* dst = NULL; |
1116 | | size_t dstsize = 0; |
1117 | | unsigned rc = 1; |
1118 | | |
1119 | | switch (bpp) |
1120 | | { |
1121 | | case 32: |
1122 | | rc = lodepng_encode32(&dst, &dstsize, data, width, height); |
1123 | | break; |
1124 | | case 24: |
1125 | | rc = lodepng_encode24(&dst, &dstsize, data, width, height); |
1126 | | break; |
1127 | | default: |
1128 | | break; |
1129 | | } |
1130 | | if (rc) |
1131 | | return NULL; |
1132 | | *pSize = (UINT32)dstsize; |
1133 | | return dst; |
1134 | | } |
1135 | | #else |
1136 | 0 | WLog_WARN(TAG, "PNG not supported in this build"); |
1137 | 0 | return NULL; |
1138 | 0 | #endif |
1139 | 0 | } |
1140 | | |
1141 | | SSIZE_T winpr_convert_from_png(WINPR_ATTR_UNUSED const BYTE* comp_data, |
1142 | | WINPR_ATTR_UNUSED size_t comp_data_bytes, |
1143 | | WINPR_ATTR_UNUSED UINT32* width, WINPR_ATTR_UNUSED UINT32* height, |
1144 | | WINPR_ATTR_UNUSED UINT32* bpp, |
1145 | | WINPR_ATTR_UNUSED BYTE** ppdecomp_data) |
1146 | 0 | { |
1147 | | #if defined(WINPR_UTILS_IMAGE_PNG) |
1148 | | size_t len = 0; |
1149 | | *ppdecomp_data = |
1150 | | winpr_read_png_from_buffer(comp_data, comp_data_bytes, &len, width, height, bpp); |
1151 | | if (!*ppdecomp_data) |
1152 | | return -1; |
1153 | | return (SSIZE_T)len; |
1154 | | #elif defined(WITH_LODEPNG) |
1155 | | *bpp = 32; |
1156 | | return lodepng_decode32((unsigned char**)ppdecomp_data, width, height, comp_data, |
1157 | | comp_data_bytes); |
1158 | | #else |
1159 | 0 | WLog_WARN(TAG, "PNG not supported in this build"); |
1160 | 0 | return -1; |
1161 | 0 | #endif |
1162 | 0 | } |
1163 | | |
1164 | | BOOL winpr_image_format_is_supported(UINT32 format) |
1165 | 0 | { |
1166 | 0 | switch (format) |
1167 | 0 | { |
1168 | 0 | case WINPR_IMAGE_BITMAP: |
1169 | | #if defined(WINPR_UTILS_IMAGE_PNG) || defined(WITH_LODEPNG) |
1170 | | case WINPR_IMAGE_PNG: |
1171 | | #endif |
1172 | | #if defined(WINPR_UTILS_IMAGE_JPEG) |
1173 | | case WINPR_IMAGE_JPEG: |
1174 | | #endif |
1175 | | #if defined(WINPR_UTILS_IMAGE_WEBP) |
1176 | | case WINPR_IMAGE_WEBP: |
1177 | | #endif |
1178 | 0 | return TRUE; |
1179 | 0 | default: |
1180 | 0 | return FALSE; |
1181 | 0 | } |
1182 | 0 | } |
1183 | | |
1184 | | static BYTE* convert(const wImage* image, size_t* pstride, WINPR_ATTR_UNUSED UINT32 flags) |
1185 | 0 | { |
1186 | 0 | WINPR_ASSERT(image); |
1187 | 0 | WINPR_ASSERT(pstride); |
1188 | |
|
1189 | 0 | *pstride = 0; |
1190 | 0 | if (image->bitsPerPixel < 24) |
1191 | 0 | return NULL; |
1192 | | |
1193 | 0 | const size_t stride = image->width * 4ull; |
1194 | 0 | BYTE* data = calloc(stride, image->height); |
1195 | 0 | if (data) |
1196 | 0 | { |
1197 | 0 | for (size_t y = 0; y < image->height; y++) |
1198 | 0 | { |
1199 | 0 | const BYTE* srcLine = &image->data[image->scanline * y]; |
1200 | 0 | BYTE* dstLine = &data[stride * y]; |
1201 | 0 | if (image->bitsPerPixel == 32) |
1202 | 0 | memcpy(dstLine, srcLine, stride); |
1203 | 0 | else |
1204 | 0 | { |
1205 | 0 | for (size_t x = 0; x < image->width; x++) |
1206 | 0 | { |
1207 | 0 | const BYTE* src = &srcLine[image->bytesPerPixel * x]; |
1208 | 0 | BYTE* dst = &dstLine[4ull * x]; |
1209 | 0 | BYTE b = *src++; |
1210 | 0 | BYTE g = *src++; |
1211 | 0 | BYTE r = *src++; |
1212 | |
|
1213 | 0 | *dst++ = b; |
1214 | 0 | *dst++ = g; |
1215 | 0 | *dst++ = r; |
1216 | 0 | *dst++ = 0xff; |
1217 | 0 | } |
1218 | 0 | } |
1219 | 0 | } |
1220 | 0 | *pstride = stride; |
1221 | 0 | } |
1222 | 0 | return data; |
1223 | 0 | } |
1224 | | |
1225 | | static BOOL compare_byte_relaxed(BYTE a, BYTE b, UINT32 flags) |
1226 | 0 | { |
1227 | 0 | if (a != b) |
1228 | 0 | { |
1229 | 0 | if ((flags & WINPR_IMAGE_CMP_FUZZY) != 0) |
1230 | 0 | { |
1231 | 0 | const int diff = abs((int)a) - abs((int)b); |
1232 | | /* filter out quantization errors */ |
1233 | 0 | if (diff > 6) |
1234 | 0 | return FALSE; |
1235 | 0 | } |
1236 | 0 | else |
1237 | 0 | { |
1238 | 0 | return FALSE; |
1239 | 0 | } |
1240 | 0 | } |
1241 | 0 | return TRUE; |
1242 | 0 | } |
1243 | | |
1244 | | static BOOL compare_pixel(const BYTE* pa, const BYTE* pb, UINT32 flags) |
1245 | 0 | { |
1246 | 0 | WINPR_ASSERT(pa); |
1247 | 0 | WINPR_ASSERT(pb); |
1248 | |
|
1249 | 0 | if (!compare_byte_relaxed(*pa++, *pb++, flags)) |
1250 | 0 | return FALSE; |
1251 | 0 | if (!compare_byte_relaxed(*pa++, *pb++, flags)) |
1252 | 0 | return FALSE; |
1253 | 0 | if (!compare_byte_relaxed(*pa++, *pb++, flags)) |
1254 | 0 | return FALSE; |
1255 | 0 | if ((flags & WINPR_IMAGE_CMP_IGNORE_ALPHA) == 0) |
1256 | 0 | { |
1257 | 0 | if (!compare_byte_relaxed(*pa++, *pb++, flags)) |
1258 | 0 | return FALSE; |
1259 | 0 | } |
1260 | 0 | return TRUE; |
1261 | 0 | } |
1262 | | |
1263 | | BOOL winpr_image_equal(const wImage* imageA, const wImage* imageB, UINT32 flags) |
1264 | 0 | { |
1265 | 0 | if (imageA == imageB) |
1266 | 0 | return TRUE; |
1267 | 0 | if (!imageA || !imageB) |
1268 | 0 | return FALSE; |
1269 | | |
1270 | 0 | if (imageA->height != imageB->height) |
1271 | 0 | return FALSE; |
1272 | 0 | if (imageA->width != imageB->width) |
1273 | 0 | return FALSE; |
1274 | | |
1275 | 0 | if ((flags & WINPR_IMAGE_CMP_IGNORE_DEPTH) == 0) |
1276 | 0 | { |
1277 | 0 | if (imageA->bitsPerPixel != imageB->bitsPerPixel) |
1278 | 0 | return FALSE; |
1279 | 0 | if (imageA->bytesPerPixel != imageB->bytesPerPixel) |
1280 | 0 | return FALSE; |
1281 | 0 | } |
1282 | | |
1283 | 0 | BOOL rc = FALSE; |
1284 | 0 | size_t astride = 0; |
1285 | 0 | size_t bstride = 0; |
1286 | 0 | BYTE* dataA = convert(imageA, &astride, flags); |
1287 | 0 | BYTE* dataB = convert(imageA, &bstride, flags); |
1288 | 0 | if (dataA && dataB && (astride == bstride)) |
1289 | 0 | { |
1290 | 0 | rc = TRUE; |
1291 | 0 | for (size_t y = 0; y < imageA->height; y++) |
1292 | 0 | { |
1293 | 0 | const BYTE* lineA = &dataA[astride * y]; |
1294 | 0 | const BYTE* lineB = &dataB[bstride * y]; |
1295 | |
|
1296 | 0 | for (size_t x = 0; x < imageA->width; x++) |
1297 | 0 | { |
1298 | 0 | const BYTE* pa = &lineA[x * 4ull]; |
1299 | 0 | const BYTE* pb = &lineB[x * 4ull]; |
1300 | |
|
1301 | 0 | if (!compare_pixel(pa, pb, flags)) |
1302 | 0 | rc = FALSE; |
1303 | 0 | } |
1304 | 0 | } |
1305 | 0 | } |
1306 | 0 | free(dataA); |
1307 | 0 | free(dataB); |
1308 | 0 | return rc; |
1309 | 0 | } |
1310 | | |
1311 | | const char* winpr_image_format_mime(UINT32 format) |
1312 | 0 | { |
1313 | 0 | switch (format) |
1314 | 0 | { |
1315 | 0 | case WINPR_IMAGE_BITMAP: |
1316 | 0 | return "image/bmp"; |
1317 | 0 | case WINPR_IMAGE_PNG: |
1318 | 0 | return "image/png"; |
1319 | 0 | case WINPR_IMAGE_WEBP: |
1320 | 0 | return "image/webp"; |
1321 | 0 | case WINPR_IMAGE_JPEG: |
1322 | 0 | return "image/jpeg"; |
1323 | 0 | default: |
1324 | 0 | return NULL; |
1325 | 0 | } |
1326 | 0 | } |
1327 | | |
1328 | | const char* winpr_image_format_extension(UINT32 format) |
1329 | 0 | { |
1330 | 0 | switch (format) |
1331 | 0 | { |
1332 | 0 | case WINPR_IMAGE_BITMAP: |
1333 | 0 | return "bmp"; |
1334 | 0 | case WINPR_IMAGE_PNG: |
1335 | 0 | return "png"; |
1336 | 0 | case WINPR_IMAGE_WEBP: |
1337 | 0 | return "webp"; |
1338 | 0 | case WINPR_IMAGE_JPEG: |
1339 | 0 | return "jpg"; |
1340 | 0 | default: |
1341 | 0 | return NULL; |
1342 | 0 | } |
1343 | 0 | } |
1344 | | |
1345 | | void* winpr_image_write_buffer(wImage* image, UINT32 format, size_t* psize) |
1346 | 0 | { |
1347 | 0 | WINPR_ASSERT(image); |
1348 | 0 | switch (format) |
1349 | 0 | { |
1350 | 0 | case WINPR_IMAGE_BITMAP: |
1351 | 0 | { |
1352 | 0 | UINT32 outsize = 0; |
1353 | 0 | size_t size = 1ull * image->height * image->scanline; |
1354 | 0 | void* data = winpr_bitmap_write_buffer(image->data, size, image->width, image->height, |
1355 | 0 | image->scanline, image->bitsPerPixel, &outsize); |
1356 | 0 | *psize = outsize; |
1357 | 0 | return data; |
1358 | 0 | } |
1359 | 0 | case WINPR_IMAGE_WEBP: |
1360 | 0 | { |
1361 | 0 | UINT32 outsize = 0; |
1362 | 0 | size_t size = 1ull * image->height * image->scanline; |
1363 | 0 | void* data = winpr_convert_to_webp(image->data, size, image->width, image->height, |
1364 | 0 | image->scanline, image->bitsPerPixel, &outsize); |
1365 | 0 | *psize = outsize; |
1366 | 0 | return data; |
1367 | 0 | } |
1368 | 0 | case WINPR_IMAGE_JPEG: |
1369 | 0 | { |
1370 | 0 | UINT32 outsize = 0; |
1371 | 0 | size_t size = 1ull * image->height * image->scanline; |
1372 | 0 | void* data = winpr_convert_to_jpeg(image->data, size, image->width, image->height, |
1373 | 0 | image->scanline, image->bitsPerPixel, &outsize); |
1374 | 0 | *psize = outsize; |
1375 | 0 | return data; |
1376 | 0 | } |
1377 | 0 | case WINPR_IMAGE_PNG: |
1378 | 0 | { |
1379 | 0 | UINT32 outsize = 0; |
1380 | 0 | size_t size = 1ull * image->height * image->scanline; |
1381 | 0 | void* data = winpr_convert_to_png(image->data, size, image->width, image->height, |
1382 | 0 | image->scanline, image->bitsPerPixel, &outsize); |
1383 | 0 | *psize = outsize; |
1384 | 0 | return data; |
1385 | 0 | } |
1386 | 0 | default: |
1387 | 0 | *psize = 0; |
1388 | | return NULL; |
1389 | 0 | } |
1390 | 0 | } |