24 #include <winpr/config.h>
26 #include <winpr/wtypes.h>
27 #include <winpr/crt.h>
28 #include <winpr/file.h>
30 #include <winpr/image.h>
32 #if defined(WINPR_UTILS_IMAGE_PNG)
36 #if defined(WINPR_UTILS_IMAGE_JPEG)
37 #define INT32 INT32_WINPR
42 #if defined(WINPR_UTILS_IMAGE_WEBP)
43 #include <webp/encode.h>
44 #include <webp/decode.h>
47 #if defined(WITH_LODEPNG)
50 #include <winpr/stream.h>
54 #define TAG WINPR_TAG("utils.image")
56 static SSIZE_T winpr_convert_from_jpeg(
const BYTE* comp_data,
size_t comp_data_bytes, UINT32* width,
57 UINT32* height, UINT32* bpp, BYTE** ppdecomp_data);
58 static SSIZE_T winpr_convert_from_png(
const BYTE* comp_data,
size_t comp_data_bytes, UINT32* width,
59 UINT32* height, UINT32* bpp, BYTE** ppdecomp_data);
60 static SSIZE_T winpr_convert_from_webp(
const BYTE* comp_data,
size_t comp_data_bytes, UINT32* width,
61 UINT32* height, UINT32* bpp, BYTE** ppdecomp_data);
68 Stream_Write_UINT8(s, bf->bfType[0]);
69 Stream_Write_UINT8(s, bf->bfType[1]);
70 Stream_Write_UINT32(s, bf->bfSize);
71 Stream_Write_UINT16(s, bf->bfReserved1);
72 Stream_Write_UINT16(s, bf->bfReserved2);
73 Stream_Write_UINT32(s, bf->bfOffBits);
79 static wLog* log = NULL;
87 Stream_Read_UINT8(s, bf->bfType[0]);
88 Stream_Read_UINT8(s, bf->bfType[1]);
89 Stream_Read_UINT32(s, bf->bfSize);
90 Stream_Read_UINT16(s, bf->bfReserved1);
91 Stream_Read_UINT16(s, bf->bfReserved2);
92 Stream_Read_UINT32(s, bf->bfOffBits);
96 WLog_Print(log, WLOG_ERROR,
"Invalid bitmap::bfSize=%" PRIu32
", require at least %" PRIuz,
101 if ((bf->bfType[0] !=
'B') || (bf->bfType[1] !=
'M'))
103 WLog_Print(log, WLOG_ERROR,
"Invalid bitmap header [%c%c], expected [BM]", bf->bfType[0],
107 return Stream_CheckAndLogRequiredCapacityWLog(log, s,
116 Stream_Write_UINT32(s, bi->biSize);
117 Stream_Write_INT32(s, bi->biWidth);
118 Stream_Write_INT32(s, bi->biHeight);
119 Stream_Write_UINT16(s, bi->biPlanes);
120 Stream_Write_UINT16(s, bi->biBitCount);
121 Stream_Write_UINT32(s, bi->biCompression);
122 Stream_Write_UINT32(s, bi->biSizeImage);
123 Stream_Write_INT32(s, bi->biXPelsPerMeter);
124 Stream_Write_INT32(s, bi->biYPelsPerMeter);
125 Stream_Write_UINT32(s, bi->biClrUsed);
126 Stream_Write_UINT32(s, bi->biClrImportant);
135 const size_t start = Stream_GetPosition(s);
136 Stream_Read_UINT32(s, bi->biSize);
137 Stream_Read_INT32(s, bi->biWidth);
138 Stream_Read_INT32(s, bi->biHeight);
139 Stream_Read_UINT16(s, bi->biPlanes);
140 Stream_Read_UINT16(s, bi->biBitCount);
141 Stream_Read_UINT32(s, bi->biCompression);
142 Stream_Read_UINT32(s, bi->biSizeImage);
143 Stream_Read_INT32(s, bi->biXPelsPerMeter);
144 Stream_Read_INT32(s, bi->biYPelsPerMeter);
145 Stream_Read_UINT32(s, bi->biClrUsed);
146 Stream_Read_UINT32(s, bi->biClrImportant);
148 if ((bi->biBitCount < 1) || (bi->biBitCount > 32))
150 WLog_WARN(TAG,
"invalid biBitCount=%" PRIu32, bi->biBitCount);
156 switch (bi->biCompression)
159 if (bi->biBitCount <= 8)
161 DWORD used = bi->biClrUsed;
163 used = (1 << bi->biBitCount) / 8;
164 offset +=
sizeof(
RGBQUAD) * used;
166 if (bi->biSizeImage == 0)
168 UINT32 stride = ((((bi->biWidth * bi->biBitCount) + 31) & ~31) >> 3);
169 bi->biSizeImage = abs(bi->biHeight) * stride;
173 offset +=
sizeof(DWORD) * 3;
176 WLog_ERR(TAG,
"unsupported biCompression %" PRIu32, bi->biCompression);
180 if (bi->biSizeImage == 0)
182 WLog_ERR(TAG,
"invalid biSizeImage %" PRIuz, bi->biSizeImage);
186 const size_t pos = Stream_GetPosition(s) - start;
187 if (bi->biSize < pos)
189 WLog_ERR(TAG,
"invalid biSize %" PRIuz
" < (actual) offset %" PRIuz, bi->biSize, pos);
194 return Stream_SafeSeek(s, bi->biSize - pos);
197 BYTE* winpr_bitmap_construct_header(
size_t width,
size_t height,
size_t bpp)
205 imgSize = width * height * (bpp / 8);
206 if ((width > INT32_MAX) || (height > INT32_MAX) || (bpp > UINT16_MAX) || (imgSize > UINT32_MAX))
209 s = Stream_New(NULL, WINPR_IMAGE_BMP_HEADER_LEN);
219 bi.biSizeImage = (UINT32)imgSize;
220 bf.bfSize = bf.bfOffBits + bi.biSizeImage;
221 bi.biWidth = (INT32)width;
222 bi.biHeight = -1 * (INT32)height;
224 bi.biBitCount = (UINT16)bpp;
225 bi.biCompression = BI_RGB;
226 bi.biXPelsPerMeter = (INT32)width;
227 bi.biYPelsPerMeter = (INT32)height;
229 bi.biClrImportant = 0;
232 switch (bi.biCompression)
235 if (bi.biBitCount <= 8)
237 DWORD used = bi.biClrUsed;
239 used = (1 << bi.biBitCount) / 8;
240 offset +=
sizeof(
RGBQUAD) * used;
244 offset +=
sizeof(DWORD) * 3;
250 if (!writeBitmapFileHeader(s, &bf))
253 if (!writeBitmapInfoHeader(s, &bi))
256 if (!Stream_EnsureRemainingCapacity(s, offset))
259 Stream_Zero(s, offset);
260 result = Stream_Buffer(s);
262 Stream_Free(s, result == 0);
270 WINPR_ATTR_MALLOC(free, 1)
271 static
void* winpr_bitmap_write_buffer(const BYTE* data,
size_t size, UINT32 width, UINT32 height,
272 UINT32 stride, UINT32 bpp, UINT32* pSize)
274 WINPR_ASSERT(data || (size == 0));
277 const size_t bpp_stride = 1ull * width * (bpp / 8);
278 if (bpp_stride > UINT32_MAX)
281 wStream* s = Stream_New(NULL, 1024);
284 stride = (UINT32)bpp_stride;
286 BYTE* bmp_header = winpr_bitmap_construct_header(width, height, bpp);
289 if (!Stream_EnsureRemainingCapacity(s, WINPR_IMAGE_BMP_HEADER_LEN))
291 Stream_Write(s, bmp_header, WINPR_IMAGE_BMP_HEADER_LEN);
293 if (!Stream_EnsureRemainingCapacity(s, 1ULL * stride * height))
296 for (
size_t y = 0; y < height; y++)
298 const BYTE* line = &data[stride * y];
300 Stream_Write(s, line, stride);
303 result = Stream_Buffer(s);
304 const size_t pos = Stream_GetPosition(s);
305 if (pos > UINT32_MAX)
307 *pSize = (UINT32)pos;
309 Stream_Free(s, result == NULL);
314 int winpr_bitmap_write(
const char* filename,
const BYTE* data,
size_t width,
size_t height,
317 return winpr_bitmap_write_ex(filename, data, 0, width, height, bpp);
320 int winpr_bitmap_write_ex(
const char* filename,
const BYTE* data,
size_t stride,
size_t width,
321 size_t height,
size_t bpp)
325 void* bmpdata = NULL;
326 const size_t bpp_stride = ((((width * bpp) + 31) & ~31) >> 3);
328 if ((stride > UINT32_MAX) || (width > UINT32_MAX) || (height > UINT32_MAX) ||
336 const size_t size = stride * 1ull * height;
337 bmpdata = winpr_bitmap_write_buffer(data, size, (UINT32)width, (UINT32)height, (UINT32)stride,
338 (UINT32)bpp, &bmpsize);
342 fp = winpr_fopen(filename,
"w+b");
345 WLog_ERR(TAG,
"failed to open file %s", filename);
349 if (fwrite(bmpdata, bmpsize, 1, fp) != 1)
360 static int write_and_free(
const char* filename,
void* data,
size_t size)
366 FILE* fp = winpr_fopen(filename,
"w+b");
370 size_t w = fwrite(data, 1, size, fp);
373 status = (w == size) ? 1 : -1;
379 int winpr_image_write(
wImage* image,
const char* filename)
382 return winpr_image_write_ex(image, image->type, filename);
385 int winpr_image_write_ex(
wImage* image, UINT32 format,
const char* filename)
390 void* data = winpr_image_write_buffer(image, format, &size);
393 return write_and_free(filename, data, size);
396 static int winpr_image_bitmap_read_buffer(
wImage* image,
const BYTE* buffer,
size_t size)
403 wStream* s = Stream_StaticConstInit(&sbuffer, buffer, size);
408 size_t bmpoffset = 0;
409 if (!readBitmapFileHeader(s, &bf) || !readBitmapInfoHeader(s, &bi, &bmpoffset))
412 if ((bf.bfType[0] !=
'B') || (bf.bfType[1] !=
'M'))
414 WLog_WARN(TAG,
"Invalid bitmap header %c%c", bf.bfType[0], bf.bfType[1]);
418 image->type = WINPR_IMAGE_BITMAP;
420 const size_t pos = Stream_GetPosition(s);
421 const size_t expect = bf.bfOffBits;
425 WLog_WARN(TAG,
"pos=%" PRIuz
", expected %" PRIuz
", offset=" PRIuz, pos, expect,
430 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, bi.biSizeImage))
435 WLog_WARN(TAG,
"bi.biWidth=%" PRId32, bi.biWidth);
439 image->width = (UINT32)bi.biWidth;
444 image->height = (UINT32)(-1 * bi.biHeight);
449 image->height = (UINT32)bi.biHeight;
452 if (image->height <= 0)
454 WLog_WARN(TAG,
"image->height=%" PRIu32, image->height);
458 image->bitsPerPixel = bi.biBitCount;
459 image->bytesPerPixel = (image->bitsPerPixel / 8UL);
460 const size_t bpp = (bi.biBitCount + 7UL) / 8UL;
461 image->scanline = (UINT32)(bi.biWidth * bpp);
462 const size_t bmpsize = 1ULL * image->scanline * image->height;
463 if (bmpsize != bi.biSizeImage)
464 WLog_WARN(TAG,
"bmpsize=%" PRIuz
" != bi.biSizeImage=%" PRIu32, bmpsize, bi.biSizeImage);
465 if (bi.biSizeImage < bmpsize)
468 image->data = (BYTE*)malloc(bi.biSizeImage);
474 Stream_Read(s, image->data, bi.biSizeImage);
477 BYTE* pDstData = &(image->data[(image->height - 1ull) * image->scanline]);
479 for (
size_t index = 0; index < image->height; index++)
481 Stream_Read(s, pDstData, image->scanline);
482 pDstData -= image->scanline;
498 int winpr_image_read(
wImage* image,
const char* filename)
502 FILE* fp = winpr_fopen(filename,
"rb");
505 WLog_ERR(TAG,
"failed to open file %s", filename);
509 (void)fseek(fp, 0, SEEK_END);
510 INT64 pos = _ftelli64(fp);
511 (void)fseek(fp, 0, SEEK_SET);
515 BYTE* buffer = malloc((
size_t)pos);
518 size_t r = fread(buffer, 1, (
size_t)pos, fp);
519 if (r == (
size_t)pos)
521 status = winpr_image_read_buffer(image, buffer, (
size_t)pos);
530 int winpr_image_read_buffer(
wImage* image,
const BYTE* buffer,
size_t size)
532 BYTE sig[12] = { 0 };
535 if (size <
sizeof(sig))
538 CopyMemory(sig, buffer,
sizeof(sig));
540 if ((sig[0] ==
'B') && (sig[1] ==
'M'))
542 image->type = WINPR_IMAGE_BITMAP;
543 status = winpr_image_bitmap_read_buffer(image, buffer, size);
545 else if ((sig[0] ==
'R') && (sig[1] ==
'I') && (sig[2] ==
'F') && (sig[3] ==
'F') &&
546 (sig[8] ==
'W') && (sig[9] ==
'E') && (sig[10] ==
'B') && (sig[11] ==
'P'))
548 image->type = WINPR_IMAGE_WEBP;
549 const SSIZE_T rc = winpr_convert_from_webp(buffer, size, &image->width, &image->height,
550 &image->bitsPerPixel, &image->data);
553 image->bytesPerPixel = (image->bitsPerPixel + 7) / 8;
554 image->scanline = image->width * image->bytesPerPixel;
558 else if ((sig[0] == 0xFF) && (sig[1] == 0xD8) && (sig[2] == 0xFF) && (sig[3] == 0xE0) &&
559 (sig[6] == 0x4A) && (sig[7] == 0x46) && (sig[8] == 0x49) && (sig[9] == 0x46) &&
562 image->type = WINPR_IMAGE_JPEG;
563 const SSIZE_T rc = winpr_convert_from_jpeg(buffer, size, &image->width, &image->height,
564 &image->bitsPerPixel, &image->data);
567 image->bytesPerPixel = (image->bitsPerPixel + 7) / 8;
568 image->scanline = image->width * image->bytesPerPixel;
572 else if ((sig[0] == 0x89) && (sig[1] ==
'P') && (sig[2] ==
'N') && (sig[3] ==
'G') &&
573 (sig[4] ==
'\r') && (sig[5] ==
'\n') && (sig[6] == 0x1A) && (sig[7] ==
'\n'))
575 image->type = WINPR_IMAGE_PNG;
576 const SSIZE_T rc = winpr_convert_from_png(buffer, size, &image->width, &image->height,
577 &image->bitsPerPixel, &image->data);
580 image->bytesPerPixel = (image->bitsPerPixel + 7) / 8;
581 image->scanline = image->width * image->bytesPerPixel;
589 wImage* winpr_image_new(
void)
599 void winpr_image_free(
wImage* image, BOOL bFreeBuffer)
610 static void* winpr_convert_to_jpeg(
const void* data,
size_t size, UINT32 width, UINT32 height,
611 UINT32 stride, UINT32 bpp, UINT32* pSize)
613 WINPR_ASSERT(data || (size == 0));
618 #if !defined(WINPR_UTILS_IMAGE_JPEG)
621 BYTE* outbuffer = NULL;
622 unsigned long outsize = 0;
623 struct jpeg_compress_struct cinfo = { 0 };
625 const size_t expect1 = 1ull * stride * height;
626 const size_t bytes = (bpp + 7) / 8;
627 const size_t expect2 = 1ull * width * height * bytes;
628 if (expect1 != expect2)
634 struct jpeg_error_mgr jerr = { 0 };
635 cinfo.err = jpeg_std_error(&jerr);
637 jpeg_create_compress(&cinfo);
638 jpeg_mem_dest(&cinfo, &outbuffer, &outsize);
640 cinfo.image_width = width;
641 cinfo.image_height = height;
642 WINPR_ASSERT(bpp <= INT32_MAX / 8);
643 cinfo.input_components = (int)(bpp + 7) / 8;
644 cinfo.in_color_space = (bpp > 24) ? JCS_EXT_BGRA : JCS_EXT_BGR;
645 cinfo.data_precision = 8;
647 jpeg_set_defaults(&cinfo);
648 jpeg_set_quality(&cinfo, 100, TRUE);
650 cinfo.comp_info[0].h_samp_factor = cinfo.comp_info[0].v_samp_factor = 1;
652 jpeg_start_compress(&cinfo, TRUE);
654 const JSAMPLE* cdata = data;
655 for (
size_t x = 0; x < height; x++)
657 WINPR_ASSERT(x * stride <= UINT32_MAX);
658 const JDIMENSION offset = (JDIMENSION)x * stride;
662 JSAMPLE* coffset = WINPR_CAST_CONST_PTR_AWAY(&cdata[offset], JSAMPLE*);
663 if (jpeg_write_scanlines(&cinfo, &coffset, 1) != 1)
668 jpeg_finish_compress(&cinfo);
669 jpeg_destroy_compress(&cinfo);
671 WINPR_ASSERT(outsize <= UINT32_MAX);
672 *pSize = (UINT32)outsize;
678 SSIZE_T winpr_convert_from_jpeg(
const BYTE* comp_data,
size_t comp_data_bytes, UINT32* width,
679 UINT32* height, UINT32* bpp, BYTE** ppdecomp_data)
682 WINPR_ASSERT(comp_data || (comp_data_bytes == 0));
684 WINPR_ASSERT(height);
686 WINPR_ASSERT(ppdecomp_data);
688 #if !defined(WINPR_UTILS_IMAGE_JPEG)
691 struct jpeg_decompress_struct cinfo = { 0 };
692 struct jpeg_error_mgr jerr;
694 BYTE* decomp_data = NULL;
696 cinfo.err = jpeg_std_error(&jerr);
697 jpeg_create_decompress(&cinfo);
698 jpeg_mem_src(&cinfo, comp_data, comp_data_bytes);
700 if (jpeg_read_header(&cinfo, 1) != JPEG_HEADER_OK)
703 cinfo.out_color_space = cinfo.num_components > 3 ? JCS_EXT_RGBA : JCS_EXT_BGR;
705 *width = cinfo.image_width;
706 *height = cinfo.image_height;
707 *bpp = cinfo.num_components * 8;
709 if (!jpeg_start_decompress(&cinfo))
712 size_t stride = 1ULL * cinfo.image_width * cinfo.num_components;
714 decomp_data = calloc(stride, cinfo.image_height);
717 while (cinfo.output_scanline < cinfo.image_height)
719 JSAMPROW row = &decomp_data[cinfo.output_scanline * stride];
720 if (jpeg_read_scanlines(&cinfo, &row, 1) != 1)
723 const size_t ssize = stride * cinfo.image_height;
724 WINPR_ASSERT(ssize < SSIZE_MAX);
725 size = (SSIZE_T)ssize;
727 jpeg_finish_decompress(&cinfo);
730 jpeg_destroy_decompress(&cinfo);
731 *ppdecomp_data = decomp_data;
736 static void* winpr_convert_to_webp(
const void* data,
size_t size, UINT32 width, UINT32 height,
737 UINT32 stride, UINT32 bpp, UINT32* pSize)
739 WINPR_ASSERT(data || (size == 0));
744 #if !defined(WINPR_UTILS_IMAGE_WEBP)
748 uint8_t* pDstData = NULL;
749 WINPR_ASSERT(width <= INT32_MAX);
750 WINPR_ASSERT(height <= INT32_MAX);
751 WINPR_ASSERT(stride <= INT32_MAX);
755 dstSize = WebPEncodeLosslessBGRA(data, (
int)width, (
int)height, (
int)stride, &pDstData);
758 dstSize = WebPEncodeLosslessBGR(data, (
int)width, (
int)height, (
int)stride, &pDstData);
764 void* rc = malloc(dstSize);
767 memcpy(rc, pDstData, dstSize);
769 WINPR_ASSERT(dstSize <= UINT32_MAX);
770 *pSize = (UINT32)dstSize;
777 SSIZE_T winpr_convert_from_webp(
const BYTE* comp_data,
size_t comp_data_bytes, UINT32* width,
778 UINT32* height, UINT32* bpp, BYTE** ppdecomp_data)
780 WINPR_ASSERT(comp_data || (comp_data_bytes == 0));
782 WINPR_ASSERT(height);
784 WINPR_ASSERT(ppdecomp_data);
789 *ppdecomp_data = NULL;
790 #if !defined(WINPR_UTILS_IMAGE_WEBP)
796 uint8_t* dst = WebPDecodeBGRA(comp_data, comp_data_bytes, &w, &h);
797 if (!dst || (w < 0) || (h < 0))
806 *ppdecomp_data = dst;
811 #if defined(WINPR_UTILS_IMAGE_PNG)
812 struct png_mem_encode
818 static void png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
821 struct png_mem_encode* p =
822 (
struct png_mem_encode*)png_get_io_ptr(png_ptr);
823 size_t nsize = p->size + length;
827 p->buffer = realloc(p->buffer, nsize);
829 p->buffer = malloc(nsize);
832 png_error(png_ptr,
"Write Error");
835 memcpy(p->buffer + p->size, data, length);
840 static void png_flush(png_structp png_ptr)
844 static SSIZE_T save_png_to_buffer(UINT32 bpp, UINT32 width, UINT32 height,
const uint8_t* data,
845 size_t size,
void** pDstData)
848 png_structp png_ptr = NULL;
849 png_infop info_ptr = NULL;
850 png_byte** row_pointers = NULL;
851 struct png_mem_encode state = { 0 };
855 if (!data || (size == 0))
858 WINPR_ASSERT(pDstData);
860 const size_t bytes_per_pixel = (bpp + 7) / 8;
861 const size_t bytes_per_row = width * bytes_per_pixel;
862 if (size < bytes_per_row * height)
866 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
871 info_ptr = png_create_info_struct(png_ptr);
872 if (info_ptr == NULL)
876 if (setjmp(png_jmpbuf(png_ptr)))
880 int colorType = PNG_COLOR_TYPE_PALETTE;
882 colorType = PNG_COLOR_TYPE_RGB;
884 colorType = PNG_COLOR_TYPE_RGB;
886 colorType = PNG_COLOR_TYPE_RGBA;
888 png_set_IHDR(png_ptr, info_ptr, width, height, 8, colorType, PNG_INTERLACE_NONE,
889 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
892 row_pointers = png_malloc(png_ptr, height *
sizeof(png_byte*));
893 for (
size_t y = 0; y < height; ++y)
895 uint8_t* row = png_malloc(png_ptr,
sizeof(uint8_t) * bytes_per_row);
896 row_pointers[y] = (png_byte*)row;
897 for (
size_t x = 0; x < width; ++x)
911 png_set_write_fn(png_ptr, &state, png_write_data, png_flush);
912 png_set_rows(png_ptr, info_ptr, row_pointers);
913 png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_BGR, NULL);
916 for (
size_t y = 0; y < height; y++)
917 png_free(png_ptr, row_pointers[y]);
918 png_free(png_ptr, row_pointers);
921 if (state.size > SSIZE_MAX)
923 rc = (SSIZE_T)state.size;
924 *pDstData = state.buffer;
926 png_destroy_write_struct(&png_ptr, &info_ptr);
936 png_uint_32 current_pos;
937 } MEMORY_READER_STATE;
939 static void read_data_memory(png_structp png_ptr, png_bytep data,
size_t length)
941 MEMORY_READER_STATE* f = png_get_io_ptr(png_ptr);
942 if (length > (f->bufsize - f->current_pos))
943 png_error(png_ptr,
"read error in read_data_memory (loadpng)");
946 memcpy(data, f->buffer + f->current_pos, length);
947 f->current_pos += length;
951 static void* winpr_read_png_from_buffer(
const void* data,
size_t SrcSize,
size_t* pSize,
952 UINT32* pWidth, UINT32* pHeight, UINT32* pBpp)
955 png_uint_32 width = 0;
956 png_uint_32 height = 0;
959 int interlace_type = 0;
960 int transforms = PNG_TRANSFORM_IDENTITY;
961 MEMORY_READER_STATE memory_reader_state = { 0 };
962 png_bytepp row_pointers = NULL;
963 png_infop info_ptr = NULL;
964 if (SrcSize > UINT32_MAX)
967 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
970 info_ptr = png_create_info_struct(png_ptr);
974 memory_reader_state.buffer = WINPR_CAST_CONST_PTR_AWAY(data, png_bytep);
975 memory_reader_state.bufsize = (UINT32)SrcSize;
976 memory_reader_state.current_pos = 0;
978 png_set_read_fn(png_ptr, &memory_reader_state, read_data_memory);
980 transforms |= PNG_TRANSFORM_BGR;
981 png_read_png(png_ptr, info_ptr, transforms, NULL);
983 if (png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type,
987 WINPR_ASSERT(bit_depth >= 0);
988 const png_byte channelcount = png_get_channels(png_ptr, info_ptr);
989 const size_t bpp = channelcount * (size_t)bit_depth;
991 row_pointers = png_get_rows(png_ptr, info_ptr);
994 const size_t stride = 1ULL * width * bpp / 8ull;
995 const size_t png_stride = png_get_rowbytes(png_ptr, info_ptr);
996 const size_t size = 1ULL * width * height * bpp / 8ull;
997 const size_t copybytes = stride > png_stride ? png_stride : stride;
1003 for (png_uint_32 i = 0; i < height; i++)
1005 memcpy(cur, row_pointers[i], copybytes);
1011 WINPR_ASSERT(bpp <= UINT32_MAX);
1012 *pBpp = (UINT32)bpp;
1017 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
1022 static void* winpr_convert_to_png(
const void* data,
size_t size, UINT32 width, UINT32 height,
1023 UINT32 stride, UINT32 bpp, UINT32* pSize)
1025 WINPR_ASSERT(data || (size == 0));
1026 WINPR_ASSERT(pSize);
1030 #if defined(WINPR_UTILS_IMAGE_PNG)
1032 SSIZE_T rc = save_png_to_buffer(bpp, width, height, data, size, &dst);
1035 *pSize = (UINT32)rc;
1037 #elif defined(WITH_LODEPNG)
1046 rc = lodepng_encode32(&dst, &dstsize, data, width, height);
1049 rc = lodepng_encode24(&dst, &dstsize, data, width, height);
1056 *pSize = (UINT32)dstsize;
1064 SSIZE_T winpr_convert_from_png(
const BYTE* comp_data,
size_t comp_data_bytes, UINT32* width,
1065 UINT32* height, UINT32* bpp, BYTE** ppdecomp_data)
1067 #if defined(WINPR_UTILS_IMAGE_PNG)
1070 winpr_read_png_from_buffer(comp_data, comp_data_bytes, &len, width, height, bpp);
1071 if (!*ppdecomp_data)
1073 return (SSIZE_T)len;
1074 #elif defined(WITH_LODEPNG)
1076 return lodepng_decode32((
unsigned char**)ppdecomp_data, width, height, comp_data,
1083 BOOL winpr_image_format_is_supported(UINT32 format)
1087 case WINPR_IMAGE_BITMAP:
1088 #if defined(WINPR_UTILS_IMAGE_PNG) || defined(WITH_LODEPNG)
1089 case WINPR_IMAGE_PNG:
1091 #if defined(WINPR_UTILS_IMAGE_JPEG)
1092 case WINPR_IMAGE_JPEG:
1094 #if defined(WINPR_UTILS_IMAGE_WEBP)
1095 case WINPR_IMAGE_WEBP:
1103 static BYTE* convert(
const wImage* image,
size_t* pstride, UINT32 flags)
1105 WINPR_ASSERT(image);
1106 WINPR_ASSERT(pstride);
1109 if (image->bitsPerPixel < 24)
1112 const size_t stride = image->width * 4ull;
1113 BYTE* data = calloc(stride, image->height);
1116 for (
size_t y = 0; y < image->height; y++)
1118 const BYTE* srcLine = &image->data[image->scanline * y];
1119 BYTE* dstLine = &data[stride * y];
1120 if (image->bitsPerPixel == 32)
1121 memcpy(dstLine, srcLine, stride);
1124 for (
size_t x = 0; x < image->width; x++)
1126 const BYTE* src = &srcLine[image->bytesPerPixel * x];
1127 BYTE* dst = &dstLine[4ull * x];
1144 static BOOL compare_byte_relaxed(BYTE a, BYTE b, UINT32 flags)
1148 if ((flags & WINPR_IMAGE_CMP_FUZZY) != 0)
1150 const int diff = abs((
int)a) - abs((
int)b);
1163 static BOOL compare_pixel(
const BYTE* pa,
const BYTE* pb, UINT32 flags)
1168 if (!compare_byte_relaxed(*pa++, *pb++, flags))
1170 if (!compare_byte_relaxed(*pa++, *pb++, flags))
1172 if (!compare_byte_relaxed(*pa++, *pb++, flags))
1174 if ((flags & WINPR_IMAGE_CMP_IGNORE_ALPHA) == 0)
1176 if (!compare_byte_relaxed(*pa++, *pb++, flags))
1182 BOOL winpr_image_equal(
const wImage* imageA,
const wImage* imageB, UINT32 flags)
1184 if (imageA == imageB)
1186 if (!imageA || !imageB)
1189 if (imageA->height != imageB->height)
1191 if (imageA->width != imageB->width)
1194 if ((flags & WINPR_IMAGE_CMP_IGNORE_DEPTH) == 0)
1196 if (imageA->bitsPerPixel != imageB->bitsPerPixel)
1198 if (imageA->bytesPerPixel != imageB->bytesPerPixel)
1205 BYTE* dataA = convert(imageA, &astride, flags);
1206 BYTE* dataB = convert(imageA, &bstride, flags);
1207 if (dataA && dataB && (astride == bstride))
1210 for (
size_t y = 0; y < imageA->height; y++)
1212 const BYTE* lineA = &dataA[astride * y];
1213 const BYTE* lineB = &dataB[bstride * y];
1215 for (
size_t x = 0; x < imageA->width; x++)
1217 const BYTE* pa = &lineA[x * 4ull];
1218 const BYTE* pb = &lineB[x * 4ull];
1220 if (!compare_pixel(pa, pb, flags))
1230 const char* winpr_image_format_mime(UINT32 format)
1234 case WINPR_IMAGE_BITMAP:
1236 case WINPR_IMAGE_PNG:
1238 case WINPR_IMAGE_WEBP:
1239 return "image/webp";
1240 case WINPR_IMAGE_JPEG:
1241 return "image/jpeg";
1247 const char* winpr_image_format_extension(UINT32 format)
1251 case WINPR_IMAGE_BITMAP:
1253 case WINPR_IMAGE_PNG:
1255 case WINPR_IMAGE_WEBP:
1257 case WINPR_IMAGE_JPEG:
1264 void* winpr_image_write_buffer(
wImage* image, UINT32 format,
size_t* psize)
1266 WINPR_ASSERT(image);
1269 case WINPR_IMAGE_BITMAP:
1272 size_t size = 1ull * image->height * image->scanline;
1273 void* data = winpr_bitmap_write_buffer(image->data, size, image->width, image->height,
1274 image->scanline, image->bitsPerPixel, &outsize);
1278 case WINPR_IMAGE_WEBP:
1281 size_t size = 1ull * image->height * image->scanline;
1282 void* data = winpr_convert_to_webp(image->data, size, image->width, image->height,
1283 image->scanline, image->bitsPerPixel, &outsize);
1287 case WINPR_IMAGE_JPEG:
1290 size_t size = 1ull * image->height * image->scanline;
1291 void* data = winpr_convert_to_jpeg(image->data, size, image->width, image->height,
1292 image->scanline, image->bitsPerPixel, &outsize);
1296 case WINPR_IMAGE_PNG:
1299 size_t size = 1ull * image->height * image->scanline;
1300 void* data = winpr_convert_to_png(image->data, size, image->width, image->height,
1301 image->scanline, image->bitsPerPixel, &outsize);