Jump to content

  • Log In with Google      Sign In   
  • Create Account


Jpeglib texture loading issue


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 FireViper   Members   -  Reputation: 129

Like
0Likes
Like

Posted 12 May 2012 - 05:25 PM

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


Sponsor:

#2 Sik_the_hedgehog   Crossbones+   -  Reputation: 1483

Like
0Likes
Like

Posted 12 May 2012 - 07:08 PM

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, 12 May 2012 - 07:10 PM.

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.

#3 FireViper   Members   -  Reputation: 129

Like
0Likes
Like

Posted 13 May 2012 - 11:24 AM

thanks for the reply, I swapped the rows and its working.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS