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