Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ImagePyramid.cpp « image « intern « freestyle « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b81f8303945365cdd67bc7090abd5ad7270857e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * ***** 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.
 *
 * ***** 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
 */

#include <iostream>

#include "GaussianFilter.h"
#include "Image.h"
#include "ImagePyramid.h"

using namespace std;

namespace Freestyle {

#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();
	}
}

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.0f / (float)(1 << (2 * level))) * (C * P1 + D * P2);
}

int ImagePyramid::width(int level)
{
	return _levels[level]->width();
}

int ImagePyramid::height(int level)
{
	return _levels[level]->height();
}

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(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(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;
		}
	}
}

} /* namespace Freestyle */