aboutsummaryrefslogtreecommitdiff
path: root/gentpl
diff options
context:
space:
mode:
authorcomex <comexk@gmail.com>2009-02-03 14:46:48 -0500
committercomex <comexk@gmail.com>2009-02-03 14:46:48 -0500
commit795bb8d7d748c717c1a78a3a31ed2415f22b476a (patch)
tree314cd842b5cce3e1599fab457ba433cd1a024191 /gentpl
parentd8ab7e96f581a4444205b947f62c19b0bd87e286 (diff)
gentpl
Diffstat (limited to 'gentpl')
-rw-r--r--gentpl/Makefile4
-rw-r--r--gentpl/gentpl.c88
-rw-r--r--gentpl/test.pngbin0 -> 230378 bytes
3 files changed, 92 insertions, 0 deletions
diff --git a/gentpl/Makefile b/gentpl/Makefile
new file mode 100644
index 0000000..4d92b53
--- /dev/null
+++ b/gentpl/Makefile
@@ -0,0 +1,4 @@
+gentpl: gentpl.c
+ gcc -o gentpl gentpl.c -lgd -std=gnu99
+clean:
+ rm -f gentpl \ No newline at end of file
diff --git a/gentpl/gentpl.c b/gentpl/gentpl.c
new file mode 100644
index 0000000..b0cdfdc
--- /dev/null
+++ b/gentpl/gentpl.c
@@ -0,0 +1,88 @@
+#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(6);
+ h.offs = htonl(0x14 + sizeof(h));
+ printf("%dx%d\n", gdImageSX(im), gdImageSY(im));
+ h.width = htons(gdImageSX(im));
+ h.height = htons(gdImageSY(im));
+
+ fwrite(&h, sizeof(h), 1, 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 b = (unsigned char) gdImageRed(im, p);
+ fputc(a, out);
+ fputc(b, out); }
+ }
+
+ 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 = (unsigned char) gdImageGreen(im, p);
+ unsigned char b = (unsigned char) gdImageBlue(im, p);
+ fputc(a, out);
+ fputc(b, out);
+ }
+ }
+ }
+ }
+
+ fclose(out);
+ gdImageDestroy(im);
+} \ No newline at end of file
diff --git a/gentpl/test.png b/gentpl/test.png
new file mode 100644
index 0000000..b1c851a
--- /dev/null
+++ b/gentpl/test.png
Binary files differ