2024年1月14日发(作者:)

#define TIFF void*#define TIFF_VERSION_CLASSIC 42#define TIFF_BIGENDIAN 0x4d4d#define TIFF_LITTLEENDIAN 0x4949#define PI 3.979323846#define SIZEOF_INT 4/* Signed 8-bit type */#define TIFF_INT8_T signed char/* Unsigned 8-bit type */#define TIFF_UINT8_T unsigned char/* Signed 16-bit type */#define TIFF_INT16_T signed short/* Unsigned 16-bit type */#define TIFF_UINT16_T unsigned short/* Signed 32-bit type formatter */#define TIFF_INT32_FORMAT "%d"/* Signed 32-bit type */#define TIFF_INT32_T signed int/* Unsigned 32-bit type */#define TIFF_UINT32_T unsigned int/* Signed 64-bit type */#define TIFF_INT64_T signed __int64/* Unsigned 64-bit type formatter */#define TIFF_UINT64_FORMAT "%I64u"/* Unsigned 64-bit type */#define TIFF_UINT64_T unsigned __int64/* Signed size type */#if defined(_WIN64)#define TIFF_SSIZE_T signed __int64#else#define TIFF_SSIZE_T signed int#endif/* Signed size type formatter */#if defined(_WIN64)#define TIFF_SSIZE_FORMAT "%I64d"#else#define TIFF_SSIZE_FORMAT "%ld"#endif/* Pointer difference type */#define TIFF_PTRDIFF_T long#define TIFF_ERROR_PATH_SUCCESS 0

#define TIFF_ERROR_PATH_NULL -1#define TIFF_ERROR_OPEN_FAILED -2#define TIFF_ERROR_BYTE_ORDER -3#define TIFF_ERROR_TIFF_VER -4

TIFF_UINT16_T IFDnum = get2( ptiff , byte_order ); int fs = fseek( ptiff ,2+IFDoffset , SEEK_SET ); TIFF_UINT16_T tdir_tag = 0 , tdir_type = 0 ; TIFF_UINT32_T tdir_count = 0 , toff_long = 0 ; for (TIFF_UINT16_T i=0x0000;i< IFDnum ;i++) { TIFF_UINT32_T file_offset = 2+IFDoffset+12*i; int fs = fseek( ptiff , 2+IFDoffset+12*i , SEEK_SET ); tdir_tag = get2( ptiff , byte_order ); if ( tdir_tag == 0x100 )//width { tdir_type = get2( ptiff , byte_order ); tdir_count = get4( ptiff , byte_order ); if ( ( tdir_type == 3 || tdir_type == 4 ) && byte_order == 0x4d4d/*Motor*/ && tdir_count == 1 ) { toff_long = (TIFF_UINT32_T)get2( ptiff , byte_order ); } else { toff_long = get4( ptiff , byte_order ); } tf ->tif_width = toff_long ; continue; } else if ( tdir_tag == 0x101 )//height { tdir_type = get2( ptiff , byte_order ); tdir_count = get4( ptiff , byte_order ); if ( ( tdir_type == 3 || tdir_type == 4 ) && byte_order == 0x4d4d/*Motor*/ && tdir_count == 1 ) { toff_long = (TIFF_UINT32_T)get2( ptiff , byte_order ); } else { toff_long = get4( ptiff , byte_order ); } tf ->tif_height = toff_long ; continue; } else if ( tdir_tag == 0x102 )//BitsPerSample { tdir_type = get2( ptiff , byte_order ); tdir_count = get4( ptiff , byte_order ); if ( tdir_count == 1 ) { if ( ( tdir_type == 3 || tdir_type == 4 ) && byte_order == 0x4d4d/*Motor*/ && tdir_count == 1 ) { toff_long = (TIFF_UINT32_T)get2( ptiff , byte_order ); } else { toff_long = get4( ptiff , byte_order ); } tf -> bit_per_samples = toff_long ; } else

else { toff_long = get4( ptiff , byte_order ); TIFF_UINT16_T* sl = (TIFF_UINT16_T*)get_byte_mem( ptiff , toff_long , tdir_count*sizeof(TIFF_UINT16_T) ); TIFF_UINT16_T* s2 = sl; for ( int i = 0 ; i < (int)tdir_count ; i++ ) { TIFF_UINT32_T s16 = sget2((TIFF_UINT8_T*)(s2 + i) , tf->tiff_byte_order ); tf -> bit_per_samples += s16 ; } } continue; } else if ( tdir_tag == 0x103 )//Compression { tdir_type = get2( ptiff , byte_order ); tdir_count = get4( ptiff , byte_order ); if ( ( tdir_type == 3 || tdir_type == 4 ) && byte_order == 0x4d4d/*Motor*/ && tdir_count == 1 ) { toff_long = (TIFF_UINT32_T)get2( ptiff , byte_order ); } else { toff_long = get4( ptiff , byte_order ); } tf ->compression = toff_long ; continue; } else if ( tdir_tag == 0x106 )//PhotometricInterpretation { tdir_type = get2( ptiff , byte_order ); tdir_count = get4( ptiff , byte_order ); if ( ( tdir_type == 3 || tdir_type == 4 ) && byte_order == 0x4d4d/*Motor*/ && tdir_count == 1 ) { toff_long = (TIFF_UINT32_T)get2( ptiff , byte_order ); } else { toff_long = get4( ptiff , byte_order ); } tf ->photo_metric_inter = toff_long ; continue; } else if ( tdir_tag == 0x111 )//StripOffsets { tdir_type = get2( ptiff , byte_order ); tdir_count = get4( ptiff , byte_order ); toff_long = get4( ptiff , byte_order ); tf ->bcount_strip_offset = tdir_count ; if ( tdir_count > 1 ) { TIFF_UINT32_T* sl = (TIFF_UINT32_T*)get_byte_mem( ptiff , toff_long , tdir_count * sizeof(TIFF_UINT32_T) ); TIFF_UINT32_T* sl2 = sl; //字节序的转换 for ( int i = 0 ; i < (int)tdir_count ; i++ ) { TIFF_UINT32_T s32 = sget4((TIFF_UINT8_T*)sl2 , tf->tiff_byte_order ); memcpy( sl2 , &s32 , 4 ); sl2+=1; } tf ->strip_line = sl ;

tf ->strip_line = sl ; } else { tf ->bcount_strip_offset = 1; tf ->strip_line = new TIFF_UINT32_T; *(tf ->strip_line) = toff_long; } continue; } else if ( tdir_tag == 0x115 )//SamplesPerPixel { tdir_type = get2( ptiff , byte_order ); tdir_count = get4( ptiff , byte_order ); if ( ( tdir_type == 3 || tdir_type == 4 ) && byte_order == 0x4d4d/*Motor*/ && tdir_count == 1 ) { toff_long = (TIFF_UINT32_T)get2( ptiff , byte_order ); } else { toff_long = get4( ptiff , byte_order ); } tf ->samples_per_pixel = toff_long ; continue; } else if ( tdir_tag == 0x116 )//RowsPerStrip { tdir_type = get2( ptiff , byte_order ); tdir_count = get4( ptiff , byte_order ); if ( ( tdir_type == 3 || tdir_type == 4 ) && byte_order == 0x4d4d/*Motor*/ && tdir_count == 1 ) { toff_long = (TIFF_UINT32_T)get2( ptiff , byte_order ); } else { toff_long = get4( ptiff , byte_order ); } tf ->rows_per_strip = toff_long; continue; } else if ( tdir_tag == 0x117 )//StripByteCounts { tdir_type = get2( ptiff , byte_order ); tdir_count = get4( ptiff , byte_order ); toff_long = get4( ptiff , byte_order ); tf ->bcount_strip_byte_counts = tdir_count ; if ( tf ->bcount_strip_byte_counts > 1 ) { TIFF_UINT32_T* sl = (TIFF_UINT32_T*)get_byte_mem( ptiff , toff_long , tdir_count*sizeof(TIFF_UINT32_T) ); TIFF_UINT32_T* sl2 = sl; //字节序的转换 for ( int i = 0 ; i < (int)tdir_count ; i++ ) { TIFF_UINT32_T s32 = sget4((TIFF_UINT8_T*)sl2 , tf->tiff_byte_order ); memcpy( sl2 , &s32 , 4 ); sl2+=1; } tf ->strip_byte_counts = sl ; } else { tf ->bcount_strip_byte_counts = 1;

tf ->bcount_strip_byte_counts = 1; tf ->strip_byte_counts = new TIFF_UINT32_T; *(tf ->strip_byte_counts) = toff_long; } continue; } else if ( tdir_tag == 0x11c )//Planar Configuration { tdir_type = get2( ptiff , byte_order ); tdir_count = get4( ptiff , byte_order ); if ( ( tdir_type == 3 || tdir_type == 4 ) && byte_order == 0x4d4d/*Motor*/ && tdir_count == 1 ) { toff_long = (TIFF_UINT32_T)get2( ptiff , byte_order ); } else { toff_long = get4( ptiff , byte_order ); } tf->planar_config = toff_long ; continue; } else if ( tdir_tag== 0x140 )//Color map { tdir_type = get2( ptiff , byte_order ); tdir_count = get4( ptiff , byte_order ); toff_long = get4( ptiff , byte_order ); int pow1 = 0; if ( tf ->bit_per_samples == 1 ) { pow1 = 1<<1; } else if ( tf ->bit_per_samples == 4 ) { pow1 = 1<<4; } else if ( tf ->bit_per_samples == 8 ) { pow1 = 1<<8; } tf ->color_map = (TIFF_UINT16_T*)get_byte_mem( ptiff , toff_long , 3*pow1*2 ); } else if ( tdir_tag== 0x142 )//tile width { tf ->_tile = true ; tdir_type = get2( ptiff , byte_order ); tdir_count = get4( ptiff , byte_order ); toff_long = get4( ptiff , byte_order ); tf ->_width = toff_long ; continue; } else if ( tdir_tag== 0x143 )//tile height { tdir_type = get2( ptiff , byte_order ); tdir_count = get4( ptiff , byte_order ); toff_long = get4( ptiff , byte_order ); tf ->_height = toff_long ; continue; } else if ( tdir_tag== 0x144 )//tile offset

else if ( tdir_tag== 0x144 )//tile offset { tdir_type = get2( ptiff , byte_order ); tdir_count = get4( ptiff , byte_order ); toff_long = get4( ptiff , byte_order ); tf ->_offset_count = tdir_count ; if ( tdir_count > 1 ) { tf ->_offset_list = (TIFF_UINT32_T*)get_byte_mem( ptiff , toff_long , tdir_count*sizeof(TIFF_UINT32_T) ); } continue; } else if ( tdir_tag== 0x145 )//tile byte count { tdir_type = get2( ptiff , byte_order ); tdir_count = get4( ptiff , byte_order ); toff_long = get4( ptiff , byte_order ); tf ->_byte_num_count = tdir_count ; if ( tdir_count > 1 ) { tf ->_byte_num_list = (TIFF_UINT32_T*)get_byte_mem( ptiff , toff_long , tdir_count*sizeof(TIFF_UINT32_T) ); } continue; } else if ( tdir_tag == 33550 ) //ModelPixelScaleTag { tf -> geo__geotiff = true ; tdir_type = get2( ptiff , byte_order ); tdir_count = get4( ptiff , byte_order ); toff_long = get4( ptiff , byte_order ); if ( tdir_count == 3 && tdir_type == 12 ) { TIFF_UINT8_T* ps = (TIFF_UINT8_T*)get_byte_mem( ptiff , toff_long , tdir_count*8*sizeof(TIFF_UINT8_T) ); tf -> geo__ = long_to_double( sget8( ps , byte_order)) ; tf -> geo__ = long_to_double( sget8( ps + 8 , byte_order)) ; tf -> geo__ = long_to_double( sget8( ps + 16 , byte_order)) ; delete[] ps; ps = NULL; } continue; } else if ( tdir_tag == 33922 ) //ModelTiePointTag { tf -> geo__geotiff = true ; tdir_type = get2( ptiff , byte_order ); tdir_count = get4( ptiff , byte_order ); toff_long = get4( ptiff , byte_order ); if ( tdir_type == 12 && tdir_count >= 6 ) { TIFF_UINT8_T* ps = (TIFF_UINT8_T*)get_byte_mem( ptiff , toff_long , tdir_count*8*sizeof(TIFF_UINT8_T) ); tf -> geo__ = long_to_double( sget8( ps , byte_order)) ; tf -> geo__ = long_to_double( sget8( ps +8 , byte_order)) ; tf -> geo__ = long_to_double( sget8( ps + 16 , byte_order)) ; tf -> geo__ = long_to_double( sget8( ps + 24, byte_order)) ; tf -> geo__ = long_to_double( sget8( ps + 32 , byte_order)) ; tf -> geo__ = long_to_double( sget8( ps + 40 , byte_order)) ;