diff options
| author | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2010-10-09 21:20:13 +0200 |
|---|---|---|
| committer | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2010-10-09 21:20:13 +0200 |
| commit | 69d1615d7dc7500039dbde951fa6cbb920eb99b8 (patch) | |
| tree | 93942d2a2dfe7a3996c3b3d217de0955000daa1d /src | |
| parent | a1e7a92e93e4760863105ae9f61ced93510e8cb2 (diff) | |
| download | cataract-69d1615d7dc7500039dbde951fa6cbb920eb99b8.tar.xz | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/generators.c | 17 | ||||
| -rw-r--r-- | src/jpeg-utils.cpp | 32 | ||||
| -rw-r--r-- | src/jpeg-utils.h | 6 | ||||
| -rw-r--r-- | src/setup.c | 8 | ||||
| -rw-r--r-- | src/setup.h | 7 |
5 files changed, 61 insertions, 9 deletions
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 <glib.h> +#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; |
