直角坐标Y=944,X=437换算为经纬度是多少啊?

TIFF图片可以存储坐标信息,但是如何对这些坐标信息进行转换,然后生成新的图片呢?首先需要知道现在TIFF图片中的坐标系,然后要知道被转换的坐标系。现在我们只支持WGS84、西安80、北京54等中国常用的坐标系。
坐标系转换的头文件:
#pragma once
#include "proj_api.h"
#define PROJ4_COUNT 1024
#define coord_wgs84 "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
#define coord_google "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs"
class CoordTrans
CoordTrans();
void define_coord_sys( const int coord_sys );
void define_ellps( const int ellps );
void define_proj_zone( const int maj_zone );
void define_7_trans( double dx , double dy , double dz , double rx , double ry , double rz , double k );
void trans_2_des( double&left , double&top , double&right , double&bottom );
void trans_2_des( double&left , double&top );
void trans_back_src( double&left , double&top );
bool init_proj( const char* src_ ,const char* des_ = coord_google );
void xy_2_latlong( double&x , double&y );
void xy_2_xy( double&x , double&y );
void longlat_2_xy( double&x , double&y );
void deinit();
char* _sz_
char* _sz_
int _coord_
double _k;
坐标转换cpp文件
#include "coord_trans.h"
#include &string.h&
#include &stdio.h&
#include "windows.h"
#include &string&
using namespace std;
struct _ellps_type_data_
const _ellps_type_data_ ellps_list[]=
{L"西安80"
, 157528},
{L"北京54"
{L"国家2000",
{L"web墨卡托", , },
typedef struct _proj_type_data_
ProjData theProj[]=
{L"(6度带)13-中央经线 75"
,13,75,0,,0,1},
{L"(6度带)14-中央经线 81"
,14,81,0,,0,1},
{L"(6度带)15-中央经线 87"
,15,87,0,,0,1},
{L"(6度带)16-中央经线 93"
,16,93,0,,0,1},
{L"(6度带)17-中央经线 99" ,17,99,0,,0,1},
{L"(6度带)18-中央经线 105"
,18,105,0,,0,1},
{L"(6度带)19-中央经线 111"
,19,111,0,,0,1},
{L"(6度带)20-中央经线 117"
,20,117,0,,0,1},
{L"(6度带)21-中央经线 123"
,21,123,0,,0,1},
{L"(6度带)22-中央经线 129"
,22,129,0,,0,1},
{L"(6度带)23-中央经线 135"
,23,135,0,,0,1},
{L"(3度带)25-中央经线 75" ,25,75,0,,0,1},
{L"(3度带)26-中央经线 78" ,26,78,0,,0,1},
{L"(3度带)27-中央经线 81" ,27,81,0,,0,1},
{L"(3度带)28-中央经线 84" ,28,84,0,,0,1},
{L"(3度带)29-中央经线 87" ,29,87,0,,0,1},
{L"(3度带)30-中央经线 90" ,30,90,0,,0,1},
{L"(3度带)31-中央经线 93" ,31,93,0,,0,1},
{L"(3度带)32-中央经线 96" ,32,96,0,,0,1},
{L"(3度带)33-中央经线 99" ,33,99,0,,0,1},
{L"(3度带)34-中央经线 102"
,34,102,0,,0,1},
{L"(3度带)35-中央经线 105"
,35,105,0,,0,1},
{L"(3度带)36-中央经线 108"
,36,108,0,,0,1},
{L"(3度带)37-中央经线 111"
,37,111,0,,0,1},
{L"(3度带)38-中央经线 114"
,38,114,0,,0,1},
{L"(3度带)39-中央经线 117"
,39,117,0,,0,1},
{L"(3度带)40-中央经线 120"
,40,120,0,,0,1},
{L"(3度带)41-中央经线 123"
,41,123,0,,0,1},
{L"(3度带)42-中央经线 126"
,42,126,0,,0,1},
{L"(3度带)43-中央经线 129"
,43,129,0,,0,1},
{L"(3度带)44-中央经线 132"
,44,132,0,,0,1},
{L"(3度带)45-中央经线 135"
,45,135,0,,0,1},
,0,0,0,0,1}
CoordTrans::CoordTrans()
_source = NULL;
_des = NULL;
_sz_source = new char[PROJ4_COUNT];
memset( _sz_source , 0 , PROJ4_COUNT );
_sz_des = new char[PROJ4_COUNT];
memset( _sz_des , 0 , PROJ4_COUNT );
void CoordTrans::define_coord_sys( const int coord_sys )
_coord_sys = coord_
void CoordTrans::define_ellps( const int ellps )
void CoordTrans::define_proj_zone( const int maj_zone )
_maj_zone = maj_
void CoordTrans::define_7_trans( double dx , double dy , double dz , double rx , double ry , double rz , double k )
void CoordTrans::trans_2_des( double&left , double&top , double&right , double&bottom )
double left2 =
double right2 =
double top2 =
double bottom2 =
if ( _coord_sys == 0 )
proj = "+proj=longlat ";
else if ( _coord_sys == 1 )
proj = "+proj=tmerc ";
char ch_ellps[128];
memset( ch_ellps , 0 , 128 );
sprintf( ch_ellps , "+a=%f +b=%f " , ellps_list[_ellps].a , ellps_list[_ellps].b );
string ellps( ch_ellps );
char ch_zone[512];
memset( ch_zone , 0 , 512 );
sprintf ( ch_zone ,"+x_0=%f +y_0=0.000000 +lat_0=0.000000 +lon_0=%f +units=m +k=1.000000 "
, theProj[_maj_zone].pfe , theProj[_maj_zone].pl );
string zone( ch_zone );
char ch_wgs84[128];
memset( ch_wgs84 , 0 , 128 );
sprintf( ch_wgs84 , "+towgs84=%f,%f,%f,%f,%f,%f,%f" , _dx , _dy , _dz , _rx , _ry , _rz , _k );
string towgs84( ch_wgs84 );
sprintf ( _sz_source ,"%s%s%s%s" , proj.c_str() , ellps.c_str() , zone.c_str() , towgs84.c_str() );
init_proj( _sz_source );
if ( _coord_sys == 0 )
longlat_2_xy( left , top );
longlat_2_xy( right , bottom );
xy_2_xy( left , top );
xy_2_xy( right , bottom );
void CoordTrans::trans_2_des( double&left , double&top )
double left2 =
double top2 =
if ( _coord_sys == 0 )
proj = "+proj=longlat ";
else if ( _coord_sys == 1 )
proj = "+proj=tmerc ";
char ch_ellps[128];
memset( ch_ellps , 0 , 128 );
sprintf( ch_ellps , "+a=%f +b=%f " , ellps_list[_ellps].a , ellps_list[_ellps].b );
string ellps( ch_ellps );
char ch_zone[512];
memset( ch_zone , 0 , 512 );
sprintf ( ch_zone ,"+x_0=%f +y_0=0.000000 +lat_0=0.000000 +lon_0=%f +units=m +k=1.000000 "
, theProj[_maj_zone].pfe , theProj[_maj_zone].pl );
string zone( ch_zone );
char ch_wgs84[128];
memset( ch_wgs84 , 0 , 128 );
sprintf( ch_wgs84 , "+towgs84=%f,%f,%f,%f,%f,%f,%f" , _dx , _dy , _dz , _rx , _ry , _rz , _k );
string towgs84( ch_wgs84 );
sprintf ( _sz_source ,"%s%s%s%s" , proj.c_str() , ellps.c_str() , zone.c_str() , towgs84.c_str() );
init_proj( _sz_source );
if ( _coord_sys == 0 )
longlat_2_xy( left , top );
xy_2_xy( left , top );
void CoordTrans::trans_back_src( double&left , double&top )
double left2 =
double top2 =
if ( _coord_sys == 0 )
proj = "+proj=longlat ";
else if ( _coord_sys == 1 )
proj = "+proj=tmerc ";
char ch_ellps[128];
memset( ch_ellps , 0 , 128 );
sprintf( ch_ellps , "+a=%f +b=%f " , ellps_list[_ellps].a , ellps_list[_ellps].b );
string ellps( ch_ellps );
char ch_zone[512];
memset( ch_zone , 0 , 512 );
sprintf ( ch_zone ,"+x_0=%f +y_0=0.000000 +lat_0=0.000000 +lon_0=%f +units=m +k=1.000000 "
, theProj[_maj_zone].pfe , theProj[_maj_zone].pl );
string zone( ch_zone );
char ch_wgs84[128];
memset( ch_wgs84 , 0 , 128 );
sprintf( ch_wgs84 , "+towgs84=%f,%f,%f,%f,%f,%f,%f" , _dx , _dy , _dz , _rx , _ry , _rz , _k );
string towgs84( ch_wgs84 );
sprintf ( _sz_des ,"%s%s%s%s" , proj.c_str() , ellps.c_str() , zone.c_str() , towgs84.c_str() );
init_proj( coord_google , _sz_des );
if ( _coord_sys == 0 )
longlat_2_xy( left , top );
xy_2_xy( left , top );
bool CoordTrans::init_proj( const char* src_ ,const char* des_ )
_source = pj_init_plus( src_ );
if ( !_source )
return false;
_des = pj_init_plus( des_ );
if ( !_des )
return false;
return true;
void CoordTrans::xy_2_latlong( double&x , double&y )
pj_transform(
_source,_des, 1, 0, &x , &y , 0 );
x *= RAD_TO_DEG;
y *= RAD_TO_DEG;
void CoordTrans::xy_2_xy( double&x , double&y )
pj_transform(
_source,_des, 1, 0, &x , &y , 0 );
void CoordTrans::longlat_2_xy( double&x , double&y )
x *= DEG_TO_RAD;
y *= DEG_TO_RAD;
pj_transform(
_source ,_des ,
1, 0, &x , &y , 0 );
void CoordTrans::deinit()
if ( _source )
pj_free( _source );
_source = NULL;
if ( _des )
pj_free( _des );
_des= NULL;
tiff处理的头文件:
#pragma once
#include "tiflib.h"
#include "coord_trans.h"
#include &math.h&
#include &string&
#include &vector&
using namespace std;
#include&Windows.h&
#define TIFF_HEADER_SIZE 8
#define DE_START 10
#define ONE_DE_SIZE 12
#define IDF_END_FLAG_SIZE 4
typedef struct
const char*
typedef struct
TIFF_UINT16_T
char* type_
TIFF_UINT16_T type_
typedef struct
TIFF_UINT16_T
TIFF_UINT16_T
TIFF_UINT32_T
TIFF_UINT32_T
}DirectoryE
typedef struct
DirectoryE
TIFF_UINT8_T* mem_
typedef struct
class tiffDeform
tiffDeform();
tiffDeform( string tiff_path , string proj4_str );
~tiffDeform();
int open( string tiff_path , string proj4_str
, int coord_type );
bool arrangement();
void save( string src_file , string save_name = "" );
void src_coord_box( );
double zone_number( double left_right );
int get_zone_num( string proj4_str );
double src_geo_xy( double left_right );
void to_web_mector( geoRECT&rect , bool b );
void to_web_mector( geoRECT&rect , bool b , double&x1 , double&y1, double&x2 , double&y2, double&x3 , double&y3, double&x4 , double&y4 );
void get_pixel_scale();
int get_src_tag_list();
int cts_new_tag_list();
void print_tag_info_list();
const char* tag_text( int i_tag );
deInfo* cts_strip_offsets();
deInfo* cts_rows_per_strip();
deInfo* cts_strip_byte_counts();
deInfo* cts_line_tag();
void mix_proj_de_list( deInfo* coord_list );
void write_file_header( );
string new_tiff_name();
void modify_strip_offset();
void sort_byte_order( TIFF_UINT8_T* buf , TIFF_UINT16_T data_type , TIFF_UINT16_T data_count );
TIFF_UINT64_T file_disk_data( DirectoryEntry de , TIFF_UINT64_T buffer_size );
void write_tag_list();
void write_img_data();
void write_img_data_ex();
void write_img_data_th();
void write_img_data_th_ex();
void write_img_data_mul_th();
void write_img_by_record();
void write_img_by_block_record();
void write_img_by_sin();
TIFF_UINT64_T double_to_long( double d_ );
void point_to_by_geo( double x , double y , int&pt_x , int&pt_y );
void point_to_by_geo_ex( double x , double y , int&pt_x , int&pt_y );
void cs_coord( deInfo* coord_list );
void cs_tie_point( double x , double y );
void cs_pixel_scale( double cx , double cy );
void des_geo_to_src_range( geoRECT des_geo , geoRECT&src_range );
void to_src_proj( geoRECT&des_geo , bool b );
void des_geo_to_src_range_first( geoRECT des_geo , geoRECT&src_range , int&first_x , int&first_y );
void to_src_proj_first( geoRECT&des_geo , bool b , double&first_x , double&first_y );
HANDLE m_htiff_
static DWORD WINAPI tiff_write_thread( LPVOID lpParameter );
TiffFile* _tiff_
string _tiff_
string _proj4_
CoordTrans _coord_
int _coord_
geoRECT _geo_rect_src , _geo_rect_
double _scaleX_src,_scaleY_
double _scaleX_des,_scaleY_
int _new_tiff_
int _new_tiff_
FILE* _line_
deInfo* de_
TIFF_UINT16_T _de_
TIFF_UINT32_T _strip_offset_
TIFF_UINT64_T pixel_scale[3];
TIFF_UINT64_T tie_point[6];
TIFF_UINT32_T* _height_
TIFF_UINT32_T _file_
HANDLE _hM
tiff处理的cpp
#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[] =
, "RATIONAL" , 8 },
, "UNDEFINED", 1 },
, "SSHORT"
{ 10 , "SRATIONAL", 8 },
{ 11 , "FLOAT"
{ 12 , "DOUBLE"
unsigned char geotiff_web_mercator_tag_dir[]=
0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x13, 0x00,
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
unsigned char geotiff_double_param[]=
0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0xf0 , 0x3f ,
0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
0x00 , 0x00 , 0x00 , 0x40 , 0xa6 , 0x54 , 0x58 , 0x41 ,
0x00 , 0x00 , 0x00 , 0x40 , 0xa6 , 0x54 , 0x58 , 0x41
unsigned char geotiff_ascii_param[]=
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 = new TiffF
memset( _tiff_file , 0 , sizeof(TiffFile) );
tiffDeform::tiffDeform( string tiff_path , string proj4_str )
_tiff_path = tiff_
_proj4_str = proj4_
_tiff_file = new TiffF
memset( _tiff_file , 0 , sizeof(TiffFile) );
tiffDeform::~tiffDeform()
if( _tiff_file == NULL )
delete _tiff_
_tiff_file = NULL;
bool tiffDeform::arrangement()
return _tiff_file-&tile.is_
int tiffDeform::open( string tiff_path , string proj4_str , int coord_type )
_tiff_path = tiff_
_proj4_str = proj4_
_coord_type = coord_
return tif_open ( tiff_path.c_str() , _tiff_file );
void tiffDeform::save(
string src_file , string save_name )
delete _tiff_
_tiff_file = NULL;
_tiff_path = src_
_tiff_file = new TiffF
memset( _tiff_file , 0 , sizeof(TiffFile) );
tif_open( _tiff_path.c_str() , _tiff_file );
src_coord_box( );
_geo_rect_des = _geo_rect_
_geo_rect_des.left = zone_number( _geo_rect_des.left ) ;
_geo_rect_des.right = zone_number( _geo_rect_des.right ) ;
double x1,y1,x2,y2,x3,y3,x4,y4;
to_web_mector( _geo_rect_des , true ,x1,y1,x2,y2,x3,y3,x4,y4 );
get_pixel_scale();
_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 );
get_src_tag_list();
cts_new_tag_list();
string temp_
save_name == "" )
temp_path = new_tiff_name() ;
temp_path = save_
_line_tiff = fopen( temp_path.c_str() , "wb" );
if ( _line_tiff == NULL )
write_file_header( );
fwrite( &( _de_num ) , 1 , 2 , _line_tiff );
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 );
write_tag_list();
modify_strip_offset();
write_img_by_record();
fclose( _line_tiff );
void tiffDeform::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_
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_ 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_ i_width++ )
double new_y = _geo_rect_des.
new_y -= ( i_height * _scaleY_des );
double new_x = _geo_rect_des.
new_x += ( i_width * _scaleX_des );
if ( _coord_type == 0 )
_coord_trans.xy_2_latlong( new_x , new_y );
_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_
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 );
fwrite( buf , _tiff_file-&samples_per_pixel , 1 , _line_tiff );
buf = NULL;
void tiffDeform::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_
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_ i_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 =
for ( int i_width = 0 ; i_width & _new_tiff_ i_width++ )
double new_y = _geo_rect_des.
new_y -= ( i_height * _scaleY_des );
double new_x = _geo_rect_des.
new_x += ( i_width * _scaleX_des );
if ( _coord_type == 0 )
_coord_trans.xy_2_latlong( new_x , new_y );
_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 ) )
TIFF_UINT32_T pos = height_start[pixel_y] + pixel_x*_tiff_file-&samples_per_
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 );
temp_buf += _tiff_file-&samples_per_
temp_buf += _tiff_file-&samples_per_
fwrite( buf , _tiff_file-&samples_per_pixel * _new_tiff_width , 1 , _line_tiff );
buf = NULL;
printf( "转换完成! \n" );
void tiffDeform::to_src_proj_first( geoRECT&des_geo , bool b , double&first_x , double&first_y )
double x1,y1,x2,y2,x3,y3,x4,y4;
x1 = des_geo.
y1 = des_geo.
x2 = des_geo. y2 = des_geo.
x3 = des_geo. y3 = des_geo.
x4 = des_geo. y4 = des_geo.
if ( _coord_type == 0 )
_coord_trans.xy_2_latlong( x1 , y1 );
_coord_trans.xy_2_xy( x1 , y1 );
first_x = x1 ;
first_y = y1 ;
if ( _coord_type == 0 )
_coord_trans.xy_2_latlong( x2 , y2 );
_coord_trans.xy_2_xy( x2 , y2 );
if ( _coord_type == 0 )
_coord_trans.xy_2_latlong( x3 , y3 );
_coord_trans.xy_2_xy( x3 , y3 );
if ( _coord_type == 0 )
_coord_trans.xy_2_latlong( x4 , y4 );
_coord_trans.xy_2_xy( x4 , y4 );
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;
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 , bool b )
double x1,y1,x2,y2,x3,y3,x4,y4;
x1 = des_geo.
y1 = des_geo.
x2 = des_geo. y2 = des_geo.
x3 = des_geo. y3 = des_geo.
x4 = des_geo. y4 = des_geo.
if ( _coord_type == 0 )
_coord_trans.xy_2_latlong( x1 , y1 );
_coord_trans.xy_2_xy( x1 , y1 );
if ( _coord_type == 0 )
_coord_trans.xy_2_latlong( x2 , y2 );
_coord_trans.xy_2_xy( x2 , y2 );
if ( _coord_type == 0 )
_coord_trans.xy_2_latlong( x3 , y3 );
_coord_trans.xy_2_xy( x3 , y3 );
if ( _coord_type == 0 )
_coord_trans.xy_2_latlong( x4 , y4 );
_coord_trans.xy_2_xy( x4 , y4 );
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;
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.right = ( des_geo.right - _geo_rect_src.left ) / _scaleX_src + 0.5 ;
if ( 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_
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_
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_
void tiffDeform::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_
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_ i_height++ )
double line_y = _geo_rect_des.
line_y -= ( i_height * _scaleY_des );
geoRECT new_line_
new_line_rect.left = _geo_rect_des.
new_line_rect.right = _geo_rect_des.
new_line_rect.top = line_
new_line_rect.botton = line_
geoRECT src_
des_geo_to_src_range( new_line_rect , src_range );
int src_top = ( int )src_range.
int src_bottom = ( int )src_range.
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 = new TIFF_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 =
for ( int i_width = 0 ; i_width & _new_tiff_ i_width++ )
double new_y = _geo_rect_des.
new_y -= ( i_height * _scaleY_des );
double new_x = _geo_rect_des.
new_x += ( i_width * _scaleX_des );
if ( _coord_type == 0 )
_coord_trans.xy_2_latlong( new_x , new_y );
_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_
temp_buf += _tiff_file-&samples_per_
int pixel_x = 0 , pixel_y = 0 ;
if ( !( pixel_y == -1 || pixel_x == -1 ) )
temp_buf += _tiff_file-&samples_per_
temp_buf += _tiff_file-&samples_per_
fwrite( buf , _tiff_file-&samples_per_pixel * _new_tiff_width , 1 , _line_tiff );
buf = NULL;
delete[] src_
src_buf = NULL ;
printf( "转换完成! \n" );
void tiffDeform::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_
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_
new_line_rect.left = _geo_rect_des.
new_line_rect.right = _geo_rect_des.
TIFF_UINT32_T LineBytes = _tiff_file-&samples_per_pixel * _tiff_file-&tif_
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_ i_height++ )
double line_y = _geo_rect_des.
line_y -= ( i_height * _scaleY_des );
new_line_rect.top = line_
new_line_rect.botton = line_
geoRECT src_
des_geo_to_src_range( new_line_rect , src_range );
int src_top = ( int )src_range.
int src_bottom = ( int )src_range.
TIFF_UINT32_T src_buf_size =
LineBytes * ( src_bottom - src_top + 1 );
TIFF_UINT8_T* src_buf = new TIFF_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 =
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_ i_width++ )
double new_y = _geo_rect_des.
new_y -= ( i_height * _scaleY_des );
double new_x = _geo_rect_des.
new_x += ( i_width * _scaleX_des );
if ( _coord_type == 0 )
_coord_trans.xy_2_latlong( new_x , new_y );
_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_
temp_buf += _tiff_file-&samples_per_
fwrite( buf , _tiff_file-&samples_per_pixel * _new_tiff_width , 1 , _line_tiff );
fwrite( "\n" , 1 , 1 , pf );
delete[] src_
src_buf = NULL ;
buf = NULL;
printf( "转换完成! \n" );
void tiffDeform::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_
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_
new_line_rect.left = _geo_rect_des.
new_line_rect.right = _geo_rect_des.
TIFF_UINT32_T LineBytes = _tiff_file-&samples_per_pixel * _tiff_file-&tif_
TIFF_UINT32_T new_line_bytes = _tiff_file-&samples_per_pixel * _new_tiff_
_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.
line_y -= ( i_height * _scaleY_des );
new_line_rect.top = line_
new_line_rect.botton = line_
geoRECT src_
des_geo_to_src_range( new_line_rect , src_range );
int src_top = ( int )src_range.
int src_bottom = ( int )src_range.
TIFF_UINT32_T src_buf_size =
LineBytes * ( src_bottom - src_top + 1 );
TIFF_UINT8_T* src_buf = new TIFF_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 =
for ( int i_width = 0 ; i_width & _new_tiff_ i_width++ )
double new_y = _geo_rect_des.
new_y -= ( i_height * _scaleY_des );
double new_x = _geo_rect_des.
new_x += ( i_width * _scaleX_des );
if ( _coord_type == 0 )
_coord_trans.xy_2_latlong( new_x , new_y );
_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_
temp_buf += _tiff_file-&samples_per_
TIFF_UINT64_T temp_file_size = _file_
temp_file_size += new_line_bytes * i_
WaitForSingleObject( _hMutex ,INFINITE );
TIFF_UINT64_T ret = _fseeki64( _line_tiff , temp_file_size , SEEK_SET );
int write_ret = fwrite( buf , new_line_bytes , 1 , _line_tiff );
ReleaseMutex( _hMutex );
delete[] src_
src_buf = NULL ;
buf = NULL;
printf( "转换完成! \n" );
DWORD WINAPI tiffDeform::tiff_write_thread( LPVOID lpParameter )
tiffDeform* pApp=( tiffDeform* )lpP
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 );
geoRECT new_line_
new_line_rect.left = pApp-&_geo_rect_des.
new_line_rect.right = pApp-&_geo_rect_des.
TIFF_UINT32_T LineBytes = pApp-&_tiff_file-&samples_per_pixel * pApp-&_tiff_file-&tif_
TIFF_UINT32_T new_line_bytes = pApp-&_tiff_file-&samples_per_pixel * pApp-&_new_tiff_
for ( int i_height = pApp-&_new_tiff_height / 2 ; i_height & pApp-&_new_tiff_ i_height++ )
double line_y = pApp-&_geo_rect_des.
line_y -= ( i_height * pApp-&_scaleY_des );
new_line_rect.top = line_
new_line_rect.botton = line_
geoRECT src_
pApp-&des_geo_to_src_range( new_line_rect , src_range );
int src_top = ( int )src_range.
int src_bottom = ( int )src_range.
TIFF_UINT32_T src_buf_size =
LineBytes * ( src_bottom - src_top + 1 );
TIFF_UINT8_T* src_buf = new TIFF_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 =
for ( int i_width = 0 ; i_width & pApp-&_new_tiff_ i_width++ )
double new_y = pApp-&_geo_rect_des.
new_y -= ( i_height * pApp-&_scaleY_des );
double new_x = pApp-&_geo_rect_des.
new_x += ( i_width * pApp-&_scaleX_des );
if ( pApp-&_coord_type == 0 )
pApp-&_coord_trans.xy_2_latlong( new_x , new_y );
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_
temp_buf += pApp-&_tiff_file-&samples_per_
TIFF_UINT64_T temp_file_size = pApp-&_file_
temp_file_size += new_line_bytes * i_
WaitForSingleObject( pApp-&_hMutex ,INFINITE );
TIFF_UINT64_T ret = _fseeki64( pApp-&_line_tiff , temp_file_size , SEEK_SET );
int write_ret = fwrite( buf ,
new_line_bytes , 1 , pApp-&_line_tiff );
ReleaseMutex( pApp-&_hMutex );
delete[] src_
src_buf = NULL ;
buf = NULL;
void tiffDeform::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_
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_
new_line_rect.left = _geo_rect_des.
new_line_rect.right = _geo_rect_des.
TIFF_UINT32_T LineBytes = _tiff_file-&samples_per_pixel * _tiff_file-&tif_
double line_y = _geo_rect_des.
line_y -= ( _tiff_file-&tif_height / 2 * _scaleY_des );
new_line_rect.top = line_
new_line_rect.botton = line_
geoRECT src_
des_geo_to_src_range( new_line_rect , src_range );
int src_top = ( int )src_range.
int src_bottom = ( int )src_range.
TIFF_UINT32_T src_buf_size =
LineBytes * ( src_bottom - src_top + 1 );
TIFF_UINT8_T* src_buf = new TIFF_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_ i_width++ )
double new_y = new_line_rect.
double new_x = _geo_rect_des.
new_x += ( i_width * _scaleX_des );
if ( _coord_type == 0 )
_coord_trans.xy_2_latlong( new_x , new_y );
_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_
first_y = pixel_
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_ i_height++ )
int pixel_first_x = 0 , pixel_first_y = 0 ;
double line_y = _geo_rect_des.
line_y -= ( i_height * _scaleY_des );
new_line_rect.top = line_
new_line_rect.botton = line_
geoRECT src_
des_geo_to_src_range_first( new_line_rect , src_range , pixel_first_x , pixel_first_y );
int src_top = ( int )src_range.
int src_bottom = ( int )src_range.
TIFF_UINT32_T src_buf_size =
LineBytes * ( src_bottom - src_top + 1 );
TIFF_UINT8_T* src_buf = new TIFF_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 =
for ( int i_width = 0 ; i_width & _new_tiff_ i_width++ )
int pixel_x = 0 , pixel_y = 0 ;
pixel_x = pixel_first_x + diff_x[ i_width ];
pixel_y = 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_
temp_buf += _tiff_file-&samples_per_
fwrite( buf , _tiff_file-&samples_per_pixel * _new_tiff_width , 1 , _line_tiff );
delete[] src_
src_buf = NULL ;
buf = NULL;
void tiffDeform::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_
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&blockC bc++){
diff[bc] = new DiffValue[ _new_tiff_width ];
memset( diff[bc] , 0 , sizeof( DiffValue ) );
int block_interval = _new_tiff_height / blockC
TIFF_UINT16_T *height_pos = (TIFF_UINT16_T *)malloc(sizeof(TIFF_UINT16_T) * blockCount);
for(bc=0; bc&blockC bc++)
height_pos[bc] = block_interval / 2 + block_interval *
_coord_trans.init_proj( coord_google , _proj4_str.c_str() );
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&blockC bc++)
memset( &temp_geo[bc] , 0 , sizeof( geoRECT ) );
temp_geo[bc].left = _geo_rect_des.
temp_geo[bc].top = _geo_rect_des.
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_
for ( int i_new_w = 0 ; i_new_w & _new_tiff_ i_new_w ++ )
for( bc = 0 ; bc & blockC bc ++ )
double new_y = temp_geo[bc].
double new_x = _geo_rect_des.
new_x += ( i_new_w * _scaleX_des );
if ( _coord_type == 0 )
_coord_trans.xy_2_latlong( new_x , new_y );
_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].
double new_x = _geo_rect_des.
new_x += ( _new_tiff_width /2 * _scaleX_des );
if ( _coord_type == 0 )
_coord_trans.xy_2_latlong( new_x , new_y );
_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_
first_y[bc] = pixel_
diff[bc][ i_new_w ].diff_x = pixel_x - first_x[bc] ;
diff[bc][ i_new_w ].diff_y = pixel_y - first_y[bc] ;
geoRECT new_line_
memset( &new_line_rect , 0 , sizeof( geoRECT ) );
new_line_rect.left = _geo_rect_des.
new_line_rect.right = _geo_rect_des.
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_ i_height++ )
double line_y = _geo_rect_des.
line_y -= ( i_height * _scaleY_des );
new_line_rect.top = line_
new_line_rect.botton = line_
geoRECT src_
des_geo_to_src_range( new_line_rect , src_range );
int src_top = ( int )src_range.
int src_bottom = ( int )src_range.
TIFF_UINT32_T src_buf_size =
LineBytes * ( src_bottom - src_top + 1 );
TIFF_UINT8_T* src_buf = new TIFF_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 =
DiffValue* ptemp = NULL ;
for(bc=0; bc&blockC bc++)
if(i_height &= block_interval * (bc+1) && i_height &= block_interval * (bc))
ptemp = diff[bc];
if(!ptemp) break;
for ( int i_width = 0 ; i_width & _new_tiff_ i_width++ )
int pixel_x = 0 , pixel_y = 0 ;
pixel_x = ( int )src_range.left + ptemp[i_width].diff_
pixel_y = ( int )src_range.botton + ptemp[i_width].diff_
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_
temp_buf += _tiff_file-&samples_per_
fwrite( buf , _tiff_file-&samples_per_pixel * _new_tiff_width , 1 , _line_tiff );
delete[] src_
src_buf = NULL ;
void tiffDeform::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_
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_
new_line_rect.left = _geo_rect_des.
new_line_rect.right = _geo_rect_des.
TIFF_UINT32_T LineBytes = _tiff_file-&samples_per_pixel * _tiff_file-&tif_
double line_y = _geo_rect_des.
line_y -= ( _tiff_file-&tif_height / 2 * _scaleY_des );
new_line_rect.top = line_
new_line_rect.botton = line_
geoRECT new_line_rect_2 = new_line_
to_src_proj( new_line_rect_2 , 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_
double slope_cos = ( new_line_rect_2.right - new_line_rect_2.left ) / hypotenuse_
double scale_x = ( double )_tiff_file-&tif_width / _new_tiff_
geoRECT src_
des_geo_to_src_range( new_line_rect , src_range );
int src_top = ( int )src_range.
int src_bottom = ( int )src_range.
TIFF_UINT32_T src_buf_size =
LineBytes * ( src_bottom - src_top + 1 );
TIFF_UINT8_T* src_buf = new TIFF_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_ i_height++ )
double line_y = _geo_rect_des.
line_y -= ( i_height * _scaleY_des );
new_line_rect.top = line_
new_line_rect.botton = line_
geoRECT src_
des_geo_to_src_range( new_line_rect , src_range );
int src_top = ( int )src_range.
int src_bottom = ( int )src_range.
TIFF_UINT32_T src_buf_size =
LineBytes * ( src_bottom - src_top + 1 );
TIFF_UINT8_T* src_buf = new TIFF_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.
new_y -= ( i_height * _scaleY_des );
double new_x = _geo_rect_des.
if ( _coord_type == 0 )
_coord_trans.xy_2_latlong( new_x , new_y );
_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 =
for ( int i_width = 0 ; i_width & _new_tiff_ i_width ++ )
int pixel_x = 0 , pixel_y = 0 ;
pixel_x = ( int )( pixel_first_x + ( i_width * scale_x + 0.5 )) ;
pixel_y = ( 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_
temp_buf += _tiff_file-&samples_per_
fwrite( buf , _tiff_file-&samples_per_pixel * _new_tiff_width , 1 , _line_tiff );
delete[] src_
src_buf = NULL ;
buf = NULL;
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 );
string tiffDeform::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_
return ( dir + temp_name ) ;
void tiffDeform::write_tag_list()
for ( int i = 0 ; i & _de_ i++ )
fseek( _line_tiff , DE_START + ONE_DE_SIZE * i , SEEK_SET );
fwrite( &( de_list[i].de.tag ) , 2 , 1 , _line_tiff );
fwrite( &( de_list[i].de.type ) , 2 , 1 , _line_tiff );
fwrite( &( de_list[i].de.count ) , 4 , 1 , _line_tiff );
if( de_list[i].de.tag == 273 )
fseek( _line_tiff , 0 , SEEK_END );
_strip_offset_pos = ftell( _line_tiff );
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.
file_disk_data( de_list[i].de , buffer_size );
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 );
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)
delete[] de_list[i].mem_
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 )
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 );
TIFF_UINT16_T tile_num = ( int )(buffer_size / 1024) ;
TIFF_UINT16_T last_num = buffer_size % 1024;
for ( int i = 0 ; i & tile_ 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 );
buf = NULL;
void tiffDeform::sort_byte_order( TIFF_UINT8_T* buf , TIFF_UINT16_T data_type , TIFF_UINT16_T data_count )
TIFF_UINT8_T* p =
for ( TIFF_UINT16_T i = 0 ; i & data_ i++ )
if ( data_type == 3 || data_type == 8 )
TIFF_UINT16_T ret = sget2( p , TIFF_BIGENDIAN );
memcpy( p , &ret , 2 );
else if ( data_type == 4 ||
data_type == 9 ||
data_type == 11 )
TIFF_UINT32_T ret = sget4( p , TIFF_BIGENDIAN );
memcpy( p , &ret , 4 );
if ( data_type == 5 || data_type == 10 )
TIFF_UINT32_T ret = sget4( p , TIFF_BIGENDIAN );
memcpy( p , &ret , 4 );
ret = sget4( p , TIFF_BIGENDIAN );
memcpy( p , &ret , 4 );
else if ( data_type == 12 )
TIFF_UINT64_T ret = sget8( p , TIFF_BIGENDIAN );
memcpy( p , &ret , 8 );
void tiffDeform::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_
for ( int i = 0 ; i & _new_tiff_ i++ )
TIFF_UINT32_T height_start = current_size + i * width_
fwrite( &height_start , 1 , 4 , _line_tiff );
void tiffDeform::write_file_header( )
fwrite( "II" , 2 , 1 , _line_tiff );
TIFF_UINT16_T ver = 0x002a;
fwrite( &ver , 2 , 1 , _line_tiff );
TIFF_UINT32_T offset = 0x;
fwrite( &offset , 4 , 1 , _line_tiff );
void tiffDeform::src_coord_box( )
geoRECT pixel_
pixel_rect.left = 0 ;
pixel_rect.top = 0;
pixel_rect.right = _tiff_file-&tif_
pixel_rect.botton = _tiff_file-&tif_
geo_coord( _tiff_file , pixel_rect , _geo_rect_src );
double tiffDeform::zone_number( double left_right )
if( _coord_type == 0 )
return left_
if( left_right/ & 1 )
return left_
int zone_n = get_zone_num( _proj4_str );
left_right += ( zone_n * 1000000 );
return left_
int tiffDeform::get_zone_num( string proj4_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( double left_right )
if( _coord_type == 0 )
return left_
if ( _geo_rect_src.left /
return left_
int zone_n = get_zone_num( _proj4_str );
left_right -= ( zone_n * 1000000 );
return left_
void tiffDeform::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_
_scaleY_des = ( _geo_rect_des.top - _geo_rect_des.botton ) / _tiff_file-&tif_
_scaleX_src = _tiff_file-&geo_tiff.pixel_scale.scaleX;
_scaleY_src = _tiff_file-&geo_tiff.pixel_scale.scaleY;
_scaleX_des = _scaleX_
_scaleY_des = _scaleY_
void tiffDeform::to_web_mector( geoRECT&rect , bool b )
_coord_trans.init_proj( _proj4_str.c_str() );
double x1 ,y1,x2 ,y2,x3 ,y3,x4 ,y4;
x1 = rect.
y1 = rect.
if ( _coord_type == 0 )
_coord_trans.longlat_2_xy( x1 , y1 );
_coord_trans.xy_2_xy( x1 , y1 );
x2 = rect.
y2 = rect.
if ( _coord_type == 0 )
_coord_trans.longlat_2_xy( x2 , y2 );
_coord_trans.xy_2_xy( x2 , y2 );
x3 = rect.
y3 = rect.
if ( _coord_type == 0 )
_coord_trans.longlat_2_xy( x3 , y3 );
_coord_trans.xy_2_xy( x3 , y3 );
x4 = rect.
y4 = rect.
if ( _coord_type == 0 )
_coord_trans.longlat_2_xy( x4 , y4 );
_coord_trans.xy_2_xy( x4 , y4 );
rect.left = x1 & x3 ? x3 : x1;
rect.top = y1 & y2 ? y2 : y1;
rect.right = x2 & x4 ? x4 : x2;
rect.botton = y3 & y4 ? y4 : y3;
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)
_coord_trans.init_proj( _proj4_str.c_str() );
x1 = rect.
y1 = rect.
if ( _coord_type == 0 )
_coord_trans.longlat_2_xy( x1 , y1 );
_coord_trans.xy_2_xy( x1 , y1 );
x2 = rect.
y2 = rect.
if ( _coord_type == 0 )
_coord_trans.longlat_2_xy( x2 , y2 );
_coord_trans.xy_2_xy( x2 , y2 );
x3 = rect.
y3 = rect.
if ( _coord_type == 0 )
_coord_trans.longlat_2_xy( x3 , y3 );
_coord_trans.xy_2_xy( x3 , y3 );
x4 = rect.
y4 = rect.
if ( _coord_type == 0 )
_coord_trans.longlat_2_xy( x4 , y4 );
_coord_trans.xy_2_xy( x4 , y4 );
rect.left = x1 & x3 ? x3 : x1;
rect.top = y1 & y2 ? y2 : y1;
rect.right = x2 & x4 ? x4 : x2;
rect.botton = y3 & y4 ? y4 : y3;
rect.left = x1 & x3 ? x3 : x1;
rect.top = y1 & y2 ? y2 : y1;
rect.right = x2 & x4 ? x4 : x2;
rect.botton = y3 & y4 ? y4 : y3;
int tiffDeform::get_src_tag_list()
TIFF_UINT32_T 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 ) ;
fseek( _tiff_file-&pfile , ifd_offset , SEEK_SET );
_de_num = get2( _tiff_file-&pfile , _tiff_file-&tiff_byte_order );
de_list = new deInfo[ _de_num ];
memset( de_list , 0 , _de_num * sizeof( deInfo ) );
for ( TIFF_UINT16_T i = 0x0000 ; i & _de_ 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 );
if ( de_list[i].de.type == 3 && _tiff_file-&tiff_byte_order == 0x4d4d && de_list[i].de.count == 1 )
de_list[i].de.offset = (TIFF_UINT32_T)get2( _tiff_file-&pfile , _tiff_file-&tiff_byte_order );
de_list[i].de.offset = get4( _tiff_file-&pfile , _tiff_file-&tiff_byte_order );
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 ;
de_list[i].data_source = 1 ;
print_tag_info_list();
return _de_
void tiffDeform::print_tag_info_list()
printf( "\n" );
for ( int i = 0 ; i & _de_ 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( int i_tag )
int i = 0 ;
while ( tag_text_list[i].i_tag != -1 )
if ( tag_text_list[i].i_tag == i_tag )
return tag_text_list[i].
return "";
deInfo* tiffDeform::cts_strip_offsets()
deInfo* temp_de = new deI
temp_de-&de.tag = 273;
temp_de-&de.type = 4;
temp_de-&de.count = _new_tiff_
temp_de-&de.offset = 0;
temp_de-&data_source = 2;
TIFF_UINT32_T* mem = new TIFF_UINT32_T[_new_tiff_height];
memset( mem , 0 , sizeof(TIFF_UINT32_T)*_new_tiff_height );
temp_de-&mem_data = (TIFF_UINT8_T*)
return temp_
deInfo* tiffDeform::cts_rows_per_strip()
deInfo* temp_de = new deI
temp_de-&de.tag = 278;
temp_de-&de.type = 3;
temp_de-&de.count = 1;
temp_de-&de.offset = 1;
temp_de-&data_source = 0;
temp_de-&mem_data = NULL;
return temp_
deInfo* tiffDeform::cts_strip_byte_counts()
deInfo* temp_de = new deI
temp_de-&de.tag = 279;
temp_de-&de.type = 4;
temp_de-&de.count = _new_tiff_
temp_de-&de.offset = 0;
temp_de-&data_source = 2;
TIFF_UINT32_T* mem = new TIFF_UINT32_T[_new_tiff_height];
memset( mem , 0 , sizeof(TIFF_UINT32_T)*_new_tiff_height );
for ( int i = 0 ; i & _new_tiff_ i++ )
mem[i] = _new_tiff_width * _tiff_file-&samples_per_
temp_de-&mem_data = (TIFF_UINT8_T*)
return temp_
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) ) ;
temp = cts_rows_per_strip();
memcpy( temp_line_tag_list + 1 , temp , sizeof(deInfo));
temp = cts_strip_byte_counts();
memcpy( temp_line_tag_list + 2 , temp , sizeof(deInfo));
temp = NULL;
return temp_line_tag_
int tiffDeform::cts_new_tag_list( )
deInfo* temp_line = cts_line_tag();
deInfo* temp_de_list = new deInfo[ _de_num ];
memset( temp_de_list , 0 , sizeof(deInfo)*( _de_num ) );
int tag_num = 0;
int j = 0 , k = 0 ;
for ( int i = 0 ; i & _de_ i++ )
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)
tag_num ++;
if ( k & 3 )
if ( de_list[i].de.tag & temp_line[k].de.tag )
memcpy( temp_de_list + j , de_list + i , sizeof( deInfo ) );
memcpy( temp_de_list + j , temp_line + k , sizeof( deInfo ) );
memcpy( temp_de_list + j , de_list + i , sizeof( deInfo ) );
int n = j - 1;
if ( temp_de_list[n].de.tag == 256 )
temp_de_list[n].de.offset = _new_tiff_
if ( temp_de_list[n].de.tag == 257 )
temp_de_list[n].de.offset = _new_tiff_
delete[] de_
de_list = NULL;
de_list = temp_de_
_de_num = tag_
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_
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 )
int count = 0;
for( int i = 0 ; i & _de_ i++ )
if ( de_list[i].de.tag & coord_list[i_coord].de.tag )
new_de_list[i_des] = de_list[i];
else if ( de_list[i].de.tag &= coord_list[i_coord].de.tag )
new_de_list[i_des] = coord_list[i_coord];
i_coord++;
if ( i_coord & 5 )
int temp = i_
for ( int i = i & 5 ; i++ )
new_de_list[ i_des ] = coord_list[ i_coord ];
i_coord ++;
delete[] de_
de_list = NULL;
de_list = new_de_
TIFF_UINT64_T tiffDeform::double_to_long( double d_ )
union { TIFF_UINT64_T double }
u.f = d_ ;
void tiffDeform::cs_pixel_scale( double cx , double cy )
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 , double y )
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;
coord_list[0].de.type = 12;
coord_list[0].de.count = 3 ;
coord_list[0].data_source = 2;
coord_list[0].mem_data = ( TIFF_UINT8_T* )pixel_
coord_list[1].de.tag = 33922;
coord_list[1].de.type = 12;
coord_list[1].de.count = 6 ;
coord_list[1].data_source = 2;
coord_list[1].mem_data = ( TIFF_UINT8_T* )tie_
coord_list[2].de.tag = 34735;
coord_list[2].de.type = 3;
coord_list[2].de.count = 80;
coord_list[2].data_source = 2;
coord_list[2].mem_data = ( TIFF_UINT8_T* )geotiff_web_mercator_tag_
coord_list[3].de.tag = 34736;
coord_list[3].de.type = 12;
coord_list[3].de.count = 7;
coord_list[3].data_source = 2;
coord_list[3].mem_data = ( TIFF_UINT8_T* )geotiff_double_
coord_list[4].de.tag = 34737;
coord_list[4].de.type = 2;
coord_list[4].de.count = 64;
coord_list[4].data_source = 2;
coord_list[4].mem_data = ( TIFF_UINT8_T* )geotiff_ascii_
使用方法:
#include &stdio.h&
#include &stdlib.h&
#include "MainH.h"
#include "tiffDeform.h"
int main( int argc, char* argv[] )
if ( argc & 4 )
print_help_info();
return -1;
string tiff_path( argv[1] );
int coord_type = 0;
string proj4_str = parse_proj4 ( argc , argv , coord_type );
tiffDeform tiff_
tiff_deform.open( tiff_path , proj4_str , coord_type );
string src_tiff = tiff_
string save_
if ( tiff_deform.arrangement() )
string cmd_sz = get_exe_dir( argv[0] );
cmd_sz += "Tile2Line.exe \"-t\" ";
cmd_sz += "\"";
cmd_sz += tiff_
cmd_sz += "\"";
cmd_sz += " ";
cmd_sz += "\"";
cmd_sz += argv[2];
cmd_sz += "\"";
system( cmd_sz.c_str() );
src_tiff = deal_tiff_name( argv[2] , tiff_path );
save_name = new_tiff_name( argv[2] , tiff_path );
tiff_deform.save( src_tiff
, save_name );
printf( "转换完成!" );
string new_tiff_name( string save_dir ,string src_name )
int pos = src_name.rfind( '\\' );
string tiff_name = src_name.substr( pos + 1 );
string temp_name = "\\new_";
temp_name += tiff_
return ( save_dir + temp_name ) ;
string deal_tiff_name( string save_dir ,string src_name )
int pos = src_name.rfind( '\\' );
string tiff_name = src_name.substr( pos + 1 );
string temp_name = "\\line_";
temp_name += tiff_
return ( save_dir + temp_name ) ;
string get_exe_dir( char* argv )
string str( argv );
int pos = str.rfind('\\') ;
return str.substr( 0 , pos + 1 );
void print_help_info()
printf("-?/H/h 显示帮助信息 \n");
printf("参数:[tiff图片完整的路径][保存TIFF的文件夹] [坐标类型 0-经纬度坐标 1-平面投影坐标] [当前tiff图片坐标信息的proj4字符串] \n");
printf(" 例 :D:\\beijing.tif D:\\xxx 0 +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs\n" );
string parse_proj4( int argc , char* argv[] ,int&coord_type )
coord_type = atoi( argv[3] );
string temp_
for ( int i = 4 ; i & i++ )
temp_sz += argv[i];
temp_sz+=" ";
return temp_sz.substr( 0 , temp_sz.size() -1 );
备注:需要连接proj4的库文件
TIFF影像的坐标文件.tfw
tfw文件是关于TIFF影像坐标信息的文本文件,定义了影像象素坐标与实际地理坐标的仿射关系,基本原理如下:
x'=Ax+By+C
y'=Dx+Ey+F
三种利用Python批量处理地理数据的方法——以栅格数据投影转换为例
时至今日,笔者已经总结了三种用Python语言结合ArcGIS10.2提供的接口去批量处理地理数据的方法。即:
1. 用IDLE、Python Tools for Visual Studi...
ENVI中批量转TIFF,Arcgis批量地理校正
一、数据介绍  本次使用的数据为时空连续臭氧产品,从武汉大学“地学感知数据质量改善与融合应用研究室”网站(网址:http://sendimage.whu.edu.cn/send-resource-do...
ArcGIS之一影像格式的转换
ArcGIS之一影像格式的转换
作者:vashon
1、打开ArcMap ,一次展开工具“Conversion Tools”、“To Raster” .......
如何将谷歌地图高清卫星影像坐标系转换成西安80坐标(WGS84)
如何将下载的影像变换为西安80坐标?
利用ARCGIS进行自定义坐标系和投影转换
ARCGIS种通过三参数和其参数进行精确投影转换
如何将卫星影像进行投影转换转换成北京54坐标
利用ARCGIS进行自定义坐标系和投影转换
ARCGIS种通过三参数和其参数进行精确投影转换
注意:投影转换成54坐标系需要下载无偏移卫星图像进行...
【python图像处理】tiff文件的保存与解析
tiff文件是一种常用的图像文件格式,支持将多幅图像保存到一个文件中,极大得方便了图像的保存和处理。python中支持tiff文件处理的是libtiff模块中的TIFF类(libtiff下载链接htt...
经纬度与平面坐标的互相转换
两个函数,实现 经纬度与平面坐标的互相转换。
GetGeo输入平面坐标的x、y坐标,返回一个转化后的IPoint。两个方法都是将x、y坐标转化后返回IPoint。其中32649代表的坐标系如下:/...
使用gdal工具将图片转换成带坐标信息的geotiff格式并去除配准后的黑边
加入坐标信息
gdal_translate.exe -of GTiff -a_srs EPSG:4326 -gcp
111..653065 -gcp ...
没有更多推荐了,}

我要回帖

更多关于 X+Y 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信