From 69d1615d7dc7500039dbde951fa6cbb920eb99b8 Mon Sep 17 00:00:00 2001 From: Tomas Bzatek Date: Sat, 9 Oct 2010 21:20:13 +0200 Subject: Add support for squared thumbnails Disabled by default, only very simple center crop implemented. The SQUARED_SIMPLE_SHAVE_AMOUNT constant may be slightly adjusted according to future experience. It's a really dumb algorithm which may not be suitable for every picture. Looking for a fast and smart algorithm to determine image weight center and radius, i.e. focus on object of interest. The OpenCV's face recognition features are worth to test and consider, though I fear the speed issues. --- sample/src/setup.xml | 7 +++++++ src/generators.c | 17 ++++++++++++++--- src/jpeg-utils.cpp | 32 +++++++++++++++++++++++++++----- src/jpeg-utils.h | 6 +++++- src/setup.c | 8 ++++++++ src/setup.h | 7 +++++++ templates/styles.css | 23 +++++++++++++++++++++++ templates/template-album.tmpl | 13 ++++++++++--- templates/template-index.tmpl | 15 ++++++++++++--- templates/template-view_photo.tmpl | 1 - 10 files changed, 113 insertions(+), 16 deletions(-) diff --git a/sample/src/setup.xml b/sample/src/setup.xml index 80d04da..72ce9dd 100644 --- a/sample/src/setup.xml +++ b/sample/src/setup.xml @@ -34,6 +34,7 @@ thn_%d_%s + + + + + + diff --git a/src/generators.c b/src/generators.c index f0ba940..cf32819 100644 --- a/src/generators.c +++ b/src/generators.c @@ -171,11 +171,17 @@ generate_image (TGallerySetup *setup, *thumb_portrait = (thumb_w / thumb_h) < 1; if ((thumb_w > 0) && (thumb_h > 0)) { + /* Squared thumbnails */ + if (setup->squared_thumbnail_type != THUMBNAIL_SQUARE_TYPE_NONE) { + thumb_w = setup->thumbnail_square_size; + thumb_h = setup->thumbnail_square_size; + } else + /* Portrait / Landscape thumbnails */ if (! *thumb_portrait) calculate_sizes (setup->thumbnail_landscape_width, setup->thumbnail_landscape_height, &thumb_w, &thumb_h); else calculate_sizes (setup->thumbnail_portrait_width, setup->thumbnail_portrait_height, &thumb_w, &thumb_h); - if (! resize_image (thumb_src_full, thumb_dst, thumb_w, thumb_h, setup->thumbnail_quality, TRUE)) + if (! resize_image (thumb_src_full, thumb_dst, thumb_w, thumb_h, setup->thumbnail_quality, TRUE, setup->squared_thumbnail_type)) log_error ("generate_image: error resizing thumbnail %s\n", thumb_src_full); } else log_error ("generate_image: thumbnail %s sizes are %lux%lu\n", thumb_src_full, thumb_w, thumb_h); @@ -208,7 +214,7 @@ generate_image (TGallerySetup *setup, } g_assert (img_src_full != NULL); - if (! resize_image (img_src_full, big_dst, new_w, new_h, bigq, FALSE)) + if (! resize_image (img_src_full, big_dst, new_w, new_h, bigq, FALSE, 0)) log_error ("generate_image: error resizing big image %s\n", img_src_full); } else @@ -413,6 +419,7 @@ write_html_album (TGallerySetup *setup, block_parser_register_key (block_parser, "IMG_LIST", "IMG_LIST"); block_parser_register_key (block_parser, "IMG_LIST_LANDSCAPE", NULL); block_parser_register_key (block_parser, "IMG_LIST_PORTRAIT", NULL); + block_parser_register_key (block_parser, "IMG_LIST_SQUARED", NULL); block_parser_register_key (block_parser, "LIST_SEPARATOR", NULL); block_parser_register_key (block_parser, "LIST_INTERSPACE", NULL); block_parser_register_key (block_parser, "GO_UP", "GO_UP"); @@ -454,7 +461,11 @@ write_html_album (TGallerySetup *setup, case INDEX_ITEM_TYPE_PICTURE: /* Skip HTML code generation if it's a hidden item */ if (! item->hidden) { - s1 = block_parser_get_data (block_parser, item->gen_portrait ? "IMG_LIST_PORTRAIT" : "IMG_LIST_LANDSCAPE"); + if (setup->squared_thumbnail_type != THUMBNAIL_SQUARE_TYPE_NONE) + s1 = "IMG_LIST_SQUARED"; + else + s1 = item->gen_portrait ? "IMG_LIST_PORTRAIT" : "IMG_LIST_LANDSCAPE"; + s1 = block_parser_get_data (block_parser, s1); replace_table_add_key_printf (local_replace_table, "ALBUM_SUBPATH", "%s/%s", item->path, setup->index_file_name); s3 = item_get_img_src (setup, items, item); replace_table_add_key_printf (local_replace_table, "IMG_SUBPAGE", "%s%s", s3, GET_EXT (setup->index_file_name)); diff --git a/src/jpeg-utils.cpp b/src/jpeg-utils.cpp index 3bde7b6..d46e064 100644 --- a/src/jpeg-utils.cpp +++ b/src/jpeg-utils.cpp @@ -231,9 +231,10 @@ free_exif_data (TExifData *data) */ gboolean resize_image (const gchar *src, const gchar *dst, - int size_x, int size_y, - int quality, - gboolean thumbnail) + int size_x, int size_y, + int quality, + gboolean thumbnail, + ThumbnailSquareType squared_thumbnail_type) { #define ThrowWandException(wand) \ { \ @@ -248,14 +249,35 @@ resize_image (const gchar *src, const gchar *dst, MagickBooleanType status; MagickWand *magick_wand; + unsigned long w; + unsigned long h; + unsigned long amount; /* Read an image. */ magick_wand = NewMagickWand(); status = MagickReadImage (magick_wand, src); if (status == MagickFalse) - ThrowWandException (magick_wand); - if (thumbnail) + ThrowWandException (magick_wand); + + /* Process thumbnail if required */ + if (thumbnail) { + switch (squared_thumbnail_type) { + case THUMBNAIL_SQUARE_TYPE_SIMPLE: + w = MagickGetImageWidth (magick_wand); + h = MagickGetImageHeight (magick_wand); + amount = MAX (w, h) * SQUARED_SIMPLE_SHAVE_AMOUNT / 100; + MagickShaveImage (magick_wand, amount, amount); + + w = MagickGetImageWidth (magick_wand); + h = MagickGetImageHeight (magick_wand); + amount = MIN (w, h); + MagickCropImage (magick_wand, amount, amount, (w - amount) / 2, (h - amount) / 2); + break; + default: + break; + } MagickThumbnailImage (magick_wand, size_x, size_y); + } else MagickResizeImage (magick_wand, size_x, size_y, LanczosFilter, 1.0); MagickSetImageCompressionQuality (magick_wand, quality); diff --git a/src/jpeg-utils.h b/src/jpeg-utils.h index 65e97c5..e31ed52 100644 --- a/src/jpeg-utils.h +++ b/src/jpeg-utils.h @@ -21,6 +21,9 @@ #endif #include +#include "setup.h" + +#define SQUARED_SIMPLE_SHAVE_AMOUNT 5 /* percent */ /* TODO: we want to have numerical values here at some point in the future */ @@ -68,7 +71,8 @@ void free_exif_data (TExifData *data); gboolean resize_image (const gchar *src, const gchar *dst, int size_x, int size_y, int quality, - gboolean thumbnail); + gboolean thumbnail, + ThumbnailSquareType squared_thumbnail_type); /* * get_image_sizes: retrieve image dimensions diff --git a/src/setup.c b/src/setup.c index 6d3c6be..daa1ff0 100644 --- a/src/setup.c +++ b/src/setup.c @@ -123,6 +123,7 @@ parse_setup_xml (const gchar *filename, TGallerySetup *setup) setup->thumbnail_landscape_height = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/thumbnail", "landscape_h", 0); setup->thumbnail_portrait_width = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/thumbnail", "portrait_w", 0); setup->thumbnail_portrait_height = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/thumbnail", "portrait_h", 0); + setup->thumbnail_square_size = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/thumbnail", "square", 0); setup->thumbnail_quality = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/thumbnail", "quality", -1); setup->preview_landscape_width = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/preview", "landscape_w", 0); setup->preview_landscape_height = xml_file_get_node_attribute_long (xml, "/gallery_setup/images/preview", "landscape_h", 0); @@ -157,6 +158,13 @@ parse_setup_xml (const gchar *filename, TGallerySetup *setup) setup->feed_title = xml_file_get_node_value (xml, "/gallery_setup/feed/title/text()"); setup->feed_base_url = xml_file_get_node_value (xml, "/gallery_setup/feed/base_url/text()"); + s = xml_file_get_node_attribute (xml, "/gallery_setup/images/squared_thumbnails", "type"); + if (s && g_ascii_strcasecmp (s, "simple") == 0) + setup->squared_thumbnail_type = THUMBNAIL_SQUARE_TYPE_SIMPLE; + else + setup->squared_thumbnail_type = THUMBNAIL_SQUARE_TYPE_NONE; + g_free (s); + xml_parser_close (xml); return TRUE; } diff --git a/src/setup.h b/src/setup.h index 8c9927f..16665c4 100644 --- a/src/setup.h +++ b/src/setup.h @@ -32,6 +32,11 @@ #define DEFAULT_INDEX_FILENAME "index.html" +typedef enum { + THUMBNAIL_SQUARE_TYPE_NONE, + THUMBNAIL_SQUARE_TYPE_SIMPLE +} ThumbnailSquareType; + /* Global gallery setup */ typedef struct { @@ -52,6 +57,7 @@ typedef struct { gchar *img_big_dir; gchar *img_orig_dir; gchar *thumbnail_name_format; + ThumbnailSquareType squared_thumbnail_type; gchar *footer; gchar *meta_author; @@ -65,6 +71,7 @@ typedef struct { unsigned long thumbnail_landscape_height; unsigned long thumbnail_portrait_width; unsigned long thumbnail_portrait_height; + unsigned long thumbnail_square_size; int preview_quality; unsigned long preview_landscape_width; diff --git a/templates/styles.css b/templates/styles.css index 0a42f53..8c3aaf2 100644 --- a/templates/styles.css +++ b/templates/styles.css @@ -222,6 +222,11 @@ div.album_item img.portrait { padding-top: 1em; } +div.album_item img.squared { + /* squared */ + padding-top: 1em; +} + .album_thumb_text { /* CSS positioning sucks^2 */ position: absolute; @@ -347,6 +352,16 @@ div.index_item img { /* padding-right: 7.7em; disabled -- word wrap */ top: 0.65em; } + +.album_text_squared { + font-size: 190%; + font-weight: bold; + position: relative; + left: 10.3em; +/* padding-right: 10.5em; disabled -- word wrap */ + top: 0.65em; +} + .album_subtext { font-size: 150%; position: relative; @@ -363,6 +378,14 @@ div.index_item img { top: 2.5em; } +.album_subtext_squared { + font-size: 150%; + position: relative; + left: 13.1em; +/* padding-right: 13.4em; disabled -- word wrap */ + top: 2.5em; +} + .album_note { position: absolute; right: 2.3em; diff --git a/templates/template-album.tmpl b/templates/template-album.tmpl index c018057..aaff9ef 100644 --- a/templates/template-album.tmpl +++ b/templates/template-album.tmpl @@ -40,7 +40,7 @@ $(IMG_TITLE) - +
@@ -48,8 +48,16 @@ $(IMG_TITLE) -
+ + + +
@@ -75,4 +83,3 @@ - diff --git a/templates/template-index.tmpl b/templates/template-index.tmpl index b2c7dba..77504e7 100644 --- a/templates/template-index.tmpl +++ b/templates/template-index.tmpl @@ -42,7 +42,7 @@ ( items) -
+
@@ -52,8 +52,18 @@ ( items) -
+ + + +
@@ -79,4 +89,3 @@ - diff --git a/templates/template-view_photo.tmpl b/templates/template-view_photo.tmpl index 7fe58e2..7557846 100644 --- a/templates/template-view_photo.tmpl +++ b/templates/template-view_photo.tmpl @@ -74,4 +74,3 @@ - -- cgit v1.2.3