PDA

View Full Version : .jpg file as a texture



vijaya
11-30-2009, 11:05 AM
How to upload an .jpg image as a texture.

Ilian Dinev
11-30-2009, 11:07 AM
Google for the IPicture COM object if you're targeting Windows. I use that.
There are also various image libraries like DevIL.

vijaya
11-30-2009, 11:11 AM
Thank u for ur reply... but i am targetting for linux

ZbuffeR
11-30-2009, 11:57 AM
then libjpeg

marshats
11-30-2009, 02:32 PM
Note DevIL (http://openil.sourceforge.net/about.php) is cross-platform -- I use it on Linux no-problem, very easy.

RigoN
11-30-2009, 07:37 PM
hi!!!
you can use SDL_image or write your own loader using libjpeg, here is my example:
rglImage.h


#ifndef _IMAGE_LIB_H
#define _IMAGE_LIB_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <jpeglib.h>
#include <setjmp.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <GL/gl.h>
#define TGA_RGB 2
#define TGA_A 3
#define TGA_RLE 10

struct RGL_TextureImage {
GLint width;
GLint height;
GLint imgChanal;
unsigned char *data;
} ;
typedef struct RGL_TextureImage RGL_TextureImage;

GLint rglLoadBMP(char *filename, RGL_TextureImage *image);
GLint rglLoadTGA(char *filename, RGL_TextureImage *image);
GLint rglLoadJPG(char *filename, RGL_TextureImage *image);
// LoadImageSDL - TGA, BMP, PNM( Portable Anymap (.pnm)), XPM(X11 Pixmap (.xpm)), XCF(GIMP native (.xcf)), GIF, PCX,
GLint rglLoadImageSDL(char *filename, RGL_TextureImage *image);

#endif

rglImage.c



#include "rglImage.h"
....
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

struct my_error_mgr {
struct jpeg_error_mgr pub;
jmp_buf setjmp_buffer;
};

typedef struct my_error_mgr *my_error_ptr;

static void my_error_exit(j_common_ptr cinfo) {
my_error_ptr myerr = (my_error_ptr)cinfo->err;
(*cinfo->err->output_message)(cinfo);
longjmp(myerr->setjmp_buffer,1);
}

int rglLoadJPG(char *filename, RGL_TextureImage *image) {

struct jpeg_decompress_struct cinfo;
struct my_error_mgr jerr;
FILE * infile;
JSAMPARRAY buffer;
GLint row_stride;
GLint cont;


if(!(infile = fopen(filename,"rb"))) return 0;
cinfo.err = jpeg_std_error(&amp;jerr.pub);
jerr.pub.error_exit = my_error_exit;
if(setjmp(jerr.setjmp_buffer)) {
jpeg_destroy_decompress(&amp;cinfo);
fclose(infile);
return 0;
}
jpeg_create_decompress(&amp;cinfo);
jpeg_stdio_src(&amp;cinfo, infile);
jpeg_read_header(&amp;cinfo,TRUE);
jpeg_start_decompress(&amp;cinfo);
row_stride = cinfo.output_width * cinfo.num_components;
buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr)&amp;cinfo,JPOOL_IMAGE,row _stride,1);

image ->width =cinfo.image_width;
image ->height = cinfo.image_height;
image->imgChanal = cinfo.num_components;
image -> data = (unsigned char *) malloc(image ->width * image ->height * cinfo.num_components);
cont = cinfo.output_height-1;
while(cinfo.output_scanline < cinfo.output_height)
{
jpeg_read_scanlines(&amp;cinfo,buffer,1);
memcpy(image -> data + cinfo.image_width * cinfo.num_components * cont,buffer[0],row_stride);
cont--;
}
fclose(infile);
jpeg_finish_decompress(&amp;cinfo);

jpeg_destroy_decompress(&amp;cinfo);


printf("Image JPG load OK %s \n", filename);
return 1;

}


int rglLoadImageSDL(char *filename, RGL_TextureImage *image)
{
GLuint rmask = 0x00ff0000;
GLuint gmask = 0x0000ff00;
GLuint bmask = 0x000000ff;
GLuint amask = 0x00000000;
SDL_Surface* result_image=NULL;
SDL_Surface* temp_image = IMG_Load(filename);
SDL_PixelFormat *format = temp_image->format;

if (format->BytesPerPixel ==4)
{
image->imgChanal =4;
amask = 0xff000000;
result_image=SDL_CreateRGBSurface(SDL_HWSURFACE,te mp_image->w,temp_image->h,32, rmask,gmask,bmask,amask);
}
if (format->BytesPerPixel ==3)
{
image->imgChanal =3;
result_image=SDL_CreateRGBSurface(SDL_HWSURFACE,te mp_image->w,temp_image->h,24, rmask,gmask,bmask,amask);
}

SDL_BlitSurface(temp_image, 0, result_image, 0);

image ->width =result_image->w;
image ->height = result_image->h;

image -> data = (unsigned char *) malloc(image ->width * image ->height * image->imgChanal);

GLubyte *rowlo, *rowdata;
rowlo = (GLubyte *)result_image->pixels + (result_image->h-1) * result_image->pitch; //last line of the image
rowdata =(GLubyte *) image ->data;

GLint i=0;
do {
memcpy(rowdata, rowlo, result_image->pitch);
rowdata += result_image->pitch;
rowlo -= result_image->pitch;
++i;
}
while ( i< result_image->h);

SDL_FreeSurface(temp_image);
SDL_FreeSurface(result_image);

return 1;
}

marshats
11-30-2009, 10:26 PM
Here's a Devil code snippet (genralized to more formats from previous working example gl3.2 example at Post265778 (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&amp;Number=265778#Post2657 78) ) :


#include <IL/ilut.h> //http://openil.sourceforge.net/tuts/tut_step/index.htm

...

struct TextureHandle {
ILubyte *p; /* pointer to image data loaded into CPU memory */
ILuint id; /* unique DevIL id of image */
ILint w; /* image width */
ILint h; /* image height */
ILenum DestFormat; /* see ilConvertImage */
ILenum DestType; /* see ilConvertImage */
GLuint genID; /* handle to GPU memory ID, from glGenTextures() */
};
TextureHandle wmap, logo, frac;


ILuint LoadImageDevIL (const char *szFileName, struct TextureHandle *T)
{
//When IL_ORIGIN_SET enabled, the origin is specified at an absolute
//position, and all images loaded or saved adhere to this set origin.
ilEnable(IL_ORIGIN_SET);
//sets the origin to be IL_ORIGIN_LOWER_LEFT when loading all images, so
//that any image with a different origin will be flipped to have the set
//origin.
ilOriginFunc(IL_ORIGIN_LOWER_LEFT);

//Now load the image file
ILuint ImageNameID;
ilGenImages(1, &amp;ImageNameID);
ilBindImage(ImageNameID);
if (!ilLoadImage(szFileName)) return 0; // failure
ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);

T->id = ImageNameID;
T->p = ilGetData();
T->w = ilGetInteger(IL_IMAGE_WIDTH);
T->h = ilGetInteger(IL_IMAGE_HEIGHT);
T->DestFormat = ilGetInteger(IL_IMAGE_FORMAT);
T->DestType = ilGetInteger(IL_IMAGE_TYPE);

// openGL specific part now
// download image data to GPU memory to improve perfromance
glGenTextures(1, &amp;T->genID);
glBindTexture(GL_TEXTURE_2D, T->genID);
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, T->DestFormat, T->w, T->h, 0, T->DestFormat, T->DestType, T->p);

printf("%s %d %d %d\n",szFileName,T->id,T->w,T->h);
return 1; // success
}

... sample usage on startup
ilInit();
assert( LoadImageDevIL ("Logo.jpg", &amp;logo) );

... in Drawing
glEnable (GL_TEXTURE_2D);
glBindTexture ( GL_TEXTURE_2D, logo.genID);
//Draw your triangles



My favorite thing about DevIL library is that if I want to load a png instead of a jpg or any other type of format I simply make ONE change to the code above ie


assert( LoadImageDevIL ("Logo.png", &amp;logo) );

vijaya
12-01-2009, 09:59 AM
Thank u ...its very useful

Yann LE PETITCORPS
12-01-2009, 05:21 PM
Hi,

I have begin to work about an OpenGL viewer that use libavcodec for decompress video files and display it to OpenGL windows (this work already on real time with multiples X11 windows via SDL but I think to finish in some days/weeks the OpenGL port for supporting very more videos blending/mixing effects with the more hardware support possible).

Exist an API in Linux that can use recents graphics cards that have certainly the necessary for to make the JPEG/MPEG1,2,4 decompression in hardware ?


@+
Yannoo

ZbuffeR
12-01-2009, 05:28 PM
Yann, your question has nothing to do in this thread...
Create your own next time, or find a better related thread.
http://en.wikipedia.org/wiki/VaAPI
For nivida :
http://en.wikipedia.org/wiki/VDPAU
For AMD :
http://en.wikipedia.org/wiki/X-Video_Bitstream_Acceleration

Yann LE PETITCORPS
12-01-2009, 05:29 PM
Perfect, thanks

My first idea is that if exist an API that can handle M(J)PEG motion, I think that it can easily handle JPEG file too :)

@+
Yannoo