File: str2ppm.c

package info (click to toggle)
vstream 0.4.5-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 216 kB
  • ctags: 812
  • sloc: ansic: 2,615; makefile: 72
file content (116 lines) | stat: -rw-r--r-- 2,550 bytes parent folder | download
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* (C) 1998 Justin Schoeman */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

extern unsigned char *getframe(char *fname);

void usage(void)
{
 printf("Usage:\n\n");
 printf(" str2ppm [-str <output stream 1> [-str <...>]] <parameter file>\n\n");
 exit(0);
}


int main(int argc, char** argv)
{
 FILE* out;
 FILE* par;
 int width, height; 
 char fname[256], fstr[256], tmp[256];
 unsigned int maxframe, i;
 unsigned char *sframe, *oframe;
 int ostr[256], ostrnum, ostrcur=0, temp, p;
 
 for(i=1; (i<argc)&&(argv[i][0]=='-'); i++)
 {
  if(strcmp(argv[i],"-str")==0)
  {
   if(argc<(i+2))usage();
   ostr[ostrcur]=open(argv[++i], O_RDWR|O_TRUNC|O_CREAT, S_IRUSR|S_IWUSR);
   fprintf(stderr, "Adding output stream %s.\n", argv[i]);
   if(ostr[ostrcur++]<0)
   {
    perror("opening out stream");
    exit(-1);
   }
  }
 }
 ostrnum=ostrcur;
 ostrcur=0;
 if(argc<(i+1))usage();
 par=fopen(argv[i],"r");
 fgets(tmp, 256, par);
 fgets(fstr, 256, par);
 fstr[strlen(fstr)-1]='\0';
 fgets(tmp, 256, par);
 fgets(tmp, 256, par);
 fgets(tmp, 256, par);
 fgets(tmp, 256, par);
 fgets(tmp, 256, par);
 fgets(tmp, 256, par);
 sscanf(tmp, "%u", &maxframe);
 fgets(tmp, 256, par);
 fgets(tmp, 256, par);
 fgets(tmp, 256, par);
 fgets(tmp, 256, par);
 fgets(tmp, 256, par);
 fgets(tmp, 256, par);
 fgets(tmp, 256, par);
 sscanf(tmp, "%u", &width);
 fgets(tmp, 256, par);
 sscanf(tmp, "%u", &height);
 fclose(par);

 oframe=(unsigned char *)malloc(width*height*2);
 if(!oframe)
 {
  perror("allocating output buffer");
  exit(-1);
 }
 
 if(ostrnum!=0)
 {
  write(ostr[0], &width, sizeof(width));
  write(ostr[0], &height, sizeof(height));
  maxframe=maxframe*2;
  write(ostr[0], &maxframe, sizeof(maxframe));
  maxframe=maxframe/2;
 }
 
 for(i=0; i<maxframe; i++)
 {
  sprintf(fname, fstr, i);
  sframe=getframe(fname);
  if(ostrnum==0)
  {
   sprintf(tmp,"%s.ppm", fname);
   out=fopen(tmp,"w");
   fprintf(out, "P6\n%d %d\n255\n", width, height);
   fwrite(sframe, width*height*3, 1, out);
   fclose(out);
  } else
  {
   i=i*2;
   write(ostr[ostrcur], &i, sizeof(i));
   i=i/2;
   for(p=0; p<(width*height); p++)
   {
    temp = (int)((int)sframe[(p*3)+2] >> 3);
    temp |= (int)(((int)sframe[(p*3)+1] >> 2) << 5);
    temp |= (int)(((int)sframe[(p*3)+0] >> 3) << 11);
    oframe[p*2]=(unsigned char)temp&0xff;
    oframe[(p*2)+1]=(unsigned char)(temp>>8);
   }
   write(ostr[ostrcur], oframe, width*height*2);
   if(++ostrcur==ostrnum)ostrcur=0;
  }
 }
 return 0;
}