home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / povsrc.sit / SOURCE / DUMP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-03  |  8.5 KB  |  338 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. *
  20. *
  21. *  from Persistence of Vision Raytracer 
  22. *  Copyright 1992 Persistence of Vision Team
  23. *---------------------------------------------------------------------------
  24. *  Copying, distribution and legal info is in the file povlegal.doc which
  25. *  should be distributed with this file. If povlegal.doc is not available
  26. *  or for more info please contact:
  27. *
  28. *       Drew Wells [POV-Team Leader] 
  29. *       CIS: 73767,1244  Internet: 73767.1244@compuserve.com
  30. *       Phone: (213) 254-4041
  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.       fprintf (stderr, "Cannot allocate memory for output file handle\n");
  46.       return(NULL);
  47.    }
  48.  
  49.    handle->Default_File_Name_p = Default_Dump_File_Name;
  50.    handle->Open_File_p = Open_Dump_File;
  51.    handle->Write_Line_p = Write_Dump_Line;
  52.    handle->Read_Line_p = Read_Dump_Line;
  53.    handle->Read_Image_p = Read_Dump_Image;
  54.    handle->Close_File_p = Close_Dump_File;
  55.    return (handle);
  56. }
  57.  
  58. char *Default_Dump_File_Name()
  59. {
  60.    return ("data.dis");
  61. }
  62.  
  63. int Open_Dump_File (handle, name, width, height, buffer_size, mode)
  64. FILE_HANDLE *handle;
  65. char *name;
  66. int *width;
  67. int *height;
  68. int buffer_size;
  69. int mode;
  70. {
  71.    int data1, data2;
  72.  
  73.    handle->mode = mode;
  74.    handle->filename = name;
  75.  
  76.    switch (mode) {
  77.    case READ_MODE:
  78.       if ((handle->file = fopen (name, READ_FILE_STRING)) == NULL) {
  79.          return(0);
  80.       }
  81.  
  82.       if (buffer_size != 0) {
  83.          if ((handle->buffer = malloc (buffer_size)) == NULL)
  84.             return(0);
  85.  
  86.          setvbuf (handle->file, handle->buffer, _IOFBF, buffer_size);
  87.       }
  88.  
  89.       if (((data1 = getc(handle->file)) == EOF)
  90.          || ((data2 = getc(handle->file)) == EOF))
  91.          return(0);
  92.  
  93.       *width  = data2 * 256 + data1;
  94.  
  95.       if (((data1 = getc(handle->file)) == EOF)
  96.          || ((data2 = getc(handle->file)) == EOF))
  97.          return(0);
  98.  
  99.       *height = data2 * 256 + data1;
  100.       handle->width = *width;
  101.       handle->height = *height;
  102.       handle->buffer_size = buffer_size;
  103.       break;
  104.  
  105.    case WRITE_MODE:
  106.       if ((handle->file = fopen (name, WRITE_FILE_STRING)) == NULL)
  107.          return(0);
  108.  
  109.       if (buffer_size != 0) {
  110.          if ((handle->buffer = malloc (buffer_size)) == NULL)
  111.             return(0);
  112.  
  113.          setvbuf (handle->file, handle->buffer, _IOFBF, buffer_size);
  114.       }
  115.  
  116.       putc(*width % 256, handle->file);  /* write to either type of file */
  117.       putc(*width / 256, handle->file);
  118.       putc(*height % 256, handle->file);
  119.       putc(*height / 256, handle->file);
  120.  
  121.       handle->width = *width;
  122.       handle->height = *height;
  123.       handle->buffer_size = buffer_size;
  124.  
  125.       break;
  126.  
  127.    case APPEND_MODE:
  128.       if ((handle->file = fopen (name, APPEND_FILE_STRING)) == NULL)
  129.          return(0);
  130.  
  131.       if (buffer_size != 0) {
  132.          if ((handle->buffer = malloc (buffer_size)) == NULL)
  133.             return(0);
  134.  
  135.          setvbuf (handle->file, handle->buffer, _IOFBF, buffer_size);
  136.       }
  137.  
  138.       handle->buffer_size = buffer_size;
  139.       break;
  140.    }
  141.    return(1);
  142. }
  143.  
  144. void Write_Dump_Line (handle, line_data, line_number)
  145. FILE_HANDLE *handle;
  146. COLOUR *line_data;
  147. int line_number;
  148. {
  149.    register int x;
  150.  
  151.    putc(line_number % 256, handle->file);
  152.    putc(line_number / 256, handle->file);
  153.  
  154.    for (x = 0 ; x < handle->width ; x++)
  155.       putc((int) floor (line_data[x].Red * 255.0), handle->file);
  156.  
  157.    for (x = 0 ; x < handle->width ; x++)
  158.       putc((int) floor (line_data[x].Green * 255.0), handle->file);
  159.  
  160.    for (x = 0 ; x < handle->width ; x++)
  161.       putc((int) floor (line_data[x].Blue * 255.0), handle->file);
  162.  
  163.    if (handle->buffer_size == 0) {
  164.       fflush(handle->file);                       /* close and reopen file for */
  165.       handle->file = freopen(handle->filename, APPEND_FILE_STRING,
  166.          handle->file);                /* integrity in case we crash*/
  167.    }
  168. }
  169.  
  170. int Read_Dump_Line (handle, line_data, line_number)
  171. FILE_HANDLE *handle;
  172. COLOUR *line_data;
  173. int *line_number;
  174. {
  175.    int data, i, c;
  176.  
  177.    if ((c = getc(handle->file)) == EOF) {
  178.       return (0);
  179.    }
  180.  
  181.    *line_number = c;
  182.  
  183.    if ((c = getc(handle->file)) == EOF)
  184.       return (-1);
  185.  
  186.    *line_number += c*256;
  187.  
  188.    for (i = 0 ; i < handle->width ; i++) {
  189.       if ((data = getc(handle->file)) == EOF)
  190.          return(-1);
  191.  
  192.       line_data[i].Red = (DBL) data / 255.0;
  193.    }
  194.  
  195.    for (i = 0 ; i < handle->width ; i++) {
  196.       if ((data = getc(handle->file)) == EOF)
  197.          return(-1);
  198.  
  199.       line_data[i].Green = (DBL) data / 255.0;
  200.    }
  201.  
  202.    for (i = 0 ; i < handle->width ; i++) {
  203.       if ((data = getc(handle->file)) == EOF)
  204.          return(-1);
  205.  
  206.       line_data[i].Blue = (DBL) data / 255.0;
  207.    }
  208.  
  209.    return (1);
  210. }
  211.  
  212. int Read_Dump_Int_Line (handle, line_data, line_number)
  213. FILE_HANDLE *handle;
  214. IMAGE_LINE *line_data;
  215. int *line_number;
  216. {
  217.    int data, i, c;
  218.  
  219.    if ((c = getc(handle->file)) == EOF) {
  220.       return (0);
  221.    }
  222.  
  223.    *line_number = c;
  224.  
  225.    if ((c = getc(handle->file)) == EOF)
  226.       return (-1);
  227.  
  228.    *line_number += c*256;
  229.  
  230.    if (((line_data->red = (unsigned char *) malloc(handle->width))==NULL) ||
  231.       ((line_data->green = (unsigned char *) malloc(handle->width))==NULL) ||
  232.       ((line_data->blue = (unsigned char *) malloc(handle->width))==NULL)) {
  233.       fprintf (stderr, "Cannot allocate memory for picture: %s\n", handle->filename);
  234.       close_all();
  235.       exit(1);
  236.    }
  237.  
  238.    for (i = 0 ; i < handle->width ; i++) {
  239.       line_data->red[i] = 0;
  240.       line_data->green[i] = 0;
  241.       line_data->blue[i] = 0;
  242.    }
  243.  
  244.    for (i = 0 ; i < handle->width ; i++) {
  245.       if ((data = getc(handle->file)) == EOF)
  246.          return(-1);
  247.  
  248.       line_data->red[i] = (unsigned char) data;
  249.    }
  250.  
  251.    for (i = 0 ; i < handle->width ; i++) {
  252.       if ((data = getc(handle->file)) == EOF)
  253.          return(-1);
  254.  
  255.       line_data->green[i] = (unsigned char) data;
  256.    }
  257.  
  258.    for (i = 0 ; i < handle->width ; i++) {
  259.       if ((data = getc(handle->file)) == EOF)
  260.          return(-1);
  261.  
  262.       line_data->blue[i] = (unsigned char) data;
  263.    }
  264.  
  265.    return (1);
  266. }
  267.  
  268. void Close_Dump_File (handle)
  269. FILE_HANDLE *handle;
  270. {
  271.    if(handle->file)
  272.       fclose (handle->file);
  273.    if (handle->buffer_size != 0)
  274.       free (handle->buffer);
  275. }
  276.  
  277. void Read_Dump_Image(Image, name)
  278. IMAGE *Image;
  279. char *name;
  280. {
  281.    int rc, row, data1, data2;
  282.    struct Image_Line line;
  283.    FILE_HANDLE handle;
  284.  
  285.    if ((handle.file = Locate_File (name, READ_FILE_STRING)) == NULL) {
  286.       fprintf (stderr, "Cannot open dump file %s\n", name);
  287.       close_all();
  288.       exit(1);
  289.    }
  290.  
  291.    if (((data1 = getc(handle.file)) == EOF)
  292.       || ((data2 = getc(handle.file)) == EOF)) {
  293.  
  294.       fprintf (stderr, "Cannot open dump file %s\n", name);
  295.       close_all();
  296.       exit(1);
  297.    }
  298.  
  299.    Image->iwidth  = data2 * 256 + data1;
  300.    handle.width = Image->iwidth;
  301.  
  302.    if (((data1 = getc(handle.file)) == EOF)
  303.       || ((data2 = getc(handle.file)) == EOF)) {
  304.  
  305.       fprintf (stderr, "Cannot open dump file %s\n", name);
  306.       close_all();
  307.       exit(1);
  308.    }
  309.  
  310.    Image->iheight = data2 * 256 + data1;
  311.    handle.height = Image->iheight;
  312.  
  313.    Image->width = (DBL)Image->iwidth;
  314.    Image->height = (DBL)Image->iheight;
  315.  
  316.    Image->Colour_Map_Size = 0;
  317.    Image->Colour_Map = NULL;
  318.  
  319.    if ((Image->data.rgb_lines = (struct Image_Line *)
  320.       malloc(Image->iheight * sizeof (struct Image_Line))) == NULL) {
  321.       fprintf (stderr, "Cannot allocate memory for picture: %s\n", name);
  322.       exit(1);
  323.    }
  324.  
  325.    while ((rc = Read_Dump_Int_Line(&handle, &line, &row)) == 1)
  326.       Image->data.rgb_lines[row] = line;
  327.  
  328.    fclose (handle.file);
  329.  
  330.    if (rc == 0)
  331.       return;
  332.    else {
  333.       close_all();
  334.       exit(1);
  335.    }
  336. }
  337.