1 2 //---------------------------------------------------------------------------- 3 // Anti-Grain Geometry - Version 2.3 4 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com) 5 // 6 // Permission to copy, use, modify, sell and distribute this software 7 // is granted provided this copyright notice appears in all copies. 8 // This software is provided "as is" without express or implied 9 // warranty, and with no claim as to its suitability for any purpose. 10 // 11 //---------------------------------------------------------------------------- 12 // Contact: [email protected] 13 // [email protected] 14 // http://www.antigrain.com 15 //---------------------------------------------------------------------------- 16 // 17 // Adaptation for high precision colors has been sponsored by 18 // Liberty Technology Systems, Inc., visit http://lib-sys.com 19 // 20 // Liberty Technology Systems, Inc. is the provider of 21 // PostScript and PDF technology for software developers. 22 // 23 //---------------------------------------------------------------------------- 24 #ifndef AGG_PIXFMT_GRAY_INCLUDED 25 #define AGG_PIXFMT_GRAY_INCLUDED 26 #include "agg_basics.h" 27 #include "agg_color_gray.h" 28 #include "agg_rendering_buffer.h" 29 namespace pdfium 30 { 31 namespace agg 32 { 33 template<class ColorT> struct blender_gray { 34 typedef ColorT color_type; 35 typedef typename color_type::value_type value_type; 36 typedef typename color_type::calc_type calc_type; 37 enum base_scale_e { base_shift = color_type::base_shift }; 38 static AGG_INLINE void blend_pix(value_type* p, unsigned cv, 39 unsigned alpha, unsigned cover = 0) 40 { 41 *p = (value_type)((((cv - calc_type(*p)) * alpha) + (calc_type(*p) << base_shift)) >> base_shift); 42 } 43 }; 44 template<class Blender, unsigned Step = 1, unsigned Offset = 0> 45 class pixel_formats_gray 46 { 47 public: 48 typedef rendering_buffer::row_data row_data; 49 typedef rendering_buffer::span_data span_data; 50 typedef typename Blender::color_type color_type; 51 typedef typename color_type::value_type value_type; 52 typedef typename color_type::calc_type calc_type; 53 enum base_scale_e { 54 base_shift = color_type::base_shift, 55 base_size = color_type::base_size, 56 base_mask = color_type::base_mask 57 }; 58 private: copy_or_blend_pix(value_type * p,const color_type & c,unsigned cover)59 static AGG_INLINE void copy_or_blend_pix(value_type* p, 60 const color_type& c, 61 unsigned cover) 62 { 63 if (c.a) { 64 calc_type alpha = (calc_type(c.a) * (cover + 1)) >> 8; 65 if(alpha == base_mask) { 66 *p = c.v; 67 } else { 68 Blender::blend_pix(p, c.v, alpha, cover); 69 } 70 } 71 } copy_or_blend_pix(value_type * p,const color_type & c)72 static AGG_INLINE void copy_or_blend_pix(value_type* p, 73 const color_type& c) 74 { 75 if (c.a) { 76 if(c.a == base_mask) { 77 *p = c.v; 78 } else { 79 Blender::blend_pix(p, c.v, c.a); 80 } 81 } 82 } 83 public: pixel_formats_gray(rendering_buffer & rb)84 pixel_formats_gray(rendering_buffer& rb) : 85 m_rbuf(&rb) 86 {} width()87 AGG_INLINE unsigned width() const 88 { 89 return m_rbuf->width(); 90 } height()91 AGG_INLINE unsigned height() const 92 { 93 return m_rbuf->height(); 94 } pixel(int x,int y)95 AGG_INLINE color_type pixel(int x, int y) const 96 { 97 value_type* p = (value_type*)m_rbuf->row(y) + x * Step + Offset; 98 return color_type(*p); 99 } row(int x,int y)100 row_data row(int x, int y) const 101 { 102 return row_data(x, 103 width() - 1, 104 m_rbuf->row(y) + 105 x * Step * sizeof(value_type) + 106 Offset * sizeof(value_type)); 107 } span(int x,int y,unsigned len)108 span_data span(int x, int y, unsigned len) 109 { 110 return span_data(x, len, 111 m_rbuf->row(y) + 112 x * Step * sizeof(value_type) + 113 Offset * sizeof(value_type)); 114 } copy_pixel(int x,int y,const color_type & c)115 AGG_INLINE void copy_pixel(int x, int y, const color_type& c) 116 { 117 *((value_type*)m_rbuf->row(y) + x * Step + Offset) = c.v; 118 } blend_pixel(int x,int y,const color_type & c,int8u cover)119 AGG_INLINE void blend_pixel(int x, int y, const color_type& c, int8u cover) 120 { 121 copy_or_blend_pix((value_type*)m_rbuf->row(y) + x * Step + Offset, c, cover); 122 } copy_hline(int x,int y,unsigned len,const color_type & c)123 AGG_INLINE void copy_hline(int x, int y, 124 unsigned len, 125 const color_type& c) 126 { 127 value_type* p = (value_type*)m_rbuf->row(y) + x * Step + Offset; 128 do { 129 *p = c.v; 130 p += Step; 131 } while(--len); 132 } blend_hline(int x,int y,unsigned len,const color_type & c,int8u cover)133 void blend_hline(int x, int y, 134 unsigned len, 135 const color_type& c, 136 int8u cover) 137 { 138 if (c.a) { 139 value_type* p = (value_type*)m_rbuf->row(y) + x * Step + Offset; 140 calc_type alpha = (calc_type(c.a) * (cover + 1)) >> 8; 141 if(alpha == base_mask) { 142 do { 143 *p = c.v; 144 p += Step; 145 } while(--len); 146 } else { 147 do { 148 Blender::blend_pix(p, c.v, alpha, cover); 149 p += Step; 150 } while(--len); 151 } 152 } 153 } blend_solid_hspan(int x,int y,unsigned len,const color_type & c,const int8u * covers)154 void blend_solid_hspan(int x, int y, 155 unsigned len, 156 const color_type& c, 157 const int8u* covers) 158 { 159 if (c.a) { 160 value_type* p = (value_type*)m_rbuf->row(y) + x * Step + Offset; 161 do { 162 calc_type alpha = (calc_type(c.a) * (calc_type(*covers) + 1)) >> 8; 163 if(alpha == base_mask) { 164 *p = c.v; 165 } else { 166 Blender::blend_pix(p, c.v, alpha, *covers); 167 } 168 p += Step; 169 ++covers; 170 } while(--len); 171 } 172 } 173 private: 174 rendering_buffer* m_rbuf; 175 }; 176 typedef blender_gray<gray8> blender_gray8; 177 typedef pixel_formats_gray<blender_gray8, 1, 0> pixfmt_gray8; 178 } 179 } // namespace pdfium 180 #endif 181