From 7831f900a2f1ad24e66b999a6ddb7d664236d0d7 Mon Sep 17 00:00:00 2001 From: Cyril Hrubis Date: Fri, 8 Jun 2012 19:30:58 +0200 Subject: [PATCH] spiv: Fix low pass filtering, edhance cache. --- demos/spiv/image_cache.c | 30 +++++++++++++++++------------- demos/spiv/image_cache.h | 6 +++--- demos/spiv/spiv.c | 43 ++++++++++++++++++++++++------------------- 3 files changed, 44 insertions(+), 35 deletions(-) diff --git a/demos/spiv/image_cache.c b/demos/spiv/image_cache.c index f2520139..7ff0c566 100644 --- a/demos/spiv/image_cache.c +++ b/demos/spiv/image_cache.c @@ -26,11 +26,13 @@ struct image { GP_Context *ctx; - int cookie; struct image *prev; struct image *next; + /* this identifies an image */ + long cookie1; + long cookie2; char path[]; }; @@ -106,8 +108,8 @@ static void remove_img(struct image_cache *self, struct image *img) static void remove_img_free(struct image_cache *self, struct image *img) { - GP_DEBUG(2, "Freeing image '%s:%i' size %u", - img->path, img->cookie, + GP_DEBUG(2, "Freeing image '%s:%10li:%10li' size %u", + img->path, img->cookie1, img->cookie2, img->ctx->bytes_per_row * img->ctx->h); remove_img(self, img); @@ -137,21 +139,22 @@ static void add_img(struct image_cache *self, struct image *img) } GP_Context *image_cache_get(struct image_cache *self, - const char *path, int cookie) + const char *path, long cookie1, long cookie2) { struct image *i; - GP_DEBUG(2, "Looking for image '%s:%i'", path, cookie); + GP_DEBUG(2, "Looking for image '%s:%10li:%10li'", path, cookie1, cookie2); for (i = self->root; i != NULL; i = i->next) - if (!strcmp(path, i->path) && i->cookie == cookie) + if (!strcmp(path, i->path) && + i->cookie1 == cookie1 && i->cookie2 == cookie2) break; if (i == NULL) return NULL; /* Push the image to the root of the list */ - GP_DEBUG(2, "Refreshing image '%s:%i", path, cookie); + GP_DEBUG(2, "Refreshing image '%s:%10li:%10li", path, cookie1, cookie2); remove_img(self, i); add_img(self, i); @@ -165,7 +168,7 @@ void image_cache_print(struct image_cache *self) printf("Image cache size %u used %u\n", self->max_size, self->cur_size); for (i = self->root; i != NULL; i = i->next) - printf(" Image '%s:%i' size %u\n", i->path, i->cookie, + printf(" Image '%s:%10li:%10li' size %u\n", i->path, i->cookie1, i->cookie2, i->ctx->bytes_per_row * i->ctx->h); } @@ -187,8 +190,8 @@ static int assert_size(struct image_cache *self, size_t size) return 0; } -int image_cache_put(struct image_cache *self, - GP_Context *ctx, const char *path, int cookie) +int image_cache_put(struct image_cache *self, GP_Context *ctx, const char *path, + long cookie1, long cookie2) { size_t size = ctx->bytes_per_row * ctx->h; @@ -202,11 +205,12 @@ int image_cache_put(struct image_cache *self, return 1; } - GP_DEBUG(2, "Adding image '%s:%i' size %zu", - img->path, img->cookie, size); + GP_DEBUG(2, "Adding image '%s:%10li:%10li' size %zu", + img->path, img->cookie1, img->cookie2, size); img->ctx = ctx; - img->cookie = cookie; + img->cookie1 = cookie1; + img->cookie2 = cookie2; strcpy(img->path, path); add_img(self, img); diff --git a/demos/spiv/image_cache.h b/demos/spiv/image_cache.h index ad3ecdd0..fc008ed0 100644 --- a/demos/spiv/image_cache.h +++ b/demos/spiv/image_cache.h @@ -43,13 +43,13 @@ struct image_cache *image_cache_create(unsigned int max_size); * Returns cached image, or NULL. */ GP_Context *image_cache_get(struct image_cache *self, - const char *path, int cookie); + const char *path, long cookie1, long cookie2); /* * Puts an image into a cache. */ -int image_cache_put(struct image_cache *self, - GP_Context *img, const char *path, int cookie); +int image_cache_put(struct image_cache *self, GP_Context *img, + const char *path, long cookie1, long cookie2); /* * Destroys image cache and all it's images. diff --git a/demos/spiv/spiv.c b/demos/spiv/spiv.c index c1ec6d29..93c066f2 100644 --- a/demos/spiv/spiv.c +++ b/demos/spiv/spiv.c @@ -134,7 +134,7 @@ GP_Context *load_image(struct loader_params *params) GP_ProgressCallback callback = {.callback = image_loader_callback, .priv = "Loading image"}; - img = image_cache_get(params->image_cache, params->img_path, 0); + img = image_cache_get(params->image_cache, params->img_path, 0, 0); /* Image not cached, load it */ if (img == NULL) { @@ -160,7 +160,7 @@ GP_Context *load_image(struct loader_params *params) img = tmp; } - image_cache_put(params->image_cache, img, params->img_path, 0); + image_cache_put(params->image_cache, img, params->img_path, 0, 0); cpu_timer_stop(&timer); } @@ -171,33 +171,33 @@ GP_Context *load_image(struct loader_params *params) GP_Context *load_resized_image(struct loader_params *params, GP_Size w, GP_Size h, float rat) { long cookie = (w & 0xffff) | (h & 0xffff)<<16; - GP_Context *img; + GP_Context *img, *res = NULL; struct cpu_timer timer; GP_ProgressCallback callback = {.callback = image_loader_callback}; /* Try to get resized cached image */ - img = image_cache_get(params->image_cache, params->img_path, cookie); + img = image_cache_get(params->image_cache, params->img_path, cookie, resampling_method); if (img != NULL) return img; /* Otherwise load image and resize it */ - img = load_image(params); - - if (img == NULL) + if ((img = load_image(params)) == NULL) return NULL; /* Do low pass filter */ - if (resampling_method != GP_INTERP_LINEAR_LF_INT) { - if (rat < 1) { - cpu_timer_start(&timer, "Blur"); - callback.priv = "Blurring Image"; - //TODO: We can't blur saved image! - if (GP_FilterGaussianBlur(img, img, 0.4/rat, 0.4/rat, - &callback) == NULL) - return NULL; - cpu_timer_stop(&timer); - } + if (resampling_method != GP_INTERP_LINEAR_LF_INT && rat < 1) { + cpu_timer_start(&timer, "Blur"); + callback.priv = "Blurring Image"; + + res = GP_FilterGaussianBlur(img, NULL, 0.4/rat, 0.4/rat, &callback); + + if (res == NULL) + return NULL; + + img = res; + + cpu_timer_stop(&timer); } cpu_timer_start(&timer, "Resampling"); @@ -205,11 +205,14 @@ GP_Context *load_resized_image(struct loader_params *params, GP_Size w, GP_Size img = GP_FilterResize(img, NULL, resampling_method, w, h, &callback); cpu_timer_stop(&timer); + /* Free low passed context if needed */ + GP_ContextFree(res); + if (img == NULL) return NULL; - image_cache_put(params->image_cache, img, params->img_path, cookie); - + image_cache_put(params->image_cache, img, params->img_path, cookie, resampling_method); + return img; } @@ -270,6 +273,8 @@ static void *image_loader(void *ptr) if (img == NULL) return NULL; + image_cache_print(params->image_cache); + switch (rotate) { case 0: break; -- 2.11.4.GIT