home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / dkbtrace_397.lzh / DKBTrace / DKBSource.LZH / raw.c < prev    next >
C/C++ Source or Header  |  1990-08-26  |  5KB  |  160 lines

  1. /*****************************************************************************
  2. *
  3. *                                     raw.c
  4. *
  5. *   from DKBTrace (c) 1990  David Buck
  6. *
  7. *  This file implements code to read in the raw 24 bit files produced by
  8. *  the raytracer to use as texture maps.
  9. *
  10. * This software is freely distributable. The source and/or object code may be
  11. * copied or uploaded to communications services so long as this notice remains
  12. * at the top of each file.  If any changes are made to the program, you must
  13. * clearly indicate in the documentation and in the programs startup message
  14. * who it was who made the changes. The documentation should also describe what
  15. * those changes were. This software may not be included in whole or in
  16. * part into any commercial package without the express written consent of the
  17. * author.  It may, however, be included in other public domain or freely
  18. * distributed software so long as the proper credit for the software is given.
  19. *
  20. * This software is provided as is without any guarantees or warranty. Although
  21. * the author has attempted to find and correct any bugs in the software, he
  22. * is not responsible for any damage caused by the use of the software.  The
  23. * author is under no obligation to provide service, corrections, or upgrades
  24. * to this package.
  25. *
  26. * Despite all the legal stuff above, if you do find bugs, I would like to hear
  27. * about them.  Also, if you have any comments or questions, you may contact me
  28. * at the following address:
  29. *
  30. *     David Buck
  31. *     22C Sonnet Cres.
  32. *     Nepean Ontario
  33. *     Canada, K2H 8W7
  34. *
  35. *  I can also be reached on the following bulleton boards:
  36. *
  37. *     ATX              (613) 526-4141
  38. *     OMX              (613) 731-3419
  39. *     Mystic           (613) 731-0088 or (613) 731-6698
  40. *
  41. *  Fidonet:   1:163/109.9
  42. *  Internet:  David_Buck@Carleton.CA
  43. *
  44. *  IBM Port by Aaron A. Collins. Aaron may be reached on the following BBS'es:
  45. *
  46. *     Lattice BBS                      (708) 916-1200
  47. *     The Information Exchange BBS     (708) 945-5575
  48. *     Stillwaters BBS                  (708) 403-2826
  49. *
  50. *****************************************************************************/
  51.  
  52.  
  53. #include "frame.h"
  54. #include "dkbproto.h"
  55.  
  56. int read_raw_byte(f)
  57.    FILE *f;
  58.    {
  59.    int c;
  60.    if ((c = getc(f)) == EOF)
  61.       return (-1);
  62.    return (c);
  63.    }
  64.  
  65. int read_raw_word(f)
  66.    FILE *f;
  67.    {
  68.    int byte1, byte2;
  69.  
  70.    byte1 = read_raw_byte(f);
  71.    if (byte1 == -1)
  72.       return(-1);
  73.  
  74.    byte2 = read_raw_byte(f);
  75.    if (byte2 == -1)
  76.       return(-1);
  77.  
  78.    return (byte1 + byte2*256);
  79.    }
  80.  
  81. void read_raw_image(Image, filename)
  82.    IMAGE *Image;
  83.    char *filename;
  84.    {
  85.    FILE *f;
  86.    int byte, i, index, row, pixels;
  87.  
  88.    if ((f = fopen(filename, "rb")) == NULL) {
  89.       printf ("Cannot open raw file %s\n", filename);
  90.       exit(1);
  91.       }
  92.  
  93.    Image->iwidth = read_raw_word(f);
  94.    if (Image->iwidth == -1) {
  95.       printf ("Cannot read size in dump file\n");
  96.       exit(1);
  97.       }
  98.  
  99.    Image->width = (DBL)Image->iwidth;
  100.  
  101.    Image->iheight = read_raw_word(f);
  102.    if (Image->iheight == -1) {
  103.       printf ("Cannot read size in dump file: %s\n", filename);
  104.       exit(1);
  105.       }
  106.  
  107.    Image->height = (DBL)Image->iheight;
  108.  
  109.    pixels = Image->iwidth * Image->iheight;
  110.  
  111.    if (((Image->red = (unsigned char *) malloc(pixels))==NULL) ||
  112.        ((Image->green = (unsigned char *) malloc(pixels))==NULL) ||
  113.        ((Image->blue = (unsigned char *) malloc(pixels))==NULL)) {
  114.       printf ("Cannot allocate memory for picture: %s\n", filename);
  115.       exit(1);
  116.       }
  117.  
  118.    for (i = 0 ; i < pixels ; i++) {
  119.       Image->red[i] = 0;
  120.       Image->green[i] = 0;
  121.       Image->blue[i] = 0;
  122.       }
  123.  
  124.    row = read_raw_word(f);
  125.    while (row != -1) {
  126.       for (i = 0 ; i < Image->iwidth ; i++) {
  127.          index = (Image->iheight-row)*Image->iwidth + i;
  128.  
  129.          byte = read_raw_byte(f);
  130.          if (byte == -1) {
  131.             printf ("Unexpected end of file in raw image: %s\n", filename);
  132.             exit(1);
  133.             }
  134.          Image->red[index] = byte;
  135.          }
  136.  
  137.       for (i = 0 ; i < Image->iwidth ; i++) {
  138.          index = (Image->iheight-row)*Image->iwidth + i;
  139.          byte = read_raw_byte(f);
  140.          if (byte == -1) {
  141.             printf ("Unexpected end of file in raw image: %s\n", filename);
  142.             exit(1);
  143.             }
  144.          Image->green[index] = byte;
  145.          }
  146.  
  147.       for (i = 0 ; i < Image->iwidth ; i++) {
  148.          index = (Image->iheight-row)*Image->iwidth + i;
  149.          byte = read_raw_byte(f);
  150.          if (byte == -1) {
  151.             printf ("Unexpected end of file in raw image: %s\n", filename);
  152.             exit(1);
  153.             }
  154.          Image->blue[index] = byte;
  155.          }
  156.       row = read_raw_word(f);
  157.       }
  158.    fclose (f);
  159.    }
  160.