void ToFPicImageWriter::WritePicFileHeader(FILE* outfile, mitkIpPicDescriptor* pic) { mitkIpUInt4_t len; mitkIpUInt4_t tagsLen; tagsLen = _mitkIpPicTagsSize( pic->info->tags_head ); len = tagsLen + 3 * sizeof(mitkIpUInt4_t) + pic->dim * sizeof(mitkIpUInt4_t); /* write oufile */ if( mitkIpPicEncryptionType(pic) == ' ' ) mitkIpPicFWrite( mitkIpPicVERSION, 1, sizeof(mitkIpPicTag_t), outfile ); else mitkIpPicFWrite( pic->info->version, 1, sizeof(mitkIpPicTag_t), outfile ); mitkIpPicFWriteLE( &len, sizeof(mitkIpUInt4_t), 1, outfile ); mitkIpPicFWriteLE( &(pic->type), sizeof(mitkIpUInt4_t), 1, outfile ); mitkIpPicFWriteLE( &(pic->bpe), sizeof(mitkIpUInt4_t), 1, outfile ); mitkIpPicFWriteLE( &(pic->dim), sizeof(mitkIpUInt4_t), 1, outfile ); mitkIpPicFWriteLE( pic->n, sizeof(mitkIpUInt4_t), pic->dim, outfile ); _mitkIpPicWriteTags( pic->info->tags_head, outfile, mitkIpPicEncryptionType(pic) ); pic->info->pixel_start_in_file = ftell( outfile ); }
mitkIpPicDescriptor * MITKipPicGetTags( char *infile_name, mitkIpPicDescriptor *pic ) { mitkIpPicFile_t infile; mitkIpPicTag_t tag_name; mitkIpUInt4_t dummy; mitkIpUInt4_t len; mitkIpUInt4_t dim; mitkIpUInt4_t n[_mitkIpPicNDIM]; mitkIpUInt4_t to_read; size_t ignored; infile = _mitkIpPicOpenPicFileIn( infile_name ); if( infile == NULL ) { /*ipPrintErr( "mitkIpPicGetTags: sorry, error opening infile\n" );*/ return( NULL ); } if( pic != NULL ) pic->info->write_protect = mitkIpFalse; /* read infile */ ignored = mitkIpPicFRead( &(tag_name[0]), 1, 4, infile ); if( strncmp( mitkIpPicVERSION, tag_name, 4 ) != 0 ) { if( infile != stdin ) mitkIpPicFClose( infile ); return( pic ); } if( pic == NULL ) pic = mitkIpPicNew(); ignored = mitkIpPicFRead( &(tag_name[4]), 1, sizeof(mitkIpPicTag_t)-4, infile ); /*strncpy( pic->info->version, tag_name, _mitkIpPicTAGLEN );*/ ignored = mitkIpPicFReadLE( &len, sizeof(mitkIpUInt4_t), 1, infile ); ignored = mitkIpPicFReadLE( &dummy, sizeof(mitkIpUInt4_t), 1, infile ); ignored = mitkIpPicFReadLE( &dummy, sizeof(mitkIpUInt4_t), 1, infile ); ignored = mitkIpPicFReadLE( &dim, sizeof(mitkIpUInt4_t), 1, infile ); ignored = mitkIpPicFReadLE( n, sizeof(mitkIpUInt4_t), dim, infile ); to_read = len - 3 * sizeof(mitkIpUInt4_t) - dim * sizeof(mitkIpUInt4_t); pic->info->tags_head = _MITKipPicReadTags( pic->info->tags_head, to_read, infile, mitkIpPicEncryptionType(pic) ); pic->info->pixel_start_in_file = mitkIpPicFTell( infile ); if( infile != stdin ) mitkIpPicFClose( infile ); return( pic ); }
mitkIpPicDescriptor * MITKipPicGet( char *infile_name, mitkIpPicDescriptor *pic ) { mitkIpPicFile_t infile; mitkIpPicTag_t tag_name; mitkIpUInt4_t len; mitkIpUInt4_t to_read; size_t size; size_t number_of_elements; size_t bytes_per_element; size_t number_of_bytes; size_t block_size; size_t number_of_blocks; size_t remaining_bytes; size_t bytes_read; size_t block_nr; mitkIpUInt1_t* data; size_t ignored; infile = _mitkIpPicOpenPicFileIn( infile_name ); if( !infile ) { /*ipPrintErr( "mitkIpPicGet: sorry, error opening infile\n" );*/ return( NULL ); } /* read infile */ ignored = mitkIpPicFRead( tag_name, 1, 4, infile ); if( strncmp( "\037\213", tag_name, 2 ) == 0 ) { fprintf( stderr, "mitkIpPicGetHeader: sorry, can't read compressed file\n" ); return( NULL ); } else if( strncmp( mitkIpPicVERSION, tag_name, 4 ) != 0 ) { #ifndef CHILIPLUGIN if( pic == NULL ) pic = _MITKipPicOldGet( infile, NULL ); else _MITKipPicOldGet( infile, pic ); if( infile != stdin ) mitkIpPicFClose( infile ); #else return NULL; #endif return( pic ); } size = 0; if( pic == NULL ) pic = mitkIpPicNew(); else { size= _mitkIpPicSize(pic); if(pic->data == NULL) size = 0; } if( pic->info != NULL ) { _mitkIpPicFreeTags( pic->info->tags_head ); pic->info->tags_head = NULL; } ignored = mitkIpPicFRead( &(tag_name[4]), 1, sizeof(mitkIpPicTag_t)-4, infile ); strncpy( pic->info->version, tag_name, _mitkIpPicTAGLEN ); ignored = mitkIpPicFReadLE( &len, sizeof(mitkIpUInt4_t), 1, infile ); ignored = mitkIpPicFReadLE( &(pic->type), sizeof(mitkIpUInt4_t), 1, infile ); ignored = mitkIpPicFReadLE( &(pic->bpe), sizeof(mitkIpUInt4_t), 1, infile ); ignored = mitkIpPicFReadLE( &(pic->dim), sizeof(mitkIpUInt4_t), 1, infile ); ignored = mitkIpPicFReadLE( &(pic->n), sizeof(mitkIpUInt4_t), pic->dim, infile ); (void *)ignored; to_read = len - 3 * sizeof(mitkIpUInt4_t) - pic->dim * sizeof(mitkIpUInt4_t); #if 0 mitkIpPicFSeek( infile, to_read, SEEK_CUR ); #else pic->info->tags_head = _MITKipPicReadTags( pic->info->tags_head, to_read, infile, mitkIpPicEncryptionType(pic) ); #endif pic->info->write_protect = mitkIpFalse; if((size == 0) || (size != _mitkIpPicSize(pic))) { if( pic->data != NULL ) { free( pic->data ); pic->data = NULL; } #ifdef WIN if ((pic->hdata = GlobalAlloc( GMEM_MOVEABLE, _mitkIpPicSize(pic) )) != 0) pic->data = GlobalLock( pic->hdata ); #else pic->data = malloc( _mitkIpPicSize(pic) ); #endif } pic->info->pixel_start_in_file = mitkIpPicFTell( infile ); /* * data is read in blocks of size 'block_size' to prevent from * errors due to large file sizes (>=2GB) */ number_of_elements = _mitkIpPicElements(pic); bytes_per_element = pic->bpe / 8; number_of_bytes = number_of_elements * bytes_per_element; block_size = 1024*1024; /* Use 1 MB blocks. Make sure that block size is smaller than 2^31 */ number_of_blocks = number_of_bytes / block_size; remaining_bytes = number_of_bytes % block_size; bytes_read = 0; block_nr = 0; /*printf( "mitkIpPicGet: number of blocks to read is %ul.\n", number_of_blocks ); */ data = (mitkIpUInt1_t*) pic->data; if( pic->type == mitkIpPicNonUniform ) { for ( block_nr = 0 ; block_nr < number_of_blocks ; ++block_nr ) bytes_read += mitkIpPicFRead( data + ( block_nr * block_size ), 1, (unsigned int) block_size, infile ); bytes_read += mitkIpPicFRead( data + ( number_of_blocks * block_size ), 1, (unsigned int) remaining_bytes, infile ); } else { for ( block_nr = 0 ; block_nr < number_of_blocks ; ++block_nr ) bytes_read += mitkIpPicFReadLE( data + ( block_nr * block_size ), 1, (unsigned int) block_size, infile ); bytes_read += mitkIpPicFReadLE( data + ( number_of_blocks * block_size ), 1, (unsigned int) remaining_bytes, infile ); } if ( bytes_read != number_of_bytes ) { fprintf( stderr, "Error while reading, only %lu bytes were read! Eof indicator is %u.\n", (long unsigned int)bytes_read, mitkIpPicFEOF(infile) ); #ifndef USE_ZLIB fprintf( stderr, "(ferror indicates %u).\n", ferror(infile)); #endif } if( infile != stdin ) mitkIpPicFClose( infile ); #ifdef WIN GlobalUnlock( pic->hdata ); #endif return( pic ); }
mitkIpPicDescriptor * mitkIpPicGet( const char *infile_name, mitkIpPicDescriptor *pic ) { mitkIpPicFile_t infile; mitkIpPicTag_t tag_name; mitkIpUInt4_t len; mitkIpUInt4_t to_read; infile = _mitkIpPicOpenPicFileIn( infile_name ); if( !infile ) { /*ipPrintErr( "mitkIpPicGet: sorry, error opening infile\n" );*/ return( NULL ); } /* read infile */ mitkIpPicFRead( tag_name, 1, 4, infile ); if( strncmp( "\037\213", tag_name, 2 ) == 0 ) { fprintf( stderr, "mitkIpPicGetHeader: sorry, can't read compressed file\n" ); return( NULL ); } else if( strncmp( mitkIpPicVERSION, tag_name, 4 ) != 0 ) { if( pic == NULL ) pic = _mitkIpPicOldGet( infile, NULL ); else _mitkIpPicOldGet( infile, pic ); if( infile != stdin ) mitkIpPicFClose( infile ); return( pic ); } if( pic == NULL ) pic = mitkIpPicNew(); mitkIpPicClear( pic ); mitkIpPicFRead( &(tag_name[4]), 1, sizeof(mitkIpPicTag_t)-4, infile ); strncpy( pic->info->version, tag_name, _mitkIpPicTAGLEN ); mitkIpPicFReadLE( &len, sizeof(mitkIpUInt4_t), 1, infile ); mitkIpPicFReadLE( &(pic->type), sizeof(mitkIpUInt4_t), 1, infile ); mitkIpPicFReadLE( &(pic->bpe), sizeof(mitkIpUInt4_t), 1, infile ); mitkIpPicFReadLE( &(pic->dim), sizeof(mitkIpUInt4_t), 1, infile ); mitkIpPicFReadLE( &(pic->n), sizeof(mitkIpUInt4_t), pic->dim, infile ); to_read = len - 3 * sizeof(mitkIpUInt4_t) - pic->dim * sizeof(mitkIpUInt4_t); #if 0 mitkIpPicFSeek( infile, to_read, SEEK_CUR ); #else pic->info->tags_head = _mitkIpPicReadTags( pic->info->tags_head, to_read, infile, mitkIpPicEncryptionType(pic) ); #endif pic->info->write_protect = mitkIpFalse; #ifdef WIN if ((pic->hdata = GlobalAlloc( GMEM_MOVEABLE, _mitkIpPicSize(pic) )) != 0) pic->data = GlobalLock( pic->hdata ); #else pic->data = malloc( _mitkIpPicSize(pic) ); #endif pic->info->pixel_start_in_file = mitkIpPicFTell( infile ); if( pic->type == mitkIpPicNonUniform ) mitkIpPicFRead( pic->data, pic->bpe / 8, _mitkIpPicElements(pic), infile ); else mitkIpPicFReadLE( pic->data, pic->bpe / 8, _mitkIpPicElements(pic), infile ); if( infile != stdin ) mitkIpPicFClose( infile ); #ifdef WIN GlobalUnlock( pic->hdata ); #endif return( pic ); }