Reformat the JPEG loader

This commit is contained in:
Yamagi Burmeister 2012-03-12 09:21:19 +00:00
parent 7db2d76833
commit 1cc06e2841

View file

@ -27,63 +27,52 @@
#include "../header/local.h"
#include <jpeglib.h>
/*
=================================================================
JPEG LOADING
NiceAss: Code from Q2Ice
=================================================================
*/
void jpg_null(j_decompress_ptr cinfo)
{
}
boolean jpg_fill_input_buffer(j_decompress_ptr cinfo)
{
ri.Con_Printf(PRINT_ALL, "Premature end of JPEG data\n");
return 1;
}
void jpg_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
{
cinfo->src->next_input_byte += (size_t) num_bytes;
cinfo->src->bytes_in_buffer -= (size_t) num_bytes;
}
void jpeg_mem_src (j_decompress_ptr cinfo, unsigned char *mem, unsigned long len)
{
cinfo->src = (struct jpeg_source_mgr *)(*cinfo->mem->alloc_small)((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(struct jpeg_source_mgr));
cinfo->src->init_source = jpg_null;
cinfo->src->fill_input_buffer = jpg_fill_input_buffer;
cinfo->src->skip_input_data = jpg_skip_input_data;
cinfo->src->resync_to_restart = jpeg_resync_to_restart;
cinfo->src->term_source = jpg_null;
cinfo->src->bytes_in_buffer = len;
cinfo->src->next_input_byte = mem;
}
/*
==============
LoadJPG
==============
*/
void
LoadJPG (char *origname, byte **pic, int *width, int *height)
jpg_null(j_decompress_ptr cinfo)
{
struct jpeg_decompress_struct cinfo;
char filename[256];
struct jpeg_error_mgr jerr;
int len;
byte *rawdata, *rgbadata, *scanline, *p, *q;
unsigned int rawsize, i;
}
boolean
jpg_fill_input_buffer(j_decompress_ptr cinfo)
{
ri.Con_Printf(PRINT_ALL, "Premature end of JPEG data\n");
return 1;
}
void
jpg_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
{
cinfo->src->next_input_byte += (size_t)num_bytes;
cinfo->src->bytes_in_buffer -= (size_t)num_bytes;
}
void
jpeg_mem_src(j_decompress_ptr cinfo, unsigned char *mem, unsigned long len)
{
cinfo->src = (struct jpeg_source_mgr *)(*cinfo->mem->alloc_small)((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(struct jpeg_source_mgr));
cinfo->src->init_source = jpg_null;
cinfo->src->fill_input_buffer = jpg_fill_input_buffer;
cinfo->src->skip_input_data = jpg_skip_input_data;
cinfo->src->resync_to_restart = jpeg_resync_to_restart;
cinfo->src->term_source = jpg_null;
cinfo->src->bytes_in_buffer = len;
cinfo->src->next_input_byte = mem;
}
void
LoadJPG(char *origname, byte **pic, int *width, int *height)
{
struct jpeg_decompress_struct cinfo;
char filename[256];
struct jpeg_error_mgr jerr;
int len;
byte *rawdata, *rgbadata, *scanline, *p, *q;
unsigned int rawsize, i;
/* Add the extension */
len = strlen( origname );
len = strlen(origname);
if ( strcmp( origname + len - 4, ".jpg" ) )
if (strcmp(origname + len - 4, ".jpg"))
{
strncpy(filename, origname, 256);
strncat(filename, ".jpg", 255);
@ -95,26 +84,28 @@ LoadJPG (char *origname, byte **pic, int *width, int *height)
*pic = NULL;
// Load JPEG file into memory
/* Load JPEG file into memory */
rawsize = ri.FS_LoadFile(filename, (void **)&rawdata);
if (!rawdata)
return;
if (rawsize < 10 || rawdata[6] != 'J' || rawdata[7] != 'F' || rawdata[8] != 'I' || rawdata[9] != 'F')
{
ri.Con_Printf (PRINT_ALL, "Invalid JPEG header: %s\n", filename);
return;
}
if ((rawsize < 10) || (rawdata[6] != 'J') || (rawdata[7] != 'F') || (rawdata[8] != 'I') || (rawdata[9] != 'F'))
{
ri.Con_Printf(PRINT_ALL, "Invalid JPEG header: %s\n", filename);
ri.FS_FreeFile(rawdata);
return;
}
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_mem_src(&cinfo, (unsigned char *) rawdata, (unsigned long) rawsize);
jpeg_mem_src(&cinfo, (unsigned char *)rawdata, (unsigned long)rawsize);
jpeg_read_header(&cinfo, true);
jpeg_start_decompress(&cinfo);
if(cinfo.output_components != 3 && cinfo.output_components != 4)
if ((cinfo.output_components != 3) && (cinfo.output_components != 4))
{
ri.Con_Printf(PRINT_ALL, "Invalid JPEG colour components\n");
jpeg_destroy_decompress(&cinfo);
@ -122,9 +113,10 @@ LoadJPG (char *origname, byte **pic, int *width, int *height)
return;
}
// Allocate Memory for decompressed image
/* Allocate Memory for decompressed image */
rgbadata = malloc(cinfo.output_width * cinfo.output_height * 4);
if(!rgbadata)
if (!rgbadata)
{
ri.Con_Printf(PRINT_ALL, "Insufficient memory for JPEG buffer\n");
jpeg_destroy_decompress(&cinfo);
@ -132,23 +124,25 @@ LoadJPG (char *origname, byte **pic, int *width, int *height)
return;
}
// Pass sizes to output
/* Pass sizes to output */
*width = cinfo.output_width;
*height = cinfo.output_height;
// Allocate Scanline buffer
scanline = malloc (cinfo.output_width * 3);
/* Allocate Scanline buffer */
scanline = malloc(cinfo.output_width * 3);
if (!scanline)
{
ri.Con_Printf (PRINT_ALL, "Insufficient memory for JPEG scanline buffer\n");
free (rgbadata);
jpeg_destroy_decompress (&cinfo);
ri.FS_FreeFile (rawdata);
ri.Con_Printf(PRINT_ALL, "Insufficient memory for JPEG scanline buffer\n");
free(rgbadata);
jpeg_destroy_decompress(&cinfo);
ri.FS_FreeFile(rawdata);
return;
}
// Read Scanlines, and expand from RGB to RGBA
/* Read Scanlines, and expand from RGB to RGBA */
q = rgbadata;
while (cinfo.output_scanline < cinfo.output_height)
{
p = scanline;
@ -165,9 +159,9 @@ LoadJPG (char *origname, byte **pic, int *width, int *height)
}
}
free (scanline);
jpeg_finish_decompress (&cinfo);
jpeg_destroy_decompress (&cinfo);
free(scanline);
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
*pic = rgbadata;
}