Beispiel #1
0
/* Writes the given image body in an already opened file.*/
int _writeInrimageData(const _image *im) {
  unsigned long size, nbv, nwrt, i;
  unsigned int v;
  unsigned char **vp;
  
  if(im->openMode != OM_CLOSE) {

    /* scalar or interlaced vectors */
    if(im->vectMode != VM_NON_INTERLACED) {
      size = im->xdim * im->ydim * im->zdim * im->vdim * im->wdim;
      nwrt = ImageIO_write(im, im->data, size);
      if(nwrt != size) return -1;
      else return 1;
    }

    /* non interlaced vectors: interlace for saving */
    else {
      nbv = im->xdim * im->ydim * im->zdim;
      size = im->xdim * im->ydim * im->zdim * im->wdim;
      vp = (unsigned char **) ImageIO_alloc(im->vdim * sizeof(unsigned char *));
      for(v = 0; v < im->vdim; v++)
	vp[v] = (unsigned char *) im->data + v * size;
      for(i = 0; i < nbv; i++)
	for(v = 0; v < im->vdim; v++) {
	  if(ImageIO_write(im, (const void *) vp[v], im->wdim) != im->wdim)
	    return -1;
	  vp[v] += im->wdim;
	}
      ImageIO_free(vp);
      return 1;
    }
  }
  else return -1;
}
Beispiel #2
0
int writePgmImage(char *name,_image *im  )
{
  char string[256];
  int max;
  unsigned int i;

  if ( im->xdim <= 0 || im->ydim <= 0 || im->zdim != 1 || im->vdim != 1 ) {
    fprintf( stderr, "writePgmImage: bad dimensions, unable to write '%s'\n", name );
    return -1;
  }
  if ( im->wordKind != WK_FIXED || im->sign != SGN_UNSIGNED 
       || ( im->wdim != 1 && im->wdim != 2 ) ) {
    fprintf( stderr, "writePgmImage: bad type, unable to write '%s'\n", name );
    return -1;  
  }

  if ( 0 )
    im->dataMode = DM_ASCII;
  
  _openWriteImage( im, name );
  
  if(!im->fd) {
    fprintf(stderr, "writePgmImage: error: unable to open file \'%s\'\n", name );
    return ImageIO_OPENING;
  }
  
  if ( im->dataMode == DM_ASCII ) 
    sprintf( string, "%s\n", PGM_ASCII_MAGIC );
  else 
    sprintf( string, "%s\n", PGM_MAGIC );
  
  ImageIO_write( im, string, strlen( string ) );
  sprintf( string, "# CREATOR: pnm.c $Revision$ $Date$\n" );
  ImageIO_write( im, string, strlen( string ) );
  sprintf( string, "%d %d\n", im->xdim, im->ydim );
  ImageIO_write( im, string, strlen( string ) );
  max = 0;
  switch ( im->wdim ) {
  case 1 :
    {
      unsigned char *buf = (unsigned char *)im->data;
      for ( i=0; i<im->xdim*im->ydim; i++, buf++ )
	if ( max < *buf ) max = *buf;
    }
    break;
  case 2 :
    {
      unsigned short *buf = (unsigned short *)im->data;
      for ( i=0; i<im->xdim*im->ydim; i++, buf++ )
	if ( max < *buf ) max = *buf;
    }
    break;
  }
  /* max == 0 causes problems for xv */
  if ( max == 0 ) max = 1;
  sprintf( string, "%d\n", max );
  ImageIO_write( im, string, strlen( string ) );

  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->wdim ) {
    default :
      /* can not occur */
      fprintf( stderr, "writePgmImage: bad type, unable to write '%s'\n", name );
      ImageIO_close( im );
      im->openMode = OM_CLOSE;
      return -1;
    case 1 :
      {
	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" );
	  if ( ImageIO_write( im, str, strlen( str ) ) <= 0 ) {
	    fprintf(stderr, "writePgmImage: error when writing data in \'%s\'\n", name );
	    return( -3 );
	  }
	} while ( i < size );
      }
      break;
    case 2 :
      {
	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" );
	  if ( ImageIO_write( im, str, strlen( str ) ) <= 0 ) {
	    fprintf(stderr, "writePgmImage: error when writing data in \'%s\'\n", name );
	    return( -3 );
	  }
	} while ( i < size );
      }
      break;
    }
  }
  else {
    if ( im->wdim == 1 || ( im->wdim == 2 && max > 255 ) ) {
      ImageIO_write( im, im->data, im->xdim*im->ydim*im->wdim );
    }
    else {
      /* 2 octets, but max <= 255
	 has to be converted on one octet
      */
      unsigned short *buf = (unsigned short *)im->data;
      unsigned char *tmp = (unsigned char *)ImageIO_alloc( im->xdim*im->ydim );
      if ( tmp == NULL ) {
	fprintf( stderr, "writePgmImage: unable to allocate auxiliary buffer\n" );
	return -1; 
      }
      for ( i=0; i<im->xdim*im->ydim; i++, buf++ )
	tmp[i] = (unsigned char)*buf;
      ImageIO_write( im, tmp, im->xdim*im->ydim );
      ImageIO_free( tmp );
    }
  }

  ImageIO_close( im );
  im->openMode = OM_CLOSE;
  return 1;
}
Beispiel #3
0
int writePpmImage( char *name,_image *im )
{
  char string[256];
  int max;
  unsigned int i;

  if ( im->xdim <= 0 || im->ydim <= 0 || im->zdim != 1 || im->vdim != 3 ) {
    fprintf( stderr, "writePpmImage: bad dimensions, unable to write '%s'\n", name );
    return -1;
  }
  if ( im->wordKind != WK_FIXED || im->sign != SGN_UNSIGNED 
       || ( im->wdim != 1 && im->wdim != 2 ) ) {
    fprintf( stderr, "writePpmImage: bad type, unable to write '%s'\n", name );
    return -1;  
  }
  
  _openWriteImage( im, name );
  
  if(!im->fd) {
    fprintf(stderr, "writeInrimage: error: unable to open file \'%s\'\n", name );
    return ImageIO_OPENING;
  }
  
  sprintf( string, "%s\n", PPM_MAGIC );
  ImageIO_write( im, string, strlen( string ) );
  sprintf( string, "# CREATOR: pnm.c $Revision$ $Date$\n" );
  ImageIO_write( im, string, strlen( string ) );
  sprintf( string, "%d %d\n", im->xdim, im->ydim );
  ImageIO_write( im, string, strlen( string ) );
  max = 0;
  switch ( im->wdim ) {
  case 1 :
    {
      unsigned char *buf = (unsigned char *)im->data;
      for ( i=0; i<im->xdim*im->ydim*3; i++, buf++ )
	if ( max < *buf ) max = *buf;
    }
    break;
  case 2 :
    {
      unsigned short *buf = (unsigned short *)im->data;
      for ( i=0; i<im->xdim*im->ydim*3; i++, buf++ )
	if ( max < *buf ) max = *buf;
    }
    break;
  }

  if ( max == 0 ) max = 1;
  sprintf( string, "%d\n", max );
  ImageIO_write( im, string, strlen( string ) );
  if ( im->wdim == 1 || ( im->wdim == 2 && max > 255 ) ) {
    ImageIO_write( im, im->data, im->xdim*im->ydim*3*im->wdim );
  }
  else {
    /* 2 octets, but max <= 255
       has to be converted on one octet
    */
    unsigned short *buf = (unsigned short *)im->data;
    unsigned char *tmp = (unsigned char *)ImageIO_alloc( im->xdim*im->ydim*3 );
    if ( tmp == NULL ) {
      fprintf( stderr, "writePpmImage: unable to allocate auxiliary buffer\n" );
      return -1; 
    }
    for ( i=0; i<im->xdim*im->ydim*3; i++, buf++ )
      tmp[i] = (unsigned char)*buf;
    ImageIO_write( im, tmp, im->xdim*im->ydim*3 );
    ImageIO_free( tmp );
  }
  ImageIO_close( im );
  im->openMode = OM_CLOSE;
  return 1;
}
Beispiel #4
0
Datei: gis.cpp Projekt: FMX/CGAL
int writeGisHeader( const _image* inr )
{
  const char *proc = "writeGisHeader";
  char *str = NULL;
  
  if ( inr->vectMode == VM_NON_INTERLACED ) {
    fprintf( stderr, "%s: can not write non interlaced data\n", proc );
    return -1;
  }

  str = (char*)ImageIO_alloc( _LGTH_STRING_ );

  /* dimensions
   */
  sprintf( str, "%d %d", inr->xdim, inr->ydim );
  if ( inr->vdim > 1 ) {
    sprintf( str+strlen(str), " %d %d", inr->zdim, inr->vdim );
  }
  else if ( inr->zdim > 1 ) {
    sprintf( str+strlen(str), " %d", inr->zdim );
  }
  sprintf( str+strlen(str), "\n" );

  /* type
   */
  sprintf( str+strlen(str), "-type " );
  switch ( inr->wordKind ) {
  case WK_FIXED :
    switch( inr->sign ) {
    case SGN_UNSIGNED :
      sprintf( str+strlen(str), "U" );
      sprintf( str+strlen(str), "%d", 8*inr->wdim );
      break;
    case SGN_SIGNED :
      sprintf( str+strlen(str), "S" );
      sprintf( str+strlen(str), "%d", 8*inr->wdim );
      break;
    default :
      fprintf( stderr, "%s: unknown wordSign\n", proc );
      ImageIO_free( str );
      return -1;    
    }
    break;
  case WK_FLOAT :
    if ( inr->wdim == sizeof( float ) ) {
      sprintf( str+strlen(str), "FLOAT" );
    }
    else if ( inr->wdim  == sizeof( double ) ) {
      sprintf( str+strlen(str), "DOUBLE" );
    }
    else {
      fprintf( stderr, "%s: unknown WK_FLOAT word dim\n", proc );
      ImageIO_free( str );
      return -1;    
    }
    break;
  default :
    fprintf( stderr, "%s: unknown wordKind for image\n", proc );
    ImageIO_free( str );
    return -1;  
  }
  sprintf( str+strlen(str), "\n" );
  
  sprintf( str+strlen(str), "-dx %f\n", inr->vx );
  sprintf( str+strlen(str), "-dy %f\n", inr->vy );
  if ( inr->zdim > 1 ) 
    sprintf( str+strlen(str), "-dz %f\n", inr->vz );
  
  if ( inr->wdim > 1 ) {
    sprintf( str+strlen(str), "-bo " );
    switch ( _getEndianness() ) {
    default :
    case END_LITTLE :
      sprintf( str+strlen(str), "DCBA" ); break;
    case END_BIG :
      sprintf( str+strlen(str), "ABCD" );   break;
    }
    sprintf( str+strlen(str), "\n" );
  }
  switch ( inr->dataMode ) {
  default :
  case DM_BINARY :
    sprintf( str+strlen(str), "-om binar\n" );
    break;
  case DM_ASCII :
    sprintf( str+strlen(str), "-om ascii\n" );
  }
  if( ImageIO_write( inr, str, strlen(str)) == 0) {
    ImageIO_free( str );
    return -1;
  }
  ImageIO_free( str );
  return 1;
}
Beispiel #5
0
Datei: gis.cpp Projekt: 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;
  
}
Beispiel #6
0
/* Writes the given inrimage header in an already opened file.*/
int _writeInrimageHeader(const _image *im, ENDIANNESS end) {
  unsigned int pos, i;
  char type[30], endianness[5], buf[257], scale[20];

  Set_numeric_locale num_locale("C");

  if(im->openMode != OM_CLOSE) {
    /* fix word kind */
    switch(im->wordKind) {

    case WK_FLOAT:
      sprintf(type, "float");
      scale[0] = '\0';
      break;

    case WK_FIXED:
      switch(im->sign) {
      case SGN_SIGNED:
	sprintf(type, "signed fixed");
	break;
      case SGN_UNSIGNED:
	sprintf(type, "unsigned fixed");
	break;
      default:
	return -1;
      }
      sprintf(scale, "SCALE=2**0\n");
      break;
      
    default:
      return -1;
    }
    
    switch(end) {
    case END_LITTLE:
      sprintf(endianness, "decm");
      break;
    case END_BIG:
      sprintf(endianness, "sun");
      break;
    default:
      /* fix architecture endianness */
      if( _getEndianness() == END_LITTLE)
	sprintf(endianness, "decm");
      else
	sprintf(endianness, "sun");
      break;
    }

    /* write header information */
    sprintf(buf, "%s\nXDIM=%i\nYDIM=%i\nZDIM=%i\nVDIM=%d\nTYPE=%s\nPIXSIZE=%i bits\n%sCPU=%s\nVX=%f\nVY=%f\nVZ=%f\n",
	    INR4_MAGIC, im->xdim, im->ydim, im->zdim, im->vdim,
	    type, im->wdim*8, scale, endianness, im->vx, im->vy, im->vz);

    if ( im->cx != 0 ) 
      sprintf(buf+strlen(buf), "XO=%d\n", im->cx );
    if ( im->cy != 0 ) 
      sprintf(buf+strlen(buf), "YO=%d\n", im->cy );
    if ( im->cz != 0 ) 
      sprintf(buf+strlen(buf), "ZO=%d\n", im->cz );
    if ( im->tx != 0.0 ) 
      sprintf(buf+strlen(buf), "TX=%f\n", im->tx );
    if ( im->ty != 0.0 ) 
      sprintf(buf+strlen(buf), "TY=%f\n", im->ty );
    if ( im->tz != 0.0 ) 
      sprintf(buf+strlen(buf), "TZ=%f\n", im->tz );

    if ( im->rx != 0.0 ) 
      sprintf(buf+strlen(buf), "RX=%f\n", im->rx );
    if ( im->ry != 0.0 ) 
      sprintf(buf+strlen(buf), "RY=%f\n", im->ry );
    if ( im->rz != 0.0 ) 
      sprintf(buf+strlen(buf), "RZ=%f\n", im->rz );


    pos = strlen(buf);  
    
    if(ImageIO_write(im, buf, strlen(buf)) == 0) return -1;
    
    
    /* write user strings */
    if ( im->user != NULL ) {
      for(i = 0; i < im->nuser; i++) {
	if ( im->user[i] == NULL ) continue;
	pos += strlen(im->user[i]) + 2;
	if(ImageIO_write(im, "#", 1) == 0) return -1;
	if(ImageIO_write(im, im->user[i], strlen(im->user[i])) == 0) return -1;
	if(ImageIO_write(im, "\n", 1) == 0) return -1;
      }
    }
    /* write end of header */
    pos = pos % 256;
    if(pos > 252) {
      for(i = pos; i < 256; i++)
	if(ImageIO_write(im, "\n", 1) != 1) return -1;
      pos = 0;
    }
    buf[0] = '\0';
    for(i = pos; i < 252; i++) strcat(buf, "\n");
    strcat(buf, "##}\n");
    
    if(ImageIO_write(im, buf, strlen(buf)) == 0) return -1;
    else return 1;
  }

  else return -1;
}