File indexing completed on 2024-04-21 15:38:22

0001 /*
0002 * Copyright (C) 2006  Koos Vriezen <koos.vriezen@gmail.com>
0003 *
0004 * This library is free software; you can redistribute it and/or
0005 * modify it under the terms of the GNU Lesser General Public
0006 * License as published by the Free Software Foundation; either
0007 * version 2 of the License, or (at your option) any later version.
0008 *
0009 * This library is distributed in the hope that it will be useful,
0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012 * Lesser General Public License for more details.
0013 *
0014 * You should have received a copy of the GNU Lesser General Public
0015 * License along with this library; if not, write to the Free Software
0016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0017 */
0018 
0019 #ifndef _KMPLAYER_TYPES_H_
0020 #define _KMPLAYER_TYPES_H_
0021 
0022 #include <stdint.h>
0023 #include "kmplayer_def.h"
0024 #include "triestring.h"
0025 
0026 namespace KMPlayer {
0027 
0028 /**
0029  * Type meant for screen coordinates
0030  */
0031 class KMPLAYER_NO_EXPORT Single {
0032     int value;
0033     friend Single operator + (const Single s1, const Single s2);
0034     friend Single operator - (const Single s1, const Single s2);
0035     friend Single operator * (const Single s1, const Single s2);
0036     friend Single operator / (const Single s1, const Single s2);
0037     friend Single operator + (const Single s1, const int i);
0038     friend Single operator - (const Single s1, const int i);
0039     friend float operator * (const Single s, const float f);
0040     friend double operator * (const Single s, const double f);
0041     friend Single operator * (const int i, const Single s);
0042     friend float operator * (const float f, const Single s);
0043     friend double operator * (const double d, const Single s);
0044     friend Single operator / (const Single s, const int i);
0045     friend float operator / (const Single s, const float f);
0046     friend double operator / (const Single s, const double d);
0047     friend double operator / (const double d, const Single s);
0048     friend bool operator > (const Single s1, const Single s2);
0049     friend bool operator > (const Single s, const int i);
0050     friend bool operator > (const int i, const Single s);
0051     friend bool operator >= (const Single s1, const Single s2);
0052     friend bool operator == (const Single s1, const Single s2);
0053     friend bool operator != (const Single s1, const Single s2);
0054     friend bool operator < (const Single s1, const Single s2);
0055     friend bool operator < (const Single s, const int i);
0056     friend bool operator < (const int i, const Single s);
0057     friend bool operator <= (const Single s1, const Single s2);
0058     friend bool operator <= (const Single s, const int i);
0059     friend Single operator - (const Single s);
0060 public:
0061     Single () : value (0) {}
0062     Single (const int v) : value (v << 8) {}
0063     Single (const float v) : value (int (256 * v)) {}
0064     Single (const double v) : value (int (256 * v)) {}
0065     Single & operator = (const Single s) { value = s.value; return *this; }
0066     Single & operator = (const int v) { value = v << 8; return *this; }
0067     Single & operator = (const float v) { value = int (256 * v); return *this; }
0068     Single & operator = (const double v) { value = int(256 * v); return *this; }
0069     Single & operator += (const Single s) { value += s.value; return *this; }
0070     Single & operator += (const int i) { value += (i << 8); return *this; }
0071     Single & operator -= (const Single s) { value -= s.value; return *this; }
0072     Single & operator -= (const int i) { value -= (i << 8); return *this; }
0073     Single & operator *= (const Single s);
0074     Single & operator *= (const float f) { value = int(value*f); return *this; }
0075     Single & operator /= (const int i) { value /= i; return *this; }
0076     Single & operator /= (const float f);
0077     operator int () const { return value >> 8; }
0078     operator double () const { return 1.0 * value / 256; }
0079     operator float () const { return 1.0 * value / 256; }
0080 };
0081 
0082 template <class T>
0083 class KMPLAYER_NO_EXPORT Point {
0084 public:
0085     Point ();
0086     Point (T _x, T _y);
0087 
0088     bool operator == (const Point<T> &p) const;
0089     bool operator != (const Point<T> &p) const;
0090 
0091     T x;
0092     T y;
0093 };
0094 
0095 template <class T>
0096 class KMPLAYER_NO_EXPORT Size {
0097 public:
0098     Size ();
0099     Size (T w, T h);
0100 
0101     bool isEmpty () const;
0102     bool operator == (const Size<T> &s) const;
0103     bool operator != (const Size<T> &s) const;
0104 
0105     T width;
0106     T height;
0107 };
0108 
0109 template <class T>
0110 class KMPLAYER_NO_EXPORT Rect {
0111 public:
0112     Rect ();
0113     Rect (T a, T b, T w, T h);
0114     Rect (T a, T b, const Size<T> &s);
0115     Rect (const Point<T> &point, const Size<T> &s);
0116     T x () const;
0117     T y () const;
0118     T width () const;
0119     T height () const;
0120     Rect<T> unite (const Rect<T> &r) const;
0121     Rect<T> intersect (const Rect<T> &r) const;
0122     bool operator == (const Rect<T> &r) const;
0123     bool operator != (const Rect<T> &r) const;
0124     bool isEmpty () const;
0125 
0126     Point<T> point;
0127     Size<T> size;
0128 };
0129 
0130 typedef Size<Single> SSize;
0131 typedef Rect<Single> SRect;
0132 
0133 typedef Size<int> ISize;
0134 typedef Point<int> IPoint;
0135 typedef Rect<int> IRect;
0136 template <> Rect<int> Rect<int>::intersect (const Rect<int> &r) const;
0137 
0138 
0139 /**                                   a  b  0
0140  * Matrix for coordinate transforms   c  d  0
0141  *                                    tx ty 1     */
0142 class KMPLAYER_NO_EXPORT Matrix {
0143     friend class SizeEvent;
0144     float a, b, c, d;
0145     Single tx, ty;
0146 public:
0147     Matrix ();
0148     Matrix (const Matrix & matrix);
0149     Matrix (Single xoff, Single yoff, float xscale, float yscale);
0150     void getXY (Single & x, Single & y) const;
0151     void getWH (Single & w, Single & h) const;
0152     IRect toScreen (const SRect &rect) const;
0153     SRect toUser (const IRect &rect) const;
0154     void transform (const Matrix & matrix);
0155     void scale (float sx, float sy);
0156     void translate (Single x, Single y);
0157     // void rotate (float phi); // add this when needed
0158 };
0159 
0160 //-----------------------------------------------------------------------------
0161 
0162 #ifdef _KDEBUG_H_
0163 # ifndef KDE_NO_DEBUG_OUTPUT
0164 inline QDebug & operator << (QDebug & dbg, Single s) {
0165     dbg << (double) (s);
0166     return dbg;
0167 }
0168 # else
0169 inline QDebug & operator << (QDebug & dbg, Single) {
0170     return dbg;
0171 }
0172 # endif
0173 #endif
0174 
0175 inline Single & Single::operator *= (const Single s) {
0176     value = (((int64_t)value) * s.value) >> 8;
0177     return *this;
0178 }
0179 
0180 inline Single & Single::operator /= (const float f) {
0181     value = (int) (value / f);
0182     return *this;
0183 }
0184 
0185 inline Single operator + (const Single s1, const Single s2) {
0186     Single s;
0187     s.value = s1.value + s2.value;
0188     return s;
0189 }
0190 
0191 inline Single operator - (const Single s1, const Single s2) {
0192     Single s;
0193     s.value = s1.value - s2.value;
0194     return s;
0195 }
0196 
0197 inline Single operator * (const Single s1, const Single s2) {
0198     Single s;
0199     s.value = (((int64_t)s1.value) * s2.value) >> 8;
0200     return s;
0201 }
0202 
0203 inline Single operator / (const Single s1, const Single s2) {
0204     Single s;
0205     s.value = ((int64_t)s1.value << 8) / s2.value;
0206     return s;
0207 }
0208 
0209 inline Single operator + (const Single s, const int i) {
0210     return s + Single (i);
0211 }
0212 
0213 inline Single operator - (const Single s, const int i) {
0214     return s - Single (i);
0215 }
0216 
0217 inline Single operator * (const int i, const Single s) {
0218     Single s1;
0219     s1.value = s.value * i;
0220     return s1;
0221 }
0222 
0223 inline Single operator * (const Single s, const int i) {
0224     return i * s;
0225 }
0226 inline float operator * (const Single s, const float f) {
0227     return s.value * f / 256;
0228 }
0229 
0230 inline double operator * (const Single s, const double d) {
0231     return s.value * d / 256;
0232 }
0233 
0234 inline float operator * (const float f, const Single s) {
0235     return s.value * f / 256;
0236 }
0237 
0238 inline double operator * (const double d, const Single s) {
0239     return s.value * d / 256;
0240 }
0241 
0242 inline Single operator / (const Single s, const int i) {
0243     Single s1;
0244     s1.value = s.value / i;
0245     return s1;
0246 }
0247 
0248 inline float operator / (const Single s, const float f) {
0249     return (s.value / f ) / 256;
0250 }
0251 
0252 inline double operator / (const Single s, const double d) {
0253     return (s.value / d ) / 256;
0254 }
0255 
0256 inline double operator / (const double d, const Single s) {
0257     return (d * 256 / s.value);
0258 }
0259 
0260 inline bool
0261 operator > (const Single s1, const Single s2) { return s1.value > s2.value; }
0262 
0263 inline bool
0264 operator > (const Single s, const int i) { return s > Single (i); }
0265 
0266 inline bool
0267 operator > (const int i, const Single s) { return Single (i) > s; }
0268 
0269 inline bool
0270 operator >= (const Single s1, const Single s2) { return s1.value >= s2.value; }
0271 
0272 inline bool
0273 operator == (const Single s1, const Single s2) { return s1.value == s2.value; }
0274 
0275 inline bool
0276 operator != (const Single s1, const Single s2) { return s1.value != s2.value; }
0277 
0278 inline bool
0279 operator < (const Single s1, const Single s2) { return s1.value < s2.value; }
0280 
0281 inline bool
0282 operator < (const Single s, const int i) { return s < Single (i); }
0283 
0284 inline bool
0285 operator < (const int i, const Single s) { return Single (i) < s; }
0286 
0287 inline bool
0288 operator <= (const Single s1, const Single s2) { return s1.value <= s2.value; }
0289 
0290 inline bool
0291 operator <= (const Single s, const int i) { return s <= Single (i); }
0292 
0293 inline Single operator - (const Single s) {
0294     Single s1;
0295     s1.value = -s.value;
0296     return s1;
0297 }
0298 
0299 //-----------------------------------------------------------------------------
0300 
0301 template <class T> inline Point<T>::Point () : x (0), y (0) {}
0302 
0303 template <class T> inline Point<T>::Point (T _x, T _y) : x (_x), y (_y) {}
0304 
0305 template <class T>
0306 inline bool Point<T>::Point::operator == (const Point<T> &p) const {
0307     return x == p.x && y == p.y;
0308 }
0309 
0310 template <class T>
0311 inline bool Point<T>::Point::operator != (const Point<T> &p) const {
0312     return !(*this == p);
0313 }
0314 
0315 //-----------------------------------------------------------------------------
0316 
0317 template <class T> inline Size<T>::Size () : width (0), height (0) {}
0318 
0319 template <class T> inline Size<T>::Size (T w, T h) : width (w), height (h) {}
0320 
0321 template <class T> inline bool Size<T>::isEmpty () const {
0322     return width <= 0 || height <= 0;
0323 }
0324 
0325 template <class T>
0326 inline bool Size<T>::Size::operator == (const Size<T> &s) const {
0327     return width == s.width && height == s.height;
0328 }
0329 
0330 template <class T>
0331 inline bool Size<T>::Size::operator != (const Size<T> &s) const {
0332     return !(*this == s);
0333 }
0334 
0335 //-----------------------------------------------------------------------------
0336 
0337 template <class T> inline Rect<T>::Rect () {}
0338 
0339 template <class T> inline  Rect<T>::Rect (T a, T b, T w, T h)
0340  : point (a, b), size (w, h) {}
0341 
0342 template <class T> inline Rect<T>::Rect (T a, T b, const Size<T> &s)
0343  : point (a, b), size (s) {}
0344 
0345 template <class T> inline Rect<T>::Rect (const Point<T> &pnt, const Size<T> &s)
0346  : point (pnt), size (s) {}
0347 
0348 template <class T> inline T Rect<T>::x () const { return point.x; }
0349 
0350 template <class T> inline T Rect<T>::y () const { return point.y; }
0351 
0352 template <class T> inline T Rect<T>::width () const { return size.width; }
0353 
0354 template <class T> inline T Rect<T>::height () const { return size.height; }
0355 
0356 template <class T> inline bool Rect<T>::operator == (const Rect<T> &r) const {
0357     return point == r.point && size == r.size;
0358 }
0359 
0360 template <class T> inline bool Rect<T>::operator != (const Rect<T> &r) const {
0361     return !(*this == r);
0362 }
0363 
0364 template <class T> inline bool Rect<T>::isEmpty () const {
0365     return size.isEmpty ();
0366 }
0367 
0368 template <class T> inline Rect<T> Rect<T>::unite (const Rect<T> &r) const {
0369     if (size.isEmpty ())
0370         return r;
0371     if (r.size.isEmpty ())
0372         return *this;
0373     T a (point.x < r.point.x ? point.x : r.point.x);
0374     T b (point.y < r.point.y ? point.y : r.point.y);
0375     return Rect<T> (a, b,
0376             ((point.x + size.width < r.point.x + r.size.width)
0377              ? r.point.x + r.size.width : point.x + size.width) - a,
0378             ((point.y + size.height < r.point.y + r.size.height)
0379              ? r.point.y + r.size.height : point.y + size.height) - b);
0380 }
0381 
0382 template <class T> inline Rect<T> Rect<T>::intersect (const Rect<T> &r) const {
0383     T a (point.x < r.point.x ? r.point.x : point.x);
0384     T b (point.y < r.point.y ? r.point.y : point.y);
0385     return Rect<T> (a, b,
0386             ((point.x + size.width < r.point.x + r.size.width)
0387              ? point.x + size.width : r.point.x + r.size.width) - a,
0388             ((point.y + size.height < r.point.y + r.size.height)
0389              ? point.y + size.height : r.point.y + r.size.height) - b);
0390 }
0391 
0392 }  // KMPlayer namespace
0393 
0394 #endif //_KMPLAYER_TYPES_H_