Blender  V2.59
FilterNormal.h
Go to the documentation of this file.
00001 /* $Id: FilterNormal.h 35082 2011-02-22 19:30:37Z jesterking $
00002 -----------------------------------------------------------------------------
00003 This source file is part of blendTex library
00004 
00005 Copyright (c) 2007 The Zdeno Ash Miklas
00006 
00007 This program is free software; you can redistribute it and/or modify it under
00008 the terms of the GNU Lesser General Public License as published by the Free Software
00009 Foundation; either version 2 of the License, or (at your option) any later
00010 version.
00011 
00012 This program is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public License along with
00017 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00018 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00019 http://www.gnu.org/copyleft/lesser.txt.
00020 -----------------------------------------------------------------------------
00021 */
00022 
00027 #if !defined FILTERNORMAL_H
00028 #define FILTERNORMAL_H
00029 
00030 #include "Common.h"
00031 
00032 #include "FilterBase.h"
00033 
00034 
00035 // scale constants for normals
00036 const float depthScaleKoef = 255.0;
00037 const float normScaleKoef = float(depthScaleKoef / 2.0);
00038 
00039 
00041 class FilterNormal : public FilterBase
00042 {
00043 public:
00045         FilterNormal (void);
00047         virtual ~FilterNormal (void) {}
00048 
00050         unsigned short getColor (void) { return m_colIdx; }
00052         void setColor (unsigned short colIdx);
00053 
00055         float getDepth (void) { return m_depth; }
00057         void setDepth (float depth);
00058 
00059 protected:
00061         float m_depth;
00063         float m_depthScale;
00064 
00066         unsigned short m_colIdx;
00067 
00069         template <class SRC> unsigned int tFilter (SRC * src, short x, short y,
00070                 short * size, unsigned int pixSize, unsigned int val = 0)
00071         {
00072                 // get value of required color
00073                 int actPix = int(VT_C(val,m_colIdx));
00074                 int upPix = actPix;
00075                 int leftPix = actPix;
00076                 // get upper and left pixel from actual pixel
00077                 if (y > 0)
00078                 {
00079                         val = convertPrevious(src - pixSize * size[0], x, y - 1, size, pixSize);
00080                         upPix = VT_C(val,m_colIdx);
00081                 }
00082                 if (x > 0)
00083                 {
00084                         val = convertPrevious(src - pixSize, x - 1, y, size, pixSize);
00085                         leftPix = VT_C(val,m_colIdx);
00086                 }
00087                 // height differences (from blue color)
00088                 float dx = (actPix - leftPix) * m_depthScale;
00089                 float dy = (actPix - upPix) * m_depthScale;
00090                 // normalize vector
00091                 float dz = float(normScaleKoef / sqrt(dx * dx + dy * dy + 1.0));
00092                 dx = dx * dz + normScaleKoef;
00093                 dy = dy * dz + normScaleKoef;
00094                 dz += normScaleKoef;
00095                 // return normal vector converted to color
00096                 VT_RGBA(val, dx, dy, dz, 0xFF);
00097                 return val;
00098         }
00099 
00101         virtual unsigned int filter (unsigned char * src, short x, short y,
00102                 short * size, unsigned int pixSize, unsigned int val = 0)
00103         { return tFilter(src, x, y, size, pixSize, val); }
00105         virtual unsigned int filter (unsigned int * src, short x, short y,
00106                 short * size, unsigned int pixSize, unsigned int val = 0)
00107         { return tFilter(src, x, y, size, pixSize, val); }
00108 };
00109 
00110 
00111 #endif