kis_u8_base_colorspace.cpp
1/*
2 * Copyright (c) 2005 Boudewijn Rempt <boud@valdyas.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19#include <tqcolor.h>
20
21#include <kdebug.h>
22
23#include "kis_abstract_colorspace.h"
24#include "kis_u8_base_colorspace.h"
25#include "kis_integer_maths.h"
26
27TQ_UINT8 KisU8BaseColorSpace::getAlpha(const TQ_UINT8 * pixel) const
28{
29 return pixel[m_alphaPos];
30}
31
32
33
34void KisU8BaseColorSpace::setAlpha(TQ_UINT8 * pixels, TQ_UINT8 alpha, TQ_INT32 nPixels) const
35{
36 if (m_alphaPos < 0) return;
37 TQ_INT32 psize = pixelSize();
38
39 pixels += m_alphaPos;
40 while (nPixels > 0) {
41 *pixels = alpha;
42 --nPixels;
43 pixels += psize;
44 }
45
46}
47
48void KisU8BaseColorSpace::multiplyAlpha(TQ_UINT8 * pixels, TQ_UINT8 alpha, TQ_INT32 nPixels)
49{
50 if (m_alphaPos < 0) return;
51 TQ_INT32 psize = pixelSize();
52
53 while (nPixels > 0) {
54 pixels[m_alphaPos] = UINT8_MULT(pixels[m_alphaPos], alpha);
55 --nPixels;
56 pixels += psize;
57 }
58}
59
60void KisU8BaseColorSpace::applyAlphaU8Mask(TQ_UINT8 * pixels, TQ_UINT8 * alpha, TQ_INT32 nPixels)
61{
62 TQ_INT32 psize = pixelSize();
63
64 while (nPixels--) {
65
66 pixels[m_alphaPos] = UINT8_MULT(*(pixels + m_alphaPos) , *alpha);
67
68 alpha++;
69 pixels += psize;
70
71 }
72}
73
74void KisU8BaseColorSpace::applyInverseAlphaU8Mask(TQ_UINT8 * pixels, TQ_UINT8 * alpha, TQ_INT32 nPixels)
75{
76 TQ_INT32 psize = pixelSize();
77
78 while(nPixels--) {
79
80 TQ_UINT16 p_alpha, s_alpha;
81
82 p_alpha = getAlpha(pixels);
83 s_alpha = MAX_SELECTED - *alpha;
84
85 setAlpha(pixels, UINT8_MULT(p_alpha, s_alpha), 1);
86
87 pixels += psize;
88 ++alpha;
89 }
90}
91
92TQString KisU8BaseColorSpace::channelValueText(const TQ_UINT8 *pixel, TQ_UINT32 channelIndex) const
93{
94 Q_ASSERT(channelIndex < (TQ_UINT32)nChannels());
95 TQ_UINT32 channelPosition = m_channels[channelIndex]->pos();
96
97 return TQString().setNum(pixel[channelPosition]);
98}
99
100TQString KisU8BaseColorSpace::normalisedChannelValueText(const TQ_UINT8 *pixel, TQ_UINT32 channelIndex) const
101{
102 Q_ASSERT(channelIndex < (TQ_UINT32)nChannels());
103 TQ_UINT32 channelPosition = m_channels[channelIndex]->pos();
104
105 return TQString().setNum(100.0 * static_cast<float>(pixel[channelPosition]) / UINT8_MAX);
106}
107
108
109TQ_UINT8 KisU8BaseColorSpace::scaleToU8(const TQ_UINT8 * pixel, TQ_INT32 channelPos)
110{
111 return pixel[channelPos];
112}
113
114TQ_UINT16 KisU8BaseColorSpace::scaleToU16(const TQ_UINT8 * pixel, TQ_INT32 channelPos)
115{
116 return UINT8_TO_UINT16(pixel[channelPos]);
117}
118
virtual TQ_UINT32 nChannels() const =0
The total number of channels for a single pixel in this color model.
virtual TQ_UINT32 pixelSize() const =0
The size in bytes of a single pixel in this color model.
virtual void multiplyAlpha(TQ_UINT8 *pixels, TQ_UINT8 alpha, TQ_INT32 nPixels)
Multiply the alpha channel of the given run of pixels by the given value.
virtual TQ_UINT8 getAlpha(const TQ_UINT8 *pixel) const
Get the alpha value of the given pixel, downscaled to an 8-bit value.
virtual TQString channelValueText(const TQ_UINT8 *pixel, TQ_UINT32 channelIndex) const
Return a string with the channel's value suitable for display in the gui.
virtual TQ_UINT16 scaleToU16(const TQ_UINT8 *srcPixel, TQ_INT32 channelPos)
Convert the value of the channel at the specified position into a 16-bit value.
virtual TQ_UINT8 scaleToU8(const TQ_UINT8 *srcPixel, TQ_INT32 channelPos)
Convert the value of the channel at the specified position into an 8-bit value.
virtual TQString normalisedChannelValueText(const TQ_UINT8 *pixel, TQ_UINT32 channelIndex) const
Return a string with the channel's value with integer channels normalised to the floating point range...
virtual void applyInverseAlphaU8Mask(TQ_UINT8 *pixels, TQ_UINT8 *alpha, TQ_INT32 nPixels)
Applies the inverted 8-bit alpha mask to the pixels.
virtual void setAlpha(TQ_UINT8 *pixels, TQ_UINT8 alpha, TQ_INT32 nPixels) const
Set the alpha channel of the given run of pixels to the given value.
virtual void applyAlphaU8Mask(TQ_UINT8 *pixels, TQ_UINT8 *alpha, TQ_INT32 nPixels)
Applies the specified 8-bit alpha mask to the pixels.