BsPixelUtil.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753
  1. #include "BsPixelUtil.h"
  2. #include "BsBitwise.h"
  3. #include "BsColor.h"
  4. #include "BsMath.h"
  5. #include "BsException.h"
  6. #include "nvtt/nvtt.h"
  7. namespace BansheeEngine
  8. {
  9. /**
  10. * @brief Performs pixel data resampling using the point filter (nearest neighbor).
  11. * Does not perform format conversions.
  12. *
  13. * @tparam elementSize Size of a single pixel in bytes.
  14. */
  15. template<UINT32 elementSize> struct NearestResampler
  16. {
  17. static void scale(const PixelData& source, const PixelData& dest)
  18. {
  19. UINT8* sourceData = source.getData();
  20. UINT8* destPtr = dest.getData();
  21. // Get steps for traversing source data in 16/48 fixed point format
  22. UINT64 stepX = ((UINT64)source.getWidth() << 48) / dest.getWidth();
  23. UINT64 stepY = ((UINT64)source.getHeight() << 48) / dest.getHeight();
  24. UINT64 stepZ = ((UINT64)source.getDepth() << 48) / dest.getDepth();
  25. UINT64 curZ = (stepZ >> 1) - 1; // Offset half a pixel to start at pixel center
  26. for (UINT32 z = dest.getFront(); z < dest.getBack(); z++, curZ += stepZ)
  27. {
  28. UINT32 offsetZ = (UINT32)(curZ >> 48) * source.getSlicePitch();
  29. UINT64 curY = (stepY >> 1) - 1; // Offset half a pixel to start at pixel center
  30. for (UINT32 y = dest.getTop(); y < dest.getBottom(); y++, curY += stepY)
  31. {
  32. UINT32 offsetY = (UINT32)(curY >> 48) * source.getRowPitch();
  33. UINT64 curX = (stepX >> 1) - 1; // Offset half a pixel to start at pixel center
  34. for (UINT32 x = dest.getLeft(); x < dest.getRight(); x++, curX += stepX)
  35. {
  36. UINT32 offsetX = (UINT32)(curX >> 48);
  37. UINT32 offsetBytes = elementSize*(offsetX + offsetY + offsetZ);
  38. UINT8* curSourcePtr = sourceData + offsetBytes;
  39. memcpy(destPtr, curSourcePtr, elementSize);
  40. destPtr += elementSize;
  41. }
  42. destPtr += elementSize*dest.getRowSkip();
  43. }
  44. destPtr += elementSize*dest.getSliceSkip();
  45. }
  46. }
  47. };
  48. /**
  49. * @brief Performs pixel data resampling using the box filter (linear).
  50. * Performs format conversions.
  51. */
  52. struct LinearResampler
  53. {
  54. static void scale(const PixelData& source, const PixelData& dest)
  55. {
  56. UINT32 sourceElemSize = PixelUtil::getNumElemBytes(source.getFormat());
  57. UINT32 destElemSize = PixelUtil::getNumElemBytes(dest.getFormat());
  58. UINT8* sourceData = source.getData();
  59. UINT8* destPtr = dest.getData();
  60. // Get steps for traversing source data in 16/48 fixed point precision format
  61. UINT64 stepX = ((UINT64)source.getWidth() << 48) / dest.getWidth();
  62. UINT64 stepY = ((UINT64)source.getHeight() << 48) / dest.getHeight();
  63. UINT64 stepZ = ((UINT64)source.getDepth() << 48) / dest.getDepth();
  64. // Contains 16/16 fixed point precision format. Most significant
  65. // 16 bits will contain the coordinate in the source image, and the
  66. // least significant 16 bits will contain the fractional part of the coordinate
  67. // that will be used for determining the blend amount.
  68. UINT32 temp = 0;
  69. UINT64 curZ = (stepZ >> 1) - 1; // Offset half a pixel to start at pixel center
  70. for (UINT32 z = dest.getFront(); z < dest.getBack(); z++, curZ += stepZ)
  71. {
  72. temp = UINT32(curZ >> 32);
  73. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  74. UINT32 sampleCoordZ1 = temp >> 16;
  75. UINT32 sampleCoordZ2 = std::min(sampleCoordZ1 + 1, (UINT32)source.getDepth() - 1);
  76. float sampleWeightZ = (temp & 0xFFFF) / 65536.0f;
  77. UINT64 curY = (stepY >> 1) - 1; // Offset half a pixel to start at pixel center
  78. for (UINT32 y = dest.getTop(); y < dest.getBottom(); y++, curY += stepY)
  79. {
  80. temp = (UINT32)(curY >> 32);
  81. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  82. UINT32 sampleCoordY1 = temp >> 16;
  83. UINT32 sampleCoordY2 = std::min(sampleCoordY1 + 1, (UINT32)source.getHeight() - 1);
  84. float sampleWeightY = (temp & 0xFFFF) / 65536.0f;
  85. UINT64 curX = (stepX >> 1) - 1; // Offset half a pixel to start at pixel center
  86. for (UINT32 x = dest.getLeft(); x < dest.getRight(); x++, curX += stepX)
  87. {
  88. temp = (UINT32)(curX >> 32);
  89. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  90. UINT32 sampleCoordX1 = temp >> 16;
  91. UINT32 sampleCoordX2 = std::min(sampleCoordX1 + 1, (UINT32)source.getWidth() - 1);
  92. float sampleWeightX = (temp & 0xFFFF) / 65536.0f;
  93. Color x1y1z1, x2y1z1, x1y2z1, x2y2z1;
  94. Color x1y1z2, x2y1z2, x1y2z2, x2y2z2;
  95. #define GETSOURCEDATA(x, y, z) sourceData + sourceElemSize*((x)+(y)*source.getRowPitch() + (z)*source.getSlicePitch())
  96. PixelUtil::unpackColor(&x1y1z1, source.getFormat(), GETSOURCEDATA(sampleCoordX1, sampleCoordY1, sampleCoordZ1));
  97. PixelUtil::unpackColor(&x2y1z1, source.getFormat(), GETSOURCEDATA(sampleCoordX2, sampleCoordY1, sampleCoordZ1));
  98. PixelUtil::unpackColor(&x1y2z1, source.getFormat(), GETSOURCEDATA(sampleCoordX1, sampleCoordY2, sampleCoordZ1));
  99. PixelUtil::unpackColor(&x2y2z1, source.getFormat(), GETSOURCEDATA(sampleCoordX2, sampleCoordY2, sampleCoordZ1));
  100. PixelUtil::unpackColor(&x1y1z2, source.getFormat(), GETSOURCEDATA(sampleCoordX1, sampleCoordY1, sampleCoordZ2));
  101. PixelUtil::unpackColor(&x2y1z2, source.getFormat(), GETSOURCEDATA(sampleCoordX2, sampleCoordY1, sampleCoordZ2));
  102. PixelUtil::unpackColor(&x1y2z2, source.getFormat(), GETSOURCEDATA(sampleCoordX1, sampleCoordY2, sampleCoordZ2));
  103. PixelUtil::unpackColor(&x2y2z2, source.getFormat(), GETSOURCEDATA(sampleCoordX2, sampleCoordY2, sampleCoordZ2));
  104. #undef GETSOURCEDATA
  105. Color accum =
  106. x1y1z1 * ((1.0f - sampleWeightX)*(1.0f - sampleWeightY)*(1.0f - sampleWeightZ)) +
  107. x2y1z1 * ( sampleWeightX *(1.0f - sampleWeightY)*(1.0f - sampleWeightZ)) +
  108. x1y2z1 * ((1.0f - sampleWeightX)* sampleWeightY *(1.0f - sampleWeightZ)) +
  109. x2y2z1 * ( sampleWeightX * sampleWeightY *(1.0f - sampleWeightZ)) +
  110. x1y1z2 * ((1.0f - sampleWeightX)*(1.0f - sampleWeightY)* sampleWeightZ ) +
  111. x2y1z2 * ( sampleWeightX *(1.0f - sampleWeightY)* sampleWeightZ ) +
  112. x1y2z2 * ((1.0f - sampleWeightX)* sampleWeightY * sampleWeightZ ) +
  113. x2y2z2 * ( sampleWeightX * sampleWeightY * sampleWeightZ );
  114. PixelUtil::packColor(accum, dest.getFormat(), destPtr);
  115. destPtr += destElemSize;
  116. }
  117. destPtr += destElemSize * dest.getRowSkip();
  118. }
  119. destPtr += destElemSize * dest.getSliceSkip();
  120. }
  121. }
  122. };
  123. /**
  124. * @brief Performs pixel data resampling using the box filter (linear).
  125. * Only handles float RGB or RGBA pixel data (32 bits per channel).
  126. */
  127. struct LinearResampler_Float32
  128. {
  129. static void scale(const PixelData& source, const PixelData& dest)
  130. {
  131. UINT32 numSourceChannels = PixelUtil::getNumElemBytes(source.getFormat()) / sizeof(float);
  132. UINT32 numDestChannels = PixelUtil::getNumElemBytes(dest.getFormat()) / sizeof(float);
  133. float* sourceData = (float*)source.getData();
  134. float* destPtr = (float*)dest.getData();
  135. // Get steps for traversing source data in 16/48 fixed point precision format
  136. UINT64 stepX = ((UINT64)source.getWidth() << 48) / dest.getWidth();
  137. UINT64 stepY = ((UINT64)source.getHeight() << 48) / dest.getHeight();
  138. UINT64 stepZ = ((UINT64)source.getDepth() << 48) / dest.getDepth();
  139. // Contains 16/16 fixed point precision format. Most significant
  140. // 16 bits will contain the coordinate in the source image, and the
  141. // least significant 16 bits will contain the fractional part of the coordinate
  142. // that will be used for determining the blend amount.
  143. UINT32 temp = 0;
  144. UINT64 curZ = (stepZ >> 1) - 1; // Offset half a pixel to start at pixel center
  145. for (UINT32 z = dest.getFront(); z < dest.getBack(); z++, curZ += stepZ)
  146. {
  147. temp = (UINT32)(curZ >> 32);
  148. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  149. UINT32 sampleCoordZ1 = temp >> 16;
  150. UINT32 sampleCoordZ2 = std::min(sampleCoordZ1 + 1, (UINT32)source.getDepth() - 1);
  151. float sampleWeightZ = (temp & 0xFFFF) / 65536.0f;
  152. UINT64 curY = (stepY >> 1) - 1; // Offset half a pixel to start at pixel center
  153. for (UINT32 y = dest.getTop(); y < dest.getBottom(); y++, curY += stepY)
  154. {
  155. temp = (UINT32)(curY >> 32);
  156. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  157. UINT32 sampleCoordY1 = temp >> 16;
  158. UINT32 sampleCoordY2 = std::min(sampleCoordY1 + 1, (UINT32)source.getHeight() - 1);
  159. float sampleWeightY = (temp & 0xFFFF) / 65536.0f;
  160. UINT64 curX = (stepX >> 1) - 1; // Offset half a pixel to start at pixel center
  161. for (UINT32 x = dest.getLeft(); x < dest.getRight(); x++, curX += stepX)
  162. {
  163. temp = (UINT32)(curX >> 32);
  164. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  165. UINT32 sampleCoordX1 = temp >> 16;
  166. UINT32 sampleCoordX2 = std::min(sampleCoordX1 + 1, (UINT32)source.getWidth() - 1);
  167. float sampleWeightX = (temp & 0xFFFF) / 65536.0f;
  168. // process R,G,B,A simultaneously for cache coherence?
  169. float accum[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  170. #define ACCUM3(x,y,z,factor) \
  171. { float f = factor; \
  172. UINT32 offset = (x + y*source.getRowPitch() + z*source.getSlicePitch())*numSourceChannels; \
  173. accum[0] += sourceData[offset + 0] * f; accum[1] += sourceData[offset + 1] * f; \
  174. accum[2] += sourceData[offset + 2] * f; }
  175. #define ACCUM4(x,y,z,factor) \
  176. { float f = factor; \
  177. UINT32 offset = (x + y*source.getRowPitch() + z*source.getSlicePitch())*numSourceChannels; \
  178. accum[0] += sourceData[offset + 0] * f; accum[1] += sourceData[offset + 1] * f; \
  179. accum[2] += sourceData[offset + 2] * f; accum[3] += sourceData[offset + 3] * f; }
  180. if (numSourceChannels == 3 || numDestChannels == 3)
  181. {
  182. // RGB
  183. ACCUM3(sampleCoordX1, sampleCoordY1, sampleCoordZ1, (1.0f - sampleWeightX) * (1.0f - sampleWeightY) * (1.0f - sampleWeightZ));
  184. ACCUM3(sampleCoordX2, sampleCoordY1, sampleCoordZ1, sampleWeightX * (1.0f - sampleWeightY) * (1.0f - sampleWeightZ));
  185. ACCUM3(sampleCoordX1, sampleCoordY2, sampleCoordZ1, (1.0f - sampleWeightX) * sampleWeightY * (1.0f - sampleWeightZ));
  186. ACCUM3(sampleCoordX2, sampleCoordY2, sampleCoordZ1, sampleWeightX * sampleWeightY * (1.0f - sampleWeightZ));
  187. ACCUM3(sampleCoordX1, sampleCoordY1, sampleCoordZ2, (1.0f - sampleWeightX) * (1.0f - sampleWeightY) * sampleWeightZ);
  188. ACCUM3(sampleCoordX2, sampleCoordY1, sampleCoordZ2, sampleWeightX * (1.0f - sampleWeightY) * sampleWeightZ);
  189. ACCUM3(sampleCoordX1, sampleCoordY2, sampleCoordZ2, (1.0f - sampleWeightX) * sampleWeightY * sampleWeightZ);
  190. ACCUM3(sampleCoordX2, sampleCoordY2, sampleCoordZ2, sampleWeightX * sampleWeightY * sampleWeightZ);
  191. accum[3] = 1.0f;
  192. }
  193. else
  194. {
  195. // RGBA
  196. ACCUM4(sampleCoordX1, sampleCoordY1, sampleCoordZ1, (1.0f - sampleWeightX) * (1.0f - sampleWeightY) * (1.0f - sampleWeightZ));
  197. ACCUM4(sampleCoordX2, sampleCoordY1, sampleCoordZ1, sampleWeightX * (1.0f - sampleWeightY) * (1.0f - sampleWeightZ));
  198. ACCUM4(sampleCoordX1, sampleCoordY2, sampleCoordZ1, (1.0f - sampleWeightX) * sampleWeightY * (1.0f - sampleWeightZ));
  199. ACCUM4(sampleCoordX2, sampleCoordY2, sampleCoordZ1, sampleWeightX * sampleWeightY * (1.0f - sampleWeightZ));
  200. ACCUM4(sampleCoordX1, sampleCoordY1, sampleCoordZ2, (1.0f - sampleWeightX) * (1.0f - sampleWeightY) * sampleWeightZ);
  201. ACCUM4(sampleCoordX2, sampleCoordY1, sampleCoordZ2, sampleWeightX * (1.0f - sampleWeightY) * sampleWeightZ);
  202. ACCUM4(sampleCoordX1, sampleCoordY2, sampleCoordZ2, (1.0f - sampleWeightX) * sampleWeightY * sampleWeightZ);
  203. ACCUM4(sampleCoordX2, sampleCoordY2, sampleCoordZ2, sampleWeightX * sampleWeightY * sampleWeightZ);
  204. }
  205. memcpy(destPtr, accum, sizeof(float)*numDestChannels);
  206. #undef ACCUM3
  207. #undef ACCUM4
  208. destPtr += numDestChannels;
  209. }
  210. destPtr += numDestChannels*dest.getRowSkip();
  211. }
  212. destPtr += numDestChannels*dest.getSliceSkip();
  213. }
  214. }
  215. };
  216. // byte linear resampler, does not do any format conversions.
  217. // only handles pixel formats that use 1 byte per color channel.
  218. // 2D only; punts 3D pixelboxes to default LinearResampler (slow).
  219. // templated on bytes-per-pixel to allow compiler optimizations, such
  220. // as unrolling loops and replacing multiplies with bitshifts
  221. /**
  222. * @brief Performs pixel data resampling using the box filter (linear).
  223. * Only handles pixel formats with one byte per channel. Does
  224. * not perform format conversion.
  225. *
  226. * @tparam channels Number of channels in the pixel format.
  227. */
  228. template<UINT32 channels> struct LinearResampler_Byte
  229. {
  230. static void scale(const PixelData& source, const PixelData& dest)
  231. {
  232. // Only optimized for 2D
  233. if (source.getDepth() > 1 || dest.getDepth() > 1)
  234. {
  235. LinearResampler::scale(source, dest);
  236. return;
  237. }
  238. UINT8* sourceData = (UINT8*)source.getData();
  239. UINT8* destPtr = (UINT8*)dest.getData();
  240. // Get steps for traversing source data in 16/48 fixed point precision format
  241. UINT64 stepX = ((UINT64)source.getWidth() << 48) / dest.getWidth();
  242. UINT64 stepY = ((UINT64)source.getHeight() << 48) / dest.getHeight();
  243. // Contains 16/16 fixed point precision format. Most significant
  244. // 16 bits will contain the coordinate in the source image, and the
  245. // least significant 16 bits will contain the fractional part of the coordinate
  246. // that will be used for determining the blend amount.
  247. UINT32 temp;
  248. UINT64 curY = (stepY >> 1) - 1; // Offset half a pixel to start at pixel center
  249. for (UINT32 y = dest.getTop(); y < dest.getBottom(); y++, curY += stepY)
  250. {
  251. temp = (UINT32)(curY >> 36);
  252. temp = (temp > 0x800)? temp - 0x800: 0;
  253. UINT32 sampleWeightY = temp & 0xFFF;
  254. UINT32 sampleCoordY1 = temp >> 12;
  255. UINT32 sampleCoordY2 = std::min(sampleCoordY1 + 1, (UINT32)source.getBottom() - source.getTop() - 1);
  256. UINT32 sampleY1Offset = sampleCoordY1 * source.getRowPitch();
  257. UINT32 sampleY2Offset = sampleCoordY2 * source.getRowPitch();
  258. UINT64 curX = (stepX >> 1) - 1; // Offset half a pixel to start at pixel center
  259. for (UINT32 x = dest.getLeft(); x < dest.getRight(); x++, curX += stepX)
  260. {
  261. temp = (UINT32)(curX >> 36);
  262. temp = (temp > 0x800)? temp - 0x800 : 0;
  263. UINT32 sampleWeightX = temp & 0xFFF;
  264. UINT32 sampleCoordX1 = temp >> 12;
  265. UINT32 sampleCoordX2 = std::min(sampleCoordX1 + 1, (UINT32)source.getRight() - source.getLeft() - 1);
  266. UINT32 sxfsyf = sampleWeightX*sampleWeightY;
  267. for (UINT32 k = 0; k < channels; k++)
  268. {
  269. UINT32 accum =
  270. sourceData[(sampleCoordX1 + sampleY1Offset)*channels+k]*(0x1000000-(sampleWeightX<<12)-(sampleWeightY<<12)+sxfsyf) +
  271. sourceData[(sampleCoordX2 + sampleY1Offset)*channels+k]*((sampleWeightX<<12)-sxfsyf) +
  272. sourceData[(sampleCoordX1 + sampleY2Offset)*channels+k]*((sampleWeightY<<12)-sxfsyf) +
  273. sourceData[(sampleCoordX2 + sampleY2Offset)*channels+k]*sxfsyf;
  274. // Round up to byte size
  275. *destPtr = (UINT8)((accum + 0x800000) >> 24);
  276. destPtr++;
  277. }
  278. }
  279. destPtr += channels*dest.getRowSkip();
  280. }
  281. }
  282. };
  283. /**
  284. * @brief Data describing a pixel format.
  285. */
  286. struct PixelFormatDescription
  287. {
  288. const char* name; /**< Name of the format. */
  289. UINT8 elemBytes; /**< Number of bytes one element (color value) uses. */
  290. UINT32 flags; /**< PixelFormatFlags set by the pixel format. */
  291. PixelComponentType componentType; /**< Data type of a single element of the format. */
  292. UINT8 componentCount; /**< Number of elements in the format. */
  293. UINT8 rbits, gbits, bbits, abits; /**< Number of bits per element in the format. */
  294. UINT32 rmask, gmask, bmask, amask; /**< Masks used by packers/unpackers. */
  295. UINT8 rshift, gshift, bshift, ashift; /**< Shifts used by packers/unpackers. */
  296. };
  297. /**
  298. * @brief A list of all available pixel formats.
  299. */
  300. PixelFormatDescription _pixelFormats[PF_COUNT] = {
  301. {"PF_UNKNOWN",
  302. /* Bytes per element */
  303. 0,
  304. /* Flags */
  305. 0,
  306. /* Component type and count */
  307. PCT_BYTE, 0,
  308. /* rbits, gbits, bbits, abits */
  309. 0, 0, 0, 0,
  310. /* Masks and shifts */
  311. 0, 0, 0, 0, 0, 0, 0, 0
  312. },
  313. //-----------------------------------------------------------------------
  314. {"PF_R8",
  315. /* Bytes per element */
  316. 1,
  317. /* Flags */
  318. 0,
  319. /* Component type and count */
  320. PCT_BYTE, 1,
  321. /* rbits, gbits, bbits, abits */
  322. 8, 0, 0, 0,
  323. /* Masks and shifts */
  324. 0x000000FF, 0, 0, 0,
  325. 0, 0, 0, 0
  326. },
  327. //-----------------------------------------------------------------------
  328. {"PF_R8G8",
  329. /* Bytes per element */
  330. 2,
  331. /* Flags */
  332. 0,
  333. /* Component type and count */
  334. PCT_BYTE, 2,
  335. /* rbits, gbits, bbits, abits */
  336. 8, 8, 0, 0,
  337. /* Masks and shifts */
  338. 0x000000FF, 0x0000FF00, 0, 0,
  339. 0, 8, 0, 0
  340. },
  341. //-----------------------------------------------------------------------
  342. {"PF_R8G8B8",
  343. /* Bytes per element */
  344. 3, // 24 bit integer -- special
  345. /* Flags */
  346. PFF_NATIVEENDIAN,
  347. /* Component type and count */
  348. PCT_BYTE, 3,
  349. /* rbits, gbits, bbits, abits */
  350. 8, 8, 8, 0,
  351. /* Masks and shifts */
  352. 0x000000FF, 0x0000FF00, 0x00FF0000, 0,
  353. 0, 8, 16, 0
  354. },
  355. //-----------------------------------------------------------------------
  356. {"PF_B8G8R8",
  357. /* Bytes per element */
  358. 3, // 24 bit integer -- special
  359. /* Flags */
  360. PFF_NATIVEENDIAN,
  361. /* Component type and count */
  362. PCT_BYTE, 3,
  363. /* rbits, gbits, bbits, abits */
  364. 8, 8, 8, 0,
  365. /* Masks and shifts */
  366. 0x00FF0000, 0x0000FF00, 0x000000FF, 0,
  367. 16, 8, 0, 0
  368. },
  369. //-----------------------------------------------------------------------
  370. {"PF_A8R8G8B8",
  371. /* Bytes per element */
  372. 4,
  373. /* Flags */
  374. PFF_HASALPHA | PFF_NATIVEENDIAN,
  375. /* Component type and count */
  376. PCT_BYTE, 4,
  377. /* rbits, gbits, bbits, abits */
  378. 8, 8, 8, 8,
  379. /* Masks and shifts */
  380. 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF,
  381. 8, 16, 24, 0
  382. },
  383. //-----------------------------------------------------------------------
  384. {"PF_A8B8G8R8",
  385. /* Bytes per element */
  386. 4,
  387. /* Flags */
  388. PFF_HASALPHA | PFF_NATIVEENDIAN,
  389. /* Component type and count */
  390. PCT_BYTE, 4,
  391. /* rbits, gbits, bbits, abits */
  392. 8, 8, 8, 8,
  393. /* Masks and shifts */
  394. 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF,
  395. 24, 16, 8, 0,
  396. },
  397. //-----------------------------------------------------------------------
  398. {"PF_B8G8R8A8",
  399. /* Bytes per element */
  400. 4,
  401. /* Flags */
  402. PFF_HASALPHA | PFF_NATIVEENDIAN,
  403. /* Component type and count */
  404. PCT_BYTE, 4,
  405. /* rbits, gbits, bbits, abits */
  406. 8, 8, 8, 8,
  407. /* Masks and shifts */
  408. 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000,
  409. 16, 8, 0, 24
  410. },
  411. //-----------------------------------------------------------------------
  412. {"PF_R8G8B8A8",
  413. /* Bytes per element */
  414. 4,
  415. /* Flags */
  416. PFF_HASALPHA | PFF_NATIVEENDIAN,
  417. /* Component type and count */
  418. PCT_BYTE, 4,
  419. /* rbits, gbits, bbits, abits */
  420. 8, 8, 8, 8,
  421. /* Masks and shifts */
  422. 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000,
  423. 0, 8, 16, 24
  424. },
  425. //-----------------------------------------------------------------------
  426. {"PF_X8R8G8B8",
  427. /* Bytes per element */
  428. 4,
  429. /* Flags */
  430. PFF_NATIVEENDIAN,
  431. /* Component type and count */
  432. PCT_BYTE, 3,
  433. /* rbits, gbits, bbits, abits */
  434. 8, 8, 8, 0,
  435. /* Masks and shifts */
  436. 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF,
  437. 8, 16, 24, 0
  438. },
  439. //-----------------------------------------------------------------------
  440. {"PF_X8B8G8R8",
  441. /* Bytes per element */
  442. 4,
  443. /* Flags */
  444. PFF_NATIVEENDIAN,
  445. /* Component type and count */
  446. PCT_BYTE, 3,
  447. /* rbits, gbits, bbits, abits */
  448. 8, 8, 8, 0,
  449. /* Masks and shifts */
  450. 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF,
  451. 24, 16, 8, 0
  452. },
  453. //-----------------------------------------------------------------------
  454. {"PF_R8G8B8X8",
  455. /* Bytes per element */
  456. 4,
  457. /* Flags */
  458. PFF_HASALPHA | PFF_NATIVEENDIAN,
  459. /* Component type and count */
  460. PCT_BYTE, 3,
  461. /* rbits, gbits, bbits, abits */
  462. 8, 8, 8, 0,
  463. /* Masks and shifts */
  464. 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000,
  465. 0, 8, 16, 0
  466. },
  467. //-----------------------------------------------------------------------
  468. {"PF_B8G8R8X8",
  469. /* Bytes per element */
  470. 4,
  471. /* Flags */
  472. PFF_HASALPHA | PFF_NATIVEENDIAN,
  473. /* Component type and count */
  474. PCT_BYTE, 3,
  475. /* rbits, gbits, bbits, abits */
  476. 8, 8, 8, 0,
  477. /* Masks and shifts */
  478. 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000,
  479. 16, 8, 0, 0
  480. },
  481. //-----------------------------------------------------------------------
  482. {"PF_BC1",
  483. /* Bytes per element */
  484. 0,
  485. /* Flags */
  486. PFF_COMPRESSED | PFF_HASALPHA,
  487. /* Component type and count */
  488. PCT_BYTE, 3, // No alpha
  489. /* rbits, gbits, bbits, abits */
  490. 0, 0, 0, 0,
  491. /* Masks and shifts */
  492. 0, 0, 0, 0, 0, 0, 0, 0
  493. },
  494. //-----------------------------------------------------------------------
  495. {"PF_BC1a",
  496. /* Bytes per element */
  497. 0,
  498. /* Flags */
  499. PFF_COMPRESSED,
  500. /* Component type and count */
  501. PCT_BYTE, 3,
  502. /* rbits, gbits, bbits, abits */
  503. 0, 0, 0, 0,
  504. /* Masks and shifts */
  505. 0, 0, 0, 0, 0, 0, 0, 0
  506. },
  507. //-----------------------------------------------------------------------
  508. {"PF_BC2",
  509. /* Bytes per element */
  510. 0,
  511. /* Flags */
  512. PFF_COMPRESSED | PFF_HASALPHA,
  513. /* Component type and count */
  514. PCT_BYTE, 4,
  515. /* rbits, gbits, bbits, abits */
  516. 0, 0, 0, 0,
  517. /* Masks and shifts */
  518. 0, 0, 0, 0, 0, 0, 0, 0
  519. },
  520. //-----------------------------------------------------------------------
  521. {"PF_BC3",
  522. /* Bytes per element */
  523. 0,
  524. /* Flags */
  525. PFF_COMPRESSED | PFF_HASALPHA,
  526. /* Component type and count */
  527. PCT_BYTE, 4,
  528. /* rbits, gbits, bbits, abits */
  529. 0, 0, 0, 0,
  530. /* Masks and shifts */
  531. 0, 0, 0, 0, 0, 0, 0, 0
  532. },
  533. //-----------------------------------------------------------------------
  534. {"PF_BC4",
  535. /* Bytes per element */
  536. 0,
  537. /* Flags */
  538. PFF_COMPRESSED,
  539. /* Component type and count */
  540. PCT_BYTE, 1,
  541. /* rbits, gbits, bbits, abits */
  542. 0, 0, 0, 0,
  543. /* Masks and shifts */
  544. 0, 0, 0, 0, 0, 0, 0, 0
  545. },
  546. //-----------------------------------------------------------------------
  547. {"PF_BC5",
  548. /* Bytes per element */
  549. 0,
  550. /* Flags */
  551. PFF_COMPRESSED,
  552. /* Component type and count */
  553. PCT_BYTE, 2,
  554. /* rbits, gbits, bbits, abits */
  555. 0, 0, 0, 0,
  556. /* Masks and shifts */
  557. 0, 0, 0, 0, 0, 0, 0, 0
  558. },
  559. //-----------------------------------------------------------------------
  560. {"PF_BC6H",
  561. /* Bytes per element */
  562. 0,
  563. /* Flags */
  564. PFF_COMPRESSED,
  565. /* Component type and count */
  566. PCT_FLOAT16, 3,
  567. /* rbits, gbits, bbits, abits */
  568. 0, 0, 0, 0,
  569. /* Masks and shifts */
  570. 0, 0, 0, 0, 0, 0, 0, 0
  571. },
  572. //-----------------------------------------------------------------------
  573. {"PF_BC7",
  574. /* Bytes per element */
  575. 0,
  576. /* Flags */
  577. PFF_COMPRESSED | PFF_HASALPHA,
  578. /* Component type and count */
  579. PCT_BYTE, 4,
  580. /* rbits, gbits, bbits, abits */
  581. 0, 0, 0, 0,
  582. /* Masks and shifts */
  583. 0, 0, 0, 0, 0, 0, 0, 0
  584. },
  585. //-----------------------------------------------------------------------
  586. {"PF_FLOAT16_R",
  587. /* Bytes per element */
  588. 2,
  589. /* Flags */
  590. PFF_FLOAT,
  591. /* Component type and count */
  592. PCT_FLOAT16, 1,
  593. /* rbits, gbits, bbits, abits */
  594. 16, 0, 0, 0,
  595. /* Masks and shifts */
  596. 0, 0, 0, 0, 0, 0, 0, 0
  597. },
  598. //-----------------------------------------------------------------------
  599. {"PF_FLOAT16_RG",
  600. /* Bytes per element */
  601. 4,
  602. /* Flags */
  603. PFF_FLOAT,
  604. /* Component type and count */
  605. PCT_FLOAT16, 2,
  606. /* rbits, gbits, bbits, abits */
  607. 16, 16, 0, 0,
  608. /* Masks and shifts */
  609. 0, 0, 0, 0, 0, 0, 0, 0
  610. },
  611. //-----------------------------------------------------------------------
  612. {"PF_FLOAT16_RGB",
  613. /* Bytes per element */
  614. 6,
  615. /* Flags */
  616. PFF_FLOAT,
  617. /* Component type and count */
  618. PCT_FLOAT16, 3,
  619. /* rbits, gbits, bbits, abits */
  620. 16, 16, 16, 0,
  621. /* Masks and shifts */
  622. 0, 0, 0, 0, 0, 0, 0, 0
  623. },
  624. //-----------------------------------------------------------------------
  625. {"PF_FLOAT16_RGBA",
  626. /* Bytes per element */
  627. 8,
  628. /* Flags */
  629. PFF_FLOAT | PFF_HASALPHA,
  630. /* Component type and count */
  631. PCT_FLOAT16, 4,
  632. /* rbits, gbits, bbits, abits */
  633. 16, 16, 16, 16,
  634. /* Masks and shifts */
  635. 0, 0, 0, 0, 0, 0, 0, 0
  636. },
  637. //-----------------------------------------------------------------------
  638. {"PF_FLOAT32_R",
  639. /* Bytes per element */
  640. 4,
  641. /* Flags */
  642. PFF_FLOAT,
  643. /* Component type and count */
  644. PCT_FLOAT32, 1,
  645. /* rbits, gbits, bbits, abits */
  646. 32, 0, 0, 0,
  647. /* Masks and shifts */
  648. 0, 0, 0, 0, 0, 0, 0, 0
  649. },
  650. //-----------------------------------------------------------------------
  651. {"PF_FLOAT32_RG",
  652. /* Bytes per element */
  653. 8,
  654. /* Flags */
  655. PFF_FLOAT,
  656. /* Component type and count */
  657. PCT_FLOAT32, 2,
  658. /* rbits, gbits, bbits, abits */
  659. 32, 32, 0, 0,
  660. /* Masks and shifts */
  661. 0, 0, 0, 0, 0, 0, 0, 0
  662. },
  663. //-----------------------------------------------------------------------
  664. {"PF_FLOAT32_RGB",
  665. /* Bytes per element */
  666. 12,
  667. /* Flags */
  668. PFF_FLOAT,
  669. /* Component type and count */
  670. PCT_FLOAT32, 3,
  671. /* rbits, gbits, bbits, abits */
  672. 32, 32, 32, 0,
  673. /* Masks and shifts */
  674. 0, 0, 0, 0, 0, 0, 0, 0
  675. },
  676. //-----------------------------------------------------------------------
  677. {"PF_FLOAT32_RGBA",
  678. /* Bytes per element */
  679. 16,
  680. /* Flags */
  681. PFF_FLOAT | PFF_HASALPHA,
  682. /* Component type and count */
  683. PCT_FLOAT32, 4,
  684. /* rbits, gbits, bbits, abits */
  685. 32, 32, 32, 32,
  686. /* Masks and shifts */
  687. 0, 0, 0, 0, 0, 0, 0, 0
  688. },
  689. //-----------------------------------------------------------------------
  690. {"PF_D32_S8X24",
  691. /* Bytes per element */
  692. 4,
  693. /* Flags */
  694. PFF_DEPTH | PFF_FLOAT,
  695. /* Component type and count */
  696. PCT_FLOAT32, 1,
  697. /* rbits, gbits, bbits, abits */
  698. 0, 0, 0, 0,
  699. /* Masks and shifts */
  700. 0, 0, 0, 0, 0, 0, 0, 0
  701. },
  702. //-----------------------------------------------------------------------
  703. {"PF_D24_S8",
  704. /* Bytes per element */
  705. 8,
  706. /* Flags */
  707. PFF_DEPTH | PFF_FLOAT,
  708. /* Component type and count */
  709. PCT_FLOAT32, 2,
  710. /* rbits, gbits, bbits, abits */
  711. 0, 0, 0, 0,
  712. /* Masks and shifts */
  713. 0, 0, 0, 0, 0, 0, 0, 0
  714. },
  715. //-----------------------------------------------------------------------
  716. {"PF_D32",
  717. /* Bytes per element */
  718. 4,
  719. /* Flags */
  720. PFF_DEPTH | PFF_FLOAT,
  721. /* Component type and count */
  722. PCT_FLOAT32, 1,
  723. /* rbits, gbits, bbits, abits */
  724. 0, 0, 0, 0,
  725. /* Masks and shifts */
  726. 0, 0, 0, 0, 0, 0, 0, 0
  727. },
  728. //-----------------------------------------------------------------------
  729. {"PF_D16",
  730. /* Bytes per element */
  731. 2,
  732. /* Flags */
  733. PFF_DEPTH | PFF_FLOAT,
  734. /* Component type and count */
  735. PCT_FLOAT16, 1,
  736. /* rbits, gbits, bbits, abits */
  737. 0, 0, 0, 0,
  738. /* Masks and shifts */
  739. 0, 0, 0, 0, 0, 0, 0, 0
  740. },
  741. };
  742. static inline const PixelFormatDescription &getDescriptionFor(const PixelFormat fmt)
  743. {
  744. const int ord = (int)fmt;
  745. assert(ord>=0 && ord<PF_COUNT);
  746. return _pixelFormats[ord];
  747. }
  748. /**
  749. * @brief Handles compression output from NVTT library for a single image.
  750. */
  751. struct NVTTCompressOutputHandler : public nvtt::OutputHandler
  752. {
  753. NVTTCompressOutputHandler(UINT8* buffer, UINT32 sizeBytes)
  754. :buffer(buffer), bufferWritePos(buffer), bufferEnd(buffer + sizeBytes)
  755. { }
  756. virtual void beginImage(int size, int width, int height, int depth, int face, int miplevel)
  757. { }
  758. virtual bool writeData(const void* data, int size)
  759. {
  760. assert((bufferWritePos + size) <= bufferEnd);
  761. memcpy(bufferWritePos, data, size);
  762. bufferWritePos += size;
  763. return true;
  764. }
  765. UINT8* buffer;
  766. UINT8* bufferWritePos;
  767. UINT8* bufferEnd;
  768. };
  769. /**
  770. * @brief Handles output from NVTT library for a mip-map chain.
  771. */
  772. struct NVTTMipmapOutputHandler : public nvtt::OutputHandler
  773. {
  774. NVTTMipmapOutputHandler(const Vector<PixelDataPtr>& buffers)
  775. :buffers(buffers), bufferWritePos(nullptr), bufferEnd(nullptr)
  776. { }
  777. virtual void beginImage(int size, int width, int height, int depth, int face, int miplevel)
  778. {
  779. assert(miplevel >= 0 && miplevel < buffers.size());
  780. assert(size == buffers[miplevel]->getConsecutiveSize());
  781. activeBuffer = buffers[miplevel];
  782. bufferWritePos = activeBuffer->getData();
  783. bufferEnd = bufferWritePos + activeBuffer->getConsecutiveSize();
  784. }
  785. virtual bool writeData(const void* data, int size)
  786. {
  787. assert((bufferWritePos + size) <= bufferEnd);
  788. memcpy(bufferWritePos, data, size);
  789. bufferWritePos += size;
  790. return true;
  791. }
  792. Vector<PixelDataPtr> buffers;
  793. PixelDataPtr activeBuffer;
  794. UINT8* bufferWritePos;
  795. UINT8* bufferEnd;
  796. };
  797. nvtt::Format toNVTTFormat(CompressedFormat format)
  798. {
  799. switch (format)
  800. {
  801. case CompressedFormat::BC1:
  802. return nvtt::Format_BC1;
  803. case CompressedFormat::BC1a:
  804. return nvtt::Format_BC1a;
  805. case CompressedFormat::BC2:
  806. return nvtt::Format_BC2;
  807. case CompressedFormat::BC3:
  808. return nvtt::Format_BC3;
  809. case CompressedFormat::BC4:
  810. return nvtt::Format_BC4;
  811. case CompressedFormat::BC5:
  812. return nvtt::Format_BC5;
  813. }
  814. // Unsupported format
  815. return nvtt::Format_BC3;
  816. }
  817. nvtt::Quality toNVTTQuality(CompressionQuality quality)
  818. {
  819. switch (quality)
  820. {
  821. case CompressionQuality::Fastest:
  822. return nvtt::Quality_Fastest;
  823. case CompressionQuality::Highest:
  824. return nvtt::Quality_Highest;
  825. case CompressionQuality::Normal:
  826. return nvtt::Quality_Normal;
  827. case CompressionQuality::Production:
  828. return nvtt::Quality_Normal;
  829. }
  830. // Unknown quality level
  831. return nvtt::Quality_Normal;
  832. }
  833. nvtt::AlphaMode toNVTTAlphaMode(AlphaMode alphaMode)
  834. {
  835. switch (alphaMode)
  836. {
  837. case AlphaMode::None:
  838. return nvtt::AlphaMode_None;
  839. case AlphaMode::Premultiplied:
  840. return nvtt::AlphaMode_Premultiplied;
  841. case AlphaMode::Transparency:
  842. return nvtt::AlphaMode_Transparency;
  843. }
  844. // Unknown alpha mode
  845. return nvtt::AlphaMode_None;
  846. }
  847. nvtt::WrapMode toNVTTWrapMode(MipMapWrapMode wrapMode)
  848. {
  849. switch (wrapMode)
  850. {
  851. case MipMapWrapMode::Clamp:
  852. return nvtt::WrapMode_Clamp;
  853. case MipMapWrapMode::Mirror:
  854. return nvtt::WrapMode_Mirror;
  855. case MipMapWrapMode::Repeat:
  856. return nvtt::WrapMode_Repeat;
  857. }
  858. // Unknown alpha mode
  859. return nvtt::WrapMode_Mirror;
  860. }
  861. PixelFormat toPixelFormat(CompressedFormat format)
  862. {
  863. switch (format)
  864. {
  865. case CompressedFormat::BC1:
  866. return PF_BC1;
  867. case CompressedFormat::BC1a:
  868. return PF_BC1a;
  869. case CompressedFormat::BC2:
  870. return PF_BC2;
  871. case CompressedFormat::BC3:
  872. return PF_BC3;
  873. case CompressedFormat::BC4:
  874. return PF_BC4;
  875. case CompressedFormat::BC5:
  876. return PF_BC5;
  877. case CompressedFormat::BC6H:
  878. return PF_BC6H;
  879. case CompressedFormat::BC7:
  880. return PF_BC7;
  881. }
  882. // Unknown format
  883. return PF_BC3;
  884. }
  885. UINT32 PixelUtil::getNumElemBytes(PixelFormat format)
  886. {
  887. return getDescriptionFor(format).elemBytes;
  888. }
  889. UINT32 PixelUtil::getMemorySize(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  890. {
  891. if(isCompressed(format))
  892. {
  893. switch(format)
  894. {
  895. // BC formats work by dividing the image into 4x4 blocks, then encoding each
  896. // 4x4 block with a certain number of bytes.
  897. case PF_BC1:
  898. case PF_BC1a:
  899. case PF_BC4:
  900. return ((width+3)/4)*((height+3)/4)*8 * depth;
  901. case PF_BC2:
  902. case PF_BC3:
  903. case PF_BC5:
  904. case PF_BC6H:
  905. case PF_BC7:
  906. return ((width+3)/4)*((height+3)/4)*16 * depth;
  907. default:
  908. BS_EXCEPT(InvalidParametersException, "Invalid compressed pixel format");
  909. }
  910. }
  911. else
  912. {
  913. return width*height*depth*getNumElemBytes(format);
  914. }
  915. }
  916. UINT32 PixelUtil::getNumElemBits(PixelFormat format)
  917. {
  918. return getDescriptionFor(format).elemBytes * 8;
  919. }
  920. UINT32 PixelUtil::getFlags(PixelFormat format)
  921. {
  922. return getDescriptionFor(format).flags;
  923. }
  924. bool PixelUtil::hasAlpha(PixelFormat format)
  925. {
  926. return (PixelUtil::getFlags(format) & PFF_HASALPHA) > 0;
  927. }
  928. bool PixelUtil::isFloatingPoint(PixelFormat format)
  929. {
  930. return (PixelUtil::getFlags(format) & PFF_FLOAT) > 0;
  931. }
  932. bool PixelUtil::isCompressed(PixelFormat format)
  933. {
  934. return (PixelUtil::getFlags(format) & PFF_COMPRESSED) > 0;
  935. }
  936. bool PixelUtil::isDepth(PixelFormat format)
  937. {
  938. return (PixelUtil::getFlags(format) & PFF_DEPTH) > 0;
  939. }
  940. bool PixelUtil::isNativeEndian(PixelFormat format)
  941. {
  942. return (PixelUtil::getFlags(format) & PFF_NATIVEENDIAN) > 0;
  943. }
  944. bool PixelUtil::isValidExtent(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  945. {
  946. if(isCompressed(format))
  947. {
  948. switch(format)
  949. {
  950. case PF_BC1:
  951. case PF_BC2:
  952. case PF_BC1a:
  953. case PF_BC3:
  954. case PF_BC4:
  955. case PF_BC5:
  956. case PF_BC6H:
  957. case PF_BC7:
  958. return ((width & 3) == 0 && (height & 3) == 0 && depth == 1);
  959. default:
  960. return true;
  961. }
  962. }
  963. else
  964. {
  965. return true;
  966. }
  967. }
  968. void PixelUtil::getBitDepths(PixelFormat format, int rgba[4])
  969. {
  970. const PixelFormatDescription& des = getDescriptionFor(format);
  971. rgba[0] = des.rbits;
  972. rgba[1] = des.gbits;
  973. rgba[2] = des.bbits;
  974. rgba[3] = des.abits;
  975. }
  976. void PixelUtil::getBitMasks(PixelFormat format, UINT32 rgba[4])
  977. {
  978. const PixelFormatDescription& des = getDescriptionFor(format);
  979. rgba[0] = des.rmask;
  980. rgba[1] = des.gmask;
  981. rgba[2] = des.bmask;
  982. rgba[3] = des.amask;
  983. }
  984. void PixelUtil::getBitShifts(PixelFormat format, UINT8 rgba[4])
  985. {
  986. const PixelFormatDescription& des = getDescriptionFor(format);
  987. rgba[0] = des.rshift;
  988. rgba[1] = des.gshift;
  989. rgba[2] = des.bshift;
  990. rgba[3] = des.ashift;
  991. }
  992. String PixelUtil::getFormatName(PixelFormat srcformat)
  993. {
  994. return getDescriptionFor(srcformat).name;
  995. }
  996. bool PixelUtil::isAccessible(PixelFormat srcformat)
  997. {
  998. if (srcformat == PF_UNKNOWN)
  999. return false;
  1000. UINT32 flags = getFlags(srcformat);
  1001. return !((flags & PFF_COMPRESSED) || (flags & PFF_DEPTH));
  1002. }
  1003. PixelComponentType PixelUtil::getElementType(PixelFormat format)
  1004. {
  1005. const PixelFormatDescription& des = getDescriptionFor(format);
  1006. return des.componentType;
  1007. }
  1008. UINT32 PixelUtil::getNumElements(PixelFormat format)
  1009. {
  1010. const PixelFormatDescription& des = getDescriptionFor(format);
  1011. return des.componentCount;
  1012. }
  1013. UINT32 PixelUtil::getMaxMipmaps(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  1014. {
  1015. UINT32 count = 0;
  1016. if((width > 0) && (height > 0))
  1017. {
  1018. do {
  1019. if(width>1) width = width/2;
  1020. if(height>1) height = height/2;
  1021. if(depth>1) depth = depth/2;
  1022. count ++;
  1023. } while(!(width == 1 && height == 1 && depth == 1));
  1024. }
  1025. return count;
  1026. }
  1027. void PixelUtil::packColor(const Color& color, PixelFormat format, void* dest)
  1028. {
  1029. packColor(color.r, color.g, color.b, color.a, format, dest);
  1030. }
  1031. void PixelUtil::packColor(UINT8 r, UINT8 g, UINT8 b, UINT8 a, PixelFormat format, void* dest)
  1032. {
  1033. const PixelFormatDescription &des = getDescriptionFor(format);
  1034. if(des.flags & PFF_NATIVEENDIAN)
  1035. {
  1036. // Shortcut for integer formats packing
  1037. UINT32 value = ((Bitwise::fixedToFixed(r, 8, des.rbits)<<des.rshift) & des.rmask) |
  1038. ((Bitwise::fixedToFixed(g, 8, des.gbits)<<des.gshift) & des.gmask) |
  1039. ((Bitwise::fixedToFixed(b, 8, des.bbits)<<des.bshift) & des.bmask) |
  1040. ((Bitwise::fixedToFixed(a, 8, des.abits)<<des.ashift) & des.amask);
  1041. // And write to memory
  1042. Bitwise::intWrite(dest, des.elemBytes, value);
  1043. }
  1044. else
  1045. {
  1046. // Convert to float
  1047. packColor((float)r/255.0f,(float)g/255.0f,(float)b/255.0f,(float)a/255.0f, format, dest);
  1048. }
  1049. }
  1050. void PixelUtil::packColor(float r, float g, float b, float a, const PixelFormat format, void* dest)
  1051. {
  1052. const PixelFormatDescription& des = getDescriptionFor(format);
  1053. if(des.flags & PFF_NATIVEENDIAN)
  1054. {
  1055. // Do the packing
  1056. const unsigned int value = ((Bitwise::floatToFixed(r, des.rbits)<<des.rshift) & des.rmask) |
  1057. ((Bitwise::floatToFixed(g, des.gbits)<<des.gshift) & des.gmask) |
  1058. ((Bitwise::floatToFixed(b, des.bbits)<<des.bshift) & des.bmask) |
  1059. ((Bitwise::floatToFixed(a, des.abits)<<des.ashift) & des.amask);
  1060. // And write to memory
  1061. Bitwise::intWrite(dest, des.elemBytes, value);
  1062. }
  1063. else
  1064. {
  1065. switch(format)
  1066. {
  1067. case PF_FLOAT32_R:
  1068. ((float*)dest)[0] = r;
  1069. break;
  1070. case PF_FLOAT32_RG:
  1071. ((float*)dest)[0] = r;
  1072. ((float*)dest)[1] = g;
  1073. break;
  1074. case PF_FLOAT32_RGB:
  1075. ((float*)dest)[0] = r;
  1076. ((float*)dest)[1] = g;
  1077. ((float*)dest)[2] = b;
  1078. break;
  1079. case PF_FLOAT32_RGBA:
  1080. ((float*)dest)[0] = r;
  1081. ((float*)dest)[1] = g;
  1082. ((float*)dest)[2] = b;
  1083. ((float*)dest)[3] = a;
  1084. break;
  1085. case PF_FLOAT16_R:
  1086. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1087. break;
  1088. case PF_FLOAT16_RG:
  1089. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1090. ((UINT16*)dest)[1] = Bitwise::floatToHalf(g);
  1091. break;
  1092. case PF_FLOAT16_RGB:
  1093. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1094. ((UINT16*)dest)[1] = Bitwise::floatToHalf(g);
  1095. ((UINT16*)dest)[2] = Bitwise::floatToHalf(b);
  1096. break;
  1097. case PF_FLOAT16_RGBA:
  1098. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1099. ((UINT16*)dest)[1] = Bitwise::floatToHalf(g);
  1100. ((UINT16*)dest)[2] = Bitwise::floatToHalf(b);
  1101. ((UINT16*)dest)[3] = Bitwise::floatToHalf(a);
  1102. break;
  1103. case PF_R8G8:
  1104. ((UINT8*)dest)[0] = (UINT8)Bitwise::floatToFixed(r, 8);
  1105. ((UINT8*)dest)[1] = (UINT8)Bitwise::floatToFixed(g, 8);
  1106. break;
  1107. case PF_R8:
  1108. ((UINT8*)dest)[0] = (UINT8)Bitwise::floatToFixed(r, 8);
  1109. break;
  1110. default:
  1111. BS_EXCEPT(NotImplementedException, "Pack to " + getFormatName(format) + " not implemented");
  1112. break;
  1113. }
  1114. }
  1115. }
  1116. void PixelUtil::unpackColor(Color* color, PixelFormat format, const void* src)
  1117. {
  1118. unpackColor(&color->r, &color->g, &color->b, &color->a, format, src);
  1119. }
  1120. void PixelUtil::unpackColor(UINT8* r, UINT8* g, UINT8* b, UINT8* a, PixelFormat format, const void* src)
  1121. {
  1122. const PixelFormatDescription &des = getDescriptionFor(format);
  1123. if(des.flags & PFF_NATIVEENDIAN)
  1124. {
  1125. // Shortcut for integer formats unpacking
  1126. const UINT32 value = Bitwise::intRead(src, des.elemBytes);
  1127. *r = (UINT8)Bitwise::fixedToFixed((value & des.rmask)>>des.rshift, des.rbits, 8);
  1128. *g = (UINT8)Bitwise::fixedToFixed((value & des.gmask)>>des.gshift, des.gbits, 8);
  1129. *b = (UINT8)Bitwise::fixedToFixed((value & des.bmask)>>des.bshift, des.bbits, 8);
  1130. if(des.flags & PFF_HASALPHA)
  1131. {
  1132. *a = (UINT8)Bitwise::fixedToFixed((value & des.amask)>>des.ashift, des.abits, 8);
  1133. }
  1134. else
  1135. {
  1136. *a = 255; // No alpha, default a component to full
  1137. }
  1138. }
  1139. else
  1140. {
  1141. // Do the operation with the more generic floating point
  1142. float rr, gg, bb, aa;
  1143. unpackColor(&rr,&gg,&bb,&aa, format, src);
  1144. *r = (UINT8)Bitwise::floatToFixed(rr, 8);
  1145. *g = (UINT8)Bitwise::floatToFixed(gg, 8);
  1146. *b = (UINT8)Bitwise::floatToFixed(bb, 8);
  1147. *a = (UINT8)Bitwise::floatToFixed(aa, 8);
  1148. }
  1149. }
  1150. void PixelUtil::unpackColor(float* r, float* g, float* b, float* a, PixelFormat format, const void* src)
  1151. {
  1152. const PixelFormatDescription &des = getDescriptionFor(format);
  1153. if(des.flags & PFF_NATIVEENDIAN)
  1154. {
  1155. // Shortcut for integer formats unpacking
  1156. const unsigned int value = Bitwise::intRead(src, des.elemBytes);
  1157. *r = Bitwise::fixedToFloat((value & des.rmask)>>des.rshift, des.rbits);
  1158. *g = Bitwise::fixedToFloat((value & des.gmask)>>des.gshift, des.gbits);
  1159. *b = Bitwise::fixedToFloat((value & des.bmask)>>des.bshift, des.bbits);
  1160. if(des.flags & PFF_HASALPHA)
  1161. {
  1162. *a = Bitwise::fixedToFloat((value & des.amask)>>des.ashift, des.abits);
  1163. }
  1164. else
  1165. {
  1166. *a = 1.0f; // No alpha, default a component to full
  1167. }
  1168. }
  1169. else
  1170. {
  1171. switch(format)
  1172. {
  1173. case PF_FLOAT32_R:
  1174. *r = *g = *b = ((float*)src)[0];
  1175. *a = 1.0f;
  1176. break;
  1177. case PF_FLOAT32_RG:
  1178. *r = ((float*)src)[0];
  1179. *g = *b = ((float*)src)[1];
  1180. *a = 1.0f;
  1181. break;
  1182. case PF_FLOAT32_RGB:
  1183. *r = ((float*)src)[0];
  1184. *g = ((float*)src)[1];
  1185. *b = ((float*)src)[2];
  1186. *a = 1.0f;
  1187. break;
  1188. case PF_FLOAT32_RGBA:
  1189. *r = ((float*)src)[0];
  1190. *g = ((float*)src)[1];
  1191. *b = ((float*)src)[2];
  1192. *a = ((float*)src)[3];
  1193. break;
  1194. case PF_FLOAT16_R:
  1195. *r = *g = *b = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1196. *a = 1.0f;
  1197. break;
  1198. case PF_FLOAT16_RG:
  1199. *r = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1200. *g = *b = Bitwise::halfToFloat(((UINT16*)src)[1]);
  1201. *a = 1.0f;
  1202. break;
  1203. case PF_FLOAT16_RGB:
  1204. *r = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1205. *g = Bitwise::halfToFloat(((UINT16*)src)[1]);
  1206. *b = Bitwise::halfToFloat(((UINT16*)src)[2]);
  1207. *a = 1.0f;
  1208. break;
  1209. case PF_FLOAT16_RGBA:
  1210. *r = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1211. *g = Bitwise::halfToFloat(((UINT16*)src)[1]);
  1212. *b = Bitwise::halfToFloat(((UINT16*)src)[2]);
  1213. *a = Bitwise::halfToFloat(((UINT16*)src)[3]);
  1214. break;
  1215. case PF_R8G8:
  1216. *r = Bitwise::fixedToFloat(((UINT8*)src)[0], 8);
  1217. *g = Bitwise::fixedToFloat(((UINT8*)src)[1], 8);
  1218. *b = 0.0f;
  1219. *a = 1.0f;
  1220. break;
  1221. case PF_R8:
  1222. *r = Bitwise::fixedToFloat(((UINT8*)src)[0], 8);
  1223. *g = 0.0f;
  1224. *b = 0.0f;
  1225. *a = 1.0f;
  1226. break;
  1227. default:
  1228. BS_EXCEPT(NotImplementedException, "Unpack from " + getFormatName(format) + " not implemented");
  1229. break;
  1230. }
  1231. }
  1232. }
  1233. void PixelUtil::bulkPixelConversion(const PixelData &src, const PixelData &dst)
  1234. {
  1235. assert(src.getWidth() == dst.getWidth() &&
  1236. src.getHeight() == dst.getHeight() &&
  1237. src.getDepth() == dst.getDepth());
  1238. // Check for compressed formats, we don't support decompression, compression or recoding
  1239. if(PixelUtil::isCompressed(src.getFormat()) || PixelUtil::isCompressed(dst.getFormat()))
  1240. {
  1241. if(src.getFormat() == dst.getFormat())
  1242. {
  1243. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1244. return;
  1245. }
  1246. else
  1247. {
  1248. BS_EXCEPT(NotImplementedException, "This method can not be used to compress or decompress images");
  1249. }
  1250. }
  1251. // The easy case
  1252. if(src.getFormat() == dst.getFormat())
  1253. {
  1254. // Everything consecutive?
  1255. if(src.isConsecutive() && dst.isConsecutive())
  1256. {
  1257. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1258. return;
  1259. }
  1260. const UINT32 srcPixelSize = PixelUtil::getNumElemBytes(src.getFormat());
  1261. const UINT32 dstPixelSize = PixelUtil::getNumElemBytes(dst.getFormat());
  1262. UINT8 *srcptr = static_cast<UINT8*>(src.getData())
  1263. + (src.getLeft() + src.getTop() * src.getRowPitch() + src.getFront() * src.getSlicePitch()) * srcPixelSize;
  1264. UINT8 *dstptr = static_cast<UINT8*>(dst.getData())
  1265. + (dst.getLeft() + dst.getTop() * dst.getRowPitch() + dst.getFront() * dst.getSlicePitch()) * dstPixelSize;
  1266. // Calculate pitches+skips in bytes
  1267. const UINT32 srcRowPitchBytes = src.getRowPitch()*srcPixelSize;
  1268. const UINT32 srcSliceSkipBytes = src.getSliceSkip()*srcPixelSize;
  1269. const UINT32 dstRowPitchBytes = dst.getRowPitch()*dstPixelSize;
  1270. const UINT32 dstSliceSkipBytes = dst.getSliceSkip()*dstPixelSize;
  1271. // Otherwise, copy per row
  1272. const UINT32 rowSize = src.getWidth()*srcPixelSize;
  1273. for (UINT32 z = src.getFront(); z < src.getBack(); z++)
  1274. {
  1275. for(UINT32 y = src.getTop(); y < src.getBottom(); y++)
  1276. {
  1277. memcpy(dstptr, srcptr, rowSize);
  1278. srcptr += srcRowPitchBytes;
  1279. dstptr += dstRowPitchBytes;
  1280. }
  1281. srcptr += srcSliceSkipBytes;
  1282. dstptr += dstSliceSkipBytes;
  1283. }
  1284. return;
  1285. }
  1286. // Converting to PF_X8R8G8B8 is exactly the same as converting to
  1287. // PF_A8R8G8B8. (same with PF_X8B8G8R8 and PF_A8B8G8R8)
  1288. if(dst.getFormat() == PF_X8R8G8B8 || dst.getFormat() == PF_X8B8G8R8)
  1289. {
  1290. // Do the same conversion, with PF_A8R8G8B8, which has a lot of
  1291. // optimized conversions
  1292. PixelFormat tempFormat = dst.getFormat() == PF_X8R8G8B8?PF_A8R8G8B8:PF_A8B8G8R8;
  1293. PixelData tempdst(dst.getWidth(), dst.getHeight(), dst.getDepth(), tempFormat);
  1294. bulkPixelConversion(src, tempdst);
  1295. return;
  1296. }
  1297. // Converting from PF_X8R8G8B8 is exactly the same as converting from
  1298. // PF_A8R8G8B8, given that the destination format does not have alpha.
  1299. if((src.getFormat() == PF_X8R8G8B8 || src.getFormat() == PF_X8B8G8R8) && !hasAlpha(dst.getFormat()))
  1300. {
  1301. // Do the same conversion, with PF_A8R8G8B8, which has a lot of
  1302. // optimized conversions
  1303. PixelFormat tempFormat = src.getFormat()==PF_X8R8G8B8?PF_A8R8G8B8:PF_A8B8G8R8;
  1304. PixelData tempsrc(src.getWidth(), src.getHeight(), src.getDepth(), tempFormat);
  1305. tempsrc.setExternalBuffer(src.getData());
  1306. bulkPixelConversion(tempsrc, dst);
  1307. return;
  1308. }
  1309. const UINT32 srcPixelSize = PixelUtil::getNumElemBytes(src.getFormat());
  1310. const UINT32 dstPixelSize = PixelUtil::getNumElemBytes(dst.getFormat());
  1311. UINT8 *srcptr = static_cast<UINT8*>(src.getData())
  1312. + (src.getLeft() + src.getTop() * src.getRowPitch() + src.getFront() * src.getSlicePitch()) * srcPixelSize;
  1313. UINT8 *dstptr = static_cast<UINT8*>(dst.getData())
  1314. + (dst.getLeft() + dst.getTop() * dst.getRowPitch() + dst.getFront() * dst.getSlicePitch()) * dstPixelSize;
  1315. // Calculate pitches+skips in bytes
  1316. const UINT32 srcRowSkipBytes = src.getRowSkip()*srcPixelSize;
  1317. const UINT32 srcSliceSkipBytes = src.getSliceSkip()*srcPixelSize;
  1318. const UINT32 dstRowSkipBytes = dst.getRowSkip()*dstPixelSize;
  1319. const UINT32 dstSliceSkipBytes = dst.getSliceSkip()*dstPixelSize;
  1320. // The brute force fallback
  1321. float r,g,b,a;
  1322. for (UINT32 z = src.getFront(); z<src.getBack(); z++)
  1323. {
  1324. for (UINT32 y = src.getTop(); y < src.getBottom(); y++)
  1325. {
  1326. for (UINT32 x = src.getLeft(); x<src.getRight(); x++)
  1327. {
  1328. unpackColor(&r, &g, &b, &a, src.getFormat(), srcptr);
  1329. packColor(r, g, b, a, dst.getFormat(), dstptr);
  1330. srcptr += srcPixelSize;
  1331. dstptr += dstPixelSize;
  1332. }
  1333. srcptr += srcRowSkipBytes;
  1334. dstptr += dstRowSkipBytes;
  1335. }
  1336. srcptr += srcSliceSkipBytes;
  1337. dstptr += dstSliceSkipBytes;
  1338. }
  1339. }
  1340. void PixelUtil::scale(const PixelData& src, const PixelData& scaled, Filter filter)
  1341. {
  1342. assert(PixelUtil::isAccessible(src.getFormat()));
  1343. assert(PixelUtil::isAccessible(scaled.getFormat()));
  1344. PixelData temp;
  1345. switch (filter)
  1346. {
  1347. default:
  1348. case FILTER_NEAREST:
  1349. if(src.getFormat() == scaled.getFormat())
  1350. {
  1351. // No intermediate buffer needed
  1352. temp = scaled;
  1353. }
  1354. else
  1355. {
  1356. // Allocate temporary buffer of destination size in source format
  1357. temp = PixelData(scaled.getWidth(), scaled.getHeight(), scaled.getDepth(), src.getFormat());
  1358. temp.allocateInternalBuffer();
  1359. }
  1360. // No conversion
  1361. switch (PixelUtil::getNumElemBytes(src.getFormat()))
  1362. {
  1363. case 1: NearestResampler<1>::scale(src, temp); break;
  1364. case 2: NearestResampler<2>::scale(src, temp); break;
  1365. case 3: NearestResampler<3>::scale(src, temp); break;
  1366. case 4: NearestResampler<4>::scale(src, temp); break;
  1367. case 6: NearestResampler<6>::scale(src, temp); break;
  1368. case 8: NearestResampler<8>::scale(src, temp); break;
  1369. case 12: NearestResampler<12>::scale(src, temp); break;
  1370. case 16: NearestResampler<16>::scale(src, temp); break;
  1371. default:
  1372. // Never reached
  1373. assert(false);
  1374. }
  1375. if(temp.getData() != scaled.getData())
  1376. {
  1377. // Blit temp buffer
  1378. PixelUtil::bulkPixelConversion(temp, scaled);
  1379. temp.freeInternalBuffer();
  1380. }
  1381. break;
  1382. case FILTER_LINEAR:
  1383. switch (src.getFormat())
  1384. {
  1385. case PF_R8G8:
  1386. case PF_R8G8B8: case PF_B8G8R8:
  1387. case PF_R8G8B8A8: case PF_B8G8R8A8:
  1388. case PF_A8B8G8R8: case PF_A8R8G8B8:
  1389. case PF_X8B8G8R8: case PF_X8R8G8B8:
  1390. if(src.getFormat() == scaled.getFormat())
  1391. {
  1392. // No intermediate buffer needed
  1393. temp = scaled;
  1394. }
  1395. else
  1396. {
  1397. // Allocate temp buffer of destination size in source format
  1398. temp = PixelData(scaled.getWidth(), scaled.getHeight(), scaled.getDepth(), src.getFormat());
  1399. temp.allocateInternalBuffer();
  1400. }
  1401. // No conversion
  1402. switch (PixelUtil::getNumElemBytes(src.getFormat()))
  1403. {
  1404. case 1: LinearResampler_Byte<1>::scale(src, temp); break;
  1405. case 2: LinearResampler_Byte<2>::scale(src, temp); break;
  1406. case 3: LinearResampler_Byte<3>::scale(src, temp); break;
  1407. case 4: LinearResampler_Byte<4>::scale(src, temp); break;
  1408. default:
  1409. // Never reached
  1410. assert(false);
  1411. }
  1412. if(temp.getData() != scaled.getData())
  1413. {
  1414. // Blit temp buffer
  1415. PixelUtil::bulkPixelConversion(temp, scaled);
  1416. temp.freeInternalBuffer();
  1417. }
  1418. break;
  1419. case PF_FLOAT32_RGB:
  1420. case PF_FLOAT32_RGBA:
  1421. if (scaled.getFormat() == PF_FLOAT32_RGB || scaled.getFormat() == PF_FLOAT32_RGBA)
  1422. {
  1423. // float32 to float32, avoid unpack/repack overhead
  1424. LinearResampler_Float32::scale(src, scaled);
  1425. break;
  1426. }
  1427. // Else, fall through
  1428. default:
  1429. // Fallback case, slow but works
  1430. LinearResampler::scale(src, scaled);
  1431. }
  1432. break;
  1433. }
  1434. }
  1435. void PixelUtil::applyGamma(UINT8* buffer, float gamma, UINT32 size, UINT8 bpp)
  1436. {
  1437. if(gamma == 1.0f)
  1438. return;
  1439. UINT32 stride = bpp >> 3;
  1440. for(size_t i = 0, j = size / stride; i < j; i++, buffer += stride)
  1441. {
  1442. float r = (float)buffer[0];
  1443. float g = (float)buffer[1];
  1444. float b = (float)buffer[2];
  1445. r = r * gamma;
  1446. g = g * gamma;
  1447. b = b * gamma;
  1448. float scale = 1.0f;
  1449. float tmp = 0.0f;
  1450. if(r > 255.0f && (tmp=(255.0f/r)) < scale)
  1451. scale = tmp;
  1452. if(g > 255.0f && (tmp=(255.0f/g)) < scale)
  1453. scale = tmp;
  1454. if(b > 255.0f && (tmp=(255.0f/b)) < scale)
  1455. scale = tmp;
  1456. r *= scale;
  1457. g *= scale;
  1458. b *= scale;
  1459. buffer[0] = (UINT8)r;
  1460. buffer[1] = (UINT8)g;
  1461. buffer[2] = (UINT8)b;
  1462. }
  1463. }
  1464. PixelDataPtr PixelUtil::compress(const PixelData& src, const CompressionOptions& options)
  1465. {
  1466. // Note: NVTT site has implementations for these two formats for when I decide to add them
  1467. if (options.format == CompressedFormat::BC6H || options.format == CompressedFormat::BC7)
  1468. BS_EXCEPT(InvalidParametersException, "Specified formats are not yet supported.");
  1469. if (src.getDepth() != 1)
  1470. BS_EXCEPT(InvalidParametersException, "3D textures are not supported.");
  1471. PixelFormat pf = toPixelFormat(options.format);
  1472. // TODO - Get rid of this limitation? Maybe it works without it with no additional changes.
  1473. if (!isValidExtent(src.getWidth(), src.getHeight(), 1, pf))
  1474. BS_EXCEPT(InvalidParametersException, "Source texture dimensions must be divisible by 4.");
  1475. if (isCompressed(src.getFormat()))
  1476. BS_EXCEPT(InvalidParametersException, "Source data cannot be compressed.");
  1477. PixelData argbData(src.getWidth(), src.getHeight(), 1, PF_A8R8G8B8);
  1478. argbData.allocateInternalBuffer();
  1479. bulkPixelConversion(src, argbData);
  1480. nvtt::InputOptions io;
  1481. io.setTextureLayout(nvtt::TextureType_2D, src.getWidth(), src.getHeight());
  1482. io.setMipmapData(argbData.getData(), src.getWidth(), src.getHeight());
  1483. io.setMipmapGeneration(false);
  1484. io.setAlphaMode(toNVTTAlphaMode(options.alphaMode));
  1485. io.setNormalMap(options.isNormalMap);
  1486. if (options.isSRGB)
  1487. io.setGamma(2.2f, 2.2f);
  1488. else
  1489. io.setGamma(1.0f, 1.0f);
  1490. nvtt::CompressionOptions co;
  1491. co.setFormat(toNVTTFormat(options.format));
  1492. co.setQuality(toNVTTQuality(options.quality));
  1493. PixelDataPtr outputPixelData = bs_shared_ptr<PixelData>(src.getWidth(), src.getHeight(), 1, pf);
  1494. outputPixelData->allocateInternalBuffer();
  1495. NVTTCompressOutputHandler outputHandler(outputPixelData->getData(), outputPixelData->getConsecutiveSize());
  1496. nvtt::OutputOptions oo;
  1497. oo.setOutputHeader(false);
  1498. oo.setOutputHandler(&outputHandler);
  1499. nvtt::Compressor compressor;
  1500. if (!compressor.process(io, co, oo))
  1501. BS_EXCEPT(InternalErrorException, "Compressing failed.");
  1502. return outputPixelData;
  1503. }
  1504. Vector<PixelDataPtr> PixelUtil::genMipmaps(const PixelData& src, const MipMapGenOptions& options)
  1505. {
  1506. if (src.getDepth() != 1)
  1507. BS_EXCEPT(InvalidParametersException, "3D textures are not supported.");
  1508. // Note: Add support for floating point mips, no reason they shouldn't be supported other than
  1509. // nvtt doesn't support them natively
  1510. if (isCompressed(src.getFormat()) || isFloatingPoint(src.getFormat()))
  1511. BS_EXCEPT(InvalidParametersException, "Source data cannot be compressed or in floating point format.");
  1512. if (!Math::isPow2(src.getWidth()) || !Math::isPow2(src.getHeight()))
  1513. BS_EXCEPT(InvalidParametersException, "Texture width & height must be powers of 2.");
  1514. PixelData argbData(src.getWidth(), src.getHeight(), 1, PF_A8R8G8B8);
  1515. argbData.allocateInternalBuffer();
  1516. bulkPixelConversion(src, argbData);
  1517. nvtt::InputOptions io;
  1518. io.setTextureLayout(nvtt::TextureType_2D, src.getWidth(), src.getHeight());
  1519. io.setMipmapData(argbData.getData(), src.getWidth(), src.getHeight());
  1520. io.setMipmapGeneration(true);
  1521. io.setNormalMap(options.isNormalMap);
  1522. io.setNormalizeMipmaps(options.normalizeMipmaps);
  1523. io.setWrapMode(toNVTTWrapMode(options.wrapMode));
  1524. nvtt::CompressionOptions co;
  1525. co.setFormat(nvtt::Format_RGBA);
  1526. UINT32 numMips = getMaxMipmaps(src.getWidth(), src.getHeight(), 1, src.getFormat());
  1527. Vector<PixelDataPtr> argbMipBuffers;
  1528. // Note: This can be done more effectively without creating so many temp buffers
  1529. // and working with the original formats directly, but it would complicate the code
  1530. // too much at the moment.
  1531. UINT32 curWidth = src.getWidth();
  1532. UINT32 curHeight = src.getHeight();
  1533. for (UINT32 i = 0; i < numMips; i++)
  1534. {
  1535. if (curWidth > 1)
  1536. curWidth = curWidth / 2;
  1537. if (curHeight > 1)
  1538. curHeight = curHeight / 2;
  1539. argbMipBuffers.push_back(bs_shared_ptr<PixelData>(curWidth, curHeight, 1, PF_A8R8G8B8));
  1540. argbMipBuffers.back()->allocateInternalBuffer();
  1541. }
  1542. NVTTMipmapOutputHandler outputHandler(argbMipBuffers);
  1543. nvtt::OutputOptions oo;
  1544. oo.setOutputHeader(false);
  1545. oo.setOutputHandler(&outputHandler);
  1546. nvtt::Compressor compressor;
  1547. if (!compressor.process(io, co, oo))
  1548. BS_EXCEPT(InternalErrorException, "Mipmap generation failed.");
  1549. argbData.freeInternalBuffer();
  1550. Vector<PixelDataPtr> outputMipBuffers;
  1551. for (UINT32 i = 0; i < (UINT32)argbMipBuffers.size(); i++)
  1552. {
  1553. PixelDataPtr argbBuffer = argbMipBuffers[i];
  1554. PixelDataPtr outputBuffer = bs_shared_ptr<PixelData>(argbBuffer->getWidth(), argbBuffer->getHeight(), 1, src.getFormat());
  1555. outputBuffer->allocateInternalBuffer();
  1556. bulkPixelConversion(*argbBuffer, *outputBuffer);
  1557. argbBuffer->freeInternalBuffer();
  1558. }
  1559. return outputMipBuffers;
  1560. }
  1561. }