krita

kis_math_toolbox.cpp

00001 /*
00002  *  This file is part of the KDE project
00003  *
00004  *  Copyright (c) 2005 Cyrille Berger <cberger@cberger.net>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "kis_math_toolbox.h"
00022 
00023 #ifdef HAVE_OPENEXR
00024 #include <half.h>
00025 #endif
00026 
00027 #include "kis_basic_math_toolbox.h"
00028 #include "kis_iterators_pixel.h"
00029 
00030 
00031 KisMathToolbox::KisMathToolbox(KisID id) : m_id(id)
00032 {
00033 }
00034 
00035 KisMathToolbox::~KisMathToolbox()
00036 {
00037 }
00038 
00039 KisMathToolboxFactoryRegistry::KisMathToolboxFactoryRegistry()
00040 {
00041     add(new KisBasicMathToolbox());
00042 }
00043 KisMathToolboxFactoryRegistry::~KisMathToolboxFactoryRegistry()
00044 {
00045 }
00046 template<typename T>
00047 double toDouble(Q_UINT8* data, int channelpos )
00048 {
00049     return (float)( *((T*)(data + channelpos)) );
00050 }
00051 
00052 typedef double (*PtrToDouble)(Q_UINT8*, int);
00053 
00054 template<typename T>
00055 void fromDouble(Q_UINT8* data, int channelpos, double v )
00056 {
00057     *((T*)(data + channelpos)) = (T)v;
00058 }
00059 
00060 typedef void (*PtrFromDouble)(Q_UINT8*, int, double);
00061 
00062 
00063 void KisMathToolbox::transformToFR(KisPaintDeviceSP src, KisFloatRepresentation* fr, const QRect& rect)
00064 {
00065     Q_INT32 depth = src->colorSpace()->nColorChannels();
00066     QMemArray<PtrToDouble> f(depth);
00067     QValueVector<KisChannelInfo *> cis = src->colorSpace()->channels();
00068     for(Q_INT32 k = 0; k < depth; k++)
00069     {
00070         switch( cis[k]->channelValueType() )
00071         {
00072             case KisChannelInfo::UINT8:
00073                 f[k] = toDouble<Q_UINT8>;
00074                 break;
00075             case KisChannelInfo::UINT16:
00076                 f[k] = toDouble<Q_UINT16>;
00077                 break;
00078 #ifdef HAVE_OPENEXR     
00079             case KisChannelInfo::FLOAT16:
00080                 f[k] = toDouble<half>;
00081                 break;
00082 #endif
00083             case KisChannelInfo::FLOAT32:
00084                 f[k] = toDouble<float>;
00085                 break;
00086             case KisChannelInfo::INT8:
00087                 f[k] = toDouble<Q_INT8>;
00088                 break;
00089             case KisChannelInfo::INT16:
00090                 f[k] = toDouble<Q_INT16>;
00091                 break;
00092             default:
00093                 kdWarning() << "Unsupported value type in KisMathToolbox" << endl;
00094                 return;
00095         }
00096     }
00097     
00098     for(int i = rect.y(); i < rect.height(); i++)
00099     {
00100         KisHLineIteratorPixel srcIt = src->createHLineIterator(rect.x(), i, rect.width(), false );
00101         float *dstIt = fr->coeffs + (i-rect.y()) * fr->size * fr->depth;
00102         while( ! srcIt.isDone() )
00103         {
00104             Q_UINT8* v1 = srcIt.rawData();
00105             for( int k = 0; k < depth; k++)
00106             {
00107                 *dstIt = f[k](v1, cis[k]->pos());
00108                 ++dstIt;
00109             }
00110             ++srcIt;
00111         }
00112     }
00113 }
00114 
00115 void KisMathToolbox::transformFromFR(KisPaintDeviceSP dst, KisFloatRepresentation* fr, const QRect& rect)
00116 {
00117     Q_INT32 depth = dst->colorSpace()->nColorChannels();
00118     QMemArray<PtrFromDouble> f(depth);
00119     QValueVector<KisChannelInfo *> cis = dst->colorSpace()->channels();
00120     for(Q_INT32 k = 0; k < depth; k++)
00121     {
00122         switch( cis[k]->channelValueType() )
00123         {
00124             case KisChannelInfo::UINT8:
00125                 f[k] = fromDouble<Q_UINT8>;
00126                 break;
00127             case KisChannelInfo::UINT16:
00128                 f[k] = fromDouble<Q_UINT16>;
00129                 break;
00130 #ifdef HAVE_OPENEXR
00131             case KisChannelInfo::FLOAT16:
00132                 f[k] = fromDouble<half>;
00133                 break;
00134 #endif      
00135             case KisChannelInfo::FLOAT32:
00136                 f[k] = fromDouble<float>;
00137                 break;
00138             case KisChannelInfo::INT8:
00139                 f[k] = fromDouble<Q_INT8>;
00140                 break;
00141             case KisChannelInfo::INT16:
00142                 f[k] = fromDouble<Q_INT16>;
00143                 break;
00144             default:
00145                 kdWarning() << "Unsupported value type in KisMathToolbox" << endl;
00146                 return;
00147         }
00148     }
00149     for(int i = rect.y(); i < rect.height(); i++)
00150     {
00151         KisHLineIteratorPixel dstIt = dst->createHLineIterator(rect.x(), i, rect.width(), true );
00152         float *srcIt = fr->coeffs + (i-rect.y()) * fr->size * fr->depth;
00153         while( ! dstIt.isDone() )
00154         {
00155             Q_UINT8* v1 = dstIt.rawData();
00156             for( int k = 0; k < depth; k++)
00157             {
00158                 f[k](v1, cis[k]->pos(), *srcIt);
00159                 ++srcIt;
00160             }
00161             ++dstIt;
00162         }
00163     }
00164 }
00165 
00166 #include "kis_math_toolbox.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys