2014-09-04 00:20:50 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Image Utils
|
|
|
|
*
|
|
|
|
* Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2016-01-14 02:09:03 +03:00
|
|
|
* Copyright 2016 Inuvika Inc.
|
|
|
|
* Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com>
|
2014-09-04 00:20:50 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 12:08:00 +03:00
|
|
|
#include <winpr/config.h>
|
2014-09-04 00:20:50 +04:00
|
|
|
|
2017-08-29 10:09:38 +03:00
|
|
|
#include <winpr/wtypes.h>
|
2014-09-04 00:20:50 +04:00
|
|
|
#include <winpr/crt.h>
|
2021-05-25 20:27:13 +03:00
|
|
|
#include <winpr/file.h>
|
2014-09-04 00:20:50 +04:00
|
|
|
|
|
|
|
#include <winpr/image.h>
|
|
|
|
|
2023-07-26 16:55:06 +03:00
|
|
|
#if defined(WITH_LODEPNG)
|
|
|
|
#include <lodepng.h>
|
|
|
|
#endif
|
2017-11-23 14:00:52 +03:00
|
|
|
#include <winpr/stream.h>
|
2014-09-28 19:02:39 +04:00
|
|
|
|
2014-09-09 18:35:04 +04:00
|
|
|
#include "../log.h"
|
|
|
|
#define TAG WINPR_TAG("utils.image")
|
|
|
|
|
2017-11-23 14:00:52 +03:00
|
|
|
static BOOL writeBitmapFileHeader(wStream* s, const WINPR_BITMAP_FILE_HEADER* bf)
|
|
|
|
{
|
|
|
|
if (!Stream_EnsureRemainingCapacity(s, sizeof(WINPR_BITMAP_FILE_HEADER)))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
Stream_Write_UINT8(s, bf->bfType[0]);
|
|
|
|
Stream_Write_UINT8(s, bf->bfType[1]);
|
|
|
|
Stream_Write_UINT32(s, bf->bfSize);
|
|
|
|
Stream_Write_UINT16(s, bf->bfReserved1);
|
|
|
|
Stream_Write_UINT16(s, bf->bfReserved2);
|
|
|
|
Stream_Write_UINT32(s, bf->bfOffBits);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL readBitmapFileHeader(wStream* s, WINPR_BITMAP_FILE_HEADER* bf)
|
|
|
|
{
|
2022-04-19 15:29:17 +03:00
|
|
|
if (!s || !bf || (!Stream_CheckAndLogRequiredLength(TAG, s, sizeof(WINPR_BITMAP_FILE_HEADER))))
|
2017-11-23 14:00:52 +03:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
Stream_Read_UINT8(s, bf->bfType[0]);
|
|
|
|
Stream_Read_UINT8(s, bf->bfType[1]);
|
|
|
|
Stream_Read_UINT32(s, bf->bfSize);
|
|
|
|
Stream_Read_UINT16(s, bf->bfReserved1);
|
|
|
|
Stream_Read_UINT16(s, bf->bfReserved2);
|
|
|
|
Stream_Read_UINT32(s, bf->bfOffBits);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL writeBitmapInfoHeader(wStream* s, const WINPR_BITMAP_INFO_HEADER* bi)
|
|
|
|
{
|
|
|
|
if (!Stream_EnsureRemainingCapacity(s, sizeof(WINPR_BITMAP_INFO_HEADER)))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
Stream_Write_UINT32(s, bi->biSize);
|
2021-06-16 13:58:21 +03:00
|
|
|
Stream_Write_INT32(s, bi->biWidth);
|
|
|
|
Stream_Write_INT32(s, bi->biHeight);
|
2017-11-23 14:00:52 +03:00
|
|
|
Stream_Write_UINT16(s, bi->biPlanes);
|
|
|
|
Stream_Write_UINT16(s, bi->biBitCount);
|
|
|
|
Stream_Write_UINT32(s, bi->biCompression);
|
|
|
|
Stream_Write_UINT32(s, bi->biSizeImage);
|
2021-06-16 13:58:21 +03:00
|
|
|
Stream_Write_INT32(s, bi->biXPelsPerMeter);
|
|
|
|
Stream_Write_INT32(s, bi->biYPelsPerMeter);
|
2017-11-23 14:00:52 +03:00
|
|
|
Stream_Write_UINT32(s, bi->biClrUsed);
|
|
|
|
Stream_Write_UINT32(s, bi->biClrImportant);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL readBitmapInfoHeader(wStream* s, WINPR_BITMAP_INFO_HEADER* bi)
|
|
|
|
{
|
2022-04-19 15:29:17 +03:00
|
|
|
if (!s || !bi || (!Stream_CheckAndLogRequiredLength(TAG, s, sizeof(WINPR_BITMAP_INFO_HEADER))))
|
2017-11-23 14:00:52 +03:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
Stream_Read_UINT32(s, bi->biSize);
|
|
|
|
Stream_Read_INT32(s, bi->biWidth);
|
|
|
|
Stream_Read_INT32(s, bi->biHeight);
|
|
|
|
Stream_Read_UINT16(s, bi->biPlanes);
|
|
|
|
Stream_Read_UINT16(s, bi->biBitCount);
|
|
|
|
Stream_Read_UINT32(s, bi->biCompression);
|
|
|
|
Stream_Read_UINT32(s, bi->biSizeImage);
|
|
|
|
Stream_Read_INT32(s, bi->biXPelsPerMeter);
|
|
|
|
Stream_Read_INT32(s, bi->biYPelsPerMeter);
|
|
|
|
Stream_Read_UINT32(s, bi->biClrUsed);
|
|
|
|
Stream_Read_UINT32(s, bi->biClrImportant);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-06-16 15:43:07 +03:00
|
|
|
BYTE* winpr_bitmap_construct_header(size_t width, size_t height, size_t bpp)
|
2014-09-04 00:20:50 +04:00
|
|
|
{
|
2021-10-14 09:42:49 +03:00
|
|
|
BYTE* result = NULL;
|
2022-10-10 15:53:27 +03:00
|
|
|
WINPR_BITMAP_FILE_HEADER bf = { 0 };
|
|
|
|
WINPR_BITMAP_INFO_HEADER bi = { 0 };
|
2021-10-14 09:42:49 +03:00
|
|
|
wStream* s;
|
2021-07-28 16:18:03 +03:00
|
|
|
size_t imgSize;
|
|
|
|
|
|
|
|
imgSize = width * height * (bpp / 8);
|
|
|
|
if ((width > INT32_MAX) || (height > INT32_MAX) || (bpp > UINT16_MAX) || (imgSize > UINT32_MAX))
|
2021-06-16 15:43:07 +03:00
|
|
|
return NULL;
|
2014-09-04 00:20:50 +04:00
|
|
|
|
2021-10-14 09:42:49 +03:00
|
|
|
s = Stream_New(NULL, WINPR_IMAGE_BMP_HEADER_LEN);
|
|
|
|
if (!s)
|
2020-06-01 09:33:12 +03:00
|
|
|
return NULL;
|
|
|
|
|
2014-09-04 02:46:57 +04:00
|
|
|
bf.bfType[0] = 'B';
|
|
|
|
bf.bfType[1] = 'M';
|
|
|
|
bf.bfReserved1 = 0;
|
|
|
|
bf.bfReserved2 = 0;
|
2021-06-16 15:43:07 +03:00
|
|
|
bf.bfOffBits = (UINT32)sizeof(WINPR_BITMAP_FILE_HEADER) + sizeof(WINPR_BITMAP_INFO_HEADER);
|
2021-07-28 16:18:03 +03:00
|
|
|
bi.biSizeImage = (UINT32)imgSize;
|
2014-09-04 02:46:57 +04:00
|
|
|
bf.bfSize = bf.bfOffBits + bi.biSizeImage;
|
2021-06-16 15:43:07 +03:00
|
|
|
bi.biWidth = (INT32)width;
|
|
|
|
bi.biHeight = -1 * (INT32)height;
|
2014-09-04 02:46:57 +04:00
|
|
|
bi.biPlanes = 1;
|
2021-06-16 15:43:07 +03:00
|
|
|
bi.biBitCount = (UINT16)bpp;
|
2014-09-04 02:46:57 +04:00
|
|
|
bi.biCompression = 0;
|
2021-06-16 15:43:07 +03:00
|
|
|
bi.biXPelsPerMeter = (INT32)width;
|
|
|
|
bi.biYPelsPerMeter = (INT32)height;
|
2014-09-04 02:46:57 +04:00
|
|
|
bi.biClrUsed = 0;
|
|
|
|
bi.biClrImportant = 0;
|
2021-06-16 15:43:07 +03:00
|
|
|
bi.biSize = (UINT32)sizeof(WINPR_BITMAP_INFO_HEADER);
|
2014-09-04 00:20:50 +04:00
|
|
|
|
2021-10-14 09:42:49 +03:00
|
|
|
if (!writeBitmapFileHeader(s, &bf))
|
2017-11-23 14:00:52 +03:00
|
|
|
goto fail;
|
2014-09-04 00:20:50 +04:00
|
|
|
|
2021-10-14 09:42:49 +03:00
|
|
|
if (!writeBitmapInfoHeader(s, &bi))
|
2017-11-23 14:00:52 +03:00
|
|
|
goto fail;
|
|
|
|
|
2021-10-14 09:42:49 +03:00
|
|
|
result = Stream_Buffer(s);
|
2020-06-01 09:33:12 +03:00
|
|
|
fail:
|
2021-10-14 09:42:49 +03:00
|
|
|
Stream_Free(s, result == 0);
|
|
|
|
return result;
|
2020-06-01 09:33:12 +03:00
|
|
|
}
|
2014-09-04 00:20:50 +04:00
|
|
|
|
2020-06-01 09:33:12 +03:00
|
|
|
/**
|
|
|
|
* Refer to "Compressed Image File Formats: JPEG, PNG, GIF, XBM, BMP" book
|
|
|
|
*/
|
|
|
|
|
2021-06-16 15:43:07 +03:00
|
|
|
int winpr_bitmap_write(const char* filename, const BYTE* data, size_t width, size_t height,
|
|
|
|
size_t bpp)
|
2022-12-16 12:08:02 +03:00
|
|
|
{
|
|
|
|
return winpr_bitmap_write_ex(filename, data, 0, width, height, bpp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int winpr_bitmap_write_ex(const char* filename, const BYTE* data, size_t stride, size_t width,
|
|
|
|
size_t height, size_t bpp)
|
2020-06-01 09:33:12 +03:00
|
|
|
{
|
2023-04-27 10:23:51 +03:00
|
|
|
FILE* fp = NULL;
|
2020-06-01 09:33:12 +03:00
|
|
|
BYTE* bmp_header = NULL;
|
2022-12-16 12:08:02 +03:00
|
|
|
const size_t bpp_stride = width * (bpp / 8);
|
|
|
|
|
|
|
|
if (stride == 0)
|
|
|
|
stride = bpp_stride;
|
2020-06-01 09:33:12 +03:00
|
|
|
|
|
|
|
int ret = -1;
|
2021-05-31 12:42:03 +03:00
|
|
|
fp = winpr_fopen(filename, "w+b");
|
2020-06-01 09:33:12 +03:00
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "failed to open file %s", filename);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bmp_header = winpr_bitmap_construct_header(width, height, bpp);
|
|
|
|
if (!bmp_header)
|
2020-12-03 09:22:17 +03:00
|
|
|
goto fail;
|
2017-11-23 14:00:52 +03:00
|
|
|
|
2022-12-16 12:08:02 +03:00
|
|
|
if (fwrite(bmp_header, WINPR_IMAGE_BMP_HEADER_LEN, 1, fp) != 1)
|
2017-11-23 14:00:52 +03:00
|
|
|
goto fail;
|
|
|
|
|
2022-12-16 12:08:02 +03:00
|
|
|
for (size_t y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
const void* line = &data[stride * y];
|
|
|
|
if (fwrite(line, bpp_stride, 1, fp) != 1)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2017-11-23 14:00:52 +03:00
|
|
|
ret = 1;
|
|
|
|
fail:
|
2023-04-27 10:23:51 +03:00
|
|
|
if (fp)
|
|
|
|
fclose(fp);
|
2020-06-01 09:33:12 +03:00
|
|
|
free(bmp_header);
|
2015-06-26 16:58:01 +03:00
|
|
|
return ret;
|
2014-09-04 02:46:57 +04:00
|
|
|
}
|
2014-09-04 00:20:50 +04:00
|
|
|
|
2014-09-04 02:46:57 +04:00
|
|
|
int winpr_image_write(wImage* image, const char* filename)
|
|
|
|
{
|
2014-09-28 19:02:39 +04:00
|
|
|
int status = -1;
|
|
|
|
|
|
|
|
if (image->type == WINPR_IMAGE_BITMAP)
|
|
|
|
{
|
2017-11-23 14:00:52 +03:00
|
|
|
status = winpr_bitmap_write(filename, image->data, image->width, image->height,
|
|
|
|
image->bitsPerPixel);
|
2014-09-28 19:02:39 +04:00
|
|
|
}
|
2023-07-26 16:55:06 +03:00
|
|
|
#if defined(WITH_LODEPNG)
|
2014-09-28 19:02:39 +04:00
|
|
|
else
|
|
|
|
{
|
2021-06-16 15:43:07 +03:00
|
|
|
unsigned lodepng_status;
|
2014-09-28 19:02:39 +04:00
|
|
|
lodepng_status = lodepng_encode32_file(filename, image->data, image->width, image->height);
|
|
|
|
status = (lodepng_status) ? -1 : 1;
|
|
|
|
}
|
2023-07-26 16:55:06 +03:00
|
|
|
#endif
|
2014-09-28 19:02:39 +04:00
|
|
|
return status;
|
2014-09-04 02:46:57 +04:00
|
|
|
}
|
|
|
|
|
2023-07-26 16:55:06 +03:00
|
|
|
#if defined(WITH_LODEPNG)
|
2017-11-23 14:00:52 +03:00
|
|
|
static int winpr_image_png_read_fp(wImage* image, FILE* fp)
|
2014-09-28 19:02:39 +04:00
|
|
|
{
|
2017-08-11 11:07:46 +03:00
|
|
|
INT64 size;
|
2014-09-28 19:02:39 +04:00
|
|
|
BYTE* data;
|
|
|
|
UINT32 width;
|
|
|
|
UINT32 height;
|
2021-07-28 16:18:03 +03:00
|
|
|
unsigned lodepng_status;
|
2017-08-11 11:07:46 +03:00
|
|
|
_fseeki64(fp, 0, SEEK_END);
|
|
|
|
size = _ftelli64(fp);
|
|
|
|
_fseeki64(fp, 0, SEEK_SET);
|
2021-06-16 15:43:07 +03:00
|
|
|
if (size < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
data = (BYTE*)malloc((size_t)size);
|
2014-09-28 19:02:39 +04:00
|
|
|
|
|
|
|
if (!data)
|
|
|
|
return -1;
|
|
|
|
|
2021-06-16 15:43:07 +03:00
|
|
|
if (fread((void*)data, (size_t)size, 1, fp) != 1)
|
2015-08-28 12:07:25 +03:00
|
|
|
{
|
|
|
|
free(data);
|
2015-06-26 16:58:01 +03:00
|
|
|
return -1;
|
2015-08-28 12:07:25 +03:00
|
|
|
}
|
2014-09-28 19:02:39 +04:00
|
|
|
|
2021-06-16 15:43:07 +03:00
|
|
|
lodepng_status = lodepng_decode32(&(image->data), &width, &height, data, (size_t)size);
|
2014-09-28 19:02:39 +04:00
|
|
|
free(data);
|
|
|
|
|
|
|
|
if (lodepng_status)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
image->width = width;
|
|
|
|
image->height = height;
|
|
|
|
image->bitsPerPixel = 32;
|
|
|
|
image->bytesPerPixel = 4;
|
|
|
|
image->scanline = image->bytesPerPixel * image->width;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-09-02 16:02:55 +03:00
|
|
|
static int winpr_image_png_read_buffer(wImage* image, const BYTE* buffer, size_t size)
|
2014-09-30 00:08:08 +04:00
|
|
|
{
|
|
|
|
UINT32 width;
|
|
|
|
UINT32 height;
|
2021-07-28 16:18:03 +03:00
|
|
|
unsigned lodepng_status = lodepng_decode32(&(image->data), &width, &height, buffer, size);
|
2014-09-30 00:08:08 +04:00
|
|
|
|
|
|
|
if (lodepng_status)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
image->width = width;
|
|
|
|
image->height = height;
|
|
|
|
image->bitsPerPixel = 32;
|
|
|
|
image->bytesPerPixel = 4;
|
|
|
|
image->scanline = image->bytesPerPixel * image->width;
|
|
|
|
return 1;
|
|
|
|
}
|
2023-07-26 16:55:06 +03:00
|
|
|
#endif
|
2014-09-30 00:08:08 +04:00
|
|
|
|
2017-11-23 14:00:52 +03:00
|
|
|
static int winpr_image_bitmap_read_fp(wImage* image, FILE* fp)
|
2014-09-04 02:46:57 +04:00
|
|
|
{
|
2017-11-23 14:00:52 +03:00
|
|
|
int rc = -1;
|
2021-07-28 16:18:03 +03:00
|
|
|
UINT32 index;
|
2014-09-04 02:46:57 +04:00
|
|
|
BOOL vFlip;
|
|
|
|
BYTE* pDstData;
|
2017-11-23 14:00:52 +03:00
|
|
|
wStream* s;
|
2022-10-10 15:53:27 +03:00
|
|
|
wStream sbuffer = { 0 };
|
|
|
|
BYTE buffer[sizeof(WINPR_BITMAP_FILE_HEADER) + sizeof(WINPR_BITMAP_INFO_HEADER)] = { 0 };
|
|
|
|
WINPR_BITMAP_FILE_HEADER bf = { 0 };
|
|
|
|
WINPR_BITMAP_INFO_HEADER bi = { 0 };
|
2017-12-12 12:35:02 +03:00
|
|
|
|
|
|
|
if (!image || !fp)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
image->data = NULL;
|
|
|
|
|
2022-10-10 15:53:27 +03:00
|
|
|
s = Stream_StaticInit(&sbuffer, buffer, sizeof(buffer));
|
2014-09-04 02:46:57 +04:00
|
|
|
|
2017-11-23 14:00:52 +03:00
|
|
|
if (!s)
|
2015-06-26 16:58:01 +03:00
|
|
|
return -1;
|
2014-09-04 02:46:57 +04:00
|
|
|
|
2017-11-23 14:00:52 +03:00
|
|
|
if (fread(Stream_Buffer(s), Stream_Capacity(s), 1, fp) != 1)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!readBitmapFileHeader(s, &bf) || !readBitmapInfoHeader(s, &bi))
|
|
|
|
goto fail;
|
|
|
|
|
2014-09-04 02:46:57 +04:00
|
|
|
if ((bf.bfType[0] != 'B') || (bf.bfType[1] != 'M'))
|
2017-11-23 14:00:52 +03:00
|
|
|
goto fail;
|
2014-09-04 00:20:50 +04:00
|
|
|
|
2014-09-28 19:02:39 +04:00
|
|
|
image->type = WINPR_IMAGE_BITMAP;
|
|
|
|
|
2017-08-11 11:07:46 +03:00
|
|
|
if (_ftelli64(fp) != bf.bfOffBits)
|
|
|
|
_fseeki64(fp, bf.bfOffBits, SEEK_SET);
|
2014-09-04 02:46:57 +04:00
|
|
|
|
2021-07-28 16:18:03 +03:00
|
|
|
if (bi.biWidth < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
image->width = (UINT32)bi.biWidth;
|
2014-09-04 02:46:57 +04:00
|
|
|
|
|
|
|
if (bi.biHeight < 0)
|
|
|
|
{
|
|
|
|
vFlip = FALSE;
|
2021-07-28 16:18:03 +03:00
|
|
|
image->height = (UINT32)(-1 * bi.biHeight);
|
2014-09-04 02:46:57 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vFlip = TRUE;
|
2021-07-28 16:18:03 +03:00
|
|
|
image->height = (UINT32)bi.biHeight;
|
2014-09-04 02:46:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
image->bitsPerPixel = bi.biBitCount;
|
|
|
|
image->bytesPerPixel = (image->bitsPerPixel / 8);
|
2015-04-19 13:29:17 +03:00
|
|
|
image->scanline = (bi.biSizeImage / image->height);
|
2019-11-06 17:24:51 +03:00
|
|
|
image->data = (BYTE*)malloc(bi.biSizeImage);
|
2014-09-04 02:46:57 +04:00
|
|
|
|
|
|
|
if (!image->data)
|
2017-11-23 14:00:52 +03:00
|
|
|
goto fail;
|
2014-09-04 02:46:57 +04:00
|
|
|
|
|
|
|
if (!vFlip)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
if (fread((void*)image->data, bi.biSizeImage, 1, fp) != 1)
|
2017-11-23 14:00:52 +03:00
|
|
|
goto fail;
|
2014-09-04 02:46:57 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pDstData = &(image->data[(image->height - 1) * image->scanline]);
|
|
|
|
|
|
|
|
for (index = 0; index < image->height; index++)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
if (fread((void*)pDstData, image->scanline, 1, fp) != 1)
|
2017-11-23 14:00:52 +03:00
|
|
|
goto fail;
|
|
|
|
|
2014-09-04 02:46:57 +04:00
|
|
|
pDstData -= image->scanline;
|
|
|
|
}
|
|
|
|
}
|
2014-09-04 00:20:50 +04:00
|
|
|
|
2017-11-23 14:00:52 +03:00
|
|
|
rc = 1;
|
|
|
|
fail:
|
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
free(image->data);
|
|
|
|
image->data = NULL;
|
|
|
|
}
|
|
|
|
|
2014-09-04 00:20:50 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2014-09-04 02:46:57 +04:00
|
|
|
|
2019-09-02 16:02:55 +03:00
|
|
|
static int winpr_image_bitmap_read_buffer(wImage* image, const BYTE* buffer, size_t size)
|
2014-09-30 00:08:08 +04:00
|
|
|
{
|
2017-11-23 14:00:52 +03:00
|
|
|
int rc = -1;
|
2021-07-28 16:18:03 +03:00
|
|
|
UINT32 index;
|
2014-09-30 00:08:08 +04:00
|
|
|
BOOL vFlip;
|
|
|
|
BYTE* pDstData;
|
|
|
|
WINPR_BITMAP_FILE_HEADER bf;
|
|
|
|
WINPR_BITMAP_INFO_HEADER bi;
|
2022-02-01 13:06:22 +03:00
|
|
|
wStream sbuffer = { 0 };
|
|
|
|
wStream* s = Stream_StaticConstInit(&sbuffer, buffer, size);
|
2014-09-30 00:08:08 +04:00
|
|
|
|
2017-11-23 14:00:52 +03:00
|
|
|
if (!s)
|
|
|
|
return -1;
|
2014-09-30 00:08:08 +04:00
|
|
|
|
2017-11-23 14:00:52 +03:00
|
|
|
if (!readBitmapFileHeader(s, &bf) || !readBitmapInfoHeader(s, &bi))
|
|
|
|
goto fail;
|
2014-09-30 00:08:08 +04:00
|
|
|
|
|
|
|
if ((bf.bfType[0] != 'B') || (bf.bfType[1] != 'M'))
|
2017-11-23 14:00:52 +03:00
|
|
|
goto fail;
|
2014-09-30 00:08:08 +04:00
|
|
|
|
|
|
|
image->type = WINPR_IMAGE_BITMAP;
|
|
|
|
|
2020-04-29 16:47:00 +03:00
|
|
|
if (Stream_GetPosition(s) > bf.bfOffBits)
|
|
|
|
goto fail;
|
|
|
|
if (!Stream_SafeSeek(s, bf.bfOffBits - Stream_GetPosition(s)))
|
|
|
|
goto fail;
|
2023-01-24 14:19:56 +03:00
|
|
|
if (!Stream_CheckAndLogRequiredCapacity(TAG, s, bi.biSizeImage))
|
2017-11-23 14:00:52 +03:00
|
|
|
goto fail;
|
2014-09-30 00:08:08 +04:00
|
|
|
|
2021-07-28 16:18:03 +03:00
|
|
|
if (bi.biWidth < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
image->width = (UINT32)bi.biWidth;
|
2014-09-30 00:08:08 +04:00
|
|
|
|
|
|
|
if (bi.biHeight < 0)
|
|
|
|
{
|
|
|
|
vFlip = FALSE;
|
2021-07-28 16:18:03 +03:00
|
|
|
image->height = (UINT32)(-1 * bi.biHeight);
|
2014-09-30 00:08:08 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vFlip = TRUE;
|
2021-07-28 16:18:03 +03:00
|
|
|
image->height = (UINT32)bi.biHeight;
|
2014-09-30 00:08:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
image->bitsPerPixel = bi.biBitCount;
|
|
|
|
image->bytesPerPixel = (image->bitsPerPixel / 8);
|
2015-04-20 16:13:23 +03:00
|
|
|
image->scanline = (bi.biSizeImage / image->height);
|
2019-11-06 17:24:51 +03:00
|
|
|
image->data = (BYTE*)malloc(bi.biSizeImage);
|
2014-09-30 00:08:08 +04:00
|
|
|
|
|
|
|
if (!image->data)
|
2017-11-23 14:00:52 +03:00
|
|
|
goto fail;
|
2014-09-30 00:08:08 +04:00
|
|
|
|
|
|
|
if (!vFlip)
|
2017-11-23 14:00:52 +03:00
|
|
|
Stream_Read(s, image->data, bi.biSizeImage);
|
2014-09-30 00:08:08 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
pDstData = &(image->data[(image->height - 1) * image->scanline]);
|
|
|
|
|
|
|
|
for (index = 0; index < image->height; index++)
|
|
|
|
{
|
2017-11-23 14:00:52 +03:00
|
|
|
Stream_Read(s, pDstData, image->scanline);
|
2014-09-30 00:08:08 +04:00
|
|
|
pDstData -= image->scanline;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-23 14:00:52 +03:00
|
|
|
rc = 1;
|
|
|
|
fail:
|
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
free(image->data);
|
|
|
|
image->data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
2014-09-30 00:08:08 +04:00
|
|
|
}
|
|
|
|
|
2014-09-28 19:02:39 +04:00
|
|
|
int winpr_image_read(wImage* image, const char* filename)
|
|
|
|
{
|
|
|
|
FILE* fp;
|
|
|
|
BYTE sig[8];
|
|
|
|
int status = -1;
|
2021-05-25 20:27:13 +03:00
|
|
|
|
|
|
|
fp = winpr_fopen(filename, "rb");
|
2014-09-28 19:02:39 +04:00
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "failed to open file %s", filename);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (fread((void*)&sig, sizeof(sig), 1, fp) != 1 || _fseeki64(fp, 0, SEEK_SET) < 0)
|
2015-06-26 16:58:01 +03:00
|
|
|
{
|
|
|
|
fclose(fp);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-09-28 19:02:39 +04:00
|
|
|
|
|
|
|
if ((sig[0] == 'B') && (sig[1] == 'M'))
|
|
|
|
{
|
|
|
|
image->type = WINPR_IMAGE_BITMAP;
|
|
|
|
status = winpr_image_bitmap_read_fp(image, fp);
|
|
|
|
}
|
2023-07-26 16:55:06 +03:00
|
|
|
#if defined(WITH_LODEPNG)
|
2014-09-28 19:02:39 +04:00
|
|
|
else if ((sig[0] == 0x89) && (sig[1] == 'P') && (sig[2] == 'N') && (sig[3] == 'G') &&
|
2017-11-23 14:00:52 +03:00
|
|
|
(sig[4] == '\r') && (sig[5] == '\n') && (sig[6] == 0x1A) && (sig[7] == '\n'))
|
2014-09-28 19:02:39 +04:00
|
|
|
{
|
|
|
|
image->type = WINPR_IMAGE_PNG;
|
|
|
|
status = winpr_image_png_read_fp(image, fp);
|
|
|
|
}
|
2023-07-26 16:55:06 +03:00
|
|
|
#endif
|
2014-09-28 19:02:39 +04:00
|
|
|
|
2017-11-23 14:00:52 +03:00
|
|
|
fclose(fp);
|
2014-09-28 19:02:39 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-06-16 15:43:07 +03:00
|
|
|
int winpr_image_read_buffer(wImage* image, const BYTE* buffer, size_t size)
|
2014-09-30 00:08:08 +04:00
|
|
|
{
|
|
|
|
BYTE sig[8];
|
|
|
|
int status = -1;
|
|
|
|
|
|
|
|
if (size < 8)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
CopyMemory(sig, buffer, 8);
|
|
|
|
|
|
|
|
if ((sig[0] == 'B') && (sig[1] == 'M'))
|
|
|
|
{
|
|
|
|
image->type = WINPR_IMAGE_BITMAP;
|
|
|
|
status = winpr_image_bitmap_read_buffer(image, buffer, size);
|
|
|
|
}
|
2023-07-26 16:55:06 +03:00
|
|
|
#if defined(WITH_LODEPNG)
|
2014-09-30 00:08:08 +04:00
|
|
|
else if ((sig[0] == 0x89) && (sig[1] == 'P') && (sig[2] == 'N') && (sig[3] == 'G') &&
|
2017-11-23 14:00:52 +03:00
|
|
|
(sig[4] == '\r') && (sig[5] == '\n') && (sig[6] == 0x1A) && (sig[7] == '\n'))
|
2014-09-30 00:08:08 +04:00
|
|
|
{
|
|
|
|
image->type = WINPR_IMAGE_PNG;
|
|
|
|
status = winpr_image_png_read_buffer(image, buffer, size);
|
|
|
|
}
|
2023-07-26 16:55:06 +03:00
|
|
|
#endif
|
2014-09-30 00:08:08 +04:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2017-11-23 14:00:52 +03:00
|
|
|
wImage* winpr_image_new(void)
|
2014-09-04 02:46:57 +04:00
|
|
|
{
|
|
|
|
wImage* image;
|
2019-11-06 17:24:51 +03:00
|
|
|
image = (wImage*)calloc(1, sizeof(wImage));
|
2014-09-04 02:46:57 +04:00
|
|
|
|
|
|
|
if (!image)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
void winpr_image_free(wImage* image, BOOL bFreeBuffer)
|
|
|
|
{
|
|
|
|
if (!image)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (bFreeBuffer)
|
|
|
|
free(image->data);
|
|
|
|
|
|
|
|
free(image);
|
|
|
|
}
|