Skip to content

Commit

Permalink
Add a new `image-transform-smoothing' user option
Browse files Browse the repository at this point in the history
* doc/lispref/display.texi (Image Descriptors): Document it.
* lisp/image.el (image-transform-smoothing): New user option.
(create-image): Use it.
(image--default-smoothing): New function.
  • Loading branch information
larsmagne committed Mar 12, 2021
1 parent fd3705a commit 5dff53f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
9 changes: 7 additions & 2 deletions doc/lispref/display.texi
Original file line number Diff line number Diff line change
Expand Up @@ -5399,8 +5399,13 @@ is platform dependent, but should be equivalent to bilinear
filtering. Disabling smoothing will use the nearest neighbor
algorithm.

The default, if this property is not specified, is for down-scaling to
apply smoothing, and for up-scaling to not apply smoothing.
If this property is not specified, @code{create-image} will use the
@code{image-transform-smoothing} user option to say whether scaling
should be done or not. This option can be @code{nil} (no smoothing),
@code{t} (use smoothing) or a predicate function that's called with
the image object as the only parameter, and should return either
@code{nil} or @code{t}. The default is for down-scaling to apply
smoothing, and for large up-scaling to not apply smoothing.

@item :index @var{frame}
@xref{Multi-Frame Images}.
Expand Down
6 changes: 6 additions & 0 deletions etc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,12 @@ and nil to disable smoothing.
The default behaviour of smoothing on down-scaling and not smoothing
on up-scaling remains unchanged.

+++
*** New user option 'image-transform-smoothing'.
This controls whether to use smoothing or not for an image. Values
include nil (no smoothing), t (do smoothing) or a predicate function
that's called with the image object and should return nil/t.

** EWW

+++
Expand Down
51 changes: 46 additions & 5 deletions lisp/image.el
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ based on the font pixel size."
(const :tag "Automatically compute" auto))
:version "26.1")

(defcustom image-transform-smoothing #'image--default-smoothing
"Whether to do smoothing when applying transforms to images.
Common transforms are rescaling and rotation.
Valid values are nil (no smoothing), t (smoothing) or a predicate
function that is called with the image specification and should return
either nil or non-nil."
:type '(choice (const :tag "Do smoothing" t)
(const :tag "No smoothing" nil)
function)
:version "28.1")

(defcustom image-use-external-converter nil
"If non-nil, `create-image' will use external converters for exotic formats.
Emacs handles most of the common image formats (SVG, JPEG, PNG, GIF
Expand Down Expand Up @@ -485,11 +497,40 @@ Image file names that are not absolute are searched for in the
type 'png
data-p t)))
(when (image-type-available-p type)
(append (list 'image :type type (if data-p :data :file) file-or-data)
(and (not (plist-get props :scale))
(list :scale
(image-compute-scaling-factor image-scaling-factor)))
props)))
(let ((image
(append (list 'image :type type (if data-p :data :file)
file-or-data)
(and (not (plist-get props :scale))
;; Add default scaling.
(list :scale
(image-compute-scaling-factor
image-scaling-factor)))
props)))
;; Add default smoothing.
(unless (plist-member props :transform-smoothing)
(setq image (nconc image
(list :transform-smoothing
(pcase image-transform-smoothing
('t t)
('nil nil)
(func (funcall func image)))))))
image)))

(defun image--default-smoothing (image)
"Say whether IMAGE should be smoothed when transformed."
(let* ((props (nthcdr 5 image))
(scaling (plist-get props :scale))
(rotation (plist-get props :rotation)))
(cond
;; We always smooth when scaling down and small upwards scaling.
((and scaling (< scaling 2))
t)
;; Smooth when doing non-90-degree rotation
((and rotation
(or (not (zerop (mod rotation 1)))
(not (zerop (% (truncate rotation) 90)))))
t)
(t nil))))

(defun image--set-property (image property value)
"Set PROPERTY in IMAGE to VALUE.
Expand Down

0 comments on commit 5dff53f

Please sign in to comment.