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 >
Wrap
Text File
|
1990-10-01
|
5KB
|
204 lines
/*
* outputp.c
*
* Copyright (C) 1989, Craig E. Kolb
*
* This software may be freely copied, modified, and redistributed,
* provided that this copyright notice is preserved on all copies.
*
* There is no warranty or other guarantee of fitness for this software,
* it is provided solely . Bug reports or fixes may be sent
* to the author, who may or may not act on them as he desires.
*
* You may not include this software in a program or other software product
* without supplying the source, or without informing the end-user that the
* source is available for no extra charge.
*
* If you modify this software, you should include a notice giving the
* name of the person performing the modification, the date of modification,
* and the reason for such modification.
*
* $Id: outputp.c,v 3.0.1.3 90/04/04 14:53:04 craig Exp $
*
* $Log: outputp.c,v $
* Revision 3.0.1.3 90/04/04 14:53:04 craig
* patch5: Lint removal.
*
* Revision 3.0.1.2 90/02/12 16:09:05 craig
* patch4: Fixed bug with duplicate headers in mtv files when Appending.
*
* Revision 3.0.1.1 89/12/06 16:33:17 craig
* patch2: Added calls to new error/warning routines.
*
* Revision 3.0 89/10/27 02:05:58 craig
* Baseline for first official release.
*
*/
#include <stdio.h>
#ifdef OSK
#include <errno.h>
#endif
#include "typedefs.h"
#include "constants.h"
#include "funcdefs.h"
#ifndef NORLE
#include "rle.h"
static rle_pixel **buffer; /* Output buffer */
#endif
FILE *imgfile; /* Raster output file */
char outfilename[BUFSIZ]; /* Name of output file */
int Appending; /* Appending to output file? */
/*
* Convert floating-point to unsigned char (0-255).
* This could easily be a macro, but the old SGI compilers dump core
* on it for some reason.
*/
unsigned char
correct(x)
double x;
{
if (x < 0)
return 0;
if (x > 255)
return 255;
return x;
}
#ifndef NORLE
/*
* Open image file and write RLE header.
*/
startpic(argv)
char *argv[];
{
extern int Xres, Yres;
if (*outfilename) {
if (Appending) {
/*
* If we're appending, we *still* have to write
* the Utah Raster header to the file. This is
* due to strangeness in the Utah Raster toolkit,
* which does some vital initialization in sv_setup().
*/
imgfile = rle_open_f("rayshade",outfilename, "r+");
} else
imgfile = rle_open_f("rayshade",outfilename, "w");
if (imgfile == (FILE *)NULL)
yyerror("Cannot open %s for writing.", outfilename);
} else
imgfile = stdout;
rle_dflt_hdr.rle_file = imgfile;
rle_dflt_hdr.xmax = Xres -1;
rle_dflt_hdr.ymax = Yres -1;
rle_dflt_hdr.xmin = rle_dflt_hdr.ymin = 0;
rle_dflt_hdr.ncolors = 3;
rle_dflt_hdr.alpha = 0;
RLE_CLR_BIT( rle_dflt_hdr, RLE_ALPHA );
rle_addhist(argv,(rle_hdr*)NULL,&rle_dflt_hdr);
rle_put_setup(&rle_dflt_hdr);
/*
* Flush the header. If we don't, and LINDA forks off
* a bunch of workers, strange things will happen (they'll
* all flush the buffer when they die, and you end up with
* lots of headers at the end of the file).
*/
fflush(rle_dflt_hdr.rle_file);
if ( rle_row_alloc( &rle_dflt_hdr, &buffer ) < 0 )
{
fprintf( stderr, "rayshade: Unable to allocate image memory.\n" );
#ifndef OSK
exit( RLE_NO_SPACE );
#else
exit(errno);
#endif
}
/*
buffer[0]=(unsigned char *)Malloc(sizeof(unsigned char)*(unsigned)Xres);
buffer[1]=(unsigned char *)Malloc(sizeof(unsigned char)*(unsigned)Xres);
buffer[2]=(unsigned char *)Malloc(sizeof(unsigned char)*(unsigned)Xres);
*/
if (Appending)
(void)fseek(imgfile, 0L, 2); /* Go to end of file */
}
/*
* Write a scanline of output.
* "buf" is an array of Color structures of size Xres. Each color
* component is normalized to [0, 1.].
*/
outline(buf)
Color *buf;
{
register int i;
extern int Xres;
for(i = 0;i < Xres;i++) {
/*
* Scale colors to fit unsigned char and check for
* over/underflow.
*/
buffer[0][i] = correct(255 * buf[i].r);
buffer[1][i] = correct(255 * buf[i].g);
buffer[2][i] = correct(255 * buf[i].b);
}
rle_putrow(buffer, Xres, &rle_dflt_hdr);
}
/*
* Close image file.
*/
endpic()
{
rle_row_free( &rle_dflt_hdr, buffer );
rle_puteof(&rle_dflt_hdr);
(void)fclose(imgfile);
}
#else /* NORLE */
startpic(argv)
char *argv[];
{
extern int Xres, Yres;
if (*outfilename) {
if (Appending)
imgfile = fopen(outfilename, "a");
else
imgfile = fopen(outfilename, "w");
if (imgfile == (FILE *)NULL)
RSerror("Cannot open %s for writing.", outfilename);
} else
imgfile = stdout;
if (!Appending)
fprintf(imgfile,"%d %d\n",Xres, Yres);
fflush(imgfile);
}
outline(buf)
Color *buf;
{
register int i;
extern int Xres;
for (i = 0; i < Xres; i++) {
(void)fputc(correct(255.*buf[i].r), imgfile);
(void)fputc(correct(255.*buf[i].g), imgfile);
(void)fputc(correct(255.*buf[i].b), imgfile);
}
fflush(imgfile);
}
endpic()
{
(void)fclose(imgfile);
}
#endif /* NORLE */