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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
|
/* read and write masks
*/
/* Copyright: 1990, N. Dessipris.
*
* Author: Nicos Dessipris
* Written on: 29/04/1991
* Modified on: 10/8/1992, J.Cupitt
* - Mask reading routines no longer fail if scale and offset are missing.
* Instead, they set default values of scale=1, offset=0.
* - Code tidied up, better error recovery.
* - Bugs fixed in im_dup_*mask. No longer coredump.
* - Bugs fixed in im_write_*mask. Now work for non-square matricies.
* - im_copy_*mask_matrix, im_copy_matrix_*mask added: copy VIPS mask
* structures into Numerical Recipies in C style matricies and vice
* versa. Both structures should have been built before copy attempted.
* See im_create_*mask, im_*mat_alloc. The matrix should be indexed by 0
* to size-1.
* 9/7/93 JC
* - some ANSIfication and tidies
* - im_free_*mask() now return zero, so they can be used as close
* callbacks.
* 7/10/94 JC
* - new IM_NEW(), IM_ARRAY() macros added
* 27/4/95 JC
* - oops! forgot to init IM_ARRAY() memory to zero
* 7/8/96 JC
* - im_scale_dmask() rewritten
* 7/5/98 JC
* - im_read_*mask() rewritten, now more robust
* - im_write_*mask() rewritten
* - new functions im_write_*mask_name()
* 28/7/99 JC
* - im_create_imaskv(), im_create_dmaskv() make masks and init from
* varargs
* - tabs allowed as column separators
* 9/2/05
* - "," allowed as column separator ... helps CSV read
* 31/5/06
* - use g_ascii_strtod() and friends
* 2006-09-08 tcv
* - add im_norm_dmask()
* 1/9/09
* - move im_print_*mask() here
* 12/11/09
* - reading a float mask with im_read_imask() produced an incorrect
* error messagge
* 21/10/10
* - gtk-doc
* - you can use commas to separate header fields
* - small cleanups
* 30/11/10
* - im_scale_dmask() normalises to 20, not 100 ... we hit the fast
* conv path more often
* 24/1/10
* - oops, missing braces in write dmask removed spaces between items
*/
/*
This file is part of VIPS.
VIPS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include <vips/vips.h>
/**
* INTMASK:
* @xsize: mask width
* @ysize: mask height
* @scale: mask scale factor
* @offset: mask offset
* @coeff: array of mask elements
* @filename: the file this mask was read from, or should be written to
*
* An integer mask.
*
* @scale lets the mask represent fractional values: for
* example, in integer convolution (see im_conv()) the result of the
* convolution is divided by @scale and then added to @offset before being
* written to the output image.
*
* @scale and @offset default to 1 and 0. Various functions, such as
* im_conv(), will fail if @scale is zero.
*
* You can read and write the matrix elements in @coeff.
*/
/**
* DOUBLEMASK:
* @xsize: mask width
* @ysize: mask height
* @scale: mask scale factor
* @offset: mask offset
* @coeff: array of mask elements
* @filename: the file this mask was read from, or should be written to
*
* A floating-point mask.
*
* As with #INTMASK, in convolution (see im_convf()) the result of the
* convolution is divided by @scale and then added to @offset before being
* written to the output image.
*
* @scale and @offset default to 1.0 and 0.0. Various functions, such as
* im_conv(), will fail if @scale is zero.
*
* You can read and write the matrix elements in @coeff.
*/
/* Size of line buffer for reading.
*/
#define MAX_LINE (32768)
/**
* im_free_imask:
* @in: mask to free
*
* Free mask structure and any attached arrays. Return zero, so we can use
* these functions as close callbacks.
*
* See also: im_free_dmask().
*
* Returns: zero.
*/
int
im_free_imask( INTMASK *in )
{
if( !in )
return( 0 );
IM_FREE( in->coeff );
IM_FREE( in->filename );
IM_FREE( in );
return( 0 );
}
/**
* im_free_dmask:
* @in: mask to free
*
* Free mask structure and any attached arrays. Return zero, so we can use
* these functions as close callbacks.
*
* See also: im_free_dmask().
*
* Returns: zero.
*/
int
im_free_dmask( DOUBLEMASK *in )
{
if( !in )
return( 0 );
IM_FREE( in->coeff );
IM_FREE( in->filename );
IM_FREE( in );
return( 0 );
}
/**
* im_create_imask:
* @filename: set mask filename to this
* @xsize: mask width
* @ysize: mask height
*
* Create an empty imask. You need to loop over @coeff to set the values.
*
* See also: im_create_imaskv().
*
* Returns: The newly-allocated mask.
*/
INTMASK *
im_create_imask( const char *filename, int xsize, int ysize )
{
INTMASK *out;
int size = xsize * ysize;
/* Check args.
*/
if( xsize <= 0 || ysize <= 0 || filename == NULL ) {
im_error( "im_create_imask", "%s", _( "bad arguments" ) );
return( NULL );
}
/* Allocate and initialise structure.
*/
if( !(out = IM_NEW( NULL, INTMASK )) )
return( NULL );
out->coeff = NULL;
out->filename = NULL;
out->scale = 1;
out->offset = 0;
out->xsize = 0;
out->ysize = 0;
if( !(out->coeff = IM_ARRAY( NULL, size, int )) ) {
im_free_imask( out );
return( NULL );
}
(void) memset( (char *) out->coeff, 0, size * sizeof( int ) );
if( !(out->filename = im_strdup( NULL, filename )) ) {
im_free_imask( out );
return( NULL );
}
out->xsize = xsize;
out->ysize = ysize;
return( out );
}
/**
* im_create_imaskv:
* @filename: set mask filename to this
* @xsize: mask width
* @ysize: mask height
* @...: values to set for the mask
*
* Create an imask and initialise it from the function parameter list.
*
* See also: im_create_imask().
*
* Returns: The newly-allocated mask.
*/
INTMASK *
im_create_imaskv( const char *filename, int xsize, int ysize, ... )
{
va_list ap;
INTMASK *out;
int i;
if( !(out = im_create_imask( filename, xsize, ysize )) )
return( NULL );
va_start( ap, ysize );
for( i = 0; i < xsize * ysize; i++ )
out->coeff[i] = va_arg( ap, int );
va_end( ap );
return( out );
}
/**
* im_create_dmask:
* @filename: set mask filename to this
* @xsize: mask width
* @ysize: mask height
*
* Create an empty dmask. You need to loop over @coeff to set the values.
*
* See also: im_create_dmaskv(), im_vips2mask().
*
* Returns: The newly-allocated mask.
*/
DOUBLEMASK *
im_create_dmask( const char *filename, int xsize, int ysize )
{
DOUBLEMASK *out;
int size = xsize * ysize;
/* Check args.
*/
if( xsize <= 0 || ysize <= 0 || filename == NULL ) {
im_error( "im_create_dmask", "%s", _( "bad arguments" ) );
return( NULL );
}
/* Allocate and initialise structure.
*/
if( !(out = IM_NEW( NULL, DOUBLEMASK )) )
return( NULL );
out->coeff = NULL;
out->filename = NULL;
out->scale = 1.0;
out->offset = 0.0;
out->xsize = 0;
out->ysize = 0;
if( !(out->coeff = IM_ARRAY( NULL, size, double )) ) {
im_free_dmask( out );
return( NULL );
}
(void) memset( (char *) out->coeff, 0, size * sizeof( double ) );
if( !(out->filename = im_strdup( NULL, filename )) ) {
im_free_dmask( out );
return( NULL );
}
out->xsize = xsize;
out->ysize = ysize;
return( out );
}
/**
* im_create_dmaskv:
* @filename: set mask filename to this
* @xsize: mask width
* @ysize: mask height
* @...: values to set for the mask
*
* Create a dmask and initialise it from the function parameter list.
*
* See also: im_create_dmask().
*
* Returns: The newly-allocated mask.
*/
DOUBLEMASK *
im_create_dmaskv( const char *filename, int xsize, int ysize, ... )
{
va_list ap;
DOUBLEMASK *out;
int i;
if( !(out = im_create_dmask( filename, xsize, ysize )) )
return( NULL );
va_start( ap, ysize );
for( i = 0; i < xsize * ysize; i++ )
out->coeff[i] = va_arg( ap, double );
va_end( ap );
return( out );
}
/* Read a line from a file!
*/
static int
get_line( FILE *fp, char *buf )
{
if( !fgets( buf, MAX_LINE, fp ) ) {
im_error( "read_mask", "%s", _( "unexpected EOF" ) );
return( -1 );
}
return( 0 );
}
/* width, height, optional scale, optional offset.
*/
static int
read_header( FILE *fp, int *xs, int *ys, double *scale, double *offset )
{
char buf[MAX_LINE];
char *p, *q;
double v[4];
int i;
/* Read the first line: should contain size and optional
* scale + offset.
*/
if( get_line( fp, buf ) )
return( -1 );
/* Read as space separated doubles. \n is in the break list because
* our line will (usually) have a trailing \n which we want to count
* as whitespace.
*/
p = buf;
for( i = 0, p = buf;
i < 4 && (q = im_break_token( p, " \";,\t\n" ));
i++, p = q )
v[i] = g_ascii_strtod( p, NULL );
if( (i != 2 && i != 4) ||
ceil( v[0] ) != v[0] ||
ceil( v[1] ) != v[1] ||
v[0] <= 0 ||
v[1] <= 0 ) {
im_error( "read_header",
"%s", _( "error reading matrix header" ) );
return( -1 );
}
if( i == 4 && v[2] == 0 ) {
im_error( "read_header",
"%s", _( "scale should be non-zero" ) );
return( -1 );
}
*xs = v[0];
*ys = v[1];
if( i == 2 ) {
*scale = 1.0;
*offset = 0.0;
}
else {
*scale = v[2];
*offset = v[3];
}
return( 0 );
}
/**
* im_read_dmask:
* @filename: read matrix from this file
*
* Reads a matrix from a file.
*
* Matrix files have a simple format that's supposed to be easy to create with
* a text editor or a spreadsheet.
*
* The first line has four numbers for width, height, scale and
* offset (scale and offset may be omitted, in which case they default to 1.0
* and 0.0). Scale must be non-zero. Width and height must be positive
* integers. The numbers are separated by any mixture of spaces, commas,
* tabs and quotation marks ("). The scale and offset fields may be
* floating-point, and must use '.'
* as a decimal separator.
*
* Subsequent lines each hold one line of matrix data, with numbers again
* separated by any mixture of spaces, commas,
* tabs and quotation marks ("). The numbers may be floating-point, and must
* use '.'
* as a decimal separator.
*
* Extra characters at the ends of lines or at the end of the file are
* ignored.
*
* See also: im_read_imask(), im_gauss_dmask().
*
* Returns: the loaded mask on success, or NULL on error.
*/
DOUBLEMASK *
im_read_dmask( const char *filename )
{
FILE *fp;
double sc, off;
int xs, ys;
DOUBLEMASK *out;
int x, y, i;
char buf[MAX_LINE];
if( !(fp = im__file_open_read( filename, NULL, TRUE )) )
return( NULL );
if( read_header( fp, &xs, &ys, &sc, &off ) ) {
fclose( fp );
return( NULL );
}
if( !(out = im_create_dmask( filename, xs, ys )) ) {
fclose( fp );
return( NULL );
}
out->scale = sc;
out->offset = off;
for( i = 0, y = 0; y < ys; y++ ) {
char *p;
if( get_line( fp, buf ) ) {
im_free_dmask( out );
fclose( fp );
return( NULL );
}
for( p = buf, x = 0; p && x < xs;
x++, i++, p = im_break_token( p, " \t,\";" ) )
out->coeff[i] = g_ascii_strtod( p, NULL );
}
fclose( fp );
return( out );
}
/**
* im_read_imask:
* @filename: read matrix from this file
*
* Reads an integer matrix from a file.
*
* This function works exactly as im_read_dmask(), but the loaded matrix is
* checked for 'int-ness'. All coefficients must be integers, and scale and
* offset must be integers.
*
* See also: im_read_dmask().
*
* Returns: the loaded mask on success, or NULL on error.
*/
INTMASK *
im_read_imask( const char *filename )
{
DOUBLEMASK *dmask;
INTMASK *imask;
int i;
if( !(dmask = im_read_dmask( filename )) )
return( NULL );
if( ceil( dmask->scale ) != dmask->scale ||
ceil( dmask->offset ) != dmask->offset ) {
im_error( "im_read_imask",
"%s", _( "scale and offset should be int" ) );
im_free_dmask( dmask );
return( NULL );
}
for( i = 0; i < dmask->xsize * dmask->ysize; i++ )
if( ceil( dmask->coeff[i] ) != dmask->coeff[i] ) {
im_error( "im_read_imask", _( "ceofficient at "
"position (%d, %d) is not int" ),
i % dmask->xsize,
i / dmask->xsize );
im_free_dmask( dmask );
return( NULL );
}
if( !(imask = im_create_imask( filename,
dmask->xsize, dmask->ysize )) ) {
im_free_dmask( dmask );
return( NULL );
}
imask->scale = dmask->scale;
imask->offset = dmask->offset;
for( i = 0; i < dmask->xsize * dmask->ysize; i++ )
imask->coeff[i] = dmask->coeff[i];
im_free_dmask( dmask );
return( imask );
}
/**
* im_scale_dmask:
* @in: mask to scale
* @filename: filename for returned mask
*
* Scale the dmask to make an imask with a maximum value of 20.
*
* See also: im_norm_dmask().
*
* Returns: the converted mask, or NULL on error.
*/
INTMASK *
im_scale_dmask( DOUBLEMASK *in, const char *filename )
{
const int size = in->xsize * in->ysize;
INTMASK *out;
double maxval, dsum;
int i;
int isum;
if( im_check_dmask( "im_scale_dmask", in ) ||
!(out = im_create_imask( filename, in->xsize, in->ysize )) )
return( NULL );
/* Find mask max.
*/
maxval = in->coeff[0];
for( i = 0; i < size; i++ )
if( in->coeff[i] > maxval )
maxval = in->coeff[i];
/* Copy and scale, setting max to 20.
*/
for( i = 0; i < size; i++ )
out->coeff[i] = IM_RINT( in->coeff[i] * 20.0 / maxval );
out->offset = in->offset;
/* Set the scale to match the adjustment to max.
*/
isum = 0;
dsum = 0.0;
for( i = 0; i < size; i++ ) {
isum += out->coeff[i];
dsum += in->coeff[i];
}
if( dsum == in->scale )
out->scale = isum;
else if( dsum == 0.0 )
out->scale = 1.0;
else
out->scale = IM_RINT( in->scale * isum / dsum );
return( out );
}
/**
* im_dmask2imask:
* @in: mask to convert
* @filename: filename for returned mask
*
* Make an imask from the dmask, rounding to nearest.
*
* See also: im_scale_dmask().
*
* Returns: the converted mask, or NULL on error.
*/
INTMASK *
im_dmask2imask( DOUBLEMASK *in, const char *filename )
{
const int size = in->xsize * in->ysize;
INTMASK *out;
int i;
if( im_check_dmask( "im_dmask2imask", in ) ||
!(out = im_create_imask( filename, in->xsize, in->ysize )) )
return( NULL );
for( i = 0; i < size; i++ )
out->coeff[i] = IM_RINT( in->coeff[i] );
out->offset = IM_RINT( in->offset );
out->scale = IM_RINT( in->scale );
return( out );
}
/**
* im_imask2dmask:
* @in: mask to convert
* @filename: filename for returned mask
*
* Make a dmask from the imask.
*
* See also: im_dmask2imask().
*
* Returns: the converted mask, or NULL on error.
*/
DOUBLEMASK *
im_imask2dmask( INTMASK *in, const char *filename )
{
const int size = in->xsize * in->ysize;
DOUBLEMASK *out;
int i;
if( im_check_imask( "im_imask2dmask", in ) ||
!(out = im_create_dmask( filename, in->xsize, in->ysize )) )
return( NULL );
for( i = 0; i < size; i++ )
out->coeff[i] = in->coeff[i];
out->offset = in->offset;
out->scale = in->scale;
return( out );
}
/**
* im_norm_dmask:
* @mask: mask to scale
*
* Normalise the dmask. Apply the scale and offset to each element to make
* a mask with scale 1, offset zero.
*
* See also: im_scale_dmask().
*
* Returns: 0 on success, or -1 on error.
*/
void
im_norm_dmask( DOUBLEMASK *mask )
{
const int n = mask->xsize * mask->ysize;
const double scale = (mask->scale == 0) ? 0 : (1.0 / mask->scale);
int i;
if( im_check_dmask( "im_norm_dmask", mask ) ||
(1.0 == scale && 0.0 == mask->offset) )
return;
for( i = 0; i < n; i++ )
mask->coeff[i] = mask->coeff[i] * scale + mask->offset;
mask->scale = 1.0;
mask->offset = 0.0;
}
/**
* im_dup_imask:
* @in: mask to duplicate
* @filename: filename to set for the new mask
*
* Duplicate an imask.
*
* See also: im_dup_dmask().
*
* Returns: the mask copy, or NULL on error.
*/
INTMASK *
im_dup_imask( INTMASK *in, const char *filename )
{
INTMASK *out;
int i;
if( im_check_imask( "im_dup_imask", in ) ||
!(out = im_create_imask( filename, in->xsize, in->ysize )) )
return( NULL );
out->offset = in->offset;
out->scale = in->scale;
for( i = 0; i < in->xsize * in->ysize; i++ )
out->coeff[i] = in->coeff[i];
return( out );
}
/**
* im_dup_dmask:
* @in: mask to duplicate
* @filename: filename to set for the new mask
*
* Duplicate a dmask.
*
* See also: im_dup_imask().
*
* Returns: the mask copy, or NULL on error.
*/
DOUBLEMASK *
im_dup_dmask( DOUBLEMASK *in, const char *filename )
{
DOUBLEMASK *out;
int i;
if( im_check_dmask( "im_dup_dmask", in ) ||
!(out = im_create_dmask( filename, in->xsize, in->ysize )) )
return( NULL );
out->offset = in->offset;
out->scale = in->scale;
for( i = 0; i < in->xsize * in->ysize; i++ )
out->coeff[i] = in->coeff[i];
return( out );
}
/* Write to file.
*/
static int
write_line( FILE *fp, const char *fmt, ... )
{
va_list ap;
va_start( ap, fmt );
if( !vfprintf( fp, fmt, ap ) ) {
im_error( "write_mask", "%s", _( "write error, disc full?" ) );
return( -1 );
}
va_end( ap );
return( 0 );
}
static int
write_double( FILE *fp, double d )
{
char buf[G_ASCII_DTOSTR_BUF_SIZE];
fprintf( fp, "%s", g_ascii_dtostr( buf, sizeof( buf ), d ) );
return( 0 );
}
/**
* im_write_imask_name:
* @in: mask to write
* @filename: filename to write to
*
* Write an imask to a file. See im_read_dmask() for a description of the mask
* file format.
*
* See also: im_write_imask().
*
* Returns: 0 on success, or -1 on error.
*/
int
im_write_imask_name( INTMASK *in, const char *filename )
{
FILE *fp;
int x, y, i;
if( im_check_imask( "im_write_imask_name", in ) ||
!(fp = im__file_open_write( filename, TRUE )) )
return( -1 );
if( write_line( fp, "%d %d", in->xsize, in->ysize ) ) {
fclose( fp );
return( -1 );
}
if( in->scale != 1 || in->offset != 0 )
write_line( fp, " %d %d", in->scale, in->offset );
write_line( fp, "\n" );
for( i = 0, y = 0; y < in->ysize; y++ ) {
for( x = 0; x < in->xsize; x++, i++ )
write_line( fp, "%d ", in->coeff[i] );
if( write_line( fp, "\n" ) ) {
fclose( fp );
return( -1 );
}
}
fclose( fp );
return( 0 );
}
/**
* im_write_imask:
* @in: mask to write
*
* Write an imask to a file.
*
* See also: im_write_imask_name().
*
* Returns: 0 on success, or -1 on error.
*/
int
im_write_imask( INTMASK *in )
{
if( !in->filename ) {
im_error( "im_write_imask", "%s", _( "filename not set" ) );
return( -1 );
}
return( im_write_imask_name( in, in->filename ) );
}
/**
* im_write_dmask_name:
* @in: mask to write
* @filename: filename to write to
*
* Write a dmask to a file. See im_read_dmask() for a description of the mask
* file format.
*
* See also: im_write_dmask().
*
* Returns: 0 on success, or -1 on error.
*/
int
im_write_dmask_name( DOUBLEMASK *in, const char *filename )
{
FILE *fp;
int x, y, i;
if( im_check_dmask( "im_write_dmask_name", in ) ||
!(fp = im__file_open_write( filename, TRUE )) )
return( -1 );
if( write_line( fp, "%d %d", in->xsize, in->ysize ) ) {
fclose( fp );
return( -1 );
}
if( in->scale != 1.0 || in->offset != 0.0 ) {
write_line( fp, " " );
write_double( fp, in->scale );
write_line( fp, " " );
write_double( fp, in->offset );
}
write_line( fp, "\n" );
for( i = 0, y = 0; y < in->ysize; y++ ) {
for( x = 0; x < in->xsize; x++, i++ ) {
write_double( fp, in->coeff[i] );
write_line( fp, " " );
}
if( write_line( fp, "\n" ) ) {
fclose( fp );
return( -1 );
}
}
fclose( fp );
return( 0 );
}
/**
* im_write_dmask:
* @in: mask to write
*
* Write a dmask to a file. See im_read_dmask() for a description of the mask
* file format.
*
* See also: im_write_dmask_name().
*
* Returns: 0 on success, or -1 on error.
*/
int
im_write_dmask( DOUBLEMASK *in )
{
if( !in->filename ) {
im_error( "im_write_dmask", "%s", _( "filename not set" ) );
return( -1 );
}
return( im_write_dmask_name( in, in->filename ) );
}
/* Copy an imask into a matrix. Only used internally by matrix package for
* invert.
*/
void
im_copy_imask_matrix( INTMASK *mask, int **matrix )
{
int x, y;
int *p = mask->coeff;
for( y = 0; y < mask->ysize; y++ )
for( x = 0; x < mask->xsize; x++ )
matrix[x][y] = *p++;
}
/* Copy a matrix into an imask.
*/
void
im_copy_matrix_imask( int **matrix, INTMASK *mask )
{
int x, y;
int *p = mask->coeff;
for( y = 0; y < mask->ysize; y++ )
for( x = 0; x < mask->xsize; x++ )
*p++ = matrix[x][y];
}
/* Copy a dmask into a matrix.
*/
void
im_copy_dmask_matrix( DOUBLEMASK *mask, double **matrix )
{
int x, y;
double *p = mask->coeff;
for( y = 0; y < mask->ysize; y++ )
for( x = 0; x < mask->xsize; x++ )
matrix[x][y] = *p++;
}
/* Copy a matrix to a dmask.
*/
void
im_copy_matrix_dmask( double **matrix, DOUBLEMASK *mask )
{
int x, y;
double *p = mask->coeff;
for( y = 0; y < mask->ysize; y++ )
for( x = 0; x < mask->xsize; x++ )
*p++ = matrix[x][y];
}
/**
* im_print_imask:
* @in: mask to print
*
* Print an imask to stdout.
*
* See also: im_print_dmask().
*/
void
im_print_imask( INTMASK *in )
{
int i, j, k;
printf( "%s: %d %d %d %d\n",
in->filename, in->xsize, in->ysize, in->scale, in->offset );
for( k = 0, j = 0; j < in->ysize; j++ ) {
for( i = 0; i < in->xsize; i++, k++ )
printf( "%d\t", in->coeff[k] );
printf( "\n" );
}
}
/**
* im_print_dmask:
* @in: mask to print
*
* Print a dmask to stdout.
*
* See also: im_print_imask().
*/
void
im_print_dmask( DOUBLEMASK *in )
{
int i, j, k;
printf( "%s: %d %d %f %f\n",
in->filename, in->xsize, in->ysize, in->scale, in->offset );
for( k = 0, j = 0; j < in->ysize; j++ ) {
for( i = 0; i < in->xsize; i++, k++ )
printf( "%f\t", in->coeff[k] );
printf( "\n" );
}
}
/**
* im_local_dmask:
* @out: image to make the mask local to
* @mask: mask to local-ize
*
* @out takes ownership of @mask: when @out is closed, @mask will be closed
* for you. If im_local_dmask() itself fails, the mask is also freed.
*
* See also: im_local_imask().
*
* Returns: the mask, or NULL on error.
*/
DOUBLEMASK *
im_local_dmask( VipsImage *out, DOUBLEMASK *mask )
{
if( im_check_dmask( "im_local_dmask", mask ) )
return( NULL );
if( im_add_close_callback( out,
(im_callback_fn) im_free_dmask, mask, NULL ) ) {
im_free_dmask( mask );
return( NULL );
}
return( mask );
}
/**
* im_local_imask:
* @out: image to make the mask local to
* @mask: mask to local-ize
*
* @out takes ownership of @mask: when @out is closed, @mask will be closed
* for you. If im_local_imask() itself fails, the mask is also freed.
*
* See also: im_local_dmask().
*
* Returns: the mask, or NULL on error.
*/
INTMASK *
im_local_imask( VipsImage *out, INTMASK *mask )
{
if( im_check_imask( "im_local_dmask", mask ) )
return( NULL );
if( im_add_close_callback( out,
(im_callback_fn) im_free_imask, mask, NULL ) ) {
im_free_imask( mask );
return( NULL );
}
return( mask );
}
|