| 1 | // Clip Library | 
|---|
| 2 | // Copyright (c) 2015-2022 David Capello | 
|---|
| 3 | // | 
|---|
| 4 | // This file is released under the terms of the MIT license. | 
|---|
| 5 | // Read LICENSE.txt for more information. | 
|---|
| 6 |  | 
|---|
| 7 | #include "clip.h" | 
|---|
| 8 |  | 
|---|
| 9 | namespace clip { | 
|---|
| 10 |  | 
|---|
| 11 | unsigned long image_spec::required_data_size() const | 
|---|
| 12 | { | 
|---|
| 13 | unsigned long n = (bytes_per_row * height); | 
|---|
| 14 |  | 
|---|
| 15 | // For 24bpp we add some extra space to access the last pixel (3 | 
|---|
| 16 | // bytes) as an uint32_t | 
|---|
| 17 | if (bits_per_pixel == 24) { | 
|---|
| 18 | if ((n % 4) > 0) | 
|---|
| 19 | n += 4 - (n % 4); | 
|---|
| 20 | else | 
|---|
| 21 | ++n; | 
|---|
| 22 | } | 
|---|
| 23 |  | 
|---|
| 24 | return n; | 
|---|
| 25 | } | 
|---|
| 26 |  | 
|---|
| 27 | image::image() | 
|---|
| 28 | : m_own_data(false), | 
|---|
| 29 | m_data(nullptr) | 
|---|
| 30 | { | 
|---|
| 31 | } | 
|---|
| 32 |  | 
|---|
| 33 | image::image(const image_spec& spec) | 
|---|
| 34 | : m_own_data(true), | 
|---|
| 35 | m_data(new char[spec.required_data_size()]), | 
|---|
| 36 | m_spec(spec) { | 
|---|
| 37 | } | 
|---|
| 38 |  | 
|---|
| 39 | image::image(const void* data, const image_spec& spec) | 
|---|
| 40 | : m_own_data(false), | 
|---|
| 41 | m_data((char*)data), | 
|---|
| 42 | m_spec(spec) { | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | image::image(const image& image) | 
|---|
| 46 | : m_own_data(false), | 
|---|
| 47 | m_data(nullptr), | 
|---|
| 48 | m_spec(image.m_spec) { | 
|---|
| 49 | copy_image(image); | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | image::image(image&& image) | 
|---|
| 53 | : m_own_data(false), | 
|---|
| 54 | m_data(nullptr) { | 
|---|
| 55 | move_image(std::move(image)); | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | image::~image() { | 
|---|
| 59 | reset(); | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | image& image::operator=(const image& image) { | 
|---|
| 63 | copy_image(image); | 
|---|
| 64 | return *this; | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | image& image::operator=(image&& image) { | 
|---|
| 68 | move_image(std::move(image)); | 
|---|
| 69 | return *this; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | void image::reset() { | 
|---|
| 73 | if (m_own_data) { | 
|---|
| 74 | delete[] m_data; | 
|---|
| 75 | m_own_data = false; | 
|---|
| 76 | m_data = nullptr; | 
|---|
| 77 | } | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | void image::copy_image(const image& image) { | 
|---|
| 81 | reset(); | 
|---|
| 82 |  | 
|---|
| 83 | m_spec = image.spec(); | 
|---|
| 84 | std::size_t n = m_spec.required_data_size(); | 
|---|
| 85 |  | 
|---|
| 86 | m_own_data = true; | 
|---|
| 87 | m_data = new char[n]; | 
|---|
| 88 | std::copy(image.data(), | 
|---|
| 89 | image.data()+n, | 
|---|
| 90 | m_data); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | void image::move_image(image&& image) { | 
|---|
| 94 | std::swap(m_own_data, image.m_own_data); | 
|---|
| 95 | std::swap(m_data, image.m_data); | 
|---|
| 96 | std::swap(m_spec, image.m_spec); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | } // namespace clip | 
|---|
| 100 |  | 
|---|