how to decompress jpeg image by libjpeg-library

You had errors because you didn’t reconcile the other image loading code using Devil when you converted to your use of libjpeg. The following code runs but you have to have three new images: logo.jpg and logo2.jpg of size 64x64 and a blank.jpg of size 128x128. I don’t claim this code is the best I just hacked your previous post until it worked :wink: You will have to understand it an clean it up to your preference of coding style. Note I moved genID into the texture_info structure and consequently remove the textures[] array.


#include <GL/gl.h>

#include <GL/glut.h>
#include <stdlib.h>

#include <stdio.h>

#include <jpeglib.h>

#include <jerror.h>

#include <IL/il.h>
#include <GL/glext.h>
#define NUM_TEXTURES 20

GLfloat gAngle = 0.0;
GLUquadricObj *IDquadric;

GLuint gState = 0;

typedef struct
{
	const char * name;
	int format;
	unsigned int size;
	GLuint genID;
} texture_info_t;

static texture_info_t textures_info[] =
{
	{ "marble.jpg", GL_RGB, 64, 0 },
	{ "chess.jpg", GL_LUMINANCE, 64, 0},
	{ "chrome.jpg", GL_RGB, 64, 0},
	{ "mercedes.jpg", GL_RGB, 64, 0},
	{ "satin.jpg", GL_RGB, 64, 0},
	{ "outline.jpg", GL_LUMINANCE, 64, 0},
	{ "marble.jpg", GL_RGB, 64, 0},
	{ "marble.jpg", GL_RGB, 64, 0},
	{ "satin.jpg", GL_RGB, 64, 0},
	{ "satin.jpg", GL_RGB, 64, 0}, // 
	{ "logo.jpg", GL_RGB, 64, 0}, // kludge to start at 10 for logo, logo2, blank
	{ "logo2.jpg", GL_RGB, 64, 0},
	{ "blank.jpg", GL_RGB, 128, 0},
	{ NULL, 0, 0, 0}
};

static int left_click = GLUT_UP;
static int right_click = GLUT_UP;
static int xold;
static int yold;
static float rotate_x = 30;
static float rotate_y = 15;
static float translate_x = 0;
static float translate_y = 0;
static float plane_xy[3] = {1, 0, 0};
static float plane_yz[3] = {0, 0, 1};

int load_texture (const char * filename,unsigned char *
dest,const int format,const unsigned int size)
{
	FILE *fd;

	struct jpeg_decompress_struct cinfo;

	struct jpeg_error_mgr jerr;

	unsigned char * line;

	cinfo.err = jpeg_std_error (&jerr);

	jpeg_create_decompress (&cinfo);

	if (0 == (fd = fopen(filename, "rb")))

		return 1;

	jpeg_stdio_src (&cinfo, fd);

	jpeg_read_header (&cinfo, TRUE);

	if ((cinfo.image_width != size) || (cinfo.image_height !=
		size))

		return 1;

	if (GL_RGB == format)
	{

		if (cinfo.out_color_space == JCS_GRAYSCALE)

			return 1;

	}
	else

	if (cinfo.out_color_space != JCS_GRAYSCALE)

		return 1;

	jpeg_start_decompress (&cinfo);

	while (cinfo.output_scanline < cinfo.output_height)
	{

		line = dest +
			(GL_RGB == format ? 3 * size : size)
			* cinfo.output_scanline;

		jpeg_read_scanlines (&cinfo, &line, 1);

	}

	jpeg_finish_decompress (&cinfo);

	jpeg_destroy_decompress (&cinfo);

  fclose(fd);

	return 0;

}


void timer(int value)
{
	const int desiredFPS=120;
	glutTimerFunc(1000/desiredFPS, timer, ++value);
	GLfloat dt = 1./desiredFPS;

	gAngle += dt*360./8.;

	glutPostRedisplay();
}


void draw_cylinder()
{
	glPushMatrix();
	glTranslatef(-5.,0,-100);
	glRotatef(gAngle,1.,1.,0.);
	gluCylinder(IDquadric,10.0f,10.0f,10.0f,32,32);
	glPopMatrix();
}


void display()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_MODELVIEW);

	if (gState)
	{
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_COLOR,GL_DST_COLOR);

		glBlendColor(.5,.5,.5,.5);

		glBindTexture ( GL_TEXTURE_2D, textures_info[10].genID);
		draw_cylinder();

		glBindTexture ( GL_TEXTURE_2D, textures_info[11].genID);
		draw_cylinder();

		glDisable(GL_BLEND);
	}
	else
	{
		glBindTexture ( GL_TEXTURE_2D, textures_info[12].genID);
		draw_cylinder();
	}

	glutSwapBuffers();
}


void CreateMultiTexture()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();

	GLint viewport[4];
	glGetIntegerv(GL_VIEWPORT, viewport);

	glViewport(0,0,textures_info[12].size,textures_info[12].size);

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_COLOR,GL_DST_COLOR);

	glBlendColor(.5,.5,.5,.5);

	glBindTexture ( GL_TEXTURE_2D, textures_info[10].genID);
	glBegin(GL_QUADS);
	glTexCoord2f(0,0); glVertex2f(-1,-1);
	glTexCoord2f(1,0); glVertex2f( 1,-1);
	glTexCoord2f(1,1); glVertex2f( 1, 1);
	glTexCoord2f(0,1); glVertex2f(-1, 1);
	glEnd();

	glBindTexture ( GL_TEXTURE_2D, textures_info[11].genID);
	glBegin(GL_QUADS);
	glTexCoord2f(0,0); glVertex2f(-1,-1);
	glTexCoord2f(1,0); glVertex2f( 1,-1);
	glTexCoord2f(1,1); glVertex2f( 1, 1);
	glTexCoord2f(0,1); glVertex2f(-1, 1);
	glEnd();

	glDisable(GL_BLEND);

	glBindTexture(GL_TEXTURE_2D,textures_info[12].genID);
	glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, textures_info[12].size, textures_info[12].size, 0);

	glViewport(viewport[0], viewport[1], viewport[2] ,viewport[3]);
	glMatrixMode(GL_MODELVIEW);
	glPopMatrix();
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();

	glutSwapBuffers();

	glutDisplayFunc(display);
}


void cleanupQuadric(void)
{
	gluDeleteQuadric(IDquadric);
	printf( "cleanupQuadric completed
" );
}


void init()
{
	glClearColor(0.0, 0.0, 0.0, 0.0);

	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

	IDquadric=gluNewQuadric();
	gluQuadricNormals(IDquadric, GLU_SMOOTH);
	gluQuadricTexture(IDquadric, GL_TRUE);
	atexit(cleanupQuadric);

	GLdouble Vol = 10*1.8;
	GLdouble Left=-Vol;
	GLdouble Right=Vol;
	GLdouble Bottom=-Vol;
	GLdouble Top=Vol;
	GLdouble Near=0;
	GLdouble Far=2*Vol;

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(Left, Right, Bottom, Top, Near, Far);

	GLdouble eyeX=0;
	GLdouble eyeY=0;
	GLdouble eyeZ=-100+Vol;
	GLdouble centerX=0;
	GLdouble centerY=0;
	GLdouble centerZ=-100;
	GLdouble upX=0;
	GLdouble upY=1;
	GLdouble upZ=0;
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(eyeX,eyeY,eyeZ,
		centerX,centerY,centerZ,
		upX,upY,upZ);

	ilInit();

	glEnable (GL_TEXTURE_2D);
}


void keyboard(unsigned char key, int x, int y)
{
	switch (key)
	{
		case 27:
			exit(0);
			break;
		default:
			gState++;
			gState %= 2;
			printf("%d
",gState);
			break;
	}
}


int main(int argc, char** argv)
{
	unsigned char texture[NUM_TEXTURES][3 * 128 * 128];
	unsigned int i;

	glutInit (&argc, argv);
	glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
	glutInitWindowSize (500, 500);
	glutCreateWindow ("Texture gen");

	glClearColor (0, 0, 0, 0);
	glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	glEnable (GL_TEXTURE_2D);

	for (i = 0; textures_info[i].size != 0; ++i)
	{
	  glGenTextures (1, &textures_info[i].genID);

		if (load_texture (textures_info[i].name,texture[i],
			textures_info[i].format,textures_info[i].size) != 0)
			return 1;

		glBindTexture (GL_TEXTURE_2D, textures_info[i].genID);

		glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
			GL_LINEAR);

		glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
			GL_LINEAR);

		glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
			GL_REPEAT);

		glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
			GL_REPEAT);

		gluBuild2DMipmaps (GL_TEXTURE_2D, textures_info[i].format,
			textures_info[i].size,
			textures_info[i].size,
			textures_info[i].format,
			GL_UNSIGNED_BYTE, texture[i]);
	}

	glutDisplayFunc (CreateMultiTexture);
	glutTimerFunc(0,timer,0);
	glutKeyboardFunc(keyboard);

	init();

	glutMainLoop();

	return 0;
}

Hi,
Thank you for your hint,

The code generates the full cylinder, if i commented the line glEnable(GL_CULL_FACE);

One more is above code is not taking the images from the textures array.Even after i created logo, logo2 , blank images with size you specified.
and Output window with cylinder is also not Displaying.

What shall i do ?

please guide me.

Are the new images log*.jpg and blank.jpg all black?

I cut and pasted the above code and recompiled it. If I had black images then nothing would show but otherwise the cylinder showed with logo*.jpg images (don’t care about blank.jpg colors just that its size is 128x128) that were say copies marble.jpg.

Also you should see a textured square in the lower left corner of the screen (no cylinder will be visible) if you comment out the line “glutDisplayFunc(display);” at the end of CreateMultiTexture() function. Can you please confirm what you see?

Hi,

Thanks a lot. I verified what you told in the above post.

I used the above code as it is, except textures_info[] array which i pasted below :

static texture_info_t textures_info[] =
{
// { “/localhome/user/Temp/sample/marble.jpg”, GL_RGB, 64, 0 },
// { “/localhome/user/Temp/sample/chess.jpg”, GL_LUMINANCE, 64, 0},

// { “/localhome/user/Temp/sample/chrome.jpg”, GL_RGB, 64, 0},
// { “/localhome/user/Temp/sample/mercedes.jpg”, GL_RGB, 64, 0},
// { “/localhome/user/Temp/sample/satin.jpg”, GL_RGB, 64, 0},
// { “/localhome/user/Temp/sample/outline.jpg”, GL_LUMINANCE, 64, 0},

// { “/localhome/user/Temp/sample/marble.jpg”, GL_RGB, 64, 0},
// { “/localhome/user/Temp/sample/marble.jpg”, GL_RGB, 64, 0},
// { “/localhome/user/Temp/sample/satin.jpg”, GL_RGB, 64, 0},
// { “/localhome/user/Temp/sample/satin.jpg”, GL_RGB, 64, 0},
{ “/localhome/user/Temp/sample/logo.jpg”, GL_RGB, 64, 0},
{ “/localhome/user/Temp/sample/logo2.jpg”, GL_RGB, 64, 0},

 { "/localhome/user/Temp/sample/blank12.jpg", GL_RGB, 128, 0},
    { NULL, 0, 0, 0}

};

where logo.jpg & logo2.jpg are images not blank, where as blank12.jpg is a blank image.
if i comment the lines which i show in this post. displayed cylinder only, no images on that,
if i remove the comments in the above lines, its not showing output window.

i have all the images in the folder

“/localhome/user/Temp/sample/” folder. So i gave that path.

What will be the problem ?

Even if i remove all the comments which are there in the above lines and commented the below line :
glutDisplayFunc(display)

Then also it’s not displaying any window.

please guide me.

In void CreateMultiTexture() there are hardcoded references


textures_info[10].genID
textures_info[11].genID
textures_info[12].size 
etc

so that explains why you must not comment out the lines in
static texture_info_t textures_info[] for now.

The filename strings as “/localhome/user/Temp/sample/marble.jpg” with forward slashes works on linux/mingw but probably not windows – what os are you on? To remove any doubt, just leave that code alone and copy all those image files into the same place where the executable is created to avoid the path specification for now.

The fact that it is not displaying a window at all (case with comments removed) – you have exit on failure code in load_texture() – I suspect that one of your jpegs is not loading or loading with a failure hence causes the code to exit before a window can be drawn. I suggest modifying in main


if (load_texture (textures_info[i].name,texture[i],textures_info[i].format,textures_info[i].size) != 0) 
{
 printf("failed to load an image %s",textures_info[i].name,texture[i] );
 return 1;
}
printf("successs: loaded %s",textures_info[i].name,texture[i]);

ps I will be out of the office for the next two weeks on vacation. Sorry I won’t be able to help during that time.

Hi,

Thank you so much.

I replaced lines in main program(where the function called loadtexture()) with the above lines .
so after that i am getting messages like :


successs: loaded marble.jpgsuccesss: loaded chess.jpgsuccesss: loaded blank12.jpg1
cleanupQuadric completed
user@lxdevenv:~/Temp/sample$ 


The output is cylinder with no images on that.

i dint get the code which you posted :



textures_info[10].genID
textures_info[11].genID
textures_info[12].size 
etc

What this indicates ?

please help me from this …

Hi,

Could you help me on this .please help me from this problem .

Thanking You.

The comment on the 10,11,12 indexes is just a temporary kludge to not alter your existing code – I don’t use the first 0-9 references but if you comment any of them out then for instance what I expect to be [10] becomes mis-defined as a lower index number – basically do not comment out any of the “static texture_info_t textures_info[] = …” block for now while debugging.

It is really difficult to detect the problem without having your exact textures – I had a similar post to this thread which requests how to share files in this forum – please read Post269741 so you can share your exact texture files. I really can’t help much more without having the same exact files as you – the code runs fine on my machine but yours doesn’t and the only difference is the textures actually used.

Hi,

Thank you very much,

I am sending link where the program is :

http://uploading.com/files/44a2e4mf/recglut.c/

and the images are shared in the below link :

http://img514.imageshack.us/gal.php?g=mercedesgk.jpg

i am not getting the above process.
please help me to clear the problem.

Thankinh you.

The image files downloaded are


blankcr.jpg  chessx.jpg  chromecd.jpg  golds.jpg  marblel.jpg  mercedesgk.jpg

and don’t match your files in the code


static texture_info_t textures_info[] =
{
	{ "/localhome/user/Temp/sample/marble.jpg", GL_RGB, 64, 0 },
	{ "/localhome/user/Temp/sample/chess.jpg", GL_LUMINANCE, 64, 0},
//	{ "/localhome/user/Temp/sample/chrome.jpg", GL_RGB, 64, 0},
//	{ "/localhome/user/Temp/sample/mercedes.jpg", GL_RGB, 64, 0},
	{ "/localhome/user/Temp/sample/satin.jpg", GL_RGB, 64, 0},
	{ "/localhome/user/Temp/sample/outline.jpg", GL_LUMINANCE, 64, 0},
	{ "/localhome/user/Temp/sample/marble.jpg", GL_RGB, 64, 0},
	{ "/localhome/user/Temp/sample/marble.jpg", GL_RGB, 64, 0},
//	{ "satin.jpg", GL_RGB, 64, 0},
//	{ "satin.jpg", GL_RGB, 64, 0}, 
//	{ "logo.jpg", GL_RGB, 64, 0}, 
//	{ "logo2.jpg", GL_RGB, 64, 0},
	{ "/localhome/user/Temp/sample/blank.jpg", GL_RGB, 128, 0},
	{ NULL, 0, 0, 0}
};

Where’s the download for “satin.jpg”? “outline.jpg”?

Do the pixel dimensions make sense for the downloaded files?


 identify krisna12/*
krisna12/blankcr.jpg JPEG 128x128 128x128+0+0 8-bit DirectClass 907b 
krisna12/chessx.jpg[1] JPEG 64x64 64x64+0+0 8-bit PseudoClass 256c 237b 
krisna12/chromecd.jpg[2] JPEG 64x64 64x64+0+0 8-bit DirectClass 1.2kb 
krisna12/golds.jpg[3] JPEG 64x64 64x64+0+0 8-bit DirectClass 705b 
krisna12/marblel.jpg[4] JPEG 64x64 64x64+0+0 8-bit DirectClass 2.28kb 
krisna12/mercedesgk.jpg[5] JPEG 64x64 64x64+0+0 8-bit DirectClass 416b

Hi,

Thank you Very much.

The pixel dimension are same as they in the above post.

The images are placed with the same name which i specified. but i dont know why the names of the images are changing.

and satin.jpg and outline.jpg are similarly there in my folder,but i dint loaded that’s it.

Could you link the code which you are executing in your system ?

please help me from this why it is not displaying images ?

Thanking you.

The code runs fine on my machine provided you set the following exactly as is:


static texture_info_t textures_info[] =
{
	{ "marble.jpg", GL_RGB, 64, 0 },
	{ "chess.jpg", GL_LUMINANCE, 64, 0},
	{ "chrome.jpg", GL_RGB, 64, 0},
	{ "mercedes.jpg", GL_RGB, 64, 0},
	{ "satin.jpg", GL_RGB, 64, 0},
	{ "outline.jpg", GL_LUMINANCE, 64, 0},
	{ "marble.jpg", GL_RGB, 64, 0},
	{ "marble.jpg", GL_RGB, 64, 0},
	{ "satin.jpg", GL_RGB, 64, 0},
	{ "satin.jpg", GL_RGB, 64, 0}, 
	{ "logo.jpg", GL_RGB, 64, 0}, 
	{ "logo2.jpg", GL_RGB, 64, 0},
	{ "blank.jpg", GL_RGB, 128, 0},
	{ NULL, 0, 0, 0}
};

The reason that you cannot comment out lines/image references in this code segment at random (just because you don’t use them) is because the code is referencing textures_info[10], textures_info[11], and textures_info[12] explicitly elsewhere. If you want to comment out textures then you have to fix the code to not reference textures_info[10], [11], [12] accordingly.