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);
243WINPR_ATTR_MALLOC(Stream_Free, 1)
244static
wStream* winpr_bitmap_construct_header_stream(
size_t width,
size_t height,
size_t bpp)
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;
288 offset +=
sizeof(
RGBQUAD) * used;
290 if (bi.biSizeImage == 0)
292 const UINT64 rawustride =
293 WINPR_ASSERTING_INT_CAST(UINT64, bi.biWidth) * bi.biBitCount;
294 const UINT64 ustride = ((rawustride + 31) & ~31u) >> 3;
295 if (ustride > UINT32_MAX)
297 WLog_ERR(TAG,
"bi->biWidth * bi->biBitCount > UINT32_MAX");
301 const UINT32 xstride = WINPR_ASSERTING_INT_CAST(uint32_t, ustride);
302 const UINT64 usize = WINPR_ASSERTING_INT_CAST(UINT64, llabs(bi.biHeight)) * xstride;
303 if (usize > UINT32_MAX)
305 WLog_ERR(TAG,
"abs(bi->biHeight) * stride > UINT32_MAX");
308 bi.biSizeImage = WINPR_ASSERTING_INT_CAST(uint32_t, usize);
312 offset +=
sizeof(DWORD) * 3;
318 bf.bfOffBits += offset;
320 if (!writeBitmapFileHeader(s, &bf))
323 if (!writeBitmapInfoHeader(s, &bi))
326 if (!Stream_EnsureRemainingCapacity(s, offset))
329 Stream_Zero(s, offset);
330 const size_t pos = Stream_GetPosition(s);
331 if (pos != bf.bfOffBits)
335 Stream_Free(s, TRUE);
339BYTE* winpr_bitmap_construct_header(
size_t width,
size_t height,
size_t bpp)
341 wStream* s = winpr_bitmap_construct_header_stream(width, height, bpp);
344 void* data = Stream_Buffer(s);
345 Stream_Free(s, FALSE);
353WINPR_ATTR_MALLOC(free, 1)
354static
void* winpr_bitmap_write_buffer(const BYTE* data,
size_t size, UINT32 width, UINT32 height,
355 UINT32 stride, UINT32 bpp, UINT32* pSize)
357 WINPR_ASSERT(data || (size == 0));
360 void* result =
nullptr;
361 if ((height == 0) || (width == 0) || (bpp == 0) || (width > SIZE_MAX / height) ||
362 (width > SIZE_MAX / bpp))
365 size_t bpp_stride = 1ull * width * bpp;
368 if ((bpp_stride % 8) != 0)
369 bpp_stride += 8 - (bpp_stride % 8);
373 if ((bpp_stride % 4) != 0)
374 bpp_stride += 4 - (bpp_stride % 4);
376 if ((bpp_stride < 4) || (bpp_stride > UINT32_MAX))
380 stride = (UINT32)bpp_stride;
382 if (stride > SIZE_MAX / height)
385 if (stride > size / height)
388 if (bpp_stride > stride)
391 wStream* s = winpr_bitmap_construct_header_stream(width, height, bpp);
395 for (
size_t y = 0; y < height; y++)
397 if (!Stream_EnsureRemainingCapacity(s, stride))
400 size_t offset = y * stride;
401 if (offset + stride <= size)
403 const BYTE* line = &data[offset];
405 Stream_Write(s, line, stride);
408 Stream_Zero(s, stride);
411 result = Stream_Buffer(s);
413 const size_t pos = Stream_GetPosition(s);
414 if (pos > UINT32_MAX)
416 *pSize = (UINT32)pos;
419 Stream_Free(s, result ==
nullptr);
423int winpr_bitmap_write(
const char* filename,
const BYTE* data,
size_t width,
size_t height,
426 return winpr_bitmap_write_ex(filename, data, 0, width, height, bpp);
429int winpr_bitmap_write_ex(
const char* filename,
const BYTE* data,
size_t stride,
size_t width,
430 size_t height,
size_t bpp)
434 void* bmpdata =
nullptr;
435 const size_t bpp_stride = ((((width * bpp) + 31) & (size_t)~31) >> 3);
437 if ((stride > UINT32_MAX) || (width > UINT32_MAX) || (height > UINT32_MAX) ||
447 const size_t size = stride * 1ull * height;
448 bmpdata = winpr_bitmap_write_buffer(data, size, (UINT32)width, (UINT32)height,
449 (UINT32)stride, (UINT32)bpp, &bmpsize);
454 fp = winpr_fopen(filename,
"w+b");
457 WLog_ERR(TAG,
"failed to open file %s", filename);
461 if (fwrite(bmpdata, bmpsize, 1, fp) != 1)
474static int write_and_free(
const char* filename,
void* data,
size_t size)
481 FILE* fp = winpr_fopen(filename,
"w+b");
486 const size_t w = fwrite(data, 1, size, fp);
488 status = (w == size) ? 1 : -1;
496int winpr_image_write(
wImage* image,
const char* filename)
499 return winpr_image_write_ex(image, WINPR_ASSERTING_INT_CAST(uint32_t, image->type), filename);
502int winpr_image_write_ex(
wImage* image, UINT32 format,
const char* filename)
507 void* data = winpr_image_write_buffer(image, format, &size);
510 return write_and_free(filename, data, size);
514static int winpr_image_bitmap_read_buffer(
wImage* image,
const BYTE* buffer,
size_t size)
520 wStream sbuffer = WINPR_C_ARRAY_INIT;
521 wStream* s = Stream_StaticConstInit(&sbuffer, buffer, size);
526 size_t bmpoffset = 0;
527 if (!readBitmapFileHeader(s, &bf) || !readBitmapInfoHeader(s, &bi, &bmpoffset))
530 if ((bf.bfType[0] !=
'B') || (bf.bfType[1] !=
'M'))
532 WLog_WARN(TAG,
"Invalid bitmap header %c%c", bf.bfType[0], bf.bfType[1]);
536 image->type = WINPR_IMAGE_BITMAP;
541 const size_t pos = Stream_GetPosition(s);
542 const size_t expect = bf.bfOffBits;
543 if ((pos > expect) || (bmpoffset > expect - pos))
545 WLog_WARN(TAG,
"pos=%" PRIuz
", expected %" PRIuz
", offset=%" PRIuz, pos, expect,
550 if (!Stream_SafeSeek(s, expect - pos))
554 if (!Stream_CheckAndLogRequiredLength(TAG, s, bi.biSizeImage))
559 WLog_WARN(TAG,
"bi.biWidth=%" PRId32, bi.biWidth);
563 image->width = (UINT32)bi.biWidth;
568 image->height = (UINT32)(-1 * bi.biHeight);
573 image->height = (UINT32)bi.biHeight;
576 if (image->width <= 0)
578 WLog_WARN(TAG,
"image->width=%" PRIu32, image->width);
582 if (image->height <= 0)
584 WLog_WARN(TAG,
"image->height=%" PRIu32, image->height);
588 image->bitsPerPixel = bi.biBitCount;
590 const size_t bpp = (bi.biBitCount + 7UL) / 8UL;
591 image->bytesPerPixel = WINPR_ASSERTING_INT_CAST(uint32_t, bpp);
592 if (image->bytesPerPixel == 0)
595 if (image->width > (UINT32_MAX / image->bytesPerPixel) - 4ull)
598 image->scanline = image->width * image->bytesPerPixel;
599 if ((image->scanline % 4) != 0)
600 image->scanline += 4 - image->scanline % 4;
602 if (image->height > UINT32_MAX / image->scanline)
606 const size_t bmpsize = 1ULL * image->scanline * image->height;
607 if (bmpsize != bi.biSizeImage)
608 WLog_WARN(TAG,
"bmpsize=%" PRIuz
" != bi.biSizeImage=%" PRIu32, bmpsize,
612 size_t scanline = image->scanline;
613 if (bi.biSizeImage < bmpsize)
616 const size_t uscanline = image->width * bpp;
617 const size_t unaligned = image->height * uscanline;
618 if (bi.biSizeImage != unaligned)
620 scanline = uscanline;
623 image->data =
nullptr;
625 const size_t asize = 1ULL * image->scanline * image->height;
627 image->data = (BYTE*)malloc(asize);
635 BYTE* pDstData = image->data;
637 for (
size_t index = 0; index < image->height; index++)
639 Stream_Read(s, pDstData, scanline);
640 pDstData += image->scanline;
645 BYTE* pDstData = &(image->data[(image->height - 1ull) * image->scanline]);
647 for (
size_t index = 0; index < image->height; index++)
649 Stream_Read(s, pDstData, scanline);
650 pDstData -= image->scanline;
663 image->data =
nullptr;
669int winpr_image_read(
wImage* image,
const char* filename)
673 FILE* fp = winpr_fopen(filename,
"rb");
676 WLog_ERR(TAG,
"failed to open file %s", filename);
680 (void)fseek(fp, 0, SEEK_END);
681 INT64 pos = _ftelli64(fp);
682 (void)fseek(fp, 0, SEEK_SET);
686 BYTE* buffer = malloc((
size_t)pos);
689 size_t r = fread(buffer, 1, (
size_t)pos, fp);
690 if (r == (
size_t)pos)
692 status = winpr_image_read_buffer(image, buffer, (
size_t)pos);
701int winpr_image_read_buffer(
wImage* image,
const BYTE* buffer,
size_t size)
703 BYTE sig[12] = WINPR_C_ARRAY_INIT;
706 if (size <
sizeof(sig))
709 CopyMemory(sig, buffer,
sizeof(sig));
711 if ((sig[0] ==
'B') && (sig[1] ==
'M'))
713 image->type = WINPR_IMAGE_BITMAP;
714 status = winpr_image_bitmap_read_buffer(image, buffer, size);
716 else if ((sig[0] ==
'R') && (sig[1] ==
'I') && (sig[2] ==
'F') && (sig[3] ==
'F') &&
717 (sig[8] ==
'W') && (sig[9] ==
'E') && (sig[10] ==
'B') && (sig[11] ==
'P'))
719 image->type = WINPR_IMAGE_WEBP;
721 winpr_convert_from_webp(buffer, size, &image->width, &image->height,
722 &image->bitsPerPixel, &image->data, &image->scanline);
725 image->bytesPerPixel = (image->bitsPerPixel + 7) / 8;
729 else if ((sig[0] == 0xFF) && (sig[1] == 0xD8) && (sig[2] == 0xFF) && (sig[3] == 0xE0) &&
730 (sig[6] == 0x4A) && (sig[7] == 0x46) && (sig[8] == 0x49) && (sig[9] == 0x46) &&
733 image->type = WINPR_IMAGE_JPEG;
735 winpr_convert_from_jpeg(buffer, size, &image->width, &image->height,
736 &image->bitsPerPixel, &image->data, &image->scanline);
739 image->bytesPerPixel = (image->bitsPerPixel + 7) / 8;
743 else if ((sig[0] == 0x89) && (sig[1] ==
'P') && (sig[2] ==
'N') && (sig[3] ==
'G') &&
744 (sig[4] ==
'\r') && (sig[5] ==
'\n') && (sig[6] == 0x1A) && (sig[7] ==
'\n'))
746 image->type = WINPR_IMAGE_PNG;
748 winpr_convert_from_png(buffer, size, &image->width, &image->height,
749 &image->bitsPerPixel, &image->data, &image->scanline);
752 image->bytesPerPixel = (image->bitsPerPixel + 7) / 8;
760wImage* winpr_image_new(
void)
770void winpr_image_free(
wImage* image, BOOL bFreeBuffer)
781WINPR_ATTR_MALLOC(free, 1)
782static
void* winpr_convert_to_jpeg(WINPR_ATTR_UNUSED const
void* data,
783 WINPR_ATTR_UNUSED
size_t size, UINT32 width, UINT32 height,
784 UINT32 stride, UINT32 bpp, UINT32* pSize)
786 WINPR_ASSERT(data || (size == 0));
790 if ((width == 0) || (height == 0) || (stride > SIZE_MAX / height) || (bpp < 1) || (bpp > 32))
793#if !defined(WINPR_UTILS_IMAGE_JPEG)
794 WLog_WARN(TAG,
"JPEG not supported in this build");
797 BYTE* outbuffer =
nullptr;
798 unsigned long outsize = 0;
799 struct jpeg_compress_struct cinfo = WINPR_C_ARRAY_INIT;
801 const size_t expect1 = 1ull * stride * height;
802 const size_t bytes = (bpp + 7) / 8;
803 const size_t expect2 = 1ull * width * height * bytes;
804 if (expect1 < expect2)
810 struct jpeg_error_mgr jerr = WINPR_C_ARRAY_INIT;
811 cinfo.err = jpeg_std_error(&jerr);
813 jpeg_create_compress(&cinfo);
814 jpeg_mem_dest(&cinfo, &outbuffer, &outsize);
816 cinfo.image_width = width;
817 cinfo.image_height = height;
818 WINPR_ASSERT(bpp <= INT32_MAX / 8);
819 cinfo.input_components = (int)(bpp + 7) / 8;
820 cinfo.in_color_space = (bpp > 24) ? JCS_EXT_BGRA : JCS_EXT_BGR;
821 cinfo.data_precision = 8;
823 jpeg_set_defaults(&cinfo);
824 jpeg_set_quality(&cinfo, 100, TRUE);
826 cinfo.comp_info[0].h_samp_factor = cinfo.comp_info[0].v_samp_factor = 1;
828 jpeg_start_compress(&cinfo, TRUE);
830 const JSAMPLE* cdata = data;
831 for (
size_t x = 0; x < height; x++)
833 WINPR_ASSERT(x * stride <= UINT32_MAX);
834 const JDIMENSION offset = (JDIMENSION)x * stride;
838 JSAMPLE* coffset = WINPR_CAST_CONST_PTR_AWAY(&cdata[offset], JSAMPLE*);
839 if (jpeg_write_scanlines(&cinfo, &coffset, 1) != 1)
843 WINPR_ASSERT(outsize <= UINT32_MAX);
844 *pSize = (UINT32)outsize;
847 jpeg_finish_compress(&cinfo);
848 jpeg_destroy_compress(&cinfo);
855SSIZE_T winpr_convert_from_jpeg(WINPR_ATTR_UNUSED
const BYTE* comp_data,
856 WINPR_ATTR_UNUSED
size_t comp_data_bytes,
857 WINPR_ATTR_UNUSED UINT32* width, WINPR_ATTR_UNUSED UINT32* height,
858 WINPR_ATTR_UNUSED UINT32* bpp,
859 WINPR_ATTR_UNUSED BYTE** ppdecomp_data, UINT32* scanline)
862 WINPR_ASSERT(comp_data || (comp_data_bytes == 0));
864 WINPR_ASSERT(height);
866 WINPR_ASSERT(ppdecomp_data);
867 WINPR_ASSERT(scanline);
872 *ppdecomp_data =
nullptr;
875#if !defined(WINPR_UTILS_IMAGE_JPEG)
876 WLog_WARN(TAG,
"JPEG not supported in this build");
879 struct jpeg_decompress_struct cinfo = WINPR_C_ARRAY_INIT;
880 struct jpeg_error_mgr jerr;
882 BYTE* decomp_data =
nullptr;
884 cinfo.err = jpeg_std_error(&jerr);
885 jpeg_create_decompress(&cinfo);
886 jpeg_mem_src(&cinfo, comp_data, comp_data_bytes);
888 if (jpeg_read_header(&cinfo, 1) != JPEG_HEADER_OK)
891 cinfo.out_color_space = cinfo.num_components > 3 ? JCS_EXT_RGBA : JCS_EXT_BGR;
892 const int components = MAX(3, cinfo.num_components);
894 *width = WINPR_ASSERTING_INT_CAST(uint32_t, cinfo.image_width);
895 *height = WINPR_ASSERTING_INT_CAST(uint32_t, cinfo.image_height);
896 *bpp = WINPR_ASSERTING_INT_CAST(uint32_t, cinfo.num_components * 8);
898 if (!jpeg_start_decompress(&cinfo))
901 size_t stride = 1ULL * cinfo.image_width * WINPR_ASSERTING_INT_CAST(
size_t, components);
903 if ((stride == 0) || (cinfo.image_height == 0) || (stride > UINT32_MAX))
905 *scanline = (UINT32)stride;
907 decomp_data = calloc(stride, cinfo.image_height);
910 while (cinfo.output_scanline < cinfo.image_height)
912 JSAMPROW row = &decomp_data[cinfo.output_scanline * stride];
913 if (jpeg_read_scanlines(&cinfo, &row, 1) != 1)
916 const size_t ssize = stride * cinfo.image_height;
917 WINPR_ASSERT(ssize < SSIZE_MAX);
918 size = (SSIZE_T)ssize;
920 jpeg_finish_decompress(&cinfo);
923 jpeg_destroy_decompress(&cinfo);
924 *ppdecomp_data = decomp_data;
929WINPR_ATTR_MALLOC(free, 1)
930static
void* winpr_convert_to_webp(WINPR_ATTR_UNUSED const
void* data,
931 WINPR_ATTR_UNUSED
size_t size, WINPR_ATTR_UNUSED UINT32 width,
932 WINPR_ATTR_UNUSED UINT32 height, WINPR_ATTR_UNUSED UINT32 stride,
933 WINPR_ATTR_UNUSED UINT32 bpp, UINT32* pSize)
935 WINPR_ASSERT(data || (size == 0));
940#if !defined(WINPR_UTILS_IMAGE_WEBP)
941 WLog_WARN(TAG,
"WEBP not supported in this build");
945 uint8_t* pDstData =
nullptr;
946 WINPR_ASSERT(width <= INT32_MAX);
947 WINPR_ASSERT(height <= INT32_MAX);
948 WINPR_ASSERT(stride <= INT32_MAX);
952 dstSize = WebPEncodeLosslessBGRA(data, (
int)width, (
int)height, (
int)stride, &pDstData);
955 dstSize = WebPEncodeLosslessBGR(data, (
int)width, (
int)height, (
int)stride, &pDstData);
963 rc = malloc(dstSize);
966 memcpy(rc, pDstData, dstSize);
968 WINPR_ASSERT(dstSize <= UINT32_MAX);
969 *pSize = (UINT32)dstSize;
977SSIZE_T winpr_convert_from_webp(WINPR_ATTR_UNUSED
const BYTE* comp_data,
978 WINPR_ATTR_UNUSED
size_t comp_data_bytes, UINT32* width,
979 UINT32* height, UINT32* bpp, BYTE** ppdecomp_data, UINT32* scanline)
981 WINPR_ASSERT(comp_data || (comp_data_bytes == 0));
983 WINPR_ASSERT(height);
985 WINPR_ASSERT(ppdecomp_data);
986 WINPR_ASSERT(scanline);
991 *ppdecomp_data =
nullptr;
994#if !defined(WINPR_UTILS_IMAGE_WEBP)
995 WLog_WARN(TAG,
"WEBP not supported in this build");
1001 uint8_t* dst = WebPDecodeBGRA(comp_data, comp_data_bytes, &w, &h);
1002 if (!dst || (w < 0) || (h < 0))
1008 if ((w <= 0) || ((UINT32)w > UINT32_MAX / 4ull) || (h <= 0) ||
1009 ((UINT32)h > UINT32_MAX / 4ul / (UINT32)w))
1014 *width = WINPR_ASSERTING_INT_CAST(uint32_t, w);
1015 *height = WINPR_ASSERTING_INT_CAST(uint32_t, h);
1017 *scanline = (*width) * 4ul;
1018 *ppdecomp_data = dst;
1019 return 1ll * (*scanline) * h;
1023#if defined(WINPR_UTILS_IMAGE_PNG)
1024struct png_mem_encode
1030static void png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
1033 struct png_mem_encode* p =
1034 (
struct png_mem_encode*)png_get_io_ptr(png_ptr);
1035 size_t nsize = p->size + length;
1040 char* tmp = realloc(p->buffer, nsize);
1045 p->buffer = malloc(nsize);
1048 png_error(png_ptr,
"Write Error");
1051 memcpy(p->buffer + p->size, data, length);
1056static void png_flush(WINPR_ATTR_UNUSED png_structp png_ptr)
1061static SSIZE_T save_png_to_buffer(UINT32 bpp, UINT32 width, uint32_t stride, UINT32 height,
1062 const uint8_t* data,
size_t size,
void** pDstData)
1065 png_structp png_ptr =
nullptr;
1066 png_infop info_ptr =
nullptr;
1067 png_byte** row_pointers =
nullptr;
1068 struct png_mem_encode state = WINPR_C_ARRAY_INIT;
1070 *pDstData =
nullptr;
1072 if (!data || (size == 0))
1075 WINPR_ASSERT(pDstData);
1077 if (size < (1ULL * stride * height))
1081 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
nullptr,
nullptr,
nullptr);
1082 if (png_ptr ==
nullptr)
1086 info_ptr = png_create_info_struct(png_ptr);
1087 if (info_ptr ==
nullptr)
1091 if (setjmp(png_jmpbuf(png_ptr)))
1095 int colorType = PNG_COLOR_TYPE_PALETTE;
1097 colorType = PNG_COLOR_TYPE_RGB;
1099 colorType = PNG_COLOR_TYPE_RGB;
1101 colorType = PNG_COLOR_TYPE_RGBA;
1103 png_set_IHDR(png_ptr, info_ptr, width, height, 8, colorType, PNG_INTERLACE_NONE,
1104 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1107 row_pointers = (png_byte**)png_malloc(png_ptr, height *
sizeof(png_byte*));
1108 for (
size_t y = 0; y < height; ++y)
1110 const uint8_t* line = &data[y * stride];
1111 uint8_t* row = png_malloc(png_ptr,
sizeof(uint8_t) * stride);
1112 row_pointers[y] = (png_byte*)row;
1113 for (
size_t x = 0; x < width; ++x)
1127 png_set_write_fn(png_ptr, &state, png_write_data, png_flush);
1128 png_set_rows(png_ptr, info_ptr, row_pointers);
1129 png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_BGR,
nullptr);
1132 for (
size_t y = 0; y < height; y++)
1133 png_free(png_ptr, row_pointers[y]);
1134 png_free(png_ptr, (
void*)row_pointers);
1137 if (state.size > SSIZE_MAX)
1139 rc = (SSIZE_T)state.size;
1140 *pDstData = state.buffer;
1142 png_destroy_write_struct(&png_ptr, &info_ptr);
1151 png_uint_32 bufsize;
1152 png_uint_32 current_pos;
1153} MEMORY_READER_STATE;
1155static void read_data_memory(png_structp png_ptr, png_bytep data,
size_t length)
1157 MEMORY_READER_STATE* f = png_get_io_ptr(png_ptr);
1158 if (length > (f->bufsize - f->current_pos))
1159 png_error(png_ptr,
"read error in read_data_memory (loadpng)");
1162 memcpy(data, f->buffer + f->current_pos, length);
1163 f->current_pos += length;
1167WINPR_ATTR_MALLOC(free, 1)
1168static
void* winpr_read_png_from_buffer(const
void* data,
size_t SrcSize,
size_t* pSize,
1169 UINT32* pWidth, UINT32* pHeight, UINT32* pBpp,
1173 png_uint_32 width = 0;
1174 png_uint_32 height = 0;
1177 int interlace_type = 0;
1178 int transforms = PNG_TRANSFORM_IDENTITY;
1179 MEMORY_READER_STATE memory_reader_state = WINPR_C_ARRAY_INIT;
1180 png_bytepp row_pointers =
nullptr;
1181 png_infop info_ptr =
nullptr;
1189 if (SrcSize > UINT32_MAX)
1192 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
nullptr,
nullptr,
nullptr);
1195 info_ptr = png_create_info_struct(png_ptr);
1199 memory_reader_state.buffer = WINPR_CAST_CONST_PTR_AWAY(data, png_bytep);
1200 memory_reader_state.bufsize = (UINT32)SrcSize;
1201 memory_reader_state.current_pos = 0;
1203 png_set_read_fn(png_ptr, &memory_reader_state, read_data_memory);
1205 transforms |= PNG_TRANSFORM_BGR;
1206 png_read_png(png_ptr, info_ptr, transforms,
nullptr);
1208 if (png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type,
1209 nullptr,
nullptr) != 1)
1212 WINPR_ASSERT(bit_depth >= 0);
1213 const png_byte channelcount = png_get_channels(png_ptr, info_ptr);
1214 const size_t bpp = channelcount * (size_t)bit_depth;
1216 row_pointers = png_get_rows(png_ptr, info_ptr);
1219 if ((width == 0) || (width > SIZE_MAX / bpp))
1222 const size_t png_stride = png_get_rowbytes(png_ptr, info_ptr);
1223 if ((png_stride == 0) || (height == 0) || (png_stride > SIZE_MAX / height))
1225 const size_t size = 1ULL * png_stride * height;
1227 rc = calloc(png_stride, height);
1231 for (png_uint_32 i = 0; i < height; i++)
1233 memcpy(cur, row_pointers[i], png_stride);
1239 WINPR_ASSERT(bpp <= UINT32_MAX);
1240 *pBpp = (UINT32)bpp;
1241 *pScanline = (UINT32)png_stride;
1246 png_destroy_read_struct(&png_ptr, &info_ptr,
nullptr);
1251WINPR_ATTR_MALLOC(free, 1)
1252static
void* winpr_convert_to_png(WINPR_ATTR_UNUSED const
void* data, WINPR_ATTR_UNUSED
size_t size,
1253 UINT32 width, UINT32 height, UINT32 stride, UINT32 bpp,
1256 WINPR_ASSERT(data || (size == 0));
1257 WINPR_ASSERT(pSize);
1261 if ((width == 0) || (height == 0) || (stride == 0) || (bpp == 0))
1263 if (height > UINT32_MAX / stride)
1266#if defined(WINPR_UTILS_IMAGE_PNG)
1267 void* dst =
nullptr;
1268 SSIZE_T rc = save_png_to_buffer(bpp, width, stride, height, data, size, &dst);
1271 *pSize = (UINT32)rc;
1273#elif defined(WITH_LODEPNG)
1275 BYTE* dst =
nullptr;
1282 rc = lodepng_encode32(&dst, &dstsize, data, width, height);
1285 rc = lodepng_encode24(&dst, &dstsize, data, width, height);
1292 *pSize = (UINT32)dstsize;
1296 WLog_WARN(TAG,
"PNG not supported in this build");
1301SSIZE_T winpr_convert_from_png(WINPR_ATTR_UNUSED
const BYTE* comp_data,
1302 WINPR_ATTR_UNUSED
size_t comp_data_bytes, UINT32* width,
1303 UINT32* height, UINT32* bpp, BYTE** ppdecomp_data, UINT32* scanline)
1305 WINPR_ASSERT(comp_data || (comp_data_bytes == 0));
1306 WINPR_ASSERT(width);
1307 WINPR_ASSERT(height);
1309 WINPR_ASSERT(ppdecomp_data);
1310 WINPR_ASSERT(scanline);
1315 *ppdecomp_data =
nullptr;
1318#if defined(WINPR_UTILS_IMAGE_PNG)
1321 winpr_read_png_from_buffer(comp_data, comp_data_bytes, &len, width, height, bpp, scanline);
1322 if (!*ppdecomp_data)
1324 return (SSIZE_T)len;
1325#elif defined(WITH_LODEPNG)
1327 if (*width > UINT32_MAX / 4)
1331 lodepng_decode32((
unsigned char**)ppdecomp_data, width, height, comp_data, comp_data_bytes);
1332 *scanline = *width * 4;
1335 WLog_WARN(TAG,
"PNG not supported in this build");
1340BOOL winpr_image_format_is_supported(UINT32 format)
1344 case WINPR_IMAGE_BITMAP:
1345#if defined(WINPR_UTILS_IMAGE_PNG) || defined(WITH_LODEPNG)
1346 case WINPR_IMAGE_PNG:
1348#if defined(WINPR_UTILS_IMAGE_JPEG)
1349 case WINPR_IMAGE_JPEG:
1351#if defined(WINPR_UTILS_IMAGE_WEBP)
1352 case WINPR_IMAGE_WEBP:
1360WINPR_ATTR_MALLOC(free, 1)
1361static BYTE* convert(const
wImage* image,
size_t* pstride, WINPR_ATTR_UNUSED UINT32 flags)
1363 WINPR_ASSERT(image);
1364 WINPR_ASSERT(pstride);
1367 if (image->bitsPerPixel < 24)
1370 const size_t stride = image->width * 4ull;
1371 BYTE* data = calloc(stride, image->height);
1374 for (
size_t y = 0; y < image->height; y++)
1376 const BYTE* srcLine = &image->data[image->scanline * y];
1377 BYTE* dstLine = &data[stride * y];
1378 if (image->bitsPerPixel == 32)
1379 memcpy(dstLine, srcLine, stride);
1382 for (
size_t x = 0; x < image->width; x++)
1384 const BYTE* src = &srcLine[image->bytesPerPixel * x];
1385 BYTE* dst = &dstLine[4ull * x];
1403static BOOL compare_byte_relaxed(BYTE a, BYTE b, UINT32 flags)
1407 if ((flags & WINPR_IMAGE_CMP_FUZZY) != 0)
1409 const int diff = abs((
int)a) - abs((
int)b);
1423static BOOL compare_pixel(
const BYTE* pa,
const BYTE* pb, UINT32 flags)
1428 if (!compare_byte_relaxed(*pa++, *pb++, flags))
1430 if (!compare_byte_relaxed(*pa++, *pb++, flags))
1432 if (!compare_byte_relaxed(*pa++, *pb++, flags))
1434 if ((flags & WINPR_IMAGE_CMP_IGNORE_ALPHA) == 0)
1436 if (!compare_byte_relaxed(*pa++, *pb++, flags))
1442BOOL winpr_image_equal(
const wImage* imageA,
const wImage* imageB, UINT32 flags)
1444 if (imageA == imageB)
1446 if (!imageA || !imageB)
1449 if (imageA->height != imageB->height)
1451 if (imageA->width != imageB->width)
1454 if ((flags & WINPR_IMAGE_CMP_IGNORE_DEPTH) == 0)
1456 if (imageA->bitsPerPixel != imageB->bitsPerPixel)
1458 if (imageA->bytesPerPixel != imageB->bytesPerPixel)
1465 BYTE* dataA = convert(imageA, &astride, flags);
1466 BYTE* dataB = convert(imageA, &bstride, flags);
1467 if (dataA && dataB && (astride == bstride))
1470 for (
size_t y = 0; y < imageA->height; y++)
1472 const BYTE* lineA = &dataA[astride * y];
1473 const BYTE* lineB = &dataB[bstride * y];
1475 for (
size_t x = 0; x < imageA->width; x++)
1477 const BYTE* pa = &lineA[x * 4ull];
1478 const BYTE* pb = &lineB[x * 4ull];
1480 if (!compare_pixel(pa, pb, flags))
1490const char* winpr_image_format_mime(UINT32 format)
1494 case WINPR_IMAGE_BITMAP:
1496 case WINPR_IMAGE_PNG:
1498 case WINPR_IMAGE_WEBP:
1499 return "image/webp";
1500 case WINPR_IMAGE_JPEG:
1501 return "image/jpeg";
1507const char* winpr_image_format_extension(UINT32 format)
1511 case WINPR_IMAGE_BITMAP:
1513 case WINPR_IMAGE_PNG:
1515 case WINPR_IMAGE_WEBP:
1517 case WINPR_IMAGE_JPEG:
1524void* winpr_image_write_buffer(
wImage* image, UINT32 format,
size_t* psize)
1526 WINPR_ASSERT(image);
1529 case WINPR_IMAGE_BITMAP:
1532 size_t size = 1ull * image->height * image->scanline;
1533 void* data = winpr_bitmap_write_buffer(image->data, size, image->width, image->height,
1534 image->scanline, image->bitsPerPixel, &outsize);
1538 case WINPR_IMAGE_WEBP:
1541 size_t size = 1ull * image->height * image->scanline;
1542 void* data = winpr_convert_to_webp(image->data, size, image->width, image->height,
1543 image->scanline, image->bitsPerPixel, &outsize);
1547 case WINPR_IMAGE_JPEG:
1550 size_t size = 1ull * image->height * image->scanline;
1551 void* data = winpr_convert_to_jpeg(image->data, size, image->width, image->height,
1552 image->scanline, image->bitsPerPixel, &outsize);
1556 case WINPR_IMAGE_PNG:
1559 size_t size = 1ull * image->height * image->scanline;
1560 void* data = winpr_convert_to_png(image->data, size, image->width, image->height,
1561 image->scanline, image->bitsPerPixel, &outsize);