source: git/src/img.c @ ed0f5b6

RELEASE/1.0RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereostereo-2025walls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since ed0f5b6 was ed0f5b6, checked in by Olly Betts <olly@…>, 24 years ago

"cavern --quiet" no longer suppresses node statistics.

extend: give up if the .3d file is truncated or corrupted part way
through.

Fixed dropping of first character of survey station names when
reading old .3d files.

git-svn-id: file:///home/survex-svn/survex/trunk@1199 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 19.7 KB
Line 
1/* > img.c
2 * Routines for reading and writing Survex ".3d" image files
3 * Copyright (C) 1993-2001 Olly Betts
4 *
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.
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
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
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
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
23
24#include <stdio.h>
25#include <string.h>
26#include <stdlib.h>
27#include <time.h>
28#include <ctype.h>
29
30#include "img.h"
31#ifdef IMG_HOSTED
32# include "debug.h"
33# include "filelist.h"
34# include "filename.h"
35# include "message.h"
36# include "useful.h"
37# define TIMENA msg(/*Date and time not available.*/108)
38# define TIMEFMT msg(/*%a,%Y.%m.%d %H:%M:%S %Z*/107)
39#else
40# define INT32_T long
41# define TIMENA "Time not available."
42# define TIMEFMT "%a,%Y.%m.%d %H:%M:%S %Z"
43# define EXT_SVX_3D "3d"
44# define xosmalloc(L) malloc((L))
45# define xosrealloc(L,S) realloc((L),(S))
46# define osfree(P) free((P))
47# define ossizeof(T) sizeof(T)
48/* in IMG_HOSTED mode, this tests if a filename refers to a directory */
49# define fDirectory(X) 0
50/* open file FNM with mode MODE, maybe using path PTH and/or extension EXT */
51/* path isn't used in img.c, but EXT is */
52# define fopenWithPthAndExt(PTH,FNM,EXT,MODE,X) fopen(FNM,MODE)
53# define fFalse 0
54# define fTrue 1
55# define bool int
56/* dummy do {...} while(0) hack to permit stuff like
57 * if (s) fputsnl(s,fh); else exit(1);
58 * to work as intended
59 */
60# define fputsnl(S, FH) do {fputs((S), (FH)); putc('\n', (FH));} while(0)
61# define ASSERT(X)
62
63static long
64get32(FILE *fh)
65{
66   long w;
67   w = getc(fh);
68   w |= (long)getc(fh) << 8l;
69   w |= (long)getc(fh) << 16l;
70   w |= (long)getc(fh) << 24l;
71   return w;
72}
73
74static void
75put32(long w, FILE *fh)
76{
77   putc((char)(w), fh);
78   putc((char)(w >> 8l), fh);
79   putc((char)(w >> 16l), fh);
80   putc((char)(w >> 24l), fh);
81}
82
83#endif
84
85unsigned int img_output_version = 3;
86
87#define TMPBUFLEN 256
88static char tmpbuf[TMPBUFLEN];
89
90#ifdef IMG_HOSTED
91static enum {
92   IMG_NONE = 0,
93   IMG_FILENOTFOUND = /*Couldn't open data file `%s'*/24,
94   IMG_OUTOFMEMORY  = /*Out of memory %.0s*/38,
95   IMG_DIRECTORY    = /*Filename `%s' refers to directory*/44,
96   IMG_CANTOPENOUT  = /*Failed to open output file `%s'*/47,
97   IMG_BADFORMAT    = /*Bad 3d image file `%s'*/106
98} img_errno = IMG_NONE;
99#else
100static img_errcode img_errno = IMG_NONE;
101#endif
102
103/* Read a line from a stream to a buffer. Any eol chars are removed
104 * from the file and the length of the string excluding '\0' is returned */
105static int
106getline(char *buf, size_t len, FILE *fh)
107{
108   size_t i = 0;
109   int ch;
110
111   ch = getc(fh);
112   while (ch != '\n' && ch != '\r' && ch != EOF && i < len - 1) {
113      buf[i++] = ch;
114      ch = getc(fh);
115   }
116   if (ch == '\n' || ch == '\r') {
117      /* remove any further eol chars (for DOS text files) */
118      do {
119         ch = getc(fh);
120      } while (ch == '\n' || ch == '\r');
121      ungetc(ch, fh); /* we don't want it, so put it back */
122   }
123   buf[i] = '\0';
124   return i;
125}
126
127#ifndef IMG_HOSTED
128img_errcode
129img_error(void)
130{
131   return img_errno;
132}
133#else
134int
135img_error(void)
136{
137   return (int)img_errno;
138}
139#endif
140
141img *
142img_open_survey(const char *fnm, char *title_buf, char *date_buf,
143                const char *survey)
144{
145   img *pimg;
146
147   if (fDirectory(fnm)) {
148      img_errno = IMG_DIRECTORY;
149      return NULL;
150   }
151
152   pimg = (img *)xosmalloc(ossizeof(img));
153   if (pimg == NULL) {
154      img_errno = IMG_OUTOFMEMORY;
155      return NULL;
156   }
157
158   pimg->buf_len = 257;
159   pimg->label_buf = xosmalloc(pimg->buf_len);
160   if (!pimg->label_buf) {
161      osfree(pimg);
162      img_errno = IMG_OUTOFMEMORY;
163      return NULL;
164   }
165
166   pimg->fh = fopenWithPthAndExt("", fnm, EXT_SVX_3D, "rb", NULL);
167   if (pimg->fh == NULL) {
168      osfree(pimg);
169      img_errno = IMG_FILENOTFOUND;
170      return NULL;
171   }
172
173   getline(tmpbuf, TMPBUFLEN, pimg->fh); /* id string */
174   if (strcmp(tmpbuf, "Survex 3D Image File") != 0) {
175      fclose(pimg->fh);
176      osfree(pimg);
177      img_errno = IMG_BADFORMAT;
178      return NULL;
179   }
180
181   getline(tmpbuf, TMPBUFLEN, pimg->fh); /* file format version */
182   pimg->version = (tolower(*tmpbuf) == 'b'); /* binary file iff B/b prefix */
183   /* knock off the 'B' or 'b' if it's there */
184   if (strcmp(tmpbuf + pimg->version, "v0.01") == 0) {
185      pimg->flags = 0;
186   } else if (pimg->version == 0 && tmpbuf[0] == 'v' &&
187              (tmpbuf[1] >= '2' || tmpbuf[1] <= '3') &&
188              tmpbuf[2] == '\0') {
189      pimg->version = tmpbuf[1] - '0';
190   } else {
191      fclose(pimg->fh);
192      osfree(pimg);
193      img_errno = IMG_BADFORMAT;
194      return NULL;
195   }
196   getline((title_buf ? title_buf : tmpbuf), TMPBUFLEN, pimg->fh);
197   getline((date_buf ? date_buf : tmpbuf), TMPBUFLEN, pimg->fh);
198   pimg->fRead = fTrue; /* reading from this file */
199   img_errno = IMG_NONE;
200   pimg->start = ftell(pimg->fh);
201
202   /* for version 3 we use label to store the prefix for reuse */
203   pimg->label_len = 0;
204
205   pimg->survey = NULL;
206   pimg->survey_len = 0;
207
208   if (survey) {
209      size_t len = strlen(survey);
210      if (len) {
211         if (survey[len - 1] == '.') len--;
212         if (len) {
213            pimg->survey = osmalloc(len + 2);
214            memcpy(pimg->survey, survey, len);
215            pimg->survey[len] = '.';
216            pimg->survey[len + 1] = '\0';
217         }
218      }
219      pimg->survey_len = len;
220   }
221
222   /* [version 0] not in the middle of a 'LINE' command
223    * [version 3] not in the middle of turning a LINE into a MOVE */
224   pimg->pending = 0;
225
226   return pimg;
227}
228
229void
230img_rewind(img *pimg)
231{
232   fseek(pimg->fh, pimg->start, SEEK_SET);
233
234   /* [version 0] not in the middle of a 'LINE' command
235    * [version 3] not in the middle of turning a LINE into a MOVE */
236   pimg->pending = 0;
237
238   img_errno = IMG_NONE;
239   
240   /* for version 3 we use label to store the prefix for reuse */
241   pimg->label_len = 0;
242}
243
244img *
245img_open_write(const char *fnm, char *title_buf, bool fBinary)
246{
247   time_t tm;
248   img *pimg;
249
250   fBinary = fBinary;
251
252   if (fDirectory(fnm)) {
253      img_errno = IMG_DIRECTORY;
254      return NULL;
255   }
256
257   pimg = (img *)xosmalloc(ossizeof(img));
258   if (pimg == NULL) {
259      img_errno = IMG_OUTOFMEMORY;
260      return NULL;
261   }
262
263   pimg->buf_len = 257;
264   pimg->label_buf = xosmalloc(pimg->buf_len);
265   if (!pimg->label_buf) {
266      osfree(pimg);
267      img_errno = IMG_OUTOFMEMORY;
268      return NULL;
269   }
270
271   pimg->fh = fopen(fnm, "wb");
272   if (!pimg->fh) {
273      osfree(pimg);
274      img_errno = IMG_CANTOPENOUT;
275      return NULL;
276   }
277
278   /* Output image file header */
279   fputs("Survex 3D Image File\n", pimg->fh); /* file identifier string */
280   if (img_output_version < 2) {
281      pimg->version = 1;
282      fputs("Bv0.01\n", pimg->fh); /* binary file format version number */
283   } else {
284      pimg->version = (img_output_version > 2) ? 3 : 2;
285      fprintf(pimg->fh, "v%d\n", pimg->version); /* file format version no. */
286   }
287   fputsnl(title_buf, pimg->fh);
288   tm = time(NULL);
289   if (tm == (time_t)-1) {
290      fputsnl(TIMENA, pimg->fh);
291   } else {
292      /* output current date and time in format specified */
293      strftime(tmpbuf, TMPBUFLEN, TIMEFMT, localtime(&tm));
294      fputsnl(tmpbuf, pimg->fh);
295   }
296   pimg->fRead = fFalse; /* writing to this file */
297   img_errno = IMG_NONE;
298
299   /* for version 3 we use label to store the prefix for reuse */
300   pimg->label_buf[0] = '\0';
301   pimg->label_len = 0;
302
303   return pimg;
304}
305
306int
307img_read_item(img *pimg, img_point *p)
308{
309   int result;
310   pimg->flags = 0;
311   pimg->label = pimg->label_buf;
312
313   if (pimg->version == 3) {
314      int opt;
315      if (pimg->pending >= 0x80) {
316         *p = pimg->mv;
317         pimg->flags = (int)(pimg->pending) & 0x3f;
318         pimg->pending = 0;
319         return img_LINE; // FIXME
320      }
321      again3: /* label to goto if we get a prefix */
322      opt = getc(pimg->fh);
323      switch (opt >> 6) {
324       case 0:
325         if (opt == 0) {
326            if (!pimg->label_len) return img_STOP; /* end of data marker */
327            pimg->label_len = 0;
328            goto again3;
329         }
330         if (opt < 15) {
331            /* 1-14 mean trim that many levels from current prefix */
332            int c;
333            /* zero prefix using "0" */
334            if (pimg->label_len <= 16) {
335               img_errno = IMG_BADFORMAT;
336               return img_BAD;
337            }
338            c = pimg->label_len - 16;
339            opt &= 0x07;
340            while (pimg->label_buf[c] != '.' || --opt > 0) {
341               if (--c < 0) {
342                  /* zero prefix using "0" */
343                  img_errno = IMG_BADFORMAT;
344                  return img_BAD;
345               }
346            }
347            c++;
348            pimg->label_buf[c] = '\0';
349            pimg->label_len = c;
350            goto again3;
351         }
352         if (opt == 15) {
353            result = img_MOVE;
354            break;
355         }
356         /* 16-31 mean remove (n - 15) characters from the prefix */
357         pimg->label_len -= (opt - 15);
358         /* zero prefix using 0 */
359         if (pimg->label_len <= 0) {
360            img_errno = IMG_BADFORMAT;
361            return img_BAD;
362         }
363         goto again3;
364       case 1: case 2: {
365         char *q;
366         size_t len;
367
368         len = getc(pimg->fh);
369         if (len == 0xfe) {
370            len += get16(pimg->fh);
371         } else if (len == 0xff) {
372            len = get32(pimg->fh);
373            if (len < 0xfe + 0xffff) {
374               img_errno = IMG_BADFORMAT;
375               return img_BAD;
376            }
377         }
378
379         q = pimg->label_buf + pimg->label_len;
380         pimg->label_len += len;
381         if (pimg->label_len >= pimg->buf_len) {
382            pimg->label_buf = xosrealloc(pimg->label_buf, pimg->label_len + 1);
383            if (!pimg->label_buf) {
384               img_errno = IMG_OUTOFMEMORY;
385               return img_BAD;
386            }
387            pimg->buf_len = pimg->label_len + 1;
388         }
389         if (len && fread(q, len, 1, pimg->fh) != 1) {
390            img_errno = IMG_BADFORMAT;
391            return img_BAD;
392         }
393         q[len] = '\0';
394
395         result = opt & 0x40 ? img_LABEL : img_LINE;
396
397         if (pimg->survey_len) {
398            size_t l = pimg->survey_len;
399            const char *s = pimg->label_buf;
400            if (result == img_LINE) {
401               if (strncmp(pimg->survey, s, l) != 0 ||
402                   !(s[l] == '.' || s[l] == '\0')) {
403                  pimg->mv.x = get32(pimg->fh) / 100.0;
404                  pimg->mv.y = get32(pimg->fh) / 100.0;
405                  pimg->mv.z = get32(pimg->fh) / 100.0;
406                  pimg->pending = 15;
407                  goto again3;
408               }
409            } else {
410               if (strncmp(pimg->survey, s, l + 1) != 0) {
411                  fseek(pimg->fh, 12, SEEK_CUR);
412                  goto again3;
413               }
414            }
415            pimg->label += l;
416            /* skip the dot if there */
417            if (*pimg->label) pimg->label++;
418         }
419
420         if (result == img_LINE && pimg->pending) {
421            *p = pimg->mv;
422            pimg->mv.x = get32(pimg->fh) / 100.0;
423            pimg->mv.y = get32(pimg->fh) / 100.0;
424            pimg->mv.z = get32(pimg->fh) / 100.0;
425            pimg->pending = opt;
426            return img_MOVE;
427         }
428         pimg->flags = (int)opt & 0x3f;
429         break;
430       }
431       default:
432         img_errno = IMG_BADFORMAT;
433         return img_BAD;
434      }
435      p->x = get32(pimg->fh) / 100.0;
436      p->y = get32(pimg->fh) / 100.0;
437      p->z = get32(pimg->fh) / 100.0;
438      pimg->pending = 0;
439      return result;
440   } else if (pimg->version > 0) {
441      static long opt_lookahead = 0;
442      static double x = 0.0, y = 0.0, z = 0.0;
443      long opt;
444      again: /* label to goto if we get a cross */
445      if (pimg->version == 1) {
446         if (opt_lookahead) {
447            opt = opt_lookahead;
448            opt_lookahead = 0;
449         } else {
450            opt = get32(pimg->fh);
451         }
452      } else {
453         opt = getc(pimg->fh);
454      }
455      switch (opt) {
456       case -1: case 0:
457         return img_STOP; /* end of data marker */
458       case 1:
459         (void)get32(pimg->fh);
460         (void)get32(pimg->fh);
461         (void)get32(pimg->fh);
462         goto again;
463       case 2: case 3: {
464         char *q;
465         int ch;
466         result = img_LABEL;
467         ch = getc(pimg->fh);
468         if (ch == EOF) {
469            img_errno = IMG_BADFORMAT;
470            return img_BAD;
471         }
472         if (ch != '\\') ungetc(ch, pimg->fh);
473         fgets(pimg->label_buf, 257, pimg->fh);
474         q = pimg->label_buf + strlen(pimg->label_buf) - 1;
475         if (*q != '\n') {
476            img_errno = IMG_BADFORMAT;
477            return img_BAD;
478         }
479         /* Ignore empty labels in some .3d files (caused by a bug) */
480         if (q == pimg->label_buf) goto again;
481         *q = '\0';
482         pimg->flags = img_SFLAG_UNDERGROUND; /* no flags given... */
483         if (opt == 2) goto done;
484         break;
485       }
486       case 6: case 7: {
487         size_t len;
488         result = img_LABEL;
489         if (opt == 7)
490            pimg->flags = getc(pimg->fh);
491         else
492            pimg->flags = img_SFLAG_UNDERGROUND; /* no flags given... */
493         len = get32(pimg->fh);
494         /* Ignore empty labels in some .3d files (caused by a bug) */
495         if (len == 0) goto again;
496         if (len >= pimg->buf_len) {
497            pimg->label_buf = xosrealloc(pimg->label_buf, len + 1);
498            if (!pimg->label_buf) {
499               img_errno = IMG_OUTOFMEMORY;
500               return img_BAD;
501            }
502            pimg->buf_len = len + 1;
503         }
504         if (fread(pimg->label_buf, len, 1, pimg->fh) != 1) {
505            img_errno = IMG_BADFORMAT;
506            return img_BAD;
507         }
508         break;
509       }
510       case 4:
511         result = img_MOVE;
512         break;
513       case 5:
514         result = img_LINE;
515         break;
516       default:
517         switch ((int)opt & 0xc0) {
518          case 0x80:
519            pimg->flags = (int)opt & 0x3f;
520            result = img_LINE;
521            break;
522          case 0x40: {
523            char *q;
524            pimg->flags = (int)opt & 0x3f;
525            result = img_LABEL;
526            if (!fgets(pimg->label_buf, 257, pimg->fh)) {
527               img_errno = IMG_BADFORMAT;
528               return img_BAD;
529            }
530            q = pimg->label_buf + strlen(pimg->label_buf) - 1;
531            /* Ignore empty-labels in some .3d files (caused by a bug) */
532            if (q == pimg->label_buf) goto again;
533            if (*q != '\n') {
534               img_errno = IMG_BADFORMAT;
535               return img_BAD;
536            }
537            *q = '\0';
538            break;
539          }
540          case 0xc0:
541            /* use this for an extra leg or station flag if we need it */
542          default:
543            img_errno = IMG_BADFORMAT;
544            return img_BAD;
545         }
546         break;
547      }
548      x = get32(pimg->fh) / 100.0;
549      y = get32(pimg->fh) / 100.0;
550      z = get32(pimg->fh) / 100.0;
551
552      if (result == img_LABEL && pimg->survey_len) {
553         if (strncmp(pimg->label_buf, pimg->survey, pimg->survey_len + 1) != 0)
554            goto again;
555         pimg->label += pimg->survey_len + 1;
556      }
557
558      done:
559      p->x = x;
560      p->y = y;
561      p->z = z;
562
563      if (result == img_MOVE && pimg->version == 1) {
564         /* peek at next code and see if it's an old-style label */
565         opt_lookahead = get32(pimg->fh);
566         if (opt_lookahead == 2) return img_read_item(pimg, p);
567      }
568
569      return result;
570   } else {
571      ascii_again:
572      if (feof(pimg->fh)) return img_STOP;
573      if (pimg->pending) {
574         pimg->pending = 0;
575         result = img_LINE;
576      } else {
577         /* Stop if nothing found */
578         if (fscanf(pimg->fh, "%s", tmpbuf) < 1) return img_STOP;
579         if (strcmp(tmpbuf, "move") == 0)
580            result = img_MOVE;
581         else if (strcmp(tmpbuf, "draw") == 0)
582            result = img_LINE;
583         else if (strcmp(tmpbuf, "line") == 0) {
584            /* set flag to indicate to process second triplet as LINE */
585            pimg->pending = 1;
586            result = img_MOVE;
587         } else if (strcmp(tmpbuf, "cross") == 0) {
588            if (fscanf(pimg->fh, "%lf%lf%lf", &p->x, &p->y, &p->z) < 3) {
589               img_errno = IMG_BADFORMAT;             
590               return img_BAD;
591            }
592            goto ascii_again;
593         } else if (strcmp(tmpbuf, "name") == 0) {
594            int ch;
595            ch = getc(pimg->fh);
596            if (ch == EOF) {
597               img_errno = IMG_BADFORMAT;
598               return img_BAD;
599            }
600            if (ch != '\\') ungetc(ch, pimg->fh);
601            if (fscanf(pimg->fh, "%256s", pimg->label_buf) < 1 ||
602                strlen(pimg->label_buf) == 256) {
603               img_errno = IMG_BADFORMAT;
604               return img_BAD;
605            }
606            result = img_LABEL;
607         } else {
608            img_errno = IMG_BADFORMAT;
609            return img_BAD; /* unknown keyword */
610         }
611      }
612
613      if (fscanf(pimg->fh, "%lf%lf%lf", &p->x, &p->y, &p->z) < 3) {
614         img_errno = IMG_BADFORMAT;
615         return img_BAD;
616      }
617
618      if (result == img_LABEL) {
619         if (pimg->survey_len &&
620          (strncmp(pimg->label_buf, pimg->survey, pimg->survey_len + 1) != 0))
621            goto ascii_again;
622         pimg->label += pimg->survey_len + 1;
623      }
624
625      return result;
626   }
627}
628
629static void
630write_v3label(img *pimg, int opt, const char *s)
631{
632   size_t len, n, dot;
633
634   /* find length of common prefix */
635   dot = 0;
636   for (len = 0; s[len] == pimg->label_buf[len] && s[len] != '\0'; len++) {
637      if (s[len] == '.') dot = len + 1;
638   }
639
640   ASSERT(len <= pimg->label_len);
641   n = pimg->label_len - len;
642   if (len == 0) {
643      if (pimg->label_len) putc(0, pimg->fh);
644   } else if (n <= 16) {
645      if (n) putc(n + 15, pimg->fh);
646   } else if (dot == 0) {
647      if (pimg->label_len) putc(0, pimg->fh);
648      len = 0;     
649   } else {
650      const char *p = pimg->label_buf + dot;
651      n = 1;
652      for (len = pimg->label_len - dot - 17; len; len--) {
653         if (*p++ == '.') n++;
654      }
655      if (n <= 14) {
656         putc(n, pimg->fh);
657         len = dot;
658      } else {
659         if (pimg->label_len) putc(0, pimg->fh);
660         len = 0;
661      }
662   }
663
664   n = strlen(s + len);
665   putc(opt, pimg->fh);
666   if (n < 0xfe) {
667      putc(n, pimg->fh);
668   } else if (n < 0xffff + 0xfe) {
669      putc(0xfe, pimg->fh);
670      put16(n - 0xfe, pimg->fh);
671   } else {
672      putc(0xff, pimg->fh);
673      put32(n, pimg->fh);
674   }
675   fwrite(s + len, n, 1, pimg->fh);
676
677   n += len;
678   pimg->label_len = n;
679   if (n >= pimg->buf_len) {
680      char *p = xosrealloc(pimg->label_buf, n + 1);
681      if (p) {
682         pimg->label_buf = p;
683         pimg->buf_len = n + 1;
684      } else {
685         /* can't store this station, so just reset to no prefix */
686         if (pimg->label_len) {
687            /* can't here... putc(0, pimg->fh); */
688            abort();
689            pimg->label_buf[0] = '\0';
690            pimg->label_len = 0;
691         }
692      }
693   }
694   memcpy(pimg->label_buf + len, s + len, n - len + 1);
695}
696
697void
698img_write_item(img *pimg, int code, int flags, const char *s,
699               double x, double y, double z)
700{
701   if (!pimg) return;
702   if (pimg->version == 3) {
703      int opt = 0;
704      switch (code) {
705       case img_LABEL:
706         write_v3label(pimg, 0x40 | flags, s);
707         opt = 0;
708         break;
709       case img_MOVE:
710         opt = 15;
711         break;
712       case img_LINE:
713         write_v3label(pimg, 0x80 | flags, s ? s : "");
714         opt = 0;
715         break;
716       default: /* ignore for now */
717         return;
718      }
719      if (opt) putc(opt, pimg->fh);
720      /* Output in cm */
721      put32((INT32_T)(x * 100.0), pimg->fh);
722      put32((INT32_T)(y * 100.0), pimg->fh);
723      put32((INT32_T)(z * 100.0), pimg->fh);
724   } else {
725      size_t len;
726      INT32_T opt = 0;
727      ASSERT(pimg->version > 0);
728      switch (code) {
729       case img_LABEL:
730         if (pimg->version == 1) {
731            /* put a move before each label */
732            img_write_item(pimg, img_MOVE, 0, NULL, x, y, z);
733            put32(2, pimg->fh);
734            fputsnl(s, pimg->fh);
735            return;
736         }
737         len = strlen(s);
738         if (len > 255 || strchr(s, '\n')) {
739            /* long label - not in early incarnations of v2 format, but few
740             * 3d files will need these, so better not to force incompatibility
741             * with a new version I think... */
742            putc(7, pimg->fh);
743            putc(flags, pimg->fh);
744            put32(len, pimg->fh);
745            fputs(s, pimg->fh);
746         } else {
747            putc(0x40 | (flags & 0x3f), pimg->fh);
748            fputsnl(s, pimg->fh);
749         }
750         opt = 0;
751         break;
752       case img_MOVE:
753         opt = 4;
754         break;
755       case img_LINE:
756         if (pimg->version > 1) {
757            opt = 0x80 | (flags & 0x3f);
758            break;
759         }
760         opt = 5;
761         break;
762       default: /* ignore for now */
763         return;
764      }
765      if (pimg->version == 1) {
766         put32(opt, pimg->fh);
767      } else {
768         if (opt) putc(opt, pimg->fh);
769      }
770      /* Output in cm */
771      put32((INT32_T)(x * 100.0), pimg->fh);
772      put32((INT32_T)(y * 100.0), pimg->fh);
773      put32((INT32_T)(z * 100.0), pimg->fh);
774   }
775}
776
777void
778img_close(img *pimg)
779{
780   if (pimg) {
781      if (pimg->fh) {
782         if (pimg->fRead) {
783            osfree(pimg->survey);
784         } else {
785            /* write end of data marker */
786            switch (pimg->version) {
787             case 1:
788               put32((INT32_T)-1, pimg->fh);
789               break;
790             case 2:
791               putc(0, pimg->fh);
792               break;
793             case 3:
794               if (pimg->label_len) putc(0, pimg->fh);
795               putc(0, pimg->fh);
796               break;
797            }
798         }
799         fclose(pimg->fh);
800      }
801      osfree(pimg->label_buf);
802      osfree(pimg);
803   }
804}
Note: See TracBrowser for help on using the repository browser.