aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHarry Jeffery <harry@exec64.co.uk>2019-01-20 23:03:37 +0000
committerHarry Jeffery <harry@exec64.co.uk>2019-01-29 22:25:05 +0000
commit2d5bb23bede92adf10f496f62e38c6611591e923 (patch)
tree7215a948866202de6b17b86a31feb8bf4bdb582a /src
parentc987d66cef2205fc1cf4284104bd8bcc6eb092d0 (diff)
downloadimv-2d5bb23bede92adf10f496f62e38c6611591e923.tar.gz
bitmap: Support multiple pixel formats
Diffstat (limited to 'src')
-rw-r--r--src/backend_freeimage.c1
-rw-r--r--src/bitmap.c2
-rw-r--r--src/bitmap.h6
-rw-r--r--src/image.c15
4 files changed, 22 insertions, 2 deletions
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);