krita

kis_alpha_mask.cc

00001 /*
00002  *  Copyright (c) 2004 Boudewijn Rempt <boud@valdyas.org>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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     // The brushes are mostly grayscale on a white background,
00108     // although some do have a colors. The alpha channel is seldom
00109     // used, so we take the average gray value of this pixel of
00110     // the brush as the setting for the opacitiy. We need to
00111     // invert it, because 255, 255, 255 is white, which is
00112     // completely transparent, but 255 corresponds to
00113     // OPACITY_OPAQUE.
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 
KDE Home | KDE Accessibility Home | Description of Access Keys