Debian Codegraph @

converter/other/fiasco/fiascotopnm.c

raw
1 /*
2 * Decode WFA-files
3 *
4 * Written by: Ullrich Hafner
5 * Michael Unger
6 *
7 * This file is part of FIASCO (Fractal Image And Sequence COdec)
8 * Copyright (C) 1994-2000 Ullrich Hafner
9 */
10
11 /*
12 * $Date: 2000/10/28 17:39:29 $
13 * $Author: hafner $
14 * $Revision: 5.7 $
15 * $State: Exp $
16 */
17
18 #define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */
19 #define _BSD_SOURCE 1 /* Make sure strdup() is in string.h */
20 #define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */
21
22 #include "config.h"
23 #include "pnm.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <math.h>
28 #include <getopt.h>
29
30 #include "mallocvar.h"
31 #include "nstring.h"
32
33 #include "types.h"
34 #include "macros.h"
35
36 #include "misc.h"
37 #include "params.h"
38 #include "fiasco.h"
39
40
41
42 static void
43 getOutputTemplate(const char * const imageName,
44 const char * const wfaName,
45 bool_t const color,
46 const char ** const basename,
47 const char ** const suffixP) {
48 /*----------------------------------------------------------------------------
49
50 Generate image filename template for output of image sequences.
51 'wfa_name' is the filename of the WFA stream.
52 Images are either saved with filename 'basename'.'suffix' (still images)
53 or 'basename'.%03d.'suffix' (videos).
54 -----------------------------------------------------------------------------*/
55 char * suffixLoc;
56
57 /* Generate filename template */
58 if (!imageName || streq(imageName, "") || streq(imageName, "-")) {
59 if (!wfaName || streq(wfaName, "-"))
60 *basename = strdup("stdin");
61 else
62 *basename = strdup(wfaName);
63 suffixLoc = NULL;
64 } else {
65 *basename = strdup(imageName);
66 suffixLoc = strrchr(*basename, '.');
67 }
68
69 if (suffixLoc) {
70 /* found name 'basename.suffix' */
71
72 *suffixLoc = '\0'; /* remove dot from *basename*/
73
74 if (*(suffixLoc+1) == '\0')
75 *suffixP = strdup(color ? "ppm" : "pgm");
76 else
77 *suffixP = strdup(suffixLoc + 1);
78 } else /* no suffix found, generate one */
79 *suffixP = strdup(color ? "ppm" : "pgm");
80 }
81
82
83
84 static param_t params [] = {
85 {"output", "FILE", 'o', PSTR, {0}, "-",
86 "Write raw PNM frame(s) to `%s'."},
87 {"double", NULL, 'd', PFLAG, {0}, "FALSE",
88 "Interpolate images to double size before display."},
89 {"fast", NULL, 'r', PFLAG, {0}, "FALSE",
90 "Use 4:2:0 format for fast, low quality output."},
91 {"panel", NULL, 'p', PFLAG, {0}, "FALSE",
92 "Display control panel."},
93 {"magnify", "NUM", 'm', PINT, {0}, "0",
94 "Magnify/reduce image size by a factor of 4^`%s'."},
95 {"framerate", "NUM", 'F', PINT, {0}, "-1",
96 "Set display rate to `%s' frames per second."},
97 {"smoothing", "NUM", 's', PINT, {0}, "-1",
98 "Smooth image(s) by factor `%s' (0-100)"},
99 {NULL, NULL, 0, 0, {0}, NULL, NULL }
100 };
101
102 static int
103 checkargs(int argc,
104 const char ** const argv,
105 bool_t * const double_resolution,
106 bool_t * const panel,
107 int * const fps,
108 const char ** const image_name,
109 fiasco_d_options_t ** const options) {
110 /*----------------------------------------------------------------------------
111 Check validness of command line parameters and of the parameter files.
112
113 Return value: index in argv of the first argv-element that is not an option.
114
115 Side effects:
116 -----------------------------------------------------------------------------*/
117 int optind; /* last processed commandline param */
118
119 optind = parseargs(params, argc, argv,
120 "Decode FIASCO-FILEs and write frame(s) to disk.",
121 "With no FIASCO-FILE, or if FIASCO-FILE is -, "
122 "read standard input.\n"
123 "Environment:\n"
124 "FIASCO_DATA Search path for automata files. "
125 "Default: ./\n"
126 "FIASCO_IMAGES Save path for image files. "
127 "Default: ./", " [FIASCO-FILE]...",
128 FIASCO_SHARE, "system.fiascorc", ".fiascorc");
129
130 *image_name = (char *) parameter_value (params, "output");
131 *double_resolution = *((bool_t *) parameter_value (params, "double"));
132 *panel = *((bool_t *) parameter_value (params, "panel"));
133 *fps = *((int *) parameter_value (params, "framerate"));
134
135 /* Additional options ... (have to be set with the fiasco_set_... methods)
136 */
137 *options = fiasco_d_options_new();
138
139 {
140 int const n = *((int *)parameter_value(params, "smoothing"));
141
142 if (!fiasco_d_options_set_smoothing(*options, MAX(-1, n)))
143 pm_error("%s", fiasco_get_error_message());
144 }
145
146 {
147 int const n = *((int *)parameter_value(params, "magnify"));
148
149 if (!fiasco_d_options_set_magnification(*options, n))
150 pm_error("%s", fiasco_get_error_message());
151 }
152
153 {
154 bool_t const n = *((bool_t *)parameter_value(params, "fast"));
155
156 if (!fiasco_d_options_set_4_2_0_format(*options, n > 0 ? YES : NO))
157 pm_error("%s", fiasco_get_error_message ());
158 }
159
160 return optind;
161 }
162
163
164
165 static void
166 video_decoder(const char * const wfa_name,
167 const char * const image_name,
168 bool_t const panel,
169 bool_t const double_resolution,
170 int const fpsArg,
171 fiasco_d_options_t * const options) {
172 do {
173 int fps;
174 unsigned int width, height;
175 unsigned int frames;
176 unsigned int n;
177 fiasco_decoder_t * decoder_state;
178 char * filename;
179 const char * basename; /* basename of decoded frame */
180 const char * suffix; /* suffix of decoded frame */
181 unsigned int frame_time;
182
183 if (!(decoder_state = fiasco_decoder_new(wfa_name, options)))
184 pm_error("%s", fiasco_get_error_message ());
185
186 if (fpsArg <= 0) /* then use value of FIASCO file */
187 fps = fiasco_decoder_get_rate(decoder_state);
188 else
189 fps = fpsArg;
190
191 frame_time = fps ? (1000 / fps) : (1000 / 25);
192
193 if (!(width = fiasco_decoder_get_width(decoder_state)))
194 pm_error("%s", fiasco_get_error_message ());
195
196 if (!(height = fiasco_decoder_get_height(decoder_state)))
197 pm_error("%s", fiasco_get_error_message ());
198
199 if (!(frames = fiasco_decoder_get_length(decoder_state)))
200 pm_error("%s", fiasco_get_error_message ());
201
202 getOutputTemplate(image_name, wfa_name,
203 fiasco_decoder_is_color(decoder_state),
204 &basename, &suffix);
205
206 MALLOCARRAY_NOFAIL(filename,
207 strlen (basename) + strlen (suffix) + 2
208 + 10 + (int) (log10 (frames) + 1));
209
210 for (n = 0; n < frames; ++n) {
211 clock_t fps_timer; /* frames per second timer struct */
212
213 prg_timer(&fps_timer, START);
214
215 if (image_name) {
216 /* just write frame to disk */
217 if (frames == 1) {
218 if (streq(image_name, "-"))
219 strcpy(filename, "-");
220 else
221 sprintf(filename, "%s.%s", basename, suffix);
222 } else {
223 pm_message("Decoding frame %d to file `%s.%0*d.%s",
224 n, basename, (int) (log10 (frames - 1) + 1),
225 n, suffix);
226 sprintf(filename, "%s.%0*d.%s", basename,
227 (int) (log10 (frames - 1) + 1), n, suffix);
228 }
229
230 if (!fiasco_decoder_write_frame (decoder_state, filename))
231 pm_error("%s", fiasco_get_error_message ());
232 }
233 if (frame_time) {/* defeat compiler warning */}
234 }
235 free(filename);
236
237 fiasco_decoder_delete(decoder_state);
238 } while (panel);
239 }
240
241
242
243 int
244 main(int argc, const char **argv) {
245
246 const char * imageName; /* output filename */
247 bool_t doubleResolution;/* double resolution of image */
248 bool_t panel; /* control panel */
249 int fps; /* frame display rate */
250 fiasco_d_options_t * options;/* additional coder options */
251 unsigned int lastArg; /* last processed cmdline parameter */
252
253 lastArg = checkargs(argc, argv, &doubleResolution, &panel, &fps,
254 &imageName, &options);
255
256 if (lastArg >= argc)
257 video_decoder("-", imageName, panel, doubleResolution, fps, options);
258 else
259 while (lastArg++ < argc)
260 video_decoder(argv [lastArg - 1], imageName, panel,
261 doubleResolution, fps, options);
262
263 return 0;
264 }
265
266
267