BsPixelUtil.cpp 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758
  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 < (int)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(PixelFormat format)
  798. {
  799. switch (format)
  800. {
  801. case PF_BC1:
  802. return nvtt::Format_BC1;
  803. case PF_BC1a:
  804. return nvtt::Format_BC1a;
  805. case PF_BC2:
  806. return nvtt::Format_BC2;
  807. case PF_BC3:
  808. return nvtt::Format_BC3;
  809. case PF_BC4:
  810. return nvtt::Format_BC4;
  811. case PF_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. UINT32 PixelUtil::getNumElemBytes(PixelFormat format)
  862. {
  863. return getDescriptionFor(format).elemBytes;
  864. }
  865. UINT32 PixelUtil::getMemorySize(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  866. {
  867. if(isCompressed(format))
  868. {
  869. switch(format)
  870. {
  871. // BC formats work by dividing the image into 4x4 blocks, then encoding each
  872. // 4x4 block with a certain number of bytes.
  873. case PF_BC1:
  874. case PF_BC1a:
  875. case PF_BC4:
  876. return ((width+3)/4)*((height+3)/4)*8 * depth;
  877. case PF_BC2:
  878. case PF_BC3:
  879. case PF_BC5:
  880. case PF_BC6H:
  881. case PF_BC7:
  882. return ((width+3)/4)*((height+3)/4)*16 * depth;
  883. default:
  884. BS_EXCEPT(InvalidParametersException, "Invalid compressed pixel format");
  885. }
  886. }
  887. else
  888. {
  889. return width*height*depth*getNumElemBytes(format);
  890. }
  891. }
  892. void PixelUtil::getSizeForMipLevel(UINT32 width, UINT32 height, UINT32 depth, UINT32 mipLevel,
  893. UINT32& mipWidth, UINT32& mipHeight, UINT32& mipDepth)
  894. {
  895. mipWidth = width;
  896. mipHeight = height;
  897. mipDepth = depth;
  898. for (UINT32 i = 0; i < mipLevel; i++)
  899. {
  900. if (mipWidth != 1) mipWidth /= 2;
  901. if (mipHeight != 1) mipHeight /= 2;
  902. if (mipDepth != 1) mipDepth /= 2;
  903. }
  904. }
  905. UINT32 PixelUtil::getNumElemBits(PixelFormat format)
  906. {
  907. return getDescriptionFor(format).elemBytes * 8;
  908. }
  909. UINT32 PixelUtil::getFlags(PixelFormat format)
  910. {
  911. return getDescriptionFor(format).flags;
  912. }
  913. bool PixelUtil::hasAlpha(PixelFormat format)
  914. {
  915. return (PixelUtil::getFlags(format) & PFF_HASALPHA) > 0;
  916. }
  917. bool PixelUtil::isFloatingPoint(PixelFormat format)
  918. {
  919. return (PixelUtil::getFlags(format) & PFF_FLOAT) > 0;
  920. }
  921. bool PixelUtil::isCompressed(PixelFormat format)
  922. {
  923. return (PixelUtil::getFlags(format) & PFF_COMPRESSED) > 0;
  924. }
  925. bool PixelUtil::isDepth(PixelFormat format)
  926. {
  927. return (PixelUtil::getFlags(format) & PFF_DEPTH) > 0;
  928. }
  929. bool PixelUtil::isNativeEndian(PixelFormat format)
  930. {
  931. return (PixelUtil::getFlags(format) & PFF_NATIVEENDIAN) > 0;
  932. }
  933. bool PixelUtil::isValidExtent(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  934. {
  935. if(isCompressed(format))
  936. {
  937. switch(format)
  938. {
  939. case PF_BC1:
  940. case PF_BC2:
  941. case PF_BC1a:
  942. case PF_BC3:
  943. case PF_BC4:
  944. case PF_BC5:
  945. case PF_BC6H:
  946. case PF_BC7:
  947. return ((width & 3) == 0 && (height & 3) == 0 && depth == 1);
  948. default:
  949. return true;
  950. }
  951. }
  952. else
  953. {
  954. return true;
  955. }
  956. }
  957. void PixelUtil::getBitDepths(PixelFormat format, int rgba[4])
  958. {
  959. const PixelFormatDescription& des = getDescriptionFor(format);
  960. rgba[0] = des.rbits;
  961. rgba[1] = des.gbits;
  962. rgba[2] = des.bbits;
  963. rgba[3] = des.abits;
  964. }
  965. void PixelUtil::getBitMasks(PixelFormat format, UINT32 rgba[4])
  966. {
  967. const PixelFormatDescription& des = getDescriptionFor(format);
  968. rgba[0] = des.rmask;
  969. rgba[1] = des.gmask;
  970. rgba[2] = des.bmask;
  971. rgba[3] = des.amask;
  972. }
  973. void PixelUtil::getBitShifts(PixelFormat format, UINT8 rgba[4])
  974. {
  975. const PixelFormatDescription& des = getDescriptionFor(format);
  976. rgba[0] = des.rshift;
  977. rgba[1] = des.gshift;
  978. rgba[2] = des.bshift;
  979. rgba[3] = des.ashift;
  980. }
  981. String PixelUtil::getFormatName(PixelFormat srcformat)
  982. {
  983. return getDescriptionFor(srcformat).name;
  984. }
  985. bool PixelUtil::isAccessible(PixelFormat srcformat)
  986. {
  987. if (srcformat == PF_UNKNOWN)
  988. return false;
  989. UINT32 flags = getFlags(srcformat);
  990. return !((flags & PFF_COMPRESSED) || (flags & PFF_DEPTH));
  991. }
  992. PixelComponentType PixelUtil::getElementType(PixelFormat format)
  993. {
  994. const PixelFormatDescription& des = getDescriptionFor(format);
  995. return des.componentType;
  996. }
  997. UINT32 PixelUtil::getNumElements(PixelFormat format)
  998. {
  999. const PixelFormatDescription& des = getDescriptionFor(format);
  1000. return des.componentCount;
  1001. }
  1002. UINT32 PixelUtil::getMaxMipmaps(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  1003. {
  1004. UINT32 count = 0;
  1005. if((width > 0) && (height > 0))
  1006. {
  1007. do {
  1008. if(width>1) width = width/2;
  1009. if(height>1) height = height/2;
  1010. if(depth>1) depth = depth/2;
  1011. count ++;
  1012. } while(!(width == 1 && height == 1 && depth == 1));
  1013. }
  1014. return count;
  1015. }
  1016. void PixelUtil::packColor(const Color& color, PixelFormat format, void* dest)
  1017. {
  1018. packColor(color.r, color.g, color.b, color.a, format, dest);
  1019. }
  1020. void PixelUtil::packColor(UINT8 r, UINT8 g, UINT8 b, UINT8 a, PixelFormat format, void* dest)
  1021. {
  1022. const PixelFormatDescription &des = getDescriptionFor(format);
  1023. if(des.flags & PFF_NATIVEENDIAN)
  1024. {
  1025. // Shortcut for integer formats packing
  1026. UINT32 value = ((Bitwise::fixedToFixed(r, 8, des.rbits)<<des.rshift) & des.rmask) |
  1027. ((Bitwise::fixedToFixed(g, 8, des.gbits)<<des.gshift) & des.gmask) |
  1028. ((Bitwise::fixedToFixed(b, 8, des.bbits)<<des.bshift) & des.bmask) |
  1029. ((Bitwise::fixedToFixed(a, 8, des.abits)<<des.ashift) & des.amask);
  1030. // And write to memory
  1031. Bitwise::intWrite(dest, des.elemBytes, value);
  1032. }
  1033. else
  1034. {
  1035. // Convert to float
  1036. packColor((float)r/255.0f,(float)g/255.0f,(float)b/255.0f,(float)a/255.0f, format, dest);
  1037. }
  1038. }
  1039. void PixelUtil::packColor(float r, float g, float b, float a, const PixelFormat format, void* dest)
  1040. {
  1041. const PixelFormatDescription& des = getDescriptionFor(format);
  1042. if(des.flags & PFF_NATIVEENDIAN)
  1043. {
  1044. // Do the packing
  1045. const unsigned int value = ((Bitwise::floatToFixed(r, des.rbits)<<des.rshift) & des.rmask) |
  1046. ((Bitwise::floatToFixed(g, des.gbits)<<des.gshift) & des.gmask) |
  1047. ((Bitwise::floatToFixed(b, des.bbits)<<des.bshift) & des.bmask) |
  1048. ((Bitwise::floatToFixed(a, des.abits)<<des.ashift) & des.amask);
  1049. // And write to memory
  1050. Bitwise::intWrite(dest, des.elemBytes, value);
  1051. }
  1052. else
  1053. {
  1054. switch(format)
  1055. {
  1056. case PF_FLOAT32_R:
  1057. ((float*)dest)[0] = r;
  1058. break;
  1059. case PF_FLOAT32_RG:
  1060. ((float*)dest)[0] = r;
  1061. ((float*)dest)[1] = g;
  1062. break;
  1063. case PF_FLOAT32_RGB:
  1064. ((float*)dest)[0] = r;
  1065. ((float*)dest)[1] = g;
  1066. ((float*)dest)[2] = b;
  1067. break;
  1068. case PF_FLOAT32_RGBA:
  1069. ((float*)dest)[0] = r;
  1070. ((float*)dest)[1] = g;
  1071. ((float*)dest)[2] = b;
  1072. ((float*)dest)[3] = a;
  1073. break;
  1074. case PF_FLOAT16_R:
  1075. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1076. break;
  1077. case PF_FLOAT16_RG:
  1078. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1079. ((UINT16*)dest)[1] = Bitwise::floatToHalf(g);
  1080. break;
  1081. case PF_FLOAT16_RGB:
  1082. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1083. ((UINT16*)dest)[1] = Bitwise::floatToHalf(g);
  1084. ((UINT16*)dest)[2] = Bitwise::floatToHalf(b);
  1085. break;
  1086. case PF_FLOAT16_RGBA:
  1087. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1088. ((UINT16*)dest)[1] = Bitwise::floatToHalf(g);
  1089. ((UINT16*)dest)[2] = Bitwise::floatToHalf(b);
  1090. ((UINT16*)dest)[3] = Bitwise::floatToHalf(a);
  1091. break;
  1092. case PF_R8G8:
  1093. ((UINT8*)dest)[0] = (UINT8)Bitwise::floatToFixed(r, 8);
  1094. ((UINT8*)dest)[1] = (UINT8)Bitwise::floatToFixed(g, 8);
  1095. break;
  1096. case PF_R8:
  1097. ((UINT8*)dest)[0] = (UINT8)Bitwise::floatToFixed(r, 8);
  1098. break;
  1099. default:
  1100. BS_EXCEPT(NotImplementedException, "Pack to " + getFormatName(format) + " not implemented");
  1101. break;
  1102. }
  1103. }
  1104. }
  1105. void PixelUtil::unpackColor(Color* color, PixelFormat format, const void* src)
  1106. {
  1107. unpackColor(&color->r, &color->g, &color->b, &color->a, format, src);
  1108. }
  1109. void PixelUtil::unpackColor(UINT8* r, UINT8* g, UINT8* b, UINT8* a, PixelFormat format, const void* src)
  1110. {
  1111. const PixelFormatDescription &des = getDescriptionFor(format);
  1112. if(des.flags & PFF_NATIVEENDIAN)
  1113. {
  1114. // Shortcut for integer formats unpacking
  1115. const UINT32 value = Bitwise::intRead(src, des.elemBytes);
  1116. *r = (UINT8)Bitwise::fixedToFixed((value & des.rmask)>>des.rshift, des.rbits, 8);
  1117. *g = (UINT8)Bitwise::fixedToFixed((value & des.gmask)>>des.gshift, des.gbits, 8);
  1118. *b = (UINT8)Bitwise::fixedToFixed((value & des.bmask)>>des.bshift, des.bbits, 8);
  1119. if(des.flags & PFF_HASALPHA)
  1120. {
  1121. *a = (UINT8)Bitwise::fixedToFixed((value & des.amask)>>des.ashift, des.abits, 8);
  1122. }
  1123. else
  1124. {
  1125. *a = 255; // No alpha, default a component to full
  1126. }
  1127. }
  1128. else
  1129. {
  1130. // Do the operation with the more generic floating point
  1131. float rr, gg, bb, aa;
  1132. unpackColor(&rr,&gg,&bb,&aa, format, src);
  1133. *r = (UINT8)Bitwise::floatToFixed(rr, 8);
  1134. *g = (UINT8)Bitwise::floatToFixed(gg, 8);
  1135. *b = (UINT8)Bitwise::floatToFixed(bb, 8);
  1136. *a = (UINT8)Bitwise::floatToFixed(aa, 8);
  1137. }
  1138. }
  1139. void PixelUtil::unpackColor(float* r, float* g, float* b, float* a, PixelFormat format, const void* src)
  1140. {
  1141. const PixelFormatDescription &des = getDescriptionFor(format);
  1142. if(des.flags & PFF_NATIVEENDIAN)
  1143. {
  1144. // Shortcut for integer formats unpacking
  1145. const unsigned int value = Bitwise::intRead(src, des.elemBytes);
  1146. *r = Bitwise::fixedToFloat((value & des.rmask)>>des.rshift, des.rbits);
  1147. *g = Bitwise::fixedToFloat((value & des.gmask)>>des.gshift, des.gbits);
  1148. *b = Bitwise::fixedToFloat((value & des.bmask)>>des.bshift, des.bbits);
  1149. if(des.flags & PFF_HASALPHA)
  1150. {
  1151. *a = Bitwise::fixedToFloat((value & des.amask)>>des.ashift, des.abits);
  1152. }
  1153. else
  1154. {
  1155. *a = 1.0f; // No alpha, default a component to full
  1156. }
  1157. }
  1158. else
  1159. {
  1160. switch(format)
  1161. {
  1162. case PF_FLOAT32_R:
  1163. *r = *g = *b = ((float*)src)[0];
  1164. *a = 1.0f;
  1165. break;
  1166. case PF_FLOAT32_RG:
  1167. *r = ((float*)src)[0];
  1168. *g = *b = ((float*)src)[1];
  1169. *a = 1.0f;
  1170. break;
  1171. case PF_FLOAT32_RGB:
  1172. *r = ((float*)src)[0];
  1173. *g = ((float*)src)[1];
  1174. *b = ((float*)src)[2];
  1175. *a = 1.0f;
  1176. break;
  1177. case PF_FLOAT32_RGBA:
  1178. *r = ((float*)src)[0];
  1179. *g = ((float*)src)[1];
  1180. *b = ((float*)src)[2];
  1181. *a = ((float*)src)[3];
  1182. break;
  1183. case PF_FLOAT16_R:
  1184. *r = *g = *b = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1185. *a = 1.0f;
  1186. break;
  1187. case PF_FLOAT16_RG:
  1188. *r = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1189. *g = *b = Bitwise::halfToFloat(((UINT16*)src)[1]);
  1190. *a = 1.0f;
  1191. break;
  1192. case PF_FLOAT16_RGB:
  1193. *r = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1194. *g = Bitwise::halfToFloat(((UINT16*)src)[1]);
  1195. *b = Bitwise::halfToFloat(((UINT16*)src)[2]);
  1196. *a = 1.0f;
  1197. break;
  1198. case PF_FLOAT16_RGBA:
  1199. *r = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1200. *g = Bitwise::halfToFloat(((UINT16*)src)[1]);
  1201. *b = Bitwise::halfToFloat(((UINT16*)src)[2]);
  1202. *a = Bitwise::halfToFloat(((UINT16*)src)[3]);
  1203. break;
  1204. case PF_R8G8:
  1205. *r = Bitwise::fixedToFloat(((UINT8*)src)[0], 8);
  1206. *g = Bitwise::fixedToFloat(((UINT8*)src)[1], 8);
  1207. *b = 0.0f;
  1208. *a = 1.0f;
  1209. break;
  1210. case PF_R8:
  1211. *r = Bitwise::fixedToFloat(((UINT8*)src)[0], 8);
  1212. *g = 0.0f;
  1213. *b = 0.0f;
  1214. *a = 1.0f;
  1215. break;
  1216. default:
  1217. BS_EXCEPT(NotImplementedException, "Unpack from " + getFormatName(format) + " not implemented");
  1218. break;
  1219. }
  1220. }
  1221. }
  1222. void PixelUtil::bulkPixelConversion(const PixelData &src, PixelData &dst)
  1223. {
  1224. assert(src.getWidth() == dst.getWidth() &&
  1225. src.getHeight() == dst.getHeight() &&
  1226. src.getDepth() == dst.getDepth());
  1227. // Check for compressed formats, we don't support decompression
  1228. if(PixelUtil::isCompressed(src.getFormat()))
  1229. {
  1230. if(src.getFormat() == dst.getFormat())
  1231. {
  1232. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1233. return;
  1234. }
  1235. else
  1236. {
  1237. BS_EXCEPT(NotImplementedException, "This method can not be used to compress or decompress images");
  1238. }
  1239. }
  1240. // Check for compression
  1241. if (PixelUtil::isCompressed(dst.getFormat()))
  1242. {
  1243. if (src.getFormat() == dst.getFormat())
  1244. {
  1245. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1246. return;
  1247. }
  1248. else
  1249. {
  1250. CompressionOptions co;
  1251. co.format = dst.getFormat();
  1252. compress(src, dst, co);
  1253. return;
  1254. }
  1255. }
  1256. // The easy case
  1257. if(src.getFormat() == dst.getFormat())
  1258. {
  1259. // Everything consecutive?
  1260. if(src.isConsecutive() && dst.isConsecutive())
  1261. {
  1262. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1263. return;
  1264. }
  1265. const UINT32 srcPixelSize = PixelUtil::getNumElemBytes(src.getFormat());
  1266. const UINT32 dstPixelSize = PixelUtil::getNumElemBytes(dst.getFormat());
  1267. UINT8 *srcptr = static_cast<UINT8*>(src.getData())
  1268. + (src.getLeft() + src.getTop() * src.getRowPitch() + src.getFront() * src.getSlicePitch()) * srcPixelSize;
  1269. UINT8 *dstptr = static_cast<UINT8*>(dst.getData())
  1270. + (dst.getLeft() + dst.getTop() * dst.getRowPitch() + dst.getFront() * dst.getSlicePitch()) * dstPixelSize;
  1271. // Calculate pitches+skips in bytes
  1272. const UINT32 srcRowPitchBytes = src.getRowPitch()*srcPixelSize;
  1273. const UINT32 srcSliceSkipBytes = src.getSliceSkip()*srcPixelSize;
  1274. const UINT32 dstRowPitchBytes = dst.getRowPitch()*dstPixelSize;
  1275. const UINT32 dstSliceSkipBytes = dst.getSliceSkip()*dstPixelSize;
  1276. // Otherwise, copy per row
  1277. const UINT32 rowSize = src.getWidth()*srcPixelSize;
  1278. for (UINT32 z = src.getFront(); z < src.getBack(); z++)
  1279. {
  1280. for(UINT32 y = src.getTop(); y < src.getBottom(); y++)
  1281. {
  1282. memcpy(dstptr, srcptr, rowSize);
  1283. srcptr += srcRowPitchBytes;
  1284. dstptr += dstRowPitchBytes;
  1285. }
  1286. srcptr += srcSliceSkipBytes;
  1287. dstptr += dstSliceSkipBytes;
  1288. }
  1289. return;
  1290. }
  1291. // Converting to PF_X8R8G8B8 is exactly the same as converting to
  1292. // PF_A8R8G8B8. (same with PF_X8B8G8R8 and PF_A8B8G8R8)
  1293. if(dst.getFormat() == PF_X8R8G8B8 || dst.getFormat() == PF_X8B8G8R8)
  1294. {
  1295. // Do the same conversion, with PF_A8R8G8B8, which has a lot of
  1296. // optimized conversions
  1297. PixelFormat tempFormat = dst.getFormat() == PF_X8R8G8B8?PF_A8R8G8B8:PF_A8B8G8R8;
  1298. PixelData tempdst(dst.getWidth(), dst.getHeight(), dst.getDepth(), tempFormat);
  1299. bulkPixelConversion(src, tempdst);
  1300. return;
  1301. }
  1302. // Converting from PF_X8R8G8B8 is exactly the same as converting from
  1303. // PF_A8R8G8B8, given that the destination format does not have alpha.
  1304. if((src.getFormat() == PF_X8R8G8B8 || src.getFormat() == PF_X8B8G8R8) && !hasAlpha(dst.getFormat()))
  1305. {
  1306. // Do the same conversion, with PF_A8R8G8B8, which has a lot of
  1307. // optimized conversions
  1308. PixelFormat tempFormat = src.getFormat()==PF_X8R8G8B8?PF_A8R8G8B8:PF_A8B8G8R8;
  1309. PixelData tempsrc(src.getWidth(), src.getHeight(), src.getDepth(), tempFormat);
  1310. tempsrc.setExternalBuffer(src.getData());
  1311. bulkPixelConversion(tempsrc, dst);
  1312. return;
  1313. }
  1314. const UINT32 srcPixelSize = PixelUtil::getNumElemBytes(src.getFormat());
  1315. const UINT32 dstPixelSize = PixelUtil::getNumElemBytes(dst.getFormat());
  1316. UINT8 *srcptr = static_cast<UINT8*>(src.getData())
  1317. + (src.getLeft() + src.getTop() * src.getRowPitch() + src.getFront() * src.getSlicePitch()) * srcPixelSize;
  1318. UINT8 *dstptr = static_cast<UINT8*>(dst.getData())
  1319. + (dst.getLeft() + dst.getTop() * dst.getRowPitch() + dst.getFront() * dst.getSlicePitch()) * dstPixelSize;
  1320. // Calculate pitches+skips in bytes
  1321. const UINT32 srcRowSkipBytes = src.getRowSkip()*srcPixelSize;
  1322. const UINT32 srcSliceSkipBytes = src.getSliceSkip()*srcPixelSize;
  1323. const UINT32 dstRowSkipBytes = dst.getRowSkip()*dstPixelSize;
  1324. const UINT32 dstSliceSkipBytes = dst.getSliceSkip()*dstPixelSize;
  1325. // The brute force fallback
  1326. float r,g,b,a;
  1327. for (UINT32 z = src.getFront(); z<src.getBack(); z++)
  1328. {
  1329. for (UINT32 y = src.getTop(); y < src.getBottom(); y++)
  1330. {
  1331. for (UINT32 x = src.getLeft(); x<src.getRight(); x++)
  1332. {
  1333. unpackColor(&r, &g, &b, &a, src.getFormat(), srcptr);
  1334. packColor(r, g, b, a, dst.getFormat(), dstptr);
  1335. srcptr += srcPixelSize;
  1336. dstptr += dstPixelSize;
  1337. }
  1338. srcptr += srcRowSkipBytes;
  1339. dstptr += dstRowSkipBytes;
  1340. }
  1341. srcptr += srcSliceSkipBytes;
  1342. dstptr += dstSliceSkipBytes;
  1343. }
  1344. }
  1345. void PixelUtil::scale(const PixelData& src, PixelData& scaled, Filter filter)
  1346. {
  1347. assert(PixelUtil::isAccessible(src.getFormat()));
  1348. assert(PixelUtil::isAccessible(scaled.getFormat()));
  1349. PixelData temp;
  1350. switch (filter)
  1351. {
  1352. default:
  1353. case FILTER_NEAREST:
  1354. if(src.getFormat() == scaled.getFormat())
  1355. {
  1356. // No intermediate buffer needed
  1357. temp = scaled;
  1358. }
  1359. else
  1360. {
  1361. // Allocate temporary buffer of destination size in source format
  1362. temp = PixelData(scaled.getWidth(), scaled.getHeight(), scaled.getDepth(), src.getFormat());
  1363. temp.allocateInternalBuffer();
  1364. }
  1365. // No conversion
  1366. switch (PixelUtil::getNumElemBytes(src.getFormat()))
  1367. {
  1368. case 1: NearestResampler<1>::scale(src, temp); break;
  1369. case 2: NearestResampler<2>::scale(src, temp); break;
  1370. case 3: NearestResampler<3>::scale(src, temp); break;
  1371. case 4: NearestResampler<4>::scale(src, temp); break;
  1372. case 6: NearestResampler<6>::scale(src, temp); break;
  1373. case 8: NearestResampler<8>::scale(src, temp); break;
  1374. case 12: NearestResampler<12>::scale(src, temp); break;
  1375. case 16: NearestResampler<16>::scale(src, temp); break;
  1376. default:
  1377. // Never reached
  1378. assert(false);
  1379. }
  1380. if(temp.getData() != scaled.getData())
  1381. {
  1382. // Blit temp buffer
  1383. PixelUtil::bulkPixelConversion(temp, scaled);
  1384. temp.freeInternalBuffer();
  1385. }
  1386. break;
  1387. case FILTER_LINEAR:
  1388. switch (src.getFormat())
  1389. {
  1390. case PF_R8G8:
  1391. case PF_R8G8B8: case PF_B8G8R8:
  1392. case PF_R8G8B8A8: case PF_B8G8R8A8:
  1393. case PF_A8B8G8R8: case PF_A8R8G8B8:
  1394. case PF_X8B8G8R8: case PF_X8R8G8B8:
  1395. if(src.getFormat() == scaled.getFormat())
  1396. {
  1397. // No intermediate buffer needed
  1398. temp = scaled;
  1399. }
  1400. else
  1401. {
  1402. // Allocate temp buffer of destination size in source format
  1403. temp = PixelData(scaled.getWidth(), scaled.getHeight(), scaled.getDepth(), src.getFormat());
  1404. temp.allocateInternalBuffer();
  1405. }
  1406. // No conversion
  1407. switch (PixelUtil::getNumElemBytes(src.getFormat()))
  1408. {
  1409. case 1: LinearResampler_Byte<1>::scale(src, temp); break;
  1410. case 2: LinearResampler_Byte<2>::scale(src, temp); break;
  1411. case 3: LinearResampler_Byte<3>::scale(src, temp); break;
  1412. case 4: LinearResampler_Byte<4>::scale(src, temp); break;
  1413. default:
  1414. // Never reached
  1415. assert(false);
  1416. }
  1417. if(temp.getData() != scaled.getData())
  1418. {
  1419. // Blit temp buffer
  1420. PixelUtil::bulkPixelConversion(temp, scaled);
  1421. temp.freeInternalBuffer();
  1422. }
  1423. break;
  1424. case PF_FLOAT32_RGB:
  1425. case PF_FLOAT32_RGBA:
  1426. if (scaled.getFormat() == PF_FLOAT32_RGB || scaled.getFormat() == PF_FLOAT32_RGBA)
  1427. {
  1428. // float32 to float32, avoid unpack/repack overhead
  1429. LinearResampler_Float32::scale(src, scaled);
  1430. break;
  1431. }
  1432. // Else, fall through
  1433. default:
  1434. // Fallback case, slow but works
  1435. LinearResampler::scale(src, scaled);
  1436. }
  1437. break;
  1438. }
  1439. }
  1440. void PixelUtil::applyGamma(UINT8* buffer, float gamma, UINT32 size, UINT8 bpp)
  1441. {
  1442. if(gamma == 1.0f)
  1443. return;
  1444. UINT32 stride = bpp >> 3;
  1445. for(size_t i = 0, j = size / stride; i < j; i++, buffer += stride)
  1446. {
  1447. float r = (float)buffer[0];
  1448. float g = (float)buffer[1];
  1449. float b = (float)buffer[2];
  1450. r = r * gamma;
  1451. g = g * gamma;
  1452. b = b * gamma;
  1453. float scale = 1.0f;
  1454. float tmp = 0.0f;
  1455. if(r > 255.0f && (tmp=(255.0f/r)) < scale)
  1456. scale = tmp;
  1457. if(g > 255.0f && (tmp=(255.0f/g)) < scale)
  1458. scale = tmp;
  1459. if(b > 255.0f && (tmp=(255.0f/b)) < scale)
  1460. scale = tmp;
  1461. r *= scale;
  1462. g *= scale;
  1463. b *= scale;
  1464. buffer[0] = (UINT8)r;
  1465. buffer[1] = (UINT8)g;
  1466. buffer[2] = (UINT8)b;
  1467. }
  1468. }
  1469. void PixelUtil::compress(const PixelData& src, PixelData& dst, const CompressionOptions& options)
  1470. {
  1471. if (!isCompressed(options.format))
  1472. BS_EXCEPT(InvalidParametersException, "Wanted format is not a compressed format.");
  1473. // Note: NVTT site has implementations for these two formats for when I decide to add them
  1474. if (options.format == PF_BC6H || options.format == PF_BC7)
  1475. BS_EXCEPT(InvalidParametersException, "Specified formats are not yet supported.");
  1476. if (src.getDepth() != 1)
  1477. BS_EXCEPT(InvalidParametersException, "3D textures are not supported.");
  1478. PixelFormat pf = options.format;
  1479. if (isCompressed(src.getFormat()))
  1480. BS_EXCEPT(InvalidParametersException, "Source data cannot be compressed.");
  1481. PixelData bgraData(src.getWidth(), src.getHeight(), 1, PF_B8G8R8A8);
  1482. bgraData.allocateInternalBuffer();
  1483. bulkPixelConversion(src, bgraData);
  1484. nvtt::InputOptions io;
  1485. io.setTextureLayout(nvtt::TextureType_2D, src.getWidth(), src.getHeight());
  1486. io.setMipmapData(bgraData.getData(), src.getWidth(), src.getHeight());
  1487. io.setMipmapGeneration(false);
  1488. io.setAlphaMode(toNVTTAlphaMode(options.alphaMode));
  1489. io.setNormalMap(options.isNormalMap);
  1490. if (options.isSRGB)
  1491. io.setGamma(2.2f, 2.2f);
  1492. else
  1493. io.setGamma(1.0f, 1.0f);
  1494. nvtt::CompressionOptions co;
  1495. co.setFormat(toNVTTFormat(options.format));
  1496. co.setQuality(toNVTTQuality(options.quality));
  1497. NVTTCompressOutputHandler outputHandler(dst.getData(), dst.getConsecutiveSize());
  1498. nvtt::OutputOptions oo;
  1499. oo.setOutputHeader(false);
  1500. oo.setOutputHandler(&outputHandler);
  1501. nvtt::Compressor compressor;
  1502. if (!compressor.process(io, co, oo))
  1503. BS_EXCEPT(InternalErrorException, "Compressing failed.");
  1504. }
  1505. Vector<PixelDataPtr> PixelUtil::genMipmaps(const PixelData& src, const MipMapGenOptions& options)
  1506. {
  1507. if (src.getDepth() != 1)
  1508. BS_EXCEPT(InvalidParametersException, "3D textures are not supported.");
  1509. // Note: Add support for floating point mips, no reason they shouldn't be supported other than
  1510. // nvtt doesn't support them natively
  1511. if (isCompressed(src.getFormat()) || isFloatingPoint(src.getFormat()))
  1512. BS_EXCEPT(InvalidParametersException, "Source data cannot be compressed or in floating point format.");
  1513. if (!Math::isPow2(src.getWidth()) || !Math::isPow2(src.getHeight()))
  1514. BS_EXCEPT(InvalidParametersException, "Texture width & height must be powers of 2.");
  1515. PixelData argbData(src.getWidth(), src.getHeight(), 1, PF_A8R8G8B8);
  1516. argbData.allocateInternalBuffer();
  1517. bulkPixelConversion(src, argbData);
  1518. nvtt::InputOptions io;
  1519. io.setTextureLayout(nvtt::TextureType_2D, src.getWidth(), src.getHeight());
  1520. io.setMipmapData(argbData.getData(), src.getWidth(), src.getHeight());
  1521. io.setMipmapGeneration(true);
  1522. io.setNormalMap(options.isNormalMap);
  1523. io.setNormalizeMipmaps(options.normalizeMipmaps);
  1524. io.setWrapMode(toNVTTWrapMode(options.wrapMode));
  1525. nvtt::CompressionOptions co;
  1526. co.setFormat(nvtt::Format_RGBA);
  1527. UINT32 numMips = getMaxMipmaps(src.getWidth(), src.getHeight(), 1, src.getFormat());
  1528. Vector<PixelDataPtr> argbMipBuffers;
  1529. // Note: This can be done more effectively without creating so many temp buffers
  1530. // and working with the original formats directly, but it would complicate the code
  1531. // too much at the moment.
  1532. UINT32 curWidth = src.getWidth();
  1533. UINT32 curHeight = src.getHeight();
  1534. for (UINT32 i = 0; i < numMips; i++)
  1535. {
  1536. argbMipBuffers.push_back(bs_shared_ptr<PixelData>(curWidth, curHeight, 1, PF_A8R8G8B8));
  1537. argbMipBuffers.back()->allocateInternalBuffer();
  1538. if (curWidth > 1)
  1539. curWidth = curWidth / 2;
  1540. if (curHeight > 1)
  1541. curHeight = curHeight / 2;
  1542. }
  1543. argbMipBuffers.push_back(bs_shared_ptr<PixelData>(curWidth, curHeight, 1, PF_A8R8G8B8));
  1544. argbMipBuffers.back()->allocateInternalBuffer();
  1545. NVTTMipmapOutputHandler outputHandler(argbMipBuffers);
  1546. nvtt::OutputOptions oo;
  1547. oo.setOutputHeader(false);
  1548. oo.setOutputHandler(&outputHandler);
  1549. nvtt::Compressor compressor;
  1550. if (!compressor.process(io, co, oo))
  1551. BS_EXCEPT(InternalErrorException, "Mipmap generation failed.");
  1552. argbData.freeInternalBuffer();
  1553. Vector<PixelDataPtr> outputMipBuffers;
  1554. for (UINT32 i = 0; i < (UINT32)argbMipBuffers.size(); i++)
  1555. {
  1556. PixelDataPtr argbBuffer = argbMipBuffers[i];
  1557. PixelDataPtr outputBuffer = bs_shared_ptr<PixelData>(argbBuffer->getWidth(), argbBuffer->getHeight(), 1, src.getFormat());
  1558. outputBuffer->allocateInternalBuffer();
  1559. bulkPixelConversion(*argbBuffer, *outputBuffer);
  1560. argbBuffer->freeInternalBuffer();
  1561. outputMipBuffers.push_back(outputBuffer);
  1562. }
  1563. return outputMipBuffers;
  1564. }
  1565. }