Add Russian translation provided by Валерий Крувялис <valkru@mail.ru>
[xiph-mirror.git] / tarkin / nikrat.c
blobeb44f94f0b19410c2032e6deddf31ddba9d2be63
1 // Simple 'detarkinizer'. Turn tarkin stream into a series of .pgm files
2 //
3 // Usage: nikrat [filename]
4 //
5 // $Id: nikrat.c,v 1.1 2001/02/13 01:06:24 giles Exp $
6 //
7 // $Log: nikrat.c,v $
8 // Revision 1.1 2001/02/13 01:06:24 giles
9 // Initial revision
12 #include "tarkin.h"
14 // Global Variables
15 tarkindata td;
17 // Function declarations
18 void dieusage();
19 void dumptofile(char *fn, char *data, tarkindata *td);
21 int main(int argc, char **argv)
23 FILE *fi;
24 unsigned char ln[256], *data, *dp, *wksp;
25 int a, d, fd, dims[3];
26 int x, y, z;
27 oggpack_buffer o;
28 ulong len, lensub, l;
29 struct stat st;
30 long i;
31 float f;
33 if (argc < 2)
34 dieusage();
36 if (!(fi = fopen(argv[1], "r"))) {
37 sprintf(ln, "Can't open %s", argv[1]);
38 perror(ln);
39 exit(1);
42 fgets(ln, 255, fi);
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);
46 td.x_bits = a;
47 td.x_workspace = d;
49 for (a = 0, d = 1; d < td.y_dim; a++, d <<= 1);
50 td.y_bits = a;
51 td.y_workspace = d;
53 for (a = 0, d = 1; d < td.z_dim; a++, d <<= 1);
54 td.z_bits = a;
55 td.z_workspace = d;
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;
59 fclose(fi);
61 // Prepare bitstream for unpacking
62 stat(argv[1], &st);
63 len = st.st_size;
64 lensub = len;
65 fd = open(argv[1], O_RDONLY);
66 if ((data = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0)) == (void*)-1) {
67 close(fd);
68 perror("Can't mmap file");
69 exit(1);
71 for (dp = data; *dp != '\n' && lensub; dp++, lensub--);
72 dp++;
73 lensub--;
74 _oggpack_readinit(&o, dp, lensub);
76 // Unpack bitstream
77 unpackblock(&td, &o);
79 // iDWT on bitstream
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)
85 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);
103 // Shutdown
104 free(wksp);
105 free(td.vectors);
106 free(td.dwtv);
107 munmap(data, len);
108 close(fd);
111 void dumptofile(char *fn, char *data, tarkindata *td)
113 FILE *fo;
114 char tmp[256];
115 int y;
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);
126 fclose(fo);
129 void dieusage()
131 fprintf(stderr, "Usage:\ndetark [filename]\n\n[filename] - Filename with compressed Tarkin stream\n");
132 exit(1);