[winpr,image] add winpr_bitmap_write_ex

Added a helper function writing a bitmap from an image that has a stride
that is not width * <bytes per pixel>
This commit is contained in:
akallabeth 2022-12-16 10:08:02 +01:00 committed by Martin Fleisz
parent 9d6a06a197
commit 784becd946
2 changed files with 20 additions and 3 deletions

View File

@ -83,6 +83,8 @@ extern "C"
WINPR_API int winpr_bitmap_write(const char* filename, const BYTE* data, size_t width,
size_t height, size_t bpp);
WINPR_API int winpr_bitmap_write_ex(const char* filename, const BYTE* data, size_t stride,
size_t width, size_t height, size_t bpp);
WINPR_API BYTE* winpr_bitmap_construct_header(size_t width, size_t height, size_t bpp);
WINPR_API int winpr_image_write(wImage* image, const char* filename);

View File

@ -151,10 +151,19 @@ fail:
int winpr_bitmap_write(const char* filename, const BYTE* data, size_t width, size_t height,
size_t bpp)
{
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)
{
FILE* fp;
BYTE* bmp_header = NULL;
size_t img_size = width * height * (bpp / 8);
const size_t bpp_stride = width * (bpp / 8);
if (stride == 0)
stride = bpp_stride;
int ret = -1;
fp = winpr_fopen(filename, "w+b");
@ -169,10 +178,16 @@ int winpr_bitmap_write(const char* filename, const BYTE* data, size_t width, siz
if (!bmp_header)
goto fail;
if (fwrite(bmp_header, WINPR_IMAGE_BMP_HEADER_LEN, 1, fp) != 1 ||
fwrite((const void*)data, img_size, 1, fp) != 1)
if (fwrite(bmp_header, WINPR_IMAGE_BMP_HEADER_LEN, 1, fp) != 1)
goto fail;
for (size_t y = 0; y < height; y++)
{
const void* line = &data[stride * y];
if (fwrite(line, bpp_stride, 1, fp) != 1)
goto fail;
}
ret = 1;
fail:
fclose(fp);