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