aboutsummaryrefslogtreecommitdiff
path: root/src/nad_init.c
diff options
context:
space:
mode:
authorFrank Warmerdam <warmerdam@pobox.com>2011-11-22 22:51:47 +0000
committerFrank Warmerdam <warmerdam@pobox.com>2011-11-22 22:51:47 +0000
commitcf37b02b5540ddf8fb1d9fcf4658a55e3fcaeb33 (patch)
tree111b38c0a3d5a0b7b54c790130aebd107c8a2781 /src/nad_init.c
parentd2d560d04c4a1cf78ba3cedb8be9993e3037870c (diff)
downloadPROJ-cf37b02b5540ddf8fb1d9fcf4658a55e3fcaeb33.tar.gz
PROJ-cf37b02b5540ddf8fb1d9fcf4658a55e3fcaeb33.zip
implement support for ctable2 format (read/write)
git-svn-id: http://svn.osgeo.org/metacrs/proj/trunk@2121 4e78687f-474d-0410-85f9-8d5e500ac6b2
Diffstat (limited to 'src/nad_init.c')
-rw-r--r--src/nad_init.c149
1 files changed, 143 insertions, 6 deletions
diff --git a/src/nad_init.c b/src/nad_init.c
index da10dd2b..20f1adb6 100644
--- a/src/nad_init.c
+++ b/src/nad_init.c
@@ -45,6 +45,38 @@
#endif /* _WIN32_WCE */
/************************************************************************/
+/* swap_words() */
+/* */
+/* Convert the byte order of the given word(s) in place. */
+/************************************************************************/
+
+static int byte_order_test = 1;
+#define IS_LSB (((unsigned char *) (&byte_order_test))[0] == 1)
+
+static void swap_words( void *data_in, int word_size, int word_count )
+
+{
+ int word;
+ unsigned char *data = (unsigned char *) data_in;
+
+ for( word = 0; word < word_count; word++ )
+ {
+ int i;
+
+ for( i = 0; i < word_size/2; i++ )
+ {
+ int t;
+
+ t = data[i];
+ data[i] = data[word_size-i-1];
+ data[word_size-i-1] = t;
+ }
+
+ data += word_size;
+ }
+}
+
+/************************************************************************/
/* nad_ctable_load() */
/* */
/* Load the data portion of a ctable formatted grid. */
@@ -66,6 +98,79 @@ int nad_ctable_load( projCtx ctx, struct CTABLE *ct, FILE *fid )
pj_dalloc( ct->cvs );
ct->cvs = NULL;
+ pj_log( ctx, PJ_LOG_ERROR,
+ "ctable loading failed on fread() - binary incompatible?\n" );
+ pj_ctx_set_errno( ctx, -38 );
+ return 0;
+ }
+
+ return 1;
+}
+
+/************************************************************************/
+/* nad_ctable_init() */
+/* */
+/* Read the header portion of a "ctable" format grid. */
+/************************************************************************/
+
+struct CTABLE *nad_ctable_init( projCtx ctx, FILE * fid )
+{
+ struct CTABLE *ct;
+ int id_end;
+
+ /* read the table header */
+ ct = (struct CTABLE *) pj_malloc(sizeof(struct CTABLE));
+ if( ct == NULL
+ || fread( ct, sizeof(struct CTABLE), 1, fid ) != 1 )
+ {
+ pj_ctx_set_errno( ctx, -38 );
+ return NULL;
+ }
+
+ /* do some minimal validation to ensure the structure isn't corrupt */
+ if( ct->lim.lam < 1 || ct->lim.lam > 100000
+ || ct->lim.phi < 1 || ct->lim.phi > 100000 )
+ {
+ pj_ctx_set_errno( ctx, -38 );
+ return NULL;
+ }
+
+ /* trim white space and newlines off id */
+ for( id_end = strlen(ct->id)-1; id_end > 0; id_end-- )
+ {
+ if( ct->id[id_end] == '\n' || ct->id[id_end] == ' ' )
+ ct->id[id_end] = '\0';
+ else
+ break;
+ }
+
+ ct->cvs = NULL;
+
+ return ct;
+}
+
+/************************************************************************/
+/* nad_ctable2_load() */
+/* */
+/* Load the data portion of a ctable2 formatted grid. */
+/************************************************************************/
+
+int nad_ctable2_load( projCtx ctx, struct CTABLE *ct, FILE *fid )
+
+{
+ int a_size;
+
+ fseek( fid, 160, SEEK_SET );
+
+ /* read all the actual shift values */
+ a_size = ct->lim.lam * ct->lim.phi;
+ ct->cvs = (FLP *) pj_malloc(sizeof(FLP) * a_size);
+ if( ct->cvs == NULL
+ || fread(ct->cvs, sizeof(FLP), a_size, fid) != a_size )
+ {
+ pj_dalloc( ct->cvs );
+ ct->cvs = NULL;
+
if( getenv("PROJ_DEBUG") != NULL )
{
fprintf( stderr,
@@ -76,29 +181,61 @@ int nad_ctable_load( projCtx ctx, struct CTABLE *ct, FILE *fid )
return 0;
}
+ if( !IS_LSB )
+ {
+ swap_words( ct->cvs, 4, a_size * 2 );
+ }
+
return 1;
}
/************************************************************************/
-/* nad_ctable_init() */
+/* nad_ctable2_init() */
/* */
-/* Read the header portion of a "ctable" format grid. */
+/* Read the header portion of a "ctable2" format grid. */
/************************************************************************/
-struct CTABLE *nad_ctable_init( projCtx ctx, FILE * fid )
+struct CTABLE *nad_ctable2_init( projCtx ctx, FILE * fid )
{
struct CTABLE *ct;
int id_end;
+ char header[160];
+
+ if( fread( header, sizeof(header), 1, fid ) != 1 )
+ {
+ pj_ctx_set_errno( ctx, -38 );
+ return NULL;
+ }
+
+ if( !IS_LSB )
+ {
+ swap_words( header + 96, 8, 4 );
+ swap_words( header + 128, 4, 2 );
+ }
+
+ if( strncmp(header,"CTABLE V2",9) != 0 )
+ {
+ pj_log( ctx, PJ_LOG_ERROR, "ctable2 - wrong header!" );
+ pj_ctx_set_errno( ctx, -38 );
+ return NULL;
+ }
/* read the table header */
ct = (struct CTABLE *) pj_malloc(sizeof(struct CTABLE));
- if( ct == NULL
- || fread( ct, sizeof(struct CTABLE), 1, fid ) != 1 )
+ if( ct == NULL )
{
pj_ctx_set_errno( ctx, -38 );
return NULL;
}
+ memcpy( ct->id, header + 16, 80 );
+ memcpy( &ct->ll.lam, header + 96, 8 );
+ memcpy( &ct->ll.phi, header + 104, 8 );
+ memcpy( &ct->del.lam, header + 112, 8 );
+ memcpy( &ct->del.phi, header + 120, 8 );
+ memcpy( &ct->lim.lam, header + 128, 4 );
+ memcpy( &ct->lim.phi, header + 132, 4 );
+
/* do some minimal validation to ensure the structure isn't corrupt */
if( ct->lim.lam < 1 || ct->lim.lam > 100000
|| ct->lim.phi < 1 || ct->lim.phi > 100000 )
@@ -142,7 +279,7 @@ struct CTABLE *nad_init(projCtx ctx, char *name)
if (!(fid = pj_open_lib(ctx, fname, "rb"))) {
return 0;
}
-
+
ct = nad_ctable_init( ctx, fid );
if( ct != NULL )
{