Subject: bug#13887: 24.3; doc-view will render blurry images when image-magick is available

From: Tassilo Horn <[email-masked]>
Date: Wed, 06 Mar 2013 20:43:16 +0100

E Sabof <[email-masked]> writes:

> This function gives the native pixel dimensions of an image:
>
> (defun es-image-file-pixel-dimensions (file)
>   (let* ((type (image-type file nil nil))
>          (spec (list 'image :type type :file file)))
>     (image-size spec t)))
>
> When
>
> (> (getf (cdr (doc-view-current-image)) :width)
>    (car (es-image-file-pixel-dimensions
>          (getf (cdr (doc-view-current-image)) :file))))
>
> there is quality loss.

Hm, ok.  Maybe I'll give it a whirl.

> What I don't want to see is margins having a different width - for the
> same reasons you wouldn't put a photograph in a frame in the top left
> corner.

Could you test-drive this patch.  It implements horizontal centering of
the image.

--8<---------------cut here---------------start------------->8---
=== modified file 'lisp/doc-view.el'
--- lisp/doc-view.el	2013-02-28 04:02:36 +0000
+++ lisp/doc-view.el	2013-03-06 19:35:43 +0000
@@ -1250,23 +1250,34 @@
   (when doc-view-pending-cache-flush
     (clear-image-cache)
     (setq doc-view-pending-cache-flush nil))
-  (let ((ol (doc-view-current-overlay))
-        (image (if (and file (file-readable-p file))
-		   (if (not (and doc-view-scale-internally
-                                 (fboundp 'imagemagick-types)))
-		       (apply 'create-image file doc-view--image-type nil args)
-		     (unless (member :width args)
-		       (setq args `(,@args :width ,doc-view-image-width)))
-		     (apply 'create-image file 'imagemagick nil args))))
-        (slice (doc-view-current-slice)))
+  (let* ((ol (doc-view-current-overlay))
+	 (image (if (and file (file-readable-p file))
+		    (if (not (and doc-view-scale-internally
+				  (fboundp 'imagemagick-types)))
+			(apply 'create-image file doc-view--image-type nil args)
+		      (unless (member :width args)
+			(setq args `(,@args :width ,doc-view-image-width)))
+		      (apply 'create-image file 'imagemagick nil args))))
+	 (slice (doc-view-current-slice))
+	 (img-width (car (image-size image)))
+	 (displayed-img-width (if slice
+				  (* (/ (float (nth 2 slice))
+					(car (image-size image 'pixels)))
+				     img-width)
+				img-width))
+	 (window-width (window-width (selected-window))))
     (setf (doc-view-current-image) image)
     (move-overlay ol (point-min) (point-max))
+    (when (> window-width displayed-img-width)
+      (overlay-put ol 'before-string
+    		   (propertize " " 'display
+    			       `(space :align-to (+ center (-0.5 . ,displayed-img-width))))))
     (overlay-put ol 'display
                  (cond
                   (image
                    (if slice
                        (list (cons 'slice slice) image)
-                     image))
+		     image))
                   ;; We're trying to display a page that doesn't exist.
                   (doc-view-current-converter-processes
                    ;; Maybe the page doesn't exist *yet*.
--8<---------------cut here---------------end--------------->8---

>  Ideally I'd also center vertically (should the image be shorter than
> the window)

I think vertical centering is a bit more tricky since there's nothing
like :align-to or vertically stretchable spaces...

Bye,
Tassilo