txtrWriter.C
6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/******************************************************************************\ Copyright 1995 The University of North Carolina at Chapel Hill.
All Rights Reserved.
Permission to use, copy, modify and distribute this software and its
documentation for educational, research and non-profit purposes, without
fee, and without a written agreement is hereby granted, provided that the
above copyright notice and the following three paragraphs appear in all
copies.
IN NO EVENT SHALL THE UNIVERSITY OF NORTH CAROLINA
AT CHAPEL HILL BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE
UNIVERSITY OF NORTH CAROLINA HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
THE UNIVERSITY OF NORTH CAROLINA SPECIFICALLY DISCLAIM ANY WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HERUNDER IS
ON AN "AS IS" BASIS, AND THE UNIVERSITY OF NORTH CAROLINA HAS NO OBLIGATIONS
TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
The author may be contacted via:
US Mail: Mike Goslin
Department of Computer Science
Sitterson Hall, CB #3175
University of N. Carolina
Chapel Hill, NC 27599-3175
Phone: (919)962-1719
EMail: goslin@cs.unc.edu
*******************************************************************************/
/*******************************************************************************
*
* FILENAME: txtrWriter.C
* CLASS: TxtrWriter
* DESCRIPTION: SGI Texture map file (.rgb) writer class
* AUTHOR: Mike Goslin
* CREATED: 3/24/95
* REVISIONS:
*
*******************************************************************************/
#include <stdlib.h>
#include <iostream.h>
#include <math.h>
#include <string.h>
#include "fileWriter.H"
/*******************************************************************************
* Function: Constructor
* Class: TxtrWriter
* Access: Public
* Description: Default constructor
* Input:
* Output:
*******************************************************************************/
TxtrWriter::TxtrWriter(void)
{
file = NULL;
strcpy(fname, "DEFAULT");
image = NULL;
}
/*******************************************************************************
* Function: Constructor
* Class: TxtrWriter
* Access: Public
* Description:
* Input: single size for both u and v dimensions
* Output:
*******************************************************************************/
TxtrWriter::TxtrWriter(uint size)
{
file = NULL;
strcpy(fname, "DEFAULT");
if (size > MAX_XSIZE)
{
fprintf(stderr, "Texture size requested is larger than MAX_SIZE\n");
exit(0);
}
image = new Image(size);
}
/*******************************************************************************
* Function: Destructor
* Class: TxtrWriter
* Access: Public
* Description:
* Input:
* Output:
*******************************************************************************/
TxtrWriter::~TxtrWriter()
{
delete image;
}
/*******************************************************************************
* Function: putbyte
* Class: TxtrWriter
* Access: Public
* Description:
* Input:
* Output:
*******************************************************************************/
int TxtrWriter::putbyte(unsigned char val)
{
unsigned char buf[1];
buf[0] = val;
return fwrite(buf,1,1,file);
}
/*******************************************************************************
* Function: putshort
* Class: TxtrWriter
* Access: Public
* Description:
* Input:
* Output:
*******************************************************************************/
int TxtrWriter::putshort(unsigned short val)
{
unsigned char buf[2];
buf[0] = (uchar)(val>>8);
buf[1] = (uchar)(val>>0);
return fwrite(buf,2,1,file);
}
/*******************************************************************************
* Function: putlong
* Class: TxtrWriter
* Access: Public
* Description:
* Input:
* Output:
*******************************************************************************/
int TxtrWriter::putlong(unsigned long val)
{
unsigned char buf[4];
buf[0] = (unsigned char)(val>>24);
buf[1] = (unsigned char)(val>>16);
buf[2] = (unsigned char)(val>>8);
buf[3] = (unsigned char)(val>>0);
return fwrite(buf,4,1,file);
}
/*******************************************************************************
* Function: Write
* Class: TxtrWriter
* Access: Public
* Description:
* Input:
* Output:
*******************************************************************************/
uint TxtrWriter::Write(uint x, uint y, txtrData value)
{
image->set(x, y, value);
return (1);
}
/*******************************************************************************
* Function: Write
* Class: TxtrWriter
* Access: Public
* Description:
* Input:
* Output:
*******************************************************************************/
uint TxtrWriter::Write(char *_fname)
{
char iname[80];
int i;
strcpy(fname, _fname);
if (openFile() == 0)
{
fprintf(stderr, "openFile: Could not open file < %s >\n", fname);
return (0);
}
/* data is 3*ysize pointers to xsize chars */
putshort(474); /* MAGIC */
putbyte(0); /* STORAGE is VERBATIM */
putbyte(1); /* BPC is 1 */
putshort(3); /* DIMENSION is 2 */
putshort((short)image->sizex()); /* XSIZE */
putshort((short)image->sizey()); /* YSIZE */
putshort(3); /* ZSIZE */
putlong(0); /* PIXMIN is 0 */
putlong(255); /* PIXMAX is 255 */
for(i=0; i<4; i++) /* DUMMY 4 bytes */
putbyte(0);
strcpy(iname,fname);
fwrite(iname,80,1,file); /* IMAGENAME */
putlong(0); /* COLORMAP is 0 */
for(i=0; i<404; i++) /* DUMMY 404 bytes */
putbyte(0);
image->Write(file);
fclose(file);
return (1);
}