Skip to content
Snippets Groups Projects
image.lisp 100 KiB
Newer Older
dan's avatar
dan committed
;;; -*- 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))
dan's avatar
dan committed

(defconstant +empty-data-z+
  '#.(make-array '(0 0) :element-type 'pixarray-1-element-type))
dan's avatar
dan committed

(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))
csr21's avatar
csr21 committed
  (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
dan's avatar
dan committed
  (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))
csr21's avatar
csr21 committed
  (pixarray +empty-data-z+ :type pixarray))
dan's avatar
dan committed

(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))))
csr21's avatar
csr21 committed
		(unless unit (setq unit +image-unit+))
dan's avatar
dan committed
		(unless pad
		  (setq pad
			(dolist (pad '(32 16 8))
csr21's avatar
csr21 committed
			  (when (and (index<= pad +image-pad+)
dan's avatar
dan committed
				     (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+
dan's avatar
dan committed
 '#.(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)
dan's avatar
dan committed

(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)
csr21's avatar
csr21 committed
      (let ((byte-reverse +image-byte-reverse+))
dan's avatar
dan committed
	(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)
csr21's avatar
csr21 committed
      (let ((byte-reverse +image-byte-reverse+))
dan's avatar
dan committed
	(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)
csr21's avatar
csr21 committed
      (let ((byte-reverse +image-byte-reverse+))
dan's avatar
dan committed
	(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)
csr21's avatar
csr21 committed
      (let ((byte-reverse +image-byte-reverse+))
dan's avatar
dan committed
	(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+
dan's avatar
dan committed
 '#.(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  |#
dan's avatar
dan committed
	(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 )))))
dan's avatar
dan committed

;;; 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+
dan's avatar
dan committed
 '#.(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 |#
dan's avatar
dan committed

(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
csr21's avatar
csr21 committed
	     (aref +image-swap-function+ from-index
dan's avatar
dan committed
		   (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)))
csr21's avatar
csr21 committed
	     (aref +image-swap-lsb-first-p+ from-index))))
dan's avatar
dan committed
	(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)