1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
/* This file is part of jpegpixi, a program to interpolate pixels in
JFIF image files.
Copyright (C) 2002, 2003, 2004 Martin Dickopp
Jpegpixi is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
Jpegpixi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with jpegpixi; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "util.h"
#include <stdio.h>
#if HAVE_MATH_H
# include <math.h>
#else
double fabs ();
#endif
#if HAVE_FLOAT_H
# include <float.h>
#endif
#ifndef DBL_MIN
# define DBL_MIN 1e-37
#endif
#ifndef DBL_MAX
# define DBL_MAX 1e37
#endif
#include "rbtree.h"
#include "jpegpixi.h"
#include "optpixi.h"
/* Check if downsampled coordinates are valid. */
#define XY_VALID(xx,yy) \
((xx) >= 0 && (xx) < (int)comp->downsampled_width && (yy) >= 0 && (yy) < (int)comp->downsampled_height)
/* Tree of weight data blocks. */
struct rbtree weight_s_tree;
static void set_weighted_pixels (struct jfif *jfif, const jpeg_component_info *comp, int icomp,
int x_pos, int y_pos, int x_size, int y_size, enum dimdir_t d, int poly_order);
static int weight_s_cmp (const void *a, const void *b) gcc_attr_pure gcc_attr_nonnull (());
/* Initialize interpolator. */
void
init_interpolator (void)
{
rbtree_create (&weight_s_tree);
}
/* Interpolate a pixel block. */
void
interpolate (const struct jpeg_decompress_struct *const jpg, struct jfif *const jfif,
const struct point_dimdir_s *const point, const enum method_t method)
{
int icomp;
/* Iterate over components. */
for (icomp = 0; icomp < jpg->num_components; ++icomp)
{
const jpeg_component_info *const comp = &(jpg->comp_info [icomp]);
/* Downsampled position of the pixel block. */
const int x_pos = point->p.x * comp->h_samp_factor / jpg->max_h_samp_factor;
const int y_pos = point->p.y * comp->v_samp_factor / jpg->max_v_samp_factor;
/* Downsampled size of the pixel block. */
const int x_size = ((point->p.x_size + jpg->max_h_samp_factor / comp->h_samp_factor - 1)
/ (jpg->max_h_samp_factor / comp->h_samp_factor));
const int y_size = ((point->p.y_size + jpg->max_v_samp_factor / comp->v_samp_factor - 1)
/ (jpg->max_v_samp_factor / comp->v_samp_factor));
/* Show pixel block. */
if (opt_verbose && icomp == 0)
fprintf (stderr, _("%s: interpolating %u,%u,%u,%u (%s)\n"), invocation_name,
point->p.x, point->p.y, point->p.x_size, point->p.y_size,
point->d == vertical ? _("vertical") : point->d == horizontal ? _("horizontal") : _("2-dim"));
/* Interpolate pixel. */
switch (method)
{
case average:
/* Average of adjacent pixels. */
switch (point->d)
{
case vertical:
{
int x;
for (x = 0; x < x_size; ++x)
{
int sum_weight = 0;
double v = 0.0;
if (XY_VALID (x_pos + x, y_pos - 1))
{
v += get_pixel (jfif, icomp, x_pos + x, y_pos - 1);
++sum_weight;
}
if (XY_VALID (x_pos + x, y_pos + y_size))
{
v += get_pixel (jfif, icomp, x_pos + x, y_pos + y_size);
++sum_weight;
}
if (sum_weight > 0)
{
int y;
v /= (double)sum_weight;
for (y = 0; y < y_size; ++y)
if (XY_VALID (x_pos + x, y_pos + y))
set_pixel (jfif, icomp, x_pos + x, y_pos + y, v);
}
}
}
break;
case horizontal:
{
int y;
for (y = 0; y < y_size; ++y)
{
int sum_weight = 0;
double v = 0.0;
if (XY_VALID (x_pos - 1, y_pos + y))
{
v += get_pixel (jfif, icomp, x_pos - 1, y_pos + y);
++sum_weight;
}
if (XY_VALID (x_pos + x_size, y_pos + y))
{
v += get_pixel (jfif, icomp, x_pos + x_size, y_pos + y);
++sum_weight;
}
if (sum_weight > 0)
{
int x;
v /= (double)sum_weight;
for (x = 0; x < x_size; ++x)
if (XY_VALID (x_pos + x, y_pos + y))
set_pixel (jfif, icomp, x_pos + x, y_pos + y, v);
}
}
}
break;
case twodim:
{
int sum_weight = 0;
double v = 0.0;
int x, y;
for (x = 0; x < x_size; ++x)
{
if (XY_VALID (x_pos + x, y_pos - 1))
{
v += get_pixel (jfif, icomp, x_pos + x, y_pos - 1);
++sum_weight;
}
if (XY_VALID (x_pos + x, y_pos + y_size))
{
v += get_pixel (jfif, icomp, x_pos + x, y_pos + y_size);
++sum_weight;
}
}
for (y = 0; y < y_size; ++y)
{
if (XY_VALID (x_pos - 1, y_pos + y))
{
v += get_pixel (jfif, icomp, x_pos - 1, y_pos + y);
++sum_weight;
}
if (XY_VALID (x_pos + x_size, y_pos + y))
{
v += get_pixel (jfif, icomp, x_pos + x_size, y_pos + y);
++sum_weight;
}
}
if (sum_weight > 0)
{
v /= (double)sum_weight;
for (x = 0; x < x_size; ++x)
for (y = 0; y < y_size; ++y)
if (XY_VALID (x_pos + x, y_pos + y))
set_pixel (jfif, icomp, x_pos + x, y_pos + y, v);
}
}
}
break;
case linear:
/* (Bi)linear interpolation. */
set_weighted_pixels (jfif, comp, icomp, x_pos, y_pos, x_size, y_size, point->d, 1);
break;
case quadratic:
/* (Bi)quadratic interpolation. */
set_weighted_pixels (jfif, comp, icomp, x_pos, y_pos, x_size, y_size, point->d, 2);
break;
case cubic:
/* (Bi)cubic interpolation. */
set_weighted_pixels (jfif, comp, icomp, x_pos, y_pos, x_size, y_size, point->d, 3);
}
}
}
static void
set_weighted_pixels (struct jfif *const jfif, const jpeg_component_info *const comp, const int icomp,
const int x_pos, const int y_pos, const int x_size, const int y_size,
const enum dimdir_t d, const int poly_order)
{
const struct weight_s *w;
int x, y;
/* Obtain weight data block. */
{
struct weight_s tmp;
/* In the one-dimensional case, `x_size' must be 1, and the size must be stored in `y_size'. */
tmp.x_size = (d == twodim ? x_size : 1);
tmp.y_size = (d == horizontal ? x_size : y_size);
tmp.poly_order = poly_order;
tmp.is_twodim = d == twodim;
w = rbtree_find (&weight_s_tree, &tmp, weight_s_cmp);
if (w == 0)
{
struct weight_s *const new_w = get_weights (tmp.x_size, tmp.y_size, tmp.is_twodim, poly_order);
if ((w = rbtree_insert (&weight_s_tree, new_w, sizeof *new_w, weight_s_cmp, 0)) == 0)
mem_alloc_failed ();
free (new_w);
}
}
/* Calculate weighted pixel sum. */
for (y = 0; y < y_size; ++y)
for (x = 0; x < x_size; ++x)
if (XY_VALID (x_pos + x, y_pos + y))
{
const double *const weights = w->weights [d == vertical ? y : d == horizontal ? x : x + x_size * y];
double sum_weight = 0.0, v = 0.0;
size_t i;
for (i = 0; i < w->num_pos; ++i)
{
/* In the one-dimensional case, only the y coordinate is used. */
const int xx = x_pos + (d == vertical ? x : d == horizontal ? w->pos [i].y : w->pos [i].x);
const int yy = y_pos + (d == horizontal ? y : w->pos [i].y);
if (XY_VALID (xx, yy))
{
const double weight = weights [i];
v += weight * get_pixel (jfif, icomp, xx, yy);
sum_weight += weight;
}
}
if (fabs (v) <= DBL_MIN)
set_pixel (jfif, icomp, x_pos + x, y_pos + y, 0.0);
else if (sum_weight >= DBL_MIN)
set_pixel (jfif, icomp, x_pos + x, y_pos + y, v / sum_weight);
else if (v >= 0.0)
set_pixel (jfif, icomp, x_pos + x, y_pos + y, DBL_MAX);
else
set_pixel (jfif, icomp, x_pos + x, y_pos + y, -DBL_MAX);
}
}
/* Compare two weight data blocks. */
static int
weight_s_cmp (const void *const a, const void *const b)
{
const struct weight_s *const weight_s_a = a;
const struct weight_s *const weight_s_b = b;
if (weight_s_a->is_twodim < weight_s_b->is_twodim)
return -1;
else if (weight_s_a->is_twodim > weight_s_b->is_twodim)
return 1;
else if (weight_s_a->poly_order < weight_s_b->poly_order)
return -1;
else if (weight_s_a->poly_order > weight_s_b->poly_order)
return 1;
else if (weight_s_a->x_size < weight_s_b->x_size)
return -1;
else if (weight_s_a->x_size > weight_s_b->x_size)
return 1;
else if (weight_s_a->y_size < weight_s_b->y_size)
return -1;
else if (weight_s_a->y_size > weight_s_b->y_size)
return 1;
else
return 0;
}
|