Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

OgreImageResampler.h

Go to the documentation of this file.
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-2005 The OGRE Team
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 */
00025 #ifndef OGREIMAGERESAMPLER_H
00026 #define OGREIMAGERESAMPLER_H
00027 
00028 #include <algorithm>
00029 
00030 // this file is inlined into OgreImage.cpp!
00031 // do not include anywhere else.
00032 namespace Ogre {
00033 
00034 // define uint64 type
00035 #if OGRE_COMPILER == OGRE_COMPILER_MSVC
00036 typedef unsigned __int64 uint64;
00037 #else
00038 typedef unsigned long long uint64;
00039 #endif
00040 
00041 // variable name hints:
00042 // sx_48 = 16/48-bit fixed-point x-position in source
00043 // stepx = difference between adjacent sx_48 values
00044 // sx1 = lower-bound integer x-position in source
00045 // sx2 = upper-bound integer x-position in source
00046 // sxf = fractional weight beween sx1 and sx2
00047 // x,y,z = location of output pixel in destination
00048 
00049 // nearest-neighbor resampler, does not convert formats.
00050 // templated on bytes-per-pixel to allow compiler optimizations, such
00051 // as simplifying memcpy() and replacing multiplies with bitshifts
00052 template<unsigned int elemsize> struct NearestResampler {
00053     static void scale(const PixelBox& src, const PixelBox& dst) {
00054         // assert(src.format == dst.format);
00055 
00056         // srcdata stays at beginning, pdst is a moving pointer
00057         uchar* srcdata = (uchar*)src.data;
00058         uchar* pdst = (uchar*)dst.data;
00059 
00060         // sx_48,sy_48,sz_48 represent current position in source
00061         // using 16/48-bit fixed precision, incremented by steps
00062         uint64 stepx = ((uint64)src.getWidth() << 48) / dst.getWidth();
00063         uint64 stepy = ((uint64)src.getHeight() << 48) / dst.getHeight();
00064         uint64 stepz = ((uint64)src.getDepth() << 48) / dst.getDepth();
00065 
00066         // note: ((stepz>>1) - 1) is an extra half-step increment to adjust
00067         // for the center of the destination pixel, not the top-left corner
00068         uint64 sz_48 = (stepz >> 1) - 1;
00069         for (size_t z = dst.front; z < dst.back; z++, sz_48 += stepz) {
00070             size_t srczoff = (size_t)(sz_48 >> 48) * src.slicePitch;
00071             
00072             uint64 sy_48 = (stepy >> 1) - 1;
00073             for (size_t y = dst.top; y < dst.bottom; y++, sy_48 += stepy) {
00074                 size_t srcyoff = (size_t)(sy_48 >> 48) * src.rowPitch;
00075             
00076                 uint64 sx_48 = (stepx >> 1) - 1;
00077                 for (size_t x = dst.left; x < dst.right; x++, sx_48 += stepx) {
00078                     uchar* psrc = srcdata +
00079                         elemsize*((size_t)(sx_48 >> 48) + srcyoff + srczoff);
00080                     memcpy(pdst, psrc, elemsize);
00081                     pdst += elemsize;
00082                 }
00083                 pdst += elemsize*dst.getRowSkip();
00084             }
00085             pdst += elemsize*dst.getSliceSkip();
00086         }
00087     }
00088 };
00089 
00090 
00091 // default floating-point linear resampler, does format conversion
00092 struct LinearResampler {
00093     static void scale(const PixelBox& src, const PixelBox& dst) {
00094         size_t srcelemsize = PixelUtil::getNumElemBytes(src.format);
00095         size_t dstelemsize = PixelUtil::getNumElemBytes(dst.format);
00096 
00097         // srcdata stays at beginning, pdst is a moving pointer
00098         uchar* srcdata = (uchar*)src.data;
00099         uchar* pdst = (uchar*)dst.data;
00100         
00101         // sx_48,sy_48,sz_48 represent current position in source
00102         // using 16/48-bit fixed precision, incremented by steps
00103         uint64 stepx = ((uint64)src.getWidth() << 48) / dst.getWidth();
00104         uint64 stepy = ((uint64)src.getHeight() << 48) / dst.getHeight();
00105         uint64 stepz = ((uint64)src.getDepth() << 48) / dst.getDepth();
00106         
00107         // temp is 16/16 bit fixed precision, used to adjust a source
00108         // coordinate (x, y, or z) backwards by half a pixel so that the
00109         // integer bits represent the first sample (eg, sx1) and the
00110         // fractional bits are the blend weight of the second sample
00111         unsigned int temp;
00112 
00113         // note: ((stepz>>1) - 1) is an extra half-step increment to adjust
00114         // for the center of the destination pixel, not the top-left corner
00115         uint64 sz_48 = (stepz >> 1) - 1;
00116         for (size_t z = dst.front; z < dst.back; z++, sz_48+=stepz) {
00117             temp = sz_48 >> 32;
00118             temp = (temp > 0x8000)? temp - 0x8000 : 0;
00119             size_t sz1 = temp >> 16;                 // src z, sample #1
00120             size_t sz2 = std::min(sz1+1,src.getDepth()-1);// src z, sample #2
00121             float szf = (temp & 0xFFFF) / 65536.f; // weight of sample #2
00122 
00123             uint64 sy_48 = (stepy >> 1) - 1;
00124             for (size_t y = dst.top; y < dst.bottom; y++, sy_48+=stepy) {
00125                 temp = sy_48 >> 32;
00126                 temp = (temp > 0x8000)? temp - 0x8000 : 0;
00127                 size_t sy1 = temp >> 16;                    // src y #1
00128                 size_t sy2 = std::min(sy1+1,src.getHeight()-1);// src y #2
00129                 float syf = (temp & 0xFFFF) / 65536.f; // weight of #2
00130                 
00131                 uint64 sx_48 = (stepx >> 1) - 1;
00132                 for (size_t x = dst.left; x < dst.right; x++, sx_48+=stepx) {
00133                     temp = sx_48 >> 32;
00134                     temp = (temp > 0x8000)? temp - 0x8000 : 0;
00135                     size_t sx1 = temp >> 16;                    // src x #1
00136                     size_t sx2 = std::min(sx1+1,src.getWidth()-1);// src x #2
00137                     float sxf = (temp & 0xFFFF) / 65536.f; // weight of #2
00138                 
00139                     ColourValue x1y1z1, x2y1z1, x1y2z1, x2y2z1;
00140                     ColourValue x1y1z2, x2y1z2, x1y2z2, x2y2z2;
00141 
00142 #define UNPACK(dst,x,y,z) PixelUtil::unpackColour(&dst, src.format, \
00143     srcdata + srcelemsize*((x)+(y)*src.rowPitch+(z)*src.slicePitch))
00144 
00145                     UNPACK(x1y1z1,sx1,sy1,sz1); UNPACK(x2y1z1,sx2,sy1,sz1);
00146                     UNPACK(x1y2z1,sx1,sy2,sz1); UNPACK(x2y2z1,sx2,sy2,sz1);
00147                     UNPACK(x1y1z2,sx1,sy1,sz2); UNPACK(x2y1z2,sx2,sy1,sz2);
00148                     UNPACK(x1y2z2,sx1,sy2,sz2); UNPACK(x2y2z2,sx2,sy2,sz2);
00149 #undef UNPACK
00150 
00151                     ColourValue accum =
00152                         x1y1z1 * ((1.0f - sxf)*(1.0f - syf)*(1.0f - szf)) +
00153                         x2y1z1 * (        sxf *(1.0f - syf)*(1.0f - szf)) +
00154                         x1y2z1 * ((1.0f - sxf)*        syf *(1.0f - szf)) +
00155                         x2y2z1 * (        sxf *        syf *(1.0f - szf)) +
00156                         x1y1z2 * ((1.0f - sxf)*(1.0f - syf)*        szf ) +
00157                         x2y1z2 * (        sxf *(1.0f - syf)*        szf ) +
00158                         x1y2z2 * ((1.0f - sxf)*        syf *        szf ) +
00159                         x2y2z2 * (        sxf *        syf *        szf );
00160 
00161                     PixelUtil::packColour(accum, dst.format, pdst);
00162 
00163                     pdst += dstelemsize;
00164                 }
00165                 pdst += dstelemsize*dst.getRowSkip();
00166             }
00167             pdst += dstelemsize*dst.getSliceSkip();
00168         }
00169     }
00170 };
00171 
00172 
00173 // float32 linear resampler, converts FLOAT32_RGB/FLOAT32_RGBA only.
00174 // avoids overhead of pixel unpack/repack function calls
00175 struct LinearResampler_Float32 {
00176     static void scale(const PixelBox& src, const PixelBox& dst) {
00177         size_t srcchannels = PixelUtil::getNumElemBytes(src.format) / sizeof(float);
00178         size_t dstchannels = PixelUtil::getNumElemBytes(dst.format) / sizeof(float);
00179         // assert(srcchannels == 3 || srcchannels == 4);
00180         // assert(dstchannels == 3 || dstchannels == 4);
00181 
00182         // srcdata stays at beginning, pdst is a moving pointer
00183         float* srcdata = (float*)src.data;
00184         float* pdst = (float*)dst.data;
00185         
00186         // sx_48,sy_48,sz_48 represent current position in source
00187         // using 16/48-bit fixed precision, incremented by steps
00188         uint64 stepx = ((uint64)src.getWidth() << 48) / dst.getWidth();
00189         uint64 stepy = ((uint64)src.getHeight() << 48) / dst.getHeight();
00190         uint64 stepz = ((uint64)src.getDepth() << 48) / dst.getDepth();
00191         
00192         // temp is 16/16 bit fixed precision, used to adjust a source
00193         // coordinate (x, y, or z) backwards by half a pixel so that the
00194         // integer bits represent the first sample (eg, sx1) and the
00195         // fractional bits are the blend weight of the second sample
00196         unsigned int temp;
00197 
00198         // note: ((stepz>>1) - 1) is an extra half-step increment to adjust
00199         // for the center of the destination pixel, not the top-left corner
00200         uint64 sz_48 = (stepz >> 1) - 1;
00201         for (size_t z = dst.front; z < dst.back; z++, sz_48+=stepz) {
00202             temp = sz_48 >> 32;
00203             temp = (temp > 0x8000)? temp - 0x8000 : 0;
00204             size_t sz1 = temp >> 16;                 // src z, sample #1
00205             size_t sz2 = std::min(sz1+1,src.getDepth()-1);// src z, sample #2
00206             float szf = (temp & 0xFFFF) / 65536.f; // weight of sample #2
00207 
00208             uint64 sy_48 = (stepy >> 1) - 1;
00209             for (size_t y = dst.top; y < dst.bottom; y++, sy_48+=stepy) {
00210                 temp = sy_48 >> 32;
00211                 temp = (temp > 0x8000)? temp - 0x8000 : 0;
00212                 size_t sy1 = temp >> 16;                    // src y #1
00213                 size_t sy2 = std::min(sy1+1,src.getHeight()-1);// src y #2
00214                 float syf = (temp & 0xFFFF) / 65536.f; // weight of #2
00215                 
00216                 uint64 sx_48 = (stepx >> 1) - 1;
00217                 for (size_t x = dst.left; x < dst.right; x++, sx_48+=stepx) {
00218                     temp = sx_48 >> 32;
00219                     temp = (temp > 0x8000)? temp - 0x8000 : 0;
00220                     size_t sx1 = temp >> 16;                    // src x #1
00221                     size_t sx2 = std::min(sx1+1,src.getWidth()-1);// src x #2
00222                     float sxf = (temp & 0xFFFF) / 65536.f; // weight of #2
00223                     
00224                     // process R,G,B,A simultaneously for cache coherence?
00225                     float accum[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
00226 
00227 #define ACCUM3(x,y,z,factor) \
00228     { float f = factor; \
00229     size_t off = (x+y*src.rowPitch+z*src.slicePitch)*srcchannels; \
00230     accum[0]+=srcdata[off+0]*f; accum[1]+=srcdata[off+1]*f; \
00231     accum[2]+=srcdata[off+2]*f; }
00232 
00233 #define ACCUM4(x,y,z,factor) \
00234     { float f = factor; \
00235     size_t off = (x+y*src.rowPitch+z*src.slicePitch)*srcchannels; \
00236     accum[0]+=srcdata[off+0]*f; accum[1]+=srcdata[off+1]*f; \
00237     accum[2]+=srcdata[off+2]*f; accum[3]+=srcdata[off+3]*f; }
00238 
00239                     if (srcchannels == 3 || dstchannels == 3) {
00240                         // RGB, no alpha
00241                         ACCUM3(sx1,sy1,sz1,(1.0f-sxf)*(1.0f-syf)*(1.0f-szf));
00242                         ACCUM3(sx2,sy1,sz1,      sxf *(1.0f-syf)*(1.0f-szf));
00243                         ACCUM3(sx1,sy2,sz1,(1.0f-sxf)*      syf *(1.0f-szf));
00244                         ACCUM3(sx2,sy2,sz1,      sxf *      syf *(1.0f-szf));
00245                         ACCUM3(sx1,sy1,sz2,(1.0f-sxf)*(1.0f-syf)*      szf );
00246                         ACCUM3(sx2,sy1,sz2,      sxf *(1.0f-syf)*      szf );
00247                         ACCUM3(sx1,sy2,sz2,(1.0f-sxf)*      syf *      szf );
00248                         ACCUM3(sx2,sy2,sz2,      sxf *      syf *      szf );
00249                         accum[3] = 1.0f;
00250                     } else {
00251                         // RGBA
00252                         ACCUM4(sx1,sy1,sz1,(1.0f-sxf)*(1.0f-syf)*(1.0f-szf));
00253                         ACCUM4(sx2,sy1,sz1,      sxf *(1.0f-syf)*(1.0f-szf));
00254                         ACCUM4(sx1,sy2,sz1,(1.0f-sxf)*      syf *(1.0f-szf));
00255                         ACCUM4(sx2,sy2,sz1,      sxf *      syf *(1.0f-szf));
00256                         ACCUM4(sx1,sy1,sz2,(1.0f-sxf)*(1.0f-syf)*      szf );
00257                         ACCUM4(sx2,sy1,sz2,      sxf *(1.0f-syf)*      szf );
00258                         ACCUM4(sx1,sy2,sz2,(1.0f-sxf)*      syf *      szf );
00259                         ACCUM4(sx2,sy2,sz2,      sxf *      syf *      szf );
00260                     }
00261 
00262                     memcpy(pdst, accum, sizeof(float)*dstchannels);
00263 
00264 #undef ACCUM3
00265 #undef ACCUM4
00266 
00267                     pdst += dstchannels;
00268                 }
00269                 pdst += dstchannels*dst.getRowSkip();
00270             }
00271             pdst += dstchannels*dst.getSliceSkip();
00272         }
00273     }
00274 };
00275 
00276 
00277 
00278 // byte linear resampler, does not do any format conversions.
00279 // only handles pixel formats that use 1 byte per color channel.
00280 // 2D only; punts 3D pixelboxes to default LinearResampler (slow).
00281 // templated on bytes-per-pixel to allow compiler optimizations, such
00282 // as unrolling loops and replacing multiplies with bitshifts
00283 template<unsigned int channels> struct LinearResampler_Byte {
00284     static void scale(const PixelBox& src, const PixelBox& dst) {
00285         // assert(src.format == dst.format);
00286 
00287         // only optimized for 2D
00288         if (src.getDepth() > 1 || dst.getDepth() > 1) {
00289             LinearResampler::scale(src, dst);
00290             return;
00291         }
00292 
00293         // srcdata stays at beginning of slice, pdst is a moving pointer
00294         uchar* srcdata = (uchar*)src.data;
00295         uchar* pdst = (uchar*)dst.data;
00296 
00297         // sx_48,sy_48 represent current position in source
00298         // using 16/48-bit fixed precision, incremented by steps
00299         uint64 stepx = ((uint64)src.getWidth() << 48) / dst.getWidth();
00300         uint64 stepy = ((uint64)src.getHeight() << 48) / dst.getHeight();
00301         
00302         // bottom 28 bits of temp are 16/12 bit fixed precision, used to
00303         // adjust a source coordinate backwards by half a pixel so that the
00304         // integer bits represent the first sample (eg, sx1) and the
00305         // fractional bits are the blend weight of the second sample
00306         unsigned int temp;
00307         
00308         uint64 sy_48 = (stepy >> 1) - 1;
00309         for (size_t y = dst.top; y < dst.bottom; y++, sy_48+=stepy) {
00310             temp = sy_48 >> 36;
00311             temp = (temp > 0x800)? temp - 0x800: 0;
00312             unsigned int syf = temp & 0xFFF;
00313             size_t sy1 = temp >> 12;
00314             size_t sy2 = std::min(sy1+1, src.bottom-src.top-1);
00315             size_t syoff1 = sy1 * src.rowPitch;
00316             size_t syoff2 = sy2 * src.rowPitch;
00317 
00318             uint64 sx_48 = (stepx >> 1) - 1;
00319             for (size_t x = dst.left; x < dst.right; x++, sx_48+=stepx) {
00320                 temp = sx_48 >> 36;
00321                 temp = (temp > 0x800)? temp - 0x800 : 0;
00322                 unsigned int sxf = temp & 0xFFF;
00323                 size_t sx1 = temp >> 12;
00324                 size_t sx2 = std::min(sx1+1, src.right-src.left-1);
00325 
00326                 unsigned int sxfsyf = sxf*syf;
00327                 for (unsigned int k = 0; k < channels; k++) {
00328                     unsigned int accum =
00329                         srcdata[(sx1 + syoff1)*channels+k]*(0x1000000-(sxf<<12)-(syf<<12)+sxfsyf) +
00330                         srcdata[(sx2 + syoff1)*channels+k]*((sxf<<12)-sxfsyf) +
00331                         srcdata[(sx1 + syoff2)*channels+k]*((syf<<12)-sxfsyf) +
00332                         srcdata[(sx2 + syoff2)*channels+k]*sxfsyf;
00333                     // accum is computed using 8/24-bit fixed-point math
00334                     // (maximum is 0xFF000000; rounding will not cause overflow)
00335                     *pdst++ = (accum + 0x800000) >> 24;
00336                 }
00337             }
00338             pdst += channels*dst.getRowSkip();
00339         }
00340     }
00341 };
00342 
00343 }
00344 
00345 #endif

Copyright © 2000-2005 by The OGRE Team
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Mar 12 14:37:43 2006