How to Decode an image using openGL

Hi,

How to decode an image using openGL library libjpeg ..
which are the steps needed to do this using C language..
actually my work is to decode the image, store it on the buffer,
and place it on cube surface..

please guide me,,any answer will appreciated ..

Here is one way of finding source code examples of how to use libjpeg to decode a JPEG image.

Hi,

Thank you so much for your help.. it helps me a lot..
as i am new to openGL. i dont know whether it is a basic question which i am asking know.
the question is :

The program jpeg_sample.c which is there in tar file is executing with out any error ..
i think decompression is done well ,,
but its not displaying any JPEG image after execution ...

Could you tell how to display jpeg image after decoding in openGL

and i am not getting from where it is taking an jpeg image in that code ..

Please guide me , any answer will appreciated..

We cannot see the source code from here.

Given that you do not appear to much experience with the concept of textures in OpenGL, maybe you should use an image library such as DevIL.

Here is a tutorial on DevIL. It includes an example of how to load and display a JPEG using OpenGL.

Hi,

The source code is:

\#include <stdio.h>

#include <jpeglib.h>
#include <stdlib.h>

unsigned char *raw_image = NULL;

int width = 2000;
int height = 1300;
int bytes_per_pixel = 3;
int color_space = JCS_RGB;

int read_jpeg_file( char *filename )
{

struct jpeg\_decompress_struct cinfo;
struct jpeg\_error_mgr jerr;

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;
\}

cinfo.err = jpeg\_std_error\( &jerr \);

jpeg\_create_decompress\( &cinfo \);

jpeg\_stdio_src\( &cinfo, infile \);

jpeg\_read_header\( &cinfo, TRUE \);


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 \);


jpeg\_start_decompress\( &cinfo \);


raw_image = \(unsigned char*\)malloc\( cinfo.output\_width\*cinfo.output\_height*cinfo.num_components \);

row_pointer[0] = \(unsigned char *\)malloc\( cinfo.output\_width*cinfo.num_components \);

while\( cinfo.output_scanline &lt; cinfo.image_height \)
\{
    jpeg\_read_scanlines\( &cinfo, row_pointer, 1 \);
    for\( i=0; i&lt;cinfo.image\_width*cinfo.num_components;i\+\+\) 
        raw_image[location\+\+] = row_pointer[0][i];
\}

jpeg\_finish_decompress\( &cinfo \);
jpeg\_destroy_decompress\( &cinfo \);
free\( row_pointer[0] \);
fclose\( infile \);

return 1;

}

int write_jpeg_file( char *filename )
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;

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\);


cinfo.image_width = width;    
cinfo.image_height = height;
cinfo.input_components = bytes\_per_pixel;
cinfo.in\_color_space = color_space;

jpeg\_set_defaults\( &cinfo \);

jpeg\_start_compress\( &cinfo, TRUE \);

while\( cinfo.next_scanline &lt; cinfo.image_height \)
\{
    row_pointer[0] = &raw_image[ cinfo.next_scanline * cinfo.image_width *  cinfo.input_components];
    jpeg\_write_scanlines\( &cinfo, row_pointer, 1 \);
\}

jpeg\_finish_compress\( &cinfo \);
jpeg\_destroy_compress\( &cinfo \);
fclose\( outfile \);

return 1;

}

int main()
{
char *infilename = "rro1.bmp", *outfilename = "rr.jpg";

if\( read\_jpeg_file\( infilename \) &gt; 0 \) 
\{
    
        if\( write\_jpeg_file\( outfilename \) &lt; 0 \) return -1;
\}
else return -1;



return 0;

}

This code is working without any error message. but Displayed image is not decompressed .. Image is not a clear picture after decompressed ..
i need to display a decompressed JPEG image at the out put..

Please help me out with this problem,,,any answer will appreciated ..

Hi,

Sorry for late reply , i got the result of decompressed image, but now i need to insert images on Cylinder surfaces.

Please Help me out ...answer will appreciated ...

Could you post what you have so far please?

Hi Sir,

Sorry for late Reply ,,, i am in out of station ..

i have code to generate cylinder, but i need to insert images on the surface .

code :

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

#define TIMER 100

GLUquadric *myQuad;

static float xrot;
#define TMMODE_SINGLE 2
#define TMMODE_TWOPASS 3
#ifdef GL_HP_texture_lighting
#define TMMODE_HPEXT 4
#endif /* GL_HP_texture_lighting */
static int tmMode;
#define USE_ALPHA_TEST 10
static int alphaTest = 0;

static GLuint textures[2];

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

glLoadIdentity \(\);
gluLookAt \(0., 0., 5.,
           0., 1., 1.,
           0., 1., 0.\);

glRotatef \(xrot, 0.0, 1.0, 0.0\);
glTranslatef \(0., 0., 1.\);

/* Only pass or first of two passes */
gluCylinder \(myQuad, 1.0, 1.0, 4., 24, 8\);

if (tmMode == TMMODE_TWOPASS) {
GLfloat white[4] = {1., 1., 1., 1.};

/* second pass here*/
glPushAttrib (GL_ALL_ATTRIB_BITS);
glColor3f (0., 0., 0.);
glDepthFunc (GL_LEQUAL);
glEnable (GL_BLEND);
glLightfv (GL_LIGHT1, GL_SPECULAR, white);
glBindTexture (GL_TEXTURE_2D, textures[1]);

    gluCylinder \(myQuad, .5, .5, 2., 24, 8\);

    glPopAttrib \(\);

    /* Bug! Should have popped... */
    glBindTexture \(GL\_TEXTURE_2D, textures[0]\);
\}

glFlush\(\);
glutSwapBuffers\(\);

}

void reshape(int w, int h)
{
glViewport (0, 0, w, h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (50., (float)w/(float)h, 1., 20.);

/* Leave us in modelview mode for our display routine */
glMatrixMode\(GL_MODELVIEW\);

}

static void cbMainMenu (int value)
{
GLfloat white[4] = {1., 1., 1., 1.};
GLfloat black[4] = {0., 0., 0., 0.};

if \(value == 99\) \{
    exit \(0\);
\}

if (value < USE_ALPHA_TEST)
tmMode = value;

switch \(value\) \{

/* case TMMODE_NONE: {
glDisable (GL_TEXTURE_2D);
glLightfv (GL_LIGHT1, GL_SPECULAR, white);
break;
}/
case TMMODE_SINGLE: {
glEnable (GL_TEXTURE_2D);
glLightfv (GL_LIGHT1, GL_SPECULAR, white);
#ifdef GL_HP_texture_lighting
glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_LIGHTING_MODE_HP, GL_TEXTURE_POST_SPECULAR_HP);
#endif /
GL_HP_texture_lighting /
break;
}
case TMMODE_TWOPASS: {
glEnable (GL_TEXTURE_2D);
glLightfv (GL_LIGHT1, GL_SPECULAR, black);
#ifdef GL_HP_texture_lighting
glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_LIGHTING_MODE_HP, GL_TEXTURE_POST_SPECULAR_HP);
#endif /
GL_HP_texture_lighting /
break;
}
#ifdef GL_HP_texture_lighting
case TMMODE_HPEXT: {
glEnable (GL_TEXTURE_2D);
glLightfv (GL_LIGHT1, GL_SPECULAR, white);
glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_LIGHTING_MODE_HP, GL_TEXTURE_PRE_SPECULAR_HP);
break;
}
#endif /
GL_HP_texture_lighting */
case USE_ALPHA_TEST: {
alphaTest = ~alphaTest;
if (alphaTest)
glEnable (GL_ALPHA_TEST);
else
glDisable (GL_ALPHA_TEST);
break;
}
}
}

static void timer (int value)
{
xrot += 1.f;
if (xrot > 360.f) xrot -= 360.f;

glutPostRedisplay \(\);
glutTimerFunc \(TIMER, timer, 0\);

}

static void initTextureMap (int w, int h, unsigned char *tm)
{
int i, j, ci=w>>2, cj=h>>2;

for \(i=0; i&lt;w; i\+\+\) \{
    for \(j=0; j&lt;h; j\+\+\) \{
        tm[0] = tm[1] = tm[2] = tm[3] = 0;
        if \(\(i&ci\)^\(j&cj\)\) \{
            /* red has full alpha, black has zero alpha */
            tm[0] = tm[3] = 255;
        \}
        tm \+= 4;
    \}
\}

}

static void init ()
{
int mainMenu;
GLfloat pos[4] = {3., 5., 2., 1.};
GLfloat white[4] = {1., 1., 1., 1.};

xrot = 0.;

glClearColor \(.3, .3, .3, 0.\);
glColor4f \(.0, .0, 1.0, 1.\); /* cylinder is grey by default */
glDisable \(GL_DITHER\);
glEnable \(GL\_DEPTH_TEST\);

/* Set up alpha test to toss 0 alpha */
glAlphaFunc \(GL_NOTEQUAL, 0\);

/* blending function used in TMMODE_TWOPASS */
glBlendFunc \(GL_ONE, GL_ONE\);

/* Set up light1 */
glEnable \(GL_LIGHTING\);
glEnable \(GL_LIGHT1\);
glLightfv \(GL_LIGHT1, GL_POSITION, pos\);
glLightfv \(GL_LIGHT1, GL_DIFFUSE, white\);
glLightfv \(GL_LIGHT1, GL_SPECULAR, white\);

/* ambient and diffuse will track glColor */
glEnable \(GL\_COLOR_MATERIAL\);
glColorMaterial \(GL_FRONT, GL\_AMBIENT\_AND_DIFFUSE\);
glMaterialf \(GL_FRONT, GL_SHININESS, 30.f\);
glMaterialfv \(GL_FRONT, GL_SPECULAR, white\);

/* Set up texture coord generation */
\{
    /* Use Z plane for T generation because the cylinder lies along the Z axis */

//

    float tPlane[4] = \{1., 1., 1., 1.\};
    glTexGenfv \(GL_T, GL\_OBJECT_PLANE, tPlane\);
\}
glTexGeni \(GL_S, GL\_TEXTURE\_GEN_MODE, GL\_OBJECT_LINEAR\);
glTexGeni \(GL_T, GL\_TEXTURE\_GEN_MODE, GL\_OBJECT_LINEAR\);
glEnable \(GL\_TEXTURE\_GEN_S\);
glEnable \(GL\_TEXTURE\_GEN_T\);

/* prepare to use two texture objects */
glGenTextures (2, textures);

/* init first texture object, a red & black checkerboard, ho-hum */
glBindTexture \(GL\_TEXTURE_2D, textures[0]\);
glTexParameterf\(GL\_TEXTURE_2D, GL\_TEXTURE\_MAG_FILTER, GL_LINEAR\);
glTexParameterf\(GL\_TEXTURE_2D, GL\_TEXTURE\_MIN_FILTER, GL_LINEAR\);
glTexEnvi \(GL\_TEXTURE_ENV, GL\_TEXTURE\_ENV_MODE, GL_MODULATE\);
\{

#define TDIM 32
unsigned char *pixels;
pixels = (unsigned char ) malloc (TDIM*TDIM4);
initTextureMap (TDIM, TDIM, pixels);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, TDIM, TDIM,
0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
free (pixels);
}

/* Second texture is a 2x2 white texture map for the sdpecular pass of TMMODE_TWOPASS */
glBindTexture \(GL\_TEXTURE_2D, textures[1]\);
glTexParameterf\(GL\_TEXTURE_2D, GL\_TEXTURE\_MAG_FILTER, GL_LINEAR\);
glTexParameterf\(GL\_TEXTURE_2D, GL\_TEXTURE\_MIN_FILTER, GL_LINEAR\);
glTexEnvi \(GL\_TEXTURE_ENV, GL\_TEXTURE\_ENV_MODE, GL_MODULATE\);
\{
    unsigned char pixels[2\*2*4];
    memset \(pixels, 0xff, 2\*2*4\);
    glTexImage2D \(GL\_TEXTURE_2D, 0, GL_RGBA, 2, 2,
              0, GL_RGBA, GL\_UNSIGNED_BYTE, pixels\);
\}

/* Leave first texture bound */
glBindTexture \(GL\_TEXTURE_2D, textures[0]\);

myQuad = gluNewQuadric \(\);

glutDisplayFunc \(display\);
glutReshapeFunc \(reshape\);
glutTimerFunc \(TIMER, timer, 0\);

mainMenu = glutCreateMenu \(cbMainMenu\);

//glutAddMenuEntry ("No texture mapping", TMMODE_NONE);
glutAddMenuEntry ("Single pass", TMMODE_SINGLE);
glutAddMenuEntry ("Two pass", TMMODE_TWOPASS);
#ifdef GL_HP_texture_lighting
glutAddMenuEntry ("HP pre-specular", TMMODE_HPEXT);
#endif /* GL_HP_texture_lighting */
glutAddMenuEntry ("Toggle alpha test", USE_ALPHA_TEST);
glutAddMenuEntry ("Quit", 99);
glutAttachMenu (GLUT_RIGHT_BUTTON);
// tmMode = TMMODE_NONE;
}

void main(int argc, char** argv)
{
/* Pretty much standard GLUT init code sequence... */
glutInit (&argc,argv);
glutInitDisplayMode (GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize (300,300);
glutInitWindowPosition (0,0);
glutCreateWindow ("specular lighting and texture mapping");

init \(\);

glutMainLoop \(\);

}

This is the code i have .. please guide me how to insert images on the surface of the cylinder.. any answer is appreciated..