00001 #include <QPainter>
00002 #include <QImage>
00003
00004 #include "mythpainter_yuva.h"
00005 #include "mythfontproperties.h"
00006 #include "mythlogging.h"
00007
00008 #define MAX_FONT_CACHE 32
00009
00010 QColor inline rgb_to_yuv(const QColor &original);
00011
00012 MythYUVAPainter::~MythYUVAPainter()
00013 {
00014 foreach(MythFontProperties* font, m_convertedFonts)
00015 delete font;
00016 }
00017
00018 void MythYUVAPainter::DrawImage(const QRect &dest, MythImage *im,
00019 const QRect &src, int alpha)
00020 {
00021 if (im->format() != QImage::Format_ARGB32)
00022 {
00023 QImage converted = im->convertToFormat(QImage::Format_ARGB32);
00024 im->Assign(converted);
00025 }
00026
00027 im->ConvertToYUV();
00028 MythQImagePainter::DrawImage(dest, im, src, alpha);
00029 }
00030
00031 void MythYUVAPainter::DrawText(const QRect &dest, const QString &msg, int flags,
00032 const MythFontProperties &font, int alpha,
00033 const QRect &boundRect)
00034 {
00035 MythFontProperties *converted = GetConvertedFont(font);
00036 if (converted)
00037 {
00038 MythImage *im = GetImageFromString(msg, flags, dest, *converted);
00039 if (im)
00040 im->SetToYUV();
00041 MythQImagePainter::DrawText(dest, msg, flags, *converted,
00042 alpha, boundRect);
00043 }
00044 }
00045
00046 void MythYUVAPainter::DrawRect(const QRect &area, const QBrush &fillBrush,
00047 const QPen &linePen, int alpha)
00048 {
00049 QBrush brush(fillBrush);
00050 brush.setColor(rgb_to_yuv(brush.color()));
00051 QPen pen(linePen);
00052 pen.setColor(rgb_to_yuv(pen.color()));
00053
00054 MythImage *im = GetImageFromRect(area, 0, 0, brush, pen);
00055 if (im)
00056 im->SetToYUV();
00057
00058 MythQImagePainter::DrawRect(area, brush, pen, alpha);
00059 }
00060
00061 void MythYUVAPainter::DrawRoundRect(const QRect &area, int cornerRadius,
00062 const QBrush &fillBrush, const QPen &linePen,
00063 int alpha)
00064 {
00065 QBrush brush(fillBrush);
00066 brush.setColor(rgb_to_yuv(brush.color()));
00067 QPen pen(linePen);
00068 pen.setColor(rgb_to_yuv(pen.color()));
00069
00070 MythImage *im = GetImageFromRect(area, cornerRadius, 0, brush, pen);
00071 if (im)
00072 im->SetToYUV();
00073
00074 MythQImagePainter::DrawRoundRect(area, cornerRadius, brush, pen, alpha);
00075 }
00076
00077 void MythYUVAPainter::DrawEllipse(const QRect &area, const QBrush &fillBrush,
00078 const QPen &linePen, int alpha)
00079 {
00080 QBrush brush(fillBrush);
00081 brush.setColor(rgb_to_yuv(brush.color()));
00082 QPen pen(linePen);
00083 pen.setColor(rgb_to_yuv(pen.color()));
00084
00085 MythImage *im = GetImageFromRect(area, 0, 1, brush, pen);
00086 if (im)
00087 im->SetToYUV();
00088
00089 MythQImagePainter::DrawEllipse(area, brush, pen, alpha);
00090 }
00091
00092 MythFontProperties* MythYUVAPainter::GetConvertedFont(const MythFontProperties &font)
00093 {
00094 QString original = font.GetHash();
00095
00096 if (m_convertedFonts.contains(original))
00097 {
00098 m_expireList.remove(original);
00099 m_expireList.push_back(original);
00100 }
00101 else
00102 {
00103 QColor yuv_color;
00104 MythFontProperties *new_font = new MythFontProperties();
00105 yuv_color = rgb_to_yuv(font.color());
00106 new_font->SetFace(font.face());
00107 new_font->SetColor(yuv_color);
00108
00109 if (font.hasShadow())
00110 {
00111 QPoint offset;
00112 QColor color;
00113 int alpha;
00114 font.GetShadow(offset, color, alpha);
00115 yuv_color = rgb_to_yuv(color);
00116 new_font->SetShadow(true, offset, yuv_color, alpha);
00117 }
00118
00119 if (font.hasOutline())
00120 {
00121 QColor color;
00122 int size, alpha;
00123 font.GetOutline(color, size, alpha);
00124 yuv_color = rgb_to_yuv(color);
00125 new_font->SetOutline(true, yuv_color, size, alpha);
00126 }
00127
00128 m_convertedFonts.insert(original, new_font);
00129 m_expireList.push_back(original);
00130
00131 if (m_convertedFonts.size() > MAX_FONT_CACHE)
00132 {
00133 QString expire = m_expireList.front();
00134 m_expireList.pop_front();
00135 if (m_convertedFonts.contains(expire))
00136 {
00137 delete m_convertedFonts.value(expire);
00138 m_convertedFonts.remove(expire);
00139 }
00140 }
00141 }
00142
00143 return m_convertedFonts.value(original);
00144 }
00145
00146 #define SCALEBITS 8
00147 #define ONE_HALF (1 << (SCALEBITS - 1))
00148 #define FIX(x) ((int) ((x) * (1L<<SCALEBITS) ))
00149
00150 QColor inline rgb_to_yuv(const QColor &original)
00151 {
00152 int r = original.red();
00153 int g = original.green();
00154 int b = original.blue();
00155 int a = original.alpha();
00156
00157 int y = (FIX(0.299) * r + FIX(0.587) * g +
00158 FIX(0.114) * b + ONE_HALF) >> SCALEBITS;
00159 int u = ((- FIX(0.169) * r - FIX(0.331) * g +
00160 FIX(0.499) * b + ONE_HALF) >> SCALEBITS) + 128;
00161 int v = ((FIX(0.499) * r - FIX(0.418) * g -
00162 FIX(0.0813) * b + ONE_HALF) >> SCALEBITS) + 128;
00163
00164 return QColor(y, u, v, a);
00165 }
00166