1 // Tarkin 'fixed' demo code
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".
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 $
14 // Revision 1.1 2001/02/13 01:06:24 giles
26 // Function declarations
27 int sortfunc(const unsigned int *a
, const unsigned int *b
);
30 int main(int argc
, char **argv
)
35 unsigned char ln
[256], *data
;
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
47 if(!(fi
= fopen("0.pgm", "r"))) { perror("Can't open file 0.pgm"); exit(1); }
52 if(!a
&& strncmp("P5", ln
, 2)) { fprintf(stderr
, "Error: Need PGM file for input\n"); exit(0); }
55 sscanf(ln
, "%ld %ld", &(td
.x_dim
), &(td
.y_dim
));
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
);
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
);
76 for(a
=0;a
<td
.sz_workspace
;a
++) td
.vectors
[a
] = data
[a
];
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);
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
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
);
102 int sortfunc(const unsigned int *a
, const unsigned int *b
)
107 aa
= (uint
)*a
; bb
= (uint
)*b
;
108 f
= (fabs(td
.vectors
[bb
])-fabs(td
.vectors
[aa
]));
110 return(f
>0 ? 1 : -1);
115 fprintf(stderr
, "Usage:\ntarkin [percent]\n\n[percent] - Percent of DWT vectors to retain (can be fractional)\n");