From 651d68f5652c36fbb260334f1a46a2705d8a0de8 Mon Sep 17 00:00:00 2001 From: giraffedata Date: Sat, 23 Jan 2010 22:16:50 +0000 Subject: Release 10.35.73 git-svn-id: http://svn.code.sf.net/p/netpbm/code/super_stable@1103 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- Makefile.version | 2 +- converter/other/fiasco/codec/coder.c | 1472 +++++++++++++++++--------------- converter/other/fiasco/codec/decoder.c | 2 +- converter/other/fiasco/fiasco.h | 15 +- converter/other/fiasco/lib/image.c | 228 +++-- converter/other/fiasco/lib/image.h | 12 +- converter/other/pnmtopalm/palmtopnm.c | 26 +- doc/HISTORY | 13 + editor/pnmconvol.c | 86 +- 9 files changed, 1053 insertions(+), 803 deletions(-) diff --git a/Makefile.version b/Makefile.version index 8b8531e7..03923193 100644 --- a/Makefile.version +++ b/Makefile.version @@ -1,3 +1,3 @@ NETPBM_MAJOR_RELEASE = 10 NETPBM_MINOR_RELEASE = 35 -NETPBM_POINT_RELEASE = 69 +NETPBM_POINT_RELEASE = 73 diff --git a/converter/other/fiasco/codec/coder.c b/converter/other/fiasco/codec/coder.c index df878d87..927ebbda 100644 --- a/converter/other/fiasco/codec/coder.c +++ b/converter/other/fiasco/codec/coder.c @@ -1,8 +1,8 @@ /* - * coder.c: WFA coder toplevel functions + * coder.c: WFA coder toplevel functions * - * Written by: Ullrich Hafner - * + * Written by: Ullrich Hafner + * * This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec) * Copyright (C) 1994-2000 Ullrich Hafner */ @@ -18,6 +18,7 @@ #define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */ #include "config.h" +#include "pm_c_util.h" #include "pnm.h" #include @@ -53,328 +54,320 @@ /***************************************************************************** - global variables + global variables *****************************************************************************/ -const real_t MAXCOSTS = 1e20; +const real_t MAXCOSTS = 1e20; /***************************************************************************** - prototypes + private code *****************************************************************************/ -static coding_t * -alloc_coder (char const * const *inputname, const c_options_t *options, - wfa_info_t *wi); -static void -free_coder (coding_t *c); static char * -get_input_image_name (char const * const *templptr, unsigned ith_image); -static void -video_coder (char const * const *image_template, bitfile_t *output, - wfa_t *wfa, coding_t *c); -static void -frame_coder (wfa_t *wfa, coding_t *c, bitfile_t *output); -static void -print_statistics (char c, real_t costs, const wfa_t *wfa, const image_t *image, - const range_t *range); -static frame_type_e -pattern2type (unsigned frame, const char *pattern); - -/***************************************************************************** - - public code - -*****************************************************************************/ - -int -fiasco_coder (char const * const *inputname, const char *outputname, - float quality, const fiasco_c_options_t *options) +get_input_image_name (char const * const *templptr, unsigned ith_image) /* - * FIASCO coder. - * Encode image or video frames given by the array of filenames `inputname' - * and write to the outputfile `outputname'. - * If 'inputname' = NULL or - * 'inputname [0]' == NULL or - * 'inputname [0]' == "-", read standard input. - * If 'outputname' == NULL or "-", write on standard output. - * 'quality' defines the approximation quality and is 1 (worst) to 100 (best). + * Construct the i-th image-name using templates. + * If the template contains a '[' it must be of the form + * "prefix[start-end{+,-}step]suffix" + * where "{+,-}step" is optional. + * Leading zeros of "start" are significant. + * + * Example: + * "image0[12-01-1].pgm" yields image012.pgm, image011.pgm, ..., image001.pgm * * Return value: - * 1 on success - * 0 otherwise + * ptr to name of image 'ith_image' or NULL if ith_image is out of range. */ { - try - { - char const * const default_input [] = {"-", NULL}; - fiasco_c_options_t *default_options = NULL; - const c_options_t *cop; - char const * const *template; - - /* - * Check parameters - */ - if (!inputname || !inputname [0] || streq (inputname [0], "-")) - template = default_input; - else - template = inputname; - - if (quality <= 0) - { - set_error (_("Compression quality has to be positive.")); - return 0; - } - else if (quality >= 100) - { - warning (_("Quality typically is 1 (worst) to 100 (best).\n" - "Be prepared for a long running time.")); - } - - if (options) - { - cop = cast_c_options ((fiasco_c_options_t *) options); - if (!cop) - return 0; - } - else - { - default_options = fiasco_c_options_new (); - cop = cast_c_options (default_options); - } - - /* - * Open output stream and initialize WFA - */ - { - bitfile_t *output = open_bitfile (outputname, "FIASCO_DATA", - WRITE_ACCESS); - if (!output) - { - set_error (_("Can't write outputfile `%s'.\n%s"), - outputname ? outputname : "", - get_system_error ()); - if (default_options) - fiasco_c_options_delete (default_options); - return 0; - } - else - { - wfa_t *wfa = alloc_wfa (YES); - coding_t *c = alloc_coder (template, cop, wfa->wfainfo); - - read_basis (cop->basis_name, wfa); - append_basis_states (wfa->basis_states, wfa, c); - - c->price = 128 * 64 / quality; - - video_coder (template, output, wfa, c); - - close_bitfile (output); - free_wfa (wfa); - free_coder (c); - - if (default_options) - fiasco_c_options_delete (default_options); - } - } - return 1; - } - catch - { - return 0; - } -} + while (*templptr) + { + const char *template = *templptr++; + char *s; + + if (!(s = strchr (template, '['))) /* no template, just a filename */ + { + if (ith_image == 0) + return strdup (template); + else + ith_image--; + } + else /* template parser */ + { + unsigned n_digits; /* # of digits in image name no. */ + char *s2; + char *suffix; /* characters after template end */ + char prefix [MAXSTRLEN]; /* chars up to the template start */ + unsigned first; /* first image number */ + unsigned last; /* last image number */ + int image_num; /* current image number */ + int increment = 1; + int dummy; + + strcpy (prefix, template); + prefix [s - template] = '\0'; + + for (s2 = ++s, n_digits = 0; ISDIGIT (*s2); s2++, n_digits++) + ; + if (sscanf (s, "%d", &dummy) == 0 || dummy < 0) + error ("Input name template conversion failure.\n" + "Check spelling of template."); + first = (unsigned) dummy; + + if (*s2++ != '-') + error ("Input name template conversion failure.\n" + "Check spelling of template."); + + for (s = s2; ISDIGIT (*s2); s2++) + ; + if (sscanf (s, "%d", &dummy) == 0 || dummy < 0) + error ("Input name template conversion failure.\n" + "Check spelling of template."); + last = (unsigned) dummy; + + if (*s2 == '+' || *s2 == '-') + { + for (s = s2++; ISDIGIT (*s2); s2++) + ; + if (sscanf (s, "%d", &increment) == 0) + error ("Input name template conversion failure.\n" + "Check spelling of template."); + } + if (*s2 != ']') + error ("Input name template conversion failure.\n" + "Check spelling of template."); + suffix = s2 + 1; + + image_num = first + increment * ith_image; + if (image_num < 0) + error ("Input name template conversion failure.\n" + "Check spelling of template."); + + if ((increment > 0 && (unsigned) image_num > last) || + (increment <= 0 && (unsigned) image_num < last)) + { + /* TODO: check this */ + ith_image -= (last - first) / increment + 1; + } + else + { + char formatstr [MAXSTRLEN]; + /* format string for image filename */ + char image_name [MAXSTRLEN]; + /* image file name to be composed */ + + strcpy (formatstr, "%s%0?d%s"); + formatstr [4] = '0' + (char) n_digits; + sprintf (image_name, formatstr, prefix, image_num, suffix); + return strdup (image_name); + } + } + } + return NULL; +} -/***************************************************************************** - private code - -*****************************************************************************/ static coding_t * -alloc_coder (char const * const *inputname, const c_options_t *options, - wfa_info_t *wi) +alloc_coder (char const * const * const inputname, + const c_options_t * const options, + wfa_info_t * const wi, + unsigned int const stdinwidth, + unsigned int const stdinheight, + xelval const stdinmaxval, + int const stdinformat) /* * Coder structure constructor. * Allocate memory for the FIASCO coder structure and * fill in default values specified by 'options'. * * Return value: - * pointer to the new coder structure or NULL on error + * pointer to the new coder structure or NULL on error */ { - coding_t *c = NULL; + coding_t * c; + + c = NULL; /* initial value */ /* * Check whether all specified image frames are readable and of same type */ - { - char *filename; - int width, w = 0, height, h = 0; - bool_t color, c = NO; - unsigned n; + { + char *filename; + int width, w = 0, height, h = 0; + bool_t color, c = NO; + unsigned n; - for (n = 0; (filename = get_input_image_name (inputname, n)); n++) - { - FILE *file; - xelval maxval; - int format; - if (filename == NULL) - file = stdin; - else - file = pm_openr(filename); - pnm_readpnminit(file, &width, &height, &maxval, &format); - color = (PNM_FORMAT_TYPE(format) == PPM_FORMAT) ? TRUE: FALSE; - - pm_close(file); - if (n) - { - if (w != width || h != height || c != color) - { - set_error (_("Format of image frame `%s' doesn't match."), - filename ? filename : ""); - return NULL; - } - } - else - { - w = width; - h = height; - c = color; - } - Free (filename); - } - wi->frames = n; - wi->width = w; - wi->height = h; - wi->color = c; - } + for (n = 0; (filename = get_input_image_name (inputname, n)); n++) + { + xelval maxval; + int format; + if (streq(filename, "-")) { + width = stdinwidth; + height = stdinheight; + maxval = stdinmaxval; + format = stdinformat; + } else { + FILE *file; + + file = pm_openr(filename); + + pnm_readpnminit(file, &width, &height, &maxval, &format); + + pm_close(file); + } + color = (PNM_FORMAT_TYPE(format) == PPM_FORMAT) ? TRUE: FALSE; + + if (n > 0) + { + if (w != width || h != height || c != color) + { + set_error (_("Format of image frame `%s' doesn't match."), + filename ? filename : ""); + return NULL; + } + } + else + { + w = width; + h = height; + c = color; + } + Free (filename); + } + wi->frames = n; + wi->width = w; + wi->height = h; + wi->color = c; + } - /* + /* * Levels ... */ - { - unsigned lx, ly; + { + unsigned lx, ly; - lx = (unsigned) (log2 (wi->width - 1) + 1); - ly = (unsigned) (log2 (wi->height - 1) + 1); + lx = (unsigned) (log2 (wi->width - 1) + 1); + ly = (unsigned) (log2 (wi->height - 1) + 1); - wi->level = max (lx, ly) * 2 - ((ly == lx + 1) ? 1 : 0); - } + wi->level = max (lx, ly) * 2 - ((ly == lx + 1) ? 1 : 0); + } - c = Calloc (1, sizeof (coding_t)); + c = Calloc (1, sizeof (coding_t)); - c->options = *options; - c->options.lc_min_level = max (options->lc_min_level, 3); - c->options.lc_max_level = min (options->lc_max_level, wi->level - 1); + c->options = *options; + c->options.lc_min_level = max (options->lc_min_level, 3); + c->options.lc_max_level = min (options->lc_max_level, wi->level - 1); - c->tiling = alloc_tiling (options->tiling_method, - options->tiling_exponent, wi->level); + c->tiling = alloc_tiling (options->tiling_method, + options->tiling_exponent, wi->level); - if (wi->frames > 1 && c->tiling->exponent > 0) - { - c->tiling->exponent = 0; - warning (_("Image tiling valid only with still image compression.")); - } + if (wi->frames > 1 && c->tiling->exponent > 0) + { + c->tiling->exponent = 0; + warning (_("Image tiling valid only with still image compression.")); + } - if (c->options.lc_max_level >= wi->level - c->tiling->exponent) - { - message ("'max_level' changed from %d to %d due to image tiling level.", - c->options.lc_max_level, wi->level - c->tiling->exponent - 1); - c->options.lc_max_level = wi->level - c->tiling->exponent - 1; - } + if (c->options.lc_max_level >= wi->level - c->tiling->exponent) + { + message ("'max_level' changed from %d to %d " + "due to image tiling level.", + c->options.lc_max_level, wi->level - c->tiling->exponent - 1); + c->options.lc_max_level = wi->level - c->tiling->exponent - 1; + } - if (c->options.lc_min_level > c->options.lc_max_level) - c->options.lc_min_level = c->options.lc_max_level; + if (c->options.lc_min_level > c->options.lc_max_level) + c->options.lc_min_level = c->options.lc_max_level; - /* - * p_min_level, p_max_level min and max level for ND/MC prediction - * [p_min_level, p_max_level] must be a subset of [min_level, max_level] ! - */ - wi->p_min_level = max (options->p_min_level, c->options.lc_min_level); - wi->p_max_level = min (options->p_max_level, c->options.lc_max_level); - if (wi->p_min_level > wi->p_max_level) - wi->p_min_level = wi->p_max_level; - - c->options.images_level = min (c->options.images_level, - c->options.lc_max_level - 1); + /* + * p_min_level, p_max_level min and max level for ND/MC prediction + * [p_min_level, p_max_level] must be a subset of [min_level, max_level] ! + */ + wi->p_min_level = max (options->p_min_level, c->options.lc_min_level); + wi->p_max_level = min (options->p_max_level, c->options.lc_max_level); + if (wi->p_min_level > wi->p_max_level) + wi->p_min_level = wi->p_max_level; + + c->options.images_level = min (c->options.images_level, + c->options.lc_max_level - 1); - c->products_level = max (0, ((signed int) c->options.lc_max_level - - (signed int) c->options.images_level - 1)); - c->pixels = Calloc (size_of_level (c->options.lc_max_level), - sizeof (real_t)); - c->images_of_state = Calloc (MAXSTATES, sizeof (real_t *)); - c->ip_images_state = Calloc (MAXSTATES, sizeof (real_t *)); - c->ip_states_state = Calloc (MAXSTATES * MAXLEVEL, sizeof (real_t *)); + c->products_level = max (0, ((signed int) c->options.lc_max_level + - (signed int) c->options.images_level - 1)); + c->pixels = Calloc (size_of_level (c->options.lc_max_level), + sizeof (real_t)); + c->images_of_state = Calloc (MAXSTATES, sizeof (real_t *)); + c->ip_images_state = Calloc (MAXSTATES, sizeof (real_t *)); + c->ip_states_state = Calloc (MAXSTATES * MAXLEVEL, sizeof (real_t *)); - debug_message ("Imageslevel :%d, Productslevel :%d", - c->options.images_level, c->products_level); - debug_message ("Memory : (%d + %d + %d * 'states') * 'states' + %d", - size_of_tree (c->options.images_level) * 4, - size_of_tree (c->products_level) * 4, - (c->options.lc_max_level - c->options.images_level), - size_of_level (c->options.lc_max_level)); + debug_message ("Imageslevel :%d, Productslevel :%d", + c->options.images_level, c->products_level); + debug_message ("Memory : (%d + %d + %d * 'states') * 'states' + %d", + size_of_tree (c->options.images_level) * 4, + size_of_tree (c->products_level) * 4, + (c->options.lc_max_level - c->options.images_level), + size_of_level (c->options.lc_max_level)); - /* + /* * Domain pools ... */ - c->domain_pool = NULL; - c->d_domain_pool = NULL; - - /* - * Coefficients model ... - */ - c->coeff = NULL; - c->d_coeff = NULL; - - /* - * Max. number of states and edges - */ - wi->max_states = max (min (options->max_states, MAXSTATES), 1); - c->options.max_elements = max (min (options->max_elements, MAXEDGES), 1); - - /* - * Title and comment strings - */ - wi->title = strdup (options->title); - wi->comment = strdup (options->comment); + c->domain_pool = NULL; + c->d_domain_pool = NULL; + + /* + * Coefficients model ... + */ + c->coeff = NULL; + c->d_coeff = NULL; + + /* + * Max. number of states and edges + */ + wi->max_states = max (min (options->max_states, MAXSTATES), 1); + c->options.max_elements = max (min (options->max_elements, MAXEDGES), 1); + + /* + * Title and comment strings + */ + wi->title = strdup (options->title); + wi->comment = strdup (options->comment); - /* - * Reduced precision format - */ - wi->rpf - = alloc_rpf (options->rpf_mantissa, options->rpf_range); - wi->dc_rpf - = alloc_rpf (options->dc_rpf_mantissa, options->dc_rpf_range); - wi->d_rpf - = alloc_rpf (options->d_rpf_mantissa, options->d_rpf_range); - wi->d_dc_rpf - = alloc_rpf (options->d_dc_rpf_mantissa, options->d_dc_rpf_range); + /* + * Reduced precision format + */ + wi->rpf + = alloc_rpf (options->rpf_mantissa, options->rpf_range); + wi->dc_rpf + = alloc_rpf (options->dc_rpf_mantissa, options->dc_rpf_range); + wi->d_rpf + = alloc_rpf (options->d_rpf_mantissa, options->d_rpf_range); + wi->d_dc_rpf + = alloc_rpf (options->d_dc_rpf_mantissa, options->d_dc_rpf_range); - /* - * Color image options ... - */ - wi->chroma_max_states = max (1, options->chroma_max_states); + /* + * Color image options ... + */ + wi->chroma_max_states = max (1, options->chroma_max_states); - /* + /* * Set up motion compensation struct. * p_min_level, p_max_level are also used for ND prediction */ - wi->search_range = options->search_range; - wi->fps = options->fps; - wi->half_pixel = options->half_pixel_prediction; - wi->cross_B_search = options->half_pixel_prediction; - wi->B_as_past_ref = options->B_as_past_ref; - wi->smoothing = options->smoothing; + wi->search_range = options->search_range; + wi->fps = options->fps; + wi->half_pixel = options->half_pixel_prediction; + wi->cross_B_search = options->half_pixel_prediction; + wi->B_as_past_ref = options->B_as_past_ref; + wi->smoothing = options->smoothing; - c->mt = alloc_motion (wi); + c->mt = alloc_motion (wi); - return c; + return c; } + + static void free_coder (coding_t *c) /* @@ -384,7 +377,7 @@ free_coder (coding_t *c) * No return value. * * Side effects: - * structure 'coder' is discarded. + * structure 'coder' is discarded. */ { free_tiling (c->tiling); @@ -397,285 +390,8 @@ free_coder (coding_t *c) Free (c); } -static char * -get_input_image_name (char const * const *templptr, unsigned ith_image) -/* - * Construct the i-th image-name using templates. - * If the template contains a '[' it must be of the form - * "prefix[start-end{+,-}step]suffix" - * where "{+,-}step" is optional. - * Leading zeros of "start" are significant. - * - * Example: - * "image0[12-01-1].pgm" yields image012.pgm, image011.pgm, ..., image001.pgm - * - * Return value: - * ptr to name of image 'ith_image' or NULL if ith_image is out of range. - */ -{ - while (*templptr) - { - const char *template = *templptr++; - char *s; - - if (!(s = strchr (template, '['))) /* no template, just a filename */ - { - if (ith_image == 0) - return strdup (template); - else - ith_image--; - } - else /* template parser */ - { - unsigned n_digits; /* # of digits in image name no. */ - char *s2; - char *suffix; /* characters after template end */ - char prefix [MAXSTRLEN]; /* chars up to the template start */ - unsigned first; /* first image number */ - unsigned last; /* last image number */ - int image_num; /* current image number */ - int increment = 1; - int dummy; - - strcpy (prefix, template); - prefix [s - template] = '\0'; - - for (s2 = ++s, n_digits = 0; ISDIGIT (*s2); s2++, n_digits++) - ; - if (sscanf (s, "%d", &dummy) == 0 || dummy < 0) - error ("Input name template conversion failure.\n" - "Check spelling of template."); - first = (unsigned) dummy; - - if (*s2++ != '-') - error ("Input name template conversion failure.\n" - "Check spelling of template."); - - for (s = s2; ISDIGIT (*s2); s2++) - ; - if (sscanf (s, "%d", &dummy) == 0 || dummy < 0) - error ("Input name template conversion failure.\n" - "Check spelling of template."); - last = (unsigned) dummy; - - if (*s2 == '+' || *s2 == '-') - { - for (s = s2++; ISDIGIT (*s2); s2++) - ; - if (sscanf (s, "%d", &increment) == 0) - error ("Input name template conversion failure.\n" - "Check spelling of template."); - } - if (*s2 != ']') - error ("Input name template conversion failure.\n" - "Check spelling of template."); - suffix = s2 + 1; - - image_num = first + increment * ith_image; - if (image_num < 0) - error ("Input name template conversion failure.\n" - "Check spelling of template."); - - if ((increment > 0 && (unsigned) image_num > last) || - (increment <= 0 && (unsigned) image_num < last)) - { - /* TODO: check this */ - ith_image -= (last - first) / increment + 1; - } - else - { - char formatstr [MAXSTRLEN]; /* format string for image filename */ - char image_name [MAXSTRLEN]; /* image file name to be composed */ - - strcpy (formatstr, "%s%0?d%s"); - formatstr [4] = '0' + (char) n_digits; - sprintf (image_name, formatstr, prefix, image_num, suffix); - return strdup (image_name); - } - } - } - return NULL; -} - -static void -video_coder (char const * const *image_template, bitfile_t *output, - wfa_t *wfa, coding_t *c) -/* - * Toplevel function to encode a sequence of video frames specified - * by 'image_template'. The output is written to stream 'output'. - * Coding options are given by 'c'. - * - * No return value. - */ -{ - unsigned display; /* picture number in display order */ - int future_display; /* number of future reference */ - int frame; /* current frame number */ - char *image_name; /* image name of current frame */ - image_t *reconst = NULL; /* decoded reference image */ - bool_t future_frame = NO; /* YES if last frame was in future */ - - debug_message ("Generating %d WFA's ...", wfa->wfainfo->frames); - - future_display = -1; - frame = -1; - display = 0; - - while ((image_name = get_input_image_name (image_template, display))) - { - frame_type_e type; /* current frame type: I, B, P */ - - /* - * Determine type of next frame. - * Skip already coded frames (future reference!) - */ - if (display == 0 && !c->options.reference_filename) - type = I_FRAME; /* Force first frame to be intra */ - else - type = pattern2type (display, c->options.pattern); - - if (type != I_FRAME && c->options.reference_filename) - /* Load reference from disk */ - { - debug_message ("Reading reference frame `%s'.", - c->options.reference_filename); - reconst = read_image (c->options.reference_filename); - c->options.reference_filename = NULL; - } - if ((int) display == future_display) /* Skip already coded future ref */ - { - display++; - continue; - } - else if (type == B_FRAME && (int) display > future_display) - { - unsigned i = display; - /* - * Search for future reference - */ - while (type == B_FRAME) - { - char *name; /* image name of future frame */ - - i++; - name = get_input_image_name (image_template, i); - - if (!name) /* Force last valid frame to be 'P' */ - { - future_display = i - 1; - type = P_FRAME; - } - else - { - future_display = i; - image_name = name; - type = pattern2type (i, c->options.pattern); - } - frame = future_display; - } - } - else - { - frame = display; - display++; - } - - debug_message ("Coding \'%s\' [%c-frame].", image_name, - type == I_FRAME ? 'I' : (type == P_FRAME ? 'P' : 'B')); - - /* - * Depending on current frame type update past and future frames - * which are needed as reference frames. - */ - c->mt->frame_type = type; - if (type == I_FRAME) - { - if (c->mt->past) /* discard past frame */ - free_image (c->mt->past); - c->mt->past = NULL; - if (c->mt->future) /* discard future frame */ - free_image (c->mt->future); - c->mt->future = NULL; - if (reconst) /* discard current frame */ - free_image (reconst); - reconst = NULL; - } - else if (type == P_FRAME) - { - if (c->mt->past) /* discard past frame */ - free_image (c->mt->past); - c->mt->past = reconst; /* past frame <- current frame */ - reconst = NULL; - if (c->mt->future) /* discard future frame */ - free_image (c->mt->future); - c->mt->future = NULL; - } - else /* B_FRAME */ - { - if (future_frame) /* last frame was future frame */ - { - if (c->mt->future) /* discard future frame */ - free_image (c->mt->future); - c->mt->future = reconst; /* future frame <- current frame */ - reconst = NULL; - } - else - { - if (wfa->wfainfo->B_as_past_ref == YES) - { - if (c->mt->past) /* discard past frame */ - free_image (c->mt->past); - c->mt->past = reconst; /* past frame <- current frame */ - reconst = NULL; - } - else - { - if (reconst) /* discard current frame */ - free_image (reconst); - reconst = NULL; - } - } - } - - /* - * Start WFA coding of current frame - */ - future_frame = frame == future_display; - c->mt->number = frame; - c->mt->original = read_image (image_name); - if (c->tiling->exponent && type == I_FRAME) - perform_tiling (c->mt->original, c->tiling); - - frame_coder (wfa, c, output); - - /* - * Regenerate image: - * 1. Compute approximation of WFA ranges (real image bintree order) - * 2. Generate byte image in rasterscan order - * 3. Apply motion compensation - */ - reconst = decode_image (wfa->wfainfo->width, wfa->wfainfo->height, - FORMAT_4_4_4, NULL, wfa); - - if (type != I_FRAME) - restore_mc (0, reconst, c->mt->past, c->mt->future, wfa); - if (c->mt->original) - free_image (c->mt->original); - c->mt->original = NULL; - - remove_states (wfa->basis_states, wfa); /* Clear WFA structure */ - } - if (reconst) - free_image (reconst); - if (c->mt->future) - free_image (c->mt->future); - if (c->mt->past) - free_image (c->mt->past); - if (c->mt->original) - free_image (c->mt->original); -} static frame_type_e pattern2type (unsigned frame, const char *pattern) @@ -700,6 +416,72 @@ pattern2type (unsigned frame, const char *pattern) return retval; } + + +static void +print_statistics (char c, real_t costs, const wfa_t *wfa, const image_t *image, + const range_t *range) +{ + unsigned max_level, min_level, state, label, lincomb; + + for (max_level = 0, min_level = MAXLEVEL, state = wfa->basis_states; + state < wfa->states; state++) + { + for (lincomb = 0, label = 0; label < MAXLABELS; label++) + lincomb += isrange(wfa->tree[state][label]) ? 1 : 0; + + if (lincomb) + { + max_level = max (max_level, + (unsigned) (wfa->level_of_state [state] - 1)); + min_level = min (min_level, + (unsigned) (wfa->level_of_state [state] - 1)); + } + } + debug_message ("Image partitioning: maximum level %d , minimum level %d", + max_level, min_level); + debug_message ("WFA contains %d states (%d basis states).", + wfa->states, wfa->basis_states); + debug_message ("Estimated error: %.2f (RMSE: %.2f, PSNR: %.2f dB).", + (double) range->err, + sqrt (range->err / image->width / image->height), + 10 * log ( 255.0 * 255.0 / + (range->err / image->width / image->height)) + / log (10.0)); + debug_message ("Estimated filesize: %.0f bits (%.0f bytes).", + (double) (range->tree_bits + range->matrix_bits + + range->weights_bits + + range->mv_tree_bits + range->mv_coord_bits + + range->nd_tree_bits + range->nd_weights_bits), + (double) (range->tree_bits + range->matrix_bits + + range->weights_bits + range->mv_tree_bits + + range->mv_coord_bits + range->nd_tree_bits + + range->nd_weights_bits) / 8); + if (c) + debug_message ("(%cT: %.0f, %cM: %.0f, %cW: %.0f, %cMC: %.0f, " + "%cMV: %.0f, %cNT: %.0f, %cNW: %.0f.)", + c, (double) range->tree_bits, + c, (double) range->matrix_bits, + c, (double) range->weights_bits, + c, (double) range->mv_tree_bits, + c, (double) range->mv_coord_bits, + c, (double) range->nd_tree_bits, + c, (double) range->nd_weights_bits); + else + debug_message ("(T: %.0f, M: %.0f, W: %.0f, MC: %.0f, MV: %.0f, " + "NT: %.0f, NW: %.0f.)", + (double) range->tree_bits, + (double) range->matrix_bits, + (double) range->weights_bits, + (double) range->mv_tree_bits, + (double) range->mv_coord_bits, + (double) range->nd_tree_bits, + (double) range->nd_weights_bits); + debug_message ("Total costs : %.2f", (double) costs); +} + + + static void frame_coder (wfa_t *wfa, coding_t *c, bitfile_t *output) /* @@ -712,9 +494,9 @@ frame_coder (wfa_t *wfa, coding_t *c, bitfile_t *output) */ { unsigned state; - range_t range; /* first range == the entire image */ - real_t costs; /* total costs (minimized quantity) */ - unsigned bits; /* number of bits written on disk */ + range_t range; /* first range == the entire image */ + real_t costs; /* total costs (minimized quantity) */ + unsigned bits; /* number of bits written on disk */ clock_t ptimer; prg_timer (&ptimer, START); @@ -726,48 +508,48 @@ frame_coder (wfa_t *wfa, coding_t *c, bitfile_t *output) c->domain_pool = alloc_domain_pool (c->options.id_domain_pool, - wfa->wfainfo->max_states, - c->options.max_elements, wfa); + wfa->wfainfo->max_states, + c->options.max_elements, wfa); c->d_domain_pool = alloc_domain_pool ((c->options.prediction - || c->mt->frame_type != I_FRAME) - ? c->options.id_d_domain_pool : "constant", - wfa->wfainfo->max_states, - c->options.max_elements, wfa); + || c->mt->frame_type != I_FRAME) + ? c->options.id_d_domain_pool : "constant", + wfa->wfainfo->max_states, + c->options.max_elements, wfa); c->coeff = alloc_coeff_model (c->options.id_rpf_model, - wfa->wfainfo->rpf, - wfa->wfainfo->dc_rpf, - c->options.lc_min_level, - c->options.lc_max_level); + wfa->wfainfo->rpf, + wfa->wfainfo->dc_rpf, + c->options.lc_min_level, + c->options.lc_max_level); c->d_coeff = alloc_coeff_model (c->options.id_d_rpf_model, - wfa->wfainfo->d_rpf, - wfa->wfainfo->d_dc_rpf, - c->options.lc_min_level, - c->options.lc_max_level); + wfa->wfainfo->d_rpf, + wfa->wfainfo->d_dc_rpf, + c->options.lc_min_level, + c->options.lc_max_level); - if (!c->mt->original->color) /* grayscale image */ + if (!c->mt->original->color) /* grayscale image */ { memset (&range, 0, sizeof (range_t)); range.level = wfa->wfainfo->level; costs = subdivide (MAXCOSTS, GRAY, RANGE, &range, wfa, c, - c->options.prediction || c->mt->frame_type != I_FRAME, - NO); + c->options.prediction || c->mt->frame_type != I_FRAME, + NO); if (c->options.progress_meter != FIASCO_PROGRESS_NONE) - message (""); + message (""); - if (isrange (range.tree)) /* entire image is approx. by lc? */ - error ("No root state generated!"); + if (isrange (range.tree)) /* entire image is approx. by lc? */ + error ("No root state generated!"); else - wfa->root_state = range.tree; + wfa->root_state = range.tree; print_statistics ('\0', costs, wfa, c->mt->original, &range); } else { int YCb_node = -1; - int tree [3]; /* 3 root states of each color comp. */ + int tree [3]; /* 3 root states of each color comp. */ color_e band; /* @@ -781,62 +563,62 @@ frame_coder (wfa_t *wfa, coding_t *c, bitfile_t *output) */ for (band = first_band (YES); band <= last_band (YES) ; band++) { - debug_message ("Encoding color component %d", band); - tree [band] = RANGE; - if (band == Cb) - { - unsigned min_level; - - c->domain_pool->chroma (wfa->wfainfo->chroma_max_states, wfa, - c->domain_pool->model); - /* - * Don't use a finer partioning for the chrominancy bands than for - * the luminancy band. - */ - for (min_level = MAXLEVEL, state = wfa->basis_states; - state < wfa->states; state++) - { - unsigned lincomb, label; - - for (lincomb = 0, label = 0; label < MAXLABELS; label++) - lincomb += isrange (wfa->tree [state][label]) ? 1 : 0; - if (lincomb) - min_level = min (min_level, - (unsigned) (wfa->level_of_state [state] - - 1)); - } - c->options.lc_min_level = min_level; - if (c->mt->frame_type != I_FRAME) /* subtract mc of luminance */ - subtract_mc (c->mt->original, c->mt->past, c->mt->future, wfa); - } - - memset (&range, 0, sizeof (range_t)); - range.level = wfa->wfainfo->level; - - costs = subdivide (MAXCOSTS, band, tree [Y], &range, wfa, c, - c->mt->frame_type != I_FRAME && band == Y, NO); - if (c->options.progress_meter != FIASCO_PROGRESS_NONE) - message (""); - { - char colors [] = {'Y', 'B', 'R'}; - - print_statistics (colors [band], costs, wfa, - c->mt->original, &range); - } - - if (isrange (range.tree)) /* whole image is approx. by a l.c. */ - error ("No root state generated for color component %d!", band); - else - tree[band] = range.tree; - - if (band == Cb) - { - wfa->tree [wfa->states][0] = tree[Y]; - wfa->tree [wfa->states][1] = tree[Cb]; - YCb_node = wfa->states; - append_state (YES, compute_final_distribution (wfa->states, wfa), - wfa->wfainfo->level + 1, wfa, c); - } + debug_message ("Encoding color component %d", band); + tree [band] = RANGE; + if (band == Cb) + { + unsigned min_level; + + c->domain_pool->chroma (wfa->wfainfo->chroma_max_states, wfa, + c->domain_pool->model); + /* + * Don't use a finer partioning for the chrominancy bands than for + * the luminancy band. + */ + for (min_level = MAXLEVEL, state = wfa->basis_states; + state < wfa->states; state++) + { + unsigned lincomb, label; + + for (lincomb = 0, label = 0; label < MAXLABELS; label++) + lincomb += isrange (wfa->tree [state][label]) ? 1 : 0; + if (lincomb) + min_level = min (min_level, + (unsigned) (wfa->level_of_state [state] + - 1)); + } + c->options.lc_min_level = min_level; + if (c->mt->frame_type != I_FRAME) /* subtract mc of luminance */ + subtract_mc (c->mt->original, c->mt->past, c->mt->future, wfa); + } + + memset (&range, 0, sizeof (range_t)); + range.level = wfa->wfainfo->level; + + costs = subdivide (MAXCOSTS, band, tree [Y], &range, wfa, c, + c->mt->frame_type != I_FRAME && band == Y, NO); + if (c->options.progress_meter != FIASCO_PROGRESS_NONE) + message (""); + { + char colors [] = {'Y', 'B', 'R'}; + + print_statistics (colors [band], costs, wfa, + c->mt->original, &range); + } + + if (isrange (range.tree)) /* whole image is approx. by a l.c. */ + error ("No root state generated for color component %d!", band); + else + tree[band] = range.tree; + + if (band == Cb) + { + wfa->tree [wfa->states][0] = tree[Y]; + wfa->tree [wfa->states][1] = tree[Cb]; + YCb_node = wfa->states; + append_state (YES, compute_final_distribution (wfa->states, wfa), + wfa->wfainfo->level + 1, wfa, c); + } } /* * generate two virtual states (*) @@ -850,11 +632,11 @@ frame_coder (wfa_t *wfa, coding_t *c, bitfile_t *output) wfa->tree [wfa->states][0] = tree[Cr]; wfa->tree [wfa->states][1] = RANGE; append_state (YES, compute_final_distribution (wfa->states, wfa), - wfa->wfainfo->level + 1, wfa, c); + wfa->wfainfo->level + 1, wfa, c); wfa->tree[wfa->states][0] = YCb_node; wfa->tree[wfa->states][1] = wfa->states - 1; append_state (YES, compute_final_distribution (wfa->states, wfa), - wfa->wfainfo->level + 2, wfa, c); + wfa->wfainfo->level + 2, wfa, c); wfa->root_state = wfa->states - 1; } @@ -865,22 +647,22 @@ frame_coder (wfa_t *wfa, coding_t *c, bitfile_t *output) if (c->images_of_state [state]) { - Free (c->images_of_state [state]); - c->images_of_state [state] = NULL; + Free (c->images_of_state [state]); + c->images_of_state [state] = NULL; } if (c->ip_images_state [state]) { - Free (c->ip_images_state [state]); - c->ip_images_state [state] = NULL; + Free (c->ip_images_state [state]); + c->ip_images_state [state] = NULL; } for (level = c->options.images_level + 1; - level <= c->options.lc_max_level; - level++) - if (c->ip_states_state [state][level]) - { - Free (c->ip_states_state [state][level]); - c->ip_states_state [state][level] = NULL; - } + level <= c->options.lc_max_level; + level++) + if (c->ip_states_state [state][level]) + { + Free (c->ip_states_state [state][level]); + c->ip_states_state [state][level] = NULL; + } } @@ -889,11 +671,11 @@ frame_coder (wfa_t *wfa, coding_t *c, bitfile_t *output) bits = bits_processed (output) - bits; debug_message ("Total number of bits written: %d (%d bytes, %5.3f bpp)", - bits, bits >> 3, - bits / (double) (c->mt->original->height - * c->mt->original->width)); + bits, bits >> 3, + bits / (double) (c->mt->original->height + * c->mt->original->width)); debug_message ("Total encoding time (real): %d sec", - prg_timer (&ptimer, STOP) / 1000); + prg_timer (&ptimer, STOP) / 1000); c->domain_pool->free (c->domain_pool); c->d_domain_pool->free (c->d_domain_pool); @@ -902,64 +684,356 @@ frame_coder (wfa_t *wfa, coding_t *c, bitfile_t *output) c->d_coeff->free (c->d_coeff); } + + static void -print_statistics (char c, real_t costs, const wfa_t *wfa, const image_t *image, - const range_t *range) +video_coder(char const * const * const image_template, + bitfile_t * const output, + wfa_t * const wfa, + coding_t * const c, + unsigned int const stdinwidth, + unsigned int const stdinheight, + unsigned int const stdinmaxval, + unsigned int const stdinformat) +/* + * Toplevel function to encode a sequence of video frames specified + * by 'image_template'. The output is written to stream 'output'. + * Coding options are given by 'c'. + * + * No return value. + */ { - unsigned max_level, min_level, state, label, lincomb; + unsigned display; /* picture number in display order */ + int future_display; /* number of future reference */ + int frame; /* current frame number */ + char *image_name; + /* image name of current frame. File name or "-" for Standard Input */ + image_t *reconst = NULL; /* decoded reference image */ + bool_t future_frame = NO; /* YES if last frame was in future */ - for (max_level = 0, min_level = MAXLEVEL, state = wfa->basis_states; - state < wfa->states; state++) - { - for (lincomb = 0, label = 0; label < MAXLABELS; label++) - lincomb += isrange(wfa->tree[state][label]) ? 1 : 0; - - if (lincomb) - { - max_level = max (max_level, - (unsigned) (wfa->level_of_state [state] - 1)); - min_level = min (min_level, - (unsigned) (wfa->level_of_state [state] - 1)); - } - } - debug_message ("Image partitioning: maximum level %d , minimum level %d", - max_level, min_level); - debug_message ("WFA contains %d states (%d basis states).", - wfa->states, wfa->basis_states); - debug_message ("Estimated error: %.2f (RMSE: %.2f, PSNR: %.2f dB).", - (double) range->err, - sqrt (range->err / image->width / image->height), - 10 * log ( 255.0 * 255.0 / - (range->err / image->width / image->height)) - / log (10.0)); - debug_message ("Estimated filesize: %.0f bits (%.0f bytes).", - (double) (range->tree_bits + range->matrix_bits - + range->weights_bits - + range->mv_tree_bits + range->mv_coord_bits - + range->nd_tree_bits + range->nd_weights_bits), - (double) (range->tree_bits + range->matrix_bits - + range->weights_bits + range->mv_tree_bits - + range->mv_coord_bits + range->nd_tree_bits - + range->nd_weights_bits) / 8); - if (c) - debug_message ("(%cT: %.0f, %cM: %.0f, %cW: %.0f, %cMC: %.0f, " - "%cMV: %.0f, %cNT: %.0f, %cNW: %.0f.)", - c, (double) range->tree_bits, - c, (double) range->matrix_bits, - c, (double) range->weights_bits, - c, (double) range->mv_tree_bits, - c, (double) range->mv_coord_bits, - c, (double) range->nd_tree_bits, - c, (double) range->nd_weights_bits); - else - debug_message ("(T: %.0f, M: %.0f, W: %.0f, MC: %.0f, MV: %.0f, " - "NT: %.0f, NW: %.0f.)", - (double) range->tree_bits, - (double) range->matrix_bits, - (double) range->weights_bits, - (double) range->mv_tree_bits, - (double) range->mv_coord_bits, - (double) range->nd_tree_bits, - (double) range->nd_weights_bits); - debug_message ("Total costs : %.2f", (double) costs); + debug_message ("Generating %d WFA's ...", wfa->wfainfo->frames); + + future_display = -1; + frame = -1; + display = 0; + + while ((image_name = get_input_image_name (image_template, display))) + { + frame_type_e type; /* current frame type: I, B, P */ + + /* + * Determine type of next frame. + * Skip already coded frames (future reference!) + */ + if (display == 0 && !c->options.reference_filename) + type = I_FRAME; /* Force first frame to be intra */ + else + type = pattern2type (display, c->options.pattern); + + if (type != I_FRAME && c->options.reference_filename) + /* Load reference from disk */ + { + debug_message ("Reading reference frame `%s'.", + c->options.reference_filename); + reconst = read_image_file (c->options.reference_filename); + c->options.reference_filename = NULL; + } + if ((int) display == future_display) + { + /* Skip already coded future ref */ + display++; + continue; + } + else if (type == B_FRAME && (int) display > future_display) + { + unsigned i = display; + /* + * Search for future reference + */ + while (type == B_FRAME) + { + char *name; /* image name of future frame */ + + i++; + name = get_input_image_name (image_template, i); + + if (!name) /* Force last valid frame to be 'P' */ + { + future_display = i - 1; + type = P_FRAME; + } + else + { + future_display = i; + image_name = name; + type = pattern2type (i, c->options.pattern); + } + frame = future_display; + } + } + else + { + frame = display; + display++; + } + + debug_message ("Coding \'%s\' [%c-frame].", image_name, + type == I_FRAME ? 'I' : (type == P_FRAME ? 'P' : 'B')); + + /* + * Depending on current frame type update past and future frames + * which are needed as reference frames. + */ + c->mt->frame_type = type; + if (type == I_FRAME) + { + if (c->mt->past) /* discard past frame */ + free_image (c->mt->past); + c->mt->past = NULL; + if (c->mt->future) /* discard future frame */ + free_image (c->mt->future); + c->mt->future = NULL; + if (reconst) /* discard current frame */ + free_image (reconst); + reconst = NULL; + } + else if (type == P_FRAME) + { + if (c->mt->past) /* discard past frame */ + free_image (c->mt->past); + c->mt->past = reconst; /* past frame <- current frame */ + reconst = NULL; + if (c->mt->future) /* discard future frame */ + free_image (c->mt->future); + c->mt->future = NULL; + } + else /* B_FRAME */ + { + if (future_frame) /* last frame was future frame */ + { + if (c->mt->future) /* discard future frame */ + free_image (c->mt->future); + c->mt->future = reconst; /* future frame <- current frame */ + reconst = NULL; + } + else + { + if (wfa->wfainfo->B_as_past_ref == YES) + { + if (c->mt->past) /* discard past frame */ + free_image (c->mt->past); + c->mt->past = reconst; /* past frame <- current frame */ + reconst = NULL; + } + else + { + if (reconst) /* discard current frame */ + free_image (reconst); + reconst = NULL; + } + } + } + + /* + * Start WFA coding of current frame + */ + future_frame = frame == future_display; + c->mt->number = frame; + + if (streq(image_name, "-")) + c->mt->original = read_image_stream(stdin, + stdinwidth, stdinheight, + stdinmaxval, stdinformat); + else + c->mt->original = read_image_file(image_name); + + if (c->tiling->exponent && type == I_FRAME) + perform_tiling (c->mt->original, c->tiling); + + frame_coder (wfa, c, output); + + /* + * Regenerate image: + * 1. Compute approximation of WFA ranges (real image bintree order) + * 2. Generate byte image in rasterscan order + * 3. Apply motion compensation + */ + reconst = decode_image (wfa->wfainfo->width, wfa->wfainfo->height, + FORMAT_4_4_4, NULL, wfa); + + if (type != I_FRAME) + restore_mc (0, reconst, c->mt->past, c->mt->future, wfa); + + if (c->mt->original) + free_image (c->mt->original); + c->mt->original = NULL; + + remove_states (wfa->basis_states, wfa); /* Clear WFA structure */ + } + + if (reconst) + free_image (reconst); + if (c->mt->future) + free_image (c->mt->future); + if (c->mt->past) + free_image (c->mt->past); + if (c->mt->original) + free_image (c->mt->original); +} + + + +static void +read_stdin_header(const char * const * const template, + unsigned int * const widthP, + unsigned int * const heightP, + xelval * const maxvalP, + int * const formatP) +/* Read the PNM header from the Standard Input stream, if 'template' says + one of the images is to come from Standard Input. + + Return the contents of that stream as *widthP, etc. +*/ +{ + unsigned int i; + bool endOfList; + bool stdinFound; + + for (i = 0, stdinFound = FALSE, endOfList = FALSE; !endOfList; ++i) { + const char * const name = get_input_image_name(template, i); + + if (!name) + endOfList = TRUE; + else { + if (streq(name, "-")) + stdinFound = TRUE; + } + } + + if (stdinFound) { + int width, height; + + pnm_readpnminit(stdin, &width, &height, maxvalP, formatP); + + *widthP = width; + *heightP = height; + } } + + + +/***************************************************************************** + + public code + +*****************************************************************************/ + +int +fiasco_coder (char const * const *inputname, const char *outputname, + float quality, const fiasco_c_options_t *options) +/* + * FIASCO coder. + * Encode image or video frames given by the array of filenames `inputname' + * and write to the outputfile `outputname'. + * If 'inputname' = NULL or + * 'inputname [0]' == NULL or + * 'inputname [0]' == "-", read standard input. + * If 'outputname' == NULL or "-", write on standard output. + * 'quality' defines the approximation quality and is 1 (worst) to 100 (best). + * + * Return value: + * 1 on success + * 0 otherwise + */ +{ + try + { + char const * const default_input [] = {"-", NULL}; + fiasco_c_options_t *default_options = NULL; + const c_options_t *cop; + char const * const *template; + unsigned int stdinheight, stdinwidth; + xelval stdinmaxval; + int stdinformat; + + /* + * Check parameters + */ + if (!inputname || !inputname [0] || streq (inputname [0], "-")) + template = default_input; + else + template = inputname; + + if (quality <= 0) + { + set_error (_("Compression quality has to be positive.")); + return 0; + } + else if (quality >= 100) + { + warning (_("Quality typically is 1 (worst) to 100 (best).\n" + "Be prepared for a long running time.")); + } + + if (options) + { + cop = cast_c_options ((fiasco_c_options_t *) options); + if (!cop) + return 0; + } + else + { + default_options = fiasco_c_options_new (); + cop = cast_c_options (default_options); + } + + read_stdin_header(template, &stdinwidth, &stdinheight, + &stdinmaxval, &stdinformat); + + /* + * Open output stream and initialize WFA + */ + { + bitfile_t *output = open_bitfile (outputname, "FIASCO_DATA", + WRITE_ACCESS); + if (!output) + { + set_error (_("Can't write outputfile `%s'.\n%s"), + outputname ? outputname : "", + get_system_error ()); + if (default_options) + fiasco_c_options_delete (default_options); + return 0; + } + else + { + wfa_t *wfa = alloc_wfa (YES); + coding_t *c = alloc_coder(template, cop, wfa->wfainfo, + stdinwidth, stdinheight, + stdinmaxval, stdinformat); + + read_basis (cop->basis_name, wfa); + append_basis_states (wfa->basis_states, wfa, c); + + c->price = 128 * 64 / quality; + + video_coder (template, output, wfa, c, + stdinwidth, stdinheight, stdinmaxval, + stdinformat); + + close_bitfile (output); + free_wfa (wfa); + free_coder (c); + + if (default_options) + fiasco_c_options_delete (default_options); + } + } + return 1; + } + catch + { + return 0; + } +} + diff --git a/converter/other/fiasco/codec/decoder.c b/converter/other/fiasco/codec/decoder.c index c3982617..77d5340f 100644 --- a/converter/other/fiasco/codec/decoder.c +++ b/converter/other/fiasco/codec/decoder.c @@ -223,7 +223,7 @@ get_next_frame (bool_t store_wfa, int enlarge_factor, "reference frame is given.", video->wfa->frame_type == B_FRAME ? 'B' : 'P'); - video->frame = read_image (reference_frame); + video->frame = read_image_file (reference_frame); video->sframe = NULL; } diff --git a/converter/other/fiasco/fiasco.h b/converter/other/fiasco/fiasco.h index 235b1279..59367bb8 100644 --- a/converter/other/fiasco/fiasco.h +++ b/converter/other/fiasco/fiasco.h @@ -28,6 +28,8 @@ * $State: Exp $ */ +#include "pnm.h" + #undef __BEGIN_DECLS #undef __END_DECLS #ifdef __cplusplus @@ -260,8 +262,17 @@ fiasco_decoder_get_comment (fiasco_decoder_t *decoder); image functions ****************************************************************************/ -/* Read FIASCO image (raw ppm or pgm format) */ -fiasco_image_t * fiasco_image_new (const char *filename); +/* Create FIASCO image (from PNM image file) */ +fiasco_image_t * +fiasco_image_new_file(const char * const filename); + +/* Create FIASCO image (from PNM image stream) */ +fiasco_image_t * +fiasco_image_new_stream(FILE * const ifP, + unsigned int const width, + unsigned int const height, + xelval const maxval, + int const format); /* Discard FIASCO image */ void fiasco_image_delete (fiasco_image_t *image); diff --git a/converter/other/fiasco/lib/image.c b/converter/other/fiasco/lib/image.c index 019ba03c..0168734c 100644 --- a/converter/other/fiasco/lib/image.c +++ b/converter/other/fiasco/lib/image.c @@ -1,8 +1,8 @@ /* - * image.c: Input and output of PNM images. + * image.c: Input and output of PNM images. * - * Written by: Ullrich Hafner - * + * Written by: Ullrich Hafner + * * This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec) * Copyright (C) 1994-2000 Ullrich Hafner */ @@ -28,7 +28,7 @@ /***************************************************************************** - prototypes + prototypes *****************************************************************************/ @@ -37,7 +37,7 @@ init_chroma_tables (void); /***************************************************************************** - local variables + local variables *****************************************************************************/ static int *Cr_r_tab = NULL; @@ -47,41 +47,80 @@ static int *Cb_b_tab = NULL; /***************************************************************************** - public code + public code *****************************************************************************/ +static fiasco_image_t * +make_image_base(void) +{ + fiasco_image_t * const imageP = Calloc (1, sizeof (fiasco_image_t)); + + if (imageP == NULL) + pm_error("Failed to allocate memory for image object"); + else { + imageP->delete = fiasco_image_delete; + imageP->get_width = fiasco_image_get_width; + imageP->get_height = fiasco_image_get_height; + imageP->is_color = fiasco_image_is_color; + } + return imageP; +} + + + fiasco_image_t * -fiasco_image_new (const char *filename) +fiasco_image_new_file(const char * const filename) /* * FIASCO image constructor. * Allocate memory for the FIASCO image structure and - * load the specified image `filename'. The image has to be in - * raw pgm or ppm format. + * load the image from PNM file `filename'. * * Return value: - * pointer to the new image structure - * or NULL in case of an error + * pointer to the new image structure + * or NULL in case of an error */ { - try - { - fiasco_image_t *image = Calloc (1, sizeof (fiasco_image_t)); + fiasco_image_t * imageP; - image->private = read_image (filename); - image->delete = fiasco_image_delete; - image->get_width = fiasco_image_get_width; - image->get_height = fiasco_image_get_height; - image->is_color = fiasco_image_is_color; + imageP = make_image_base(); - return image; - } - catch - { - return NULL; - } + imageP->private = read_image_file(filename); + + return imageP; +} + + + +fiasco_image_t * +fiasco_image_new_stream(FILE * const ifP, + unsigned int const width, + unsigned int const height, + xelval const maxval, + int const format) +/* + * FIASCO image constructor. + * Allocate memory for the FIASCO image structure and + * load the image from the PNM stream in *ifP, which is positioned just + * after the header. 'width', 'height', 'maxval', and 'format' are the + * parameters of the image, i.e. the contents of that header. + * + * Return value: + * pointer to the new image structure + * or NULL in case of an error + */ +{ + fiasco_image_t * imageP; + + imageP = make_image_base(); + + imageP->private = read_image_stream(ifP, width, height, maxval, format); + + return imageP; } + + void fiasco_image_delete (fiasco_image_t *image) /* @@ -91,7 +130,7 @@ fiasco_image_delete (fiasco_image_t *image) * No return value. * * Side effects: - * structure 'image' is discarded. + * structure 'image' is discarded. */ { image_t *this = cast_image (image); @@ -149,7 +188,7 @@ cast_image (fiasco_image_t *image) * Check whether `image' is a valid object of type image_t. * * Return value: - * pointer to dfiasco_t struct on success + * pointer to dfiasco_t struct on success * NULL otherwise */ { @@ -158,8 +197,8 @@ cast_image (fiasco_image_t *image) { if (!streq (this->id, "IFIASCO")) { - set_error (_("Parameter `image' doesn't match required type.")); - return NULL; + set_error (_("Parameter `image' doesn't match required type.")); + return NULL; } } else @@ -182,7 +221,7 @@ alloc_image (unsigned width, unsigned height, bool_t color, format_e format) * are stored in 4:4:4 or 4:2:0 format. * * Return value: - * pointer to the new image structure. + * pointer to the new image structure. */ { image_t *image; @@ -193,21 +232,21 @@ alloc_image (unsigned width, unsigned height, bool_t color, format_e format) if (!color) format = FORMAT_4_4_4; - image = Calloc (1, sizeof (image_t)); - image->width = width; - image->height = height; - image->color = color; - image->format = format; + image = Calloc (1, sizeof (image_t)); + image->width = width; + image->height = height; + image->color = color; + image->format = format; image->reference_count = 1; strcpy (image->id, "IFIASCO"); for (band = first_band (color); band <= last_band (color); band++) if (format == FORMAT_4_2_0 && band != Y) - image->pixels [band] = Calloc ((width * height) >> 2, - sizeof (word_t)); + image->pixels [band] = Calloc ((width * height) >> 2, + sizeof (word_t)); else - image->pixels [band] = Calloc (width * height, sizeof (word_t)); + image->pixels [band] = Calloc (width * height, sizeof (word_t)); return image; } @@ -219,23 +258,23 @@ clone_image (image_t *image) * Construct new image by copying the given `image'. * * Return value: - * pointer to the new image structure. + * pointer to the new image structure. */ { image_t *new = alloc_image (image->width, image->height, image->color, - image->format); + image->format); color_e band; for (band = first_band (new->color); band <= last_band (new->color); band++) if (new->format == FORMAT_4_2_0 && band != Y) { - memcpy (new->pixels [band], image->pixels [band], - ((new->width * new->height) >> 2) * sizeof (word_t)); + memcpy (new->pixels [band], image->pixels [band], + ((new->width * new->height) >> 2) * sizeof (word_t)); } else { - memcpy (new->pixels [band], image->pixels [band], - new->width * new->height * sizeof (word_t)); + memcpy (new->pixels [band], image->pixels [band], + new->width * new->height * sizeof (word_t)); } return new; @@ -250,22 +289,22 @@ free_image (image_t *image) * No return value. * * Side effects: - * structure 'image' is discarded. + * structure 'image' is discarded. */ { if (image != NULL) { if (--image->reference_count) - return; /* image is still referenced */ + return; /* image is still referenced */ else { - color_e band; + color_e band; - for (band = first_band (image->color); - band <= last_band (image->color); band++) - if (image->pixels [band]) - Free (image->pixels [band]); - Free (image); + for (band = first_band (image->color); + band <= last_band (image->color); band++) + if (image->pixels [band]) + Free (image->pixels [band]); + Free (image); } } else @@ -327,27 +366,19 @@ read_image_data(image_t * const image, FILE *input, const bool_t color, image_t * -read_image (const char *image_name) +read_image_stream(FILE * const ifP, + unsigned int const width, + unsigned int const height, + xelval const maxval, + int const format) /* - * Read image 'image_name'. - * - * Return value: - * pointer to the image structure. + * Read one PNM image from stream *ifP, which is positioned just after the + * header. 'width', 'height', 'maxval', and 'format' are the parameters of + * the image (i.e. the contents of that header). */ { - FILE *input; /* input stream */ - image_t *image; /* pointer to new image structure */ - int width, height; /* image size */ - xelval maxval; /* Maxval of image */ - int format; /* Image's format code */ - bool_t color; /* color image ? (YES/NO) */ - - if (image_name == NULL) - input = stdin; - else - input = pm_openr((char*)image_name); - - pnm_readpnminit(input, &width, &height, &maxval, &format); + image_t *image; /* pointer to new image structure */ + bool_t color; /* color image ? (YES/NO) */ if (PNM_FORMAT_TYPE(format) == PPM_FORMAT) color = YES; @@ -362,12 +393,40 @@ read_image (const char *image_name) image = alloc_image (width, height, color, FORMAT_4_4_4); - read_image_data(image, input, color, width, height, maxval, format); + read_image_data(image, ifP, color, width, height, maxval, format); - pm_close(input); - return image; -} +} + + + +image_t * +read_image_file(const char * const filename) +/* + * Read the PNM image from the file named 'filename'. + * + * Return value: + * pointer to the image structure. + */ +{ + FILE * ifP; + int width, height; /* image dimensions */ + xelval maxval; /* Maxval of image */ + int format; /* Image's format code */ + image_t * imageP; /* pointer to new image structure */ + + ifP = pm_openr(filename); + + pnm_readpnminit(ifP, &width, &height, &maxval, &format); + + imageP = read_image_stream(ifP, width, height, maxval, format); + + pm_close(ifP); + + return imageP; +} + + void write_image (const char *image_name, const image_t *image) @@ -377,12 +436,12 @@ write_image (const char *image_name, const image_t *image) * No return value. */ { - FILE *output; /* output stream */ + FILE *output; /* output stream */ int format; int row; int i; /* Cursor into image->pixel arrays */ xel * xelrow; - unsigned *gray_clip; /* clipping table */ + unsigned *gray_clip; /* clipping table */ assert (image && image_name); @@ -394,12 +453,12 @@ write_image (const char *image_name, const image_t *image) if (image_name == NULL) output = stdout; - else if (strcmp(image_name, "-") == 0) + else if (streq(image_name, "-")) output = stdout; else output = pm_openw((char*)image_name); - gray_clip = init_clipping (); /* mapping of int -> unsigned */ + gray_clip = init_clipping (); /* mapping of int -> unsigned */ if (!gray_clip) error (fiasco_get_error_message ()); init_chroma_tables (); @@ -445,21 +504,21 @@ same_image_type (const image_t *img1, const image_t *img2) * Check whether the given images 'img1' and `img2' are of the same type. * * Return value: - * YES if images 'img1' and `img2' are of the same type - * NO otherwise. + * YES if images 'img1' and `img2' are of the same type + * NO otherwise. */ { assert (img1 && img2); return ((img1->width == img2->width) - && (img1->height == img2->height) - && (img1->color == img2->color) - && (img1->format == img2->format)); + && (img1->height == img2->height) + && (img1->color == img2->color) + && (img1->format == img2->format)); } /***************************************************************************** - private code + private code *****************************************************************************/ @@ -509,4 +568,3 @@ init_chroma_tables (void) Cb_g_tab += 256 + 128; Cb_b_tab += 256 + 128; } - diff --git a/converter/other/fiasco/lib/image.h b/converter/other/fiasco/lib/image.h index 958049f6..00978526 100644 --- a/converter/other/fiasco/lib/image.h +++ b/converter/other/fiasco/lib/image.h @@ -20,6 +20,7 @@ #include #include "types.h" #include "fiasco.h" +#include "pnm.h" typedef enum {FORMAT_4_4_4, FORMAT_4_2_0} format_e; @@ -48,8 +49,17 @@ free_image (image_t *image); FILE * read_pnmheader (const char *image_name, unsigned *width, unsigned *height, bool_t *color); + +image_t * +read_image_stream(FILE * const ifP, + unsigned int const width, + unsigned int const height, + xelval const maxval, + int const format); + image_t * -read_image (const char *image_name); +read_image_file(const char * const filename); + void write_image (const char *image_name, const image_t *image); bool_t diff --git a/converter/other/pnmtopalm/palmtopnm.c b/converter/other/pnmtopalm/palmtopnm.c index 9cd695e3..f2febef1 100644 --- a/converter/other/pnmtopalm/palmtopnm.c +++ b/converter/other/pnmtopalm/palmtopnm.c @@ -321,10 +321,13 @@ interpretHeader(struct palmHeader * const palmHeaderP, palmHeaderP->transparentValue = transparentValue; palmHeaderP->transparentIndex = transparentIndex; - if (palmHeaderP->version == 3 && (flags & PALM_DIRECT_COLOR_FLAG)) - /* There's no directColorInfoType section in a version 3 Palm Bitmap */ - pm_error("PALM_DIRECT_COLOR_FLAG is not valid for version 3 " - "encoding type."); + if (palmHeaderP->version == 3 && ((flags & PALM_DIRECT_COLOR_FLAG) && + (pixelFormat != PALM_FORMAT_565))) + /* There's no directColorInfoType section in a version 3 Palm Bitmap + so we also need PALM_FORMAT_565 for this flag to make sense + */ + pm_error("PALM_DIRECT_COLOR_FLAG is set but pixelFormat is not" + "PALM_FORMAT_565."); palmHeaderP->directColor = ((flags & PALM_DIRECT_COLOR_FLAG) || palmHeaderP->pixelFormat == PALM_FORMAT_565); @@ -629,11 +632,18 @@ getColorInfo(struct palmHeader const palmHeader, directColorInfoP->pixelFormat.redbits = 5; directColorInfoP->pixelFormat.greenbits = 6; directColorInfoP->pixelFormat.bluebits = 5; - /* FIXME Just guessing here ... */ directColorInfoP->transparentColor = - (((palmHeader.transparentValue >> 11) & 0x1F) << 16) | - (((palmHeader.transparentValue >> 5) & 0x3F) << 8) | - (((palmHeader.transparentValue >> 0) & 0x1F) << 0); + /* See convertRowToPnmDirect for this trick + + This will break once maxval isn't always set 255 for + directColor + */ + ((((palmHeader.transparentValue >> 11) & 0x1F) * 255 / 0x1F) + << 16) | + ((((palmHeader.transparentValue >> 5) & 0x3F) * 255 / 0x3F) + << 8) | + ((((palmHeader.transparentValue >> 0) & 0x1F) * 255 / 0x1F) + << 0); } else if (palmHeader.directColor) { *colormapP = NULL; *directColorInfoP = directInfoType; diff --git a/doc/HISTORY b/doc/HISTORY index 7f6dc1fd..065efd5c 100644 --- a/doc/HISTORY +++ b/doc/HISTORY @@ -4,6 +4,19 @@ Netpbm. CHANGE HISTORY -------------- +10.01.23 BJH Release 10.35.73 + + Restore ability of Pnmconvol convolution matrix to be a + pseudo-plain-PNM with samples that exceed the maxval. Lost in + 10.30 due to addition of maxval-checking code to libnetpbm. + + pnmtofiasco: fix bug: doesn't work with Standard Input. + + palmtopnm: fix incorrect "PALM_DIRECT_COLOR_FLAG is not valid + for version 3 encoding type" failure. + + palmtopnm: fix incorrect output with version 3 direct color. + 09.12.29 BJH Release 10.35.72 libnetpbm: When reading plain format PNM with PAM routines, diff --git a/editor/pnmconvol.c b/editor/pnmconvol.c index 0bf44ce3..651b9049 100644 --- a/editor/pnmconvol.c +++ b/editor/pnmconvol.c @@ -17,6 +17,8 @@ /* A change history is at the bottom */ +#include + #include "pnm.h" #include "shhopt.h" #include "mallocvar.h" @@ -1821,11 +1823,86 @@ convolveIt(int const format, +static void +readKernel(const char * const fileName, + int * const colsP, + int * const rowsP, + xelval * const maxvalP, + int * const formatP, + xel *** const xelsP) { +/*---------------------------------------------------------------------------- + Read in the pseudo-PNM that is the convolution matrix. + + This is essentially pnm_readpnm(), except that it can take sample values + that exceed the maxval, which is not legal in PNM. That's why it's + psuedo-PNM and not true PNM. +-----------------------------------------------------------------------------*/ + + /* pm_getuint() is supposed to be internal to libnetpbm, but since we're + doing this backward compatibility hack here, we use it anyway. + */ + + unsigned int + pm_getuint(FILE * const file); + + FILE * fileP; + xel ** xels; + int cols, rows; + xelval maxval; + int format; + unsigned int row; + + fileP = pm_openr(fileName); + + pnm_readpnminit(fileP, &cols, &rows, &maxval, &format); + + xels = pnm_allocarray(cols, rows); + + for (row = 0; row < rows; ++row) { + if (format == PGM_FORMAT || format == PPM_FORMAT) { + /* Plain format -- can't use pnm_readpnmrow() because it will + reject a sample > maxval + */ + unsigned int col; + for (col = 0; col < cols; ++col) { + switch (format) { + case PGM_FORMAT: { + gray const g = pm_getuint(fileP); + PNM_ASSIGN1(xels[row][col], g); + } break; + case PPM_FORMAT: { + pixval const r = pm_getuint(fileP); + pixval const g = pm_getuint(fileP); + pixval const b = pm_getuint(fileP); + + PNM_ASSIGN(xels[row][col], r, g, b); + } break; + default: + assert(false); + } + } + } else { + /* Raw or PBM format -- pnm_readpnmrow() won't do any maxval + checking + */ + pnm_readpnmrow(fileP, xels[row], cols, maxval, format); + } + } + *colsP = cols; + *rowsP = rows; + *maxvalP = maxval; + *formatP = format; + *xelsP = xels; + + pm_close(fileP); +} + + + int main(int argc, char * argv[]) { struct cmdlineInfo cmdline; - FILE* cifp; xel** cxels; int cformat; xelval cmaxval; @@ -1838,11 +1915,8 @@ main(int argc, char * argv[]) { parseCommandLine(argc, argv, &cmdline); - cifp = pm_openr(cmdline.kernelFilespec); - - /* Read in the convolution matrix. */ - cxels = pnm_readpnm(cifp, &ccols, &crows, &cmaxval, &cformat); - pm_close(cifp); + readKernel(cmdline.kernelFilespec, + &ccols, &crows, &cmaxval, &cformat, &cxels); if (ccols % 2 != 1 || crows % 2 != 1) pm_error("the convolution matrix must have an odd number of " -- cgit 1.4.1