6 #include <leptonica/allheaders.h>
7 #define assert(x) if(!(x)) __asm__("int3");
9 #pragma RcB2 LINK "-llept"
18 static const l_int32 L_RED_SHIFT =
19 8 * (sizeof(l_uint32) - 1 - COLOR_RED); //24
20 static const l_int32 L_GREEN_SHIFT =
21 8 * (sizeof(l_uint32) - 1 - COLOR_GREEN); //16
22 static const l_int32 L_BLUE_SHIFT =
23 8 * (sizeof(l_uint32) - 1 - COLOR_BLUE); //8
24 static const l_int32 L_ALPHA_SHIFT =
25 8 * (sizeof(l_uint32) - 1 - L_ALPHA_CHANNEL); //0
27 composeRGBPixel(l_int32 rval, l_int32 gval, l_int32 bval, l_uint32 *ppixel)
29 *ppixel = (rval << L_RED_SHIFT) | (gval << L_GREEN_SHIFT) | (bval << L_BLUE_SHIFT);
33 static unsigned get_sprite_start(unsigned sprite_nr
, unsigned row_nr
, unsigned sprite_w
, unsigned sprite_h
, unsigned sprites_per_row
) {
34 unsigned sprite_row
= sprite_nr
/ sprites_per_row
;
35 unsigned row_off
= (row_nr
* sprite_w
* sprites_per_row
);
36 unsigned res
= (sprite_row
* sprite_w
* sprite_h
* sprites_per_row
) + row_off
+ ((sprite_nr
% sprites_per_row
) * sprite_w
);
37 //printf("sprite %.2u, row %.2u : %u\n", sprite_nr, row_nr, res);
41 static int usage(char *a0
) {
42 printf("syntax: %s [-t] [-s WxH] infile outfile\n"
43 "where WxH denotes the dimensions of a single picture in the spritesheet\n"
44 "-t, if given, means the first color in the bitmap is the transparent color\n"
45 "if -s is omitted, the file will be considered a single sprite\n"
46 "if the file extension is .c, a C file will be written, else a binary ppic\n", a0
);
50 #define STRSZ(lit) lit, sizeof(lit) - 1
51 #define LIT_SIZE(lit) ( sizeof(lit) - 1)
52 int main(int argc
, char** argv
) {
54 struct palpic pp
= palpic_empty
;
60 unsigned sprite_w
= 0, sprite_h
= 0;
61 unsigned sprite_count
;
64 while((c
= getopt(argc
, argv
, "s:t")) != EOF
) switch(c
) {
65 case 't': pp
.flags
|= PPF_TRANSPARENT
; break;
68 char* str_h
= strchr(str_w
, 'x');
70 sprite_w
= atoi(str_w
);
71 sprite_h
= atoi(str_h
);
74 default: return usage(argv
[0]);
77 if(!argv
[optind
] || !argv
[optind
+1]) return usage(argv
[0]);
79 char *in_filename
= argv
[optind
];
80 char *out_filename
= argv
[optind
+1];
82 if(access(in_filename
, R_OK
) == -1) {
88 int to_c
= (cp
= strrchr(out_filename
, '.')) && cp
== out_filename
+ strlen(out_filename
) - 2 && cp
[1] == 'c';
89 char struct_name
[256];
91 char *st_start
= strrchr(out_filename
, '/');
92 if(st_start
) st_start
++;
93 else st_start
= out_filename
;
94 size_t l
= snprintf(struct_name
, sizeof struct_name
, "%s", st_start
);
99 infile
= pixRead(in_filename
);
101 pixGetDimensions(infile
, &w
, &h
, NULL
);
103 if(sprite_w
&& sprite_h
) {
104 sprite_count
= (h
* w
) / (sprite_h
* sprite_w
);
105 unsigned rest
= (h
* w
) % (sprite_h
* sprite_w
);
107 printf("error: the picture dimensions are no multiple of the sprite dimensions!\n");
111 printf("error: the picture width is no multiple of the sprite width!\n");
121 unsigned sprites_per_row
= w
/ sprite_w
;
123 /* we always align sprites vertically */
124 pp
.height
= sprite_h
* sprite_count
;
127 pp
.spritecount
= sprite_count
;
129 pix32
= pixConvertTo32(infile
);
130 prgb
* bufptr
= (prgb
*) pix32
->data
;
132 unsigned x
, y
= 0, p
;
133 for(y
= 0; y
< (unsigned) h
; y
++) {
134 for(x
= 0; x
< (unsigned) w
; x
++) {
135 for(p
= 0; p
< pp
.palcount
; p
++)
136 if(pal
[p
].val
== bufptr
->val
)
138 pal
[pp
.palcount
++] = *bufptr
;
144 FILE* outfile
= fopen(out_filename
, "w");
149 palpic_fileformat(&pp
);
150 fwrite(&pp
, sizeof(pp
), 1, outfile
);
151 fwrite(pal
, sizeof(prgb
) * pp
.palcount
, 1, outfile
);
153 for(sprite
= 0; sprite
< sprite_count
; sprite
++) {
154 for(y
= 0; y
< sprite_h
; y
++) {
155 unsigned sprite_start_y
= get_sprite_start(sprite
, y
, sprite_w
, sprite_h
, sprites_per_row
);
156 bufptr
= &((prgb
*) pix32
->data
)[sprite_start_y
];
158 for(x
= 0; x
< sprite_w
; x
++) {
159 for(p
= 0; p
< pp
.palcount
; p
++)
160 if(pal
[p
].val
== bufptr
->val
) {
162 fwrite(&val
, sizeof(val
), 1, outfile
);
165 fprintf(stderr
, "bug: could not find color in palette\n");
174 snprintf(buf
, sizeof(buf
),
175 "#include \"palpic.h\"\n"
176 "#define PAL_COUNT %d\n"
177 "#define SPRITE_COUNT %d\n"
179 "#define HEIGHT %d\n"
182 "#define STRUCT_NAME %s\n\n"
183 "static const struct {\n"
184 "\tstruct palpic header;\n"
185 "\tprgb palette[PAL_COUNT];\n"
186 "\tuint8_t data[WIDTH * HEIGHT];\n"
187 "} STRUCT_NAME = { \n"
188 "\t{ {'p', 'P', 'i', 'C', }, 1, PAL_COUNT, SPRITE_COUNT, WIDTH, HEIGHT, %s, 0 },\n"
195 (pp
.flags
& PPF_TRANSPARENT
? "PPF_TRANSPARENT" : "0")
197 fwrite(buf
, strlen(buf
), 1, outfile
);
199 for(p
= 0; p
< pp
.palcount
; p
++) {
200 if(p
== 0 && pp
.flags
& PPF_TRANSPARENT
)
201 snprintf(buf
, sizeof(buf
), "PRGB(%3u, %3u, %3u), ", (unsigned) 0,(unsigned) 0, (unsigned) 0);
203 snprintf(buf
, sizeof(buf
), "PRGB(%3u, %3u, %3u), ", (unsigned) pal
[p
].colors
.r
,(unsigned) pal
[p
].colors
.g
, (unsigned) pal
[p
].colors
.b
);
204 fwrite(buf
, strlen(buf
), 1, outfile
);
206 fwrite(STRSZ("\n\t\t"), 1, outfile
);
209 snprintf(buf
, sizeof(buf
), "\n\t},\n\t{\n\t\t");
210 fwrite(buf
, strlen(buf
), 1, outfile
);
211 unsigned counter
= 0;
212 unsigned values_per_line
= sprite_w
;
213 const char *value_format
= pp
.palcount
> 99 ? "%3u," : "%2u,";
214 const char *o_format
= pp
.palcount
> 99 ? " o," : " o,";
215 for(sprite
= 0; sprite
< sprite_count
; sprite
++) {
216 fprintf(outfile
, "/* sprite #%.3u */\n\t\t", sprite
);
217 for(y
= 0; y
< sprite_h
; y
++) {
218 unsigned sprite_start_y
= get_sprite_start(sprite
, y
, sprite_w
, sprite_h
, sprites_per_row
);
219 assert(sprite_start_y
+ sprite_w
<= h
* w
);
220 bufptr
= &((prgb
*) pix32
->data
)[sprite_start_y
];
222 for(x
= 0; x
< sprite_w
; x
++) {
223 for(p
= 0; p
< pp
.palcount
; p
++) {
224 if(pal
[p
].val
== bufptr
->val
) {
226 snprintf(buf
, sizeof(buf
), o_format
);
228 snprintf(buf
, sizeof(buf
), value_format
, (unsigned) p
);
229 fwrite(buf
, strlen(buf
), 1, outfile
);
230 if(counter
% values_per_line
== values_per_line
- 1)
231 fwrite(STRSZ("\n\t\t"), 1, outfile
);
235 fprintf(stderr
, "bug: could not find color in palette\n");
244 fwrite(STRSZ("\n\t},\n"), 1, outfile
);
246 snprintf(buf
, sizeof(buf
),
250 "#undef SPRITE_COUNT\n"
253 "#undef STRUCT_NAME\n\n");
254 fwrite(buf
, strlen(buf
), 1, outfile
);