int im_subtract( IMAGE *in1, IMAGE *in2, IMAGE *out ) { /* Basic checks. */ if( im_piocheck( in1, out ) || im_pincheck( in2 ) ) return( -1 ); if( in1->Bands != in2->Bands && (in1->Bands != 1 && in2->Bands != 1) ) { im_error( "im_subtract", _( "not same number of bands" ) ); return( -1 ); } if( in1->Coding != IM_CODING_NONE || in2->Coding != IM_CODING_NONE ) { im_error( "im_subtract", _( "not uncoded" ) ); return( -1 ); } if( im_cp_descv( out, in1, in2, NULL ) ) return( -1 ); /* What number of bands will we write? */ out->Bands = IM_MAX( in1->Bands, in2->Bands ); /* What output type will we write? int, float or complex. */ if( im_iscomplex( in1 ) || im_iscomplex( in2 ) ) { /* What kind of complex? */ if( in1->BandFmt == IM_BANDFMT_DPCOMPLEX || in2->BandFmt == IM_BANDFMT_DPCOMPLEX ) /* Output will be DPCOMPLEX. */ out->BandFmt = IM_BANDFMT_DPCOMPLEX; else out->BandFmt = IM_BANDFMT_COMPLEX; } else if( im_isfloat( in1 ) || im_isfloat( in2 ) ) { /* What kind of float? */ if( in1->BandFmt == IM_BANDFMT_DOUBLE || in2->BandFmt == IM_BANDFMT_DOUBLE ) out->BandFmt = IM_BANDFMT_DOUBLE; else out->BandFmt = IM_BANDFMT_FLOAT; } else /* Must be int+int -> int. */ out->BandFmt = iformat[in1->BandFmt][in2->BandFmt]; /* And process! */ if( im__cast_and_call( in1, in2, out, (im_wrapmany_fn) subtract_buffer, NULL ) ) return( -1 ); /* Success! */ return( 0 ); }
int im_c2real( IMAGE *in, IMAGE *out ) { if( in->Coding != IM_CODING_NONE || !im_iscomplex( in ) ) { im_errormsg( "im_c2real: input should be uncoded complex" ); return( -1 ); } if( im_cp_desc( out, in ) ) return( -1 ); /* Output will be float or double. */ if( in->BandFmt == IM_BANDFMT_DPCOMPLEX ) { out->BandFmt = IM_BANDFMT_DOUBLE; out->Bbits = IM_BBITS_DOUBLE; } else { out->BandFmt = IM_BANDFMT_FLOAT; out->Bbits = IM_BBITS_FLOAT; } /* Do the processing. */ if( im_wrapone( in, out, (im_wrapone_fn) buffer_c2real, in, NULL ) ) return( -1 ); return( 0 ); }
static int fwfft1( IMAGE *dummy, IMAGE *in, IMAGE *out ) { if( im_iscomplex( in ) ) return( cfwfft1( dummy, in, out ) ); else return( rfwfft1( dummy, in, out ) ); }
int im_freqflt( IMAGE *in, IMAGE *mask, IMAGE *out ) { IMAGE *dummy; /* Placeholder for memory free. */ if( !(dummy = im_open( "memory-1", "p" )) ) return( -1 ); if( im_iscomplex( in ) ) { /* Easy case! Assume it has already been transformed. */ IMAGE *t1 = im_open_local( dummy, "im_freqflt-1", "p" ); if( !t1 || im_multiply( in, mask, t1 ) || im_invfftr( t1, out ) ) { im_close( dummy ); return( -1 ); } } else { /* Harder - fft first, then mult, then force back to start * type. * * Optimisation: output of im_invfft() is float buffer, we * will usually chagetype to char, so rather than keeping a * large float buffer and partial to char from that, do * changetype to a memory buffer, and copy to out from that. */ IMAGE *t[3]; IMAGE *t3; if( im_open_local_array( dummy, t, 3, "im_freqflt-1", "p" ) || !(t3 = im_open_local( out, "im_freqflt-3", "t" )) || im_fwfft( in, t[0] ) || im_multiply( t[0], mask, t[1] ) || im_invfftr( t[1], t[2] ) || im_clip2fmt( t[2], t3, in->BandFmt ) || im_copy( t3, out ) ) { im_close( dummy ); return( -1 ); } } im_close( dummy ); return( 0 ); }
/* Find the average of an image. */ int im_deviate( IMAGE *in, double *out ) { double sum[2] = { 0.0, 0.0 }; gint64 N; /* Check our args. */ if( im_pincheck( in ) ) return( -1 ); if( in->Coding != IM_CODING_NONE ) { im_error( "im_deviate", _( "not uncoded" ) ); return( -1 ); } if( im_iscomplex( in ) ) { im_error( "im_deviate", _( "bad input type" ) ); return( -1 ); } /* Loop over input, summing pixels. */ if( im_iterate( in, start_fn, scan_fn, stop_fn, &sum, NULL ) ) return( -1 ); /* NOTE: NR suggests a two-pass algorithm to minimise roundoff. But that's too expensive for us :-( so do it the old one-pass way. */ /* Calculate and return deviation. Add a fabs to stop sqrt(<=0). */ N = (gint64) in->Xsize * in->Ysize * in->Bands; *out = sqrt( fabs( sum[1] - (sum[0] * sum[0] / N) ) / (N - 1) ); return( 0 ); }
/* Transform a 1 band image with vips's built-in fft routine. */ static int fwfft1( IMAGE *dummy, IMAGE *in, IMAGE *out ) { int size = in->Xsize * in->Ysize; int bpx = im_ispoweroftwo( in->Xsize ); int bpy = im_ispoweroftwo( in->Ysize ); float *buf, *q, *p1, *p2; int x, y; /* Buffers for real and imaginary parts. */ IMAGE *real = im_open_local( dummy, "fwfft1:1", "t" ); IMAGE *imag = im_open_local( dummy, "fwfft1:2", "t" ); /* Temporaries. */ IMAGE *t1 = im_open_local( dummy, "fwfft1:3", "p" ); if( !real || !imag || !t1 ) return( -1 ); if( im_pincheck( in ) || im_outcheck( out ) ) return( -1 ); if( in->Coding != IM_CODING_NONE || in->Bands != 1 || im_iscomplex( in ) ) { im_error( "im_fwfft", _( "one band non-complex uncoded only" ) ); return( -1 ); } if( !bpx || !bpy ) { im_error( "im_fwfft", _( "sides must be power of 2" ) ); return( -1 ); } /* Make sure we have a float input image. */ if( im_clip2f( in, real ) ) return( -1 ); /* Make a buffer of 0 floats of the same size for the imaginary part. */ if( im_black( t1, in->Xsize, in->Ysize, 1 ) ) return( -1 ); if( im_clip2f( t1, imag ) ) return( -1 ); /* Transform! */ if( im__fft_sp( (float *) real->data, (float *) imag->data, bpx - 1, bpy - 1 ) ) { im_error( "im_fwfft", _( "fft_sp failed" ) ); return( -1 ); } /* WIO to out. */ if( im_cp_desc( out, in ) ) return( -1 ); out->Bbits = IM_BBITS_COMPLEX; out->BandFmt = IM_BANDFMT_COMPLEX; if( im_setupout( out ) ) return( -1 ); if( !(buf = (float *) IM_ARRAY( dummy, IM_IMAGE_SIZEOF_LINE( out ), PEL )) ) return( -1 ); /* Gather together real and imag parts. We have to normalise output! */ for( p1 = (float *) real->data, p2 = (float *) imag->data, y = 0; y < out->Ysize; y++ ) { q = buf; for( x = 0; x < out->Xsize; x++ ) { q[0] = *p1++ / size; q[1] = *p2++ / size; q += 2; } if( im_writeline( y, out, (PEL *) buf ) ) return( -1 ); } return( 0 ); }
gboolean im_isscalar( IMAGE *im ) { return( !im_iscomplex( im ) ); }
int im_resize_linear( IMAGE *in, IMAGE *out, int X, int Y ) { double dx, dy, xscale, yscale; double Xnew, Ynew; /* inv. coord. of the interpolated pt */ int x, y; int Xint, Yint; int bb; PEL *input, *opline; PEL *q, *p; int ils, ips, ies; /* Input and output line, pel and */ int ols, ops, oes; /* element sizes */ if( im_iocheck( in, out ) ) return( -1 ); if( im_iscomplex( in ) ) { im_errormsg( "im_lowpass: non-complex input only" ); return( -1 ); } if( in->Coding != IM_CODING_NONE ) { im_errormsg("im_lowpass: input should be uncoded"); return( -1 ); } if( im_cp_desc( out, in ) ) return( -1 ); out->Xsize = X; out->Ysize = Y; if( im_setupout( out ) ) return( -1 ); ils = IM_IMAGE_SIZEOF_LINE( in ); ips = IM_IMAGE_SIZEOF_PEL( in ); ies = IM_IMAGE_SIZEOF_ELEMENT( in ); ols = IM_IMAGE_SIZEOF_LINE( out ); ops = IM_IMAGE_SIZEOF_PEL( out ); oes = IM_IMAGE_SIZEOF_ELEMENT( out ); /* buffer lines ***************/ if( !(opline = IM_ARRAY( out, ols, PEL )) ) return( -1 ); /* Resampling *************/ input = (PEL*) in->data; xscale = ((double)in->Xsize-1)/(X-1); yscale = ((double)in->Ysize-1)/(Y-1); for (y=0; y<Y; y++) { q = opline; for (x=0; x<X; x++) { Xnew = x*xscale; Ynew = y*yscale; Xint = floor(Xnew); Yint = floor(Ynew); dx = Xnew - Xint; dy = Ynew - Yint; p = input + Xint*ips + Yint*ils; switch( in->BandFmt ) { case IM_BANDFMT_UCHAR: LOOP( unsigned char); break; case IM_BANDFMT_USHORT: LOOP( unsigned short ); break; case IM_BANDFMT_UINT: LOOP( unsigned int ); break; case IM_BANDFMT_CHAR: LOOP( signed char ); break; case IM_BANDFMT_SHORT: LOOP( signed short ); break; case IM_BANDFMT_INT: LOOP( signed int ); break; case IM_BANDFMT_FLOAT: LOOP( float ); break; case IM_BANDFMT_DOUBLE: LOOP( double ); break; default: im_errormsg( "im_lowpass: unsupported image type" ); return( -1 ); /*NOTREACHED*/ } } if (im_writeline(y, out, opline) ) return(-1); } return(0); }
/* Fall back to vips's built-in fft. */ static int invfft1( IMAGE *dummy, IMAGE *in, IMAGE *out ) { int bpx = im_ispoweroftwo( in->Xsize ); int bpy = im_ispoweroftwo( in->Ysize ); float *buf, *q, *p1; int x, y; /* Buffers for real and imaginary parts. */ IMAGE *real = im_open_local( dummy, "invfft1:1", "t" ); IMAGE *imag = im_open_local( dummy, "invfft1:2", "t" ); /* Temps. */ IMAGE *t1 = im_open_local( dummy, "invfft1:3", "p" ); IMAGE *t2 = im_open_local( dummy, "invfft1:4", "p" ); if( !real || !imag || !t1 ) return( -1 ); if( im_pincheck( in ) || im_outcheck( out ) ) return( -1 ); if( in->Coding != IM_CODING_NONE || in->Bands != 1 || !im_iscomplex( in ) ) { im_error( "im_invfft", "%s", _( "one band complex uncoded only" ) ); return( -1 ); } if( !bpx || !bpy ) { im_error( "im_invfft", "%s", _( "sides must be power of 2" ) ); return( -1 ); } /* Make sure we have a single-precision complex input image. */ if( im_clip2fmt( in, t1, IM_BANDFMT_COMPLEX ) ) return( -1 ); /* Extract real and imag parts. We have to complement the imaginary. */ if( im_c2real( t1, real ) ) return( -1 ); if( im_c2imag( t1, t2 ) || im_lintra( -1.0, t2, 0.0, imag ) ) return( -1 ); /* Transform! */ if( im__fft_sp( (float *) real->data, (float *) imag->data, bpx - 1, bpy - 1 ) ) { im_error( "im_invfft", "%s", _( "fft_sp failed" ) ); return( -1 ); } /* WIO to out. */ if( im_cp_desc( out, in ) ) return( -1 ); out->BandFmt = IM_BANDFMT_FLOAT; if( im_setupout( out ) ) return( -1 ); if( !(buf = (float *) IM_ARRAY( dummy, IM_IMAGE_SIZEOF_LINE( out ), PEL )) ) return( -1 ); /* Just write real part. */ for( p1 = (float *) real->data, y = 0; y < out->Ysize; y++ ) { q = buf; for( x = 0; x < out->Xsize; x++ ) { q[x] = *p1++; } if( im_writeline( y, out, (PEL *) buf ) ) return( -1 ); } return( 0 ); }