这里有一个很mini的Jpeg解码器:
http://www.h4ck3r.net/2009/12/02/mini-jpeg-decoder/
只有一个文件,只有不到700行代码。为什么能使用如此简单的代码解码,具体原理待以后研究。我把代码贴在后面,以便查阅:
作者原文转载如下:
Occasionally, I have the need to load some image data. Typically, the data can be relatively easily converted offline to raw RGB or .TGA using ImageMagick. By converting ahead of time to something simple, there's generally not much need to write $random_format_loader
.
But, for some stuff I've been doing recently, the data's coming over the network, or is embedded in a larger file, so it's cumbersome to modify it as a preprocess.
So, I'd like to be able to load JPEG, PNG, and DDS (which is basically a wrapper around DXTn).
Tackling JPEG first, I was a little frightened of what I might be getting into. Most places point to libjpeg which is quite a scary huge beast. I'm sure it's mostly huge and scary for legitimate reasons (encoding support, various obscure variants, optimizations, etc.) but it's not something I'm interested in having a dependency on to just load a picture.
A little hunting turned up Tiny Jpeg Decoder by Luc Saillard, and NanoJPEG by Martin Fiedler. I found NanoJPEG nicer, and it's a very pleasant ~900 lines of straight C. The resulting image from NanoJPEG looked a bit better too.
I crammed Martin's implementation into one C++ .h
file that doesn't require any supporting .cpp
, and also hacked it up to be to my liking.
My mashed up version doesn't use any global/static data, so I guess there's a small benefit there if you were using it in multiple threads, but I haven't actually exercised that.
Martin's original version supports some configuration #defines for controlling different methods of upsampling, and avoiding using use of stdlib, and so is probably better for most people. But, here's mine anyway in case someone finds it useful.