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 +#include + +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 -#include - -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 #include -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();