qimage_operation.cpp 7.4 KB

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