示例#1
0
int writeInrimageData(FILE *f, const inrimage *inr) {
  _image *img;
  int i;

  img = inrimage2_image(inr);
  img->fd = f;
  img->openMode = OM_FILE;
  i = _writeInrimageData(img);
  img->data = NULL;
  _freeImage(img);

  if(i >= 0) return 0;
  else return i;
}
示例#2
0
文件: gis.cpp 项目: FMX/CGAL
int writeGis( char *name, _image* im) {
  char *outputName;
  int length, extLength=0, res;


  length=strlen(name);
  outputName= (char *)ImageIO_alloc(length+8);
  
  if ( strncmp( name+length-4, ".dim", 4 ) == 0 ) {
    extLength = 4;
  }
  else if ( strncmp( name+length-4, ".ima", 4 ) == 0 ) {
    extLength = 4;
  }
  else if ( strncmp( name+length-7, ".ima.gz", 7 ) == 0 ) {
    extLength = 7;
  }
  else if ( strncmp( name+length-7, ".dim.gz", 7 ) == 0 ) {
    extLength = 7;
  }

  strncpy( outputName, name, length-extLength );
  if ( strncmp( name+length-7, ".dim.gz", 7 ) == 0 )
    strcpy( outputName+length-extLength, ".dim.gz" );
  else
    strcpy( outputName+length-extLength, ".dim" );

  _openWriteImage(im, outputName);
  if( !im->fd ) {
    fprintf(stderr, "writeGis: error: unable to open file \'%s\'\n", outputName);
    if ( outputName != NULL ) ImageIO_free( outputName );
    return ImageIO_OPENING;
  }

  res = writeGisHeader(im);
  if (res < 0 ) {
    fprintf(stderr, "writeGis: error: unable to write header of \'%s\'\n",
	    outputName);
    if ( outputName != NULL ) ImageIO_free( outputName );
    ImageIO_close( im );
    im->fd = NULL;
    im->openMode = OM_CLOSE;
    return( res );
  }

  ImageIO_close(im);
  
  strncpy( outputName, name, length-extLength );
  if ( strncmp( name+length-3, ".gz", 3 ) == 0 ) {
    strcpy( outputName+length-extLength, ".ima.gz" );
  }
  else {
    strcpy( outputName+length-extLength, ".ima" );
  }

  _openWriteImage(im, outputName);

  if( !im->fd ) {
    fprintf(stderr, "writeGis: error: unable to open file \'%s\'\n", outputName);
    if ( outputName != NULL ) ImageIO_free( outputName );
    return ImageIO_OPENING;
  }

  if ( im->dataMode == DM_ASCII ) {
    int i, j, n, size;
    char *str = (char*)ImageIO_alloc( _LGTH_STRING_+1 );
    size = im->xdim * im->ydim * im->zdim * im->vdim;
    n = ( im->xdim < 16 ) ? im->xdim : 16;
    i = 0;

    switch( im->wordKind ) {
    default :
      fprintf(stderr, "writeGis: such word kind not handled in ascii mode for file \'%s\'\n", outputName);
      if ( outputName != NULL ) ImageIO_free( outputName );
      return( -3 );
    case WK_FIXED :
      switch ( im->wdim ) {
      default :
	fprintf(stderr, "writeGis: such word dim not handled in ascii mode for file \'%s\'\n", outputName);
	if ( outputName != NULL ) ImageIO_free( outputName );
	return( -3 );
      case 1 :
	switch ( im->sign ) {
	default :
	  fprintf(stderr, "writeGis: such sign not handled in ascii mode for file \'%s\'\n", outputName);
	  if ( outputName != NULL ) ImageIO_free( outputName );
	  return( -3 );
	case SGN_UNSIGNED :
	  {
	    unsigned char *theBuf = ( unsigned char * )im->data;
	    do {
	      memset( str, 0, _LGTH_STRING_ );
	      for ( j=0; j<n && i<size; j++, i++ ) {
		sprintf( str+strlen(str), "%d", theBuf[i] );
		if ( j<n && i<size ) sprintf( str+strlen(str), " " );
	      }
	      sprintf( str+strlen(str), "\n" );
	      res = ImageIO_write( im, str, strlen( str )  );
	      if ( res  <= 0 ) {
		fprintf(stderr, "writeGis: error when writing data in \'%s\'\n", outputName);
		if ( outputName != NULL ) ImageIO_free( outputName );
		return( -3 );
	      }
	    } while ( i < size );
	  }
	  break;
	case SGN_SIGNED :
	  {
	    char *theBuf = ( char * )im->data;
	    do {
	      memset( str, 0, _LGTH_STRING_ );
	      for ( j=0; j<n && i<size; j++, i++ ) {
		sprintf( str+strlen(str), "%d", theBuf[i] );
		if ( j<n && i<size ) sprintf( str+strlen(str), " " );
	      }
	      sprintf( str+strlen(str), "\n" );
	      res = ImageIO_write( im, str, strlen( str )  );
	      if ( res  <= 0 ) {
		fprintf(stderr, "writeGis: error when writing data in \'%s\'\n", outputName);
		if ( outputName != NULL ) ImageIO_free( outputName );
		return( -3 );
	      }
	    } while ( i < size );
	  }
	  break;
	} /* end of switch ( im->sign ) */
	break;
      case 2 :
	switch ( im->sign ) {
	default :
	  fprintf(stderr, "writeGis: such sign not handled in ascii mode for file \'%s\'\n", outputName);
	  if ( outputName != NULL ) ImageIO_free( outputName );
	  return( -3 );
	case SGN_UNSIGNED :
	  {
	    unsigned short int *theBuf = ( unsigned short int * )im->data;
	    do {
	      memset( str, 0, _LGTH_STRING_ );
	      for ( j=0; j<n && i<size; j++, i++ ) {
		sprintf( str+strlen(str), "%d", theBuf[i] );
		if ( j<n && i<size ) sprintf( str+strlen(str), " " );
	      }
	      sprintf( str+strlen(str), "\n" );
	      res = ImageIO_write( im, str, strlen( str )  );
	      if ( res  <= 0 ) {
		fprintf(stderr, "writeGis: error when writing data in \'%s\'\n", outputName);
		if ( outputName != NULL ) ImageIO_free( outputName );
		return( -3 );
	      }
	    } while ( i < size );
	  }
	  break;
	case SGN_SIGNED :
	  {
	    short int *theBuf = ( short int * )im->data;
	    do {
	      memset( str, 0, _LGTH_STRING_ );
	      for ( j=0; j<n && i<size; j++, i++ ) {
		sprintf( str+strlen(str), "%d", theBuf[i] );
		if ( j<n && i<size ) sprintf( str+strlen(str), " " );
	      }
	      sprintf( str+strlen(str), "\n" );
	      res = ImageIO_write( im, str, strlen( str )  );
	      if ( res  <= 0 ) {
		fprintf(stderr, "writeGis: error when writing data in \'%s\'\n", outputName);
		if ( outputName != NULL ) ImageIO_free( outputName );
		return( -3 );
	      }
	    } while ( i < size );
	  }
	  break;
	} /* end of switch ( im->sign ) */
	break;	
      } /* end of switch ( im->wdim ) */
    } /* end of switch( im->wordKind ) */

    ImageIO_free( str ); 
  }
  else {
    res = _writeInrimageData(im);
  }
  if ( outputName != NULL ) ImageIO_free( outputName );
  return res;
  
}