Jump to content
  • Advertisement
Sign in to follow this  
FireViper

OpenGL Jpeglib texture loading issue

This topic is 2286 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 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;
}

Share this post


Link to post
Share on other sites
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). Edited by Sik_the_hedgehog

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!