-
Notifications
You must be signed in to change notification settings - Fork 2
/
Utils.cpp
233 lines (198 loc) · 6.42 KB
/
Utils.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "stdafx.h"
#include "Utils.h"
Surface::Surface( const Surface &cpy )
{
iWidth = cpy.iWidth;
iHeight = cpy.iHeight;
iPitch = cpy.iPitch;
if( cpy.pRGBA )
{
pRGBA = new unsigned char[iHeight * iPitch];
}
else
{
pRGBA = nullptr;
}
}
void BitmapToSurface( HBITMAP hBitmap, Surface *pSurf )
{
const HDC hDC = CreateCompatibleDC(nullptr );
const HGDIOBJ hOldBitmap = SelectObject( hDC, hBitmap );
BITMAPINFO bi;
memset( &bi, 0, sizeof(bi) );
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
GetDIBits( hDC, hBitmap, 0, 0, nullptr, &bi, DIB_RGB_COLORS );
pSurf->iWidth = bi.bmiHeader.biWidth;
pSurf->iHeight = bi.bmiHeader.biHeight;
pSurf->iPitch = bi.bmiHeader.biWidth * 4;
pSurf->pRGBA = static_cast<unsigned char*>(new unsigned char[pSurf->iWidth * pSurf->iHeight * 4]);
bi.bmiHeader.biHeight = -bi.bmiHeader.biHeight;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biSizeImage = pSurf->iHeight * pSurf->iWidth * 4;
//LONG biXPelsPerMeter;
//LONG biYPelsPerMeter;
// DWORD biClrUsed;
// DWORD biClrImportant;
if( !GetDIBits(hDC, hBitmap, 0, pSurf->iHeight, pSurf->pRGBA, &bi, DIB_RGB_COLORS) )
{
int q = 1;
}
SelectObject( hDC, hOldBitmap );
DeleteDC( hDC );
}
void GrayScaleToAlpha( Surface *pSurf )//void *pImg, int iWidth, int iHeight, int iPitch )
{
char *p = reinterpret_cast<char*>(pSurf->pRGBA);
for( int iY = 0; iY < pSurf->iHeight; ++iY )
{
auto *const pos = reinterpret_cast<unsigned*>(p);
for( int iX = 0; iX < pSurf->iWidth; ++iX )
{
unsigned pixel = pos[iX];
pixel <<= 24;
pixel |= 0x00FFFFFF;
pos[iX] = pixel;
}
p += pSurf->iPitch;
}
}
void GetBounds( const Surface *pSurf, RECT *out )
{
out->left = out->right = out->top = out->bottom = 0;
int FirstCol = 9999, LastCol = 0, FirstRow = 9999, LastRow = 0;
const char *p = reinterpret_cast<const char*>(pSurf->pRGBA);
for( int row = 0; row < pSurf->iHeight; ++row )
{
const auto *pos = reinterpret_cast<const unsigned*>(p);
for( int col = 0; col < pSurf->iWidth; ++col )
{
const unsigned pixel = pos[col];
if( (pixel & 0xFFFFFF) == 0 )
continue; /* black */
FirstCol = min(FirstCol, col);
LastCol = max(LastCol, col);
FirstRow = min(FirstRow, row);
LastRow = max(LastRow, row);
}
p += pSurf->iPitch;
}
if( FirstCol != 9999 && FirstRow != 9999 )
{
out->left = FirstCol;
out->top = FirstRow;
out->right = LastCol+1;
out->bottom = LastRow+1;
}
}
#include "libpng/include/zlib.h"
#include "libpng/include/png.h"
//#include <cstdio>
#if defined(_MSC_VER)
#pragma comment(lib, "libpng/lib/libpng.lib")
#pragma warning(disable: 4611) /* interaction between '_setjmp' and C++ object destruction is non-portable */
#endif
static void File_png_write( png_struct *pPng, png_byte *pData, png_size_t iSize )
{
FILE *f = static_cast<FILE*>(pPng->io_ptr);
const size_t iGot = fwrite( pData, static_cast<int>(iSize), 1, f );
if( iGot == 0 )
png_error( pPng, strerror(errno) );
}
static void File_png_flush( png_struct *pPng )
{
FILE *f = static_cast<FILE*>(pPng->io_ptr);
const int iGot = fflush(f);
if( iGot == -1 )
png_error( pPng, strerror(errno) );
}
struct error_info
{
char *szErr;
};
static void PNG_Error( png_struct *pPng, const char *szError )
{
auto *pInfo = static_cast<error_info*>(pPng->error_ptr);
strncpy( pInfo->szErr, szError, 1024 );
pInfo->szErr[1023] = 0;
longjmp( pPng->jmpbuf, 1 );
}
static void PNG_Warning( png_struct *png, const char *warning )
{
}
extern "C" {
FILE* _iob = nullptr;
}
/* Since libpng forces us to use longjmp, this function shouldn't create any C++
* objects, and needs to watch out for memleaks. */
bool SavePNG( FILE *f, char szErrorbuf[1024], const Surface *pSurf )
{
/* RageSurfaceUtils::ConvertSurface( pImgIn, pImg, pImgIn->w, pImgIn->h, 32,
Swap32BE( 0xFF000000 ),
Swap32BE( 0x00FF0000 ),
Swap32BE( 0x0000FF00 ),
Swap32BE( 0x000000FF ) );
*/
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");
fprintf((FILE*)szErrorbuf, "Had to add this here for the linker");
return false;
}
png_info *pInfo = png_create_info_struct(pPng);
if( pInfo == nullptr )
{
png_destroy_read_struct( &pPng, nullptr, nullptr );
//sprintf( szErrorbuf, "creating png_create_info_struct failed");
return false;
}
if( setjmp(pPng->jmpbuf) )
{
png_destroy_read_struct( &pPng, &pInfo, png_infopp_NULL );
return false;
}
png_set_write_fn( pPng, f, File_png_write, File_png_flush );
png_set_compression_level( pPng, 1 );
png_set_IHDR( pPng, pInfo, pSurf->iWidth, pSurf->iHeight, 8, PNG_COLOR_TYPE_RGBA,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE );
png_write_info( pPng, pInfo );
png_set_filler( pPng, 0, PNG_FILLER_AFTER );
const auto pixels = static_cast<png_byte*>(pSurf->pRGBA);
for( int y = 0; y < pSurf->iHeight; y++ )
png_write_row( pPng, pixels + pSurf->iPitch*y );
png_write_end( pPng, pInfo );
png_destroy_write_struct( &pPng, &pInfo );
return true;
}
/*
* Copyright (c) 2003-2007 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.
*/
int fprintf()
{
return 0;
}