RageSurface_Load_JPEG

This commit is contained in:
Glenn Maynard
2004-05-18 04:46:21 +00:00
parent b59e1cce88
commit d6482a88df
5 changed files with 325 additions and 26 deletions
+3
View File
@@ -1,6 +1,7 @@
#include "global.h"
#include "RageSurface_Load.h"
#include "RageSurface_Load_PNG.h"
#include "RageSurface_Load_JPEG.h"
#include "RageSurface_Load_GIF.h"
#include "RageUtil.h"
#include "SDL_image.h"
@@ -15,6 +16,8 @@ SDL_Surface *RageSurface::LoadFile( const CString &sPath )
ret = RageSurface_Load_PNG( sPath, error );
else if( !ext.CompareNoCase("gif") )
ret = RageSurface_Load_GIF( sPath, error );
else if( !ext.CompareNoCase("jpg") )
ret = RageSurface_Load_JPEG( sPath, error );
else
{
SDL_RWops *rw = OpenRWops( sPath );
+239
View File
@@ -0,0 +1,239 @@
#include "global.h"
#include "RageSurface_Load_JPEG.h"
#include "RageUtil.h"
#include "RageLog.h"
#include "SDL_utils.h"
#include "SDL_endian.h"
#include "RageFile.h"
#include <setjmp.h>
#if defined(WIN32)
/* work around namespace bugs in win32/libjpeg: */
#define XMD_H
#undef FAR
#include "libjpeg/jpeglib.h"
#include "libjpeg/jerror.h"
#pragma comment(lib, "libjpeg/jpeg.lib")
#pragma warning(disable: 4611) /* interaction between '_setjmp' and C++ object destruction is non-portable */
#else
#include <jpeglib.h>
#include <jerror.h>
#endif
struct my_jpeg_error_mgr
{
struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf setjmp_buffer; /* for return to caller */
char errorbuf[JMSG_LENGTH_MAX];
};
void my_output_message( j_common_ptr cinfo )
{
my_jpeg_error_mgr *myerr = (my_jpeg_error_mgr *) cinfo->err;
(*cinfo->err->format_message)( cinfo, myerr->errorbuf );
}
void my_error_exit( j_common_ptr cinfo )
{
my_jpeg_error_mgr *myerr = (my_jpeg_error_mgr *) cinfo->err;
(*cinfo->err->output_message)(cinfo);
longjmp( myerr->setjmp_buffer, 1 );
}
struct RageFile_source_mgr
{
struct jpeg_source_mgr pub; /* public fields */
RageFile *file; /* source stream */
JOCTET buffer[1024*4];
bool start_of_file; /* have we gotten any data yet? */
};
void RageFile_JPEG_init_source( j_decompress_ptr cinfo )
{
RageFile_source_mgr *src = (RageFile_source_mgr *) cinfo->src;
src->start_of_file = true;
src->pub.next_input_byte = NULL;
src->pub.bytes_in_buffer = 0;
}
boolean RageFile_JPEG_fill_input_buffer( j_decompress_ptr cinfo )
{
RageFile_source_mgr *src = (RageFile_source_mgr *) cinfo->src;
size_t nbytes = src->file->Read( src->buffer, sizeof(src->buffer) );
if( nbytes <= 0 )
{
if( src->start_of_file ) /* Treat empty input file as fatal error */
ERREXIT( cinfo, JERR_INPUT_EMPTY );
WARNMS( cinfo, JWRN_JPEG_EOF );
/* Insert a fake EOI marker */
src->buffer[0] = (JOCTET) 0xFF;
src->buffer[1] = (JOCTET) JPEG_EOI;
nbytes = 2;
}
src->pub.next_input_byte = src->buffer;
src->pub.bytes_in_buffer = nbytes;
src->start_of_file = FALSE;
return TRUE;
}
void RageFile_JPEG_skip_input_data( j_decompress_ptr cinfo, long num_bytes )
{
RageFile_source_mgr *src = (RageFile_source_mgr *) cinfo->src;
int in_buffer = min( (long) src->pub.bytes_in_buffer, num_bytes );
src->pub.next_input_byte += in_buffer;
src->pub.bytes_in_buffer -= in_buffer;
if( num_bytes )
src->file->SeekCur( num_bytes );
}
void RageFile_JPEG_term_source( j_decompress_ptr cinfo )
{
}
static SDL_Surface *RageSurface_Load_JPEG( RageFile *f, const char *fn, char errorbuf[JMSG_LENGTH_MAX] )
{
struct jpeg_decompress_struct cinfo;
struct my_jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;
jerr.pub.output_message = my_output_message;
SDL_Surface *img = NULL;
if( setjmp(jerr.setjmp_buffer) )
{
my_jpeg_error_mgr *myerr = (my_jpeg_error_mgr *) cinfo.err;
memcpy( errorbuf, myerr->errorbuf, JMSG_LENGTH_MAX );
jpeg_destroy_decompress( &cinfo );
if( img )
SDL_FreeSurface( img );
return NULL;
}
/* Now we can initialize the JPEG decompression object. */
jpeg_create_decompress( &cinfo );
/* Step 2: specify data source (eg, a file) */
RageFile_source_mgr RageFileJpegSource;
RageFileJpegSource.pub.init_source = RageFile_JPEG_init_source;
RageFileJpegSource.pub.fill_input_buffer = RageFile_JPEG_fill_input_buffer;
RageFileJpegSource.pub.skip_input_data = RageFile_JPEG_skip_input_data;
RageFileJpegSource.pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
RageFileJpegSource.pub.term_source = RageFile_JPEG_term_source;
RageFileJpegSource.file = f;
cinfo.src = (jpeg_source_mgr *) &RageFileJpegSource;
jpeg_read_header( &cinfo, TRUE );
switch( cinfo.jpeg_color_space )
{
case JCS_GRAYSCALE:
cinfo.out_color_space = JCS_GRAYSCALE;
break;
case JCS_YCCK:
case JCS_CMYK:
sprintf( errorbuf, "Color format \"%s\" not supported", cinfo.jpeg_color_space == JCS_YCCK? "YCCK":"CMYK" );
jpeg_destroy_decompress( &cinfo );
return NULL;
default:
cinfo.out_color_space = JCS_RGB;
break;
}
jpeg_start_decompress( &cinfo );
if( cinfo.out_color_space == JCS_GRAYSCALE )
{
img = SDL_CreateRGBSurfaceSane( SDL_SWSURFACE, cinfo.output_width, cinfo.output_height, 8, 0, 0, 0, 0 );
for( int i = 0; i < 256; ++i )
{
SDL_Color color;
color.r = color.g = color.b = (int8_t) i;
color.unused = 0xFF;
mySDL_SetPalette( img, &color, i, 1 );
}
} else {
img = SDL_CreateRGBSurfaceSane( SDL_SWSURFACE, cinfo.output_width, cinfo.output_height, 24,
mySDL_SwapBE24( 0xFF0000 ),
mySDL_SwapBE24( 0x00FF00 ),
mySDL_SwapBE24( 0x0000FF ),
mySDL_SwapBE24( 0x000000 ) );
}
while( cinfo.output_scanline < cinfo.output_height )
{
JSAMPROW p = (JSAMPROW) img->pixels;
p += cinfo.output_scanline * img->pitch;
jpeg_read_scanlines(&cinfo, &p, 1);
}
jpeg_finish_decompress( &cinfo );
jpeg_destroy_decompress( &cinfo );
return img;
}
SDL_Surface *RageSurface_Load_JPEG( const CString &sPath, CString &error )
{
RageFile f;
if( !f.Open( sPath ) )
{
error = f.GetError();
return NULL;
}
char errorbuf[1024];
SDL_Surface *ret = RageSurface_Load_JPEG( &f, sPath, errorbuf );
if( ret == NULL )
{
error = errorbuf;
return ret;
}
return ret;
}
/*
* (c) 2004 Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
+32
View File
@@ -0,0 +1,32 @@
#ifndef RAGE_SURFACE_LOAD_JPEG_H
#define RAGE_SURFACE_LOAD_JPEG_H
struct SDL_Surface;
SDL_Surface *RageSurface_Load_JPEG( const CString &sPath, CString &error );
#endif
/*
* (c) 2004 Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
+45 -26
View File
@@ -65,7 +65,7 @@ IntDir=.\../Debug6
TargetDir=\temp\stepmania\Program
TargetName=StepMania-debug
SOURCE="$(InputPath)"
PreLink_Cmds=disasm\verinc cl /Zl /nologo /c verstub.cpp /Fo$(IntDir)\
PreLink_Cmds=disasm\verinc cl /Zl /nologo /c verstub.cpp /Fo$(IntDir)\
PostBuild_Cmds=disasm\mapconv $(IntDir)\$(TargetName).map $(TargetDir)\StepMania.vdi
# End Special Build Tool
@@ -82,22 +82,22 @@ PostBuild_Cmds=disasm\mapconv $(IntDir)\$(TargetName).map $(TargetDir)\StepMania
# PROP Intermediate_Dir "../Debug_Xbox"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
CPP=cl.exe
# ADD BASE CPP /nologo /W3 /Gm /GX /Zi /Od /D "WIN32" /D "_XBOX" /D "_DEBUG" /YX /FD /G6 /Ztmp /c
# ADD CPP /nologo /W3 /Gm /GX /Zi /Od /I "." /I "SDL-1.2.5\include" /I "SDL_image-1.2" /I "plib-1.6.0" /I "SDL_sound-1.0.0" /I "vorbis" /D "WIN32" /D "_DEBUG" /D "_XBOX" /D "OGG_ONLY" /D "DEBUG" /FR /YX"global.h" /FD /G6 /Ztmp /c
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
XBCP=xbecopy.exe
# ADD BASE XBCP /NOLOGO
# ADD XBCP /NOLOGO
XBE=imagebld.exe
# ADD BASE XBE /nologo /stack:0x10000 /debug
# ADD XBE /nologo /stack:0x10000 /debug /out:"../default.xbe"
LINK32=link.exe
# ADD BASE LINK32 xapilibd.lib d3d8d.lib d3dx8d.lib xgraphicsd.lib dsoundd.lib dmusicd.lib xnetd.lib xboxkrnl.lib /nologo /incremental:no /debug /machine:I386 /subsystem:xbox /fixed:no /TMP
# ADD LINK32 $(intdir)\verstub.obj xapilibd.lib d3d8d.lib d3dx8d.lib xgraphicsd.lib dsoundd.lib dmusicd.lib xnetd.lib xboxkrnl.lib /nologo /map /debug /machine:I386 /nodefaultlib:"libcd" /nodefaultlib:"libcmt" /out:"../StepManiaXbox-debug.exe" /subsystem:xbox /fixed:no /TMP
# SUBTRACT LINK32 /incremental:no
XBE=imagebld.exe
# ADD BASE XBE /nologo /stack:0x10000 /debug
# ADD XBE /nologo /stack:0x10000 /debug /out:"../default.xbe"
XBCP=xbecopy.exe
# ADD BASE XBCP /NOLOGO
# ADD XBCP /NOLOGO
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
CPP=cl.exe
# ADD BASE CPP /nologo /W3 /Gm /GX /Zi /Od /D "WIN32" /D "_XBOX" /D "_DEBUG" /YX /FD /G6 /Ztmp /c
# ADD CPP /nologo /W3 /Gm /GX /Zi /Od /I "." /I "SDL-1.2.5\include" /I "SDL_image-1.2" /I "plib-1.6.0" /I "SDL_sound-1.0.0" /I "vorbis" /D "WIN32" /D "_DEBUG" /D "_XBOX" /D "OGG_ONLY" /D "DEBUG" /FR /YX"global.h" /FD /G6 /Ztmp /c
# Begin Special Build Tool
PreLink_Cmds=disasm\verinc cl /Zl /nologo /c PostBuild_Cmds=disasm\mapconv $(IntDir)\$(TargetName).map $(TargetDir)\StepMania.vdi ia32.vdi
# End Special Build Tool
@@ -138,7 +138,7 @@ IntDir=.\../Release6
TargetDir=\temp\stepmania\Program
TargetName=StepMania
SOURCE="$(InputPath)"
PreLink_Cmds=disasm\verinc cl /Zl /nologo /c verstub.cpp /Fo$(IntDir)\
PreLink_Cmds=disasm\verinc cl /Zl /nologo /c verstub.cpp /Fo$(IntDir)\
PostBuild_Cmds=disasm\mapconv $(IntDir)\$(TargetName).map $(TargetDir)\StepMania.vdi
# End Special Build Tool
@@ -156,23 +156,23 @@ PostBuild_Cmds=disasm\mapconv $(IntDir)\$(TargetName).map $(TargetDir)\StepMania
# PROP Intermediate_Dir "../Release_Xbox"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
CPP=cl.exe
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /I "." /I "SDL-1.2.5\include" /I "SDL_image-1.2" /I "plib-1.6.0" /D "WIN32" /D "_XBOX" /D "_DEBUG" /Fr /YX"global.h" /FD /c
# ADD CPP /nologo /MT /W3 /GX /O2 /I "." /I "SDL-1.2.5\include" /I "SDL_image-1.2" /I "plib-1.6.0" /I "SDL_sound-1.0.0" /I "vorbis" /D "WIN32" /D "NDEBUG" /D "_XBOX" /FR /YX /FD /c
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
XBCP=xbecopy.exe
# ADD BASE XBCP /NOLOGO
# ADD XBCP /NOLOGO
XBE=imagebld.exe
# ADD BASE XBE /nologo /stack:0x10000 /debug
# ADD XBE /nologo /testid:"123456" /stack:0x10000 /debug /out:"../default.xbe"
LINK32=link.exe
# ADD BASE LINK32 $(intdir)\verstub.obj xapilibd.lib d3d8d.lib d3dx8d.lib xgraphicsd.lib dsoundd.lib dmusicd.lib xnetd.lib xboxkrnl.lib /nologo /pdb:"../debug6/StepMania-debug.pdb" /map /debug /machine:IX86 /nodefaultlib:"libcmtd.lib" /out:"../StepMania-debug.exe"
# SUBTRACT BASE LINK32 /verbose /profile /pdb:none /incremental:no /nodefaultlib
# ADD LINK32 $(intdir)\verstub.obj xapilib.lib d3d8.lib d3dx8.lib xgraphics.lib dsound.lib dmusic.lib xnet.lib xboxkrnl.lib libcmt.lib /nologo /incremental:no /map /machine:I386 /nodefaultlib:"libc" /nodefaultlib:"libcmtd" /out:"../StepManiaXbox.exe" /subsystem:xbox /fixed:no /TMP /OPT:REF
# SUBTRACT LINK32 /pdb:none /debug
XBE=imagebld.exe
# ADD BASE XBE /nologo /stack:0x10000 /debug
# ADD XBE /nologo /testid:"123456" /stack:0x10000 /debug /out:"../default.xbe"
XBCP=xbecopy.exe
# ADD BASE XBCP /NOLOGO
# ADD XBCP /NOLOGO
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
CPP=cl.exe
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /I "." /I "SDL-1.2.5\include" /I "SDL_image-1.2" /I "plib-1.6.0" /D "WIN32" /D "_XBOX" /D "_DEBUG" /Fr /YX"global.h" /FD /c
# ADD CPP /nologo /MT /W3 /GX /O2 /I "." /I "SDL-1.2.5\include" /I "SDL_image-1.2" /I "plib-1.6.0" /I "SDL_sound-1.0.0" /I "vorbis" /D "WIN32" /D "NDEBUG" /D "_XBOX" /FR /YX /FD /c
# Begin Special Build Tool
PreLink_Cmds=disasm\verinc cl /Zl /nologo /c PostBuild_Cmds=disasm\mapconv $(IntDir)\$(TargetName).map $(TargetDir)\StepMania.vdi ia32.vdi
# End Special Build Tool
@@ -963,6 +963,25 @@ SOURCE=.\RageSurface_Load_GIF.h
# End Source File
# Begin Source File
SOURCE=.\RageSurface_Load_JPEG.cpp
!IF "$(CFG)" == "StepMania - Win32 Debug"
!ELSEIF "$(CFG)" == "StepMania - Xbox Debug"
!ELSEIF "$(CFG)" == "StepMania - Win32 Release"
!ELSEIF "$(CFG)" == "StepMania - Xbox Release"
!ENDIF
# End Source File
# Begin Source File
SOURCE=.\RageSurface_Load_JPEG.h
# End Source File
# Begin Source File
SOURCE=.\RageSurface_Load_PNG.cpp
!IF "$(CFG)" == "StepMania - Win32 Debug"
+6
View File
@@ -1899,6 +1899,12 @@ cl /Zl /nologo /c verstub.cpp /Fo&quot;$(IntDir)&quot;\
<File
RelativePath="RageSurface_Load_GIF.h">
</File>
<File
RelativePath="RageSurface_Load_JPEG.cpp">
</File>
<File
RelativePath="RageSurface_Load_JPEG.h">
</File>
<File
RelativePath="RageSurface_Load_PNG.cpp">
</File>