00001 #include <cassert>
00002 #include <stdint.h>
00003
00004 #include <qpainter.h>
00005 #include <qapplication.h>
00006
00007 #include "mythimage.h"
00008 #include "mythmainwindow.h"
00009 #include "mythcontext.h"
00010
00011 MythImage::MythImage(MythPainter *parent)
00012 {
00013 assert(parent);
00014
00015 m_Parent = parent;
00016 m_Changed = false;
00017
00018 m_RefCount = 0;
00019
00020 m_isGradient = false;
00021 }
00022
00023 MythImage::~MythImage()
00024 {
00025 m_Parent->DeleteFormatImage(this);
00026 }
00027
00028
00029 void MythImage::UpRef(void)
00030 {
00031 m_RefCount++;
00032 }
00033
00034 bool MythImage::DownRef(void)
00035 {
00036 m_RefCount--;
00037 if (m_RefCount < 0)
00038 {
00039 delete this;
00040 return true;
00041 }
00042 return false;
00043 }
00044
00045 void MythImage::Assign(const QImage &img)
00046 {
00047 *(QImage *)this = img;
00048 SetChanged();
00049 }
00050
00051 void MythImage::Assign(const QPixmap &pix)
00052 {
00053 Assign(pix.convertToImage());
00054 }
00055
00056 void MythImage::Resize(const QSize &newSize)
00057 {
00058 if (m_isGradient)
00059 {
00060 *(QImage *)this = QImage(newSize, 32);
00061 MakeGradient(*this, m_gradBegin, m_gradEnd, m_gradAlpha);
00062 SetChanged();
00063 }
00064 else
00065 {
00066 Assign(smoothScale(newSize));
00067 }
00068 }
00069
00070 MythImage *MythImage::FromQImage(QImage **img)
00071 {
00072 if (!img || !*img)
00073 return NULL;
00074
00075 MythImage *ret = GetMythPainter()->GetFormatImage();
00076 ret->Assign(**img);
00077 delete *img;
00078 *img = NULL;
00079 return ret;
00080 }
00081
00082
00083 bool MythImage::Load(const QString &filename)
00084 {
00085 QImage *im = gContext->LoadScaleImage(filename);
00086 if (im)
00087 {
00088 Assign(*im);
00089 delete im;
00090 return true;
00091 }
00092
00093 return false;
00094 }
00095
00096 void MythImage::MakeGradient(QImage &image, const QColor &begin, const QColor &end, int alpha)
00097 {
00098 image.setAlphaBuffer(alpha < 255);
00099
00100
00101 float rstep = float(end.red() - begin.red()) /
00102 float(image.height());
00103 float gstep = float(end.green() - begin.green()) /
00104 float(image.height());
00105 float bstep = float(end.blue() - begin.blue()) /
00106 float(image.height());
00107
00108 uint32_t black = qRgba(0, 0, 0, alpha);
00109
00110 uint32_t *ptr = (uint32_t *)image.scanLine(0);
00111 for (int x = 0; x < image.width(); x++, ptr++)
00112 *ptr = black;
00113
00114 for (int y = 1; y < image.height() - 1; y++)
00115 {
00116 int r = (int)(begin.red() + (y * rstep));
00117 int g = (int)(begin.green() + (y * gstep));
00118 int b = (int)(begin.blue() + (y * bstep));
00119 uint32_t color = qRgba(r, g, b, alpha);
00120 ptr = (uint32_t *)image.scanLine(y);
00121
00122 *ptr = black; ptr++;
00123 for (int x = 0; x < image.width() - 2; x++, ptr++)
00124 *ptr = color;
00125 *ptr = black;
00126 }
00127
00128 ptr = (uint32_t *)image.scanLine(image.height() - 1);
00129 for (int x = 0; x < image.width(); x++, ptr++)
00130 *ptr = black;
00131 }
00132
00133 MythImage *MythImage::Gradient(const QSize & size, const QColor &begin,
00134 const QColor &end, uint alpha)
00135 {
00136 QImage img(size.width(), size.height(), 32);
00137
00138 MakeGradient(img, begin, end, alpha);
00139
00140 MythImage *ret = GetMythPainter()->GetFormatImage();
00141 ret->Assign(img);
00142 ret->m_isGradient = true;
00143 ret->m_gradBegin = begin;
00144 ret->m_gradEnd = end;
00145 ret->m_gradAlpha = alpha;
00146 return ret;
00147 }
00148