| 1 |
/* |
| 2 |
* FIASCO coder |
| 3 |
* |
| 4 |
* Written by: Ullrich Hafner |
| 5 |
* |
| 6 |
* This file is part of FIASCO (Fractal Image And Sequence COdec) |
| 7 |
* Copyright (C) 1994-2000 Ullrich Hafner |
| 8 |
*/ |
| 9 |
|
| 10 |
/* |
| 11 |
* $Date: 2000/10/28 17:39:29 $ |
| 12 |
* $Author: hafner $ |
| 13 |
* $Revision: 5.4 $ |
| 14 |
* $State: Exp $ |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "config.h" |
| 18 |
#include <stdlib.h> |
| 19 |
#include <string.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include "pm_c_util.h" |
| 23 |
#include "mallocvar.h" |
| 24 |
#include "pnm.h" |
| 25 |
|
| 26 |
#include "types.h" |
| 27 |
#include "macros.h" |
| 28 |
|
| 29 |
#include "misc.h" |
| 30 |
#include "params.h" |
| 31 |
#include "fiasco.h" |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
static param_t params[] = { |
| 36 |
/* Options for standard user */ |
| 37 |
{"image-name", "FILE", 'i', PSTR, {0}, NULL, |
| 38 |
"Compress raw PPM/PGM image(s) `%s'."}, |
| 39 |
{"output-name", "FILE", 'o', PSTR, {0}, "-", |
| 40 |
"Write automaton to `%s' (`-' means stdout)."}, |
| 41 |
{"quality", "REAL", 'q', PFLOAT, {0}, "20.0", |
| 42 |
"Set quality of compression to `%s'."}, |
| 43 |
{"title", "NAME", 't', PSTR, {0}, "", |
| 44 |
"Set title of FIASCO stream to `%s'."}, |
| 45 |
{"comment", "NAME", 'c', PSTR, {0}, "", |
| 46 |
"Set comment of FIASCO stream to `%s'."}, |
| 47 |
{"chroma-qfactor", "REAL", '\0', PFLOAT, {0}, "2", |
| 48 |
"Decrease chroma band quality `%s' times."}, |
| 49 |
{"basis-name", "FILE", '\0', PSTR, {0}, "small.fco", |
| 50 |
"Preload basis `%s' into FIASCO."}, |
| 51 |
{"optimize", "NUM", 'z', PINT, {0}, "0", |
| 52 |
"Set optimization level to `%s'."}, |
| 53 |
{"dictionary-size", "NUM", '\0', PINT, {0}, "10000", |
| 54 |
"Set max# size of dictionary to `%s'."}, |
| 55 |
{"chroma-dictionary", "NUM", '\0', PINT, {0}, "40", |
| 56 |
"Set max# size of chroma dictionary to `%s'.."}, |
| 57 |
{"min-level", "NUM", '\0', PINT, {0}, "6", |
| 58 |
"Start prediction on block level `%s'."}, |
| 59 |
{"max-level", "NUM", '\0', PINT, {0}, "10", |
| 60 |
"Stop prediction on block level `%s'."}, |
| 61 |
{"tiling-exponent", "NUM", '\0', PINT, {0}, "4", |
| 62 |
"Set exponent of image permutation to `%s'."}, |
| 63 |
{"tiling-method", "NAME", '\0', PSTR, {0}, "desc-variance", |
| 64 |
"Set type of permutation to `%s'."}, |
| 65 |
{"rpf-range", "REAL", '\0', PFLOAT, {0}, "1.5", |
| 66 |
"Set quantization range to `%s'."}, |
| 67 |
{"rpf-mantissa", "NUM", '\0', PINT, {0}, "3", |
| 68 |
"Set quantization mantissa to `%s' bits."}, |
| 69 |
{"dc-rpf-range", "REAL", '\0', PFLOAT, {0}, "1", |
| 70 |
"Set quant. range (DC part) to `%s'."}, |
| 71 |
{"dc-rpf-mantissa", "NUM", '\0', PINT, {0}, "5", |
| 72 |
"Set quant. mantissa (DC part) to `%s' bits."}, |
| 73 |
{"pattern", "NAME", '\0', PSTR, {0}, "ippppppppp", |
| 74 |
"Set frame type sequence to `%s'."}, |
| 75 |
{"fps", "NUM", '\0', PINT, {0}, "25", |
| 76 |
"Set display rate to `%s' frames per second."}, |
| 77 |
{"half-pixel", NULL, '\0', PFLAG, {0}, "FALSE", |
| 78 |
"Use half-pixel precision for mc."}, |
| 79 |
{"cross-B-search", NULL, '\0', PFLAG, {0}, "FALSE", |
| 80 |
"Use cross-B-search for interpolated mc."}, |
| 81 |
{"B-as-past-ref", NULL, '\0', PFLAG, {0}, "FALSE", |
| 82 |
"Use B-frames as reference images." }, |
| 83 |
{"prediction", NULL, '\0', PFLAG, {0}, "FALSE", |
| 84 |
"Use additional predictive coding."}, |
| 85 |
{"progress-meter", "NUM", '\0', PINT, {0}, "2", |
| 86 |
"Set type of progress meter to `%s'."}, |
| 87 |
{"smooth", "NUM", '\0', PINT, {0}, "70", |
| 88 |
"Smooth image(s) by factor `%s' (0-100)"}, |
| 89 |
#if 0 |
| 90 |
/* |
| 91 |
* Options currently not activated (maybe in future versions of FIASCO) |
| 92 |
*/ |
| 93 |
{"min-level", "NUM", 'm', PINT, {0}, "4", |
| 94 |
"Start compression on block level `%s'."}, |
| 95 |
{"max-level", "NUM", 'M', PINT, {0}, "12", |
| 96 |
"Stop compression on block level `%s'."}, |
| 97 |
{"max-elements", "NUM", 'N', PINT, {0}, "8", |
| 98 |
"Set max# of elements in an approx. to `%s'." }, |
| 99 |
{"domain-pool", "NAME", '\0', PSTR, {0}, "rle", |
| 100 |
"Set domain pool of r-lc to `%s'."}, |
| 101 |
{"coeff", "NAME", '\0', PSTR, {0}, "adaptive", |
| 102 |
"Set coefficients model to `%s'."}, |
| 103 |
/* DELTA APPROXIMATION */ |
| 104 |
{"d-domain-pool", "NAME", '\0', PSTR, {0}, "rle", |
| 105 |
"Set domain pool of d-lc to `%s'."}, |
| 106 |
{"d-coeff", "NAME", '\0', PSTR, {0}, "adaptive", |
| 107 |
"Set d coefficients model to `%s'."}, |
| 108 |
{"d-range", "REAL", '\0', PFLOAT, {0}, "1.5", |
| 109 |
"Set range of RPF for delta lc to `%s'."}, |
| 110 |
{"d-mantissa", "NUM", '\0', PINT, {0}, "3", |
| 111 |
"Set #m-bits of RPF for delta lc to `%s'."}, |
| 112 |
{"d-dc-range", "REAL", '\0', PFLOAT, {0}, "1", |
| 113 |
"Set DC range of RPF of delta lc to `%s'."}, |
| 114 |
{"d-dc-mantissa", "NUM", '\0', PINT, {0}, "5", |
| 115 |
"Set #m-bits of delta RPF for DC domain to `%s'."}, |
| 116 |
/* ADVANCED */ |
| 117 |
{"images-level", "NUM", '\0', PINT, {0}, "5", |
| 118 |
"Compute state images up to level `%s'."}, |
| 119 |
{"delta-domains", NULL, '\0', PFLAG, {0}, "FALSE", |
| 120 |
"Use delta domains every time."}, |
| 121 |
{"normal-domains", NULL, '\0', PFLAG, {0}, "FALSE", |
| 122 |
"Use normal domains every time."}, |
| 123 |
/* VIDEO COMPRESSION */ |
| 124 |
{"smooth", "REAL", 's', PFLOAT, {0}, "1.0", |
| 125 |
"Smooth frames by factor `%s' (0.5 - 1.0)"}, |
| 126 |
{"reference-frame", "FILE", '\0', PSTR, {0}, NULL, |
| 127 |
"Use PPM/PGM image `%s' as reference frame."}, |
| 128 |
#endif |
| 129 |
{NULL, NULL, 0, PSTR, {0}, NULL, NULL } |
| 130 |
}; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
static void |
| 135 |
checkargs(int argc, |
| 136 |
const char ** argv, |
| 137 |
const char *** const imageTemplateListP, |
| 138 |
char ** const wfa_name, |
| 139 |
float * const quality, |
| 140 |
fiasco_c_options_t ** const options) { |
| 141 |
/*---------------------------------------------------------------------------- |
| 142 |
Check validness of command line parameters and of the parameter files. |
| 143 |
|
| 144 |
Return value: |
| 145 |
1 on success |
| 146 |
0 otherwise |
| 147 |
-----------------------------------------------------------------------------*/ |
| 148 |
int optind; /* last processed commandline param */ |
| 149 |
char * image_name; /* filename given by option '--input_name' */ |
| 150 |
const char ** imageTemplateList; |
| 151 |
|
| 152 |
optind = parseargs(params, argc, argv, |
| 153 |
"Compress raw PPM/PGM image FILEs to a FIASCO file.", |
| 154 |
"With no image FILE, or if FILE is -, " |
| 155 |
"read standard input.\n" |
| 156 |
"FILE must be either a filename" |
| 157 |
" or an image template of the form:\n" |
| 158 |
"`prefix[start-end{+,-}step]suffix'\n" |
| 159 |
"e.g., img0[12-01-1].pgm is substituted by" |
| 160 |
" img012.pgm ... img001.pgm\n\n" |
| 161 |
"Environment:\n" |
| 162 |
"FIASCO_DATA Search and save path for FIASCO files. " |
| 163 |
"Default: ./\n" |
| 164 |
"FIASCO_IMAGES Search path for image files. " |
| 165 |
"Default: ./", " [FILE]...", |
| 166 |
FIASCO_SHARE, "system.fiascorc", ".fiascorc"); |
| 167 |
|
| 168 |
/* Default options */ |
| 169 |
image_name = (char *) parameter_value(params, "image-name"); |
| 170 |
*wfa_name = (char *) parameter_value(params, "output-name"); |
| 171 |
for (;;) { |
| 172 |
*quality = * (float *) parameter_value(params, "quality"); |
| 173 |
if (*quality > 100) |
| 174 |
pm_message("Typical range of quality: (0,100]. " |
| 175 |
"Expect some trouble on slow machines."); |
| 176 |
if (*quality > 0) |
| 177 |
break; |
| 178 |
ask_and_set(params, "quality", |
| 179 |
"Please enter coding quality 'q' ('q' > 0): "); |
| 180 |
} |
| 181 |
|
| 182 |
/* Non-option command line params */ |
| 183 |
if (optind < argc) { |
| 184 |
unsigned int i; |
| 185 |
if (image_name) |
| 186 |
pm_error("Multiple image name template arguments. " |
| 187 |
"Option --image-name already specified with '%s'", |
| 188 |
image_name); |
| 189 |
|
| 190 |
MALLOCARRAY_NOFAIL(imageTemplateList, argc - optind + 1); |
| 191 |
|
| 192 |
for (i = 0; optind < argc; ++i, ++optind) |
| 193 |
imageTemplateList[i] = argv[optind]; |
| 194 |
imageTemplateList[i] = NULL; |
| 195 |
} else { |
| 196 |
/* option -i image_name */ |
| 197 |
|
| 198 |
MALLOCARRAY_NOFAIL(imageTemplateList, 2); |
| 199 |
|
| 200 |
imageTemplateList[0] = image_name; |
| 201 |
imageTemplateList[1] = NULL; |
| 202 |
} |
| 203 |
/* Additional options ... (have to be set with the fiasco_set_... methods) |
| 204 |
*/ |
| 205 |
{ |
| 206 |
*options = fiasco_c_options_new(); |
| 207 |
|
| 208 |
{ |
| 209 |
const char * const pattern = parameter_value(params, "pattern"); |
| 210 |
|
| 211 |
if (!fiasco_c_options_set_frame_pattern (*options, pattern)) |
| 212 |
pm_error("%s", fiasco_get_error_message ()); |
| 213 |
} |
| 214 |
|
| 215 |
{ |
| 216 |
const char *const basis = parameter_value(params, "basis-name"); |
| 217 |
|
| 218 |
if (!fiasco_c_options_set_basisfile (*options, basis)) |
| 219 |
pm_error("%s", fiasco_get_error_message ()); |
| 220 |
} |
| 221 |
|
| 222 |
{ |
| 223 |
int const n = |
| 224 |
*(int *)parameter_value(params, "chroma-dictionary"); |
| 225 |
float const q = |
| 226 |
*(float *)parameter_value(params, "chroma-qfactor"); |
| 227 |
|
| 228 |
if (!fiasco_c_options_set_chroma_quality(*options, q, MAX(0, n))) |
| 229 |
pm_error("%s", fiasco_get_error_message ()); |
| 230 |
} |
| 231 |
|
| 232 |
{ |
| 233 |
int const n = *((int *)parameter_value(params, "smooth")); |
| 234 |
|
| 235 |
if (!fiasco_c_options_set_smoothing(*options, MAX(0, n))) |
| 236 |
pm_error("%s", fiasco_get_error_message ()); |
| 237 |
} |
| 238 |
|
| 239 |
{ |
| 240 |
int const n = *(int *)parameter_value(params, "progress-meter"); |
| 241 |
fiasco_progress_e type = (n < 0) ? |
| 242 |
FIASCO_PROGRESS_NONE : (fiasco_progress_e) n; |
| 243 |
|
| 244 |
if (!fiasco_c_options_set_progress_meter(*options, type)) |
| 245 |
pm_error("%s", fiasco_get_error_message ()); |
| 246 |
} |
| 247 |
|
| 248 |
{ |
| 249 |
const char * const t = parameter_value(params, "title"); |
| 250 |
|
| 251 |
if (strlen(t) > 0 && !fiasco_c_options_set_title(*options, t)) |
| 252 |
pm_error("%s", fiasco_get_error_message ()); |
| 253 |
} |
| 254 |
|
| 255 |
{ |
| 256 |
const char * const c = parameter_value(params, "comment"); |
| 257 |
|
| 258 |
if (strlen (c) > 0 && !fiasco_c_options_set_comment (*options, c)) |
| 259 |
pm_error("%s", fiasco_get_error_message ()); |
| 260 |
} |
| 261 |
|
| 262 |
{ |
| 263 |
int const exponentOpt = |
| 264 |
*(int *)parameter_value(params, "tiling-exponent"); |
| 265 |
const char * const opt = parameter_value (params, "tiling-method"); |
| 266 |
|
| 267 |
int succeeded; |
| 268 |
fiasco_tiling_e method; |
| 269 |
|
| 270 |
if (strcaseeq(opt, "desc-variance")) |
| 271 |
method = FIASCO_TILING_VARIANCE_DSC; |
| 272 |
else if (strcaseeq(opt, "asc-variance")) |
| 273 |
method = FIASCO_TILING_VARIANCE_ASC; |
| 274 |
else if (strcaseeq(opt, "asc-spiral")) |
| 275 |
method = FIASCO_TILING_SPIRAL_ASC; |
| 276 |
else if (strcaseeq(opt, "dsc-spiral")) |
| 277 |
method = FIASCO_TILING_SPIRAL_DSC; |
| 278 |
else |
| 279 |
pm_error("Invalid tiling method '%s' specified.", opt); |
| 280 |
|
| 281 |
succeeded = |
| 282 |
fiasco_c_options_set_tiling(*options, |
| 283 |
method, MAX(0, exponentOpt)); |
| 284 |
if (!succeeded) |
| 285 |
pm_error("%s", fiasco_get_error_message ()); |
| 286 |
} |
| 287 |
|
| 288 |
{ |
| 289 |
int const dictionarySize = |
| 290 |
*(int *)parameter_value(params, "dictionary-size"); |
| 291 |
int const optimizeOpt = |
| 292 |
*(int *)parameter_value(params, "optimize"); |
| 293 |
|
| 294 |
int maxBlockLevel; |
| 295 |
int minBlockLevel; |
| 296 |
int maxElements; |
| 297 |
int optimizationLevel; |
| 298 |
bool succeeded; |
| 299 |
|
| 300 |
if (optimizeOpt <= 0) { |
| 301 |
optimizationLevel = 0; |
| 302 |
maxBlockLevel = 10; |
| 303 |
minBlockLevel = 6; |
| 304 |
maxElements = 3; |
| 305 |
} else { |
| 306 |
optimizationLevel = optimizeOpt - 1; |
| 307 |
maxBlockLevel = 12; |
| 308 |
minBlockLevel = 4; |
| 309 |
maxElements = 5; |
| 310 |
} |
| 311 |
|
| 312 |
fiasco_c_options_set_optimizations( |
| 313 |
*options, minBlockLevel, maxBlockLevel, maxElements, |
| 314 |
MAX(0, dictionarySize), optimizationLevel, &succeeded); |
| 315 |
if (!succeeded) |
| 316 |
pm_error("%s", fiasco_get_error_message ()); |
| 317 |
} |
| 318 |
{ |
| 319 |
int const M = *(int *)parameter_value(params, "max-level"); |
| 320 |
int const m = *(int *)parameter_value(params, "min-level"); |
| 321 |
int const p = *(int *)parameter_value(params, "prediction"); |
| 322 |
|
| 323 |
if (!fiasco_c_options_set_prediction (*options, |
| 324 |
p, MAX(0, m), MAX(0, M))) |
| 325 |
pm_error("%s", fiasco_get_error_message ()); |
| 326 |
} |
| 327 |
{ |
| 328 |
float const r = |
| 329 |
*(float *)parameter_value(params, "rpf-range"); |
| 330 |
float const dcR = |
| 331 |
*(float *)parameter_value(params, "dc-rpf-range"); |
| 332 |
int const m = |
| 333 |
*(int *)parameter_value(params, "rpf-mantissa"); |
| 334 |
int const dcM = |
| 335 |
*(int *)parameter_value(params, "dc-rpf-mantissa"); |
| 336 |
|
| 337 |
fiasco_rpf_range_e range, dcRange; |
| 338 |
|
| 339 |
if (r < 1) |
| 340 |
range = FIASCO_RPF_RANGE_0_75; |
| 341 |
else if (r < 1.5) |
| 342 |
range = FIASCO_RPF_RANGE_1_00; |
| 343 |
else if (r < 2.0) |
| 344 |
range = FIASCO_RPF_RANGE_1_50; |
| 345 |
else |
| 346 |
range = FIASCO_RPF_RANGE_2_00; |
| 347 |
|
| 348 |
if (dcR < 1) |
| 349 |
dcRange = FIASCO_RPF_RANGE_0_75; |
| 350 |
else if (dcR < 1.5) |
| 351 |
dcRange = FIASCO_RPF_RANGE_1_00; |
| 352 |
else if (dcR < 2.0) |
| 353 |
dcRange = FIASCO_RPF_RANGE_1_50; |
| 354 |
else |
| 355 |
dcRange = FIASCO_RPF_RANGE_2_00; |
| 356 |
|
| 357 |
if (!fiasco_c_options_set_quantization(*options, |
| 358 |
MAX(0, m), range, |
| 359 |
MAX(0, dcM), dcRange)) |
| 360 |
pm_error("%s", fiasco_get_error_message ()); |
| 361 |
} |
| 362 |
|
| 363 |
if (fiasco_get_verbosity() == FIASCO_ULTIMATE_VERBOSITY) |
| 364 |
write_parameters(params, stderr); |
| 365 |
} |
| 366 |
*imageTemplateListP = imageTemplateList; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
int |
| 372 |
main(int argc, const char **argv) { |
| 373 |
|
| 374 |
char const ** image_template; /* template for input image files */ |
| 375 |
char * wfa_name; /* filename of output WFA */ |
| 376 |
float quality; /* approximation quality */ |
| 377 |
fiasco_c_options_t * options; /* additional coder options */ |
| 378 |
int retval; |
| 379 |
|
| 380 |
pm_proginit(&argc, argv); |
| 381 |
|
| 382 |
checkargs(argc, argv, &image_template, &wfa_name, &quality, &options); |
| 383 |
|
| 384 |
if (fiasco_coder(image_template, wfa_name, quality, options)) |
| 385 |
retval = 0; |
| 386 |
else { |
| 387 |
pm_message("Encoding failed. %s", fiasco_get_error_message()); |
| 388 |
retval = 1; |
| 389 |
} |
| 390 |
return retval; |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|