This is a library I wrote many years ago for decoding popular image files. This library can decode some of the most popular image formats:
Here’s the zip file containing the source code:
Ok… so, what’s the big deal? Why not just use an opensource library and be done with it?
While that may be the most appropriate solution in a lot of cases, especially in large unix (posix) systems, it may be quite difficult to do so on smaller embedded systems where porting existing code may be difficult.
This is where this library can come in handy. There are no dependencies required to port this code. The code is straight forward and simple. While there is a function that is used to read the image from a file, this can be easily modified to read the image from a buffer or rom.
The library is not meant to be high performance. It’s not the best or prettiest code. Honestly it’s not even that efficient for what it does. It’s goal is simplicity and portability.
It doesn’t use any specialized processor functions, architechture specific assembly, SIMD instructions or have any specific hardware requirements. It will run on the simplest CPUs where the only limits are the ram or the rom size required to store the image data and the decoded rgb buffer. It will work on little-endian and big-endian hardware.
I have used this library on at least a dozen different architectures and as many OSes, and had no issues porting it. On some of these platforms, porting the existing open source image codec library like libjpeg and their dependencies may not be possible or can take days of effort, meanwhile this one should compile out of the box.
If it doesn’t compile, it’s because it’s looking for file operations, and you’re on something that doesn’t use files. Replace the file open and read operations to pull data out of a buffer, and you should be good to go.
To use the library, compile the code, and use the following functions to decode the image of your choice:
int readBMP(char *filename, unsigned char **buffer, int *width, int *height, int *components); int readGIF(char *filename, unsigned char **buffer, int *width, int *height, int *components); int readJPG(char *filename, unsigned char **buffer, int *width, int *height, int *components); int readPCX(char *filename, unsigned char **buffer, int *width, int *height, int *components); int readTGA(char *filename, unsigned char **buffer, int *width, int *height, int *components);
If you want to read from a buffer, replace the openxxxfile() function with the following:
xxxbuffer = encoded_buffer; xxxcurbyte = encoded_buffer; xxxlength = encoded_buffer_size; xxxmaxbyte = encoded_buffer + encoded_buffer_size;
where xxx is the image type to decode (tga, pcx, bmp, jpg, gif).
The code here is free for any purpose, but is provided without any warranty or support. I will not respond to bugs reports, requests for fixes or enhancement. Misuse of this code is also your responsibility.
That said, I hope you find this helpful and useful.