| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 |
| 19 | #ifndef VRLE_H |
| 20 | #define VRLE_H |
| 21 |
| 22 | #include <vector> |
| 23 | #include "vcowptr.h" |
| 24 | #include "vglobal.h" |
| 25 | #include "vpoint.h" |
| 26 | #include "vrect.h" |
| 27 |
| 28 | V_BEGIN_NAMESPACE |
| 29 |
| 30 | class VRle { |
| 31 | public: |
| 32 | struct Span { |
| 33 | short x; |
| 34 | short y; |
| 35 | ushort len; |
| 36 | uchar coverage; |
| 37 | }; |
| 38 | typedef void (*VRleSpanCb)(size_t count, const VRle::Span *spans, |
| 39 | void *userData); |
| 40 | bool empty() const; |
| 41 | VRect boundingRect() const; |
| 42 | void setBoundingRect(const VRect &bbox); |
| 43 | void addSpan(const VRle::Span *span, size_t count); |
| 44 |
| 45 | void reset(); |
| 46 | void translate(const VPoint &p); |
| 47 | void invert(); |
| 48 |
| 49 | void operator*=(int alpha); |
| 50 |
| 51 | void intersect(const VRect &r, VRleSpanCb cb, void *userData) const; |
| 52 | void intersect(const VRle &rle, VRleSpanCb cb, void *userData) const; |
| 53 |
| 54 | VRle operator&(const VRle &o) const; |
| 55 | VRle operator-(const VRle &o) const; |
| 56 | VRle operator+(const VRle &o) const; |
| 57 | VRle operator^(const VRle &o) const; |
| 58 |
| 59 | static VRle toRle(const VRect &rect); |
| 60 |
| 61 | bool unique() const {return d.unique();} |
| 62 | int refCount() const { return d.refCount();} |
| 63 | void clone(const VRle &o); |
| 64 |
| 65 | private: |
| 66 | struct VRleData { |
| 67 | enum class OpCode { |
| 68 | Add, |
| 69 | Xor |
| 70 | }; |
| 71 | bool empty() const { return mSpans.empty(); } |
| 72 | void addSpan(const VRle::Span *span, size_t count); |
| 73 | void updateBbox() const; |
| 74 | VRect bbox() const; |
| 75 | void setBbox(const VRect &bbox) const; |
| 76 | void reset(); |
| 77 | void translate(const VPoint &p); |
| 78 | void operator*=(int alpha); |
| 79 | void invert(); |
| 80 | void opIntersect(const VRect &, VRle::VRleSpanCb, void *) const; |
| 81 | void opGeneric(const VRle::VRleData &, const VRle::VRleData &, OpCode code); |
| 82 | void opSubstract(const VRle::VRleData &, const VRle::VRleData &); |
| 83 | void opIntersect(const VRle::VRleData &, const VRle::VRleData &); |
| 84 | void addRect(const VRect &rect); |
| 85 | void clone(const VRle::VRleData &); |
| 86 | std::vector<VRle::Span> mSpans; |
| 87 | VPoint mOffset; |
| 88 | mutable VRect mBbox; |
| 89 | mutable bool mBboxDirty = true; |
| 90 | }; |
| 91 | friend void opIntersectHelper(const VRle::VRleData &obj1, |
| 92 | const VRle::VRleData &obj2, |
| 93 | VRle::VRleSpanCb cb, void *userData); |
| 94 | vcow_ptr<VRleData> d; |
| 95 | }; |
| 96 |
| 97 | inline bool VRle::empty() const |
| 98 | { |
| 99 | return d->empty(); |
| 100 | } |
| 101 |
| 102 | inline void VRle::addSpan(const VRle::Span *span, size_t count) |
| 103 | { |
| 104 | d.write().addSpan(span, count); |
| 105 | } |
| 106 |
| 107 | inline VRect VRle::boundingRect() const |
| 108 | { |
| 109 | return d->bbox(); |
| 110 | } |
| 111 |
| 112 | inline void VRle::setBoundingRect(const VRect & bbox) |
| 113 | { |
| 114 | d->setBbox(bbox); |
| 115 | } |
| 116 |
| 117 | inline void VRle::invert() |
| 118 | { |
| 119 | d.write().invert(); |
| 120 | } |
| 121 |
| 122 | inline void VRle::operator*=(int alpha) |
| 123 | { |
| 124 | d.write() *= alpha; |
| 125 | } |
| 126 |
| 127 | inline VRle VRle::operator&(const VRle &o) const |
| 128 | { |
| 129 | if (empty() || o.empty()) return VRle(); |
| 130 |
| 131 | VRle result; |
| 132 | result.d.write().opIntersect(d.read(), o.d.read()); |
| 133 |
| 134 | return result; |
| 135 | } |
| 136 |
| 137 | inline VRle VRle::operator+(const VRle &o) const |
| 138 | { |
| 139 | if (empty()) return o; |
| 140 | if (o.empty()) return *this; |
| 141 |
| 142 | VRle result; |
| 143 | result.d.write().opGeneric(d.read(), o.d.read(), VRleData::OpCode::Add); |
| 144 |
| 145 | return result; |
| 146 | } |
| 147 |
| 148 | inline VRle VRle::operator^(const VRle &o) const |
| 149 | { |
| 150 | if (empty()) return o; |
| 151 | if (o.empty()) return *this; |
| 152 |
| 153 | VRle result; |
| 154 | result.d.write().opGeneric(d.read(), o.d.read(), VRleData::OpCode::Xor); |
| 155 |
| 156 | return result; |
| 157 | } |
| 158 |
| 159 | inline VRle VRle::operator-(const VRle &o) const |
| 160 | { |
| 161 | if (empty()) return VRle(); |
| 162 | if (o.empty()) return *this; |
| 163 |
| 164 | VRle result; |
| 165 | result.d.write().opSubstract(d.read(), o.d.read()); |
| 166 |
| 167 | return result; |
| 168 | } |
| 169 |
| 170 | inline void VRle::reset() |
| 171 | { |
| 172 | d.write().reset(); |
| 173 | } |
| 174 |
| 175 | inline void VRle::clone(const VRle &o) |
| 176 | { |
| 177 | d.write().clone(o.d.read()); |
| 178 | } |
| 179 |
| 180 | inline void VRle::translate(const VPoint &p) |
| 181 | { |
| 182 | d.write().translate(p); |
| 183 | } |
| 184 |
| 185 | inline void VRle::intersect(const VRect &r, VRleSpanCb cb, void *userData) const |
| 186 | { |
| 187 | d->opIntersect(r, cb, userData); |
| 188 | } |
| 189 |
| 190 | inline void VRle::intersect(const VRle &r, VRleSpanCb cb, void *userData) const |
| 191 | { |
| 192 | if (empty() || r.empty()) return; |
| 193 | opIntersectHelper(d.read(), r.d.read(), cb, userData); |
| 194 | } |
| 195 |
| 196 | V_END_NAMESPACE |
| 197 |
| 198 | #endif // VRLE_H |
| 199 |