Annotation of nono/lib/bitmap.h, revision 1.1.1.6

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       root      136:        void DrawLine(Color c, int x1, int y1, int x2, int y2);
                    137:        void DrawLineH(Color c, int x1, int y1, int x2);
                    138:        void DrawLineV(Color c, int x1, int y1, int y2);
                    139: 
1.1.1.5   root      140:        void DrawRect(Color c, int dx, int dy, uint dw, uint dh) {
                    141:                DrawRect(c, Rect(dx, dy, dw, dh));
                    142:        }
1.1       root      143:        void DrawRect(Color c, const Rect& rect);
                    144: 
1.1.1.5   root      145:        // I1 画像を描画
                    146:        void DrawBitmapI1(int dx, int dy, const BitmapI1& src,
1.1       root      147:                const Color *palette);
1.1.1.5   root      148:        // I1 画像を scale 倍に拡大して描画
                    149:        void DrawBitmapI1Scale(int dx, int dy, const BitmapI1& src,
                    150:                const Color *palette, int scale);
                    151: 
                    152:        // I8 画像を描画
                    153:        void DrawBitmapI8(int dx, int dy, const BitmapI8& src, const Color *palette)
                    154:        {
                    155:                Rect sr(0, 0, src.GetWidth(), src.GetHeight());
                    156:                DrawBitmapI8(dx, dy, src, palette, sr);
                    157:        }
                    158:        // I8 画像の指定範囲を描画
                    159:        void DrawBitmapI8(int dx, int dy, const BitmapI8& src, const Color *palette,
                    160:                const Rect& sr);
                    161:        // I8 画像の全域を指定ラスターのみ描画
                    162:        void DrawBitmapI8Raster(const BitmapI8& src, const Color *palette,
                    163:                const uint8 *update_raster);
                    164: 
                    165:        // I8 画像を分数で拡大縮小して描画
                    166:        void DrawBitmapI8Scale(const Rect& dr, const BitmapI8& src,
                    167:                const Color *palette,
1.1.1.2   root      168:                int sxN, int sxD, int sxS, int syN, int syD, int syS);
1.1       root      169: 
1.1.1.5   root      170:        // RGBX 画像を描画
                    171:        void DrawBitmap(int dx, int dy, const BitmapRGBX& src);
                    172: 
                    173:        // RGBX 画像が dr のサイズになるよう拡大縮小して描画
                    174:        void DrawBitmapStretch(const Rect& dr, const BitmapRGBX& src);
                    175: 
                    176:        // RGBX 画像を (N-1)/N 倍に縮小して描画
                    177:        void DrawBitmapNtoNm1(const Rect& dr, const BitmapRGBX& src, uint N);
                    178: 
                    179:        // RGBX 画像を N/(N-1) 倍に拡大して描画
                    180:        void DrawBitmapNm1toN(const Rect& dr, const BitmapRGBX& src, uint N);
                    181: 
                    182:        // RGBX 画像が dr のサイズになるよう面積平均法で拡大縮小して描画
                    183:        void DrawBitmapMean(const Rect& dr, const BitmapRGBX& src);
                    184: 
                    185:        // RGB に変換。
                    186:        void ConvertToRGB(BitmapRGB& dst) const;
1.1       root      187: 
                    188:  private:
1.1.1.5   root      189:        // 呼ばれなくて存在しない関数宣言は無視されるので並べて書く。
1.1.1.4   root      190: 
1.1.1.5   root      191:        void CopyLineI8_gen(uint32 *d, const uint8 *s, uint width,
                    192:                const Color *palette);
                    193:        void CopyLineI8_avx2(uint32 *d, const uint8 *s, uint width,
                    194:                const Color *palette);
                    195: 
                    196:        Color Mean(const RectF& sr) const;
                    197: 
                    198:        void ConvertToRGB_gen(BitmapRGB& dst) const;
                    199:        void ConvertToRGB_avx2(BitmapRGB& dst) const;
                    200: 
                    201: #if defined(HAVE_AVX2)
                    202:        bool enable_avx2 {};
                    203: #endif
1.1.1.4   root      204: };

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.