| 1 |
/* |
| 2 |
* misc.c: Some useful functions, that don't fit in one of |
| 3 |
* the other files and that are needed by at least |
| 4 |
* two modules. |
| 5 |
* |
| 6 |
* Written by: Stefan Frank |
| 7 |
* Ullrich Hafner |
| 8 |
* |
| 9 |
* This file is part of FIASCO (Fractal Image And Sequence COdec) |
| 10 |
* Copyright (C) 1994-2000 Ullrich Hafner |
| 11 |
*/ |
| 12 |
|
| 13 |
/* |
| 14 |
* $Date: 2000/06/14 20:49:37 $ |
| 15 |
* $Author: hafner $ |
| 16 |
* $Revision: 5.1 $ |
| 17 |
* $State: Exp $ |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "config.h" |
| 21 |
|
| 22 |
#include <math.h> |
| 23 |
#include <ctype.h> |
| 24 |
|
| 25 |
#ifdef TIME_WITH_SYS_TIME |
| 26 |
# include <sys/time.h> |
| 27 |
# include <time.h> |
| 28 |
#else /* not TIME_WITH_SYS_TIME */ |
| 29 |
# if HAVE_SYS_TIME_H |
| 30 |
# include <sys/time.h> |
| 31 |
# else /* not HAVE_SYS_TIME_H */ |
| 32 |
# include <time.h> |
| 33 |
# endif /* not HAVE_SYS_TIME_H */ |
| 34 |
#endif /* not TIME_WITH_SYS_TIME */ |
| 35 |
|
| 36 |
#include <stdlib.h> |
| 37 |
#include <string.h> |
| 38 |
|
| 39 |
#include "pm_c_util.h" |
| 40 |
|
| 41 |
#include "types.h" |
| 42 |
#include "macros.h" |
| 43 |
#include "error.h" |
| 44 |
|
| 45 |
#include "bit-io.h" |
| 46 |
#include "misc.h" |
| 47 |
|
| 48 |
/***************************************************************************** |
| 49 |
|
| 50 |
prototypes |
| 51 |
|
| 52 |
*****************************************************************************/ |
| 53 |
|
| 54 |
static void |
| 55 |
remove_comments (FILE *file); |
| 56 |
|
| 57 |
/***************************************************************************** |
| 58 |
|
| 59 |
public code |
| 60 |
|
| 61 |
*****************************************************************************/ |
| 62 |
|
| 63 |
void * |
| 64 |
Calloc (size_t n, size_t size) |
| 65 |
/* |
| 66 |
* Allocate memory like calloc (). |
| 67 |
* |
| 68 |
* Return value: Pointer to the new block of memory on success, |
| 69 |
* otherwise the program is terminated. |
| 70 |
*/ |
| 71 |
{ |
| 72 |
void *ptr; /* pointer to the new memory block */ |
| 73 |
|
| 74 |
if (n <= 0 || size <= 0) |
| 75 |
error ("Can't allocate memory for %d items of size %d", |
| 76 |
(int) n, (int) size); |
| 77 |
|
| 78 |
ptr = calloc (n, size); |
| 79 |
if (!ptr) |
| 80 |
error ("Out of memory!"); |
| 81 |
|
| 82 |
return ptr; |
| 83 |
} |
| 84 |
|
| 85 |
void |
| 86 |
Free (void *memory) |
| 87 |
/* |
| 88 |
* Free memory given by the pointer 'memory' |
| 89 |
* |
| 90 |
* No return value. |
| 91 |
*/ |
| 92 |
{ |
| 93 |
if (memory != NULL) |
| 94 |
free (memory); |
| 95 |
else |
| 96 |
warning ("Can't free memory block <NULL>."); |
| 97 |
} |
| 98 |
|
| 99 |
unsigned |
| 100 |
prg_timer (clock_t *last_timer, enum action_e action) |
| 101 |
/* |
| 102 |
* If 'action' == START then store current value of system timer. |
| 103 |
* If 'action' == STOP then compute number of elapsed micro seconds since |
| 104 |
* the last time 'prg_timer' was called |
| 105 |
* with 'action' == START. |
| 106 |
* |
| 107 |
* Return value: |
| 108 |
* Number of elapsed micro seconds if 'action' == STOP |
| 109 |
* 0 if 'action' == START |
| 110 |
* |
| 111 |
* Side effects: |
| 112 |
* last_timer is set to current timer if action == START |
| 113 |
*/ |
| 114 |
{ |
| 115 |
assert (last_timer); |
| 116 |
|
| 117 |
if (action == START) |
| 118 |
{ |
| 119 |
*last_timer = clock (); |
| 120 |
return 0; |
| 121 |
} |
| 122 |
else |
| 123 |
return (clock () - *last_timer) / (CLOCKS_PER_SEC / 1000.0); |
| 124 |
} |
| 125 |
|
| 126 |
real_t |
| 127 |
read_real (FILE *infile) |
| 128 |
/* |
| 129 |
* Read one real value from the given input stream 'infile'. |
| 130 |
* |
| 131 |
* Return value: |
| 132 |
* real value on success |
| 133 |
*/ |
| 134 |
{ |
| 135 |
float input; |
| 136 |
|
| 137 |
assert (infile); |
| 138 |
|
| 139 |
remove_comments (infile); |
| 140 |
if (fscanf(infile, "%f", &input) != 1) |
| 141 |
error("Can't read float value!"); |
| 142 |
|
| 143 |
return (real_t) input; |
| 144 |
} |
| 145 |
|
| 146 |
int |
| 147 |
read_int (FILE *infile) |
| 148 |
/* |
| 149 |
* Read one integer value from the given input stream 'infile'. |
| 150 |
* |
| 151 |
* Return value: |
| 152 |
* integer value on success |
| 153 |
*/ |
| 154 |
{ |
| 155 |
int input; /* integer */ |
| 156 |
|
| 157 |
assert (infile); |
| 158 |
|
| 159 |
remove_comments (infile); |
| 160 |
if (fscanf(infile, "%d", &input) != 1) |
| 161 |
error("Can't read integer value!"); |
| 162 |
|
| 163 |
return input; |
| 164 |
} |
| 165 |
|
| 166 |
static void |
| 167 |
remove_comments (FILE *file) |
| 168 |
/* |
| 169 |
* Remove shell/pgm style comments (#) from the input 'file' |
| 170 |
* |
| 171 |
* No return value. |
| 172 |
*/ |
| 173 |
{ |
| 174 |
int c; /* current character */ |
| 175 |
|
| 176 |
assert (file); |
| 177 |
|
| 178 |
do |
| 179 |
{ |
| 180 |
while (isspace(c = getc (file))) |
| 181 |
; |
| 182 |
if (c == EOF) |
| 183 |
error ("EOF reached, input seems to be truncated!"); |
| 184 |
if (c == '#') |
| 185 |
{ |
| 186 |
int dummy; |
| 187 |
|
| 188 |
while (((dummy = getc (file)) != '\n') && dummy != EOF) |
| 189 |
; |
| 190 |
if (dummy == EOF) |
| 191 |
error ("EOF reached, input seems to be truncated!"); |
| 192 |
} |
| 193 |
else |
| 194 |
ungetc (c, file); |
| 195 |
} while (c == '#'); |
| 196 |
} |
| 197 |
|
| 198 |
void |
| 199 |
write_rice_code (unsigned value, unsigned rice_k, bitfile_t *output) |
| 200 |
/* |
| 201 |
* Write 'value' to the stream 'output' using Rice coding with base 'rice_k'. |
| 202 |
* |
| 203 |
* No return value. |
| 204 |
*/ |
| 205 |
{ |
| 206 |
unsigned unary; /* unary part of Rice Code */ |
| 207 |
|
| 208 |
assert (output); |
| 209 |
|
| 210 |
for (unary = value >> rice_k; unary; unary--) |
| 211 |
put_bit (output, 1); |
| 212 |
put_bit (output, 0); |
| 213 |
put_bits (output, value & ((1 << rice_k) - 1), rice_k); |
| 214 |
} |
| 215 |
|
| 216 |
unsigned |
| 217 |
read_rice_code (unsigned rice_k, bitfile_t *input) |
| 218 |
/* |
| 219 |
* Read a Rice encoded integer (base 'rice_k') from the stream 'input'. |
| 220 |
* |
| 221 |
* Return value: |
| 222 |
* decoded integer |
| 223 |
*/ |
| 224 |
{ |
| 225 |
unsigned unary; /* unary part of Rice code */ |
| 226 |
|
| 227 |
assert (input); |
| 228 |
|
| 229 |
for (unary = 0; get_bit (input); unary++) /* unary part */ |
| 230 |
; |
| 231 |
|
| 232 |
return (unary << rice_k) | get_bits (input, rice_k); |
| 233 |
} |
| 234 |
|
| 235 |
void |
| 236 |
write_bin_code (unsigned value, unsigned maxval, bitfile_t *output) |
| 237 |
/* |
| 238 |
* Write 'value' to the stream 'output' using an adjusted binary code |
| 239 |
* based on given 'maxval'. |
| 240 |
* |
| 241 |
* No return value. |
| 242 |
*/ |
| 243 |
{ |
| 244 |
unsigned k; |
| 245 |
unsigned r; |
| 246 |
|
| 247 |
assert (output && maxval && value <= maxval); |
| 248 |
|
| 249 |
k = log2 (maxval + 1); |
| 250 |
r = (maxval + 1) % (1 << k); |
| 251 |
|
| 252 |
if (value < maxval + 1 - 2 * r) /* 0, ... , maxval - 2r */ |
| 253 |
put_bits (output, value, k); |
| 254 |
else /* maxval - 2r + 1, ..., maxval */ |
| 255 |
put_bits (output, value + maxval + 1 - 2 * r, k + 1); |
| 256 |
} |
| 257 |
|
| 258 |
unsigned |
| 259 |
read_bin_code (unsigned maxval, bitfile_t *input) |
| 260 |
/* |
| 261 |
* Read a bincode encoded integer from the stream 'input'. |
| 262 |
* |
| 263 |
* Return value: |
| 264 |
* decoded integer |
| 265 |
*/ |
| 266 |
{ |
| 267 |
unsigned k; |
| 268 |
unsigned r; |
| 269 |
unsigned value; |
| 270 |
|
| 271 |
assert (input); |
| 272 |
|
| 273 |
k = log2 (maxval + 1); |
| 274 |
r = (maxval + 1) % (1 << k); |
| 275 |
|
| 276 |
value = get_bits (input, k); |
| 277 |
if (value < maxval + 1 - 2 * r) |
| 278 |
return value; |
| 279 |
else |
| 280 |
{ |
| 281 |
value <<= 1; |
| 282 |
if (get_bit (input)) |
| 283 |
value++; |
| 284 |
return value - maxval - 1 + 2 * r; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
unsigned |
| 289 |
bits_rice_code (unsigned value, unsigned rice_k) |
| 290 |
/* |
| 291 |
* Compute number of bits needed for coding integer 'value' |
| 292 |
* with given Rice code 'rice_k'. |
| 293 |
* |
| 294 |
* Return value: |
| 295 |
* number of bits |
| 296 |
*/ |
| 297 |
{ |
| 298 |
unsigned unary; |
| 299 |
unsigned bits = 0; |
| 300 |
|
| 301 |
for (unary = value >> rice_k; unary; unary--) |
| 302 |
bits++; |
| 303 |
bits += rice_k + 1; |
| 304 |
|
| 305 |
return bits; |
| 306 |
} |
| 307 |
|
| 308 |
unsigned |
| 309 |
bits_bin_code (unsigned value, unsigned maxval) |
| 310 |
/* |
| 311 |
* Compute number of bits needed for coding integer 'value' |
| 312 |
* with adjusted binary code of given maximum value 'maxval'. |
| 313 |
* |
| 314 |
* Return value: |
| 315 |
* number of bits |
| 316 |
*/ |
| 317 |
{ |
| 318 |
unsigned k; |
| 319 |
unsigned r; |
| 320 |
|
| 321 |
assert (maxval && value <= maxval); |
| 322 |
|
| 323 |
k = log2 (maxval + 1); |
| 324 |
r = (maxval + 1) % (1 << k); |
| 325 |
|
| 326 |
return value < maxval + 1 - 2 * r ? k : k + 1; |
| 327 |
} |
| 328 |
|
| 329 |
unsigned * |
| 330 |
init_clipping (void) |
| 331 |
/* |
| 332 |
* Initialize the clipping tables |
| 333 |
* |
| 334 |
* Return value: |
| 335 |
* pointer to clipping table |
| 336 |
*/ |
| 337 |
{ |
| 338 |
static unsigned *gray_clip = NULL; /* clipping array */ |
| 339 |
|
| 340 |
if (gray_clip == NULL) /* initialize clipping table */ |
| 341 |
{ |
| 342 |
int i; /* counter */ |
| 343 |
|
| 344 |
gray_clip = calloc (256 * 3, sizeof (unsigned)); |
| 345 |
if (!gray_clip) |
| 346 |
{ |
| 347 |
set_error (_("Out of memory.")); |
| 348 |
return NULL; |
| 349 |
} |
| 350 |
gray_clip += 256; |
| 351 |
|
| 352 |
for (i = -256; i < 512; i++) |
| 353 |
if (i < 0) |
| 354 |
gray_clip [i] = 0; |
| 355 |
else if (i > 255) |
| 356 |
gray_clip [i] = 255; |
| 357 |
else |
| 358 |
gray_clip [i] = i; |
| 359 |
} |
| 360 |
|
| 361 |
return gray_clip; |
| 362 |
} |
| 363 |
|
| 364 |
#ifndef HAVE_MEMMOVE |
| 365 |
void * |
| 366 |
memmove (void *v_dst, const void *v_src, size_t n) |
| 367 |
/* |
| 368 |
* Copy 'n' bytes from memory area 'src' to memory area 'dest'. |
| 369 |
* The memory areas may overlap. |
| 370 |
* |
| 371 |
* Return value: |
| 372 |
* pointer 'dest' |
| 373 |
*/ |
| 374 |
{ |
| 375 |
byte_t *to, *dst = (byte_t *) v_dst; |
| 376 |
const byte_t *from, *src = (byte_t *) v_src; |
| 377 |
|
| 378 |
assert (v_dst && v_src); |
| 379 |
|
| 380 |
if (dst <= src) |
| 381 |
{ |
| 382 |
from = src; |
| 383 |
to = dst; |
| 384 |
for (; n; n--) |
| 385 |
*to++ = *from++; |
| 386 |
} |
| 387 |
else |
| 388 |
{ |
| 389 |
from = src + (n - 1); |
| 390 |
to = dst + (n - 1); |
| 391 |
for (; n; n--) |
| 392 |
*to-- = *from--; |
| 393 |
} |
| 394 |
|
| 395 |
return v_dst; |
| 396 |
} |
| 397 |
#endif /* not HAVE_MEMMOVE */ |
| 398 |
|
| 399 |
/* Note that some systems have a "log2()" in the math library and some |
| 400 |
have a "log2" macro. So we name ours Log2. But to avoid lots of |
| 401 |
differences from the original fiasco source code, we define a |
| 402 |
macro log2 in config.h to expand to Log2. |
| 403 |
*/ |
| 404 |
double |
| 405 |
Log2 (double x) |
| 406 |
/* |
| 407 |
* Return value: |
| 408 |
* base-2 logarithm of 'x' |
| 409 |
*/ |
| 410 |
{ |
| 411 |
return log (x) / 0.69314718; |
| 412 |
} |
| 413 |
|
| 414 |
real_t |
| 415 |
variance (const word_t *pixels, unsigned x0, unsigned y0, |
| 416 |
unsigned width, unsigned height, unsigned cols) |
| 417 |
/* |
| 418 |
* Compute variance of subimage ('x0', y0', 'width', 'height') of |
| 419 |
* the image data given by 'pixels' ('cols' is the number of pixels |
| 420 |
* in one row of the image). |
| 421 |
* |
| 422 |
* Return value: |
| 423 |
* variance |
| 424 |
*/ |
| 425 |
{ |
| 426 |
real_t average; /* average of pixel values */ |
| 427 |
real_t variance; /* variance of pixel values */ |
| 428 |
unsigned x, y; /* pixel counter */ |
| 429 |
unsigned n; /* number of pixels */ |
| 430 |
|
| 431 |
assert (pixels); |
| 432 |
|
| 433 |
for (average = 0, n = 0, y = y0; y < y0 + height; y++) |
| 434 |
for (x = x0; x < MIN(x0 + width, cols); x++, n++) |
| 435 |
average += pixels [y * cols + x] / 16; |
| 436 |
|
| 437 |
average /= n; |
| 438 |
|
| 439 |
for (variance = 0, y = y0; y < y0 + height; y++) |
| 440 |
for (x = x0; x < MIN(x0 + width, cols); x++) |
| 441 |
variance += square ((pixels [y * cols + x] / 16) - average); |
| 442 |
|
| 443 |
return variance; |
| 444 |
} |
| 445 |
|
| 446 |
int |
| 447 |
sort_asc_word (const void *value1, const void *value2) |
| 448 |
/* |
| 449 |
* Sorting function for quicksort. |
| 450 |
* Smallest values come first. |
| 451 |
*/ |
| 452 |
{ |
| 453 |
if (* (word_t *) value1 < * (word_t *) value2) |
| 454 |
return -1; |
| 455 |
else if (* (word_t *) value1 > * (word_t *) value2) |
| 456 |
return +1; |
| 457 |
else |
| 458 |
return 0; |
| 459 |
} |
| 460 |
|
| 461 |
int |
| 462 |
sort_desc_word (const void *value1, const void *value2) |
| 463 |
/* |
| 464 |
* Sorting function for quicksort. |
| 465 |
* Largest values come first. |
| 466 |
*/ |
| 467 |
{ |
| 468 |
if (* (word_t *) value1 > * (word_t *) value2) |
| 469 |
return -1; |
| 470 |
else if (* (word_t *) value1 < * (word_t *) value2) |
| 471 |
return +1; |
| 472 |
else |
| 473 |
return 0; |
| 474 |
} |
| 475 |
|
| 476 |
int |
| 477 |
sort_asc_pair (const void *value1, const void *value2) |
| 478 |
/* |
| 479 |
* Sorting function for quicksort. |
| 480 |
* Smallest values come first. |
| 481 |
*/ |
| 482 |
{ |
| 483 |
word_t v1 = ((pair_t *) value1)->key; |
| 484 |
word_t v2 = ((pair_t *) value2)->key; |
| 485 |
|
| 486 |
if (v1 < v2) |
| 487 |
return -1; |
| 488 |
else if (v1 > v2) |
| 489 |
return +1; |
| 490 |
else |
| 491 |
return 0; |
| 492 |
} |
| 493 |
|
| 494 |
int |
| 495 |
sort_desc_pair (const void *value1, const void *value2) |
| 496 |
/* |
| 497 |
* Sorting function for quicksort. |
| 498 |
* Largest values come first. |
| 499 |
*/ |
| 500 |
{ |
| 501 |
word_t v1 = ((pair_t *) value1)->key; |
| 502 |
word_t v2 = ((pair_t *) value2)->key; |
| 503 |
|
| 504 |
if (v1 > v2) |
| 505 |
return -1; |
| 506 |
else if (v1 < v2) |
| 507 |
return +1; |
| 508 |
else |
| 509 |
return 0; |
| 510 |
} |