00001 // Filename: pixelBuffer.I 00002 // Created by: drose (05Feb99) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 00020 //////////////////////////////////////////////////////////////////// 00021 // Function: rgb_buffer 00022 // Access: Public 00023 // Description: Constructs a PixelBuffer suitable for RGB 00024 //////////////////////////////////////////////////////////////////// 00025 INLINE PixelBuffer PixelBuffer:: 00026 rgb_buffer(int xsize, int ysize) { 00027 return PixelBuffer(xsize, ysize, 3, sizeof(uchar), T_unsigned_byte, 00028 F_rgb); 00029 } 00030 00031 //////////////////////////////////////////////////////////////////// 00032 // Function: rgba_buffer 00033 // Access: Public 00034 // Description: Constructs a PixelBuffer suitable for RGBA 00035 //////////////////////////////////////////////////////////////////// 00036 INLINE PixelBuffer PixelBuffer:: 00037 rgba_buffer(int xsize, int ysize) { 00038 return PixelBuffer(xsize, ysize, 4, sizeof(uchar), T_unsigned_byte, 00039 F_rgba); 00040 } 00041 00042 //////////////////////////////////////////////////////////////////// 00043 // Function: depth_buffer 00044 // Access: Public 00045 // Description: Constructs a PixelBuffer suitable for depth maps 00046 //////////////////////////////////////////////////////////////////// 00047 INLINE PixelBuffer PixelBuffer:: 00048 depth_buffer(int xsize, int ysize) { 00049 return PixelBuffer(xsize, ysize, 1, sizeof(float), T_float, 00050 F_depth_component); 00051 } 00052 00053 //////////////////////////////////////////////////////////////////// 00054 // Function: stencil_buffer 00055 // Access: Public 00056 // Description: Constructs a PixelBuffer suitable for stencil buffers 00057 //////////////////////////////////////////////////////////////////// 00058 INLINE PixelBuffer PixelBuffer:: 00059 stencil_buffer(int xsize, int ysize) { 00060 return PixelBuffer(xsize, ysize, 1, sizeof(uchar), T_unsigned_byte, 00061 F_stencil_index); 00062 } 00063 00064 //////////////////////////////////////////////////////////////////// 00065 // Function: PixelBuffer::Destructor 00066 // Access: Public 00067 // Description: 00068 //////////////////////////////////////////////////////////////////// 00069 INLINE PixelBuffer:: 00070 ~PixelBuffer(void) { 00071 } 00072 00073 00074 INLINE void PixelBuffer:: 00075 set_size(int x_org, int y_org, int x_size, int y_size) { 00076 if ((_xsize != x_size) || (_ysize != y_size) || 00077 (_xorg != x_org) || (_yorg != y_org)) { 00078 make_dirty(); 00079 } 00080 00081 _xsize = x_size; 00082 _ysize = y_size; 00083 _xorg = x_org; 00084 _yorg = y_org; 00085 } 00086 00087 //////////////////////////////////////////////////////////////////// 00088 // Function: PixelBuffer::set_xsize 00089 // Access: 00090 // Description: 00091 //////////////////////////////////////////////////////////////////// 00092 INLINE void PixelBuffer::set_xsize(int size) 00093 { 00094 if (_xsize != size) { 00095 _xsize = size; 00096 make_dirty(); 00097 } 00098 } 00099 00100 //////////////////////////////////////////////////////////////////// 00101 // Function: PixelBuffer::set_ysize 00102 // Access: 00103 // Description: 00104 //////////////////////////////////////////////////////////////////// 00105 INLINE void PixelBuffer::set_ysize(int size) 00106 { 00107 if (_ysize != size) { 00108 _ysize = size; 00109 make_dirty(); 00110 } 00111 } 00112 00113 //////////////////////////////////////////////////////////////////// 00114 // Function: PixelBuffer::set_xorg 00115 // Access: 00116 // Description: 00117 //////////////////////////////////////////////////////////////////// 00118 INLINE void PixelBuffer::set_xorg(int org) 00119 { 00120 if (_xorg != org) { 00121 _xorg = org; 00122 make_dirty(); 00123 } 00124 } 00125 00126 //////////////////////////////////////////////////////////////////// 00127 // Function: PixelBuffer::set_yorg 00128 // Access: 00129 // Description: 00130 //////////////////////////////////////////////////////////////////// 00131 INLINE void PixelBuffer::set_yorg(int org) 00132 { 00133 if (_yorg != org) { 00134 _yorg = org; 00135 make_dirty(); 00136 } 00137 } 00138 00139 //////////////////////////////////////////////////////////////////// 00140 // Function: PixelBuffer::set_format 00141 // Access: Public 00142 // Description: 00143 //////////////////////////////////////////////////////////////////// 00144 INLINE void PixelBuffer:: 00145 set_format(PixelBuffer::Format format) 00146 { 00147 if (_format != format) { 00148 _format = format; 00149 make_dirty(); 00150 } 00151 } 00152 00153 //////////////////////////////////////////////////////////////////// 00154 // Function: PixelBuffer::get_xsize 00155 // Access: Public 00156 // Description: 00157 //////////////////////////////////////////////////////////////////// 00158 INLINE int PixelBuffer:: 00159 get_xsize() const { 00160 return _xsize; 00161 } 00162 00163 //////////////////////////////////////////////////////////////////// 00164 // Function: PixelBuffer::get_ysize 00165 // Access: Public 00166 // Description: 00167 //////////////////////////////////////////////////////////////////// 00168 INLINE int PixelBuffer:: 00169 get_ysize() const { 00170 return _ysize; 00171 } 00172 00173 00174 //////////////////////////////////////////////////////////////////// 00175 // Function: PixelBuffer::get_xorg 00176 // Access: Public 00177 // Description: 00178 //////////////////////////////////////////////////////////////////// 00179 INLINE int PixelBuffer:: 00180 get_xorg() const { 00181 return _xorg; 00182 } 00183 00184 //////////////////////////////////////////////////////////////////// 00185 // Function: PixelBuffer::get_yorg 00186 // Access: Public 00187 // Description: 00188 //////////////////////////////////////////////////////////////////// 00189 INLINE int PixelBuffer:: 00190 get_yorg() const { 00191 return _yorg; 00192 } 00193 00194 //////////////////////////////////////////////////////////////////// 00195 // Function: PixelBuffer::get_border 00196 // Access: Public 00197 // Description: 00198 //////////////////////////////////////////////////////////////////// 00199 INLINE int PixelBuffer:: 00200 get_border() const { 00201 return _border; 00202 } 00203 00204 //////////////////////////////////////////////////////////////////// 00205 // Function: PixelBuffer::get_num_components 00206 // Access: Public 00207 // Description: 00208 //////////////////////////////////////////////////////////////////// 00209 INLINE int PixelBuffer:: 00210 get_num_components() const { 00211 return _components; 00212 } 00213 00214 //////////////////////////////////////////////////////////////////// 00215 // Function: PixelBuffer::get_component_width 00216 // Access: Public 00217 // Description: 00218 //////////////////////////////////////////////////////////////////// 00219 INLINE int PixelBuffer:: 00220 get_component_width() const { 00221 return _component_width; 00222 } 00223 00224 //////////////////////////////////////////////////////////////////// 00225 // Function: PixelBuffer::get_format 00226 // Access: Public 00227 // Description: 00228 //////////////////////////////////////////////////////////////////// 00229 INLINE PixelBuffer::Format PixelBuffer:: 00230 get_format() const { 00231 return _format; 00232 } 00233 00234 //////////////////////////////////////////////////////////////////// 00235 // Function: PixelBuffer::get_image_type 00236 // Access: Public 00237 // Description: 00238 //////////////////////////////////////////////////////////////////// 00239 INLINE PixelBuffer::Type PixelBuffer:: 00240 get_image_type() const { 00241 return _type; 00242 } 00243 00244 //////////////////////////////////////////////////////////////////// 00245 // Function: PixelBuffer::set_uchar_rgb_texel 00246 // Access: Public 00247 // Description: This is only valid when the PixelBuffer is an RGB 00248 // buffer with uchar components. 00249 //////////////////////////////////////////////////////////////////// 00250 INLINE void PixelBuffer:: 00251 set_uchar_rgb_texel(const uchar color[3], int x, int y, int width) 00252 { 00253 int i = y * 3 * width + x * 3; 00254 _image[i] = color[0]; 00255 _image[i+1] = color[1]; 00256 _image[i+2] = color[2]; 00257 } 00258 00259 //////////////////////////////////////////////////////////////////// 00260 // Function: PixelBuffer::store_unscaled_byte 00261 // Access: Private 00262 // Description: This is used by load() to store the next consecutive 00263 // component value into the indicated element of the 00264 // array, which is taken to be an array of unsigned 00265 // bytes. The value is assumed to be in the range 00266 // 0-255. 00267 //////////////////////////////////////////////////////////////////// 00268 INLINE void PixelBuffer:: 00269 store_unscaled_byte(int &index, int value) { 00270 _image[index++] = (uchar)value; 00271 } 00272 00273 //////////////////////////////////////////////////////////////////// 00274 // Function: PixelBuffer::store_unscaled_short 00275 // Access: Private 00276 // Description: This is used by load() to store the next consecutive 00277 // component value into the indicated element of the 00278 // array, which is taken to be an array of unsigned 00279 // shorts. The value is assumed to be in the range 00280 // 0-65535. 00281 //////////////////////////////////////////////////////////////////// 00282 INLINE void PixelBuffer:: 00283 store_unscaled_short(int &index, int value) { 00284 union { 00285 ushort us; 00286 uchar uc[2]; 00287 } v; 00288 v.us = (ushort)value; 00289 _image[index++] = v.uc[0]; 00290 _image[index++] = v.uc[1]; 00291 } 00292 00293 //////////////////////////////////////////////////////////////////// 00294 // Function: PixelBuffer::store_scaled_byte 00295 // Access: Private 00296 // Description: This is used by load() to store the next consecutive 00297 // component value into the indicated element of the 00298 // array, which is taken to be an array of unsigned 00299 // bytes. The value will be scaled by the indicated 00300 // factor before storing it. 00301 //////////////////////////////////////////////////////////////////// 00302 INLINE void PixelBuffer:: 00303 store_scaled_byte(int &index, int value, double scale) { 00304 store_unscaled_byte(index, (int)(value * scale)); 00305 } 00306 00307 //////////////////////////////////////////////////////////////////// 00308 // Function: PixelBuffer::store_scaled_short 00309 // Access: Private 00310 // Description: This is used by load() to store the next consecutive 00311 // component value into the indicated element of the 00312 // array, which is taken to be an array of unsigned 00313 // shorts. The value will be scaled by the indicated 00314 // factor before storing it. 00315 //////////////////////////////////////////////////////////////////// 00316 INLINE void PixelBuffer:: 00317 store_scaled_short(int &index, int value, double scale) { 00318 store_unscaled_short(index, (int)(value * scale)); 00319 } 00320 00321 //////////////////////////////////////////////////////////////////// 00322 // Function: PixelBuffer::get_unsigned_byte 00323 // Access: Private 00324 // Description: This is used by store() to retieve the next 00325 // consecutive component value from the indicated 00326 // element of the array, which is taken to be an array 00327 // of unsigned bytes. 00328 //////////////////////////////////////////////////////////////////// 00329 INLINE double PixelBuffer:: 00330 get_unsigned_byte(int &index) const { 00331 nassertr(index >= 0 && index < (int)_image.size(), 0.0); 00332 return (double)_image[index++] / 255.0; 00333 } 00334 00335 //////////////////////////////////////////////////////////////////// 00336 // Function: PixelBuffer::get_unsigned_short 00337 // Access: Private 00338 // Description: This is used by store() to retieve the next 00339 // consecutive component value from the indicated 00340 // element of the array, which is taken to be an array 00341 // of unsigned shorts. 00342 //////////////////////////////////////////////////////////////////// 00343 INLINE double PixelBuffer:: 00344 get_unsigned_short(int &index) const { 00345 nassertr(index >= 0 && index+1 < (int)_image.size(), 0.0); 00346 union { 00347 ushort us; 00348 uchar uc[2]; 00349 } v; 00350 v.uc[0] = _image[index++]; 00351 v.uc[1] = _image[index++]; 00352 return (double)v.us / 65535.0; 00353 }