source: git/src/img.h @ 76cf7f1

RELEASE/1.2debug-cidebug-ci-sanitiserswalls-datawalls-data-hanging-as-warning
Last change on this file since 76cf7f1 was 942629a, checked in by Olly Betts <olly@…>, 6 years ago

Make clearer that img can read more formats

The documentation in the header previously implied that it could
only read Survex .3d files.

  • Property mode set to 100644
File size: 13.3 KB
RevLine 
[421b7d2]1/* img.h
[942629a]2 * Header file for routines to read and write processed survey data files
3 *
4 * These routines support reading processed survey data in a variety of formats
5 * - currently:
6 *
7 * - Survex ".3d" image files
8 * - Survex ".pos" files
9 * - Compass Plot files (".plt" and ".plf")
10 * - CMAP XYZ files (".sht", ".adj", ".una", ".xyz")
11 *
12 * Writing Survex ".3d" image files is supported.
13 *
[7826201]14 * Copyright (C) Olly Betts 1993,1994,1997,2001,2002,2003,2004,2005,2006,2010,2011,2012,2013,2014,2016,2018
[846746e]15 *
[89231c4]16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
[846746e]20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
[89231c4]23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU General Public License for more details.
[846746e]25 *
[89231c4]26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
[d7d7fa7]28 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
[d1b1380]29 */
30
31#ifndef IMG_H
32# define IMG_H
33
[1ee204e]34/* Define IMG_API_VERSION if you want more recent versions of the img API.
35 *
36 * 0 (default)  The old API.  date1 and date2 give the survey date as time_t.
37 *              Set to 0 for "unknown".
38 * 1            days1 and days2 give survey dates as days since 1st Jan 1900.
39 *              Set to -1 for "unknown".
40 */
41#ifndef IMG_API_VERSION
42# define IMG_API_VERSION 0
43#elif IMG_API_VERSION > 1
44# error IMG_API_VERSION > 1 too new
45#endif
46
[8df22e9]47#ifdef __cplusplus
[f2daf3b]48extern "C" {
49#endif
50
[0d2b588]51#include <stdio.h>
[dfb7670]52#include <time.h> /* for time_t */
[0d2b588]53
[d1b1380]54# define img_BAD   -2
55# define img_STOP  -1
56# define img_MOVE   0
57# define img_LINE   1
[437caf3]58/* NB: img_CROSS is never output and ignored on input.
[0ff23cb]59 * Put crosses where labels are. */
60/* # define img_CROSS  2 */
[d1b1380]61# define img_LABEL  3
[ee05463]62# define img_XSECT  4
[d7d7fa7]63# define img_XSECT_END 5
[22753f1]64/* Loop closure information for the *preceding* traverse (img_MOVE + one or
[5dc3775]65 * more img_LINEs). */
[c61aa79]66# define img_ERROR_INFO 6
[5c3c61a]67
[ab42a9c]68/* Leg flags */
[5c3c61a]69# define img_FLAG_SURFACE   0x01
70# define img_FLAG_DUPLICATE 0x02
[95c3272]71# define img_FLAG_SPLAY     0x04
[d1b1380]72
[ab42a9c]73/* Station flags */
[5cb0e72]74# define img_SFLAG_SURFACE     0x01
75# define img_SFLAG_UNDERGROUND 0x02
[dfb4240]76# define img_SFLAG_ENTRANCE    0x04
77# define img_SFLAG_EXPORTED    0x08
78# define img_SFLAG_FIXED       0x10
[a2c33ae]79# define img_SFLAG_ANON        0x20
[dcbcae0]80# define img_SFLAG_WALL        0x40
[5cb0e72]81
[0c50ce3]82/* File-wide flags */
83# define img_FFLAG_EXTENDED 0x80
84
[8490ad9]85/* When writing img_XSECT, img_XFLAG_END in pimg->flags means this is the last
86 * img_XSECT in this tube:
87 */
[ee05463]88# define img_XFLAG_END      0x01
89
[eb5aea0]90# define img_STYLE_UNKNOWN   -1
91# define img_STYLE_NORMAL     0
92# define img_STYLE_DIVING     1
93# define img_STYLE_CARTESIAN  2
94# define img_STYLE_CYLPOLAR   3
[ac28f4f]95# define img_STYLE_NOSURVEY   4
[eb5aea0]96
[be97baf]97/* 3D coordinates (in metres) */
[0ed0e16]98typedef struct {
[a9f5117]99   double x, y, z;
[0ed0e16]100} img_point;
[421b7d2]101
[d1b1380]102typedef struct {
[fbd182d]103   /* Members you can access when reading (don't touch when writing): */
[23f7ea7]104   char *label;
[5c3c61a]105   int flags;
[a2ad284]106   char *title;
[43d63e57]107   /* If the coordinate system was specified, this contains a PROJ4 string
108    * describing it.  If not, this member will be NULL.
109    */
110   char *cs;
[ec812baa]111   /* Older .3d format versions stored a human readable datestamp string.
112    * Format versions >= 8 versions store a string consisting of "@" followed
113    * by the number of seconds since midnight UTC on 1/1/1970.  Some foreign
114    * formats contain a human readable string, others no date information
115    * (which results in "?" being returned).
[42bd3576]116    */
[a2ad284]117   char *datestamp;
[4f70ebc]118   /* The datestamp as a time_t (or (time_t)-1 if not available).
119    *
120    * For 3d format versions >= 8, this is a reliable value and in UTC.  Older
121    * 3d format versions store a human readable time, which img will attempt
122    * to decode, but it may fail, particularly with handling timezones.  Even
123    * if it does work, beware that times in local time where DST applies are
124    * inherently ambiguous around when the clocks go back.
125    *
126    * CMAP XYZ files contain a timestamp.  It's probably in localtime (but
127    * without any timezone information) and the example files are all pre-2000
128    * and have two digit years.  We do our best to turn these into a useful
129    * time_t value.
130    */
131   time_t datestamp_numeric;
[eef68f9]132   char separator; /* character used to separate survey levels ('.' usually) */
[fbd182d]133
134   /* Members that can be set when writing: */
[1ee204e]135#if IMG_API_VERSION == 0
[b5a3219]136   time_t date1, date2;
[1ee204e]137#else /* IMG_API_VERSION == 1 */
138   int days1, days2;
139#endif
[ee7af72]140   double l, r, u, d;
[fbd182d]141
[5dc3775]142   /* Error information - valid when img_ERROR_INFO is returned: */
[c61aa79]143   int n_legs;
144   double length;
145   double E, H, V;
[fbd182d]146
[8be4d09]147   /* The filename actually opened (e.g. may have ".3d" added).
148    *
149    * This is only set if img opened the filename - if an existing stream
150    * is used (via img_read_stream() or similar) then this member will be
151    * NULL.
152    */
[c61aa79]153   char * filename_opened;
[fbd182d]154
155   /* Non-zero if reading an extended elevation: */
[eef68f9]156   int is_extended_elevation;
[fbd182d]157
158   /* Members that can be set when writing: */
[eb5aea0]159   /* The style of the data - one of the img_STYLE_* constants above */
160   int style;
161
[fbd182d]162   /* All other members are for internal use only: */
[a420b49]163   FILE *fh;          /* file handle of image file */
[8be4d09]164   int (*close_func)(FILE*);
[0ff23cb]165   char *label_buf;
[23f7ea7]166   size_t buf_len;
[d69255f]167   size_t label_len;
[1fad6d4]168   int fRead;        /* 1 for reading, 0 for writing */
[f2588ca]169   long start;
[0da6e60]170   /* version of file format:
[f1c7c9c7]171    *  -4 => CMAP .xyz file, shot format
172    *  -3 => CMAP .xyz file, station format
[ad4eaf3f]173    *  -2 => Compass .plt file
[0da6e60]174    *  -1 => .pos file
175    *   0 => 0.01 ascii
176    *   1 => 0.01 binary,
177    *   2 => byte actions and flags
[ad4eaf3f]178    *   3 => prefixes for legs; compressed prefixes
[b5a3219]179    *   4 => survey date
[f4c5932]180    *   5 => LRUD info
[c61aa79]181    *   6 => error info
[1ee204e]182    *   7 => more compact dates with wider range
[ec812baa]183    *   8 => lots of changes
[0da6e60]184    */
[5c3c61a]185   int version;
[76bbb7c9]186   char *survey;
187   size_t survey_len;
188   int pending; /* for old style text format files and survey filtering */
189   img_point mv;
[1ee204e]190#if IMG_API_VERSION == 0
[ee05463]191   time_t olddate1, olddate2;
[1ee204e]192#else /* IMG_API_VERSION == 1 */
193   int olddays1, olddays2;
194#endif
[eb5aea0]195   int oldstyle;
[d1b1380]196} img;
197
[5c3c61a]198/* Which version of the file format to output (defaults to newest) */
199extern unsigned int img_output_version;
200
[2bb17c9]201/* Minimum supported value for img_output_version: */
202#define IMG_VERSION_MIN 1
203
204/* Maximum supported value for img_output_version: */
[ec812baa]205#define IMG_VERSION_MAX 8
[2bb17c9]206
[942629a]207/* Open a processed survey data file for reading
[7826201]208 *
[d1b1380]209 * fnm is the filename
[7826201]210 *
[d1b1380]211 * Returns pointer to an img struct or NULL
212 */
[a2ad284]213#define img_open(F) img_open_survey((F), NULL)
[76bbb7c9]214
[942629a]215/* Open a processed survey data file for reading
[7826201]216 *
[76bbb7c9]217 * fnm is the filename
[7826201]218 *
[76bbb7c9]219 * survey points to a survey name to restrict reading to (or NULL for all
220 * survey data in the file)
[7826201]221 *
222 * Returns pointer to an img struct or NULL
[76bbb7c9]223 */
[a2ad284]224img *img_open_survey(const char *fnm, const char *survey);
[d1b1380]225
[22753f1]226/* Read processed survey data from an existing stream.
[8be4d09]227 *
228 * stream is a FILE* open on the stream (can be NULL which will give error
229 * IMG_FILENOTFOUND so you don't need to handle that case specially).  The
230 * stream should be opened for reading in binary mode and positioned at the
231 * start of the survey data file.
232 *
233 * close_func is a function to call to close the stream (most commonly
234 * fclose, or pclose if the stream was opened using popen()) or NULL if
235 * the caller wants to take care of closing the stream.
236 *
237 * filename is used to determine the format based on the file extension,
238 * and also the leafname with the extension removed is used for the survey
239 * title for formats which don't support a title or when no title is
240 * specified.  If you're not interested in default titles, you can just
241 * pass the extension including a leading "." - e.g. ".3d".  May not be
242 * NULL.
243 *
244 * Returns pointer to an img struct or NULL on error.  Any close function
245 * specified is called on error (unless stream is NULL).
246 */
247#define img_read_stream(S, C, F) img_read_stream_survey((S), (C), (F), NULL)
248
[22753f1]249/* Read processed survey data from an existing stream.
[8be4d09]250 *
251 * stream is a FILE* open on the stream (can be NULL which will give error
252 * IMG_FILENOTFOUND so you don't need to handle that case specially).  The
253 * stream should be opened for reading in binary mode and positioned at the
254 * start of the survey data file.
255 *
256 * close_func is a function to call to close the stream (most commonly
257 * fclose, or pclose if the stream was opened using popen()) or NULL if
258 * the caller wants to take care of closing the stream.
259 *
260 * filename is used to determine the format based on the file extension,
261 * and also the leafname with the extension removed is used for the survey
262 * title for formats which don't support a title or when no title is
263 * specified.  If you're not interested in default titles, you can just
264 * pass the extension including a leading "." - e.g. ".3d".  filename must
265 * not be NULL.
266 *
267 * survey points to a survey name to restrict reading to (or NULL for all
268 * survey data in the file)
269 *
270 * Returns pointer to an img struct or NULL on error.  Any close function
271 * specified is called on error.
272 */
273img *img_read_stream_survey(FILE *stream, int (*close_func)(FILE*),
274                            const char *filename,
275                            const char *survey);
276
[a3b3aef]277/* Open a .3d file for output with no specified coordinate system
278 *
279 * This is a very thin wrapper around img_open_write_cs() which passes NULL for
280 * cs, provided for compatibility with the API provided before support for
281 * coordinate systems was added.
282 *
283 * See img_open_write_cs() for documentation.
284 */
285#define img_open_write(F, T, S) img_open_write_cs(F, T, NULL, S)
286
287/* Open a .3d file for output in a specified coordinate system
[7826201]288 *
[d1b1380]289 * fnm is the filename
[7826201]290 *
[0c50ce3]291 * title is the title
[7826201]292 *
[a3b3aef]293 * cs is a PROJ4 string describing the coordinate system (or NULL to not
294 * specify a coordinate system).
295 *
[0c50ce3]296 * flags contains a bitwise-or of any file-wide flags - currently only one
[a3b3aef]297 * is available: img_FFLAG_EXTENDED.
[0c50ce3]298 *
[ad9a5cd]299 * Returns pointer to an img struct or NULL for error (check img_error()
300 * for details)
[d1b1380]301 */
[a3b3aef]302img *img_open_write_cs(const char *fnm, const char *title, const char * cs,
303                       int flags);
[43d63e57]304
[a3b3aef]305/* Write a .3d file to a stream
[7826201]306 *
[a3b3aef]307 * stream is a FILE* open on the stream (can be NULL which will give error
308 * IMG_FILENOTFOUND so you don't need to handle that case specially).  The
309 * stream should be opened for writing in binary mode.
310 *
311 * close_func is a function to call to close the stream (most commonly
312 * fclose, or pclose if the stream was opened using popen()) or NULL if
313 * the caller wants to take care of closing the stream.
[7826201]314 *
[43d63e57]315 * title is the title
[7826201]316 *
[a3b3aef]317 * cs is a PROJ4 string describing the coordinate system (or NULL to not
318 * specify a coordinate system).
[8be4d09]319 *
[43d63e57]320 * flags contains a bitwise-or of any file-wide flags - currently only one
321 * is available: img_FFLAG_EXTENDED.
322 *
323 * Returns pointer to an img struct or NULL for error (check img_error()
[a3b3aef]324 * for details).  Any close function specified is called on error (unless
325 * stream is NULL).
[43d63e57]326 */
[a3b3aef]327img *img_write_stream(FILE *stream, int (*close_func)(FILE*),
328                      const char *title, const char * cs, int flags);
[d1b1380]329
[942629a]330/* Read an item from a processed survey data file
[7826201]331 *
[d1b1380]332 * pimg is a pointer to an img struct returned by img_open()
[7826201]333 *
[0ed0e16]334 * coordinates are returned in p
[7826201]335 *
[23f7ea7]336 * flags and label name are returned in fields in pimg
[7826201]337 *
[d1b1380]338 * Returns img_XXXX as #define-d above
339 */
[23f7ea7]340int img_read_item(img *pimg, img_point *p);
[d1b1380]341
[0ed0e16]342/* Write a item to a .3d file
[7826201]343 *
[d1b1380]344 * pimg is a pointer to an img struct returned by img_open_write()
[7826201]345 *
[d1b1380]346 * code is one of the img_XXXX #define-d above
[7826201]347 *
[ee05463]348 * flags is the leg, station, or xsect flags
349 * (meaningful for img_LINE, img_LABEL, and img_XSECT respectively)
[7826201]350 *
[95c3272]351 * s is the label (only meaningful for img_LABEL)
[7826201]352 *
[0ed0e16]353 * x, y, z are the coordinates
[d1b1380]354 */
[95c3272]355void img_write_item(img *pimg, int code, int flags, const char *s,
[a9f5117]356                    double x, double y, double z);
[f2588ca]357
[c61aa79]358/* Write error information for the current traverse
[7826201]359 *
[c61aa79]360 * n_legs is the number of legs in the traverse
[7826201]361 *
[c61aa79]362 * length is the traverse length (in m)
[7826201]363 *
[c61aa79]364 * E is the ratio of the observed misclosure to the theoretical one
[7826201]365 *
[c61aa79]366 * H is the ratio of the observed horizontal misclosure to the theoretical one
[7826201]367 *
[c61aa79]368 * V is the ratio of the observed vertical misclosure to the theoretical one
369 */
370void img_write_errors(img *pimg, int n_legs, double length,
371                      double E, double H, double V);
372
[942629a]373/* rewind a processed survey data file opened for reading
374 *
375 * This is useful if you want to read the data in several passes.
[7826201]376 *
[f2588ca]377 * pimg is a pointer to an img struct returned by img_open()
[7826201]378 *
[9f3e5df]379 * Returns: non-zero for success, zero for error (check img_error() for
380 *   details)
[f2588ca]381 */
[9f3e5df]382int img_rewind(img *pimg);
[f2588ca]383
[942629a]384/* Close a processed survey data file
[7826201]385 *
[d1b1380]386 * pimg is a pointer to an img struct returned by img_open() or
387 *   img_open_write()
[7826201]388 *
[ad9a5cd]389 * Returns: non-zero for success, zero for error (check img_error() for
390 *   details)
[d1b1380]391 */
[ad9a5cd]392int img_close(img *pimg);
[d1b1380]393
394/* Codes returned by img_error */
[a420b49]395typedef enum {
396   IMG_NONE = 0, IMG_FILENOTFOUND, IMG_OUTOFMEMORY,
[ad9a5cd]397   IMG_CANTOPENOUT, IMG_BADFORMAT, IMG_DIRECTORY,
[6c1c4f3]398   IMG_READERROR, IMG_WRITEERROR, IMG_TOONEW
[a420b49]399} img_errcode;
[d1b1380]400
401/* Read the error code
[7826201]402 *
[a405bc1]403 * If img_open(), img_open_survey() or img_open_write() returns NULL, or
404 * img_rewind() or img_close() returns 0, or img_read_item() returns img_BAD
405 * then you can call this function to discover why.
406 */
[a420b49]407img_errcode img_error(void);
[d1b1380]408
[8df22e9]409#ifdef __cplusplus
[f2daf3b]410}
411#endif
412
[d1b1380]413#endif
Note: See TracBrowser for help on using the repository browser.