/** * 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_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 ); }
/** * 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 ); }
static Morph * morph_new( IMAGE *in, IMAGE *out, INTMASK *mask, MorphOp op ) { const int n_mask = mask->xsize * mask->ysize; Morph *morph; int i; /* If in is not uchar, do (!=0) to make a uchar image. */ if( in->BandFmt != IM_BANDFMT_UCHAR ) { IMAGE *t; if( !(t = im_open_local( out, "morph_new", "p" )) || im_notequalconst( in, t, 0 ) ) return( NULL ); in = t; } if( im_piocheck( in, out ) || im_check_uncoded( "morph", in ) || im_check_format( "morph", in, IM_BANDFMT_UCHAR ) || im_check_imask( "morph", mask ) ) return( NULL ); for( i = 0; i < n_mask; i++ ) if( mask->coeff[i] != 0 && mask->coeff[i] != 128 && mask->coeff[i] != 255 ) { im_error( "morph", _( "bad mask element (%d " "should be 0, 128 or 255)" ), mask->coeff[i] ); return( NULL ); } if( !(morph = IM_NEW( out, Morph )) ) return( NULL ); morph->in = in; morph->out = out; morph->mask = NULL; morph->op = op; morph->n_pass = 0; for( i = 0; i < MAX_PASS; i++ ) morph->pass[i].vector = NULL; if( im_add_close_callback( out, (im_callback_fn) morph_close, morph, NULL ) || !(morph->mask = im_dup_imask( mask, "morph" )) ) return( NULL ); /* Generate code for this mask / image, if possible. */ if( vips_vector_isenabled() ) { if( pass_compile( morph ) ) pass_free( morph ); } return( morph ); }