-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffi-wand.el
3166 lines (2655 loc) · 110 KB
/
ffi-wand.el
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; ffi-wand.el --- FFI to libMagickWand -*- lexical-binding:t -*-
;; Copyright (C) 2016-2019 by Zajcev Evgeny
;; Author: Zajcev Evgeny <zevlg@yandex.ru>
;; Created: Wed Nov 30 00:40:03 2016
;; Keywords: ffi, multimedia
;; ffi-wand.el is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; ffi-wand.el is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with ffi-wand.el. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Check Emacs is built with imagemagick support:
;;
;; (memq 'imagemagick image-types) ==> non-nil
;;
;;; Code:
(require 'cl-macs)
(require 'ffi)
(defcustom wand-library-name "libMagickWand"
"*Library name for libMagickWand.
It might be \"libMagickWand-6.Q16\" or something like this."
:type 'string)
(define-ffi-library libmagickwand wand-library-name)
(define-ffi-array Wand-Array4096Type :char 4096)
(defvar Wand-BooleanType :long)
(define-ffi-struct Wand-PrivateType
(id :type :ulong)
(name :type Wand-Array4096Type)
(exception :type :pointer)
(image-info :type :pointer) ; ImageInfo*
(quantize-info :type :pointer)
(images :type :pointer)
(active :type Wand-BooleanType)
(pend :type Wand-BooleanType)
(debug :type Wand-BooleanType)
(signature :type :ulong))
(define-ffi-struct Wand-InfoType
(name :type :pointer)
(description :type :pointer)
(version :type :pointer)
(note :type :pointer)
(module :type :pointer)
(image-info :type :pointer)
(decoder :type :pointer)
(encoder :type :pointer)
(magick :type :pointer) ; IsImageFormatHandler
(client-date :type :pointer)
(adjoin :type Wand-BooleanType)
(raw :type Wand-BooleanType)
(endian-support :type Wand-BooleanType)
(blob-support :type Wand-BooleanType)
(seekable-stream :type Wand-BooleanType)
(thread-support :type :uint)
(stealth :type Wand-BooleanType)
;; deprecated, use GetMagickInfoList()
(previous :type :pointer)
(next :type :pointer)
(signature :type :ulong))
(defconst Wand-ExceptionType :int)
(define-ffi-struct Wand-ExceptionInfoType
(severity :type Wand-ExceptionType)
(error_number :type :int)
(reason :type :pointer)
(description :type :pointer)
(exceptions :type :pointer)
(relinquish :type Wand-BooleanType)
(semaphore :type :pointer)
(signature :type :ulong))
(define-ffi-struct Wand-PointInfoType
(x :type :double)
(y :type :double))
(defconst Wand-OrientationType :int)
(defmacro Wand-Orientation-get (kw)
(ecase kw
((:topleft :top-left) 1)
((:topright :top-right) 2)
((:bottomright :bottom-right) 3)
((:bottomleft :bottom-left) 4)
((:lefttop :left-top) 5)
((:righttop :right-top) 6)
((:rightbottom :right-bottom) 7)
((:leftbottom :left-bottom) 8)))
(define-ffi-function Wand:MagickWandGenesis "MagickWandGenesis" :void
nil libmagickwand)
;; (define-ffi-function Wand:MagickWandTerminus "MagickWandTerminus" :void
;; nil libmagickwand)
;;{{{ `-- Wand:version
(define-ffi-function Wand:GetMagickVersion "GetMagickVersion" :pointer
(:pointer) libmagickwand)
(defun Wand:version ()
"Return Image Magick version string."
(with-ffi-temporary (n :ulong)
(let ((ret (Wand:GetMagickVersion n)))
(unless (ffi-pointer-null-p ret)
(ffi-get-c-string ret)))))
;;}}}
;;{{{ `-- MagickWand operations
(define-ffi-function Wand:RelinquishMemory "MagickRelinquishMemory" :pointer
(:pointer) libmagickwand)
(defconst Wand-WandType :pointer)
(define-ffi-function Wand:acquire-id "AcquireWandId" :size_t
nil libmagickwand)
(define-ffi-function Wand:relinquish-id "RelinquishWandId" :void
(:size_t) libmagickwand)
;; Return a newly allocated MagickWand.
(define-ffi-function Wand:make-wand "NewMagickWand" Wand-WandType
nil libmagickwand)
;; Clear all resources associated with the WAND.
;; This does not free the memory, i.e. @var{wand} can furtherly be used
;; as a context, see `Wand:delete-wand'."
(define-ffi-function Wand:clear-wand "ClearMagickWand" :void
(Wand-WandType) libmagickwand)
;; Return a cloned copy of WAND.
(define-ffi-function Wand:copy-wand "CloneMagickWand" Wand-WandType
(Wand-WandType) libmagickwand)
;; Gets the image at the current image index.
(define-ffi-function Wand:get-image "MagickGetImage" Wand-WandType
(Wand-WandType) libmagickwand)
;; Extracts a region of the image and returns it as a a new wand.
(define-ffi-function Wand:image-region "MagickGetImageRegion" Wand-WandType
;; wand dx dy x y
(Wand-WandType :ulong :ulong :ulong :ulong) libmagickwand)
;; Delete the WAND.
;; This frees all resources associated with the WAND.
;; WARNING: Do not use WAND after calling this function!
(define-ffi-function Wand:destroy-wand "DestroyMagickWand" :void
(Wand-WandType) libmagickwand)
(defun Wand:delete-wand (wand)
;; Workaround some ugly bug, causing
;; magick/semaphore.c:290: LockSemaphoreInfo: Assertion `semaphore_info != (SemaphoreInfo *) ((void *)0)' failed
;; - make sure wand_semaphore is ok
(Wand:acquire-id)
(Wand:destroy-wand wand))
;; Return non-nil if WAND is a magick wand, nil otherwise.
(define-ffi-function Wand:wandp "IsMagickWand" Wand-BooleanType
(Wand-WandType) libmagickwand)
(defmacro Wand-with-wand (wand &rest forms)
"With allocated WAND do FORMS."
`(let ((,wand (Wand:make-wand)))
(unwind-protect
(progn ,@forms)
(Wand:delete-wand ,wand))))
(put 'Wand-with-wand 'lisp-indent-function 'defun)
;; MagickIdentifyImage() identifies an image by printing its
;; attributes to the file. Attributes include the image width, height,
;; size, and others.
(define-ffi-function Wand:MagickIdentifyImage "MagickIdentifyImage" :pointer
(Wand-WandType) libmagickwand)
(defun Wand:identify-image (wand)
"Return info about the image stored in WAND."
(let ((ii (Wand:MagickIdentifyImage wand)))
(unless (ffi-pointer-null-p ii)
(unwind-protect
(ffi-get-c-string ii)
(Wand:RelinquishMemory ii)))))
(define-ffi-function Wand:MagickGetException "MagickGetException" :pointer
(Wand-WandType :pointer) libmagickwand)
(defun Wand:exception (wand)
"Return reason of any error that occurs when using API."
(with-ffi-temporary (c-ext Wand-ExceptionType)
(ffi-get-c-string (Wand:MagickGetException wand c-ext))))
;;}}}
(define-ffi-function Wand:MagickReadImage "MagickReadImage" Wand-BooleanType
(Wand-WandType :pointer) libmagickwand)
(defun Wand:read-image (wand file)
"Read FILE and associate it with WAND.
Return non-nil if file has been loaded successfully."
(let ((fname (expand-file-name file)))
;; simple error catchers
(unless (file-readable-p fname)
(error "File unreadable %s" fname))
(when (zerop (Wand:wandp wand))
(error "Non-wand arg: %S" wand))
(with-ffi-string (fncstr fname)
(when (zerop (Wand:MagickReadImage wand fncstr))
(error "Can't read file %s: %s"
file (Wand:exception image-wand))))
t))
(defun Wand:read-image-data (wand data)
(with-ffi-string (dtcstr data)
(Wand:MagickReadImage wand dtcstr)))
(define-ffi-function Wand:MagickWriteImage "MagickWriteImage" Wand-BooleanType
(Wand-WandType :pointer) libmagickwand)
(defun Wand:write-image (wand file)
"Write the image associated with WAND to FILE."
(let ((fname (expand-file-name file)))
;; simple error catchers
(unless (file-writable-p fname)
(error "File unwritable %s" fname))
(when (zerop (Wand:wandp wand))
(error "Non-wand arg: %S" wand))
(with-ffi-string (fncstr fname)
(Wand:MagickWriteImage wand fncstr))))
(define-ffi-function Wand:GetImageBlob "MagickGetImageBlob" :pointer
(Wand-WandType :pointer) libmagickwand)
;; Use `Wand:RelinquishMemory' when done with blob
(defun Wand:image-blob (wand)
"Return WAND's direct image data according to format.
Use \(setf \(Wand:image-format w\) FMT\) to set format."
(with-ffi-temporary (len :uint)
(let ((data (Wand:GetImageBlob wand len))
(llen (ffi--mem-ref len :uint)))
(cons llen data))))
;; MagickResetImagePage() resets the Wand page canvas and position.
(define-ffi-function Wand:MagickResetImagePage "MagickResetImagePage"
Wand-BooleanType
(Wand-WandType :pointer) libmagickwand)
(defun Wand:reset-image-page (wand &optional geometry)
"Reset the WAND page canvas and position to GEOMETRY.
If GEOMETRY is ommited then 0x0+0+0 is used."
(with-ffi-string (cgeom (or geometry "0x0+0+0"))
(Wand:MagickResetImagePage wand cgeom)))
;; Magick Properties
(define-ffi-function Wand:GetMagickProperty "GetMagickProperty" :pointer
(:pointer :pointer :pointer) libmagickwand)
(defun Wand:get-magick-property (wand prop)
"From WAND get magick property PROP.
PROP can be one of: `base', `channels', `colorspace', `depth',
`directory', `extension', `height', `input', `magick', `name',
`page', `size', `width', `xresolution', `yresolution'."
(when (member prop '("group" "kurtosis" "max" "mean"
"min" "output" "scene" "skewness"
"standard-deviation" "standard_deviation"
"unique" "zero"))
(error "Unsupported magick property: %s" prop))
(with-ffi-string (cprop prop)
(let ((ret (Wand:GetMagickProperty
(ffi-null-pointer) (Wand-PrivateType-images wand)
cprop)))
(unless (ffi-pointer-null-p ret)
(ffi-get-c-string ret)))))
(defun Wand:image-orig-width (wand)
"Return original width of the image associated with WAND."
(string-to-number (Wand:get-magick-property wand "width")))
(defun Wand:image-orig-height (wand)
"Return original height of the image associated with WAND."
(string-to-number (Wand:get-magick-property wand "height")))
;;}}}
;;{{{ `-- Image properties
(defun Wand-fetch-relinquish-strings (strs slen)
"Fetch strings from strings array STRS of length SLEN."
(unless (ffi-pointer-null-p strs)
(unwind-protect
(loop for off from 0 below slen
collect (ffi-get-c-string (ffi-aref strs :pointer off)))
(Wand:RelinquishMemory strs))))
(define-ffi-function Wand:MagickGetImageProperties "MagickGetImageProperties" :pointer
(Wand-WandType :pointer :pointer) libmagickwand)
(defun Wand:image-properties (w pattern)
"Return list of image properties that match PATTERN."
(with-ffi-temporary (clen :ulong)
(with-ffi-string (cptr pattern)
(let ((props (Wand:MagickGetImageProperties w cptr clen)))
(Wand-fetch-relinquish-strings props (ffi--mem-ref clen :ulong))))))
(define-ffi-function Wand:MagickGetImageProperty "MagickGetImageProperty" :pointer
(Wand-WandType :pointer) libmagickwand)
(define-ffi-function Wand:MagickSetImageProperty "MagickSetImageProperty"
Wand-BooleanType
(Wand-WandType :pointer :pointer) libmagickwand)
(define-ffi-function Wand:MagickDeleteImageProperty "MagickDeleteImageProperty"
Wand-BooleanType
(Wand-WandType :pointer) libmagickwand)
(defun Wand:image-property (w property)
"Return value for PROPERTY.
Use \(setf \(Wand:image-property w prop\) VAL\) to set property."
(with-ffi-string (cprop property)
(let ((pv (Wand:MagickGetImageProperty w cprop)))
(unless (ffi-pointer-null-p pv)
(unwind-protect
(ffi-get-c-string pv)
(Wand:RelinquishMemory pv))))))
(defsetf Wand:image-property (w prop) (val)
(let ((vsym (cl-gensym "vsym-"))
(prop-c (cl-gensym))
(vsym-c (cl-gensym)))
`(let ((,vsym ,val))
(with-ffi-string (,prop-c ,prop)
(if ,vsym
(with-ffi-string (,vsym-c ,vsym)
(Wand:MagickSetImageProperty ,w ,prop-c ,vsym-c))
(Wand:MagickDeleteImageProperty ,w ,prop-c))))))
(define-ffi-function Wand:MagickGetQuantumRange "MagickGetQuantumRange" :pointer
(:pointer) libmagickwand)
(defun Wand:quantum-range ()
(with-ffi-temporary (qr :ulong)
(Wand:MagickGetQuantumRange qr)
(ffi--mem-ref qr :ulong)))
;; Very simple properties editor
(defun wand-prop-editor ()
"Run properties editor."
(interactive)
(let* ((iw image-wand)
(props (cl-remove-if-not
#'(lambda (prop)
(string-match wand-properties-pattern prop))
(Wand:image-properties iw ""))))
(save-window-excursion
(with-temp-buffer
(save-excursion
(mapc #'(lambda (prop)
(insert prop ": " (Wand:image-property iw prop) "\n"))
props))
(pop-to-buffer (current-buffer))
(text-mode)
(message "Press %s when done, or %s to cancel"
(key-description
(car (where-is-internal 'exit-recursive-edit)))
(key-description
(car (where-is-internal 'abort-recursive-edit))))
(recursive-edit)
;; User pressed C-M-c, parse buffer and store new props
(goto-char (point-min))
(while (not (eobp))
(let* ((st (buffer-substring (point-at-bol) (point-at-eol)))
(pv (split-string st ": ")))
(setf (Wand:image-property iw (first pv)) (second pv)))
(forward-line 1))))))
;;}}}
;;{{{ `-- Image size/orientation/other stuff
(define-ffi-function Wand:MagickGetSize "MagickGetSize" Wand-BooleanType
(Wand-WandType :pointer :pointer) libmagickwand)
(define-ffi-function Wand:MagickSetSize "MagickSetSize" Wand-BooleanType
(Wand-WandType :ulong :ulong) libmagickwand)
(defun Wand:image-size (wand)
"Return size of the image, associated with WAND."
(with-ffi-temporaries ((w :ulong) (h :ulong))
(unless (zerop (Wand:MagickGetSize wand w h))
(cons (ffi--mem-ref w :ulong) (ffi--mem-ref h :ulong)))))
(defsetf Wand:image-size (wand) (size)
`(Wand:MagickSetSize ,wand (car ,size) (cdr ,size)))
(define-ffi-function Wand:image-height "MagickGetImageHeight" :ulong
(Wand-WandType) libmagickwand)
(define-ffi-function Wand:image-width "MagickGetImageWidth" :ulong
(Wand-WandType) libmagickwand)
(define-ffi-function Wand:GetImageOrientation "MagickGetImageOrientation"
Wand-OrientationType
(Wand-WandType) libmagickwand)
(define-ffi-function Wand:SetImageOrientation "MagickSetImageOrientation"
Wand-BooleanType
(Wand-WandType Wand-OrientationType) libmagickwand)
(defun Wand:image-orientation (w)
"Return orientation for the image hold by W.
Use \(setf \(Wand:image-orientation w\) orient\) to set new one."
(Wand:GetImageOrientation w))
(defsetf Wand:image-orientation (w) (orient)
`(Wand:SetImageOrientation ,w ,orient))
(defconst Wand-EndianType :int)
(defconst Wand-EndianUndefined 0)
(defconst Wand-EndianLSB 1)
(defconst Wand-EndianMSB 2)
(define-ffi-function Wand:GetImageEndian "MagickGetImageEndian"
Wand-EndianType
(Wand-WandType) libmagickwand)
(define-ffi-function Wand:SetImageEndian "MagickSetImageEndian"
Wand-BooleanType
(Wand-WandType Wand-EndianType) libmagickwand)
(defun Wand:image-endian (w)
"Return endian for the image hold by W.
Use \(setf \(Wand:image-endian w\) endian\) to set new one."
(Wand:GetImageEndian w))
(defsetf Wand:image-endian (w) (endian)
`(Wand:SetImageEndian ,w ,endian))
(defconst Wand-ColorspaceType :int)
(defconst Wand-ColorspaceTypes
'(("RGB" . 1) ("GRAY" . 2) ("Transparent" . 3) ("OHTA" . 4) ("Lab" . 5)
("XYZ" . 6) ("YCbCr" . 7) ("YCC" . 8) ("YIQ" . 9) ("YPbPr" . 10)
("YUV" . 11) ("CMYK" . 12) ("sRGB" . 13) ("HSB" . 14) ("HSL" . 15)
("HWB" . 16) ("Rec601Luma" . 17) ("Rec601YCbCr" . 18) ("Rec709Luma" . 19)
("Rec709YCbCr" . 20) ("Log" . 21) ("CMY" . 22)))
(define-ffi-function Wand:SetImageColorspace "MagickTransformImageColorspace"
Wand-BooleanType
(Wand-WandType Wand-ColorspaceType) libmagickwand)
;;}}}
;;{{{ `-- Image format operations
(define-ffi-function Wand:MagicGetFormat "MagickGetFormat" :pointer
(Wand-WandType) libmagickwand)
(define-ffi-function Wand:MagickSetFormat "MagickSetFormat" Wand-BooleanType
(Wand-WandType :pointer) libmagickwand)
(defun Wand:wand-format (w)
(let ((ret (Wand:MagicGetFormat w)))
(unless (ffi-pointer-null-p ret)
(ffi-get-c-string ret))))
(defsetf Wand:wand-format (w) (nfmt)
(let ((nfmtsym (cl-gensym)))
`(with-ffi-string (,nfmtsym ,nfmt)
(Wand:MagickSetFormat ,w ,nfmtsym))))
(define-ffi-function Wand:GetImageFormat "MagickGetImageFormat" :pointer
(Wand-WandType) libmagickwand)
(define-ffi-function Wand:SetImageFormat "MagickSetImageFormat" Wand-BooleanType
(Wand-WandType :pointer) libmagickwand)
(defun Wand:image-format (w)
"Return format for the image hold by W.
Use \(setf \(Wand:image-format w\) FMT\) to set new one."
(let ((ret (Wand:GetImageFormat w)))
(unless (ffi-pointer-null-p ret)
(ffi-get-c-string ret))))
(defsetf Wand:image-format (w) (fmt)
(let ((nfmtsym (cl-gensym)))
`(with-ffi-string (,nfmtsym ,fmt)
(Wand:SetImageFormat ,w ,nfmtsym))))
(define-ffi-function Wand:GetMagickInfo "GetMagickInfo" :pointer
;; format exception
(:pointer :pointer) libmagickwand)
(defun Wand:get-magick-info (fmt)
(with-ffi-temporary (c-mexc Wand-ExceptionInfoType)
(with-ffi-string (c-fmt fmt)
(Wand:GetMagickInfo c-fmt c-mexc))))
(define-ffi-function Wand:GetMagickInfoList "GetMagickInfoList" :pointer
;; fmt-pattern &num exception
(:pointer :pointer :pointer) libmagickwand)
;;}}}
;;{{{ `-- Images list operations
(define-ffi-function Wand:images-num "MagickGetNumberImages" :ulong
(Wand-WandType) libmagickwand)
(define-ffi-function Wand:HasNextImage "MagickHasNextImage" Wand-BooleanType
(Wand-WandType) libmagickwand)
(defsubst Wand:has-next-image (w)
(= (Wand:HasNextImage w) 1))
(define-ffi-function Wand:next-image "MagickNextImage" Wand-BooleanType
(Wand-WandType) libmagickwand)
(define-ffi-function Wand:has-prev-image "MagickHasPreviousImage" Wand-BooleanType
(Wand-WandType) libmagickwand)
(define-ffi-function Wand:prev-image "MagickPreviousImage" Wand-BooleanType
(Wand-WandType) libmagickwand)
(define-ffi-function Wand:iterator-index "MagickGetIteratorIndex" :long
(Wand-WandType) libmagickwand)
(define-ffi-function Wand:MagickSetIteratorIndex "MagickSetIteratorIndex"
Wand-BooleanType
(Wand-WandType :long) libmagickwand)
(defsetf Wand:iterator-index (w) (idx)
`(Wand:MagickSetIteratorIndex ,w ,idx))
(define-ffi-function Wand:set-first-iterator "MagickSetFirstIterator" :void
(Wand-WandType) libmagickwand)
(define-ffi-function Wand:set-last-iterator "MagickSetLastIterator" :void
(Wand-WandType) libmagickwand)
;;}}}
;;{{{ `-- PixelWand operations
(defvar Wand-PixelType :pointer)
(define-ffi-function Wand:NewPixelWand "NewPixelWand" Wand-PixelType
nil libmagickwand)
(define-ffi-function Wand:DestroyPixelWand "DestroyPixelWand" Wand-PixelType
(Wand-PixelType) libmagickwand)
(defmacro Wand-with-pixel-wand (pw &rest forms)
"With allocated pixel wand PW do FORMS."
`(let ((,pw (Wand:NewPixelWand)))
(unwind-protect
(progn ,@forms)
(Wand:DestroyPixelWand ,pw))))
(put 'Wand-with-pixel-wand 'lisp-indent-function 'defun)
(define-ffi-function Wand:pixel-red "PixelGetRed" :double
(Wand-PixelType) libmagickwand)
(define-ffi-function Wand:pixel-green "PixelGetGreen" :double
(Wand-PixelType) libmagickwand)
(define-ffi-function Wand:pixel-blue "PixelGetBlue" :double
(Wand-PixelType) libmagickwand)
(define-ffi-function Wand:PixelSetRed "PixelSetRed" :void
(Wand-PixelType :double) libmagickwand)
(define-ffi-function Wand:PixelSetGreen "PixelSetGreen" :void
(Wand-PixelType :double) libmagickwand)
(define-ffi-function Wand:PixelSetBlue "PixelSetBlue" :void
(Wand-PixelType :double) libmagickwand)
(defsetf Wand:pixel-red (pw) (r)
`(Wand:PixelSetRed ,pw ,r))
(defsetf Wand:pixel-green (pw) (g)
`(Wand:PixelSetGreen ,pw ,g))
(defsetf Wand:pixel-blue (pw) (b)
`(Wand:PixelSetBlue ,pw ,b))
(defun Wand:pixel-rgb-components (pw)
"Return RGB components for pixel wand PW."
(mapcar #'(lambda (c) (truncate (* (funcall c pw) 65535.0)))
'(Wand:pixel-red Wand:pixel-green Wand:pixel-blue)))
(defsetf Wand:pixel-rgb-components (pw) (rgb)
"For pixel wand PW set RGB components."
`(mapcar* #'(lambda (sf c) (funcall sf ,pw (/ c 65535.0)))
'(Wand:PixelSetRed Wand:PixelSetGreen Wand:PixelSetBlue)
,rgb))
;; PixelGetColorAsString() returns the color of the pixel wand as a
;; string.
(define-ffi-function Wand:PixelGetColorAsString "PixelGetColorAsString" :pointer
(Wand-PixelType) libmagickwand)
(defun Wand:pixel-color (pw)
(let ((pcs (Wand:PixelGetColorAsString pw)))
(unless (ffi-pointer-null-p pcs)
(ffi-get-c-string pcs))))
;; PixelSetColor() sets the color of the pixel wand with a string
;; (e.g. "blue", "#0000ff", "rgb(0,0,255)", "cmyk(100,100,100,10)",
;; etc.).
(define-ffi-function Wand:PixelSetColor "PixelSetColor" Wand-BooleanType
(Wand-PixelType :pointer) libmagickwand)
(defsetf Wand:pixel-color (pw) (color)
(let ((colcsym (cl-gensym)))
`(with-ffi-string (,colcsym ,color)
(Wand:PixelSetColor ,pw ,colcsym))))
;; PixelGetAlpha() returns the normalized alpha color of the pixel
;; wand.
(define-ffi-function Wand:pixel-alpha "PixelGetAlpha" :double
(Wand-PixelType) libmagickwand)
;; PixelSetAlpha() sets the normalized alpha color of the pixel wand.
;; The level of transparency: 1.0 is fully opaque and 0.0 is fully
;; transparent.
(define-ffi-function Wand:PixelSetAlpha "PixelSetAlpha" :void
(Wand-PixelType :double) libmagickwand)
(defsetf Wand:pixel-alpha (pw) (alpha)
`(Wand:PixelSetAlpha ,pw ,alpha))
;;}}}
;;{{{ `-- DrawingWand operations
(defconst Wand-DrawingType :pointer)
(defconst Wand-PaintMethodType :int)
(defmacro Wand-PaintMethod-get (kw)
(ecase kw
(:point 1)
(:replace 2)
((:floodfill :flood-fill) 3)
((:filltoborder :fill-to-border) 4)
(:reset 5)))
;; MagickDrawImage() renders the drawing wand on the current image.
(define-ffi-function Wand:MagickDrawImage "MagickDrawImage" Wand-BooleanType
(Wand-WandType Wand-DrawingType) libmagickwand)
(define-ffi-function Wand:MagickAnnotateImage "MagickAnnotateImage"
Wand-BooleanType
;; wand draw x y angle text
(Wand-WandType Wand-DrawingType :double :double :double :pointer) libmagickwand)
(define-ffi-function Wand:clear-drawing-wand "ClearDrawingWand" :void
(Wand-DrawingType) libmagickwand)
(define-ffi-function Wand:copy-drawing-wand "CloneDrawingWand" Wand-DrawingType
(Wand-DrawingType) libmagickwand)
(define-ffi-function Wand:delete-drawing-wand "DestroyDrawingWand" Wand-DrawingType
(Wand-DrawingType) libmagickwand)
(define-ffi-function Wand:make-drawing-wand "NewDrawingWand" Wand-DrawingType
nil libmagickwand)
(defmacro Wand-with-drawing-wand (dw &rest forms)
"With allocated drawing wand DW do FORMS."
`(let ((,dw (Wand:make-drawing-wand)))
(unwind-protect
(progn ,@forms)
(Wand:delete-drawing-wand ,dw))))
(put 'Wand-with-drawing-wand 'lisp-indent-function 'defun)
(define-ffi-function Wand:draw-arc "DrawArc" :void
;; draw sx sy ex ey sd ed
(Wand-DrawingType :double :double :double :double :double :double) libmagickwand)
(define-ffi-function Wand:draw-circle "DrawCircle" :void
;; draw ox oy px py
(Wand-DrawingType :double :double :double :double) libmagickwand)
(define-ffi-function Wand:draw-rectangle "DrawRectangle" :void
;; draw ox oy ex ey
(Wand-DrawingType :double :double :double :double) libmagickwand)
(define-ffi-function Wand:draw-round-rectangle "DrawRoundRectangle" :void
;; draw x1 y1 x2 y2 rx ry
(Wand-DrawingType :double :double :double :double :double :double) libmagickwand)
(define-ffi-function Wand:draw-color "DrawColor" :void
;; draw x y paint-method
(Wand-DrawingType :double :double Wand-PaintMethodType) libmagickwand)
(define-ffi-function Wand:DrawPolygon "DrawPolygon" :void
;; draw n-points (pointer PointInfo)
(Wand-DrawingType :ulong :pointer) libmagickwand)
(define-ffi-function Wand:DrawPolyline "DrawPolyline" :void
;; draw n-points (pointer PointInfo)
(Wand-DrawingType :ulong :pointer) libmagickwand)
(defmacro Wand-with-ffi-points (binding &rest body)
(declare (indent defun))
(let ((npo (cl-gensym)) (poi (cl-gensym)) (offseter (cl-gensym)))
`(let (,offseter)
(with-ffi-temporaries ((,poi PointInfo)
(,(car binding)
(* (length ,@(cdr binding))
(ffi--type-size PointInfo))))
(setq ,offseter ,(car binding))
(dolist (,npo ,@(cdr binding))
(setf (PointInfo-x ,poi) (float (car ,npo))
(PointInfo-y ,poi) (float (cdr ,npo)))
(ffi--mem-set ,offseter PointInfo ,poi)
(setq ,offseter
(ffi-pointer+ ,offseter (ffi--type-size PointInfo))))
,@body))))
(defun Wand:draw-lines (dw points)
(Wand-with-ffi-points (c-pinfo points)
(Wand:DrawPolyline dw (length points) c-pinfo)))
(define-ffi-function Wand:DrawGetFillColor "DrawGetFillColor" :void
(Wand-DrawingType Wand-PixelType) libmagickwand)
(define-ffi-function Wand:DrawSetFillColor "DrawSetFillColor" :void
(Wand-DrawingType Wand-PixelType) libmagickwand)
(defun Wand:draw-fill-color (dw)
(let ((pw (Wand:NewPixelWand)))
(Wand:DrawGetFillColor dw pw)
pw))
(defsetf Wand:draw-fill-color (w) (p)
`(Wand:DrawSetFillColor ,w ,p))
(define-ffi-function Wand:draw-fill-opacity "DrawGetFillOpacity" :double
(Wand-DrawingType) libmagickwand)
(define-ffi-function Wand:DrawSetFillOpacity "DrawSetFillOpacity" :void
(Wand-DrawingType :double) libmagickwand)
(defsetf Wand:draw-fill-opacity (w) (fo)
`(Wand:DrawSetFillOpacity ,w ,fo))
(define-ffi-function Wand:DrawGetStrokeColor "DrawGetStrokeColor" :void
(Wand-DrawingType Wand-PixelType) libmagickwand)
(define-ffi-function Wand:DrawSetStrokeColor "DrawSetStrokeColor" :void
(Wand-DrawingType Wand-PixelType) libmagickwand)
(defun Wand:draw-stroke-color (dw)
(let ((pw (Wand:NewPixelWand)))
(Wand:DrawGetStrokeColor dw pw)
pw))
(defsetf Wand:draw-stroke-color (w) (p)
`(Wand:DrawSetStrokeColor ,w ,p))
(define-ffi-function Wand:draw-stroke-width "DrawGetStrokeWidth" :double
(Wand-DrawingType) libmagickwand)
(define-ffi-function Wand:DrawSetStrokeWidth "DrawSetStrokeWidth" :void
(Wand-DrawingType :double) libmagickwand)
(defsetf Wand:draw-stroke-width (dw) (sw)
`(Wand:DrawSetStrokeWidth ,dw (float ,sw)))
(define-ffi-function Wand:draw-stroke-opacity "DrawGetStrokeOpacity" :double
(Wand-DrawingType) libmagickwand)
(define-ffi-function Wand:DrawSetStrokeOpacity "DrawSetStrokeOpacity" :void
(Wand-DrawingType :double) libmagickwand)
(defsetf Wand:draw-stroke-opacity (dw) (so)
`(Wand:DrawSetStrokeOpacity ,dw ,so))
(define-ffi-function Wand:draw-stroke-antialias "DrawGetStrokeAntialias"
Wand-BooleanType
(Wand-DrawingType) libmagickwand)
(define-ffi-function Wand:DrawSetStrokeAntialias "DrawSetStrokeAntialias" :void
(Wand-DrawingType Wand-BooleanType) libmagickwand)
(defsetf Wand:draw-stroke-antialias (dw) (aa)
`(Wand:DrawSetStrokeAntialias ,dw (if ,aa 1 0)))
;;}}}
;;{{{ `-- Image pixels operations
(defvar Wand-StorageType :int)
(defmacro Wand-StorageType-get (kw)
(ecase kw
(:undefined-pixel 0)
(:char-pixel 1)))
(define-ffi-function Wand:MagickExportImagePixels "MagickExportImagePixels" Wand-BooleanType
(Wand-WandType
:long ;from-width
:long ;from-height
:ulong ;delta-width
:ulong ;delta-height
:pointer ;map (c-string)
Wand-StorageType
:pointer) ;target
libmagickwand)
;; Use `Wand:RelinquishMemory' when done
(defun Wand:get-image-pixels-internal
(wand img-type from-width from-height delta-width delta-height)
"Return WAND's raw string of image pixel data (RGB triples).
FROM-WIDTH, FROM-HEIGHT, DELTA-WIDTH, DELTA-HEIGHT specifies region to
fetch data from."
(let* ((mapn-tsz (ecase img-type
(rgb (cons "RGB" 3))
(rgba (cons "RGBA" 4))
(bgr (cons "BGR" 3))
(bgra (cons "BGRA" 4))
(bgrp (cons "BGRP" 4))))
(rsize (* delta-width delta-height (cdr mapn-tsz)))
(target (ffi-allocate rsize)))
(with-ffi-string (mapncstr (car mapn-tsz))
(Wand:MagickExportImagePixels
wand from-width from-height delta-width delta-height
mapncstr (Wand-StorageType-get :char-pixel) target)
(list rsize target (make-finalizer (lambda () (ffi-free target)))))))
(defun Wand:get-image-pixels (wand)
"Return WAND's raw string of image pixel data (RGB triples)."
(Wand:get-image-pixels-internal
wand 'rgb 0 0 (Wand:image-width wand) (Wand:image-height wand)))
(defun Wand:pixels-extract-colors (ss &optional n)
"Extract colors from SS string.
Return list of lists of N int elements representing RBG(A) values."
(let ((cls (cl-loop for i from 0 below (car ss)
collect (ffi-aref (cdr ss) :uchar i)))
(rls nil))
(while cls
(push (cl-subseq cls 0 (or n 3)) rls)
(setq cls (nthcdr (or n 3) cls)))
(nreverse rls)))
(defun Wand:get-image-rgb-pixels (wand x y w h)
"Extract RGB pixels from WAND."
(let ((pxd (Wand:get-image-pixels-internal
wand 'rgb x y w h)))
(unwind-protect
(Wand:pixels-extract-colors pxd 3)
(ffi-free (cdr pxd)))))
(defun Wand:get-rgb-pixel-at (wand x y)
"Return WAND's RGB pixel at X, Y."
(car (Wand:get-image-rgb-pixels wand x y 1 1)))
;; MagickConstituteImage() adds an image to the wand comprised of the
;; pixel data you supply. The pixel data must be in scanline order
;; top-to-bottom. The data can be char, short int, int, float, or
;; double. Float and double require the pixels to be normalized
;; [0..1], otherwise [0..Max], where Max is the maximum value the type
;; can accomodate (e.g. 255 for char). For example, to create a
;; 640x480 image from unsigned red-green-blue character data, use
(define-ffi-function Wand:MagickConstituteImage "MagickConstituteImage"
Wand-BooleanType
;; width height map(string) storage-type pixels
(Wand-WandType :ulong :ulong :pointer Wand-StorageType :pointer)
libmagickwand)
(defun Wand:constitute-image (wand w h map pxl-type pixels)
(let ((mapn-tsz (ecase pxl-type
(rgb (cons "RGB" 3))
(rgba (cons "RGBA" 4))
(bgr (cons "BGR" 3))
(bgra (cons "BGRA" 4))
(bgrp (cons "BGRP" 4)))))
(with-ffi-string (c-map (car mapn-tsz))
(with-ffi-temporary (c-pxls (ffi-allocate (* w h (cdr mapn-tsz))))
(Wand:MagickConstituteImage
wand w h c-map (Wand-StorageType-get :char-pixel) c-pxls)))))
;;}}}
;;{{{ `-- Image modification functions
(defvar Wand-FilterType :int)
(defvar Wand-FilterTypes
'(("point" . 1) ("box" . 2) ("triangle" . 3) ("hermite" . 4)
("hanning" . 5) ("hamming" . 6) ("blackman" . 7) ("gaussian" . 8)
("quadratic" . 9) ("cubic" . 10) ("catrom" . 11) ("mitchell" . 12)
("jinc" . 13) ("sinc" . 14) ("sincfast" . 15) ("kaiser" . 16)
("welsh" . 17) ("parzen" . 18) ("bohman" . 19) ("bartlett" . 20)
("lagrange" . 21) ("lanczos" . 22) ("lanczossharp" . 23) ("lanczos2" . 24)
("lanczos2sharp" . 25) ("robidoux" . 26) ("robidouxsharp" . 27)
("cosine" . 28) ("spline" . 29) ("lanczosradius" . 30) ("sentinel" . 31)))
(defvar Wand-CompositeOperatorType :int)
(defconst Wand-CompositeOperators
'(("no" . 1) ("add" . 2) ("atop" . 3) ("blend" . 4)
("bumpmap" . 5) ("change-mask" . 6) ("clear" . 7)
("color-burn" . 8) ("color-dodge" . 9) ("colorize" . 10)
("copy-black" . 11) ("copy-blue" . 12) ("copy" . 13)
("copy-cyan" . 14) ("copy-green" . 15) ("copy-magenta" . 16)
("copy-opacity" . 17) ("copy-red" . 18) ("copy-yellow" . 19)
("darken" . 20) ("dst-atop" . 21) ("dst" . 22) ("dst-in" . 23)
("dst-out" . 24) ("dst-over" . 25) ("difference" . 26)
("displace" . 27) ("dissolve" . 28) ("exclusion" . 29)
("hardlight" . 30) ("hue" . 31) ("in" . 32) ("lighten" . 33)
("linearlight" . 34) ("luminize" . 35) ("minus" . 36)
("modulate" . 37) ("multiply" . 38) ("out" . 39) ("over" . 40)
("overlay" . 41) ("plus" . 42) ("replace" . 43) ("saturate" . 44)
("screen" . 45) ("softlight" . 46) ("src-atop" . 47) ("src" . 48)
("src-in" . 49) ("src-out" . 50) ("src-over" . 51) ("subtract" . 52)
("threshold" . 53) ("xor" . 54) ("divide" . 55)
))
(defvar Wand-NoiseType :int)
(defconst Wand-NoiseTypes
'(("uniform" . 1) ("guassian" . 2) ("mult-gaussian" . 3)
("impulse" . 4) ("laplacian" . 5) ("poisson" . 6) ("random" . 7)))
(defvar Wand-PreviewType :int)
(defconst Wand-PreviewTypes
'(("rotate" . 1) ("shear" . 2) ("roll" . 32)
("hue" . 4) ("saturation" . 5) ("brightness" . 6)
("gamma" . 7) ("spiff" . 8) ("dull" . 9) ("grayscale" . 10)
("quantize" . 11) ("despeckle" . 12) ("reduce-noise" . 13)
("add-noise" . 14) ("sharpen" . 15) ("blur" . 16)
("threshold" . 17) ("edgedetect" . 18) ("spread" . 19)
("solarize" . 20) ("shade" . 21) ("raise" . 22)
("segment" . 23) ("swirl" . 24) ("implode" . 25)
("wave" . 26) ("oilpaint" . 27) ("charcoal-drawing" . 28)
("jpeg" . 29)))
(define-ffi-function Wand:RotateImage "MagickRotateImage" Wand-BooleanType
(Wand-WandType Wand-PixelType :double) libmagickwand)
;;Scale the image in WAND to the dimensions WIDTHxHEIGHT.
(define-ffi-function Wand:scale-image "MagickScaleImage" Wand-BooleanType
(Wand-WandType :ulong :ulong) libmagickwand)
;; Sample the image
(define-ffi-function Wand:sample-image "MagickSampleImage" Wand-BooleanType
(Wand-WandType :ulong :ulong) libmagickwand)
(define-ffi-function Wand:resize-image "MagickResizeImage" Wand-BooleanType
(Wand-WandType :ulong :ulong Wand-FilterType
:double ;blur
)
libmagickwand)
(ignore-errors
(define-ffi-function Wand:liquid-rescale "MagickLiquidRescaleImage"
Wand-BooleanType
(Wand-WandType :ulong :ulong
:double ;delta-x
:double ;rigidity
)
libmagickwand))
(define-ffi-function Wand:flip-image "MagickFlipImage" Wand-BooleanType
(Wand-WandType) libmagickwand)
(define-ffi-function Wand:flop-image "MagickFlopImage" Wand-BooleanType
(Wand-WandType) libmagickwand)
(define-ffi-function Wand:transpose-image "MagickTransposeImage"
Wand-BooleanType
(Wand-WandType) libmagickwand)
(define-ffi-function Wand:transverse-image "MagickTransverseImage"
Wand-BooleanType
(Wand-WandType) libmagickwand)
;; MagickWaveImage() creates a "ripple" effect in the image by
;; shifting the pixels vertically along a sine wave whose amplitude
;; and wavelength is specified by the given parameters.
;; The AMPLITUDE argument is a float and defines the how large
;; waves are.
;; The WAVELENGTH argument is a float and defines how often the
;; waves occur.
(define-ffi-function Wand:wave-image "MagickWaveImage" Wand-BooleanType
(Wand-WandType :double :double) libmagickwand)
;; Swirl the image associated with WAND by DEGREES.
(define-ffi-function Wand:swirl-image "MagickSwirlImage" Wand-BooleanType
(Wand-WandType :double) libmagickwand)
(define-ffi-function Wand:MagickPosterizeImage "MagickPosterizeImage"