1 // Copyright NVIDIA Corporation 2007 -- Ignacio Castano <icastano@nvidia.com>
2 //
3 // Permission is hereby granted, free of charge, to any person
4 // obtaining a copy of this software and associated documentation
5 // files (the "Software"), to deal in the Software without
6 // restriction, including without limitation the rights to use,
7 // copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following
10 // conditions:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 // OTHER DEALINGS IN THE SOFTWARE.
23 
24 #include <nvcore/Ptr.h>
25 
26 #include <nvmath/Color.h>
27 
28 #include <nvimage/NormalMap.h>
29 #include <nvimage/Filter.h>
30 #include <nvimage/FloatImage.h>
31 #include <nvimage/Image.h>
32 
33 using namespace nv;
34 
35 
processPixel(const FloatImage * img,uint x,uint y)36 static float processPixel(const FloatImage * img, uint x, uint y)
37 {
38 	nvDebugCheck(img != NULL);
39 
40 	const uint w = img->width();
41 	const uint h = img->height();
42 
43 	float d = img->pixel(x, y, 0);
44 
45 	float fx0 = (float) x / w;
46 	float fy0 = (float) y / h;
47 
48 	float best_ratio = INF;
49 	uint best_x = w;
50 	uint best_y = h;
51 
52 	for (uint yy = 0; yy < h; yy++)
53 	{
54 		for (uint xx = 0; xx < w; xx++)
55 		{
56 			float ch = d - img->pixel(xx, yy, 0);
57 
58 			if (ch > 0)
59 			{
60 				float dx = float(xx - x);
61 				float dy = float(yy - y);
62 
63 				float ratio = (dx * dx + dy * dy) / ch;
64 
65 				if (ratio < best_ratio)
66 				{
67 					best_x = xx;
68 					best_y = yy;
69 				}
70 			}
71 		}
72 	}
73 
74 	if (best_x != w)
75 	{
76 		nvDebugCheck(best_y !=h);
77 
78 		float dx = float(best_x - x) / w;
79 		float dy = float(best_y - y) / h;
80 
81 		float cw = sqrtf(dx*dx + dy*dy);
82 		float ch = d - img->pixel(xx, yy, 0);
83 
84 		return min(1, sqrtf(cw / ch));
85 	}
86 
87 	return 1;
88 }
89 
90 
91 // Create cone map using the given kernels.
createConeMap(const Image * img,Vector4::Arg heightWeights)92 FloatImage * createConeMap(const Image * img, Vector4::Arg heightWeights)
93 {
94 	nvCheck(img != NULL);
95 
96 	const uint w = img->width();
97 	const uint h = img->height();
98 
99 	AutoPtr<FloatImage> fimage(new FloatImage());
100 	//fimage->allocate(2, w, h);
101 	fimage->allocate(4, w, h);
102 
103 	// Compute height and store in red channel:
104 	float * heightChannel = fimage->channel(0);
105 	for(uint i = 0; i < w*h; i++)
106 	{
107 		Vector4 color = toVector4(img->pixel(i));
108 		heightChannel[i] = dot(color, heightWeights);
109 	}
110 
111 	// Compute cones:
112 	for(uint y = 0; y < h; y++)
113 	{
114 		for(uint x = 0; x < w; x++)
115 		{
116 			processPixel(fimage, x, y);
117 		}
118 	}
119 
120 	return fimage.release();
121 }
122 
123