Simple C/C++ source for a JPEG loader anyone?

Discussion in 'Game Development (Technical)' started by Fabio, Oct 21, 2005.

  1. Fabio

    Original Member

    Sep 30, 2005
    Likes Received:
    does anybody know where can I find a C/C++ source for a JPEG (not 2000) simple image loader (maybe even better also saver, but this is not too important)?

    Yes, I know the official site and the source they provide, but it's just that when I see 100 source files without any visible hierarchy I start getting headaches, and I think that "fixing" it to integrate it with my sources would take more time than writing by myself the next compression technique to revolution the world. :rolleyes:
    Also, I could compile it to a lib, but I don't want neither this..

    What I'd ideally like is a single (or few) source with a function to which you simply pass JPEG data and a pointer, and it decodes the image. I'd prefer to feed it data via a callback, since I want to handle files by myself, but I can modify the source to feature this anyway.

    A simple function sounds far too simple for typical humans.. we normally like to complicate things a lot! ;)

    Any luck?
  2. Emmanuel

    Moderator Original Member

    Nov 23, 2004
    Likes Received:
    Check out CxImage, it loads everything under the sun, from files or your own resource packs, and has a nice and easy C++ interface.

    Best regards,
  3. mDEV

    Original Member

    Sep 6, 2005
    Likes Received:
    to load JPG (to A8R8G8B8 pixel format) :

    struct image
        long width;
        long height;
        void* pImgData;
    BOOL LoadJPG( const char* pFilename, image& img )
    	FILE *pFile=NULL;
    	if ((pFile = fopen(pFilename, "rb")) == NULL)
    		return FALSE;
    	struct jpeg_decompress_struct cinfo;
    	struct my_error_mgr jerr;
    	cinfo.err = jpeg_std_error(&; = my_error_exit;
    	if (setjmp(jerr.setjmp_buffer)) {
    		return FALSE;
    	jpeg_stdio_src(&cinfo, pFile);
    	jpeg_read_header(&cinfo, TRUE);
    	int stride = cinfo.output_width * cinfo.output_components;
    	unsigned char** buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, stride, 1);
            img.width = cinfo.output_width;
            img.height = cinfo.output_height;
    	img.pImgData = (void*)new unsigned long[cinfo.output_width*cinfo.output_height];
    	unsigned long* pixPtr = (unsigned long*)img.pImgData;
            const int a = 255;
    	while (cinfo.output_scanline < cinfo.output_height) {
    		jpeg_read_scanlines(&cinfo, buffer, 1);
    		unsigned char* pcomp = *buffer;
    		for (uint i = 0; i < cinfo.output_width; i++) {
    			int r = *pcomp++;
    			int g = *pcomp++;
    			int b = *pcomp++;
    			*pixPtr ++ = (a<<24) | (r << 16) | (g << 8) | (b);
            return TRUE;
  4. PeterM

    Original Member

    Aug 5, 2004
    Likes Received:
    I think the JPEG format is too complex to have a single source file with the entire loading code contained within. Unless you really want a 100K+ source file.

    It doesn't actually take that long to integrate the official JPEG source.

    Basically, add all the j*.h and j*.c files to your project, leaving out jmem*.c apart from jmemmgr.c and jmemnobs.c.

    That should work, if not, it'll be easy to find the problem. It's all about adding the right files. All the j*.* files are library files. The others are sources for utilities to compress and decompress various image formats.

    I'm pretty sure it's all in the "install" or "readme" docs.

    Something to watch out for, is that if you ever want to compile a jpeg.dll, that the FILE* pointers you'd normall pass to jpeglib cannot cross a DLL boundary. This means you have to provide your own loading functions, which are wrappers for FILE*s if required.

    The more advanced your code gets, you'll perhaps want to support loading from memory buffers or network streams, so you'll have to do this anyway.

    Hope this helps,

Share This Page

  • About Indie Gamer

    When the original Dexterity Forums closed in 2004, Indie Gamer was born and a diverse community has grown out of a passion for creating great games. Here you will find over 10 years of in-depth discussion on game design, the business of game development, and marketing/sales. Indie Gamer also provides a friendly place to meet up with other Developers, Artists, Composers and Writers.
  • Buy us a beer!

    Indie Gamer is delicately held together by a single poor bastard who thankfully gets help from various community volunteers. If you frequent this site or have found value in something you've learned here, help keep the site running by donating a few dollars (for beer of course)!

    Sure, I'll Buy You a Beer