home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / GRAPHICS / rayshade.lzh / outputp.c < prev    next >
Text File  |  1990-10-01  |  5KB  |  204 lines

  1. /*
  2.  * outputp.c
  3.  *
  4.  * Copyright (C) 1989, Craig E. Kolb
  5.  *
  6.  * This software may be freely copied, modified, and redistributed,
  7.  * provided that this copyright notice is preserved on all copies.
  8.  *
  9.  * There is no warranty or other guarantee of fitness for this software,
  10.  * it is provided solely .  Bug reports or fixes may be sent
  11.  * to the author, who may or may not act on them as he desires.
  12.  *
  13.  * You may not include this software in a program or other software product
  14.  * without supplying the source, or without informing the end-user that the
  15.  * source is available for no extra charge.
  16.  *
  17.  * If you modify this software, you should include a notice giving the
  18.  * name of the person performing the modification, the date of modification,
  19.  * and the reason for such modification.
  20.  *
  21.  * $Id: outputp.c,v 3.0.1.3 90/04/04 14:53:04 craig Exp $
  22.  *
  23.  * $Log:    outputp.c,v $
  24.  * Revision 3.0.1.3  90/04/04  14:53:04  craig
  25.  * patch5: Lint removal.
  26.  * 
  27.  * Revision 3.0.1.2  90/02/12  16:09:05  craig
  28.  * patch4: Fixed bug with duplicate headers in mtv files when Appending.
  29.  * 
  30.  * Revision 3.0.1.1  89/12/06  16:33:17  craig
  31.  * patch2: Added calls to new error/warning routines.
  32.  * 
  33.  * Revision 3.0  89/10/27  02:05:58  craig
  34.  * Baseline for first official release.
  35.  * 
  36.  */
  37. #include <stdio.h>
  38. #ifdef OSK
  39. #include <errno.h>
  40. #endif
  41. #include "typedefs.h"
  42. #include "constants.h"
  43. #include "funcdefs.h"
  44. #ifndef NORLE
  45. #include "rle.h"
  46. static rle_pixel **buffer;    /* Output buffer */
  47. #endif
  48.  
  49. FILE *imgfile;            /* Raster output file */
  50. char outfilename[BUFSIZ];    /* Name of output file */
  51. int Appending;            /* Appending to output file? */
  52.  
  53. /*
  54.  * Convert floating-point to unsigned char (0-255).
  55.  * This could easily be a macro, but the old SGI compilers dump core
  56.  * on it for some reason.
  57.  */
  58. unsigned char
  59. correct(x)
  60. double x;
  61. {
  62.     if (x < 0)
  63.         return 0;
  64.     if (x > 255)
  65.         return 255;
  66.     return x;
  67. }
  68.  
  69. #ifndef NORLE
  70. /*
  71.  * Open image file and write RLE header.
  72.  */
  73. startpic(argv)
  74. char *argv[];
  75. {
  76.     extern int Xres, Yres;
  77.  
  78.     if (*outfilename) {
  79.         if (Appending) {
  80.             /*
  81.              * If we're appending, we *still* have to write
  82.              * the Utah Raster header to the file.  This is
  83.              * due to strangeness in the Utah Raster toolkit,
  84.              * which does some vital initialization in sv_setup().
  85.              */
  86.             imgfile = rle_open_f("rayshade",outfilename, "r+");
  87.         } else
  88.             imgfile = rle_open_f("rayshade",outfilename, "w");
  89.         if (imgfile == (FILE *)NULL)
  90.             yyerror("Cannot open %s for writing.", outfilename);
  91.     } else
  92.         imgfile = stdout;
  93.     rle_dflt_hdr.rle_file = imgfile;
  94.     rle_dflt_hdr.xmax = Xres -1;
  95.     rle_dflt_hdr.ymax = Yres -1;
  96.     rle_dflt_hdr.xmin = rle_dflt_hdr.ymin = 0;
  97.     rle_dflt_hdr.ncolors = 3;
  98.     rle_dflt_hdr.alpha = 0;
  99.     RLE_CLR_BIT( rle_dflt_hdr, RLE_ALPHA );
  100.     rle_addhist(argv,(rle_hdr*)NULL,&rle_dflt_hdr);
  101.     rle_put_setup(&rle_dflt_hdr);
  102.     /*
  103.      * Flush the header.  If we don't, and LINDA forks off
  104.      * a bunch of workers, strange things will happen (they'll
  105.      * all flush the buffer when they die, and you end up with
  106.      * lots of headers at the end of the file).
  107.      */
  108.     fflush(rle_dflt_hdr.rle_file);
  109.     if ( rle_row_alloc( &rle_dflt_hdr, &buffer ) < 0 )
  110.     {
  111.         fprintf( stderr, "rayshade: Unable to allocate image memory.\n" );
  112. #ifndef OSK
  113.         exit( RLE_NO_SPACE );
  114. #else
  115.         exit(errno);
  116. #endif
  117.     }
  118. /*
  119.     buffer[0]=(unsigned char *)Malloc(sizeof(unsigned char)*(unsigned)Xres);
  120.     buffer[1]=(unsigned char *)Malloc(sizeof(unsigned char)*(unsigned)Xres);
  121.     buffer[2]=(unsigned char *)Malloc(sizeof(unsigned char)*(unsigned)Xres);
  122. */
  123.     if (Appending)
  124.         (void)fseek(imgfile, 0L, 2);    /* Go to end of file */
  125. }
  126.  
  127.  
  128. /*
  129.  * Write a scanline of output.
  130.  * "buf" is an array of Color structures of size Xres.  Each color
  131.  * component is normalized to [0, 1.].
  132.  */
  133. outline(buf)
  134. Color *buf;
  135. {
  136.     register int i;
  137.     extern int Xres;
  138.  
  139.     for(i = 0;i < Xres;i++) {
  140.         /*
  141.          * Scale colors to fit unsigned char and check for
  142.          * over/underflow.
  143.          */
  144.         buffer[0][i] = correct(255 * buf[i].r);
  145.         buffer[1][i] = correct(255 * buf[i].g);
  146.         buffer[2][i] = correct(255 * buf[i].b);
  147.     }
  148.     rle_putrow(buffer, Xres, &rle_dflt_hdr);
  149. }
  150.  
  151. /*
  152.  * Close image file.
  153.  */
  154. endpic()
  155. {
  156.     rle_row_free( &rle_dflt_hdr, buffer );
  157.     rle_puteof(&rle_dflt_hdr);
  158.     (void)fclose(imgfile);
  159. }
  160.  
  161. #else /* NORLE */
  162.  
  163. startpic(argv)
  164. char *argv[];
  165. {
  166.     extern int Xres, Yres;
  167.  
  168.     if (*outfilename) {
  169.         if (Appending)
  170.             imgfile = fopen(outfilename, "a");
  171.         else
  172.             imgfile = fopen(outfilename, "w");
  173.         if (imgfile == (FILE *)NULL)
  174.             RSerror("Cannot open %s for writing.", outfilename);
  175.     } else
  176.         imgfile = stdout;
  177.  
  178.     if (!Appending)
  179.         fprintf(imgfile,"%d %d\n",Xres, Yres);
  180.  
  181.     fflush(imgfile);
  182. }
  183.  
  184. outline(buf)
  185. Color *buf;
  186. {
  187.     register int i;
  188.     extern int Xres;
  189.  
  190.     for (i = 0; i < Xres; i++) {
  191.         (void)fputc(correct(255.*buf[i].r), imgfile);
  192.         (void)fputc(correct(255.*buf[i].g), imgfile);
  193.         (void)fputc(correct(255.*buf[i].b), imgfile);
  194.     }
  195.     fflush(imgfile);
  196. }
  197.  
  198. endpic()
  199. {
  200.     (void)fclose(imgfile);
  201. }
  202. #endif /* NORLE */
  203.  
  204.