|
|
1.1 root 1: //
2: // nono
3: // Copyright (C) 2021 nono project
4: // Licensed under nono-license.txt
5: //
6:
7: //
8: // ビットマップ
9: //
10:
11: #pragma once
12:
13: #include "header.h"
1.1.1.5 root 14: #include "alignedarray.h"
1.1 root 15: #include "color.h"
16: #include "rect.h"
17:
18: // ビットマップ (基本クラス)
19: class BitmapBase
20: {
21: protected:
1.1.1.5 root 22: BitmapBase(uint depth_, uint width_, uint height_, const void *buf_);
1.1 root 23:
1.1.1.4 root 24: // サイズを指定してキャンバスを作成。
25: // buf_ は NULL ならこっちでメモリを確保。
1.1.1.5 root 26: void Create(uint width_, uint height_, const void *buf_);
1.1 root 27: public:
28: virtual ~BitmapBase();
29:
1.1.1.6 root 30: // コピーコンストラクタ
31: BitmapBase(const BitmapBase& src);
32: // コピー代入演算子
33: BitmapBase& operator=(const BitmapBase& src);
34:
1.1.1.4 root 35: // サイズを指定してキャンバスを作成。
1.1.1.5 root 36: void Create(uint width_, uint height_) {
1.1.1.4 root 37: Create(width_, height_, NULL);
38: }
39:
1.1.1.5 root 40: uint GetWidth() const { return width; }
41: uint GetHeight() const { return height; }
42: uint GetStride() const { return stride; }
1.1 root 43:
44: uint8 *GetBuf() { return buf; }
45: const uint8 *GetBuf() const { return buf; }
46:
1.1.1.4 root 47: // y 行目先頭のアドレスを返す
48: uint8 *GetRowPtr(int y) const {
1.1.1.5 root 49: assertmsg(y < height, "y=%d height=%u", y, height);
1.1.1.4 root 50: return &buf[y * stride];
51: }
52:
53: // (x, y) のアドレスを返す
54: virtual uint8 *GetPtr(int x, int y) const {
1.1.1.5 root 55: assertmsg(x < width, "x=%d width=%u", x, width);
56: assertmsg(y < height, "y=%d height=%u", y, height);
1.1.1.4 root 57: return &buf[y * stride + x * depth / 8];
58: }
59:
60: // 全域をコピーしてくる。
61: void CopyFrom(const BitmapBase *src);
62:
1.1 root 63: protected:
1.1.1.5 root 64: uint width {}; // 幅 [pixel]
65: uint height {}; // 高さ [pixel]
66: uint stride {}; // ストライド [byte]
67: uint depth {}; // 色深度 [bit]
1.1 root 68:
69: uint8 *buf {};
1.1.1.5 root 70: AlignedArray<uint8, 32> owned_buf {};
1.1 root 71: };
72:
73: // 1bpp ビットマップ
74: class BitmapI1 : public BitmapBase
75: {
76: using inherited = BitmapBase;
77: public:
78: BitmapI1();
1.1.1.5 root 79: BitmapI1(uint width_, uint height_);
80: BitmapI1(const void *buf_, uint width_, uint height_);
1.1 root 81: ~BitmapI1() override;
82:
1.1.1.4 root 83: uint8 *GetPtr(int x, int y) const override;
1.1.1.6 root 84:
85: // このビットマップ(フォント)をボールドにした新しいビットマップを返す。
86: BitmapI1 ConvertToBold() const;
87: // このビットマップ(フォント)をイタリックにした新しいビットマップを返す。
88: BitmapI1 ConvertToItalic() const;
1.1 root 89: };
90:
91: // 8bpp ビットマップ
92: class BitmapI8 : public BitmapBase
93: {
94: using inherited = BitmapBase;
95: public:
96: BitmapI8();
1.1.1.5 root 97: BitmapI8(uint width_, uint height_);
98: BitmapI8(const void *buf_, uint width_, uint height_);
1.1 root 99: ~BitmapI8() override;
1.1.1.2 root 100: };
1.1 root 101:
102: // 24bpp ビットマップ
103: class BitmapRGB : public BitmapBase
104: {
105: using inherited = BitmapBase;
106: public:
107: BitmapRGB();
1.1.1.5 root 108: BitmapRGB(uint width_, uint height_);
109: BitmapRGB(const void *buf_, uint width_, uint height_);
1.1 root 110: ~BitmapRGB() override;
1.1.1.5 root 111: };
112:
113: // 32bpp (RGBX) ビットマップ。
114: // メモリ上 { R, G, B, X } の順。X は不問。
115: class BitmapRGBX : public BitmapBase
116: {
117: using inherited = BitmapBase;
118: public:
119: BitmapRGBX();
120: BitmapRGBX(uint width_, uint height_);
121: BitmapRGBX(const void *buf_, uint width_, uint height_);
122: ~BitmapRGBX() override;
1.1 root 123:
124: // 色 c で全体を塗りつぶす
1.1.1.4 root 125: void Fill(Color c) {
126: FillRect(c, Rect(0, 0, width, height));
127: }
1.1.1.5 root 128: void FillRect(Color c, int dx, int dy, uint dw, uint dh) {
1.1.1.4 root 129: FillRect(c, Rect(dx, dy, dw, dh));
130: }
131: void FillRect(Color c, const Rect& rect);
1.1 root 132:
1.1.1.5 root 133: // rect の一番上のラスターを rect の残りのラスターにコピーする
134: void CopyFromTop(const Rect& rect);
135:
1.1.1.7 ! root 136: void DrawPoint(Color c, int x, int y);
! 137:
1.1 root 138: void DrawLine(Color c, int x1, int y1, int x2, int y2);
139: void DrawLineH(Color c, int x1, int y1, int x2);
140: void DrawLineV(Color c, int x1, int y1, int y2);
141:
1.1.1.5 root 142: void DrawRect(Color c, int dx, int dy, uint dw, uint dh) {
143: DrawRect(c, Rect(dx, dy, dw, dh));
144: }
1.1 root 145: void DrawRect(Color c, const Rect& rect);
146:
1.1.1.7 ! root 147: void DrawFillCircle(Color b, Color f, int dx, int dy, uint dw, uint dh) {
! 148: DrawFillCircle(b, f, Rect(dx, dy, dw, dh));
! 149: }
! 150: void DrawFillCircle(Color border, Color fill, const Rect& rect);
! 151:
1.1.1.5 root 152: // I1 画像を描画
153: void DrawBitmapI1(int dx, int dy, const BitmapI1& src,
1.1 root 154: const Color *palette);
1.1.1.5 root 155: // I1 画像を scale 倍に拡大して描画
156: void DrawBitmapI1Scale(int dx, int dy, const BitmapI1& src,
157: const Color *palette, int scale);
158:
159: // I8 画像を描画
160: void DrawBitmapI8(int dx, int dy, const BitmapI8& src, const Color *palette)
161: {
162: Rect sr(0, 0, src.GetWidth(), src.GetHeight());
163: DrawBitmapI8(dx, dy, src, palette, sr);
164: }
165: // I8 画像の指定範囲を描画
166: void DrawBitmapI8(int dx, int dy, const BitmapI8& src, const Color *palette,
167: const Rect& sr);
168: // I8 画像の全域を指定ラスターのみ描画
169: void DrawBitmapI8Raster(const BitmapI8& src, const Color *palette,
170: const uint8 *update_raster);
171:
172: // I8 画像を分数で拡大縮小して描画
173: void DrawBitmapI8Scale(const Rect& dr, const BitmapI8& src,
174: const Color *palette,
1.1.1.2 root 175: int sxN, int sxD, int sxS, int syN, int syD, int syS);
1.1 root 176:
1.1.1.5 root 177: // RGBX 画像を描画
1.1.1.7 ! root 178: void DrawBitmap(int dx, int dy, const BitmapRGBX& src, const Rect& sr);
! 179: void DrawBitmap(int dx, int dy, const BitmapRGBX& src) {
! 180: DrawBitmap(dx, dy, src, Rect(0, 0, src.GetWidth(), src.GetHeight()));
! 181: }
1.1.1.5 root 182:
183: // RGBX 画像が dr のサイズになるよう拡大縮小して描画
184: void DrawBitmapStretch(const Rect& dr, const BitmapRGBX& src);
185:
186: // RGBX 画像を (N-1)/N 倍に縮小して描画
187: void DrawBitmapNtoNm1(const Rect& dr, const BitmapRGBX& src, uint N);
188:
189: // RGBX 画像を N/(N-1) 倍に拡大して描画
190: void DrawBitmapNm1toN(const Rect& dr, const BitmapRGBX& src, uint N);
191:
192: // RGBX 画像が dr のサイズになるよう面積平均法で拡大縮小して描画
193: void DrawBitmapMean(const Rect& dr, const BitmapRGBX& src);
194:
195: // RGB に変換。
196: void ConvertToRGB(BitmapRGB& dst) const;
1.1 root 197:
198: private:
1.1.1.5 root 199: // 呼ばれなくて存在しない関数宣言は無視されるので並べて書く。
1.1.1.4 root 200:
1.1.1.5 root 201: void CopyLineI8_gen(uint32 *d, const uint8 *s, uint width,
202: const Color *palette);
203: void CopyLineI8_avx2(uint32 *d, const uint8 *s, uint width,
204: const Color *palette);
205:
206: Color Mean(const RectF& sr) const;
207:
208: void ConvertToRGB_gen(BitmapRGB& dst) const;
209: void ConvertToRGB_avx2(BitmapRGB& dst) const;
210:
211: #if defined(HAVE_AVX2)
212: bool enable_avx2 {};
213: #endif
1.1.1.4 root 214: };
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.