diff options
| author | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2015-02-01 16:23:30 +0100 |
|---|---|---|
| committer | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2015-02-01 16:23:30 +0100 |
| commit | b2d0c4ea27c3885d69c9067b579a075a65ff3834 (patch) | |
| tree | 0d2ac06f9c32babe69a5b88e8c8604e2f5b4ba91 /src/generators.c | |
| parent | 97919e37642b851af85c37f751284fd62bd74019 (diff) | |
| download | cataract-b2d0c4ea27c3885d69c9067b579a075a65ff3834.tar.xz | |
theming: Introduce image size fallback mode
This changes image size handling a little bit. An optional <fallback size="xxx" />
tag tells the generator to use different image size when an image for the current
image size has not been supplied from the album XML file.
This is effectively used for the legacy <nofullsize> tag fallback. More generally
this is useful for templates showing additional image sizes. Also saves space as
images not having the custom size supplied are not generated from the source image.
Diffstat (limited to 'src/generators.c')
| -rw-r--r-- | src/generators.c | 93 |
1 files changed, 58 insertions, 35 deletions
diff --git a/src/generators.c b/src/generators.c index 9183888..3bd00a3 100644 --- a/src/generators.c +++ b/src/generators.c @@ -204,7 +204,7 @@ get_image_paths (TGallerySetup *setup, gboolean is_original; gchar *s1, *s2, *s3; gchar *target_image_dir; - TImageSize *preview_image_size; + TImageSize *src_image_size; if (full_img_src) *full_img_src = NULL; @@ -221,19 +221,45 @@ get_image_paths (TGallerySetup *setup, (image_size->is_thumbnail && item->hidden) || (image_size->is_thumbnail && items->type == GALLERY_TYPE_INDEX && (item->thumbnail == NULL || strlen (item->thumbnail) == 0))) return; + if (is_original && nofullsize && image_size->fallback_size == NULL) { + log_error ("Requested legacy nofullsize flag but fallback image size not defined.\n"); + return; + } s1 = NULL; if (image_size->is_thumbnail && items->type == GALLERY_TYPE_INDEX) s1 = item->thumbnail; - if (s1 == NULL && ! is_original && item->image_sizes != NULL) { + + if (s1 == NULL && ! is_original && item->image_sizes != NULL) s1 = g_hash_table_lookup (item->image_sizes, image_size->name); - } - if (s1 == NULL) + + if (s1 == NULL && is_original && ! nofullsize) s1 = item->path; - /* legacy behaviour fallback */ - if (s1 == NULL && item->image_sizes != NULL) - s1 = g_hash_table_lookup (item->image_sizes, "preview"); + /* go through the fallback */ + while (s1 == NULL && image_size->fallback_size != NULL) { + image_size = lookup_image_size_for_name (setup, image_size->fallback_size); + is_original = g_ascii_strcasecmp ("original", image_size->name) == 0; + if (is_original) + s1 = nofullsize ? NULL : item->path; + else + s1 = item->image_sizes ? g_hash_table_lookup (item->image_sizes, image_size->name) : NULL; + } + + /* we have reached our target image size, s1 == NULL means the image should be resized from the original size */ + if (s1 == NULL) { + s1 = item->path; /* nofullsize is handled by the fallback */ + if (s1 == NULL) { + /* no original image available, try fallback as a last resort (i.e. preview fallback) */ + src_image_size = lookup_image_size_for_name (setup, "original"); + while (s1 == NULL && src_image_size->fallback_size != NULL) { + src_image_size = lookup_image_size_for_name (setup, src_image_size->fallback_size); + if (item->image_sizes != NULL) + s1 = g_hash_table_lookup (item->image_sizes, src_image_size->name); + } + } + } + if (s1 == NULL) { log_error ("Unable to find image source for item #%d (\"%s\") for image size \"%s\"\n", get_item_index (items, item), item->title, image_size->name); return; @@ -242,17 +268,7 @@ get_image_paths (TGallerySetup *setup, if (full_img_src) *full_img_src = g_build_filename (path_info->src_dir, s1, NULL); - /* Use preview image for targets but leave original source if nofullsize is defined (legacy behaviour) */ - preview_image_size = NULL; - if (is_original && nofullsize && item->image_sizes != NULL) { - s2 = g_hash_table_lookup (item->image_sizes, "preview"); - if (s2 != NULL) { - s1 = s2; - preview_image_size = lookup_image_size_for_name (setup, "preview"); - } - } - target_image_dir = g_strdup_printf ("%s%s", TARGET_IMAGE_DIR_PREFIX, preview_image_size ? preview_image_size->name : image_size->name); - + target_image_dir = g_strdup_printf ("%s%s", TARGET_IMAGE_DIR_PREFIX, image_size->name); s2 = g_path_get_basename (s1); if (image_size->is_thumbnail) { s3 = g_strdup_printf (THUMBNAIL_NAME_FORMAT, get_item_index (items, item), s2); @@ -427,7 +443,11 @@ generate_image (TGallerySetup *setup, is_original = g_ascii_strcasecmp ("original", image_size->name) == 0; is_preview = g_ascii_strcasecmp ("preview", image_size->name) == 0; - if (is_original && IS_NOFULLSIZE (item, items, setup)) + if (is_original && (IS_NOFULLSIZE (item, items, setup) || item->path == NULL)) + continue; + + /* fallback mode, don't generate any image and use different image size */ + if (image_size->fallback_size != NULL && ! is_original && (! item->image_sizes || g_hash_table_lookup (item->image_sizes, image_size->name) == NULL)) continue; get_image_paths (setup, items, item, path_info, image_size, &img_src, &img_dst, NULL); @@ -447,7 +467,7 @@ generate_image (TGallerySetup *setup, res = res || needs_update (img_src, img_dst); if (! query_update) { /* Determine whether to perform file copy or resize */ - if (! image_size->is_thumbnail && image_size->no_resize && (is_original || (item->image_sizes && g_hash_table_lookup (item->image_sizes, image_size->name)))) { + if (! image_size->is_thumbnail && image_size->no_resize && ((is_original && item->path) || (item->image_sizes && g_hash_table_lookup (item->image_sizes, image_size->name)))) { if (! copy_file (img_src, img_dst)) log_error (" Error copying image %s to %s\n", img_src, img_dst); } @@ -771,27 +791,32 @@ get_img_exif_data (TGallerySetup *setup, if (exif == NULL) { img_orig_src = NULL; orig_image_size = lookup_image_size_for_name (setup, "original"); - get_image_paths (setup, items, item, path_info, orig_image_size ? orig_image_size : image_size, &img_orig_src, NULL, NULL); - if (img_orig_src != NULL && g_access (img_orig_src, R_OK) == 0) - exif = read_exif (img_orig_src); - /* -- silently succeed - if (exif == NULL) - log_error ("write_html_image: error getting exif data from file \"%s\"\n", img_orig_src); - */ - g_free (img_orig_src); + if (orig_image_size) { + get_image_paths (setup, items, item, path_info, orig_image_size, &img_orig_src, NULL, NULL); + if (img_orig_src != NULL && g_access (img_orig_src, R_OK) == 0) + exif = read_exif (img_orig_src); + /* -- silently succeed + if (exif == NULL) + log_error ("write_html_image: error getting exif data from file \"%s\"\n", img_orig_src); + */ + g_free (img_orig_src); + } } - /* Try destination image instead, though it might have the metadata stripped (as in source file already) */ + /* Try destination image size instead, though it might have the metadata stripped */ if (exif == NULL) { - img_dst = NULL; - get_image_paths (setup, items, item, path_info, image_size, NULL, &img_dst, NULL); - if (img_dst != NULL && g_access (img_dst, R_OK) == 0) + img_orig_src = img_dst = NULL; + get_image_paths (setup, items, item, path_info, image_size, &img_orig_src, &img_dst, NULL); + if (img_orig_src != NULL && g_access (img_orig_src, R_OK) == 0) + exif = read_exif (img_orig_src); + if (exif == NULL && img_dst != NULL && g_access (img_dst, R_OK) == 0) exif = read_exif (img_dst); /* -- silently succeed if (exif == NULL) log_error ("write_html_image: error getting exif data from file \"%s\"\n", img_dst); */ g_free (img_dst); + g_free (img_orig_src); } if (exif != NULL) @@ -901,9 +926,7 @@ process_img_item (TGallerySetup *setup, /* TODO: legacy stuff, subject to removal */ orig_image_size = lookup_image_size_for_name (setup, "original"); - if (orig_image_size == NULL) - orig_image_size = image_size; - if (image_size != orig_image_size && img_orig_dst != NULL && ! IS_NOFULLSIZE (item, items, setup)) { + if (orig_image_size != NULL && img_orig_dst != NULL && ! IS_NOFULLSIZE (item, items, setup)) { img_orig_dst = img_orig_dst_page = NULL; get_image_paths (setup, items, item, path_info, orig_image_size, NULL, &img_orig_dst, &img_orig_dst_page); get_image_sizes (img_orig_dst, &img_orig_w, &img_orig_h, setup->autorotate); |
