summaryrefslogtreecommitdiff
path: root/third_party/agg23/agg_pixfmt_gray.h
blob: 5a80935479d41fc517db66d2523f845e08ce2453 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

//----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.3
// 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
//----------------------------------------------------------------------------
//
// Adaptation for high precision colors has been sponsored by
// Liberty Technology Systems, Inc., visit http://lib-sys.com
//
// Liberty Technology Systems, Inc. is the provider of
// PostScript and PDF technology for software developers.
//
//----------------------------------------------------------------------------
#ifndef AGG_PIXFMT_GRAY_INCLUDED
#define AGG_PIXFMT_GRAY_INCLUDED
#include "agg_basics.h"
#include "agg_color_gray.h"
#include "agg_rendering_buffer.h"
namespace agg
{
template<class ColorT> struct blender_gray  {
    typedef ColorT color_type;
    typedef typename color_type::value_type value_type;
    typedef typename color_type::calc_type calc_type;
    enum base_scale_e { base_shift = color_type::base_shift };
    static AGG_INLINE void blend_pix(value_type* p, unsigned cv,
                                     unsigned alpha, unsigned cover = 0)
    {
        *p = (value_type)((((cv - calc_type(*p)) * alpha) + (calc_type(*p) << base_shift)) >> base_shift);
    }
};
template<class Blender, unsigned Step = 1, unsigned Offset = 0>
class pixel_formats_gray 
{
public:
    typedef rendering_buffer::row_data row_data;
    typedef rendering_buffer::span_data span_data;
    typedef typename Blender::color_type color_type;
    typedef typename color_type::value_type value_type;
    typedef typename color_type::calc_type calc_type;
    enum base_scale_e {
        base_shift = color_type::base_shift,
        base_size  = color_type::base_size,
        base_mask  = color_type::base_mask
    };
private:
    static AGG_INLINE void copy_or_blend_pix(value_type* p,
            const color_type& c,
            unsigned cover)
    {
        if (c.a) {
            calc_type alpha = (calc_type(c.a) * (cover + 1)) >> 8;
            if(alpha == base_mask) {
                *p = c.v;
            } else {
                Blender::blend_pix(p, c.v, alpha, cover);
            }
        }
    }
    static AGG_INLINE void copy_or_blend_pix(value_type* p,
            const color_type& c)
    {
        if (c.a) {
            if(c.a == base_mask) {
                *p = c.v;
            } else {
                Blender::blend_pix(p, c.v, c.a);
            }
        }
    }
public:
    pixel_formats_gray(rendering_buffer& rb) :
        m_rbuf(&rb)
    {}
    AGG_INLINE unsigned width()  const
    {
        return m_rbuf->width();
    }
    AGG_INLINE unsigned height() const
    {
        return m_rbuf->height();
    }
    AGG_INLINE color_type pixel(int x, int y) const
    {
        value_type* p = (value_type*)m_rbuf->row(y) + x * Step + Offset;
        return color_type(*p);
    }
    row_data row(int x, int y) const
    {
        return row_data(x,
                        width() - 1,
                        m_rbuf->row(y) +
                        x * Step * sizeof(value_type) +
                        Offset * sizeof(value_type));
    }
    span_data span(int x, int y, unsigned len)
    {
        return span_data(x, len,
                         m_rbuf->row(y) +
                         x * Step * sizeof(value_type) +
                         Offset * sizeof(value_type));
    }
    AGG_INLINE void copy_pixel(int x, int y, const color_type& c)
    {
        *((value_type*)m_rbuf->row(y) + x * Step + Offset) = c.v;
    }
    AGG_INLINE void blend_pixel(int x, int y, const color_type& c, int8u cover)
    {
        copy_or_blend_pix((value_type*)m_rbuf->row(y) + x * Step + Offset, c, cover);
    }
    AGG_INLINE void copy_hline(int x, int y,
                               unsigned len,
                               const color_type& c)
    {
        value_type* p = (value_type*)m_rbuf->row(y) + x * Step + Offset;
        do {
            *p = c.v;
            p += Step;
        } while(--len);
    }
    void blend_hline(int x, int y,
                     unsigned len,
                     const color_type& c,
                     int8u cover)
    {
        if (c.a) {
            value_type* p = (value_type*)m_rbuf->row(y) + x * Step + Offset;
            calc_type alpha = (calc_type(c.a) * (cover + 1)) >> 8;
            if(alpha == base_mask) {
                do {
                    *p = c.v;
                    p += Step;
                } while(--len);
            } else {
                do {
                    Blender::blend_pix(p, c.v, alpha, cover);
                    p += Step;
                } while(--len);
            }
        }
    }
    void blend_solid_hspan(int x, int y,
                           unsigned len,
                           const color_type& c,
                           const int8u* covers)
    {
        if (c.a) {
            value_type* p = (value_type*)m_rbuf->row(y) + x * Step + Offset;
            do {
                calc_type alpha = (calc_type(c.a) * (calc_type(*covers) + 1)) >> 8;
                if(alpha == base_mask) {
                    *p = c.v;
                } else {
                    Blender::blend_pix(p, c.v, alpha, *covers);
                }
                p += Step;
                ++covers;
            } while(--len);
        }
    }
private:
    rendering_buffer* m_rbuf;
};
typedef blender_gray<gray8>      blender_gray8;
typedef pixel_formats_gray<blender_gray8, 1, 0> pixfmt_gray8;
}
#endif