agg_alpha_mask_u8.h

来自「这是VCF框架的代码」· C头文件 代码 · 共 500 行 · 第 1/2 页

H
500
字号
//----------------------------------------------------------------------------// Anti-Grain Geometry - Version 2.4// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)//// Permission to copy, use, modify, sell and distribute this software // is granted provided this copyright notice appears in all copies. // This software is provided "as is" without express or implied// warranty, and with no claim as to its suitability for any purpose.////----------------------------------------------------------------------------// Contact: mcseem@antigrain.com//          mcseemagg@yahoo.com//          http://www.antigrain.com//----------------------------------------------------------------------------//// scanline_u8 class////----------------------------------------------------------------------------#ifndef AGG_ALPHA_MASK_U8_INCLUDED#define AGG_ALPHA_MASK_U8_INCLUDED#include <string.h>#include "agg_basics.h"#include "agg_rendering_buffer.h"namespace agg{    //===================================================one_component_mask_u8    struct one_component_mask_u8    {        static unsigned calculate(const int8u* p) { return *p; }    };        //=====================================================rgb_to_gray_mask_u8    template<unsigned R, unsigned G, unsigned B>    struct rgb_to_gray_mask_u8    {        static unsigned calculate(const int8u* p)         {             return (p[R]*77 + p[G]*150 + p[B]*29) >> 8;         }    };    //==========================================================alpha_mask_u8    template<unsigned Step=1, unsigned Offset=0, class MaskF=one_component_mask_u8>    class alpha_mask_u8    {    public:        typedef int8u cover_type;        typedef alpha_mask_u8<Step, Offset, MaskF> self_type;        enum cover_scale_e        {             cover_shift = 8,            cover_none  = 0,            cover_full  = 255        };        alpha_mask_u8() : m_rbuf(0) {}        alpha_mask_u8(rendering_buffer& rbuf) : m_rbuf(&rbuf) {}        void attach(rendering_buffer& rbuf) { m_rbuf = &rbuf; }        MaskF& mask_function() { return m_mask_function; }        const MaskF& mask_function() const { return m_mask_function; }                //--------------------------------------------------------------------        cover_type pixel(int x, int y) const        {            if(x >= 0 && y >= 0 &&                x < (int)m_rbuf->width() &&                y <= (int)m_rbuf->height())            {                return (cover_type)m_mask_function.calculate(                                        m_rbuf->row_ptr(y) + x * Step + Offset);            }            return 0;        }        //--------------------------------------------------------------------        cover_type combine_pixel(int x, int y, cover_type val) const        {            if(x >= 0 && y >= 0 &&                x < (int)m_rbuf->width() &&                y <= (int)m_rbuf->height())            {                return (cover_type)((cover_full + val *                                      m_mask_function.calculate(                                        m_rbuf->row_ptr(y) + x * Step + Offset)) >>                                      cover_shift);            }            return 0;        }        //--------------------------------------------------------------------        void fill_hspan(int x, int y, cover_type* dst, int num_pix) const        {            int xmax = m_rbuf->width() - 1;            int ymax = m_rbuf->height() - 1;            int count = num_pix;            cover_type* covers = dst;            if(y < 0 || y > ymax)            {                memset(dst, 0, num_pix * sizeof(cover_type));                return;            }            if(x < 0)            {                count += x;                if(count <= 0)                 {                    memset(dst, 0, num_pix * sizeof(cover_type));                    return;                }                memset(covers, 0, -x * sizeof(cover_type));                covers -= x;                x = 0;            }            if(x + count > xmax)            {                int rest = x + count - xmax - 1;                count -= rest;                if(count <= 0)                 {                    memset(dst, 0, num_pix * sizeof(cover_type));                    return;                }                memset(covers + count, 0, rest * sizeof(cover_type));            }            const int8u* mask = m_rbuf->row_ptr(y) + x * Step + Offset;            do            {                *covers++ = (cover_type)m_mask_function.calculate(mask);                mask += Step;            }            while(--count);        }        //--------------------------------------------------------------------        void combine_hspan(int x, int y, cover_type* dst, int num_pix) const        {            int xmax = m_rbuf->width() - 1;            int ymax = m_rbuf->height() - 1;            int count = num_pix;            cover_type* covers = dst;            if(y < 0 || y > ymax)            {                memset(dst, 0, num_pix * sizeof(cover_type));                return;            }            if(x < 0)            {                count += x;                if(count <= 0)                 {                    memset(dst, 0, num_pix * sizeof(cover_type));                    return;                }                memset(covers, 0, -x * sizeof(cover_type));                covers -= x;                x = 0;            }            if(x + count > xmax)            {                int rest = x + count - xmax - 1;                count -= rest;                if(count <= 0)                 {                    memset(dst, 0, num_pix * sizeof(cover_type));                    return;                }                memset(covers + count, 0, rest * sizeof(cover_type));            }            const int8u* mask = m_rbuf->row_ptr(y) + x * Step + Offset;            do            {                *covers = (cover_type)((cover_full + (*covers) *                                        m_mask_function.calculate(mask)) >>                                        cover_shift);                ++covers;                mask += Step;            }            while(--count);        }        //--------------------------------------------------------------------        void fill_vspan(int x, int y, cover_type* dst, int num_pix) const        {            int xmax = m_rbuf->width() - 1;            int ymax = m_rbuf->height() - 1;            int count = num_pix;            cover_type* covers = dst;            if(x < 0 || x > xmax)            {                memset(dst, 0, num_pix * sizeof(cover_type));                return;            }            if(y < 0)            {                count += y;                if(count <= 0)                 {                    memset(dst, 0, num_pix * sizeof(cover_type));                    return;                }                memset(covers, 0, -y * sizeof(cover_type));                covers -= y;                y = 0;            }            if(y + count > ymax)            {                int rest = y + count - ymax - 1;                count -= rest;                if(count <= 0)                 {                    memset(dst, 0, num_pix * sizeof(cover_type));                    return;                }                memset(covers + count, 0, rest * sizeof(cover_type));            }            const int8u* mask = m_rbuf->row_ptr(y) + x * Step + Offset;            do            {                *covers++ = (cover_type)m_mask_function.calculate(mask);                mask += m_rbuf->stride();            }            while(--count);        }        //--------------------------------------------------------------------        void combine_vspan(int x, int y, cover_type* dst, int num_pix) const        {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?