Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoiding 'convert' calls in scope.sh #1446

Open
zaeph opened this issue Jan 19, 2019 · 1 comment
Open

Avoiding 'convert' calls in scope.sh #1446

zaeph opened this issue Jan 19, 2019 · 1 comment

Comments

@zaeph
Copy link
Contributor

zaeph commented Jan 19, 2019

Runtime Environment

  • Operating system and version: Arch
  • Terminal emulator and version: Terminator 1.91, Terminology 1.3.2
  • Python version: 3.7.2
  • Ranger version/commit: 1.9.2
  • Locale: en_GB

Context

We're making a handful of convert calls in scope.sh, some of which could be easily avoided if we reworked the code for previewing images.
Notably, working on #1443 which implements font previews, I couldn't get our previews to open a .png preview despite using the proper hash. It wasn't coming form w3m either because it can handle .png just fine. Reviewing the code, I've stumbled upon this:

ranger/ranger/core/actions.py

Lines 1019 to 1023 in b4cb267

def sha1_encode(path):
if version_info[0] < 3:
return os.path.join(ranger.args.cachedir, sha1(path).hexdigest()) + '.jpg'
return os.path.join(ranger.args.cachedir,
sha1(path.encode('utf-8', 'backslashreplace')).hexdigest()) + '.jpg'

So, .jpg are hard-coded, and since the program I use to generate font previews only outputs .png or .bmp, I have to resort to a convert call which is not an elegant solution.

ranger/ranger/data/scope.sh

Lines 132 to 151 in f9de84d

# Font
application/font*|application/*opentype)
preview_png="/tmp/$(basename "${IMAGE_CACHE_PATH%.*}").png"
if fontimage -o "${preview_png}" \
--pixelsize "120" \
--fontname \
--pixelsize "80" \
--text " ABCDEFGHIJKLMNOPQRSTUVWXYZ " \
--text " abcdefghijklmnopqrstuvwxyz " \
--text " 0123456789.:,;(*!?') ff fl fi ffi ffl " \
--text " The quick brown fox jumps over the lazy dog. " \
"${FILE_PATH}";
then
convert -- "${preview_png}" "${IMAGE_CACHE_PATH}" \
&& rm "${preview_png}" \
&& exit 6
else
exit 1
fi
;;

Whilst the aforementioned is clearly something that we could avoid by modifying actions.py, there are other calls to convert inside scope.sh which we could also reconsider for the sake of efficiency.

# SVG
# image/svg+xml)
# convert "${FILE_PATH}" "${IMAGE_CACHE_PATH}" && exit 6
# exit 1;;

ranger/ranger/data/scope.sh

Lines 102 to 111 in b4cb267

# Image
image/*)
local orientation
orientation="$( identify -format '%[EXIF:Orientation]\n' -- "${FILE_PATH}" )"
# If orientation data is present and the image actually
# needs rotating ("1" means no rotation)...
if [[ -n "$orientation" && "$orientation" != 1 ]]; then
# ...auto-rotate the image according to the EXIF data.
convert -- "${FILE_PATH}" -auto-orient "${IMAGE_CACHE_PATH}" && exit 6
fi

HTH

@toonn
Copy link
Member

toonn commented Jan 19, 2019

We should still allow the cached format to be different from the actual format though, since they might render faster. I'm thinking TIF or SVG.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants