A single-file pure Tcl baseline JPEG decoder library. Works in Tcl 8.5, Tcl 8.6, Tcl 9, and Jim Tcl 0.75 and later.
- JPEG/JFIF and JPEG/Exif
- Huffman coding
- YCbCy (standard three-channel color) and grayscale
- 8-bit color channels
- Chroma subsampling (horizontal and vertical 4:2:2 subsampling and 4:2:0 subsampling has been tested)
- Restart markers
- Progressive and lossless encoding
- Arithmetic coding
- CMYK color
- 12-bit color channels
To learn how to write a JPEG decoder.
The decoder is too slow to replace JPEG decoders for Tcl written in C for anything but the smallest images. However, it can be used as a benchmark to compare different Tcl implementations and different versions of the same implementation. (See below.) The code may help you understand JPEG compression. It is small (around 850 lines), written in a functional style (the decoder consists of pure functions insofar as they exist in Tcl), and stores data in easy-to-inspect immutable data structures.
(If you want to understand how a JPEG decoder works, based on my experience I recommend reading the Wikipedia article on JPEG and following the JPEG Huffman coding tutorial by Calvin Hass before anything else. If you don't know Tcl and want to read the code, Learn Tcl in Y Minutes should teach you enough to get started. Tcl is pretty much a collection of independent commands; once you know the syntax, you can look them up as you go.)
The script benchmark.tcl
can evaluate the performance of the decoder by timing how long it takes to decode several test images included in the repository.
Here are the results gathered on an AMD Phenom II CPU.
Running in Tcl 8.5.19 (64-bit) on x86_64 Linux
5 iterations per image
==========================
grayscale.jpg 10 ms
landscape.jpg 19548 ms
landscape-q.jpg 11159 ms
63.6 MB
Running in Tcl 8.6.5 (64-bit) on x86_64 Linux
5 iterations per image
==========================
grayscale.jpg 10 ms
landscape.jpg 18758 ms
landscape-q.jpg 10587 ms
75.3 MB
Running in Jim 0.75 (64-bit) on x86_64 linux
5 iterations per image
==========================
grayscale.jpg 19 ms
landscape.jpg 38508 ms
landscape-q.jpg 22832 ms
88.8 MB
MIT.
The test-data/landscape*
photo by m wrona, licensed under CC0.