Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 267 lines (238 sloc) 8.35 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
;;; w3m-image.el --- Image conversion routines.

;; Copyright (C) 2001, 2002, 2003, 2005, 2007, 2008
;; TSUCHIYA Masatoshi <tsuchiya@namazu.org>

;; Authors: Yuuichi Teranishi <teranisi@gohome.org>
;; Keywords: w3m, WWW, hypermedia

;; This file is a part of emacs-w3m.

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.


;;; Commentary:

;; This file contains the stuffs to convert images for emacs-w3m.
;; For more detail about emacs-w3m, see:
;;
;; http://emacs-w3m.namazu.org/
;;
;; Routines in this file require ImageMagick's convert.
;; For more detail about ImageMagick, see:
;;
;; http://www.imagemagick.org/

;;; Code:

(eval-when-compile
  (require 'cl))

(require 'w3m-util)
(require 'w3m-proc)

(eval-when-compile
  (if (not (fboundp 'defcustom))
      (require 'pcustom)))

;; Functions and variables which should be defined in the other module
;; at run-time.
(eval-when-compile
  (defvar w3m-async-exec)
  (defvar w3m-current-url)
  (defvar w3m-profile-directory)
  (defvar w3m-work-buffer-name)
  (defvar w3m-work-buffer-list))

(defcustom w3m-imagick-convert-program (if noninteractive
nil
(w3m-which-command "convert"))
  "*Program name of ImageMagick's `convert'."
  :group 'w3m
  :set (lambda (symbol value)
(custom-set-default symbol (if (and (not noninteractive)
value)
(if (file-name-absolute-p value)
(if (file-executable-p value)
value)
(w3m-which-command value)))))
  :type 'file)

(defcustom w3m-imagick-identify-program (if noninteractive
nil
(w3m-which-command "identify"))
  "*Program name of ImageMagick's `identify'."
  :group 'w3m
  :set (lambda (symbol value)
(custom-set-default symbol (if (and (not noninteractive)
value)
(if (file-name-absolute-p value)
(if (file-executable-p value)
value)
(w3m-which-command value)))))
  :type 'file)

;;; Image handling functions.
(defcustom w3m-resize-images (and w3m-imagick-convert-program t)
  "*If non-nil, resize images to the specified width and height."
  :group 'w3m
  :set (lambda (symbol value)
(custom-set-default symbol (and w3m-imagick-convert-program value)))
  :type 'boolean)

(put 'w3m-imagick-convert-program 'available-p 'unknown)

(defun w3m-imagick-convert-program-available-p ()
  "Return non-nil if ImageMagick's `convert' program is available.
If not, `w3m-imagick-convert-program' and `w3m-resize-images' are made
nil forcibly."
  (cond ((eq (get 'w3m-imagick-convert-program 'available-p) 'yes)
t)
((eq (get 'w3m-imagick-convert-program 'available-p) 'no)
nil)
((and (stringp w3m-imagick-convert-program)
(file-executable-p w3m-imagick-convert-program))
(put 'w3m-imagick-convert-program 'available-p 'yes)
;; Check whether convert supports png32.
(put 'w3m-imagick-convert-program 'png32
(unless (or (featurep 'xemacs)
(< emacs-major-version 22))
(with-temp-buffer
(set-buffer-multibyte nil)
(insert "P1 1 1 1")
(condition-case nil
(call-process-region (point-min) (point-max)
w3m-imagick-convert-program
t t nil "pbm:-" "png32:-")
(error))
(goto-char (point-min))
(looking-at "\211PNG\r\n"))))
t)
(t
(when w3m-imagick-convert-program
(message "ImageMagick's `convert' program is not available")
(sit-for 1))
(setq w3m-imagick-convert-program nil
w3m-resize-images nil)
(put 'w3m-imagick-convert-program 'available-p 'no)
nil)))

;;; Synchronous image conversion.
(defun w3m-imagick-convert-buffer (from-type to-type &rest args)
  (when (w3m-imagick-convert-program-available-p)
    (let* ((in-file (make-temp-name
(expand-file-name "w3mel" w3m-profile-directory)))
(buffer-file-coding-system 'binary)
(coding-system-for-read 'binary)
(coding-system-for-write 'binary)
(default-process-coding-system (cons 'binary 'binary))
return)
      (write-region (point-min) (point-max) in-file nil 'nomsg)
      (erase-buffer)
      (setq return
(apply 'call-process
w3m-imagick-convert-program
nil t nil
(append args (list
(concat
(if from-type
(concat from-type ":"))
in-file)
(if to-type
(if (and (string-equal to-type "png")
(get 'w3m-imagick-convert-program
'png32))
"png32:-"
(concat to-type ":-"))
"-")))))
      (when (file-exists-p in-file) (delete-file in-file))
      (if (and (numberp return)
(zerop return))
t
(message "Image conversion failed (code `%s')"
(if (stringp return)
(string-as-multibyte return)
return))
nil))))

(defun w3m-imagick-convert-data (data from-type to-type &rest args)
  (with-temp-buffer
    (set-buffer-multibyte nil)
    (insert data)
    (and (apply 'w3m-imagick-convert-buffer from-type to-type args)
(not (zerop (buffer-size)))
(buffer-string))))

;;; Asynchronous image conversion.
(defun w3m-imagick-start-convert-data (handler
data from-type to-type &rest args)
  (w3m-process-do-with-temp-buffer
      (success (when (w3m-imagick-convert-program-available-p)
(set-buffer-multibyte nil)
(insert data)
(apply 'w3m-imagick-start-convert-buffer
handler from-type to-type args)))
    (if (and success
(not (zerop (buffer-size))))
(buffer-string))))

(defun w3m-imagick-start-convert-buffer (handler from-type to-type &rest args)
  (lexical-let ((in-file (make-temp-name
(expand-file-name "w3mel" w3m-profile-directory)))
(out-buffer (current-buffer)))
    (setq w3m-current-url "non-existent")
    (let ((coding-system-for-write 'binary)
(buffer-file-coding-system 'binary)
jka-compr-compression-info-list
format-alist)
      (write-region (point-min) (point-max) in-file nil 'nomsg))
    (w3m-process-do
(success (with-current-buffer out-buffer
(erase-buffer)
(w3m-process-start
handler
w3m-imagick-convert-program
(append args
(list
(concat
(if from-type
(concat from-type ":"))
in-file)
(if to-type
(if (and (string-equal to-type "png")
(get 'w3m-imagick-convert-program
'png32))
"png32:-"
(concat to-type ":-"))
"-"))))))
      (when (file-exists-p in-file)
(delete-file in-file))
      success)))

(defun w3m-resize-image (data width height handler)
  "Resize image DATA to WIDTH and HEIGHT asynchronously.
HANDLER is called after conversion with resized data as an argument."
  (w3m-process-do
      (result (w3m-imagick-start-convert-data
handler
data nil nil "-geometry"
(concat (number-to-string width)
"x"
(number-to-string height)
"!")))
    result))

(defun w3m-resize-image-by-rate (data rate handler)
  "Resize image DATA at RATE asynchronously.
HANDLER is called after conversion with resized data as an argument.
Note that this function requires that the `convert' program allows the
`-resize' option."
  (w3m-process-do
      (result (w3m-imagick-start-convert-data
handler
data nil nil "-resize"
(concat (number-to-string rate) "%")))
    result))

(defun w3m-favicon-usable-p ()
  "Check whether ImageMagick's `convert' supports a Windoze ico format in
a large number of bits per pixel."
  (let ((xpm (condition-case nil
(w3m-imagick-convert-data
(string 0 0 1 0 1 0 2 1 0 0 1 0 24 0 52 0
0 0 22 0 0 0 40 0 0 0 2 0 0 0 2 0
0 0 1 0 24 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 255 255 255 0 0 0 0 0 0)
"ico" "xpm")
(error nil))))
    (and xpm (string-match "\"2 1 2 1\"" xpm) t)))

(provide 'w3m-image)

;;; w3m-image.el ends here
Something went wrong with that request. Please try again.