int im_rotquad( IMAGE *in, IMAGE *out ) { IMAGE *t[6]; int xd = in->Xsize / 2; int yd = in->Ysize / 2; if( in->Xsize < 2 || in->Ysize < 2 ) return( im_copy( in, out ) ); if( im_open_local_array( out, t, 6, "im_rotquad-1", "p" ) || /* Extract 4 areas. */ im_extract_area( in, t[0], 0, 0, xd, yd ) || im_extract_area( in, t[1], xd, 0, in->Xsize - xd, yd ) || im_extract_area( in, t[2], 0, yd, xd, in->Ysize - yd ) || im_extract_area( in, t[3], xd, yd, in->Xsize - xd, in->Ysize - yd ) || /* Reassemble, rotated. */ im_insert( t[3], t[2], t[4], in->Xsize - xd, 0 ) || im_insert( t[1], t[0], t[5], in->Xsize - xd, 0 ) || im_insert( t[4], t[5], out, 0, in->Ysize - yd ) ) return( -1 ); out->Xoffset = xd; out->Yoffset = yd; return( 0 ); }
/* Call im_extract_area via arg vector. */ static int extract_area_vec( im_object *argv ) { int x = *((int *) argv[2]); int y = *((int *) argv[3]); int w = *((int *) argv[4]); int h = *((int *) argv[5]); return( im_extract_area( argv[0], argv[1], x, y, w, h ) ); }
/** * im_tbjoin: * @top: image to go on top * @bottom: image to go on bottom * @out: output image * * Join @top and @bottom together, up-down. If one is wider than the * other, @out will be has wide as the smaller. * * If the number of bands differs, one of the images * must have one band. In this case, an n-band image is formed from the * one-band image by joining n copies of the one-band image together, and then * the two n-band images are operated upon. * * The two input images are cast up to the smallest common type (see table * Smallest common format in * <link linkend="VIPS-arithmetic">arithmetic</link>). * * See also: im_insert(), im_tbjoin(). * * Returns: 0 on success, -1 on error */ int im_tbjoin( IMAGE *top, IMAGE *bottom, IMAGE *out ) { IMAGE *t1; /* Paste top and bottom together, cut off any leftovers. */ if( !(t1 = im_open_local( out, "im_tbjoin:1", "p" )) || im_insert( top, bottom, t1, 0, top->Ysize ) || im_extract_area( t1, out, 0, 0, IM_MIN( top->Xsize, bottom->Xsize ), t1->Ysize ) ) return( -1 ); out->Xoffset = 0; out->Yoffset = top->Ysize; return( 0 ); }
/** * im_draw_smudge: * @image: image to smudge * @left: area to smudge * @top: area to smudge * @width: area to smudge * @height: area to smudge * * Smudge a section of @image. Each pixel in the area @left, @top, @width, * @height is replaced by the average of the surrounding 3x3 pixels. * * This an inplace operation, so @image is changed. It does not thread and will * not work well as part of a pipeline. On 32-bit machines it will be limited * to 2GB images. * * See also: im_draw_line(). * * Returns: 0 on success, or -1 on error. */ int im_draw_smudge( VipsImage *im, int left, int top, int width, int height ) { Rect area, image, clipped; IMAGE *t[2]; area.left = left; area.top = top; area.width = width; area.height = height; image.left = 0; image.top = 0; image.width = im->Xsize; image.height = im->Ysize; im_rect_intersectrect( &area, &image, &clipped ); if( im_rect_isempty( &clipped ) ) return( 0 ); if( !blur ) { blur = im_create_imaskv( "im_draw_smudge", 3, 1, 1, 4, 1 ); blur->scale = 6; } if( !(t[0] = im_open( "im_draw_smudge", "p" )) ) return( -1 ); if( !(t[1] = im_open_local( t[0], "im_draw_smudge", "p" )) || im_convsep( im, t[0], blur ) || im_extract_area( t[0], t[1], clipped.left, clipped.top, clipped.width, clipped.height ) || im_draw_image( im, t[1], clipped.left, clipped.top ) ) { im_close( t[0] ); return( -1 ); } im_close( t[0] ); return( 0 ); }
/* Extract a rect. */ static int extract_rect( IMAGE *in, IMAGE *out, Rect *r ) { return( im_extract_area( in, out, r->left, r->top, r->width, r->height ) ); }
int main (int argc, char **argv) { const int NTMPS = 3; VipsImage *in, *out; VipsImage *tmps[NTMPS]; INTMASK *mask; int stat; const char *ifile, *ofile; int extractTop = 100, extractBtm = 200; check(argc == 3, "Syntax: %s <input> <output>", argv[0]); ifile = argv[1]; ofile = argv[2]; timer_start(ifile, "Setup"); if (im_init_world (argv[0])) error_exit ("unable to start VIPS"); in = im_open( ifile, "r" ); if (!in) vips_error_exit( "unable to read %s", ifile ); check(in->Ysize > 5 && in->Xsize > 5, "Input image must be larger than 5 in both dimensions", extractBtm); stat = im_open_local_array(in, tmps, NTMPS, "tt", "p"); check(!stat, "Unable to create temps."); mask = mk_convmat(); timer_done(); /* Reduce the extraction size if it's bigger than the image. */ if (extractBtm + extractTop >= in->Ysize || extractBtm + extractTop >= in->Xsize) { extractTop = 2; extractBtm = 2; }/* if */ timer_start(ifile, "im_extract_area"); check( !im_extract_area(in, tmps[0], extractTop, extractTop, in->Xsize - extractBtm, in->Ysize - extractBtm), "extract failed."); timer_done(); timer_start(ifile, "im_affine"); check( !im_affine(tmps[0], tmps[1], 0.9, 0, 0, 0.9, 0, 0, 0, 0, in->Xsize * 0.9, in->Ysize * 0.9), "im_affine failed."); timer_done(); timer_start(ifile, "im_conv"); check( !im_conv (tmps[1], tmps[2], mask), "im_conv failed."); timer_done(); timer_start(ofile, "writing output"); out = im_open(ofile, "w"); check(!!out, "file output failed."); im_copy(tmps[2], out); timer_done(); timer_start(ofile, "teardown"); im_close(out); im_close(in); timer_done(); print_times(); return 0; }/* main */
/** * im_correl: * @ref: reference image * @sec: secondary image * @xref: position in reference image * @yref: position in reference image * @xsec: position in secondary image * @ysec: position in secondary image * @hwindowsize: half window size * @hsearchsize: half search size * @correlation: return detected correlation * @x: return found position * @y: return found position * * This operation finds the position of @sec within @ref. * * The area around * (@xsec, @ysec) is searched for the best match to the area around (@xref, * @yref). It searches an area of size @hsearchsize for a * match of size @hwindowsize. The position of the best match is * returned, together with the correlation at that point. * * Only the first band of each image is correlated. @ref and @sec may be * very large --- the function extracts and generates just the * parts needed. Correlation is done with im_spcor(); the position of * the maximum is found with im_maxpos(). * * See also: im_match_linear(), im_match_linear_search(), im_lrmosaic(). * * Returns: 0 on success, -1 on error */ int im_correl( IMAGE *ref, IMAGE *sec, int xref, int yref, int xsec, int ysec, int hwindowsize, int hsearchsize, double *correlation, int *x, int *y ) { IMAGE *surface = im_open( "surface", "t" ); IMAGE *t1, *t2, *t3, *t4; Rect refr, secr; Rect winr, srhr; Rect wincr, srhcr; if( !surface || !(t1 = im_open_local( surface, "correlate:1", "p" )) || !(t2 = im_open_local( surface, "correlate:1", "p" )) || !(t3 = im_open_local( surface, "correlate:1", "p" )) || !(t4 = im_open_local( surface, "correlate:1", "p" )) ) return( -1 ); /* Find position of window and search area, and clip against image * size. */ refr.left = 0; refr.top = 0; refr.width = ref->Xsize; refr.height = ref->Ysize; winr.left = xref - hwindowsize; winr.top = yref - hwindowsize; winr.width = hwindowsize*2 + 1; winr.height = hwindowsize*2 + 1; im_rect_intersectrect( &refr, &winr, &wincr ); secr.left = 0; secr.top = 0; secr.width = sec->Xsize; secr.height = sec->Ysize; srhr.left = xsec - hsearchsize; srhr.top = ysec - hsearchsize; srhr.width = hsearchsize*2 + 1; srhr.height = hsearchsize*2 + 1; im_rect_intersectrect( &secr, &srhr, &srhcr ); /* Extract window and search area. */ if( im_extract_area( ref, t1, wincr.left, wincr.top, wincr.width, wincr.height ) || im_extract_area( sec, t2, srhcr.left, srhcr.top, srhcr.width, srhcr.height ) ) { im_close( surface ); return( -1 ); } /* Make sure we have just one band. From im_*mosaic() we will, but * from im_match_linear_search() etc. we may not. */ if( t1->Bands != 1 ) { if( im_extract_band( t1, t3, 0 ) ) { im_close( surface ); return( -1 ); } t1 = t3; } if( t2->Bands != 1 ) { if( im_extract_band( t2, t4, 0 ) ) { im_close( surface ); return( -1 ); } t2 = t4; } /* Search! */ if( im_spcor( t2, t1, surface ) ) { im_close( surface ); return( -1 ); } /* Find maximum of correlation surface. */ if( im_maxpos( surface, x, y, correlation ) ) { im_close( surface ); return( -1 ); } im_close( surface ); /* Translate back to position within sec. */ *x += srhcr.left; *y += srhcr.top; return( 0 ); }
/* The main part of the benchmark ... transform labq to labq. Chain several of * these together to get a CPU-bound operation. */ static int benchmark( IMAGE *in, IMAGE *out ) { IMAGE *t[18]; double one[3] = { 1.0, 1.0, 1.0 }; double zero[3] = { 0.0, 0.0, 0.0 }; double darken[3] = { 1.0 / 1.18, 1.0, 1.0 }; double whitepoint[3] = { 1.06, 1.0, 1.01 }; double shadow[3] = { -2, 0, 0 }; double white[3] = { 100, 0, 0 }; DOUBLEMASK *d652d50 = im_create_dmaskv( "d652d50", 3, 3, 1.13529, -0.0604663, -0.0606321, 0.0975399, 0.935024, -0.0256156, -0.0336428, 0.0414702, 0.994135 ); im_add_close_callback( out, (im_callback_fn) im_free_dmask, d652d50, NULL ); return( /* Set of descriptors for this operation. */ im_open_local_array( out, t, 18, "im_benchmark", "p" ) || /* Unpack to float. */ im_LabQ2Lab( in, t[0] ) || /* Crop 100 pixels off all edges. */ im_extract_area( t[0], t[1], 100, 100, t[0]->Xsize - 200, t[0]->Ysize - 200 ) || /* Shrink by 10%, bilinear interp. */ im_affinei_all( t[1], t[2], vips_interpolate_bilinear_static(), 0.9, 0, 0, 0.9, 0, 0 ) || /* Find L ~= 100 areas (white surround). */ im_extract_band( t[2], t[3], 0 ) || im_moreconst( t[3], t[4], 99 ) || /* Adjust white point and shadows. */ im_lintra_vec( 3, darken, t[2], zero, t[5] ) || im_Lab2XYZ( t[5], t[6] ) || im_recomb( t[6], t[7], d652d50 ) || im_lintra_vec( 3, whitepoint, t[7], zero, t[8] ) || im_lintra( 1.5, t[8], 0.0, t[9] ) || im_XYZ2Lab( t[9], t[10] ) || im_lintra_vec( 3, one, t[10], shadow, t[11] ) || /* Make a solid white image. */ im_black( t[12], t[4]->Xsize, t[4]->Ysize, 3 ) || im_lintra_vec( 3, zero, t[12], white, t[13] ) || /* Reattach border. */ im_ifthenelse( t[4], t[13], t[11], t[14] ) || /* Sharpen. */ im_Lab2LabQ( t[14], t[15] ) || im_sharpen( t[15], out, 11, 2.5, 40, 20, 0.5, 1.5 ) ); }
int im__find_lroverlap( IMAGE *ref_in, IMAGE *sec_in, IMAGE *out, int bandno_in, int xref, int yref, int xsec, int ysec, int halfcorrelation, int halfarea, int *dx0, int *dy0, double *scale1, double *angle1, double *dx1, double *dy1 ) { Rect left, right, overlap; IMAGE *ref, *sec; IMAGE *t[6]; TIE_POINTS points, *p_points; TIE_POINTS newpoints, *p_newpoints; int dx, dy; int i; /* Test cor and area. */ if( halfcorrelation < 0 || halfarea < 0 || halfarea < halfcorrelation ) { im_error( "im_lrmosaic", "%s", _( "bad area parameters" ) ); return( -1 ); } /* Set positions of left and right. */ left.left = 0; left.top = 0; left.width = ref_in->Xsize; left.height = ref_in->Ysize; right.left = xref - xsec; right.top = yref - ysec; right.width = sec_in->Xsize; right.height = sec_in->Ysize; /* Find overlap. */ im_rect_intersectrect( &left, &right, &overlap ); if( overlap.width < 2 * halfarea + 1 || overlap.height < 2 * halfarea + 1 ) { im_error( "im_lrmosaic", "%s", _( "overlap too small for search" ) ); return( -1 ); } /* Extract overlaps as 8-bit, 1 band. */ if( !(ref = im_open_local( out, "temp_one", "t" )) || !(sec = im_open_local( out, "temp_two", "t" )) || im_open_local_array( out, t, 6, "im_lrmosaic", "p" ) || im_extract_area( ref_in, t[0], overlap.left, overlap.top, overlap.width, overlap.height ) || im_extract_area( sec_in, t[1], overlap.left - right.left, overlap.top - right.top, overlap.width, overlap.height ) ) return( -1 ); if( ref_in->Coding == IM_CODING_LABQ ) { if( im_LabQ2Lab( t[0], t[2] ) || im_LabQ2Lab( t[1], t[3] ) || im_Lab2disp( t[2], t[4], im_col_displays( 1 ) ) || im_Lab2disp( t[3], t[5], im_col_displays( 1 ) ) || im_extract_band( t[4], ref, 1 ) || im_extract_band( t[5], sec, 1 ) ) return( -1 ); } else if( ref_in->Coding == IM_CODING_NONE ) { if( im_extract_band( t[0], t[2], bandno_in ) || im_extract_band( t[1], t[3], bandno_in ) || im_scale( t[2], ref ) || im_scale( t[3], sec ) ) return( -1 ); } else { im_error( "im_lrmosaic", "%s", _( "unknown Coding type" ) ); return( -1 ); } /* Initialise and fill TIE_POINTS */ p_points = &points; p_newpoints = &newpoints; p_points->reference = ref_in->filename; p_points->secondary = sec_in->filename; p_points->nopoints = IM_MAXPOINTS; p_points->deltax = 0; p_points->deltay = 0; p_points->halfcorsize = halfcorrelation; p_points->halfareasize = halfarea; /* Initialise the structure */ for( i = 0; i < IM_MAXPOINTS; i++ ) { p_points->x_reference[i] = 0; p_points->y_reference[i] = 0; p_points->x_secondary[i] = 0; p_points->y_secondary[i] = 0; p_points->contrast[i] = 0; p_points->correlation[i] = 0.0; p_points->dx[i] = 0.0; p_points->dy[i] = 0.0; p_points->deviation[i] = 0.0; } /* Search ref for possible tie-points. Sets: p_points->contrast, * p_points->x,y_reference. */ if( im__lrcalcon( ref, p_points ) ) return( -1 ); /* For each candidate point, correlate against corresponding part of * sec. Sets x,y_secondary and fills correlation and dx, dy. */ if( im__chkpair( ref, sec, p_points ) ) return( -1 ); /* First call to im_clinear(). */ if( im__initialize( p_points ) ) return( -1 ); /* Improve the selection of tiepoints until all abs(deviations) are * < 1.0 by deleting all wrong points. */ if( im__improve( p_points, p_newpoints ) ) return( -1 ); /* Average remaining offsets. */ if( im__avgdxdy( p_newpoints, &dx, &dy ) ) return( -1 ); /* Offset with overlap position. */ *dx0 = -right.left + dx; *dy0 = -right.top + dy; /* Write 1st order parameters too. */ *scale1 = newpoints.l_scale; *angle1 = newpoints.l_angle; *dx1 = newpoints.l_deltax; *dy1 = newpoints.l_deltay; return( 0 ); }
int im__find_tboverlap( IMAGE *ref_in, IMAGE *sec_in, IMAGE *out, int bandno_in, int xref, int yref, int xsec, int ysec, int halfcorrelation, int halfarea, int *dx0, int *dy0, double *scale1, double *angle1, double *dx1, double *dy1 ) { IMAGE *ref, *sec; TIE_POINTS points, *p_points; /* defined in mosaic.h */ TIE_POINTS newpoints, *p_newpoints; int i; int dx, dy; Rect top, bottom, overlap; /* Check ref and sec are compatible. */ if( ref_in->Bands != sec_in->Bands || ref_in->BandFmt != sec_in->BandFmt || ref_in->Coding != sec_in->Coding ) { im_errormsg( "im_tbmosaic: input images incompatible" ); return( -1 ); } /* Test cor and area. */ if( halfcorrelation < 0 || halfarea < 0 || halfarea < halfcorrelation ) { im_errormsg( "im_tbmosaic: bad area parameters" ); return( -1 ); } /* Set positions of top and bottom. */ top.left = 0; top.top = 0; top.width = ref_in->Xsize; top.height = ref_in->Ysize; bottom.left = xref - xsec; bottom.top = yref - ysec; bottom.width = sec_in->Xsize; bottom.height = sec_in->Ysize; /* Find overlap. */ im_rect_intersectrect( &top, &bottom, &overlap ); if( overlap.width < 2*halfarea + 1 || overlap.height < 2*halfarea + 1 ) { im_errormsg( "im_tbmosaic: overlap too small for search" ); return( -1 ); } /* Extract overlaps. */ ref = im_open_local( out, "temp_one", "t" ); sec = im_open_local( out, "temp_two", "t" ); if( !ref || !sec ) return( -1 ); if( ref_in->Coding == IM_CODING_LABQ ) { IMAGE *t1 = im_open_local( out, "temp:3", "p" ); IMAGE *t2 = im_open_local( out, "temp:4", "p" ); IMAGE *t3 = im_open_local( out, "temp:5", "p" ); IMAGE *t4 = im_open_local( out, "temp:6", "p" ); IMAGE *t5 = im_open_local( out, "temp:7", "p" ); IMAGE *t6 = im_open_local( out, "temp:8", "p" ); if( !t1 || !t2 || !t3 || !t4 || !t5 || !t6 ) return( -1 ); if( im_extract_area( ref_in, t1, overlap.left, overlap.top, overlap.width, overlap.height ) ) return( -1 ); if( im_extract_area( sec_in, t2, overlap.left - bottom.left, overlap.top - bottom.top, overlap.width, overlap.height ) ) return( -1 ); if( im_LabQ2Lab( t1, t3 ) || im_LabQ2Lab( t2, t4 ) || im_Lab2disp( t3, t5, im_col_displays( 1 ) ) || im_Lab2disp( t4, t6, im_col_displays( 1 ) ) ) return( -1 ); /* Extract the green. */ if( im_extract_band( t5, ref, 1 ) || im_extract_band( t6, sec, 1 ) ) return( -1 ); } else if( ref_in->Coding == IM_CODING_NONE ) { IMAGE *t1 = im_open_local( out, "temp:9", "p" ); IMAGE *t2 = im_open_local( out, "temp:10", "p" ); IMAGE *t3 = im_open_local( out, "temp:11", "p" ); IMAGE *t4 = im_open_local( out, "temp:12", "p" ); if( !t1 || !t2 || !t3 || !t4 ) return( -1 ); if( im_extract_area( ref_in, t1, overlap.left, overlap.top, overlap.width, overlap.height ) ) return( -1 ); if( im_extract_area( sec_in, t2, overlap.left - bottom.left, overlap.top - bottom.top, overlap.width, overlap.height ) ) return( -1 ); if( im_extract_band( t1, t3, bandno_in ) || im_extract_band( t2, t4, bandno_in ) ) return( -1 ); if( im_scale( t3, ref ) || im_scale( t4, sec ) ) return( -1 ); } else { im_errormsg( "im_tbmosaic: unknown Coding type" ); return( -1 ); } /* Initialise and fill TIE_POINTS */ p_points = &points; p_newpoints = &newpoints; p_points->reference = ref_in->filename; p_points->secondary = sec_in->filename; p_points->nopoints = IM_MAXPOINTS; p_points->deltax = 0; p_points->deltay = 0; p_points->halfcorsize = halfcorrelation; p_points->halfareasize = halfarea; /* Initialise the structure */ for( i = 0; i < IM_MAXPOINTS; i++ ) { p_points->x_reference[i] = 0; p_points->y_reference[i] = 0; p_points->x_secondary[i] = 0; p_points->y_secondary[i] = 0; p_points->contrast[i] = 0; p_points->correlation[i] = 0.0; p_points->dx[i] = 0.0; p_points->dy[i] = 0.0; p_points->deviation[i] = 0.0; } /* Search ref for possible tie-points. Sets: p_points->contrast, * p_points->x,y_reference. */ if( im__tbcalcon( ref, p_points ) ) return( -1 ); /* For each candidate point, correlate against corresponding part of * sec. Sets x,y_secondary and fills correlation and dx, dy. */ if( im__chkpair( ref, sec, p_points ) ) return( -1 ); /* First call to im_clinear(). */ if( im__initialize( p_points ) ) return( -1 ); /* Improve the selection of tiepoints until all abs(deviations) are * < 1.0 by deleting all wrong points. */ if( im__improve( p_points, p_newpoints ) ) return( -1 ); /* Average remaining offsets. */ if( im__avgdxdy( p_newpoints, &dx, &dy ) ) return( -1 ); /* Offset with overlap position. */ *dx0 = -bottom.left + dx; *dy0 = -bottom.top + dy; /* Write 1st order parameters too. */ *scale1 = newpoints.l_scale; *angle1 = newpoints.l_angle; *dx1 = newpoints.l_deltax; *dy1 = newpoints.l_deltay; return( 0 ); }