PTJD
A single-file pure Tcl baseline JPEG decoder library. Works in Tcl 8.5, Tcl 8.6 and Jim Tcl 0.75 and later.
Q & A
What is supported?
- 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
What isn't?
- Progressive and lossless encoding
- Arithmetic coding
- CMYK color
- 12-bit color channels
Why?
To learn how to write a JPEG decoder.
What is it good for?
The decoder is far too slow to replace C-based JPEG decoders for Tcl 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 (circa 700 lines), written in a functional style (the decoder consists of pure functions insofar as those 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 own experience I recommend reading the Wikipedia article on JPEG and following the JPEG Huffman Coding Tutorial before anything else. If you don't know Tcl, Learn Tcl in Y Minutes should teach you enough to start reading the decoder's code. Tcl is pretty much a collection of independent commands; once you know the syntax you can look them up as you go along.)
What is the performance like?
The script benchmark.tcl
can be used to evaluate the performance of the decoder by timing how long it takes to decode several test images included in this repository. Here are the sample results obtained by running the benchmark 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
License
MIT.
test-data/landscape*
photo by Maciej Wrona, licensed under CC0.