From acb80e111bfdd64c50310c3b0cec2de3f36251cd Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Mon, 9 Nov 2015 15:58:36 +0000 Subject: Resample overlarge images --- main.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index 97fb135..9738b46 100644 --- a/main.c +++ b/main.c @@ -54,9 +54,10 @@ struct { FIBITMAP *frame; SDL_Texture *tex; int width, height; + int max_width, max_height; int cur_frame, next_frame, num_frames, playing; double frame_time; -} g_img = {NULL,NULL,NULL,0,0,0,0,0,0,0}; +} g_img = {NULL,NULL,NULL,0,0,0,0,0,0,0,0,0}; void toggle_fullscreen() { @@ -173,6 +174,41 @@ void prev_path() g_path.dir = -1; } +void resample_image() +{ + double max_aspect = (double)g_img.max_width/(double)g_img.max_height; + double img_aspect = (double)g_img.width/(double)g_img.height; + double scale; + + if(max_aspect > img_aspect) { + //Image will become too tall before it becomes too wide + scale = (double)g_img.max_height/(double)g_img.height; + } else { + //Image will become too wide before it becomes too tall + scale = (double)g_img.max_width/(double)g_img.width; + } + + int new_width = g_img.width * scale; + int new_height = g_img.height * scale; + + fprintf(stderr, + "Warning: '%s' [%ix%i] is too large to fit into a SDL texture. " + "Resampling to %ix%i\n", + g_path.cur->path, + g_img.width, g_img.height, + new_width, new_height); + + //perform scaling + g_img.width = new_width; + g_img.height = new_height; + + FIBITMAP *resampled = FreeImage_Rescale(g_img.frame, + g_img.width, g_img.height, FILTER_CATMULLROM); + + FreeImage_Unload(g_img.frame); + g_img.frame = resampled; +} + void render_image(FIBITMAP *image) { if(g_img.frame) { @@ -182,6 +218,10 @@ void render_image(FIBITMAP *image) g_img.width = FreeImage_GetWidth(g_img.frame); g_img.height = FreeImage_GetHeight(g_img.frame); + if(g_img.width > g_img.max_width || g_img.height > g_img.max_height) { + resample_image(); + } + char* pixels = (char*)FreeImage_GetBits(g_img.frame); if(g_img.tex) { @@ -191,6 +231,9 @@ void render_image(FIBITMAP *image) SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_STATIC, g_img.width, g_img.height); + if(g_img.tex == NULL) { + fprintf(stderr, "SDL Error when creating texture: %s\n", SDL_GetError()); + } SDL_Rect area = {0,0,g_img.width,g_img.height}; SDL_UpdateTexture(g_img.tex, &area, pixels, 4 * g_img.width); g_view.redraw = 1; @@ -412,6 +455,12 @@ int main(int argc, char** argv) //Use linear sampling for scaling SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"); + //We need to know how big our textures can be + SDL_RendererInfo ri; + SDL_GetRendererInfo(g_renderer, &ri); + g_img.max_width = ri.max_texture_width; + g_img.max_height = ri.max_texture_height; + //Put us in fullscren by default if requested if(g_options.fullscreen) { toggle_fullscreen(); -- cgit v1.2.3