krita

kis_u16_base_colorspace.cc

00001 /*
00002  *  Copyright (c) 2005 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 #include "kdebug.h"
00019 
00020 #include "kis_global.h"
00021 #include "kis_abstract_colorspace.h"
00022 #include "kis_integer_maths.h"
00023 #include "kis_u16_base_colorspace.h"
00024 
00025 
00026 Q_UINT8 KisU16BaseColorSpace::getAlpha(const Q_UINT8 * U8_pixel) const
00027 {
00028     if (m_alphaPos < 0) return OPACITY_OPAQUE;
00029 
00030     U8_pixel+= m_alphaPos;
00031 
00032     const Q_UINT16 *pixel = reinterpret_cast<const Q_UINT16 *>(U8_pixel);
00033     return UINT16_TO_UINT8(*pixel);
00034 }
00035 
00036 
00037 void KisU16BaseColorSpace::setAlpha(Q_UINT8 *U8_pixel, Q_UINT8 alpha, Q_INT32 nPixels) const
00038 {
00039     if (m_alphaPos < 0) return;
00040     Q_INT32 psize = pixelSize();
00041 
00042 
00043     while (nPixels > 0) {
00044 
00045         Q_UINT16 *pixel = reinterpret_cast<Q_UINT16 *>(U8_pixel + m_alphaPos);
00046         pixel[0] = UINT8_TO_UINT16(alpha);
00047 
00048         --nPixels;
00049         U8_pixel += psize;
00050     }
00051 }
00052 
00053 void KisU16BaseColorSpace::multiplyAlpha(Q_UINT8 *U8_pixel, Q_UINT8 U8_alpha, Q_INT32 nPixels)
00054 {
00055     if (m_alphaPos < 0) return;
00056 
00057     Q_INT32 psize = pixelSize();
00058     Q_UINT16 alpha = UINT8_TO_UINT16(U8_alpha);
00059 
00060     while (nPixels > 0) {
00061 
00062         Q_UINT16 *pixelAlpha = reinterpret_cast<Q_UINT16 *>(U8_pixel + m_alphaPos);
00063         *pixelAlpha = UINT16_MULT(*pixelAlpha, alpha);
00064 
00065         --nPixels;
00066         U8_pixel += psize;
00067     }
00068 }
00069 
00070 void KisU16BaseColorSpace::applyAlphaU8Mask(Q_UINT8 * U8_pixel, Q_UINT8 * alpha8, Q_INT32 nPixels)
00071 {
00072     if (m_alphaPos < 0) return;
00073 
00074     Q_INT32 psize = pixelSize();
00075 
00076     while (nPixels--) {
00077 
00078         // Go to the alpha position (which is given in bytes from the start of the pixel,
00079         // and cast to short.
00080 
00081         Q_UINT16 *pixelAlpha = reinterpret_cast<Q_UINT16 *>(U8_pixel + m_alphaPos);
00082         *pixelAlpha = UINT8_MULT(*pixelAlpha, *alpha8);
00083 
00084         ++alpha8;
00085         U8_pixel += psize;
00086 
00087     }
00088 }
00089 
00090 void KisU16BaseColorSpace::applyInverseAlphaU8Mask(Q_UINT8 * U8_pixels, Q_UINT8 * alpha8, Q_INT32 nPixels)
00091 {
00092 
00093     if (m_alphaPos < 0) return;
00094 
00095     Q_INT32 psize = pixelSize();
00096 
00097 
00098     while(nPixels--) {
00099 
00100             Q_UINT16 s_alpha8;
00101             Q_UINT32 p_alpha, s_alpha16;
00102 
00103             Q_UINT16 *alpha = reinterpret_cast<Q_UINT16 *>(U8_pixels + m_alphaPos);
00104 
00105             p_alpha = *(alpha);
00106             s_alpha8 = MAX_SELECTED - *alpha8;
00107             s_alpha16 = UINT8_TO_UINT16(s_alpha8);
00108 
00109             // Go to the alpha position (which is given in bytes from the start of the pixel,
00110             // and cast to short.
00111 
00112             alpha[0] = UINT16_MULT(p_alpha, s_alpha16);
00113 
00114             U8_pixels += psize;
00115             ++alpha8;
00116     }
00117 }
00118 
00119 QString KisU16BaseColorSpace::channelValueText(const Q_UINT8 *U8_pixel, Q_UINT32 channelIndex) const
00120 {
00121     Q_ASSERT(channelIndex < (Q_UINT32)nChannels());
00122     const Q_UINT16 *pixel = reinterpret_cast<const Q_UINT16 *>(U8_pixel);
00123     Q_UINT32 channelPosition = channels()[channelIndex]->pos() / sizeof(Q_UINT16);
00124 
00125     return QString().setNum(pixel[channelPosition]);
00126 }
00127 
00128 QString KisU16BaseColorSpace::normalisedChannelValueText(const Q_UINT8 *U8_pixel, Q_UINT32 channelIndex) const
00129 {
00130     Q_ASSERT(channelIndex < (Q_UINT32)nChannels());
00131     const Q_UINT16 *pixel = reinterpret_cast<const Q_UINT16 *>(U8_pixel);
00132     Q_UINT32 channelPosition = m_channels[channelIndex]->pos() / sizeof(Q_UINT16);
00133 
00134     return QString().setNum(100.0 * static_cast<float>(pixel[channelPosition]) / UINT16_MAX);
00135 }
00136 
00137 Q_UINT8 KisU16BaseColorSpace::scaleToU8(const Q_UINT8 * U8_pixel, Q_INT32 channelPos)
00138 {
00139     const Q_UINT16 *pixel = reinterpret_cast<const Q_UINT16 *>(U8_pixel);
00140     return UINT16_TO_UINT8(pixel[channelPos]);
00141 }
00142 
00143 Q_UINT16 KisU16BaseColorSpace::scaleToU16(const Q_UINT8 * U8_pixel, Q_INT32 channelPos)
00144 {
00145     const Q_UINT16 *pixel = reinterpret_cast<const Q_UINT16 *>(U8_pixel);
00146     return pixel[channelPos];
00147 }
00148 
KDE Home | KDE Accessibility Home | Description of Access Keys