MythTV  master
mythpainter_yuva.cpp
Go to the documentation of this file.
1 #include <QPainter>
2 #include <QImage>
3 
4 #include "mythpainter_yuva.h"
5 #include "mythfontproperties.h"
6 #include "mythlogging.h"
7 
8 #define MAX_FONT_CACHE 32
9 
10 QColor inline rgb_to_yuv(const QColor &original);
11 
13 {
14  Teardown();
15  foreach(MythFontProperties* font, m_convertedFonts)
16  delete font;
17 }
18 
19 void MythYUVAPainter::DrawImage(const QRect &dest, MythImage *im,
20  const QRect &src, int alpha)
21 {
22  if (im->format() != QImage::Format_ARGB32)
23  {
24  QImage converted = im->convertToFormat(QImage::Format_ARGB32);
25  im->Assign(converted);
26  }
27 
28  im->ConvertToYUV();
29  MythQImagePainter::DrawImage(dest, im, src, alpha);
30 }
31 
32 void MythYUVAPainter::DrawText(const QRect &dest, const QString &msg, int flags,
33  const MythFontProperties &font, int alpha,
34  const QRect &boundRect)
35 {
36  MythFontProperties *converted = GetConvertedFont(font);
37  if (converted)
38  {
39  // We pull an image here, in the hopes that when DrawText
40  // pulls an image this will still be in the cache and have
41  // the right properties.
42  MythImage *im = GetImageFromString(msg, flags, dest, *converted);
43  if (im)
44  {
45  im->SetToYUV();
46  im->DecrRef();
47  im = nullptr;
48  }
49 
50  MythQImagePainter::DrawText(dest, msg, flags, *converted,
51  alpha, boundRect);
52  }
53 }
54 
55 void MythYUVAPainter::DrawRect(const QRect &area, const QBrush &fillBrush,
56  const QPen &linePen, int alpha)
57 {
58  QBrush brush(fillBrush);
59 
60  switch (fillBrush.style())
61  {
62  case Qt::LinearGradientPattern:
63  case Qt::RadialGradientPattern:
64  case Qt::ConicalGradientPattern:
65  {
66  QGradient gradient = *fillBrush.gradient();
67  QGradientStops stops = gradient.stops();
68  for (QGradientStops::iterator it = stops.begin(); it != stops.end(); ++it)
69  {
70  it->second = rgb_to_yuv(it->second);
71  it->second.setAlpha(alpha);
72  }
73  gradient.setStops(stops);
74  brush = gradient;
75  }
76  break;
77  default:
78  brush.setColor(rgb_to_yuv(brush.color()));
79  break;
80  }
81 
82  QPen pen(linePen);
83  pen.setColor(rgb_to_yuv(pen.color()));
84 
85  // We pull an image here, in the hopes that when DrawRect
86  // pulls an image this will still be in the cache and have
87  // the right properties.
88  MythImage *im = GetImageFromRect(area, 0, 0, brush, pen);
89  if (im)
90  {
91  im->SetToYUV();
92  im->DecrRef();
93  im = nullptr;
94  }
95 
96  MythQImagePainter::DrawRect(area, brush, pen, alpha);
97 }
98 
99 void MythYUVAPainter::DrawRoundRect(const QRect &area, int cornerRadius,
100  const QBrush &fillBrush, const QPen &linePen,
101  int alpha)
102 {
103  QBrush brush(fillBrush);
104  brush.setColor(rgb_to_yuv(brush.color()));
105  QPen pen(linePen);
106  pen.setColor(rgb_to_yuv(pen.color()));
107 
108  // We pull an image here, in the hopes that when DrawRect
109  // pulls an image this will still be in the cache and have
110  // the right properties.
111  MythImage *im = GetImageFromRect(area, cornerRadius, 0, brush, pen);
112  if (im)
113  {
114  im->SetToYUV();
115  im->DecrRef();
116  im = nullptr;
117  }
118 
119  MythQImagePainter::DrawRoundRect(area, cornerRadius, brush, pen, alpha);
120 }
121 
122 void MythYUVAPainter::DrawEllipse(const QRect &area, const QBrush &fillBrush,
123  const QPen &linePen, int alpha)
124 {
125  QBrush brush(fillBrush);
126  brush.setColor(rgb_to_yuv(brush.color()));
127  QPen pen(linePen);
128  pen.setColor(rgb_to_yuv(pen.color()));
129 
130  // We pull an image here, in the hopes that when DrawRect
131  // pulls an image this will still be in the cache and have
132  // the right properties.
133  MythImage *im = GetImageFromRect(area, 0, 1, brush, pen);
134  if (im)
135  {
136  im->SetToYUV();
137  im->DecrRef();
138  im = nullptr;
139  }
140 
141  MythQImagePainter::DrawEllipse(area, brush, pen, alpha);
142 }
143 
145 {
146  QString original = font.GetHash();
147 
148  if (m_convertedFonts.contains(original))
149  {
150  m_expireList.remove(original);
151  m_expireList.push_back(original);
152  }
153  else
154  {
155  QColor yuv_color;
156  MythFontProperties *new_font = new MythFontProperties();
157  yuv_color = rgb_to_yuv(font.color());
158  new_font->SetFace(font.face());
159  new_font->SetColor(yuv_color);
160 
161  if (font.hasShadow())
162  {
163  QPoint offset;
164  QColor color;
165  int alpha;
166  font.GetShadow(offset, color, alpha);
167  yuv_color = rgb_to_yuv(color);
168  new_font->SetShadow(true, offset, yuv_color, alpha);
169  }
170 
171  if (font.hasOutline())
172  {
173  QColor color;
174  int size, alpha;
175  font.GetOutline(color, size, alpha);
176  yuv_color = rgb_to_yuv(color);
177  new_font->SetOutline(true, yuv_color, size, alpha);
178  }
179 
180  m_convertedFonts.insert(original, new_font);
181  m_expireList.push_back(original);
182 
183  if (m_convertedFonts.size() > MAX_FONT_CACHE)
184  {
185  QString expire = m_expireList.front();
186  m_expireList.pop_front();
187  if (m_convertedFonts.contains(expire))
188  {
189  delete m_convertedFonts.value(expire);
190  m_convertedFonts.remove(expire);
191  }
192  }
193  }
194 
195  return m_convertedFonts.value(original);
196 }
197 
198 #define SCALEBITS 8
199 #define ONE_HALF (1 << (SCALEBITS - 1))
200 #define FIX(x) ((int) ((x) * (1L<<SCALEBITS) /*+ 0.5*/))
201 
202 QColor inline rgb_to_yuv(const QColor &original)
203 {
204  int r = original.red();
205  int g = original.green();
206  int b = original.blue();
207  int a = original.alpha();
208 
209  int y = (FIX(0.299) * r + FIX(0.587) * g +
210  FIX(0.114) * b + ONE_HALF) >> SCALEBITS;
211  int u = ((- FIX(0.169) * r - FIX(0.331) * g +
212  FIX(0.499) * b + ONE_HALF) >> SCALEBITS) + 128;
213  int v = ((FIX(0.499) * r - FIX(0.418) * g -
214  FIX(0.0813) * b + ONE_HALF) >> SCALEBITS) + 128;
215 
216  return {y, u, v, a};
217 }
virtual void Teardown(void)
Definition: mythpainter.cpp:29
QColor color(void) const
void SetOutline(bool on, const QColor &color, int size, int alpha)
int DecrRef(void) override
Decrements reference count and deletes on 0.
Definition: mythimage.cpp:55
void DrawEllipse(const QRect &area, const QBrush &fillBrush, const QPen &linePen, int alpha) override
#define MAX_FONT_CACHE
void DrawRoundRect(const QRect &area, int cornerRadius, const QBrush &fillBrush, const QPen &linePen, int alpha) override
void DrawImage(const QRect &r, MythImage *im, const QRect &src, int alpha) override
void GetShadow(QPoint &offset, QColor &color, int &alpha) const
void SetColor(const QColor &color)
QString GetHash(void) const
void DrawImage(const QRect &dest, MythImage *im, const QRect &src, int alpha) override
void DrawText(const QRect &dest, const QString &msg, int flags, const MythFontProperties &font, int alpha, const QRect &boundRect) override
void DrawRoundRect(const QRect &area, int cornerRadius, const QBrush &fillBrush, const QPen &linePen, int alpha) override
void DrawEllipse(const QRect &area, const QBrush &fillBrush, const QPen &linePen, int alpha) override
unsigned char r
Definition: ParseText.cpp:329
#define SCALEBITS
QMap< QString, MythFontProperties * > m_convertedFonts
QColor rgb_to_yuv(const QColor &original)
MythImage * GetImageFromRect(const QRect &area, int radius, int ellipse, const QBrush &fillBrush, const QPen &linePen)
unsigned char b
Definition: ParseText.cpp:329
QFont face(void) const
MythFontProperties * GetConvertedFont(const MythFontProperties &font)
MythImage * GetImageFromString(const QString &msg, int flags, const QRect &r, const MythFontProperties &font)
void GetOutline(QColor &color, int &size, int &alpha) const
void DrawText(const QRect &r, const QString &msg, int flags, const MythFontProperties &font, int alpha, const QRect &boundRect) override
bool hasOutline(void) const
void DrawRect(const QRect &area, const QBrush &fillBrush, const QPen &linePen, int alpha) override
void SetFace(const QFont &face)
#define ONE_HALF
#define FIX(x)
std::list< QString > m_expireList
void ConvertToYUV(void)
Definition: mythimage.cpp:433
void SetShadow(bool on, const QPoint &offset, const QColor &color, int alpha)
void Assign(const QImage &img)
Definition: mythimage.cpp:80
bool hasShadow(void) const
void DrawRect(const QRect &area, const QBrush &fillBrush, const QPen &linePen, int alpha) override
void SetToYUV(void)
Definition: mythimage.h:52
unsigned char g
Definition: ParseText.cpp:329