TinyJpeg
TinyJpeg are two classes for jpeg encoder and decoder. Aim was to write decoder with minimal code size which was
to a certain extent acheived (decoder can be compiled to approx. 3kB of code) Special features - lossless jpeg
compression, which was even further extended allowing for better compression ratios. There is also nice huffman coder
template and couple of DCT classes (one is enhanced with MMX, but the asm is not very nice)
status: made part of ÜberLame
language: C++
os: win32 / linux (msvc / g++)
version history
version | changes from previous version |
v0.50 | written jpeg decoder |
v0.60 | added jpeg encoder |
v0.70 | added MMX IDCT support |
v0.80 | rewritten huffman coder templates so it now compile under linux |
v0.90 | added lossless jpeg support |
v1.00 | final code refinements, added config section |
readme
Motivation
- wanted fast & tiny minimal jpeg decoder (baseline gray / RGB) for tiny programs like games / intros
- wanted to see how jpeg lossless compression performs (in the end it's not so much worse than png
on photographs, it can be even better if you reduce sample precission a bit. it doesn't do very well
with line drawing, etc. (jpeg was meant for continous tone images) which is fixed by additional
(non std! - i'm not keen on broadening wild formats, but i found no program to support lossless mode
jpegs so i favor better compression ratio) RLE compression)
- wanted jpeg encoder that would handle more formats and lossless compression (not meant to be part of
product distribution, rather part of tools used to create distribution datafiles)
Why use it (features)
- it's written in C++
- it's tiny (decoder is slightly over 3kB, encoder is under 7kB; for comparison - jpeglib is about 30kB)
- code is quite clean (if you don't look at frequent #ifdef's) and short (2x ~2000 lines with comments),
there is option to print verbose information on encoding / decoding process (educational purposes or
jpeg files analysis)
- it's cross-platform - it can be compiled under MSVC/windows and g++/linux (and certainly under more
OSes and compillers; contains msvc project and a simple makefile for encoder and decoder demo app)
- decoder is considerably fast and can decode grayscale, RG, RGB, YCCK and CMYK images in lossy mode
(it can decode any number of channels, but i wrote converting functions for just those four ones)
(in lossless mode any colorspace conversion is useless, it can simply decode up to four-channel images)
- encoder can encode in grayscale, RG, RGB and CMYK / YCCK modes (source is always RGB, but it can be
easily changed)
- encoder uses adaptive huffman coding as default (which means smaller resulting images)
- support for lossless jpegs and non-standard extended lossles jpegs (something jpeglib doesn't have)
- preparing support for runtime sample-width selection (8 / 12 for lossy, 8 - 12 for lossless; jpeglib
neither has this feature)
- it's not widely used and it's not gdi++ which means there's very low propability of viri attack via
data execution
- it comes with nice TGA reader class and multi-platform timer class (not to mention nice & fast
1D and 2D DCT classes)
- it's completely for free (completely means free of any warranty of usability, harmlessness
or purpose accomplishment)
Why don't use it (limitations)
- it's written by me
- encoder is a bit slow
- doesn't handle progressive images (and i don't plan to support them, this lib is supposed to stay tiny,
not to mention i don't need progressive images for my purposes at all)
- it doesn't have as rich interface as jpeglib (hey, it's tiny!)
- it's not tiny at all (right, it's small, but it could have been even smaller if i didn't use templates,
operator overloading and inheritance ... if i sticked to C-like C++, i could sure have saved a few kB)
Todo
- better image data structure, something like:
struct
TImage {
int
n_width, n_height;
int
n_sample_bit_num; int
n_pixel_align; enum
{
color_R, color_Gray, color_RG,
color_RGB,
color_RGBA,
color_CMYK } n_format;
unsigned
char
*p_buffer;
};
- with such a format, it's possible to call OpenGL TexImage* or DrawPixels without wasting memory by
expanding data with small bpp to 8 bpp and without loss of precission by clipping data with large
bpp to 8 bpp.
there will be function to explicitly select desired data format and color conversion in decoder,
in encoder it will be possible to select target colorspace and sample precission
- maybe a few MMX/SSE optimizations
downloads