1 // Simple 'detarkinizer'. Turn tarkin stream into a series of .pgm files
3 // Usage: nikrat [filename]
5 // $Id: nikrat.c,v 1.1 2001/02/13 01:06:24 giles Exp $
8 // Revision 1.1 2001/02/13 01:06:24 giles
17 // Function declarations
19 void dumptofile(char *fn
, char *data
, tarkindata
*td
);
21 int main(int argc
, char **argv
)
24 unsigned char ln
[256], *data
, *dp
, *wksp
;
25 int a
, d
, fd
, dims
[3];
36 if (!(fi
= fopen(argv
[1], "r"))) {
37 sprintf(ln
, "Can't open %s", argv
[1]);
44 sscanf(ln
, "%d %d %d", &td
.x_dim
, &td
.y_dim
, &td
.z_dim
);
45 for (a
= 0, d
= 1; d
< td
.x_dim
; a
++, d
<<= 1);
49 for (a
= 0, d
= 1; d
< td
.y_dim
; a
++, d
<<= 1);
53 for (a
= 0, d
= 1; d
< td
.z_dim
; a
++, d
<<= 1);
57 td
.sz
= td
.x_dim
* td
.y_dim
* td
.z_dim
;
58 td
.sz_workspace
= td
.x_workspace
* td
.y_workspace
* td
.z_workspace
;
61 // Prepare bitstream for unpacking
65 fd
= open(argv
[1], O_RDONLY
);
66 if ((data
= mmap(0, len
, PROT_READ
, MAP_SHARED
, fd
, 0)) == (void*)-1) {
68 perror("Can't mmap file");
71 for (dp
= data
; *dp
!= '\n' && lensub
; dp
++, lensub
--);
74 _oggpack_readinit(&o
, dp
, lensub
);
80 dims
[0] = td
.x_workspace
; dims
[1] = td
.y_workspace
; dims
[2] = td
.z_workspace
;
81 dwt(td
.vectors
, dims
, 3, -1);
83 for (a
= 0; a
< td
.sz_workspace
; a
++) {
84 if (td
.vectors
[a
] < 0.0)
86 if (td
.vectors
[a
] > 255.0)
87 td
.vectors
[a
] = 255.0;
90 // Write bitplanes to .pgms
91 wksp
= (char *)malloc(td
.x_dim
* td
.y_dim
);
92 for(z
= 0; z
< td
.z_dim
; z
++) {
93 sprintf(ln
, "out_%d", z
);
94 for (y
=0; y
< td
.y_dim
; y
++) {
95 for (x
= 0; x
< td
.x_dim
; x
++) {
96 f
= td
.vectors
[x
+ y
* td
.x_workspace
+ z
* td
.x_workspace
* td
.y_workspace
];
97 wksp
[x
+ y
* td
.x_dim
] = (unsigned char)f
;
100 dumptofile(ln
, wksp
, &td
);
111 void dumptofile(char *fn
, char *data
, tarkindata
*td
)
117 sprintf(tmp
, "%s.pgm", fn
);
119 fo
= fopen(tmp
, "w");
121 fprintf(fo
, "P5\n%d %d\n255\n", td
->x_dim
, td
->y_dim
);
122 for (y
= 0; y
< td
->y_dim
; y
++) {
123 fwrite(data
+ y
* td
->x_dim
, td
->x_dim
, 1, fo
);
131 fprintf(stderr
, "Usage:\ndetark [filename]\n\n[filename] - Filename with compressed Tarkin stream\n");