summaryrefslogtreecommitdiffstats
path: root/Demo/sgi/video/v2i.c
blob: 5f8f3b5dec3c25f8338a607259eca3593c833e01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* Convert the first image of a CMIF video movie file to SGI .rgb format.
   usage: v2i videofile imagefile [planemask]
   link with -limage
*/

#include <stdio.h>
#include <gl/image.h>

long bm[1280];
short rb[1280], gb[1280], bb[1280];
long w, h, pf;

#define R(comp) ((comp) & 0xff)
#define G(comp) (((comp)>>8) & 0xff)
#define B(comp) (((comp)>>16) & 0xff)

main(argc, argv)
    char **argv;
{
    char lbuf[100];
    int x, y;
    int i;
    IMAGE * of;
    int pmask;

    if( argc != 3 && argc != 4) {
       fprintf(stderr, "Usage: v2i videofile imgfile [planemask]\n");
       exit(2);
    }
    if ( argc == 4)
	pmask = atoi(argv[3]);
    else
	pmask = 7;
    if ( freopen(argv[1], "r", stdin) == NULL ) {
	perror(argv[1]);
	exit(1);
    }
    if (fgets(lbuf, sizeof lbuf, stdin) == NULL) {
	    fprintf(stderr, "Immediate EOF\n");
	    exit(1);
    }
    if (strncmp(lbuf, "CMIF", 4) == 0) {
	    /* Skip optional header line */
	    if (fgets(lbuf, sizeof lbuf, stdin) == NULL) {
		    fprintf(stderr, "Immediate EOF after header\n");
		    exit(1);
	    }
    }
    pf = 2; /* Default */
    if ( sscanf(lbuf, "(%d,%d,%d)", &w, &h, &pf) < 2) {
	fprintf(stderr, "%s: bad size spec: %s\n", argv[0], lbuf);
	exit(1);
    }
    fgets(lbuf, sizeof lbuf, stdin); /* Skip time info */
    if ( w > 1280 ) {
	fprintf(stderr, "%s: Sorry, too wide\n", argv[0]);
	exit(1);
    }
    if ( (of=iopen(argv[2], "w", RLE(1), 3, w, h, 3)) == 0) {
	perror(argv[2]);
	exit(1);
    }
    for( y=0; y<h; y++) {
	if( fread(bm, sizeof(long), w, stdin) != w) {
	    fprintf(stderr, "%s: short read\n", argv[0]);
	    exit(1);
	}
	for( x=0; x<w; x++) {
	    if ( pmask & 1) rb[x] = R(bm[x]);
	    if ( pmask & 2) gb[x] = G(bm[x]);
	    if ( pmask & 4) bb[x] = B(bm[x]);
	}
	putrow(of, rb, y, 0);
	putrow(of, gb, y, 1);
	putrow(of, bb, y, 2);
    }
    iclose(of);
    exit(0);
}