home *** CD-ROM | disk | FTP | other *** search
/ Chestnut's Multimedia Mania / MM_MANIA.ISO / graphics / povsrc20 / dump.c < prev    next >
C/C++ Source or Header  |  1993-07-28  |  9KB  |  359 lines

  1. /****************************************************************************
  2. *                   dump.c
  3. *
  4. *  This module contains the code to read and write the dump file format.
  5. *  The format is as follows:
  6. *
  7. *  (header:)
  8. *    wwww hhhh       - Width, Height (16 bits, LSB first)
  9. *
  10. *  (each scanline:)
  11. *    llll            - Line number (16 bits, LSB first)
  12. *    rr rr rr ...    - Red data for line (8 bits per pixel,
  13. *                       left to right, 0-255 (255=bright, 0=dark))
  14. *    gg gg gg ...    - Green data for line (8 bits per pixel,
  15. *                       left to right, 0-255 (255=bright, 0=dark))
  16. *    bb bb bb ...    - Blue data for line (8 bits per pixel,
  17. *                       left to right, 0-255 (255=bright, 0=dark))
  18. *
  19. *  from Persistence of Vision Raytracer
  20. *  Copyright 1993 Persistence of Vision Team
  21. *---------------------------------------------------------------------------
  22. *  NOTICE: This source code file is provided so that users may experiment
  23. *  with enhancements to POV-Ray and to port the software to platforms other 
  24. *  than those supported by the POV-Ray Team.  There are strict rules under
  25. *  which you are permitted to use this file.  The rules are in the file
  26. *  named POVLEGAL.DOC which should be distributed with this file. If 
  27. *  POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  28. *  Team Coordinator by leaving a message in CompuServe's Graphics Developer's
  29. *  Forum.  The latest version of POV-Ray may be found there as well.
  30. *
  31. * This program is based on the popular DKB raytracer version 2.12.
  32. * DKBTrace was originally written by David K. Buck.
  33. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  34. *
  35. *****************************************************************************/
  36.  
  37. #include "frame.h"
  38. #include "povproto.h"
  39.  
  40. FILE_HANDLE *Get_Dump_File_Handle()
  41.   {
  42.   FILE_HANDLE *handle;
  43.  
  44.   if ((handle = (FILE_HANDLE *) malloc(sizeof(FILE_HANDLE))) == NULL) 
  45.     {
  46.     fprintf (stderr, "Cannot allocate memory for output file handle\n");
  47.     return(NULL);
  48.     }
  49.  
  50.   handle->Default_File_Name_p = Default_Dump_File_Name;
  51.   handle->Open_File_p = Open_Dump_File;
  52.   handle->Write_Line_p = Write_Dump_Line;
  53.   handle->Read_Line_p = Read_Dump_Line;
  54.   handle->Read_Image_p = Read_Dump_Image;
  55.   handle->Close_File_p = Close_Dump_File;
  56.   return (handle);
  57.   }
  58.  
  59. char *Default_Dump_File_Name()
  60.   {
  61.   return ("data.dis");
  62.   }
  63.  
  64. int Open_Dump_File (handle, name, width, height, buffer_size, mode)
  65. FILE_HANDLE *handle;
  66. char *name;
  67. int *width;
  68. int *height;
  69. int buffer_size;
  70. int mode;
  71.   {
  72.   int data1, data2;
  73.  
  74.   handle->mode = mode;
  75.   handle->filename = name;
  76.  
  77.   switch (mode) 
  78.   {
  79.   case READ_MODE:
  80.     if ((handle->file = fopen (name, READ_FILE_STRING)) == NULL) 
  81.       {
  82.       return(0);
  83.       }
  84.  
  85.     if (buffer_size != 0) 
  86.       {
  87.       if ((handle->buffer = malloc (buffer_size)) == NULL)
  88.         return(0);
  89.  
  90.       setvbuf (handle->file, handle->buffer, _IOFBF, buffer_size);
  91.       }
  92.  
  93.     if (((data1 = getc(handle->file)) == EOF)
  94.       || ((data2 = getc(handle->file)) == EOF))
  95.       return(0);
  96.  
  97.     *width  = data2 * 256 + data1;
  98.  
  99.     if (((data1 = getc(handle->file)) == EOF)
  100.       || ((data2 = getc(handle->file)) == EOF))
  101.       return(0);
  102.  
  103.     *height = data2 * 256 + data1;
  104.     handle->width = *width;
  105.     handle->height = *height;
  106.     handle->buffer_size = buffer_size;
  107.     break;
  108.  
  109.   case WRITE_MODE:
  110.     if ((handle->file = fopen (name, WRITE_FILE_STRING)) == NULL)
  111.       return(0);
  112.  
  113.     if (buffer_size != 0) 
  114.       {
  115.       if ((handle->buffer = malloc (buffer_size)) == NULL)
  116.         return(0);
  117.  
  118.       setvbuf (handle->file, handle->buffer, _IOFBF, buffer_size);
  119.       }
  120.  
  121.     putc(*width % 256, handle->file);  /* write to either type of file */
  122.     putc(*width / 256, handle->file);
  123.     putc(*height % 256, handle->file);
  124.     putc(*height / 256, handle->file);
  125.  
  126.     handle->width = *width;
  127.     handle->height = *height;
  128.     handle->buffer_size = buffer_size;
  129.  
  130.     break;
  131.  
  132.   case APPEND_MODE:
  133.     if ((handle->file = fopen (name, APPEND_FILE_STRING)) == NULL)
  134.       return(0);
  135.  
  136.     if (buffer_size != 0) 
  137.       {
  138.       if ((handle->buffer = malloc (buffer_size)) == NULL)
  139.         return(0);
  140.  
  141.       setvbuf (handle->file, handle->buffer, _IOFBF, buffer_size);
  142.       }
  143.  
  144.     handle->buffer_size = buffer_size;
  145.     break;
  146.   }
  147.   return(1);
  148.   }
  149.  
  150. void Write_Dump_Line (handle, line_data, line_number)
  151. FILE_HANDLE *handle;
  152. COLOUR *line_data;
  153. int line_number;
  154.   {
  155.   register int x;
  156.  
  157.   putc(line_number % 256, handle->file);
  158.   putc(line_number / 256, handle->file);
  159.  
  160.   for (x = 0 ; x < handle->width ; x++)
  161.     putc((int) floor (line_data[x].Red * 255.0), handle->file);
  162.  
  163.   for (x = 0 ; x < handle->width ; x++)
  164.     putc((int) floor (line_data[x].Green * 255.0), handle->file);
  165.  
  166.   for (x = 0 ; x < handle->width ; x++)
  167.     putc((int) floor (line_data[x].Blue * 255.0), handle->file);
  168.  
  169.   if (handle->buffer_size == 0) 
  170.     {
  171.     fflush(handle->file);                       /* close and reopen file for */
  172.     handle->file = freopen(handle->filename, APPEND_FILE_STRING,
  173.       handle->file);                /* integrity in case we crash*/
  174.     }
  175.   }
  176.  
  177. int Read_Dump_Line (handle, line_data, line_number)
  178. FILE_HANDLE *handle;
  179. COLOUR *line_data;
  180. int *line_number;
  181.   {
  182.   int data, i, c;
  183.  
  184.   if ((c = getc(handle->file)) == EOF) 
  185.     {
  186.     return (0);
  187.     }
  188.  
  189.   *line_number = c;
  190.  
  191.   if ((c = getc(handle->file)) == EOF)
  192.     return (-1);
  193.  
  194.   *line_number += c*256;
  195.  
  196.   for (i = 0 ; i < handle->width ; i++) 
  197.     {
  198.     if ((data = getc(handle->file)) == EOF)
  199.       return(-1);
  200.  
  201.     line_data[i].Red = (DBL) data / 255.0;
  202.     }
  203.  
  204.   for (i = 0 ; i < handle->width ; i++) 
  205.     {
  206.     if ((data = getc(handle->file)) == EOF)
  207.       return(-1);
  208.  
  209.     line_data[i].Green = (DBL) data / 255.0;
  210.     }
  211.  
  212.   for (i = 0 ; i < handle->width ; i++) 
  213.     {
  214.     if ((data = getc(handle->file)) == EOF)
  215.       return(-1);
  216.  
  217.     line_data[i].Blue = (DBL) data / 255.0;
  218.     }
  219.  
  220.   return (1);
  221.   }
  222.  
  223. int Read_Dump_Int_Line (handle, line_data, line_number)
  224. FILE_HANDLE *handle;
  225. IMAGE_LINE *line_data;
  226. int *line_number;
  227.   {
  228.   int data, i, c;
  229.  
  230.   if ((c = getc(handle->file)) == EOF) 
  231.     {
  232.     return (0);
  233.     }
  234.  
  235.   *line_number = c;
  236.  
  237.   if ((c = getc(handle->file)) == EOF)
  238.     return (-1);
  239.  
  240.   *line_number += c*256;
  241.  
  242.   if (((line_data->red = (unsigned char *) malloc(handle->width))==NULL) ||
  243.     ((line_data->green = (unsigned char *) malloc(handle->width))==NULL) ||
  244.     ((line_data->blue = (unsigned char *) malloc(handle->width))==NULL)) 
  245.     {
  246.     fprintf (stderr, "Cannot allocate memory for picture: %s\n", handle->filename);
  247.     close_all();
  248.     exit(1);
  249.     }
  250.  
  251.   for (i = 0 ; i < handle->width ; i++) 
  252.     {
  253.     line_data->red[i] = 0;
  254.     line_data->green[i] = 0;
  255.     line_data->blue[i] = 0;
  256.     }
  257.  
  258.   for (i = 0 ; i < handle->width ; i++) 
  259.     {
  260.     if ((data = getc(handle->file)) == EOF)
  261.       return(-1);
  262.  
  263.     line_data->red[i] = (unsigned char) data;
  264.     }
  265.  
  266.   for (i = 0 ; i < handle->width ; i++) 
  267.     {
  268.     if ((data = getc(handle->file)) == EOF)
  269.       return(-1);
  270.  
  271.     line_data->green[i] = (unsigned char) data;
  272.     }
  273.  
  274.   for (i = 0 ; i < handle->width ; i++) 
  275.     {
  276.     if ((data = getc(handle->file)) == EOF)
  277.       return(-1);
  278.  
  279.     line_data->blue[i] = (unsigned char) data;
  280.     }
  281.  
  282.   return (1);
  283.   }
  284.  
  285. void Close_Dump_File (handle)
  286. FILE_HANDLE *handle;
  287.   {
  288.   if(handle->file)
  289.     fclose (handle->file);
  290.   if (handle->buffer_size != 0)
  291.     free (handle->buffer);
  292.   }
  293.  
  294. void Read_Dump_Image(Image, name)
  295. IMAGE *Image;
  296. char *name;
  297.   {
  298.   int rc, row, data1, data2;
  299.   struct Image_Line line;
  300.   FILE_HANDLE handle;
  301.  
  302.   if ((handle.file = Locate_File (name, READ_FILE_STRING)) == NULL) 
  303.     {
  304.     fprintf (stderr, "Cannot open dump file %s\n", name);
  305.     close_all();
  306.     exit(1);
  307.     }
  308.  
  309.   if (((data1 = getc(handle.file)) == EOF)
  310.     || ((data2 = getc(handle.file)) == EOF)) 
  311.     {
  312.  
  313.     fprintf (stderr, "Cannot open dump file %s\n", name);
  314.     close_all();
  315.     exit(1);
  316.     }
  317.  
  318.   Image->iwidth  = data2 * 256 + data1;
  319.   handle.width = Image->iwidth;
  320.  
  321.   if (((data1 = getc(handle.file)) == EOF)
  322.     || ((data2 = getc(handle.file)) == EOF)) 
  323.     {
  324.  
  325.     fprintf (stderr, "Cannot open dump file %s\n", name);
  326.     close_all();
  327.     exit(1);
  328.     }
  329.  
  330.   Image->iheight = data2 * 256 + data1;
  331.   handle.height = Image->iheight;
  332.  
  333.   Image->width = (DBL)Image->iwidth;
  334.   Image->height = (DBL)Image->iheight;
  335.  
  336.   Image->Colour_Map_Size = 0;
  337.   Image->Colour_Map = NULL;
  338.  
  339.   if ((Image->data.rgb_lines = (struct Image_Line *)
  340.     malloc(Image->iheight * sizeof (struct Image_Line))) == NULL) 
  341.     {
  342.     fprintf (stderr, "Cannot allocate memory for picture: %s\n", name);
  343.     exit(1);
  344.     }
  345.  
  346.   while ((rc = Read_Dump_Int_Line(&handle, &line, &row)) == 1)
  347.     Image->data.rgb_lines[row] = line;
  348.  
  349.   fclose (handle.file);
  350.  
  351.   if (rc == 0)
  352.     return;
  353.   else 
  354.     {
  355.     close_all();
  356.     exit(1);
  357.     }
  358.   }
  359.