📄 std_forward_info.c
字号:
/*****************************************************************************/
/* Copyright 1998, Hewlett-Packard Company */
/* All rights reserved */
/* File: "std_forward_info.c" */
/* Description: Standard implementation of the `forward_info' object. */
/* Author: David Taubman */
/* Affiliation: Hewlett-Packard and */
/* The University of New South Wales, Australia */
/* Version: VM9.0 */
/* Last Revised: 18 April, 2001 */
/*****************************************************************************/
/*****************************************************************************/
/* Modifications tagged with comment lines or delimiters involving the */
/* string, "David T mod" or "mod by David T", have been made by David */
/* Taubman; they are copyrighted by Hewlett-Packard Company with all rights */
/* reserved for the modified parts. */
/*****************************************************************************/
/*****************************************************************************/
/* Modified by David Taubman to include masking-sensitive distortion */
/* calculations for R-D optimization (`-Cvis' option). Material identified */
/* by "David T Cvis mod" comments has been added by David Taubman; it is */
/* copyrighted by the University of New South Wales (Copyright 1999) with */
/* all rights reserved for the modified parts. */
/*****************************************************************************/
/*****************************************************************************/
/* Modified to include Lagrangian Rate Allocation */
/* Copyright 1999 Fujifilm Software Colifornia Inc. */
/* Copyright 1999 Science Applications International Corporation (SAIC). */
/* Copyright 1995 University of Arizona, Arizona Board of Regents. */
/* All Rights Reserved for modified parts. */
/*****************************************************************************/
/*****************************************************************************/
/* Modified by David Taubman to support arbitrary reference points for the */
/* transform and the various regular partitions, so as to facilitate */
/* cropping and geometric transformations in the compressed domain and to */
/* enable full support for CRF's single-sample overlap Wavelet transform, */
/* as originally documented in Seoul. Changes are too numerous to flag */
/* individually within the code. Changes copyrighted by HP with all rights */
/* reserved for the modified parts. */
/*****************************************************************************/
/*****************************************************************************/
/* Modified by David Taubman to reflect slight modifications in the canvas */
/* coordinate system conventions. Changes copyrighted by HP with all rights */
/* reserved for the modified parts. */
/*****************************************************************************/
/*****************************************************************************/
/* Modified by David Taubman to support interface modifications, arbitrary */
/* changes in coding parameters from component to component and from tile */
/* to tile, to support the full generality of PART-1 of the JPEG2000 */
/* standard, and to support most anticipated generality of PART-2. Changes */
/* are too numerous to flag individually within the code, which in some */
/* places has been completely rewritten. All changes copyrighted by HP with */
/* all rights reserved for the modified parts. */
/*****************************************************************************/
/*****************************************************************************/
/* Modified by David Taubman to allow overlapping tiles. Changes */
/* copyrighted by HP with all rights reserved for the modified parts. */
/*****************************************************************************/
/*****************************************************************************/
/* Modified by David Taubman to implement changes in Tokyo between CD and */
/* FCD for Part-1 of the standard. Copyrighted by HP with all rights */
/* reserved for the modified parts. */
/*****************************************************************************/
/*****************************************************************************/
/* Modified for general wavelet decompositions. */
/* Copyright 2000 Science Applications International Corporation (SAIC). */
/* Copyright 1995 University of Arizona, Arizona Board of Regents. */
/* All Rights Reserved for modified parts. */
/*****************************************************************************/
/*****************************************************************************/
/* Modified for general offset and scalar quantization. */
/* Copyright 2000 The MITRE Corporation. */
/* All Rights Reserved for modified parts. */
/*****************************************************************************/
/*****************************************************************************/
/* Modified by Jianxin Wei, Australian Defence Force Academy (ADFA) */
/* to implement the Odd Tile-Size Low-Pass First Convention (OTLPF_CONVENTION)*/
/* Copyright 2000 University of New South Wales, Australia. */
/* All rights reserved for modified parts. */
/*****************************************************************************/
#include <local_services.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <assert.h>
#include <ifc.h>
#include <markers.h>
#include "std_forward_info_local.h"
/* ========================================================================= */
/* -------- File Format for Specifying Quantization Scaling Factors -------- */
/* ========================================================================= */
/* The `forward_info' object's initialization function accepts two
arguments, `-Fsteps' and `-Fweights', which specify files whose
contents identify scaling factors to be applied to the relevant
quantization parameters in each subband. The factors supplied via
the `-Fsteps' file are interpreted as quantization step quotients;
the default quantization step size values are divided by the relevant
scale factors in this case and the `step_wmse' weights to be returned
by `forward_info__get_quant_info' are adjusted to ensure that they
still correctly represent the distortion contributions of the scaled
step sizes (i.e. the distortion weights are not themselves scaled). This
means that step sizes which are scaled down are assigned correspondingly
smaller `step_wmse' values. The factors supplied via the `-Fweights'
file are interpreted as the square roots of distortion weighting factors;
they are squared and multiplied by the relevant `step_wmse' values, but
have no impact on the step sizes themselves. In both cases, the file
format is exactly the same and it is the purpose of this description to
specify the syntax of this file format.
The file consists of one or more sections, where each section identifies
scale factors for one or more components. Each section commences with
a single line of the form
(<C_min> <C_max>)
where C_min and C_max identify the range of 1-based component indices,
to which the relevant scaling factors are to be applied.
The parentheses surrounding the C_min and C_max values on this first
line must be present. The factors are applied uniformly to corresponding
subbands in all components of the specified range.
Within each section, the factors are grouped into one or more
level blocks, which are separated by one or more blank lines. Within
a level block, blank lines are disallowed for obvious reasons. The first
level block identifies scaling factors for the subbands consituting the
highest resolution level (highest frequency subbands); the second level
block identifies scaling factors for the subbands of the second highest
resolution level and so forth. If there are more resolution levels
than level blocks, the last level block's factors are applied to the
subbands of all subsequent resolution levels.
The first line of each level block must have the form
<LL_factor> <HL_factor> <LH_factor> <HH_factor>
It identifies scaling factors to be applied to all subbands whose major
orientation is LL, HL (horizontally high-pass), LH (vertically high-pass)
or HH (diagonally high-pass), respectively. If this is the lowest
resolution level then the LL_factor is applied to the LL band and the
other 3 factors are ignored (although they must appear in the file). For
all other resolution levels, the LL_factor is ignored. The major
orientation is the first out of a maximum of 3 2D DWT stages which are
supported within any given resolution level -- although the lowest
resolution level cannot have further 2D DWT stages. The major orientation
is sufficient for the Mallat decomposition. It is possible, though not
mandatory, to specify additional scaling factors for any given level
block. To specify additional scaling factors (these will be multiplied
by the factors determined by the first line) for subbands which might be
found at a second level in the decomposition of the LH, HL and HH bands
from the primary decomposition, three more lines of text must appear,
each with the form
<LL_factor> <HL_factor> <LH_factor> <HH_factor>
The lines hold the additional factors to be applied to subbands with
the HL, LH and HH major orientations, in that order, where the
new values are to be applied to subbands with the LL, HL, LH and
HH minor orientations, respectively. If the relevant resolution level
does not have a second level of high-pass subband decomposition (most
common), or if it is the lowest resolution level (LL band only), these
extra specifications are simply ignored. The same approach is used to
allow specification of additional factors for a third level of high-pass
subband decomposition. In this case, another 12 lines of text must appear,
each with the form
<LL_factor> <HL_factor> <LH_factor> <HH_factor>
The first group of four lines specifies additional factors for the
subbands whose major orientation was HL, with minor orientations of
LL, HL, LH and HH, respectively; the second group specifies additional
factors for the subbands whose major orientation was LH, with minor
orientations of LL, HL, LH and HH, respectively; and so forth. The
four scale factors on each line are to be applied to subbands whose
most-minor orientation is LL, HL, LH and HH, respectively. It is
unlikely that this ability to specify scale factors at the minor and
most-minor orientation level will be heavily utilized.
We point out finally that any line of text, including the blank
lines required to separate level blocks, may contain comments, separated
from the initial part of the line by a `#' character. */
/* ========================================================================= */
/* -------------------------- Internal Functions --------------------------- */
/* ========================================================================= */
/*****************************************************************************/
/* MACRO CEIL_RATIO */
/*****************************************************************************/
#define CEIL_RATIO(_num,_den) (((_num)+(_den)-1)/(_den))
/*****************************************************************************/
/* STATIC find_compatible_factors */
/*****************************************************************************/
static int
find_compatible_factors(int num_dims, int *dims, int *factors, int missing)
/* This function does all the essential work of `compute_subsampling_factors'.
On input, there are `num_dims' dimensions, stored in the `dims' array.
The function computes a hi-res canvas dimension and a collection of
sub-sampling factors such that ceil(d/f_i)-ceil(m/f_i)=d_i, where d
is the hi-res canvas dimension, m is the missing dimension, d_i is the
i'th entry in the `dims' array and f_i is the i'th sub-sampling factor.
The function fills in the `factors' array with these factors and returns
the hi-res dimension. If a compatible set of sub-sampling factors cannot
be found, in the range 0 to 255, the function returns 0. */
{
int n, ref_dim, ref_factor;
int min_canvas, max_canvas, miss;
int factor, min_dim=0, max_dim=0; /* TJF: =0 to avoid warnings */
/* The reference component is the one which will require the least
amount of sub-sampling. Determine its dimension. */
for (ref_dim=0, n=0; n < num_dims; n++)
if (dims[n] > ref_dim)
ref_dim = dims[n];
/* Walk through all possible sub-sampling factors for the reference
dimension. */
for (ref_factor=1; ref_factor < 256; ref_factor++)
{
miss = CEIL_RATIO(missing,ref_factor);
max_canvas = (miss + ref_dim) * ref_factor;
min_canvas = max_canvas - ref_factor + 1;
for (n=0; n < num_dims; n++)
{
factor = CEIL_RATIO(min_canvas-missing,dims[n]+2);
for (; factor < 256; factor++)
{
miss = CEIL_RATIO(missing,factor);
max_dim = (miss + dims[n]) * factor;
min_dim = max_dim - factor + 1;
if (max_dim >= min_canvas)
break;
}
if (max_dim < max_canvas)
max_canvas = max_dim;
if (min_dim > min_canvas)
min_canvas = min_dim;
if ((factor > 255) || (min_canvas > max_canvas))
break;
factors[n] = factor;
}
if (n == num_dims)
return(min_canvas);
}
return(0);
}
/*****************************************************************************/
/* STATIC compute_subsampling_factors */
/*****************************************************************************/
static void
compute_subsampling_factors(int num_components,
canvas_dims_ptr comp_dims,
std_component_info_ptr comp_info,
canvas_dims_ptr grid_dims)
/* On entry, the number of high res missing rows and columns is available
through `grid_dims->top_row' and `grid_dims->left_col'. Our objective
is to fill in the high res image dimensions in `grid_dims->rows' and
`grid_dims->cols' and the sub-sampling factors for each
component, based on the supplied dimensions. The idea is simply to
use a brute force search for sub-sampling factors which are compatible
with the supplied component dimensions and the "missing image" dimensions.
For more information regarding this compatibility, the reader is referred
to the special topic entitled "Blocks, Tiles and Frames" in "ifc.h".
A brute force search is appropriate because the sub-sampling factors must
lie in the range 1 through 255. */
{
int dims[16384], factors[16384], hires; /* J. Kasner fix */
int c;
/* Start with the vertical sampling parameters. */
for (c=0; c < num_components; c++)
dims[c] = comp_dims[c].rows;
hires =
find_compatible_factors(num_components,dims,factors,grid_dims->top_row);
if (hires == 0)
local_error("Unable to find a high resolution grid and a collection "
"of sub-sampling factors to relate the supplied component "
"dimensions!");
grid_dims->rows = hires - grid_dims->top_row;
for (c=0; c < num_components; c++)
comp_info[c].vert_subsampling = factors[c];
/* Now compute the horizontal sampling parameters. */
for (c=0; c < num_components; c++)
dims[c] = comp_dims[c].cols;
hires =
find_compatible_factors(num_components,dims,factors,grid_dims->left_col);
if (hires == 0)
local_error("Unable to find a high resolution grid and a collection "
"of sub-sampling factors to relate the supplied component "
"dimensions!");
grid_dims->cols = hires - grid_dims->left_col;
for (c=0; c < num_components; c++)
comp_info[c].hor_subsampling = factors[c];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -