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

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.4   root       30:        // サイズを指定してキャンバスを作成。
1.1.1.5 ! root       31:        void Create(uint width_, uint height_) {
1.1.1.4   root       32:                Create(width_, height_, NULL);
                     33:        }
                     34: 
1.1.1.5 ! root       35:        uint GetWidth() const { return width; }
        !            36:        uint GetHeight() const { return height; }
        !            37:        uint GetStride() const { return stride; }
1.1       root       38: 
                     39:        uint8 *GetBuf() { return buf; }
                     40:        const uint8 *GetBuf() const { return buf; }
                     41: 
1.1.1.4   root       42:        // y 行目先頭のアドレスを返す
                     43:        uint8 *GetRowPtr(int y) const {
1.1.1.5 ! root       44:                assertmsg(y < height, "y=%d height=%u", y, height);
1.1.1.4   root       45:                return &buf[y * stride];
                     46:        }
                     47: 
                     48:        // (x, y) のアドレスを返す
                     49:        virtual uint8 *GetPtr(int x, int y) const {
1.1.1.5 ! root       50:                assertmsg(x < width, "x=%d width=%u", x, width);
        !            51:                assertmsg(y < height, "y=%d height=%u", y, height);
1.1.1.4   root       52:                return &buf[y * stride + x * depth / 8];
                     53:        }
                     54: 
                     55:        // 全域をコピーしてくる。
                     56:        void CopyFrom(const BitmapBase *src);
                     57: 
1.1       root       58:  protected:
1.1.1.5 ! root       59:        uint width {};          // 幅   [pixel]
        !            60:        uint height {};         // 高さ [pixel]
        !            61:        uint stride {};         // ストライド [byte]
        !            62:        uint depth {};          // 色深度 [bit]
1.1       root       63: 
                     64:        uint8 *buf {};
1.1.1.5 ! root       65:        AlignedArray<uint8, 32> owned_buf {};
1.1       root       66: };
                     67: 
                     68: // 1bpp ビットマップ
                     69: class BitmapI1 : public BitmapBase
                     70: {
                     71:        using inherited = BitmapBase;
                     72:  public:
                     73:        BitmapI1();
1.1.1.5 ! root       74:        BitmapI1(uint width_, uint height_);
        !            75:        BitmapI1(const void *buf_, uint width_, uint height_);
1.1       root       76:        ~BitmapI1() override;
                     77: 
1.1.1.4   root       78:        uint8 *GetPtr(int x, int y) const override;
1.1       root       79: };
                     80: 
                     81: // 8bpp ビットマップ
                     82: class BitmapI8 : public BitmapBase
                     83: {
                     84:        using inherited = BitmapBase;
                     85:  public:
                     86:        BitmapI8();
1.1.1.5 ! root       87:        BitmapI8(uint width_, uint height_);
        !            88:        BitmapI8(const void *buf_, uint width_, uint height_);
1.1       root       89:        ~BitmapI8() override;
1.1.1.2   root       90: };
1.1       root       91: 
                     92: // 24bpp ビットマップ
                     93: class BitmapRGB : public BitmapBase
                     94: {
                     95:        using inherited = BitmapBase;
                     96:  public:
                     97:        BitmapRGB();
1.1.1.5 ! root       98:        BitmapRGB(uint width_, uint height_);
        !            99:        BitmapRGB(const void *buf_, uint width_, uint height_);
1.1       root      100:        ~BitmapRGB() override;
1.1.1.5 ! root      101: };
        !           102: 
        !           103: // 32bpp (RGBX) ビットマップ。
        !           104: // メモリ上 { R, G, B, X } の順。X は不問。
        !           105: class BitmapRGBX : public BitmapBase
        !           106: {
        !           107:        using inherited = BitmapBase;
        !           108:  public:
        !           109:        BitmapRGBX();
        !           110:        BitmapRGBX(uint width_, uint height_);
        !           111:        BitmapRGBX(const void *buf_, uint width_, uint height_);
        !           112:        ~BitmapRGBX() override;
1.1       root      113: 
                    114:        // 色 c で全体を塗りつぶす
1.1.1.4   root      115:        void Fill(Color c) {
                    116:                FillRect(c, Rect(0, 0, width, height));
                    117:        }
1.1.1.5 ! root      118:        void FillRect(Color c, int dx, int dy, uint dw, uint dh) {
1.1.1.4   root      119:                FillRect(c, Rect(dx, dy, dw, dh));
                    120:        }
                    121:        void FillRect(Color c, const Rect& rect);
1.1       root      122: 
1.1.1.5 ! root      123:        // rect の一番上のラスターを rect の残りのラスターにコピーする
        !           124:        void CopyFromTop(const Rect& rect);
        !           125: 
1.1       root      126:        void DrawLine(Color c, int x1, int y1, int x2, int y2);
                    127:        void DrawLineH(Color c, int x1, int y1, int x2);
                    128:        void DrawLineV(Color c, int x1, int y1, int y2);
                    129: 
1.1.1.5 ! root      130:        void DrawRect(Color c, int dx, int dy, uint dw, uint dh) {
        !           131:                DrawRect(c, Rect(dx, dy, dw, dh));
        !           132:        }
1.1       root      133:        void DrawRect(Color c, const Rect& rect);
                    134: 
1.1.1.5 ! root      135:        // I1 画像を描画
        !           136:        void DrawBitmapI1(int dx, int dy, const BitmapI1& src,
1.1       root      137:                const Color *palette);
1.1.1.5 ! root      138:        // I1 画像を scale 倍に拡大して描画
        !           139:        void DrawBitmapI1Scale(int dx, int dy, const BitmapI1& src,
        !           140:                const Color *palette, int scale);
        !           141: 
        !           142:        // I8 画像を描画
        !           143:        void DrawBitmapI8(int dx, int dy, const BitmapI8& src, const Color *palette)
        !           144:        {
        !           145:                Rect sr(0, 0, src.GetWidth(), src.GetHeight());
        !           146:                DrawBitmapI8(dx, dy, src, palette, sr);
        !           147:        }
        !           148:        // I8 画像の指定範囲を描画
        !           149:        void DrawBitmapI8(int dx, int dy, const BitmapI8& src, const Color *palette,
        !           150:                const Rect& sr);
        !           151:        // I8 画像の全域を指定ラスターのみ描画
        !           152:        void DrawBitmapI8Raster(const BitmapI8& src, const Color *palette,
        !           153:                const uint8 *update_raster);
        !           154: 
        !           155:        // I8 画像を分数で拡大縮小して描画
        !           156:        void DrawBitmapI8Scale(const Rect& dr, const BitmapI8& src,
        !           157:                const Color *palette,
1.1.1.2   root      158:                int sxN, int sxD, int sxS, int syN, int syD, int syS);
1.1       root      159: 
1.1.1.5 ! root      160:        // RGBX 画像を描画
        !           161:        void DrawBitmap(int dx, int dy, const BitmapRGBX& src);
        !           162: 
        !           163:        // RGBX 画像が dr のサイズになるよう拡大縮小して描画
        !           164:        void DrawBitmapStretch(const Rect& dr, const BitmapRGBX& src);
        !           165: 
        !           166:        // RGBX 画像を (N-1)/N 倍に縮小して描画
        !           167:        void DrawBitmapNtoNm1(const Rect& dr, const BitmapRGBX& src, uint N);
        !           168: 
        !           169:        // RGBX 画像を N/(N-1) 倍に拡大して描画
        !           170:        void DrawBitmapNm1toN(const Rect& dr, const BitmapRGBX& src, uint N);
        !           171: 
        !           172:        // RGBX 画像が dr のサイズになるよう面積平均法で拡大縮小して描画
        !           173:        void DrawBitmapMean(const Rect& dr, const BitmapRGBX& src);
        !           174: 
        !           175:        // RGB に変換。
        !           176:        void ConvertToRGB(BitmapRGB& dst) const;
1.1       root      177: 
                    178:  private:
1.1.1.5 ! root      179:        // 呼ばれなくて存在しない関数宣言は無視されるので並べて書く。
1.1.1.4   root      180: 
1.1.1.5 ! root      181:        void CopyLineI8_gen(uint32 *d, const uint8 *s, uint width,
        !           182:                const Color *palette);
        !           183:        void CopyLineI8_avx2(uint32 *d, const uint8 *s, uint width,
        !           184:                const Color *palette);
        !           185: 
        !           186:        Color Mean(const RectF& sr) const;
        !           187: 
        !           188:        void ConvertToRGB_gen(BitmapRGB& dst) const;
        !           189:        void ConvertToRGB_avx2(BitmapRGB& dst) const;
        !           190: 
        !           191: #if defined(HAVE_AVX2)
        !           192:        bool enable_avx2 {};
        !           193: #endif
1.1.1.4   root      194: };

unix.superglobalmegacorp.com

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