image.C
6.23 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
214
215
/******************************************************************************\ 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: image.C
* CLASS: Image
* DESCRIPTION:
* AUTHOR: Mike Goslin
* CREATED: 3/29/95
* REVISIONS:
*
*******************************************************************************/
#include <stdlib.h>
#include <iostream.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
#include "image.H"
/*******************************************************************************
* Function: allocate
* Class: Image
* Access: Private
* Description: Allocates memory for an image of size x by y of type type
* Input:
* Output: A void pointer to the matrix in memory
*******************************************************************************/
uint Image::allocate(void)
{
if ((data = (txtrData **)calloc(ysize*sizeof(txtrData), type)) == NULL)
{
fprintf(stderr, "Couldn't allocate memory for texture map\n");
data = NULL;
return (0);
}
for (int i=0; i<ysize; i++)
{
if ((data[i] = (txtrData *)calloc(xsize*sizeof(txtrData), type))== NULL)
{
fprintf(stderr, "Couldn't allocate memory for texture map\n");
data = NULL;
return (0);
}
}
return (1);
}
/*******************************************************************************
* Function: Constructor
* Class: Image
* Access: Public
* Description: Creates a default image of size 1x1 of txtrData
* Input:
* Output:
*******************************************************************************/
Image::Image(void)
{
xsize = 1;
ysize = 1;
type = IM_DATA;
allocate();
}
/*******************************************************************************
* Function: Constructor
* Class: Image
* Access: Public
* Description:
* Input:
* Output:
*******************************************************************************/
Image::Image(uint size)
{
xsize = size;
ysize = size;
type = IM_DATA;
allocate();
}
/*******************************************************************************
* Function: Constructor
* Class: Image
* Access: Public
* Description:
* Input:
* Output:
*******************************************************************************/
Image::Image(uint size, uint _type)
{
xsize = size;
ysize = size;
type = _type;
allocate();
}
/*******************************************************************************
* Function: Destructor
* Class: Image
* Access: Public
* Description:
* Input:
* Output:
*******************************************************************************/
Image::~Image()
{
for (int i=0; i<ysize; i++)
free(data[i]);
free(data);
}
/*******************************************************************************
* Function: set
* Class: Image
* Access: Public
* Description:
* Input:
* Output:
*******************************************************************************/
void Image::set(uint col, uint row, txtrData value)
{
VEC3COPY(data[row][col], value);
}
/*******************************************************************************
* Function: set
* Class: Image
* Access: Public
* Description:
* Input:
* Output:
*******************************************************************************/
void Image::set(txtrData value)
{
for (int y=0; y<ysize; y++)
for (int x=0; x<xsize; x++)
set(y, x, value);
}
/*******************************************************************************
* Function: operator()
* Class: Image
* Access: Public
* Description:
* Input:
* Output:
*******************************************************************************/
txtrData& Image::operator()(uint row, uint col)
{
return (data[row][col]);
}
/*******************************************************************************
* Function: Write
* Class: Image
* Access: Public
* Description:
* Input:
* Output:
*******************************************************************************/
uint Image::Write(FILE *file)
{
int x, y;
// write all red, then all green, then all blue
for (y=0; y<ysize; y++)
for (x=0; x<xsize; x++)
fwrite(&data[y][x][0], sizeof(unsigned char),
1, file);
for (y=0; y<ysize; y++)
for (x=0; x<xsize; x++)
fwrite(&data[y][x][1], sizeof(unsigned char),
1, file);
for (y=0; y<ysize; y++)
for (x=0; x<xsize; x++)
fwrite(&data[y][x][2], sizeof(unsigned char),
1, file);
return (1);
}