[421b7d2] | 1 | /* img.h |
---|
[d1b1380] | 2 | * Header file for routines to read and write Survex ".3d" image files |
---|
[4f70ebc] | 3 | * Copyright (C) Olly Betts 1993,1994,1997,2001,2002,2003,2004,2005,2006,2010,2011,2012,2013,2014 |
---|
[846746e] | 4 | * |
---|
[89231c4] | 5 | * This program is free software; you can redistribute it and/or modify |
---|
| 6 | * it under the terms of the GNU General Public License as published by |
---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 8 | * (at your option) any later version. |
---|
[846746e] | 9 | * |
---|
| 10 | * This program is distributed in the hope that it will be useful, |
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
[89231c4] | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
[846746e] | 14 | * |
---|
[89231c4] | 15 | * You should have received a copy of the GNU General Public License |
---|
| 16 | * along with this program; if not, write to the Free Software |
---|
[d7d7fa7] | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[d1b1380] | 18 | */ |
---|
| 19 | |
---|
| 20 | #ifndef IMG_H |
---|
| 21 | # define IMG_H |
---|
| 22 | |
---|
[1ee204e] | 23 | /* Define IMG_API_VERSION if you want more recent versions of the img API. |
---|
| 24 | * |
---|
| 25 | * 0 (default) The old API. date1 and date2 give the survey date as time_t. |
---|
| 26 | * Set to 0 for "unknown". |
---|
| 27 | * 1 days1 and days2 give survey dates as days since 1st Jan 1900. |
---|
| 28 | * Set to -1 for "unknown". |
---|
| 29 | */ |
---|
| 30 | #ifndef IMG_API_VERSION |
---|
| 31 | # define IMG_API_VERSION 0 |
---|
| 32 | #elif IMG_API_VERSION > 1 |
---|
| 33 | # error IMG_API_VERSION > 1 too new |
---|
| 34 | #endif |
---|
| 35 | |
---|
[8df22e9] | 36 | #ifdef __cplusplus |
---|
[f2daf3b] | 37 | extern "C" { |
---|
| 38 | #endif |
---|
| 39 | |
---|
[0d2b588] | 40 | #include <stdio.h> |
---|
[dfb7670] | 41 | #include <time.h> /* for time_t */ |
---|
[0d2b588] | 42 | |
---|
[d1b1380] | 43 | # define img_BAD -2 |
---|
| 44 | # define img_STOP -1 |
---|
| 45 | # define img_MOVE 0 |
---|
| 46 | # define img_LINE 1 |
---|
[437caf3] | 47 | /* NB: img_CROSS is never output and ignored on input. |
---|
[0ff23cb] | 48 | * Put crosses where labels are. */ |
---|
| 49 | /* # define img_CROSS 2 */ |
---|
[d1b1380] | 50 | # define img_LABEL 3 |
---|
[ee05463] | 51 | # define img_XSECT 4 |
---|
[d7d7fa7] | 52 | # define img_XSECT_END 5 |
---|
[c61aa79] | 53 | # define img_ERROR_INFO 6 |
---|
[5c3c61a] | 54 | |
---|
[ab42a9c] | 55 | /* Leg flags */ |
---|
[5c3c61a] | 56 | # define img_FLAG_SURFACE 0x01 |
---|
| 57 | # define img_FLAG_DUPLICATE 0x02 |
---|
[95c3272] | 58 | # define img_FLAG_SPLAY 0x04 |
---|
[d1b1380] | 59 | |
---|
[ab42a9c] | 60 | /* Station flags */ |
---|
[5cb0e72] | 61 | # define img_SFLAG_SURFACE 0x01 |
---|
| 62 | # define img_SFLAG_UNDERGROUND 0x02 |
---|
[dfb4240] | 63 | # define img_SFLAG_ENTRANCE 0x04 |
---|
| 64 | # define img_SFLAG_EXPORTED 0x08 |
---|
| 65 | # define img_SFLAG_FIXED 0x10 |
---|
[a2c33ae] | 66 | # define img_SFLAG_ANON 0x20 |
---|
[dcbcae0] | 67 | # define img_SFLAG_WALL 0x40 |
---|
[5cb0e72] | 68 | |
---|
[0c50ce3] | 69 | /* File-wide flags */ |
---|
| 70 | # define img_FFLAG_EXTENDED 0x80 |
---|
| 71 | |
---|
[8490ad9] | 72 | /* When writing img_XSECT, img_XFLAG_END in pimg->flags means this is the last |
---|
| 73 | * img_XSECT in this tube: |
---|
| 74 | */ |
---|
[ee05463] | 75 | # define img_XFLAG_END 0x01 |
---|
| 76 | |
---|
[eb5aea0] | 77 | # define img_STYLE_UNKNOWN -1 |
---|
| 78 | # define img_STYLE_NORMAL 0 |
---|
| 79 | # define img_STYLE_DIVING 1 |
---|
| 80 | # define img_STYLE_CARTESIAN 2 |
---|
| 81 | # define img_STYLE_CYLPOLAR 3 |
---|
[ac28f4f] | 82 | # define img_STYLE_NOSURVEY 4 |
---|
[eb5aea0] | 83 | |
---|
[be97baf] | 84 | /* 3D coordinates (in metres) */ |
---|
[0ed0e16] | 85 | typedef struct { |
---|
[a9f5117] | 86 | double x, y, z; |
---|
[0ed0e16] | 87 | } img_point; |
---|
[421b7d2] | 88 | |
---|
[d1b1380] | 89 | typedef struct { |
---|
[6f6356c4] | 90 | /* members you can access when reading (don't touch when writing) */ |
---|
[23f7ea7] | 91 | char *label; |
---|
[5c3c61a] | 92 | int flags; |
---|
[a2ad284] | 93 | char *title; |
---|
[ec812baa] | 94 | /* Older .3d format versions stored a human readable datestamp string. |
---|
| 95 | * Format versions >= 8 versions store a string consisting of "@" followed |
---|
| 96 | * by the number of seconds since midnight UTC on 1/1/1970. Some foreign |
---|
| 97 | * formats contain a human readable string, others no date information |
---|
| 98 | * (which results in "?" being returned). |
---|
[42bd3576] | 99 | */ |
---|
[a2ad284] | 100 | char *datestamp; |
---|
[4f70ebc] | 101 | /* The datestamp as a time_t (or (time_t)-1 if not available). |
---|
| 102 | * |
---|
| 103 | * For 3d format versions >= 8, this is a reliable value and in UTC. Older |
---|
| 104 | * 3d format versions store a human readable time, which img will attempt |
---|
| 105 | * to decode, but it may fail, particularly with handling timezones. Even |
---|
| 106 | * if it does work, beware that times in local time where DST applies are |
---|
| 107 | * inherently ambiguous around when the clocks go back. |
---|
| 108 | * |
---|
| 109 | * CMAP XYZ files contain a timestamp. It's probably in localtime (but |
---|
| 110 | * without any timezone information) and the example files are all pre-2000 |
---|
| 111 | * and have two digit years. We do our best to turn these into a useful |
---|
| 112 | * time_t value. |
---|
| 113 | */ |
---|
| 114 | time_t datestamp_numeric; |
---|
[eef68f9] | 115 | char separator; /* character used to separate survey levels ('.' usually) */ |
---|
[1ee204e] | 116 | #if IMG_API_VERSION == 0 |
---|
[b5a3219] | 117 | time_t date1, date2; |
---|
[1ee204e] | 118 | #else /* IMG_API_VERSION == 1 */ |
---|
| 119 | int days1, days2; |
---|
| 120 | #endif |
---|
[ee7af72] | 121 | double l, r, u, d; |
---|
[c61aa79] | 122 | /* Error information - valid when IMG_ERROR is returned: */ |
---|
| 123 | int n_legs; |
---|
| 124 | double length; |
---|
| 125 | double E, H, V; |
---|
| 126 | /* The filename actually opened (e.g. may have ".3d" added). */ |
---|
| 127 | char * filename_opened; |
---|
[eef68f9] | 128 | int is_extended_elevation; |
---|
[eb5aea0] | 129 | /* The style of the data - one of the img_STYLE_* constants above */ |
---|
| 130 | int style; |
---|
| 131 | |
---|
[5c3c61a] | 132 | /* all other members are for internal use only */ |
---|
[a420b49] | 133 | FILE *fh; /* file handle of image file */ |
---|
[0ff23cb] | 134 | char *label_buf; |
---|
[23f7ea7] | 135 | size_t buf_len; |
---|
[d69255f] | 136 | size_t label_len; |
---|
[1fad6d4] | 137 | int fRead; /* 1 for reading, 0 for writing */ |
---|
[f2588ca] | 138 | long start; |
---|
[0da6e60] | 139 | /* version of file format: |
---|
[f1c7c9c7] | 140 | * -4 => CMAP .xyz file, shot format |
---|
| 141 | * -3 => CMAP .xyz file, station format |
---|
[ad4eaf3f] | 142 | * -2 => Compass .plt file |
---|
[0da6e60] | 143 | * -1 => .pos file |
---|
| 144 | * 0 => 0.01 ascii |
---|
| 145 | * 1 => 0.01 binary, |
---|
| 146 | * 2 => byte actions and flags |
---|
[ad4eaf3f] | 147 | * 3 => prefixes for legs; compressed prefixes |
---|
[b5a3219] | 148 | * 4 => survey date |
---|
[f4c5932] | 149 | * 5 => LRUD info |
---|
[c61aa79] | 150 | * 6 => error info |
---|
[1ee204e] | 151 | * 7 => more compact dates with wider range |
---|
[ec812baa] | 152 | * 8 => lots of changes |
---|
[0da6e60] | 153 | */ |
---|
[5c3c61a] | 154 | int version; |
---|
[76bbb7c9] | 155 | char *survey; |
---|
| 156 | size_t survey_len; |
---|
| 157 | int pending; /* for old style text format files and survey filtering */ |
---|
| 158 | img_point mv; |
---|
[1ee204e] | 159 | #if IMG_API_VERSION == 0 |
---|
[ee05463] | 160 | time_t olddate1, olddate2; |
---|
[1ee204e] | 161 | #else /* IMG_API_VERSION == 1 */ |
---|
| 162 | int olddays1, olddays2; |
---|
| 163 | #endif |
---|
[eb5aea0] | 164 | int oldstyle; |
---|
[d1b1380] | 165 | } img; |
---|
| 166 | |
---|
[5c3c61a] | 167 | /* Which version of the file format to output (defaults to newest) */ |
---|
| 168 | extern unsigned int img_output_version; |
---|
| 169 | |
---|
[2bb17c9] | 170 | /* Minimum supported value for img_output_version: */ |
---|
| 171 | #define IMG_VERSION_MIN 1 |
---|
| 172 | |
---|
| 173 | /* Maximum supported value for img_output_version: */ |
---|
[ec812baa] | 174 | #define IMG_VERSION_MAX 8 |
---|
[2bb17c9] | 175 | |
---|
[0ed0e16] | 176 | /* Open a .3d file for reading |
---|
[d1b1380] | 177 | * fnm is the filename |
---|
| 178 | * Returns pointer to an img struct or NULL |
---|
| 179 | */ |
---|
[a2ad284] | 180 | #define img_open(F) img_open_survey((F), NULL) |
---|
[76bbb7c9] | 181 | |
---|
| 182 | /* Open a .3d file for reading |
---|
| 183 | * fnm is the filename |
---|
| 184 | * Returns pointer to an img struct or NULL |
---|
| 185 | * survey points to a survey name to restrict reading to (or NULL for all |
---|
| 186 | * survey data in the file) |
---|
| 187 | */ |
---|
[a2ad284] | 188 | img *img_open_survey(const char *fnm, const char *survey); |
---|
[d1b1380] | 189 | |
---|
| 190 | /* Open a .3d file for output |
---|
| 191 | * fnm is the filename |
---|
[0c50ce3] | 192 | * title is the title |
---|
| 193 | * flags contains a bitwise-or of any file-wide flags - currently only one |
---|
| 194 | * is available: img_FFLAG_EXTENDED. (The third parameter used to be |
---|
| 195 | * 'fBinary', but has been ignored for many years, so the parameter has |
---|
| 196 | * been repurposed for flags - for this reason, img.c deliberately ignores bit |
---|
| 197 | * 1 being set, but callers should be written/updated not to set it). |
---|
| 198 | * |
---|
[ad9a5cd] | 199 | * Returns pointer to an img struct or NULL for error (check img_error() |
---|
| 200 | * for details) |
---|
[d1b1380] | 201 | */ |
---|
[0c50ce3] | 202 | img *img_open_write(const char *fnm, char *title, int flags); |
---|
[d1b1380] | 203 | |
---|
[0ed0e16] | 204 | /* Read an item from a .3d file |
---|
[d1b1380] | 205 | * pimg is a pointer to an img struct returned by img_open() |
---|
[0ed0e16] | 206 | * coordinates are returned in p |
---|
[23f7ea7] | 207 | * flags and label name are returned in fields in pimg |
---|
[d1b1380] | 208 | * Returns img_XXXX as #define-d above |
---|
| 209 | */ |
---|
[23f7ea7] | 210 | int img_read_item(img *pimg, img_point *p); |
---|
[d1b1380] | 211 | |
---|
[0ed0e16] | 212 | /* Write a item to a .3d file |
---|
[d1b1380] | 213 | * pimg is a pointer to an img struct returned by img_open_write() |
---|
| 214 | * code is one of the img_XXXX #define-d above |
---|
[ee05463] | 215 | * flags is the leg, station, or xsect flags |
---|
| 216 | * (meaningful for img_LINE, img_LABEL, and img_XSECT respectively) |
---|
[95c3272] | 217 | * s is the label (only meaningful for img_LABEL) |
---|
[0ed0e16] | 218 | * x, y, z are the coordinates |
---|
[d1b1380] | 219 | */ |
---|
[95c3272] | 220 | void img_write_item(img *pimg, int code, int flags, const char *s, |
---|
[a9f5117] | 221 | double x, double y, double z); |
---|
[f2588ca] | 222 | |
---|
[c61aa79] | 223 | /* Write error information for the current traverse |
---|
| 224 | * n_legs is the number of legs in the traverse |
---|
| 225 | * length is the traverse length (in m) |
---|
| 226 | * E is the ratio of the observed misclosure to the theoretical one |
---|
| 227 | * H is the ratio of the observed horizontal misclosure to the theoretical one |
---|
| 228 | * V is the ratio of the observed vertical misclosure to the theoretical one |
---|
| 229 | */ |
---|
| 230 | void img_write_errors(img *pimg, int n_legs, double length, |
---|
| 231 | double E, double H, double V); |
---|
| 232 | |
---|
[f2588ca] | 233 | /* rewind a .3d file opened for reading so the data can be read in |
---|
| 234 | * several passes |
---|
| 235 | * pimg is a pointer to an img struct returned by img_open() |
---|
[9f3e5df] | 236 | * Returns: non-zero for success, zero for error (check img_error() for |
---|
| 237 | * details) |
---|
[f2588ca] | 238 | */ |
---|
[9f3e5df] | 239 | int img_rewind(img *pimg); |
---|
[f2588ca] | 240 | |
---|
[d1b1380] | 241 | /* Close a .3d file |
---|
| 242 | * pimg is a pointer to an img struct returned by img_open() or |
---|
| 243 | * img_open_write() |
---|
[ad9a5cd] | 244 | * Returns: non-zero for success, zero for error (check img_error() for |
---|
| 245 | * details) |
---|
[d1b1380] | 246 | */ |
---|
[ad9a5cd] | 247 | int img_close(img *pimg); |
---|
[d1b1380] | 248 | |
---|
| 249 | /* Codes returned by img_error */ |
---|
[a420b49] | 250 | typedef enum { |
---|
| 251 | IMG_NONE = 0, IMG_FILENOTFOUND, IMG_OUTOFMEMORY, |
---|
[ad9a5cd] | 252 | IMG_CANTOPENOUT, IMG_BADFORMAT, IMG_DIRECTORY, |
---|
[6c1c4f3] | 253 | IMG_READERROR, IMG_WRITEERROR, IMG_TOONEW |
---|
[a420b49] | 254 | } img_errcode; |
---|
[d1b1380] | 255 | |
---|
| 256 | /* Read the error code |
---|
[a405bc1] | 257 | * If img_open(), img_open_survey() or img_open_write() returns NULL, or |
---|
| 258 | * img_rewind() or img_close() returns 0, or img_read_item() returns img_BAD |
---|
| 259 | * then you can call this function to discover why. |
---|
| 260 | */ |
---|
[a420b49] | 261 | img_errcode img_error(void); |
---|
[d1b1380] | 262 | |
---|
[8df22e9] | 263 | #ifdef __cplusplus |
---|
[f2daf3b] | 264 | } |
---|
| 265 | #endif |
---|
| 266 | |
---|
[d1b1380] | 267 | #endif |
---|