home *** CD-ROM | disk | FTP | other *** search
/ Fractal Creations (Second Edition) / FRACTALS_2E.iso / frasrc.exe / F16.C < prev    next >
C/C++ Source or Header  |  1992-07-15  |  3KB  |  93 lines

  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 <stdio.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include "targa_lc.h"
  29. #include "prototyp.h"
  30.  
  31. #ifndef XFRACT
  32. extern char rlebuf[258];    /* RLE-state variables */
  33. #else
  34. char rlebuf[258];    /* RLE-state variables */
  35. #endif
  36. static int state, count, bufp;
  37.  
  38. /**************************************
  39. **
  40. ** Open previously saved Targa16 type 10 file filling in hs, vs, and
  41. ** csize with values in the header.  If *csize is not zero, the block
  42. ** pointed to by cp is filled with the comment block.  The caller
  43. ** _must_ allocate 256 bytes for this purpose before calling.
  44. */
  45.  
  46. FILE *t16_open(char *fname, int *hs, int *vs, int *csize, U8 *cp)
  47. {
  48.     char filename[64];
  49.     U8 header[HEADERSIZE];
  50.     FILE *fp;
  51.  
  52.     strcpy(filename, fname);
  53.     if (strchr(filename, '.') == NULL) strcat(filename, ".TGA");
  54.     if ((fp = fopen(filename, READMODE)) == NULL) return NULL;
  55.  
  56.     fread(header, HEADERSIZE, 1, fp);
  57.     if ((header[O_FILETYPE] != T_RLERGB) || (header[O_ESIZE] != 16)) {
  58.     fclose(fp);
  59.     return NULL;
  60.     }
  61.     GET16(header[O_HSIZE], *hs);
  62.     GET16(header[O_VSIZE], *vs);
  63.     if (*csize = header[O_COMMENTLEN]) fread(cp, *csize, 1, fp);
  64.  
  65.     state = count = bufp = 0;
  66.     return fp;
  67. }
  68.  
  69. int t16_getline(FILE *fp, int hs, U16 *data)
  70. {
  71.     int i;
  72.  
  73.     for (i=0; i<hs; ++i) {
  74.     if (state == 0) {
  75.         bufp = 0;
  76.         if ((count = getc(fp)) > 127) {
  77.         state = 1;
  78.         count -= 127;
  79.         fread(rlebuf, 2, 1, fp);
  80.         } else {
  81.         state = 2;
  82.         ++count;
  83.         fread(rlebuf, 2, count, fp);
  84.         }
  85.     }
  86.     GET16(rlebuf[bufp], data[i]);
  87.     if (--count == 0) state = 0;
  88.     if (state == 2) bufp += 2;
  89.     }
  90.     return 0;
  91. }
  92.  
  93.