Coverage Report

Created: 2026-09-01 06:35

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