Tachyon (current)  Current Main Branch
sgirgb.c
Go to the documentation of this file.
1 /*
2  * sgirgb.h - This file deals with SGI RGB format image files (reading/writing)
3  *
4  * (C) Copyright 1994-2022 John E. Stone
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * $Id: sgirgb.c,v 1.6 2022/02/18 17:55:28 johns Exp $
8  *
9  */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <math.h>
15 
16 #define TACHYON_INTERNAL 1
17 #include "tachyon.h"
18 #include "util.h"
19 #include "imageio.h" /* error codes etc */
20 #include "sgirgb.h"
21 
22 static void putbyte(FILE * outf, unsigned char val) {
23  unsigned char buf[1];
24  buf[0] = val;
25  fwrite(buf, 1, 1, outf);
26 }
27 
28 static void putshort(FILE * outf, unsigned short val) {
29  unsigned char buf[2];
30  buf[0] = val >> 8;
31  buf[1] = val & 0xff;
32  fwrite(buf, 2, 1, outf);
33 }
34 
35 static void putint(FILE * outf, unsigned int val) {
36  unsigned char buf[4];
37  buf[0] = (unsigned char) (val >> 24);
38  buf[1] = (unsigned char) (val >> 16);
39  buf[2] = (unsigned char) (val >> 8);
40  buf[3] = (unsigned char) (val & 0xff);
41  fwrite(buf, 4, 1, outf);
42 }
43 
44 int writergb(char *name, int xres, int yres, unsigned char *imgdata) {
45  FILE * ofp;
46  char iname[80]; /* Image name */
47  int x, y, i;
48 
49  if ((ofp = fopen(name, "wb")) != NULL) {
50  putshort(ofp, 474); /* Magic */
51  putbyte(ofp, 0); /* STORAGE is VERBATIM */
52  putbyte(ofp, 1); /* BPC is 1 */
53  putshort(ofp, 3); /* DIMENSION is 3 */
54  putshort(ofp, (unsigned short) xres); /* XSIZE */
55  putshort(ofp, (unsigned short) yres); /* YSIZE */
56  putshort(ofp, 3); /* ZSIZE */
57  putint(ofp, 0); /* PIXMIN is 0 */
58  putint(ofp, 255); /* PIXMAX is 255 */
59 
60  for(i=0; i<4; i++) /* DUMMY 4 bytes */
61  putbyte(ofp, 0);
62 
63  strcpy(iname, "Tachyon Ray Tracer Image");
64  fwrite(iname, 80, 1, ofp); /* IMAGENAME */
65  putint(ofp, 0); /* COLORMAP is 0 */
66 
67  for(i=0; i<404; i++) /* DUMMY 404 bytes */
68  putbyte(ofp,0);
69 
70  for(i=0; i<3; i++)
71  for(y=0; y<yres; y++)
72  for(x=0; x<xres; x++)
73  fwrite(&imgdata[(y*xres + x)*3 + i], 1, 1, ofp);
74 
75  fclose(ofp);
76  }
77 
78  return IMAGENOERR;
79 }
80 
int writergb(char *name, int xres, int yres, unsigned char *imgdata)
Definition: sgirgb.c:44
Tachyon cross-platform timers, special math function wrappers, and RNGs.
#define IMAGENOERR
no error
Definition: imageio.h:16
static void putbyte(FILE *outf, unsigned char val)
Definition: sgirgb.c:22
static void putshort(FILE *outf, unsigned short val)
Definition: sgirgb.c:28
static void putint(FILE *outf, unsigned int val)
Definition: sgirgb.c:35
Tachyon public API function prototypes and declarations used to drive the ray tracing engine...