Thread: C++ Equivalent JPEG Functions?

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    193

    C++ Equivalent JPEG Functions?

    I was wondering if there were jpeg functions in C++ that are equivalent to the following PHP jpeg functions:

    imagesx (width)
    imagesy (height)
    imagecreatefromjpeg (load the jpeg basically)
    imagejpeg (save the jpeg basically)
    imagedestroy (destroy the image handle)

    If there isnt equivalent functions, how would I check to see if a file is a VALID jpeg file (not just with a .jpg extension), retrieve the width and height, and "change" the quality of the image. In PHP, I use imagejpeg(image); to decrease the filesize of the image, but it doesnt necessarily "change" the quality.

    Thanks in advanced for any help!!! It would be GREATLY appreciated!!!

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I'm sure it said somewhere that C++ can't do anything with JPEGS. Unless you're using API

    Edit: Must have read wrong
    Last edited by bumfluff; 04-29-2006 at 04:14 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Sure, you just use a jpeg library and you're there.

    Some graphics libraries have support for this, which do you have?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    I'm using API (I think I am at least, I have a window with a menu and dialog boxes and other things).

    I have libjpeg currently, but I could download any library if necessary (preferably a .devpak since I'm using Bloodshed Dev-C++).

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    I have downloaded and installed the JPGalleg for Allegro library. I was able to call the function load_jpg, but the program comes up with the this program is not responding thing and the program quits. When I change the load_jpg function to load_bitmap and use a .bmp image, it works just fine and dandy. I'm having troubles getting it to a load a .jpg image. Any help is greatly appreciated.

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Any help is greatly appreciated. All I need to do is retrieve the width and height of the image and to be able to "change" the quality of the image (kinda like opening and image in MS Paint and hitting save, the filesize goes down unless you have already saved it with MS Paint).

    Thanks.

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    It doesnt seem like anyone can answer my question. This is what I have so far.

    Code:
    #include <jpgalleg.h> // this file automatically includes allegro.h...i dont want to include it twice
    
    int main() {
         BITMAP *bmp;
    
         allegro_init();
         jpgalleg_init();
    
         bmp = load_jpg("jpg.jpg", NULL); // This line causes the error described below
    
         if(!bmp) {
              allegro_message("Error loading jpg.jpg (error code: %d)\n", jpgalleg_error);
              return -1;
         }
    
         allegro_message("Width: %d, Height: %d", bmp->w, bmp->h);
    
         destroy_bitmap(bmp);
         return 0;
    }
    END_OF_MAIN()
    Libraries being linked when compiling in order: -lalleg -ljpgal -lallp

    For some reason, if I dont link the library -lallp, then it wont work. None of the examples and none of the documentation says to link this library. This could be the problem.

    The above example compiles; however, when I run the .exe and it comes to the load_jpg() function of the code, it returns the "test.exe has encountered a problem and needs to close. We are sorry for the inconvenience." error.

    What am I doing wrong? Thanks in advanced.

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    All ya'll had to say was to switch the order in which the libraries are ordered. You have to put -ljpgal first, then -lalleg. Then you dont have to include -lallp. The above example works now, thanks to myself for actually reading the documentation FAQ portion. If I have any other JPEG related questions, I will start a new topic since no one decided to post in this one.

  9. #9

    Join Date
    May 2005
    Posts
    1,042
    Code I wrote using the JPEG library manual (info found on MSDN I believe). I also strongly believe something similar is on nehe.gamedev.net (OpenGL graphics tutorials). This loads the pertinent data utilizing the jpeg library (jpeg.h + jpeg.lib in my project) and returns a texture.

    Code:
    UINT	TextureManager::LoadJPG(char * FileName)
    {
    	FILE	*pFile = fopen(FileName, "rb");
    	if(!pFile) //texture doesn't exist 
    	{
    	//	trace << "cannot find texture: " << FileName << "\n";
    		return	NULL;
    	}
    	jpeg_decompress_struct jpg;
    	
    	jpeg_error_mgr jerr;
    
    	jpg.err = jpeg_std_error(&jerr); //for some dumb reason we really need this 
    
    	int		channels, width, height;
    	
    	jpeg_create_decompress(&jpg);		
    
    	jpeg_stdio_src(&jpg, pFile);
    	
    	jpeg_read_header(&jpg, 1); 
    
    	jpeg_start_decompress(&jpg);
    
    	channels = jpg.num_components;
    	
    	width = jpg.image_width;
    
    	height = jpg.image_height;
    
    	int rowsize = jpg.image_width * jpg.num_components;
    	
    	byte	*data = new byte [rowsize * height];
    			
    	byte** t = new byte*[height];
    
    	for (int i = 0; i < height; i++)
    		t[height-i-1] = &(data[i * rowsize]); //reads from 0 to height-1
    											  //want to work backwards so he start from top and works down
    
    	i = 0;
    	while (jpg.output_scanline < jpg.output_height) //jpeg library modifies these behind the scenes
    	{
    		i += jpeg_read_scanlines(&jpg,&t[i], jpg.output_height - i);
    	}
    	delete[]	t;
    	jpeg_finish_decompress(&jpg);
    	jpeg_destroy_decompress(&jpg);
    
    	GLenum	type;
    	if(channels == 3)
    	{
    		type = GL_RGB;
    	}
    	if(channels == 4)
    	{
    		type = GL_RGBA;
    	}
    	return	GenerateInternalTexture(data, channels, width, height, type);
    }
    I hope that may help some.
    I'm not immature, I'm refined in the opposite direction.

  10. #10
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Thanks for your help, but I think I got my problem solved. I had ordered the libraries incorrectly, which I didnt think made that big of a difference.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  2. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  3. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  4. Inline functions and inheritance
    By hpy_gilmore8 in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2004, 06:46 PM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM