OgreImageResampler.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef OGREIMAGERESAMPLER_H
  25. #define OGREIMAGERESAMPLER_H
  26. #include <algorithm>
  27. // this file is inlined into OgreImage.cpp!
  28. // do not include anywhere else.
  29. namespace Ogre {
  30. /** \addtogroup Core
  31. * @{
  32. */
  33. /** \addtogroup Image
  34. * @{
  35. */
  36. // variable name hints:
  37. // sx_48 = 16/48-bit fixed-point x-position in source
  38. // stepx = difference between adjacent sx_48 values
  39. // sx1 = lower-bound integer x-position in source
  40. // sx2 = upper-bound integer x-position in source
  41. // sxf = fractional weight beween sx1 and sx2
  42. // x,y,z = location of output pixel in destination
  43. // nearest-neighbor resampler, does not convert formats.
  44. // templated on bytes-per-pixel to allow compiler optimizations, such
  45. // as simplifying memcpy() and replacing multiplies with bitshifts
  46. template<unsigned int elemsize> struct NearestResampler {
  47. static void scale(const PixelBox& src, const PixelBox& dst) {
  48. // assert(src.format == dst.format);
  49. // srcdata stays at beginning, pdst is a moving pointer
  50. uchar* srcdata = (uchar*)src.data;
  51. uchar* pdst = (uchar*)dst.data;
  52. // sx_48,sy_48,sz_48 represent current position in source
  53. // using 16/48-bit fixed precision, incremented by steps
  54. uint64 stepx = ((uint64)src.getWidth() << 48) / dst.getWidth();
  55. uint64 stepy = ((uint64)src.getHeight() << 48) / dst.getHeight();
  56. uint64 stepz = ((uint64)src.getDepth() << 48) / dst.getDepth();
  57. // note: ((stepz>>1) - 1) is an extra half-step increment to adjust
  58. // for the center of the destination pixel, not the top-left corner
  59. uint64 sz_48 = (stepz >> 1) - 1;
  60. for (size_t z = dst.front; z < dst.back; z++, sz_48 += stepz) {
  61. size_t srczoff = (size_t)(sz_48 >> 48) * src.slicePitch;
  62. uint64 sy_48 = (stepy >> 1) - 1;
  63. for (size_t y = dst.top; y < dst.bottom; y++, sy_48 += stepy) {
  64. size_t srcyoff = (size_t)(sy_48 >> 48) * src.rowPitch;
  65. uint64 sx_48 = (stepx >> 1) - 1;
  66. for (size_t x = dst.left; x < dst.right; x++, sx_48 += stepx) {
  67. uchar* psrc = srcdata +
  68. elemsize*((size_t)(sx_48 >> 48) + srcyoff + srczoff);
  69. memcpy(pdst, psrc, elemsize);
  70. pdst += elemsize;
  71. }
  72. pdst += elemsize*dst.getRowSkip();
  73. }
  74. pdst += elemsize*dst.getSliceSkip();
  75. }
  76. }
  77. };
  78. // default floating-point linear resampler, does format conversion
  79. struct LinearResampler {
  80. static void scale(const PixelBox& src, const PixelBox& dst) {
  81. size_t srcelemsize = PixelUtil::getNumElemBytes(src.format);
  82. size_t dstelemsize = PixelUtil::getNumElemBytes(dst.format);
  83. // srcdata stays at beginning, pdst is a moving pointer
  84. uchar* srcdata = (uchar*)src.data;
  85. uchar* pdst = (uchar*)dst.data;
  86. // sx_48,sy_48,sz_48 represent current position in source
  87. // using 16/48-bit fixed precision, incremented by steps
  88. uint64 stepx = ((uint64)src.getWidth() << 48) / dst.getWidth();
  89. uint64 stepy = ((uint64)src.getHeight() << 48) / dst.getHeight();
  90. uint64 stepz = ((uint64)src.getDepth() << 48) / dst.getDepth();
  91. // temp is 16/16 bit fixed precision, used to adjust a source
  92. // coordinate (x, y, or z) backwards by half a pixel so that the
  93. // integer bits represent the first sample (eg, sx1) and the
  94. // fractional bits are the blend weight of the second sample
  95. unsigned int temp;
  96. // note: ((stepz>>1) - 1) is an extra half-step increment to adjust
  97. // for the center of the destination pixel, not the top-left corner
  98. uint64 sz_48 = (stepz >> 1) - 1;
  99. for (size_t z = dst.front; z < dst.back; z++, sz_48+=stepz) {
  100. temp = static_cast<unsigned int>(sz_48 >> 32);
  101. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  102. size_t sz1 = temp >> 16; // src z, sample #1
  103. size_t sz2 = std::min(sz1+1,src.getDepth()-1);// src z, sample #2
  104. float szf = (temp & 0xFFFF) / 65536.f; // weight of sample #2
  105. uint64 sy_48 = (stepy >> 1) - 1;
  106. for (size_t y = dst.top; y < dst.bottom; y++, sy_48+=stepy) {
  107. temp = static_cast<unsigned int>(sy_48 >> 32);
  108. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  109. size_t sy1 = temp >> 16; // src y #1
  110. size_t sy2 = std::min(sy1+1,src.getHeight()-1);// src y #2
  111. float syf = (temp & 0xFFFF) / 65536.f; // weight of #2
  112. uint64 sx_48 = (stepx >> 1) - 1;
  113. for (size_t x = dst.left; x < dst.right; x++, sx_48+=stepx) {
  114. temp = static_cast<unsigned int>(sx_48 >> 32);
  115. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  116. size_t sx1 = temp >> 16; // src x #1
  117. size_t sx2 = std::min(sx1+1,src.getWidth()-1);// src x #2
  118. float sxf = (temp & 0xFFFF) / 65536.f; // weight of #2
  119. ColourValue x1y1z1, x2y1z1, x1y2z1, x2y2z1;
  120. ColourValue x1y1z2, x2y1z2, x1y2z2, x2y2z2;
  121. #define UNPACK(dst,x,y,z) PixelUtil::unpackColour(&dst, src.format, \
  122. srcdata + srcelemsize*((x)+(y)*src.rowPitch+(z)*src.slicePitch))
  123. UNPACK(x1y1z1,sx1,sy1,sz1); UNPACK(x2y1z1,sx2,sy1,sz1);
  124. UNPACK(x1y2z1,sx1,sy2,sz1); UNPACK(x2y2z1,sx2,sy2,sz1);
  125. UNPACK(x1y1z2,sx1,sy1,sz2); UNPACK(x2y1z2,sx2,sy1,sz2);
  126. UNPACK(x1y2z2,sx1,sy2,sz2); UNPACK(x2y2z2,sx2,sy2,sz2);
  127. #undef UNPACK
  128. ColourValue accum =
  129. x1y1z1 * ((1.0f - sxf)*(1.0f - syf)*(1.0f - szf)) +
  130. x2y1z1 * ( sxf *(1.0f - syf)*(1.0f - szf)) +
  131. x1y2z1 * ((1.0f - sxf)* syf *(1.0f - szf)) +
  132. x2y2z1 * ( sxf * syf *(1.0f - szf)) +
  133. x1y1z2 * ((1.0f - sxf)*(1.0f - syf)* szf ) +
  134. x2y1z2 * ( sxf *(1.0f - syf)* szf ) +
  135. x1y2z2 * ((1.0f - sxf)* syf * szf ) +
  136. x2y2z2 * ( sxf * syf * szf );
  137. PixelUtil::packColour(accum, dst.format, pdst);
  138. pdst += dstelemsize;
  139. }
  140. pdst += dstelemsize*dst.getRowSkip();
  141. }
  142. pdst += dstelemsize*dst.getSliceSkip();
  143. }
  144. }
  145. };
  146. // float32 linear resampler, converts FLOAT32_RGB/FLOAT32_RGBA only.
  147. // avoids overhead of pixel unpack/repack function calls
  148. struct LinearResampler_Float32 {
  149. static void scale(const PixelBox& src, const PixelBox& dst) {
  150. size_t srcchannels = PixelUtil::getNumElemBytes(src.format) / sizeof(float);
  151. size_t dstchannels = PixelUtil::getNumElemBytes(dst.format) / sizeof(float);
  152. // assert(srcchannels == 3 || srcchannels == 4);
  153. // assert(dstchannels == 3 || dstchannels == 4);
  154. // srcdata stays at beginning, pdst is a moving pointer
  155. float* srcdata = (float*)src.data;
  156. float* pdst = (float*)dst.data;
  157. // sx_48,sy_48,sz_48 represent current position in source
  158. // using 16/48-bit fixed precision, incremented by steps
  159. uint64 stepx = ((uint64)src.getWidth() << 48) / dst.getWidth();
  160. uint64 stepy = ((uint64)src.getHeight() << 48) / dst.getHeight();
  161. uint64 stepz = ((uint64)src.getDepth() << 48) / dst.getDepth();
  162. // temp is 16/16 bit fixed precision, used to adjust a source
  163. // coordinate (x, y, or z) backwards by half a pixel so that the
  164. // integer bits represent the first sample (eg, sx1) and the
  165. // fractional bits are the blend weight of the second sample
  166. unsigned int temp;
  167. // note: ((stepz>>1) - 1) is an extra half-step increment to adjust
  168. // for the center of the destination pixel, not the top-left corner
  169. uint64 sz_48 = (stepz >> 1) - 1;
  170. for (size_t z = dst.front; z < dst.back; z++, sz_48+=stepz) {
  171. temp = static_cast<unsigned int>(sz_48 >> 32);
  172. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  173. size_t sz1 = temp >> 16; // src z, sample #1
  174. size_t sz2 = std::min(sz1+1,src.getDepth()-1);// src z, sample #2
  175. float szf = (temp & 0xFFFF) / 65536.f; // weight of sample #2
  176. uint64 sy_48 = (stepy >> 1) - 1;
  177. for (size_t y = dst.top; y < dst.bottom; y++, sy_48+=stepy) {
  178. temp = static_cast<unsigned int>(sy_48 >> 32);
  179. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  180. size_t sy1 = temp >> 16; // src y #1
  181. size_t sy2 = std::min(sy1+1,src.getHeight()-1);// src y #2
  182. float syf = (temp & 0xFFFF) / 65536.f; // weight of #2
  183. uint64 sx_48 = (stepx >> 1) - 1;
  184. for (size_t x = dst.left; x < dst.right; x++, sx_48+=stepx) {
  185. temp = static_cast<unsigned int>(sx_48 >> 32);
  186. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  187. size_t sx1 = temp >> 16; // src x #1
  188. size_t sx2 = std::min(sx1+1,src.getWidth()-1);// src x #2
  189. float sxf = (temp & 0xFFFF) / 65536.f; // weight of #2
  190. // process R,G,B,A simultaneously for cache coherence?
  191. float accum[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  192. #define ACCUM3(x,y,z,factor) \
  193. { float f = factor; \
  194. size_t off = (x+y*src.rowPitch+z*src.slicePitch)*srcchannels; \
  195. accum[0]+=srcdata[off+0]*f; accum[1]+=srcdata[off+1]*f; \
  196. accum[2]+=srcdata[off+2]*f; }
  197. #define ACCUM4(x,y,z,factor) \
  198. { float f = factor; \
  199. size_t off = (x+y*src.rowPitch+z*src.slicePitch)*srcchannels; \
  200. accum[0]+=srcdata[off+0]*f; accum[1]+=srcdata[off+1]*f; \
  201. accum[2]+=srcdata[off+2]*f; accum[3]+=srcdata[off+3]*f; }
  202. if (srcchannels == 3 || dstchannels == 3) {
  203. // RGB, no alpha
  204. ACCUM3(sx1,sy1,sz1,(1.0f-sxf)*(1.0f-syf)*(1.0f-szf));
  205. ACCUM3(sx2,sy1,sz1, sxf *(1.0f-syf)*(1.0f-szf));
  206. ACCUM3(sx1,sy2,sz1,(1.0f-sxf)* syf *(1.0f-szf));
  207. ACCUM3(sx2,sy2,sz1, sxf * syf *(1.0f-szf));
  208. ACCUM3(sx1,sy1,sz2,(1.0f-sxf)*(1.0f-syf)* szf );
  209. ACCUM3(sx2,sy1,sz2, sxf *(1.0f-syf)* szf );
  210. ACCUM3(sx1,sy2,sz2,(1.0f-sxf)* syf * szf );
  211. ACCUM3(sx2,sy2,sz2, sxf * syf * szf );
  212. accum[3] = 1.0f;
  213. } else {
  214. // RGBA
  215. ACCUM4(sx1,sy1,sz1,(1.0f-sxf)*(1.0f-syf)*(1.0f-szf));
  216. ACCUM4(sx2,sy1,sz1, sxf *(1.0f-syf)*(1.0f-szf));
  217. ACCUM4(sx1,sy2,sz1,(1.0f-sxf)* syf *(1.0f-szf));
  218. ACCUM4(sx2,sy2,sz1, sxf * syf *(1.0f-szf));
  219. ACCUM4(sx1,sy1,sz2,(1.0f-sxf)*(1.0f-syf)* szf );
  220. ACCUM4(sx2,sy1,sz2, sxf *(1.0f-syf)* szf );
  221. ACCUM4(sx1,sy2,sz2,(1.0f-sxf)* syf * szf );
  222. ACCUM4(sx2,sy2,sz2, sxf * syf * szf );
  223. }
  224. memcpy(pdst, accum, sizeof(float)*dstchannels);
  225. #undef ACCUM3
  226. #undef ACCUM4
  227. pdst += dstchannels;
  228. }
  229. pdst += dstchannels*dst.getRowSkip();
  230. }
  231. pdst += dstchannels*dst.getSliceSkip();
  232. }
  233. }
  234. };
  235. // byte linear resampler, does not do any format conversions.
  236. // only handles pixel formats that use 1 byte per color channel.
  237. // 2D only; punts 3D pixelboxes to default LinearResampler (slow).
  238. // templated on bytes-per-pixel to allow compiler optimizations, such
  239. // as unrolling loops and replacing multiplies with bitshifts
  240. template<unsigned int channels> struct LinearResampler_Byte {
  241. static void scale(const PixelBox& src, const PixelBox& dst) {
  242. // assert(src.format == dst.format);
  243. // only optimized for 2D
  244. if (src.getDepth() > 1 || dst.getDepth() > 1) {
  245. LinearResampler::scale(src, dst);
  246. return;
  247. }
  248. // srcdata stays at beginning of slice, pdst is a moving pointer
  249. uchar* srcdata = (uchar*)src.data;
  250. uchar* pdst = (uchar*)dst.data;
  251. // sx_48,sy_48 represent current position in source
  252. // using 16/48-bit fixed precision, incremented by steps
  253. uint64 stepx = ((uint64)src.getWidth() << 48) / dst.getWidth();
  254. uint64 stepy = ((uint64)src.getHeight() << 48) / dst.getHeight();
  255. // bottom 28 bits of temp are 16/12 bit fixed precision, used to
  256. // adjust a source coordinate backwards by half a pixel so that the
  257. // integer bits represent the first sample (eg, sx1) and the
  258. // fractional bits are the blend weight of the second sample
  259. unsigned int temp;
  260. uint64 sy_48 = (stepy >> 1) - 1;
  261. for (size_t y = dst.top; y < dst.bottom; y++, sy_48+=stepy) {
  262. temp = static_cast<unsigned int>(sy_48 >> 36);
  263. temp = (temp > 0x800)? temp - 0x800: 0;
  264. unsigned int syf = temp & 0xFFF;
  265. size_t sy1 = temp >> 12;
  266. size_t sy2 = std::min(sy1+1, src.bottom-src.top-1);
  267. size_t syoff1 = sy1 * src.rowPitch;
  268. size_t syoff2 = sy2 * src.rowPitch;
  269. uint64 sx_48 = (stepx >> 1) - 1;
  270. for (size_t x = dst.left; x < dst.right; x++, sx_48+=stepx) {
  271. temp = static_cast<unsigned int>(sx_48 >> 36);
  272. temp = (temp > 0x800)? temp - 0x800 : 0;
  273. unsigned int sxf = temp & 0xFFF;
  274. size_t sx1 = temp >> 12;
  275. size_t sx2 = std::min(sx1+1, src.right-src.left-1);
  276. unsigned int sxfsyf = sxf*syf;
  277. for (unsigned int k = 0; k < channels; k++) {
  278. unsigned int accum =
  279. srcdata[(sx1 + syoff1)*channels+k]*(0x1000000-(sxf<<12)-(syf<<12)+sxfsyf) +
  280. srcdata[(sx2 + syoff1)*channels+k]*((sxf<<12)-sxfsyf) +
  281. srcdata[(sx1 + syoff2)*channels+k]*((syf<<12)-sxfsyf) +
  282. srcdata[(sx2 + syoff2)*channels+k]*sxfsyf;
  283. // accum is computed using 8/24-bit fixed-point math
  284. // (maximum is 0xFF000000; rounding will not cause overflow)
  285. *pdst++ = static_cast<uchar>((accum + 0x800000) >> 24);
  286. }
  287. }
  288. pdst += channels*dst.getRowSkip();
  289. }
  290. }
  291. };
  292. /** @} */
  293. /** @} */
  294. }
  295. #endif