Jpeglib texture loading issue

Started by
1 comment, last by FireViper 11 years, 11 months ago
I'm trying to load jpg files using jpeglib, and found this example (http://www.efkhoury....ing-ijg-libjpeg)
I got it to work but the textures are being rendered incorrectly. I have to open them using mspaint, rotate them 180 degrees and flip them horizontally, in order for them to render correctly. I noticed the same thing happened when I tried to load png files. After searching google, I found out I have to swap the rows of the texture buffer before loading it in to opengl. However, I couldn't to find any code (or pseudo code) on it. If someone could tell me how to do this, it would be great.

This is the function I'm using to load the texture:

unsigned char* LoadJPG(const string& filename, int& width,int& height, int& nChannel)
{
unsigned char * big_buff;
struct jpeg_decompress_struct cinfo;
struct my_error_mgr jerr;

FILE * infile;
JSAMPARRAY buffer;
int row_stride;

if ((infile = fopen(filename.data(), "rb")) == NULL)
return NULL;

cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;

if (setjmp(jerr.setjmp_buffer))
{
jpeg_destroy_decompress(&cinfo);
fclose(infile);
return NULL;
}

jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, infile);

(void) jpeg_read_header(&cinfo, TRUE);
(void) jpeg_start_decompress(&cinfo);
row_stride = cinfo.output_width * cinfo.output_components;

buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
big_buff = (unsigned char*)malloc(cinfo.output_height * cinfo.output_width * cinfo.output_components);

while (cinfo.output_scanline < cinfo.output_height)
{
JDIMENSION read_now = jpeg_read_scanlines(&cinfo, buffer, 1);
memcpy(&big_buff[(cinfo.output_scanline - read_now) * cinfo.output_width * cinfo.output_components], buffer[0], row_stride);
}

width = cinfo.output_width;
height = cinfo.output_height;
nChannel = cinfo.num_components;

(void) jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(infile);
return big_buff;
}
Advertisement
1) Flipping vertically does exactly the same thing.
2) How are you specifying the texture coordinates? (for all we know you could have been swapping the vertical axis in the coordinates)

EDIT: also, if you want to continue with your idea... you just need to store rows from bottom to top when reading the scanlines (i.e. instead of starting from the first line in the texture, start from the last).
Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.
thanks for the reply, I swapped the rows and its working.

This topic is closed to new replies.

Advertisement