BsPixelUtil.cpp 56 KB

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