1 // Copyright 2007,2008 Segher Boessenkool <segher@kernel.crashing.org>
2 // Licensed under the terms of the GNU GPL, version 2
3 // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
9 static u8 buf
[5000000]; // yeah yeah, static buffer, whatever
11 static void i4(int w
, int h
, int o
, char *name
)
16 out
= fopen(name
, "wb");
18 fprintf(out
, "P6 %d %d 255\n", w
, h
);
20 for (y
= 0; y
< h
; y
++)
21 for (x
= 0; x
< w
; x
++) {
24 int x0
, x1
, y0
, y1
, off
;
25 int ww
= round_up(w
, 8);
31 off
= x0
+ 8*y0
+ 64*x1
+ 8*ww
*y1
;
43 fwrite(pix
, 1, 3, out
);
49 static void i8(int w
, int h
, int o
, char *name
)
54 out
= fopen(name
, "wb");
56 fprintf(out
, "P6 %d %d 255\n", w
, h
);
58 for (y
= 0; y
< h
; y
++)
59 for (x
= 0; x
< w
; x
++) {
62 int x0
, x1
, y0
, y1
, off
;
63 int ww
= round_up(w
, 8);
69 off
= x0
+ 8*y0
+ 32*x1
+ 4*ww
*y1
;
77 fwrite(pix
, 1, 3, out
);
83 static void ia4(int w
, int h
, int o
, char *name
)
88 out
= fopen(name
, "wb");
90 fprintf(out
, "P6 %d %d 255\n", w
, h
);
92 for (y
= 0; y
< h
; y
++)
93 for (x
= 0; x
< w
; x
++) {
96 int x0
, x1
, y0
, y1
, off
;
97 int ww
= round_up(w
, 8);
103 off
= x0
+ 8*y0
+ 32*x1
+ 4*ww
*y1
;
107 //raw = (raw >> 4) * 0x11;
108 raw
= (raw
& 0xf) * 0x11;
114 fwrite(pix
, 1, 3, out
);
120 static void rgb5a3(int w
, int h
, int o
, char *name
)
125 out
= fopen(name
, "wb");
127 fprintf(out
, "P6 %d %d 255\n", w
, h
);
129 for (y
= 0; y
< h
; y
++)
130 for (x
= 0; x
< w
; x
++) {
133 int x0
, x1
, y0
, y1
, off
;
134 int ww
= round_up(w
, 4);
140 off
= x0
+ 4*y0
+ 16*x1
+ 4*ww
*y1
;
142 raw
= buf
[o
+ 2*off
] << 8;
143 raw
|= buf
[o
+ 2*off
+ 1];
147 pix
[0] = (raw
>> 7) & 0xf8;
148 pix
[1] = (raw
>> 2) & 0xf8;
149 pix
[2] = (raw
<< 3) & 0xf8;
151 pix
[0] = (raw
>> 4) & 0xf0;
153 pix
[2] = (raw
<< 4) & 0xf0;
156 fwrite(pix
, 1, 3, out
);
162 static u16
avg(u16 w0
, u16 w1
, u16 c0
, u16 c1
)
169 a
= (w0
*a0
+ w1
*a1
) / (w0
+ w1
);
174 a
= (w0
*a0
+ w1
*a1
) / (w0
+ w1
);
179 a
= (w0
*a0
+ w1
*a1
) / (w0
+ w1
);
185 static void cmp(int w
, int h
, int o
, char *name
)
190 out
= fopen(name
, "wb");
192 fprintf(out
, "P6 %d %d 255\n", w
, h
);
194 for (y
= 0; y
< h
; y
++)
195 for (x
= 0; x
< w
; x
++) {
199 int x0
, x1
, x2
, y0
, y1
, y2
, off
;
200 int ww
= round_up(w
, 8);
210 off
= 8*x1
+ 16*y1
+ 32*x2
+ 4*ww
*y2
;
212 c
[0] = be16(buf
+ o
+ off
);
213 c
[1] = be16(buf
+ o
+ off
+ 2);
215 c
[2] = avg(2, 1, c
[0], c
[1]);
216 c
[3] = avg(1, 2, c
[0], c
[1]);
218 c
[2] = avg(1, 1, c
[0], c
[1]);
222 px
= be32(buf
+ o
+ off
+ 4);
224 raw
= c
[(px
>> (30 - 2*ix
)) & 3];
226 pix
[0] = (raw
>> 8) & 0xf8;
227 pix
[1] = (raw
>> 3) & 0xf8;
228 pix
[2] = (raw
<< 3) & 0xf8;
230 fwrite(pix
, 1, 3, out
);
236 int main(int argc
, char **argv
)
241 in
= fopen(argv
[1], "rb");
242 fread(buf
, 1, sizeof buf
, in
);
245 h
= be16(buf
+ 0x14);
246 w
= be16(buf
+ 0x16);
247 t
= be32(buf
+ 0x18);
248 o
= be32(buf
+ 0x1c);
250 fprintf(stderr
, "type %02x -- %s\n", t
, argv
[1]);
252 // XXX: check more header stuff here
255 i4(w
, h
, o
, argv
[2]);
259 i8(w
, h
, o
, argv
[2]);
263 ia4(w
, h
, o
, argv
[2]);
267 rgb5a3(w
, h
, o
, argv
[2]);
271 cmp(w
, h
, o
, argv
[2]);
275 fprintf(stderr
, "unhandled type %02x\n", t
);