aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarry Jeffery <harry@exec64.co.uk>2015-11-06 14:17:26 +0000
committerHarry Jeffery <harry@exec64.co.uk>2015-11-06 14:17:26 +0000
commit77dfcacf38b14ce4c5a8223b40343c2bcea54dde (patch)
tree99a8fa8ac5c2d2616ca0e263bccba58f63f8e581
parentf4d54f4982f4f5e5dceb72e72a1980f1eb63e0b8 (diff)
downloadimv-77dfcacf38b14ce4c5a8223b40343c2bcea54dde.tar.gz
Load images using freeimage
-rw-r--r--Makefile2
-rw-r--r--freeimage.c24
-rw-r--r--loader.c30
-rw-r--r--main.c4
4 files changed, 27 insertions, 33 deletions
diff --git a/Makefile b/Makefile
index e8d49a1..58f9efc 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
.PHONY: clean
CFLAGS = -g -W -Wall -std=c11 `sdl2-config --cflags`
-LDFLAGS = `sdl2-config --libs` -lpng -ljpeg -ltiff
+LDFLAGS = `sdl2-config --libs` -lpng -ljpeg -ltiff -lfreeimage
TARGET = imv
SOURCES = $(wildcard *.c)
diff --git a/freeimage.c b/freeimage.c
new file mode 100644
index 0000000..6b62751
--- /dev/null
+++ b/freeimage.c
@@ -0,0 +1,24 @@
+#include <SDL2/SDL.h>
+#include <FreeImage.h>
+
+SDL_Texture* imv_load_freeimage(SDL_Renderer *r, const char* path)
+{
+ FREE_IMAGE_FORMAT fmt = FreeImage_GetFileType(path,0);
+ FIBITMAP *image = FreeImage_Load(fmt, path, 0);
+ FreeImage_FlipVertical(image);
+
+ FIBITMAP* temp = image;
+ image = FreeImage_ConvertTo32Bits(image);
+ FreeImage_Unload(temp);
+
+ int w = FreeImage_GetWidth(image);
+ int h = FreeImage_GetHeight(image);
+ char* pixels = (char*)FreeImage_GetBits(image);
+
+ SDL_Texture *img = SDL_CreateTexture(r,
+ SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_STATIC, w, h);
+ SDL_Rect area = {0,0,w,h};
+ SDL_UpdateTexture(img, &area, pixels, sizeof(unsigned int) * w);
+ FreeImage_Unload(image);
+ return img;
+}
diff --git a/loader.c b/loader.c
deleted file mode 100644
index 9164074..0000000
--- a/loader.c
+++ /dev/null
@@ -1,30 +0,0 @@
-#include <SDL2/SDL.h>
-#include <string.h>
-
-SDL_Texture* imv_load_png(SDL_Renderer *r, const char* path);
-SDL_Texture* imv_load_jpeg(SDL_Renderer *r, const char* path);
-SDL_Texture* imv_load_tiff(SDL_Renderer *r, const char* path);
-
-SDL_Texture* imv_load_image(SDL_Renderer *r, const char* path)
-{
- if(!path)
- return NULL;
-
- const char* ext = strrchr(path, '.');
-
- if(!ext)
- return NULL;
-
- if(strcasecmp(ext, ".png") == 0) {
- return imv_load_png(r, path);
- } else if(strcasecmp(ext, ".jpeg") == 0 || strcasecmp(ext, ".jpg") == 0) {
- return imv_load_jpeg(r, path);
- } else if(strcasecmp(ext, ".tif") == 0 || strcasecmp(ext, ".tiff") == 0) {
- return imv_load_tiff(r, path);
- }
-
- fprintf(stderr,
- "Could not determine filetype of '%s' from its extension.\n",
- path);
- return NULL;
-}
diff --git a/main.c b/main.c
index e46617c..b066fc5 100644
--- a/main.c
+++ b/main.c
@@ -1,7 +1,7 @@
#include <stdio.h>
#include <SDL2/SDL.h>
-SDL_Texture* imv_load_image(SDL_Renderer *r, const char* path);
+SDL_Texture* imv_load_freeimage(SDL_Renderer *r, const char* path);
struct loop_item_s {
struct loop_item_s *prev;
@@ -194,7 +194,7 @@ int main(int argc, char** argv)
SDL_DestroyTexture(img);
img = NULL;
}
- img = imv_load_image(renderer, g_path.cur->path);
+ img = imv_load_freeimage(renderer, g_path.cur->path);
if(img == NULL) {
fprintf(stderr, "Ignoring unsupported file: %s\n", g_path.cur->path);
remove_current_path();