mirror of
https://github.com/morgan9e/FreeRDP
synced 2026-04-15 00:44:19 +09:00
libwinpr-utils: add png support
This commit is contained in:
@@ -23,8 +23,12 @@
|
||||
#include <winpr/winpr.h>
|
||||
#include <winpr/wtypes.h>
|
||||
|
||||
#define WINPR_IMAGE_BITMAP 0
|
||||
#define WINPR_IMAGE_PNG 1
|
||||
|
||||
struct _wImage
|
||||
{
|
||||
int type;
|
||||
int width;
|
||||
int height;
|
||||
BYTE* data;
|
||||
|
||||
@@ -35,6 +35,10 @@ set(${MODULE_PREFIX}_COLLECTIONS_SRCS
|
||||
collections/StreamPool.c
|
||||
collections/MessageQueue.c
|
||||
collections/MessagePipe.c)
|
||||
|
||||
set(${MODULE_PREFIX}_LODEPNG_SRCS
|
||||
lodepng/lodepng.c
|
||||
lodepng/lodepng.h)
|
||||
|
||||
set(${MODULE_PREFIX}_TRIO_SRCS
|
||||
trio/strio.h
|
||||
@@ -83,15 +87,17 @@ set(${MODULE_PREFIX}_SRCS
|
||||
ssl.c)
|
||||
|
||||
if (ANDROID)
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||
endif()
|
||||
|
||||
winpr_module_add(${${MODULE_PREFIX}_SRCS}
|
||||
${${MODULE_PREFIX}_COLLECTIONS_SRCS}
|
||||
${${MODULE_PREFIX}_LODEPNG_SRCS}
|
||||
${${MODULE_PREFIX}_TRIO_SRCS}
|
||||
${${MODULE_PREFIX}_WLOG_SRCS})
|
||||
|
||||
winpr_include_directory_add(
|
||||
"lodepng"
|
||||
"trio"
|
||||
"."
|
||||
${ZLIB_INCLUDE_DIRS}
|
||||
|
||||
@@ -25,6 +25,8 @@
|
||||
|
||||
#include <winpr/image.h>
|
||||
|
||||
#include "lodepng/lodepng.h"
|
||||
|
||||
#include "../log.h"
|
||||
#define TAG WINPR_TAG("utils.image")
|
||||
|
||||
@@ -124,31 +126,77 @@ int winpr_bitmap_write(const char* filename, BYTE* data, int width, int height,
|
||||
|
||||
int winpr_image_write(wImage* image, const char* filename)
|
||||
{
|
||||
return winpr_bitmap_write(filename, image->data, image->width, image->height, image->bitsPerPixel);
|
||||
int status = -1;
|
||||
|
||||
if (image->type == WINPR_IMAGE_BITMAP)
|
||||
{
|
||||
status = winpr_bitmap_write(filename, image->data, image->width, image->height, image->bitsPerPixel);
|
||||
}
|
||||
else
|
||||
{
|
||||
int lodepng_status;
|
||||
|
||||
lodepng_status = lodepng_encode32_file(filename, image->data, image->width, image->height);
|
||||
|
||||
status = (lodepng_status) ? -1 : 1;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
int winpr_image_read(wImage* image, const char* filename)
|
||||
int winpr_image_png_read_fp(wImage* image, FILE* fp)
|
||||
{
|
||||
int size;
|
||||
BYTE* data;
|
||||
UINT32 width;
|
||||
UINT32 height;
|
||||
int lodepng_status;
|
||||
|
||||
fseek(fp, 0, SEEK_END);
|
||||
size = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
|
||||
data = (BYTE*) malloc(size);
|
||||
|
||||
if (!data)
|
||||
return -1;
|
||||
|
||||
fread((void*) data, size, 1, fp);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
lodepng_status = lodepng_decode32(&(image->data), &width, &height, data, size);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int winpr_image_bitmap_read_fp(wImage* image, FILE* fp)
|
||||
{
|
||||
FILE* fp;
|
||||
int index;
|
||||
BOOL vFlip;
|
||||
BYTE* pDstData;
|
||||
WINPR_BITMAP_FILE_HEADER bf;
|
||||
WINPR_BITMAP_INFO_HEADER bi;
|
||||
|
||||
fp = fopen(filename, "r+b");
|
||||
|
||||
if (!fp)
|
||||
{
|
||||
WLog_ERR(TAG, "failed to open file %s", filename);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fread((void*) &bf, sizeof(WINPR_BITMAP_FILE_HEADER), 1, fp);
|
||||
|
||||
if ((bf.bfType[0] != 'B') || (bf.bfType[1] != 'M'))
|
||||
return -1;
|
||||
|
||||
image->type = WINPR_IMAGE_BITMAP;
|
||||
|
||||
fread((void*) &bi, sizeof(WINPR_BITMAP_INFO_HEADER), 1, fp);
|
||||
|
||||
if (ftell(fp) != bf.bfOffBits)
|
||||
@@ -198,6 +246,42 @@ int winpr_image_read(wImage* image, const char* filename)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int winpr_image_read(wImage* image, const char* filename)
|
||||
{
|
||||
FILE* fp;
|
||||
BYTE sig[8];
|
||||
int status = -1;
|
||||
|
||||
fp = fopen(filename, "r+b");
|
||||
|
||||
if (!fp)
|
||||
{
|
||||
WLog_ERR(TAG, "failed to open file %s", filename);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fread((void*) &sig, sizeof(sig), 1, fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
|
||||
if ((sig[0] == 'B') && (sig[1] == 'M'))
|
||||
{
|
||||
image->type = WINPR_IMAGE_BITMAP;
|
||||
status = winpr_image_bitmap_read_fp(image, fp);
|
||||
}
|
||||
else if ((sig[0] == 0x89) && (sig[1] == 'P') && (sig[2] == 'N') && (sig[3] == 'G') &&
|
||||
(sig[4] == '\r') && (sig[5] == '\n') && (sig[6] == 0x1A) && (sig[7] == '\n'))
|
||||
{
|
||||
image->type = WINPR_IMAGE_PNG;
|
||||
status = winpr_image_png_read_fp(image, fp);
|
||||
}
|
||||
else
|
||||
{
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
wImage* winpr_image_new()
|
||||
{
|
||||
wImage* image;
|
||||
|
||||
5834
winpr/libwinpr/utils/lodepng/lodepng.c
Normal file
5834
winpr/libwinpr/utils/lodepng/lodepng.c
Normal file
File diff suppressed because it is too large
Load Diff
1563
winpr/libwinpr/utils/lodepng/lodepng.h
Normal file
1563
winpr/libwinpr/utils/lodepng/lodepng.h
Normal file
File diff suppressed because it is too large
Load Diff
@@ -6,6 +6,7 @@ set(${MODULE_PREFIX}_DRIVER ${MODULE_NAME}.c)
|
||||
|
||||
set(${MODULE_PREFIX}_TESTS
|
||||
TestIni.c
|
||||
TestImage.c
|
||||
TestQueue.c
|
||||
TestPrint.c
|
||||
TestPubSub.c
|
||||
|
||||
9
winpr/libwinpr/utils/test/TestImage.c
Normal file
9
winpr/libwinpr/utils/test/TestImage.c
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
#include <winpr/crt.h>
|
||||
#include <winpr/image.h>
|
||||
|
||||
int TestImage(int argc, char* argv[])
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user