forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.cpp
More file actions
312 lines (268 loc) · 6.79 KB
/
image.cpp
File metadata and controls
312 lines (268 loc) · 6.79 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// image.cpp
//
// Copyright (C) 2001, Chris Laurel
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include <algorithm>
#include <cassert>
#include <fstream>
#include <iostream>
#include <celengine/glsupport.h>
#include <celutil/debug.h>
#include <celutil/filetype.h>
#include <celutil/gettext.h>
#include <celimage/imageformats.h>
#include "image.h"
using namespace std;
using celestia::PixelFormat;
namespace
{
// All rows are padded to a size that's a multiple of 4 bytes
int pad(int n)
{
return (n + 3) & ~0x3;
}
int formatComponents(PixelFormat fmt)
{
switch (fmt)
{
case PixelFormat::RGBA:
case PixelFormat::BGRA:
return 4;
case PixelFormat::RGB:
case PixelFormat::BGR:
return 3;
case PixelFormat::LUM_ALPHA:
return 2;
case PixelFormat::ALPHA:
case PixelFormat::LUMINANCE:
return 1;
// Compressed formats
case PixelFormat::DXT1:
return 3;
case PixelFormat::DXT3:
case PixelFormat::DXT5:
return 4;
// Unknown format
default:
return 0;
}
}
int calcMipLevelSize(PixelFormat fmt, int w, int h, int mip)
{
w = max(w >> mip, 1);
h = max(h >> mip, 1);
switch (fmt)
{
case PixelFormat::DXT1:
// 4x4 blocks, 8 bytes per block
return ((w + 3) / 4) * ((h + 3) / 4) * 8;
case PixelFormat::DXT3:
case PixelFormat::DXT5:
// 4x4 blocks, 16 bytes per block
return ((w + 3) / 4) * ((h + 3) / 4) * 16;
default:
return h * pad(w * formatComponents(fmt));
}
}
} // anonymous namespace
Image::Image(PixelFormat fmt, int w, int h, int mip) :
width(w),
height(h),
mipLevels(mip),
format(fmt)
{
components = formatComponents(fmt);
assert(components != 0);
pitch = pad(w * components);
size = 1;
for (int i = 0; i < mipLevels; i++)
size += calcMipLevelSize(fmt, w, h, i);
pixels = make_unique<uint8_t[]>(size);
}
bool Image::isValid() const noexcept
{
return pixels != nullptr;
}
int Image::getWidth() const
{
return width;
}
int Image::getHeight() const
{
return height;
}
int Image::getPitch() const
{
return pitch;
}
int Image::getMipLevelCount() const
{
return mipLevels;
}
int Image::getSize() const
{
return size;
}
PixelFormat Image::getFormat() const
{
return format;
}
int Image::getComponents() const
{
return components;
}
uint8_t* Image::getPixels()
{
return pixels.get();
}
uint8_t* Image::getPixelRow(int mip, int row)
{
/*int w = max(width >> mip, 1); Unused*/
int h = max(height >> mip, 1);
if (mip >= mipLevels || row >= h)
return nullptr;
// Row addressing of compressed textures is not allowed
if (isCompressed())
return nullptr;
return getMipLevel(mip) + row * pitch;
}
uint8_t* Image::getPixelRow(int row)
{
return getPixelRow(0, row);
}
uint8_t* Image::getMipLevel(int mip)
{
if (mip >= mipLevels)
return nullptr;
int offset = 0;
for (int i = 0; i < mip; i++)
offset += calcMipLevelSize(format, width, height, i);
return pixels.get() + offset;
}
int Image::getMipLevelSize(int mip) const
{
if (mip >= mipLevels)
return 0;
return calcMipLevelSize(format, width, height, mip);
}
bool Image::isCompressed() const
{
switch (format)
{
case PixelFormat::DXT1:
case PixelFormat::DXT3:
case PixelFormat::DXT5:
return true;
default:
return false;
}
}
bool Image::hasAlpha() const
{
switch (format)
{
case PixelFormat::DXT3:
case PixelFormat::DXT5:
case PixelFormat::RGBA:
case PixelFormat::BGRA:
case PixelFormat::LUM_ALPHA:
case PixelFormat::ALPHA:
return true;
default:
return false;
}
}
/**
* Convert an input height map to a normal map. Ideally, a single channel
* input should be used. If not, the first color channel of the input image
* is the one only one used when generating normals. This produces the
* expected results for grayscale values in RGB images.
*/
Image* Image::computeNormalMap(float scale, bool wrap) const
{
// Can't do anything with compressed input; there are probably some other
// formats that should be rejected as well . . .
if (isCompressed())
return nullptr;
auto* normalMap = new Image(PixelFormat::RGBA, width, height);
uint8_t* nmPixels = normalMap->getPixels();
int nmPitch = normalMap->getPitch();
// Compute normals using differences between adjacent texels.
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int i0 = i;
int j0 = j;
int i1 = i - 1;
int j1 = j - 1;
if (i1 < 0)
{
if (wrap)
{
i1 = height - 1;
}
else
{
i0++;
i1++;
}
}
if (j1 < 0)
{
if (wrap)
{
j1 = width - 1;
}
else
{
j0++;
j1++;
}
}
auto h00 = (int) pixels[i0 * pitch + j0 * components];
auto h10 = (int) pixels[i0 * pitch + j1 * components];
auto h01 = (int) pixels[i1 * pitch + j0 * components];
float dx = (float) (h10 - h00) * (1.0f / 255.0f) * scale;
float dy = (float) (h01 - h00) * (1.0f / 255.0f) * scale;
auto mag = (float) sqrt(dx * dx + dy * dy + 1.0f);
float rmag = 1.0f / mag;
int n = i * nmPitch + j * 4;
nmPixels[n] = (uint8_t) (128 + 127 * dx * rmag);
nmPixels[n + 1] = (uint8_t) (128 + 127 * dy * rmag);
nmPixels[n + 2] = (uint8_t) (128 + 127 * rmag);
nmPixels[n + 3] = 255;
}
}
return normalMap;
}
Image* LoadImageFromFile(const fs::path& filename)
{
ContentType type = DetermineFileType(filename);
Image* img = nullptr;
fmt::fprintf(clog, _("Loading image from file %s\n"), filename.string());
switch (type)
{
case Content_JPEG:
img = LoadJPEGImage(filename);
break;
case Content_BMP:
img = LoadBMPImage(filename);
break;
case Content_PNG:
img = LoadPNGImage(filename);
break;
case Content_DDS:
case Content_DXT5NormalMap:
img = LoadDDSImage(filename);
break;
default:
DPRINTF(LOG_LEVEL_ERROR, "%s: unrecognized or unsupported image file type.\n", filename.string());
break;
}
return img;
}