kis_u16_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#include "kdebug.h"
19
20#include "kis_global.h"
21#include "kis_abstract_colorspace.h"
22#include "kis_integer_maths.h"
23#include "kis_u16_base_colorspace.h"
24
25
26TQ_UINT8 KisU16BaseColorSpace::getAlpha(const TQ_UINT8 * U8_pixel) const
27{
28 if (m_alphaPos < 0) return OPACITY_OPAQUE;
29
30 U8_pixel+= m_alphaPos;
31
32 const TQ_UINT16 *pixel = reinterpret_cast<const TQ_UINT16 *>(U8_pixel);
33 return UINT16_TO_UINT8(*pixel);
34}
35
36
37void KisU16BaseColorSpace::setAlpha(TQ_UINT8 *U8_pixel, TQ_UINT8 alpha, TQ_INT32 nPixels) const
38{
39 if (m_alphaPos < 0) return;
40 TQ_INT32 psize = pixelSize();
41
42
43 while (nPixels > 0) {
44
45 TQ_UINT16 *pixel = reinterpret_cast<TQ_UINT16 *>(U8_pixel + m_alphaPos);
46 pixel[0] = UINT8_TO_UINT16(alpha);
47
48 --nPixels;
49 U8_pixel += psize;
50 }
51}
52
53void KisU16BaseColorSpace::multiplyAlpha(TQ_UINT8 *U8_pixel, TQ_UINT8 U8_alpha, TQ_INT32 nPixels)
54{
55 if (m_alphaPos < 0) return;
56
57 TQ_INT32 psize = pixelSize();
58 TQ_UINT16 alpha = UINT8_TO_UINT16(U8_alpha);
59
60 while (nPixels > 0) {
61
62 TQ_UINT16 *pixelAlpha = reinterpret_cast<TQ_UINT16 *>(U8_pixel + m_alphaPos);
63 *pixelAlpha = UINT16_MULT(*pixelAlpha, alpha);
64
65 --nPixels;
66 U8_pixel += psize;
67 }
68}
69
70void KisU16BaseColorSpace::applyAlphaU8Mask(TQ_UINT8 * U8_pixel, TQ_UINT8 * alpha8, TQ_INT32 nPixels)
71{
72 if (m_alphaPos < 0) return;
73
74 TQ_INT32 psize = pixelSize();
75
76 while (nPixels--) {
77
78 // Go to the alpha position (which is given in bytes from the start of the pixel,
79 // and cast to short.
80
81 TQ_UINT16 *pixelAlpha = reinterpret_cast<TQ_UINT16 *>(U8_pixel + m_alphaPos);
82 *pixelAlpha = UINT8_MULT(*pixelAlpha, *alpha8);
83
84 ++alpha8;
85 U8_pixel += psize;
86
87 }
88}
89
90void KisU16BaseColorSpace::applyInverseAlphaU8Mask(TQ_UINT8 * U8_pixels, TQ_UINT8 * alpha8, TQ_INT32 nPixels)
91{
92
93 if (m_alphaPos < 0) return;
94
95 TQ_INT32 psize = pixelSize();
96
97
98 while(nPixels--) {
99
100 TQ_UINT16 s_alpha8;
101 TQ_UINT32 p_alpha, s_alpha16;
102
103 TQ_UINT16 *alpha = reinterpret_cast<TQ_UINT16 *>(U8_pixels + m_alphaPos);
104
105 p_alpha = *(alpha);
106 s_alpha8 = MAX_SELECTED - *alpha8;
107 s_alpha16 = UINT8_TO_UINT16(s_alpha8);
108
109 // Go to the alpha position (which is given in bytes from the start of the pixel,
110 // and cast to short.
111
112 alpha[0] = UINT16_MULT(p_alpha, s_alpha16);
113
114 U8_pixels += psize;
115 ++alpha8;
116 }
117}
118
119TQString KisU16BaseColorSpace::channelValueText(const TQ_UINT8 *U8_pixel, TQ_UINT32 channelIndex) const
120{
121 Q_ASSERT(channelIndex < (TQ_UINT32)nChannels());
122 const TQ_UINT16 *pixel = reinterpret_cast<const TQ_UINT16 *>(U8_pixel);
123 TQ_UINT32 channelPosition = channels()[channelIndex]->pos() / sizeof(TQ_UINT16);
124
125 return TQString().setNum(pixel[channelPosition]);
126}
127
128TQString KisU16BaseColorSpace::normalisedChannelValueText(const TQ_UINT8 *U8_pixel, TQ_UINT32 channelIndex) const
129{
130 Q_ASSERT(channelIndex < (TQ_UINT32)nChannels());
131 const TQ_UINT16 *pixel = reinterpret_cast<const TQ_UINT16 *>(U8_pixel);
132 TQ_UINT32 channelPosition = m_channels[channelIndex]->pos() / sizeof(TQ_UINT16);
133
134 return TQString().setNum(100.0 * static_cast<float>(pixel[channelPosition]) / UINT16_MAX);
135}
136
137TQ_UINT8 KisU16BaseColorSpace::scaleToU8(const TQ_UINT8 * U8_pixel, TQ_INT32 channelPos)
138{
139 const TQ_UINT16 *pixel = reinterpret_cast<const TQ_UINT16 *>(U8_pixel);
140 return UINT16_TO_UINT8(pixel[channelPos]);
141}
142
143TQ_UINT16 KisU16BaseColorSpace::scaleToU16(const TQ_UINT8 * U8_pixel, TQ_INT32 channelPos)
144{
145 const TQ_UINT16 *pixel = reinterpret_cast<const TQ_UINT16 *>(U8_pixel);
146 return pixel[channelPos];
147}
148
virtual TQValueVector< KisChannelInfo * > channels() const =0
Return a vector describing all the channels this color model has.
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 scaleToU8(const TQ_UINT8 *srcPixel, TQ_INT32 channelPos)
Convert the value of the channel at the specified position into an 8-bit value.
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 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 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 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 void applyInverseAlphaU8Mask(TQ_UINT8 *pixels, TQ_UINT8 *alpha, TQ_INT32 nPixels)
Applies the inverted 8-bit alpha mask to the pixels.
virtual TQ_UINT8 getAlpha(const TQ_UINT8 *pixel) const
Get the alpha value of the given pixel, downscaled to an 8-bit value.
virtual void applyAlphaU8Mask(TQ_UINT8 *pixels, TQ_UINT8 *alpha, TQ_INT32 nPixels)
Applies the specified 8-bit alpha mask to the pixels.