krita
kis_alpha_mask.cc00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <cfloat>
00020 #include <qimage.h>
00021 #include <qvaluevector.h>
00022
00023 #include <kdebug.h>
00024
00025 #include "kis_global.h"
00026 #include "kis_alpha_mask.h"
00027
00028 KisAlphaMask::KisAlphaMask(const QImage& img, bool hasColor)
00029 {
00030 m_width = img.width();
00031 m_height = img.height();
00032
00033 if (hasColor) {
00034 copyAlpha(img);
00035 }
00036 else {
00037 computeAlpha(img);
00038 }
00039 }
00040
00041 KisAlphaMask::KisAlphaMask(const QImage& img)
00042 {
00043 m_width = img.width();
00044 m_height = img.height();
00045
00046 if (!img.allGray()) {
00047 copyAlpha(img);
00048 }
00049 else {
00050 computeAlpha(img);
00051 }
00052 }
00053
00054 KisAlphaMask::KisAlphaMask(Q_INT32 width, Q_INT32 height)
00055 {
00056 m_width = width;
00057 m_height = height;
00058
00059 m_data.resize(width * height, OPACITY_TRANSPARENT);
00060 }
00061
00062 KisAlphaMask::~KisAlphaMask()
00063 {
00064 }
00065
00066 Q_INT32 KisAlphaMask::width() const
00067 {
00068 return m_width;
00069 }
00070
00071 Q_INT32 KisAlphaMask::height() const
00072 {
00073 return m_height;
00074 }
00075
00076 void KisAlphaMask::setAlphaAt(Q_INT32 x, Q_INT32 y, Q_UINT8 alpha)
00077 {
00078 if (y >= 0 && y < m_height && x >= 0 && x < m_width) {
00079 m_data[(y * m_width) + x] = alpha;
00080 }
00081 }
00082
00083 void KisAlphaMask::copyAlpha(const QImage& img)
00084 {
00085 for (int y = 0; y < img.height(); y++) {
00086 for (int x = 0; x < img.width(); x++) {
00087 QRgb c = img.pixel(x,y);
00088 Q_UINT8 a = (qGray(c) * qAlpha(c)) / 255;
00089 m_data.push_back(a);
00090
00091 }
00092 }
00093 }
00094
00095 void KisAlphaMask::computeAlpha(const QImage& img)
00096 {
00097
00098
00099
00100
00101
00102
00103
00104
00105 for (int y = 0; y < img.height(); y++) {
00106 for (int x = 0; x < img.width(); x++) {
00107 m_data.push_back(255 - qRed(img.pixel(x, y)));
00108 }
00109 }
00110 }
00111
00112 KisAlphaMaskSP KisAlphaMask::interpolate(KisAlphaMaskSP mask1, KisAlphaMaskSP mask2, double t)
00113 {
00114 Q_ASSERT((mask1->width() == mask2->width()) && (mask1->height() == mask2->height()));
00115 Q_ASSERT(t > -DBL_EPSILON && t < 1 + DBL_EPSILON);
00116
00117 int width = mask1->width();
00118 int height = mask1->height();
00119 KisAlphaMaskSP outputMask = new KisAlphaMask(width, height);
00120 Q_CHECK_PTR(outputMask);
00121
00122 for (int x = 0; x < width; x++) {
00123 for (int y = 0; y < height; y++) {
00124 Q_UINT8 d = static_cast<Q_UINT8>((1 - t) * mask1->alphaAt(x, y) + t * mask2->alphaAt(x, y));
00125 outputMask->setAlphaAt(x, y, d);
00126 }
00127 }
00128
00129 return outputMask;
00130 }
00131
00132
|