forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimage_buffer.h
68 lines (54 loc) · 1.34 KB
/
image_buffer.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
// Aseprite Document Library
// Copyright (C) 2019-2024 Igara Studio S.A.
// Copyright (C) 2001-2016 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifndef DOC_IMAGE_BUFFER_H_INCLUDED
#define DOC_IMAGE_BUFFER_H_INCLUDED
#pragma once
#include "base/disable_copying.h"
#include "base/ints.h"
#include "doc/aligned_memory.h"
#include <algorithm>
#include <cstddef>
#include <cstdlib>
#include <memory>
namespace doc {
class ImageBuffer {
public:
ImageBuffer(std::size_t size = 1)
: m_size(doc_align_size(size))
, m_buffer((uint8_t*)doc_aligned_alloc(m_size))
{
if (!m_buffer)
throw std::bad_alloc();
}
~ImageBuffer() noexcept
{
if (m_buffer)
doc_aligned_free(m_buffer);
}
std::size_t size() const { return m_size; }
uint8_t* buffer() { return (uint8_t*)m_buffer; }
void resizeIfNecessary(std::size_t size)
{
if (size > m_size) {
if (m_buffer) {
doc_aligned_free(m_buffer);
m_buffer = nullptr;
}
m_size = doc_align_size(size);
m_buffer = (uint8_t*)doc_aligned_alloc(m_size);
if (!m_buffer)
throw std::bad_alloc();
}
}
private:
size_t m_size;
uint8_t* m_buffer;
DISABLE_COPYING(ImageBuffer);
};
using ImageBufferPtr = std::shared_ptr<ImageBuffer>;
} // namespace doc
#endif