-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixmap.h
77 lines (58 loc) · 1.4 KB
/
pixmap.h
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
#pragma once
#include <cstdint>
#include <cstddef>
#include <cstring>
#include <vector>
#include <algorithm>
namespace gge {
enum class pixel_type { GRAY, GRAY_ALPHA, RGB, RGB_ALPHA };
namespace detail {
template <pixel_type PixelType>
struct pixel_type_to_size;
template <>
struct pixel_type_to_size<pixel_type::GRAY>
{
static const size_t size = 1;
};
template <>
struct pixel_type_to_size<pixel_type::GRAY_ALPHA>
{
static const size_t size = 2;
};
template <>
struct pixel_type_to_size<pixel_type::RGB>
{
static const size_t size = 3;
};
template <>
struct pixel_type_to_size<pixel_type::RGB_ALPHA>
{
static const size_t size = 4;
};
} // detail
template <pixel_type PixelType>
struct pixmap
{
pixmap(size_t width, size_t height)
: width(width)
, height(height)
, data(width*height*pixel_size)
{ }
pixmap resize(size_t new_width, size_t new_height) const
{
pixmap new_pixmap(new_width, new_height);
const size_t copy_height = std::min(height, new_height);
const size_t copy_width = std::min(width, new_width);
for (size_t i = 0; i < copy_height; i++) {
uint8_t *dest = &new_pixmap.data[i*new_width*pixel_size];
const uint8_t *src = &data[i*width*pixel_size];
::memcpy(dest, src, copy_width*pixel_size);
}
return new_pixmap;
}
static const size_t pixel_size = detail::pixel_type_to_size<PixelType>::size;
size_t width;
size_t height;
std::vector<uint8_t> data;
};
} // gge