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
7 typedef unsigned char u8
;
8 typedef unsigned short u16
;
9 typedef unsigned int u32
;
11 static u8 buf
[5000000]; // yeah yeah, static buffer, whatever
13 static u16
be16(u8
*p
)
15 return (p
[0] << 8) | p
[1];
18 static u32
be32(u8
*p
)
20 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
23 static void i4(int w
, int h
, int o
, char *name
)
28 out
= fopen(name
, "wb");
30 fprintf(out
, "P6 %d %d 255\n", w
, h
);
32 for (y
= 0; y
< h
; y
++)
33 for (x
= 0; x
< w
; x
++) {
36 int x0
, x1
, y0
, y1
, off
;
37 int ww
= (w
+ 7) & ~7;
43 off
= x0
+ 8*y0
+ 64*x1
+ 8*ww
*y1
;
55 fwrite(pix
, 1, 3, out
);
61 static void i8(int w
, int h
, int o
, char *name
)
66 out
= fopen(name
, "wb");
68 fprintf(out
, "P6 %d %d 255\n", w
, h
);
70 for (y
= 0; y
< h
; y
++)
71 for (x
= 0; x
< w
; x
++) {
74 int x0
, x1
, y0
, y1
, off
;
75 int ww
= (w
+ 7) & ~7;
81 off
= x0
+ 8*y0
+ 32*x1
+ 4*ww
*y1
;
89 fwrite(pix
, 1, 3, out
);
95 static void ia4(int w
, int h
, int o
, char *name
)
100 out
= fopen(name
, "wb");
102 fprintf(out
, "P6 %d %d 255\n", w
, h
);
104 for (y
= 0; y
< h
; y
++)
105 for (x
= 0; x
< w
; x
++) {
108 int x0
, x1
, y0
, y1
, off
;
109 int ww
= (w
+ 7) & ~7;
115 off
= x0
+ 8*y0
+ 32*x1
+ 4*ww
*y1
;
119 //raw = (raw >> 4) * 0x11;
120 raw
= (raw
& 0xf) * 0x11;
126 fwrite(pix
, 1, 3, out
);
132 static void rgb5a3(int w
, int h
, int o
, char *name
)
137 out
= fopen(name
, "wb");
139 fprintf(out
, "P6 %d %d 255\n", w
, h
);
141 for (y
= 0; y
< h
; y
++)
142 for (x
= 0; x
< w
; x
++) {
145 int x0
, x1
, y0
, y1
, off
;
146 int ww
= (w
+ 3) & ~3;
152 off
= x0
+ 4*y0
+ 16*x1
+ 4*ww
*y1
;
154 raw
= buf
[o
+ 2*off
] << 8;
155 raw
|= buf
[o
+ 2*off
+ 1];
159 pix
[0] = (raw
>> 7) & 0xf8;
160 pix
[1] = (raw
>> 2) & 0xf8;
161 pix
[2] = (raw
<< 3) & 0xf8;
163 pix
[0] = (raw
>> 4) & 0xf0;
165 pix
[2] = (raw
<< 4) & 0xf0;
168 fwrite(pix
, 1, 3, out
);
174 static u16
avg(u16 w0
, u16 w1
, u16 c0
, u16 c1
)
181 a
= (w0
*a0
+ w1
*a1
) / (w0
+ w1
);
186 a
= (w0
*a0
+ w1
*a1
) / (w0
+ w1
);
191 a
= (w0
*a0
+ w1
*a1
) / (w0
+ w1
);
197 static void cmp(int w
, int h
, int o
, char *name
)
202 out
= fopen(name
, "wb");
204 fprintf(out
, "P6 %d %d 255\n", w
, h
);
206 for (y
= 0; y
< h
; y
++)
207 for (x
= 0; x
< w
; x
++) {
211 int x0
, x1
, x2
, y0
, y1
, y2
, off
;
212 int ww
= (w
+ 7) & ~7;
222 off
= 8*x1
+ 16*y1
+ 32*x2
+ 4*ww
*y2
;
224 c
[0] = be16(buf
+ o
+ off
);
225 c
[1] = be16(buf
+ o
+ off
+ 2);
227 c
[2] = avg(2, 1, c
[0], c
[1]);
228 c
[3] = avg(1, 2, c
[0], c
[1]);
230 c
[2] = avg(1, 1, c
[0], c
[1]);
234 px
= be32(buf
+ o
+ off
+ 4);
236 raw
= c
[(px
>> (30 - 2*ix
)) & 3];
238 pix
[0] = (raw
>> 8) & 0xf8;
239 pix
[1] = (raw
>> 3) & 0xf8;
240 pix
[2] = (raw
<< 3) & 0xf8;
242 fwrite(pix
, 1, 3, out
);
248 int main(int argc
, char **argv
)
253 in
= fopen(argv
[1], "rb");
254 fread(buf
, 1, sizeof buf
, in
);
257 h
= be16(buf
+ 0x14);
258 w
= be16(buf
+ 0x16);
259 t
= be32(buf
+ 0x18);
260 o
= be32(buf
+ 0x1c);
262 fprintf(stderr
, "type %02x -- %s\n", t
, argv
[1]);
264 // XXX: check more header stuff here
267 i4(w
, h
, o
, argv
[2]);
271 i8(w
, h
, o
, argv
[2]);
275 ia4(w
, h
, o
, argv
[2]);
279 rgb5a3(w
, h
, o
, argv
[2]);
283 cmp(w
, h
, o
, argv
[2]);
287 fprintf(stderr
, "unhandled type %02x\n", t
);