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 342
|
/* -*- c -*- */
/*
* zoom.c
*
* metapixel
*
* Copyright (C) 2004 Mark Probst
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <math.h>
#include <stdlib.h>
#include <assert.h>
#include "zoom.h"
#ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif
#ifndef MAX
#define MAX(a,b) ((a)>(b)?(a):(b))
#endif
#define MAX_FILTER FILTER_MITCHELL
typedef struct
{
int index;
float weight;
int iweight;
} sample_t;
typedef struct
{
int num_samples;
sample_t samples[0];
} sample_window_t;
static float
filter_box (float x)
{
if (x < -0.5)
return 0.0;
if (x <= 0.5)
return 1.0;
return 0.0;
}
static float
filter_triangle (float x)
{
if (x < -1.0)
return 0.0;
if (x < 0.0)
return 1.0 + x;
if (x < 1.0)
return 1.0 - x;
return 0.0;
}
/*
* see Mitchell&Netravali, "Reconstruction Filters in Computer
* Graphics", Proceedings of the 15th annual conference on Computer
* graphics and interactive techniques, ACM Press 1988
*/
static float
filter_mitchell (float x)
{
#define B (1.0 / 3.0)
#define C (1.0 / 3.0)
static float a0 = ( 6.0 - 2.0 * B ) / 6.0;
static float a2 = (-18.0 + 12.0 * B + 6.0 * C) / 6.0;
static float a3 = ( 12.0 - 9.0 * B - 6.0 * C) / 6.0;
static float b0 = ( 8.0 * B + 24.0 * C) / 6.0;
static float b1 = ( - 12.0 * B - 48.0 * C) / 6.0;
static float b2 = ( 6.0 * B + 30.0 * C) / 6.0;
static float b3 = ( - B - 6.0 * C) / 6.0;
x = fabsf(x);
if (x < 1.0)
return a0 + (x * x) * (a2 + x * a3);
if (x < 2.0)
return b0 + x * (b1 + x * (b2 + x * b3));
return 0.0;
#undef b
#undef c
}
static filter_t filters[] = {
{ &filter_box, 0.5 },
{ &filter_triangle, 1.0 },
{ &filter_mitchell, 2.0 }
};
filter_t*
get_filter (int index)
{
if (index < 0 || index > MAX_FILTER)
return 0;
return &filters[index];
}
#define NUM_ACCURACY_BITS 12
static sample_window_t*
make_sample_window (float center, float scale, float support_radius, filter_func_t filter_func, int num_indexes)
{
float lower_bound = center - support_radius;
float upper_bound = center + support_radius;
int lower_index = floor(lower_bound + 0.5);
int upper_index = floor(upper_bound - 0.5);
int num_samples;
sample_window_t *window;
int i;
float weight_sum;
lower_index = MAX(0, lower_index);
upper_index = MIN(num_indexes - 1, upper_index);
if (upper_index < lower_index)
upper_index = lower_index = floor(center);
num_samples = upper_index - lower_index + 1;
assert(num_samples > 0);
window = (sample_window_t*)malloc(sizeof(sample_window_t) + num_samples * sizeof(sample_t));
assert(window != 0);
window->num_samples = num_samples;
weight_sum = 0.0;
for (i = 0; i < num_samples; ++i)
{
int index = lower_index + i;
float sample_center = (float)index + 0.5;
window->samples[i].index = index;
window->samples[i].weight = filter_func((sample_center - center) / scale);
weight_sum += window->samples[i].weight;
}
assert(weight_sum > 0.0);
for (i = 0; i < num_samples; ++i)
{
window->samples[i].weight /= weight_sum;
window->samples[i].iweight = (1 << NUM_ACCURACY_BITS) * window->samples[i].weight;
}
return window;
}
static sample_window_t**
make_sample_windows (float filter_scale, float filter_support_radius, filter_func_t filter_func,
int dest_size, int src_size, float scale)
{
sample_window_t **sample_windows;
int i;
sample_windows = (sample_window_t**)malloc(dest_size * sizeof(sample_window_t*));
assert(sample_windows != 0);
for (i = 0; i < dest_size; ++i)
{
float dest_center = (float)i + 0.5;
float src_center = dest_center / scale;
sample_windows[i] = make_sample_window(src_center, filter_scale, filter_support_radius,
filter_func, src_size);
assert(sample_windows[i] != 0);
}
return sample_windows;
}
static void
free_sample_windows (sample_window_t **sample_windows, int size)
{
int i;
for (i = 0; i < size; ++i)
free(sample_windows[i]);
free(sample_windows);
}
static void
zoom_unidirectional (unsigned char *dest, unsigned char *src, int num_channels, sample_window_t **sample_windows,
int num_pixels_in_entity, int num_entities,
int dest_pixel_advance, int src_pixel_advance,
int dest_entity_advance, int src_entity_advance)
{
int i;
unsigned char *dest_entity, *src_entity;
int channels[num_channels];
dest_entity = dest;
src_entity = src;
for (i = 0; i < num_entities; ++i)
{
int j;
unsigned char *dest_pixel;
dest_pixel = dest_entity;
for (j = 0; j < num_pixels_in_entity; ++j)
{
int k;
for (k = 0; k < num_channels; ++k)
channels[k] = 0;
for (k = 0; k < sample_windows[j]->num_samples; ++k)
{
int l;
sample_t *sample = &sample_windows[j]->samples[k];
unsigned char *src_pixel = &src_entity[sample->index * src_pixel_advance];
for (l = 0; l < num_channels; ++l)
channels[l] += (int)src_pixel[l] * sample->iweight;
/* ((((int)src_pixel[l]) << CHANNEL_SHIFT) + (1 << (CHANNEL_SHIFT - 1))) * sample->weight; */
}
for (k = 0; k < num_channels; ++k)
{
int value = channels[k] >> NUM_ACCURACY_BITS;
dest_pixel[k] = MAX(0, MIN(255, value));
}
dest_pixel += dest_pixel_advance;
}
dest_entity += dest_entity_advance;
src_entity += src_entity_advance;
}
}
void
zoom_image (unsigned char *dest, unsigned char *src,
filter_t *filter, int num_channels,
int dest_width, int dest_height, int dest_row_stride,
int src_width, int src_height, int src_row_stride)
{
float x_scale, y_scale;
float filter_x_scale, filter_y_scale;
float filter_x_support_radius, filter_y_support_radius;
sample_window_t **x_sample_windows, **y_sample_windows;
unsigned char *temp_image;
assert(dest != 0 && src != 0 && filter != 0);
assert(dest_width > 0 && dest_height > 0);
x_scale = (float)dest_width / (float)src_width;
y_scale = (float)dest_height / (float)src_height;
filter_x_scale = MAX(1.0, 1.0 / x_scale);
filter_y_scale = MAX(1.0, 1.0 / y_scale);
filter_x_support_radius = filter->support_radius * filter_x_scale;
filter_y_support_radius = filter->support_radius * filter_y_scale;
x_sample_windows = make_sample_windows(filter_x_scale, filter_x_support_radius, filter->func,
dest_width, src_width, x_scale);
y_sample_windows = make_sample_windows(filter_y_scale, filter_y_support_radius, filter->func,
dest_height, src_height, y_scale);
temp_image = (unsigned char*)malloc(num_channels * dest_width * src_height);
zoom_unidirectional(temp_image, src, num_channels, x_sample_windows,
dest_width, src_height,
num_channels, num_channels,
dest_row_stride, src_row_stride);
zoom_unidirectional(dest, temp_image, num_channels, y_sample_windows,
dest_height, dest_width,
dest_row_stride, dest_row_stride,
num_channels, num_channels);
free(temp_image);
free_sample_windows(x_sample_windows, dest_width);
free_sample_windows(y_sample_windows, dest_height);
}
#ifdef TEST_ZOOM
#include <stdio.h>
#include "readimage.h"
#include "writeimage.h"
int
main (int argc, char *argv[])
{
unsigned char *src, *dst;
int src_width, src_height;
int dst_width, dst_height;
void *png_write_data;
if (argc != 5)
{
fprintf(stderr, "Usage: %s <in-image> <out-width> <out-height> <out-image>\n", argv[0]);
return 1;
}
src = read_image(argv[1], &src_width, &src_height);
assert(src != 0);
dst_width = atoi(argv[2]);
dst_height = atoi(argv[3]);
dst = (unsigned char*)malloc(3 * dst_width * dst_height);
zoom_image(dst, src, get_filter(FILTER_TRIANGLE), 3,
dst_width, dst_height, dst_width * 3,
src_width, src_height, src_width * 3);
write_image(argv[4], dst_width, dst_height, dst, IMAGE_FORMAT_PNG);
return 0;
}
#endif
|