libjpeg problems linking in cpp

This is a discussion on libjpeg problems linking in cpp within the C++ Programming forums, part of the General Programming Boards category; I have a problem with linking a simple libjpeg program in cpp with devcpp as my compiler, this is my ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    58

    libjpeg problems linking in cpp

    I have a problem with linking a simple libjpeg program in cpp with devcpp as my compiler, this is my code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    extern "C" {
    #include <jpeglib.h>
    }
    
    /* we will be using this uninitialized pointer later to store raw, uncompressd image */
    unsigned char *raw_image = NULL;
    
    /* dimensions of the image we want to write */
    int width = 1600;
    int height = 1200;
    int bytes_per_pixel = 3;   /* or 1 for GRACYSCALE images */
    int color_space = JCS_RGB; /* or JCS_GRAYSCALE for grayscale images */
    
    /**
     * read_jpeg_file Reads from a jpeg file on disk specified by filename and saves into the 
     * raw_image buffer in an uncompressed format.
     * 
     * \returns positive integer if successful, -1 otherwise
     * \param *filename char string specifying the file name to read from
     *
     */
    
    int read_jpeg_file( char *filename )
    {
    	/* these are standard libjpeg structures for reading(decompression) */
    	struct jpeg_decompress_struct cinfo;
    	struct jpeg_error_mgr jerr;
    	/* libjpeg data structure for storing one row, that is, scanline of an image */
    	JSAMPROW row_pointer[1];
    	
    	FILE *infile = fopen( filename, "rb" );
    	unsigned long location = 0;
    	int i = 0;
    	
    	if ( !infile )
    	{
    		printf("Error opening jpeg file %s\n!", filename );
    		return -1;
    	}
    	/* here we set up the standard libjpeg error handler */
    	cinfo.err = jpeg_std_error( &jerr );
    	/* setup decompression process and source, then read JPEG header */
    	jpeg_create_decompress( &cinfo );
    	/* this makes the library read from infile */
    	jpeg_stdio_src( &cinfo, infile );
    	/* reading the image header which contains image information */
    	jpeg_read_header( &cinfo, TRUE );
    	/* Uncomment the following to output image information, if needed. */
    	/*--
    	printf( "JPEG File Information: \n" );
    	printf( "Image width and height: %d pixels and %d pixels.\n", cinfo.image_width, cinfo.image_height );
    	printf( "Color components per pixel: %d.\n", cinfo.num_components );
    	printf( "Color space: %d.\n", cinfo.jpeg_color_space );
    	--*/
    	/* Start decompression jpeg here */
    	jpeg_start_decompress( &cinfo );
    
    	/* allocate memory to hold the uncompressed image */
    	raw_image = (unsigned char*)malloc( cinfo.output_width*cinfo.output_height*cinfo.num_components );
    	/* now actually read the jpeg into the raw buffer */
    	row_pointer[0] = (unsigned char *)malloc( cinfo.output_width*cinfo.num_components );
    	/* read one scan line at a time */
    	while( cinfo.output_scanline < cinfo.image_height )
    	{
    		jpeg_read_scanlines( &cinfo, row_pointer, 1 );
    		for( i=0; i<cinfo.image_width*cinfo.num_components;i++) 
    			raw_image[location++] = row_pointer[0][i];
    	}
    	/* wrap up decompression, destroy objects, free pointers and close open files */
    	jpeg_finish_decompress( &cinfo );
    	jpeg_destroy_decompress( &cinfo );
    	free( row_pointer[0] );
    	fclose( infile );
    	/* yup, we succeeded! */
    	return 1;
    }
    
    /**
     * write_jpeg_file Writes the raw image data stored in the raw_image buffer
     * to a jpeg image with default compression and smoothing options in the file
     * specified by *filename.
     *
     * \returns positive integer if successful, -1 otherwise
     * \param *filename char string specifying the file name to save to
     *
     */
    int write_jpeg_file( char *filename )
    {
    	struct jpeg_compress_struct cinfo;
    	struct jpeg_error_mgr jerr;
    	
    	/* this is a pointer to one row of image data */
    	JSAMPROW row_pointer[1];
    	FILE *outfile = fopen( filename, "wb" );
    	
    	if ( !outfile )
    	{
    		printf("Error opening output jpeg file %s\n!", filename );
    		return -1;
    	}
    	cinfo.err = jpeg_std_error( &jerr );
    	jpeg_create_compress(&cinfo);
    	jpeg_stdio_dest(&cinfo, outfile);
    
    	/* Setting the parameters of the output file here */
    	cinfo.image_width = width;	
    	cinfo.image_height = height;
    	cinfo.input_components = bytes_per_pixel;
    	cinfo.in_color_space = color_space;
        /* default compression parameters, we shouldn't be worried about these */
    	jpeg_set_defaults( &cinfo );
    	/* Now do the compression .. */
    	jpeg_start_compress( &cinfo, TRUE );
    	/* like reading a file, this time write one row at a time */
    	while( cinfo.next_scanline < cinfo.image_height )
    	{
    		row_pointer[0] = &raw_image[ cinfo.next_scanline * cinfo.image_width *  cinfo.input_components];
    		jpeg_write_scanlines( &cinfo, row_pointer, 1 );
    	}
    	/* similar to read file, clean up after we're done compressing */
    	jpeg_finish_compress( &cinfo );
    	jpeg_destroy_compress( &cinfo );
    	fclose( outfile );
    	/* success code is 1! */
    	return 1;
    }
    
    int main()
    {
    	char *infilename = "test.jpg", *outfilename = "test_out.jpg";
    
    	/* Try opening a jpeg*/
    	if( read_jpeg_file( infilename ) > 0 ) 
    	{
    		/* then copy it to another file */
    		if( write_jpeg_file( outfilename ) < 0 ) return -1;
    	}
    	else return -1;
    	return 0;
    }
    as you see I have included the extern C to tell the compiler that libjpeg is a c header, to treat it as such, but even then I still get this error in my linker

    C:\Users\user\Desktop\main2.cpp In function `int write_jpeg_file(char*)':
    112 C:\Users\user\Desktop\main2.cpp invalid conversion from `int' to `J_COLOR_SPACE'
    C:\Users\user\Desktop\Makefile.win [Build Error] [main2.o] Error 1

    at line 112 which is this:

    cinfo.in_color_space = color_space;

    Its been bothering me for quite some time now, and I really want image operability in my new WIN32 API Mandelbrot generator, any insight would be really useful and appreciated. Thank you

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Obvious fix: make color_space of type J_COLOR_SPACE. In C++, enums can be used as integers, but not the other way around.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    58

    Thanks!

    Thank you! Sorry that I may be new to this topic, I dont know what enums are and how to convert the two. If someone can explain that I would greatly appreciate it.

    Actually I did a:

    J_COLOR_SPACE color_space = JCS_RGB;

    but when I compile I get an error that says:

    JPEG parameter struct mismatch: library thinks size is 432, caller expects 464

    Actually I guess this post would be a diary of success *kinda*, the problem dissipated after a few compiles... and the program works fine. Anyone have any idea why I even got this error in the first place?

    And thank you very much tabstop for your help
    Last edited by parad0x13; 07-19-2008 at 03:54 PM. Reason: Problem dissipated

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    JPEG parameter struct mismatch: library thinks size is 432, caller expects 464
    Weird. Sounds like you've got mismatched compiled library and headers.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    An enumerated type is just that: you have an enumeration (that is, a list) of all the possible values it can have. So you might have
    Code:
    enum day_of_week { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };
    This creates a brand new type (just like int, float, and so on are types) called "day_of_week" that can have one of the seven possible values enumerated beside it. Internally, the values are stored as integers; you can get the integer value out of an enumeration, but you can't go the other way.
    Code:
    day_of_week today = Saturday; 
    int also_today = today; //fine -- also_today will equal 5
    day_of_week tomorrow = today+1; //error -- can't convert int into a day_of_week
    (Edit: actually, that's not entirely true -- I can't say that they're stored as integers, but each item in the enumeration has an integer value associated with it. They can be the same, though. You should look up enum in your C++ book for details.)

    As to the other thing: I doubt you recompiled the library, so there must have been something wrong in your file; my hat of guessing believes that you had a type either a size of an array go bad, or a type mismatch (maybe something was defaulting to int when it should have been short, or something). Who knows, though.
    Last edited by tabstop; 07-19-2008 at 04:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking problems, class problems
    By Kheila in forum C++ Programming
    Replies: 12
    Last Post: 11-22-2005, 12:47 AM
  2. Linking problems trying to compile QT application
    By Maragato in forum C++ Programming
    Replies: 1
    Last Post: 08-19-2005, 09:08 PM
  3. Grrr.... SDL Linking Problem
    By 7EVEN in forum Game Programming
    Replies: 5
    Last Post: 08-12-2005, 08:44 PM
  4. More linker problems
    By Ganoosh in forum C++ Programming
    Replies: 4
    Last Post: 07-12-2005, 10:27 PM
  5. Linking problems
    By Ganoosh in forum C++ Programming
    Replies: 4
    Last Post: 07-12-2005, 02:16 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21