Advertisement

JPEG compression

Started by April 04, 2002 07:35 PM
9 comments, last by phantom007 22 years, 10 months ago
If someone knows a good method of using Jpeg's in an openGL program can they please e-mail me. I have the intel program but I must have mis-read something because I can't get it too work. It doesn't have to be the intel way, if anyone knows of a better method I would love to hear from you. koala@koalacomics.com.au A strategy sports game that can turn into open warfare 'Bloody Football' - www.koalacomics.com.au/bloody [edited by - phantom007 on April 4, 2002 8:36:23 PM]
www.koalacomics.com.au for a humour comic
www.lobag.com for my fantasy RPG PBBG
www.testcricketmanager.com for my sports management game
DevIL (formerly OpenIL).
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Advertisement
Intel''s "jpglib" or something like that.. very nice thing.. but personaly I don''t like to include jpegs..

There are more worlds than the one that you hold in your hand...

You should never let your fears become the boundaries of your dreams.
At the moment the program I have made is using .TGA files which account for over 7.5MB of the 8MB zip file. Using JPEG''s would allow the same graphics to only use 1MB, a huge saving
www.koalacomics.com.au for a humour comic
www.lobag.com for my fantasy RPG PBBG
www.testcricketmanager.com for my sports management game

  bool LoadJPEG(TextureImage *texture, char *filename){   JPEG_CORE_PROPERTIES		image;	byte							*imageBits = NULL,		// the bits, before swapping									*imageBits2 = NULL;		//				 after swapping	int							imageSize = 0,									imageWidth = 0,									imageHeight = 0;   ZeroMemory( ℑ, sizeof( JPEG_CORE_PROPERTIES ) );	// try to init the image ''container''   if( ijlInit( ℑ ) != IJL_OK ) {		return 0;	}   	image.JPGFile = const_cast<char*>(filename);	// try to read the params from the file   if( ijlRead( ℑ, IJL_JFILE_READPARAMS ) != IJL_OK )  {			return 0;   }	// check info about the channels	switch(image.JPGChannels) {		case 1:		{			image.JPGColor = IJL_G;			break;		}		case 3:		{          image.JPGColor = IJL_YCBCR;          break;		}      default:		{          // This catches everything else, but no          // color twist will be performed by the IJL.			image.DIBColor = (IJL_COLOR)IJL_OTHER;			image.JPGColor = (IJL_COLOR)IJL_OTHER;			break;		}	}   image.DIBWidth    = image.JPGWidth;   image.DIBHeight   = image.JPGHeight;   image.DIBChannels = 3;   image.DIBPadBytes = IJL_DIB_PAD_BYTES(image.DIBWidth,image.DIBChannels);   imageSize = (image.DIBWidth * image.DIBChannels + image.DIBPadBytes) * image.DIBHeight;	// allocate memory to store the image bytes (the info OGL wants)   imageBits = new byte[ imageSize ];	imageBits2 = new byte[ imageSize ];   if( (imageBits == NULL) || (imageBits2 == NULL) ) {            		return false;   }           image.DIBBytes = imageBits;	// Read the image bytes from the image   if( ijlRead( ℑ, IJL_JFILE_READWHOLEIMAGE ) != IJL_OK ) {		delete [] imageBits;		delete [] imageBits2;   }	// free the image container   if( ijlFree( ℑ ) != IJL_OK ) {						return false;			   }	imageWidth	= image.DIBWidth;	imageHeight = image.DIBHeight;	// the JPG stores its image bytes in a different order, instead of	// going from lower to higher lines it goes higher to lower.	// Use imageBits2 after this loop.	for (int j = 0;j < imageHeight; j++) {		for (int i = 0;i < imageWidth; i++) {			imageBits2[(j * imageWidth+i)* 3] = imageBits[((imageHeight-1-j) * imageWidth+i)* 3+2];			imageBits2[(j * imageWidth+i)* 3+1] = imageBits[((imageHeight-1-j) * imageWidth+i)* 3+1];			imageBits2[(j * imageWidth+i)* 3+2] = imageBits[((imageHeight-1-j) * imageWidth+i)* 3];		}	}	// OpenGL part: generate ids, bind texture and create texture	glGenTextures(6,&texture[1].texID);	glBindTexture( GL_TEXTURE_2D, texture[0].texID);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imageWidth, imageHeight, 0, GL_RGB, 					 GL_UNSIGNED_BYTE, imageBits2 );	// free memory	delete [] imageBits;	delete [] imageBits2;   return true;}  


Thats the Jpeg loader I use, utilizing the intel jpg library. I found the example somewhere a while ago but I don''t remember where.

you have to add ijl11.lib jpeg library file into your project
and include ijl.h header file. search google for the intel jpeg library to find these files.


Thanks for the code, I have installed everything but it tells me that TextureImage is an undeclared identifier from the heading ->
bool LoadJPEG(TextureImage *texture, char *filename)

What does your line of code that you use in your main program look like to call a Jpeg? use the jpeg? and finally unload the jpeg?

Please excuse my ignorance if you find this easy to work out ''cause I don''t
www.koalacomics.com.au for a humour comic
www.lobag.com for my fantasy RPG PBBG
www.testcricketmanager.com for my sports management game
Advertisement
ack my bad I forgot to give you the structure to hold the image. I have this in global declarations but you may do it differently, in any event here it is:


          typedef struct				// Create texture image structure{	GLubyte	*imageData;		// Image Data (Up To 32 Bits)	GLuint	bpp;			// Image Color Depth In Bits Per Pixel.	GLuint	width;			// Image Width	GLuint	height;			// Image Height	GLuint	texID;			// Texture ID Used To Select A Texture} TextureImage;				// Structure NameTextureImage textures[20];									// Storage For One Texture  


to use the Jpeg just do as you normally would for a TGA or BMP. You can see how to use it in the function parameter list also. here is a typical example of modified NeHe code for InitGL to use this jpeg loader:


            int InitGL(GLvoid)						{	if (!LoadTGA(&textures[0],"Font.tga")	||	    !LoadJPEG(&textures[1],"abc.jpg")	||	    !LoadJPEG(&textures[2],"xyz.jpg") ||	    !LoadJPEG(&textures[3],"yadda.jpg")            )	{           		return false;							// If Loading Failed, Return False	}	quadratic=gluNewQuadric();							// Create A Pointer To The Quadric Object (Return 0 If No Memory)	gluQuadricNormals(quadratic, GLU_SMOOTH);			// Create Smooth Normals 	gluQuadricTexture(quadratic, GL_TRUE);				// Create Texture Coords 	glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); // Set The Texture Generation Mode For S To Sphere Mapping (NEW)	glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); // Set The Texture Generation Mode For T To Sphere Mapping (NEW)	BuildFont();											// Build The Font	glClearColor(0.0f, 0.4f, 0.0f, 0.0f);					// Background color	glClearDepth(1.0f);										// Depth Buffer Setup	glDepthFunc(GL_LEQUAL);							// The Type Of Depth Testing To Do	glEnable(GL_DEPTH_TEST);								// Enable depth testing	glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);	// Select The Type Of Blending	glEnable(GL_BLEND);							// Enable Blending (disable alpha testing)	glShadeModel(GL_SMOOTH);								// Enable Smooth Shading	glEnable(GL_TEXTURE_2D);								// Enable Texture Mapping	glBindTexture(GL_TEXTURE_2D, textures[0].texID);		// Select The Font Texture	return true;											// Initialization Went OK}        


[edited by - element2001 on April 5, 2002 4:01:38 PM]

[edited by - element2001 on April 5, 2002 4:02:28 PM]

Now there''s alot of stuff in initGL that you probably don''t need, for example I use quadrics and also sphere mapping and blending. You may not need those, so only modify what you need in your program.
oops i also just discovered this line is wrong in the loader:

glGenTextures(6,&texture[1].texID);

change it to:

glGenTextures(1,&texture[1].texID);

this will load 1 jpg per pass to the function.
I have the following code in my normal program but I get the error "LoadJPEG : missing storage-class or type specifiers", "too many initializers" & "''initializing'' : cannot convert from ''char [24]'' to ''int''" from the "LoadJPEG(&start_back,"textures/start_back.jpg");" line.

TextureImage start_back;

LoadJPEG(&start_back,"textures/start_back.jpg");

A strategy sports game that can turn into open warfare
''Bloody Football'' - www.koalacomics.com.au/bloody
www.koalacomics.com.au for a humour comic
www.lobag.com for my fantasy RPG PBBG
www.testcricketmanager.com for my sports management game

This topic is closed to new replies.

Advertisement