TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicimageutils.cpp
Go to the documentation of this file.
1 #include "musicimageutils.h"
2 
3 #include <QBitmap>
4 #include <QBuffer>
5 #include <QPainter>
6 
7 QPixmap TTK::Image::roundedPixmap(const QPixmap &input, int ratioX, int ratioY)
8 {
9  return roundedPixmap(input, QRect(QPoint(0, 0), input.size()), ratioX, ratioY);
10 }
11 
12 QPixmap TTK::Image::roundedPixmap(const QPixmap &input, const QSize &size, int ratioX, int ratioY)
13 {
14  return roundedPixmap(input, QRect(QPoint(0, 0), size), ratioX, ratioY);
15 }
16 
17 QPixmap TTK::Image::roundedPixmap(const QPixmap &input, const QRect &rect, int ratioX, int ratioY)
18 {
19  if(input.isNull())
20  {
21  return QPixmap();
22  }
23 
24  QPixmap image = input.scaled(rect.size());
25  image.setMask(generateMask(rect, ratioX, ratioY));
26  return image;
27 }
28 
29 QPixmap TTK::Image::roundedPixmap(const QPixmap &input, const QPixmap &mask, const QSize &size)
30 {
31  if(input.isNull() || mask.isNull())
32  {
33  return QPixmap();
34  }
35 
36  QPixmap image(mask);
37  QPainter painter(&image);
38  painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
39  painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
40  painter.drawPixmap(0, 0, input.scaled(size));
41  return image;
42 }
43 
44 QBitmap TTK::Image::generateMask(const QRect &rect, int ratioX, int ratioY)
45 {
46  QBitmap mask(rect.size());
47  QPainter painter(&mask);
48  painter.fillRect(rect, Qt::white);
49  painter.setBrush(QColor(0, 0, 0));
50  painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
51  painter.drawRoundedRect(rect, ratioX, ratioY);
52  return mask;
53 }
54 
55 QByteArray TTK::Image::generatePixmapData(const QPixmap &input)
56 {
57  if(input.isNull())
58  {
59  return {};
60  }
61 
62  QByteArray data;
63  QBuffer buffer(&data);
64  if(buffer.open(QIODevice::WriteOnly))
65  {
66  input.save(&buffer, JPG_FILE_SUFFIX);
67  }
68  buffer.close();
69  return data;
70 }
71 
72 void TTK::Image::fusionPixmap(QImage &back, const QImage &front, const QPoint &pt)
73 {
74  if(front.isNull())
75  {
76  return;
77  }
78 
79  QPainter painter(&back);
80  painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
81  painter.drawImage(pt.x(), pt.y(), front);
82 }
83 
84 void TTK::Image::fusionPixmap(QPixmap &back, const QPixmap &front, const QPoint &pt)
85 {
86  if(front.isNull())
87  {
88  return;
89  }
90 
91  QPainter painter(&back);
92  painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
93  painter.drawPixmap(pt.x(), pt.y(), front);
94 }
95 
96 QRgb TTK::Image::colorContrast(const QRgb color)
97 {
98  // Counting the perceptive luminance - human eye favors green color...
99  int v = 255 - (2.0 * qRed(color) + 3.0 * qGreen(color) + qBlue(color)) / 6.0;
100  v = v < 128 ? 0 : 255;
101  // 0, bright colors; 255, dark colors
102  return qRgb(v, v, v);
103 }
104 
105 QPixmap TTK::Image::grayScalePixmap(const QPixmap &input, int radius)
106 {
107  QImage pix = input.toImage();
108  for(int w = 0; w < pix.width(); ++w)
109  {
110  for(int h = 0; h < pix.height(); ++h)
111  {
112  int gray = qGray(pix.pixel(w, h)) + radius;
113  gray = qBound(0, gray, 255);
114  const QRgb rgb = qRgb(gray, gray, gray);
115  pix.setPixel(w, h, rgb);
116  }
117  }
118  return QPixmap::fromImage(pix);
119 }
120 
121 static int colorBurnTransform(int c, int delta)
122 {
123  if(0 > delta || delta > 0xFF)
124  {
125  return c;
126  }
127 
128  const int result = (c - (c * delta)/(0xFF - delta));
129  if(result > 0xFF)
130  {
131  return 0xFF;
132  }
133  else if(result < 0)
134  {
135  return 0;
136  }
137  return result;
138 }
139 
140 void TTK::Image::reRenderImage(int delta, const QImage *input, QImage *output)
141 {
142  for(int w = 0; w < input->width(); ++w)
143  {
144  for(int h = 0; h < input->height(); ++h)
145  {
146  const QRgb rgb = input->pixel(w, h);
147  output->setPixel(w, h, qRgb(colorBurnTransform(qRed(rgb), delta),
148  colorBurnTransform(qGreen(rgb), delta),
149  colorBurnTransform(qBlue(rgb), delta)));
150  }
151  }
152 }
#define JPG_FILE_SUFFIX
Definition: musicobject.h:32
voidpf void uLong size
Definition: ioapi.h:136
TTK_MODULE_EXPORT QPixmap grayScalePixmap(const QPixmap &input, int radius=0)
TTK_MODULE_EXPORT QPixmap roundedPixmap(const QPixmap &input, int ratioX, int ratioY)
TTK_MODULE_EXPORT QByteArray generatePixmapData(const QPixmap &input)
TTK_MODULE_EXPORT QBitmap generateMask(const QRect &rect, int ratioX, int ratioY)
static int colorBurnTransform(int c, int delta)
TTK_MODULE_EXPORT unsigned int colorContrast(const unsigned int color)
TTK_MODULE_EXPORT void reRenderImage(int delta, const QImage *input, QImage *output)
TTK_MODULE_EXPORT void fusionPixmap(QImage &back, const QImage &front, const QPoint &pt)