diff options
author | Tamito Kajiyama <rd6t-kjym@asahi-net.or.jp> | 2012-12-22 22:25:01 +0400 |
---|---|---|
committer | Tamito Kajiyama <rd6t-kjym@asahi-net.or.jp> | 2012-12-22 22:25:01 +0400 |
commit | fa0211df269a3398dd70467982f9e129c79e501b (patch) | |
tree | 404ee267890602b49470cb640986b50d2c2055c1 /source/blender/freestyle/intern/image/ImagePyramid.cpp | |
parent | 8b57a67f3eb57366c2b3abcb8f3b04403d339e1a (diff) |
Another "insanely" big code clean-up patch by Bastien Montagne, many thanks!
Diffstat (limited to 'source/blender/freestyle/intern/image/ImagePyramid.cpp')
-rw-r--r-- | source/blender/freestyle/intern/image/ImagePyramid.cpp | 310 |
1 files changed, 168 insertions, 142 deletions
diff --git a/source/blender/freestyle/intern/image/ImagePyramid.cpp b/source/blender/freestyle/intern/image/ImagePyramid.cpp index 542ab7917e3..c7616fbce23 100644 --- a/source/blender/freestyle/intern/image/ImagePyramid.cpp +++ b/source/blender/freestyle/intern/image/ImagePyramid.cpp @@ -1,166 +1,192 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2010 Blender Foundation. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file blender/freestyle/intern/image/ImagePyramid.cpp + * \ingroup freestyle + * \brief Class to represent a pyramid of images + * \author Stephane Grabli + * \date 25/12/2003 + */ -// -// Copyright (C) : Please refer to the COPYRIGHT file distributed -// with this source distribution. -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -// -/////////////////////////////////////////////////////////////////////////////// -#include "ImagePyramid.h" -#include "Image.h" -#include "GaussianFilter.h" #include <iostream> +#include "GaussianFilter.h" +#include "Image.h" +#include "ImagePyramid.h" + using namespace std; -//ImagePyramid::ImagePyramid(const GrayImage& level0, unsigned nbLevels){ -// //BuildPyramid(level0,nbLevels); -//} - -ImagePyramid::ImagePyramid(const ImagePyramid& iBrother){ - if(!_levels.empty()){ - for(vector<GrayImage*>::iterator im=_levels.begin(), imend=_levels.end(); - im!=imend; - ++im){ - _levels.push_back(new GrayImage(**im)); - } - } +#if 0 +ImagePyramid::ImagePyramid(const GrayImage& level0, unsigned nbLevels) +{ + BuildPyramid(level0,nbLevels); +} +#endif + +ImagePyramid::ImagePyramid(const ImagePyramid& iBrother) +{ + if (!_levels.empty()) { + for (vector<GrayImage*>::iterator im = _levels.begin(), imend = _levels.end(); im != imend; ++im) { + _levels.push_back(new GrayImage(**im)); + } + } } -ImagePyramid::~ImagePyramid(){ - if(!_levels.empty()){ - for(vector<GrayImage*>::iterator im=_levels.begin(), imend=_levels.end(); - im!=imend; - ++im){ - delete (*im); - } - _levels.clear(); - } + +ImagePyramid::~ImagePyramid() +{ + if (!_levels.empty()) { + for (vector<GrayImage*>::iterator im = _levels.begin(), imend = _levels.end(); im != imend; ++im) { + delete (*im); + } + _levels.clear(); + } } -GrayImage * ImagePyramid::getLevel(int l){ - return _levels[l]; +GrayImage * ImagePyramid::getLevel(int l) +{ + return _levels[l]; } -float ImagePyramid::pixel(int x, int y, int level){ - GrayImage *img = _levels[level]; - if(0 == level){ - return img->pixel(x,y); - } - unsigned int i = 1<<level; - unsigned int sx = x>>level; - unsigned int sy = y>>level; - if(sx >= img->width()) - sx = img->width()-1; - if(sy >= img->height()) - sy = img->height()-1; - - // bilinear interpolation - float A = i*(sx+1)-x; - float B = x-i*sx; - float C = i*(sy+1)-y; - float D = y-i*sy; - - float P1(0), P2(0); - P1 = A*img->pixel(sx,sy); - if(sx < img->width()-1){ - if(x%i != 0) - P1 += B*img->pixel(sx+1,sy); - }else{ - P1 += B*img->pixel(sx,sy); - } - if(sy<img->height()-1){ - if(y%i != 0){ - P2 = A*img->pixel(sx,sy+1); - if(sx < img->width()-1){ - if(x%i != 0) - P2 += B*img->pixel(sx+1,sy+1); - }else{ - P2 += B*img->pixel(sx,sy+1); - } - } - }else{ - P2 = P1; - } - return (1.f/(float)(1<<2*level))*(C*P1 + D*P2); +float ImagePyramid::pixel(int x, int y, int level) +{ + GrayImage *img = _levels[level]; + if (0 == level) { + return img->pixel(x, y); + } + unsigned int i = 1 << level; + unsigned int sx = x >> level; + unsigned int sy = y >> level; + if (sx >= img->width()) + sx = img->width() - 1; + if (sy >= img->height()) + sy = img->height() - 1; + + // bilinear interpolation + float A = i * (sx + 1) - x; + float B = x - i * sx; + float C = i * (sy + 1) - y; + float D = y - i * sy; + + float P1(0), P2(0); + P1 = A * img->pixel(sx, sy); + if (sx < img->width() - 1) { + if (x % i != 0) + P1 += B * img->pixel(sx + 1, sy); + } + else { + P1 += B * img->pixel(sx, sy); + } + if (sy < img->height() - 1) { + if (y % i != 0) { + P2 = A * img->pixel(sx, sy + 1); + if (sx < img->width() - 1) { + if (x % i != 0) + P2 += B * img->pixel(sx + 1, sy + 1); + } + else { + P2 += B * img->pixel(sx, sy + 1); + } + } + } + else { + P2 = P1; + } + return (1.0f / (float)(1 << (2 * level))) * (C * P1 + D * P2); } -int ImagePyramid::width(int level){ - return _levels[level]->width(); +int ImagePyramid::width(int level) +{ + return _levels[level]->width(); } -int ImagePyramid::height(int level){ - return _levels[level]->height(); +int ImagePyramid::height(int level) +{ + return _levels[level]->height(); } -GaussianPyramid::GaussianPyramid(const GrayImage& level0, unsigned nbLevels, float iSigma) - : ImagePyramid() +GaussianPyramid::GaussianPyramid(const GrayImage& level0, unsigned nbLevels, float iSigma) : ImagePyramid() { - _sigma = iSigma; - BuildPyramid(level0,nbLevels); -} -GaussianPyramid::GaussianPyramid(GrayImage* level0, unsigned nbLevels, float iSigma) - : ImagePyramid() + _sigma = iSigma; + BuildPyramid(level0, nbLevels); +} + +GaussianPyramid::GaussianPyramid(GrayImage* level0, unsigned nbLevels, float iSigma) : ImagePyramid() { - _sigma = iSigma; - BuildPyramid(level0,nbLevels); + _sigma = iSigma; + BuildPyramid(level0, nbLevels); } -GaussianPyramid::GaussianPyramid(const GaussianPyramid& iBrother) -: ImagePyramid(iBrother){ - _sigma = iBrother._sigma; +GaussianPyramid::GaussianPyramid(const GaussianPyramid& iBrother) : ImagePyramid(iBrother) +{ + _sigma = iBrother._sigma; } -void GaussianPyramid::BuildPyramid(const GrayImage& level0, unsigned nbLevels){ - GrayImage *pLevel = new GrayImage(level0); - BuildPyramid(pLevel, nbLevels); + +void GaussianPyramid::BuildPyramid(const GrayImage& level0, unsigned nbLevels) +{ + GrayImage *pLevel = new GrayImage(level0); + BuildPyramid(pLevel, nbLevels); } -void GaussianPyramid::BuildPyramid(GrayImage* level0, unsigned nbLevels){ - GrayImage *pLevel = level0; - _levels.push_back(pLevel); - GaussianFilter gf(_sigma); - // build the nbLevels: - unsigned w = pLevel->width(); - unsigned h = pLevel->height(); - if(nbLevels!=0) - { - for(unsigned i=0; i<nbLevels; ++i){ //soc - w = pLevel->width()>>1; - h = pLevel->height()>>1; - GrayImage *img = new GrayImage(w,h); - for(unsigned y=0; y<h; ++y){ - for(unsigned x=0; x<w; ++x){ - float v = gf.getSmoothedPixel<GrayImage>(pLevel, 2*x,2*y); - img->setPixel(x,y,v); - } - } - _levels.push_back(img); - pLevel = img; - } - }else{ - while((w>1) && (h>1)){ - w = pLevel->width()>>1; - h = pLevel->height()>>1; - GrayImage *img = new GrayImage(w,h); - for(unsigned y=0; y<h; ++y){ - for(unsigned x=0; x<w; ++x){ - float v = gf.getSmoothedPixel<GrayImage>(pLevel, 2*x,2*y); - img->setPixel(x,y,v); - } - } - _levels.push_back(img); - pLevel = img; - } - } +void GaussianPyramid::BuildPyramid(GrayImage* level0, unsigned nbLevels) +{ + GrayImage *pLevel = level0; + _levels.push_back(pLevel); + GaussianFilter gf(_sigma); + // build the nbLevels: + unsigned w = pLevel->width(); + unsigned h = pLevel->height(); + if (nbLevels != 0) { + for (unsigned int i = 0; i < nbLevels; ++i) { //soc + w = pLevel->width() >> 1; + h = pLevel->height() >> 1; + GrayImage *img = new GrayImage(w, h); + for (unsigned int y = 0; y < h; ++y) { + for (unsigned int x = 0; x < w; ++x) { + float v = gf.getSmoothedPixel<GrayImage>(pLevel, 2 * x, 2 * y); + img->setPixel(x, y, v); + } + } + _levels.push_back(img); + pLevel = img; + } + } + else { + while ((w > 1) && (h > 1)) { + w = pLevel->width() >> 1; + h = pLevel->height() >> 1; + GrayImage *img = new GrayImage(w, h); + for (unsigned int y = 0; y < h; ++y) { + for (unsigned int x = 0; x < w; ++x) { + float v = gf.getSmoothedPixel<GrayImage>(pLevel, 2 * x, 2 * y); + img->setPixel(x, y, v); + } + } + _levels.push_back(img); + pLevel = img; + } + } } |