* lsdb.el (lsdb-expose-x-face): Fix the last change.
[elisp/lsdb.git] / xbm-thumb.el
1 ;;; xbm-thumb.el --- create XBM thumbnail under Emacs.
2 ;; Copyright (C) 2000 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 2000-02-26
6 ;; Keywords: xbm, image
7
8 ;; This file is not part of any package.
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25
26 ;;; Commentary:
27 ;; 
28
29 ;;; Code:
30
31 (defvar xbm-thumb-dot-threshold 1)
32
33 (defun xbm-thumb-fold-left (function accu sequence)
34   (if (null sequence) accu
35     (xbm-thumb-fold-left
36      function (funcall function accu (car sequence))
37      (cdr sequence))))
38
39 (defun xbm-thumb-aggregate-block (b1 b2)
40   (let ((idx 128) (result 0))
41     (while (> idx 1)
42       (setq result
43             (logior (lsh result 1)
44                     (if (< xbm-thumb-dot-threshold
45                            (xbm-thumb-fold-left
46                             #'+ 0 (list
47                                    (logand b1 idx)
48                                    (logand b1 (lsh idx -1))
49                                    (logand b2 idx)
50                                    (logand b2 (lsh idx -1)))))
51                         1 0))
52             idx (lsh idx -2)))
53     result))
54
55 (defun xbm-thumb-aggregate-row (row)
56   (let ((len (/ (length row) 2))
57         (result "")
58         (i 0))
59     (while (< i len)
60       (setq result
61             (format "%s\\x%02x" result
62                     (logior
63                      (lsh (xbm-thumb-aggregate-block
64                            (aref row (1+ i)) (aref row (+ i 1 len))) 4)
65                      (xbm-thumb-aggregate-block
66                       (aref row i) (aref row (+ i len)))))
67             i (+ i 2)))
68     result))
69
70 ;;;###autoload
71 (defun xbm-make-thumbnail (data)
72   "Create XBM thumbnail."
73   (let* ((string (nth 2 data))
74          (len (length string))
75          (width (/ (car data) 8))
76          (result "")
77          (i 0))
78     (while (< i len)
79       (setq result
80             (concat result
81                     (xbm-thumb-aggregate-row
82                      (substring string i (setq i (+ i (* 2 width))))))))
83     (list
84      (/ (car data) 2) (/ (nth 1 data) 2)
85      (car (read-from-string (concat "\"" result "\""))))))
86
87 (provide 'xbm-thumb)
88
89 ;;; xbm-thumb.el ends here