00001 /****************************************************************************** 00002 * $Id: geo_trans_c-source.html 385 2001-03-05 04:58:33Z warmerda $ 00003 * 00004 * Project: libgeotiff 00005 * Purpose: Code to abstract translation between pixel/line and PCS 00006 * coordinates. 00007 * Author: Frank Warmerdam, warmerda@home.com 00008 * 00009 ****************************************************************************** 00010 * Copyright (c) 1999, Frank Warmerdam 00011 * 00012 * Permission is hereby granted, free of charge, to any person obtaining a 00013 * copy of this software and associated documentation files (the "Software"), 00014 * to deal in the Software without restriction, including without limitation 00015 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00016 * and/or sell copies of the Software, and to permit persons to whom the 00017 * Software is furnished to do so, subject to the following conditions: 00018 * 00019 * The above copyright notice and this permission notice shall be included 00020 * in all copies or substantial portions of the Software. 00021 * 00022 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00023 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00024 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00025 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00026 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00027 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00028 * DEALINGS IN THE SOFTWARE. 00029 ****************************************************************************** 00030 * 00031 * $Log$ 00031 * Revision 1.2 2001/03/05 04:58:33 warmerda 00031 * updated 00031 * 00032 * Revision 1.7 2001/03/05 03:25:23 warmerda 00033 * restructure cleanup, and apply to GTIFPCSToImage() 00034 * 00035 * Revision 1.6 2001/03/04 22:37:39 warmerda 00036 * fixed memory leak for fields fetched with gt_methods.get - Alan Gray 00037 * 00038 * Revision 1.5 2000/08/22 03:32:46 warmerda 00039 * removed GTIFTiepointTranslate code 00040 * 00041 * Revision 1.4 1999/09/17 01:19:51 warmerda 00042 * Fixed bug in use of transform matrix. 00043 * 00044 * Revision 1.3 1999/09/16 21:25:40 warmerda 00045 * Added tiepoint, and transformation matrix based translation. Note 00046 * that we don't try to invert the transformation matrix for 00047 * GTIFPCSToImage(). 00048 * 00049 * Revision 1.2 1999/09/07 20:00:40 warmerda 00050 * Fixed count/tiepoint_count bug in GTIFPCSToImage(). 00051 * 00052 * Revision 1.1 1999/05/04 03:07:57 warmerda 00053 * New 00054 * 00055 */ 00056 00057 #include "geotiff.h" 00058 #include "geo_tiffp.h" /* external TIFF interface */ 00059 #include "geo_keyp.h" /* private interface */ 00060 #include "geokeys.h" 00061 00062 /************************************************************************/ 00063 /* GTIFTiepointTranslate() */ 00064 /************************************************************************/ 00065 00066 int GTIFTiepointTranslate( int gcp_count, double * gcps_in, double * gcps_out, 00067 double x_in, double y_in, 00068 double *x_out, double *y_out ) 00069 00070 { 00071 /* I would appreciate a _brief_ block of code for doing second order 00072 polynomial regression here! */ 00073 return FALSE; 00074 } 00075 00076 00077 /************************************************************************/ 00078 /* GTIFImageToPCS() */ 00079 /************************************************************************/ 00080 00099 int GTIFImageToPCS( GTIF *gtif, double *x, double *y ) 00100 00101 { 00102 int res = FALSE; 00103 int tiepoint_count, count, transform_count; 00104 tiff_t *tif=gtif->gt_tif; 00105 double *tiepoints = 0; 00106 double *pixel_scale = 0; 00107 double *transform = 0; 00108 00109 00110 if (!(gtif->gt_methods.get)(tif, GTIFF_TIEPOINTS, 00111 &tiepoint_count, &tiepoints )) 00112 tiepoint_count = 0; 00113 00114 if (!(gtif->gt_methods.get)(tif, GTIFF_PIXELSCALE, &count, &pixel_scale )) 00115 count = 0; 00116 00117 if (!(gtif->gt_methods.get)(tif, GTIFF_TRANSMATRIX, 00118 &transform_count, &transform )) 00119 transform_count = 0; 00120 00121 /* -------------------------------------------------------------------- */ 00122 /* If the pixelscale count is zero, but we have tiepoints use */ 00123 /* the tiepoint based approach. */ 00124 /* -------------------------------------------------------------------- */ 00125 if( tiepoint_count > 6 && count == 0 ) 00126 { 00127 res = GTIFTiepointTranslate( tiepoint_count / 6, 00128 tiepoints, tiepoints + 3, 00129 *x, *y, x, y ); 00130 } 00131 00132 /* -------------------------------------------------------------------- */ 00133 /* If we have a transformation matrix, use it. */ 00134 /* -------------------------------------------------------------------- */ 00135 else if( transform_count == 16 ) 00136 { 00137 double x_in = *x, y_in = *y; 00138 00139 *x = x_in * transform[0] + y_in * transform[1] + transform[3]; 00140 *y = x_in * transform[4] + y_in * transform[5] + transform[7]; 00141 00142 res = TRUE; 00143 } 00144 00145 /* -------------------------------------------------------------------- */ 00146 /* For now we require one tie point, and a valid pixel scale. */ 00147 /* -------------------------------------------------------------------- */ 00148 else if( count < 3 || tiepoint_count < 6 ) 00149 { 00150 res = FALSE; 00151 } 00152 00153 else 00154 { 00155 *x = (*x - tiepoints[0]) * pixel_scale[0] + tiepoints[3]; 00156 *y = (*y - tiepoints[1]) * (-1 * pixel_scale[1]) + tiepoints[4]; 00157 00158 res = TRUE; 00159 } 00160 00161 /* -------------------------------------------------------------------- */ 00162 /* Cleanup */ 00163 /* -------------------------------------------------------------------- */ 00164 if(tiepoints) 00165 _GTIFFree(tiepoints); 00166 if(pixel_scale) 00167 _GTIFFree(pixel_scale); 00168 if(transform) 00169 _GTIFFree(transform); 00170 00171 return res; 00172 } 00173 00174 /************************************************************************/ 00175 /* GTIFPCSToImage() */ 00176 /************************************************************************/ 00177 00196 int GTIFPCSToImage( GTIF *gtif, double *x, double *y ) 00197 00198 { 00199 double *tiepoints; 00200 int tiepoint_count, count; 00201 double *pixel_scale; 00202 tiff_t *tif=gtif->gt_tif; 00203 int result = FALSE; 00204 00205 /* -------------------------------------------------------------------- */ 00206 /* Fetch tiepoints and pixel scale. */ 00207 /* -------------------------------------------------------------------- */ 00208 if (!(gtif->gt_methods.get)(tif, GTIFF_TIEPOINTS, 00209 &tiepoint_count, &tiepoints )) 00210 tiepoint_count = 0; 00211 00212 if (!(gtif->gt_methods.get)(tif, GTIFF_PIXELSCALE, &count, &pixel_scale )) 00213 count = 0; 00214 00215 /* -------------------------------------------------------------------- */ 00216 /* If the pixelscale count is zero, but we have tiepoints use */ 00217 /* the tiepoint based approach. */ 00218 /* -------------------------------------------------------------------- */ 00219 if( tiepoint_count > 6 && count == 0 ) 00220 { 00221 result = GTIFTiepointTranslate( tiepoint_count / 6, 00222 tiepoints + 3, tiepoints, 00223 *x, *y, x, y ); 00224 } 00225 00226 /* -------------------------------------------------------------------- */ 00227 /* For now we require one tie point, and a valid pixel scale. */ 00228 /* -------------------------------------------------------------------- */ 00229 else if( count >= 3 && tiepoint_count >= 6 ) 00230 { 00231 *x = (*x - tiepoints[3]) / pixel_scale[0] + tiepoints[0]; 00232 *y = (*y - tiepoints[4]) / (-1 * pixel_scale[1]) + tiepoints[1]; 00233 00234 result = TRUE; 00235 } 00236 00237 /* -------------------------------------------------------------------- */ 00238 /* Cleanup. */ 00239 /* -------------------------------------------------------------------- */ 00240 if(tiepoints) 00241 _GTIFFree(tiepoints); 00242 if(pixel_scale) 00243 _GTIFFree(pixel_scale); 00244 00245 return result; 00246 } 00247