home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2213 / output.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  1.4 KB  |  109 lines

  1.  
  2. /*
  3.  * output.c - This files contains all of the output image related functions.
  4.  * 
  5.  * Copyright (C) 1990, Kory Hamzeh
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include "rt.h"
  10. #include "externs.h"
  11.  
  12. #define CLAMPING    FALSE
  13.  
  14. FILE           *out_fp;
  15.  
  16. /*
  17.  * Init_output_file()
  18.  * 
  19.  * Create and initialize the output image file.
  20.  */
  21.  
  22. Init_output_file(filename)
  23. char           *filename;
  24. {
  25.     long            xx;
  26.  
  27.     if (filename)
  28.     {
  29.         if ((out_fp = fopen(output_file, "w")) == NULL)
  30.         {
  31.             fprintf(stderr, "%s: unable to create output file '%s'\n",
  32.                 my_name, output_file);
  33.             exit(1);
  34.         }
  35.     }
  36.     else
  37.     {
  38.         out_fp = stdout;
  39.     }
  40.  
  41.     if(do_image_size)
  42.     {
  43.         fprintf(out_fp, "%d %d\n", view.x_res, view.y_res);
  44.     }
  45. }
  46.  
  47. /*
  48.  * Close_output_file()
  49.  * 
  50.  * Do just like it sez.
  51.  */
  52.  
  53. Close_output_file(filename)
  54. char           *filename;
  55. {
  56.     if (filename)
  57.         fclose(out_fp);
  58. }
  59.  
  60. /*
  61.  * Write_pixel()
  62.  * 
  63.  * Write the given RGB color pixel to the output file. Apply clamping if
  64.  * necessary.
  65.  */
  66.  
  67. Write_pixel(c)
  68. COLOR          *c;
  69. {
  70.     unsigned char   rr, gg, bb;
  71.     double          mc;
  72.  
  73. #if CLAMPING
  74.     if (c->r > 1.0)
  75.         c->r = 1.0;
  76.     if (c->g > 1.0)
  77.         c->g = 1.0;
  78.     if (c->b == 1.0)
  79.         c->b = 1.0;
  80. #endif
  81.  
  82.     mc = c->r;
  83.     if (c->g > mc)
  84.         mc = c->g;
  85.     if (c->b > mc)
  86.         mc = c->b;
  87.  
  88.     if (mc > 1)
  89.     {
  90.         c->r /= mc;
  91.         c->g /= mc;
  92.         c->b /= mc;
  93.     }
  94.  
  95.     rr = 255.0 * c->r;
  96.     gg = 255.0 * c->g;
  97.     bb = 255.0 * c->b;
  98.  
  99.     putc(rr, out_fp);
  100.     putc(gg, out_fp);
  101.     putc(bb, out_fp);
  102. }
  103.  
  104. Flush_output_file()
  105. {
  106.     return ;
  107. }
  108.  
  109.