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; }
int writeAnalyze( 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, ".hdr", 4 ) == 0 ) { extLength = 4; } else if ( strncmp( name+length-4, ".img", 4 ) == 0 ) { extLength = 4; } else if ( strncmp( name+length-7, ".img.gz", 7 ) == 0 ) { extLength = 7; } else if ( strncmp( name+length-7, ".hdr.gz", 7 ) == 0 ) { extLength = 7; } strncpy( outputName, name, length-extLength ); if ( strncmp( name+length-7, ".hdr.gz", 7 ) == 0 ) strcpy( outputName+length-extLength, ".hdr.gz" ); else strcpy( outputName+length-extLength, ".hdr" ); _openWriteImage(im, outputName); if( !im->fd ) { fprintf(stderr, "writeAnalyze: error: unable to open file \'%s\'\n", outputName); if ( outputName != NULL ) ImageIO_free( outputName ); return ImageIO_OPENING; } res = writeAnalyzeHeader(im); if ( res < 0 ) { fprintf(stderr, "writeAnalyze: 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, ".img.gz" ); } else { strcpy( outputName+length-extLength, ".img" ); } _openWriteImage(im, outputName); if( !im->fd ) { fprintf(stderr, "writeAnalyze: error: unable to open file \'%s\'\n", outputName); if ( outputName != NULL ) ImageIO_free( outputName ); return ImageIO_OPENING; } res = writeAnalyzeData(im); if (res < 0) { fprintf(stderr, "writeAnalyze: error: unable to write data in \'%s\'\n", outputName ); ImageIO_close( im ); im->fd = NULL; im->openMode = OM_CLOSE; return( res ); } if ( outputName != NULL ) ImageIO_free( outputName ); ImageIO_close( im ); im->fd = NULL; im->openMode = OM_CLOSE; return ( res ); }
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; }
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; }