Archived

This topic is now archived and is closed to further replies.

JPEG texturing, I need help!!!

This topic is 5618 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m writing something that requires me to load up a .jpg image and use it as a texture... I have been unable to find any good libraries to do this, and I really need some help.. So, if anyone would be so kind as to maybe post some code, or point me to a website that I might have missed, I would be very thankful... -Fredric

Share this post


Link to post
Share on other sites
I''ve taken a look at Devil, and it didn''t worked a lot less then when I tried to load up the jpg with a tga loader...

Perhaps a jpg->bmp converter would be a good idea, anyone know a good library for that?

-Fredric

Share this post


Link to post
Share on other sites
quote:
Original post by FDGaming
I''ve taken a look at Devil, and it didn''t worked a lot less then when I tried to load up the jpg with a tga loader...

Perhaps a jpg->bmp converter would be a good idea, anyone know a good library for that?

-Fredric




DevIL is IMO the best image library out there. Perhaps you should explain your problems with it, because chances are, it is not working well because of something you are doing wrong.

As for a jpg->bmp conversion, you can use just about any image manipulation program for that. The GIMP is an excellent open source program that lets you work with a wide range of image formats, and even Paint lets you load and save .jpg files (load up your JPG, then Save As a bitmap).

Share this post


Link to post
Share on other sites
Ok, here''s the code (btw, it''s not my work - tnx to Snake ):

-----------------------------------------------------------------
#include "jpeglib.h"
#pragma comment(lib, "jpeg.lib")

//***************** Prototypes ************************//

// function for loading .jpg
tImage *LoadJPG(const char *strFileName);

// for .jpg decoding and decompression
void DecodeJPG(jpeg_decompress_struct* cinfo, tImage*pImageData);



///////////////////////////////// DECODE JPG ///////////
/* here we decode .jpg */

void DecodeJPG(jpeg_decompress_struct* cinfo, tImage *pImageData)
{
// reads JPG''s header
jpeg_read_header(cinfo, TRUE);

// decompression
jpeg_start_decompress(cinfo);


pImageData->channels = cinfo->num_components;
pImageData->sizeX = cinfo->image_width;
pImageData->sizeY = cinfo->image_height;

int rowSpan = cinfo->image_width * cinfo->num_components;


pImageData->data = ((unsigned char*)malloc(sizeof(unsigned char)*rowSpan*pImageData->sizeY));

unsigned char** rowPtr = new unsigned char*[pImageData->sizeY];

for (int i = 0; i < pImageData->sizeY; i++)
rowPtr = &(pImageData->data[i * rowSpan]);

int rowsRead = 0;
while (cinfo->output_scanline < cinfo->output_height)
{
rowsRead += jpeg_read_scanlines(cinfo,
&rowPtr[rowsRead], cinfo->output_height - rowsRead);
}


// finish decompression
jpeg_finish_decompress(cinfo);
}


///////////////////////////////// LOAD JPG /////////////
/* here we load .jpg */


tImage *LoadJPG(const char *strFileName)
{
struct jpeg_decompress_struct cinfo;
tImage *pImageData = NULL;
FILE *pFile;


if((pFile = fopen(strFileName, "rb")) == NULL)
{
MessageBox(g_hWnd, "Loading Failed!", "Error", MB_OK);
return NULL;
}


jpeg_error_mgr jerr;

// Have our compression info object point to the error handler address
cinfo.err = jpeg_std_error(&jerr);


jpeg_create_decompress(&cinfo);


jpeg_stdio_src(&cinfo, pFile);


pImageData = (tImage*)malloc(sizeof(tImage));


DecodeJPG(&cinfo, pImageData);

jpeg_destroy_decompress(&cinfo);

fclose(pFile);

return pImageData;
}

Share this post


Link to post
Share on other sites