File: Image.h

package info (click to toggle)
pymol 2.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 43,312 kB
  • sloc: cpp: 480,106; python: 79,860; ansic: 28,343; javascript: 6,792; sh: 47; makefile: 30; csh: 8
file content (238 lines) | stat: -rw-r--r-- 5,891 bytes parent folder | download
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
#pragma once

#include <cstdint>
#include <exception>
#include <vector>
#include "pymol/algorithm.h"
#include "pymol/type_traits.h"

namespace pymol
{

class ill_informed_image : public std::exception
{
  virtual const char* what() const noexcept
  {
    return "Image Construction ill-informed.";
  }
};

class Image
{
private:
  std::vector<unsigned char> m_data;
  int m_width{};
  int m_height{};
  bool m_stereo{false};

public:
  /**
   * Channel indices.
   * Examples:
   * - bits()[Channel::ALPHA] -> first pixel's alpha channel
   * - bits()[Channel::RED + 2 * getPixelSize()] -> third pixel's red channel
   */
  enum Channel : std::uint8_t { RED = 0, GREEN = 1, BLUE = 2, ALPHA = 3 };

  /**
   * Get the size of one pixel in bytes (should be 4)
   */
  static std::size_t getPixelSize() { return sizeof(std::uint32_t); }

  Image() = default;

  /**
   * Construct a black, full-transparent (alpha=0) image.
   * @param width Width in pixels
   * @param height Height in pixels
   * @param stereo Make a stereo image (doubles the buffer size)
   */
  Image(int width, int height, bool stereo = false)
      : m_width(width), m_height(height), m_stereo(stereo)
  {
    if (m_width < 0 || m_height < 0) {
      throw ill_informed_image{};
    }
    auto newSize = width * height * getPixelSize();

    if (stereo) {
      newSize *= 2;
    }
    m_data.resize(newSize, 0x00);
  }

  /**
   * Get the width and height in pixels
   */
  const std::pair<int, int> getSize() const
  {
    return std::make_pair(m_width, m_height);
  }

  /**
   * Get the size of the image (not the stereo buffer) in bytes.
   * Should be equal to getWidth() * getHeight() * getPixelSize().
   * If this is a stereo image, then bits() will point to a buffer
   * of size getSizeInBytes() * 2.
   */
  const std::size_t getSizeInBytes() const noexcept
  {
    if (!m_stereo) {
      return m_data.size();
    } else {
      return m_data.size() / 2;
    }
  }

  /**
   * Get the width in pixels
   */
  const int getWidth() const noexcept { return m_width; }

  /**
   * Get the height in pixels
   */
  const int getHeight() const noexcept { return m_height; }

  /**
   * True if this instance holds a stereo image (two images).
   * bits() will point to the left image and
   * bits() + getSizeInBytes() will point to the right image.
   */
  const bool isStereo() const noexcept { return m_stereo; }

  /**
   * Returns a pointer to the first pixel's first channel.
   * Channels are 8 bit values.
   * @see pixels()
   */
  unsigned char* bits() noexcept { return m_data.data(); }
  const unsigned char* bits() const noexcept { return m_data.data(); }

  /**
   * Returns a pointer to the first pixel.
   * Pixels are 32 bit RGBA values.
   * @see bits()
   */
  std::uint32_t* pixels() noexcept
  {
    return reinterpret_cast<std::uint32_t*>(bits());
  }
  const std::uint32_t* pixels() const noexcept
  {
    return reinterpret_cast<const std::uint32_t*>(bits());
  }

  /**
   * True if width and height are both zero.
   */
  bool empty() const noexcept { return m_data.empty(); }
  bool operator==(const Image& other) const noexcept
  {
    return m_width == other.m_width && m_height == other.m_height &&
           m_stereo == other.m_stereo && m_data == other.m_data;
  }
  bool operator!=(const Image& other) const noexcept
  {
    return !(*this == other);
  }

  /**
   * Makes this image a stereo image by appending @a img.
   * @pre isStereo() and img.isStereo() are both false
   * @post isStereo() is true
   */
  void merge(const Image& img)
  {
    if (m_stereo || img.m_stereo || getSize() != img.getSize()) {
      throw ill_informed_image{};
    }
    m_data.insert(m_data.end(), img.m_data.begin(), img.m_data.end());
    m_stereo = true;
  }

  /**
   * Erases the image
   * @post getWidth() == 0
   * @post getHeight() == 0
   * @post isStereo() == false
   */
  void erase() { *this = pymol::Image(); }

  /**
   * Convert a side-by-side image to a stereo image.
   *
   * @verbatim
     stereo=off          stereo=on
     +---- W -----+      +- W/2 -+
     H Left Right |  ->  H Left  |
     +------------+      +-------+
                         H Right |
                         +-------+
     @endverbatim
   * @pre isStereo() is false
   * @pre getWidth() is even
   * @return A new image with isStereo() == true
   */
  Image deinterlace(bool toSwap = false) const
  {
    if (m_stereo || (m_width % 2 == 1)) {
      throw ill_informed_image{};
    }

    auto half_width = m_width / 2;
    Image newImg(half_width, m_height, true);
    auto* src = pixels();
    auto* src_end = src + m_width * m_height;
    auto* dst1 = newImg.pixels();
    auto* dst2 = dst1 + (m_height * half_width);

    if (toSwap) {
      std::swap(dst1, dst2);
    }

    while (src != src_end) {
      dst1 = std::copy(src, src + half_width, dst1);
      src += half_width;
      dst2 = std::copy(src, src + half_width, dst2);
      src += half_width;
    }
    return newImg;
  }

  /**
   * Convert a stereo image to a side-by-side (non-stereo) image.
   *
   * @verbatim
     stereo=on        stereo=off
     +-- W --+        +---- W*2 ----+
     H Left  |   ->   H Left  Right |
     +-------+        +-------------+
     H Right |
     +-------+
     @endverbatim
   * @pre isStereo() is true
   * @return A new image with isStereo() == false
   */
  Image interlace() const
  {
    if (!m_stereo) {
      throw ill_informed_image{};
    }

    Image newImg(m_width * 2, m_height);
    auto* src = pixels();
    auto* dst = newImg.pixels();
    auto* dst_end = dst + (m_width * 2 * m_height);
    auto offset = m_width * m_height;

    for (; dst != dst_end; src += m_width) {
      dst = std::copy_n(src, m_width, dst);
      dst = std::copy_n(src + offset, m_width, dst);
    }
    return newImg;
  }

  bool m_needs_alpha_reset{}; /* needs alpha reset */
};
} // namespace pymol