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 #define STRSZ(lit) lit, sizeof(lit) - 1
42 #define LIT_SIZE(lit) ( sizeof(lit) - 1)
43 int main(int argc
, char** argv
) {
45 struct palpic pp
= palpic_empty
;
51 unsigned startarg
= 1;
52 for(; startarg
< (unsigned) argc
; startarg
++)
53 if(!memcmp(argv
[startarg
], STRSZ("-sprite="))) break;
54 if(startarg
== argc
) {
55 printf("syntax: %s [-t] -sprite=WxH infile outfile\n"
56 "where WxH denotes the dimensions of a single picture in the spritesheet\n"
57 "-t, if given, means the first color in the bitmap is the transparent color\n", argv
[0]);
60 if(startarg
== 2 && argv
[1][0] == '-' && argv
[1][1] == 't' && argv
[1][2] == 0)
61 pp
.flags
|= PPF_TRANSPARENT
;
65 char *in_filename
= argv
[startarg
];
66 if(access(in_filename
, R_OK
) == -1) {
70 char *out_filename
= argv
[startarg
+ 1];
72 unsigned sprite_w
, sprite_h
;
73 unsigned sprite_count
;
74 int to_c
= (cp
= strrchr(out_filename
, '.')) && cp
== out_filename
+ strlen(out_filename
) - 2 && cp
[1] == 'c';
75 char struct_name
[256];
77 char *st_start
= strrchr(out_filename
, '/');
78 if(st_start
) st_start
++;
79 else st_start
= out_filename
;
80 size_t l
= snprintf(struct_name
, sizeof struct_name
, "%s", st_start
);
85 infile
= pixRead(in_filename
);
87 pixGetDimensions(infile
, &w
, &h
, NULL
);
90 char* str_w
= argv
[startarg
-1] + LIT_SIZE("-sprite=");
91 char* str_h
= strchr(str_w
, 'x');
93 sprite_w
= atoi(str_w
);
94 sprite_h
= atoi(str_h
);
95 sprite_count
= (h
* w
) / (sprite_h
* sprite_w
);
96 unsigned rest
= (h
* w
) % (sprite_h
* sprite_w
);
98 printf("error: the picture dimensions are no multiple of the sprite dimensions!\n");
102 printf("error: the picture width is no multiple of the sprite width!\n");
112 unsigned sprites_per_row
= w
/ sprite_w
;
114 /* we always align sprites vertically */
115 pp
.height
= sprite_h
* sprite_count
;
118 pix32
= pixConvertTo32(infile
);
119 prgb
* bufptr
= (prgb
*) pix32
->data
;
121 unsigned x
, y
= 0, p
;
122 for(y
= 0; y
< (unsigned) h
; y
++) {
123 for(x
= 0; x
< (unsigned) w
; x
++) {
124 for(p
= 0; p
< pp
.palcount
; p
++)
125 if(pal
[p
].val
== bufptr
->val
)
127 pal
[pp
.palcount
++] = *bufptr
;
133 FILE* outfile
= fopen(out_filename
, "w");
138 palpic_fileformat(&pp
);
139 fwrite(&pp
, sizeof(pp
), 1, outfile
);
140 fwrite(pal
, sizeof(prgb
) * pp
.palcount
, 1, outfile
);
142 for(sprite
= 0; sprite
< sprite_count
; sprite
++) {
143 for(y
= 0; y
< sprite_h
; y
++) {
144 unsigned sprite_start_y
= get_sprite_start(sprite
, y
, sprite_w
, sprite_h
, sprites_per_row
);
145 bufptr
= &((prgb
*) pix32
->data
)[sprite_start_y
];
147 for(x
= 0; x
< sprite_w
; x
++) {
148 for(p
= 0; p
< pp
.palcount
; p
++)
149 if(pal
[p
].val
== bufptr
->val
) {
151 fwrite(&val
, sizeof(val
), 1, outfile
);
154 fprintf(stderr
, "bug: could not find color in palette\n");
163 snprintf(buf
, sizeof(buf
),
164 "#include \"palpic.h\"\n"
165 "#define PAL_COUNT %d\n"
166 "#define SPRITE_COUNT %d\n"
168 "#define HEIGHT %d\n"
171 "#define STRUCT_NAME %s\n\n"
172 "static const struct {\n"
173 "\tstruct palpic header;\n"
174 "\tprgb palette[PAL_COUNT];\n"
175 "\tuint8_t data[WIDTH * HEIGHT];\n"
176 "} STRUCT_NAME = { \n"
177 "\t{ {'p', 'P', 'i', 'C', }, 1, PAL_COUNT, SPRITE_COUNT, WIDTH, HEIGHT, %s, 0 },\n"
184 (pp
.flags
& PPF_TRANSPARENT
? "PPF_TRANSPARENT" : "0")
186 fwrite(buf
, strlen(buf
), 1, outfile
);
188 for(p
= 0; p
< pp
.palcount
; p
++) {
189 if(p
== 0 && pp
.flags
& PPF_TRANSPARENT
)
190 snprintf(buf
, sizeof(buf
), "PRGB(%3u, %3u, %3u), ", (unsigned) 0,(unsigned) 0, (unsigned) 0);
192 snprintf(buf
, sizeof(buf
), "PRGB(%3u, %3u, %3u), ", (unsigned) pal
[p
].colors
.r
,(unsigned) pal
[p
].colors
.g
, (unsigned) pal
[p
].colors
.b
);
193 fwrite(buf
, strlen(buf
), 1, outfile
);
195 fwrite(STRSZ("\n\t\t"), 1, outfile
);
198 snprintf(buf
, sizeof(buf
), "\n\t},\n\t{\n\t\t");
199 fwrite(buf
, strlen(buf
), 1, outfile
);
200 unsigned counter
= 0;
201 unsigned values_per_line
= sprite_w
;
202 const char *value_format
= pp
.palcount
> 99 ? "%3u," : "%2u,";
203 const char *o_format
= pp
.palcount
> 99 ? " o," : " o,";
204 for(sprite
= 0; sprite
< sprite_count
; sprite
++) {
205 fprintf(outfile
, "/* sprite #%.3u */\n\t\t", sprite
);
206 for(y
= 0; y
< sprite_h
; y
++) {
207 unsigned sprite_start_y
= get_sprite_start(sprite
, y
, sprite_w
, sprite_h
, sprites_per_row
);
208 assert(sprite_start_y
+ sprite_w
<= h
* w
);
209 bufptr
= &((prgb
*) pix32
->data
)[sprite_start_y
];
211 for(x
= 0; x
< sprite_w
; x
++) {
212 for(p
= 0; p
< pp
.palcount
; p
++) {
213 if(pal
[p
].val
== bufptr
->val
) {
215 snprintf(buf
, sizeof(buf
), o_format
);
217 snprintf(buf
, sizeof(buf
), value_format
, (unsigned) p
);
218 fwrite(buf
, strlen(buf
), 1, outfile
);
219 if(counter
% values_per_line
== values_per_line
- 1)
220 fwrite(STRSZ("\n\t\t"), 1, outfile
);
224 fprintf(stderr
, "bug: could not find color in palette\n");
233 fwrite(STRSZ("\n\t},\n"), 1, outfile
);
235 snprintf(buf
, sizeof(buf
),
239 "#undef SPRITE_COUNT\n"
242 "#undef STRUCT_NAME\n\n");
243 fwrite(buf
, strlen(buf
), 1, outfile
);