-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImageObject.cpp
242 lines (209 loc) · 7.47 KB
/
ImageObject.cpp
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//=================================================================================================================================
//
// Author: Maurice Ribble
// 3D Application Research Group
// ATI Research, Inc.
//
// Implementation of the ImageObject class. This class holds and manages all image data information.
//
//=================================================================================================================================
// $Id: //depot/3darg/Tools/Handheld/esTestApps/common/ImageObject.cpp#2 $
//
// Last check-in: $DateTime: 2008/03/21 10:01:35 $
// Last edited by: $Author: dginsbur $
//=================================================================================================================================
// (C) ATI Research, Inc. 2006 All rights reserved.
//=================================================================================================================================
#include "ImageObject.h"
#include <algorithm>
#include "assert.h"
using namespace std;
//=================================================================================================================================
///
/// Constructor
///
/// \param width - The image width
/// \param height - The image height
/// \param numChannels - Number of channels (ie RGB=3, RGBA=4)
/// \param bitsPerChannel - The bits per channel
///
/// \return void
//=================================================================================================================================
ImageObject::ImageObject( uint32 width, uint32 height, uint32 numChannels, uint32 bitsPerChannel )
{
uint32 totalSize;
assert ( width > 0 );
assert ( height > 0 );
assert ( ( numChannels > 0 ) && ( numChannels <= 4 ) );
assert ( bitsPerChannel == 8 );
m_width = width;
m_height = height;
m_numChannels = numChannels;
m_bitsPerChannel = bitsPerChannel;
totalSize = m_width * m_height * m_numChannels * m_bitsPerChannel / sizeof(uint8);
m_data = new uint8[totalSize];
assert( m_data );
m_compressedType = IMG_NONE;
m_compressedSize = 0;
m_compressedData = NULL;
}
//=================================================================================================================================
///
/// Destructor
///
/// \param void
///
/// \return void
//=================================================================================================================================
ImageObject::~ImageObject()
{
if(m_data) {delete [] m_data; m_data=NULL;}
if(m_compressedData) {delete [] m_compressedData; m_compressedData=NULL;}
}
//=================================================================================================================================
///
/// Access a single component of a single pixel
///
/// \param x - x position of the pixel
/// \param y - y position of the pixel
/// \param channel - channel fo the pixel (ie R, G, B, A)
///
/// \return a reference to the pixel
//=================================================================================================================================
uint8& ImageObject::Pixel( uint32 x, uint32 y, uint32 channel )
{
uint32 pixelPitch = m_numChannels * m_bitsPerChannel / 8;
uint32 linePitch = m_width * pixelPitch;
x = min( x, m_width );
y = min( y, m_height );
channel = min( channel, m_numChannels );
return m_data[y * linePitch + x * pixelPitch + channel];
}
//=================================================================================================================================
///
/// Sets up the type for compressed data
///
/// \param type - the type of compression we will be doing
///
/// \return pass or fail bool
//=================================================================================================================================
bool ImageObject::SetupCompressedData( CompressedType type )
{
if ( m_compressedData )
{
delete [] m_compressedData;
}
switch ( type )
{
case IMG_NONE:
case IMG_ATITC_RGB:
case IMG_ATITC_RGBA:
case IMG_ATI2N:
case IMG_ATI1N:
case IMG_ETC1:
case IMG_ETC3:
case IMG_ETC5:
break;
default:
assert( 0 );
}
m_compressedData = NULL;
m_compressedType = type;
m_compressedSize = 0;
return true;
}
//=================================================================================================================================
///
/// Sets up the size and data buffer for a compressed texture
///
/// \param size - The size of the compressed data
///
/// \return void
//=================================================================================================================================
void ImageObject::SetCompressedSize( uint32 size )
{
if ( m_compressedData )
{
delete [] m_compressedData;
}
m_compressedData = new uint8[size];
assert( m_compressedData );
m_compressedSize = size;
}
//=================================================================================================================================
///
/// Changes the main data (not compressed data) to red
///
/// \param void
///
/// \return void
//=================================================================================================================================
void ImageObject::MakeImageRed()
{
uint32 x, y, c;
for ( y = 0; y < m_height; ++y )
{
for ( x = 0; x < m_height; ++x )
{
Pixel( x, y, 0 ) = 255;
for ( c = 1; c < m_numChannels; ++c )
{
Pixel( x, y, c ) = 0;
}
}
}
}
//=================================================================================================================================
///
/// Changes the main data from RGBA -> ARGB or RGB -> 1RGB
///
/// \param void
///
/// \return True if image has three or four channels, false otherwise
//=================================================================================================================================
bool ImageObject::MakeImageARGB()
{
uint32 x, y;
if ( m_numChannels != 3 && m_numChannels != 4 )
return false;
if ( m_numChannels == 4 )
{
for ( y = 0; y < m_height; ++y )
{
for ( x = 0; x < m_height; ++x )
{
uint8 r = Pixel( x, y, 0 );
uint8 g = Pixel( x, y, 1 );
uint8 b = Pixel( x, y, 2 );
uint8 a = Pixel( x, y, 3 );
Pixel( x, y, 0 ) = b;
Pixel( x, y, 1 ) = g;
Pixel( x, y, 2 ) = r;
Pixel( x, y, 3 ) = a;
}
}
}
else
{
uint32 nPixelSize = 4 * m_bitsPerChannel / 8;
uint8 *pNewData = new uint8[nPixelSize * m_width * m_height];
for ( y = 0; y < m_height; ++y )
{
for ( x = 0; x < m_height; ++x )
{
uint32 nOffset = y * m_width * nPixelSize + x * nPixelSize;
uint8 r = Pixel( x, y, 0 );
uint8 g = Pixel( x, y, 1 );
uint8 b = Pixel( x, y, 2 );
pNewData[nOffset + 0 ] = b;
pNewData[nOffset + 1 ] = g;
pNewData[nOffset + 2 ] = r;
pNewData[nOffset + 3 ] = 255;
}
}
delete [] m_data;
m_data = pNewData;
m_numChannels = 4;
}
return true;
}