44 img = genRGBAVolumeT<unsigned char,IT_UNSIGNED_BYTE>(data,trfunc,light_dir,alpha_from_data);
47 img = genRGBAVolumeT<unsigned short,IT_UNSIGNED_SHORT>(data,trfunc,light_dir,alpha_from_data);
50 img = genRGBAVolumeT<float,IT_FLOAT>(data,trfunc,light_dir,alpha_from_data);
52 Log::error(
"genRGBAVolume() called with non supported data type().\n");
62 img = genRGBAVolumeT<unsigned char,IT_UNSIGNED_BYTE>(data,trfunc,alpha_from_data);
65 img = genRGBAVolumeT<unsigned short,IT_UNSIGNED_SHORT>(data,trfunc,alpha_from_data);
68 img = genRGBAVolumeT<float,IT_FLOAT>(data,trfunc,alpha_from_data);
70 Log::error(
"genRGBAVolume() called with non supported data type().\n");
75 template<
typename data_type, EImageType img_type>
82 Log::error(
"genRGBAVolume() called with non IF_LUMINANCE data format().\n");
85 if (data->
type() != img_type)
87 Log::error(
"genRGBAVolume() called with invalid data type().\n");
92 Log::error(
"genRGBAVolume() called with non 3D data.\n");
97 Log::error(
"genRGBAVolume() transfer function image must be an 1D image.\n");
102 Log::error(
"genRGBAVolume() transfer function format() must be IF_RGBA.\n");
107 Log::error(
"genRGBAVolume() transfer function format() must be IT_UNSIGNED_BYTE.\n");
111 float normalizer_num = 0;
116 case IT_FLOAT: normalizer_num = 1.0f;
break;
124 int w = data->
width();
126 int d = data->
depth();
127 int pitch = data->
pitch();
128 const unsigned char* lum_px = data->
pixels();
132 for(
int z=0; z<d; ++z)
136 z1 =
clamp(z1, 0, d-1);
137 z2 =
clamp(z2, 0, d-1);
138 for(
int y=0; y<h; ++y)
142 y1 =
clamp(y1, 0, h-1);
143 y2 =
clamp(y2, 0, h-1);
144 for(
int x=0; x<w; ++x, ++rgba_px)
147 float lum = (*(data_type*)(lum_px + x*
sizeof(data_type) + y*pitch + z*pitch*h)) * normalizer_num;
149 float xval = lum*trfunc->
width();
151 if (xval > trfunc->
width()-1.001f)
152 xval = trfunc->
width()-1.001f;
156 float w21 = (float)
fract(xval);
157 float w11 = 1.0f - w21;
160 fvec4 rgba = (c11*w11 + c21*w21)*(1.0f/255.0f);
165 x1 =
clamp(x1, 0, w-1);
166 x2 =
clamp(x2, 0, w-1);
167 data_type vx1 = (*(data_type*)(lum_px + x1*
sizeof(data_type) + y *pitch + z *pitch*h));
168 data_type vx2 = (*(data_type*)(lum_px + x2*
sizeof(data_type) + y *pitch + z *pitch*h));
169 data_type vy1 = (*(data_type*)(lum_px + x *
sizeof(data_type) + y1*pitch + z *pitch*h));
170 data_type vy2 = (*(data_type*)(lum_px + x *
sizeof(data_type) + y2*pitch + z *pitch*h));
171 data_type vz1 = (*(data_type*)(lum_px + x *
sizeof(data_type) + y *pitch + z1*pitch*h));
172 data_type vz2 = (*(data_type*)(lum_px + x *
sizeof(data_type) + y *pitch + z2*pitch*h));
173 fvec3 N1(
float(vx1-vx2),
float(vy1-vy2),
float(vz1-vz2));
175 fvec3 N2 = -N1 * 0.15f;
176 float l1 =
max(
dot(N1,L),0.0f);
177 float l2 =
max(
dot(N2,L),0.0f);
178 rgba.
r() = rgba.
r()*l1 + rgba.
r()*l2+0.2f;
179 rgba.
g() = rgba.
g()*l1 + rgba.
g()*l2+0.2f;
180 rgba.
b() = rgba.
b()*l1 + rgba.
b()*l2+0.2f;
181 rgba.
r() =
clamp(rgba.
r(), 0.0f, 1.0f);
182 rgba.
g() =
clamp(rgba.
g(), 0.0f, 1.0f);
183 rgba.
b() =
clamp(rgba.
b(), 0.0f, 1.0f);
186 rgba_px->
r() = (
unsigned char)(rgba.
r()*255.0f);
187 rgba_px->
g() = (
unsigned char)(rgba.
g()*255.0f);
188 rgba_px->
b() = (
unsigned char)(rgba.
b()*255.0f);
190 rgba_px->
a() = (
unsigned char)(lum*255.0f);
192 rgba_px->
a() = (
unsigned char)(rgba.
a()*255.0f);
200 template<
typename data_type, EImageType img_type>
203 if (!trfunc || !data)
207 Log::error(
"genRGBAVolume() called with non IF_LUMINANCE data format().\n");
210 if (data->
type() != img_type)
212 Log::error(
"genRGBAVolume() called with invalid data type().\n");
217 Log::error(
"genRGBAVolume() called with non 3D data.\n");
222 Log::error(
"genRGBAVolume() transfer function image must be an 1D image.\n");
227 Log::error(
"genRGBAVolume() transfer function format() must be IF_RGBA.\n");
232 Log::error(
"genRGBAVolume() transfer function format() must be IT_UNSIGNED_BYTE.\n");
236 float normalizer_num = 0;
241 case IT_FLOAT: normalizer_num = 1.0f;
break;
247 int w = data->
width();
249 int d = data->
depth();
250 int pitch = data->
pitch();
251 const unsigned char* lum_px = data->
pixels();
255 for(
int z=0; z<d; ++z)
259 z1 =
clamp(z1, 0, d-1);
260 z2 =
clamp(z2, 0, d-1);
261 for(
int y=0; y<h; ++y)
265 y1 =
clamp(y1, 0, h-1);
266 y2 =
clamp(y2, 0, h-1);
267 for(
int x=0; x<w; ++x, ++rgba_px)
270 float lum = (*(data_type*)(lum_px + x*
sizeof(data_type) + y*pitch + z*pitch*h)) * normalizer_num;
272 float xval = lum*trfunc->
width();
274 if (xval > trfunc->
width()-1.001f)
275 xval = trfunc->
width()-1.001f;
279 float w21 = (float)
fract(xval);
280 float w11 = 1.0f - w21;
283 fvec4 rgba = (c11*w11 + c21*w21)*(1.0f/255.0f);
286 rgba_px->
r() = (
unsigned char)(rgba.
r()*255.0f);
287 rgba_px->
g() = (
unsigned char)(rgba.
g()*255.0f);
288 rgba_px->
b() = (
unsigned char)(rgba.
b()*255.0f);
290 rgba_px->
a() = (
unsigned char)(lum*255.0f);
292 rgba_px->
a() = (
unsigned char)(rgba.
a()*255.0f);
307 float* src_px = (
float*)img->
pixels();
309 int w = img->
width();
313 for(
int z=0; z<gradient->
depth(); ++z)
315 for(
int y=0; y<gradient->
height(); ++y)
317 for(
int x=0; x<gradient->
width(); ++x)
320 int xp = x+1, xn = x-1;
321 int yp = y+1, yn = y-1;
322 int zp = z+1, zn = z-1;
330 A.
x() = src_px[xn + w*y + w*h*z];
331 B.
x() = src_px[xp + w*y + w*h*z];
332 A.
y() = src_px[x + w*yn + w*h*z];
333 B.
y() = src_px[x + w*yp + w*h*z];
334 A.
z() = src_px[x + w*y + w*h*zn];
335 B.
z() = src_px[x + w*y + w*h*zp];
338 dst_px[x + w*y + w*h*z] = (
normalize(A - B) * 0.5f + 0.5f);
352 for(
int z=0; z<gradient->
depth(); ++z)
354 for(
int y=0; y<gradient->
height(); ++y)
356 for(
int x=0; x<gradient->
width(); ++x)
359 int xp = x+1, xn = x-1;
360 int yp = y+1, yn = y-1;
361 int zp = z+1, zn = z-1;
VLVOLUME_EXPORT ref< Image > genGradientNormals(const Image *data)
Generates an image whose RGB components represent the normals computed from the input image gradient ...
float clamp(float x, float minval, float maxval)
const Vector3 & normalize(T_Scalar *len=NULL)
Vector4< float > fvec4
A 4 components vector with float precision.
const unsigned char * pixels() const
Raw pointer to pixels.
const T_Scalar & z() const
ref< Image > convertType(EImageType new_type) const
Converts the type() of an image.
const T_Scalar & r() const
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
The Vector4 class is a template class that implements a generic 4 components vector, see also vl::fvec4, vl::dvec4, vl::uvec4, vl::ivec4, vl::svec4, vl::usvec4, vl::bvec4, vl::ubvec4.
VLVOLUME_EXPORT ref< Image > genRGBAVolumeT(const Image *data, const Image *trfunc, const fvec3 &light_dir, bool alpha_from_data)
Internally used.
void allocate3D(int x, int y, int z, int bytealign, EImageFormat format, EImageType type)
Visualization Library main namespace.
float dot(float a, float b)
VLVOLUME_EXPORT ref< Image > genRGBAVolume(const Image *data, const Image *trfunc, const fvec3 &light_dir, bool alpha_from_data=true)
Generates an RGBA image based on the given data source and transfer function.
const T_Scalar & g() const
float max(float a, float b)
EImageDimension dimension() const
Implements the OpenGL Shading Language convenience functions for scalar and vector operations...
const T_Scalar & y() const
const T_Scalar & b() const
ref< Image > convertFormat(EImageFormat new_format) const
Converts the format() of an image.
const T_Scalar & x() const
Implements a generic 1d, 2d, 3d and cubemap image that can have mipmaps.
The ref<> class is used to reference-count an Object.
fvec4 sample(int x, int y=0, int z=0) const
Returns the color associated to the specified pixel.
EImageFormat format() const
const T_Scalar & a() const