qimage_operation.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include "qimage_operation.h"
  2. void image_info(const QImage& img)
  3. {
  4. qDebug("QImage: w:%d, h:%d, (format:%d,alpha:%d,grey:%d,null:%d),"
  5. "color count:%d, size(%d,%d), sizeinbytes:%lld,"
  6. "depth:%d, devicePixelRatio:%f, dotsX:%d, dotsY:%d,"
  7. "offset(%d,%d), rect(%d,%d,%d,%d),"
  8. "(hMM:%d,wMM:%d,logDpiX:%d,logDpiY:%d,phsDpiX:%d,phsDpiY:%d)",
  9. img.width(), img.height(), img.format(), img.hasAlphaChannel(),
  10. img.isGrayscale(), img.isNull(), img.colorCount(), img.size().width(),
  11. img.size().height(), img.sizeInBytes(), img.depth(),
  12. img.devicePixelRatio(), img.dotsPerMeterX(), img.dotsPerMeterY(),
  13. img.offset().x(), img.offset().y(), img.rect().x(), img.rect().y(),
  14. img.rect().width(), img.rect().height(), img.heightMM(), img.widthMM(),
  15. img.logicalDpiX(), img.logicalDpiY(), img.physicalDpiX(),
  16. img.physicalDpiY());
  17. }
  18. void create_image(QImage& img)
  19. {
  20. image_info(img);
  21. /*int w = img.width();
  22. int h = img.height();*/
  23. // QRgb value;
  24. // value = qRgb(237, 187, 51); // 0xffedba31
  25. // image.setPixel(2, 1, value);
  26. int n = 50;
  27. QRgb value[50];
  28. for (int i = 0; i < n; i++)
  29. value[i] = int(0xff000000 + 0xffffff * i / n);
  30. Q_ASSERT(img.format() == QImage::Format_RGB32);
  31. random_image(img);
  32. }
  33. bool draw_img_text(QImage& img, const QString& str, const QRect rt, QPen pen, QFont font)
  34. {
  35. QPainter p;
  36. if (!p.begin(&img))
  37. return false;
  38. p.setPen(pen);
  39. p.setFont(font);
  40. p.drawText(rt, Qt::AlignCenter, str);
  41. return p.end();
  42. }
  43. bool draw_img_rect(QImage& img, const QRect rt, QPen pen)
  44. {
  45. QPainter p;
  46. if (!p.begin(&img))
  47. return false;
  48. p.setPen(pen);
  49. p.drawRect(rt);
  50. return p.end();
  51. }
  52. void grey_image(QImage& img)
  53. {
  54. #if 1
  55. img = img.convertToFormat(QImage::Format_Grayscale8);
  56. #else
  57. int depth = sizeof(QRgb); // 4
  58. for (int i = 0; i < img.height(); i++)
  59. {
  60. uchar* scan = img.scanLine(i);
  61. for (int j = 0; j < img.width(); j++)
  62. {
  63. QRgb* rgbpixel = reinterpret_cast<QRgb*>(scan + j * depth);
  64. int gray = qGray(*rgbpixel);
  65. *rgbpixel = QColor(gray, gray, gray).rgb();
  66. }
  67. }
  68. #endif
  69. }
  70. void random_image(QImage& img)
  71. {
  72. #if 1
  73. int depth = img.depth() / 8;
  74. for (int i = 0; i < img.height(); i++)
  75. {
  76. uchar* scan = img.scanLine(i);
  77. for (int j = 0; j < img.width(); j++)
  78. {
  79. QRgb* rgbpixel = reinterpret_cast<QRgb*>(scan + j * depth);
  80. int r = QRandomGenerator::global()->generate();
  81. int g = QRandomGenerator::global()->generate();
  82. int b = QRandomGenerator::global()->generate();
  83. //*rgbpixel = QColor(r % 255, g % 255, b % 255).rgb();
  84. *rgbpixel = qRgb(r, g, b);
  85. }
  86. }
  87. #else
  88. for (int i = 0; i < img.height(); i++)
  89. {
  90. for (int j = 0; j < img.width(); j++)
  91. {
  92. // img.setPixel(j, i, value[w % n]);
  93. int r = QRandomGenerator::global()->generate();
  94. int g = QRandomGenerator::global()->generate();
  95. int b = QRandomGenerator::global()->generate();
  96. img.setPixel(j, i, qRgb(r % 255, g % 255, b % 255));
  97. }
  98. }
  99. #endif
  100. }
  101. void split_image(QImage& img, QImage& r_img, QImage& b_img, QImage& g_img)
  102. {
  103. Q_ASSERT(img.size() == r_img.size());
  104. Q_ASSERT(r_img.size() == b_img.size());
  105. Q_ASSERT(r_img.size() == g_img.size());
  106. int depth = img.depth() / 8;
  107. for (int i = 0; i < img.height(); i++)
  108. {
  109. uchar* scan = img.scanLine(i);
  110. uchar* r_scan = r_img.scanLine(i);
  111. uchar* b_scan = b_img.scanLine(i);
  112. uchar* g_scan = g_img.scanLine(i);
  113. for (int j = 0; j < img.width(); j++)
  114. {
  115. QRgb* all_rgbpixel = reinterpret_cast<QRgb*>(scan + j * depth);
  116. QRgb* r_rgbpixel = reinterpret_cast<QRgb*>(r_scan + j * depth);
  117. QRgb* g_rgbpixel = reinterpret_cast<QRgb*>(g_scan + j * depth);
  118. QRgb* b_rgbpixel = reinterpret_cast<QRgb*>(b_scan + j * depth);
  119. uint r = qRed(*all_rgbpixel);
  120. uint g = qGreen(*all_rgbpixel);
  121. uint b = qBlue(*all_rgbpixel);
  122. *r_rgbpixel = qRgb(r, 0, 0);
  123. *g_rgbpixel = qRgb(0, g, 0);
  124. *b_rgbpixel = qRgb(0, 0, b);
  125. }
  126. }
  127. }
  128. void invert_image(QImage& img)
  129. {
  130. img.invertPixels();
  131. }
  132. void mirro_image(QImage& img, bool horizontal, bool vertical)
  133. {
  134. img = img.mirrored(horizontal, vertical);
  135. }
  136. void swap_image(QImage& img)
  137. {
  138. img = img.rgbSwapped();
  139. }
  140. void scale_image(QImage& img, int width, int height)
  141. {
  142. img =
  143. img.scaled(width, height, Qt::IgnoreAspectRatio, Qt::FastTransformation);
  144. }
  145. void transform_image(QImage& img, const QTransform& matrix, Qt::TransformationMode mode)
  146. {
  147. img = img.transformed(matrix, mode);
  148. }
  149. QImage gamma_image(const QImage& img, double exp)
  150. {
  151. QImage retImg(img);
  152. int depth = img.depth() / 8;
  153. for (int i = 0; i < img.height(); i++)
  154. {
  155. const uchar* scan = img.scanLine(i);
  156. uchar* ret_scan = retImg.scanLine(i);
  157. for (int j = 0; j < img.width(); j++)
  158. {
  159. const QRgb* rgbpixel = reinterpret_cast<const QRgb*>(scan + j * depth);
  160. QRgb* ret_rgbpixel = reinterpret_cast<QRgb*>(ret_scan + j * depth);
  161. const double r = qRed(*rgbpixel) / 255.0;
  162. const double g = qGreen(*rgbpixel) / 255.0;
  163. const double b = qBlue(*rgbpixel) / 255.0;
  164. *ret_rgbpixel = QColor(255 * std::pow(r, exp), 255 * std::pow(g, exp),
  165. 255 * std::pow(b, exp))
  166. .rgb();
  167. }
  168. }
  169. return retImg;
  170. }
  171. /*
  172. QImage applyEffectToImage(QImage& src, QGraphicsEffect* effect, int extent)
  173. {
  174. QGraphicsScene scene;
  175. QGraphicsPixmapItem item;
  176. item.setPixmap(QPixmap::fromImage(src));
  177. item.setGraphicsEffect(effect);
  178. scene.addItem(&item);
  179. QImage res(src.size() + QSize(extent * 2, extent * 2),
  180. QImage::Format_ARGB32); res.fill(Qt::transparent); QPainter ptr(&res);
  181. scene.render(&ptr, QRectF(), QRectF(-extent, -extent, src.width() +
  182. extent * 2, src.height() + extent * 2)); return res;
  183. }
  184. QImage blur_img(QImage& img, int radius, int extent)
  185. {
  186. QGraphicsBlurEffect* e = new QGraphicsBlurEffect();
  187. e->setBlurRadius(radius);
  188. return applyEffectToImage(img, e, extent);
  189. }
  190. QImage dropshadow_img(QImage& img, int radius, int offsetX, int offsetY, QColor
  191. color, int extent)
  192. {
  193. QGraphicsDropShadowEffect* e = new QGraphicsDropShadowEffect();
  194. e->setColor(color);
  195. e->setOffset(offsetX, offsetY);
  196. e->setBlurRadius(radius);
  197. return applyEffectToImage(img, e, extent);
  198. }
  199. QImage colorize_img(QImage& img, QColor color, double strength)
  200. {
  201. QGraphicsColorizeEffect* e = new QGraphicsColorizeEffect();
  202. e->setColor(color);
  203. e->setStrength(strength);
  204. return applyEffectToImage(img, e);
  205. }
  206. QImage opacity_img(QImage& img, double opacity)
  207. {
  208. QGraphicsOpacityEffect* e = new QGraphicsOpacityEffect();
  209. e->setOpacity(opacity);
  210. //e->setOpacityMask();
  211. return applyEffectToImage(img, e);
  212. }
  213. */