/* ** read slice onset times from ASCII file, if not in header */ float * ReadSlicetimes(VString filename) { FILE *fp; int i, j, id, n = 256; float onset; float *onset_array = NULL; char buf[LEN]; onset_array = (float *) VCalloc(n, sizeof(float)); fp = fopen(filename, "r"); if(!fp) VError(" error opening file %s", filename); /* fprintf(stderr," reading file: %s\n",filename); */ i = 0; while(!feof(fp)) { if(i >= n) VError(" too many lines in file %s, max is %d", filename, n); for(j = 0; j < LEN; j++) buf[j] = '\0'; fgets(buf, LEN, fp); if(buf[0] == '%' || buf[0] == '#') continue; if(strlen(buf) < 2) continue; if(sscanf(buf, "%f", &onset) != 1) VError(" line %d: illegal input format", i + 1); onset_array[i] = onset; i++; } fclose(fp); return onset_array; }
/*! \fn VImage VDTDilate(VImage src,VImage dest,VDouble radius) \brief 3D morphological dilation \param src input image (bit repn) \param dest output image (bit repn) \param radius radius of the spherical structural element */ VImage VDTDilate(VImage src,VImage dest,VDouble radius) { VImage float_image; VBit *bin_pp; VFloat *float_pp; int i,nbands,nrows,ncols,npixels; if (VPixelRepn(src) != VBitRepn) VError("Input image must be of type VBit"); nbands = VImageNBands(src); nrows = VImageNRows(src); ncols = VImageNColumns(src); npixels = nbands * nrows * ncols; float_image = VChamferDist3d(src,NULL,VFloatRepn); if (! float_image) VError("VDTDilate failed.\n"); dest = VSelectDestImage("VDTDilate",dest,nbands,nrows,ncols,VBitRepn); if (! dest) return NULL; float_pp = (VFloat *) VPixelPtr(float_image,0,0,0); bin_pp = (VBit *) VPixelPtr(dest,0,0,0); for (i=0; i<npixels; i++) *bin_pp++ = ((*float_pp++ > radius) ? 0 : 1); VDestroyImage(float_image); VCopyImageAttrs (src,dest); return dest; }
int main(int argc, char **argv) { static VString filename = ""; static VLong neighb = 2; static VLong iter = 1; static VOptionDescRec options[] = { { "n", VLongRepn, 1, &neighb, VOptionalOpt, TYPDict, "type of neighbourhood used for smoothing" }, { "iter", VLongRepn, 1, &iter, VOptionalOpt, NULL, "number of iterations" }, { "image", VStringRepn, 1, &filename, VOptionalOpt, NULL, "grey value image" }, }; FILE *in_file, *out_file, *fp; VAttrList list, list1; VImage src = NULL, dest, image = NULL; VAttrListPosn posn; /* Parse command line arguments: */ VParseFilterCmd(VNumber(options), options, argc, argv, &in_file, &out_file); /* Read transformation image: */ fp = VOpenInputFile(filename, TRUE); list1 = VReadFile(fp, NULL); if(! list1) VError("Error reading image"); fclose(fp); for(VFirstAttr(list1, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & image); if(VPixelRepn(image) != VUByteRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & image); break; } if(image == NULL) VError(" image not found"); /* Read source image(s): */ if(!(list = VReadFile(in_file, NULL))) exit(1); for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & src); dest = VTopSmoothImage3d(src, image, NULL, neighb, iter); if(dest == NULL) exit(1); VSetAttrValue(& posn, NULL, VImageRepn, dest); VDestroyImage(src); } /* Write the results to the output file: */ if(VWriteFile(out_file, list)) fprintf(stderr, "%s: done.\n", argv[0]); return 0; }
/* ** read a 2nd level design file */ VImage VRead2ndLevel(FILE *fp) { VImage dest = NULL; int i, j, nrows, ncols; float val; char buf[LEN], token[32]; nrows = ncols = 0; while(!feof(fp)) { memset(buf, 0, LEN); if(!fgets(buf, LEN, fp)) break; if(strlen(buf) < 1) continue; if(buf[0] == '%') continue; j = 0; while(VStringToken(buf, token, j, 30)) { if(!sscanf(token, "%f", &val)) VError("illegal input string: %s", token); j++; } if(ncols == 0) ncols = j; if(j < 1) continue; else if(ncols != j) VError(" inconsistent number of columns in row %d", nrows + 1); nrows++; } rewind(fp); dest = VCreateImage(1, nrows, ncols, VFloatRepn); VFillImage(dest, VAllBands, 0); VSetAttr(VImageAttrList(dest), "modality", NULL, VStringRepn, "X"); VSetAttr(VImageAttrList(dest), "name", NULL, VStringRepn, "X"); VSetAttr(VImageAttrList(dest), "ntimesteps", NULL, VLongRepn, (VLong)VImageNRows(dest)); VSetAttr(VImageAttrList(dest), "nsessions", NULL, VShortRepn, (VShort)1); VSetAttr(VImageAttrList(dest), "designtype", NULL, VShortRepn, (VShort)2); i = 0; while(!feof(fp)) { memset(buf, 0, LEN); if(!fgets(buf, LEN, fp)) break; if(strlen(buf) < 1) continue; if(buf[0] == '%') continue; j = 0; while(VStringToken(buf, token, j, 30)) { sscanf(token, "%f", &val); VPixel(dest, 0, i, j, VFloat) = val; j++; } if(j < 1) continue; i++; } return dest; }
int main(int argc, char *argv[]) { static VString filename = ""; static VBoolean fisher = FALSE; static VShort minval = 0; static VOptionDescRec options[] = { {"mask", VStringRepn, 1, (VPointer) &filename, VRequiredOpt, NULL, "mask"}, {"fisher", VBooleanRepn, 1, (VPointer) &fisher, VOptionalOpt, NULL, "Whether to do fisher transform"}, {"minval", VShortRepn, 1, (VPointer) &minval, VOptionalOpt, NULL, "signal threshold"} }; FILE *in_file, *out_file, *fp; VAttrList list = NULL, list1 = NULL, list2 = NULL; VAttrListPosn posn; VImage dest = NULL, mask = NULL; char prg_name[100]; char ver[100]; getLipsiaVersion(ver, sizeof(ver)); sprintf(prg_name, "vsimmat V%s", ver); fprintf(stderr, "%s\n", prg_name); VParseFilterCmd(VNumber(options), options, argc, argv, &in_file, &out_file); /* ** read mask */ fp = VOpenInputFile(filename, TRUE); list1 = VReadFile(fp, NULL); if(! list1) VError("Error reading mask file"); fclose(fp); for(VFirstAttr(list1, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & mask); if(VPixelRepn(mask) != VBitRepn) { mask = NULL; continue; } } if(mask == NULL) VError(" no mask found"); /* ** read functional data */ if(!(list = VReadFile(in_file, NULL))) exit(1); fclose(in_file); /* ** process */ dest = SimilarityMatrix(list, mask, minval, fisher); list2 = VCreateAttrList(); VAppendAttr(list2, "matrix", NULL, VImageRepn, dest); VHistory(VNumber(options), options, prg_name, &list, &list2); if(! VWriteFile(out_file, list2)) exit(1); fprintf(stderr, "%s: done.\n", argv[0]); return 0; }
VImage VSelSlices (VImage src,VImage dest,VShort first,VShort last) { int nbands,nrows,ncols,nbands_neu,npixels; int i,b,bn; VRepnKind repn; nbands = VImageNBands(src); nrows = VImageNRows(src); ncols = VImageNColumns(src); repn = VPixelRepn(src); if (first < 0) VError("VSelSlices: parameter <first> must be positive"); if (last < 0) last = nbands-1; if (last >= nbands) last = nbands-1; if (first > last) VError("VSelSlices: <first> must be less than <last>."); nbands_neu = last - first + 1; if (dest == NULL) dest = VCreateImage(nbands_neu,nrows,ncols,repn); npixels = nrows * ncols; #define SelSlices(type) \ { \ type *src_pp, *dest_pp; \ bn = 0; \ for (b=first; b<=last; b++) { \ src_pp = VPixelPtr(src,b,0,0); \ dest_pp = VPixelPtr(dest,bn,0,0); \ for (i=0; i<npixels; i++) *dest_pp++ = *src_pp++; \ bn++; \ } \ } /* Instantiate the macro once for each source pixel type: */ switch (repn) { case VBitRepn: SelSlices (VBit); break; case VUByteRepn: SelSlices (VUByte); break; case VSByteRepn: SelSlices (VSByte); break; case VShortRepn: SelSlices (VShort); break; case VLongRepn: SelSlices (VLong); break; case VFloatRepn: SelSlices (VFloat); break; case VDoubleRepn: SelSlices (VDouble); break; default: ; } /* Let the destination inherit any attributes of the source image: */ VCopyImageAttrs (src, dest); return dest; }
void ReadField (VString Name, VImage& X, VImage& Y, VImage& Z, VAttrList& history_list) { FILE* file; /* input file */ VAttrList list; /* attribute list */ VAttrListPosn pos; /* position in list */ /* initialize results */ X = Y = Z = NULL; /* open file */ file = fopen (Name, "r"); if (!file) { VError ("Failed to open input file '%s'", Name); return; } /* read file */ list = VReadFile (file, NULL); if (!list) { VError ("Failed to read input file '%s'", Name); fclose (file); return; } /* Read History */ history_list = VCopyAttrList(VReadHistory(&list)); /* extract field images */ if (VLookupAttr (list, "X", &pos)) { VGetAttrValue (&pos, NULL, VImageRepn, &X); VSetAttrValue (&pos, NULL, VImageRepn, NULL); } else VError ("Input file '%s' does not contain an image 'X'", Name); if (VLookupAttr (list, "Y", &pos)) { VGetAttrValue (&pos, NULL, VImageRepn, &Y); VSetAttrValue (&pos, NULL, VImageRepn, NULL); } else VError ("Input file '%s' does not contain an image 'Y'", Name); if (VLookupAttr (list, "Z", &pos)) { VGetAttrValue (&pos, NULL, VImageRepn, &Z); VSetAttrValue (&pos, NULL, VImageRepn, NULL); } else VError ("Input file '%s' does not contain an image 'Z'", Name); /* clean-up*/ VDestroyAttrList (list); fclose (file); } /* ReadField */
void VImage::call_option_string( const char *operation_name, const char *option_string, VOption *options ) throw( VError ) { VipsOperation *operation; VIPS_DEBUG_MSG( "vips_call_by_name: starting for %s ...\n", operation_name ); if( !(operation = vips_operation_new( operation_name )) ) { if( options ) delete options; throw( VError() ); } /* Set str options before vargs options, so the user can't * override things we set deliberately. */ if( option_string && vips_object_set_from_string( VIPS_OBJECT( operation ), option_string ) ) { vips_object_unref_outputs( VIPS_OBJECT( operation ) ); g_object_unref( operation ); delete options; throw( VError() ); } if( options ) options->set_operation( operation ); /* Build from cache. */ if( vips_cache_operation_buildp( &operation ) ) { vips_object_unref_outputs( VIPS_OBJECT( operation ) ); delete options; throw( VError() ); } /* Walk args again, writing output. */ if( options ) options->get_operation( operation ); /* We're done with options! */ delete options; /* The operation we have built should now have been reffed by * one of its arguments or have finished its work. Either * way, we can unref. */ g_object_unref( operation ); }
/* ** read several rows of data from a file */ void VReadObjectData (FILE *fp,int object_id,VImage *image) { static VImageInfo imageInfo; static VAttrList list=NULL; fseek(fp,0L,SEEK_SET); if (! ReadHeader (fp)) VError("error reading header"); if (! (list = ReadAttrList (fp))) VError("error reading attr list"); if (! VGetImageInfo(fp,list,object_id,&imageInfo)) VError(" error reading image info"); if (imageInfo.nbands != VImageNBands((*image))) VError("incorrect number of bands"); if (imageInfo.nrows != VImageNRows((*image))) VError("incorrect number of rows"); if (imageInfo.ncolumns != VImageNColumns((*image))) VError("incorrect number of columns"); if (imageInfo.repn != VPixelRepn((*image))) VError("incorrect pixel representation"); if (! VReadBlockData (fp,&imageInfo,0,imageInfo.nrows,image)) VError(" error reading data"); }
void ReadImages (VString Name, VAttrList& Images, VAttrList& history_list) { FILE* file; /* input file */ VAttrList list, list1; /* attribute list */ VAttrListPosn pos; /* position in list */ VImage image; /* image in list */ /* initialize results */ Images = NULL; /* open file */ file = fopen (Name, "r"); if (!file) { VError ("Failed to open input file '%s'", Name); return; } /* read file */ list = VReadFile (file, NULL); if (!list) { VError ("Failed to read input file '%s'", Name); fclose (file); return; } /* Read History */ list1 = VReadHistory(&list); if (list1==NULL) list1=VCreateAttrList(); history_list = VCopyAttrList(list1); /* extract images */ for (VFirstAttr (list, &pos); VAttrExists (&pos); VNextAttr (&pos)) { if (VGetAttrRepn (&pos) != VImageRepn) continue; if (!Images) Images = VCreateAttrList (); VGetAttrValue (&pos, NULL, VImageRepn, &image); VSetAttrValue (&pos, NULL, VImageRepn, NULL); VAppendAttr (Images, "image", NULL, VImageRepn, image); } if (!Images) VError ("Input file '%s' does not contain an image", Name); /* clean-up*/ VDestroyAttrList (list); fclose (file); } /* ReadImages */
VImage VImageManager::vtimestep( VAttrList list, VImage dest, int step ) { VFillImage( dest, VAllBands, 0 ); VPointer src_pp = NULL; VPointer dest_pp = NULL; VAttrListPosn posn; VShort *ptr1 = NULL; VShort *ptr2 = NULL; VString str; VImage src; int n = 0; int npixels = 0; bool revert = false; for ( VFirstAttr ( list, & posn ); VAttrExists ( & posn ); VNextAttr ( & posn ) ) { if ( VGetAttrRepn ( & posn ) != VImageRepn ) continue; VGetAttrValue ( &posn, NULL, VImageRepn, &src ); if ( VPixelRepn( src ) != VShortRepn ) continue; if ( ( VGetAttr ( VImageAttrList ( src ), "MPIL_vista_0", NULL, VStringRepn, ( VPointer ) & str ) != VAttrFound ) && ( VGetAttr ( VImageAttrList ( src ), "repetition_time", NULL, VStringRepn, ( VPointer ) & str ) != VAttrFound ) ) continue; /* doch nicht rumdrehen! if (VGetAttr (VImageAttrList (src), "extent", NULL, VStringRepn, (VPointer) & str) != VAttrFound) revert = true; */ if ( n == 0 ) VCopyImageAttrs ( src, dest ); if ( VImageNRows( src ) > 1 ) { if ( VSelectBand ( "vtimestep", src, step, &npixels, &src_pp ) == FALSE ) VError( "err reading data" ); int destBand = ( revert ) ? VImageNBands( dest ) - n - 1 : n; dest_pp = VPixelPtr( dest, destBand, 0, 0 ); ptr1 = ( VShort * ) src_pp; ptr2 = ( VShort * ) dest_pp; memcpy ( ptr2, ptr1, npixels * sizeof( VShort ) ); } n++; } return dest; }
int main (int argc,char *argv[]) { static VFloat sigma = 1.5; static VOptionDescRec options[] = { {"sigma",VFloatRepn,1,(VPointer) &sigma,VOptionalOpt,NULL,"std dev"} }; FILE *in_file,*out_file; VAttrList list=NULL; VAttrListPosn posn; VImage src=NULL,dest=NULL; char prg[50]; sprintf(prg,"vgauss2d V%s", getVersion()); fprintf (stderr, "%s\n", prg); VParseFilterCmd (VNumber (options),options,argc,argv,&in_file,&out_file); if (! (list = VReadFile (in_file, NULL))) exit (1); fclose(in_file); for (VFirstAttr (list, & posn); VAttrExists (& posn); VNextAttr (& posn)) { if (VGetAttrRepn (& posn) != VImageRepn) continue; VGetAttrValue (& posn, NULL,VImageRepn, & src); dest = VFilterGauss2d (src,NULL,(double)sigma); VSetAttrValue (& posn, NULL,VImageRepn,dest); } if (src == NULL) VError(" no input image found"); VHistory(VNumber(options),options,prg,&list,&list); if (! VWriteFile (out_file, list)) exit (1); fprintf (stderr, "%s: done.\n", argv[0]); return 0; }
void VImage::write_to_buffer( const char *suffix, void **buf, size_t *size, VOption *options ) { char filename[VIPS_PATH_MAX]; char option_string[VIPS_PATH_MAX]; const char *operation_name; VipsBlob *blob; vips__filename_split8( suffix, filename, option_string ); if( !(operation_name = vips_foreign_find_save_buffer( filename )) ) { delete options; throw VError(); } call_option_string( operation_name, option_string, (options ? options : VImage::option())-> set( "in", *this )-> set( "buffer", &blob ) ); if( blob ) { if( buf ) { *buf = VIPS_AREA( blob )->data; VIPS_AREA( blob )->free_fn = NULL; } if( size ) *size = VIPS_AREA( blob )->length; vips_area_unref( VIPS_AREA( blob ) ); } }
VImage VImage::new_from_buffer( void *buf, size_t len, const char *option_string, VOption *options ) { const char *operation_name; VipsBlob *blob; VImage out; if( !(operation_name = vips_foreign_find_load_buffer( buf, len )) ) { delete options; throw( VError() ); } /* We don't take a copy of the data or free it. */ blob = vips_blob_new( NULL, buf, len ); options = (options ? options : VImage::option())-> set( "buffer", blob )-> set( "out", &out ); vips_area_unref( VIPS_AREA( blob ) ); call_option_string( operation_name, option_string, options ); return( out ); }
static void FreeAttrValue (VStringConst routine, VAttrRec *a) { VTypeMethods *methods; switch (a->repn) { case VAttrListRepn: VDestroyAttrList (a->value); break; case VBundleRepn: VDestroyBundle (a->value); break; case VPointerRepn: case VStringRepn: break; default: if (! (methods = VRepnMethods (a->repn))) VError ("%s: %s attribute has invalid repn %d", routine, a->name, a->repn); (methods->destroy) (a->value); } }
int main(int argc, char *argv[]) { static VString cfile = ""; static VFloat tr = 2; static VBoolean normalize = FALSE; static VOptionDescRec options[] = { {"in", VStringRepn, 1, (VPointer) &cfile, VRequiredOpt, NULL, "file"}, {"tr", VFloatRepn, 1, (VPointer) &tr, VRequiredOpt, NULL, "TR in seconds"}, {"normalize", VBooleanRepn, 1, (VPointer) &normalize, VOptionalOpt, NULL, "Whether to normalize"} }; FILE *out_file = NULL; VAttrList list = NULL; VImage dest = NULL; char prg_name[100]; char ver[100]; getLipsiaVersion(ver, sizeof(ver)); sprintf(prg_name, "vcovariates V%s", ver); fprintf(stderr, "%s\n", prg_name); VParseFilterCmd(VNumber(options), options, argc, argv, NULL, &out_file); if(tr > 500) VError(" tr must be given in seconds, not milliseconds"); dest = VCovariates(cfile, tr, normalize); list = VCreateAttrList(); VAppendAttr(list, "image", NULL, VImageRepn, dest); /* Output: */ if(! VWriteFile(out_file, list)) exit(1); fprintf(stderr, " %s: done.\n", argv[0]); return 0; }
/* ** concordance mapping */ VImage VCCM2(float *A1,float *A2,VImage map,int type) { VImage dest=NULL; size_t b,r,c,i,j,k,kk,nvoxels; int nslices=0,nrows=0,ncols=0; double *arr1=NULL,*arr2=NULL,u; nslices = VPixel(map,0,3,0,VShort); nrows = VPixel(map,0,3,1,VShort); ncols = VPixel(map,0,3,2,VShort); nvoxels = VImageNColumns(map); dest = VCreateImage(nslices,nrows,ncols,VFloatRepn); VFillImage(dest,VAllBands,0); VCopyImageAttrs (map, dest); VSetAttr(VImageAttrList(dest),"modality",NULL,VStringRepn,"conimg"); arr1 = (double *) VCalloc(nvoxels,sizeof(double)); arr2 = (double *) VCalloc(nvoxels,sizeof(double)); fprintf(stderr," concordance mapping...\n"); for (i=0; i<nvoxels; i++) { if (i%50 == 0) fprintf(stderr," %6d of %d\r",i,nvoxels); b = (int)VPixel(map,0,0,i,VShort); r = (int)VPixel(map,0,1,i,VShort); c = (int)VPixel(map,0,2,i,VShort); for (j=0; j<nvoxels; j++) arr1[j] = arr2[j] = 0; kk = 0; for (j=0; j<i; j++) { k=j+i*(i+1)/2; arr1[kk] = (double)A1[k]; arr2[kk] = (double)A2[k]; kk++; } for (j=i+1; j<nvoxels; j++) { k=i+j*(j+1)/2; arr1[kk] = (double)A1[k]; arr2[kk] = (double)A2[k]; kk++; } u = 0; switch (type) { case 0: u = kendall(arr1,arr2,nvoxels); break; case 1: u = CCC(arr1,arr2,nvoxels); break; default: VError("illegal type"); } VPixel(dest,b,r,c,VFloat) = (VFloat)u; } fprintf(stderr,"\n"); return dest; }
VImage VImage::write( VImage out ) { if( vips_image_write( this->get_image(), out.get_image() ) ) throw VError(); return( out ); }
VBoolean WriteImages (VString Name, VAttrList Images, VAttrList& history_list) { FILE* file; /* output file */ VAttrList list; /* attribute list */ VAttrListPosn pos; /* position in list */ VImage image; /* image in list */ VBoolean success; /* success flag */ char history[]="history"; /* open file */ file = fopen (Name, "w"); if (!file) { VError ("Failed to open output file '%s'", Name); return FALSE; } /* create list */ list = VCreateAttrList(); /* insert images */ for (VFirstAttr (Images, &pos); VAttrExists (&pos); VNextAttr (&pos)) { VGetAttrValue (&pos, NULL, VImageRepn, &image); VAppendAttr (list, VGetAttrName (&pos), NULL, VImageRepn, image); } /* Prepend history */ VPrependAttr(list, history ,NULL,VAttrListRepn,history_list); /* write file */ success = VWriteFile (file, list); if (!success) VError ("Failed to write output file '%s'", Name); /* remove images */ for (VFirstAttr (list, &pos); VAttrExists (&pos); VNextAttr (&pos)) if (VGetAttrRepn (&pos) == VImageRepn) VSetAttrValue (&pos, NULL, VImageRepn, NULL); /* clean-up*/ VDestroyAttrList (list); fclose (file); return success; } /* WriteImages */
VBoolean VUnpackData (VRepnKind repn, size_t nels, VPointer packed, VPackOrder packed_order, size_t *length, VPointer *unpacked, VBoolean *alloced) { VPackOrder unpacked_order; size_t unpacked_elsize = VRepnSize (repn) * CHAR_BIT; size_t packed_elsize = VRepnPrecision (repn); size_t unpacked_length = nels * VRepnSize (repn); /* If a space for the unpacked data was supplied, ensure there's enough of it: */ if (! alloced && unpacked_length > *length) { VWarning ("VUnpackData: Insufficient space for unpacked data"); return FALSE; } *length = unpacked_length; /* Determine the present machine's internal byte order: */ unpacked_order = MachineByteOrder (); /* If the desired byte order matches that of the present machine's, and the unpacked and packed data element sizes are identical, just return the packed data: */ if (unpacked_order == packed_order && unpacked_elsize == packed_elsize) { if (alloced) { *unpacked = packed; *alloced = FALSE; } else if (packed != *unpacked) memcpy (*unpacked, packed, unpacked_length); return TRUE; } /* Unpack data elements into the buffer: */ if (packed_elsize == unpacked_elsize) { /* If the packed and unpacked are the same size, do a straight copy: */ if (packed != *unpacked) memcpy (*unpacked, packed, unpacked_length); /* Swap bytes if necessary: */ if (packed_order != unpacked_order && packed_elsize > 8) SwapBytes (nels, packed_elsize / 8, (char *) *unpacked); } else if (packed_elsize == 1) { /* If the elements are VBits, this unpacks them: */ VUnpackBits (nels, packed_order, (char *) packed, (char *) *unpacked); } else /* Unpacking multi-byte integers or floats is currently not supported: */ VError ("VUnpackData: " "Unpacking %s from %d to %d bits is not supported", VRepnName (repn), packed_elsize, unpacked_elsize); return TRUE; }
/*! \fn VImage VMapImageRange(VImage src,VImage dest,VRepnKind repn) \brief The minimum and maximum grey values of the input images are computed, and a linear mapping is performed mapping the input minimum(maximum) of the input image to the min(max) value of the output pixel repn. E.g. if the input image min is -17 and its max is +2376, and the output repn is VUByteRepn, then the linear mapping function maps -17 to 0 and +2376 to 255. \param src input image (any repn) \param dest output image \param repn the output pixel repn (e.g. VUByteRepn) */ VImage VMapImageRange(VImage src,VImage dest,VRepnKind repn) { int i,nbands,nrows,ncols,npixels; float u,ymin,ymax,dmin,dmax,slope; if (repn == VDoubleRepn || repn == VLongRepn || repn == VSByteRepn) VError(" VMapImageRange: double, long and sbyte are not supported"); nbands = VImageNBands(src); nrows = VImageNRows(src); ncols = VImageNColumns(src); npixels = VImageNPixels(src); dest = VSelectDestImage("VMapImageRange",dest,nbands,nrows,ncols,repn); if (! dest) VError(" err creating dest image"); VFillImage(dest,VAllBands,0); ymin = VPixelMaxValue(src); ymax = VPixelMinValue(src); for (i=0; i<npixels; i++) { u = VGetPixelValue (src,i); if (u < ymin) ymin = u; if (u > ymax) ymax = u; } dmax = VPixelMaxValue(dest); dmin = VPixelMinValue(dest); slope = (dmax - dmin) / (ymax - ymin); for (i=0; i<npixels; i++) { u = VGetPixelValue (src,i); u = slope * (u - ymin); if (u < dmin) u = dmin; if (u > dmax) u = dmax; VSetPixelValue(dest,i,(VFloat) u); } VCopyImageAttrs (src, dest); return dest; }
VImage ConcatDesigns(VImage *design, int nimages) { VImage dest = NULL; VString buf = NULL; VDouble x; int i, r, c, row, col; int nrows, ncols; nrows = ncols = 0; for(i = 0; i < nimages; i++) { ncols += VImageNColumns(design[i]); nrows += VImageNRows(design[i]); } dest = VCreateImage(1, nrows, ncols, VFloatRepn); VFillImage(dest, VAllBands, 0); VCopyImageAttrs(design[0], dest); VSetAttr(VImageAttrList(dest), "nsessions", NULL, VShortRepn, (VShort)nimages); buf = (VString) VMalloc(80); buf[0] = '\0'; for(i = 0; i < nimages; i++) sprintf(buf, "%s %d", buf, (int)VImageNRows(design[i])); VSetAttr(VImageAttrList(dest), "session_lengths", NULL, VStringRepn, buf); buf[0] = '\0'; for(i = 0; i < nimages; i++) sprintf(buf, "%s %d", buf, (int)VImageNColumns(design[i])); VSetAttr(VImageAttrList(dest), "session_covariates", NULL, VStringRepn, buf); row = col = 0; for(i = 0; i < nimages; i++) { for(r = 0; r < VImageNRows(design[i]); r++) { for(c = 0; c < VImageNColumns(design[i]); c++) { x = VGetPixel(design[i], 0, r, c); if(row + r >= VImageNRows(dest)) VError(" row: %d\n", row + r); if(col + c >= VImageNColumns(dest)) VError(" column: %d\n", col + c); VSetPixel(dest, 0, row + r, col + c, x); } } row += VImageNRows(design[i]); col += VImageNColumns(design[i]); } return dest; }
VBoolean Compatible (VImage Image1, VImage Image2) { /* compare image sizes */ if ((VImageNBands (Image1) != VImageNBands (Image2)) || (VImageNRows (Image1) != VImageNRows (Image2)) || (VImageNColumns (Image1) != VImageNColumns (Image2))) { VError ("Images have different sizes"); return FALSE; } return TRUE; } /* Compatible */
void VSetPixel(VImage image, int band, int row, int column, VDoublePromoted value) { VPointer p = VPixelPtr (image, band, row, column); switch (VPixelRepn (image)) { case VBitRepn: *(VBit *) p = value; break; case VUByteRepn: *(VUByte *) p = value; break; case VSByteRepn: *(VSByte *) p = value; break; case VShortRepn: *(VShort *) p = value; break; case VLongRepn: *(VLong *) p = value; break; case VFloatRepn: *(VFloat *) p = value; break; case VDoubleRepn: *(VDouble *) p = value; break; default: VError("VSetPixel: %s images not supported", VPixelRepnName (image)); } }
VPackOrder MachineByteOrder () { union { short s; char c[sizeof (short)]; } u; u.s = 1; if (u.c[0] == 1) return VLsbFirst; if (u.c[sizeof (short) - 1] != 1) VError ("VPackImage or VUnpackImage: Byte order not recognized"); return VMsbFirst; }
VDouble VGetPixel (VImage image, int band, int row, int column) { VPointer p = VPixelPtr (image, band, row, column); switch (VPixelRepn (image)) { case VBitRepn: return (VDouble) *(VBit *) p; case VUByteRepn: return (VDouble) *(VUByte *) p; case VSByteRepn: return (VDouble) *(VSByte *) p; case VShortRepn: return (VDouble) *(VShort *) p; case VLongRepn: return (VDouble) *(VLong *) p; case VFloatRepn: return (VDouble) *(VFloat *) p; case VDoubleRepn: return (VDouble) *(VDouble *) p; default: VError("VGetPixel: %s images not supported", VPixelRepnName (image)); } return 0.0; /* to make lint happy */ }
int main (int argc,char *argv[]) { static VShort aneighb = 1; static VShort arepn = 1; static VOptionDescRec options[] = { {"n",VShortRepn,1,(VPointer) &aneighb, VOptionalOpt,ADJDict,"neighbourhood type"}, {"repn",VShortRepn,1,(VPointer) &arepn, VOptionalOpt,TYPEDict,"output representation type (ubyte or short)"} }; FILE *in_file,*out_file; VAttrList list=NULL; VAttrListPosn posn; VImage src=NULL,dest=NULL; int nl=0,neighb; VRepnKind repn; char prg[50]; sprintf(prg,"vlabel2d V%s", getVersion()); fprintf (stderr, "%s\n", prg); VParseFilterCmd (VNumber (options),options,argc,argv,&in_file,&out_file); if (arepn == 0) repn = VUByteRepn; else repn = VShortRepn; if (aneighb == 0) neighb = 4; else neighb = 8; if (! (list = VReadFile (in_file, NULL))) exit (1); fclose(in_file); for (VFirstAttr (list, & posn); VAttrExists (& posn); VNextAttr (& posn)) { if (VGetAttrRepn (& posn) != VImageRepn) continue; VGetAttrValue (& posn, NULL,VImageRepn, & src); dest = VLabelImage2d(src,NULL, (int) neighb,repn,&nl); fprintf(stderr," numlabels= %d\n",nl); VSetAttrValue (& posn, NULL,VImageRepn,dest); } if (src == NULL) VError(" no input image found"); VHistory(VNumber(options),options,prg,&list,&list); if (! VWriteFile (out_file, list)) exit (1); fprintf (stderr, "%s: done.\n", argv[0]); return 0; }
/*! \fn VImage VMedianImage2d (VImage src, VImage dest, int dim, VBoolean ignore) \param src input image \param dest output image \param dim kernel size \param ignore whether to ignore zero voxels */ VImage VMedianImage2d (VImage src, VImage dest, int dim, VBoolean ignore) { int nbands,nrows,ncols; int i,len,b,r,c,rr,cc,d=0; gsl_vector *vec=NULL; double u,tiny=1.0e-10; if (dim%2 == 0) VError("VMedianImage2d: dim (%d) must be odd",dim); nrows = VImageNRows (src); ncols = VImageNColumns (src); nbands = VImageNBands (src); d = dim/2; len = dim * dim; vec = gsl_vector_calloc(len); dest = VCopyImage(src,dest,VAllBands); for (b=0; b<nbands; b++) { for (r=d; r<nrows-d; r++) { for (c=d; c<ncols-d; c++) { gsl_vector_set_zero(vec); u = VGetPixel(src,b,r,c); if ( !ignore || ABS(u) > tiny) { i = 0; for (rr=r-d; rr<=r+d; rr++) { for (cc=c-d; cc<=c+d; cc++) { u = VGetPixel(src,b,rr,cc); if (ABS(u) > 0) { gsl_vector_set(vec,i,u); i++; } } } gsl_sort_vector(vec); u = gsl_stats_median_from_sorted_data(vec->data,vec->stride,i); VSetPixel(dest,b,r,c,u); } } } } return dest; }
bool UpdateSearch_Inner(double V_west, double theta_best) { // given wind speed and direction, find TAS error int V_west_x = iround(100*V_west*cos(theta_best)); int V_west_y = iround(100*V_west*sin(theta_best)); int verr = VError(V_west_x, V_west_y); // search for minimum error // (this is not monotonous) if (verr<error_best) { error_best = verr; V_west_best = V_west; theta_west_best = theta_best; return true; } else { return false; } }
int main (int argc,char *argv[]) { static VLong hemi = 0; static VOptionDescRec options[] = { { "hemi", VLongRepn, 1, & hemi, VOptionalOpt, HEMIDict, "Hemisphere to retain" } }; FILE *in_file, *out_file; VAttrList list; VAttrListPosn posn; VImage isrc=NULL; VGraph gsrc=NULL,dest=NULL; char prg[50]; sprintf(prg,"vhemi V%s", getVersion()); fprintf (stderr, "%s\n", prg); /* Parse command line arguments and identify files: */ VParseFilterCmd (VNumber (options), options, argc, argv, & in_file, & out_file); /* Read the grey input file: */ list = VReadFile (in_file, NULL); if (! list) VError("Error reading input image"); fclose(in_file); for (VFirstAttr (list, & posn); VAttrExists (& posn); VNextAttr (& posn)) { if (VGetAttrRepn (& posn) == VImageRepn) { VGetAttrValue (& posn, NULL, VImageRepn, & isrc); VImageHemi(&isrc,hemi); VSetAttrValue (& posn, NULL,VImageRepn,isrc); } else if (VGetAttrRepn (& posn) == VGraphRepn) { VGetAttrValue (& posn, NULL, VGraphRepn, & gsrc); dest = VGraphHemi(gsrc,hemi); VSetAttrValue (& posn, NULL,VGraphRepn,dest); } } /* Write out the results: */ VHistory(VNumber(options),options,prg,&list,&list); if (! VWriteFile (out_file,list)) exit (1); fprintf (stderr, "%s: done.\n", argv[0]); return 0; }