File: common\f16.c
1 /**************************************
2 **
3 ** F16.C : Code to read 16-bit fractal data sets. Uses
4 ** strictly Targa16 type 10 files (run-length encoded 16-bit RGB).
5 */
6
7 /* Lee Daniel Crocker CompuServe: 73407,2030 <== Preferred
8 ** 1380 Jewett Ave. BIX: lcrocker
9 ** Pittsburg, CA 94565 Usenet: ...!ames!pacbell!sactoh0!siva!lee
10 **
11 ** This code is hereby placed in the public domain. You are free to
12 ** use, modify, usurp, laugh at, destroy, or otherwise abuse it in any
13 ** way you see fit.
14 **
15 ** "If you steal from one author it's plagiarism; if you steal from
16 ** many it's research." --Wilson Mizner
17 */
18
19 /* 16 bit .tga files were generated for continuous potential "potfile"s
20 from version 9.? thru version 14. Replaced by double row gif type
21 file (.pot) in version 15. Delete this code after a few more revs.
22 The part which wrote 16 bit .tga files has already been deleted.
23 */
24
25 #include <string.h>
26 #include <ctype.h>
27 /* see Fractint.c for a description of the "include" hierarchy */
28 #include "port.h"
29 #include "prototyp.h"
30 #include "targa_lc.h"
31
32 #ifdef XFRACT
33 char rlebuf[258]; /* RLE-state variables */ 34 #endif
35 static int state, count, bufp;
36
37 /**************************************
38 **
39 ** Open previously saved Targa16 type 10 file filling in hs, vs, and
40 ** csize with values in the header. If *csize is not zero, the block
41 ** pointed to by cp is filled with the comment block. The caller
42 ** _must_ allocate 256 bytes for this purpose before calling.
43 */
44
45 FILE *t16_open(char *fname, int *hs, int *vs, int *csize, U8 *cp)
46 {
47 char filename[64];
48 U8 header[HEADERSIZE];
49 FILE *fp;
50
51 strcpy(filename, fname);
52 if (has_ext(filename) == NULL) strcat(filename, ".TGA");
53 if ((fp = fopen(filename, READMODE)) == NULL) return NULL;
54
55 fread(header, HEADERSIZE, 1, fp);
56 if ((header[O_FILETYPE] != T_RLERGB) || (header[O_ESIZE] != 16)) {
57 fclose(fp);
58 return NULL;
59 }
60 GET16(header[O_HSIZE], *hs);
61 GET16(header[O_VSIZE], *vs);
62 if ((*csize = header[O_COMMENTLEN]) != 0) fread(cp, *csize, 1, fp);
63
64 state = count = bufp = 0;
65 return fp;
66 }
67
68 int t16_getline(FILE *fp, int hs, U16 *data)
69 {
70 int i;
71
72 for (i=0; i<hs; ++i) {
73 if (state == 0) {
74 bufp = 0;
75 if ((count = getc(fp)) > 127) {
76 state = 1;
77 count -= 127;
78 fread(rlebuf, 2, 1, fp);
79 } else {
80 state = 2;
81 ++count;
82 fread(rlebuf, 2, count, fp);
83 }
84 }
85 GET16(rlebuf[bufp], data[i]);
86 if (--count == 0) state = 0;
87 if (state == 2) bufp += 2;
88 }
89 return 0;
90 }
91
92