From 7d310e4897887909099afa1cb9333dd85eae3bd4 Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Sun, 26 Nov 2017 13:43:05 +0000 Subject: Use imv_bitmap for bitmaps intead of FIBITMAP --- src/bitmap.c | 22 ++++++++++++++++++++++ src/bitmap.h | 13 +++++++++++++ src/image.c | 13 ++++--------- src/image.h | 4 ++-- src/imv.c | 8 ++++---- src/loader.c | 16 ++++++++++++++-- 6 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 src/bitmap.c create mode 100644 src/bitmap.h diff --git a/src/bitmap.c b/src/bitmap.c new file mode 100644 index 0000000..b37d0e4 --- /dev/null +++ b/src/bitmap.c @@ -0,0 +1,22 @@ +#include "bitmap.h" + +#include +#include + +struct imv_bitmap *imv_bitmap_clone(struct imv_bitmap *bmp) +{ + struct imv_bitmap *copy = malloc(sizeof(struct imv_bitmap)); + const size_t num_bytes = 4 * bmp->width * bmp->height; + copy->width = bmp->width; + copy->height = bmp->height; + copy->height = bmp->height; + copy->data = malloc(num_bytes); + memcpy(copy->data, bmp->data, num_bytes); + return copy; +} + +void imv_bitmap_free(struct imv_bitmap *bmp) +{ + free(bmp->data); + free(bmp); +} diff --git a/src/bitmap.h b/src/bitmap.h new file mode 100644 index 0000000..0f08959 --- /dev/null +++ b/src/bitmap.h @@ -0,0 +1,13 @@ +#ifndef IMV_BITMAP_H +#define IMV_BITMAP_H + +struct imv_bitmap { + int width; + int height; + unsigned char *data; +}; + +struct imv_bitmap *imv_bitmap_clone(struct imv_bitmap *bmp); +void imv_bitmap_free(struct imv_bitmap *bmp); + +#endif diff --git a/src/image.c b/src/image.c index 155a57b..617d45c 100644 --- a/src/image.c +++ b/src/image.c @@ -41,10 +41,10 @@ void imv_image_free(struct imv_image *image) free(image); } -int imv_image_set_bitmap(struct imv_image *image, FIBITMAP *bmp) +int imv_image_set_bitmap(struct imv_image *image, struct imv_bitmap *bmp) { - image->width = FreeImage_GetWidth(bmp); - image->height = FreeImage_GetHeight(bmp); + image->width = bmp->width; + image->height = bmp->height; /* figure out how many chunks are needed, and create them */ if(image->num_chunks > 0) { @@ -100,21 +100,16 @@ int imv_image_set_bitmap(struct imv_image *image, FIBITMAP *bmp) return 1; } - BYTE* pixels = malloc(4 * image->width * image->height); - FreeImage_ConvertToRawBits(pixels, bmp, 4 * image->width, 32, - FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, TRUE); - for(int y = 0; y < image->num_chunks_tall; ++y) { for(int x = 0; x < image->num_chunks_wide; ++x) { ptrdiff_t offset = 4 * x * image->chunk_width + y * 4 * image->width * image->chunk_height; - BYTE* addr = pixels + offset; + unsigned char* addr = bmp->data + offset; SDL_UpdateTexture(image->chunks[x + y * image->num_chunks_wide], NULL, addr, 4 * image->width); } } - free(pixels); return 0; } diff --git a/src/image.h b/src/image.h index c67e55f..e2fda4f 100644 --- a/src/image.h +++ b/src/image.h @@ -1,8 +1,8 @@ #ifndef IMV_IMAGE_H #define IMV_IMAGE_H +#include "bitmap.h" #include -#include struct imv_image; @@ -13,7 +13,7 @@ struct imv_image *imv_image_create(SDL_Renderer *r); void imv_image_free(struct imv_image *image); /* Updates the image to contain the data in the bitmap parameter */ -int imv_image_set_bitmap(struct imv_image *image, FIBITMAP *bmp); +int imv_image_set_bitmap(struct imv_image *image, struct imv_bitmap *bmp); /* Draw the image at the given position with the given scale */ void imv_image_draw(struct imv_image *image, int x, int y, double scale); diff --git a/src/imv.c b/src/imv.c index ef03fb8..642a060 100644 --- a/src/imv.c +++ b/src/imv.c @@ -644,11 +644,11 @@ static void handle_event(struct imv *imv, SDL_Event *event) if(event->type == imv->events.NEW_IMAGE) { /* new image to display */ - FIBITMAP *bmp = event->user.data1; + struct imv_bitmap *bmp = event->user.data1; imv_image_set_bitmap(imv->image, bmp); - imv->current_image.width = FreeImage_GetWidth(bmp); - imv->current_image.height = FreeImage_GetHeight(bmp); - FreeImage_Unload(bmp); + imv->current_image.width = bmp->width; + imv->current_image.height = bmp->height; + imv_bitmap_free(bmp); imv->need_redraw = true; imv->need_rescale |= event->user.code; return; diff --git a/src/loader.c b/src/loader.c index 3090c4d..f8876dd 100644 --- a/src/loader.c +++ b/src/loader.c @@ -1,4 +1,5 @@ #include "loader.h" +#include "bitmap.h" #include #include #include @@ -47,6 +48,17 @@ static int is_thread_cancelled(void) return sigismember(&sigmask, SIGUSR1); } +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->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); + return bmp; +} + struct imv_loader *imv_loader_create(void) { struct imv_loader *ldr = malloc(sizeof(struct imv_loader)); @@ -294,7 +306,7 @@ static void *bg_new_img(void *data) SDL_Event event; SDL_zero(event); event.type = ldr->new_image_event; - event.user.data1 = FreeImage_Clone(bmp); + event.user.data1 = to_imv_bitmap(bmp); event.user.code = 1; /* is a new image */ SDL_PushEvent(&event); @@ -399,7 +411,7 @@ static void *bg_next_frame(void *data) SDL_Event event; SDL_zero(event); event.type = ldr->new_image_event; - event.user.data1 = FreeImage_Clone(ldr->bmp); + event.user.data1 = to_imv_bitmap(ldr->bmp); event.user.code = 0; /* not a new image */ SDL_PushEvent(&event); -- cgit v1.2.3