anchor.diff:  
   (in-package #:jpeg)
   
  -(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))
  +(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))
   
   (eval-when (:compile-toplevel :load-toplevel :execute)
     (defvar *optimize*  '(optimize (safety 0) (space 0) (debug 0) (speed 3))))
  
positive.code:          (nwidth (mul (descriptor-width image) (descriptor-ncomp image)))
           (dend (mul (descriptor-height image) nwidth))
           (fr (make-array ncomp :initial-contents
                           (loop for i fixnum from 0 below ncomp ;collecting sampling rates for a components in the scan
                                 for cid fixnum = (first (svref (scan-cdesc scan) i))
                                 for pos fixnum = (position cid (descriptor-cid image))
                                 collecting (list (svref (descriptor-H image) pos)
                                                  (svref (descriptor-V image) pos)))))
           (Hmax (loop for entry across fr maximize (first entry)))
           (Vmax (loop for entry across fr maximize (second entry)))
           (x-growth (ash Hmax 3))
           (y-growth (ash Vmax 3))
           (freqs (make-array ncomp :initial-contents
                              (loop for i fixnum from 0 below ncomp ;collecting sampling frequencies
                                    for cid fixnum = (first (svref (scan-cdesc scan) i))
                                    for pos fixnum = (position cid (descriptor-cid image))
                                    collecting (list (svref (descriptor-iH image) pos)
                                                  (svref (descriptor-iV image) pos)))))
           (preds (make-array ncomp :initial-element 0)))
      (declare #.*optimize*
               (type fixnum ncomp Hmax Vmax x-growth y-growth nwidth)
               (type (simple-vector *) freqs fr)
               (dynamic-extent fr freqs))
      (catch 'marker
        (loop with tables = (make-array ncomp
                                        :initial-contents
                                        (loop for i fixnum from 0 below ncomp
                                              for ta fixnum = (logand (second (svref (scan-cdesc scan) i)) 15)
                                              for td fixnum = (ash (second (svref (scan-cdesc scan) i)) -4)
                                              collecting (vector (svref (descriptor-huff-ac image) ta)
                                                                 (svref (descriptor-huff-dc image) td)))) do
              (loop for comp fixnum from 0 below ncomp
                    for pos fixnum = (position (first (svref (scan-cdesc scan) comp)) (descriptor-cid image)) ;an offset for byte positioning
                    for q-tab = (svref (descriptor-qtables image) (svref (descriptor-qdest image) comp))
                    for H fixnum = (first (svref freqs comp))
                    for V fixnum = (second (svref freqs comp))
                    for nw fixnum = (mul nwidth V)
                    for nx fixnum = (mul (descriptor-ncomp image) H)
                    for blocks-y fixnum = (second (svref fr comp))
                    for blocks-x fixnum = (first (svref fr comp)) do
                    (loop for y fixnum from 0 below blocks-y
                          for y-pos fixnum from (mul (ash y 3) V) by (ash V 3) do
                          (loop for x fixnum from 0 below blocks-x
                                for x-pos fixnum from (mul (ash x 3) H) by (ash H 3)
                                for decoded-block = (izigzag (decode-block (descriptor-zz image) (svref tables comp) nextbit s) +zzbuf+) do
                                ;; DC decoding and predictor update
                                (incf (dbref decoded-block 0 0) (svref preds comp))
                                (setf (svref preds comp) (dbref decoded-block 0 0))
                                (when (and (< (plus x-pos (scan-x scan)) (descriptor-width image))
                                           (< (plus y-pos (scan-y scan)) (descriptor-height image)))
                                  ;; inverse DCT and block write to the buffer
                                  (inverse-llm-dct decoded-block q-tab)
                                  (upsample image scan decoded-block x-pos y-pos H V pos nwidth nw nx dend)))))
              (incf (scan-x scan) x-growth)
              (when (<= (descriptor-width image) (scan-x scan))
                (incf (scan-y scan) y-growth)
                (setf (scan-x scan) 0))))))
  
  ;;; Scan decoding subroutine
  (defun decode-scan (image i s)
    (let ((scan (svref (descriptor-scans image) i)))
      (read-byte s) ;length
      (read-byte s)
      (loop with ncomp fixnum = (setf (scan-ncomp scan) (read-byte s))
            for j fixnum from 0 below ncomp do
            (setf (first (svref (scan-cdesc scan) j)) (read-byte s)) ;component ID
            (setf (second (svref (scan-cdesc scan) j)) (read-byte s))) ;Td and Ta nibbles
      (read-byte s)
      (read-byte s)
      (read-byte s)
      (if (= (descriptor-restart-interval image) 0)
          (decode-chunk image scan s) ;reading the whole scan at once
        (loop for term = (decode-chunk image scan s)
              while (eq 'restart term)
              finally (return term))))) ;or in pieces
  
  ;;; Macro that bounds value in 0..255 range
  (defmacro limit (n)
  
negative.code:   (loop for mk fixnum = (cond ((zerop term) (read-marker s))
                                (t term)) do
          (setf term 0)
          (cond ((= #xe0 (logand #xf0 mk)) ; APPn marker
                 (read-app s))
                (t (cond ((= mk +M_DAC+) (error "Arithmetic encoding not supported"))
                         ((= mk +M_DRI+) (read-dri image s))
  