-
Notifications
You must be signed in to change notification settings - Fork 2
/
xpm-to-gif.lisp
73 lines (67 loc) · 3.13 KB
/
xpm-to-gif.lisp
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
;;;; Copyright © 2006 Jeremy English <jhe@jeremyenglish.org>
;;;;
;;;; Permission to use, copy, modify, distribute, and sell this software and its
;;;; documentation for any purpose is hereby granted without fee, provided that
;;;; the above copyright notice appear in all copies and that both that
;;;; copyright notice and this permission notice appear in supporting
;;;; documentation. No representations are made about the suitability of this
;;;; software for any purpose. It is provided "as is" without express or
;;;; implied warranty.
;;;;
;;;; Created: 18-December-2006
(in-package :xpm-to-gif)
(defun rgb-color-string (color)
"Returns a color string if one is found for this color. If the color
passed is none then none is returned. If a color cannot be found we
return nil."
(if (string-equal color "none")
color
(let* ((color-type (color-type color))
(color-string
(if (equal color-type 'rgb)
color
(if (equal color-type 'name)
(x11-rgb-string color)
nil))))
color-string)))
(defun xpm-to-canvas (xpm color-table &key (none-color 0))
"Takes an xpm reader and return a skippy canvas and color-table."
(let ((canvas (make-canvas :height (height xpm) :width (width xpm))))
(loop for y from 0 to (1- (height xpm)) do
(loop for x from 0 to (1- (width xpm)) do
(let ((color
(rgb-color-string (color xpm (pixel-key xpm x y)))))
(if (equal (color-type color) 'rgb)
(multiple-value-bind (r g b)
(parse-rgb color)
(let ((c-index
(ensure-color (rgb-color r g b)
color-table)))
(setf (pixel-ref canvas x y) c-index)))
(when (string-equal color "none")
(let ((c-index
(ensure-color none-color
color-table)))
(setf (pixel-ref canvas x y) c-index)))))))
(values canvas color-table)))
(defun xpm-file-to-canvas (file-name color-table &key (none-color 0))
"Takes a gif file name, color-table and a optional none-color. The
none-color is the color you want to use for xpm colors set to None. A
skippy canvas is returned."
(xpm-to-canvas
(make-xpm-reader file-name) color-table :none-color none-color))
(defun xpm-file-to-gif (input-file output-file &key (none-color 0))
"Takes the name of a gif input file, xpm output file and a optional
none-color. The none-color is the color you want to use for xpm colors
set to None."
(multiple-value-bind (canvas color-table)
(xpm-file-to-canvas input-file
(make-color-table)
:none-color none-color)
(let ((data-stream
(make-data-stream
:color-table color-table
:width (width canvas)
:height (height canvas))))
(add-image (canvas-image canvas) data-stream)
(output-data-stream data-stream output-file))))