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

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

  1. Fabio

    Original Member

    Joined:
    Sep 30, 2005
    Messages:
    499
    Likes Received:
    0
    Hello!
    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

    Joined:
    Nov 23, 2004
    Messages:
    859
    Likes Received:
    0
    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,
    Emmanuel
     
  3. mDEV

    Original Member

    Joined:
    Sep 6, 2005
    Messages:
    44
    Likes Received:
    0
    to load JPG (to A8R8G8B8 pixel format) :

    Code:
    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(&jerr.pub);
    	jerr.pub.error_exit = my_error_exit;
    
    	if (setjmp(jerr.setjmp_buffer)) {
    		jpeg_destroy_decompress(&cinfo);
    		fclose(pFile);
    		return FALSE;
    	}
    
    	jpeg_create_decompress(&cinfo);
    	jpeg_stdio_src(&cinfo, pFile);
    	jpeg_read_header(&cinfo, TRUE);
    	jpeg_start_decompress(&cinfo);
    
    	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);
    		}
    	}
    	
    	jpeg_finish_decompress(&cinfo);
    	jpeg_destroy_decompress(&cinfo);
    
    	fclose(pFile);
            return TRUE;
    }
    
     
  4. PeterM

    Original Member

    Joined:
    Aug 5, 2004
    Messages:
    343
    Likes Received:
    0
    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,
    Pete
     

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