matlab拼接tiff文件_TIFF图像文件格式详解

这篇博客介绍了如何在MATLAB中拼接TIFF文件,详细讲解了TIFF图像文件的各个关键标签,如ImageWidth、Compression、PhotometricInterpretation等,并提供了相关数据类型的定义。同时,文章还涉及到地理信息相关的GeoKey,如Popular Visualisation CRS/Mercator的设置,以及GeoTIFF的参数存储,包括GeoDoubleParamsTag和GeoAsciiParamsTag的内容。通过示例代码展示了处理TIFF文件和转换坐标的过程。
摘要由CSDN通过智能技术生成

#include "tiffDeform.h"TagText tag_text_list[]={

{254 , "NewSubfileType"},

{256 , "ImageWidth"},

{257 , "ImageLength"},

{258 , "BitsPerSample"},

{259 , "Compression"},

{262 , "PhotometricInterpretation"},

{273 , "StripOffsets"},

{274 , "相对于图像的行和列的方向"},

{277 , "SamplesPerPixel"},

{278 , "RowsPerStrip"},

{279 , "StripByteCounts"},

{282 , "XResolution"},

{283 , "YResolution"},

{284 , "PlanarConfiguration"},

{296 , "ResolutionUnit"},

{305 , "Software"},

{306 , "DateTime"},

{322 , "TileWidth"},

{323 , "TileLength"},

{324 , "TileOffsets"},

{325 , "TileByteCounts"},

{339 , "SampleFormat"},

{33550 , "ModelPixelScaleTag"},

{33922 , "ModelTiepointTag"},

{34264 , "ModelTransformationTag"},

{34735 , "GeoKeyDirectoryTag"},

{34736 , "GeoDoubleParamsTag"},

{34737 , "GeoAsciiParamsTag"},

{-1 , ""}

};

DataType data_type_list[]={

{0 , "NULL" , 0 },//NULL

{ 1 , "BYTE" , 1 },//BYTE 8-bit unsigned integer

{ 2 , "ASCII" , 1 },//ASCII 8-bit byte that contains a 7-bit ASCII code; the last byte must be NUL (binary zero)

{ 3 , "SHORT" , 2 },//SHORT 16-bit (2-byte) unsigned integer

{ 4 , "LONG" , 4 },//LONG 32-bit (4-byte) unsigned integer

{ 5 , "RATIONAL" , 8 },//RATIONAL Two LONGs: the first represents the numerator

{ 6 , "SBYTE" , 1 },//SBYTE An 8-bit signed (twos-complement) integer.

{ 7 , "UNDEFINED", 1 },//UNDEFINED An 8-bit byte that may contain anything, depending on the definition of the field.

{ 8 , "SSHORT" , 2 },//SSHORT A 16-bit (2-byte) signed (twos-complement) integer

{ 9 , "SLONG" , 4 },//SLONG A 32-bit (4-byte) signed (twos-complement) integer

{ 10 , "SRATIONAL", 8 },//SRATIONAL Two SLONG’s: the first represents the numerator of afraction, the second the denominator

{ 11 , "FLOAT" , 4 },//FLOAT Single precision (4-byte) IEEE format

{ 12 , "DOUBLE" , 8 } //DOUBLE Double precision (8-byte) IEEE format.

};/*Geo Key : 1024 = 1

Geo Key : 1025 = 1

Geo Key : 1026 = Popular Visualisation CRS / Mercator

Geo Key : 2048 = 32767

Geo Key : 2049 = Popular Visualisation CRS

Geo Key : 2050 = 32767

Geo Key : 2054 = 9102

Geo Key : 2056 = 32767

Geo Key : 2057 = 6378137.000000

Geo Key : 2058 = 6378137.000000

Geo Key : 3072 = 32767

Geo Key : 3074 = 32767

Geo Key : 3075 = 7

Geo Key : 3076 = 9001

Geo Key : 3080 = 0.000000

Geo Key : 3081 = 0.000000

Geo Key : 3082 = 0.000000

Geo Key : 3083 = 0.000000

Geo Key : 3092 = 1.000000*/unsignedchar geotiff_web_mercator_tag_dir[]={/*目录版本号(2 bytes),修订版本号(2 bytes),副版本号(2 bytes),地理键的个数(2 bytes)*/

0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x13, 0x00, // /*地理键ID(2 bytes),存放位置(2 bytes),元素的个数(2 bytes),值/索引(2 bytes)*/

0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00,0x01, 0x04, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00,0x02, 0x04, 0xb1, 0x87, 0x25, 0x00, 0x00, 0x00,0x00, 0x08, 0x00, 0x00, 0x01, 0x00, 0xff, 0x7f,0x01, 0x08, 0xb1, 0x87, 0x1a, 0x00, 0x25, 0x00,0x02, 0x08, 0x00, 0x00, 0x01, 0x00, 0xff, 0x7f,0x06, 0x08, 0x00, 0x00, 0x01, 0x00, 0x8e, 0x23,0x08, 0x08, 0x00, 0x00, 0x01, 0x00, 0xff, 0x7f,0x09, 0x08, 0xb0, 0x87, 0x01, 0x00, 0x05, 0x00,0x0a, 0x08, 0xb0, 0x87, 0x01, 0x00, 0x06, 0x00,0x00, 0x0c, 0x00, 0x00, 0x01, 0x00, 0xff, 0x7f,0x02, 0x0c, 0x00, 0x00, 0x01, 0x00, 0xff, 0x7f,0x03, 0x0c, 0x00, 0x00, 0x01, 0x00, 0x07, 0x00,0x04, 0x0c, 0x00, 0x00, 0x01, 0x00, 0x29, 0x23,0x08, 0x0c, 0xb0, 0x87, 0x01, 0x00, 0x01, 0x00,0x09, 0x0c, 0xb0, 0x87, 0x01, 0x00, 0x00, 0x00,0x0a, 0x0c, 0xb0, 0x87, 0x01, 0x00, 0x03, 0x00,0x0b, 0x0c, 0xb0, 0x87, 0x01, 0x00, 0x04, 0x00,0x14, 0x0c, 0xb0, 0x87, 0x01, 0x00, 0x02, 0x00};

unsignedchar geotiff_double_param[]={0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , //0.0

0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , //0.0

0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0xf0 , 0x3f , //1.0

0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , //0.0

0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , //0.0

0x00 , 0x00 , 0x00 , 0x40 , 0xa6 , 0x54 , 0x58 , 0x41 , //6378137.0000000000

0x00 , 0x00 , 0x00 , 0x40 , 0xa6 , 0x54 , 0x58 , 0x41 //6378137.0000000000

};

unsignedchar geotiff_ascii_param[]= /*Popular Visualisation CRS / Mercator|Popular Visualisation CRS|*/{0x50 , 0x6f , 0x70 , 0x75 , 0x6c , 0x61 , 0x72 , 0x20,0x56 , 0x69 , 0x73 , 0x75 , 0x61 , 0x6c , 0x69 , 0x73,0x61 , 0x74 , 0x69 , 0x6f , 0x6e , 0x20 , 0x43 , 0x52,0x53 , 0x20 , 0x2f , 0x20 , 0x4d , 0x65 , 0x72 , 0x63,0x61 , 0x74 , 0x6f , 0x72 , 0x7c , 0x50 , 0x6f , 0x70,0x75 , 0x6c , 0x61 , 0x72 , 0x20 , 0x56 , 0x69 , 0x73,0x75 , 0x61 , 0x6c , 0x69 , 0x73 , 0x61 , 0x74 , 0x69,0x6f , 0x6e , 0x20 , 0x43 , 0x52 , 0x53 , 0x7c , 0x00};

tiffDeform::tiffDeform()

{

_tiff_file=NULL;

_tiff_file= newTiffFile;

memset( _tiff_file ,0 , sizeof(TiffFile) );

}

tiffDeform::tiffDeform(string tiff_path , stringproj4_str )

{

_tiff_path=tiff_path ;

_proj4_str=proj4_str ;

_tiff_file= newTiffFile;

memset( _tiff_file ,0 , sizeof(TiffFile) );

}

tiffDeform::~tiffDeform()

{if( _tiff_file ==NULL )

{delete_tiff_file;

_tiff_file=NULL;

}

}booltiffDeform::arrangement()

{return _tiff_file->tile.is_tile;

}int tiffDeform::open( string tiff_path , string proj4_str , intcoord_type )

{

_tiff_path=tiff_path ;

_proj4_str=proj4_str ;

_coord_type=coord_type;returntif_open ( tiff_path.c_str() , _tiff_file );

}void tiffDeform::save( string src_file , stringsave_name )

{delete_tiff_file;

_tiff_file=NULL;

_tiff_path=src_file;

_tiff_file= newTiffFile;

memset( _tiff_file ,0 , sizeof(TiffFile) );

tif_open( _tiff_path.c_str() , _tiff_file );//1.获得原始图片的范围坐标

src_coord_box( );//2.计算转换为WebMector的坐标范围

_geo_rect_des =_geo_rect_src;

_geo_rect_des.left=zone_number( _geo_rect_des.left ) ;

_geo_rect_des.right=zone_number( _geo_rect_des.right ) ;doublex1,y1,x2,y2,x3,y3,x4,y4;

to_web_mector( _geo_rect_des ,true,x1,y1,x2,y2,x3,y3,x4,y4 );//3.获得像元比例

get_pixel_scale();//4.计算新生成的图片的长度和宽度

_new_tiff_width = (int)(( _geo_rect_des.right - _geo_rect_des.left )/_scaleX_des + 0.5);

_new_tiff_height= (int)(( _geo_rect_des.top - _geo_rect_des.botton )/_scaleY_des + 0.5);//5.获得原图片的TAG列表

get_src_tag_list();//6.构造新的TAG列表

cts_new_tag_list();stringtemp_path;if ( save_name == "")

{

temp_path=new_tiff_name() ;

}else{

temp_path=save_name;

}

_line_tiff= fopen( temp_path.c_str() , "wb");if ( _line_tiff ==NULL )

{return;

}//7.写入文件头

write_file_header( );//8.写入tag的数量

fwrite( &( _de_num ) , 1 , 2, _line_tiff );//9.写入空的DE占位空间

TIFF_UINT8_T* place_holder = new TIFF_UINT8_T[ _de_num * ONE_DE_SIZE +IDF_END_FLAG_SIZE ];

memset( place_holder ,0 , _de_num * ONE_DE_SIZE +IDF_END_FLAG_SIZE );

fwrite( place_holder ,1 , _de_num * ONE_DE_SIZE +IDF_END_FLAG_SIZE , _line_tiff );

TIFF_UINT64_T write_file_size=ftell( _line_tiff );//10.写入具体的TAG内容和对应的偏移量

write_tag_list();//11.修改图像数据的偏移量

modify_strip_offset();//12.写入图像数据//write_img_data();//write_img_data_ex();//write_img_data_th();//write_img_data_th_ex();//write_img_data_mul_th( );

write_img_by_record();//write_img_by_sin();//write_img_by_block_record();//13.关闭文件//Sleep( 1000 );

fclose( _line_tiff );

}voidtiffDeform::write_img_data()

{

TIFF_UINT32_T* height_start = new TIFF_UINT32_T[ _new_tiff_height * sizeof(TIFF_UINT32_T) ];

memset( height_start ,0 , _new_tiff_height * sizeof(TIFF_UINT32_T) );int bits = _tiff_file->bit_per_samples ;if ( bits >= 24)

{if ( !_tiff_file->tile.is_tile )//不是瓦片数据

{

fresh_line_start( _tiff_file , height_start );

}

}

fseek( _line_tiff ,0, SEEK_END );

_coord_trans.init_proj( coord_google , _proj4_str.c_str() );for ( int i_height = 0 ; i_height < _new_tiff_height ; i_height++)

{

system("cls");

printf("%s \n Coord Trans %d / %d \n" , _tiff_path.c_str() , i_height + 1, _new_tiff_height );for ( int i_width = 0 ; i_width < _new_tiff_width ; i_width++)

{double new_y =_geo_rect_des.top;

new_y-= ( i_height *_scaleY_des );double new_x =_geo_rect_des.left;

new_x+= ( i_width *_scaleX_des );if ( _coord_type == 0)

{

_coord_trans.xy_2_latlong( new_x , new_y );

}else{

_coord_trans.xy_2_xy( new_x , new_y );

}

new_x=src_geo_xy( new_x );int pixel_x = 0 , pixel_y = 0;

point_to_by_geo( new_x , new_y , pixel_x , pixel_y );

TIFF_UINT8_T* buf = new TIFF_UINT8_T[ _tiff_file->samples_per_pixel ];

memset( buf ,0 , _tiff_file->samples_per_pixel );if ( !( pixel_y == -1 || pixel_x == -1) )

{

TIFF_UINT32_T pos= height_start[pixel_y] + pixel_x*_tiff_file->samples_per_pixel;

fseek( _tiff_file->pfile , height_start[pixel_y] + pixel_x*_tiff_file->samples_per_pixel , SEEK_SET );

fread( buf , _tiff_file->samples_per_pixel , 1 , _tiff_file->pfile );

fwrite( buf , _tiff_file->samples_per_pixel , 1, _line_tiff );

}else{

fwrite( buf , _tiff_file->samples_per_pixel , 1, _line_tiff );

}delete[] buf;

buf=NULL;

}

}

}voidtiffDeform::write_img_data_ex()

{

TIFF_UINT32_T* height_start = new TIFF_UINT32_T[ _new_tiff_height * sizeof(TIFF_UINT32_T) ];

memset( height_start ,0 , _new_tiff_height * sizeof(TIFF_UINT32_T) );int bits = _tiff_file->bit_per_samples ;if ( bits >= 24)

{if ( !_tiff_file->tile.is_tile )//不是瓦片数据

{

fresh_line_start( _tiff_file , height_start );

}

}

fseek( _line_tiff ,0, SEEK_END );

_coord_trans.init_proj( coord_google , _proj4_str.c_str() );

printf("转换中请稍后... \n");for ( int i_height = 0 ; i_height < _new_tiff_height ; i_height++)

{//system("cls");//printf( " %s \n Coord Trans %d / %d \n" , _tiff_path.c_str() , i_height + 1 , _new_tiff_height );

TIFF_UINT8_T* buf = new TIFF_UINT8_T[ _tiff_file->samples_per_pixel *_new_tiff_width ];

memset( buf ,0 , _tiff_file->samples_per_pixel *_new_tiff_width );

TIFF_UINT8_T* temp_buf =buf ;for ( int i_width = 0 ; i_width < _new_tiff_width ; i_width++)

{double new_y =_geo_rect_des.top;

new_y-= ( i_height *_scaleY_des );double new_x =_geo_rect_des.left;

new_x+= ( i_width *_scaleX_des );if ( _coord_type == 0)

{

_coord_trans.xy_2_latlong( new_x , new_y );

}else{

_coord_trans.xy_2_xy( new_x , new_y );

}

new_x= src_geo_xy( new_x );//添加带号

int pixel_x = 0 , pixel_y = 0;

point_to_by_geo( new_x , new_y , pixel_x , pixel_y );//TIFF_UINT8_T* buf = new TIFF_UINT8_T[ _tiff_file->samples_per_pixel ];//memset( buf , 0 , _tiff_file->samples_per_pixel );

if ( !( pixel_y == -1 || pixel_x == -1) )

{

TIFF_UINT32_T pos= height_start[pixel_y] + pixel_x*_tiff_file->samples_per_pixel;

fseek( _tiff_file->pfile , height_start[pixel_y] + pixel_x*_tiff_file->samples_per_pixel , SEEK_SET );

fread( temp_buf , _tiff_file->samples_per_pixel , 1 , _tiff_file->pfile );//fwrite( buf , _tiff_file->samples_per_pixel , 1 , _line_tiff );

temp_buf += _tiff_file->samples_per_pixel ;

}else{//fwrite( buf , _tiff_file->samples_per_pixel , 1 , _line_tiff );

temp_buf += _tiff_file->samples_per_pixel ;

}//delete[] buf;//buf = NULL;

}

fwrite( buf , _tiff_file->samples_per_pixel * _new_tiff_width , 1, _line_tiff );delete[] buf;

buf=NULL;

}

printf("转换完成! \n");

}void tiffDeform::to_src_proj_first( geoRECT&des_geo , bool b , double&first_x , double&first_y )

{/*(1) (2)

+-----+

| |

| |

+-----+

(3) (4)*/

//_coord_trans.init_proj( coord_google , _proj4_str.c_str() );

doublex1,y1,x2,y2,x3,y3,x4,y4;

x1= des_geo.left; y1 =des_geo.top;

x2= des_geo.right; y2 =des_geo.top;

x3= des_geo.left ; y3 =des_geo.botton;

x4= des_geo.right; y4 =des_geo.botton;if ( _coord_type == 0)

{

_coord_trans.xy_2_latlong( x1 , y1 );

}else{

_coord_trans.xy_2_xy( x1 , y1 );

}

first_x=x1 ;

first_y=y1 ;if ( _coord_type == 0)

{

_coord_trans.xy_2_latlong( x2 , y2 );

}else{

_coord_trans.xy_2_xy( x2 , y2 );

}if ( _coord_type == 0)

{

_coord_trans.xy_2_latlong( x3 , y3 );

}else{

_coord_trans.xy_2_xy( x3 , y3 );

}if ( _coord_type == 0)

{

_coord_trans.xy_2_latlong( x4 , y4 );

}else{

_coord_trans.xy_2_xy( x4 , y4 );

}if( b )

{

des_geo.left= x1 < x3 ?x3 : x1;

des_geo.top= y1 > y2 ?y2 : y1;

des_geo.right= x2 > x4 ?x4 : x2;

des_geo.botton= y3 < y4 ?y4 : y3;

}else{

des_geo.left= x1 > x3 ?x3 : x1;

des_geo.top= y1 < y2 ?y2 : y1;

des_geo.right= x2 < x4 ?x4 : x2;

des_geo.botton= y3 > y4 ?y4 : y3;

}

}void tiffDeform::to_src_proj( geoRECT&des_geo , boolb )

{/*(1) (2)

+-----+

| |

| |

+-----+

(3) (4)*/

//_coord_trans.init_proj( coord_google , _proj4_str.c_str() );

doublex1,y1,x2,y2,x3,y3,x4,y4;

x1= des_geo.left; y1 =des_geo.top;

x2= des_geo.right; y2 =des_geo.top;

x3= des_geo.left ; y3 =des_geo.botton;

x4= des_geo.right; y4 =des_geo.botton;if ( _coord_type == 0)

{

_coord_trans.xy_2_latlong( x1 , y1 );

}else{

_coord_trans.xy_2_xy( x1 , y1 );

}if ( _coord_type == 0)

{

_coord_trans.xy_2_latlong( x2 , y2 );

}else{

_coord_trans.xy_2_xy( x2 , y2 );

}if ( _coord_type == 0)

{

_coord_trans.xy_2_latlong( x3 , y3 );

}else{

_coord_trans.xy_2_xy( x3 , y3 );

}if ( _coord_type == 0)

{

_coord_trans.xy_2_latlong( x4 , y4 );

}else{

_coord_trans.xy_2_xy( x4 , y4 );

}if( b )

{

des_geo.left= x1 < x3 ?x3 : x1;

des_geo.top= y1 > y2 ?y2 : y1;

des_geo.right= x2 > x4 ?x4 : x2;

des_geo.botton= y3 < y4 ?y4 : y3;

}else{

des_geo.left= x1 > x3 ?x3 : x1;

des_geo.top= y1 < y2 ?y2 : y1;

des_geo.right= x2 < x4 ?x4 : x2;

des_geo.botton= y3 > y4 ?y4 : y3;

}

}void tiffDeform::des_geo_to_src_range( geoRECT des_geo , geoRECT&src_range )

{

to_src_proj( des_geo ,false);

src_range.left= ( des_geo.left - _geo_rect_src.left ) / _scaleX_src - 0.5;if ( src_range.left < 0)

{//src_range.left = 0 ;

}

src_range.right= ( des_geo.right - _geo_rect_src.left ) / _scaleX_src + 0.5;if ( src_range.right > _tiff_file->tif_width )

{//src_range.right = _tiff_file->tif_width ;

}

src_range.top= ( _geo_rect_src.top - des_geo.top ) / _scaleY_src - 0.5;if ( src_range.top < 0)

{

src_range.top= 0;

}

src_range.botton= ( _geo_rect_src.top - des_geo.botton ) / _scaleY_src + 0.5;if ( src_range.botton > _tiff_file->tif_height )

{

src_range.botton= _tiff_file->tif_height ;

}

}void tiffDeform::des_geo_to_src_range_first( geoRECT des_geo , geoRECT&src_range , int&first_x , int&first_y )

{double fx = 0.0 , fy = 0.0;

to_src_proj_first( des_geo ,false, fx , fy );

first_x= (int)(( fx - _geo_rect_src.left ) / _scaleX_src - 0.5) ;

first_y= (int)(( _geo_rect_src.top - fy ) / _scaleY_src - 0.5);

src_range.left= ( des_geo.left - _geo_rect_src.left ) / _scaleX_src - 0.5;if ( src_range.left < 0)

{

src_range.left= 0;

}

src_range.right= ( des_geo.right - _geo_rect_src.left ) / _scaleX_src + 0.5;if ( src_range.right > _tiff_file->tif_width )

{

src_range.right= _tiff_file->tif_width ;

}

src_range.top= ( _geo_rect_src.top - des_geo.top ) / _scaleY_src - 0.5;if ( src_range.top < 0)

{

src_range.top= 0;

}

src_range.botton= ( _geo_rect_src.top - des_geo.botton ) / _scaleY_src + 0.5;if ( src_range.botton > _tiff_file->tif_height )

{

src_range.botton= _tiff_file->tif_height ;

}

}voidtiffDeform::write_img_data_th()

{

TIFF_UINT32_T* height_start = new TIFF_UINT32_T[ _new_tiff_height * sizeof(TIFF_UINT32_T) ];

memset( height_start ,0 , _new_tiff_height * sizeof(TIFF_UINT32_T) );int bits = _tiff_file->bit_per_samples ;if ( bits >= 24)

{if ( !_tiff_file->tile.is_tile )//不是瓦片数据

{

fresh_line_start( _tiff_file , height_start );

}

}

fseek( _line_tiff ,0, SEEK_END );

printf("转换中请稍后... \n");

_coord_trans.init_proj( coord_google , _proj4_str.c_str() );for ( int i_height = 0 ; i_height < _new_tiff_height ; i_height++)

{double line_y =_geo_rect_des.top;

line_y-= ( i_height *_scaleY_des );

geoRECT new_line_rect ;

new_line_rect.left=_geo_rect_des.left ;

new_line_rect.right=_geo_rect_des.right ;

new_line_rect.top=line_y ;

new_line_rect.botton=line_y ;

geoRECT src_range;

des_geo_to_src_range( new_line_rect , src_range );int src_top = ( int)src_range.top;int src_bottom = ( int)src_range.botton;

TIFF_UINT32_T src_buf_size= _tiff_file->samples_per_pixel * _tiff_file->tif_width * ( src_bottom - src_top + 1);

TIFF_UINT8_T* src_buf = newTIFF_UINT8_T[ src_buf_size ];

memset( src_buf ,0, src_buf_size );

fseek( _tiff_file->pfile , height_start[src_top] , SEEK_SET );

fread( src_buf ,src_buf_size ,1 , _tiff_file->pfile );

TIFF_UINT8_T* buf = new TIFF_UINT8_T[ _tiff_file->samples_per_pixel *_new_tiff_width ];

memset( buf ,0 , _tiff_file->samples_per_pixel *_new_tiff_width );

TIFF_UINT8_T* temp_buf =buf ;for ( int i_width = 0 ; i_width < _new_tiff_width ; i_width++)

{double new_y =_geo_rect_des.top;

new_y-= ( i_height *_scaleY_des );double new_x =_geo_rect_des.left;

new_x+= ( i_width *_scaleX_des );#if 1

if ( _coord_type == 0)

{

_coord_trans.xy_2_latlong( new_x , new_y );

}else{

_coord_trans.xy_2_xy( new_x , new_y );

}

new_x= src_geo_xy( new_x );//添加带号

int pixel_x = 0 , pixel_y = 0;

point_to_by_geo( new_x , new_y , pixel_x , pixel_y );if ( !( pixel_y == -1 || pixel_x == -1) )

{

memcpy ( temp_buf

, src_buf+ ( pixel_y - src_top ) * _tiff_file->samples_per_pixel * _tiff_file->tif_width + pixel_x * _tiff_file->samples_per_pixel

, _tiff_file->samples_per_pixel );

temp_buf+= _tiff_file->samples_per_pixel ;

}else{

temp_buf+= _tiff_file->samples_per_pixel ;

}#else

int pixel_x = 0 , pixel_y = 0;if ( !( pixel_y == -1 || pixel_x == -1) )

{

temp_buf+= _tiff_file->samples_per_pixel ;

}else{

temp_buf+= _tiff_file->samples_per_pixel ;

}#endif}

fwrite( buf , _tiff_file->samples_per_pixel * _new_tiff_width , 1, _line_tiff );delete[] buf;

buf=NULL;delete[] src_buf;

src_buf=NULL ;

}

printf("转换完成! \n");

}voidtiffDeform::write_img_data_th_ex()

{

TIFF_UINT32_T* height_start = new TIFF_UINT32_T[ _new_tiff_height * sizeof(TIFF_UINT32_T) ];

memset( height_start ,0 , _new_tiff_height * sizeof(TIFF_UINT32_T) );int bits = _tiff_file->bit_per_samples ;if ( bits >= 24)

{if ( !_tiff_file->tile.is_tile )//不是瓦片数据

{

fresh_line_start( _tiff_file , height_start );

}

}

fseek( _line_tiff ,0, SEEK_END );

printf("转换中请稍后... \n");

TIFF_UINT8_T* buf = new TIFF_UINT8_T[ _tiff_file->samples_per_pixel *_new_tiff_width ];

memset( buf ,0 , _tiff_file->samples_per_pixel *_new_tiff_width );

_coord_trans.init_proj( coord_google , _proj4_str.c_str() );

geoRECT new_line_rect ;

new_line_rect.left=_geo_rect_des.left ;

new_line_rect.right=_geo_rect_des.right ;

TIFF_UINT32_T LineBytes= _tiff_file->samples_per_pixel * _tiff_file->tif_width;

FILE* pf = fopen( "1.txt" , "w");char* pwrite = new char[128];int first_x = 0 , first_y = 0;for ( int i_height = 0 ; i_height < _new_tiff_height ; i_height++)

{double line_y =_geo_rect_des.top;

line_y-= ( i_height *_scaleY_des );

new_line_rect.top=line_y ;

new_line_rect.botton=line_y ;

geoRECT src_range;

des_geo_to_src_range( new_line_rect , src_range );int src_top = ( int)src_range.top;int src_bottom = ( int)src_range.botton;

TIFF_UINT32_T src_buf_size= LineBytes * ( src_bottom - src_top + 1);

TIFF_UINT8_T* src_buf = newTIFF_UINT8_T[ src_buf_size ];

memset( src_buf ,0, src_buf_size );

fseek( _tiff_file->pfile , height_start[src_top] , SEEK_SET );

fread( src_buf ,src_buf_size ,1 , _tiff_file->pfile );

TIFF_UINT8_T* temp_buf =buf ;

memset( pwrite ,0 , 128);

sprintf( pwrite ,"第 %d 行\n", i_height );

fwrite( pwrite , strlen(pwrite) ,1, pf );for ( int i_width = 0 ; i_width < _new_tiff_width ; i_width++)

{double new_y =_geo_rect_des.top;

new_y-= ( i_height *_scaleY_des );double new_x =_geo_rect_des.left;

new_x+= ( i_width *_scaleX_des );if ( _coord_type == 0)

{

_coord_trans.xy_2_latlong( new_x , new_y );

}else{

_coord_trans.xy_2_xy( new_x , new_y );

}

new_x= src_geo_xy( new_x );//添加带号

int pixel_x = 0 , pixel_y = 0;

point_to_by_geo( new_x , new_y , pixel_x , pixel_y );if ( i_width == 0)

{

first_x=pixel_x;

first_y=pixel_y;

}

memset( pwrite ,0 , 128);

sprintf( pwrite ,"(%d,%d)" , pixel_x - first_x , pixel_y -first_y );

fwrite( pwrite , strlen(pwrite) ,1, pf );if ( !( pixel_y == -1 || pixel_x == -1) )

{

memcpy ( temp_buf

, src_buf+ ( pixel_y - src_top ) * LineBytes + pixel_x * _tiff_file->samples_per_pixel

, _tiff_file->samples_per_pixel );

temp_buf+= _tiff_file->samples_per_pixel ;

}else{

temp_buf+= _tiff_file->samples_per_pixel ;

}

}

fwrite( buf , _tiff_file->samples_per_pixel * _new_tiff_width , 1, _line_tiff );

fwrite("\n" , 1 , 1, pf );delete[] src_buf;

src_buf=NULL ;

}delete[] buf;

buf=NULL;

printf("转换完成! \n");

}voidtiffDeform::write_img_data_mul_th()

{

_height_start= new TIFF_UINT32_T[ _new_tiff_height * sizeof(TIFF_UINT32_T) ];

memset( _height_start ,0 , _new_tiff_height * sizeof(TIFF_UINT32_T) );int bits = _tiff_file->bit_per_samples ;if ( bits >= 24)

{if ( !_tiff_file->tile.is_tile )//不是瓦片数据

{

fresh_line_start( _tiff_file , _height_start );

}

}

fseek( _line_tiff ,0, SEEK_END );

_file_size=ftell( _line_tiff );

printf("转换中请稍后... \n");

TIFF_UINT8_T* buf = new TIFF_UINT8_T[ _tiff_file->samples_per_pixel *_new_tiff_width ];

memset( buf ,0 , _tiff_file->samples_per_pixel *_new_tiff_width );

_coord_trans.init_proj( coord_google , _proj4_str.c_str() );

geoRECT new_line_rect ;

new_line_rect.left=_geo_rect_des.left ;

new_line_rect.right=_geo_rect_des.right ;

TIFF_UINT32_T LineBytes= _tiff_file->samples_per_pixel * _tiff_file->tif_width;

TIFF_UINT32_T new_line_bytes= _tiff_file->samples_per_pixel *_new_tiff_width;

_hMutex= CreateMutex( NULL,FALSE,L"MyFileMutex");

m_htiff_write= CreateThread( NULL , 0 , tiff_write_thread , this , 0 , NULL );//启动新线程

for ( int i_height = 0 ; i_height < _new_tiff_height / 2 ; i_height++)

{double line_y =_geo_rect_des.top;

line_y-= ( i_height *_scaleY_des );

new_line_rect.top=line_y ;

new_line_rect.botton=line_y ;

geoRECT src_range;

des_geo_to_src_range( new_line_rect , src_range );int src_top = ( int)src_range.top;int src_bottom = ( int)src_range.botton;

TIFF_UINT32_T src_buf_size= LineBytes * ( src_bottom - src_top + 1);

TIFF_UINT8_T* src_buf = newTIFF_UINT8_T[ src_buf_size ];

memset( src_buf ,0, src_buf_size );

fseek( _tiff_file->pfile , _height_start[src_top] , SEEK_SET );

fread( src_buf ,src_buf_size ,1 , _tiff_file->pfile );

TIFF_UINT8_T* temp_buf =buf ;for ( int i_width = 0 ; i_width < _new_tiff_width ; i_width++)

{double new_y =_geo_rect_des.top;

new_y-= ( i_height *_scaleY_des );double new_x =_geo_rect_des.left;

new_x+= ( i_width *_scaleX_des );if ( _coord_type == 0)

{

_coord_trans.xy_2_latlong( new_x , new_y );

}else{

_coord_trans.xy_2_xy( new_x , new_y );

}

new_x= src_geo_xy( new_x );//添加带号

int pixel_x = 0 , pixel_y = 0;

point_to_by_geo( new_x , new_y , pixel_x , pixel_y );if ( !( pixel_y == -1 || pixel_x == -1) )

{

memcpy ( temp_buf

, src_buf+ ( pixel_y - src_top ) * LineBytes + pixel_x * _tiff_file->samples_per_pixel

, _tiff_file->samples_per_pixel );

temp_buf+= _tiff_file->samples_per_pixel ;

}else{

temp_buf+= _tiff_file->samples_per_pixel ;

}

}

TIFF_UINT64_T temp_file_size=_file_size ;

temp_file_size+= /*_tiff_file->samples_per_pixel * _new_tiff_width*/new_line_bytes *i_height ;

WaitForSingleObject( _hMutex ,INFINITE );// //等待临界资源,即锁定文件

TIFF_UINT64_T ret = /*fseek*/_fseeki64( _line_tiff , temp_file_size , SEEK_SET );int write_ret = fwrite( buf , /*_tiff_file->samples_per_pixel * _new_tiff_width*/new_line_bytes , 1, _line_tiff );

ReleaseMutex( _hMutex );//释放互斥量,解除锁定

delete[] src_buf;

src_buf=NULL ;

}delete[] buf;

buf=NULL;

printf("转换完成! \n");

}

DWORD WINAPI tiffDeform::tiff_write_thread( LPVOID lpParameter )

{

tiffDeform* pApp=( tiffDeform*)lpParameter;

printf("转换中请稍后... \n");

TIFF_UINT8_T* buf = new TIFF_UINT8_T[ pApp->_tiff_file->samples_per_pixel * pApp->_new_tiff_width ];

memset( buf ,0 , pApp->_tiff_file->samples_per_pixel * pApp->_new_tiff_width );//pApp->_coord_trans.init_proj( coord_google , pApp->_proj4_str.c_str() );

geoRECT new_line_rect ;

new_line_rect.left= pApp->_geo_rect_des.left ;

new_line_rect.right= pApp->_geo_rect_des.right ;

TIFF_UINT32_T LineBytes= pApp->_tiff_file->samples_per_pixel * pApp->_tiff_file->tif_width ;

TIFF_UINT32_T new_line_bytes= pApp->_tiff_file->samples_per_pixel * pApp->_new_tiff_width;for ( int i_height = pApp->_new_tiff_height / 2 ; i_height < pApp->_new_tiff_height ; i_height++)

{double line_y = pApp->_geo_rect_des.top;

line_y-= ( i_height * pApp->_scaleY_des );

new_line_rect.top=line_y ;

new_line_rect.botton=line_y ;

geoRECT src_range;

pApp->des_geo_to_src_range( new_line_rect , src_range );int src_top = ( int)src_range.top;int src_bottom = ( int)src_range.botton;

TIFF_UINT32_T src_buf_size= LineBytes * ( src_bottom - src_top + 1);

TIFF_UINT8_T* src_buf = newTIFF_UINT8_T[ src_buf_size ];

memset( src_buf ,0, src_buf_size );

fseek( pApp->_tiff_file->pfile , pApp->_height_start[src_top] , SEEK_SET );

fread( src_buf ,src_buf_size ,1 , pApp->_tiff_file->pfile );

TIFF_UINT8_T* temp_buf =buf ;for ( int i_width = 0 ; i_width < pApp->_new_tiff_width ; i_width++)

{double new_y = pApp->_geo_rect_des.top;

new_y-= ( i_height * pApp->_scaleY_des );double new_x = pApp->_geo_rect_des.left;

new_x+= ( i_width * pApp->_scaleX_des );if ( pApp->_coord_type == 0)

{

pApp->_coord_trans.xy_2_latlong( new_x , new_y );

}else{

pApp->_coord_trans.xy_2_xy( new_x , new_y );

}

new_x= pApp->src_geo_xy( new_x );//添加带号

int pixel_x = 0 , pixel_y = 0;

pApp->point_to_by_geo( new_x , new_y , pixel_x , pixel_y );if ( !( pixel_y == -1 || pixel_x == -1) )

{

memcpy ( temp_buf

, src_buf+ ( pixel_y - src_top ) * LineBytes + pixel_x * pApp->_tiff_file->samples_per_pixel

, pApp->_tiff_file->samples_per_pixel );

temp_buf+= pApp->_tiff_file->samples_per_pixel ;

}else{

temp_buf+= pApp->_tiff_file->samples_per_pixel ;

}

}

TIFF_UINT64_T temp_file_size= pApp->_file_size ;

temp_file_size+= /*pApp->_tiff_file->samples_per_pixel * pApp->_new_tiff_width*/new_line_bytes *i_height ;

WaitForSingleObject( pApp->_hMutex ,INFINITE );// //等待临界资源,即锁定文件

TIFF_UINT64_T ret = /*fseek*/_fseeki64( pApp->_line_tiff , temp_file_size , SEEK_SET );int write_ret = fwrite( buf , /*pApp->_tiff_file->samples_per_pixel * pApp->_new_tiff_width*/ new_line_bytes , 1 , pApp->_line_tiff );

ReleaseMutex( pApp->_hMutex );//释放互斥量,解除锁定

delete[] src_buf;

src_buf=NULL ;

}delete[] buf;

buf=NULL;return 0;

}voidtiffDeform::write_img_by_record()

{

TIFF_UINT32_T* height_start = new TIFF_UINT32_T[ _new_tiff_height * sizeof(TIFF_UINT32_T) ];

memset( height_start ,0 , _new_tiff_height * sizeof(TIFF_UINT32_T) );int bits = _tiff_file->bit_per_samples ;if ( bits >= 8)

{if ( !_tiff_file->tile.is_tile )//不是瓦片数据

{

fresh_line_start( _tiff_file , height_start );

}

}

_fseeki64( _line_tiff ,0, SEEK_END );

printf("转换中请稍后... \n");

TIFF_UINT8_T* buf = new TIFF_UINT8_T[ _tiff_file->samples_per_pixel *_new_tiff_width ];

memset( buf ,0 , _tiff_file->samples_per_pixel *_new_tiff_width );

_coord_trans.init_proj( coord_google , _proj4_str.c_str() );

geoRECT new_line_rect ;

new_line_rect.left=_geo_rect_des.left ;

new_line_rect.right=_geo_rect_des.right ;

TIFF_UINT32_T LineBytes= _tiff_file->samples_per_pixel * _tiff_file->tif_width;//计算中间行的斜率

double line_y =_geo_rect_des.top;

line_y-= ( _tiff_file->tif_height / 2 *_scaleY_des );

new_line_rect.top=line_y ;

new_line_rect.botton=line_y ;

geoRECT src_range;

des_geo_to_src_range( new_line_rect , src_range );int src_top = ( int)src_range.top;int src_bottom = ( int)src_range.botton;

TIFF_UINT32_T src_buf_size= LineBytes * ( src_bottom - src_top + 1);

TIFF_UINT8_T* src_buf = newTIFF_UINT8_T[ src_buf_size ];

memset( src_buf ,0, src_buf_size );

_fseeki64( _tiff_file->pfile , height_start[src_top] , SEEK_SET );

fread( src_buf ,src_buf_size ,1 , _tiff_file->pfile );int *diff_x = new int[ _new_tiff_width ] ;memset( diff_x , 0, _new_tiff_width );int *diff_y = new int[ _new_tiff_width ] ;memset( diff_y , 0, _new_tiff_width );int first_x = 0 , first_y = 0;for ( int i_width = 0 ; i_width < _new_tiff_width ; i_width++)

{double new_y =new_line_rect.top;double new_x =_geo_rect_des.left;

new_x+= ( i_width *_scaleX_des );if ( _coord_type == 0)

{

_coord_trans.xy_2_latlong( new_x , new_y );

}else{

_coord_trans.xy_2_xy( new_x , new_y );

}

new_x= src_geo_xy( new_x );//添加带号

int pixel_x = 0 , pixel_y = 0;

point_to_by_geo_ex( new_x , new_y , pixel_x , pixel_y );if ( i_width == 0)

{

diff_x[0] = 0;

diff_y[0] = 0;

first_x=pixel_x ;

first_y=pixel_y ;

}else{

diff_x[ i_width ]= pixel_x -first_x;

diff_y[ i_width ]= pixel_y -first_y;

}

}for ( int i_height = 0 ; i_height < _new_tiff_height ; i_height++)

{int pixel_first_x = 0 , pixel_first_y = 0;double line_y =_geo_rect_des.top;

line_y-= ( i_height *_scaleY_des );

new_line_rect.top=line_y ;

new_line_rect.botton=line_y ;

geoRECT src_range;

des_geo_to_src_range_first( new_line_rect , src_range , pixel_first_x , pixel_first_y );int src_top = ( int)src_range.top;int src_bottom = ( int)src_range.botton;

TIFF_UINT32_T src_buf_size= LineBytes * ( src_bottom - src_top + 1);

TIFF_UINT8_T* src_buf = newTIFF_UINT8_T[ src_buf_size ];

memset( src_buf ,0, src_buf_size );

_fseeki64( _tiff_file->pfile , height_start[src_top] , SEEK_SET );

fread( src_buf ,src_buf_size ,1 , _tiff_file->pfile );

TIFF_UINT8_T* temp_buf =buf ;for ( int i_width = 0 ; i_width < _new_tiff_width ; i_width++)

{int pixel_x = 0 , pixel_y = 0;

pixel_x= /*( int )src_range.left*/pixel_first_x +diff_x[ i_width ];

pixel_y= /*( int )src_range.top*/pixel_first_y +diff_y[ i_width ];if ( pixel_y < (int)src_range.top || pixel_y > (int) src_range.botton )

{

pixel_y= -1;

}if ( pixel_x < 0 || pixel_x > _tiff_file->tif_width )

{

pixel_x= 0;

}if ( !( pixel_y == -1 || pixel_x == -1) )

{

memcpy ( temp_buf

, src_buf+ ( pixel_y - src_top ) * LineBytes + pixel_x * _tiff_file->samples_per_pixel

, _tiff_file->samples_per_pixel );

temp_buf+= _tiff_file->samples_per_pixel ;

}else{

temp_buf+= _tiff_file->samples_per_pixel ;

}

}

fwrite( buf , _tiff_file->samples_per_pixel * _new_tiff_width , 1, _line_tiff );delete[] src_buf;

src_buf=NULL ;

}delete[] buf;

buf=NULL;//printf( "转换完成! \n" );

}voidtiffDeform::write_img_by_block_record( )

{

TIFF_UINT32_T* height_start = new TIFF_UINT32_T[ _new_tiff_height * sizeof( TIFF_UINT32_T ) ];

memset( height_start ,0 , _new_tiff_height * sizeof(TIFF_UINT32_T) );int bits = _tiff_file->bit_per_samples ;if ( bits >= 24)

{if ( !_tiff_file->tile.is_tile )//不是瓦片数据

{

fresh_line_start( _tiff_file , height_start );

}

}

fseek( _line_tiff ,0, SEEK_END );int bc = 0;int blockCount = 64;

DiffValue** diff = (DiffValue **)malloc(blockCount * sizeof(DiffValue *));;for(bc=0; bc

diff[bc]= newDiffValue[ _new_tiff_width ];

memset( diff[bc] ,0 , sizeof( DiffValue ) );

}//分成四部分分别计算差值/*DiffValue* diff1 = new DiffValue[ _new_tiff_width ];

memset( diff1 , 0 , sizeof( DiffValue ) );

DiffValue* diff2 = new DiffValue[ _new_tiff_width ];

memset( diff2 , 0 , sizeof( DiffValue ) );

DiffValue* diff3 = new DiffValue[ _new_tiff_width ];

memset( diff3 , 0 , sizeof( DiffValue ) );

DiffValue* diff4 = new DiffValue[ _new_tiff_width ];

memset( diff4 , 0 , sizeof( DiffValue ) );*/

int block_interval = _new_tiff_height /blockCount ;/*TIFF_UINT16_T height_pos[4] = { 0 , 0 , 0 , 0 } ; //每部分的中间位置

height_pos[0] = block_interval / 2; //第一部分的中间行

height_pos[1] = block_interval + block_interval / 2; //第二部分的中间位置

height_pos[2] = _new_tiff_height / 2 + block_interval / 2;//第三部分的中间位置行

height_pos[3] = _new_tiff_height - block_interval / 2; //第四部分的中间位置行*/TIFF_UINT16_T*height_pos = (TIFF_UINT16_T *)malloc(sizeof(TIFF_UINT16_T) *blockCount);for(bc=0; bc

{

height_pos[bc]= block_interval / 2 + block_interval * bc; //第一部分的中间行

}

_coord_trans.init_proj( coord_google , _proj4_str.c_str() );//初始化proj4

/*geoRECT temp_geo2 , temp_geo1 , temp_geo3 , temp_geo4 ;

memset( &temp_geo1 , 0 , sizeof( geoRECT ) );

memset( &temp_geo2 , 0 , sizeof( geoRECT ) );

memset( &temp_geo3 , 0 , sizeof( geoRECT ) );

memset( &temp_geo4 , 0 , sizeof( geoRECT ) );

temp_geo1.left = _geo_rect_des.left ;

temp_geo1.top = _geo_rect_des.top ;

temp_geo1.top -= ( height_pos[0] * _scaleY_des );

temp_geo2.left = _geo_rect_des.left ;

temp_geo2.top = _geo_rect_des.top ;

temp_geo2.top -= ( height_pos[1] * _scaleY_des );

temp_geo3.left = _geo_rect_des.left ;

temp_geo3.top = _geo_rect_des.top ;

temp_geo3.top -= ( height_pos[2] * _scaleY_des );

temp_geo4.left = _geo_rect_des.left ;

temp_geo4.top = _geo_rect_des.top ;

temp_geo4.top -= ( height_pos[3] * _scaleY_des );

int first1_x = 0 , first1_y = 0 ;

int first2_x = 0 , first2_y = 0 ;

int first3_x = 0 , first3_y = 0 ;

int first4_x = 0 , first4_y = 0 ;*/geoRECT*temp_geo = (geoRECT *)malloc(sizeof(geoRECT) *blockCount);int *first_x = (int *)malloc(sizeof(int) *blockCount);int *first_y = (int *)malloc(sizeof(int) *blockCount);for(bc=0; bc

{

memset(&temp_geo[bc] , 0 , sizeof( geoRECT ) );

temp_geo[bc].left=_geo_rect_des.left ;

temp_geo[bc].top=_geo_rect_des.top ;

temp_geo[bc].top-= ( height_pos[bc] *_scaleY_des );

first_x[bc]= 0;

first_y[bc]= 0;

}

TIFF_UINT32_T LineBytes= _tiff_file->samples_per_pixel * _tiff_file->tif_width;for ( int i_new_w = 0 ; i_new_w < _new_tiff_width ; i_new_w ++)

{for( bc = 0 ; bc < blockCount ; bc ++)

{double new_y =temp_geo[bc].top;double new_x =_geo_rect_des.left;

new_x+= ( i_new_w *_scaleX_des );if ( _coord_type == 0)

{

_coord_trans.xy_2_latlong( new_x , new_y );

}else{

_coord_trans.xy_2_xy( new_x , new_y );

}

new_x= src_geo_xy( new_x );//添加带号

int pixel_x = 0 , pixel_y = 0;

point_to_by_geo( new_x , new_y , pixel_x , pixel_y );if ( i_new_w == 0)

{double new_y =temp_geo[bc].top;double new_x =_geo_rect_des.left;

new_x+= ( _new_tiff_width /2 *_scaleX_des );if ( _coord_type == 0)

{

_coord_trans.xy_2_latlong( new_x , new_y );

}else{

_coord_trans.xy_2_xy( new_x , new_y );

}

new_x= src_geo_xy( new_x );//添加带号

int pixel_x = 0 , pixel_y = 0;

point_to_by_geo( new_x , new_y , pixel_x , pixel_y );

diff[bc][_new_tiff_width/2].diff_x = 0;

diff[bc][_new_tiff_width/2].diff_y = 0;

first_x[bc]=pixel_x ;

first_y[bc]=pixel_y ;

}else{

diff[bc][ i_new_w ].diff_x= pixel_x -first_x[bc] ;

diff[bc][ i_new_w ].diff_y= pixel_y -first_y[bc] ;

}

}/*//第一部分

double new_y = temp_geo1.top;

double new_x = _geo_rect_des.left;

new_x += ( i_new_w * _scaleX_des );

if ( _coord_type == 0 )

{

_coord_trans.xy_2_latlong( new_x , new_y );

}

else

{

_coord_trans.xy_2_xy( new_x , new_y );

}

new_x = src_geo_xy( new_x );//添加带号

int pixel_x = 0 , pixel_y = 0 ;

point_to_by_geo( new_x , new_y , pixel_x , pixel_y );

if ( i_new_w == 0 )

{

diff1[0].diff_x = 0 ;

diff1[0].diff_y = 0 ;

first1_x = pixel_x ;

first1_y = pixel_y ;

}

else

{

diff1[ i_new_w ].diff_x = pixel_x - first1_x ;

diff1[ i_new_w ].diff_y = pixel_y - first1_y ;

}

//第二部分

new_y = temp_geo2.top;

new_x = _geo_rect_des.left;

new_x += ( i_new_w * _scaleX_des );

if ( _coord_type == 0 )

{

_coord_trans.xy_2_latlong( new_x , new_y );

}

else

{

_coord_trans.xy_2_xy( new_x , new_y );

}

new_x = src_geo_xy( new_x );//添加带号

pixel_x = 0 , pixel_y = 0 ;

point_to_by_geo( new_x , new_y , pixel_x , pixel_y );

if ( i_new_w == 0 )

{

diff2[0].diff_x = 0 ;

diff2[0].diff_y = 0 ;

first2_x = pixel_x ;

first2_y = pixel_y ;

}

else

{

diff2[ i_new_w ].diff_x = pixel_x - first2_x ;

diff2[ i_new_w ].diff_y = pixel_y - first2_y ;

}

//第三部分

new_y = temp_geo3.top;

new_x = _geo_rect_des.left;

new_x += ( i_new_w * _scaleX_des );

if ( _coord_type == 0 )

{

_coord_trans.xy_2_latlong( new_x , new_y );

}

else

{

_coord_trans.xy_2_xy( new_x , new_y );

}

new_x = src_geo_xy( new_x );//添加带号

pixel_x = 0 , pixel_y = 0 ;

point_to_by_geo( new_x , new_y , pixel_x , pixel_y );

if ( i_new_w == 0 )

{

diff3[0].diff_x = 0 ;

diff3[0].diff_y = 0 ;

first3_x = pixel_x ;

first3_y = pixel_y ;

}

else

{

diff3[ i_new_w ].diff_x = pixel_x - first3_x ;

diff3[ i_new_w ].diff_y = pixel_y - first3_y ;

}

//第四部分

new_y = temp_geo4.top;

new_x = _geo_rect_des.left;

new_x += ( i_new_w * _scaleX_des );

if ( _coord_type == 0 )

{

_coord_trans.xy_2_latlong( new_x , new_y );

}

else

{

_coord_trans.xy_2_xy( new_x , new_y );

}

new_x = src_geo_xy( new_x );//添加带号

pixel_x = 0 , pixel_y = 0 ;

point_to_by_geo( new_x , new_y , pixel_x , pixel_y );

if ( i_new_w == 0 )

{

diff4[0].diff_x = 0 ;

diff4[0].diff_y = 0 ;

first4_x = pixel_x ;

first4_y = pixel_y ;

}

else

{

diff4[ i_new_w ].diff_x = pixel_x - first4_x ;

diff4[ i_new_w ].diff_y = pixel_y - first4_y ;

}*/}

geoRECT new_line_rect ;

memset(&new_line_rect , 0 , sizeof( geoRECT ) );

new_line_rect.left=_geo_rect_des.left ;

new_line_rect.right=_geo_rect_des.right ;

TIFF_UINT8_T* buf = new TIFF_UINT8_T[ _tiff_file->samples_per_pixel *_new_tiff_width ];

memset( buf ,0 , _tiff_file->samples_per_pixel *_new_tiff_width );for ( int i_height = 0 ; i_height < _new_tiff_height ; i_height++)

{double line_y =_geo_rect_des.top;

line_y-= ( i_height *_scaleY_des );

new_line_rect.top=line_y ;

new_line_rect.botton=line_y ;

geoRECT src_range;

des_geo_to_src_range( new_line_rect , src_range );int src_top = ( int)src_range.top;int src_bottom = ( int)src_range.botton;

TIFF_UINT32_T src_buf_size= LineBytes * ( src_bottom - src_top + 1);

TIFF_UINT8_T* src_buf = newTIFF_UINT8_T[ src_buf_size ];

memset( src_buf ,0, src_buf_size );

fseek( _tiff_file->pfile , height_start[src_top] , SEEK_SET );

fread( src_buf ,src_buf_size ,1 , _tiff_file->pfile );

TIFF_UINT8_T* temp_buf =buf ;

DiffValue* ptemp =NULL ;for(bc=0; bc

{if(i_height <= block_interval * (bc+1) && i_height >= block_interval *(bc))

{

ptemp=diff[bc];

}

}if(!ptemp) break;/*if ( i_height <= block_interval )

{

ptemp = diff1;

}

else if ( i_height > block_interval && i_height <= _new_tiff_height/2 )

{

ptemp = diff2;

}

else if ( i_height > _new_tiff_height/2 && i_height < ( _new_tiff_height/2 + block_interval ) )

{

ptemp = diff3 ;

}

else if ( i_height >= ( _new_tiff_height/2 + block_interval ) )

{

ptemp = diff4;

}*/

for ( int i_width = 0 ; i_width < _new_tiff_width ; i_width++)

{int pixel_x = 0 , pixel_y = 0;//if( i_width <= _new_tiff_width/2 )

/*{

pixel_x = ( int )src_range.left - ptemp[i_width].diff_x ;

pixel_y = ( int )src_range.botton - ptemp[i_width].diff_y ;

}*/

/*else*/{

pixel_x= ( int )src_range.left +ptemp[i_width].diff_x ;

pixel_y= ( int )src_range.botton +ptemp[i_width].diff_y ;

}if ( pixel_y < (int)src_range.top || pixel_y > (int) src_range.botton )

{

pixel_y= -1;

}if ( pixel_x < 0 || pixel_x > _tiff_file->tif_width )

{

pixel_x= 0;

}if ( !( pixel_y == -1 || pixel_x == -1) )

{

memcpy ( temp_buf

, src_buf+ ( pixel_y - src_top ) * LineBytes + pixel_x * _tiff_file->samples_per_pixel

, _tiff_file->samples_per_pixel );

temp_buf+= _tiff_file->samples_per_pixel ;

}else{

temp_buf+= _tiff_file->samples_per_pixel ;

}

}

fwrite( buf , _tiff_file->samples_per_pixel * _new_tiff_width , 1, _line_tiff );delete[] src_buf;

src_buf=NULL ;

}/*delete[] diff1;

diff1 = NULL ;

delete[] diff2;

diff2 = NULL ;

delete[] diff3;

diff3 = NULL ;

delete[] diff4;

diff4 = NULL ;*/}voidtiffDeform::write_img_by_sin()

{

TIFF_UINT32_T* height_start = new TIFF_UINT32_T[ _new_tiff_height * sizeof(TIFF_UINT32_T) ];

memset( height_start ,0 , _new_tiff_height * sizeof(TIFF_UINT32_T) );int bits = _tiff_file->bit_per_samples ;if ( bits >= 24)

{if ( !_tiff_file->tile.is_tile )//不是瓦片数据

{

fresh_line_start( _tiff_file , height_start );

}

}

fseek( _line_tiff ,0, SEEK_END );

printf("转换中请稍后... \n");

TIFF_UINT8_T* buf = new TIFF_UINT8_T[ _tiff_file->samples_per_pixel *_new_tiff_width ];

memset( buf ,0 , _tiff_file->samples_per_pixel *_new_tiff_width );

_coord_trans.init_proj( coord_google , _proj4_str.c_str() );

geoRECT new_line_rect ;

new_line_rect.left=_geo_rect_des.left ;

new_line_rect.right=_geo_rect_des.right ;

TIFF_UINT32_T LineBytes= _tiff_file->samples_per_pixel * _tiff_file->tif_width;//计算中间行的斜率

double line_y =_geo_rect_des.top;

line_y-= ( _tiff_file->tif_height / 2 *_scaleY_des );

new_line_rect.top=line_y ;

new_line_rect.botton=line_y ;

geoRECT new_line_rect_2=new_line_rect ;

to_src_proj( new_line_rect_2 ,/*true*/false);//倾斜角的正切值

double slope_tan = ( new_line_rect_2.top - new_line_rect_2.botton ) / ( new_line_rect_2.right -new_line_rect_2.left );//倾斜角的正弦值

double hypotenuse_line = sqrt( ( new_line_rect_2.top - new_line_rect_2.botton )*( new_line_rect_2.top -new_line_rect_2.botton )+ ( new_line_rect_2.right - new_line_rect_2.left )*( new_line_rect_2.right -new_line_rect_2.left ) );double slope_sin = ( new_line_rect_2.top - new_line_rect_2.botton ) /hypotenuse_line ;//倾斜角的余弦值

double slope_cos = ( new_line_rect_2.right - new_line_rect_2.left ) /hypotenuse_line;//X方向上的放大比例

double scale_x = ( double )_tiff_file->tif_width /_new_tiff_width ;

geoRECT src_range;

des_geo_to_src_range( new_line_rect , src_range );int src_top = ( int)src_range.top;int src_bottom = ( int)src_range.botton;

TIFF_UINT32_T src_buf_size= LineBytes * ( src_bottom - src_top + 1);

TIFF_UINT8_T* src_buf = newTIFF_UINT8_T[ src_buf_size ];

memset( src_buf ,0, src_buf_size );

fseek( _tiff_file->pfile , height_start[src_top] , SEEK_SET );

fread( src_buf ,src_buf_size ,1 , _tiff_file->pfile );for ( int i_height = 0 ; i_height < _new_tiff_height ; i_height++)

{double line_y =_geo_rect_des.top;

line_y-= ( i_height *_scaleY_des );

new_line_rect.top=line_y ;

new_line_rect.botton=line_y ;

geoRECT src_range;

des_geo_to_src_range( new_line_rect , src_range );int src_top = ( int)src_range.top;int src_bottom = ( int)src_range.botton;

TIFF_UINT32_T src_buf_size= LineBytes * ( src_bottom - src_top + 1);

TIFF_UINT8_T* src_buf = newTIFF_UINT8_T[ src_buf_size ];

memset( src_buf ,0, src_buf_size );

fseek( _tiff_file->pfile , height_start[src_top] , SEEK_SET );

fread( src_buf ,src_buf_size ,1 , _tiff_file->pfile );//计算第一个点坐标

int pixel_first_x = 0 , pixel_first_y = 0;

{double new_y =_geo_rect_des.top;

new_y-= ( i_height *_scaleY_des );double new_x =_geo_rect_des.left;if ( _coord_type == 0)

{

_coord_trans.xy_2_latlong( new_x , new_y );

}else{

_coord_trans.xy_2_xy( new_x , new_y );

}

new_x=src_geo_xy( new_x );

point_to_by_geo_ex( new_x , new_y , pixel_first_x , pixel_first_y );

}

TIFF_UINT8_T* temp_buf =buf ;for ( int i_width = 0 ; i_width < _new_tiff_width ; i_width ++)

{int pixel_x = 0 , pixel_y = 0;

pixel_x= /*( int )src_range.left*/( int )( pixel_first_x + ( i_width * scale_x + 0.5)) ;

pixel_y= /*( int )src_range.botton*/( int )( pixel_first_y - ( pixel_x * slope_tan + 0.5)) ;if ( pixel_y < (int)src_range.top || pixel_y > (int) src_range.botton )

{

pixel_y= -1;

}if ( pixel_x < 0 || pixel_x > _tiff_file->tif_width )

{

pixel_x= 0;

}if ( !( pixel_y == -1 || pixel_x == -1) )

{

memcpy ( temp_buf

, src_buf+ ( pixel_y - src_top ) * LineBytes + pixel_x * _tiff_file->samples_per_pixel

, _tiff_file->samples_per_pixel );

temp_buf+= _tiff_file->samples_per_pixel ;

}else{

temp_buf+= _tiff_file->samples_per_pixel ;

}

}

fwrite( buf , _tiff_file->samples_per_pixel * _new_tiff_width , 1, _line_tiff );delete[] src_buf;

src_buf=NULL ;

}delete[] buf;

buf=NULL;//printf( "转换完成! \n" );

}void tiffDeform::point_to_by_geo( double x , double y , int&pt_x , int&pt_y )

{

pt_x= (int)(( x - _geo_rect_src.left )/_scaleX_src + 0.5 );//计算像素点的在原始图片的范围

pt_y = (int)( ( _geo_rect_src.top - y ) / _scaleY_src + 0.5);if ( !(pt_x <= _tiff_file->tif_width && pt_x >= 0) )//像素点出了范围

{

pt_x= -1;

}if( !(pt_y <= _tiff_file->tif_height && pt_y >= 0 ) )//像素点出了范围

{

pt_y= -1;

}

}void tiffDeform::point_to_by_geo_ex( double x , double y , int&pt_x , int&pt_y )

{

pt_x= (int)(( x - _geo_rect_src.left )/_scaleX_src + 0.5 );//计算像素点的在原始图片的范围

pt_y = (int)( ( _geo_rect_src.top - y ) / _scaleY_src + 0.5);

}stringtiffDeform::new_tiff_name()

{int pos = _tiff_path.rfind( '\\');string tiff_name = _tiff_path.substr( pos + 1);string dir = _tiff_path.substr( 0 , pos + 1);string temp_name = "line_";

temp_name+=tiff_name ;return ( dir +temp_name ) ;

}voidtiffDeform::write_tag_list()

{for ( int i = 0 ; i < _de_num ; i++)

{

fseek( _line_tiff , DE_START+ ONE_DE_SIZE *i , SEEK_SET );

fwrite(&( de_list[i].de.tag ) , 2 , 1 , _line_tiff ); //TAG 2字节

fwrite( &( de_list[i].de.type ) , 2 , 1 , _line_tiff ); //数据类型 2字节

fwrite( &( de_list[i].de.count ) , 4 , 1 , _line_tiff ); //count 4字节

if( de_list[i].de.tag == 273 )//Strip offset

{

fseek( _line_tiff ,0, SEEK_END );

_strip_offset_pos=ftell( _line_tiff );

}//写入offset

if( de_list[i].data_source == 0 )//直接写入值

{

fwrite(&( de_list[i].de.offset ) , 4 , 1, _line_tiff );

}else if ( de_list[i].data_source == 1 )//文件对应的偏移量

{

fseek( _line_tiff ,0, SEEK_END );

TIFF_UINT32_T pos=ftell( _line_tiff );

TIFF_UINT64_T buffer_size= data_type_list[de_list[i].de.type].type_size *de_list[i].de.count;

file_disk_data( de_list[i].de , buffer_size );//修改TAG对应的数据存放的地址

fseek( _line_tiff , DE_START + ONE_DE_SIZE * i + 8, SEEK_SET );

fwrite(&pos , 1 , 4, _line_tiff );

}else if ( de_list[i].data_source == 2 )//内存

{

fseek( _line_tiff ,0, SEEK_END );

TIFF_UINT32_T pos=ftell( _line_tiff );

fwrite ( de_list[i].mem_data ,1 , data_type_list[de_list[i].de.type].type_size *de_list[i].de.count , _line_tiff );//修改TAG对应的数据存放的地址

fseek( _line_tiff , DE_START + ONE_DE_SIZE * i + 8, SEEK_SET );

fwrite(&pos , 1 , 4, _line_tiff );if ( de_list[i].de.tag == 33550

|| de_list[i].de.tag == 33922

|| de_list[i].de.tag == 34735

|| de_list[i].de.tag == 34736

|| de_list[i].de.tag == 34737)//这些属于静态内存不需要释放

{continue;

}delete[] de_list[i].mem_data;

}

}

}

TIFF_UINT64_T tiffDeform::file_disk_data( DirectoryEntry de , TIFF_UINT64_T buffer_size )

{

fseek( _tiff_file->pfile , de.offset , SEEK_SET );

TIFF_UINT8_T* buf = new TIFF_UINT8_T[1024];

memset( buf ,0 , 1024);

TIFF_UINT64_T fs= 0;

TIFF_UINT16_T read_size= 0;if ( buffer_size <= 1024 )//若小于1024字节,则读取之后直接写入即可

{

read_size= fread( buf , 1 , buffer_size , _tiff_file->pfile );if( _tiff_file->tiff_byte_order == TIFF_BIGENDIAN && data_type_list[de.type].type_size != 1)

{

sort_byte_order( buf , de.type , de.count );

}

fs+= fwrite ( buf , 1, read_size , _line_tiff );

}else//若大于1024字节,则分批写入1024字节,最后写入不足1024的字节

{

TIFF_UINT16_T tile_num= ( int )(buffer_size / 1024) ;

TIFF_UINT16_T last_num= buffer_size % 1024;for ( int i = 0 ; i < tile_num ; i++)

{

read_size= fread( buf , 1 , 1024 , _tiff_file->pfile );//注意参数的顺序

if( _tiff_file->tiff_byte_order == TIFF_BIGENDIAN && data_type_list[de.type].type_size != 1)

{

sort_byte_order( buf , de.type , de.count );

}

fs+= fwrite ( buf , 1, read_size , _line_tiff );

}

read_size= fread( buf , 1 , last_num , _tiff_file->pfile );if( _tiff_file->tiff_byte_order == TIFF_BIGENDIAN && data_type_list[de.type].type_size != 1)

{

sort_byte_order( buf , de.type , de.count );

}

fs+= fwrite ( buf , 1, last_num , _line_tiff );

}delete[] buf;

buf=NULL;returnfs;

}void tiffDeform::sort_byte_order( TIFF_UINT8_T*buf , TIFF_UINT16_T data_type , TIFF_UINT16_T data_count )

{

TIFF_UINT8_T* p =buf;for ( TIFF_UINT16_T i = 0 ; i < data_count ; i++)

{if ( data_type == 3 || data_type == 8 )//SHORT

{

TIFF_UINT16_T ret=sget2( p , TIFF_BIGENDIAN );

memcpy( p ,&ret , 2);

p+= 2;

}else if ( data_type == 4 || data_type == 9 || data_type == 11 )//LONG

{

TIFF_UINT32_T ret=sget4( p , TIFF_BIGENDIAN );

memcpy( p ,&ret , 4);

p+= 4;

}if ( data_type == 5 || data_type == 10)

{

TIFF_UINT32_T ret=sget4( p , TIFF_BIGENDIAN );

memcpy( p ,&ret , 4);

p+= 4;

ret=sget4( p , TIFF_BIGENDIAN );

memcpy( p ,&ret , 4);

p+= 4;

}else if ( data_type == 12 )//DOUBLE

{

TIFF_UINT64_T ret=sget8( p , TIFF_BIGENDIAN );

memcpy( p ,&ret , 8);

p+= 8;

}

}

}//修改strip offset所对应的值

voidtiffDeform::modify_strip_offset()

{

fseek( _line_tiff ,0, SEEK_END );

TIFF_UINT32_T current_size=ftell( _line_tiff );

fseek( _line_tiff , _strip_offset_pos , SEEK_SET );

TIFF_UINT32_T width_bytes= _new_tiff_width * _tiff_file->samples_per_pixel ;for ( int i = 0 ; i < _new_tiff_height ; i++)

{

TIFF_UINT32_T height_start= current_size + i *width_bytes;

fwrite(&height_start , 1 , 4, _line_tiff );

}

}voidtiffDeform::write_file_header( )

{//字节序

fwrite( "II" , 2 , 1, _line_tiff );//版本号

TIFF_UINT16_T ver = 0x002a;

fwrite(&ver , 2 , 1, _line_tiff );//Tag偏移量

TIFF_UINT32_T offset = 0x00000008;

fwrite(&offset , 4 , 1, _line_tiff );

}voidtiffDeform::src_coord_box( )

{

geoRECT pixel_rect;

pixel_rect.left= 0;

pixel_rect.top= 0;

pixel_rect.right= _tiff_file->tif_width;

pixel_rect.botton= _tiff_file->tif_height ;

geo_coord( _tiff_file , pixel_rect , _geo_rect_src );

}double tiffDeform::zone_number( doubleleft_right )

{if( _coord_type == 0)

{returnleft_right;

}if( left_right/10000000 > 1)

{returnleft_right;

}else{int zone_n =get_zone_num( _proj4_str );

left_right+= ( zone_n * 1000000);returnleft_right;

}

}int tiffDeform::get_zone_num( stringproj4_str )

{int pos = proj4_str.find("+x_0=");string str = proj4_str.substr( pos + 5);return atoi( str.substr( 0 , 2).c_str() );

}double tiffDeform::src_geo_xy( doubleleft_right )

{if( _coord_type == 0)

{returnleft_right;

}if ( _geo_rect_src.left / 10000000 > 1)

{returnleft_right;

}else{int zone_n =get_zone_num( _proj4_str );

left_right-= ( zone_n * 1000000);returnleft_right;

}

}voidtiffDeform::get_pixel_scale()

{if( _coord_type == 0 )//经纬度坐标

{

_scaleX_src= _tiff_file->geo_tiff.pixel_scale.scaleX;

_scaleY_src= _tiff_file->geo_tiff.pixel_scale.scaleY;

_scaleX_des= ( _geo_rect_des.right - _geo_rect_des.left )/_tiff_file->tif_width;

_scaleY_des= ( _geo_rect_des.top - _geo_rect_des.botton ) / _tiff_file->tif_height;

}else//平面投影坐标

{

_scaleX_src= _tiff_file->geo_tiff.pixel_scale.scaleX;

_scaleY_src= _tiff_file->geo_tiff.pixel_scale.scaleY;

_scaleX_des=_scaleX_src;

_scaleY_des=_scaleY_src;

}

}void tiffDeform::to_web_mector( geoRECT&rect , boolb )

{/*(1) (2)

+-----+

| |

| |

+-----+

(3) (4)*/_coord_trans.init_proj( _proj4_str.c_str() );doublex1 ,y1,x2 ,y2,x3 ,y3,x4 ,y4;

x1=rect.left;

y1=rect.top;if ( _coord_type == 0)

{

_coord_trans.longlat_2_xy( x1 , y1 );

}else{

_coord_trans.xy_2_xy( x1 , y1 );

}

x2=rect.right;

y2=rect.top;if ( _coord_type == 0)

{

_coord_trans.longlat_2_xy( x2 , y2 );

}else{

_coord_trans.xy_2_xy( x2 , y2 );

}

x3=rect.left;

y3=rect.botton;if ( _coord_type == 0)

{

_coord_trans.longlat_2_xy( x3 , y3 );

}else{

_coord_trans.xy_2_xy( x3 , y3 );

}

x4=rect.right;

y4=rect.botton;if ( _coord_type == 0)

{

_coord_trans.longlat_2_xy( x4 , y4 );

}else{

_coord_trans.xy_2_xy( x4 , y4 );

}if( b )

{

rect.left= x1 < x3 ?x3 : x1;

rect.top= y1 > y2 ?y2 : y1;

rect.right= x2 > x4 ?x4 : x2;

rect.botton= y3 < y4 ?y4 : y3;

}else{

rect.left= x1 > x3 ?x3 : x1;

rect.top= y1 < y2 ?y2 : y1;

rect.right= x2 < x4 ?x4 : x2;

rect.botton= y3 > y4 ?y4 : y3;

}

}void tiffDeform::to_web_mector( geoRECT&rect , bool b , double&x1 , double&y1, double&x2 , double&y2, double&x3 , double&y3, double&x4 , double&y4)

{/*(1) (2)

+-----+

| |

| |

+-----+

(3) (4)*/_coord_trans.init_proj( _proj4_str.c_str() );//double x1 ,y1,x2 ,y2,x3 ,y3,x4 ,y4;

x1 =rect.left;

y1=rect.top;if ( _coord_type == 0)

{

_coord_trans.longlat_2_xy( x1 , y1 );

}else{

_coord_trans.xy_2_xy( x1 , y1 );

}

x2=rect.right;

y2=rect.top;if ( _coord_type == 0)

{

_coord_trans.longlat_2_xy( x2 , y2 );

}else{

_coord_trans.xy_2_xy( x2 , y2 );

}

x3=rect.left;

y3=rect.botton;if ( _coord_type == 0)

{

_coord_trans.longlat_2_xy( x3 , y3 );

}else{

_coord_trans.xy_2_xy( x3 , y3 );

}

x4=rect.right;

y4=rect.botton;if ( _coord_type == 0)

{

_coord_trans.longlat_2_xy( x4 , y4 );

}else{

_coord_trans.xy_2_xy( x4 , y4 );

}if( b )

{

rect.left= x1 < x3 ?x3 : x1;

rect.top= y1 > y2 ?y2 : y1;

rect.right= x2 > x4 ?x4 : x2;

rect.botton= y3 < y4 ?y4 : y3;

}else{

rect.left= x1 > x3 ?x3 : x1;

rect.top= y1 < y2 ?y2 : y1;

rect.right= x2 < x4 ?x4 : x2;

rect.botton= y3 > y4 ?y4 : y3;

}

}inttiffDeform::get_src_tag_list()

{

TIFF_UINT32_T ifd_offset;//第一个IFD的偏移量

fseek( _tiff_file->pfile , 0, SEEK_SET );

fseek( _tiff_file->pfile ,4,SEEK_SET );

ifd_offset= get4( _tiff_file->pfile , _tiff_file->tiff_byte_order ) ;//定位到IFD的位置

fseek( _tiff_file->pfile , ifd_offset , SEEK_SET );//得到IFD的数量

_de_num = get2( _tiff_file->pfile , _tiff_file->tiff_byte_order );

de_list= newdeInfo[ _de_num ];

memset( de_list ,0 , _de_num * sizeof( deInfo ) );//循环得到DE

for ( TIFF_UINT16_T i = 0x0000 ; i < _de_num ; i++)

{

fseek( _tiff_file->pfile , ifd_offset + ONE_DE_SIZE * i + 2 , SEEK_SET );//文件指针复原指向

de_list[i].de.tag= get2( _tiff_file->pfile , _tiff_file->tiff_byte_order );

de_list[i].de.type= get2( _tiff_file->pfile , _tiff_file->tiff_byte_order );

de_list[i].de.count= get4( _tiff_file->pfile , _tiff_file->tiff_byte_order );//如果是大端字节序并且是short类型,则只会读取四个字节中的前两个字节

if ( de_list[i].de.type == 3 && _tiff_file->tiff_byte_order == 0x4d4d/*Motor*/ && de_list[i].de.count == 1)

{

de_list[i].de.offset= (TIFF_UINT32_T)get2( _tiff_file->pfile , _tiff_file->tiff_byte_order );

}else{

de_list[i].de.offset= get4( _tiff_file->pfile , _tiff_file->tiff_byte_order );

}//如果是 SHORT 或者 LONG 并且数量为1,则直接存储在Offset中,并不存储地址

if( ( de_list[i].de.type == 3 || de_list[i].de.type == 4 ) && de_list[i].de.count == 1)

{

de_list[i].data_source= 0;

}else{

de_list[i].data_source= 1;

}

}

print_tag_info_list();return_de_num ;

}voidtiffDeform::print_tag_info_list()

{

printf("\n");for ( int i = 0 ; i < _de_num ; i++)

{char outStr[1024];

memset( outStr ,0 , 1024 * sizeof( char) );

sprintf( outStr ,"0x%04x[ %5d %-26s ] , 0x%02x , 0x%04x( %5d ) , 0x%08x , %d \n", de_list[i].de.tag

, de_list[i].de.tag

, tag_text( de_list[i].de.tag )

, de_list[i].de.type

, de_list[i].de.count

, de_list[i].de.count

, de_list[i].de.offset

, de_list[i].data_source ) ;

printf( outStr );

}

}const char* tiffDeform::tag_text( inti_tag )

{int i = 0;while ( tag_text_list[i].i_tag != -1)

{if ( tag_text_list[i].i_tag ==i_tag )

{returntag_text_list[i].text;

}

i++;

}return "";

}

deInfo*tiffDeform::cts_strip_offsets()

{

deInfo* temp_de = newdeInfo;

temp_de->de.tag = 273;

temp_de->de.type = 4;//long

temp_de->de.count =_new_tiff_height;

temp_de->de.offset = 0;

temp_de->data_source = 2;

TIFF_UINT32_T* mem = newTIFF_UINT32_T[_new_tiff_height];

memset( mem ,0 , sizeof(TIFF_UINT32_T)*_new_tiff_height );

temp_de->mem_data = (TIFF_UINT8_T*)mem;returntemp_de;

}

deInfo*tiffDeform::cts_rows_per_strip()

{

deInfo* temp_de = newdeInfo;

temp_de->de.tag = 278;

temp_de->de.type = 3;//short

temp_de->de.count = 1;

temp_de->de.offset = 1;

temp_de->data_source = 0;

temp_de->mem_data =NULL;returntemp_de;

}

deInfo*tiffDeform::cts_strip_byte_counts()

{

deInfo* temp_de = newdeInfo;

temp_de->de.tag = 279;

temp_de->de.type = 4;//short

temp_de->de.count =_new_tiff_height;

temp_de->de.offset = 0;

temp_de->data_source = 2;

TIFF_UINT32_T* mem = newTIFF_UINT32_T[_new_tiff_height];

memset( mem ,0 , sizeof(TIFF_UINT32_T)*_new_tiff_height );for ( int i = 0 ; i < _new_tiff_height ; i++)

{

mem[i]= _new_tiff_width * _tiff_file->samples_per_pixel;

}

temp_de->mem_data = (TIFF_UINT8_T*)mem;returntemp_de;

}

deInfo*tiffDeform::cts_line_tag()

{

deInfo* temp_line_tag_list = new deInfo[3];

memset( temp_line_tag_list ,0 , sizeof(deInfo) * 3);

deInfo* temp =cts_strip_offsets();

memcpy( temp_line_tag_list , temp ,sizeof(deInfo) ) ;deletetemp;

temp=cts_rows_per_strip();

memcpy( temp_line_tag_list+ 1 , temp , sizeof(deInfo));deletetemp;

temp=cts_strip_byte_counts();

memcpy( temp_line_tag_list+ 2 , temp , sizeof(deInfo));deletetemp;

temp=NULL;returntemp_line_tag_list;

}inttiffDeform::cts_new_tag_list( )

{

deInfo* temp_line =cts_line_tag();

deInfo* temp_de_list = new deInfo[ _de_num ];//tile 4个标签 line 只需要3个标签

memset( temp_de_list , 0 , sizeof(deInfo)*( _de_num ) );int tag_num = 0;int j = 0 , k = 0;for ( int i = 0 ; i < _de_num ; i++)

{if ( de_list[i].de.tag == 33550

|| de_list[i].de.tag == 33922

|| de_list[i].de.tag == 34735

|| de_list[i].de.tag == 34736

|| de_list[i].de.tag == 34737)

{continue;

}

tag_num++;if ( k < 3 )//line只有三个标签

{if ( de_list[i].de.tag

{

memcpy( temp_de_list+ j , de_list + i , sizeof( deInfo ) );

j++;

}else{

memcpy( temp_de_list+ j , temp_line + k , sizeof( deInfo ) );

j++;

k++;//i--;

}

}else{

memcpy( temp_de_list+ j , de_list + i , sizeof( deInfo ) );

j++;

}int n = j - 1;if ( temp_de_list[n].de.tag == 256 )//ImageWidth

{

temp_de_list[n].de.offset=_new_tiff_width;

}if ( temp_de_list[n].de.tag == 257 )//ImageHeight

{

temp_de_list[n].de.offset=_new_tiff_height;

}

}delete[] de_list;

de_list=NULL;

de_list=temp_de_list;

_de_num=tag_num;

cs_pixel_scale( _scaleX_des , _scaleY_des );

cs_tie_point( _geo_rect_des.left , _geo_rect_des.top );

deInfo geo_coord_list[5];

memset( geo_coord_list ,0 , 5 * sizeof( deInfo ) );

cs_coord( geo_coord_list );

mix_proj_de_list( geo_coord_list );

print_tag_info_list();return_de_num;

}void tiffDeform::mix_proj_de_list( deInfo*coord_list )

{

deInfo* new_de_list = new deInfo[ _de_num + 5];

memset( new_de_list ,0 , sizeof( deInfo ) * ( _de_num + 5) );int i_des = 0;int i_coord = 0;if ( _de_num < 5)

{return;

}int count = 0;for( int i = 0 ; i < _de_num ; i++)

{if ( de_list[i].de.tag

{

new_de_list[i_des]=de_list[i];

i_des++;

count++;

}else if ( de_list[i].de.tag >=coord_list[i_coord].de.tag )

{

new_de_list[i_des]=coord_list[i_coord];

i_des++;

i_coord++;

count++;

}

}if ( i_coord < 5)

{int temp =i_coord;for ( int i = temp ; i < 5 ; i++)

{

new_de_list[ i_des ]=coord_list[ i_coord ];

i_des++;

i_coord++;

count++;

}

}delete[] de_list;

de_list=NULL;

de_list=new_de_list;

_de_num=count;

}

TIFF_UINT64_T tiffDeform::double_to_long(doubled_ )

{

union { TIFF_UINT64_T i;doublef; } u;

u.f=d_ ;returnu.i ;

}void tiffDeform::cs_pixel_scale( double cx , doublecy )

{

pixel_scale[0] =double_to_long ( cx );

pixel_scale[1] =double_to_long ( cy );

pixel_scale[2] = 0;

}void tiffDeform::cs_tie_point( double x , doubley )

{

tie_point[0] = 0;

tie_point[1] = 0;

tie_point[2] = 0;

tie_point[3] =double_to_long( x ) ;

tie_point[4] =double_to_long( y ) ;

tie_point[5] = 0;

}void tiffDeform::cs_coord( deInfo*coord_list )

{

coord_list[0].de.tag = 33550;//GeoTagPixelScale

coord_list[0].de.type = 12;//double

coord_list[0].de.count = 3;

coord_list[0].data_source = 2;//内存数据

coord_list[0].mem_data = ( TIFF_UINT8_T*)pixel_scale;

coord_list[1].de.tag = 33922;//GeoTagTiePoint

coord_list[1].de.type = 12;//double

coord_list[1].de.count = 6;

coord_list[1].data_source = 2;//内存数据

coord_list[1].mem_data = ( TIFF_UINT8_T*)tie_point;

coord_list[2].de.tag = 34735;//GeoTagDirectory

coord_list[2].de.type = 3;//short

coord_list[2].de.count = 80;

coord_list[2].data_source = 2;//内存数据

coord_list[2].mem_data = ( TIFF_UINT8_T*)geotiff_web_mercator_tag_dir;

coord_list[3].de.tag = 34736;//GeoDoubleParamsTag

coord_list[3].de.type = 12;//double

coord_list[3].de.count = 7;

coord_list[3].data_source = 2;//内存数据

coord_list[3].mem_data = ( TIFF_UINT8_T*)geotiff_double_param;

coord_list[4].de.tag = 34737;//GeoAsciiParamsTag

coord_list[4].de.type = 2;//ascii

coord_list[4].de.count = 64;

coord_list[4].data_source = 2;//内存数据

coord_list[4].mem_data = ( TIFF_UINT8_T*)geotiff_ascii_param;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值