winpr: image: add API to construct bmp header

This commit is contained in:
Kobi Mizrachi 2020-06-01 09:33:12 +03:00 committed by akallabeth
parent 44c50ff1d5
commit 920acd4c0e
2 changed files with 42 additions and 23 deletions

View File

@ -66,6 +66,8 @@ typedef struct _WINPR_BITMAP_CORE_HEADER WINPR_BITMAP_CORE_HEADER;
#define WINPR_IMAGE_BITMAP 0 #define WINPR_IMAGE_BITMAP 0
#define WINPR_IMAGE_PNG 1 #define WINPR_IMAGE_PNG 1
#define WINPR_IMAGE_BMP_HEADER_LEN 54
struct _wImage struct _wImage
{ {
int type; int type;
@ -85,6 +87,7 @@ extern "C"
WINPR_API int winpr_bitmap_write(const char* filename, const BYTE* data, int width, int height, WINPR_API int winpr_bitmap_write(const char* filename, const BYTE* data, int width, int height,
int bpp); int bpp);
WINPR_API BYTE* winpr_bitmap_construct_header(int width, int height, int bpp);
WINPR_API int winpr_image_write(wImage* image, const char* filename); WINPR_API int winpr_image_write(wImage* image, const char* filename);
WINPR_API int winpr_image_read(wImage* image, const char* filename); WINPR_API int winpr_image_read(wImage* image, const char* filename);

View File

@ -100,24 +100,18 @@ static BOOL readBitmapInfoHeader(wStream* s, WINPR_BITMAP_INFO_HEADER* bi)
return TRUE; return TRUE;
} }
/** BYTE* winpr_bitmap_construct_header(int width, int height, int bpp)
* Refer to "Compressed Image File Formats: JPEG, PNG, GIF, XBM, BMP" book
*/
int winpr_bitmap_write(const char* filename, const BYTE* data, int width, int height, int bpp)
{ {
FILE* fp;
WINPR_BITMAP_FILE_HEADER bf; WINPR_BITMAP_FILE_HEADER bf;
WINPR_BITMAP_INFO_HEADER bi; WINPR_BITMAP_INFO_HEADER bi;
wStream* s; wStream s;
int ret = -1; BYTE* buffer = NULL;
fp = fopen(filename, "w+b");
if (!fp) buffer = malloc(WINPR_IMAGE_BMP_HEADER_LEN);
{ if (!buffer)
WLog_ERR(TAG, "failed to open file %s", filename); return NULL;
return -1;
} Stream_StaticInit(&s, buffer, WINPR_IMAGE_BMP_HEADER_LEN);
bf.bfType[0] = 'B'; bf.bfType[0] = 'B';
bf.bfType[1] = 'M'; bf.bfType[1] = 'M';
@ -136,27 +130,49 @@ int winpr_bitmap_write(const char* filename, const BYTE* data, int width, int he
bi.biClrUsed = 0; bi.biClrUsed = 0;
bi.biClrImportant = 0; bi.biClrImportant = 0;
bi.biSize = sizeof(WINPR_BITMAP_INFO_HEADER); bi.biSize = sizeof(WINPR_BITMAP_INFO_HEADER);
s = Stream_New(NULL, sizeof(WINPR_BITMAP_FILE_HEADER) + sizeof(WINPR_BITMAP_INFO_HEADER));
if (!s) if (!writeBitmapFileHeader(&s, &bf))
goto fail; goto fail;
if (!writeBitmapFileHeader(s, &bf)) if (!writeBitmapInfoHeader(&s, &bi))
goto fail; goto fail;
if (!writeBitmapInfoHeader(s, &bi)) return buffer;
goto fail; fail:
return NULL;
}
Stream_SealLength(s); /**
* Refer to "Compressed Image File Formats: JPEG, PNG, GIF, XBM, BMP" book
*/
if (fwrite(Stream_Buffer(s), Stream_Length(s), 1, fp) != 1 || int winpr_bitmap_write(const char* filename, const BYTE* data, int width, int height, int bpp)
fwrite((void*)data, bi.biSizeImage, 1, fp) != 1) {
FILE* fp;
BYTE* bmp_header = NULL;
UINT32 img_size = width * height * (bpp / 8);
int ret = -1;
fp = fopen(filename, "w+b");
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)
return -1;
if (fwrite(bmp_header, WINPR_IMAGE_BMP_HEADER_LEN, 1, fp) != 1 ||
fwrite((void*)data, img_size, 1, fp) != 1)
goto fail; goto fail;
ret = 1; ret = 1;
fail: fail:
fclose(fp); fclose(fp);
Stream_Free(s, TRUE); free(bmp_header);
return ret; return ret;
} }