From 2d5bb23bede92adf10f496f62e38c6611591e923 Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Sun, 20 Jan 2019 23:03:37 +0000 Subject: bitmap: Support multiple pixel formats --- src/backend_freeimage.c | 1 + src/bitmap.c | 2 +- src/bitmap.h | 6 ++++++ src/image.c | 15 ++++++++++++++- 4 files changed, 22 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/backend_freeimage.c b/src/backend_freeimage.c index 73f8f9a..6b89324 100644 --- a/src/backend_freeimage.c +++ b/src/backend_freeimage.c @@ -42,6 +42,7 @@ static struct imv_bitmap *to_imv_bitmap(FIBITMAP *in_bmp) struct imv_bitmap *bmp = malloc(sizeof(struct imv_bitmap)); bmp->width = FreeImage_GetWidth(in_bmp); bmp->height = FreeImage_GetHeight(in_bmp); + bmp->format = IMV_ARGB; bmp->data = malloc(4 * bmp->width * bmp->height); FreeImage_ConvertToRawBits(bmp->data, in_bmp, 4 * bmp->width, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, TRUE); diff --git a/src/bitmap.c b/src/bitmap.c index b37d0e4..ffae72c 100644 --- a/src/bitmap.c +++ b/src/bitmap.c @@ -9,7 +9,7 @@ struct imv_bitmap *imv_bitmap_clone(struct imv_bitmap *bmp) const size_t num_bytes = 4 * bmp->width * bmp->height; copy->width = bmp->width; copy->height = bmp->height; - copy->height = bmp->height; + copy->format = bmp->format; copy->data = malloc(num_bytes); memcpy(copy->data, bmp->data, num_bytes); return copy; diff --git a/src/bitmap.h b/src/bitmap.h index 0f08959..61ef0ce 100644 --- a/src/bitmap.h +++ b/src/bitmap.h @@ -1,9 +1,15 @@ #ifndef IMV_BITMAP_H #define IMV_BITMAP_H +enum imv_pixelformat { + IMV_ARGB, + IMV_ABGR, +}; + struct imv_bitmap { int width; int height; + enum imv_pixelformat format; unsigned char *data; }; diff --git a/src/image.c b/src/image.c index 8934cbc..5d0546e 100644 --- a/src/image.c +++ b/src/image.c @@ -44,6 +44,18 @@ void imv_image_free(struct imv_image *image) free(image); } +static int convert_pixelformat(enum imv_pixelformat fmt) +{ + if (fmt == IMV_ARGB) { + return SDL_PIXELFORMAT_ARGB8888; + } else if (fmt == IMV_ABGR) { + return SDL_PIXELFORMAT_ABGR8888; + } else { + fprintf(stderr, "Unknown pixel format. Defaulting to ARGB\n"); + return SDL_PIXELFORMAT_ARGB8888; + } +} + int imv_image_set_bitmap(struct imv_image *image, struct imv_bitmap *bmp) { image->width = bmp->width; @@ -73,6 +85,7 @@ int imv_image_set_bitmap(struct imv_image *image, struct imv_bitmap *bmp) image->num_chunks = image->num_chunks_wide * image->num_chunks_tall; image->chunks = malloc(sizeof(SDL_Texture*) * image->num_chunks); + const int format = convert_pixelformat(bmp->format); int failed_at = -1; for(int y = 0; y < image->num_chunks_tall; ++y) { for(int x = 0; x < image->num_chunks_wide; ++x) { @@ -80,7 +93,7 @@ int imv_image_set_bitmap(struct imv_image *image, struct imv_bitmap *bmp) const int is_last_v_chunk = (y == image->num_chunks_tall - 1); image->chunks[x + y * image->num_chunks_wide] = SDL_CreateTexture(image->renderer, - SDL_PIXELFORMAT_ARGB8888, + format, SDL_TEXTUREACCESS_STATIC, is_last_h_chunk ? image->last_chunk_width : image->chunk_width, is_last_v_chunk ? image->last_chunk_height : image->chunk_height); -- cgit v1.2.3