Load images using freeimage

This commit is contained in:
Harry Jeffery 2015-11-06 14:17:26 +00:00
parent f4d54f4982
commit 77dfcacf38
4 changed files with 27 additions and 33 deletions

View file

@ -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)

24
freeimage.c Normal file
View file

@ -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;
}

View file

@ -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;
}

4
main.c
View file

@ -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();