00001 /* 00002 ----------------------------------------------------------------------------- 00003 This source file is part of OGRE 00004 (Object-oriented Graphics Rendering Engine) 00005 For the latest info, see http://www.ogre3d.org/ 00006 00007 Copyright (c) 2000-2006 Torus Knot Software Ltd 00008 Also see acknowledgements in Readme.html 00009 00010 This program is free software; you can redistribute it and/or modify it under 00011 the terms of the GNU Lesser General Public License as published by the Free Software 00012 Foundation; either version 2 of the License, or (at your option) any later 00013 version. 00014 00015 This program is distributed in the hope that it will be useful, but WITHOUT 00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public License along with 00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple 00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to 00022 http://www.gnu.org/copyleft/lesser.txt. 00023 00024 You may alternatively use this source under the terms of a specific version of 00025 the OGRE Unrestricted License provided you have obtained such a license from 00026 Torus Knot Software Ltd. 00027 ----------------------------------------------------------------------------- 00028 */ 00029 #ifndef OGREIMAGERESAMPLER_H 00030 #define OGREIMAGERESAMPLER_H 00031 00032 #include <algorithm> 00033 00034 // this file is inlined into OgreImage.cpp! 00035 // do not include anywhere else. 00036 namespace Ogre { 00037 00038 // variable name hints: 00039 // sx_48 = 16/48-bit fixed-point x-position in source 00040 // stepx = difference between adjacent sx_48 values 00041 // sx1 = lower-bound integer x-position in source 00042 // sx2 = upper-bound integer x-position in source 00043 // sxf = fractional weight beween sx1 and sx2 00044 // x,y,z = location of output pixel in destination 00045 00046 // nearest-neighbor resampler, does not convert formats. 00047 // templated on bytes-per-pixel to allow compiler optimizations, such 00048 // as simplifying memcpy() and replacing multiplies with bitshifts 00049 template<unsigned int elemsize> struct NearestResampler { 00050 static void scale(const PixelBox& src, const PixelBox& dst) { 00051 // assert(src.format == dst.format); 00052 00053 // srcdata stays at beginning, pdst is a moving pointer 00054 uchar* srcdata = (uchar*)src.data; 00055 uchar* pdst = (uchar*)dst.data; 00056 00057 // sx_48,sy_48,sz_48 represent current position in source 00058 // using 16/48-bit fixed precision, incremented by steps 00059 uint64 stepx = ((uint64)src.getWidth() << 48) / dst.getWidth(); 00060 uint64 stepy = ((uint64)src.getHeight() << 48) / dst.getHeight(); 00061 uint64 stepz = ((uint64)src.getDepth() << 48) / dst.getDepth(); 00062 00063 // note: ((stepz>>1) - 1) is an extra half-step increment to adjust 00064 // for the center of the destination pixel, not the top-left corner 00065 uint64 sz_48 = (stepz >> 1) - 1; 00066 for (size_t z = dst.front; z < dst.back; z++, sz_48 += stepz) { 00067 size_t srczoff = (size_t)(sz_48 >> 48) * src.slicePitch; 00068 00069 uint64 sy_48 = (stepy >> 1) - 1; 00070 for (size_t y = dst.top; y < dst.bottom; y++, sy_48 += stepy) { 00071 size_t srcyoff = (size_t)(sy_48 >> 48) * src.rowPitch; 00072 00073 uint64 sx_48 = (stepx >> 1) - 1; 00074 for (size_t x = dst.left; x < dst.right; x++, sx_48 += stepx) { 00075 uchar* psrc = srcdata + 00076 elemsize*((size_t)(sx_48 >> 48) + srcyoff + srczoff); 00077 memcpy(pdst, psrc, elemsize); 00078 pdst += elemsize; 00079 } 00080 pdst += elemsize*dst.getRowSkip(); 00081 } 00082 pdst += elemsize*dst.getSliceSkip(); 00083 } 00084 } 00085 }; 00086 00087 00088 // default floating-point linear resampler, does format conversion 00089 struct LinearResampler { 00090 static void scale(const PixelBox& src, const PixelBox& dst) { 00091 size_t srcelemsize = PixelUtil::getNumElemBytes(src.format); 00092 size_t dstelemsize = PixelUtil::getNumElemBytes(dst.format); 00093 00094 // srcdata stays at beginning, pdst is a moving pointer 00095 uchar* srcdata = (uchar*)src.data; 00096 uchar* pdst = (uchar*)dst.data; 00097 00098 // sx_48,sy_48,sz_48 represent current position in source 00099 // using 16/48-bit fixed precision, incremented by steps 00100 uint64 stepx = ((uint64)src.getWidth() << 48) / dst.getWidth(); 00101 uint64 stepy = ((uint64)src.getHeight() << 48) / dst.getHeight(); 00102 uint64 stepz = ((uint64)src.getDepth() << 48) / dst.getDepth(); 00103 00104 // temp is 16/16 bit fixed precision, used to adjust a source 00105 // coordinate (x, y, or z) backwards by half a pixel so that the 00106 // integer bits represent the first sample (eg, sx1) and the 00107 // fractional bits are the blend weight of the second sample 00108 unsigned int temp; 00109 00110 // note: ((stepz>>1) - 1) is an extra half-step increment to adjust 00111 // for the center of the destination pixel, not the top-left corner 00112 uint64 sz_48 = (stepz >> 1) - 1; 00113 for (size_t z = dst.front; z < dst.back; z++, sz_48+=stepz) { 00114 temp = sz_48 >> 32; 00115 temp = (temp > 0x8000)? temp - 0x8000 : 0; 00116 size_t sz1 = temp >> 16; // src z, sample #1 00117 size_t sz2 = std::min(sz1+1,src.getDepth()-1);// src z, sample #2 00118 float szf = (temp & 0xFFFF) / 65536.f; // weight of sample #2 00119 00120 uint64 sy_48 = (stepy >> 1) - 1; 00121 for (size_t y = dst.top; y < dst.bottom; y++, sy_48+=stepy) { 00122 temp = sy_48 >> 32; 00123 temp = (temp > 0x8000)? temp - 0x8000 : 0; 00124 size_t sy1 = temp >> 16; // src y #1 00125 size_t sy2 = std::min(sy1+1,src.getHeight()-1);// src y #2 00126 float syf = (temp & 0xFFFF) / 65536.f; // weight of #2 00127 00128 uint64 sx_48 = (stepx >> 1) - 1; 00129 for (size_t x = dst.left; x < dst.right; x++, sx_48+=stepx) { 00130 temp = sx_48 >> 32; 00131 temp = (temp > 0x8000)? temp - 0x8000 : 0; 00132 size_t sx1 = temp >> 16; // src x #1 00133 size_t sx2 = std::min(sx1+1,src.getWidth()-1);// src x #2 00134 float sxf = (temp & 0xFFFF) / 65536.f; // weight of #2 00135 00136 ColourValue x1y1z1, x2y1z1, x1y2z1, x2y2z1; 00137 ColourValue x1y1z2, x2y1z2, x1y2z2, x2y2z2; 00138 00139 #define UNPACK(dst,x,y,z) PixelUtil::unpackColour(&dst, src.format, \ 00140 srcdata + srcelemsize*((x)+(y)*src.rowPitch+(z)*src.slicePitch)) 00141 00142 UNPACK(x1y1z1,sx1,sy1,sz1); UNPACK(x2y1z1,sx2,sy1,sz1); 00143 UNPACK(x1y2z1,sx1,sy2,sz1); UNPACK(x2y2z1,sx2,sy2,sz1); 00144 UNPACK(x1y1z2,sx1,sy1,sz2); UNPACK(x2y1z2,sx2,sy1,sz2); 00145 UNPACK(x1y2z2,sx1,sy2,sz2); UNPACK(x2y2z2,sx2,sy2,sz2); 00146 #undef UNPACK 00147 00148 ColourValue accum = 00149 x1y1z1 * ((1.0f - sxf)*(1.0f - syf)*(1.0f - szf)) + 00150 x2y1z1 * ( sxf *(1.0f - syf)*(1.0f - szf)) + 00151 x1y2z1 * ((1.0f - sxf)* syf *(1.0f - szf)) + 00152 x2y2z1 * ( sxf * syf *(1.0f - szf)) + 00153 x1y1z2 * ((1.0f - sxf)*(1.0f - syf)* szf ) + 00154 x2y1z2 * ( sxf *(1.0f - syf)* szf ) + 00155 x1y2z2 * ((1.0f - sxf)* syf * szf ) + 00156 x2y2z2 * ( sxf * syf * szf ); 00157 00158 PixelUtil::packColour(accum, dst.format, pdst); 00159 00160 pdst += dstelemsize; 00161 } 00162 pdst += dstelemsize*dst.getRowSkip(); 00163 } 00164 pdst += dstelemsize*dst.getSliceSkip(); 00165 } 00166 } 00167 }; 00168 00169 00170 // float32 linear resampler, converts FLOAT32_RGB/FLOAT32_RGBA only. 00171 // avoids overhead of pixel unpack/repack function calls 00172 struct LinearResampler_Float32 { 00173 static void scale(const PixelBox& src, const PixelBox& dst) { 00174 size_t srcchannels = PixelUtil::getNumElemBytes(src.format) / sizeof(float); 00175 size_t dstchannels = PixelUtil::getNumElemBytes(dst.format) / sizeof(float); 00176 // assert(srcchannels == 3 || srcchannels == 4); 00177 // assert(dstchannels == 3 || dstchannels == 4); 00178 00179 // srcdata stays at beginning, pdst is a moving pointer 00180 float* srcdata = (float*)src.data; 00181 float* pdst = (float*)dst.data; 00182 00183 // sx_48,sy_48,sz_48 represent current position in source 00184 // using 16/48-bit fixed precision, incremented by steps 00185 uint64 stepx = ((uint64)src.getWidth() << 48) / dst.getWidth(); 00186 uint64 stepy = ((uint64)src.getHeight() << 48) / dst.getHeight(); 00187 uint64 stepz = ((uint64)src.getDepth() << 48) / dst.getDepth(); 00188 00189 // temp is 16/16 bit fixed precision, used to adjust a source 00190 // coordinate (x, y, or z) backwards by half a pixel so that the 00191 // integer bits represent the first sample (eg, sx1) and the 00192 // fractional bits are the blend weight of the second sample 00193 unsigned int temp; 00194 00195 // note: ((stepz>>1) - 1) is an extra half-step increment to adjust 00196 // for the center of the destination pixel, not the top-left corner 00197 uint64 sz_48 = (stepz >> 1) - 1; 00198 for (size_t z = dst.front; z < dst.back; z++, sz_48+=stepz) { 00199 temp = sz_48 >> 32; 00200 temp = (temp > 0x8000)? temp - 0x8000 : 0; 00201 size_t sz1 = temp >> 16; // src z, sample #1 00202 size_t sz2 = std::min(sz1+1,src.getDepth()-1);// src z, sample #2 00203 float szf = (temp & 0xFFFF) / 65536.f; // weight of sample #2 00204 00205 uint64 sy_48 = (stepy >> 1) - 1; 00206 for (size_t y = dst.top; y < dst.bottom; y++, sy_48+=stepy) { 00207 temp = sy_48 >> 32; 00208 temp = (temp > 0x8000)? temp - 0x8000 : 0; 00209 size_t sy1 = temp >> 16; // src y #1 00210 size_t sy2 = std::min(sy1+1,src.getHeight()-1);// src y #2 00211 float syf = (temp & 0xFFFF) / 65536.f; // weight of #2 00212 00213 uint64 sx_48 = (stepx >> 1) - 1; 00214 for (size_t x = dst.left; x < dst.right; x++, sx_48+=stepx) { 00215 temp = sx_48 >> 32; 00216 temp = (temp > 0x8000)? temp - 0x8000 : 0; 00217 size_t sx1 = temp >> 16; // src x #1 00218 size_t sx2 = std::min(sx1+1,src.getWidth()-1);// src x #2 00219 float sxf = (temp & 0xFFFF) / 65536.f; // weight of #2 00220 00221 // process R,G,B,A simultaneously for cache coherence? 00222 float accum[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; 00223 00224 #define ACCUM3(x,y,z,factor) \ 00225 { float f = factor; \ 00226 size_t off = (x+y*src.rowPitch+z*src.slicePitch)*srcchannels; \ 00227 accum[0]+=srcdata[off+0]*f; accum[1]+=srcdata[off+1]*f; \ 00228 accum[2]+=srcdata[off+2]*f; } 00229 00230 #define ACCUM4(x,y,z,factor) \ 00231 { float f = factor; \ 00232 size_t off = (x+y*src.rowPitch+z*src.slicePitch)*srcchannels; \ 00233 accum[0]+=srcdata[off+0]*f; accum[1]+=srcdata[off+1]*f; \ 00234 accum[2]+=srcdata[off+2]*f; accum[3]+=srcdata[off+3]*f; } 00235 00236 if (srcchannels == 3 || dstchannels == 3) { 00237 // RGB, no alpha 00238 ACCUM3(sx1,sy1,sz1,(1.0f-sxf)*(1.0f-syf)*(1.0f-szf)); 00239 ACCUM3(sx2,sy1,sz1, sxf *(1.0f-syf)*(1.0f-szf)); 00240 ACCUM3(sx1,sy2,sz1,(1.0f-sxf)* syf *(1.0f-szf)); 00241 ACCUM3(sx2,sy2,sz1, sxf * syf *(1.0f-szf)); 00242 ACCUM3(sx1,sy1,sz2,(1.0f-sxf)*(1.0f-syf)* szf ); 00243 ACCUM3(sx2,sy1,sz2, sxf *(1.0f-syf)* szf ); 00244 ACCUM3(sx1,sy2,sz2,(1.0f-sxf)* syf * szf ); 00245 ACCUM3(sx2,sy2,sz2, sxf * syf * szf ); 00246 accum[3] = 1.0f; 00247 } else { 00248 // RGBA 00249 ACCUM4(sx1,sy1,sz1,(1.0f-sxf)*(1.0f-syf)*(1.0f-szf)); 00250 ACCUM4(sx2,sy1,sz1, sxf *(1.0f-syf)*(1.0f-szf)); 00251 ACCUM4(sx1,sy2,sz1,(1.0f-sxf)* syf *(1.0f-szf)); 00252 ACCUM4(sx2,sy2,sz1, sxf * syf *(1.0f-szf)); 00253 ACCUM4(sx1,sy1,sz2,(1.0f-sxf)*(1.0f-syf)* szf ); 00254 ACCUM4(sx2,sy1,sz2, sxf *(1.0f-syf)* szf ); 00255 ACCUM4(sx1,sy2,sz2,(1.0f-sxf)* syf * szf ); 00256 ACCUM4(sx2,sy2,sz2, sxf * syf * szf ); 00257 } 00258 00259 memcpy(pdst, accum, sizeof(float)*dstchannels); 00260 00261 #undef ACCUM3 00262 #undef ACCUM4 00263 00264 pdst += dstchannels; 00265 } 00266 pdst += dstchannels*dst.getRowSkip(); 00267 } 00268 pdst += dstchannels*dst.getSliceSkip(); 00269 } 00270 } 00271 }; 00272 00273 00274 00275 // byte linear resampler, does not do any format conversions. 00276 // only handles pixel formats that use 1 byte per color channel. 00277 // 2D only; punts 3D pixelboxes to default LinearResampler (slow). 00278 // templated on bytes-per-pixel to allow compiler optimizations, such 00279 // as unrolling loops and replacing multiplies with bitshifts 00280 template<unsigned int channels> struct LinearResampler_Byte { 00281 static void scale(const PixelBox& src, const PixelBox& dst) { 00282 // assert(src.format == dst.format); 00283 00284 // only optimized for 2D 00285 if (src.getDepth() > 1 || dst.getDepth() > 1) { 00286 LinearResampler::scale(src, dst); 00287 return; 00288 } 00289 00290 // srcdata stays at beginning of slice, pdst is a moving pointer 00291 uchar* srcdata = (uchar*)src.data; 00292 uchar* pdst = (uchar*)dst.data; 00293 00294 // sx_48,sy_48 represent current position in source 00295 // using 16/48-bit fixed precision, incremented by steps 00296 uint64 stepx = ((uint64)src.getWidth() << 48) / dst.getWidth(); 00297 uint64 stepy = ((uint64)src.getHeight() << 48) / dst.getHeight(); 00298 00299 // bottom 28 bits of temp are 16/12 bit fixed precision, used to 00300 // adjust a source coordinate backwards by half a pixel so that the 00301 // integer bits represent the first sample (eg, sx1) and the 00302 // fractional bits are the blend weight of the second sample 00303 unsigned int temp; 00304 00305 uint64 sy_48 = (stepy >> 1) - 1; 00306 for (size_t y = dst.top; y < dst.bottom; y++, sy_48+=stepy) { 00307 temp = sy_48 >> 36; 00308 temp = (temp > 0x800)? temp - 0x800: 0; 00309 unsigned int syf = temp & 0xFFF; 00310 size_t sy1 = temp >> 12; 00311 size_t sy2 = std::min(sy1+1, src.bottom-src.top-1); 00312 size_t syoff1 = sy1 * src.rowPitch; 00313 size_t syoff2 = sy2 * src.rowPitch; 00314 00315 uint64 sx_48 = (stepx >> 1) - 1; 00316 for (size_t x = dst.left; x < dst.right; x++, sx_48+=stepx) { 00317 temp = sx_48 >> 36; 00318 temp = (temp > 0x800)? temp - 0x800 : 0; 00319 unsigned int sxf = temp & 0xFFF; 00320 size_t sx1 = temp >> 12; 00321 size_t sx2 = std::min(sx1+1, src.right-src.left-1); 00322 00323 unsigned int sxfsyf = sxf*syf; 00324 for (unsigned int k = 0; k < channels; k++) { 00325 unsigned int accum = 00326 srcdata[(sx1 + syoff1)*channels+k]*(0x1000000-(sxf<<12)-(syf<<12)+sxfsyf) + 00327 srcdata[(sx2 + syoff1)*channels+k]*((sxf<<12)-sxfsyf) + 00328 srcdata[(sx1 + syoff2)*channels+k]*((syf<<12)-sxfsyf) + 00329 srcdata[(sx2 + syoff2)*channels+k]*sxfsyf; 00330 // accum is computed using 8/24-bit fixed-point math 00331 // (maximum is 0xFF000000; rounding will not cause overflow) 00332 *pdst++ = (accum + 0x800000) >> 24; 00333 } 00334 } 00335 pdst += channels*dst.getRowSkip(); 00336 } 00337 } 00338 }; 00339 00340 } 00341 00342 #endif
Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Sep 27 22:02:23 2009