-
Notifications
You must be signed in to change notification settings - Fork 46
/
image.lisp
2668 lines (2581 loc) · 100 KB
/
image.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
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
;;; -*- Mode:Lisp; Package:XLIB; Syntax:COMMON-LISP; Base:10; Lowercase:T -*-
;;; CLX Image functions
;;;
;;; TEXAS INSTRUMENTS INCORPORATED
;;; P.O. BOX 2909
;;; AUSTIN, TEXAS 78769
;;;
;;; Copyright (C) 1987 Texas Instruments Incorporated.
;;;
;;; Permission is granted to any individual or institution to use, copy, modify,
;;; and distribute this software, provided that this complete copyright and
;;; permission notice is maintained, intact, in all copies and supporting
;;; documentation.
;;;
;;; Texas Instruments Incorporated provides this software "as is" without
;;; express or implied warranty.
;;;
(in-package :xlib)
(defmacro with-image-data-buffer ((buffer size) &body body)
(declare (indentation 0 4 1 1))
`(let ((.reply-buffer. (allocate-reply-buffer ,size)))
(declare (type reply-buffer .reply-buffer.))
(unwind-protect
(let ((,buffer (reply-ibuf8 .reply-buffer.)))
(declare (type buffer-bytes ,buffer))
(with-vector (,buffer buffer-bytes)
,@body))
(deallocate-reply-buffer .reply-buffer.))))
(def-clx-class (image (:constructor nil) (:copier nil) (:predicate nil))
;; Public structure
(width 0 :type card16 :read-only t)
(height 0 :type card16 :read-only t)
(depth 1 :type card8 :read-only t)
(plist nil :type list))
;; Image-Plist accessors:
(defmacro image-name (image) `(getf (image-plist ,image) :name))
(defmacro image-x-hot (image) `(getf (image-plist ,image) :x-hot))
(defmacro image-y-hot (image) `(getf (image-plist ,image) :y-hot))
(defmacro image-red-mask (image) `(getf (image-plist ,image) :red-mask))
(defmacro image-blue-mask (image) `(getf (image-plist ,image) :blue-mask))
(defmacro image-green-mask (image) `(getf (image-plist ,image) :green-mask))
(defun print-image (image stream depth)
(declare (type image image)
(ignore depth))
(print-unreadable-object (image stream :type t)
(when (image-name image)
(write-string (string (image-name image)) stream)
(write-string " " stream))
(prin1 (image-width image) stream)
(write-string "x" stream)
(prin1 (image-height image) stream)
(write-string "x" stream)
(prin1 (image-depth image) stream)))
(defconstant +empty-data-x+ '#.(make-sequence '(array card8 (*)) 0))
(defconstant +empty-data-z+
'#.(make-array '(0 0) :element-type 'pixarray-1-element-type))
(def-clx-class (image-x (:include image) (:copier nil)
(:print-function print-image))
;; Use this format for shoveling image data
;; Private structure. Accessors for these NOT exported.
(format :z-pixmap :type (member :bitmap :xy-pixmap :z-pixmap))
(bytes-per-line 0 :type card16)
(bits-per-pixel 1 :type (member 1 4 8 16 24 32))
(bit-lsb-first-p +image-bit-lsb-first-p+ :type generalized-boolean) ; Bit order
(byte-lsb-first-p +image-byte-lsb-first-p+ :type generalized-boolean) ; Byte order
(data +empty-data-x+ :type (array card8 (*))) ; row-major
(unit +image-unit+ :type (member 8 16 32)) ; Bitmap unit
(pad +image-pad+ :type (member 8 16 32)) ; Scanline pad
(left-pad 0 :type card8)) ; Left pad
(def-clx-class (image-xy (:include image) (:copier nil)
(:print-function print-image))
;; Public structure
;; Use this format for image processing
(bitmap-list nil :type list)) ;; list of bitmaps
(def-clx-class (image-z (:include image) (:copier nil)
(:print-function print-image))
;; Public structure
;; Use this format for image processing
(bits-per-pixel 1 :type (member 1 4 8 16 24 32))
(pixarray +empty-data-z+ :type pixarray))
(defun create-image (&key width height depth
(data (required-arg data))
plist name x-hot y-hot
red-mask blue-mask green-mask
bits-per-pixel format bytes-per-line
(byte-lsb-first-p
#+clx-little-endian t
#-clx-little-endian nil)
(bit-lsb-first-p
#+clx-little-endian t
#-clx-little-endian nil)
unit pad left-pad)
;; Returns an image-x image-xy or image-z structure, depending on the
;; type of the :DATA parameter.
(declare
(type (or null card16) width height) ; Required
(type (or null card8) depth) ; Defualts to 1
(type (or buffer-bytes ; Returns image-x
list ; Returns image-xy
pixarray) data) ; Returns image-z
(type list plist)
(type (or null stringable) name)
(type (or null card16) x-hot y-hot)
(type (or null pixel) red-mask blue-mask green-mask)
(type (or null (member 1 4 8 16 24 32)) bits-per-pixel)
;; The following parameters are ignored for image-xy and image-z:
(type (or null (member :bitmap :xy-pixmap :z-pixmap))
format) ; defaults to :z-pixmap
(type (or null card16) bytes-per-line)
(type generalized-boolean byte-lsb-first-p bit-lsb-first-p)
(type (or null (member 8 16 32)) unit pad)
(type (or null card8) left-pad))
(declare (clx-values image))
(let ((image
(etypecase data
(buffer-bytes ; image-x
(let ((data data))
(declare (type buffer-bytes data))
(unless depth (setq depth (or bits-per-pixel 1)))
(unless format
(setq format (if (= depth 1) :xy-pixmap :z-pixmap)))
(unless bits-per-pixel
(setq bits-per-pixel
(cond ((eq format :xy-pixmap) 1)
((index> depth 24) 32)
((index> depth 16) 24)
((index> depth 8) 16)
((index> depth 4) 8)
((index> depth 1) 4)
(t 1))))
(unless width (required-arg width))
(unless height (required-arg height))
(unless bytes-per-line
(let* ((pad (or pad 8))
(bits-per-line (index* width bits-per-pixel))
(padded-bits-per-line
(index* (index-ceiling bits-per-line pad) pad)))
(declare (type array-index pad bits-per-line
padded-bits-per-line))
(setq bytes-per-line (index-ceiling padded-bits-per-line 8))))
(unless unit (setq unit +image-unit+))
(unless pad
(setq pad
(dolist (pad '(32 16 8))
(when (and (index<= pad +image-pad+)
(zerop
(index-mod
(index* bytes-per-line 8) pad)))
(return pad)))))
(unless left-pad (setq left-pad 0))
(make-image-x
:width width :height height :depth depth :plist plist
:format format :data data
:bits-per-pixel bits-per-pixel
:bytes-per-line bytes-per-line
:byte-lsb-first-p byte-lsb-first-p
:bit-lsb-first-p bit-lsb-first-p
:unit unit :pad pad :left-pad left-pad)))
(list ; image-xy
(let ((data data))
(declare (type list data))
(unless depth (setq depth (length data)))
(when data
(unless width (setq width (array-dimension (car data) 1)))
(unless height (setq height (array-dimension (car data) 0))))
(make-image-xy
:width width :height height :plist plist :depth depth
:bitmap-list data)))
(pixarray ; image-z
(let ((data data))
(declare (type pixarray data))
(unless width (setq width (array-dimension data 1)))
(unless height (setq height (array-dimension data 0)))
(unless bits-per-pixel
(setq bits-per-pixel
(etypecase data
(pixarray-32 32)
(pixarray-24 24)
(pixarray-16 16)
(pixarray-8 8)
(pixarray-4 4)
(pixarray-1 1)))))
(unless depth (setq depth bits-per-pixel))
(make-image-z
:width width :height height :depth depth :plist plist
:bits-per-pixel bits-per-pixel :pixarray data)))))
(declare (type image image))
(when name (setf (image-name image) name))
(when x-hot (setf (image-x-hot image) x-hot))
(when y-hot (setf (image-y-hot image) y-hot))
(when red-mask (setf (image-red-mask image) red-mask))
(when blue-mask (setf (image-blue-mask image) blue-mask))
(when green-mask (setf (image-green-mask image) green-mask))
image))
;;;-----------------------------------------------------------------------------
;;; Swapping stuff
(defun image-noswap
(src dest srcoff destoff srclen srcinc destinc height lsb-first-p)
(declare (type buffer-bytes src dest)
(type array-index srcoff destoff srclen srcinc destinc)
(type card16 height)
(type generalized-boolean lsb-first-p)
(ignore lsb-first-p))
#.(declare-buffun)
(if (index= srcinc destinc)
(buffer-replace
dest src destoff
(index+ destoff (index* srcinc (index1- height)) srclen)
srcoff)
(do* ((h height (index1- h))
(srcstart srcoff (index+ srcstart srcinc))
(deststart destoff (index+ deststart destinc))
(destend (index+ deststart srclen) (index+ deststart srclen)))
((index-zerop h))
(declare (type array-index srcstart deststart destend)
(type card16 h))
(buffer-replace dest src deststart destend srcstart))))
(defun image-swap-two-bytes
(src dest srcoff destoff srclen srcinc destinc height lsb-first-p)
(declare (type buffer-bytes src dest)
(type array-index srcoff destoff srclen srcinc destinc)
(type card16 height)
(type generalized-boolean lsb-first-p))
#.(declare-buffun)
(with-vector (src buffer-bytes)
(with-vector (dest buffer-bytes)
(do ((length (index* (index-ceiling srclen 2) 2))
(h height (index1- h))
(srcstart srcoff (index+ srcstart srcinc))
(deststart destoff (index+ deststart destinc)))
((index-zerop h))
(declare (type array-index length srcstart deststart)
(type card16 h))
(when (and (index= h 1) (not (index= srclen length)))
(index-decf length 2)
(if lsb-first-p
(setf (aref dest (index1+ (index+ deststart length)))
(the card8 (aref src (index+ srcstart length))))
(setf (aref dest (index+ deststart length))
(the card8 (aref src (index1+ (index+ srcstart length)))))))
(do ((i length (index- i 2))
(srcidx srcstart (index+ srcidx 2))
(destidx deststart (index+ destidx 2)))
((index-zerop i))
(declare (type array-index i srcidx destidx))
(setf (aref dest destidx)
(the card8 (aref src (index1+ srcidx))))
(setf (aref dest (index1+ destidx))
(the card8 (aref src srcidx))))))))
(defun image-swap-three-bytes
(src dest srcoff destoff srclen srcinc destinc height lsb-first-p)
(declare (type buffer-bytes src dest)
(type array-index srcoff destoff srclen srcinc destinc)
(type card16 height)
(type generalized-boolean lsb-first-p))
#.(declare-buffun)
(with-vector (src buffer-bytes)
(with-vector (dest buffer-bytes)
(do ((length (index* (index-ceiling srclen 3) 3))
(h height (index1- h))
(srcstart srcoff (index+ srcstart srcinc))
(deststart destoff (index+ deststart destinc)))
((index-zerop h))
(declare (type array-index length srcstart deststart)
(type card16 h))
(when (and (index= h 1) (not (index= srclen length)))
(index-decf length 3)
(when (index= (index- srclen length) 2)
(setf (aref dest (index+ deststart length 1))
(the card8 (aref src (index+ srcstart length 1)))))
(if lsb-first-p
(setf (aref dest (index+ deststart length 2))
(the card8 (aref src (index+ srcstart length))))
(setf (aref dest (index+ deststart length))
(the card8 (aref src (index+ srcstart length 2))))))
(do ((i length (index- i 3))
(srcidx srcstart (index+ srcidx 3))
(destidx deststart (index+ destidx 3)))
((index-zerop i))
(declare (type array-index i srcidx destidx))
(setf (aref dest destidx)
(the card8 (aref src (index+ srcidx 2))))
(setf (aref dest (index1+ destidx))
(the card8 (aref src (index1+ srcidx))))
(setf (aref dest (index+ destidx 2))
(the card8 (aref src srcidx))))))))
(defun image-swap-four-bytes
(src dest srcoff destoff srclen srcinc destinc height lsb-first-p)
(declare (type buffer-bytes src dest)
(type array-index srcoff destoff srclen srcinc destinc)
(type card16 height)
(type generalized-boolean lsb-first-p))
#.(declare-buffun)
(with-vector (src buffer-bytes)
(with-vector (dest buffer-bytes)
(do ((length (index* (index-ceiling srclen 4) 4))
(h height (index1- h))
(srcstart srcoff (index+ srcstart srcinc))
(deststart destoff (index+ deststart destinc)))
((index-zerop h))
(declare (type array-index length srcstart deststart)
(type card16 h))
(when (and (index= h 1) (not (index= srclen length)))
(index-decf length 4)
(unless lsb-first-p
(setf (aref dest (index+ deststart length))
(the card8 (aref src (index+ srcstart length 3)))))
(when (if lsb-first-p
(index= (index- srclen length) 3)
(not (index-zerop (index-logand srclen 2))))
(setf (aref dest (index+ deststart length 1))
(the card8 (aref src (index+ srcstart length 2)))))
(when (if (null lsb-first-p)
(index= (index- srclen length) 3)
(not (index-zerop (index-logand srclen 2))))
(setf (aref dest (index+ deststart length 2))
(the card8 (aref src (index+ srcstart length 1)))))
(when lsb-first-p
(setf (aref dest (index+ deststart length 3))
(the card8 (aref src (index+ srcstart length))))))
(do ((i length (index- i 4))
(srcidx srcstart (index+ srcidx 4))
(destidx deststart (index+ destidx 4)))
((index-zerop i))
(declare (type array-index i srcidx destidx))
(setf (aref dest destidx)
(the card8 (aref src (index+ srcidx 3))))
(setf (aref dest (index1+ destidx))
(the card8 (aref src (index+ srcidx 2))))
(setf (aref dest (index+ destidx 2))
(the card8 (aref src (index1+ srcidx))))
(setf (aref dest (index+ destidx 3))
(the card8 (aref src srcidx))))))))
(defun image-swap-words
(src dest srcoff destoff srclen srcinc destinc height lsb-first-p)
(declare (type buffer-bytes src dest)
(type array-index srcoff destoff srclen srcinc destinc)
(type card16 height)
(type generalized-boolean lsb-first-p))
#.(declare-buffun)
(with-vector (src buffer-bytes)
(with-vector (dest buffer-bytes)
(do ((length (index* (index-ceiling srclen 4) 4))
(h height (index1- h))
(srcstart srcoff (index+ srcstart srcinc))
(deststart destoff (index+ deststart destinc)))
((index-zerop h))
(declare (type array-index length srcstart deststart)
(type card16 h))
(when (and (index= h 1) (not (index= srclen length)))
(index-decf length 4)
(unless lsb-first-p
(setf (aref dest (index+ deststart length 1))
(the card8 (aref src (index+ srcstart length 3)))))
(when (if lsb-first-p
(index= (index- srclen length) 3)
(not (index-zerop (index-logand srclen 2))))
(setf (aref dest (index+ deststart length))
(the card8 (aref src (index+ srcstart length 2)))))
(when (if (null lsb-first-p)
(index= (index- srclen length) 3)
(not (index-zerop (index-logand srclen 2))))
(setf (aref dest (index+ deststart length 3))
(the card8 (aref src (index+ srcstart length 1)))))
(when lsb-first-p
(setf (aref dest (index+ deststart length 2))
(the card8 (aref src (index+ srcstart length))))))
(do ((i length (index- i 4))
(srcidx srcstart (index+ srcidx 4))
(destidx deststart (index+ destidx 4)))
((index-zerop i))
(declare (type array-index i srcidx destidx))
(setf (aref dest destidx)
(the card8 (aref src (index+ srcidx 2))))
(setf (aref dest (index1+ destidx))
(the card8 (aref src (index+ srcidx 3))))
(setf (aref dest (index+ destidx 2))
(the card8 (aref src srcidx)))
(setf (aref dest (index+ destidx 3))
(the card8 (aref src (index1+ srcidx)))))))))
(defun image-swap-nibbles
(src dest srcoff destoff srclen srcinc destinc height lsb-first-p)
(declare (type buffer-bytes src dest)
(type array-index srcoff destoff srclen srcinc destinc)
(type card16 height)
(type generalized-boolean lsb-first-p)
(ignore lsb-first-p))
#.(declare-buffun)
(with-vector (src buffer-bytes)
(with-vector (dest buffer-bytes)
(do ((h height (index1- h))
(srcstart srcoff (index+ srcstart srcinc))
(deststart destoff (index+ deststart destinc)))
((index-zerop h))
(declare (type array-index srcstart deststart)
(type card16 h))
(do ((i srclen (index1- i))
(srcidx srcstart (index1+ srcidx))
(destidx deststart (index1+ destidx)))
((index-zerop i))
(declare (type array-index i srcidx destidx))
(setf (aref dest destidx)
(the card8
(let ((byte (aref src srcidx)))
(declare (type card8 byte))
(dpb (the card4 (ldb (byte 4 0) byte))
(byte 4 4)
(the card4 (ldb (byte 4 4) byte)))))))))))
(defun image-swap-nibbles-left
(src dest srcoff destoff srclen srcinc destinc height lsb-first-p)
(declare (type buffer-bytes src dest)
(type array-index srcoff destoff srclen srcinc destinc)
(type card16 height)
(type generalized-boolean lsb-first-p)
(ignore lsb-first-p))
#.(declare-buffun)
(with-vector (src buffer-bytes)
(with-vector (dest buffer-bytes)
(do ((h height (index1- h))
(srcstart srcoff (index+ srcstart srcinc))
(deststart destoff (index+ deststart destinc)))
((index-zerop h))
(declare (type array-index srcstart deststart)
(type card16 h))
(do ((i srclen (index1- i))
(srcidx srcstart (index1+ srcidx))
(destidx deststart (index1+ destidx)))
((index= i 1)
(setf (aref dest destidx)
(the card8
(let ((byte1 (aref src srcidx)))
(declare (type card8 byte1))
(dpb (the card4 (ldb (byte 4 0) byte1))
(byte 4 4)
0)))))
(declare (type array-index i srcidx destidx))
(setf (aref dest destidx)
(the card8
(let ((byte1 (aref src srcidx))
(byte2 (aref src (index1+ srcidx))))
(declare (type card8 byte1 byte2))
(dpb (the card4 (ldb (byte 4 0) byte1))
(byte 4 4)
(the card4 (ldb (byte 4 4) byte2)))))))))))
(defconstant +image-byte-reverse+
'#.(coerce
'#(
0 128 64 192 32 160 96 224 16 144 80 208 48 176 112 240
8 136 72 200 40 168 104 232 24 152 88 216 56 184 120 248
4 132 68 196 36 164 100 228 20 148 84 212 52 180 116 244
12 140 76 204 44 172 108 236 28 156 92 220 60 188 124 252
2 130 66 194 34 162 98 226 18 146 82 210 50 178 114 242
10 138 74 202 42 170 106 234 26 154 90 218 58 186 122 250
6 134 70 198 38 166 102 230 22 150 86 214 54 182 118 246
14 142 78 206 46 174 110 238 30 158 94 222 62 190 126 254
1 129 65 193 33 161 97 225 17 145 81 209 49 177 113 241
9 137 73 201 41 169 105 233 25 153 89 217 57 185 121 249
5 133 69 197 37 165 101 229 21 149 85 213 53 181 117 245
13 141 77 205 45 173 109 237 29 157 93 221 61 189 125 253
3 131 67 195 35 163 99 227 19 147 83 211 51 179 115 243
11 139 75 203 43 171 107 235 27 155 91 219 59 187 123 251
7 135 71 199 39 167 103 231 23 151 87 215 55 183 119 247
15 143 79 207 47 175 111 239 31 159 95 223 63 191 127 255)
'(vector card8)))
(defun image-swap-bits
(src dest srcoff destoff srclen srcinc destinc height lsb-first-p)
(declare (type buffer-bytes src dest)
(type array-index srcoff destoff srclen srcinc destinc)
(type card16 height)
(type generalized-boolean lsb-first-p)
(ignore lsb-first-p))
#.(declare-buffun)
(with-vector (src buffer-bytes)
(with-vector (dest buffer-bytes)
(let ((byte-reverse +image-byte-reverse+))
(with-vector (byte-reverse (simple-array card8 (256)))
(macrolet ((br (byte)
`(the card8 (aref byte-reverse (the card8 ,byte)))))
(do ((h height (index1- h))
(srcstart srcoff (index+ srcstart srcinc))
(deststart destoff (index+ deststart destinc)))
((index-zerop h))
(declare (type array-index srcstart deststart)
(type card16 h))
(do ((i srclen (index1- i))
(srcidx srcstart (index1+ srcidx))
(destidx deststart (index1+ destidx)))
((index-zerop i))
(declare (type array-index i srcidx destidx))
(setf (aref dest destidx) (br (aref src srcidx)))))))))))
(defun image-swap-bits-and-two-bytes
(src dest srcoff destoff srclen srcinc destinc height lsb-first-p)
(declare (type buffer-bytes src dest)
(type array-index srcoff destoff srclen srcinc destinc)
(type card16 height)
(type generalized-boolean lsb-first-p))
#.(declare-buffun)
(with-vector (src buffer-bytes)
(with-vector (dest buffer-bytes)
(let ((byte-reverse +image-byte-reverse+))
(with-vector (byte-reverse (simple-array card8 (256)))
(macrolet ((br (byte)
`(the card8 (aref byte-reverse (the card8 ,byte)))))
(do ((length (index* (index-ceiling srclen 2) 2))
(h height (index1- h))
(srcstart srcoff (index+ srcstart srcinc))
(deststart destoff (index+ deststart destinc)))
((index-zerop h))
(declare (type array-index length srcstart deststart)
(type card16 h))
(when (and (index= h 1) (not (index= srclen length)))
(index-decf length 2)
(if lsb-first-p
(setf (aref dest (index1+ (index+ deststart length)))
(br (aref src (index+ srcstart length))))
(setf (aref dest (index+ deststart length))
(br (aref src (index1+ (index+ srcstart length)))))))
(do ((i length (index- i 2))
(srcidx srcstart (index+ srcidx 2))
(destidx deststart (index+ destidx 2)))
((index-zerop i))
(declare (type array-index i srcidx destidx))
(setf (aref dest destidx)
(br (aref src (index1+ srcidx))))
(setf (aref dest (index1+ destidx))
(br (aref src srcidx)))))))))))
(defun image-swap-bits-and-four-bytes
(src dest srcoff destoff srclen srcinc destinc height lsb-first-p)
(declare (type buffer-bytes src dest)
(type array-index srcoff destoff srclen srcinc destinc)
(type card16 height)
(type generalized-boolean lsb-first-p))
#.(declare-buffun)
(with-vector (src buffer-bytes)
(with-vector (dest buffer-bytes)
(let ((byte-reverse +image-byte-reverse+))
(with-vector (byte-reverse (simple-array card8 (256)))
(macrolet ((br (byte)
`(the card8 (aref byte-reverse (the card8 ,byte)))))
(do ((length (index* (index-ceiling srclen 4) 4))
(h height (index1- h))
(srcstart srcoff (index+ srcstart srcinc))
(deststart destoff (index+ deststart destinc)))
((index-zerop h))
(declare (type array-index length srcstart deststart)
(type card16 h))
(when (and (index= h 1) (not (index= srclen length)))
(index-decf length 4)
(unless lsb-first-p
(setf (aref dest (index+ deststart length))
(br (aref src (index+ srcstart length 3)))))
(when (if lsb-first-p
(index= (index- srclen length) 3)
(not (index-zerop (index-logand srclen 2))))
(setf (aref dest (index+ deststart length 1))
(br (aref src (index+ srcstart length 2)))))
(when (if (null lsb-first-p)
(index= (index- srclen length) 3)
(not (index-zerop (index-logand srclen 2))))
(setf (aref dest (index+ deststart length 2))
(br (aref src (index+ srcstart length 1)))))
(when lsb-first-p
(setf (aref dest (index+ deststart length 3))
(br (aref src (index+ srcstart length))))))
(do ((i length (index- i 4))
(srcidx srcstart (index+ srcidx 4))
(destidx deststart (index+ destidx 4)))
((index-zerop i))
(declare (type array-index i srcidx destidx))
(setf (aref dest destidx)
(br (aref src (index+ srcidx 3))))
(setf (aref dest (index1+ destidx))
(br (aref src (index+ srcidx 2))))
(setf (aref dest (index+ destidx 2))
(br (aref src (index1+ srcidx))))
(setf (aref dest (index+ destidx 3))
(br (aref src srcidx)))))))))))
(defun image-swap-bits-and-words
(src dest srcoff destoff srclen srcinc destinc height lsb-first-p)
(declare (type buffer-bytes src dest)
(type array-index srcoff destoff srclen srcinc destinc)
(type card16 height)
(type generalized-boolean lsb-first-p))
#.(declare-buffun)
(with-vector (src buffer-bytes)
(with-vector (dest buffer-bytes)
(let ((byte-reverse +image-byte-reverse+))
(with-vector (byte-reverse (simple-array card8 (256)))
(macrolet ((br (byte)
`(the card8 (aref byte-reverse (the card8 ,byte)))))
(do ((length (index* (index-ceiling srclen 4) 4))
(h height (index1- h))
(srcstart srcoff (index+ srcstart srcinc))
(deststart destoff (index+ deststart destinc)))
((index-zerop h))
(declare (type array-index length srcstart deststart)
(type card16 h))
(when (and (index= h 1) (not (index= srclen length)))
(index-decf length 4)
(unless lsb-first-p
(setf (aref dest (index+ deststart length 1))
(br (aref src (index+ srcstart length 3)))))
(when (if lsb-first-p
(index= (index- srclen length) 3)
(not (index-zerop (index-logand srclen 2))))
(setf (aref dest (index+ deststart length))
(br (aref src (index+ srcstart length 2)))))
(when (if (null lsb-first-p)
(index= (index- srclen length) 3)
(not (index-zerop (index-logand srclen 2))))
(setf (aref dest (index+ deststart length 3))
(br (aref src (index+ srcstart length 1)))))
(when lsb-first-p
(setf (aref dest (index+ deststart length 2))
(br (aref src (index+ srcstart length))))))
(do ((i length (index- i 4))
(srcidx srcstart (index+ srcidx 4))
(destidx deststart (index+ destidx 4)))
((index-zerop i))
(declare (type array-index i srcidx destidx))
(setf (aref dest destidx)
(br (aref src (index+ srcidx 2))))
(setf (aref dest (index1+ destidx))
(br (aref src (index+ srcidx 3))))
(setf (aref dest (index+ destidx 2))
(br (aref src srcidx)))
(setf (aref dest (index+ destidx 3))
(br (aref src (index1+ srcidx))))))))))))
;;; The following table gives the bit ordering within bytes (when accessed
;;; sequentially) for a scanline containing 32 bits, with bits numbered 0 to
;;; 31, where bit 0 should be leftmost on the display. For a given byte
;;; labelled A-B, A is for the most significant bit of the byte, and B is
;;; for the least significant bit.
;;;
;;; legend:
;;; 1 scanline-unit = 8
;;; 2 scanline-unit = 16
;;; 4 scanline-unit = 32
;;; M byte-order = MostSignificant
;;; L byte-order = LeastSignificant
;;; m bit-order = MostSignificant
;;; l bit-order = LeastSignificant
;;;
;;;
;;; format ordering
;;;
;;; 1Mm 00-07 08-15 16-23 24-31
;;; 2Mm 00-07 08-15 16-23 24-31
;;; 4Mm 00-07 08-15 16-23 24-31
;;; 1Ml 07-00 15-08 23-16 31-24
;;; 2Ml 15-08 07-00 31-24 23-16
;;; 4Ml 31-24 23-16 15-08 07-00
;;; 1Lm 00-07 08-15 16-23 24-31
;;; 2Lm 08-15 00-07 24-31 16-23
;;; 4Lm 24-31 16-23 08-15 00-07
;;; 1Ll 07-00 15-08 23-16 31-24
;;; 2Ll 07-00 15-08 23-16 31-24
;;; 4Ll 07-00 15-08 23-16 31-24
;;;
;;;
;;; The following table gives the required conversion between any two
;;; formats. It is based strictly on the table above. If you believe one,
;;; you should believe the other.
;;;
;;; legend:
;;; n no changes
;;; s reverse 8-bit units within 16-bit units
;;; l reverse 8-bit units within 32-bit units
;;; w reverse 16-bit units within 32-bit units
;;; r reverse bits within 8-bit units
;;; sr s+R
;;; lr l+R
;;; wr w+R
(defconstant +image-swap-function+
'#.(make-array
'(12 12) :initial-contents
(let ((n 'image-noswap)
(s 'image-swap-two-bytes)
(l 'image-swap-four-bytes)
(w 'image-swap-words)
(r 'image-swap-bits)
(sr 'image-swap-bits-and-two-bytes)
(lr 'image-swap-bits-and-four-bytes)
(wr 'image-swap-bits-and-words))
(list #| 1Mm 2Mm 4Mm 1Ml 2Ml 4Ml 1Lm 2Lm 4Lm 1Ll 2Ll 4Ll |#
(list #| 1Mm |# n n n r sr lr n s l r r r )
(list #| 2Mm |# n n n r sr lr n s l r r r )
(list #| 4Mm |# n n n r sr lr n s l r r r )
(list #| 1Ml |# r r r n s l r sr lr n n n )
(list #| 2Ml |# sr sr sr s n w sr r wr s s s )
(list #| 4Ml |# lr lr lr l w n lr wr r l l l )
(list #| 1Lm |# n n n r sr lr n s l r r r )
(list #| 2Lm |# s s s sr r wr s n w sr sr sr)
(list #| 4Lm |# l l l lr wr r l w n lr lr lr)
(list #| 1Ll |# r r r n s l r sr lr n n n )
(list #| 2Ll |# r r r n s l r sr lr n n n )
(list #| 4Ll |# r r r n s l r sr lr n n n )))))
;;; Of course, the table above is a lie. We also need to factor in the
;;; order of the source data to cope with swapping half of a unit at the
;;; end of a scanline, since we are trying to avoid de-ref'ing off the
;;; end of the source.
;;;
;;; Defines whether the first half of a unit has the first half of the data
(defconstant +image-swap-lsb-first-p+
'#.(make-array
12 :initial-contents
(list t #| 1mm |#
t #| 2mm |#
t #| 4mm |#
t #| 1ml |#
nil #| 2ml |#
nil #| 4ml |#
t #| 1lm |#
nil #| 2lm |#
nil #| 4lm |#
t #| 1ll |#
t #| 2ll |#
t #| 4ll |#
)))
(defun image-swap-function
(bits-per-pixel
from-bitmap-unit from-byte-lsb-first-p from-bit-lsb-first-p
to-bitmap-unit to-byte-lsb-first-p to-bit-lsb-first-p)
(declare (type (member 1 4 8 16 24 32) bits-per-pixel)
(type (member 8 16 32) from-bitmap-unit to-bitmap-unit)
(type generalized-boolean from-byte-lsb-first-p from-bit-lsb-first-p
to-byte-lsb-first-p to-bit-lsb-first-p)
(clx-values function lsb-first-p))
(cond ((index= bits-per-pixel 1)
(let ((from-index
(index+
(ecase from-bitmap-unit (32 2) (16 1) (8 0))
(if from-bit-lsb-first-p 3 0)
(if from-byte-lsb-first-p 6 0))))
(values
(aref +image-swap-function+ from-index
(index+
(ecase to-bitmap-unit (32 2) (16 1) (8 0))
(if to-bit-lsb-first-p 3 0)
(if to-byte-lsb-first-p 6 0)))
(aref +image-swap-lsb-first-p+ from-index))))
(t
(values
(if (if (index= bits-per-pixel 4)
(eq from-bit-lsb-first-p to-bit-lsb-first-p)
(eq from-byte-lsb-first-p to-byte-lsb-first-p))
'image-noswap
(ecase bits-per-pixel
(4 'image-swap-nibbles)
(8 'image-noswap)
(16 'image-swap-two-bytes)
(24 'image-swap-three-bytes)
(32 'image-swap-four-bytes)))
from-byte-lsb-first-p))))
;;;-----------------------------------------------------------------------------
;;; GET-IMAGE
(defun read-pixarray-1 (buffer-bbuf index array x y width height
padded-bytes-per-line bits-per-pixel)
(declare (type buffer-bytes buffer-bbuf)
(type pixarray-1 array)
(type card16 x y width height)
(type array-index index padded-bytes-per-line)
(type (member 1 4 8 16 24 32) bits-per-pixel)
(ignore bits-per-pixel))
#.(declare-buffun)
(with-vector (buffer-bbuf buffer-bytes)
(do* ((start (index+ index
(index* y padded-bytes-per-line)
(index-ceiling x 8))
(index+ start padded-bytes-per-line))
(y 0 (index1+ y))
(left-bits (the array-index
(mod (the (integer #x-FFFF 0) (- x))
8)))
(right-bits (index-mod (index- width left-bits) 8))
(middle-bits (- width left-bits right-bits))
(middle-bytes (floor middle-bits 8)))
((index>= y height))
(declare (type array-index start y left-bits right-bits))
(declare (fixnum middle-bits middle-bytes))
(cond ((< middle-bits 0)
(let ((byte (aref buffer-bbuf (index1- start)))
(x left-bits))
(declare (type card8 byte)
(type array-index x))
(when (index> right-bits 6)
(setf (aref array y (index- x 1))
(read-image-load-byte 1 7 byte)))
(when (and (index> left-bits 1)
(index> right-bits 5))
(setf (aref array y (index- x 2))
(read-image-load-byte 1 6 byte)))
(when (and (index> left-bits 2)
(index> right-bits 4))
(setf (aref array y (index- x 3))
(read-image-load-byte 1 5 byte)))
(when (and (index> left-bits 3)
(index> right-bits 3))
(setf (aref array y (index- x 4))
(read-image-load-byte 1 4 byte)))
(when (and (index> left-bits 4)
(index> right-bits 2))
(setf (aref array y (index- x 5))
(read-image-load-byte 1 3 byte)))
(when (and (index> left-bits 5)
(index> right-bits 1))
(setf (aref array y (index- x 6))
(read-image-load-byte 1 2 byte)))
(when (index> left-bits 6)
(setf (aref array y (index- x 7))
(read-image-load-byte 1 1 byte)))))
(t
(unless (index-zerop left-bits)
(let ((byte (aref buffer-bbuf (index1- start)))
(x left-bits))
(declare (type card8 byte)
(type array-index x))
(setf (aref array y (index- x 1))
(read-image-load-byte 1 7 byte))
(when (index> left-bits 1)
(setf (aref array y (index- x 2))
(read-image-load-byte 1 6 byte))
(when (index> left-bits 2)
(setf (aref array y (index- x 3))
(read-image-load-byte 1 5 byte))
(when (index> left-bits 3)
(setf (aref array y (index- x 4))
(read-image-load-byte 1 4 byte))
(when (index> left-bits 4)
(setf (aref array y (index- x 5))
(read-image-load-byte 1 3 byte))
(when (index> left-bits 5)
(setf (aref array y (index- x 6))
(read-image-load-byte 1 2 byte))
(when (index> left-bits 6)
(setf (aref array y (index- x 7))
(read-image-load-byte 1 1 byte))
))))))))
(do* ((end (index+ start middle-bytes))
(i start (index1+ i))
(x left-bits (index+ x 8)))
((index>= i end)
(unless (index-zerop right-bits)
(let ((byte (aref buffer-bbuf end))
(x (index+ left-bits middle-bits)))
(declare (type card8 byte)
(type array-index x))
(setf (aref array y (index+ x 0))
(read-image-load-byte 1 0 byte))
(when (index> right-bits 1)
(setf (aref array y (index+ x 1))
(read-image-load-byte 1 1 byte))
(when (index> right-bits 2)
(setf (aref array y (index+ x 2))
(read-image-load-byte 1 2 byte))
(when (index> right-bits 3)
(setf (aref array y (index+ x 3))
(read-image-load-byte 1 3 byte))
(when (index> right-bits 4)
(setf (aref array y (index+ x 4))
(read-image-load-byte 1 4 byte))
(when (index> right-bits 5)
(setf (aref array y (index+ x 5))
(read-image-load-byte 1 5 byte))
(when (index> right-bits 6)
(setf (aref array y (index+ x 6))
(read-image-load-byte 1 6 byte))
)))))))))
(declare (type array-index end i x))
(let ((byte (aref buffer-bbuf i)))
(declare (type card8 byte))
(setf (aref array y (index+ x 0))
(read-image-load-byte 1 0 byte))
(setf (aref array y (index+ x 1))
(read-image-load-byte 1 1 byte))
(setf (aref array y (index+ x 2))
(read-image-load-byte 1 2 byte))
(setf (aref array y (index+ x 3))
(read-image-load-byte 1 3 byte))
(setf (aref array y (index+ x 4))
(read-image-load-byte 1 4 byte))
(setf (aref array y (index+ x 5))
(read-image-load-byte 1 5 byte))
(setf (aref array y (index+ x 6))
(read-image-load-byte 1 6 byte))
(setf (aref array y (index+ x 7))
(read-image-load-byte 1 7 byte))))
)))))
(defun read-pixarray-4 (buffer-bbuf index array x y width height
padded-bytes-per-line bits-per-pixel)
(declare (type buffer-bytes buffer-bbuf)
(type pixarray-4 array)
(type card16 x y width height)
(type array-index index padded-bytes-per-line)
(type (member 1 4 8 16 24 32) bits-per-pixel)
(ignore bits-per-pixel))
#.(declare-buffun)
(with-vector (buffer-bbuf buffer-bytes)
(do* ((start (index+ index
(index* y padded-bytes-per-line)
(index-ceiling x 2))
(index+ start padded-bytes-per-line))
(y 0 (index1+ y))
(left-nibbles (mod (the fixnum (- x)) 2))
(right-nibbles (index-mod (index- width left-nibbles) 2))
(middle-nibbles (index- width left-nibbles right-nibbles))
(middle-bytes (index-floor middle-nibbles 2)))
((index>= y height))
(declare (type array-index start y
left-nibbles right-nibbles middle-nibbles middle-bytes))
(unless (index-zerop left-nibbles)
(setf (aref array y 0)
(read-image-load-byte
4 4 (aref buffer-bbuf (index1- start)))))
(do* ((end (index+ start middle-bytes))
(i start (index1+ i))
(x left-nibbles (index+ x 2)))
((index>= i end)
(unless (index-zerop right-nibbles)
(setf (aref array y (index+ left-nibbles middle-nibbles))
(read-image-load-byte 4 0 (aref buffer-bbuf end)))))
(declare (type array-index end i x))
(let ((byte (aref buffer-bbuf i)))
(declare (type card8 byte))
(setf (aref array y (index+ x 0))
(read-image-load-byte 4 0 byte))
(setf (aref array y (index+ x 1))
(read-image-load-byte 4 4 byte))))
)))
(defun read-pixarray-8 (buffer-bbuf index array x y width height
padded-bytes-per-line bits-per-pixel)
(declare (type buffer-bytes buffer-bbuf)
(type pixarray-8 array)
(type card16 x y width height)
(type array-index index padded-bytes-per-line)
(type (member 1 4 8 16 24 32) bits-per-pixel)
(ignore bits-per-pixel))
#.(declare-buffun)
(with-vector (buffer-bbuf buffer-bytes)
(do* ((start (index+ index
(index* y padded-bytes-per-line)
x)
(index+ start padded-bytes-per-line))
(y 0 (index1+ y)))
((index>= y height))
(declare (type array-index start y))
(do* ((end (index+ start width))
(i start (index1+ i))
(x 0 (index1+ x)))
((index>= i end))
(declare (type array-index end i x))
(setf (aref array y x)
(the card8 (aref buffer-bbuf i)))))))
(defun read-pixarray-16 (buffer-bbuf index array x y width height
padded-bytes-per-line bits-per-pixel)
(declare (type buffer-bytes buffer-bbuf)
(type pixarray-16 array)
(type card16 width height)
(type array-index index padded-bytes-per-line)
(type (member 1 4 8 16 24 32) bits-per-pixel)
(ignore bits-per-pixel))
#.(declare-buffun)