#174150 - SatNav - Thu May 20, 2010 3:34 pm
Hi, I'm using the jpeglib provided for devkitpro to load jpeg files at runtime, for use as background images in my Rubik's Cube sim game. After a few days hacking/blundering around, I've got some code that I think should work, but unfortunately doesn't. I was hoping that perhaps someone with some experience with jpeglib might be able to shed some light on my problem. Here is my code:
Basically, the program hangs after reading 8 lines of a 400x300 jpg image. It seems like it might be that I'm running out of memory, and crashing through some program code or something stupid. I'm not sure whether I'm allocating memory safely (or exactly how to, with that row_ptr array), so any tips in that regard are very welcome.
And I know someone is going to suggest headspins gba-jpeg thing. I'm considering it, but I'd rather apply a simple fix for my code (if one exists), than completely rewrite for a different library.
Cheers,
Mark
_________________
Cube-DS: A Kick-Ass Rubik's Cube Game for DS
http://cube-ds.googlecode.com
Code: |
bool loadJPG(char* filename)
{ FILE* fp; /* Open the prospective JPG file. */ if ((fp = fopen(filename, "rb")) == NULL) { return false; } else { struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; uint row_stride; u8* jpgData; /* Initialize the JPEG decompression object with default error handling. */ cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); /* Specify data source for decompression */ jpeg_stdio_src(&cinfo, fp); /* Read file header, set default decompression parameters */ (void) jpeg_read_header(&cinfo, TRUE); uint width = cinfo.output_width; uint height = cinfo.output_height; uint channels = cinfo.output_components; row_stride = width * channels; jpeg_start_decompress(&cinfo); printf("jpeg_start_decompress\n"); /* Allocate the image_data buffer. */ if ((jpgData = (u8*) malloc(row_stride * height))==NULL) { //png_destroy_read_struct(&png_ptr, &info_ptr, NULL); return false; } u8** row_ptr = new u8 * [height]; for(uint i=0; i<height; i++) row_ptr[i] = &jpgData[i*row_stride]; if(row_ptr[height-1]==NULL) return false; printf("allocated memory\n"); /* loop through file, creating image array */ uint lines_read=0; while (cinfo.output_scanline < cinfo.output_height) { lines_read += jpeg_read_scanlines(&cinfo, (JSAMPARRAY)&row_ptr[lines_read], 1); printf("%i\n", lines_read); } delete [] row_ptr; jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); printf("jpeg_finish_decompress\n"); backgroundTexData = new rgb[65536]; for(uint i=0; i< 65536; i++) { uint dx = i / 256; uint dy = i % 256; uint sx = dx * width / 256; uint sy = dy * height / 256; uint source = sy * width + sx; backgroundTexData[i]=RGB8(jpgData[source*channels], jpgData[source*channels+1], jpgData[source*channels+2]); printf("%i\n", backgroundTexData[i]); } free(jpgData); return true; } return false; } |
Basically, the program hangs after reading 8 lines of a 400x300 jpg image. It seems like it might be that I'm running out of memory, and crashing through some program code or something stupid. I'm not sure whether I'm allocating memory safely (or exactly how to, with that row_ptr array), so any tips in that regard are very welcome.
And I know someone is going to suggest headspins gba-jpeg thing. I'm considering it, but I'd rather apply a simple fix for my code (if one exists), than completely rewrite for a different library.
Cheers,
Mark
_________________
Cube-DS: A Kick-Ass Rubik's Cube Game for DS
http://cube-ds.googlecode.com