Coverage Report

Created: 2024-05-20 06:11

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