forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRageSurface_Save_PNG.cpp
164 lines (140 loc) · 4.89 KB
/
RageSurface_Save_PNG.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "global.h"
#include "RageSurface_Save_PNG.h"
#include "RageSurface.h"
#include "RageSurfaceUtils.h"
#include "RageFile.h"
#include "RageLog.h"
#include "RageUtil.h"
#if defined(_MSC_VER)
#include "../extern/libpng/include/png.h"
#if defined(_BINARY_PNG)
#pragma comment(lib, "libpng.lib")
#endif
#pragma warning(disable: 4611) /* interaction between '_setjmp' and C++ object destruction is non-portable */
#else
#include <png.h>
#endif // _MSC_VER
static void SafePngError( png_struct *pPng, const std::string &sStr )
{
/* png_error will call PNG_Error, which will longjmp. If we just pass
* GetError().c_str() to it, a temporary may be created; since control
* never returns, it may never be destructed and leak. */
static char error[256];
strncpy( error, sStr.c_str(), sizeof(error) );
error[sizeof(error)-1] = 0;
png_error( pPng, error );
}
static void RageFile_png_write( png_struct *pPng, png_byte *pData, png_size_t iSize )
{
RageFile *pFile = (RageFile *) png_get_io_ptr(pPng);
int iGot = pFile->Write( pData, iSize );
if( iGot == -1 )
SafePngError( pPng, pFile->GetError() );
}
static void RageFile_png_flush( png_struct *pPng )
{
RageFile *pFile = (RageFile *) png_get_io_ptr(pPng);
int iGot = pFile->Flush();
if( iGot == -1 )
SafePngError( pPng, pFile->GetError() );
}
struct error_info
{
char *szErr;
};
static void PNG_Error( png_struct *pPng, const char *szError )
{
error_info *pInfo = (error_info *) png_get_error_ptr(pPng);
strncpy( pInfo->szErr, szError, 1024 );
pInfo->szErr[1023] = 0;
longjmp( png_jmpbuf(pPng), 1 );
}
static void PNG_Warning( png_struct *png, const char *warning )
{
LOG->Trace( "saving PNG: warning: %s", warning );
}
/* Since libpng forces us to use longjmp, this function shouldn't create any C++
* objects, and needs to watch out for memleaks. */
static bool RageSurface_Save_PNG( RageFile &f, char szErrorbuf[1024], RageSurface *pImgIn )
{
bool bAlpha = pImgIn->format->Amask != 0;
RageSurface *pImg;
bool bDeleteImg = RageSurfaceUtils::ConvertSurface( pImgIn, pImg, pImgIn->w, pImgIn->h, 32,
Swap32BE( 0xFF000000 ),
Swap32BE( 0x00FF0000 ),
Swap32BE( 0x0000FF00 ),
Swap32BE( 0x000000FF ) );
if( !bDeleteImg )
pImg = pImgIn;
error_info error;
error.szErr = szErrorbuf;
png_struct *pPng = png_create_write_struct( PNG_LIBPNG_VER_STRING, &error, PNG_Error, PNG_Warning );
if( pPng == nullptr )
{
sprintf( szErrorbuf, "creating png_create_write_struct failed");
return false;
}
png_info *pInfo = png_create_info_struct(pPng);
if( pInfo == nullptr )
{
png_destroy_write_struct( &pPng, nullptr );
if( bDeleteImg )
delete pImg;
sprintf( szErrorbuf, "creating png_create_info_struct failed");
return false;
}
if( setjmp(png_jmpbuf(pPng)) )
{
png_destroy_write_struct( &pPng, &pInfo );
return false;
}
png_set_write_fn( pPng, &f, RageFile_png_write, RageFile_png_flush );
png_set_compression_level( pPng, 1 );
png_set_IHDR( pPng, pInfo, pImg->w, pImg->h, 8, PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE );
png_write_info( pPng, pInfo );
png_set_filler( pPng, 0, PNG_FILLER_AFTER );
png_byte *pixels = (png_byte *) pImg->pixels;
for( int y = 0; y < pImg->h; y++ )
png_write_row( pPng, pixels + pImg->pitch*y );
png_write_end( pPng, pInfo );
png_destroy_write_struct( &pPng, &pInfo );
/* Free the converted image. */
if( bDeleteImg )
delete pImg;
return true;
}
bool RageSurfaceUtils::SavePNG( RageSurface *pImg, RageFile &f, std::string &sError )
{
char szErrorBuf[1024];
if( !RageSurface_Save_PNG(f, szErrorBuf, pImg) )
{
sError = szErrorBuf;
return false;
}
return true;
}
/*
* (c) 2004-2006 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.
*/