chalk

kis_math_toolbox.cpp
1/*
2 * This file is part of the KDE project
3 *
4 * Copyright (c) 2005 Cyrille Berger <cberger@cberger.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21#include "kis_math_toolbox.h"
22
23#ifdef HAVE_OPENEXR
24#include <half.h>
25#endif
26
27#include "kis_basic_math_toolbox.h"
28#include "kis_iterators_pixel.h"
29
30
31KisMathToolbox::KisMathToolbox(KisID id) : m_id(id)
32{
33}
34
35KisMathToolbox::~KisMathToolbox()
36{
37}
38
39KisMathToolboxFactoryRegistry::KisMathToolboxFactoryRegistry()
40{
41 add(new KisBasicMathToolbox());
42}
43KisMathToolboxFactoryRegistry::~KisMathToolboxFactoryRegistry()
44{
45}
46template<typename T>
47double toDouble(TQ_UINT8* data, int channelpos )
48{
49 return (float)( *((T*)(data + channelpos)) );
50}
51
52typedef double (*PtrToDouble)(TQ_UINT8*, int);
53
54template<typename T>
55void fromDouble(TQ_UINT8* data, int channelpos, double v )
56{
57 *((T*)(data + channelpos)) = (T)v;
58}
59
60typedef void (*PtrFromDouble)(TQ_UINT8*, int, double);
61
62
63void KisMathToolbox::transformToFR(KisPaintDeviceSP src, KisFloatRepresentation* fr, const TQRect& rect)
64{
65 TQ_INT32 depth = src->colorSpace()->nColorChannels();
66 TQMemArray<PtrToDouble> f(depth);
67 TQValueVector<KisChannelInfo *> cis = src->colorSpace()->channels();
68 for(TQ_INT32 k = 0; k < depth; k++)
69 {
70 switch( cis[k]->channelValueType() )
71 {
72 case KisChannelInfo::UINT8:
73 f[k] = toDouble<TQ_UINT8>;
74 break;
75 case KisChannelInfo::UINT16:
76 f[k] = toDouble<TQ_UINT16>;
77 break;
78#ifdef HAVE_OPENEXR
79 case KisChannelInfo::FLOAT16:
80 f[k] = toDouble<half>;
81 break;
82#endif
83 case KisChannelInfo::FLOAT32:
84 f[k] = toDouble<float>;
85 break;
86 case KisChannelInfo::INT8:
87 f[k] = toDouble<TQ_INT8>;
88 break;
89 case KisChannelInfo::INT16:
90 f[k] = toDouble<TQ_INT16>;
91 break;
92 default:
93 kdWarning() << "Unsupported value type in KisMathToolbox" << endl;
94 return;
95 }
96 }
97
98 for(int i = rect.y(); i < rect.height(); i++)
99 {
100 KisHLineIteratorPixel srcIt = src->createHLineIterator(rect.x(), i, rect.width(), false );
101 float *dstIt = fr->coeffs + (i-rect.y()) * fr->size * fr->depth;
102 while( ! srcIt.isDone() )
103 {
104 TQ_UINT8* v1 = srcIt.rawData();
105 for( int k = 0; k < depth; k++)
106 {
107 *dstIt = f[k](v1, cis[k]->pos());
108 ++dstIt;
109 }
110 ++srcIt;
111 }
112 }
113}
114
115void KisMathToolbox::transformFromFR(KisPaintDeviceSP dst, KisFloatRepresentation* fr, const TQRect& rect)
116{
117 TQ_INT32 depth = dst->colorSpace()->nColorChannels();
118 TQMemArray<PtrFromDouble> f(depth);
119 TQValueVector<KisChannelInfo *> cis = dst->colorSpace()->channels();
120 for(TQ_INT32 k = 0; k < depth; k++)
121 {
122 switch( cis[k]->channelValueType() )
123 {
124 case KisChannelInfo::UINT8:
125 f[k] = fromDouble<TQ_UINT8>;
126 break;
127 case KisChannelInfo::UINT16:
128 f[k] = fromDouble<TQ_UINT16>;
129 break;
130#ifdef HAVE_OPENEXR
131 case KisChannelInfo::FLOAT16:
132 f[k] = fromDouble<half>;
133 break;
134#endif
135 case KisChannelInfo::FLOAT32:
136 f[k] = fromDouble<float>;
137 break;
138 case KisChannelInfo::INT8:
139 f[k] = fromDouble<TQ_INT8>;
140 break;
141 case KisChannelInfo::INT16:
142 f[k] = fromDouble<TQ_INT16>;
143 break;
144 default:
145 kdWarning() << "Unsupported value type in KisMathToolbox" << endl;
146 return;
147 }
148 }
149 for(int i = rect.y(); i < rect.height(); i++)
150 {
151 KisHLineIteratorPixel dstIt = dst->createHLineIterator(rect.x(), i, rect.width(), true );
152 float *srcIt = fr->coeffs + (i-rect.y()) * fr->size * fr->depth;
153 while( ! dstIt.isDone() )
154 {
155 TQ_UINT8* v1 = dstIt.rawData();
156 for( int k = 0; k < depth; k++)
157 {
158 f[k](v1, cis[k]->pos(), *srcIt);
159 ++srcIt;
160 }
161 ++dstIt;
162 }
163 }
164}
165
166#include "kis_math_toolbox.moc"
Chalk has a large number of extensible resources.
Definition kis_id.h:57