LIBEUNOMIA
読み取り中…
検索中…
一致する文字列を見つけられません
imagebuffer.h
[詳解]
1/*
2 * Copyright 2002-2021 oZ/acy (名賀月晃嗣)
3 * Redistribution and use in source and binary forms,
4 * with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 */
40#ifndef INCLUDE_GUARD_EUNOMIA_IMAGEBUFFER_H
41#define INCLUDE_GUARD_EUNOMIA_IMAGEBUFFER_H
42
43#include <algorithm>
44#include <optional>
45#include <cstdint>
46#include "exception.h"
47#include "noncopyable.h"
48#include "rect.h"
49
50
51namespace eunomia
52{
53
57struct Point
58{
59 int x;
60 int y;
61
62 Point() = default;
63 constexpr Point(int xx, int yy) noexcept : x(xx), y(yy) {}
64};
65
66
71{
72public:
73 RangeOverException() : Exception("eunomia::RangeOverException") {}
74};
75
76
77
78
82template<class C_>
83class ImageBuffer : Noncopyable<ImageBuffer<C_>>
84{
85public:
86 typedef C_ ColourType;
87
88protected:
89 std::uint8_t* buf_;
90 int w_;
91 int h_;
92 int pitch_;
93
101 ImageBuffer(int w, int h, int p) noexcept
102 : buf_(nullptr), w_(w), h_(h), pitch_(p)
103 {}
104
105public:
107 virtual ~ImageBuffer() = default;
108
109 //====================================
110 // 各種情報取得
111 //====================================
112
114 int width() const noexcept { return w_; }
116 int height() const noexcept { return h_; }
118 int pitch() const noexcept { return pitch_; }
119
123 const uint8_t* buffer() const noexcept { return buf_; }
124
126 C_* lineBuffer(int y) noexcept
127 { return reinterpret_cast<C_*>(buf_ + pitch_ * y); }
128
130 const C_* lineBuffer(int y) const noexcept
131 { return reinterpret_cast<const C_*>(buf_ + pitch_ * y); }
132
133 //====================================
134 // 畫素の參照
135 //====================================
136
140 C_& pixel(int x, int y) noexcept { return lineBuffer(y)[x]; }
141
145 const C_& pixel(int x, int y) const noexcept { return lineBuffer(y)[x]; }
146
152 C_& at(int x, int y)
153 {
154 if (x < 0 || y < 0 || x >= w_ || y >= h_)
155 throw RangeOverException();
156 return pixel(x, y);
157 }
158
164 const C_& at(int x, int y) const
165 {
166 if (x < 0 || y < 0 || x >= w_ || y >= h_)
167 throw RangeOverException();
168 return pixel(x, y);
169 }
170
171
172 //==================================================================
173 // 圖形描畫
174 //==================================================================
175
182 void line(int x1, int y1, int x2, int y2, const C_& color);
183
192 void box(
193 int left, int top, int right, int bottom, const C_& color,
194 bool fill = false);
195
203 void ellipse(
204 int x, int y, int a, int b, const C_& color, bool fill = false);
205
212 void circle(int x, int y, int r, const C_& color, bool fill = false)
213 {
214 ellipse(x, y, r, r, color, fill);
215 }
216
221 void paintFill(int x, int y, const C_& color);
222
225 void clear(const C_& color);
226
227
228 //==================================================================
229 // 轉送
230 //==================================================================
231
253 template<class CSrc, class Copier>
254 void
256 const ImageBuffer<CSrc>& src, int sx, int sy, int w, int h,
257 int dx, int dy, const std::optional<Rect>& cliprect, Copier copier);
258
272 template<class CSrc>
273 void
275 const ImageBuffer<CSrc>& src, int sx, int sy, int w, int h,
276 int dx, int dy, const std::optional<Rect>& cliprect = std::nullopt)
277 {
278 blt(
279 src, sx, sy, w, h, dx, dy, cliprect,
280 [](const CSrc& spixel, C_& dpixel){ dpixel = spixel; });
281 }
282
283
284 //==============================================
285 // その他
286 //==============================================
287
295 template<class Func>
297 {
298 for (int j = 0; j < h_; ++j)
299 std::for_each_n(lineBuffer(j), w_, func);
300 }
301};
302
303
310template<class C_>
311void grayscale(C_& pixel)
312{
313 std::uint8_t l
314 = pixel.red * 0.2990 + pixel.green * 0.5870 + pixel.blue * 0.1140;
315
316 pixel.red = l;
317 pixel.green = l;
318 pixel.blue = l;
319}
320
321
322}//end of namespace eunomia
323
324
325#include "ibuf_blt.h"
326#include "ibuf_draw.h"
327
328
329#endif // INCLUDE_GUARD_EUNOMIA_IMAGEBUFFER_H
例外
Definition exception.h:50
畫像バッファ基底クラステンプレート
Definition imagebuffer.h:84
void paintFill(int x, int y, const C_ &color)
塗り潰し
Definition ibuf_draw.h:375
void forEachPixel(Func func)
畫素毎の處理
Definition imagebuffer.h:296
void clear(const C_ &color)
バッファ全體の塗り潰し
Definition ibuf_draw.h:45
int width() const noexcept
Definition imagebuffer.h:114
ImageBuffer(int w, int h, int p) noexcept
構築子
Definition imagebuffer.h:101
int pitch() const noexcept
ピッチ
Definition imagebuffer.h:118
void blt(const ImageBuffer< CSrc > &src, int sx, int sy, int w, int h, int dx, int dy, const std::optional< Rect > &cliprect, Copier copier)
轉送
std::uint8_t * buf_
畫像バッファ
Definition imagebuffer.h:89
void circle(int x, int y, int r, const C_ &color, bool fill=false)
圓の描畫
Definition imagebuffer.h:212
const C_ & at(int x, int y) const
畫素(x, y)の參照
Definition imagebuffer.h:164
C_ ColourType
Definition imagebuffer.h:86
void line(int x1, int y1, int x2, int y2, const C_ &color)
線分の描畫
Definition ibuf_draw.h:60
C_ & pixel(int x, int y) noexcept
畫素(x, y)の參照
Definition imagebuffer.h:140
const C_ * lineBuffer(int y) const noexcept
ラインバッファの先頭アドレスの取得
Definition imagebuffer.h:130
C_ * lineBuffer(int y) noexcept
ラインバッファの先頭アドレスの取得
Definition imagebuffer.h:126
uint8_t * buffer() noexcept
バッファの先頭アドレスの取得
Definition imagebuffer.h:121
void ellipse(int x, int y, int a, int b, const C_ &color, bool fill=false)
楕圓の描畫
Definition ibuf_draw.h:258
int w_
Definition imagebuffer.h:90
C_ & at(int x, int y)
畫素(x, y)の參照
Definition imagebuffer.h:152
int h_
高さ
Definition imagebuffer.h:91
void blt(const ImageBuffer< CSrc > &src, int sx, int sy, int w, int h, int dx, int dy, const std::optional< Rect > &cliprect=std::nullopt)
轉送
Definition imagebuffer.h:274
virtual ~ImageBuffer()=default
解體子
int pitch_
ピッチ = 水平方向1ラインのビット數
Definition imagebuffer.h:92
const C_ & pixel(int x, int y) const noexcept
畫素(x, y)の參照
Definition imagebuffer.h:145
int height() const noexcept
高さ
Definition imagebuffer.h:116
const uint8_t * buffer() const noexcept
バッファの先頭アドレスの取得
Definition imagebuffer.h:123
void box(int left, int top, int right, int bottom, const C_ &color, bool fill=false)
長方形の描畫
Definition ibuf_draw.h:200
コピー禁止クラステンプレート
Definition noncopyable.h:60
範圍逸脱例外
Definition imagebuffer.h:71
RangeOverException()
Definition imagebuffer.h:73
例外クラス
Definition colour.h:49
void grayscale(C_ &pixel)
グレイスケール化
Definition imagebuffer.h:311
コピー禁止クラステンプレート
長方形表現型
Definition imagebuffer.h:58
int y
Y座標
Definition imagebuffer.h:60
constexpr Point(int xx, int yy) noexcept
Definition imagebuffer.h:63
Point()=default
int x
X座標
Definition imagebuffer.h:59