(defvar mew-mime-image-cache-length 30 "*Cache size")
(defvar mew-mime-image-max-coeff 0.9 "*Frame に対する画像の最大値の割合")
(defvar mew-mime-image-cache nil)
(defun mew-mime-image (cache begin end format)
"hoge"
(message "Loading image...")
(set-buffer (mew-buffer-message))
(let* ((fld (mew-minfo-get-summary))
(msg (mew-current-get-msg (mew-frame-id)))
(part (mew-current-get-part (mew-frame-id)))
(key `(,fld ,msg ,part ,begin ,end))
(maxwidth (truncate (* (frame-pixel-width (selected-frame))
mew-mime-image-max-coeff)))
(maxheight (truncate (* (frame-pixel-height (selected-frame))
mew-mime-image-max-coeff)))
original img conv width heigth x y)
(mew-erase-buffer)
(condition-case nil
(if (setq img (assoc key mew-mime-image-cache))
(mew-elet
(setq mew-mime-image-cache
(cons img (delete img mew-mime-image-cache)))
(setq conv 'cache)
(insert-image (cdr img)))
(with-temp-buffer
(save-excursion
(set-buffer cache)
(setq img (mew-buffer-substring begin end))
(setq original img))
(insert img)
(buffer-disable-undo)
(set-buffer-multibyte nil)
(mew-flet
(apply 'call-process-region
(point-min) (point-max)
"identify" t (current-buffer) nil '("-"))
(goto-char (point-min))
(setq width 1)
(setq heigth 1)
(when (re-search-forward " \\([1-9][0-9]*\\)x\\([1-9][0-9]*\\)+" nil t)
(setq width (string-to-number (match-string 1)))
(setq heigth (string-to-number (match-string 2))))
(when (or (> width maxwidth) (> heigth maxheight))
(erase-buffer)
(setq conv t)
(setq x (/ width (float maxwidth)))
(setq y (/ heigth (float maxheight)))
(setq width (floor (/ width (max x y))))
(setq heigth (floor (/ heigth (max x y))))
(insert img)
(message "Loading image(convert)...")
(apply 'call-process-region
(point-min) (point-max)
"convert" t (current-buffer) nil
(list "-sample"
(format "%dx%d!" width heigth)
(format "%s:-" format)
(format "%s:-" format)))
(setq img (buffer-substring (point-min) (point-max))))))
(setq img (create-image (string-as-unibyte img) nil t))
(setq mew-mime-image-cache (cons (cons key img) mew-mime-image-cache))
(when (> (length mew-mime-image-cache) mew-mime-image-cache-length)
(setcdr (nthcdr (1- mew-mime-image-cache-length) mew-mime-image-cache) nil))
(mew-elet (insert-image img)))
(error ()))
(goto-char (point-min))
(message "Loading image%s...done"
(cond
((eq conv 'cache) "(cache)")
(conv "(resize)")
(t "")))))