- (shell-quote-argument file)))))
-
-(defun gnus-convert-image-to-gray-x-face (file depth)
- (let* ((mapfile (make-temp-name (expand-file-name "gnus." mm-tmp-directory)))
- (levels (expt 2 depth))
- (step (/ 255 (1- levels)))
- color-alist bits bits-list mask pixel x-faces)
- (with-temp-file mapfile
- (insert "P3\n")
- (insert (format "%d 1\n" levels))
- (insert "255\n")
- (dotimes (i levels)
- (insert (format "%d %d %d\n"
- (* step i) (* step i) (* step i)))
- (push (cons (* step i) i) color-alist)))
- (when (file-exists-p file)
- (with-temp-buffer
- (insert (shell-command-to-string
- (format "giftopnm %s | ppmnorm 2>/dev/null | pnmscale -width 48 -height 48 | ppmquant -fs -map %s 2>/dev/null | ppmtopgm | pnmnoraw"
- (shell-quote-argument file)
- mapfile)))
- (goto-char (point-min))
- (forward-line 3)
- (while (setq pixel (ignore-errors (read (current-buffer))))
- (push (cdr (assq pixel color-alist)) bits-list))
- (setq bits-list (nreverse bits-list))
- (dotimes (bit-number depth)
- (setq mask (expt 2 bit-number))
- (with-temp-buffer
- (insert "P1\n48 48\n")
- (dolist (bits bits-list)
- (insert (if (zerop (logand bits mask)) "0 " "1 ")))
- (shell-command-on-region
- (point-min) (point-max)
- ;; the following is taken from xbmtoikon:
- "pbmtoicon | sed '/^[ ]*[*\\\\/]/d; s/[ ]//g; s/,$//' | tr , '\\012' | sed 's/^0x//; s/^/0x/' | pr -l1 -t -w22 -3 -s, | sed 's/,*$/,/' | compface"
- (current-buffer) t)
- (push (buffer-string) x-faces))))
- (dotimes (i (length x-faces))
- (insert (if (zerop i) "X-Face:" (format "X-Face-%s:" i))
- (nth i x-faces))))
- (delete-file mapfile)))
+ (shell-quote-argument (expand-file-name file))))))
+
+;;;###autoload
+(defun gnus-face-from-file (file)
+ "Return a Face header based on an image file.
+
+Depending on `gnus-convert-image-to-face-command' it may accept
+different input formats."
+ (interactive "fImage file name: ")
+ (when (file-exists-p file)
+ (let ((done nil)
+ (attempt "")
+ (quant 16))
+ (while (and (not done)
+ (> quant 1))
+ (setq attempt
+ (let ((coding-system-for-read 'binary))
+ (gnus-shell-command-to-string
+ (format gnus-convert-image-to-face-command
+ (shell-quote-argument (expand-file-name file))
+ quant))))
+ (if (> (length attempt) 726)
+ (progn
+ (setq quant (- quant (if (< quant 10) 1 2)))
+ (gnus-message 9 "Length %d; trying quant %d"
+ (length attempt) quant))
+ (setq done t)))
+ (if done
+ (mm-with-unibyte-buffer
+ (insert attempt)
+ (gnus-face-encode))
+ nil))))
+
+(defun gnus-face-encode ()
+ (let ((step 72))
+ (base64-encode-region (point-min) (point-max))
+ (goto-char (point-min))
+ (while (search-forward "\n" nil t)
+ (replace-match ""))
+ (goto-char (point-min))
+ (while (> (- (point-max) (point))
+ step)
+ (forward-char step)
+ (insert "\n ")
+ (setq step 76))
+ (buffer-string)))