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

Do not upscale picture #84

Closed
ploum opened this issue Mar 21, 2022 · 9 comments
Closed

Do not upscale picture #84

ploum opened this issue Mar 21, 2022 · 9 comments

Comments

@ploum
Copy link

ploum commented Mar 21, 2022

By default, displaying a picture with Chafa takes the whole terminal.

It would be nice to not upscale smaller pictures so the user can get a sense of the size of the picture (for example if it is an icon).

The "does not upscale" option could work together with the size option to set the size as "maximal size". If I set the size to 100 but I’m viewing an icon which is 10x10, the icon stays at 10x10.

@hpjansson
Copy link
Owner

Chafa doesn't actually take the whole terminal by default. You can see this with a tiny image (something like /usr/share/icons/Adwaita/16x16/devices/computer.png). However, it does upscale quite a bit to keep images legible when rendered with symbols: 1 horizontal pixel becomes 1 horizontal cell. The vertical size depends on font ratio, 1/2 by default. So a 16x16 image becomes 16x8 cells.

If we try to map it to something closer to the actual pixel size, it would likely take up only 1 or 2 cells, which would make it unrecognizable. One could argue that we could use a smaller size when there is a pixel-perfect alternative (sixels/kitty/iterm), but I think it's nice to produce a consistent screen size across all protocols...

@ploum
Copy link
Author

ploum commented Mar 22, 2022 via email

@hpjansson
Copy link
Owner

hpjansson commented Mar 24, 2022

This is somewhat complicated by the fact that the Chafa API expresses dimensions in terms of cells, so some scaling (and aspect distortion) will happen no matter what. At smaller sizes, the relative distortion is greater. That is, the lost fraction is a greater portion of the total (e.g. rounding 2x2.5 to 2x3 results in greater distortion than rounding 40x40.5 to 40x41). So: Making images tiny can also make them kind of ugly.

Regarding a maximum size, there is currently such a thing, although it's not directly controllable. It's taken from the detected terminal size (falling back to 80x25 if detection fails). It could make sense to allow direct control of that.

Here's what I think we could do:

  • Introduce a --scale <num> parameter taking a decimal number. It would default to 8.0 (current behavior) or maybe 4.0. Passing --scale 1 would do what you want. The existing --size parameter would override it if both are specified.
  • Introduce a --frame-size NxM parameter to specify the maximum size in cells. This would override the detected terminal size. The resulting image could be smaller, as determined by the scaling factor, but not bigger than this. Again --size would override, producing a bigger image.

I think this would do what you want and give some extra flexibility too. And it preserves existing behavior. What do you think?

Edit: Actually, --size currently acts like --frame-size described above. I'd be changing its behavior to force the image dimensions, but still respect aspect unless --stretch was passed.

@ploum
Copy link
Author

ploum commented Mar 25, 2022 via email

@hpjansson
Copy link
Owner

Correct, --scale would respect the max size (terminal window or --frame-size). --size always wins, it'd override everything.

It would work the same with pixel-perfect (or near-perfect) rendering; it'd cover the same number of cells as the symbol output does (keeps it predictable).

@ploum
Copy link
Author

ploum commented Apr 5, 2022

I’m a bit confused with all those options.

How would I say "makes it natural size except if it’s bigger than the terminal, in which case resize to the terminal size" ?

@hpjansson
Copy link
Owner

--scale would respect the terminal's size. So you'd only need --scale 1, nothing else.

@ploum
Copy link
Author

ploum commented Apr 6, 2022

Then it’s perfect for my needs :-)

@hpjansson
Copy link
Owner

The 1.12.0 release now has the --scale <n> option!

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

No branches or pull requests

2 participants