Add Russian translation provided by Валерий Крувялис <valkru@mail.ru>
[xiph-mirror.git] / tarkin / tarkin.c
blob85379e3267a62d64502579a659e667c22b01c075
1 // Tarkin 'fixed' demo code
2 //
3 // Sample fixed-length encoding top layer. Limited to 1 block of 32
4 // frames. A lot of work will need to be done here in order to make
5 // a nice, tidy interface that'll convert .avi's, .mov's or whatever
6 // else... for the time being, it only converts a series of 32 .pgm
7 // files named "0.pgm" "1.pgm" .. "31.pgm".
8 //
9 // Usage: tarkin [percentage] (where percentage is % of DWT vectors to retain)
11 // $Id: tarkin.c,v 1.1 2001/02/13 01:06:24 giles Exp $
13 // $Log: tarkin.c,v $
14 // Revision 1.1 2001/02/13 01:06:24 giles
15 // Initial revision
18 #include "tarkin.h"
20 // Global Definitions
21 #define FRAMES 32
23 // Global Variables
24 tarkindata td;
26 // Function declarations
27 int sortfunc(const unsigned int *a, const unsigned int *b);
28 void dieusage();
30 int main(int argc, char **argv)
32 uint *sortarr;
33 int a, d, x, y, z;
34 float percent;
35 unsigned char ln[256], *data;
36 FILE *fi;
37 oggpack_buffer o;
38 ulong bytes, dims[3];
40 // Future home of options processing here
41 if(argc<2) dieusage();
42 if(!(percent = (float)atof(argv[1]))) dieusage();
44 // For this code, we're using .pgms. Take the image dimensions from the first
45 // frame and set up the tarkindata struct, then grab image data to populate the
46 // workspace.
47 if(!(fi = fopen("0.pgm", "r"))) { perror("Can't open file 0.pgm"); exit(1); }
48 for(a=0;a<3;a++) {
49 fgets(ln, 255, fi);
50 if(*ln == '#') a--;
51 else {
52 if(!a && strncmp("P5", ln, 2)) { fprintf(stderr, "Error: Need PGM file for input\n"); exit(0); }
53 if(a==1) {
54 ln[20] = 0;
55 sscanf(ln, "%ld %ld", &(td.x_dim), &(td.y_dim));
59 td.z_dim = FRAMES;
60 for(a=0,d=1;d<td.x_dim;a++,d<<=1); td.x_bits = a; td.x_workspace = d;
61 for(a=0,d=1;d<td.y_dim;a++,d<<=1); td.y_bits = a; td.y_workspace = d;
62 for(a=0,d=1;d<td.z_dim;a++,d<<=1); td.z_bits = a; td.z_workspace = d;
63 td.sz = td.x_dim*td.y_dim*td.z_dim;
64 td.sz_workspace = td.x_workspace*td.y_workspace*td.z_workspace;
65 td.vectors = (float *)calloc(td.sz_workspace, sizeof(float));
66 data = malloc(td.sz_workspace);
67 for(y=0;y<td.y_dim;y++) fread(data+y*td.x_workspace, td.x_dim, 1, fi);
68 fclose(fi);
69 for(z=1;z<td.z_dim;z++) {
70 sprintf(ln, "%d.pgm", z);
71 if(!(fi = fopen(ln, "r"))) { fprintf(stderr, "Error opening "); perror(ln); exit(1); }
72 for(a=0;a<3;a++) { fgets(ln, 256, fi); if(*ln == '#') a--; }
73 for(y=0;y<td.y_dim;y++) fread(data+z*td.x_workspace*td.y_workspace+y*td.x_workspace, td.x_dim, 1, fi);
74 fclose(fi);
76 for(a=0;a<td.sz_workspace;a++) td.vectors[a] = data[a];
77 free(data);
79 // Execute DWT transform
80 dims[0] = td.x_workspace; dims[1] = td.y_workspace; dims[2] = td.z_workspace;
81 dwt(td.vectors, dims, 3, 1);
83 // Filter DWT vectors
84 sortarr = (uint *)calloc(td.sz_workspace+1, sizeof(uint));
85 for(a=0;a<td.sz_workspace;a++) sortarr[a]=a;
86 qsort(sortarr, td.sz_workspace, sizeof(uint), &sortfunc);
87 td.vectorcount = (td.sz*percent/100);
88 for(x=td.vectorcount;x<td.sz_workspace;x++) td.vectors[sortarr[x]]=0;
90 // Pack vectors into bitstream
91 packblock(&td, &o);
93 // Write bitstream
94 fi = fopen("output.tark", "w");
95 fprintf(fi, "%d %d %d\n", td.x_dim, td.y_dim, td.z_dim);
96 data = _oggpack_buffer(&o);
97 bytes = _oggpack_bytes(&o);
98 fwrite(data, bytes, 1, fi);
99 fclose(fi);
102 int sortfunc(const unsigned int *a, const unsigned int *b)
104 uint aa, bb;
105 float f;
107 aa = (uint)*a; bb = (uint)*b;
108 f = (fabs(td.vectors[bb])-fabs(td.vectors[aa]));
109 if(f==0) return 0;
110 return(f>0 ? 1 : -1);
113 void dieusage()
115 fprintf(stderr, "Usage:\ntarkin [percent]\n\n[percent] - Percent of DWT vectors to retain (can be fractional)\n");
116 exit(1);