From ae299885d5bb3001dd7d6105d12ecbce2b06f13b Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Fri, 6 Nov 2015 12:59:54 +0000 Subject: Add TIFF support --- Makefile | 2 +- loader.c | 3 +++ tiff.c | 26 ++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 tiff.c diff --git a/Makefile b/Makefile index 09dded0..e8d49a1 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 +LDFLAGS = `sdl2-config --libs` -lpng -ljpeg -ltiff TARGET = imv SOURCES = $(wildcard *.c) diff --git a/loader.c b/loader.c index 3615908..9164074 100644 --- a/loader.c +++ b/loader.c @@ -3,6 +3,7 @@ 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) { @@ -18,6 +19,8 @@ SDL_Texture* imv_load_image(SDL_Renderer *r, const char* path) 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, diff --git a/tiff.c b/tiff.c new file mode 100644 index 0000000..eb38c4d --- /dev/null +++ b/tiff.c @@ -0,0 +1,26 @@ +#include +#include + +SDL_Texture* imv_load_tiff(SDL_Renderer *r, const char* path) +{ + TIFF* tif = TIFFOpen(path, "r"); + if(!tif) { + return NULL; + } + SDL_Texture *img = NULL; + uint32 w, h; + TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w); + TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h); + size_t npixels = w * h; + uint32 *pixels = (uint32*) malloc(npixels * sizeof (uint32)); + if (TIFFReadRGBAImageOriented(tif, w, h, pixels, ORIENTATION_TOPLEFT, 0)) { + img = SDL_CreateTexture(r, + SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STATIC, w, h); + SDL_Rect area = {0,0,w,h}; + SDL_UpdateTexture(img, &area, pixels, sizeof(uint32) * w); + } + free(pixels); + TIFFClose(tif); + return img; +} + -- cgit v1.2.3