0
101 Nov 17, 2003 at 16:11

hi again,
i load a jpeg to a quad and its upsidedown how do i fix it
its not my code :

///////////////////////////////// DECODE JPG \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////   This decodes the jpeg and fills in the tImageJPG structure
///////////////////////////////// DECODE JPG \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void CMapDoc::DecodeJPG(jpeg_decompress_struct* cinfo, ImageDataRec *pImageData)
{

// Start to decompress the jpeg file with our compression info
jpeg_start_decompress(cinfo);

// Get the image dimensions and row span to read in the pixel data
pImageData->rowSpan = cinfo->image_width * cinfo->num_components;
pImageData->sizeX  = cinfo->image_width;
pImageData->sizeY  = cinfo->image_height;

// Allocate memory for the pixel buffer
pImageData->data = new unsigned char[pImageData->rowSpan * pImageData->sizeY];

// Here we use the library's state variable cinfo.output_scanline as the
// loop counter, so that we don't have to keep track ourselves.

// Create an array of row pointers
unsigned char** rowPtr = new unsigned char*[pImageData->sizeY];
for (int i = 0; i < pImageData->sizeY; i++)
rowPtr[i] = &(pImageData->data[i*pImageData->rowSpan]);

// Now comes the juice of our work, here we extract all the pixel data
while (cinfo->output_scanline < cinfo->output_height)
{
// Read in the current row of pixels and increase the rowsRead count
}

// Delete the temporary row pointers
delete [] rowPtr;

// Finish decompressing the data
jpeg_finish_decompress(cinfo);
}

/////   This loads the JPG file and returns it's data in a AUX_RGBImageRec struct
{
struct jpeg_decompress_struct cinfo;
ImageDataRec *pImageData = NULL;
FILE *pFile;

// This is the only function you should care about. You don't need to
// really know what all of this does (since you can't cause it's a library!) :)
// Just know that you need to pass in the jpeg file name, and get a pointer
// to a tImageJPG structure which contains the width, height and pixel data.
// Be sure to free the data after you are done with it, just like a bitmap.

// Open a file pointer to the jpeg file and check if it was found and opened
if((pFile = fopen(filename, "rb")) == NULL)
{
// Display an error message saying the file was not found, then return NULL
cout << "Unable to load JPG file" << endl;
return NULL;
}

// Create an error handler
jpeg_error_mgr jerr;

// Have our compression info object point to the error handler address
cinfo.err = jpeg_std_error(&jerr);

// Initialize the decompression object
jpeg_create_decompress(&cinfo);

// Specify the data source (Our file pointer)
jpeg_stdio_src(&cinfo, pFile);

// Allocate the structure that will hold our eventual jpeg data (must free it!)
pImageData = (ImageDataRec*)malloc(sizeof(ImageDataRec));

// Decode the jpeg file and fill in the image data structure to pass back
DecodeJPG(&cinfo, pImageData);

// This releases all the stored memory for reading and decoding the jpeg
jpeg_destroy_decompress(&cinfo);

// Close the file pointer that opened the file
fclose(pFile);

// Return the jpeg data (remember, you must free this data after you are done)
return pImageData;
}


3 Replies

0
101 Nov 17, 2003 at 16:18

to whom it may concern i solved it

    for (int i = pImageData->sizeY-1; i >= 0; i--)
rowPtr[pImageData->sizeY-1-i] = &(pImageData->data[i*pImageData->rowSpan]);

0
101 Nov 28, 2003 at 08:02

Wouldn’t it be so much easier to just change your texture coordinates :P

K.

0
101 Nov 28, 2003 at 11:47

if you support multiple image formats it’s more consistent to flip the image at load time