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 Q_UINT8 KisAlphaMask::alphaAt(Q_INT32 x, Q_INT32 y) const
00077 {
00078 if (y >= 0 && y < m_height && x >= 0 && x < m_width) {
00079 return m_data[(y * m_width) + x];
00080 }
00081 else {
00082 return OPACITY_TRANSPARENT;
00083 }
00084 }
00085
00086 void KisAlphaMask::setAlphaAt(Q_INT32 x, Q_INT32 y, Q_UINT8 alpha)
00087 {
00088 if (y >= 0 && y < m_height && x >= 0 && x < m_width) {
00089 m_data[(y * m_width) + x] = alpha;
00090 }
00091 }
00092
00093 void KisAlphaMask::copyAlpha(const QImage& img)
00094 {
00095 for (int y = 0; y < img.height(); y++) {
00096 for (int x = 0; x < img.width(); x++) {
00097 QRgb c = img.pixel(x,y);
00098 Q_UINT8 a = (qGray(c) * qAlpha(c)) / 255;
00099 m_data.push_back(a);
00100
00101 }
00102 }
00103 }
00104
00105 void KisAlphaMask::computeAlpha(const QImage& img)
00106 {
00107
00108
00109
00110
00111
00112
00113
00114
00115 for (int y = 0; y < img.height(); y++) {
00116 for (int x = 0; x < img.width(); x++) {
00117 m_data.push_back(255 - qRed(img.pixel(x, y)));
00118 }
00119 }
00120 }
00121
00122 KisAlphaMaskSP KisAlphaMask::interpolate(KisAlphaMaskSP mask1, KisAlphaMaskSP mask2, double t)
00123 {
00124 Q_ASSERT((mask1->width() == mask2->width()) && (mask1->height() == mask2->height()));
00125 Q_ASSERT(t > -DBL_EPSILON && t < 1 + DBL_EPSILON);
00126
00127 int width = mask1->width();
00128 int height = mask1->height();
00129 KisAlphaMaskSP outputMask = new KisAlphaMask(width, height);
00130 Q_CHECK_PTR(outputMask);
00131
00132 for (int x = 0; x < width; x++) {
00133 for (int y = 0; y < height; y++) {
00134 Q_UINT8 d = static_cast<Q_UINT8>((1 - t) * mask1->alphaAt(x, y) + t * mask2->alphaAt(x, y));
00135 outputMask->setAlphaAt(x, y, d);
00136 }
00137 }
00138
00139 return outputMask;
00140 }
00141
00142
|