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
|
#include <gd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
struct texthead {
unsigned short height;
unsigned short width;
unsigned int format;
unsigned int offs;
unsigned int wrap_s;
unsigned int wrap_t;
unsigned int min;
unsigned int mag;
float lod_bias;
unsigned char edge_lod;
unsigned char min_lod;
unsigned char max_lod;
unsigned char unpacked;
};
int main(int argc, char **argv) {
if(argc != 3) {
printf("Usage: gentpl <input.png> <output.tpl>\n");
exit(1);
}
gdImagePtr im;
FILE *in = fopen(argv[1], "rb");
if(!in) {
printf("Failed to open %s\n", argv[1]); // why strerror not working?
exit(1);
}
im = gdImageCreateFromPng(in);
fclose(in);
FILE *out = fopen(argv[2], "wb");
if(!out) {
printf("Failed to open %s\n", argv[2]); // why strerror not working?
exit(1);
}
if(gdImageSX(im) % 4 != 0 || gdImageSY(im) % 4 != 0) {
printf("Bad dimensions\n");
exit(1);
}
char magic[] = {0x00, 0x20, 0xaf, 0x30};
fwrite(magic, 1, 4, out);
unsigned int stuff[] = {htonl(1), htonl(0xc), htonl(0x14), 0};
fwrite(stuff, 4, 4, out);
struct texthead h;
memset(&h, 0, sizeof(h));
h.format = htonl(4); // RGB565
h.offs = htonl(0x14 + sizeof(h) + 8);
printf("%dx%d\n", gdImageSX(im), gdImageSY(im));
h.width = htons(gdImageSX(im));
h.height = htons(gdImageSY(im));
h.min = htons(1);
h.mag = htons(1);
fwrite(&h, sizeof(h), 1, out);
for(int i = 0; i < 8; i++) fputc(0, out);
for(int y = 0; y < gdImageSY(im); y += 4) {
for(int x = 0; x < gdImageSX(im); x += 4) {
for(int y1 = y; y1 < y + 4; y1++) {
for(int x1 = x; x1 < x + 4; x1++) {
int p = gdImageGetPixel(im, x1, y1);
unsigned char a = 254 - 2*((unsigned char) gdImageAlpha(im, p));
if(a == 254) a++;
unsigned char r = (unsigned char) gdImageRed(im, p);
unsigned char g = (unsigned char) gdImageGreen(im, p);
unsigned char b = (unsigned char) gdImageBlue(im, p);
unsigned short s = 0;
s |= (r >> 3) << 11;
s |= (g >> 2) << 5;
s |= (b >> 3);
fputc(s >> 8, out);
fputc(s & 0xff, out);
}
}
}
}
fclose(out);
gdImageDestroy(im);
}
|