|
2 | 2 | #define MPL_AGG_WORKAROUND_H |
3 | 3 |
|
4 | 4 | #include "agg_pixfmt_rgba.h" |
| 5 | +#include "agg_trans_affine.h" |
5 | 6 |
|
6 | 7 | /********************************************************************** |
7 | 8 | WORKAROUND: This class is to workaround a bug in Agg SVN where the |
@@ -42,4 +43,87 @@ struct fixed_blender_rgba_plain : agg::conv_rgba_plain<ColorT, Order> |
42 | 43 | } |
43 | 44 | }; |
44 | 45 |
|
| 46 | + |
| 47 | +/********************************************************************** |
| 48 | + This class provides higher-accuracy nearest-neighbor interpolation for |
| 49 | + affine transforms than span_interpolator_linear by using |
| 50 | + floating-point-based interpolation instead of integer-based |
| 51 | +*/ |
| 52 | + |
| 53 | +template<class Transformer = agg::trans_affine, unsigned SubpixelShift = 8> |
| 54 | +class accurate_interpolator_affine_nn |
| 55 | +{ |
| 56 | +public: |
| 57 | + typedef Transformer trans_type; |
| 58 | + |
| 59 | + enum subpixel_scale_e |
| 60 | + { |
| 61 | + subpixel_shift = SubpixelShift, |
| 62 | + subpixel_scale = 1 << subpixel_shift |
| 63 | + }; |
| 64 | + |
| 65 | + //-------------------------------------------------------------------- |
| 66 | + accurate_interpolator_affine_nn() {} |
| 67 | + accurate_interpolator_affine_nn(trans_type& trans) : m_trans(&trans) {} |
| 68 | + accurate_interpolator_affine_nn(trans_type& trans, |
| 69 | + double x, double y, unsigned len) : |
| 70 | + m_trans(&trans) |
| 71 | + { |
| 72 | + begin(x, y, len); |
| 73 | + } |
| 74 | + |
| 75 | + //---------------------------------------------------------------- |
| 76 | + const trans_type& transformer() const { return *m_trans; } |
| 77 | + void transformer(trans_type& trans) { m_trans = &trans; } |
| 78 | + |
| 79 | + //---------------------------------------------------------------- |
| 80 | + void begin(double x, double y, unsigned len) |
| 81 | + { |
| 82 | + m_len = len - 1; |
| 83 | + |
| 84 | + m_stx1 = x; |
| 85 | + m_sty1 = y; |
| 86 | + m_trans->transform(&m_stx1, &m_sty1); |
| 87 | + m_stx1 *= subpixel_scale; |
| 88 | + m_sty1 *= subpixel_scale; |
| 89 | + |
| 90 | + m_stx2 = x + m_len; |
| 91 | + m_sty2 = y; |
| 92 | + m_trans->transform(&m_stx2, &m_sty2); |
| 93 | + m_stx2 *= subpixel_scale; |
| 94 | + m_sty2 *= subpixel_scale; |
| 95 | + } |
| 96 | + |
| 97 | + //---------------------------------------------------------------- |
| 98 | + void resynchronize(double xe, double ye, unsigned len) |
| 99 | + { |
| 100 | + m_len = len - 1; |
| 101 | + |
| 102 | + m_trans->transform(&xe, &ye); |
| 103 | + m_stx2 = xe * subpixel_scale; |
| 104 | + m_sty2 = ye * subpixel_scale; |
| 105 | + } |
| 106 | + |
| 107 | + //---------------------------------------------------------------- |
| 108 | + void operator++() |
| 109 | + { |
| 110 | + m_stx1 += (m_stx2 - m_stx1) / m_len; |
| 111 | + m_sty1 += (m_sty2 - m_sty1) / m_len; |
| 112 | + m_len--; |
| 113 | + } |
| 114 | + |
| 115 | + //---------------------------------------------------------------- |
| 116 | + void coordinates(int* x, int* y) const |
| 117 | + { |
| 118 | + // Truncate instead of round because this interpolator needs to |
| 119 | + // match the definitions for nearest-neighbor interpolation |
| 120 | + *x = int(m_stx1); |
| 121 | + *y = int(m_sty1); |
| 122 | + } |
| 123 | + |
| 124 | +private: |
| 125 | + trans_type* m_trans; |
| 126 | + unsigned m_len; |
| 127 | + double m_stx1, m_sty1, m_stx2, m_sty2; |
| 128 | +}; |
45 | 129 | #endif |
0 commit comments