/*****************************************************************************
*
*                                     raw.c
*
*   from DKBTrace (c) 1990  David Buck
*
*  This file implements code to read in the raw 24 bit files produced by
*  the raytracer to use as texture maps.
*
* This software is freely distributable. The source and/or object code may be
* copied or uploaded to communications services so long as this notice remains
* at the top of each file.  If any changes are made to the program, you must
* clearly indicate in the documentation and in the programs startup message
* who it was who made the changes. The documentation should also describe what
* those changes were. This software may not be included in whole or in
* part into any commercial package without the express written consent of the
* author.  It may, however, be included in other public domain or freely
* distributed software so long as the proper credit for the software is given.
*
* This software is provided as is without any guarantees or warranty. Although
* the author has attempted to find and correct any bugs in the software, he
* is not responsible for any damage caused by the use of the software.  The
* author is under no obligation to provide service, corrections, or upgrades
* to this package.
*
* Despite all the legal stuff above, if you do find bugs, I would like to hear
* about them.  Also, if you have any comments or questions, you may contact me
* at the following address:
*
*     David Buck
*     22C Sonnet Cres.
*     Nepean Ontario
*     Canada, K2H 8W7
*
*  I can also be reached on the following bulleton boards:
*
*     ATX              (613) 526-4141
*     OMX              (613) 731-3419
*     Mystic           (613) 731-0088 or (613) 731-6698
*
*  Fidonet:   1:163/109.9
*  Internet:  David_Buck@Carleton.CA
*
*  IBM Port by Aaron A. Collins. Aaron may be reached on the following BBS'es:
*
*     Lattice BBS                      (708) 916-1200
*     The Information Exchange BBS     (708) 945-5575
*     Stillwaters BBS                  (708) 403-2826
*
*****************************************************************************/


#include "frame.h"
#include "dkbproto.h"

int read_raw_byte(f)
   FILE *f;
   {
   int c;
   if ((c = getc(f)) == EOF)
      return (-1);
   return (c);
   }

int read_raw_word(f)
   FILE *f;
   {
   int byte1, byte2;

   byte1 = read_raw_byte(f);
   if (byte1 == -1)
      return(-1);

   byte2 = read_raw_byte(f);
   if (byte2 == -1)
      return(-1);

   return (byte1 + byte2*256);
   }

void read_raw_image(Image, filename)
   IMAGE *Image;
   char *filename;
   {
   FILE *f;
   int byte, i, index, row, pixels;

   if ((f = fopen(filename, "rb")) == NULL) {
      printf ("Cannot open raw file %s\n", filename);
      exit(1);
      }

   Image->iwidth = read_raw_word(f);
   if (Image->iwidth == -1) {
      printf ("Cannot read size in dump file\n");
      exit(1);
      }

   Image->width = (DBL)Image->iwidth;

   Image->iheight = read_raw_word(f);
   if (Image->iheight == -1) {
      printf ("Cannot read size in dump file: %s\n", filename);
      exit(1);
      }

   Image->height = (DBL)Image->iheight;

   pixels = Image->iwidth * Image->iheight;

   if (((Image->red = (unsigned char *) malloc(pixels))==NULL) ||
       ((Image->green = (unsigned char *) malloc(pixels))==NULL) ||
       ((Image->blue = (unsigned char *) malloc(pixels))==NULL)) {
      printf ("Cannot allocate memory for picture: %s\n", filename);
      exit(1);
      }

   for (i = 0 ; i < pixels ; i++) {
      Image->red[i] = 0;
      Image->green[i] = 0;
      Image->blue[i] = 0;
      }

   row = read_raw_word(f);
   while (row != -1) {
      for (i = 0 ; i < Image->iwidth ; i++) {
         index = (Image->iheight-row)*Image->iwidth + i;

         byte = read_raw_byte(f);
         if (byte == -1) {
            printf ("Unexpected end of file in raw image: %s\n", filename);
            exit(1);
            }
         Image->red[index] = byte;
         }

      for (i = 0 ; i < Image->iwidth ; i++) {
         index = (Image->iheight-row)*Image->iwidth + i;
         byte = read_raw_byte(f);
         if (byte == -1) {
            printf ("Unexpected end of file in raw image: %s\n", filename);
            exit(1);
            }
         Image->green[index] = byte;
         }

      for (i = 0 ; i < Image->iwidth ; i++) {
         index = (Image->iheight-row)*Image->iwidth + i;
         byte = read_raw_byte(f);
         if (byte == -1) {
            printf ("Unexpected end of file in raw image: %s\n", filename);
            exit(1);
            }
         Image->blue[index] = byte;
         }
      row = read_raw_word(f);
      }
   fclose (f);
   }
