1 /*
2 * vic2jfif.c --
3 *
4 * FIXME: This file needs a description here.
5 *
6 * Copyright (c) 1996-2002 The Regents of the University of California.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * A. Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * B. Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * C. Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
22 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <stdio.h>
35 #include <sys/file.h>
36
37 #include "../vic-1/packet.h"
38
39 struct hdr {
40 int cc;
41 struct vicfrm_jpg ph;
42 };
43
44 grab(int fd, u_char* buffer)
45 {
46 int cc;
47 u_char *ip;
48 struct hdr* p = (struct hdr*)buffer;
49
50 cc = read(fd, (char*)buffer, sizeof(*p));
51 if (cc < 0) {
52 perror("read");
53 exit(1);
54 }
55 if (cc < sizeof(*p))
56 return (-1);
57 cc = ntohl(p->cc);
58 if (cc < 0 || cc > 100000) {
59 fprintf(stderr, "impossible length\n");
60 exit(1);
61 }
62 cc -= sizeof(struct vicfrm_jpg);
63 ip = (char *)(p + 1);
64 if (read(fd, (char*)ip, cc) <= 0)
65 return (-1);
66
67 /* Remove the trailing zeros inserted by the DMA process */
68 ip += cc;
69 if (ip[-1] == 0) {
70 while(*--ip == 0)
71 ;
72 ip += 2;
73 }
74 *ip++ = 0xff;
75 *ip++ = 0xd9;/* EOI */
76
77 return (ip - buffer);
78 }
79
80 /*
81 * Write the first frame of a vic dump file to
82 * stdout, as a JFIF format file.
83 */
84 main(int argc, char **argv)
85 {
86 int fd, cc;
87 int start = 0;
88 int end = 0;
89 struct hdr *h;
90 u_char *p;
91 int op, n;
92 extern int optind, opterr;
93 extern char *optarg;
94 char *ofile = "out";
95 u_char buffer[32 * 1024];
96
97 opterr = 0;
98 while ((op = getopt(argc, argv, "n:s:e:o:")) != -1) {
99 switch (op) {
100
101 case 'n':
102 start = end = atoi(optarg);
103 break;
104
105 case 's':
106 start = atoi(optarg);
107 break;
108
109 case 'e':
110 end = atoi(optarg);
111 break;
112
113 case 'o':
114 ofile = optarg;
115 if (strlen(ofile) > 200)
116 exit(1);
117 break;
118 }
119 }
120 argc -= optind;
121 argv += optind;
122
123 if (argc != 1)
124 exit(1);
125
126 fd = open(*argv, O_RDONLY);
127 if (fd < 0) {
128 perror(*argv);
129 exit(1);
130 }
131 n = 0;
132 while ((cc = grab(fd, buffer)) > 0) {
133 int out;
134 char fname[256];
135
136 ++n;
137 if (n < start)
138 continue;
139 if (n > end)
140 break;
141
142 sprintf(fname, "%s-%02d.jpg", ofile, n);
143 out = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
144 if (out < 0) {
145 perror(fname);
146 exit(1);
147 }
148
149 h = (struct hdr *)buffer;
150 dumpJfifFrame(out, (char *)(h + 1), cc - sizeof(*h),
151 ntohs(h->ph.width), ntohs(h->ph.height),
152 ntohs(h->ph.quantization), 0);
153 close(out);
154 }
155 close(fd);
156
157 exit(0);
158 }
159
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.