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