00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <stdio.h>
00015 #include <stdarg.h>
00016 #include <AR/param.h>
00017
00018 #ifdef AR_LITTLE_ENDIAN
00019 typedef union {
00020 int x;
00021 unsigned char y[4];
00022 } SwapIntT;
00023
00024 typedef union {
00025 double x;
00026 unsigned char y[8];
00027 } SwapDoubleT;
00028
00029 static void byteSwapInt( int *from, int *to )
00030 {
00031 SwapIntT *w1, *w2;
00032 int i;
00033
00034 w1 = (SwapIntT *)from;
00035 w2 = (SwapIntT *)to;
00036 for( i = 0; i < 4; i++ ) {
00037 w2->y[i] = w1->y[3-i];
00038 }
00039
00040 return;
00041 }
00042
00043 static void byteSwapDouble( double *from, double *to )
00044 {
00045 SwapDoubleT *w1, *w2;
00046 int i;
00047
00048 w1 = (SwapDoubleT *)from;
00049 w2 = (SwapDoubleT *)to;
00050 for( i = 0; i < 8; i++ ) {
00051 w2->y[i] = w1->y[7-i];
00052 }
00053
00054 return;
00055 }
00056
00057 static void byteswap( ARParam *param )
00058 {
00059 ARParam wparam;
00060 int i, j;
00061
00062 byteSwapInt( &(param->xsize), &(wparam.xsize) );
00063 byteSwapInt( &(param->ysize), &(wparam.ysize) );
00064
00065 for( j = 0; j < 3; j++ ) {
00066 for( i = 0; i < 4; i++ ) {
00067 byteSwapDouble( &(param->mat[j][i]), &(wparam.mat[j][i]) );
00068 }
00069 }
00070
00071 for( i = 0; i < 4; i++ ) {
00072 byteSwapDouble( &(param->dist_factor[i]), &(wparam.dist_factor[i]) );
00073 }
00074
00075 *param = wparam;
00076 }
00077
00078 static void byteswap2( ARSParam *sparam )
00079 {
00080 ARSParam wsparam;
00081 int i, j;
00082
00083 byteSwapInt( &(sparam->xsize), &(wsparam.xsize) );
00084 byteSwapInt( &(sparam->ysize), &(wsparam.ysize) );
00085
00086 for( j = 0; j < 3; j++ ) {
00087 for( i = 0; i < 4; i++ ) {
00088 byteSwapDouble( &(sparam->matL[j][i]), &(wsparam.matL[j][i]) );
00089 byteSwapDouble( &(sparam->matR[j][i]), &(wsparam.matR[j][i]) );
00090 byteSwapDouble( &(sparam->matL2R[j][i]), &(wsparam.matL2R[j][i]) );
00091 }
00092 }
00093 for( i = 0; i < 4; i++ ) {
00094 byteSwapDouble( &(sparam->dist_factorL[i]), &(wsparam.dist_factorL[i]) );
00095 byteSwapDouble( &(sparam->dist_factorR[i]), &(wsparam.dist_factorR[i]) );
00096 }
00097
00098 *sparam = wsparam;
00099 }
00100 #endif
00101
00102
00103 int arParamSave( char *filename, int num, ARParam *param, ...)
00104 {
00105 FILE *fp;
00106 va_list ap;
00107 ARParam *param1;
00108 int i;
00109
00110 if( num < 1 ) return -1;
00111
00112 fp = fopen( filename, "wb" );
00113 if( fp == NULL ) return -1;
00114
00115 #ifdef AR_LITTLE_ENDIAN
00116 byteswap( param );
00117 #endif
00118 if( fwrite( param, sizeof(ARParam), 1, fp ) != 1 ) {
00119 fclose(fp);
00120 #ifdef AR_LITTLE_ENDIAN
00121 byteswap( param );
00122 #endif
00123 return -1;
00124 }
00125 #ifdef AR_LITTLE_ENDIAN
00126 byteswap( param );
00127 #endif
00128
00129 va_start(ap, param);
00130 for( i = 1; i < num; i++ ) {
00131 param1 = va_arg(ap, ARParam *);
00132 #ifdef AR_LITTLE_ENDIAN
00133 byteswap( param1 );
00134 #endif
00135 if( fwrite( param1, sizeof(ARParam), 1, fp ) != 1 ) {
00136 fclose(fp);
00137 #ifdef AR_LITTLE_ENDIAN
00138 byteswap( param1 );
00139 #endif
00140 return -1;
00141 }
00142 #ifdef AR_LITTLE_ENDIAN
00143 byteswap( param1 );
00144 #endif
00145 }
00146
00147 fclose(fp);
00148
00149 return 0;
00150 }
00151
00152 int arParamLoad( const char *filename, int num, ARParam *param, ...)
00153 {
00154 FILE *fp;
00155 va_list ap;
00156 ARParam *param1;
00157 int i;
00158
00159 if( num < 1 ) return -1;
00160
00161 fp = fopen( filename, "rb" );
00162 if( fp == NULL ) return -1;
00163
00164 if( fread( param, sizeof(ARParam), 1, fp ) != 1 ) {
00165 fclose(fp);
00166 return -1;
00167 }
00168 #ifdef AR_LITTLE_ENDIAN
00169 byteswap( param );
00170 #endif
00171
00172 va_start(ap, param);
00173 for( i = 1; i < num; i++ ) {
00174 param1 = va_arg(ap, ARParam *);
00175 if( fread( param1, sizeof(ARParam), 1, fp ) != 1 ) {
00176 fclose(fp);
00177 return -1;
00178 }
00179 #ifdef AR_LITTLE_ENDIAN
00180 byteswap( param1 );
00181 #endif
00182 }
00183
00184 fclose(fp);
00185
00186 return 0;
00187 }
00188
00189 int arsParamSave( char *filename, ARSParam *sparam )
00190 {
00191 FILE *fp;
00192
00193 fp = fopen( filename, "wb" );
00194 if( fp == NULL ) return -1;
00195
00196 #ifdef AR_LITTLE_ENDIAN
00197 byteswap2( sparam );
00198 #endif
00199 if( fwrite( sparam, sizeof(ARSParam), 1, fp ) != 1 ) {
00200 fclose(fp);
00201 #ifdef AR_LITTLE_ENDIAN
00202 byteswap2( sparam );
00203 #endif
00204 return -1;
00205 }
00206 #ifdef AR_LITTLE_ENDIAN
00207 byteswap2( sparam );
00208 #endif
00209
00210 fclose(fp);
00211
00212 return 0;
00213 }
00214
00215 int arsParamLoad( char *filename, ARSParam *sparam )
00216 {
00217 FILE *fp;
00218
00219 fp = fopen( filename, "rb" );
00220 if( fp == NULL ) return -1;
00221
00222 if( fread( sparam, sizeof(ARSParam), 1, fp ) != 1 ) {
00223 fclose(fp);
00224 return -1;
00225 }
00226 #ifdef AR_LITTLE_ENDIAN
00227 byteswap2( sparam );
00228 #endif
00229
00230 fclose(fp);
00231
00232 return 0;
00233 }