-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdct.rkt
230 lines (217 loc) · 9.28 KB
/
dct.rkt
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
#lang racket
;; guile-jpeg
;; Copyright (C) 2014 Andy Wingo <wingo at pobox dot com>
;; This library is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3 of the License, or (at
;; your option) any later version.
;;
;; This library is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this library; if not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Forward and inverse JPEG discrete cosine transforms.
;;
;;; Code:
(require math/array jpeg/jfif jpeg/pixbufs rnrs/bytevectors-6)
(provide jpeg->planar-image planar-image->jpeg)
(define fdct-coefficients
(let ((pi (* 2 (acos 0))))
(build-array
#(8 8)
(match-lambda
((vector u v)
;; FIXME: Produce literal f32vector here.
(for/vector ((k (in-range (* 8 8))))
(call-with-values (lambda ()
(values (quotient k 8) (remainder k 8)))
(lambda (i j)
(let ((Cu (if (zero? u) (/ 1 (sqrt 2)) 1))
(Cv (if (zero? v) (/ 1 (sqrt 2)) 1)))
(* 1/4 Cu Cv
(cos (/ (* (+ (* 2 i) 1) u pi) 16))
(cos (/ (* (+ (* 2 j) 1) v pi) 16))))))))))))
(define idct-coefficients
(let ((pi (* 2 (acos 0))))
(build-array
#(8 8)
(match-lambda
((vector i j)
(for/vector ((k (in-range (* 8 8))))
(call-with-values (lambda ()
(values (quotient k 8) (remainder k 8)))
(lambda (u v)
(let ((Cu (if (zero? u) (/ 1 (sqrt 2)) 1))
(Cv (if (zero? v) (/ 1 (sqrt 2)) 1)))
(* 1/4 Cu Cv
(cos (/ (* (+ (* 2 i) 1) u pi) 16))
(cos (/ (* (+ (* 2 j) 1) v pi) 16))))))))))))
(define (idct-block block plane pos stride)
(define (idct i j)
(let ((coeffs (array-ref idct-coefficients (vector i j))))
(let lp ((k 0) (sum 0.0))
(if (< k 64)
(let ((Suv (vector-ref block k)))
(lp (add1 k)
(if (zero? Suv)
sum
(+ sum (* (vector-ref coeffs k) Suv)))))
sum))))
(let lp ((i 0) (pos pos))
(when (< i 8)
(let lp ((j 0))
(when (< j 8)
(let* ((s (idct i j))
(sq (cond
((< s -128.0) 0)
((> s 127.0) 255)
(else (+ 128 (inexact->exact (round s)))))))
(bytevector-u8-set! plane (+ pos j) sq))
(lp (add1 j))))
(lp (add1 i) (+ pos stride)))))
(define (jpeg->planar-image jpeg)
(match jpeg
((jfif frame misc-segments mcu-array)
(let ((mcu-width (frame-mcu-width frame))
(mcu-height (frame-mcu-height frame)))
(planar-image
(frame-x frame)
(frame-y frame)
(* mcu-width (frame-samp-x frame) 8)
(* mcu-height (frame-samp-y frame) 8)
(for/vector ((k (in-naturals))
(component (in-vector (frame-components frame))))
(let* ((samp-x (component-samp-x component))
(samp-y (component-samp-y component))
(block-width (* mcu-width samp-x))
(block-height (* mcu-height samp-y))
(sample-width (* block-width 8))
(sample-height (* block-height 8))
(p (make-bytevector (* sample-width sample-height) 0)))
(for ((mcu-idx (in-naturals)) (mcu (in-array mcu-array)))
(let* ((i (quotient mcu-idx mcu-width))
(j (remainder mcu-idx mcu-width))
(mcu-y (* i samp-y 8))
(mcu-x (* j samp-x 8))
(offset (+ (* mcu-y sample-width) mcu-x)))
(for ((block-idx (in-naturals))
(block (in-array (vector-ref mcu k))))
(let* ((block-i (quotient block-idx samp-x))
(block-j (remainder block-idx samp-y))
(offset (+ offset (* block-i 8 sample-width)
(* block-j 8))))
(idct-block block p offset sample-width)))))
(plane sample-width sample-height p))))))))
;; Tables K.1 and K.2 from the JPEG specification.
(define *standard-luminance-q-table*
#(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 *standard-chrominance-q-table*
#(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))
;; As libjpeg does, we consider the above tables to be quality 50, on a
;; scale from 1 (terrible) to 100 (great). We linearly scale the values
;; so that at quality 100, all values are 1, and at quality 1 all values
;; are 255.
(define (q-tables-for-quality quality)
;; This mapping of quality to a linear scale is also from libjpeg.
(let* ((quality (exact->inexact quality)) ;; allow divide by zero -> inf
(linear-scale (if (< quality 50)
(/ 50. quality)
(- 1 (/ (- quality 50) 50)))))
(define (scale x)
(let ((x (* x linear-scale)))
(cond
((< x 1) 1)
((> x 255) 255)
(else (inexact->exact (round x))))))
(vector (vector-map scale *standard-luminance-q-table*)
(vector-map scale *standard-chrominance-q-table*)
#f
#f)))
(define (fdct-block plane pos stride q-table)
(define (fdct v u)
(let ((coeffs (array-ref fdct-coefficients (vector v u))))
(let lp ((i 0) (pos pos) (sum 0.0))
(if (< i 8)
(lp (add1 i)
(+ pos stride)
(let lp ((j 0) (k (* i 8)) (pos pos) (sum sum))
(if (< j 8)
(let ((coeff (vector-ref coeffs k))
(sample (- (bytevector-u8-ref plane pos) 128)))
(lp (add1 j)
(add1 k)
(add1 pos)
(+ sum (* coeff sample))))
sum)))
sum))))
(for/vector ((k (in-range (* 8 8))))
(let ((v (arithmetic-shift k -3))
(u (bitwise-and k 7))
(q (vector-ref q-table k)))
(let ((Svu (fdct v u)))
(* q (inexact->exact (round (/ Svu q))))))))
(define (planar-image->jpeg yuv
#:quality (quality 85)
#:q-tables (q-tables (q-tables-for-quality quality))
;; In JFIF baseline JPEG images, component
;; 0 is Y', and components 1 and 2 are Cb
;; and Cr. Assign the first quantization
;; table for luminance, and the second for
;; chrominance.
#:plane-q-table (plane-q-table (lambda (i) (if (zero? i) 0 1))))
(match yuv
((planar-image width height canvas-width canvas-height planes)
(let ((samp-x (for/fold ((samp-x 1)) ((plane (in-vector planes)))
(lcm samp-x (/ canvas-width (plane-width plane)))))
(samp-y (for/fold ((samp-x 1)) ((plane (in-vector planes)))
(lcm samp-x (/ canvas-height (plane-height plane))))))
(define (plane-samp-x plane)
(* samp-x (/ (plane-width plane) canvas-width)))
(define (plane-samp-y plane)
(* samp-y (/ (plane-height plane) canvas-height)))
(let ((components
(for/vector ((plane (in-vector planes))
(i (in-naturals)))
(component i i
(plane-samp-x plane) (plane-samp-y plane)
(plane-q-table i)))))
(jfif
(frame #f 8 height width components samp-x samp-y)
'()
(build-array
(vector (/ canvas-height 8 samp-y) (/ canvas-width 8 samp-x))
(match-lambda
((vector i j)
(for/vector ((component (in-vector components)))
(match (vector-ref planes (component-index component))
((plane plane-width plane-height samples)
(let ((samp-y (component-samp-y component))
(samp-x (component-samp-x component)))
(build-array
(vector samp-y samp-x)
(match-lambda
((vector y x)
(let* ((pos (+ (* (+ (* i samp-y) y) 8 plane-width)
(* (+ (* j samp-x) x) 8)))
(q-table-index (component-q-table component))
(q-table (vector-ref q-tables q-table-index)))
(fdct-block samples pos plane-width q-table))))))))))))))))))