source: git/src/img.c @ e939c98

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 e939c98 was 0ff23cb, checked in by Olly Betts <olly@…>, 24 years ago

Trim of any prefix given by --survey.

Removed references to img_CROSS and commented it out in the header.

print*: reduced memory usage by storing a "label" with coordinates and
using it for crosses too, rather than storing separate cross and label
elements.

git-svn-id: file:///home/survex-svn/survex/trunk@1175 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) {
553         if (pimg->survey_len &&
554          (strncmp(pimg->label_buf, pimg->survey, pimg->survey_len + 1) != 0))
555            goto again;
556         pimg->label += pimg->survey_len + 1;
557      }
558
559      done:
560      p->x = x;
561      p->y = y;
562      p->z = z;
563
564      if (result == img_MOVE && pimg->version == 1) {
565         /* peek at next code and see if it's an old-style label */
566         opt_lookahead = get32(pimg->fh);
567         if (opt_lookahead == 2) return img_read_item(pimg, p);
568      }
569
570      return result;
571   } else {
572      ascii_again:
573      if (feof(pimg->fh)) return img_STOP;
574      if (pimg->pending) {
575         pimg->pending = 0;
576         result = img_LINE;
577      } else {
578         /* Stop if nothing found */
579         if (fscanf(pimg->fh, "%s", tmpbuf) < 1) return img_STOP;
580         if (strcmp(tmpbuf, "move") == 0)
581            result = img_MOVE;
582         else if (strcmp(tmpbuf, "draw") == 0)
583            result = img_LINE;
584         else if (strcmp(tmpbuf, "line") == 0) {
585            /* set flag to indicate to process second triplet as LINE */
586            pimg->pending = 1;
587            result = img_MOVE;
588         } else if (strcmp(tmpbuf, "cross") == 0) {
589            if (fscanf(pimg->fh, "%lf%lf%lf", &p->x, &p->y, &p->z) < 3) {
590               img_errno = IMG_BADFORMAT;             
591               return img_BAD;
592            }
593            goto ascii_again;
594         } else if (strcmp(tmpbuf, "name") == 0) {
595            int ch;
596            ch = getc(pimg->fh);
597            if (ch == EOF) {
598               img_errno = IMG_BADFORMAT;
599               return img_BAD;
600            }
601            if (ch != '\\') ungetc(ch, pimg->fh);
602            if (fscanf(pimg->fh, "%256s", pimg->label_buf) < 1 ||
603                strlen(pimg->label_buf) == 256) {
604               img_errno = IMG_BADFORMAT;
605               return img_BAD;
606            }
607            result = img_LABEL;
608         } else {
609            img_errno = IMG_BADFORMAT;
610            return img_BAD; /* unknown keyword */
611         }
612      }
613
614      if (fscanf(pimg->fh, "%lf%lf%lf", &p->x, &p->y, &p->z) < 3) {
615         img_errno = IMG_BADFORMAT;
616         return img_BAD;
617      }
618
619      if (result == img_LABEL) {
620         if (pimg->survey_len &&
621          (strncmp(pimg->label_buf, pimg->survey, pimg->survey_len + 1) != 0))
622            goto ascii_again;
623         pimg->label += pimg->survey_len + 1;
624      }
625
626      return result;
627   }
628}
629
630static void
631write_v3label(img *pimg, int opt, const char *s)
632{
633   size_t len, n, dot;
634
635   /* find length of common prefix */
636   dot = 0;
637   for (len = 0; s[len] == pimg->label_buf[len] && s[len] != '\0'; len++) {
638      if (s[len] == '.') dot = len + 1;
639   }
640
641   ASSERT(len <= pimg->label_len);
642   n = pimg->label_len - len;
643   if (len == 0) {
644      if (pimg->label_len) putc(0, pimg->fh);
645   } else if (n <= 16) {
646      if (n) putc(n + 15, pimg->fh);
647   } else if (dot == 0) {
648      if (pimg->label_len) putc(0, pimg->fh);
649      len = 0;     
650   } else {
651      const char *p = pimg->label_buf + dot;
652      n = 1;
653      for (len = pimg->label_len - dot - 17; len; len--) {
654         if (*p++ == '.') n++;
655      }
656      if (n <= 14) {
657         putc(n, pimg->fh);
658         len = dot;
659      } else {
660         if (pimg->label_len) putc(0, pimg->fh);
661         len = 0;
662      }
663   }
664
665   n = strlen(s + len);
666   putc(opt, pimg->fh);
667   if (n < 0xfe) {
668      putc(n, pimg->fh);
669   } else if (n < 0xffff + 0xfe) {
670      putc(0xfe, pimg->fh);
671      put16(n - 0xfe, pimg->fh);
672   } else {
673      putc(0xff, pimg->fh);
674      put32(n, pimg->fh);
675   }
676   fwrite(s + len, n, 1, pimg->fh);
677
678   n += len;
679   pimg->label_len = n;
680   if (n >= pimg->buf_len) {
681      char *p = xosrealloc(pimg->label_buf, n + 1);
682      if (p) {
683         pimg->label_buf = p;
684         pimg->buf_len = n + 1;
685      } else {
686         /* can't store this station, so just reset to no prefix */
687         if (pimg->label_len) {
688            /* can't here... putc(0, pimg->fh); */
689            abort();
690            pimg->label_buf[0] = '\0';
691            pimg->label_len = 0;
692         }
693      }
694   }
695   memcpy(pimg->label_buf + len, s + len, n - len + 1);
696}
697
698void
699img_write_item(img *pimg, int code, int flags, const char *s,
700               double x, double y, double z)
701{
702   if (!pimg) return;
703   if (pimg->version == 3) {
704      int opt = 0;
705      switch (code) {
706       case img_LABEL:
707         write_v3label(pimg, 0x40 | flags, s);
708         opt = 0;
709         break;
710       case img_MOVE:
711         opt = 15;
712         break;
713       case img_LINE:
714         write_v3label(pimg, 0x80 | flags, s ? s : "");
715         opt = 0;
716         break;
717       default: /* ignore for now */
718         return;
719      }
720      if (opt) putc(opt, pimg->fh);
721      /* Output in cm */
722      put32((INT32_T)(x * 100.0), pimg->fh);
723      put32((INT32_T)(y * 100.0), pimg->fh);
724      put32((INT32_T)(z * 100.0), pimg->fh);
725   } else {
726      size_t len;
727      INT32_T opt = 0;
728      ASSERT(pimg->version > 0);
729      switch (code) {
730       case img_LABEL:
731         if (pimg->version == 1) {
732            /* put a move before each label */
733            img_write_item(pimg, img_MOVE, 0, NULL, x, y, z);
734            put32(2, pimg->fh);
735            fputsnl(s, pimg->fh);
736            return;
737         }
738         len = strlen(s);
739         if (len > 255 || strchr(s, '\n')) {
740            /* long label - not in early incarnations of v2 format, but few
741             * 3d files will need these, so better not to force incompatibility
742             * with a new version I think... */
743            putc(7, pimg->fh);
744            putc(flags, pimg->fh);
745            put32(len, pimg->fh);
746            fputs(s, pimg->fh);
747         } else {
748            putc(0x40 | (flags & 0x3f), pimg->fh);
749            fputsnl(s, pimg->fh);
750         }
751         opt = 0;
752         break;
753       case img_MOVE:
754         opt = 4;
755         break;
756       case img_LINE:
757         if (pimg->version > 1) {
758            opt = 0x80 | (flags & 0x3f);
759            break;
760         }
761         opt = 5;
762         break;
763       default: /* ignore for now */
764         return;
765      }
766      if (pimg->version == 1) {
767         put32(opt, pimg->fh);
768      } else {
769         if (opt) putc(opt, pimg->fh);
770      }
771      /* Output in cm */
772      put32((INT32_T)(x * 100.0), pimg->fh);
773      put32((INT32_T)(y * 100.0), pimg->fh);
774      put32((INT32_T)(z * 100.0), pimg->fh);
775   }
776}
777
778void
779img_close(img *pimg)
780{
781   if (pimg) {
782      if (pimg->fh) {
783         if (pimg->fRead) {
784            osfree(pimg->survey);
785         } else {
786            /* write end of data marker */
787            switch (pimg->version) {
788             case 1:
789               put32((INT32_T)-1, pimg->fh);
790               break;
791             case 2:
792               putc(0, pimg->fh);
793               break;
794             case 3:
795               if (pimg->label_len) putc(0, pimg->fh);
796               putc(0, pimg->fh);
797               break;
798            }
799         }
800         fclose(pimg->fh);
801      }
802      osfree(pimg->label_buf);
803      osfree(pimg);
804   }
805}
Note: See TracBrowser for help on using the repository browser.