]> gitweb.factorcode.org Git - factor.git/blob - unmaintained/images/jpeg/jpeg.factor
use radix literals
[factor.git] / unmaintained / images / jpeg / jpeg.factor
1 ! Copyright (C) 2009 Marc Fauconneau.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays byte-arrays combinators
4 compression.huffman fry grouping images images.loader
5 images.processing io io.binary io.encodings.binary
6 io.streams.byte-array io.streams.limited io.streams.throwing
7 kernel locals math math.bitwise math.blas.matrices
8 math.blas.vectors math.constants math.functions math.matrices
9 math.order math.vectors memoize namespaces sequences
10 sequences.deep ;
11 QUALIFIED-WITH: bitstreams bs
12 IN: images.jpeg
13
14 SINGLETON: jpeg-image
15
16 TUPLE: loading-jpeg < image
17     { headers }
18     { bitstream }
19     { color-info initial: { f f f f } }
20     { quant-tables initial: { f f } }
21     { huff-tables initial: { f f f f } }
22     { components } ;
23
24 "jpg" jpeg-image register-image-class
25 "jpeg" jpeg-image register-image-class
26
27 <PRIVATE
28
29 : <loading-jpeg> ( headers bitstream -- image )
30     loading-jpeg new swap >>bitstream swap >>headers ;
31
32 SINGLETONS: SOF DHT DAC RST SOI EOI SOS DQT DNL DRI DHP EXP
33 APP JPG COM TEM RES ;
34
35 ! ISO/IEC 10918-1 Table B.1
36 :: >marker ( byte -- marker )
37     byte
38     {
39       { [ dup 0xCC = ] [ { DAC } ] }
40       { [ dup 0xC4 = ] [ { DHT } ] }
41       { [ dup 0xC9 = ] [ { JPG } ] }
42       { [ dup -4 shift 0xC = ] [ SOF byte 4 bits 2array ] }
43
44       { [ dup 0xD8 = ] [ { SOI } ] }
45       { [ dup 0xD9 = ] [ { EOI } ] }
46       { [ dup 0xDA = ] [ { SOS } ] }
47       { [ dup 0xDB = ] [ { DQT } ] }
48       { [ dup 0xDC = ] [ { DNL } ] }
49       { [ dup 0xDD = ] [ { DRI } ] }
50       { [ dup 0xDE = ] [ { DHP } ] }
51       { [ dup 0xDF = ] [ { EXP } ] }
52       { [ dup -4 shift 0xD = ] [ RST byte 4 bits 2array ] }
53
54       { [ dup -4 shift 0xE = ] [ APP byte 4 bits 2array ] }
55       { [ dup 0xFE = ] [ { COM } ] }
56       { [ dup -4 shift 0xF = ] [ JPG byte 4 bits 2array ] }
57
58       { [ dup 0x01 = ] [ { TEM } ] }
59       [ { RES } ]
60     }
61     cond nip ;
62
63 TUPLE: jpeg-chunk length type data ;
64
65 : <jpeg-chunk> ( type length data -- jpeg-chunk )
66     jpeg-chunk new
67         swap >>data
68         swap >>length
69         swap >>type ;
70
71 TUPLE: jpeg-color-info
72     h v quant-table dc-huff-table ac-huff-table { diff initial: 0 } id ;
73
74 : <jpeg-color-info> ( h v quant-table -- jpeg-color-info )
75     jpeg-color-info new
76         swap >>quant-table
77         swap >>v
78         swap >>h ;
79
80 : jpeg> ( -- jpeg-image ) jpeg-image get ;
81
82 : apply-diff ( dc color -- dc' )
83     [ diff>> + dup ] [ diff<< ] bi ;
84
85 : fetch-tables ( component -- )
86     [ [ jpeg> quant-tables>> nth ] change-quant-table drop ]
87     [ [ jpeg> huff-tables>> nth ] change-dc-huff-table drop ]
88     [ [ 2 + jpeg> huff-tables>> nth ] change-ac-huff-table drop ] tri ;
89
90 : read4/4 ( -- a b ) read1 16 /mod ;
91
92 ! headers
93
94 : decode-frame ( header -- )
95     data>>
96     binary
97     [
98         read1 8 assert=
99         2 read be>
100         2 read be>
101         swap 2array jpeg> dim<<
102         read1
103         [
104             read1 read4/4 read1 <jpeg-color-info>
105             swap [ >>id ] keep jpeg> color-info>> set-nth
106         ] times
107     ] with-byte-reader ;
108
109 : decode-quant-table ( chunk -- )
110     dup data>>
111     binary
112     [
113         length>>
114         2 - 65 /
115         [
116             read4/4 [ 0 assert= ] dip
117             64 read
118             swap jpeg> quant-tables>> set-nth
119         ] times
120     ] with-byte-reader ;
121
122 : decode-huff-table ( chunk -- )
123     data>> [ binary <byte-reader> ] [ length ] bi limit-stream [
124         [ input-stream get stream>> [ count>> ] [ limit>> ] bi < ]
125         [
126             read4/4 swap 2 * +
127             16 read
128             dup [ ] [ + ] map-reduce read
129             binary [ [ read [ B{ } ] unless* ] { } map-as ] with-byte-reader
130             swap jpeg> huff-tables>> set-nth
131         ] while
132     ] stream-throw-on-eof ;
133
134 : decode-scan ( chunk -- )
135     data>>
136     binary
137     [
138         read1 iota
139         [   drop
140             read1 jpeg> color-info>> nth clone
141             read1 16 /mod [ >>dc-huff-table ] [ >>ac-huff-table ] bi*
142         ] map jpeg> components<<
143         read1 0 assert=
144         read1 63 assert=
145         read1 16 /mod [ 0 assert= ] bi@
146     ] with-byte-reader ;
147
148 : singleton-first ( seq -- elt )
149     [ length 1 assert= ] [ first ] bi ;
150
151 ERROR: not-a-baseline-jpeg-image ;
152
153 : baseline-parse ( -- )
154     jpeg> headers>> [ type>> { SOF 0 } = ] any? [ not-a-baseline-jpeg-image ] unless
155     jpeg> headers>>
156     {
157         [ [ type>> { SOF 0 } = ] filter singleton-first decode-frame ]
158         [ [ type>> { DQT } = ] filter [ decode-quant-table ] each ]
159         [ [ type>> { DHT } = ] filter [ decode-huff-table ] each ]
160         [ [ type>> { SOS } = ] filter singleton-first decode-scan ]
161     } cleave ;
162
163 : parse-marker ( -- marker )
164     read1 0xFF assert=
165     read1 >marker ;
166
167 : parse-headers ( -- chunks )
168     [ parse-marker dup { SOS } = not ]
169     [
170         2 read be>
171         dup 2 - read <jpeg-chunk>
172     ] [ produce ] keep dip swap suffix ;
173
174 MEMO: zig-zag ( -- zz )
175     {
176         {  0  1  5  6 14 15 27 28 }
177         {  2  4  7 13 16 26 29 42 }
178         {  3  8 12 17 25 30 41 43 }
179         {  9 11 18 24 31 40 44 53 }
180         { 10 19 23 32 39 45 52 54 }
181         { 20 22 33 38 46 51 55 60 }
182         { 21 34 37 47 50 56 59 61 }
183         { 35 36 48 49 57 58 62 63 }
184     } flatten ;
185
186 MEMO: yuv>bgr-matrix ( -- m )
187     {
188         { 1  2.03211  0       }
189         { 1 -0.39465 -0.58060 }
190         { 1  0        1.13983 }
191     } ;
192
193 : wave ( x u -- n ) swap 2 * 1 + * pi * 16 / cos ;
194
195 :: dct-vect ( u v -- basis )
196     { 8 8 } coord-matrix [ { u v } [ wave ] 2map product ] map^2
197     1 u v [ 0 = [ 2 sqrt / ] when ] bi@ 4 / m*n ;
198
199 MEMO: dct-matrix ( -- m ) 64 iota [ 8 /mod dct-vect flatten ] map ;
200
201 : mb-dim ( component -- dim )  [ h>> ] [ v>> ] bi 2array ;
202
203 ! : blocks ( component -- seq )
204 !    mb-dim ! coord-matrix flip concat [ [ { 2 2 } v* ] [ v+ ] bi* ] with map ;
205
206 : all-macroblocks ( quot: ( mb -- ) -- )
207     [
208         jpeg>
209         [ dim>> 8 v/n ]
210         [ color-info>> sift { 0 0 } [ mb-dim vmax ] reduce v/ ] bi
211         [ ceiling ] map
212         coord-matrix flip concat
213     ]
214     [ each ] bi* ; inline
215
216 : reverse-zigzag ( b -- b' ) zig-zag swap [ nth ] curry map ;
217
218 : idct-factor ( b -- b' ) dct-matrix v.m ;
219
220 MEMO: dct-matrix-blas ( -- m ) dct-matrix >float-blas-matrix ;
221 : V.M ( x A -- x.A ) Mtranspose swap M.V ;
222 : idct-blas ( b -- b' ) >float-blas-vector dct-matrix-blas V.M ;
223
224 : idct ( b -- b' ) idct-factor ;
225
226 :: draw-block ( block x,y color-id jpeg-image -- )
227     block dup length>> sqrt >fixnum group flip
228     dup matrix-dim coord-matrix flip
229     [
230         [ '[ _ [ second ] [ first ] bi ] dip nth nth ]
231         [ x,y v+ color-id jpeg-image draw-color ] bi
232     ] with each^2 ;
233
234 : sign-extend ( bits v -- v' )
235     swap [ ] [ 1 - 2^ < ] 2bi
236     [ -1 swap shift 1 + + ] [ drop ] if ;
237
238 : read1-jpeg-dc ( decoder -- dc )
239     [ read1-huff dup ] [ bs>> bs:read ] bi sign-extend ;
240
241 : read1-jpeg-ac ( decoder -- run/ac )
242     [ read1-huff 16 /mod dup ] [ bs>> bs:read ] bi sign-extend 2array ;
243
244 :: decode-block ( color -- pixels )
245     color dc-huff-table>> read1-jpeg-dc color apply-diff
246     64 0 <array> :> coefs
247     0 coefs set-nth
248     0 :> k!
249     [
250         color ac-huff-table>> read1-jpeg-ac
251         [ first 1 + k + k! ] [ second k coefs set-nth ] [ ] tri
252         { 0 0 } = not
253         k 63 < and
254     ] loop
255     coefs color quant-table>> v*
256     reverse-zigzag idct ;
257     
258 :: draw-macroblock-yuv420 ( mb blocks -- )
259     mb { 16 16 } v* :> pos
260     0 blocks nth pos { 0 0 } v+ 0 jpeg> draw-block
261     1 blocks nth pos { 8 0 } v+ 0 jpeg> draw-block
262     2 blocks nth pos { 0 8 } v+ 0 jpeg> draw-block
263     3 blocks nth pos { 8 8 } v+ 0 jpeg> draw-block
264     4 blocks nth 8 group 2 matrix-zoom concat pos 1 jpeg> draw-block
265     5 blocks nth 8 group 2 matrix-zoom concat pos 2 jpeg> draw-block ;
266     
267 :: draw-macroblock-yuv444 ( mb blocks -- )
268     mb { 8 8 } v* :> pos
269     3 iota [ [ blocks nth pos ] [ jpeg> draw-block ] bi ] each ;
270
271 :: draw-macroblock-y ( mb blocks -- )
272     mb { 8 8 } v* :> pos
273     0 blocks nth pos 0 jpeg> draw-block
274     64 0 <array> pos 1 jpeg> draw-block
275     64 0 <array> pos 2 jpeg> draw-block ;
276  
277     ! %fixme: color hack
278  !   color h>> 2 =
279  !   [ 8 group 2 matrix-zoom concat ] unless
280  !   pos { 8 8 } v* color jpeg> draw-block ;
281
282 : decode-macroblock ( -- blocks )
283     jpeg> components>>
284     [
285         [ mb-dim first2 * ]
286         [ [ decode-block ] curry replicate ] bi
287     ] map concat ;
288
289 : cleanup-bitstream ( bytes -- bytes' )
290     binary [
291         [
292             { 0xFF } read-until
293             read1 [ 0x00 = and ] keep swap
294         ]
295         [ drop ] produce
296         swap >marker {  EOI } assert=
297         swap suffix
298         { 0xFF } join
299     ] with-byte-reader ;
300
301 : setup-bitmap ( image -- )
302     dup dim>> 16 v/n [ ceiling ] map 16 v*n >>dim
303     BGR >>component-order
304     ubyte-components >>component-type
305     f >>upside-down?
306     dup dim>> first2 * 3 * 0 <array> >>bitmap
307     drop ;
308
309 ERROR: unsupported-colorspace ;
310 SINGLETONS: YUV420 YUV444 Y MAGIC! ;
311
312 :: detect-colorspace ( jpeg-image -- csp )
313     jpeg-image color-info>> sift :> colors
314     MAGIC!
315     colors length 1 = [ drop Y ] when
316     colors length 3 =
317     [
318         colors [ mb-dim { 1 1 } = ] all?
319         [ drop YUV444 ] when
320
321         colors unclip
322         [ [ mb-dim { 1 1 } = ] all? ]
323         [ mb-dim { 2 2 } =  ] bi* and
324         [ drop YUV420 ] when
325     ] when ;
326     
327 ! this eats ~50% cpu time
328 : draw-macroblocks ( mbs -- )
329     jpeg> detect-colorspace
330     {
331         { YUV420 [ [ first2 draw-macroblock-yuv420 ] each ] }
332         { YUV444 [ [ first2 draw-macroblock-yuv444 ] each ] }
333         { Y      [ [ first2 draw-macroblock-y ] each ] }
334         [ unsupported-colorspace ]
335     } case ;
336
337 ! this eats ~25% cpu time
338 : color-transform ( yuv -- rgb )
339     { 128 0 0 } v+ yuv>bgr-matrix swap m.v
340     [ 0 max 255 min >fixnum ] map ;
341
342 : baseline-decompress ( -- )
343     jpeg> bitstream>> cleanup-bitstream { 255 255 255 255 } append
344     >byte-array bs:<msb0-bit-reader> jpeg> bitstream<<
345     jpeg> 
346     [ bitstream>> ] 
347     [ [ [ <huffman-decoder> ] with map ] change-huff-tables drop ] bi
348     jpeg> components>> [ fetch-tables ] each
349     [ decode-macroblock 2array ] collector 
350     [ all-macroblocks ] dip
351     jpeg> setup-bitmap draw-macroblocks 
352     jpeg> bitmap>> 3 <groups> [ color-transform ] map! drop
353     jpeg> [ >byte-array ] change-bitmap drop ;
354
355 ERROR: not-a-jpeg-image ;
356
357 : loading-jpeg>image ( loading-jpeg -- image )
358     dup jpeg-image [
359         baseline-parse
360         baseline-decompress
361     ] with-variable ;
362
363 : load-jpeg ( stream -- loading-jpeg )
364     [
365         parse-marker { SOI } = [ not-a-jpeg-image ] unless
366         parse-headers
367         contents <loading-jpeg>
368     ] with-input-stream ;
369
370 PRIVATE>
371
372 M: jpeg-image stream>image ( stream jpeg-image -- bitmap )
373     drop load-jpeg loading-jpeg>image ;