forked from edicl/cl-gd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
images.lisp
419 lines (389 loc) · 19.7 KB
/
images.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: CL-GD; Base: 10 -*-
;;; $Header: /usr/local/cvsrep/gd/images.lisp,v 1.34 2009/11/23 17:05:39 edi Exp $
;;; Copyright (c) 2003-2009, Dr. Edmund Weitz. All rights reserved.
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;; * Redistributions of source code must retain the above copyright
;;; notice, this list of conditions and the following disclaimer.
;;; * Redistributions in binary form must reproduce the above
;;; copyright notice, this list of conditions and the following
;;; disclaimer in the documentation and/or other materials
;;; provided with the distribution.
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
(in-package :cl-gd)
(defmethod print-object ((image image) stream)
(print-unreadable-object (image stream :identity t :type t)
(format stream "ptr ~X size ~D/~D (~:[true color~;~:*~D color~:P~])"
(uffi:pointer-address (img image))
(image-width image) (image-height image)
(unless (true-color-p image)
(number-of-colors :image image)))))
(defun create-image (width height &optional true-color)
"Allocates and returns a GD image structure with size WIDTH x
HEIGHT. Creates a true color image if TRUE-COLOR is true. You are
responsible for destroying the image after you're done with it. It is
advisable to use WITH-IMAGE instead."
(check-type width integer)
(check-type height integer)
(let ((image-ptr
(if true-color
(gd-image-create-true-color width height)
(gd-image-create width height))))
(when (null-pointer-p image-ptr)
(error "Could not allocate image of size ~A x ~A" width height))
(let ((image (make-image image-ptr)))
image)))
(defun destroy-image (image)
"Destroys \(deallocates) IMAGE which has been created by
CREATE-IMAGE, CREATE-IMAGE-FROM-FILE, or CREATE-IMAGE-FROM-GD2-PART."
(check-type image image)
(gd-image-destroy (img image))
nil)
(defmacro with-default-image ((image) &body body)
"Executes BODY with *DEFAULT-IMAGE* bound to IMAGE so that you don't
have to provide the IMAGE keyword/optional argument to CL-GD
functions."
`(let ((*default-image* ,image))
,@body))
(defmacro with-image ((name width height &optional true-color) &body body)
"Creates an image with size WIDTH x HEIGHT, and executes BODY with
the image bound to NAME. If TRUE-COLOR is true, creates a true color
image. The image is guaranteed to be destroyed before this macro
exits."
;; we rebind everything so we have left-to-right evaluation
(with-rebinding (width height true-color)
`(with-safe-alloc (,name
(create-image ,width ,height ,true-color)
(destroy-image ,name))
,@body)))
(defmacro with-image* ((width height &optional true-color) &body body)
"Creates an image with size WIDTH x HEIGHT and executes BODY with
the image bound to *DEFAULT-IMAGE*. If TRUE-COLOR is true, creates a
true color image. The image is guaranteed to be destroyed before this
macro exits."
`(with-image (*default-image* ,width ,height ,true-color)
,@body))
(defun create-image-from-file (file-name &optional type)
"Creates an image from the file specified by FILE-NAME \(which is
either a pathname or a string). The type of the image can be provided
as TYPE or otherwise it will be guessed from the PATHNAME-TYPE of
FILE-NAME. You are responsible for destroying the image after you're
done with it. It is advisable to use WITH-IMAGE-FROM-FILE instead."
(check-type file-name (or pathname string))
(let* ((pathname-type (pathname-type file-name))
(%type (or type
(cond ((or (string-equal pathname-type "jpg")
(string-equal pathname-type "jpeg"))
:jpg)
((string-equal pathname-type "png")
:png)
((string-equal pathname-type "gd")
:gd)
((string-equal pathname-type "gd2")
:gd2)
((string-equal pathname-type "xbm")
:xbm)
#-:win32
((string-equal pathname-type "xpm")
:xpm)
#-:cl-gd-no-gif
((string-equal pathname-type "gif")
:gif)))))
(unless %type
(error "No type provided and it couldn't be guessed from filename"))
(unless (probe-file file-name)
(error "File ~S could not be found" file-name))
(when (pathnamep file-name)
(setq file-name
#+:cmu (ext:unix-namestring file-name)
#-:cmu (namestring file-name)))
(with-foreign-object (err :int)
(with-cstring (c-file-name file-name)
(let ((image (ecase %type
((:jpg :jpeg)
(gd-image-create-from-jpeg-file c-file-name err))
((:png)
(gd-image-create-from-png-file c-file-name err))
((:gd)
(gd-image-create-from-gd-file c-file-name err))
((:gd2)
(gd-image-create-from-gd2-file c-file-name err))
((:xbm)
(gd-image-create-from-xbm-file c-file-name err))
#-:win32
((:xpm)
(gd-image-create-from-xpm c-file-name))
#-:cl-gd-no-gif
((:gif)
(gd-image-create-from-gif-file c-file-name err)))))
(cond ((null-pointer-p image)
(cond ((or (eq %type :xpm)
(zerop (deref-pointer err :int)))
(error "Could not create image from ~A file ~S"
%type file-name))
(t
(error "Could not create image from ~A file ~S: errno was ~A"
%type file-name (deref-pointer err :int)))))
(t (let ((image (make-image image)))
image))))))))
(defmacro with-image-from-file ((name file-name &optional type) &body body)
"Creates an image from the file specified by FILE-NAME \(which is
either a pathname or a string) and executes BODY with the image bound
to NAME. The type of the image can be provied as TYPE or otherwise it
will be guessed from the PATHNAME-TYPE of FILE-NAME. The image is
guaranteed to be destroyed before this macro exits."
;; we rebind everything so we have left-to-right evaluation
(with-rebinding (file-name type)
`(with-safe-alloc (,name
(create-image-from-file ,file-name ,type)
(destroy-image ,name))
,@body)))
(defmacro with-image-from-file* ((file-name &optional type) &body body)
"Creates an image from the file specified by FILE-NAME \(which is
either a pathname or a string) and executes BODY with the image bound
to *DEFAULT-IMAGE*. The type of the image can be provied as TYPE or
otherwise it will be guessed from the PATHNAME-TYPE of FILE-NAME. The
image is guaranteed to be destroyed before this macro exits."
`(with-image-from-file (*default-image* ,file-name ,type)
,@body))
(defun create-image-from-gd2-part (file-name src-x src-y width height)
"Creates an image from the part of the GD2 file FILE-NAME \(which is
either a pathname or a string) specified by SRC-X, SRC-Y, WIDTH, and
HEIGHT. You are responsible for destroying the image after you're done
with it. It is advisable to use WITH-IMAGE-FROM-GD2-PART instead."
(check-type file-name (or string pathname))
(check-type src-x integer)
(check-type src-y integer)
(check-type width integer)
(check-type height integer)
(unless (probe-file file-name)
(error "File ~S could not be found" file-name))
(when (pathnamep file-name)
(setq file-name
#+:cmu (ext:unix-namestring file-name)
#-:cmu (namestring file-name)))
(with-foreign-object (err :int)
(with-cstring (c-file-name file-name)
(let ((image (gd-image-create-from-gd2-part-file c-file-name err src-x src-y width height)))
(cond ((null-pointer-p image)
(error "Could not create GD2 image from file ~S: errno was ~A"
file-name (deref-pointer err :int)))
(t image))))))
(defmacro with-image-from-gd2-part ((name file-name src-x src-y width height) &body body)
"Creates an image from the part of the GD2 file FILE-NAME \(which is
either a pathname or a string) specified SRC-X, SRC-Y, WIDTH, and
HEIGHT and executes BODY with the image bound to NAME. The type of the
image can be provied as TYPE or otherwise it will be guessed from the
PATHNAME-TYPE of FILE-NAME. The image is guaranteed to be destroyed
before this macro exits."
;; we rebind everything so we have left-to-right evaluation
(with-rebinding (file-name src-x src-y width height)
`(with-safe-alloc (,name
(create-image-from-gd2-part ,file-name ,src-x ,src-y ,width ,height)
(destroy-image ,name))
,@body)))
(defmacro with-image-from-gd2-part* ((file-name src-x src-y width height) &body body)
"Creates an image from the part of the GD2 file FILE-NAME \(which is
either a pathname or a string) specified SRC-X, SRC-Y, WIDTH, and
HEIGHT and executes BODY with the image bound to *DEFAULT-IMAGE*. The
type of the image can be provied as TYPE or otherwise it will be
guessed from the PATHNAME-TYPE of FILE-NAME. The image is guaranteed
to be destroyed before this macro exits."
`(with-image-from-gd2-part (*default-image* ,file-name ,src-x ,src-y ,width ,height)
,@body))
(defmacro make-stream-fn (name signature gd-call type-checks docstring)
"Internal macro used to generate WRITE-JPEG-TO-STREAM and friends."
`(defun ,name ,signature
,docstring
,@type-checks
(cond ((or #+(and :allegro :allegro-version>= (version>= 6 0))
(typep stream 'excl:simple-stream)
#+:lispworks4.3
(subtypep 'base-char (stream-element-type stream))
(subtypep '(unsigned-byte 8) (stream-element-type stream)))
(with-foreign-object (size :int)
(with-safe-alloc (memory ,gd-call (gd-free memory))
(let (#+:lispworks4.3
(temp-array (make-array 1 :element-type
'(unsigned-byte 8))))
(with-cast-pointer (temp memory :unsigned-byte)
(dotimes (i (deref-pointer size :int))
;; LispWorks workaround, WRITE-BYTE won't work - see
;; <http://article.gmane.org/gmane.lisp.lispworks.general/1827>
#+:lispworks4.3
(setf (aref temp-array 0)
(deref-array temp '(:array :unsigned-byte) i))
#+:lispworks4.3
(write-sequence temp-array stream)
#-:lispworks4.3
(write-byte (deref-array temp '(:array :unsigned-byte) i)
stream))
image)))))
((subtypep 'character (stream-element-type stream))
(with-foreign-object (size :int)
(with-safe-alloc (memory ,gd-call (gd-free memory))
(with-cast-pointer (temp memory
#+(or :cmu :scl :sbcl) :unsigned-char
#-(or :cmu :scl :sbcl) :char)
(dotimes (i (deref-pointer size :int))
(write-char (ensure-char-character
(deref-array temp '(:array :char) i))
stream))
image))))
(t (error "Can't use a stream with element-type ~A"
(stream-element-type stream))))))
(make-stream-fn write-jpeg-to-stream (stream &key (quality -1) (image *default-image*))
(gd-image-jpeg-ptr (img image) size quality)
((check-type stream stream)
(check-type quality (integer -1 100))
(check-type image image))
"Writes image IMAGE to stream STREAM as JPEG. If
QUALITY is not specified, the default IJG JPEG quality value is
used. Otherwise, for practical purposes, quality should be a value in
the range 0-95. STREAM must be a character stream or a binary stream
of element type \(UNSIGNED-BYTE 8). If STREAM is a character stream,
the user of this function has to make sure the external format is
yields faithful output of all 8-bit characters.")
(make-stream-fn write-png-to-stream (stream &key compression-level (image *default-image*))
(cond (compression-level
(gd-image-png-ptr-ex (img image) size compression-level))
(t
(gd-image-png-ptr (img image) size)))
((check-type stream stream)
(check-type compression-level (or null (integer -1 9)))
(check-type image image))
"Writes image IMAGE to stream STREAM as PNG. If
COMPRESSION-LEVEL is not specified, the default compression level at
the time zlib was compiled on your system will be used. Otherwise, a
compression level of 0 means 'no compression', a compression level of
1 means 'compressed, but as quickly as possible', a compression level
of 9 means 'compressed as much as possible to produce the smallest
possible file.' STREAM must be a character stream or a binary stream
of element type \(UNSIGNED-BYTE 8). If STREAM is a character stream,
the user of this function has to make sure the external format yields
faithful output of all 8-bit characters.")
#-:cl-gd-no-gif
(make-stream-fn write-gif-to-stream (stream &key (image *default-image*))
(gd-image-gif-ptr (img image) size)
((check-type stream stream)
(check-type image image))
"Writes image IMAGE to stream STREAM as GIF. STREAM
must be a character stream or a binary stream of element type
\(UNSIGNED-BYTE 8). If STREAM is a character stream, the user of this
function has to make sure the external format yields faithful output
of all 8-bit characters.")
(make-stream-fn write-wbmp-to-stream (stream &key foreground (image *default-image*))
(gd-image-wbmp-ptr (img image) size foreground)
((check-type stream stream)
(check-type foreground integer)
(check-type image image))
"Writes image IMAGE to stream STREAM as WBMP. STREAM
must be a character stream or a binary stream of element type
\(UNSIGNED-BYTE 8). If STREAM is a character stream, the user of this
function has to make sure the external format yields faithful output
of all 8-bit characters. WBMP file support is black and white
only. The color index specified by the FOREGOUND argument is the
\"foreground,\" and only pixels of this color will be set in the WBMP
file")
(make-stream-fn write-gd-to-stream (stream &key (image *default-image*))
(gd-image-gd-ptr (img image) size)
((check-type stream stream)
(check-type image image))
"Writes image IMAGE to stream STREAM as GD. STREAM
must be a character stream or a binary stream of element type
\(UNSIGNED-BYTE 8). If STREAM is a character stream, the user of this
function has to make sure the external format yields faithful output
of all 8-bit characters.")
(make-stream-fn write-gd2-to-stream (stream &key (image *default-image*))
(gd-image-gd2-ptr (img image) size)
((check-type stream stream)
(check-type image image))
"Writes image IMAGE to stream STREAM as GD2. STREAM
must be a character stream or a binary stream of element type
\(UNSIGNED-BYTE 8). If STREAM is a character stream, the user of this
function has to make sure the external format yields faithful output
of all 8-bit characters.")
(defun write-image-to-stream (stream type &rest rest &key &allow-other-keys)
"Writes image to STREAM. The type of the image is determined by TYPE
which must be one of :JPG, :JPEG, :PNG, :WBMP, :GD, or :GD2. STREAM
must be a character stream or a binary stream of element type
\(UNSIGNED-BYTE 8). If STREAM is a character stream, the user of this
function has to make sure the external format yields faithful output
of all 8-bit characters."
(apply (ecase type
((:jpg :jpeg)
#'write-jpeg-to-stream)
((:png)
#'write-png-to-stream)
((:wbmp)
#'write-wbmp-to-stream)
((:gd)
#'write-gd-to-stream)
((:gd2)
#'write-gd2-to-stream)
#-:cl-gd-no-gif
((:gif)
#'write-gif-to-stream))
stream rest))
(defun write-image-to-file (file-name &rest rest &key type (if-exists :error) &allow-other-keys)
"Writes image to the file specified by FILE-NAME \(a pathname or a
string). The TYPE argument is interpreted as in
WRITE-IMAGE-TO-STREAM. If it is not provided it is guessed from the
PATHNAME-TYPE of FILE-NAME. The IF-EXISTS keyword argument is given to
OPEN. Other keyword argument like QUALITY or COMPRESSION-LEVEL can be
provided depending on the images's type."
(with-open-file (stream file-name :direction :output
:if-exists if-exists
:element-type '(unsigned-byte 8))
(apply #'write-image-to-stream
stream
(or type
(let ((pathname-type (pathname-type (truename file-name))))
(cond ((or (string-equal pathname-type "jpg")
(string-equal pathname-type "jpeg"))
:jpg)
((string-equal pathname-type "png")
:png)
((string-equal pathname-type "wbmp")
:wbmp)
((string-equal pathname-type "gd")
:gd)
((string-equal pathname-type "gd2")
:gd2)
#-:cl-gd-no-gif
((string-equal pathname-type "gif")
:gif)
(t
(error "Can't determine the type of the image")))))
(sans rest :type :if-exists))))
(defun image-width (&optional (image *default-image*))
"Returns width of IMAGE."
(check-type image image)
(with-transformed-alternative
(((gd-image-get-sx (img image)) w-inv-transformer))
(gd-image-get-sx (img image))))
(defun image-height (&optional (image *default-image*))
(check-type image image)
"Returns height of IMAGE."
(with-transformed-alternative
(((gd-image-get-sy (img image)) h-inv-transformer))
(gd-image-get-sy (img image))))
(defun image-size (&optional (image *default-image*))
(check-type image image)
"Returns width and height of IMAGE as two values."
(with-transformed-alternative
(((gd-image-get-sx (img image)) w-inv-transformer)
((gd-image-get-sy (img image)) h-inv-transformer))
(values (gd-image-get-sx (img image))
(gd-image-get-sy (img image)))))