-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathutils.cpp
More file actions
70 lines (51 loc) · 2.15 KB
/
Copy pathutils.cpp
File metadata and controls
70 lines (51 loc) · 2.15 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
////////////////////////////////////////////////////////////////////////////////
// Distributed under the Boost Software License, Version 1.0. //
// (See accompanying file LICENSE or copy at //
// https://www.boost.org/LICENSE_1_0.txt) //
////////////////////////////////////////////////////////////////////////////////
#include "graphics/utils.h"
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include "core/data_buffer.h"
#include <stb_image_write.h>
namespace iris
{
std::vector<MipLevelData> generate_mip_maps(const MipLevelData &start_level)
{
std::vector<MipLevelData> mip_maps{start_level};
// each subsequent mipmap is half the size of the preceding one
static constexpr auto scale = 2u;
// next mip level dimensions
auto width = start_level.width / scale;
auto height = start_level.height / scale;
while ((width > 0u) && (height > 0u))
{
const auto scale_x = static_cast<float>(mip_maps.back().width) / static_cast<float>(width);
const auto scale_y = static_cast<float>(mip_maps.back().height) / static_cast<float>(height);
// we work on RGBA images
static constexpr auto stride = 4u;
DataBuffer mip_data(width * height * stride);
auto *dst = mip_data.data();
const auto *src = reinterpret_cast<const std::uint8_t *>(mip_maps.back().data.data());
// simple downsize algorithm by skipping pixels, will produce aliasing
for (auto y = 0u; y < height; ++y)
{
const auto y_nearest = static_cast<int>(std::floor(y * scale_y));
for (auto x = 0u; x < width; ++x)
{
const auto x_nearest = static_cast<int>(std::floor(x * scale_x));
std::memcpy(dst, src + ((y_nearest * mip_maps.back().width) + x_nearest) * stride, stride);
dst += stride;
}
}
mip_maps.push_back({.data = std::move(mip_data), .width = width, .height = height});
width /= scale;
height /= scale;
}
return mip_maps;
}
}