-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpng2c.cpp
206 lines (171 loc) · 4.95 KB
/
png2c.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
//
// main.cpp
// png2c
//
// Created by Magnus Runesson on 01/12/15.
// Copyright (c) 2015 Pokewhat. All rights reserved.
//
#include <SDL2/SDL.h>
#include <SDL2_Image/SDL_Image.h>
typedef struct
{
unsigned short w;
unsigned short h;
unsigned short* pixels;
unsigned char* alpha;
} Image;
Image myImage
{
10, 10, NULL, NULL
};
unsigned short pixels[] = {
0x1111,
0x2222,
0xffff
};
void writeHeader( FILE* f, char* _symbolNameBase, SDL_Surface* image )
{
fprintf( f, "#include \"Engine/Types.h\"\n" );
fprintf( f, "#include \"Engine/Graphics/Image.h\"\n" );
fprintf( f, "\n" );
}
void writePixels( FILE* f, char* _symbolNameBase, SDL_Surface* image, int* _pTotalOutputSize )
{
fprintf( f, "const uint16 %s_pixels[] =\n{\n", _symbolNameBase );
unsigned char* pixels = (unsigned char*)image->pixels;
int x, y;
for( y=0; y<image->h; y++ )
{
fprintf( f, "\t" );
for( x=0; x<image->w; x++ )
{
int rofs = ((y*image->w)+x)*4;
unsigned char b = pixels[ rofs+0 ];
unsigned char g = pixels[ rofs+1 ];
unsigned char r = pixels[ rofs+2 ];
//unsigned char a = pixels[ rofs+3 ];
r >>= 3;
g >>= 2;
b >>= 3;
unsigned short outcol = (r<<11) + (g<<5) + b;
//printf("r=%i, g=%i, b=%i, a=%i\n", r, g, b, a);
fprintf( f, "0x%04x,", outcol );
}
fprintf( f, "\n" );
}
*_pTotalOutputSize += image->w*image->h*2;
fprintf( f, "};\n\n" );
}
void writeAlpha( FILE* f, char* _symbolNameBase, SDL_Surface* image, int* _pTotalOutputSize )
{
fprintf( f, "const uint8 %s_alpha[] =\n{\n", _symbolNameBase );
unsigned char* pixels = (unsigned char*)image->pixels;
int x, y;
for( y=0; y<image->h; y++ )
{
fprintf( f, "\t" );
for( x=0; x<image->w; x++ )
{
int rofs = ((y*image->w)+x)*4;
unsigned char a = pixels[ rofs+3 ];
fprintf( f, "0x%02x,", a );
}
fprintf( f, "\n" );
}
*_pTotalOutputSize += image->w*image->h;
fprintf( f, "};\n\n" );
}
void writeImage( FILE* f, char* _symbolNameBase, SDL_Surface* _image, int* _pTotalOutputSize )
{
fprintf( f, "extern \"C\" const Image %s;\n", _symbolNameBase );
fprintf( f, "const Image %s =\n{\n", _symbolNameBase );
fprintf( f, "\t%i,%i,\n", _image->w, _image->h );
fprintf( f, "\t(uint16*)&%s_pixels,\n", _symbolNameBase );
if( SDL_ISPIXELFORMAT_ALPHA( _image->format->format ))
fprintf( f, "\t(uint8*)&%s_alpha,\n", _symbolNameBase );
else
fprintf( f, "\t(uint8*)0,\n" );
fprintf( f, "\t(uint8*)\"%s\",\n", _symbolNameBase );
/*
Don't output the blitting function anymore. Selecting the correct blit function will need to be done by the game code.
if( SDL_ISPIXELFORMAT_ALPHA( _image->format->format ))
fprintf( f, "\t&imageBlitAlpha,\n");
else
{
if((_image->w==96) && (_image->h==64))
{
fprintf( f, "\t&imageBlitFullScreen,\n" );
}
else
{
fprintf( f, "\t&imageBlitNoAlpha,\n" );
}
}
*/
fprintf( f, "};\n" );
}
void writeHeaderFile( FILE* f, char* _symbolNameBase, SDL_Surface* _image )
{
fprintf( f, "#ifndef %s_data_h\n", _symbolNameBase );
fprintf( f, "#define %s_data_h\n", _symbolNameBase );
fprintf( f, "\n" );
fprintf( f, "extern \"C\" const Image %s;\n", _symbolNameBase );
fprintf( f, "\n" );
fprintf( f, "#endif // %s_data_h\n", _symbolNameBase );
}
SDL_Surface* LoadImage( char* _fileName )
{
SDL_Surface* image = IMG_Load( _fileName );
//printf("Image=0x%016llx\n", (long long)image );
bool isAlpha = SDL_ISPIXELFORMAT_ALPHA( image->format->format );
bool isIndexed = SDL_ISPIXELFORMAT_INDEXED( image->format->format );
//printf("w=%i, h=%i, bpp=%i, format=%i, isAlpha=%s, isIndexed=%s\n", image->w, image->h, image->format->BitsPerPixel, image->format->format, isAlpha?"Yes":"No", isIndexed?"Yes":"No" );
return image;
}
FILE* openOutfileC( char* _baseOutFileName )
{
char outname_c[ 2048 ];
sprintf( outname_c, "%s.cpp", _baseOutFileName );
FILE* f = fopen( outname_c, "w" );
return f;
}
FILE* openOutfileH( char* _baseOutFileName )
{
char outname_c[ 2048 ];
sprintf( outname_c, "%s.h", _baseOutFileName );
FILE* f = fopen( outname_c, "w" );
return f;
}
int main( int _numargs, char** _apszArgh )
{
if( _numargs != 4 )
{
printf("Usage error: Program need 3 arguments:\n");
printf(" png2c <in_file.png> <out_file_base> <symbol_name>\n");
return -1;
}
char* pszInFileName = _apszArgh[ 1 ];
char* pszOutFilenameBase = _apszArgh[ 2 ];
char* pszSymbolNameBase = _apszArgh[ 3 ];
//
SDL_Surface* image = LoadImage( pszInFileName );
//
// Write cpp file
//
FILE* f = openOutfileC( pszOutFilenameBase );
int totalOutputSize = 0;
writeHeader( f, pszSymbolNameBase, image );
writePixels( f, pszSymbolNameBase, image, &totalOutputSize );
if( SDL_ISPIXELFORMAT_ALPHA( image->format->format ))
writeAlpha( f, pszSymbolNameBase, image, &totalOutputSize );
writeImage( f, pszSymbolNameBase, image, &totalOutputSize );
fclose( f );
//
// Write h file
//
f = openOutfileH( pszOutFilenameBase );
writeHeaderFile( f, pszSymbolNameBase, image );
fclose( f );
printf("Total output size: %i\n", totalOutputSize );
return 0;
}