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")
57static SSIZE_T winpr_convert_from_jpeg(
const BYTE* comp_data,
size_t comp_data_bytes, UINT32* width,
58 UINT32* height, UINT32* bpp, BYTE** ppdecomp_data);
59static SSIZE_T winpr_convert_from_png(
const BYTE* comp_data,
size_t comp_data_bytes, UINT32* width,
60 UINT32* height, UINT32* bpp, BYTE** ppdecomp_data);
61static SSIZE_T winpr_convert_from_webp(
const BYTE* comp_data,
size_t comp_data_bytes, UINT32* width,
62 UINT32* height, UINT32* bpp, BYTE** ppdecomp_data);
69 Stream_Write_UINT8(s, bf->bfType[0]);
70 Stream_Write_UINT8(s, bf->bfType[1]);
71 Stream_Write_UINT32(s, bf->bfSize);
72 Stream_Write_UINT16(s, bf->bfReserved1);
73 Stream_Write_UINT16(s, bf->bfReserved2);
74 Stream_Write_UINT32(s, bf->bfOffBits);
80 static wLog* log =
nullptr;
88 Stream_Read_UINT8(s, bf->bfType[0]);
89 Stream_Read_UINT8(s, bf->bfType[1]);
90 Stream_Read_UINT32(s, bf->bfSize);
91 Stream_Read_UINT16(s, bf->bfReserved1);
92 Stream_Read_UINT16(s, bf->bfReserved2);
93 Stream_Read_UINT32(s, bf->bfOffBits);
97 WLog_Print(log, WLOG_ERROR,
"Invalid bitmap::bfSize=%" PRIu32
", require at least %" PRIuz,
102 if ((bf->bfType[0] !=
'B') || (bf->bfType[1] !=
'M'))
104 WLog_Print(log, WLOG_ERROR,
"Invalid bitmap header [%c%c], expected [BM]", bf->bfType[0],
108 return Stream_CheckAndLogRequiredCapacityWLog(log, s,
117 Stream_Write_UINT32(s, bi->biSize);
118 Stream_Write_INT32(s, bi->biWidth);
119 Stream_Write_INT32(s, bi->biHeight);
120 Stream_Write_UINT16(s, bi->biPlanes);
121 Stream_Write_UINT16(s, bi->biBitCount);
122 Stream_Write_UINT32(s, bi->biCompression);
123 Stream_Write_UINT32(s, bi->biSizeImage);
124 Stream_Write_INT32(s, bi->biXPelsPerMeter);
125 Stream_Write_INT32(s, bi->biYPelsPerMeter);
126 Stream_Write_UINT32(s, bi->biClrUsed);
127 Stream_Write_UINT32(s, bi->biClrImportant);
136 const size_t start = Stream_GetPosition(s);
137 Stream_Read_UINT32(s, bi->biSize);
138 Stream_Read_INT32(s, bi->biWidth);
139 Stream_Read_INT32(s, bi->biHeight);
140 Stream_Read_UINT16(s, bi->biPlanes);
141 Stream_Read_UINT16(s, bi->biBitCount);
142 Stream_Read_UINT32(s, bi->biCompression);
143 Stream_Read_UINT32(s, bi->biSizeImage);
144 Stream_Read_INT32(s, bi->biXPelsPerMeter);
145 Stream_Read_INT32(s, bi->biYPelsPerMeter);
146 Stream_Read_UINT32(s, bi->biClrUsed);
147 Stream_Read_UINT32(s, bi->biClrImportant);
149 if ((bi->biBitCount < 1) || (bi->biBitCount > 32))
151 WLog_WARN(TAG,
"invalid biBitCount=%" PRIu32, bi->biBitCount);
157 WLog_WARN(TAG,
"negative biWidth=%" PRId32, bi->biWidth);
163 switch (bi->biCompression)
166 if (bi->biBitCount <= 8)
168 DWORD used = bi->biClrUsed;
170 used = (1u << bi->biBitCount) / 8;
171 offset +=
sizeof(
RGBQUAD) * used;
173 if (bi->biSizeImage == 0)
175 const UINT64 rawustride =
176 WINPR_ASSERTING_INT_CAST(UINT64, bi->biWidth) * bi->biBitCount;
177 const UINT64 ustride = ((rawustride + 31) & ~31u) >> 3;
178 if (ustride > UINT32_MAX)
180 WLog_ERR(TAG,
"bi->biWidth * bi->biBitCount > UINT32_MAX");
184 const UINT32 stride = WINPR_ASSERTING_INT_CAST(uint32_t, ustride);
185 const UINT32 usize = WINPR_ASSERTING_INT_CAST(UINT32, llabs(bi->biHeight)) * stride;
186 if (usize > UINT32_MAX)
188 WLog_ERR(TAG,
"abs(bi->biHeight) * stride > UINT32_MAX");
191 bi->biSizeImage = WINPR_ASSERTING_INT_CAST(uint32_t, usize);
195 offset +=
sizeof(DWORD) * 3;
198 WLog_ERR(TAG,
"unsupported biCompression %" PRIu32, bi->biCompression);
202 if (bi->biSizeImage == 0)
204 WLog_ERR(TAG,
"invalid biSizeImage %" PRIu32, bi->biSizeImage);
208 const size_t pos = Stream_GetPosition(s) - start;
209 if (bi->biSize < pos)
211 WLog_ERR(TAG,
"invalid biSize %" PRIu32
" < (actual) offset %" PRIuz, bi->biSize, pos);
216 return Stream_SafeSeek(s, bi->biSize - pos);
219BYTE* winpr_bitmap_construct_header(
size_t width,
size_t height,
size_t bpp)
221 BYTE* result =
nullptr;
225 size_t stride = (width * bpp + 7) / 8;
226 if ((stride % 4) != 0)
227 stride += 4 - (stride % 4);
229 size_t imgSize = stride * height;
230 if ((width > INT32_MAX) || (height > INT32_MAX) || (bpp > UINT16_MAX) || (imgSize > UINT32_MAX))
233 wStream* s = Stream_New(
nullptr, WINPR_IMAGE_BMP_HEADER_LEN);
243 bi.biSizeImage = (UINT32)imgSize;
244 bf.bfSize = bf.bfOffBits + bi.biSizeImage;
245 bi.biWidth = (INT32)width;
246 bi.biHeight = -1 * (INT32)height;
248 bi.biBitCount = (UINT16)bpp;
249 bi.biCompression = BI_RGB;
250 bi.biXPelsPerMeter = (INT32)width;
251 bi.biYPelsPerMeter = (INT32)height;
253 bi.biClrImportant = 0;
256 switch (bi.biCompression)
259 if (bi.biBitCount <= 8)
261 DWORD used = bi.biClrUsed;
263 used = (1u << bi.biBitCount) / 8;
264 offset +=
sizeof(
RGBQUAD) * used;
268 offset +=
sizeof(DWORD) * 3;
274 if (!writeBitmapFileHeader(s, &bf))
277 if (!writeBitmapInfoHeader(s, &bi))
280 if (!Stream_EnsureRemainingCapacity(s, offset))
283 Stream_Zero(s, offset);
284 result = Stream_Buffer(s);
286 Stream_Free(s, result ==
nullptr);
294WINPR_ATTR_MALLOC(free, 1)
296static
void* winpr_bitmap_write_buffer(const BYTE* data, WINPR_ATTR_UNUSED
size_t size,
297 UINT32 width, UINT32 height, UINT32 stride, UINT32 bpp,
300 WINPR_ASSERT(data || (size == 0));
302 void* result =
nullptr;
303 size_t bpp_stride = 1ull * width * (bpp / 8);
304 if ((bpp_stride % 4) != 0)
305 bpp_stride += 4 - (bpp_stride % 4);
307 if (bpp_stride > UINT32_MAX)
310 wStream* s = Stream_New(
nullptr, 1024);
313 stride = (UINT32)bpp_stride;
315 BYTE* bmp_header = winpr_bitmap_construct_header(width, height, bpp);
318 if (!Stream_EnsureRemainingCapacity(s, WINPR_IMAGE_BMP_HEADER_LEN))
320 Stream_Write(s, bmp_header, WINPR_IMAGE_BMP_HEADER_LEN);
322 if (!Stream_EnsureRemainingCapacity(s, 1ULL * bpp_stride * height))
325 for (
size_t y = 0; y < height; y++)
327 const BYTE* line = &data[stride * y];
329 Stream_Write(s, line, stride);
330 Stream_Zero(s, bpp_stride - stride);
333 result = Stream_Buffer(s);
335 const size_t pos = Stream_GetPosition(s);
336 if (pos > UINT32_MAX)
338 *pSize = (UINT32)pos;
341 Stream_Free(s, result ==
nullptr);
346int winpr_bitmap_write(
const char* filename,
const BYTE* data,
size_t width,
size_t height,
349 return winpr_bitmap_write_ex(filename, data, 0, width, height, bpp);
352int winpr_bitmap_write_ex(
const char* filename,
const BYTE* data,
size_t stride,
size_t width,
353 size_t height,
size_t bpp)
357 void* bmpdata =
nullptr;
358 const size_t bpp_stride = ((((width * bpp) + 31) & (size_t)~31) >> 3);
360 if ((stride > UINT32_MAX) || (width > UINT32_MAX) || (height > UINT32_MAX) ||
370 const size_t size = stride * 1ull * height;
371 bmpdata = winpr_bitmap_write_buffer(data, size, (UINT32)width, (UINT32)height,
372 (UINT32)stride, (UINT32)bpp, &bmpsize);
377 fp = winpr_fopen(filename,
"w+b");
380 WLog_ERR(TAG,
"failed to open file %s", filename);
384 if (fwrite(bmpdata, bmpsize, 1, fp) != 1)
396static int write_and_free(
const char* filename,
void* data,
size_t size)
403 FILE* fp = winpr_fopen(filename,
"w+b");
408 const size_t w = fwrite(data, 1, size, fp);
410 status = (w == size) ? 1 : -1;
418int winpr_image_write(
wImage* image,
const char* filename)
421 return winpr_image_write_ex(image, WINPR_ASSERTING_INT_CAST(uint32_t, image->type), filename);
424int winpr_image_write_ex(
wImage* image, UINT32 format,
const char* filename)
429 void* data = winpr_image_write_buffer(image, format, &size);
432 return write_and_free(filename, data, size);
435static int winpr_image_bitmap_read_buffer(
wImage* image,
const BYTE* buffer,
size_t size)
441 wStream sbuffer = WINPR_C_ARRAY_INIT;
442 wStream* s = Stream_StaticConstInit(&sbuffer, buffer, size);
447 size_t bmpoffset = 0;
448 if (!readBitmapFileHeader(s, &bf) || !readBitmapInfoHeader(s, &bi, &bmpoffset))
451 if ((bf.bfType[0] !=
'B') || (bf.bfType[1] !=
'M'))
453 WLog_WARN(TAG,
"Invalid bitmap header %c%c", bf.bfType[0], bf.bfType[1]);
457 image->type = WINPR_IMAGE_BITMAP;
460 const size_t pos = Stream_GetPosition(s);
461 const size_t expect = bf.bfOffBits;
464 WLog_WARN(TAG,
"pos=%" PRIuz
", expected %" PRIuz
", offset=%" PRIuz, pos, expect,
470 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, bi.biSizeImage))
475 WLog_WARN(TAG,
"bi.biWidth=%" PRId32, bi.biWidth);
479 image->width = (UINT32)bi.biWidth;
484 image->height = (UINT32)(-1 * bi.biHeight);
489 image->height = (UINT32)bi.biHeight;
492 if (image->height <= 0)
494 WLog_WARN(TAG,
"image->height=%" PRIu32, image->height);
498 image->bitsPerPixel = bi.biBitCount;
500 const size_t bpp = (bi.biBitCount + 7UL) / 8UL;
501 image->bytesPerPixel = WINPR_ASSERTING_INT_CAST(uint32_t, bpp);
503 image->scanline = WINPR_ASSERTING_INT_CAST(uint32_t, bi.biWidth) * image->bytesPerPixel;
504 if ((image->scanline % 4) != 0)
505 image->scanline += 4 - image->scanline % 4;
508 const size_t bmpsize = 1ULL * image->scanline * image->height;
509 if (bmpsize != bi.biSizeImage)
510 WLog_WARN(TAG,
"bmpsize=%" PRIuz
" != bi.biSizeImage=%" PRIu32, bmpsize,
514 size_t scanline = image->scanline;
515 if (bi.biSizeImage < bmpsize)
518 const size_t uscanline = image->width * bpp;
519 const size_t unaligned = image->height * uscanline;
520 if (bi.biSizeImage != unaligned)
522 scanline = uscanline;
525 image->data =
nullptr;
527 const size_t asize = 1ULL * image->scanline * image->height;
529 image->data = (BYTE*)malloc(asize);
537 BYTE* pDstData = image->data;
539 for (
size_t index = 0; index < image->height; index++)
541 Stream_Read(s, pDstData, scanline);
542 pDstData += image->scanline;
547 BYTE* pDstData = &(image->data[(image->height - 1ull) * image->scanline]);
549 for (
size_t index = 0; index < image->height; index++)
551 Stream_Read(s, pDstData, scanline);
552 pDstData -= image->scanline;
565 image->data =
nullptr;
571int winpr_image_read(
wImage* image,
const char* filename)
575 FILE* fp = winpr_fopen(filename,
"rb");
578 WLog_ERR(TAG,
"failed to open file %s", filename);
582 (void)fseek(fp, 0, SEEK_END);
583 INT64 pos = _ftelli64(fp);
584 (void)fseek(fp, 0, SEEK_SET);
588 BYTE* buffer = malloc((
size_t)pos);
591 size_t r = fread(buffer, 1, (
size_t)pos, fp);
592 if (r == (
size_t)pos)
594 status = winpr_image_read_buffer(image, buffer, (
size_t)pos);
603int winpr_image_read_buffer(
wImage* image,
const BYTE* buffer,
size_t size)
605 BYTE sig[12] = WINPR_C_ARRAY_INIT;
608 if (size <
sizeof(sig))
611 CopyMemory(sig, buffer,
sizeof(sig));
613 if ((sig[0] ==
'B') && (sig[1] ==
'M'))
615 image->type = WINPR_IMAGE_BITMAP;
616 status = winpr_image_bitmap_read_buffer(image, buffer, size);
618 else if ((sig[0] ==
'R') && (sig[1] ==
'I') && (sig[2] ==
'F') && (sig[3] ==
'F') &&
619 (sig[8] ==
'W') && (sig[9] ==
'E') && (sig[10] ==
'B') && (sig[11] ==
'P'))
621 image->type = WINPR_IMAGE_WEBP;
622 const SSIZE_T rc = winpr_convert_from_webp(buffer, size, &image->width, &image->height,
623 &image->bitsPerPixel, &image->data);
626 image->bytesPerPixel = (image->bitsPerPixel + 7) / 8;
627 image->scanline = image->width * image->bytesPerPixel;
631 else if ((sig[0] == 0xFF) && (sig[1] == 0xD8) && (sig[2] == 0xFF) && (sig[3] == 0xE0) &&
632 (sig[6] == 0x4A) && (sig[7] == 0x46) && (sig[8] == 0x49) && (sig[9] == 0x46) &&
635 image->type = WINPR_IMAGE_JPEG;
636 const SSIZE_T rc = winpr_convert_from_jpeg(buffer, size, &image->width, &image->height,
637 &image->bitsPerPixel, &image->data);
640 image->bytesPerPixel = (image->bitsPerPixel + 7) / 8;
641 image->scanline = image->width * image->bytesPerPixel;
645 else if ((sig[0] == 0x89) && (sig[1] ==
'P') && (sig[2] ==
'N') && (sig[3] ==
'G') &&
646 (sig[4] ==
'\r') && (sig[5] ==
'\n') && (sig[6] == 0x1A) && (sig[7] ==
'\n'))
648 image->type = WINPR_IMAGE_PNG;
649 const SSIZE_T rc = winpr_convert_from_png(buffer, size, &image->width, &image->height,
650 &image->bitsPerPixel, &image->data);
653 image->bytesPerPixel = (image->bitsPerPixel + 7) / 8;
654 image->scanline = image->width * image->bytesPerPixel;
662wImage* winpr_image_new(
void)
672void winpr_image_free(
wImage* image, BOOL bFreeBuffer)
683static void* winpr_convert_to_jpeg(WINPR_ATTR_UNUSED
const void* data,
684 WINPR_ATTR_UNUSED
size_t size, WINPR_ATTR_UNUSED UINT32 width,
685 WINPR_ATTR_UNUSED UINT32 height, WINPR_ATTR_UNUSED UINT32 stride,
686 WINPR_ATTR_UNUSED UINT32 bpp, WINPR_ATTR_UNUSED UINT32* pSize)
688 WINPR_ASSERT(data || (size == 0));
693#if !defined(WINPR_UTILS_IMAGE_JPEG)
694 WLog_WARN(TAG,
"JPEG not supported in this build");
697 BYTE* outbuffer =
nullptr;
698 unsigned long outsize = 0;
699 struct jpeg_compress_struct cinfo = WINPR_C_ARRAY_INIT;
701 const size_t expect1 = 1ull * stride * height;
702 const size_t bytes = (bpp + 7) / 8;
703 const size_t expect2 = 1ull * width * height * bytes;
704 if (expect1 < expect2)
710 struct jpeg_error_mgr jerr = WINPR_C_ARRAY_INIT;
711 cinfo.err = jpeg_std_error(&jerr);
713 jpeg_create_compress(&cinfo);
714 jpeg_mem_dest(&cinfo, &outbuffer, &outsize);
716 cinfo.image_width = width;
717 cinfo.image_height = height;
718 WINPR_ASSERT(bpp <= INT32_MAX / 8);
719 cinfo.input_components = (int)(bpp + 7) / 8;
720 cinfo.in_color_space = (bpp > 24) ? JCS_EXT_BGRA : JCS_EXT_BGR;
721 cinfo.data_precision = 8;
723 jpeg_set_defaults(&cinfo);
724 jpeg_set_quality(&cinfo, 100, TRUE);
726 cinfo.comp_info[0].h_samp_factor = cinfo.comp_info[0].v_samp_factor = 1;
728 jpeg_start_compress(&cinfo, TRUE);
730 const JSAMPLE* cdata = data;
731 for (
size_t x = 0; x < height; x++)
733 WINPR_ASSERT(x * stride <= UINT32_MAX);
734 const JDIMENSION offset = (JDIMENSION)x * stride;
738 JSAMPLE* coffset = WINPR_CAST_CONST_PTR_AWAY(&cdata[offset], JSAMPLE*);
739 if (jpeg_write_scanlines(&cinfo, &coffset, 1) != 1)
744 jpeg_finish_compress(&cinfo);
745 jpeg_destroy_compress(&cinfo);
747 WINPR_ASSERT(outsize <= UINT32_MAX);
748 *pSize = (UINT32)outsize;
754SSIZE_T winpr_convert_from_jpeg(WINPR_ATTR_UNUSED
const BYTE* comp_data,
755 WINPR_ATTR_UNUSED
size_t comp_data_bytes,
756 WINPR_ATTR_UNUSED UINT32* width, WINPR_ATTR_UNUSED UINT32* height,
757 WINPR_ATTR_UNUSED UINT32* bpp,
758 WINPR_ATTR_UNUSED BYTE** ppdecomp_data)
761 WINPR_ASSERT(comp_data || (comp_data_bytes == 0));
763 WINPR_ASSERT(height);
765 WINPR_ASSERT(ppdecomp_data);
767#if !defined(WINPR_UTILS_IMAGE_JPEG)
768 WLog_WARN(TAG,
"JPEG not supported in this build");
771 struct jpeg_decompress_struct cinfo = WINPR_C_ARRAY_INIT;
772 struct jpeg_error_mgr jerr;
774 BYTE* decomp_data =
nullptr;
776 cinfo.err = jpeg_std_error(&jerr);
777 jpeg_create_decompress(&cinfo);
778 jpeg_mem_src(&cinfo, comp_data, comp_data_bytes);
780 if (jpeg_read_header(&cinfo, 1) != JPEG_HEADER_OK)
783 cinfo.out_color_space = cinfo.num_components > 3 ? JCS_EXT_RGBA : JCS_EXT_BGR;
785 *width = WINPR_ASSERTING_INT_CAST(uint32_t, cinfo.image_width);
786 *height = WINPR_ASSERTING_INT_CAST(uint32_t, cinfo.image_height);
787 *bpp = WINPR_ASSERTING_INT_CAST(uint32_t, cinfo.num_components * 8);
789 if (!jpeg_start_decompress(&cinfo))
793 1ULL * cinfo.image_width * WINPR_ASSERTING_INT_CAST(uint32_t, cinfo.num_components);
795 if ((stride == 0) || (cinfo.image_height == 0))
798 decomp_data = calloc(stride, cinfo.image_height);
801 while (cinfo.output_scanline < cinfo.image_height)
803 JSAMPROW row = &decomp_data[cinfo.output_scanline * stride];
804 if (jpeg_read_scanlines(&cinfo, &row, 1) != 1)
807 const size_t ssize = stride * cinfo.image_height;
808 WINPR_ASSERT(ssize < SSIZE_MAX);
809 size = (SSIZE_T)ssize;
811 jpeg_finish_decompress(&cinfo);
814 jpeg_destroy_decompress(&cinfo);
815 *ppdecomp_data = decomp_data;
820static void* winpr_convert_to_webp(WINPR_ATTR_UNUSED
const void* data,
821 WINPR_ATTR_UNUSED
size_t size, WINPR_ATTR_UNUSED UINT32 width,
822 WINPR_ATTR_UNUSED UINT32 height, WINPR_ATTR_UNUSED UINT32 stride,
823 WINPR_ATTR_UNUSED UINT32 bpp, UINT32* pSize)
825 WINPR_ASSERT(data || (size == 0));
830#if !defined(WINPR_UTILS_IMAGE_WEBP)
831 WLog_WARN(TAG,
"WEBP not supported in this build");
835 uint8_t* pDstData =
nullptr;
836 WINPR_ASSERT(width <= INT32_MAX);
837 WINPR_ASSERT(height <= INT32_MAX);
838 WINPR_ASSERT(stride <= INT32_MAX);
842 dstSize = WebPEncodeLosslessBGRA(data, (
int)width, (
int)height, (
int)stride, &pDstData);
845 dstSize = WebPEncodeLosslessBGR(data, (
int)width, (
int)height, (
int)stride, &pDstData);
851 void* rc = malloc(dstSize);
854 memcpy(rc, pDstData, dstSize);
856 WINPR_ASSERT(dstSize <= UINT32_MAX);
857 *pSize = (UINT32)dstSize;
864SSIZE_T winpr_convert_from_webp(WINPR_ATTR_UNUSED
const BYTE* comp_data,
865 WINPR_ATTR_UNUSED
size_t comp_data_bytes, UINT32* width,
866 UINT32* height, UINT32* bpp, BYTE** ppdecomp_data)
868 WINPR_ASSERT(comp_data || (comp_data_bytes == 0));
870 WINPR_ASSERT(height);
872 WINPR_ASSERT(ppdecomp_data);
877 *ppdecomp_data =
nullptr;
878#if !defined(WINPR_UTILS_IMAGE_WEBP)
879 WLog_WARN(TAG,
"WEBP not supported in this build");
885 uint8_t* dst = WebPDecodeBGRA(comp_data, comp_data_bytes, &w, &h);
886 if (!dst || (w < 0) || (h < 0))
892 *width = WINPR_ASSERTING_INT_CAST(uint32_t, w);
893 *height = WINPR_ASSERTING_INT_CAST(uint32_t, h);
895 *ppdecomp_data = dst;
900#if defined(WINPR_UTILS_IMAGE_PNG)
907static void png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
910 struct png_mem_encode* p =
911 (
struct png_mem_encode*)png_get_io_ptr(png_ptr);
912 size_t nsize = p->size + length;
917 char* tmp = realloc(p->buffer, nsize);
922 p->buffer = malloc(nsize);
925 png_error(png_ptr,
"Write Error");
928 memcpy(p->buffer + p->size, data, length);
933static void png_flush(WINPR_ATTR_UNUSED png_structp png_ptr)
937static SSIZE_T save_png_to_buffer(UINT32 bpp, UINT32 width, uint32_t stride, UINT32 height,
938 const uint8_t* data,
size_t size,
void** pDstData)
941 png_structp png_ptr =
nullptr;
942 png_infop info_ptr =
nullptr;
943 png_byte** row_pointers =
nullptr;
944 struct png_mem_encode state = WINPR_C_ARRAY_INIT;
948 if (!data || (size == 0))
951 WINPR_ASSERT(pDstData);
953 if (size < (1ULL * stride * height))
957 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
nullptr,
nullptr,
nullptr);
958 if (png_ptr ==
nullptr)
962 info_ptr = png_create_info_struct(png_ptr);
963 if (info_ptr ==
nullptr)
967 if (setjmp(png_jmpbuf(png_ptr)))
971 int colorType = PNG_COLOR_TYPE_PALETTE;
973 colorType = PNG_COLOR_TYPE_RGB;
975 colorType = PNG_COLOR_TYPE_RGB;
977 colorType = PNG_COLOR_TYPE_RGBA;
979 png_set_IHDR(png_ptr, info_ptr, width, height, 8, colorType, PNG_INTERLACE_NONE,
980 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
983 row_pointers = (png_byte**)png_malloc(png_ptr, height *
sizeof(png_byte*));
984 for (
size_t y = 0; y < height; ++y)
986 const uint8_t* line = &data[y * stride];
987 uint8_t* row = png_malloc(png_ptr,
sizeof(uint8_t) * stride);
988 row_pointers[y] = (png_byte*)row;
989 for (
size_t x = 0; x < width; ++x)
1003 png_set_write_fn(png_ptr, &state, png_write_data, png_flush);
1004 png_set_rows(png_ptr, info_ptr, row_pointers);
1005 png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_BGR,
nullptr);
1008 for (
size_t y = 0; y < height; y++)
1009 png_free(png_ptr, row_pointers[y]);
1010 png_free(png_ptr, (
void*)row_pointers);
1013 if (state.size > SSIZE_MAX)
1015 rc = (SSIZE_T)state.size;
1016 *pDstData = state.buffer;
1018 png_destroy_write_struct(&png_ptr, &info_ptr);
1027 png_uint_32 bufsize;
1028 png_uint_32 current_pos;
1029} MEMORY_READER_STATE;
1031static void read_data_memory(png_structp png_ptr, png_bytep data,
size_t length)
1033 MEMORY_READER_STATE* f = png_get_io_ptr(png_ptr);
1034 if (length > (f->bufsize - f->current_pos))
1035 png_error(png_ptr,
"read error in read_data_memory (loadpng)");
1038 memcpy(data, f->buffer + f->current_pos, length);
1039 f->current_pos += length;
1043static void* winpr_read_png_from_buffer(
const void* data,
size_t SrcSize,
size_t* pSize,
1044 UINT32* pWidth, UINT32* pHeight, UINT32* pBpp)
1047 png_uint_32 width = 0;
1048 png_uint_32 height = 0;
1051 int interlace_type = 0;
1052 int transforms = PNG_TRANSFORM_IDENTITY;
1053 MEMORY_READER_STATE memory_reader_state = WINPR_C_ARRAY_INIT;
1054 png_bytepp row_pointers =
nullptr;
1055 png_infop info_ptr =
nullptr;
1056 if (SrcSize > UINT32_MAX)
1059 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
nullptr,
nullptr,
nullptr);
1062 info_ptr = png_create_info_struct(png_ptr);
1066 memory_reader_state.buffer = WINPR_CAST_CONST_PTR_AWAY(data, png_bytep);
1067 memory_reader_state.bufsize = (UINT32)SrcSize;
1068 memory_reader_state.current_pos = 0;
1070 png_set_read_fn(png_ptr, &memory_reader_state, read_data_memory);
1072 transforms |= PNG_TRANSFORM_BGR;
1073 png_read_png(png_ptr, info_ptr, transforms,
nullptr);
1075 if (png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type,
1076 nullptr,
nullptr) != 1)
1079 WINPR_ASSERT(bit_depth >= 0);
1080 const png_byte channelcount = png_get_channels(png_ptr, info_ptr);
1081 const size_t bpp = channelcount * (size_t)bit_depth;
1083 row_pointers = png_get_rows(png_ptr, info_ptr);
1086 const size_t stride = 1ULL * width * bpp / 8ull;
1087 const size_t png_stride = png_get_rowbytes(png_ptr, info_ptr);
1088 const size_t size = 1ULL * width * height * bpp / 8ull;
1089 const size_t copybytes = stride > png_stride ? png_stride : stride;
1095 for (png_uint_32 i = 0; i < height; i++)
1097 memcpy(cur, row_pointers[i], copybytes);
1103 WINPR_ASSERT(bpp <= UINT32_MAX);
1104 *pBpp = (UINT32)bpp;
1109 png_destroy_read_struct(&png_ptr, &info_ptr,
nullptr);
1114static void* winpr_convert_to_png(WINPR_ATTR_UNUSED
const void* data, WINPR_ATTR_UNUSED
size_t size,
1115 WINPR_ATTR_UNUSED UINT32 width, WINPR_ATTR_UNUSED UINT32 height,
1116 WINPR_ATTR_UNUSED UINT32 stride, WINPR_ATTR_UNUSED UINT32 bpp,
1119 WINPR_ASSERT(data || (size == 0));
1120 WINPR_ASSERT(pSize);
1124#if defined(WINPR_UTILS_IMAGE_PNG)
1125 void* dst =
nullptr;
1126 SSIZE_T rc = save_png_to_buffer(bpp, width, stride, height, data, size, &dst);
1129 *pSize = (UINT32)rc;
1131#elif defined(WITH_LODEPNG)
1133 BYTE* dst =
nullptr;
1140 rc = lodepng_encode32(&dst, &dstsize, data, width, height);
1143 rc = lodepng_encode24(&dst, &dstsize, data, width, height);
1150 *pSize = (UINT32)dstsize;
1154 WLog_WARN(TAG,
"PNG not supported in this build");
1159SSIZE_T winpr_convert_from_png(WINPR_ATTR_UNUSED
const BYTE* comp_data,
1160 WINPR_ATTR_UNUSED
size_t comp_data_bytes,
1161 WINPR_ATTR_UNUSED UINT32* width, WINPR_ATTR_UNUSED UINT32* height,
1162 WINPR_ATTR_UNUSED UINT32* bpp,
1163 WINPR_ATTR_UNUSED BYTE** ppdecomp_data)
1165#if defined(WINPR_UTILS_IMAGE_PNG)
1168 winpr_read_png_from_buffer(comp_data, comp_data_bytes, &len, width, height, bpp);
1169 if (!*ppdecomp_data)
1171 return (SSIZE_T)len;
1172#elif defined(WITH_LODEPNG)
1174 return lodepng_decode32((
unsigned char**)ppdecomp_data, width, height, comp_data,
1177 WLog_WARN(TAG,
"PNG not supported in this build");
1182BOOL winpr_image_format_is_supported(UINT32 format)
1186 case WINPR_IMAGE_BITMAP:
1187#if defined(WINPR_UTILS_IMAGE_PNG) || defined(WITH_LODEPNG)
1188 case WINPR_IMAGE_PNG:
1190#if defined(WINPR_UTILS_IMAGE_JPEG)
1191 case WINPR_IMAGE_JPEG:
1193#if defined(WINPR_UTILS_IMAGE_WEBP)
1194 case WINPR_IMAGE_WEBP:
1202static BYTE* convert(
const wImage* image,
size_t* pstride, WINPR_ATTR_UNUSED UINT32 flags)
1204 WINPR_ASSERT(image);
1205 WINPR_ASSERT(pstride);
1208 if (image->bitsPerPixel < 24)
1211 const size_t stride = image->width * 4ull;
1212 BYTE* data = calloc(stride, image->height);
1215 for (
size_t y = 0; y < image->height; y++)
1217 const BYTE* srcLine = &image->data[image->scanline * y];
1218 BYTE* dstLine = &data[stride * y];
1219 if (image->bitsPerPixel == 32)
1220 memcpy(dstLine, srcLine, stride);
1223 for (
size_t x = 0; x < image->width; x++)
1225 const BYTE* src = &srcLine[image->bytesPerPixel * x];
1226 BYTE* dst = &dstLine[4ull * x];
1243static BOOL compare_byte_relaxed(BYTE a, BYTE b, UINT32 flags)
1247 if ((flags & WINPR_IMAGE_CMP_FUZZY) != 0)
1249 const int diff = abs((
int)a) - abs((
int)b);
1262static BOOL compare_pixel(
const BYTE* pa,
const BYTE* pb, UINT32 flags)
1267 if (!compare_byte_relaxed(*pa++, *pb++, flags))
1269 if (!compare_byte_relaxed(*pa++, *pb++, flags))
1271 if (!compare_byte_relaxed(*pa++, *pb++, flags))
1273 if ((flags & WINPR_IMAGE_CMP_IGNORE_ALPHA) == 0)
1275 if (!compare_byte_relaxed(*pa++, *pb++, flags))
1281BOOL winpr_image_equal(
const wImage* imageA,
const wImage* imageB, UINT32 flags)
1283 if (imageA == imageB)
1285 if (!imageA || !imageB)
1288 if (imageA->height != imageB->height)
1290 if (imageA->width != imageB->width)
1293 if ((flags & WINPR_IMAGE_CMP_IGNORE_DEPTH) == 0)
1295 if (imageA->bitsPerPixel != imageB->bitsPerPixel)
1297 if (imageA->bytesPerPixel != imageB->bytesPerPixel)
1304 BYTE* dataA = convert(imageA, &astride, flags);
1305 BYTE* dataB = convert(imageA, &bstride, flags);
1306 if (dataA && dataB && (astride == bstride))
1309 for (
size_t y = 0; y < imageA->height; y++)
1311 const BYTE* lineA = &dataA[astride * y];
1312 const BYTE* lineB = &dataB[bstride * y];
1314 for (
size_t x = 0; x < imageA->width; x++)
1316 const BYTE* pa = &lineA[x * 4ull];
1317 const BYTE* pb = &lineB[x * 4ull];
1319 if (!compare_pixel(pa, pb, flags))
1329const char* winpr_image_format_mime(UINT32 format)
1333 case WINPR_IMAGE_BITMAP:
1335 case WINPR_IMAGE_PNG:
1337 case WINPR_IMAGE_WEBP:
1338 return "image/webp";
1339 case WINPR_IMAGE_JPEG:
1340 return "image/jpeg";
1346const char* winpr_image_format_extension(UINT32 format)
1350 case WINPR_IMAGE_BITMAP:
1352 case WINPR_IMAGE_PNG:
1354 case WINPR_IMAGE_WEBP:
1356 case WINPR_IMAGE_JPEG:
1363void* winpr_image_write_buffer(
wImage* image, UINT32 format,
size_t* psize)
1365 WINPR_ASSERT(image);
1368 case WINPR_IMAGE_BITMAP:
1371 size_t size = 1ull * image->height * image->scanline;
1372 void* data = winpr_bitmap_write_buffer(image->data, size, image->width, image->height,
1373 image->scanline, image->bitsPerPixel, &outsize);
1377 case WINPR_IMAGE_WEBP:
1380 size_t size = 1ull * image->height * image->scanline;
1381 void* data = winpr_convert_to_webp(image->data, size, image->width, image->height,
1382 image->scanline, image->bitsPerPixel, &outsize);
1386 case WINPR_IMAGE_JPEG:
1389 size_t size = 1ull * image->height * image->scanline;
1390 void* data = winpr_convert_to_jpeg(image->data, size, image->width, image->height,
1391 image->scanline, image->bitsPerPixel, &outsize);
1395 case WINPR_IMAGE_PNG:
1398 size_t size = 1ull * image->height * image->scanline;
1399 void* data = winpr_convert_to_png(image->data, size, image->width, image->height,
1400 image->scanline, image->bitsPerPixel, &outsize);