anchor.diff:  ;;; to the Independent JPEG Group - colorspace conversion and DCT algorithms were adopted from their sources;
   ;;; to Jeff Dalton for his wise paper "Common Lisp Pitfalls".
   
  -(defpackage #:jpeg (:use #:common-lisp))
  +(defpackage #:jpeg
  +  (:use #:common-lisp)
  +  (:export #:encode-image
  +           #:decode-stream
  +           #:decode-image
  +           #:jpeg-to-bmp))
  +
   (in-package #:jpeg)
  -
  -(eval-when (compile)
  -  (export '(encode-image decode-image jpeg-to-bmp)))
   
   (declaim (inline csize write-stuffed quantize get-average zigzag encode-block llm-dct descale crunch colorspace-convert subsample
                    inverse-llm-dct dequantize upsample extend recieve decode-ac decode-dc decode-block izigzag write-bits))
  
positive.code: ;;; Encodes block using specified huffman tables, returns new pred (DC prediction value)
  ;;; and last code written to stream for padding
  (defun encode-block (block tables pred s)
    (declare #.*optimize* (type fixnum pred newpred diff dcpos)
             (type (simple-vector *) block))
    (let* ((ehufsi-dc (first (first tables)))
           (ehufco-dc (second (first tables)))
  
negative.code:         (write-byte 24 o) ;bitcount, 24-bit BMP
          (write-byte 0 o)
          (write-sequence (make-array 24 :initial-element 0 :element-type 'unsigned-byte) o) ;the rest of header
          (ecase number-components
            (1
             (loop :for y :from (1- h) :downto 0 :do
                   (loop :for x :from (1- w) :downto 0 :do
                         (let ((grey (svref rgb (+ x (* y w)))))
                           (write-byte grey o)
                           (write-byte grey o)
                           (write-byte grey o)))
                   (dotimes (i compl)
                     (write-byte 0 o))))
            (3
             (loop for y fixnum from (1- h) downto 0
                   for ypos fixnum = (* y 3 w) do
                   (loop for x fixnum from ypos to (plus ypos (* (1- w) 3)) by 3 do
                         (write-byte (the unsigned-byte (svref rgb x)) o)
                         (write-byte (the unsigned-byte (svref rgb (1+ x))) o)
                         (write-byte (the unsigned-byte (svref rgb (plus 2 x))) o))
                   (loop for i fixnum from 0 below compl do ;adjusting to double-word
                         (write-byte 0 o)))))))))
  
  
  ;;; Provides simple user interface for encoder: quality may vary 1 to 5 (decreasing)
  