-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathjpeg.lisp
2026 lines (1870 loc) · 87.4 KB
/
jpeg.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
;;; ANSI Common Lisp (mostly) baseline JPEG encoder/decoder implementation
;;; Copyright [c] 1999,2015-2017 Eugene Zaikonnikov <eugene@funcall.org>
;;;
;;; This software is distributed under the terms of BSD-like license
;;; [see LICENSE for details]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; This software was sponsored by Kelly E. Murray
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Two main functions available:
;;;
;;; (encode-image filename image ncomp h w &key sampling q-tabs q-factor), where:
;;; filename - output file name
;;; ncomp - number of components (1-4)
;;; h, w - source image height and width respectively
;;; image - array of B, G, R pixels in case of three component image,
;;; array of grayscale pixels in case of single component,
;;; array of 2 or 4 pixels in the case of two or four component image respectively
;;; :q-tabs - specifies quantization tables vector, should be 1 for 1,
;;; 2 for 2, 2 for 3 and 4 entries for 4 components
;;; :sampling - sampling frequency for ncomp components by X and Y axis,
;;; e.g. '((2 2) (1 1) (1 1)) for three components, can be omitted
;;; for grayscale and RGB images
;;; :q-factor - quality specifier (1-64), default is 64
;;; Returns nothing of practical use
;;;
;;; (decode-image filename &key buffer (colorspace-conversion t))
;;; filename - jpeg file name
;;; Returns (multiple-valued) IMAGE array in the same format as encoder source image,
;;; image HEIGHT and image WIDTH
;;; A pre-allocated BUFFER can be specified (see JPEG:ALLOCATE-BUFFER).
;;; If :colorspace-conversion is NIL, no conversion from YUV space is performed.
;;;
;;; For those impatient additional function defined:
;;; (jpeg-to-bmp &key infile outfile)
;;; Converts JPEG image specified by infile into Microsoft Windows 24-bit BMP format (outfile),
;;; returns NIL
;;;
;;; Additionaly, you may use more user-friendly version of encode-image: encode-wrapper.
;;; (encoding-wrapper filename image ncomp h w &key quality)
;;; All parameters have the same meaning as in encode-image, except quality.
;;; It is an integer value ranging 1 to 5 which specifies
;;; subjective quality of a resulting image.
;;; Technical details: encoder produces interleaved jpeg file, without restarts.
;;; In a case of 3 components image will be written in JFIF format.
;;; Decoder can deal with *almost* all baseline jpeg files, regardless JFIF or not.
;;; It supports restarts, interleaved/noninterleaved files, multiscan images, 1 to 4 color
;;; channels, up to 4 quantization tables and two sets of huffman tables with random order
;;; of their definition inside the image. Decoder *does not* support DNL marker, due to
;;; it's rarity and amount of work needed to implement it, so decoder isn't baseline in a
;;; strict sense.
;;; Both encoder and decoder utilize Loeffer, Ligtenberg and Moschytz integer discrete
;;; cosine transform algorithms with 12 multiplications in each loop.
;;; Based on CCITT Rec. T.81
;;; "Information technology - digital compression and coding of continious-tone still images
;;; - requirements and guidelines".
;;; Credits:
;;; 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".
(in-package #:jpeg)
(declaim (inline csize quantize get-average zigzag read-jpeg-byte
llm-dct descale crunch colorspace-convert subsample inverse-llm-dct
dequantize upsample extend recieve decode-ac decode-dc decode-block
izigzag write-bits limit))
(deftype uint8 () '(unsigned-byte 8))
(deftype uint8-array () '(simple-array uint8 (*)))
(deftype uint8-2d-array () '(simple-array uint8-array (*)))
(deftype suint8 () '(signed-byte 8))
(deftype sint8-array () '(simple-array sint8 (*)))
(deftype sint8-2d-array () '(simple-array sint8-array (*)))
(deftype sint16 () '(signed-byte 16))
(deftype sint16-array () '(simple-array sint16 (*)))
(deftype sint16-2d-array () '(simple-array sint16-array (*)))
(deftype uint16 () '(unsigned-byte 16))
(deftype uint16-array () '(simple-array uint16 (*)))
(deftype uint16-2d-array () '(simple-array uint16-array (*)))
(deftype fixnum-array () '(simple-array fixnum (*)))
(deftype fixnum-2d-array () '(simple-array fixnum-array (*)))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defparameter *optimize* '(optimize (safety 0) (space 0) (debug 0) (speed 3))))
(eval-when (:compile-toplevel :load-toplevel :execute)
;;; For ease of reference
(defmacro u8ref (data x y)
`(the uint8 (aref (the uint8-array (aref (the uint8-2d-array ,data) ,y)) ,x)))
(defmacro s16ref (data x y)
`(the sint16 (aref (the sint16-array (aref (the sint16-2d-array ,data) ,y)) ,x)))
(defmacro u16ref (data x y)
`(the uint16 (aref (the uint16-array (aref (the uint16-2d-array ,data) ,y)) ,x)))
(defmacro fixref (data x y)
`(the fixnum (aref (the fixnum-array (aref (the fixnum-2d-array ,data) ,y)) ,x)))
;;; Integer arithmetic wrappers
(defmacro plus (a b)
`(the fixnum (+ (the fixnum ,a) (the fixnum ,b))))
(defmacro minus (a b)
#+(or clisp abcl)
`(- ,a ,b)
#-(or clisp abcl)
`(the fixnum (- (the fixnum ,a) (the fixnum ,b))))
(defmacro mul (a b)
`(the fixnum (* (the fixnum ,a) (the fixnum ,b))))
(defmacro plus3 (x y z)
`(plus (plus ,x ,y) ,z))
(defmacro mul3 (x y z)
`(mul (mul ,x ,y) ,z)))
;;; Somewhat silly, but who knows...
(when (/= (integer-length most-positive-fixnum)
(integer-length most-negative-fixnum))
(error "Can't compile with asymmetric fixnums!"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Here we define some constants (markers, quantization and huffman tables etc.)
(defmacro define-constant (name value &optional doc)
`(defconstant ,name (if (boundp ',name) (symbol-value ',name) ,value)
,@(when doc (list doc))))
(eval-when (:compile-toplevel :load-toplevel)
(defun uint8-array (&rest contents)
(the uint8-array (make-array (length contents) :element-type 'uint8
:initial-contents contents)))
(defun 2d-uint8-array (&rest contents)
(let ((nrow (length contents)))
(the uint8-2d-array
(make-array nrow
:element-type 'uint8-array
:initial-contents
(loop for row in contents
collecting (make-array (length row) :element-type 'uint8
:initial-contents row))))))
(defun 2d-sint16-array (&rest contents)
(let ((nrow (length contents)))
(the sint16-2d-array
(make-array nrow
:element-type 'sint16-array
:initial-contents
(loop for row in contents
collecting (make-array (length row) :element-type 'sint16
:initial-contents row))))))
;;; Source huffman tables for the encoder
(define-constant +luminance-dc-bits+
(uint8-array #x00 #x01 #x05 #x01 #x01 #x01 #x01 #x01
#x01 #x00 #x00 #x00 #x00 #x00 #x00 #x00))
(define-constant +luminance-dc-values+
(uint8-array #x00 #x01 #x02 #x03 #x04 #x05 #x06 #x07 #x08 #x09 #x0a #x0b))
(define-constant +chrominance-dc-bits+
(uint8-array #x00 #x03 #x01 #x01 #x01 #x01 #x01 #x01
#x01 #x01 #x01 #x00 #x00 #x00 #x00 #x00))
(define-constant +chrominance-dc-values+
(uint8-array #x00 #x01 #x02 #x03 #x04 #x05 #x06 #x07 #x08 #x09 #x0a #x0b))
(define-constant +luminance-ac-bits+
(uint8-array #x00 #x02 #x01 #x03 #x03 #x02 #x04 #x03
#x05 #x05 #x04 #x04 #x00 #x00 #x01 #x7d))
(define-constant +luminance-ac-values+
(uint8-array
#x01 #x02 #x03 #x00 #x04 #x11 #x05 #x12
#x21 #x31 #x41 #x06 #x13 #x51 #x61 #x07
#x22 #x71 #x14 #x32 #x81 #x91 #xa1 #x08
#x23 #x42 #xb1 #xc1 #x15 #x52 #xd1 #xf0
#x24 #x33 #x62 #x72 #x82 #x09 #x0a #x16
#x17 #x18 #x19 #x1a #x25 #x26 #x27 #x28
#x29 #x2a #x34 #x35 #x36 #x37 #x38 #x39
#x3a #x43 #x44 #x45 #x46 #x47 #x48 #x49
#x4a #x53 #x54 #x55 #x56 #x57 #x58 #x59
#x5a #x63 #x64 #x65 #x66 #x67 #x68 #x69
#x6a #x73 #x74 #x75 #x76 #x77 #x78 #x79
#x7a #x83 #x84 #x85 #x86 #x87 #x88 #x89
#x8a #x92 #x93 #x94 #x95 #x96 #x97 #x98
#x99 #x9a #xa2 #xa3 #xa4 #xa5 #xa6 #xa7
#xa8 #xa9 #xaa #xb2 #xb3 #xb4 #xb5 #xb6
#xb7 #xb8 #xb9 #xba #xc2 #xc3 #xc4 #xc5
#xc6 #xc7 #xc8 #xc9 #xca #xd2 #xd3 #xd4
#xd5 #xd6 #xd7 #xd8 #xd9 #xda #xe1 #xe2
#xe3 #xe4 #xe5 #xe6 #xe7 #xe8 #xe9 #xea
#xf1 #xf2 #xf3 #xf4 #xf5 #xf6 #xf7 #xf8
#xf9 #xfa))
(define-constant +chrominance-ac-bits+
(uint8-array #x00 #x02 #x01 #x02 #x04 #x04 #x03 #x04
#x07 #x05 #x04 #x04 #x00 #x01 #x02 #x77))
(define-constant +chrominance-ac-values+
(uint8-array
#x00 #x01 #x02 #x03 #x11 #x04 #x05 #x21
#x31 #x06 #x12 #x41 #x51 #x07 #x61 #x71
#x13 #x22 #x32 #x81 #x08 #x14 #x42 #x91
#xa1 #xb1 #xc1 #x09 #x23 #x33 #x52 #xf0
#x15 #x62 #x72 #xd1 #x0a #x16 #x24 #x34
#xe1 #x25 #xf1 #x17 #x18 #x19 #x1a #x26
#x27 #x28 #x29 #x2a #x35 #x36 #x37 #x38
#x39 #x3a #x43 #x44 #x45 #x46 #x47 #x48
#x49 #x4a #x53 #x54 #x55 #x56 #x57 #x58
#x59 #x5a #x63 #x64 #x65 #x66 #x67 #x68
#x69 #x6a #x73 #x74 #x75 #x76 #x77 #x78
#x79 #x7a #x82 #x83 #x84 #x85 #x86 #x87
#x88 #x89 #x8a #x92 #x93 #x94 #x95 #x96
#x97 #x98 #x99 #x9a #xa2 #xa3 #xa4 #xa5
#xa6 #xa7 #xa8 #xa9 #xaa #xb2 #xb3 #xb4
#xb5 #xb6 #xb7 #xb8 #xb9 #xba #xc2 #xc3
#xc4 #xc5 #xc6 #xc7 #xc8 #xc9 #xca #xd2
#xd3 #xd4 #xd5 #xd6 #xd7 #xd8 #xd9 #xda
#xe2 #xe3 #xe4 #xe5 #xe6 #xe7 #xe8 #xe9
#xea #xf2 #xf3 #xf4 #xf5 #xf6 #xf7 #xf8
#xf9 #xfa))
;;;Zigzag encoding matrix
(define-constant +zigzag-index+
(2d-uint8-array '(0 1 5 6 14 15 27 28)
'(2 4 7 13 16 26 29 42)
'(3 8 12 17 25 30 41 43)
'(9 11 18 24 31 40 44 53)
'(10 19 23 32 39 45 52 54)
'(20 22 33 38 46 51 55 60)
'(21 34 37 47 50 56 59 61)
'(35 36 48 49 57 58 62 63)))
;;;JPEG file markers
(defconstant +M_COM+ #xfe)
(defconstant +M_SOF0+ #xc0)
(defconstant +M_SOF2+ #xc2)
(defconstant +M_DHT+ #xc4)
(defconstant +M_RST0+ #xd0)
(defconstant +M_RST7+ #xd7)
(defconstant +M_SOI+ #xd8)
(defconstant +M_EOI+ #xd9)
(defconstant +M_SOS+ #xda)
(defconstant +M_DQT+ #xdb)
(defconstant +M_DNL+ #xdc)
(defconstant +M_DRI+ #xdd)
(defconstant +M_DAC+ #xcc)
(defconstant +M_APP0+ #xe0)
(defconstant +M_APP14+ #xee)
;;; Default quantization tables
(define-constant +q-luminance+
(2d-uint8-array '(16 11 10 16 24 40 51 61)
'(12 12 14 19 26 58 60 55)
'(14 13 16 24 40 57 69 56)
'(14 17 22 29 51 87 80 62)
'(18 22 37 56 68 109 103 77)
'(24 35 55 64 81 104 113 92)
'(49 64 78 87 103 121 120 101)
'(72 92 95 98 112 100 103 99)))
(define-constant +q-chrominance+
(2d-uint8-array '(17 18 24 47 99 99 99 99)
'(18 21 26 66 99 99 99 99)
'(24 26 56 99 99 99 99 99)
'(47 66 99 99 99 99 99 99)
'(99 99 99 99 99 99 99 99)
'(99 99 99 99 99 99 99 99)
'(99 99 99 99 99 99 99 99)
'(99 99 99 99 99 99 99 99)))
(define-constant +q-luminance-hi+
(2d-uint8-array '(10 7 6 10 15 25 32 38)
'(8 8 9 12 16 36 38 34)
'(9 8 10 15 25 36 43 35)
'(9 11 14 18 32 54 50 39)
'(11 14 23 35 42 68 64 48)
'(15 22 34 40 51 65 71 58)
'(31 40 49 54 64 76 75 63)
'(45 58 59 61 70 62 64 62)))
(define-constant +q-chrominance-hi+
(2d-uint8-array '(11 11 15 29 62 62 62 62)
'(11 13 16 41 62 62 62 62)
'(15 16 35 62 62 62 62 62)
'(29 41 62 62 62 62 62 62)
'(62 62 62 62 62 62 62 62)
'(62 62 62 62 62 62 62 62)
'(62 62 62 62 62 62 62 62)
'(62 62 62 62 62 62 62 62)))
(defconstant +max-sample+ 255)
)
;;; Quantization performance test, each branch quantizes 30000 random matrixes
(eval-when (:compile-toplevel)
(defconstant +quantize-calibration-loops+ 30000)
(format t "Performing compile-time optimization.. please wait.~%")
(finish-output)
(defun qat1 ()
(loop for i fixnum from 1 to +quantize-calibration-loops+ do
(loop for row across +q-luminance+ do
(loop for q-coef fixnum across row
maximize (round (random 128) q-coef)))))
(defun qat2 ()
(loop for i fixnum from 1 to +quantize-calibration-loops+ do
(loop for q-row across +q-luminance+ do
(loop for val fixnum = (random 128)
for absval fixnum = (abs val)
for qc fixnum across q-row
maximize
(cond ((< absval (ash qc -1))
0)
((<= absval qc)
(if (minusp val)
-1
1))
((<= (ash absval -1) qc)
(if (zerop (logand absval 1))
(if (minusp val)
-1
1)
(if (minusp val)
-2
2)))
(t
(round val qc)))))))
(compile 'qat1)
(compile 'qat2)
(defvar *quantize-optimization*
(<= (let ((time1 (get-internal-run-time)))
(qat1)
(minus (get-internal-run-time) time1))
(let ((time1 (get-internal-run-time)))
(qat2)
(minus (get-internal-run-time) time1))))
(format t "Done.~%")
(finish-output))
(define-constant +q-tables+ (vector +q-luminance+ +q-chrominance+))
;;; This table is used to map coefficients into SSSS value
(define-constant +csize+ (make-array 2047
:initial-contents
(loop for i fixnum from 0 to 2046
collecting (integer-length (abs (minus i 1023))))))
;;; Some constants for colorspace mapper
(defconstant shift (1- (integer-length (ash most-positive-fixnum -7))))
(defconstant +.299+ (round (+ (* 0.299 (ash 1 shift)) 0.5)))
(defconstant +.587+ (round (+ (* 0.587 (ash 1 shift)) 0.5)))
(defconstant +.114+ (round (+ (* 0.114 (ash 1 shift)) 0.5)))
(defconstant +-.1687+ (round (+ (* -0.1687 (ash 1 shift)) 0.5)))
(defconstant +-.3313+ (round (+ (* -0.3313 (ash 1 shift)) 0.5)))
(defconstant +-.4187+ (round (+ (* -0.4187 (ash 1 shift)) 0.5)))
(defconstant +-.0813+ (round (+ (* -0.0813 (ash 1 shift)) 0.5)))
(defconstant +.5+ (round (+ (* 0.5 (ash 1 shift)) 0.5)))
(defconstant +uvoffset+ (ash 128 shift))
(defconstant +one-half+ (1- (ash 1 (1- shift))))
(defconstant +r-y-off+ 0)
(defconstant +g-y-off+ 256)
(defconstant +b-y-off+ (* 2 256))
(defconstant +r-u-off+ (* 3 256))
(defconstant +g-u-off+ (* 4 256))
(defconstant +b-u-off+ (* 5 256))
(defconstant +r-v-off+ +b-u-off+)
(defconstant +g-v-off+ (* 6 256))
(defconstant +b-v-off+ (* 7 256))
(declaim (type fixnum-array +ctab+ +cr-r-tab+ +cb-g-tab+ +cr-g-tab+ +cb-b-tab+))
;;;Direct color conversion table
(define-constant +ctab+
(let ((table (make-array 2048 :element-type 'fixnum :initial-element 0)))
(loop for i fixnum from 0 to 255 do
(setf (aref table (plus i +r-y-off+))
(mul +.299+ i))
(setf (aref table (plus i +g-y-off+))
(mul +.587+ i))
(setf (aref table (plus i +b-y-off+))
(mul +.114+ i))
(setf (aref table (plus i +r-u-off+))
(mul +-.1687+ i))
(setf (aref table (plus i +g-u-off+))
(mul +-.3313+ i))
(setf (aref table (plus i +b-u-off+))
(+ (mul +.5+ i) +uvoffset+ +one-half+))
(setf (aref table (plus i +r-v-off+))
(+ (mul +.5+ i) +uvoffset+ +one-half+))
(setf (aref table (plus i +g-v-off+))
(mul +-.4187+ i))
(setf (aref table (plus i +b-v-off+))
(mul +-.0813+ i)))
table))
;;; Constantsants for the inverse colorspace conversion
(defconstant +1.40200+ (round (+ (* 1.40200 (ash 1 shift)) 0.5)))
(defconstant +1.77200+ (round (+ (* 1.77200 (ash 1 shift)) 0.5)))
(defconstant +-0.71414+ (round (+ (* -0.71414 (ash 1 shift)) 0.5)))
(defconstant +-0.34414+ (round (+ (* -0.34414 (ash 1 shift)) 0.5)))
;;; Inverse color conversion tables
(define-constant +cr-r-tab+ (make-array 256
:element-type 'fixnum
:initial-contents
(loop for i from 0 to 255
for x from -127
collect (ash (plus (mul +1.40200+ x) +one-half+) (- shift)))))
(define-constant +cb-g-tab+ (make-array 256
:element-type 'fixnum
:initial-contents
(loop for i from 0 to 255
for x from -127
collect (plus (mul +-0.34414+ x) +one-half+))))
(define-constant +cr-g-tab+ (make-array 256
:element-type 'fixnum
:initial-contents
(loop for i from 0 to 255
for x from -127
collect (mul +-0.71414+ x))))
(define-constant +cb-b-tab+ (make-array 256
:element-type 'fixnum
:initial-contents
(loop for i from 0 to 255
for x from -127
collect (ash (plus (mul +1.77200+ x) +one-half+) (- shift)))))
;;; Constants for LLM DCT
(defconstant dct-shift ; defining DCT scaling
(if (<= (integer-length most-positive-fixnum) 31)
(minus 13 (round (minus 31 (integer-length most-positive-fixnum)) 2))
13))
(defconstant +shift-1+ (1- dct-shift))
(defconstant +shift+1+ (1+ dct-shift))
(defconstant +shift+4+ (+ dct-shift 4))
(defconstant +FIX-0-298631336+ (round (+ (* 0.298631336 (ash 1 dct-shift)) 0.5)))
(defconstant +FIX-0-390180644+ (round (+ (* 0.390180644 (ash 1 dct-shift)) 0.5)))
(defconstant +FIX-0-541196100+ (round (+ (* 0.541196100 (ash 1 dct-shift)) 0.5)))
(defconstant +FIX-0-765366865+ (round (+ (* 0.765366865 (ash 1 dct-shift)) 0.5)))
(defconstant +FIX-0-899976223+ (round (+ (* 0.899976223 (ash 1 dct-shift)) 0.5)))
(defconstant +FIX-1-175875602+ (round (+ (* 1.175875602 (ash 1 dct-shift)) 0.5)))
(defconstant +FIX-1-501321110+ (round (+ (* 1.501321110 (ash 1 dct-shift)) 0.5)))
(defconstant +FIX-1-847759065+ (round (+ (* 1.847759065 (ash 1 dct-shift)) 0.5)))
(defconstant +FIX-1-961570560+ (round (+ (* 1.961570560 (ash 1 dct-shift)) 0.5)))
(defconstant +FIX-2-053119869+ (round (+ (* 2.053119869 (ash 1 dct-shift)) 0.5)))
(defconstant +FIX-2-562915447+ (round (+ (* 2.562915447 (ash 1 dct-shift)) 0.5)))
(defconstant +FIX-3-072711026+ (round (+ (* 3.072711026 (ash 1 dct-shift)) 0.5)))
(declaim (type uint8-array *idct-limit-array*))
;;; Post-IDCT limiting array
(defvar *idct-limit-array* (make-array 512 :initial-element 0 :element-type 'uint8))
(loop for n from 0
for i from 128 to 383 do
(setf (aref *idct-limit-array* i) n))
(loop for i from 384 to 511 do
(setf (aref *idct-limit-array* i) 255))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Error conditions
(define-condition jpeg-error (error)
())
(define-condition jpeg-encoder-error (jpeg-error)
())
(define-condition internal-jpeg-encoder-error (jpeg-encoder-error)
()
(:report (lambda (condition stream)
(declare (ignorable condition))
(format stream "Internal error"))))
(define-condition illegal-number-of-components (jpeg-encoder-error)
()
(:report (lambda (condition stream)
(declare (ignorable condition))
(format stream "Illegal number of components specified"))))
(define-condition invalid-sampling-list (jpeg-encoder-error)
((components :reader components :initarg :ncomp))
(:report (lambda (condition stream)
(format stream "Wrong sampling list for ~D component(s)" (components condition)))))
(define-condition invalid-quantization-tables (jpeg-encoder-error)
()
(:report (lambda (condition stream)
(declare (ignorable condition))
(format stream "Too many quantization tables specified"))))
(define-condition invalid-q-factor (jpeg-encoder-error)
()
(:report (lambda (condition stream)
(declare (ignorable condition))
(format stream "Invalid Q factor!"))))
(define-condition invalid-sampling (jpeg-encoder-error)
()
(:report (lambda (condition stream)
(declare (ignorable condition))
(format stream "Invalid sampling specification!"))))
(define-condition jpeg-decoder-error (jpeg-error)
())
(define-condition unsupported-jpeg-frame-marker (jpeg-decoder-error)
()
(:report (lambda (condition stream)
(declare (ignorable condition))
(format stream "Unsupported marker in the frame header"))))
(define-condition unsupported-jpeg-format (jpeg-decoder-error)
((code :reader marker-code :initarg :code))
(:report (lambda (condition stream)
(format stream "Unsupported JPEG format: ~X" (marker-code condition)))))
(define-condition unrecognized-file-format (jpeg-decoder-error)
()
(:report (lambda (condition stream)
(declare (ignorable condition))
(format stream "Unrecognized JPEG format"))))
(define-condition invalid-buffer-supplied (jpeg-decoder-error)
((buffer :reader buffer :initarg :buffer))
(:report (lambda (condition stream)
(declare (ignorable condition))
(format stream "Invalid buffer supplied: ~A" (buffer condition)))))
(define-condition unsupported-arithmetic-encoding (jpeg-decoder-error)
()
(:report (lambda (condition stream)
(declare (ignorable condition))
(format stream "Arithmetic encoding not supported"))))
(define-condition unsupported-dnl-marker (jpeg-decoder-error)
()
(:report (lambda (condition stream)
(declare (ignorable condition))
(format stream "DNL marker is not supported"))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Encoder part
;;; Subsamples inbuf into outbuf
(defun subsample (inbuf outbuf H V xlim ylim iH iV)
(declare #.*optimize*
(type fixnum H V xlim ylim iV iH)
(type sint16-2d-array inbuf)
(type (simple-array sint16-2d-array (*)) outbuf))
(loop for by fixnum from 0 below V do
(loop for bx fixnum from 0 below H
for block = (aref outbuf (plus bx (mul by H))) do
(loop for y fixnum from (the fixnum (ash by 3)) by iV
for yp fixnum from 0 to 7 do
(loop for x fixnum from (the fixnum (ash bx 3)) by iH
for xp fixnum from 0 to 7 do
(setf (s16ref block xp yp)
(the sint16 (cond ((and (<= x xlim) (<= y ylim))
(s16ref inbuf x y))
((and (> x xlim) (> y ylim))
(s16ref inbuf xlim ylim))
((> x xlim)
(s16ref inbuf xlim y))
((> y ylim)
(s16ref inbuf x ylim))
(t
(error 'internal-jpeg-encoder-error))
))))))))
;;; Returns sum of Vi*Hi
(defun count-relation (smp)
(loop for entry in smp
summing (mul (first entry) (second entry))))
;;; Cutting specified part of image (used for non-RGB images)
(defun crop-image (inbuf outbuf dx dy h w height width ncomp)
(let ((xend (plus dx (1- width)))
(yend (plus dy (1- height))))
(declare #.*optimize*
(type fixnum dx dy h w height width ncomp xend yend)
(type simple-array inbuf)
(type (simple-array sint16-2d-array (*)) outbuf))
(setf xend (min xend (1- w)))
(setf yend (min yend (1- h)))
(loop for yd fixnum from dy to yend
for ypos fixnum = (* w yd ncomp) do
(loop for xd fixnum from dx to xend
for pos fixnum = (plus (mul xd ncomp) ypos)
for cx fixnum = (minus xd dx)
for cy fixnum = (minus yd dy) do
(loop for i fixnum from 0 below ncomp do
(setf (s16ref (aref outbuf i) cx cy)
(minus (aref inbuf (plus pos i)) 128)))))
(values xend yend)))
;;; Direct color mapping
(defun colorspace-convert (RGB YUV dx dy h w height width)
(let ((xend (plus dx (1- width)))
(yend (plus dy (1- height)))
(Y (aref YUV 0))
(U (aref YUV 1))
(V (aref YUV 2)))
(declare #.*optimize*
(type fixnum dx dy h w height width xend yend)
(type sint16-2d-array Y U V)
(type fixnum-array +ctab+)
(type uint8-array RGB))
(setf xend (min xend (1- w)))
(setf yend (min yend (1- h)))
(loop for yd fixnum from dy to yend
for ypos fixnum = (mul3 w yd 3) do
(loop for xd fixnum from dx to xend
for pos fixnum = (plus (mul xd 3) ypos)
for r fixnum = (aref rgb (plus pos 2))
for g fixnum = (aref rgb (1+ pos))
for b fixnum = (aref rgb pos)
for cx fixnum = (minus xd dx)
for cy fixnum = (minus yd dy) do
(setf (s16ref Y cx cy) (minus (ash (the fixnum (+ (aref +ctab+ (plus r +r-y-off+))
(aref +ctab+ (plus g +g-y-off+))
(aref +ctab+ (plus b +b-y-off+))))
(- shift))
128))
(setf (s16ref U cx cy) (minus (ash (the fixnum (+ (aref +ctab+ (plus r +r-u-off+))
(aref +ctab+ (plus g +g-u-off+))
(aref +ctab+ (plus b +b-u-off+))))
(- shift))
128))
(setf (s16ref V cx cy) (minus (ash (the fixnum (+ (aref +ctab+ (plus r +r-v-off+))
(aref +ctab+ (plus g +g-v-off+))
(aref +ctab+ (plus b +b-v-off+))))
(- shift))
128))))
(values xend yend)))
;;; Converts given image sampling into frequencies of pixels of components
(defun convert-sampling (s Hmax Vmax)
(declare (type fixnum Hmax Vmax))
(make-array (length s)
:initial-contents (loop for entry in s
collecting (list (the fixnum (/ Hmax (first entry)))
(the fixnum (/ Vmax (second entry)))))))
;;; Quantization (also removes factor of 8 after DCT)
(defmacro quantize-block ()
(if *quantize-optimization*
'(loop for block-row across block
for q-row across q-table do
(loop for x fixnum from 0 to 7
for val fixnum = (ash (aref block-row x) -3)
for qc fixnum = (aref q-row x) do
(setf (aref block-row x) (the fixnum (round val qc)))))
'(loop for block-row across block
for q-row across q-table do
(loop for x fixnum from 0 to 7
for val fixnum = (ash (aref block-row x) -3)
for absval fixnum = (abs val)
for qc fixnum = (aref q-row x) do
(cond ((< absval (ash qc -1))
;; you won't believe, but under LWW 4.1 such ugly hack gives
;; very sufficient speedup
(setf (aref block-row x) 0))
((<= absval qc)
(if (minusp val)
(setf (aref block-row x) -1)
(setf (aref block-row x) 1)))
((<= (ash absval -1) qc)
(if (zerop (logand absval 1))
(if (minusp val)
(setf (aref block-row x) -1)
(setf (aref block-row x) 1))
(if (minusp val)
(setf (aref block-row x) -2)
(setf (aref block-row x) 2))))
(t
(setf (aref block-row x) (the fixnum (round val qc)))))))))
(defun quantize (block q-table)
(declare #.*optimize*
(type sint16-2d-array block)
(type uint8-2d-array q-table))
(quantize-block))
;;; LLM DCT aux definitions
(defun descale (x n)
(declare #.*optimize* (type fixnum x n))
(the fixnum (ash (plus x (ash 1 (1- n))) (- n))))
;;; Implementation of Loeffer, Ligtenberg and Moschytz forward DCT
(defun llm-dct (data)
(declare #.*optimize* (type sint16-2d-array data))
(loop with tmp0 fixnum and tmp1 fixnum and tmp2 fixnum
and tmp3 fixnum and tmp4 fixnum and tmp5 fixnum
and tmp6 fixnum and tmp7 fixnum and tmp10 fixnum
and tmp11 fixnum and tmp12 fixnum and tmp13 fixnum
and z1 fixnum and z2 fixnum and z3 fixnum
and z4 fixnum and z5 fixnum do
(loop for dptr across data do ; iterating over rows
(setf tmp0 (plus (aref dptr 0) (aref dptr 7)))
(setf tmp7 (minus (aref dptr 0) (aref dptr 7)))
(setf tmp1 (plus (aref dptr 1) (aref dptr 6)))
(setf tmp6 (minus (aref dptr 1) (aref dptr 6)))
(setf tmp2 (plus (aref dptr 2) (aref dptr 5)))
(setf tmp5 (minus (aref dptr 2) (aref dptr 5)))
(setf tmp3 (plus (aref dptr 3) (aref dptr 4)))
(setf tmp4 (minus (aref dptr 3) (aref dptr 4)))
(setf tmp10 (plus tmp0 tmp3))
(setf tmp13 (minus tmp0 tmp3))
(setf tmp11 (plus tmp1 tmp2))
(setf tmp12 (minus tmp1 tmp2))
(setf (aref dptr 0) (ash (plus tmp10 tmp11) 1))
(setf (aref dptr 4) (ash (minus tmp10 tmp11) 1))
(setf z1 (mul (plus tmp12 tmp13) +FIX-0-541196100+))
(setf (aref dptr 2) (descale (plus z1 (mul tmp13 +FIX-0-765366865+)) +shift-1+))
(setf (aref dptr 6) (descale (plus z1 (mul tmp12 (- +FIX-1-847759065+))) +shift-1+))
(setf z1 (plus tmp4 tmp7))
(setf z2 (plus tmp5 tmp6))
(setf z3 (plus tmp4 tmp6))
(setf z4 (plus tmp5 tmp7))
(setf z5 (mul (plus z3 z4) +FIX-1-175875602+))
(setf tmp4 (mul tmp4 +fix-0-298631336+))
(setf tmp5 (mul tmp5 +fix-2-053119869+))
(setf tmp6 (mul tmp6 +fix-3-072711026+))
(setf tmp7 (mul tmp7 +fix-1-501321110+))
(setf z1 (mul z1 (- +fix-0-899976223+)))
(setf z2 (mul z2 (- +fix-2-562915447+)))
(setf z3 (mul z3 (- +fix-1-961570560+)))
(setf z4 (mul z4 (- +fix-0-390180644+)))
(incf z3 z5)
(incf z4 z5)
(setf (aref dptr 7) (descale (plus3 tmp4 z1 z3) +shift-1+))
(setf (aref dptr 5) (descale (plus3 tmp5 z2 z4) +shift-1+))
(setf (aref dptr 3) (descale (plus3 tmp6 z2 z3) +shift-1+))
(setf (aref dptr 1) (descale (plus3 tmp7 z1 z4) +shift-1+)))
(loop for cnt fixnum from 7 downto 0 do ; second pass: on columns
(setf tmp0 (plus (s16ref data cnt 0) (s16ref data cnt 7)))
(setf tmp7 (minus (s16ref data cnt 0) (s16ref data cnt 7)))
(setf tmp1 (plus (s16ref data cnt 1) (s16ref data cnt 6)))
(setf tmp6 (minus (s16ref data cnt 1) (s16ref data cnt 6)))
(setf tmp2 (plus (s16ref data cnt 2) (s16ref data cnt 5)))
(setf tmp5 (minus (s16ref data cnt 2) (s16ref data cnt 5)))
(setf tmp3 (plus (s16ref data cnt 3) (s16ref data cnt 4)))
(setf tmp4 (minus (s16ref data cnt 3) (s16ref data cnt 4)))
(setf tmp10 (plus tmp0 tmp3))
(setf tmp13 (minus tmp0 tmp3))
(setf tmp11 (plus tmp1 tmp2))
(setf tmp12 (minus tmp1 tmp2))
(setf (s16ref data cnt 0) (descale (plus tmp10 tmp11) 1))
(setf (s16ref data cnt 4) (descale (minus tmp10 tmp11) 1))
(setf z1 (mul (plus tmp12 tmp13) +fix-0-541196100+))
(setf (s16ref data cnt 2) (descale (plus z1 (mul tmp13 +fix-0-765366865+)) +shift+1+))
(setf (s16ref data cnt 6) (descale (plus z1 (mul tmp12 (- +fix-1-847759065+))) +shift+1+))
(setf z1 (plus tmp4 tmp7))
(setf z2 (plus tmp5 tmp6))
(setf z3 (plus tmp4 tmp6))
(setf z4 (plus tmp5 tmp7))
(setf z5 (mul (plus z3 z4) +fix-1-175875602+))
(setf tmp4 (mul tmp4 +fix-0-298631336+))
(setf tmp5 (mul tmp5 +fix-2-053119869+))
(setf tmp6 (mul tmp6 +fix-3-072711026+))
(setf tmp7 (mul tmp7 +fix-1-501321110+))
(setf z1 (mul z1 (- +fix-0-899976223+)))
(setf z2 (mul z2 (- +fix-2-562915447+)))
(setf z3 (mul z3 (- +fix-1-961570560+)))
(setf z4 (mul z4 (- +fix-0-390180644+)))
(incf z3 z5)
(incf z4 z5)
(setf (s16ref data cnt 7) (descale (plus3 tmp4 z1 z3) +shift+1+))
(setf (s16ref data cnt 5) (descale (plus3 tmp5 z2 z4) +shift+1+))
(setf (s16ref data cnt 3) (descale (plus3 tmp6 z2 z3) +shift+1+))
(setf (s16ref data cnt 1) (descale (plus3 tmp7 z1 z4) +shift+1+)))
(return)))
;;; Forward DCT and quantization
(defun crunch (buf pos table)
(declare #.*optimize*
(type fixnum pos)
(type (simple-array sint16-2d-array (*)) buf))
(llm-dct (aref buf pos))
(quantize (aref buf pos) table))
;;; Q-tables scaling
(defun q-scale (table q-factor)
(declare #.*optimize*
(type uint8-2d-array table)
(type uint8 q-factor))
(when (/= q-factor 64)
(let ((factor (the uint8 (/ q-factor 64))))
(loop for q-row of-type uint8-array across table do
(loop for x fixnum from 0 to 7 do
(setf (aref q-row x)
(the fixnum (round (* (aref q-row x) factor)))))))))
;;; Function that maps value into SSSS
(defun csize (n)
(declare #.*optimize* (type fixnum n))
(aref +csize+ (plus n 1023)))
;;; zigzag ordering
(defun zigzag (buffer zz-result)
(declare #.*optimize*
(type sint16-2d-array buffer))
(loop for row of-type sint16-array across buffer
for z-row of-type uint8-array across +zigzag-index+ do
(loop for x fixnum from 0 to 7 do
(setf (aref zz-result (aref z-row x))
(the sint16 (aref row x)))))
zz-result)
(defun zigzag8 (buffer zz-result)
(declare #.*optimize*
(type uint8-2d-array buffer)
(type uint8-array zz-result))
(loop for row of-type uint8-array across buffer
for z-row of-type uint8-array across +zigzag-index+ do
(loop for x fixnum from 0 to 7 do
(setf (aref zz-result (aref z-row x))
(the fixnum (aref row x)))))
zz-result)
;;; Writes frame header
(defun write-frame-header (maxX maxY cn q-tables sampling tqv out-stream)
(declare #.*optimize* (type fixnum maxX maxY cn)
(type (simple-array uint8-2d-array (*)) q-tables)
;;(type uint8-array tqv)
)
(write-huffman-tables out-stream)
(write-quantization-tables q-tables out-stream)
;; writing frame header
(write-marker +M_SOF0+ out-stream)
(write-byte 0 out-stream) ; length
(write-byte (plus 8 (mul 3 cn)) out-stream)
(write-byte 8 out-stream) ; sample precision
(write-byte (ash maxY -8) out-stream) ; max height
(write-byte (logand maxY #xff) out-stream)
(write-byte (ash maxX -8) out-stream) ; max width
(write-byte (logand maxX #xff) out-stream)
(write-byte cn out-stream) ; number of components
(loop for entry in sampling
for i fixnum from 0 by 1 do
(write-byte i out-stream)
(write-byte ; H and V
(deposit-field (second entry) (byte 4 0)(ash (first entry) 4))
out-stream)
(write-byte (aref tqv i) out-stream))) ; Tq
;;; Writes byte with stuffing (adds zero after FF code)
(defun write-stuffed (b s)
(declare #.*optimize* (type uint8 b)
(type stream s))
(write-byte b s)
(if (= b #xFF)
(write-byte 0 s)))
;;; A function for bit streaming
;;; NB: probably it's a good idea to encapsulate this behavior into a class, but I'm
;;; afraid that method dispatch would be too slow
(defstruct (write-bits-state (:conc-name write-bits-))
(prev-byte 0 :type uint8)
(prev-length 0 :type fixnum))
(defun write-bits (bi ni s write-bits-state)
(declare #.*optimize*
(type fixnum bi ni)
(type stream s))
(loop with lim fixnum = (if (> ni 8) 1 0)
for i fixnum from lim downto 0 do
(let ((b (ldb (byte 8 (ash i 3)) bi))
(n (cond ((and (= i 1) (= ni 16)) 8)
((and (= i 0) (/= lim 0)) 8)
((= ni 8) 8)
(t (logand ni 7)))))
(declare (type fixnum b n)
(dynamic-extent b n))
(cond ((zerop n))
((>= (plus n (write-bits-prev-length write-bits-state)) 8)
(let* ((result (ash (write-bits-prev-byte write-bits-state)
(minus 8 (write-bits-prev-length write-bits-state))))
(total-length (plus n (write-bits-prev-length write-bits-state)))
(overflow (minus total-length 8)))
(declare (type fixnum overflow total-length result)
(dynamic-extent overflow total-length result))
(setf (write-bits-prev-byte write-bits-state) (ldb (byte overflow 0) b))
(write-stuffed (deposit-field
(ldb (byte (minus n overflow) overflow) b)
(byte (minus 8 (write-bits-prev-length write-bits-state)) 0)
result)
s)
(setf (write-bits-prev-length write-bits-state) overflow)))
(t (setf (write-bits-prev-byte write-bits-state)
(deposit-field b
(byte n 0)
(ash (write-bits-prev-byte write-bits-state) n)))
(incf (write-bits-prev-length write-bits-state) n))))))
;;; 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 write-bits-state)
(declare #.*optimize* (type fixnum pred)
(type sint16-array block))
(let* ((ehufsi-dc (first (first tables)))
(ehufco-dc (second (first tables)))
(ehufsi-ac (first (second tables)))
(ehufco-ac (second (second tables)))
(newpred (aref block 0))
(diff (minus newpred pred))
(dcpos (csize diff)))
(declare (type fixnum pred newpred diff dcpos)
(type fixnum-array ehufco-ac ehufco-dc ehufsi-dc ehufsi-ac)
(dynamic-extent diff dcpos))
;; writing dc code first
(write-bits (aref ehufco-dc dcpos) (aref ehufsi-dc dcpos) s write-bits-state)
(cond ((minusp diff) (write-bits (1- diff) (csize diff) s write-bits-state))
(t (write-bits diff (csize diff) s write-bits-state)))
;; writing ac sequence
(loop with r fixnum = 0 for k fixnum from 1 to 63 do
(if (zerop (aref block k))
(if (= k 63)
(progn
(write-bits (aref ehufco-ac 0) (aref ehufsi-ac 0) s write-bits-state) ; writing EOB
(return))
(incf r))
(progn
(loop while (> r 15) do
(write-bits (aref ehufco-ac #xf0) (aref ehufsi-ac #xf0) s write-bits-state)
(decf r 16))
(let* ((ssss (csize (aref block k)))
(rs (plus ssss (ash r 4))))
(write-bits (aref ehufco-ac rs) (aref ehufsi-ac rs) s write-bits-state)
(when (minusp (aref block k))
(decf (aref block k) 1))
(write-bits (aref block k) ssss s write-bits-state))
(setf r 0))))
newpred))
;;; Emits q-tables
(defun write-quantization-tables (tables s)
(let ((len (plus 2 (mul 65 (length tables))))
(zz-result (make-array 64 :element-type 'uint8)))
(write-marker +M_DQT+ s)
(write-byte (ash len -8) s) ; MSB
(write-byte (logand len #xff) s) ; LSB
(loop for table across tables
for i fixnum from 0 do
(write-byte i s)
(write-sequence (zigzag8 table zz-result) s))))
;;; Emits huffman tables in the following order:
;;; luminance DC
;;; luminance AC
;;; chrominance DC
;;; chrominance AC
(defun write-huffman-tables (s)
(let ((len (+ 2 (* 17 4)
(length +luminance-dc-values+)
(length +luminance-ac-values+)
(length +chrominance-dc-values+)
(length +chrominance-ac-values+))))
(write-marker +M_DHT+ s)
(write-byte (ash len -8) s) ; MSB
(write-byte (logand len #xff) s) ; LSB
(write-hufftable +luminance-dc-bits+ +luminance-dc-values+ 0 s)
(write-hufftable +luminance-ac-bits+ +luminance-ac-values+ 16 s)
(write-hufftable +chrominance-dc-bits+ +chrominance-dc-values+ 1 s)
(write-hufftable +chrominance-ac-bits+ +chrominance-ac-values+ 17 s)))
;;; Writes single huffman table
(defun write-hufftable (bits vals tcti s)
(declare (type fixnum tcti))
(write-byte tcti s) ; Tc/Th
(write-sequence bits s)
(write-sequence vals s))
;;; Drops specified marker into the stream
(defun write-marker (code to)