24#include <winpr/config.h>
26#include <winpr/wtypes.h>
28#include <winpr/file.h>
29#include <winpr/cast.h>
31#include <winpr/image.h>
33#if defined(WINPR_UTILS_IMAGE_PNG)
37#if defined(WINPR_UTILS_IMAGE_JPEG)
38#define INT32 INT32_WINPR
43#if defined(WINPR_UTILS_IMAGE_WEBP)
44#include <webp/encode.h>
45#include <webp/decode.h>
48#if defined(WITH_LODEPNG)
51#include <winpr/stream.h>
55#define TAG WINPR_TAG("utils.image")
57#if defined(WINPR_UTILS_IMAGE_JPEG)
59#define MAX(a, b) ((a) > (b)) ? (a) : (b)
64static SSIZE_T winpr_convert_from_jpeg(
const BYTE* comp_data,
size_t comp_data_bytes, UINT32* width,
65 UINT32* height, UINT32* bpp, BYTE** ppdecomp_data,
69static SSIZE_T winpr_convert_from_png(
const BYTE* comp_data,
size_t comp_data_bytes, UINT32* width,
70 UINT32* height, UINT32* bpp, BYTE** ppdecomp_data,
74static SSIZE_T winpr_convert_from_webp(
const BYTE* comp_data,
size_t comp_data_bytes, UINT32* width,
75 UINT32* height, UINT32* bpp, BYTE** ppdecomp_data,
83 Stream_Write_UINT8(s, bf->bfType[0]);
84 Stream_Write_UINT8(s, bf->bfType[1]);
85 Stream_Write_UINT32(s, bf->bfSize);
86 Stream_Write_UINT16(s, bf->bfReserved1);
87 Stream_Write_UINT16(s, bf->bfReserved2);
88 Stream_Write_UINT32(s, bf->bfOffBits);
94 static wLog* log =
nullptr;
102 Stream_Read_UINT8(s, bf->bfType[0]);
103 Stream_Read_UINT8(s, bf->bfType[1]);
104 Stream_Read_UINT32(s, bf->bfSize);
105 Stream_Read_UINT16(s, bf->bfReserved1);
106 Stream_Read_UINT16(s, bf->bfReserved2);
107 Stream_Read_UINT32(s, bf->bfOffBits);
111 WLog_Print(log, WLOG_ERROR,
"Invalid bitmap::bfSize=%" PRIu32
", require at least %" PRIuz,
116 if ((bf->bfType[0] !=
'B') || (bf->bfType[1] !=
'M'))
118 WLog_Print(log, WLOG_ERROR,
"Invalid bitmap header [%c%c], expected [BM]", bf->bfType[0],
122 return Stream_CheckAndLogRequiredLengthWLog(log, s,
131 Stream_Write_UINT32(s, bi->biSize);
132 Stream_Write_INT32(s, bi->biWidth);
133 Stream_Write_INT32(s, bi->biHeight);
134 Stream_Write_UINT16(s, bi->biPlanes);
135 Stream_Write_UINT16(s, bi->biBitCount);
136 Stream_Write_UINT32(s, bi->biCompression);
137 Stream_Write_UINT32(s, bi->biSizeImage);
138 Stream_Write_INT32(s, bi->biXPelsPerMeter);
139 Stream_Write_INT32(s, bi->biYPelsPerMeter);
140 Stream_Write_UINT32(s, bi->biClrUsed);
141 Stream_Write_UINT32(s, bi->biClrImportant);
150 const size_t start = Stream_GetPosition(s);
151 Stream_Read_UINT32(s, bi->biSize);
152 Stream_Read_INT32(s, bi->biWidth);
153 Stream_Read_INT32(s, bi->biHeight);
154 Stream_Read_UINT16(s, bi->biPlanes);
155 Stream_Read_UINT16(s, bi->biBitCount);
156 Stream_Read_UINT32(s, bi->biCompression);
157 Stream_Read_UINT32(s, bi->biSizeImage);
158 Stream_Read_INT32(s, bi->biXPelsPerMeter);
159 Stream_Read_INT32(s, bi->biYPelsPerMeter);
160 Stream_Read_UINT32(s, bi->biClrUsed);
161 Stream_Read_UINT32(s, bi->biClrImportant);
163 switch (bi->biBitCount)
173 WLog_WARN(TAG,
"invalid biBitCount=%" PRIu32, bi->biBitCount);
179 WLog_WARN(TAG,
"negative biWidth=%" PRId32, bi->biWidth);
185 switch (bi->biCompression)
188 if (bi->biBitCount <= 8)
190 DWORD used = bi->biClrUsed;
192 used = 1u << bi->biBitCount;
193 offset +=
sizeof(
RGBQUAD) * used;
195 if (bi->biSizeImage == 0)
197 const UINT64 rawustride =
198 WINPR_ASSERTING_INT_CAST(UINT64, bi->biWidth) * bi->biBitCount;
199 const UINT64 ustride = ((rawustride + 31) & ~31u) >> 3;
200 if (ustride > UINT32_MAX)
202 WLog_ERR(TAG,
"bi->biWidth * bi->biBitCount > UINT32_MAX");
206 const UINT32 stride = WINPR_ASSERTING_INT_CAST(uint32_t, ustride);
207 const UINT64 usize = WINPR_ASSERTING_INT_CAST(UINT64, llabs(bi->biHeight)) * stride;
208 if (usize > UINT32_MAX)
210 WLog_ERR(TAG,
"abs(bi->biHeight) * stride > UINT32_MAX");
213 bi->biSizeImage = WINPR_ASSERTING_INT_CAST(uint32_t, usize);
219 offset +=
sizeof(DWORD) * 3;
222 WLog_ERR(TAG,
"unsupported biCompression %" PRIu32, bi->biCompression);
226 if (bi->biSizeImage == 0)
228 WLog_ERR(TAG,
"invalid biSizeImage %" PRIu32, bi->biSizeImage);
232 const size_t pos = Stream_GetPosition(s) - start;
233 if (bi->biSize < pos)
235 WLog_ERR(TAG,
"invalid biSize %" PRIu32
" < (actual) offset %" PRIuz, bi->biSize, pos);
240 return Stream_SafeSeek(s, bi->biSize - pos);
243BYTE* winpr_bitmap_construct_header(
size_t width,
size_t height,
size_t bpp)
245 BYTE* result =
nullptr;
249 size_t stride = (width * bpp + 7) / 8;
250 if ((stride % 4) != 0)
251 stride += 4 - (stride % 4);
253 size_t imgSize = stride * height;
254 if ((width > INT32_MAX) || (height > INT32_MAX) || (bpp > UINT16_MAX) || (imgSize > UINT32_MAX))
257 wStream* s = Stream_New(
nullptr, WINPR_IMAGE_BMP_HEADER_LEN);
267 bi.biSizeImage = (UINT32)imgSize;
268 bf.bfSize = bf.bfOffBits + bi.biSizeImage;
269 bi.biWidth = (INT32)width;
270 bi.biHeight = -1 * (INT32)height;
272 bi.biBitCount = (UINT16)bpp;
273 bi.biCompression = BI_RGB;
274 bi.biXPelsPerMeter = (INT32)width;
275 bi.biYPelsPerMeter = (INT32)height;
277 bi.biClrImportant = 0;
280 switch (bi.biCompression)
283 if (bi.biBitCount <= 8)
285 DWORD used = bi.biClrUsed;
287 used = (1u << bi.biBitCount) / 8;
288 offset +=
sizeof(
RGBQUAD) * used;
292 offset +=
sizeof(DWORD) * 3;
298 if (!writeBitmapFileHeader(s, &bf))
301 if (!writeBitmapInfoHeader(s, &bi))
304 if (!Stream_EnsureRemainingCapacity(s, offset))
307 Stream_Zero(s, offset);
308 const size_t pos = Stream_GetPosition(s);
309 if (pos != bf.bfOffBits)
311 result = Stream_Buffer(s);
313 Stream_Free(s, result ==
nullptr);
321WINPR_ATTR_MALLOC(free, 1)
322static
void* winpr_bitmap_write_buffer(const BYTE* data,
size_t size, UINT32 width, UINT32 height,
323 UINT32 stride, UINT32 bpp, UINT32* pSize)
325 WINPR_ASSERT(data || (size == 0));
328 void* result =
nullptr;
329 if ((height == 0) || (width == 0) || (bpp == 0) || (width > SIZE_MAX / height) ||
330 (width > SIZE_MAX / bpp))
333 size_t bpp_stride = 1ull * width * bpp;
336 if ((bpp_stride % 8) != 0)
337 bpp_stride += 8 - (bpp_stride % 8);
341 if ((bpp_stride % 4) != 0)
342 bpp_stride += 4 - (bpp_stride % 4);
344 if ((bpp_stride < 4) || (bpp_stride > UINT32_MAX))
348 stride = (UINT32)bpp_stride;
350 if (stride > SIZE_MAX / height)
353 if (stride > size / height)
356 if (bpp_stride > stride)
359 wStream* s = Stream_New(
nullptr, 1024);
360 BYTE* bmp_header = winpr_bitmap_construct_header(width, height, bpp);
363 if (!Stream_EnsureRemainingCapacity(s, WINPR_IMAGE_BMP_HEADER_LEN))
365 Stream_Write(s, bmp_header, WINPR_IMAGE_BMP_HEADER_LEN);
367 for (
size_t y = 0; y < height; y++)
369 if (!Stream_EnsureRemainingCapacity(s, stride))
372 size_t offset = y * stride;
373 if (offset + stride <= size)
375 const BYTE* line = &data[offset];
377 Stream_Write(s, line, stride);
380 Stream_Zero(s, stride);
383 result = Stream_Buffer(s);
385 const size_t pos = Stream_GetPosition(s);
386 if (pos > UINT32_MAX)
388 *pSize = (UINT32)pos;
391 Stream_Free(s, result ==
nullptr);
396int winpr_bitmap_write(
const char* filename,
const BYTE* data,
size_t width,
size_t height,
399 return winpr_bitmap_write_ex(filename, data, 0, width, height, bpp);
402int winpr_bitmap_write_ex(
const char* filename,
const BYTE* data,
size_t stride,
size_t width,
403 size_t height,
size_t bpp)
407 void* bmpdata =
nullptr;
408 const size_t bpp_stride = ((((width * bpp) + 31) & (size_t)~31) >> 3);
410 if ((stride > UINT32_MAX) || (width > UINT32_MAX) || (height > UINT32_MAX) ||
420 const size_t size = stride * 1ull * height;
421 bmpdata = winpr_bitmap_write_buffer(data, size, (UINT32)width, (UINT32)height,
422 (UINT32)stride, (UINT32)bpp, &bmpsize);
427 fp = winpr_fopen(filename,
"w+b");
430 WLog_ERR(TAG,
"failed to open file %s", filename);
434 if (fwrite(bmpdata, bmpsize, 1, fp) != 1)
447static int write_and_free(
const char* filename,
void* data,
size_t size)
454 FILE* fp = winpr_fopen(filename,
"w+b");
459 const size_t w = fwrite(data, 1, size, fp);
461 status = (w == size) ? 1 : -1;
469int winpr_image_write(
wImage* image,
const char* filename)
472 return winpr_image_write_ex(image, WINPR_ASSERTING_INT_CAST(uint32_t, image->type), filename);
475int winpr_image_write_ex(
wImage* image, UINT32 format,
const char* filename)
480 void* data = winpr_image_write_buffer(image, format, &size);
483 return write_and_free(filename, data, size);
487static int winpr_image_bitmap_read_buffer(
wImage* image,
const BYTE* buffer,
size_t size)
493 wStream sbuffer = WINPR_C_ARRAY_INIT;
494 wStream* s = Stream_StaticConstInit(&sbuffer, buffer, size);
499 size_t bmpoffset = 0;
500 if (!readBitmapFileHeader(s, &bf) || !readBitmapInfoHeader(s, &bi, &bmpoffset))
503 if ((bf.bfType[0] !=
'B') || (bf.bfType[1] !=
'M'))
505 WLog_WARN(TAG,
"Invalid bitmap header %c%c", bf.bfType[0], bf.bfType[1]);
509 image->type = WINPR_IMAGE_BITMAP;
514 const size_t pos = Stream_GetPosition(s);
515 const size_t expect = bf.bfOffBits;
516 if ((pos > expect) || (bmpoffset > expect - pos))
518 WLog_WARN(TAG,
"pos=%" PRIuz
", expected %" PRIuz
", offset=%" PRIuz, pos, expect,
523 if (!Stream_SafeSeek(s, expect - pos))
527 if (!Stream_CheckAndLogRequiredLength(TAG, s, bi.biSizeImage))
532 WLog_WARN(TAG,
"bi.biWidth=%" PRId32, bi.biWidth);
536 image->width = (UINT32)bi.biWidth;
541 image->height = (UINT32)(-1 * bi.biHeight);
546 image->height = (UINT32)bi.biHeight;
549 if (image->width <= 0)
551 WLog_WARN(TAG,
"image->width=%" PRIu32, image->width);
555 if (image->height <= 0)
557 WLog_WARN(TAG,
"image->height=%" PRIu32, image->height);
561 image->bitsPerPixel = bi.biBitCount;
563 const size_t bpp = (bi.biBitCount + 7UL) / 8UL;
564 image->bytesPerPixel = WINPR_ASSERTING_INT_CAST(uint32_t, bpp);
565 if (image->bytesPerPixel == 0)
568 if ((
size_t)bi.biWidth > (SIZE_MAX / image->bytesPerPixel) - 4ull)
571 image->scanline = WINPR_ASSERTING_INT_CAST(uint32_t, bi.biWidth) * image->bytesPerPixel;
572 if ((image->scanline % 4) != 0)
573 image->scanline += 4 - image->scanline % 4;
575 if (image->height > SIZE_MAX / image->scanline)
579 const size_t bmpsize = 1ULL * image->scanline * image->height;
580 if (bmpsize != bi.biSizeImage)
581 WLog_WARN(TAG,
"bmpsize=%" PRIuz
" != bi.biSizeImage=%" PRIu32, bmpsize,
585 size_t scanline = image->scanline;
586 if (bi.biSizeImage < bmpsize)
589 const size_t uscanline = image->width * bpp;
590 const size_t unaligned = image->height * uscanline;
591 if (bi.biSizeImage != unaligned)
593 scanline = uscanline;
596 image->data =
nullptr;
598 const size_t asize = 1ULL * image->scanline * image->height;
600 image->data = (BYTE*)malloc(asize);
608 BYTE* pDstData = image->data;
610 for (
size_t index = 0; index < image->height; index++)
612 Stream_Read(s, pDstData, scanline);
613 pDstData += image->scanline;
618 BYTE* pDstData = &(image->data[(image->height - 1ull) * image->scanline]);
620 for (
size_t index = 0; index < image->height; index++)
622 Stream_Read(s, pDstData, scanline);
623 pDstData -= image->scanline;
636 image->data =
nullptr;
642int winpr_image_read(
wImage* image,
const char* filename)
646 FILE* fp = winpr_fopen(filename,
"rb");
649 WLog_ERR(TAG,
"failed to open file %s", filename);
653 (void)fseek(fp, 0, SEEK_END);
654 INT64 pos = _ftelli64(fp);
655 (void)fseek(fp, 0, SEEK_SET);
659 BYTE* buffer = malloc((
size_t)pos);
662 size_t r = fread(buffer, 1, (
size_t)pos, fp);
663 if (r == (
size_t)pos)
665 status = winpr_image_read_buffer(image, buffer, (
size_t)pos);
674int winpr_image_read_buffer(
wImage* image,
const BYTE* buffer,
size_t size)
676 BYTE sig[12] = WINPR_C_ARRAY_INIT;
679 if (size <
sizeof(sig))
682 CopyMemory(sig, buffer,
sizeof(sig));
684 if ((sig[0] ==
'B') && (sig[1] ==
'M'))
686 image->type = WINPR_IMAGE_BITMAP;
687 status = winpr_image_bitmap_read_buffer(image, buffer, size);
689 else if ((sig[0] ==
'R') && (sig[1] ==
'I') && (sig[2] ==
'F') && (sig[3] ==
'F') &&
690 (sig[8] ==
'W') && (sig[9] ==
'E') && (sig[10] ==
'B') && (sig[11] ==
'P'))
692 image->type = WINPR_IMAGE_WEBP;
694 winpr_convert_from_webp(buffer, size, &image->width, &image->height,
695 &image->bitsPerPixel, &image->data, &image->scanline);
698 image->bytesPerPixel = (image->bitsPerPixel + 7) / 8;
702 else if ((sig[0] == 0xFF) && (sig[1] == 0xD8) && (sig[2] == 0xFF) && (sig[3] == 0xE0) &&
703 (sig[6] == 0x4A) && (sig[7] == 0x46) && (sig[8] == 0x49) && (sig[9] == 0x46) &&
706 image->type = WINPR_IMAGE_JPEG;
708 winpr_convert_from_jpeg(buffer, size, &image->width, &image->height,
709 &image->bitsPerPixel, &image->data, &image->scanline);
712 image->bytesPerPixel = (image->bitsPerPixel + 7) / 8;
716 else if ((sig[0] == 0x89) && (sig[1] ==
'P') && (sig[2] ==
'N') && (sig[3] ==
'G') &&
717 (sig[4] ==
'\r') && (sig[5] ==
'\n') && (sig[6] == 0x1A) && (sig[7] ==
'\n'))
719 image->type = WINPR_IMAGE_PNG;
721 winpr_convert_from_png(buffer, size, &image->width, &image->height,
722 &image->bitsPerPixel, &image->data, &image->scanline);
725 image->bytesPerPixel = (image->bitsPerPixel + 7) / 8;
733wImage* winpr_image_new(
void)
743void winpr_image_free(
wImage* image, BOOL bFreeBuffer)
754WINPR_ATTR_MALLOC(free, 1)
755static
void* winpr_convert_to_jpeg(WINPR_ATTR_UNUSED const
void* data,
756 WINPR_ATTR_UNUSED
size_t size, UINT32 width, UINT32 height,
757 UINT32 stride, UINT32 bpp, UINT32* pSize)
759 WINPR_ASSERT(data || (size == 0));
763 if ((width == 0) || (height == 0) || (stride > SIZE_MAX / height) || (bpp < 1) || (bpp > 32))
766#if !defined(WINPR_UTILS_IMAGE_JPEG)
767 WLog_WARN(TAG,
"JPEG not supported in this build");
770 BYTE* outbuffer =
nullptr;
771 unsigned long outsize = 0;
772 struct jpeg_compress_struct cinfo = WINPR_C_ARRAY_INIT;
774 const size_t expect1 = 1ull * stride * height;
775 const size_t bytes = (bpp + 7) / 8;
776 const size_t expect2 = 1ull * width * height * bytes;
777 if (expect1 < expect2)
783 struct jpeg_error_mgr jerr = WINPR_C_ARRAY_INIT;
784 cinfo.err = jpeg_std_error(&jerr);
786 jpeg_create_compress(&cinfo);
787 jpeg_mem_dest(&cinfo, &outbuffer, &outsize);
789 cinfo.image_width = width;
790 cinfo.image_height = height;
791 WINPR_ASSERT(bpp <= INT32_MAX / 8);
792 cinfo.input_components = (int)(bpp + 7) / 8;
793 cinfo.in_color_space = (bpp > 24) ? JCS_EXT_BGRA : JCS_EXT_BGR;
794 cinfo.data_precision = 8;
796 jpeg_set_defaults(&cinfo);
797 jpeg_set_quality(&cinfo, 100, TRUE);
799 cinfo.comp_info[0].h_samp_factor = cinfo.comp_info[0].v_samp_factor = 1;
801 jpeg_start_compress(&cinfo, TRUE);
803 const JSAMPLE* cdata = data;
804 for (
size_t x = 0; x < height; x++)
806 WINPR_ASSERT(x * stride <= UINT32_MAX);
807 const JDIMENSION offset = (JDIMENSION)x * stride;
811 JSAMPLE* coffset = WINPR_CAST_CONST_PTR_AWAY(&cdata[offset], JSAMPLE*);
812 if (jpeg_write_scanlines(&cinfo, &coffset, 1) != 1)
816 WINPR_ASSERT(outsize <= UINT32_MAX);
817 *pSize = (UINT32)outsize;
820 jpeg_finish_compress(&cinfo);
821 jpeg_destroy_compress(&cinfo);
828SSIZE_T winpr_convert_from_jpeg(WINPR_ATTR_UNUSED
const BYTE* comp_data,
829 WINPR_ATTR_UNUSED
size_t comp_data_bytes,
830 WINPR_ATTR_UNUSED UINT32* width, WINPR_ATTR_UNUSED UINT32* height,
831 WINPR_ATTR_UNUSED UINT32* bpp,
832 WINPR_ATTR_UNUSED BYTE** ppdecomp_data, UINT32* scanline)
835 WINPR_ASSERT(comp_data || (comp_data_bytes == 0));
837 WINPR_ASSERT(height);
839 WINPR_ASSERT(ppdecomp_data);
840 WINPR_ASSERT(scanline);
845 *ppdecomp_data =
nullptr;
848#if !defined(WINPR_UTILS_IMAGE_JPEG)
849 WLog_WARN(TAG,
"JPEG not supported in this build");
852 struct jpeg_decompress_struct cinfo = WINPR_C_ARRAY_INIT;
853 struct jpeg_error_mgr jerr;
855 BYTE* decomp_data =
nullptr;
857 cinfo.err = jpeg_std_error(&jerr);
858 jpeg_create_decompress(&cinfo);
859 jpeg_mem_src(&cinfo, comp_data, comp_data_bytes);
861 if (jpeg_read_header(&cinfo, 1) != JPEG_HEADER_OK)
864 cinfo.out_color_space = cinfo.num_components > 3 ? JCS_EXT_RGBA : JCS_EXT_BGR;
865 const int components = MAX(3, cinfo.num_components);
867 *width = WINPR_ASSERTING_INT_CAST(uint32_t, cinfo.image_width);
868 *height = WINPR_ASSERTING_INT_CAST(uint32_t, cinfo.image_height);
869 *bpp = WINPR_ASSERTING_INT_CAST(uint32_t, cinfo.num_components * 8);
871 if (!jpeg_start_decompress(&cinfo))
874 size_t stride = 1ULL * cinfo.image_width * WINPR_ASSERTING_INT_CAST(
size_t, components);
876 if ((stride == 0) || (cinfo.image_height == 0) || (stride > UINT32_MAX))
878 *scanline = (UINT32)stride;
880 decomp_data = calloc(stride, cinfo.image_height);
883 while (cinfo.output_scanline < cinfo.image_height)
885 JSAMPROW row = &decomp_data[cinfo.output_scanline * stride];
886 if (jpeg_read_scanlines(&cinfo, &row, 1) != 1)
889 const size_t ssize = stride * cinfo.image_height;
890 WINPR_ASSERT(ssize < SSIZE_MAX);
891 size = (SSIZE_T)ssize;
893 jpeg_finish_decompress(&cinfo);
896 jpeg_destroy_decompress(&cinfo);
897 *ppdecomp_data = decomp_data;
902WINPR_ATTR_MALLOC(free, 1)
903static
void* winpr_convert_to_webp(WINPR_ATTR_UNUSED const
void* data,
904 WINPR_ATTR_UNUSED
size_t size, WINPR_ATTR_UNUSED UINT32 width,
905 WINPR_ATTR_UNUSED UINT32 height, WINPR_ATTR_UNUSED UINT32 stride,
906 WINPR_ATTR_UNUSED UINT32 bpp, UINT32* pSize)
908 WINPR_ASSERT(data || (size == 0));
913#if !defined(WINPR_UTILS_IMAGE_WEBP)
914 WLog_WARN(TAG,
"WEBP not supported in this build");
918 uint8_t* pDstData =
nullptr;
919 WINPR_ASSERT(width <= INT32_MAX);
920 WINPR_ASSERT(height <= INT32_MAX);
921 WINPR_ASSERT(stride <= INT32_MAX);
925 dstSize = WebPEncodeLosslessBGRA(data, (
int)width, (
int)height, (
int)stride, &pDstData);
928 dstSize = WebPEncodeLosslessBGR(data, (
int)width, (
int)height, (
int)stride, &pDstData);
936 rc = malloc(dstSize);
939 memcpy(rc, pDstData, dstSize);
941 WINPR_ASSERT(dstSize <= UINT32_MAX);
942 *pSize = (UINT32)dstSize;
950SSIZE_T winpr_convert_from_webp(WINPR_ATTR_UNUSED
const BYTE* comp_data,
951 WINPR_ATTR_UNUSED
size_t comp_data_bytes, UINT32* width,
952 UINT32* height, UINT32* bpp, BYTE** ppdecomp_data, UINT32* scanline)
954 WINPR_ASSERT(comp_data || (comp_data_bytes == 0));
956 WINPR_ASSERT(height);
958 WINPR_ASSERT(ppdecomp_data);
959 WINPR_ASSERT(scanline);
964 *ppdecomp_data =
nullptr;
967#if !defined(WINPR_UTILS_IMAGE_WEBP)
968 WLog_WARN(TAG,
"WEBP not supported in this build");
974 uint8_t* dst = WebPDecodeBGRA(comp_data, comp_data_bytes, &w, &h);
975 if (!dst || (w < 0) || (h < 0))
981 if ((w <= 0) || ((UINT32)w > UINT32_MAX / 4ull) || (h <= 0) ||
982 ((UINT32)h > UINT32_MAX / 4ul / (UINT32)w))
987 *width = WINPR_ASSERTING_INT_CAST(uint32_t, w);
988 *height = WINPR_ASSERTING_INT_CAST(uint32_t, h);
990 *scanline = (*width) * 4ul;
991 *ppdecomp_data = dst;
992 return 1ll * (*scanline) * h;
996#if defined(WINPR_UTILS_IMAGE_PNG)
1003static void png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
1006 struct png_mem_encode* p =
1007 (
struct png_mem_encode*)png_get_io_ptr(png_ptr);
1008 size_t nsize = p->size + length;
1013 char* tmp = realloc(p->buffer, nsize);
1018 p->buffer = malloc(nsize);
1021 png_error(png_ptr,
"Write Error");
1024 memcpy(p->buffer + p->size, data, length);
1029static void png_flush(WINPR_ATTR_UNUSED png_structp png_ptr)
1034static SSIZE_T save_png_to_buffer(UINT32 bpp, UINT32 width, uint32_t stride, UINT32 height,
1035 const uint8_t* data,
size_t size,
void** pDstData)
1038 png_structp png_ptr =
nullptr;
1039 png_infop info_ptr =
nullptr;
1040 png_byte** row_pointers =
nullptr;
1041 struct png_mem_encode state = WINPR_C_ARRAY_INIT;
1043 *pDstData =
nullptr;
1045 if (!data || (size == 0))
1048 WINPR_ASSERT(pDstData);
1050 if (size < (1ULL * stride * height))
1054 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
nullptr,
nullptr,
nullptr);
1055 if (png_ptr ==
nullptr)
1059 info_ptr = png_create_info_struct(png_ptr);
1060 if (info_ptr ==
nullptr)
1064 if (setjmp(png_jmpbuf(png_ptr)))
1068 int colorType = PNG_COLOR_TYPE_PALETTE;
1070 colorType = PNG_COLOR_TYPE_RGB;
1072 colorType = PNG_COLOR_TYPE_RGB;
1074 colorType = PNG_COLOR_TYPE_RGBA;
1076 png_set_IHDR(png_ptr, info_ptr, width, height, 8, colorType, PNG_INTERLACE_NONE,
1077 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1080 row_pointers = (png_byte**)png_malloc(png_ptr, height *
sizeof(png_byte*));
1081 for (
size_t y = 0; y < height; ++y)
1083 const uint8_t* line = &data[y * stride];
1084 uint8_t* row = png_malloc(png_ptr,
sizeof(uint8_t) * stride);
1085 row_pointers[y] = (png_byte*)row;
1086 for (
size_t x = 0; x < width; ++x)
1100 png_set_write_fn(png_ptr, &state, png_write_data, png_flush);
1101 png_set_rows(png_ptr, info_ptr, row_pointers);
1102 png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_BGR,
nullptr);
1105 for (
size_t y = 0; y < height; y++)
1106 png_free(png_ptr, row_pointers[y]);
1107 png_free(png_ptr, (
void*)row_pointers);
1110 if (state.size > SSIZE_MAX)
1112 rc = (SSIZE_T)state.size;
1113 *pDstData = state.buffer;
1115 png_destroy_write_struct(&png_ptr, &info_ptr);
1124 png_uint_32 bufsize;
1125 png_uint_32 current_pos;
1126} MEMORY_READER_STATE;
1128static void read_data_memory(png_structp png_ptr, png_bytep data,
size_t length)
1130 MEMORY_READER_STATE* f = png_get_io_ptr(png_ptr);
1131 if (length > (f->bufsize - f->current_pos))
1132 png_error(png_ptr,
"read error in read_data_memory (loadpng)");
1135 memcpy(data, f->buffer + f->current_pos, length);
1136 f->current_pos += length;
1140WINPR_ATTR_MALLOC(free, 1)
1141static
void* winpr_read_png_from_buffer(const
void* data,
size_t SrcSize,
size_t* pSize,
1142 UINT32* pWidth, UINT32* pHeight, UINT32* pBpp,
1146 png_uint_32 width = 0;
1147 png_uint_32 height = 0;
1150 int interlace_type = 0;
1151 int transforms = PNG_TRANSFORM_IDENTITY;
1152 MEMORY_READER_STATE memory_reader_state = WINPR_C_ARRAY_INIT;
1153 png_bytepp row_pointers =
nullptr;
1154 png_infop info_ptr =
nullptr;
1162 if (SrcSize > UINT32_MAX)
1165 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
nullptr,
nullptr,
nullptr);
1168 info_ptr = png_create_info_struct(png_ptr);
1172 memory_reader_state.buffer = WINPR_CAST_CONST_PTR_AWAY(data, png_bytep);
1173 memory_reader_state.bufsize = (UINT32)SrcSize;
1174 memory_reader_state.current_pos = 0;
1176 png_set_read_fn(png_ptr, &memory_reader_state, read_data_memory);
1178 transforms |= PNG_TRANSFORM_BGR;
1179 png_read_png(png_ptr, info_ptr, transforms,
nullptr);
1181 if (png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type,
1182 nullptr,
nullptr) != 1)
1185 WINPR_ASSERT(bit_depth >= 0);
1186 const png_byte channelcount = png_get_channels(png_ptr, info_ptr);
1187 const size_t bpp = channelcount * (size_t)bit_depth;
1189 row_pointers = png_get_rows(png_ptr, info_ptr);
1192 if ((width == 0) || (width > SIZE_MAX / bpp))
1195 const size_t png_stride = png_get_rowbytes(png_ptr, info_ptr);
1196 if ((png_stride == 0) || (height == 0) || (png_stride > SIZE_MAX / height))
1198 const size_t size = 1ULL * png_stride * height;
1200 rc = calloc(png_stride, height);
1204 for (png_uint_32 i = 0; i < height; i++)
1206 memcpy(cur, row_pointers[i], png_stride);
1212 WINPR_ASSERT(bpp <= UINT32_MAX);
1213 *pBpp = (UINT32)bpp;
1214 *pScanline = (UINT32)png_stride;
1219 png_destroy_read_struct(&png_ptr, &info_ptr,
nullptr);
1224WINPR_ATTR_MALLOC(free, 1)
1225static
void* winpr_convert_to_png(WINPR_ATTR_UNUSED const
void* data, WINPR_ATTR_UNUSED
size_t size,
1226 UINT32 width, UINT32 height, UINT32 stride, UINT32 bpp,
1229 WINPR_ASSERT(data || (size == 0));
1230 WINPR_ASSERT(pSize);
1234 if ((width == 0) || (height == 0) || (stride == 0) || (bpp == 0))
1236 if (height > UINT32_MAX / stride)
1239#if defined(WINPR_UTILS_IMAGE_PNG)
1240 void* dst =
nullptr;
1241 SSIZE_T rc = save_png_to_buffer(bpp, width, stride, height, data, size, &dst);
1244 *pSize = (UINT32)rc;
1246#elif defined(WITH_LODEPNG)
1248 BYTE* dst =
nullptr;
1255 rc = lodepng_encode32(&dst, &dstsize, data, width, height);
1258 rc = lodepng_encode24(&dst, &dstsize, data, width, height);
1265 *pSize = (UINT32)dstsize;
1269 WLog_WARN(TAG,
"PNG not supported in this build");
1274SSIZE_T winpr_convert_from_png(WINPR_ATTR_UNUSED
const BYTE* comp_data,
1275 WINPR_ATTR_UNUSED
size_t comp_data_bytes, UINT32* width,
1276 UINT32* height, UINT32* bpp, BYTE** ppdecomp_data, UINT32* scanline)
1278 WINPR_ASSERT(comp_data || (comp_data_bytes == 0));
1279 WINPR_ASSERT(width);
1280 WINPR_ASSERT(height);
1282 WINPR_ASSERT(ppdecomp_data);
1283 WINPR_ASSERT(scanline);
1288 *ppdecomp_data =
nullptr;
1291#if defined(WINPR_UTILS_IMAGE_PNG)
1294 winpr_read_png_from_buffer(comp_data, comp_data_bytes, &len, width, height, bpp, scanline);
1295 if (!*ppdecomp_data)
1297 return (SSIZE_T)len;
1298#elif defined(WITH_LODEPNG)
1300 if (*width > UINT32_MAX / 4)
1304 lodepng_decode32((
unsigned char**)ppdecomp_data, width, height, comp_data, comp_data_bytes);
1305 *scanline = *width * 4;
1308 WLog_WARN(TAG,
"PNG not supported in this build");
1313BOOL winpr_image_format_is_supported(UINT32 format)
1317 case WINPR_IMAGE_BITMAP:
1318#if defined(WINPR_UTILS_IMAGE_PNG) || defined(WITH_LODEPNG)
1319 case WINPR_IMAGE_PNG:
1321#if defined(WINPR_UTILS_IMAGE_JPEG)
1322 case WINPR_IMAGE_JPEG:
1324#if defined(WINPR_UTILS_IMAGE_WEBP)
1325 case WINPR_IMAGE_WEBP:
1333WINPR_ATTR_MALLOC(free, 1)
1334static BYTE* convert(const
wImage* image,
size_t* pstride, WINPR_ATTR_UNUSED UINT32 flags)
1336 WINPR_ASSERT(image);
1337 WINPR_ASSERT(pstride);
1340 if (image->bitsPerPixel < 24)
1343 const size_t stride = image->width * 4ull;
1344 BYTE* data = calloc(stride, image->height);
1347 for (
size_t y = 0; y < image->height; y++)
1349 const BYTE* srcLine = &image->data[image->scanline * y];
1350 BYTE* dstLine = &data[stride * y];
1351 if (image->bitsPerPixel == 32)
1352 memcpy(dstLine, srcLine, stride);
1355 for (
size_t x = 0; x < image->width; x++)
1357 const BYTE* src = &srcLine[image->bytesPerPixel * x];
1358 BYTE* dst = &dstLine[4ull * x];
1376static BOOL compare_byte_relaxed(BYTE a, BYTE b, UINT32 flags)
1380 if ((flags & WINPR_IMAGE_CMP_FUZZY) != 0)
1382 const int diff = abs((
int)a) - abs((
int)b);
1396static BOOL compare_pixel(
const BYTE* pa,
const BYTE* pb, UINT32 flags)
1401 if (!compare_byte_relaxed(*pa++, *pb++, flags))
1403 if (!compare_byte_relaxed(*pa++, *pb++, flags))
1405 if (!compare_byte_relaxed(*pa++, *pb++, flags))
1407 if ((flags & WINPR_IMAGE_CMP_IGNORE_ALPHA) == 0)
1409 if (!compare_byte_relaxed(*pa++, *pb++, flags))
1415BOOL winpr_image_equal(
const wImage* imageA,
const wImage* imageB, UINT32 flags)
1417 if (imageA == imageB)
1419 if (!imageA || !imageB)
1422 if (imageA->height != imageB->height)
1424 if (imageA->width != imageB->width)
1427 if ((flags & WINPR_IMAGE_CMP_IGNORE_DEPTH) == 0)
1429 if (imageA->bitsPerPixel != imageB->bitsPerPixel)
1431 if (imageA->bytesPerPixel != imageB->bytesPerPixel)
1438 BYTE* dataA = convert(imageA, &astride, flags);
1439 BYTE* dataB = convert(imageA, &bstride, flags);
1440 if (dataA && dataB && (astride == bstride))
1443 for (
size_t y = 0; y < imageA->height; y++)
1445 const BYTE* lineA = &dataA[astride * y];
1446 const BYTE* lineB = &dataB[bstride * y];
1448 for (
size_t x = 0; x < imageA->width; x++)
1450 const BYTE* pa = &lineA[x * 4ull];
1451 const BYTE* pb = &lineB[x * 4ull];
1453 if (!compare_pixel(pa, pb, flags))
1463const char* winpr_image_format_mime(UINT32 format)
1467 case WINPR_IMAGE_BITMAP:
1469 case WINPR_IMAGE_PNG:
1471 case WINPR_IMAGE_WEBP:
1472 return "image/webp";
1473 case WINPR_IMAGE_JPEG:
1474 return "image/jpeg";
1480const char* winpr_image_format_extension(UINT32 format)
1484 case WINPR_IMAGE_BITMAP:
1486 case WINPR_IMAGE_PNG:
1488 case WINPR_IMAGE_WEBP:
1490 case WINPR_IMAGE_JPEG:
1497void* winpr_image_write_buffer(
wImage* image, UINT32 format,
size_t* psize)
1499 WINPR_ASSERT(image);
1502 case WINPR_IMAGE_BITMAP:
1505 size_t size = 1ull * image->height * image->scanline;
1506 void* data = winpr_bitmap_write_buffer(image->data, size, image->width, image->height,
1507 image->scanline, image->bitsPerPixel, &outsize);
1511 case WINPR_IMAGE_WEBP:
1514 size_t size = 1ull * image->height * image->scanline;
1515 void* data = winpr_convert_to_webp(image->data, size, image->width, image->height,
1516 image->scanline, image->bitsPerPixel, &outsize);
1520 case WINPR_IMAGE_JPEG:
1523 size_t size = 1ull * image->height * image->scanline;
1524 void* data = winpr_convert_to_jpeg(image->data, size, image->width, image->height,
1525 image->scanline, image->bitsPerPixel, &outsize);
1529 case WINPR_IMAGE_PNG:
1532 size_t size = 1ull * image->height * image->scanline;
1533 void* data = winpr_convert_to_png(image->data, size, image->width, image->height,
1534 image->scanline, image->bitsPerPixel, &outsize);