15 #include "texturePeeker.h"
27 TexturePeeker(
Texture *tex, Texture::CData *cdata) {
28 if (cdata->_texture_type == Texture::TT_cube_map) {
39 if (tex->do_has_ram_image(cdata) && cdata->_ram_image_compression == Texture::CM_off) {
41 _image = tex->do_get_ram_image(cdata);
42 _x_size = cdata->_x_size;
43 _y_size = cdata->_y_size;
44 _z_size = cdata->_z_size;
45 _component_width = cdata->_component_width;
46 _num_components = cdata->_num_components;
47 _format = cdata->_format;
48 _component_type = cdata->_component_type;
50 }
else if (!cdata->_simple_ram_image._image.empty()) {
52 _image = cdata->_simple_ram_image._image;
53 _x_size = cdata->_simple_x_size;
54 _y_size = cdata->_simple_y_size;
59 _format = Texture::F_rgba;
60 _component_type = Texture::T_unsigned_byte;
64 _image = tex->do_get_uncompressed_ram_image(cdata);
65 _x_size = cdata->_x_size;
66 _y_size = cdata->_y_size;
67 _z_size = cdata->_z_size;
68 _component_width = cdata->_component_width;
69 _num_components = cdata->_num_components;
70 _format = cdata->_format;
71 _component_type = cdata->_component_type;
78 _pixel_width = _component_width * _num_components;
80 switch (_component_type) {
81 case Texture::T_unsigned_byte:
82 _get_component = Texture::get_unsigned_byte;
85 case Texture::T_unsigned_short:
86 _get_component = Texture::get_unsigned_short;
96 case Texture::F_depth_stencil:
97 case Texture::F_depth_component:
100 _get_texel = get_texel_r;
103 case Texture::F_green:
104 _get_texel = get_texel_g;
107 case Texture::F_blue:
108 _get_texel = get_texel_b;
111 case Texture::F_alpha:
112 _get_texel = get_texel_a;
115 case Texture::F_luminance:
116 _get_texel = get_texel_l;
119 case Texture::F_luminance_alpha:
120 case Texture::F_luminance_alphamask:
121 _get_texel = get_texel_la;
125 case Texture::F_rgb5:
126 case Texture::F_rgb8:
127 case Texture::F_rgb12:
128 case Texture::F_rgb332:
129 _get_texel = get_texel_rgb;
132 case Texture::F_rgba:
133 case Texture::F_rgbm:
134 case Texture::F_rgba4:
135 case Texture::F_rgba5:
136 case Texture::F_rgba8:
137 case Texture::F_rgba12:
138 case Texture::F_rgba16:
139 case Texture::F_rgba32:
140 _get_texel = get_texel_rgba;
164 int x = int((u - cfloor(u)) * (PN_stdfloat)_x_size) % _x_size;
165 int y = int((v - cfloor(v)) * (PN_stdfloat)_y_size) % _y_size;
167 nassertv(x >= 0 && x < _x_size && y >= 0 && y < _y_size);
168 const unsigned char *p = _image.
p() + (y * _x_size + x) * _pixel_width;
170 (*_get_texel)(color, p, _get_component);
187 int x = int((u - cfloor(u)) * (PN_stdfloat)_x_size) % _x_size;
188 int y = int((v - cfloor(v)) * (PN_stdfloat)_y_size) % _y_size;
189 int z = int((w - cfloor(w)) * (PN_stdfloat)_z_size) % _z_size;
191 nassertv(x >= 0 && x < _x_size && y >= 0 && y < _y_size &&
192 z >= 0 && z < _z_size);
193 const unsigned char *p = _image.
p() + (z * _x_size * _y_size + y * _x_size + x) * _pixel_width;
195 (*_get_texel)(color, p, _get_component);
211 PN_stdfloat min_u, PN_stdfloat min_v, PN_stdfloat max_u, PN_stdfloat max_v)
const {
213 init_rect_minmax(min_x, max_x, min_u, max_u, _x_size);
216 init_rect_minmax(min_y, max_y, min_v, max_v, _y_size);
218 color.set(0.0f, 0.0f, 0.0f, 0.0f);
219 PN_stdfloat net = 0.0f;
220 accum_filter_y(color, net, 0,
221 min_x, max_x, min_u, max_u,
222 min_y, max_y, min_v, max_v,
243 PN_stdfloat min_u, PN_stdfloat min_v, PN_stdfloat min_w,
244 PN_stdfloat max_u, PN_stdfloat max_v, PN_stdfloat max_w)
const {
246 init_rect_minmax(min_x, max_x, min_u, max_u, _x_size);
249 init_rect_minmax(min_y, max_y, min_v, max_v, _y_size);
252 init_rect_minmax(min_z, max_z, min_w, max_w, _z_size);
254 color.set(0.0f, 0.0f, 0.0f, 0.0f);
255 PN_stdfloat net = 0.0f;
256 accum_filter_z(color, net,
257 min_x, max_x, min_u, max_u,
258 min_y, max_y, min_v, max_v,
259 min_z, max_z, min_w, max_w);
273 init_rect_minmax(
int &min_x,
int &max_x, PN_stdfloat &min_u, PN_stdfloat &max_u,
276 PN_stdfloat t = min_u;
280 if (max_u - min_u >= 1.0f) {
284 min_x = (int)cfloor(min_u * (PN_stdfloat)x_size);
285 max_x = (int)cceil(max_u * (PN_stdfloat)x_size);
286 nassertv(min_x <= max_x);
296 int min_x,
int max_x, PN_stdfloat min_u, PN_stdfloat max_u,
297 int min_y,
int max_y, PN_stdfloat min_v, PN_stdfloat max_v,
298 int min_z,
int max_z, PN_stdfloat min_w, PN_stdfloat max_w)
const {
299 nassertv(min_z >= 0 && min_z <= _z_size &&
300 max_z >= 0 && max_z <= _z_size);
303 if (min_z >= max_z - 1) {
305 accum_filter_y(color, net, zi % _z_size,
306 min_x, max_x, min_u, max_u,
307 min_y, max_y, min_v, max_v,
312 PN_stdfloat w = (min_z + 1) - min_w * _z_size;
313 accum_filter_y(color, net, zi % _z_size,
314 min_x, max_x, min_u, max_u,
315 min_y, max_y, min_v, max_v,
322 accum_filter_y(color, net, zi % _z_size,
323 min_x, max_x, min_u, max_u,
324 min_y, max_y, min_v, max_v,
330 w = max_w * _z_size - (max_z - 1);
331 accum_filter_y(color, net, zi % _z_size,
332 min_x, max_x, min_u, max_u,
333 min_y, max_y, min_v, max_v,
344 accum_filter_y(
LColor &color, PN_stdfloat &net,
int zi,
345 int min_x,
int max_x, PN_stdfloat min_u, PN_stdfloat max_u,
346 int min_y,
int max_y, PN_stdfloat min_v, PN_stdfloat max_v,
347 PN_stdfloat weight)
const {
348 nassertv(zi >= 0 && zi < _z_size);
349 nassertv(min_y >= 0 && min_y <= _y_size &&
350 max_y >= 0 && max_y <= _y_size);
353 if (min_y >= max_y - 1) {
355 accum_filter_x(color, net, yi % _y_size, zi, min_x, max_x, min_u, max_u, weight);
359 PN_stdfloat w = (min_y + 1) - min_v * _y_size;
360 accum_filter_x(color, net, yi % _y_size, zi, min_x, max_x, min_u, max_u, weight * w);
366 accum_filter_x(color, net, yi % _y_size, zi, min_x, max_x, min_u, max_u, weight);
371 w = max_v * _y_size - (max_y - 1);
372 accum_filter_x(color, net, yi % _y_size, zi, min_x, max_x, min_u, max_u, weight * w);
382 accum_filter_x(
LColor &color, PN_stdfloat &net,
int yi,
int zi,
383 int min_x,
int max_x, PN_stdfloat min_u, PN_stdfloat max_u,
384 PN_stdfloat weight)
const {
385 nassertv(yi >= 0 && yi < _y_size && zi >= 0 && zi < _z_size);
386 nassertv(min_x >= 0 && min_x <= _x_size &&
387 max_x >= 0 && max_x <= _x_size);
390 int xi = min_x % _x_size;
391 const unsigned char *p = _image.
p() + (zi * _x_size * _y_size + yi * _x_size + xi) * _pixel_width;
393 if (min_x >= max_x - 1) {
395 accum_texel(color, net, p, weight);
399 PN_stdfloat w = (min_x + 1) - min_u * _x_size;
400 accum_texel(color, net, p, weight * w);
408 p = _image.
p() + (zi * _x_size * _y_size + yi * _x_size + xi) * _pixel_width;
411 accum_texel(color, net, p, weight);
418 p = _image.
p() + (zi * _x_size * _y_size + yi * _x_size + xi) * _pixel_width;
420 w = max_u * _x_size - (max_x - 1);
421 accum_texel(color, net, p, weight * w);
432 accum_texel(
LColor &color, PN_stdfloat &net,
const unsigned char *&p, PN_stdfloat weight)
const {
434 (*_get_texel)(c, p, _get_component);
446 get_texel_r(
LColor &color,
const unsigned char *&p, GetComponentFunc *get_component) {
447 color[0] = (*get_component)(p);
460 get_texel_g(
LColor &color,
const unsigned char *&p, GetComponentFunc *get_component) {
462 color[1] = (*get_component)(p);
474 get_texel_b(
LColor &color,
const unsigned char *&p, GetComponentFunc *get_component) {
477 color[2] = (*get_component)(p);
488 get_texel_a(
LColor &color,
const unsigned char *&p, GetComponentFunc *get_component) {
492 color[3] = (*get_component)(p);
502 get_texel_l(
LColor &color,
const unsigned char *&p, GetComponentFunc *get_component) {
503 color[0] = (*get_component)(p);
516 get_texel_la(
LColor &color,
const unsigned char *&p, GetComponentFunc *get_component) {
517 color[0] = (*get_component)(p);
520 color[3] = (*get_component)(p);
530 get_texel_rgb(
LColor &color,
const unsigned char *&p, GetComponentFunc *get_component) {
531 color[2] = (*get_component)(p);
532 color[1] = (*get_component)(p);
533 color[0] = (*get_component)(p);
544 get_texel_rgba(
LColor &color,
const unsigned char *&p, GetComponentFunc *get_component) {
545 color[2] = (*get_component)(p);
546 color[1] = (*get_component)(p);
547 color[0] = (*get_component)(p);
548 color[3] = (*get_component)(p);
const Element * p() const
Function p() is similar to the function from ConstPointerTo.
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
void clear()
To empty the PTA, use the clear() method, since assignment to NULL is problematic (given the ambiguit...
void filter_rect(LColor &color, PN_stdfloat min_u, PN_stdfloat min_v, PN_stdfloat max_u, PN_stdfloat max_v) const
Fills "color" with the average RGBA color of the texels within the rectangle defined by the specified...
This is the base class for all three-component vectors and points.
bool is_null() const
Returns true if the PointerTo is a NULL pointer, false otherwise.
void lookup(LColor &color, PN_stdfloat u, PN_stdfloat v) const
Fills "color" with the RGBA color of the texel at point (u, v).