image: Simplify image handling.

This commit is contained in:
dylan araps 2017-12-14 10:06:16 +11:00
parent 8762403de9
commit 6c238a03b2
2 changed files with 27 additions and 92 deletions

View file

@ -617,16 +617,6 @@ image_loop="off"
# Values: 'dir'
thumbnail_dir="${XDG_CACHE_HOME:-${HOME}/.cache}/thumbnails/neofetch"
# Crop mode
#
# Default: 'normal'
# Values: 'normal', 'fit', 'fill'
# Flag: --crop_mode
#
# See this wiki page to learn about the fit and fill options.
# https://github.com/dylanaraps/neofetch/wiki/What-is-Waifu-Crop%3F
crop_mode="normal"
# Crop offset
# Note: Only affects 'normal' crop mode.
#
@ -637,20 +627,21 @@ crop_mode="normal"
crop_offset="center"
# Image size
# The image is half the terminal width by default.
# NOTE: 'square' is the OLD behavior.
# NOTE: 'auto' keeps image aspect ratio and size where possible.
#
# Default: 'auto'
# Values: 'auto', '00px', '00%', 'none'
# Values: 'auto', 'square'
# Flags: --image_size
# --size
image_size="auto"
# Ggap between image and text
# Gap between image and text
#
# Default: '3'
# Default: '4'
# Values: 'num', '-num'
# Flag: --gap
gap=3
gap=4
# Image offsets
# Only works with the w3m backend.

View file

@ -2667,49 +2667,31 @@ get_image_size() {
font_width="$((term_width / columns))"
font_height="$((term_height / lines))"
# Get image size.
size="$(identify -format "%w %h" "$image")"
width="${size%% *}"
height="${size##* }"
case "$image_size" in
"auto")
image_size="$((columns * font_width / 2))"
term_height="$((term_height - term_height / 4))"
((term_height < image_size)) && \
image_size="$term_height"
"square")
width="$((columns * font_width / 2))"
height="$width"
;;
*"%")
percent="${image_size/\%}"
image_size="$((percent * term_width / 100))"
(((percent * term_height / 50) < image_size)) && \
image_size="$((percent * term_height / 100))"
;;
"none")
# Get image size so that we can do a better crop.
size="$(identify -format "%w %h" "$image")"
width="${size%% *}"
height="${size##* }"
crop_mode="none"
while (( "$width" >= ("$term_width" / 2) ||
"$height" >= "$term_height" )); do
width="$((width / 2))"
height="$((height / 2))"
*)
while (( width >= (term_width / 2) ||
height >= (term_height / 2) )); do
width="$((width * 10 / 15))"
height="$((height * 10 / 15))"
done
;;
*) image_size="${image_size/px}" ;;
esac
width="${width:-$image_size}"
height="${height:-$image_size}"
text_padding="$((width / font_width + gap + xoffset/font_width))"
}
make_thumbnail() {
# Name the thumbnail using variables so we can
# use it later.
# Name the thumbnail using variables so we can use it later.
image_name="$crop_mode-$crop_offset-$width-$height-${image##*/}"
# Handle file extensions.
@ -2722,62 +2704,24 @@ make_thumbnail() {
# Create the thumbnail dir if it doesn't exist.
mkdir -p "$thumbnail_dir"
# Check to see if the thumbnail exists before we do any cropping.
if [[ ! -f "$thumbnail_dir/$image_name" ]]; then
# Get image size so that we can do a better crop.
if [[ -z "$size" ]]; then
size="$(identify -format "%w %h" "$image")"
og_width="${size%% *}"
og_height="${size##* }"
# This checks to see if height is greater than width
# so we can do a better crop of portrait images.
size="$og_height"
((og_height > og_width)) && size="$og_width"
fi
case "$crop_mode" in
"fit")
c="$(convert "$image" \
-colorspace srgb \
-format "%[pixel:p{0,0}]" info:)"
case "$image_size" in
"square")
convert \
-background none \
"$image" \
-trim +repage \
-gravity south \
-background "$c" \
-extent "$size"x"$size" \
-scale "$width"x"$height" \
"$thumbnail_dir/$image_name"
;;
"fill")
convert \
-strip \
-quality 50 \
-gravity "$crop_offset" \
-background none \
"$image" \
-trim +repage \
-scale "$width"x"$height"^ \
-sample "$width"x"$height"^ \
-extent "$width"x"$height" \
"$thumbnail_dir/$image_name"
;;
"none") cp "$image" "$thumbnail_dir/$image_name" ;;
*)
convert \
-background none \
"$image" \
-gravity "$crop_offset" \
-crop "$size"x"$size"+0+0 \
-quality 95 \
-scale "$width"x"$height" \
"$thumbnail_dir/$image_name"
;;
*) cp "$image" "$thumbnail_dir/$image_name" ;;
esac
fi
# The final image.
image="$thumbnail_dir/$image_name"
}