コード例 #1
0
ファイル: rotmask.c プロジェクト: anasazi/POP-REU-Project
/* Rotate an imask with a set of offsets.
 */
static INTMASK *
rotimask( offset_fn fn, INTMASK *m, const char *name )
{
	INTMASK *out;
	int size = m->xsize * m->ysize;
	int *offsets;
	int i;

	if( m->xsize != m->ysize || (m->xsize % 2) == 0 ) {
		im_errormsg( "im_rotate_*mask*: mask should "
			"be square of even size" );
		return( NULL );
	}
	if( !(offsets = fn( m->xsize )) )
		return( NULL );
	if( !(out = im_create_imask( name, m->xsize, m->ysize )) ) {
		im_free( offsets );
		return( NULL );
	}
	out->scale = m->scale;
	out->offset = m->offset;

	for( i = 0; i < size; i++ )
		out->coeff[i] = m->coeff[offsets[i]];

	im_free( offsets );

	return( out );
}
コード例 #2
0
ファイル: matalloc.c プロジェクト: ChiaraCaiazza/collageMaker
void im_free_dmat(double **m, int nrl, int nrh, int ncl, int nch)
{
	int i;

	for (i=nrh; i>=nrl; i--)
		im_free((char*) (m[i]+ncl));
	im_free((char*) (m+nrl));
}
コード例 #3
0
ファイル: matalloc.c プロジェクト: ChiaraCaiazza/collageMaker
void im_free_fmat(float **m, int nrl, int nrh, int ncl, int nch)
{
	int i;

	for (i=nrh; i>=nrl; i--)
		im_free((char*) (m[i]+ncl));
	im_free((char*) (m+nrl));
}
コード例 #4
0
ファイル: im_gradcor.c プロジェクト: Ikulagin/transmem
static int gradcor_stop( void *vptr_seq, void *unrequired, void *unreq2 ){

  gradcor_seq_t *seq= (gradcor_seq_t*) vptr_seq;
  if( seq ){
    im_free( (void*) seq-> region_xgrad );
    im_free( (void*) seq-> region_ygrad );
    im_region_free( seq-> reg );
    seq-> region_xgrad= (int*) NULL;
    seq-> region_ygrad= (int*) NULL;
    seq-> reg= (REGION*) NULL;
    im_free( (void*) seq );
  }
  return 0;
}
コード例 #5
0
ファイル: window.c プロジェクト: elechak/blue
static void destroy(Link self){
    BWindow win = self->value.vptr;
    im_free( win->items);
    bwindow_free(win);
    if ( win->onClose) link_free(win->onClose);
    object_destroy(self);
}
コード例 #6
0
ファイル: im_draw_mask.c プロジェクト: nrobidoux/libvips
static int
mask_draw_labq( Mask *mask )
{
	float *lab_buffer;
	float ink_buffer[3];
	int y;

	if( !(lab_buffer = IM_ARRAY( NULL, 
		mask->image_clip.width * 3, float )) )
		return( -1 );

	imb_LabQ2Lab( DRAW( mask )->ink, ink_buffer, 1 );

	for( y = 0; y < mask->image_clip.height; y++ ) {
		PEL *to = (PEL *) IM_IMAGE_ADDR( DRAW( mask )->im, 
			mask->image_clip.left, y + mask->image_clip.top );
		PEL *mask_line = (PEL *) IM_IMAGE_ADDR( mask->mask_im, 
			mask->mask_clip.left, y + mask->mask_clip.top );

		imb_LabQ2Lab( to, lab_buffer, mask->image_clip.width );
		DBLEND( float, lab_buffer, ink_buffer );
		imb_Lab2LabQ( lab_buffer, to, mask->image_clip.width );
	}

	im_free( lab_buffer );

	return( 0 );
}
コード例 #7
0
ファイル: im.c プロジェクト: Daksh/tuxpaint-0.9.21
/**
* Initialize the IM_DATA structure.
*
* @param im    IM_DATA structure to initialize.
* @param lang  LANG_* defined constant to initialize the structure with.
*/
void im_init(IM_DATA* im, int lang)
{
  /* Free already allocated resources if initialized before */
  if(im_initialized) {
    im_free(im);
  }

  /* Initialize */
  memset(im, 0, sizeof(IM_DATA));
  im->lang = lang;

  /* Setup static globals */
  if(!im_initialized) {
    /* ADD NEW LANGUAGE SUPPORT HERE */
    im_event_fns[LANG_JA] = &im_event_ja;
    im_event_fns[LANG_KO] = &im_event_ko;
    im_event_fns[LANG_TH] = &im_event_th;
    im_event_fns[LANG_ZH_TW] = &im_event_zh_tw;

    im_initialized = 1;
  }

  #ifdef DEBUG
  assert(0 <= im->lang && im->lang < NUM_LANGS);
  if(im_event_fp) printf("Initializing IM for %s...\n", lang_prefixes[im->lang]);
  #endif

  /* Initialize the individual IM */
  im_request(im, IM_REQ_INIT);
}
コード例 #8
0
ファイル: vipsobject.c プロジェクト: jcupitt/nip2
static void
vo_free( Vo *vo )
{
	heap_unregister_element( vo->rc->heap, &vo->out );
	VIPS_UNREF( vo->object );

	im_free( vo );
}
コード例 #9
0
ファイル: header.c プロジェクト: alon/libvips
/* Print header, or parts of header.
 */
static int
print_header( IMAGE *im, gboolean many )
{
	if( !main_option_field ) {
		printf( "%s: ", im->filename );

		vips_object_print_summary( VIPS_OBJECT( im ) );

		if( main_option_all )
			(void) vips_image_map( im, print_field_fn, &many );
	}
	else if( strcmp( main_option_field, "getext" ) == 0 ) {
		if( im__has_extension_block( im ) ) {
			void *buf;
			int size;

			if( !(buf = im__read_extension_block( im, &size )) )
				return( -1 );
			printf( "%s", (char *) buf );
			im_free( buf );
		}
	}
	else if( strcmp( main_option_field, "Hist" ) == 0 ) 
		printf( "%s", im_history_get( im ) );
	else {
		GValue value = { 0 };
		GType type;

		if( im_header_get( im, main_option_field, &value ) )
			return( -1 );

		/* Display the save form, if there is one. This way we display
		 * something useful for ICC profiles, xml fields, etc.
		 */
		type = G_VALUE_TYPE( &value );
		if( g_value_type_transformable( type, IM_TYPE_SAVE_STRING ) ) {
			GValue save_value = { 0 };

			g_value_init( &save_value, IM_TYPE_SAVE_STRING );
			if( !g_value_transform( &value, &save_value ) ) 
				return( -1 );
			printf( "%s\n", im_save_string_get( &save_value ) );
			g_value_unset( &save_value );
		}
		else {
			char *str_value;

			str_value = g_strdup_value_contents( &value );
			printf( "%s\n", str_value );
			g_free( str_value );
		}

		g_value_unset( &value );
	}

	return( 0 );
}
コード例 #10
0
ファイル: matlab.c プロジェクト: Ikulagin/transmem
static void
read_destroy( Read *read )
{
	IM_FREE( read->filename );
	IM_FREEF( Mat_VarFree, read->var );
	IM_FREEF( Mat_Close, read->mat );

	im_free( read );
}
コード例 #11
0
ファイル: im_vips2jpeg.c プロジェクト: aturcotte/libvips
static void
write_destroy( Write *write )
{
	jpeg_destroy_compress( &write->cinfo );
	IM_FREEF( im_close, write->in );
	IM_FREEF( fclose, write->eman.fp );
	IM_FREE( write->row_pointer );
	IM_FREE( write->profile_bytes );
	IM_FREEF( im_close, write->inverted );
	im_free( write );
}
コード例 #12
0
ファイル: im_png2vips.c プロジェクト: Ikulagin/transmem
static void
read_destroy( Read *read )
{
	IM_FREE( read->name );
	IM_FREEF( fclose, read->fp );
	if( read->pPng )
		png_destroy_read_struct( &read->pPng, &read->pInfo, NULL );
	IM_FREE( read->row_pointer );
	IM_FREE( read->data );

	im_free( read );
}
コード例 #13
0
ファイル: im_tile_cache.c プロジェクト: aturcotte/libvips
static void
read_destroy( Read *read )
{
	IM_FREEF( g_mutex_free, read->lock );

	while( read->cache ) {
		Tile *tile = (Tile *) read->cache->data;

		tile_destroy( tile );
	}

	im_free( read );
}
コード例 #14
0
ファイル: im_tile_cache.c プロジェクト: aturcotte/libvips
static void
tile_destroy( Tile *tile )
{
	Read *read = tile->read;

	read->cache = g_slist_remove( read->cache, tile );
	read->ntiles -= 1;
	g_assert( read->ntiles >= 0 );
	tile->read = NULL;

	IM_FREEF( im_region_free, tile->region );

	im_free( tile );
}
コード例 #15
0
ファイル: top_main.cpp プロジェクト: liororama/gossimon
int main(int argc, char **argv) 
{
    void *data;
    mlog_init();
    TopSaxParser parser1;
        
    // Debugging if argc > 1
    if(argc > 1) {

        mlog_registerColorFormatter((color_formatter_func_t)sprintf_color);
        mlog_addOutputFile((char *)"/dev/tty", 1);
        mlog_setModuleLevel((char *)"top", LOG_DEBUG);
        mlog_setModuleLevel((char *)"topparser", LOG_DEBUG);
    
    }
    // init
    im_init(&data, NULL);
    
    // sleep
    sleep(1);
    
    while(1) {
        // update
        im_update(data);
        
        int i = 16000;
        im_get(data, buff, &i);
        //system("clear");
        //printf("%s", buff);
        //printf("\n");

        
        processVecT v;
        TopSaxParser parser;
        parser.parse(std::string(buff), v);
        std::cout << "================= Top Processes: " << v.size() << " ======================" << std::endl;
        for(std::vector<ProcessStatusInfo>::iterator iter = v.begin() ; iter != v.end() ; iter++) {
            ProcessStatusInfo *pi = &(*iter);
            std::cout << "XML:" << std::endl << pi->getProcessXML();
        }
        sleep(3);

    }
    
    // free
    im_free(&data);
    
    free(buff);
    return 0;
}       
コード例 #16
0
ファイル: im_generate.c プロジェクト: connorimes/parsec-3.0
/**
 * im_stop_many:
 *
 * Stop function for many images in. First client is a pointer to
 * a %NULL-terminated array of input images.
 *
 * See also: im_generate().
 */
int
im_stop_many( void *seq, void *dummy1, void *dummy2 )
{
    REGION **ar = (REGION **) seq;

    if( ar ) {
        int i;

        for( i = 0; ar[i]; i++ )
            im_region_free( ar[i] );
        im_free( (char *) ar );
    }

    return( 0 );
}
コード例 #17
0
ファイル: meminfo_main.cpp プロジェクト: liororama/gossimon
int main(int argc, char **argv) 
{
    void *data;
    mlog_init();
//    MeminfoSaxParser parser1;
        
    // Debugging if argc > 1
    if(argc > 1) {

        mlog_registerColorFormatter((color_formatter_func_t)sprintf_color);
        mlog_addOutputFile((char *)"/dev/tty", 1);
        mlog_setModuleLevel((char *)"meminfo", LOG_DEBUG);
        mlog_setModuleLevel((char *)"meminfoparser", LOG_DEBUG);
    
    }
    // init
    im_init(&data, NULL);
    
    // sleep
    sleep(1);
    
    while(1) {
        // update
        im_update(data);
        
        int i = 8192;
        im_get(data, buff, &i);
        //system("clear");
        printf("%s", buff);
        printf("\n");

        
        MeminfoSaxParser parser;
        Meminfo mi;
        parser.parse(std::string(buff), mi);
       
        std::cout << mi.get_xml();
        std::cout << "====================================================" << std::endl << std::endl;
        sleep(1);

    }
    
    // free
    im_free(&data);
    
    free(buff);
    return 0;
}       
コード例 #18
0
ファイル: region.c プロジェクト: connorimes/parsec-3.0
/**
 * im_region_free:
 * @reg: #REGION to free
 *
 * Free a region and any resources it holds.
 *
 * If @im has previously been closed, then freeing the last #REGION on @in can
 * cause @im to finally be freed as well.
 */
void
im_region_free( REGION *reg )
{
    IMAGE *im;

    if( !reg )
        return;
    im = reg->im;

    /* Stop this sequence.
     */
    im__call_stop( reg );

    /* Free any attached memory.
     */
    im_region_reset( reg );

    /* Detach from image.
     */
    g_mutex_lock( im->sslock );
    im->regions = g_slist_remove( im->regions, reg );
    g_mutex_unlock( im->sslock );
    reg->im = NULL;

    /* Was this the last region on an image with close_pending? If yes,
     * close the image too.
     */
    if( !im->regions && im->close_pending ) {
#ifdef DEBUG_IO
        printf( "im_region_free: closing pending image \"%s\"\n",
                im->filename );
#endif /*DEBUG_IO*/
        /* Time to close the image.
         */
        im->close_pending = 0;
        im_close( im );
    }

    im_free( reg );

#ifdef DEBUG
    g_mutex_lock( im__global_lock );
    g_assert( g_slist_find( im__regions_all, reg ) );
    im__regions_all = g_slist_remove( im__regions_all, reg );
    printf( "%d regions in vips\n", g_slist_length( im__regions_all ) );
    g_mutex_unlock( im__global_lock );
#endif /*DEBUG*/
}
コード例 #19
0
ファイル: im_magick2vips.c プロジェクト: Ikulagin/transmem
static int
read_destroy( Read *read )
{
#ifdef DEBUG
	printf( "im_magick2vips: read_destroy: %s\n", read->filename );
#endif /*DEBUG*/

	IM_FREEF( DestroyImage, read->image );
	IM_FREEF( DestroyImageInfo, read->image_info ); 
	IM_FREE( read->frames );
	IM_FREE( read->filename );
	DestroyExceptionInfo( &read->exception );
	IM_FREEF( g_mutex_free, read->lock );
	im_free( read );

	return( 0 );
}
コード例 #20
0
ファイル: buffer.c プロジェクト: Ikulagin/transmem
static void
buffer_cache_list_free( im_buffer_cache_list_t *cache_list )
{
	GSList *p;

	/* Need to mark undone so we don't try and take them off this hash on
	 * unref.
	 */
	for( p = cache_list->buffers; p; p = p->next ) {
		im_buffer_t *buffer = (im_buffer_t *) p->data;

		buffer->done = FALSE;
	}

	g_slist_free( cache_list->buffers );
	im_free( cache_list );
}
コード例 #21
0
ファイル: im_jpeg2vips.c プロジェクト: aturcotte/libvips
static int
attach_thumbnail( IMAGE *im, ExifData *ed )
{
	if( ed->size > 0 ) {
		char *thumb_copy;

		thumb_copy = im_malloc( NULL, ed->size );      
		memcpy( thumb_copy, ed->data, ed->size );

		if( im_meta_set_blob( im, "jpeg-thumbnail-data", 
			(im_callback_fn) im_free, thumb_copy, ed->size ) ) {
			im_free( thumb_copy );
			return( -1 );
		}
	}

	return( 0 );
}
コード例 #22
0
ファイル: im_gradcor.c プロジェクト: Ikulagin/transmem
static void *gradcor_start( IMAGE *out, void *vptr_large, void *unrequired ){

  gradcor_seq_t *seq= IM_NEW( NULL, gradcor_seq_t );
  if( ! seq )
    return NULL;

  seq-> region_xgrad= (int*) NULL;
  seq-> region_ygrad= (int*) NULL;
  seq-> region_xgrad_area= 0;
  seq-> region_ygrad_area= 0;

  seq-> reg= im_region_create( (IMAGE*) vptr_large );
  if( ! seq-> reg ){
    im_free( (void*) seq );
    return NULL;
  }
  return (void*) seq;
}
コード例 #23
0
ファイル: im_fav4.c プロジェクト: Ikulagin/transmem
/**
 * im_fav4:
 * @in: array of 4 input #IMAGE s
 * @out: output #IMAGE
 *
 * Average four identical images. 
 *
 * Deprecated.
*/
int
im_fav4( IMAGE **in, IMAGE *out)
{
	PEL *result, *buffer, *p1, *p2, *p3, *p4;
	int x,y;
	int linebytes, PICY;

/* check IMAGEs parameters 
*/
if(im_iocheck(in[1], out)) return(-1);

/* BYTE images only!
*/
if( (in[0]->BandFmt != IM_BANDFMT_CHAR) &&  (in[0]->BandFmt != IM_BANDFMT_UCHAR)) return(-1);

if ( im_cp_desc(out, in[1]) == -1)   /* copy image descriptors */
      return(-1);
if ( im_setupout(out) == -1)
      return(-1);

linebytes = in[0]->Xsize * in[0]->Bands;
PICY = in[0]->Ysize;
buffer = (PEL*)im_malloc(NULL,linebytes);
memset(buffer, 0, linebytes);

	p1 = (PEL*)in[0]->data;
	p2 = (PEL*)in[1]->data;
	p3 = (PEL*)in[2]->data;
	p4 = (PEL*)in[3]->data;

for (y = 0; y < PICY; y++)
	{
	result = buffer;
	/* average 4 pels with rounding, for whole line*/
	for (x = 0; x < linebytes; x++) {
		*result++ = (PEL)((int)((int)*p1++ + (int)*p2++ + (int)*p3++ + (int)*p4++ +2) >> 2);
		}
	im_writeline(y,out, buffer);
	}
im_free(buffer);
return(0);
}
コード例 #24
0
ファイル: im_maxpos_avg.c プロジェクト: ImageKit/libvips
/* Merge the sequence value back into the per-call state.
 */
static int
maxposavg_stop( void *seq, void *a, void *b )
{
	Maxposavg *global_maxposavg = (Maxposavg *) b;
	Maxposavg *maxposavg = (Maxposavg *) seq;

	/* Merge.
	 */
	if( maxposavg->max > global_maxposavg->max ) 
		*global_maxposavg = *maxposavg;
	else if( maxposavg->max == global_maxposavg->max ) {
		global_maxposavg->xpos += maxposavg->xpos;
		global_maxposavg->ypos += maxposavg->ypos;
		global_maxposavg->occurences += maxposavg->occurences;
	}

	im_free( seq );

	return( 0 );
}
コード例 #25
0
ファイル: buffer.c プロジェクト: Ikulagin/transmem
void
im_buffer_unref( im_buffer_t *buffer )
{
#ifdef DEBUG
	printf( "** im_buffer_unref: left = %d, top = %d, "
		"width = %d, height = %d (%p)\n",
		buffer->area.left, buffer->area.top, 
		buffer->area.width, buffer->area.height, 
		buffer );
#endif /*DEBUG*/

	g_assert( buffer->ref_count > 0 );

	buffer->ref_count -= 1;

	if( buffer->ref_count == 0 ) {
#ifdef DEBUG
		if( !buffer->done )
			printf( "im_buffer_unref: buffer was not done\n" );
#endif /*DEBUG*/

		im_buffer_undone( buffer );

		buffer->im = NULL;
		IM_FREE( buffer->buf );
		buffer->bsize = 0;
		im_free( buffer );

#ifdef DEBUG
		g_mutex_lock( im__global_lock );
		g_assert( g_slist_find( im__buffers_all, buffer ) );
		im__buffers_all = g_slist_remove( im__buffers_all, buffer );
		printf( "%d buffers in vips\n", 
			g_slist_length( im__buffers_all ) );
		g_mutex_unlock( im__global_lock );
#endif /*DEBUG*/
	}
}
コード例 #26
0
ファイル: im_min.c プロジェクト: anasazi/POP-REU-Project
/* Merge the sequence value back into the per-call state.
 */
static int
stop_fn( void *vseq, void *a, void *b )
{
	Seq *seq = (Seq *) vseq;
	MinInfo *inf = (MinInfo *) a;

	if( seq->valid ) {
		if( !inf->valid )
			/* Just copy.
			 */
			inf->value = seq->value;
		else 
			/* Merge.
			 */
			inf->value = IM_MIN( inf->value, seq->value );

		inf->valid = 1;
	}

	im_free( seq );

	return( 0 );
}
コード例 #27
0
int rrd_xport(
    int argc,
    char **argv,
    int UNUSED(*xsize),
    time_t *start,
    time_t *end,        /* which time frame do you want ?
                         * will be changed to represent reality */
    unsigned long *step,    /* which stepsize do you want?
                             * will be changed to represent reality */
    unsigned long *col_cnt, /* number of data columns in the result */
    char ***legend_v,   /* legend entries */
    rrd_value_t **data)
{                       /* two dimensional array containing the data */
    image_desc_t im;
    time_t    start_tmp = 0, end_tmp = 0;
    rrd_time_value_t start_tv, end_tv;
    char     *parsetime_error = NULL;
    struct optparse options;
    optparse_init(&options, argc, argv);

    struct optparse_long longopts[] = {
        {"start",  's', OPTPARSE_REQUIRED},
        {"end",    'e', OPTPARSE_REQUIRED},
        {"maxrows",'m', OPTPARSE_REQUIRED},
        {"step",   261, OPTPARSE_REQUIRED},
        {"enumds", 262, OPTPARSE_NONE},
        {"json",   263, OPTPARSE_NONE},
        {"showtime", 't', OPTPARSE_NONE},
        {"daemon", 'd', OPTPARSE_REQUIRED},
        {0}
    };

    rrd_graph_init(&im);

    rrd_parsetime("end-24h", &start_tv);
    rrd_parsetime("now", &end_tv);

    int enumds=0;
    int json=0;
    int showtime=0;
    
    int opt;
    while ((opt = optparse_long(&options,longopts,NULL)) != -1){

        switch (opt) {
        case 261:
            im.step = atoi(options.optarg);
            break;
        case 262:
  	    enumds=1;
            break;
        case 263:
  	    json=1;
            break;
        case 't':
  	    showtime=1;
            break;
        case 's':
            if ((parsetime_error = rrd_parsetime(options.optarg, &start_tv))) {
                rrd_set_error("start time: %s", parsetime_error);
                return -1;
            }
            break;
        case 'e':
            if ((parsetime_error = rrd_parsetime(options.optarg, &end_tv))) {
                rrd_set_error("end time: %s", parsetime_error);
                return -1;
            }
            break;
        case 'm':
            im.xsize = atol(options.optarg);
            if (im.xsize < 10) {
                rrd_set_error("maxrows below 10 rows");
                return -1;
            }
            break;
        case 'd':
        {
            if (im.daemon_addr != NULL)
            {
                rrd_set_error ("You cannot specify --daemon "
                        "more than once.");
                return (-1);
            }

            im.daemon_addr = strdup(options.optarg);
            if (im.daemon_addr == NULL)
            {
                rrd_set_error("strdup error");
                return -1;
            }
            break;
        }

        case '?':
            rrd_set_error("%s", options.errmsg);
            return -1;
        }
    }

    if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
        return -1;
    }

    if (start_tmp < 3600 * 24 * 365 * 10) {
        rrd_set_error("the first entry to fetch should be after 1980 (%ld)",
                      start_tmp);
        return -1;
    }

    if (end_tmp < start_tmp) {
        rrd_set_error("start (%ld) should be less than end (%ld)",
                      start_tmp, end_tmp);
        return -1;
    }

    im.start = start_tmp;
    im.end = end_tmp;
    im.step = max((long) im.step, (im.end - im.start) / im.xsize);

    rrd_graph_script(options.argc, options.argv, &im, options.optind);
    if (rrd_test_error()) {
        im_free(&im);
        return -1;
    }

    if (im.gdes_c == 0) {
        rrd_set_error("can't make an xport without contents");
        im_free(&im);
        return (-1);
    }

    {   /* try to connect to rrdcached */
        int status = rrdc_connect(im.daemon_addr);
        if (status != 0) return status;
    }

    if (rrd_xport_fn(&im, start, end, step, col_cnt, legend_v, data,0) == -1) {
        im_free(&im);
        return -1;
    }

    /* and create the export */
    if (!xsize) {
      int flags=0;
      if (json) { flags|=1; }
      if (showtime) { flags|=2; }
      if (enumds) { flags|=4; }
      stringbuffer_t buffer={0,0,NULL,stdout};
      rrd_xport_format_xmljson(flags,&buffer,&im,
			       *start, *end, *step,
			       *col_cnt, *legend_v,
			       *data);
    }

    im_free(&im);
    return 0;
}
コード例 #28
0
ファイル: im_draw_mask.c プロジェクト: nrobidoux/libvips
static void
mask_free( Mask *mask )
{
	im__draw_free( DRAW( mask ) );
	im_free( mask );
}
コード例 #29
0
ファイル: dicom.cpp プロジェクト: fiking/SIFT3D
/* Helper function to write an image to a directory of DICOM files using C++ */
static int write_dcm_dir_cpp(const char *path, const Image *const im,
        const Dcm_meta *const meta) {

        Image slice;

        // Initialize C intermediates
        init_im(&slice);

        // Initialize the metadata to defaults, if it is null 
        Dcm_meta meta_new;
        set_meta_defaults(meta, &meta_new);

        // Get the number of leading zeros for the file names
        const int num_slices = im->nz;
        const int num_zeros = static_cast<int>(ceil(log10(
                static_cast<double>(num_slices))));

        // Form the printf format string for file names
#define BUF_LEN 16
        char format[BUF_LEN];
        snprintf(format, BUF_LEN, "%%0%dd.%s", num_zeros, ext_dcm); 
#undef BUF_LEN

        // Resize the slice buffer
        slice.nx = im->nx; 
        slice.ny = im->ny;
        slice.nz = 1;
        slice.nc = im->nc;
        im_default_stride(&slice);
        if (im_resize(&slice)) {
                im_free(&slice);
                return SIFT3D_FAILURE;
        }

        // Copy the units to the slice
        memcpy(SIFT3D_IM_GET_UNITS(&slice), SIFT3D_IM_GET_UNITS(im), 
                IM_NDIMS * sizeof(double));

        // Get the maximum absolute value of the whole image volume
        const float max_val = im_max_abs(im);

        // Write each slice
        for (int i = 0; i < num_slices; i++) {

                // Form the slice file name
#define BUF_LEN 1024
                char buf[BUF_LEN];
                snprintf(buf, BUF_LEN, format, i);

                // Form the full file path
                std::string fullfile(path + sepStr + buf);

                // Copy the data to the slice
                int x, y, z, c;
                SIFT3D_IM_LOOP_START_C(&slice, x, y, z, c)
                        SIFT3D_IM_GET_VOX(&slice, x, y, z, c) =
                                SIFT3D_IM_GET_VOX(im, x, y, i, c);
                SIFT3D_IM_LOOP_END_C

                // Generate a new SOPInstanceUID
                dcmGenerateUniqueIdentifier(meta_new.instance_uid, 
                        SITE_INSTANCE_UID_ROOT); 

                // Set the instance number
                const unsigned int instance = static_cast<unsigned int>(i + 1);
                meta_new.instance_num = instance;

                // Write the slice to a file
                if (write_dcm(fullfile.c_str(), &slice, &meta_new, max_val)) {
                        im_free(&slice);
                        return SIFT3D_FAILURE;
                }
        }

        // Clean up
        im_free (&slice);

        return SIFT3D_SUCCESS;
}
コード例 #30
0
ファイル: regSift3D.c プロジェクト: mnburkle/SIFT3D-1
int main(int argc, char *argv[]) {

        SIFT3D sift3d;
        Reg_SIFT3D reg;
        Image src, ref;
        Mat_rm match_src, match_ref;
        void *tform, *tform_arg;
        char *src_path, *ref_path, *warped_path, *match_path, *tform_path,
                *concat_path, *keys_path, *lines_path;
        tform_type type;
        double thresh;
        int num_args, c, have_match, have_tform;

        const struct option longopts[] = {
                {"matches", required_argument, NULL, MATCHES},
                {"transform", required_argument, NULL, TRANSFORM},
                {"warped", required_argument, NULL, WARPED},
                {"concat", required_argument, NULL, CONCAT},
                {"keys", required_argument, NULL, KEYS},
                {"lines", required_argument, NULL, LINES},
                {"thresh", required_argument, NULL, THRESH},
                {"type", required_argument, NULL, TYPE},
                {0, 0, 0, 0}
        };

        const char str_affine[] = "affine";
        SIFT3D_Descriptor_store *const desc_src = &reg.desc_src;
        SIFT3D_Descriptor_store *const desc_ref = &reg.desc_ref;

        // Parse the GNU standard options
        switch (parse_gnu(argc, argv)) {
                case SIFT3D_HELP:
                        puts(help_msg);
                        print_opts_SIFT3D();
                        return 0;
                case SIFT3D_VERSION:
                        return 0;
                case SIFT3D_FALSE:
                        break;
                default:
                        err_msgu("Unexpected return from parse_gnu \n");
                        return 1;
        }

        // Initialize the data
        init_im(&src);
        init_im(&ref);
        init_Reg_SIFT3D(&reg);
        init_SIFT3D(&sift3d);
        if (init_Mat_rm(&match_src, 0, 0, DOUBLE, SIFT3D_FALSE) ||
                init_Mat_rm(&match_ref, 0, 0, DOUBLE, SIFT3D_FALSE)) {
                err_msgu("Failed basic initialization.");
                return 1;
        }

        // Initialize parameters to defaults        
        type = type_default;

        // Parse the SIFT3D options
        if ((argc = parse_args_SIFT3D(&sift3d, argc, argv, SIFT3D_FALSE)) < 0)
                return 1;
        set_SIFT3D_Reg_SIFT3D(&reg, &sift3d);

        // Parse the remaining options 
        opterr = 1;
        have_match = have_tform = SIFT3D_FALSE;
        match_path = tform_path = warped_path = concat_path = keys_path =
                lines_path = NULL;
        while ((c = getopt_long(argc, argv, "", longopts, NULL)) != -1) {
                switch (c) {
                        case MATCHES:
                                match_path = optarg;
                                have_match = SIFT3D_TRUE;
                                break;
                        case TRANSFORM:
                                tform_path = optarg;
                                have_tform = SIFT3D_TRUE;
                                break;
                        case WARPED:
                                warped_path = optarg;
                                have_tform = SIFT3D_TRUE;
                                break;
                        case CONCAT:
                                concat_path = optarg;
                                have_match = SIFT3D_TRUE;
                                break;
                        case KEYS:
                                keys_path = optarg;
                                have_match = SIFT3D_TRUE;
                                break;
                        case LINES:
                                lines_path = optarg;
                                have_match = SIFT3D_TRUE;
                                break;
                        case THRESH:
                                thresh = atof(optarg);
                                if (set_nn_thresh_Reg_SIFT3D(&reg, thresh)) {
                                        err_msg("Invalid value for thresh.");
                                        return 1;
                                }
                                break;
                        case TYPE:
                                if (!strcmp(optarg, str_affine)) {
                                        type = AFFINE;
                                } else {

                                        char msg[1024];

                                        sprintf(msg,    
                                                "Unrecognized transformation "
                                                "type: %s\n", optarg);
                                        err_msg(msg);
                                        return 1;
                                }
                                break;
                        case '?':
                        default:
                                return 1;
                }
        }

        // Ensure that at least one output was specified
        if (!have_match && !have_tform) {
                err_msg("No outputs were specified.");
                return 1;
        }

        // Parse the required arguments
        num_args = argc - optind;
        if (num_args < 2) {
                err_msg("Not enough arguments.");
                return 1;
        } else if (num_args > 2) {
                err_msg("Too many arguments.");
                return 1;
        }
        src_path = argv[optind];
        ref_path = argv[optind + 1];

        // Allocate memory for the transformation
        if ((tform = malloc(tform_type_get_size(type))) == NULL) {
                err_msg("Out of memory.");
                return 1;
        }

        // Initialize the transformation
        if (init_tform(tform, type))
                return 1;

        // Read the images
        if (read_nii(src_path, &src)) {

                char msg[1024];

                sprintf(msg, "Failed to read the source image \"%s\"", 
                        src_path);
                err_msg(msg);
                return 1;
        }
        if (read_nii(ref_path, &ref)) {

                char msg[1024];

                sprintf(msg, "Failed to read the reference image \"%s\"",
                        ref_path);
                err_msg(msg);
                return 1;
        }

        // Set the images
        if (set_src_Reg_SIFT3D(&reg, &src)) {
                err_msgu("Failed to set the source image.");
                return 1;
        }
        if (set_ref_Reg_SIFT3D(&reg, &ref)) {
                err_msgu("Failed to set the reference image.");
                return 1;
        }

        // Match the features, optionally registering the images 
        tform_arg = have_tform ? tform : NULL;
        if (register_SIFT3D(&reg, tform_arg)) {
                err_msgu("Failed to register the images.");
                return 1;
        }

        // Convert the matches to matrices
        if (get_matches_Reg_SIFT3D(&reg, &match_src, &match_ref)) {
                err_msgu("Failed to convert matches to coordinates.");
                return 1;
        }

        // Write the outputs
        if (match_path != NULL) {

                Mat_rm matches;
                int i, j;

                // Initialize intermediates
                init_Mat_rm(&matches, 0, 0, DOUBLE, SIFT3D_FALSE);

                // Form a combined matrix for both sets of matches
                if (concat_Mat_rm(&match_src, &match_ref, &matches, 1)) {
                        err_msgu("Failed to concatenate the matches.");
                        return 1;
                }

                if (write_Mat_rm(match_path, &matches)) {

                        char msg[1024];

                        sprintf(msg, "Failed to write the matches \"%s\"", 
                                match_path);
                        err_msg(msg);
                        return 1;
                }

                // Clean up
                cleanup_Mat_rm(&matches);
        }
        if (tform_path != NULL && write_tform(tform_path, tform)) {

                char msg[1024];

                sprintf(msg, "Failed to write the transformation parameters "
                        "\"%s\"", tform_path);
                err_msg(msg);
                return 1;
        }

        // Optionally warp the source image
        if (warped_path != NULL) {

                Image warped;

                // Initialize intermediates
                init_im(&warped);

                // Warp
                if (im_inv_transform(tform, &src, &warped, interp)) {
                        err_msgu("Failed to warp the source image.");
                        return 1;
                }

                // Write the warped image
                if (write_nii(warped_path, &warped)) {

                        char msg[1024];

                        sprintf(msg, "Failed to write the warped image \"%s\"",
                                warped_path);
                        err_msg(msg);
                        return 1;
                }

                // Clean up
                im_free(&warped);
        }

        // Optionally draw the matches
        if (concat_path != NULL || keys_path != NULL || lines_path != NULL) {

                Image concat, keys, lines;
                Mat_rm keys_src, keys_ref;
                Image *concat_arg, *keys_arg, *lines_arg;

                // Initialize intermediates
                init_im(&concat);
                init_im(&keys);
                init_im(&lines);
                if (init_Mat_rm(&keys_src, 0, 0, DOUBLE, SIFT3D_FALSE) ||
                        init_Mat_rm(&keys_ref, 0, 0, DOUBLE, SIFT3D_FALSE)) {
                        err_msgu("Failed to initialize keypoint matrices.");
                        return 1;
                }

                // Set up the pointers for the images
                concat_arg = concat_path == NULL ? NULL : &concat;
                keys_arg = keys_path == NULL ? NULL : &keys;
                lines_arg = lines_path == NULL ? NULL : &lines;

                // Convert the keypoints to matrices
                if (Keypoint_store_to_Mat_rm(&reg.kp_src, &keys_src) ||
                    Keypoint_store_to_Mat_rm(&reg.kp_ref, &keys_ref)) {
                        err_msgu("Failed to convert the keypoints to "
                                 "matrices.");
                        return 1;
                }

                // Draw the matches
                if (draw_matches(&src, &ref, &keys_src, &keys_ref, 
                        &match_src, &match_ref, concat_arg, keys_arg,
                        lines_arg)) {
                        err_msgu("Failed to draw the matches.");
                        return 1;
                }

                // Optionally write a concatenated image
                if (concat_path != NULL && write_nii(concat_path, &concat)) {

                        char msg[1024];

                        sprintf(msg, "Failed to write the concatenated image "
                                "\"%s\"", concat_path);
                        err_msg(msg);
                        return 1;
                }

                // Optionally write the keypoints
                if (keys_path != NULL && write_nii(keys_path, &keys)) {

                        char msg[1024];

                        sprintf(msg, "Failed to write the keypoint image "
                                "\"%s\"", concat_path);
                        err_msg(msg);
                        return 1;
                }

                // Optionally write the matches
                if (lines_path != NULL && write_nii(lines_path, &lines)) {

                        char msg[1024];

                        sprintf(msg, "Failed to write the line image "
                                "\"%s\"", concat_path);
                        err_msg(msg);
                        return 1;
                }

                // Clean up
                im_free(&concat);
                im_free(&keys);
                im_free(&lines);
        }

	return 0;
}