Updated project files to VS2008 SP1. Removed deprecated features (and warning message...
[xiph/unicode.git] / tarkin / tarkinplay.c
blob097b885da83defea3eb3d06f6e02c59c64b9944d
1 // Simple 'detarkinizer'. Turn tarkin stream into a series of .pgm files
2 //
3 // Usage: nikrat [filename]
4 //
5 // $Id: tarkinplay.c,v 1.1 2001/02/13 01:06:24 giles Exp $
6 //
7 // $Log: tarkinplay.c,v $
8 // Revision 1.1 2001/02/13 01:06:24 giles
9 // Initial revision
12 #include <stdlib.h>
13 #include <SDL/SDL.h>
14 #include "bitwise.h"
15 #include "tarkin.h"
17 // Global Variables
18 tarkindata td;
20 // Function declarations
21 void dieusage();
22 void dumptofile(char *fn, char *data, tarkindata *td);
23 void make_palette(SDL_Surface *screen);
25 int main(int argc, char **argv)
27 FILE *fi;
28 unsigned char ln[256], *data, *dp, *wksp;
29 int a, d, fd, dims[3];
30 int x, y, z;
31 oggpack_buffer o;
32 ulong len, lensub, l;
33 struct stat st;
34 long i;
35 float f;
36 SDL_Surface *screen;
37 char *image;
39 if (argc < 2)
40 dieusage();
42 if (!(fi = fopen(argv[1], "r"))) {
43 sprintf(ln, "Can't open %s", argv[1]);
44 perror(ln);
45 exit(1);
48 fgets(ln, 255, fi);
50 sscanf(ln, "%d %d %d", &td.x_dim, &td.y_dim, &td.z_dim);
51 for (a = 0, d = 1; d < td.x_dim; a++, d <<= 1);
52 td.x_bits = a;
53 td.x_workspace = d;
55 for (a = 0, d = 1; d < td.y_dim; a++, d <<= 1);
56 td.y_bits = a;
57 td.y_workspace = d;
59 for (a = 0, d = 1; d < td.z_dim; a++, d <<= 1);
60 td.z_bits = a;
61 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 fclose(fi);
67 // Prepare bitstream for unpacking
68 stat(argv[1], &st);
69 len = st.st_size;
70 lensub = len;
71 fd = open(argv[1], O_RDONLY);
72 if ((data = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0)) == (void*)-1) {
73 close(fd);
74 perror("Can't mmap file");
75 exit(1);
77 for (dp = data; *dp != '\n' && lensub; dp++, lensub--);
78 dp++;
79 lensub--;
80 _oggpack_readinit(&o, dp, lensub);
82 // Unpack bitstream
83 unpackblock(&td, &o);
85 // iDWT on bitstream
86 dims[0] = td.x_workspace; dims[1] = td.y_workspace; dims[2] = td.z_workspace;
87 dwt(td.vectors, dims, 3, -1);
89 for (a = 0; a < td.sz_workspace; a++) {
90 if (td.vectors[a] < 0.0)
91 td.vectors[a] = 0.0;
92 if (td.vectors[a] > 255.0)
93 td.vectors[a] = 255.0;
96 // Initialize video
97 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
98 printf("Unable to init SDL: %s\n", SDL_GetError());
99 exit(1);
101 atexit(SDL_Quit);
103 screen = SDL_SetVideoMode(320, 240, 8, SDL_SWSURFACE);
104 if (!screen) {
105 printf("Unable to set 320x240 video: %s\n", SDL_GetError());
106 exit(1);
109 SDL_WM_SetCaption("tarkinplay v0.1beta", NULL);
111 make_palette(screen);
113 i = 0;
114 while (!SDL_QuitRequested()) {
115 printf("Showing frame #%i...\n", i);
117 if (SDL_MUSTLOCK(screen)) {
118 SDL_LockSurface(screen);
121 image = (char *)malloc(td.x_dim * td.y_dim);
123 for (y = 0; y < td.y_dim; y++)
124 for (x = 0; x < td.x_dim; x++)
125 image[x + y * td.x_dim] = td.vectors[x + y * td.x_workspace + i * td.x_workspace * td.y_workspace];
127 memcpy(screen->pixels, image, 320*240);
129 free(image);
131 if (SDL_MUSTLOCK(screen)) {
132 SDL_UnlockSurface(screen);
135 SDL_UpdateRect(screen, 0, 0, 320, 240);
137 SDL_Delay(100);
139 i++;
140 if (i > 31) i = 0;
143 // Shutdown
144 free(td.vectors);
145 free(td.dwtv);
146 munmap(data, len);
147 close(fd);
150 void dieusage()
152 fprintf(stderr, "Usage:\ntarkinplay [filename]\n\n[filename] - Filename with compressed Tarkin stream\n");
153 exit(1);
156 void make_palette(SDL_Surface *screen)
158 int ncolors;
159 int i;
160 SDL_Color *palette;
162 ncolors = screen->format->palette->ncolors;
163 palette = (SDL_Color *)malloc(ncolors * sizeof(SDL_Color));
164 for (i = 0; i < ncolors; i++) {
165 palette[i].r = i;
166 palette[i].g = i;
167 palette[i].b = i;
169 SDL_SetColors(screen, palette, 0, ncolors);