Loading JPEG code not working [fixed]

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
User avatar
JoshDB
Posts: 87
Joined: Wed Oct 05, 2005 3:54 am

Loading JPEG code not working [fixed]

Post by JoshDB »

Hey guys, I've installed the JPEG lib from the svn and also pulled down a sample bit of code that loads JPEGs. Can you tell me what's wrong with this picture?

When I try to compile this, it gives me the error:
graphics.c: In function 'loadImageJPEG':
graphics.c:533: error: 'for' loop initial declaration used outside C99 mode
graphics.c:543: error: 'for' loop initial declaration used outside C99 mode
make: *** [graphics.o] Error 1

Code: Select all

#include <stdlib.h>
#include <malloc.h>
#include <pspdisplay.h>
#include <psputils.h>
#include <png.h>
#include <pspgu.h>

#include <jpeglib.h>
#include <jerror.h>

#include "graphics.h"
#include "framebuffer.h"


// Other code, none related...

Image* loadImageJPEG&#40;const char* filename&#41;
&#123;
        struct jpeg_decompress_struct dinfo;
        struct jpeg_error_mgr jerr;
        dinfo.err = jpeg_std_error&#40;&jerr&#41;;
        jpeg_create_decompress&#40;&dinfo&#41;;
        FILE* inFile = fopen&#40;filename, "rb"&#41;;
        if &#40;!inFile&#41; &#123;
                jpeg_destroy_decompress&#40;&dinfo&#41;;
                return NULL;
        &#125;
        jpeg_stdio_src&#40;&dinfo, inFile&#41;;
        jpeg_read_header&#40;&dinfo, TRUE&#41;;
        int width = dinfo.image_width;
        int height = dinfo.image_height;
        jpeg_start_decompress&#40;&dinfo&#41;;
        Image* image = &#40;Image*&#41; malloc&#40;sizeof&#40;Image&#41;&#41;;
        if &#40;!image&#41; &#123;
                jpeg_destroy_decompress&#40;&dinfo&#41;;
                return NULL;
        &#125;
        image->imageWidth = width;
        image->imageHeight = height;
        image->textureWidth = getNextPower2&#40;width&#41;;
        image->textureHeight = getNextPower2&#40;height&#41;;
        image->data = &#40;Color*&#41; memalign&#40;16, image->textureWidth * image->textureHeight * sizeof&#40;Color&#41;&#41;;
        u8* line = &#40;u8*&#41; malloc&#40;width * 3&#41;;
        if &#40;!line&#41; &#123;
                jpeg_destroy_decompress&#40;&dinfo&#41;;
                return NULL;
        &#125;
        if &#40;dinfo.jpeg_color_space == JCS_GRAYSCALE&#41; &#123;
                while &#40;dinfo.output_scanline < dinfo.output_height&#41; &#123;
                        int y = dinfo.output_scanline;
                        jpeg_read_scanlines&#40;&dinfo, &line, 1&#41;;
                        for &#40;int x = 0; x < width; x++&#41; &#123; // <-- Line 533
                                Color c = line&#91;x&#93;;
                                image->data&#91;x + image->textureWidth * y&#93; = c | &#40;c << 8&#41; | &#40;c << 16&#41; | 0xff000000;;
                        &#125;
                &#125;
        &#125; else &#123;
                while &#40;dinfo.output_scanline < dinfo.output_height&#41; &#123;
                        int y = dinfo.output_scanline;
                        jpeg_read_scanlines&#40;&dinfo, &line, 1&#41;;
                        u8* linePointer = line;
                        for &#40;int x = 0; x < width; x++&#41; &#123; // <-- Line 543
                                Color c = *&#40;linePointer++&#41;;
                                c |= &#40;*&#40;linePointer++&#41;&#41; << 8;
                                c |= &#40;*&#40;linePointer++&#41;&#41; << 16;
                                image->data&#91;x + image->textureWidth * y&#93; = c | 0xff000000;
                        &#125;
                &#125;
        &#125;
        jpeg_finish_decompress&#40;&dinfo&#41;;
        jpeg_destroy_decompress&#40;&dinfo&#41;;
        free&#40;line&#41;;
        return image;
&#125;
Last edited by JoshDB on Mon Jan 02, 2006 6:52 am, edited 1 time in total.
AnonymousTipster
Posts: 197
Joined: Fri Jul 01, 2005 2:50 am

Post by AnonymousTipster »

Where you have
for(int x =0; ....)
Change it to
int x=0;
for(x=0;......

You can't declare a variable inside a for() clause with GCC.
User avatar
JoshDB
Posts: 87
Joined: Wed Oct 05, 2005 3:54 am

Post by JoshDB »

Wow, thanks, it worked. Really fast response, just went to go search for 'loading JPEGs' and you responded ( :

However, although that error is gone, now I get a few others:

Image

I'm guessing these errors have something to do with the included files from the JPEG library?

Edit: Actually, could it be something in the Makefile? Perhaps not referencing the lib?
User avatar
JoshDB
Posts: 87
Joined: Wed Oct 05, 2005 3:54 am

Post by JoshDB »

Woot!

Alright, I added
LIBS = -lpspgu -lpng -lz -lm -ljpeg
To the Makefile, and it works.
Post Reply