CmPixelUtil.cpp 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  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. #include "CmPixelUtil.h"
  25. #include "CmBitwise.h"
  26. #include "CmColor.h"
  27. #include "CmException.h"
  28. namespace CamelotFramework {
  29. //-----------------------------------------------------------------------
  30. /**
  31. * Resamplers
  32. */
  33. // variable name hints:
  34. // sx_48 = 16/48-bit fixed-point x-position in source
  35. // stepx = difference between adjacent sx_48 values
  36. // sx1 = lower-bound integer x-position in source
  37. // sx2 = upper-bound integer x-position in source
  38. // sxf = fractional weight beween sx1 and sx2
  39. // x,y,z = location of output pixel in destination
  40. // nearest-neighbor resampler, does not convert formats.
  41. // templated on bytes-per-pixel to allow compiler optimizations, such
  42. // as simplifying memcpy() and replacing multiplies with bitshifts
  43. template<unsigned int elemsize> struct NearestResampler {
  44. static void scale(const PixelData& src, const PixelData& dst) {
  45. // assert(src.format == dst.format);
  46. // srcdata stays at beginning, pdst is a moving pointer
  47. UINT8* srcdata = (UINT8*)src.getData();
  48. UINT8* pdst = (UINT8*)dst.getData();
  49. // sx_48,sy_48,sz_48 represent current position in source
  50. // using 16/48-bit fixed precision, incremented by steps
  51. UINT64 stepx = ((UINT64)src.getWidth() << 48) / dst.getWidth();
  52. UINT64 stepy = ((UINT64)src.getHeight() << 48) / dst.getHeight();
  53. UINT64 stepz = ((UINT64)src.getDepth() << 48) / dst.getDepth();
  54. // note: ((stepz>>1) - 1) is an extra half-step increment to adjust
  55. // for the center of the destination pixel, not the top-left corner
  56. UINT64 sz_48 = (stepz >> 1) - 1;
  57. for (size_t z = dst.getFront(); z < dst.getBack(); z++, sz_48 += stepz) {
  58. size_t srczoff = (size_t)(sz_48 >> 48) * src.getSlicePitch();
  59. UINT64 sy_48 = (stepy >> 1) - 1;
  60. for (size_t y = dst.getTop(); y < dst.getBottom(); y++, sy_48 += stepy) {
  61. size_t srcyoff = (size_t)(sy_48 >> 48) * src.getRowPitch();
  62. UINT64 sx_48 = (stepx >> 1) - 1;
  63. for (size_t x = dst.getLeft(); x < dst.getRight(); x++, sx_48 += stepx) {
  64. UINT8* psrc = srcdata +
  65. elemsize*((size_t)(sx_48 >> 48) + srcyoff + srczoff);
  66. memcpy(pdst, psrc, elemsize);
  67. pdst += elemsize;
  68. }
  69. pdst += elemsize*dst.getRowSkip();
  70. }
  71. pdst += elemsize*dst.getSliceSkip();
  72. }
  73. }
  74. };
  75. // default floating-point linear resampler, does format conversion
  76. struct LinearResampler {
  77. static void scale(const PixelData& src, const PixelData& dst) {
  78. size_t srcelemsize = PixelUtil::getNumElemBytes(src.getFormat());
  79. size_t dstelemsize = PixelUtil::getNumElemBytes(dst.getFormat());
  80. // srcdata stays at beginning, pdst is a moving pointer
  81. UINT8* srcdata = (UINT8*)src.getData();
  82. UINT8* pdst = (UINT8*)dst.getData();
  83. // sx_48,sy_48,sz_48 represent current position in source
  84. // using 16/48-bit fixed precision, incremented by steps
  85. UINT64 stepx = ((UINT64)src.getWidth() << 48) / dst.getWidth();
  86. UINT64 stepy = ((UINT64)src.getHeight() << 48) / dst.getHeight();
  87. UINT64 stepz = ((UINT64)src.getDepth() << 48) / dst.getDepth();
  88. // temp is 16/16 bit fixed precision, used to adjust a source
  89. // coordinate (x, y, or z) backwards by half a pixel so that the
  90. // integer bits represent the first sample (eg, sx1) and the
  91. // fractional bits are the blend weight of the second sample
  92. unsigned int temp;
  93. // note: ((stepz>>1) - 1) is an extra half-step increment to adjust
  94. // for the center of the destination pixel, not the top-left corner
  95. UINT64 sz_48 = (stepz >> 1) - 1;
  96. for (size_t z = dst.getFront(); z < dst.getBack(); z++, sz_48+=stepz) {
  97. temp = static_cast<unsigned int>(sz_48 >> 32);
  98. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  99. size_t sz1 = temp >> 16; // src z, sample #1
  100. size_t sz2 = std::min(sz1+1,(size_t)src.getDepth()-1);// src z, sample #2
  101. float szf = (temp & 0xFFFF) / 65536.f; // weight of sample #2
  102. UINT64 sy_48 = (stepy >> 1) - 1;
  103. for (size_t y = dst.getTop(); y < dst.getBottom(); y++, sy_48+=stepy) {
  104. temp = static_cast<unsigned int>(sy_48 >> 32);
  105. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  106. size_t sy1 = temp >> 16; // src y #1
  107. size_t sy2 = std::min(sy1+1,(size_t)src.getHeight()-1);// src y #2
  108. float syf = (temp & 0xFFFF) / 65536.f; // weight of #2
  109. UINT64 sx_48 = (stepx >> 1) - 1;
  110. for (size_t x = dst.getLeft(); x < dst.getRight(); x++, sx_48+=stepx) {
  111. temp = static_cast<unsigned int>(sx_48 >> 32);
  112. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  113. size_t sx1 = temp >> 16; // src x #1
  114. size_t sx2 = std::min(sx1+1,(size_t)src.getWidth()-1);// src x #2
  115. float sxf = (temp & 0xFFFF) / 65536.f; // weight of #2
  116. Color x1y1z1, x2y1z1, x1y2z1, x2y2z1;
  117. Color x1y1z2, x2y1z2, x1y2z2, x2y2z2;
  118. #define UNPACK(dst,x,y,z) PixelUtil::unpackColour(&dst, src.getFormat(), \
  119. srcdata + srcelemsize*((x)+(y)*src.getRowPitch()+(z)*src.getSlicePitch()))
  120. UNPACK(x1y1z1,sx1,sy1,sz1); UNPACK(x2y1z1,sx2,sy1,sz1);
  121. UNPACK(x1y2z1,sx1,sy2,sz1); UNPACK(x2y2z1,sx2,sy2,sz1);
  122. UNPACK(x1y1z2,sx1,sy1,sz2); UNPACK(x2y1z2,sx2,sy1,sz2);
  123. UNPACK(x1y2z2,sx1,sy2,sz2); UNPACK(x2y2z2,sx2,sy2,sz2);
  124. #undef UNPACK
  125. Color accum =
  126. x1y1z1 * ((1.0f - sxf)*(1.0f - syf)*(1.0f - szf)) +
  127. x2y1z1 * ( sxf *(1.0f - syf)*(1.0f - szf)) +
  128. x1y2z1 * ((1.0f - sxf)* syf *(1.0f - szf)) +
  129. x2y2z1 * ( sxf * syf *(1.0f - szf)) +
  130. x1y1z2 * ((1.0f - sxf)*(1.0f - syf)* szf ) +
  131. x2y1z2 * ( sxf *(1.0f - syf)* szf ) +
  132. x1y2z2 * ((1.0f - sxf)* syf * szf ) +
  133. x2y2z2 * ( sxf * syf * szf );
  134. PixelUtil::packColour(accum, dst.getFormat(), pdst);
  135. pdst += dstelemsize;
  136. }
  137. pdst += dstelemsize*dst.getRowSkip();
  138. }
  139. pdst += dstelemsize*dst.getSliceSkip();
  140. }
  141. }
  142. };
  143. // float32 linear resampler, converts FLOAT32_RGB/FLOAT32_RGBA only.
  144. // avoids overhead of pixel unpack/repack function calls
  145. struct LinearResampler_Float32 {
  146. static void scale(const PixelData& src, const PixelData& dst) {
  147. size_t srcchannels = PixelUtil::getNumElemBytes(src.getFormat()) / sizeof(float);
  148. size_t dstchannels = PixelUtil::getNumElemBytes(dst.getFormat()) / sizeof(float);
  149. // assert(srcchannels == 3 || srcchannels == 4);
  150. // assert(dstchannels == 3 || dstchannels == 4);
  151. // srcdata stays at beginning, pdst is a moving pointer
  152. float* srcdata = (float*)src.getData();
  153. float* pdst = (float*)dst.getData();
  154. // sx_48,sy_48,sz_48 represent current position in source
  155. // using 16/48-bit fixed precision, incremented by steps
  156. UINT64 stepx = ((UINT64)src.getWidth() << 48) / dst.getWidth();
  157. UINT64 stepy = ((UINT64)src.getHeight() << 48) / dst.getHeight();
  158. UINT64 stepz = ((UINT64)src.getDepth() << 48) / dst.getDepth();
  159. // temp is 16/16 bit fixed precision, used to adjust a source
  160. // coordinate (x, y, or z) backwards by half a pixel so that the
  161. // integer bits represent the first sample (eg, sx1) and the
  162. // fractional bits are the blend weight of the second sample
  163. unsigned int temp;
  164. // note: ((stepz>>1) - 1) is an extra half-step increment to adjust
  165. // for the center of the destination pixel, not the top-left corner
  166. UINT64 sz_48 = (stepz >> 1) - 1;
  167. for (size_t z = dst.getFront(); z < dst.getBack(); z++, sz_48+=stepz) {
  168. temp = static_cast<unsigned int>(sz_48 >> 32);
  169. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  170. size_t sz1 = temp >> 16; // src z, sample #1
  171. size_t sz2 = std::min(sz1+1,(size_t)src.getDepth()-1);// src z, sample #2
  172. float szf = (temp & 0xFFFF) / 65536.f; // weight of sample #2
  173. UINT64 sy_48 = (stepy >> 1) - 1;
  174. for (size_t y = dst.getTop(); y < dst.getBottom(); y++, sy_48+=stepy) {
  175. temp = static_cast<unsigned int>(sy_48 >> 32);
  176. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  177. size_t sy1 = temp >> 16; // src y #1
  178. size_t sy2 = std::min(sy1+1,(size_t)src.getHeight()-1);// src y #2
  179. float syf = (temp & 0xFFFF) / 65536.f; // weight of #2
  180. UINT64 sx_48 = (stepx >> 1) - 1;
  181. for (size_t x = dst.getLeft(); x < dst.getRight(); x++, sx_48+=stepx) {
  182. temp = static_cast<unsigned int>(sx_48 >> 32);
  183. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  184. size_t sx1 = temp >> 16; // src x #1
  185. size_t sx2 = std::min(sx1+1,(size_t)src.getWidth()-1);// src x #2
  186. float sxf = (temp & 0xFFFF) / 65536.f; // weight of #2
  187. // process R,G,B,A simultaneously for cache coherence?
  188. float accum[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  189. #define ACCUM3(x,y,z,factor) \
  190. { float f = factor; \
  191. size_t off = (x+y*src.getRowPitch()+z*src.getSlicePitch())*srcchannels; \
  192. accum[0]+=srcdata[off+0]*f; accum[1]+=srcdata[off+1]*f; \
  193. accum[2]+=srcdata[off+2]*f; }
  194. #define ACCUM4(x,y,z,factor) \
  195. { float f = factor; \
  196. size_t off = (x+y*src.getRowPitch()+z*src.getSlicePitch())*srcchannels; \
  197. accum[0]+=srcdata[off+0]*f; accum[1]+=srcdata[off+1]*f; \
  198. accum[2]+=srcdata[off+2]*f; accum[3]+=srcdata[off+3]*f; }
  199. if (srcchannels == 3 || dstchannels == 3) {
  200. // RGB, no alpha
  201. ACCUM3(sx1,sy1,sz1,(1.0f-sxf)*(1.0f-syf)*(1.0f-szf));
  202. ACCUM3(sx2,sy1,sz1, sxf *(1.0f-syf)*(1.0f-szf));
  203. ACCUM3(sx1,sy2,sz1,(1.0f-sxf)* syf *(1.0f-szf));
  204. ACCUM3(sx2,sy2,sz1, sxf * syf *(1.0f-szf));
  205. ACCUM3(sx1,sy1,sz2,(1.0f-sxf)*(1.0f-syf)* szf );
  206. ACCUM3(sx2,sy1,sz2, sxf *(1.0f-syf)* szf );
  207. ACCUM3(sx1,sy2,sz2,(1.0f-sxf)* syf * szf );
  208. ACCUM3(sx2,sy2,sz2, sxf * syf * szf );
  209. accum[3] = 1.0f;
  210. } else {
  211. // RGBA
  212. ACCUM4(sx1,sy1,sz1,(1.0f-sxf)*(1.0f-syf)*(1.0f-szf));
  213. ACCUM4(sx2,sy1,sz1, sxf *(1.0f-syf)*(1.0f-szf));
  214. ACCUM4(sx1,sy2,sz1,(1.0f-sxf)* syf *(1.0f-szf));
  215. ACCUM4(sx2,sy2,sz1, sxf * syf *(1.0f-szf));
  216. ACCUM4(sx1,sy1,sz2,(1.0f-sxf)*(1.0f-syf)* szf );
  217. ACCUM4(sx2,sy1,sz2, sxf *(1.0f-syf)* szf );
  218. ACCUM4(sx1,sy2,sz2,(1.0f-sxf)* syf * szf );
  219. ACCUM4(sx2,sy2,sz2, sxf * syf * szf );
  220. }
  221. memcpy(pdst, accum, sizeof(float)*dstchannels);
  222. #undef ACCUM3
  223. #undef ACCUM4
  224. pdst += dstchannels;
  225. }
  226. pdst += dstchannels*dst.getRowSkip();
  227. }
  228. pdst += dstchannels*dst.getSliceSkip();
  229. }
  230. }
  231. };
  232. // byte linear resampler, does not do any format conversions.
  233. // only handles pixel formats that use 1 byte per color channel.
  234. // 2D only; punts 3D pixelboxes to default LinearResampler (slow).
  235. // templated on bytes-per-pixel to allow compiler optimizations, such
  236. // as unrolling loops and replacing multiplies with bitshifts
  237. template<unsigned int channels> struct LinearResampler_Byte {
  238. static void scale(const PixelData& src, const PixelData& dst) {
  239. // assert(src.format == dst.format);
  240. // only optimized for 2D
  241. if (src.getDepth() > 1 || dst.getDepth() > 1) {
  242. LinearResampler::scale(src, dst);
  243. return;
  244. }
  245. // srcdata stays at beginning of slice, pdst is a moving pointer
  246. UINT8* srcdata = (UINT8*)src.getData();
  247. UINT8* pdst = (UINT8*)dst.getData();
  248. // sx_48,sy_48 represent current position in source
  249. // using 16/48-bit fixed precision, incremented by steps
  250. UINT64 stepx = ((UINT64)src.getWidth() << 48) / dst.getWidth();
  251. UINT64 stepy = ((UINT64)src.getHeight() << 48) / dst.getHeight();
  252. // bottom 28 bits of temp are 16/12 bit fixed precision, used to
  253. // adjust a source coordinate backwards by half a pixel so that the
  254. // integer bits represent the first sample (eg, sx1) and the
  255. // fractional bits are the blend weight of the second sample
  256. unsigned int temp;
  257. UINT64 sy_48 = (stepy >> 1) - 1;
  258. for (size_t y = dst.getTop(); y < dst.getBottom(); y++, sy_48+=stepy) {
  259. temp = static_cast<unsigned int>(sy_48 >> 36);
  260. temp = (temp > 0x800)? temp - 0x800: 0;
  261. unsigned int syf = temp & 0xFFF;
  262. size_t sy1 = temp >> 12;
  263. size_t sy2 = std::min(sy1+1, (size_t)src.getBottom()-src.getTop()-1);
  264. size_t syoff1 = sy1 * src.getRowPitch();
  265. size_t syoff2 = sy2 * src.getRowPitch();
  266. UINT64 sx_48 = (stepx >> 1) - 1;
  267. for (size_t x = dst.getLeft(); x < dst.getRight(); x++, sx_48+=stepx) {
  268. temp = static_cast<unsigned int>(sx_48 >> 36);
  269. temp = (temp > 0x800)? temp - 0x800 : 0;
  270. unsigned int sxf = temp & 0xFFF;
  271. size_t sx1 = temp >> 12;
  272. size_t sx2 = std::min(sx1+1, (size_t)src.getRight()-src.getLeft()-1);
  273. unsigned int sxfsyf = sxf*syf;
  274. for (unsigned int k = 0; k < channels; k++) {
  275. unsigned int accum =
  276. srcdata[(sx1 + syoff1)*channels+k]*(0x1000000-(sxf<<12)-(syf<<12)+sxfsyf) +
  277. srcdata[(sx2 + syoff1)*channels+k]*((sxf<<12)-sxfsyf) +
  278. srcdata[(sx1 + syoff2)*channels+k]*((syf<<12)-sxfsyf) +
  279. srcdata[(sx2 + syoff2)*channels+k]*sxfsyf;
  280. // accum is computed using 8/24-bit fixed-point math
  281. // (maximum is 0xFF000000; rounding will not cause overflow)
  282. *pdst++ = static_cast<UINT8>((accum + 0x800000) >> 24);
  283. }
  284. }
  285. pdst += channels*dst.getRowSkip();
  286. }
  287. }
  288. };
  289. //-----------------------------------------------------------------------
  290. /**
  291. * A record that describes a pixel format in detail.
  292. */
  293. struct PixelFormatDescription {
  294. /* Name of the format, as in the enum */
  295. const char *name;
  296. /* Number of bytes one element (colour value) takes. */
  297. unsigned char elemBytes;
  298. /* Pixel format flags, see enum PixelFormatFlags for the bit field
  299. * definitions
  300. */
  301. UINT32 flags;
  302. /** Component type
  303. */
  304. PixelComponentType componentType;
  305. /** Component count
  306. */
  307. unsigned char componentCount;
  308. /* Number of bits for red(or luminance), green, blue, alpha
  309. */
  310. unsigned char rbits,gbits,bbits,abits; /*, ibits, dbits, ... */
  311. /* Masks and shifts as used by packers/unpackers */
  312. UINT32 rmask, gmask, bmask, amask;
  313. unsigned char rshift, gshift, bshift, ashift;
  314. };
  315. //-----------------------------------------------------------------------
  316. /** Pixel format database */
  317. PixelFormatDescription _pixelFormats[PF_COUNT] = {
  318. //-----------------------------------------------------------------------
  319. {"PF_UNKNOWN",
  320. /* Bytes per element */
  321. 0,
  322. /* Flags */
  323. 0,
  324. /* Component type and count */
  325. PCT_BYTE, 0,
  326. /* rbits, gbits, bbits, abits */
  327. 0, 0, 0, 0,
  328. /* Masks and shifts */
  329. 0, 0, 0, 0, 0, 0, 0, 0
  330. },
  331. //-----------------------------------------------------------------------
  332. {"PF_R8",
  333. /* Bytes per element */
  334. 1,
  335. /* Flags */
  336. 0,
  337. /* Component type and count */
  338. PCT_BYTE, 1,
  339. /* rbits, gbits, bbits, abits */
  340. 8, 0, 0, 0,
  341. /* Masks and shifts */
  342. 0x000000FF, 0, 0, 0,
  343. 0, 0, 0, 0
  344. },
  345. //-----------------------------------------------------------------------
  346. {"PF_R8G8",
  347. /* Bytes per element */
  348. 2,
  349. /* Flags */
  350. 0,
  351. /* Component type and count */
  352. PCT_BYTE, 2,
  353. /* rbits, gbits, bbits, abits */
  354. 8, 8, 0, 0,
  355. /* Masks and shifts */
  356. 0x000000FF, 0x0000FF00, 0, 0,
  357. 0, 8, 0, 0
  358. },
  359. //-----------------------------------------------------------------------
  360. {"PF_R8G8B8",
  361. /* Bytes per element */
  362. 3, // 24 bit integer -- special
  363. /* Flags */
  364. PFF_NATIVEENDIAN,
  365. /* Component type and count */
  366. PCT_BYTE, 3,
  367. /* rbits, gbits, bbits, abits */
  368. 8, 8, 8, 0,
  369. /* Masks and shifts */
  370. 0x000000FF, 0x0000FF00, 0x00FF0000, 0,
  371. 0, 8, 16, 0
  372. },
  373. //-----------------------------------------------------------------------
  374. {"PF_B8G8R8",
  375. /* Bytes per element */
  376. 3, // 24 bit integer -- special
  377. /* Flags */
  378. PFF_NATIVEENDIAN,
  379. /* Component type and count */
  380. PCT_BYTE, 3,
  381. /* rbits, gbits, bbits, abits */
  382. 8, 8, 8, 0,
  383. /* Masks and shifts */
  384. 0x00FF0000, 0x0000FF00, 0x000000FF, 0,
  385. 16, 8, 0, 0
  386. },
  387. //-----------------------------------------------------------------------
  388. {"PF_A8R8G8B8",
  389. /* Bytes per element */
  390. 4,
  391. /* Flags */
  392. PFF_HASALPHA | PFF_NATIVEENDIAN,
  393. /* Component type and count */
  394. PCT_BYTE, 4,
  395. /* rbits, gbits, bbits, abits */
  396. 8, 8, 8, 8,
  397. /* Masks and shifts */
  398. 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF,
  399. 8, 16, 24, 0
  400. },
  401. //-----------------------------------------------------------------------
  402. {"PF_A8B8G8R8",
  403. /* Bytes per element */
  404. 4,
  405. /* Flags */
  406. PFF_HASALPHA | PFF_NATIVEENDIAN,
  407. /* Component type and count */
  408. PCT_BYTE, 4,
  409. /* rbits, gbits, bbits, abits */
  410. 8, 8, 8, 8,
  411. /* Masks and shifts */
  412. 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF,
  413. 24, 16, 8, 0,
  414. },
  415. //-----------------------------------------------------------------------
  416. {"PF_B8G8R8A8",
  417. /* Bytes per element */
  418. 4,
  419. /* Flags */
  420. PFF_HASALPHA | PFF_NATIVEENDIAN,
  421. /* Component type and count */
  422. PCT_BYTE, 4,
  423. /* rbits, gbits, bbits, abits */
  424. 8, 8, 8, 8,
  425. /* Masks and shifts */
  426. 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000,
  427. 16, 8, 0, 24
  428. },
  429. //-----------------------------------------------------------------------
  430. {"PF_R8G8B8A8",
  431. /* Bytes per element */
  432. 4,
  433. /* Flags */
  434. PFF_HASALPHA | PFF_NATIVEENDIAN,
  435. /* Component type and count */
  436. PCT_BYTE, 4,
  437. /* rbits, gbits, bbits, abits */
  438. 8, 8, 8, 8,
  439. /* Masks and shifts */
  440. 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000,
  441. 0, 8, 16, 24
  442. },
  443. //-----------------------------------------------------------------------
  444. {"PF_X8R8G8B8",
  445. /* Bytes per element */
  446. 4,
  447. /* Flags */
  448. PFF_NATIVEENDIAN,
  449. /* Component type and count */
  450. PCT_BYTE, 3,
  451. /* rbits, gbits, bbits, abits */
  452. 8, 8, 8, 0,
  453. /* Masks and shifts */
  454. 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF,
  455. 8, 16, 24, 0
  456. },
  457. //-----------------------------------------------------------------------
  458. {"PF_X8B8G8R8",
  459. /* Bytes per element */
  460. 4,
  461. /* Flags */
  462. PFF_NATIVEENDIAN,
  463. /* Component type and count */
  464. PCT_BYTE, 3,
  465. /* rbits, gbits, bbits, abits */
  466. 8, 8, 8, 0,
  467. /* Masks and shifts */
  468. 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF,
  469. 24, 16, 8, 0
  470. },
  471. //-----------------------------------------------------------------------
  472. {"PF_R8G8B8X8",
  473. /* Bytes per element */
  474. 4,
  475. /* Flags */
  476. PFF_HASALPHA | PFF_NATIVEENDIAN,
  477. /* Component type and count */
  478. PCT_BYTE, 3,
  479. /* rbits, gbits, bbits, abits */
  480. 8, 8, 8, 0,
  481. /* Masks and shifts */
  482. 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000,
  483. 0, 8, 16, 0
  484. },
  485. //-----------------------------------------------------------------------
  486. {"PF_B8G8R8X8",
  487. /* Bytes per element */
  488. 4,
  489. /* Flags */
  490. PFF_HASALPHA | PFF_NATIVEENDIAN,
  491. /* Component type and count */
  492. PCT_BYTE, 3,
  493. /* rbits, gbits, bbits, abits */
  494. 8, 8, 8, 0,
  495. /* Masks and shifts */
  496. 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000,
  497. 16, 8, 0, 0
  498. },
  499. //-----------------------------------------------------------------------
  500. {"PF_DXT1",
  501. /* Bytes per element */
  502. 0,
  503. /* Flags */
  504. PFF_COMPRESSED | PFF_HASALPHA,
  505. /* Component type and count */
  506. PCT_BYTE, 3, // No alpha
  507. /* rbits, gbits, bbits, abits */
  508. 0, 0, 0, 0,
  509. /* Masks and shifts */
  510. 0, 0, 0, 0, 0, 0, 0, 0
  511. },
  512. //-----------------------------------------------------------------------
  513. {"PF_DXT2",
  514. /* Bytes per element */
  515. 0,
  516. /* Flags */
  517. PFF_COMPRESSED | PFF_HASALPHA,
  518. /* Component type and count */
  519. PCT_BYTE, 4,
  520. /* rbits, gbits, bbits, abits */
  521. 0, 0, 0, 0,
  522. /* Masks and shifts */
  523. 0, 0, 0, 0, 0, 0, 0, 0
  524. },
  525. //-----------------------------------------------------------------------
  526. {"PF_DXT3",
  527. /* Bytes per element */
  528. 0,
  529. /* Flags */
  530. PFF_COMPRESSED | PFF_HASALPHA,
  531. /* Component type and count */
  532. PCT_BYTE, 4,
  533. /* rbits, gbits, bbits, abits */
  534. 0, 0, 0, 0,
  535. /* Masks and shifts */
  536. 0, 0, 0, 0, 0, 0, 0, 0
  537. },
  538. //-----------------------------------------------------------------------
  539. {"PF_DXT4",
  540. /* Bytes per element */
  541. 0,
  542. /* Flags */
  543. PFF_COMPRESSED | PFF_HASALPHA,
  544. /* Component type and count */
  545. PCT_BYTE, 4,
  546. /* rbits, gbits, bbits, abits */
  547. 0, 0, 0, 0,
  548. /* Masks and shifts */
  549. 0, 0, 0, 0, 0, 0, 0, 0
  550. },
  551. //-----------------------------------------------------------------------
  552. {"PF_DXT5",
  553. /* Bytes per element */
  554. 0,
  555. /* Flags */
  556. PFF_COMPRESSED | PFF_HASALPHA,
  557. /* Component type and count */
  558. PCT_BYTE, 4,
  559. /* rbits, gbits, bbits, abits */
  560. 0, 0, 0, 0,
  561. /* Masks and shifts */
  562. 0, 0, 0, 0, 0, 0, 0, 0
  563. },
  564. //-----------------------------------------------------------------------
  565. {"PF_FLOAT16_R",
  566. /* Bytes per element */
  567. 2,
  568. /* Flags */
  569. PFF_FLOAT,
  570. /* Component type and count */
  571. PCT_FLOAT16, 1,
  572. /* rbits, gbits, bbits, abits */
  573. 16, 0, 0, 0,
  574. /* Masks and shifts */
  575. 0, 0, 0, 0, 0, 0, 0, 0
  576. },
  577. //-----------------------------------------------------------------------
  578. {"PF_FLOAT16_RG",
  579. /* Bytes per element */
  580. 4,
  581. /* Flags */
  582. PFF_FLOAT,
  583. /* Component type and count */
  584. PCT_FLOAT16, 2,
  585. /* rbits, gbits, bbits, abits */
  586. 16, 16, 0, 0,
  587. /* Masks and shifts */
  588. 0, 0, 0, 0, 0, 0, 0, 0
  589. },
  590. //-----------------------------------------------------------------------
  591. {"PF_FLOAT16_RGB",
  592. /* Bytes per element */
  593. 6,
  594. /* Flags */
  595. PFF_FLOAT,
  596. /* Component type and count */
  597. PCT_FLOAT16, 3,
  598. /* rbits, gbits, bbits, abits */
  599. 16, 16, 16, 0,
  600. /* Masks and shifts */
  601. 0, 0, 0, 0, 0, 0, 0, 0
  602. },
  603. //-----------------------------------------------------------------------
  604. {"PF_FLOAT16_RGBA",
  605. /* Bytes per element */
  606. 8,
  607. /* Flags */
  608. PFF_FLOAT | PFF_HASALPHA,
  609. /* Component type and count */
  610. PCT_FLOAT16, 4,
  611. /* rbits, gbits, bbits, abits */
  612. 16, 16, 16, 16,
  613. /* Masks and shifts */
  614. 0, 0, 0, 0, 0, 0, 0, 0
  615. },
  616. //-----------------------------------------------------------------------
  617. {"PF_FLOAT32_R",
  618. /* Bytes per element */
  619. 4,
  620. /* Flags */
  621. PFF_FLOAT,
  622. /* Component type and count */
  623. PCT_FLOAT32, 1,
  624. /* rbits, gbits, bbits, abits */
  625. 32, 0, 0, 0,
  626. /* Masks and shifts */
  627. 0, 0, 0, 0, 0, 0, 0, 0
  628. },
  629. //-----------------------------------------------------------------------
  630. {"PF_FLOAT32_RG",
  631. /* Bytes per element */
  632. 8,
  633. /* Flags */
  634. PFF_FLOAT,
  635. /* Component type and count */
  636. PCT_FLOAT32, 2,
  637. /* rbits, gbits, bbits, abits */
  638. 32, 32, 0, 0,
  639. /* Masks and shifts */
  640. 0, 0, 0, 0, 0, 0, 0, 0
  641. },
  642. //-----------------------------------------------------------------------
  643. {"PF_FLOAT32_RGB",
  644. /* Bytes per element */
  645. 12,
  646. /* Flags */
  647. PFF_FLOAT,
  648. /* Component type and count */
  649. PCT_FLOAT32, 3,
  650. /* rbits, gbits, bbits, abits */
  651. 32, 32, 32, 0,
  652. /* Masks and shifts */
  653. 0, 0, 0, 0, 0, 0, 0, 0
  654. },
  655. //-----------------------------------------------------------------------
  656. {"PF_FLOAT32_RGBA",
  657. /* Bytes per element */
  658. 16,
  659. /* Flags */
  660. PFF_FLOAT | PFF_HASALPHA,
  661. /* Component type and count */
  662. PCT_FLOAT32, 4,
  663. /* rbits, gbits, bbits, abits */
  664. 32, 32, 32, 32,
  665. /* Masks and shifts */
  666. 0, 0, 0, 0, 0, 0, 0, 0
  667. },
  668. //-----------------------------------------------------------------------
  669. {"PF_D32_S8X24",
  670. /* Bytes per element */
  671. 4,
  672. /* Flags */
  673. PFF_DEPTH | PFF_FLOAT,
  674. /* Component type and count */
  675. PCT_FLOAT32, 1,
  676. /* rbits, gbits, bbits, abits */
  677. 0, 0, 0, 0,
  678. /* Masks and shifts */
  679. 0, 0, 0, 0, 0, 0, 0, 0
  680. },
  681. //-----------------------------------------------------------------------
  682. {"PF_D24_S8",
  683. /* Bytes per element */
  684. 8,
  685. /* Flags */
  686. PFF_DEPTH | PFF_FLOAT,
  687. /* Component type and count */
  688. PCT_FLOAT32, 2,
  689. /* rbits, gbits, bbits, abits */
  690. 0, 0, 0, 0,
  691. /* Masks and shifts */
  692. 0, 0, 0, 0, 0, 0, 0, 0
  693. },
  694. //-----------------------------------------------------------------------
  695. {"PF_D32",
  696. /* Bytes per element */
  697. 4,
  698. /* Flags */
  699. PFF_DEPTH | PFF_FLOAT,
  700. /* Component type and count */
  701. PCT_FLOAT32, 1,
  702. /* rbits, gbits, bbits, abits */
  703. 0, 0, 0, 0,
  704. /* Masks and shifts */
  705. 0, 0, 0, 0, 0, 0, 0, 0
  706. },
  707. //-----------------------------------------------------------------------
  708. {"PF_D16",
  709. /* Bytes per element */
  710. 2,
  711. /* Flags */
  712. PFF_DEPTH | PFF_FLOAT,
  713. /* Component type and count */
  714. PCT_FLOAT16, 1,
  715. /* rbits, gbits, bbits, abits */
  716. 0, 0, 0, 0,
  717. /* Masks and shifts */
  718. 0, 0, 0, 0, 0, 0, 0, 0
  719. },
  720. };
  721. //-----------------------------------------------------------------------
  722. UINT32 PixelData::getConsecutiveSize() const
  723. {
  724. return PixelUtil::getMemorySize(getWidth(), getHeight(), getDepth(), mFormat);
  725. }
  726. PixelData PixelData::getSubVolume(const Box &def) const
  727. {
  728. if(PixelUtil::isCompressed(mFormat))
  729. {
  730. if(def.left == getLeft() && def.top == getTop() && def.front == getFront() &&
  731. def.right == getRight() && def.bottom == getBottom() && def.back == getBack())
  732. {
  733. // Entire buffer is being queried
  734. return *this;
  735. }
  736. CM_EXCEPT(InvalidParametersException, "Cannot return subvolume of compressed PixelBuffer");
  737. }
  738. if(!mExtents.contains(def))
  739. {
  740. CM_EXCEPT(InvalidParametersException, "Bounds out of range");
  741. }
  742. const size_t elemSize = PixelUtil::getNumElemBytes(mFormat);
  743. // Calculate new data origin
  744. // Notice how we do not propagate left/top/front from the incoming box, since
  745. // the returned pointer is already offset
  746. PixelData rval(def.getWidth(), def.getHeight(), def.getDepth(), mFormat);
  747. rval.setExternalBuffer(((UINT8*)getData()) + ((def.left-getLeft())*elemSize)
  748. + ((def.top-getTop())*mRowPitch*elemSize)
  749. + ((def.front-getFront())*mSlicePitch*elemSize));
  750. rval.mRowPitch = mRowPitch;
  751. rval.mSlicePitch = mSlicePitch;
  752. rval.mFormat = mFormat;
  753. return rval;
  754. }
  755. //-----------------------------------------------------------------------
  756. /**
  757. * Directly get the description record for provided pixel format. For debug builds,
  758. * this checks the bounds of fmt with an assertion.
  759. */
  760. static inline const PixelFormatDescription &getDescriptionFor(const PixelFormat fmt)
  761. {
  762. const int ord = (int)fmt;
  763. assert(ord>=0 && ord<PF_COUNT);
  764. return _pixelFormats[ord];
  765. }
  766. //-----------------------------------------------------------------------
  767. UINT32 PixelUtil::getNumElemBytes( PixelFormat format )
  768. {
  769. return getDescriptionFor(format).elemBytes;
  770. }
  771. //-----------------------------------------------------------------------
  772. UINT32 PixelUtil::getMemorySize(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  773. {
  774. if(isCompressed(format))
  775. {
  776. switch(format)
  777. {
  778. // DXT formats work by dividing the image into 4x4 blocks, then encoding each
  779. // 4x4 block with a certain number of bytes.
  780. case PF_DXT1:
  781. return ((width+3)/4)*((height+3)/4)*8 * depth;
  782. case PF_DXT2:
  783. case PF_DXT3:
  784. case PF_DXT4:
  785. case PF_DXT5:
  786. return ((width+3)/4)*((height+3)/4)*16 * depth;
  787. default:
  788. CM_EXCEPT(InvalidParametersException, "Invalid compressed pixel format");
  789. }
  790. }
  791. else
  792. {
  793. return width*height*depth*getNumElemBytes(format);
  794. }
  795. }
  796. //-----------------------------------------------------------------------
  797. UINT32 PixelUtil::getNumElemBits( PixelFormat format )
  798. {
  799. return getDescriptionFor(format).elemBytes * 8;
  800. }
  801. //-----------------------------------------------------------------------
  802. unsigned int PixelUtil::getFlags( PixelFormat format )
  803. {
  804. return getDescriptionFor(format).flags;
  805. }
  806. //-----------------------------------------------------------------------
  807. bool PixelUtil::hasAlpha(PixelFormat format)
  808. {
  809. return (PixelUtil::getFlags(format) & PFF_HASALPHA) > 0;
  810. }
  811. //-----------------------------------------------------------------------
  812. bool PixelUtil::isFloatingPoint(PixelFormat format)
  813. {
  814. return (PixelUtil::getFlags(format) & PFF_FLOAT) > 0;
  815. }
  816. //-----------------------------------------------------------------------
  817. bool PixelUtil::isCompressed(PixelFormat format)
  818. {
  819. return (PixelUtil::getFlags(format) & PFF_COMPRESSED) > 0;
  820. }
  821. //-----------------------------------------------------------------------
  822. bool PixelUtil::isDepth(PixelFormat format)
  823. {
  824. return (PixelUtil::getFlags(format) & PFF_DEPTH) > 0;
  825. }
  826. //-----------------------------------------------------------------------
  827. bool PixelUtil::isNativeEndian(PixelFormat format)
  828. {
  829. return (PixelUtil::getFlags(format) & PFF_NATIVEENDIAN) > 0;
  830. }
  831. //-----------------------------------------------------------------------
  832. bool PixelUtil::isValidExtent(size_t width, size_t height, size_t depth, PixelFormat format)
  833. {
  834. if(isCompressed(format))
  835. {
  836. switch(format)
  837. {
  838. case PF_DXT1:
  839. case PF_DXT2:
  840. case PF_DXT3:
  841. case PF_DXT4:
  842. case PF_DXT5:
  843. return ((width&3)==0 && (height&3)==0 && depth==1);
  844. default:
  845. return true;
  846. }
  847. }
  848. else
  849. {
  850. return true;
  851. }
  852. }
  853. //-----------------------------------------------------------------------
  854. void PixelUtil::getBitDepths(PixelFormat format, int rgba[4])
  855. {
  856. const PixelFormatDescription &des = getDescriptionFor(format);
  857. rgba[0] = des.rbits;
  858. rgba[1] = des.gbits;
  859. rgba[2] = des.bbits;
  860. rgba[3] = des.abits;
  861. }
  862. //-----------------------------------------------------------------------
  863. void PixelUtil::getBitMasks(PixelFormat format, UINT32 rgba[4])
  864. {
  865. const PixelFormatDescription &des = getDescriptionFor(format);
  866. rgba[0] = des.rmask;
  867. rgba[1] = des.gmask;
  868. rgba[2] = des.bmask;
  869. rgba[3] = des.amask;
  870. }
  871. //---------------------------------------------------------------------
  872. void PixelUtil::getBitShifts(PixelFormat format, unsigned char rgba[4])
  873. {
  874. const PixelFormatDescription &des = getDescriptionFor(format);
  875. rgba[0] = des.rshift;
  876. rgba[1] = des.gshift;
  877. rgba[2] = des.bshift;
  878. rgba[3] = des.ashift;
  879. }
  880. //-----------------------------------------------------------------------
  881. String PixelUtil::getFormatName(PixelFormat srcformat)
  882. {
  883. return getDescriptionFor(srcformat).name;
  884. }
  885. //-----------------------------------------------------------------------
  886. bool PixelUtil::isAccessible(PixelFormat srcformat)
  887. {
  888. if (srcformat == PF_UNKNOWN)
  889. return false;
  890. unsigned int flags = getFlags(srcformat);
  891. return !((flags & PFF_COMPRESSED) || (flags & PFF_DEPTH));
  892. }
  893. //-----------------------------------------------------------------------
  894. PixelComponentType PixelUtil::getComponentType(PixelFormat fmt)
  895. {
  896. const PixelFormatDescription &des = getDescriptionFor(fmt);
  897. return des.componentType;
  898. }
  899. //-----------------------------------------------------------------------
  900. UINT32 PixelUtil::getComponentCount(PixelFormat fmt)
  901. {
  902. const PixelFormatDescription &des = getDescriptionFor(fmt);
  903. return des.componentCount;
  904. }
  905. //-----------------------------------------------------------------------
  906. PixelFormat PixelUtil::getFormatFromName(const String& name, bool accessibleOnly, bool caseSensitive)
  907. {
  908. String tmp = name;
  909. if (!caseSensitive)
  910. {
  911. // We are stored upper-case format names.
  912. StringUtil::toUpperCase(tmp);
  913. }
  914. for (int i = 0; i < PF_COUNT; ++i)
  915. {
  916. PixelFormat pf = static_cast<PixelFormat>(i);
  917. if (!accessibleOnly || isAccessible(pf))
  918. {
  919. if (tmp == getFormatName(pf))
  920. return pf;
  921. }
  922. }
  923. return PF_UNKNOWN;
  924. }
  925. //-----------------------------------------------------------------------
  926. String PixelUtil::getBNFExpressionOfPixelFormats(bool accessibleOnly)
  927. {
  928. // Collect format names sorted by length, it's required by BNF compiler
  929. // that similar tokens need longer ones comes first.
  930. typedef multimap<String::size_type, String>::type FormatNameMap;
  931. FormatNameMap formatNames;
  932. for (size_t i = 0; i < PF_COUNT; ++i)
  933. {
  934. PixelFormat pf = static_cast<PixelFormat>(i);
  935. if (!accessibleOnly || isAccessible(pf))
  936. {
  937. String formatName = getFormatName(pf);
  938. formatNames.insert(std::make_pair(formatName.length(), formatName));
  939. }
  940. }
  941. // Populate the BNF expression in reverse order
  942. String result;
  943. // Note: Stupid M$ VC7.1 can't dealing operator!= with FormatNameMap::const_reverse_iterator.
  944. for (FormatNameMap::reverse_iterator j = formatNames.rbegin(); j != formatNames.rend(); ++j)
  945. {
  946. if (!result.empty())
  947. result += " | ";
  948. result += "'" + j->second + "'";
  949. }
  950. return result;
  951. }
  952. //-----------------------------------------------------------------------
  953. /*************************************************************************
  954. * Pixel packing/unpacking utilities
  955. */
  956. void PixelUtil::packColour(const Color &colour, const PixelFormat pf, void* dest)
  957. {
  958. packColour(colour.r, colour.g, colour.b, colour.a, pf, dest);
  959. }
  960. //-----------------------------------------------------------------------
  961. void PixelUtil::packColour(const UINT8 r, const UINT8 g, const UINT8 b, const UINT8 a, const PixelFormat pf, void* dest)
  962. {
  963. const PixelFormatDescription &des = getDescriptionFor(pf);
  964. if(des.flags & PFF_NATIVEENDIAN) {
  965. // Shortcut for integer formats packing
  966. unsigned int value = ((Bitwise::fixedToFixed(r, 8, des.rbits)<<des.rshift) & des.rmask) |
  967. ((Bitwise::fixedToFixed(g, 8, des.gbits)<<des.gshift) & des.gmask) |
  968. ((Bitwise::fixedToFixed(b, 8, des.bbits)<<des.bshift) & des.bmask) |
  969. ((Bitwise::fixedToFixed(a, 8, des.abits)<<des.ashift) & des.amask);
  970. // And write to memory
  971. Bitwise::intWrite(dest, des.elemBytes, value);
  972. } else {
  973. // Convert to float
  974. packColour((float)r/255.0f,(float)g/255.0f,(float)b/255.0f,(float)a/255.0f, pf, dest);
  975. }
  976. }
  977. //-----------------------------------------------------------------------
  978. void PixelUtil::packColour(const float r, const float g, const float b, const float a, const PixelFormat pf, void* dest)
  979. {
  980. // Catch-it-all here
  981. const PixelFormatDescription &des = getDescriptionFor(pf);
  982. if(des.flags & PFF_NATIVEENDIAN) {
  983. // Do the packing
  984. //std::cerr << dest << " " << r << " " << g << " " << b << " " << a << std::endl;
  985. const unsigned int value = ((Bitwise::floatToFixed(r, des.rbits)<<des.rshift) & des.rmask) |
  986. ((Bitwise::floatToFixed(g, des.gbits)<<des.gshift) & des.gmask) |
  987. ((Bitwise::floatToFixed(b, des.bbits)<<des.bshift) & des.bmask) |
  988. ((Bitwise::floatToFixed(a, des.abits)<<des.ashift) & des.amask);
  989. // And write to memory
  990. Bitwise::intWrite(dest, des.elemBytes, value);
  991. } else {
  992. switch(pf)
  993. {
  994. case PF_FLOAT32_R:
  995. ((float*)dest)[0] = r;
  996. break;
  997. case PF_FLOAT32_RG:
  998. ((float*)dest)[0] = r;
  999. ((float*)dest)[1] = g;
  1000. break;
  1001. case PF_FLOAT32_RGB:
  1002. ((float*)dest)[0] = r;
  1003. ((float*)dest)[1] = g;
  1004. ((float*)dest)[2] = b;
  1005. break;
  1006. case PF_FLOAT32_RGBA:
  1007. ((float*)dest)[0] = r;
  1008. ((float*)dest)[1] = g;
  1009. ((float*)dest)[2] = b;
  1010. ((float*)dest)[3] = a;
  1011. break;
  1012. case PF_FLOAT16_R:
  1013. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1014. break;
  1015. case PF_FLOAT16_RG:
  1016. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1017. ((UINT16*)dest)[1] = Bitwise::floatToHalf(g);
  1018. break;
  1019. case PF_FLOAT16_RGB:
  1020. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1021. ((UINT16*)dest)[1] = Bitwise::floatToHalf(g);
  1022. ((UINT16*)dest)[2] = Bitwise::floatToHalf(b);
  1023. break;
  1024. case PF_FLOAT16_RGBA:
  1025. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1026. ((UINT16*)dest)[1] = Bitwise::floatToHalf(g);
  1027. ((UINT16*)dest)[2] = Bitwise::floatToHalf(b);
  1028. ((UINT16*)dest)[3] = Bitwise::floatToHalf(a);
  1029. break;
  1030. case PF_R8G8:
  1031. ((UINT8*)dest)[0] = (UINT8)Bitwise::floatToFixed(r, 8);
  1032. ((UINT8*)dest)[1] = (UINT8)Bitwise::floatToFixed(g, 8);
  1033. break;
  1034. case PF_R8:
  1035. ((UINT8*)dest)[0] = (UINT8)Bitwise::floatToFixed(r, 8);
  1036. break;
  1037. default:
  1038. // Not yet supported
  1039. CM_EXCEPT(NotImplementedException,
  1040. "pack to "+getFormatName(pf)+" not implemented");
  1041. break;
  1042. }
  1043. }
  1044. }
  1045. //-----------------------------------------------------------------------
  1046. void PixelUtil::unpackColour(Color *colour, PixelFormat pf, const void* src)
  1047. {
  1048. unpackColour(&colour->r, &colour->g, &colour->b, &colour->a, pf, src);
  1049. }
  1050. //-----------------------------------------------------------------------
  1051. void PixelUtil::unpackColour(UINT8 *r, UINT8 *g, UINT8 *b, UINT8 *a, PixelFormat pf, const void* src)
  1052. {
  1053. const PixelFormatDescription &des = getDescriptionFor(pf);
  1054. if(des.flags & PFF_NATIVEENDIAN) {
  1055. // Shortcut for integer formats unpacking
  1056. const unsigned int value = Bitwise::intRead(src, des.elemBytes);
  1057. *r = (UINT8)Bitwise::fixedToFixed((value & des.rmask)>>des.rshift, des.rbits, 8);
  1058. *g = (UINT8)Bitwise::fixedToFixed((value & des.gmask)>>des.gshift, des.gbits, 8);
  1059. *b = (UINT8)Bitwise::fixedToFixed((value & des.bmask)>>des.bshift, des.bbits, 8);
  1060. if(des.flags & PFF_HASALPHA)
  1061. {
  1062. *a = (UINT8)Bitwise::fixedToFixed((value & des.amask)>>des.ashift, des.abits, 8);
  1063. }
  1064. else
  1065. {
  1066. *a = 255; // No alpha, default a component to full
  1067. }
  1068. } else {
  1069. // Do the operation with the more generic floating point
  1070. float rr, gg, bb, aa;
  1071. unpackColour(&rr,&gg,&bb,&aa, pf, src);
  1072. *r = (UINT8)Bitwise::floatToFixed(rr, 8);
  1073. *g = (UINT8)Bitwise::floatToFixed(gg, 8);
  1074. *b = (UINT8)Bitwise::floatToFixed(bb, 8);
  1075. *a = (UINT8)Bitwise::floatToFixed(aa, 8);
  1076. }
  1077. }
  1078. //-----------------------------------------------------------------------
  1079. void PixelUtil::unpackColour(float *r, float *g, float *b, float *a,
  1080. PixelFormat pf, const void* src)
  1081. {
  1082. const PixelFormatDescription &des = getDescriptionFor(pf);
  1083. if(des.flags & PFF_NATIVEENDIAN) {
  1084. // Shortcut for integer formats unpacking
  1085. const unsigned int value = Bitwise::intRead(src, des.elemBytes);
  1086. *r = Bitwise::fixedToFloat((value & des.rmask)>>des.rshift, des.rbits);
  1087. *g = Bitwise::fixedToFloat((value & des.gmask)>>des.gshift, des.gbits);
  1088. *b = Bitwise::fixedToFloat((value & des.bmask)>>des.bshift, des.bbits);
  1089. if(des.flags & PFF_HASALPHA)
  1090. {
  1091. *a = Bitwise::fixedToFloat((value & des.amask)>>des.ashift, des.abits);
  1092. }
  1093. else
  1094. {
  1095. *a = 1.0f; // No alpha, default a component to full
  1096. }
  1097. } else {
  1098. switch(pf)
  1099. {
  1100. case PF_FLOAT32_R:
  1101. *r = *g = *b = ((float*)src)[0];
  1102. *a = 1.0f;
  1103. break;
  1104. case PF_FLOAT32_RG:
  1105. *r = ((float*)src)[0];
  1106. *g = *b = ((float*)src)[1];
  1107. *a = 1.0f;
  1108. break;
  1109. case PF_FLOAT32_RGB:
  1110. *r = ((float*)src)[0];
  1111. *g = ((float*)src)[1];
  1112. *b = ((float*)src)[2];
  1113. *a = 1.0f;
  1114. break;
  1115. case PF_FLOAT32_RGBA:
  1116. *r = ((float*)src)[0];
  1117. *g = ((float*)src)[1];
  1118. *b = ((float*)src)[2];
  1119. *a = ((float*)src)[3];
  1120. break;
  1121. case PF_FLOAT16_R:
  1122. *r = *g = *b = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1123. *a = 1.0f;
  1124. break;
  1125. case PF_FLOAT16_RG:
  1126. *r = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1127. *g = *b = Bitwise::halfToFloat(((UINT16*)src)[1]);
  1128. *a = 1.0f;
  1129. break;
  1130. case PF_FLOAT16_RGB:
  1131. *r = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1132. *g = Bitwise::halfToFloat(((UINT16*)src)[1]);
  1133. *b = Bitwise::halfToFloat(((UINT16*)src)[2]);
  1134. *a = 1.0f;
  1135. break;
  1136. case PF_FLOAT16_RGBA:
  1137. *r = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1138. *g = Bitwise::halfToFloat(((UINT16*)src)[1]);
  1139. *b = Bitwise::halfToFloat(((UINT16*)src)[2]);
  1140. *a = Bitwise::halfToFloat(((UINT16*)src)[3]);
  1141. break;
  1142. case PF_R8G8:
  1143. *r = Bitwise::fixedToFloat(((UINT8*)src)[0], 8);
  1144. *g = Bitwise::fixedToFloat(((UINT8*)src)[1], 8);
  1145. *b = 0.0f;
  1146. *a = 1.0f;
  1147. break;
  1148. case PF_R8:
  1149. *r = Bitwise::fixedToFloat(((UINT8*)src)[0], 8);
  1150. *g = 0.0f;
  1151. *b = 0.0f;
  1152. *a = 1.0f;
  1153. break;
  1154. default:
  1155. // Not yet supported
  1156. CM_EXCEPT(NotImplementedException,
  1157. "unpack from "+getFormatName(pf)+" not implemented");
  1158. break;
  1159. }
  1160. }
  1161. }
  1162. //-----------------------------------------------------------------------
  1163. void PixelUtil::bulkPixelConversion(const PixelData &src, const PixelData &dst)
  1164. {
  1165. assert(src.getWidth() == dst.getWidth() &&
  1166. src.getHeight() == dst.getHeight() &&
  1167. src.getDepth() == dst.getDepth());
  1168. // Check for compressed formats, we don't support decompression, compression or recoding
  1169. if(PixelUtil::isCompressed(src.getFormat()) || PixelUtil::isCompressed(dst.getFormat()))
  1170. {
  1171. if(src.getFormat() == dst.getFormat())
  1172. {
  1173. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1174. return;
  1175. }
  1176. else
  1177. {
  1178. CM_EXCEPT(NotImplementedException, "This method can not be used to compress or decompress images");
  1179. }
  1180. }
  1181. // The easy case
  1182. if(src.getFormat() == dst.getFormat()) {
  1183. // Everything consecutive?
  1184. if(src.isConsecutive() && dst.isConsecutive())
  1185. {
  1186. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1187. return;
  1188. }
  1189. const size_t srcPixelSize = PixelUtil::getNumElemBytes(src.getFormat());
  1190. const size_t dstPixelSize = PixelUtil::getNumElemBytes(dst.getFormat());
  1191. UINT8 *srcptr = static_cast<UINT8*>(src.getData())
  1192. + (src.getLeft() + src.getTop() * src.getRowPitch() + src.getFront() * src.getSlicePitch()) * srcPixelSize;
  1193. UINT8 *dstptr = static_cast<UINT8*>(dst.getData())
  1194. + (dst.getLeft() + dst.getTop() * dst.getRowPitch() + dst.getFront() * dst.getSlicePitch()) * dstPixelSize;
  1195. // Calculate pitches+skips in bytes
  1196. const size_t srcRowPitchBytes = src.getRowPitch()*srcPixelSize;
  1197. //const size_t srcRowSkipBytes = src.getRowSkip()*srcPixelSize;
  1198. const size_t srcSliceSkipBytes = src.getSliceSkip()*srcPixelSize;
  1199. const size_t dstRowPitchBytes = dst.getRowPitch()*dstPixelSize;
  1200. //const size_t dstRowSkipBytes = dst.getRowSkip()*dstPixelSize;
  1201. const size_t dstSliceSkipBytes = dst.getSliceSkip()*dstPixelSize;
  1202. // Otherwise, copy per row
  1203. const size_t rowSize = src.getWidth()*srcPixelSize;
  1204. for(size_t z=src.getFront(); z<src.getBack(); z++)
  1205. {
  1206. for(size_t y=src.getTop(); y<src.getBottom(); y++)
  1207. {
  1208. memcpy(dstptr, srcptr, rowSize);
  1209. srcptr += srcRowPitchBytes;
  1210. dstptr += dstRowPitchBytes;
  1211. }
  1212. srcptr += srcSliceSkipBytes;
  1213. dstptr += dstSliceSkipBytes;
  1214. }
  1215. return;
  1216. }
  1217. // Converting to PF_X8R8G8B8 is exactly the same as converting to
  1218. // PF_A8R8G8B8. (same with PF_X8B8G8R8 and PF_A8B8G8R8)
  1219. if(dst.getFormat() == PF_X8R8G8B8 || dst.getFormat() == PF_X8B8G8R8)
  1220. {
  1221. // Do the same conversion, with PF_A8R8G8B8, which has a lot of
  1222. // optimized conversions
  1223. PixelFormat tempFormat = dst.getFormat()==PF_X8R8G8B8?PF_A8R8G8B8:PF_A8B8G8R8;
  1224. PixelData tempdst(dst.getWidth(), dst.getHeight(), dst.getDepth(), tempFormat);
  1225. bulkPixelConversion(src, tempdst);
  1226. return;
  1227. }
  1228. // Converting from PF_X8R8G8B8 is exactly the same as converting from
  1229. // PF_A8R8G8B8, given that the destination format does not have alpha.
  1230. if((src.getFormat() == PF_X8R8G8B8||src.getFormat() == PF_X8B8G8R8) && !hasAlpha(dst.getFormat()))
  1231. {
  1232. // Do the same conversion, with PF_A8R8G8B8, which has a lot of
  1233. // optimized conversions
  1234. PixelFormat tempFormat = src.getFormat()==PF_X8R8G8B8?PF_A8R8G8B8:PF_A8B8G8R8;
  1235. PixelData tempsrc(src.getWidth(), src.getHeight(), src.getDepth(), tempFormat);
  1236. tempsrc.setExternalBuffer(src.getData());
  1237. bulkPixelConversion(tempsrc, dst);
  1238. return;
  1239. }
  1240. const size_t srcPixelSize = PixelUtil::getNumElemBytes(src.getFormat());
  1241. const size_t dstPixelSize = PixelUtil::getNumElemBytes(dst.getFormat());
  1242. UINT8 *srcptr = static_cast<UINT8*>(src.getData())
  1243. + (src.getLeft() + src.getTop() * src.getRowPitch() + src.getFront() * src.getSlicePitch()) * srcPixelSize;
  1244. UINT8 *dstptr = static_cast<UINT8*>(dst.getData())
  1245. + (dst.getLeft() + dst.getTop() * dst.getRowPitch() + dst.getFront() * dst.getSlicePitch()) * dstPixelSize;
  1246. // Old way, not taking into account box dimensions
  1247. //UINT8 *srcptr = static_cast<UINT8*>(src.data), *dstptr = static_cast<UINT8*>(dst.data);
  1248. // Calculate pitches+skips in bytes
  1249. const size_t srcRowSkipBytes = src.getRowSkip()*srcPixelSize;
  1250. const size_t srcSliceSkipBytes = src.getSliceSkip()*srcPixelSize;
  1251. const size_t dstRowSkipBytes = dst.getRowSkip()*dstPixelSize;
  1252. const size_t dstSliceSkipBytes = dst.getSliceSkip()*dstPixelSize;
  1253. // The brute force fallback
  1254. float r,g,b,a;
  1255. for(size_t z=src.getFront(); z<src.getBack(); z++)
  1256. {
  1257. for(size_t y=src.getTop(); y<src.getBottom(); y++)
  1258. {
  1259. for(size_t x=src.getLeft(); x<src.getRight(); x++)
  1260. {
  1261. unpackColour(&r, &g, &b, &a, src.getFormat(), srcptr);
  1262. packColour(r, g, b, a, dst.getFormat(), dstptr);
  1263. srcptr += srcPixelSize;
  1264. dstptr += dstPixelSize;
  1265. }
  1266. srcptr += srcRowSkipBytes;
  1267. dstptr += dstRowSkipBytes;
  1268. }
  1269. srcptr += srcSliceSkipBytes;
  1270. dstptr += dstSliceSkipBytes;
  1271. }
  1272. }
  1273. void PixelUtil::scale(const PixelData &src, const PixelData &scaled, Filter filter)
  1274. {
  1275. assert(PixelUtil::isAccessible(src.getFormat()));
  1276. assert(PixelUtil::isAccessible(scaled.getFormat()));
  1277. PixelData temp;
  1278. switch (filter)
  1279. {
  1280. default:
  1281. case FILTER_NEAREST:
  1282. if(src.getFormat() == scaled.getFormat())
  1283. {
  1284. // No intermediate buffer needed
  1285. temp = scaled;
  1286. }
  1287. else
  1288. {
  1289. // Allocate temporary buffer of destination size in source format
  1290. temp = PixelData(scaled.getWidth(), scaled.getHeight(), scaled.getDepth(), src.getFormat());
  1291. temp.allocateInternalBuffer();
  1292. }
  1293. // super-optimized: no conversion
  1294. switch (PixelUtil::getNumElemBytes(src.getFormat()))
  1295. {
  1296. case 1: NearestResampler<1>::scale(src, temp); break;
  1297. case 2: NearestResampler<2>::scale(src, temp); break;
  1298. case 3: NearestResampler<3>::scale(src, temp); break;
  1299. case 4: NearestResampler<4>::scale(src, temp); break;
  1300. case 6: NearestResampler<6>::scale(src, temp); break;
  1301. case 8: NearestResampler<8>::scale(src, temp); break;
  1302. case 12: NearestResampler<12>::scale(src, temp); break;
  1303. case 16: NearestResampler<16>::scale(src, temp); break;
  1304. default:
  1305. // never reached
  1306. assert(false);
  1307. }
  1308. if(temp.getData() != scaled.getData())
  1309. {
  1310. // Blit temp buffer
  1311. PixelUtil::bulkPixelConversion(temp, scaled);
  1312. temp.freeInternalBuffer();
  1313. }
  1314. break;
  1315. case FILTER_LINEAR:
  1316. case FILTER_BILINEAR:
  1317. switch (src.getFormat())
  1318. {
  1319. case PF_R8G8:
  1320. case PF_R8G8B8: case PF_B8G8R8:
  1321. case PF_R8G8B8A8: case PF_B8G8R8A8:
  1322. case PF_A8B8G8R8: case PF_A8R8G8B8:
  1323. case PF_X8B8G8R8: case PF_X8R8G8B8:
  1324. if(src.getFormat() == scaled.getFormat())
  1325. {
  1326. // No intermediate buffer needed
  1327. temp = scaled;
  1328. }
  1329. else
  1330. {
  1331. // Allocate temp buffer of destination size in source format
  1332. temp = PixelData(scaled.getWidth(), scaled.getHeight(), scaled.getDepth(), src.getFormat());
  1333. temp.allocateInternalBuffer();
  1334. }
  1335. // super-optimized: byte-oriented math, no conversion
  1336. switch (PixelUtil::getNumElemBytes(src.getFormat()))
  1337. {
  1338. case 1: LinearResampler_Byte<1>::scale(src, temp); break;
  1339. case 2: LinearResampler_Byte<2>::scale(src, temp); break;
  1340. case 3: LinearResampler_Byte<3>::scale(src, temp); break;
  1341. case 4: LinearResampler_Byte<4>::scale(src, temp); break;
  1342. default:
  1343. // never reached
  1344. assert(false);
  1345. }
  1346. if(temp.getData() != scaled.getData())
  1347. {
  1348. // Blit temp buffer
  1349. PixelUtil::bulkPixelConversion(temp, scaled);
  1350. temp.freeInternalBuffer();
  1351. }
  1352. break;
  1353. case PF_FLOAT32_RGB:
  1354. case PF_FLOAT32_RGBA:
  1355. if (scaled.getFormat() == PF_FLOAT32_RGB || scaled.getFormat() == PF_FLOAT32_RGBA)
  1356. {
  1357. // float32 to float32, avoid unpack/repack overhead
  1358. LinearResampler_Float32::scale(src, scaled);
  1359. break;
  1360. }
  1361. // else, fall through
  1362. default:
  1363. // non-optimized: floating-point math, performs conversion but always works
  1364. LinearResampler::scale(src, scaled);
  1365. }
  1366. break;
  1367. }
  1368. }
  1369. //-----------------------------------------------------------------------------
  1370. void PixelUtil::applyGamma(UINT8 *buffer, float gamma, size_t size, UINT8 bpp)
  1371. {
  1372. if( gamma == 1.0f )
  1373. return;
  1374. //NB only 24/32-bit supported
  1375. if( bpp != 24 && bpp != 32 ) return;
  1376. UINT32 stride = bpp >> 3;
  1377. for( size_t i = 0, j = size / stride; i < j; i++, buffer += stride )
  1378. {
  1379. float r, g, b;
  1380. r = (float)buffer[0];
  1381. g = (float)buffer[1];
  1382. b = (float)buffer[2];
  1383. r = r * gamma;
  1384. g = g * gamma;
  1385. b = b * gamma;
  1386. float scale = 1.0f, tmp;
  1387. if( r > 255.0f && (tmp=(255.0f/r)) < scale )
  1388. scale = tmp;
  1389. if( g > 255.0f && (tmp=(255.0f/g)) < scale )
  1390. scale = tmp;
  1391. if( b > 255.0f && (tmp=(255.0f/b)) < scale )
  1392. scale = tmp;
  1393. r *= scale; g *= scale; b *= scale;
  1394. buffer[0] = (UINT8)r;
  1395. buffer[1] = (UINT8)g;
  1396. buffer[2] = (UINT8)b;
  1397. }
  1398. }
  1399. Color PixelData::getColorAt(UINT32 x, UINT32 y, UINT32 z)
  1400. {
  1401. Color cv;
  1402. UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
  1403. UINT32 pixelOffset = pixelSize * (z * mSlicePitch + y * mRowPitch + x);
  1404. PixelUtil::unpackColour(&cv, mFormat, (unsigned char *)getData() + pixelOffset);
  1405. return cv;
  1406. }
  1407. void PixelData::setColorAt(Color const &cv, UINT32 x, UINT32 y, UINT32 z)
  1408. {
  1409. UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
  1410. UINT32 pixelOffset = pixelSize * (z * mSlicePitch + y * mRowPitch + x);
  1411. PixelUtil::packColour(cv, mFormat, (unsigned char *)getData() + pixelOffset);
  1412. }
  1413. }