2
0

BsPixelUtil.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. #include "BsPixelUtil.h"
  2. #include "BsBitwise.h"
  3. #include "BsColor.h"
  4. #include "BsException.h"
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Performs pixel data resampling using the point filter (nearest neighbor).
  9. * Does not perform format conversions.
  10. *
  11. * @tparam elementSize Size of a single pixel in bytes.
  12. */
  13. template<UINT32 elementSize> struct NearestResampler
  14. {
  15. static void scale(const PixelData& source, const PixelData& dest)
  16. {
  17. UINT8* sourceData = source.getData();
  18. UINT8* destPtr = dest.getData();
  19. // Get steps for traversing source data in 16/48 fixed point format
  20. UINT64 stepX = ((UINT64)source.getWidth() << 48) / dest.getWidth();
  21. UINT64 stepY = ((UINT64)source.getHeight() << 48) / dest.getHeight();
  22. UINT64 stepZ = ((UINT64)source.getDepth() << 48) / dest.getDepth();
  23. UINT64 curZ = (stepZ >> 1) - 1; // Offset half a pixel to start at pixel center
  24. for (UINT32 z = dest.getFront(); z < dest.getBack(); z++, curZ += stepZ)
  25. {
  26. UINT32 offsetZ = (UINT32)(curZ >> 48) * source.getSlicePitch();
  27. UINT64 curY = (stepY >> 1) - 1; // Offset half a pixel to start at pixel center
  28. for (UINT32 y = dest.getTop(); y < dest.getBottom(); y++, curY += stepY)
  29. {
  30. UINT32 offsetY = (UINT32)(curY >> 48) * source.getRowPitch();
  31. UINT64 curX = (stepX >> 1) - 1; // Offset half a pixel to start at pixel center
  32. for (UINT32 x = dest.getLeft(); x < dest.getRight(); x++, curX += stepX)
  33. {
  34. UINT32 offsetX = (UINT32)(curX >> 48);
  35. UINT32 offsetBytes = elementSize*(offsetX + offsetY + offsetZ);
  36. UINT8* curSourcePtr = sourceData + offsetBytes;
  37. memcpy(destPtr, curSourcePtr, elementSize);
  38. destPtr += elementSize;
  39. }
  40. destPtr += elementSize*dest.getRowSkip();
  41. }
  42. destPtr += elementSize*dest.getSliceSkip();
  43. }
  44. }
  45. };
  46. /**
  47. * @brief Performs pixel data resampling using the box filter (linear).
  48. * Performs format conversions.
  49. */
  50. struct LinearResampler
  51. {
  52. static void scale(const PixelData& source, const PixelData& dest)
  53. {
  54. UINT32 sourceElemSize = PixelUtil::getNumElemBytes(source.getFormat());
  55. UINT32 destElemSize = PixelUtil::getNumElemBytes(dest.getFormat());
  56. UINT8* sourceData = source.getData();
  57. UINT8* destPtr = dest.getData();
  58. // Get steps for traversing source data in 16/48 fixed point precision format
  59. UINT64 stepX = ((UINT64)source.getWidth() << 48) / dest.getWidth();
  60. UINT64 stepY = ((UINT64)source.getHeight() << 48) / dest.getHeight();
  61. UINT64 stepZ = ((UINT64)source.getDepth() << 48) / dest.getDepth();
  62. // Contains 16/16 fixed point precision format. Most significant
  63. // 16 bits will contain the coordinate in the source image, and the
  64. // least significant 16 bits will contain the fractional part of the coordinate
  65. // that will be used for determining the blend amount.
  66. UINT32 temp = 0;
  67. UINT64 curZ = (stepZ >> 1) - 1; // Offset half a pixel to start at pixel center
  68. for (UINT32 z = dest.getFront(); z < dest.getBack(); z++, curZ += stepZ)
  69. {
  70. temp = UINT32(curZ >> 32);
  71. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  72. UINT32 sampleCoordZ1 = temp >> 16;
  73. UINT32 sampleCoordZ2 = std::min(sampleCoordZ1 + 1, (UINT32)source.getDepth() - 1);
  74. float sampleWeightZ = (temp & 0xFFFF) / 65536.0f;
  75. UINT64 curY = (stepY >> 1) - 1; // Offset half a pixel to start at pixel center
  76. for (UINT32 y = dest.getTop(); y < dest.getBottom(); y++, curY += stepY)
  77. {
  78. temp = (UINT32)(curY >> 32);
  79. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  80. UINT32 sampleCoordY1 = temp >> 16;
  81. UINT32 sampleCoordY2 = std::min(sampleCoordY1 + 1, (UINT32)source.getHeight() - 1);
  82. float sampleWeightY = (temp & 0xFFFF) / 65536.0f;
  83. UINT64 curX = (stepX >> 1) - 1; // Offset half a pixel to start at pixel center
  84. for (UINT32 x = dest.getLeft(); x < dest.getRight(); x++, curX += stepX)
  85. {
  86. temp = (UINT32)(curX >> 32);
  87. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  88. UINT32 sampleCoordX1 = temp >> 16;
  89. UINT32 sampleCoordX2 = std::min(sampleCoordX1 + 1, (UINT32)source.getWidth() - 1);
  90. float sampleWeightX = (temp & 0xFFFF) / 65536.0f;
  91. Color x1y1z1, x2y1z1, x1y2z1, x2y2z1;
  92. Color x1y1z2, x2y1z2, x1y2z2, x2y2z2;
  93. #define GETSOURCEDATA(x, y, z) sourceData + sourceElemSize*((x)+(y)*source.getRowPitch() + (z)*source.getSlicePitch())
  94. PixelUtil::unpackColor(&x1y1z1, source.getFormat(), GETSOURCEDATA(sampleCoordX1, sampleCoordY1, sampleCoordZ1));
  95. PixelUtil::unpackColor(&x2y1z1, source.getFormat(), GETSOURCEDATA(sampleCoordX2, sampleCoordY1, sampleCoordZ1));
  96. PixelUtil::unpackColor(&x1y2z1, source.getFormat(), GETSOURCEDATA(sampleCoordX1, sampleCoordY2, sampleCoordZ1));
  97. PixelUtil::unpackColor(&x2y2z1, source.getFormat(), GETSOURCEDATA(sampleCoordX2, sampleCoordY2, sampleCoordZ1));
  98. PixelUtil::unpackColor(&x1y1z2, source.getFormat(), GETSOURCEDATA(sampleCoordX1, sampleCoordY1, sampleCoordZ2));
  99. PixelUtil::unpackColor(&x2y1z2, source.getFormat(), GETSOURCEDATA(sampleCoordX2, sampleCoordY1, sampleCoordZ2));
  100. PixelUtil::unpackColor(&x1y2z2, source.getFormat(), GETSOURCEDATA(sampleCoordX1, sampleCoordY2, sampleCoordZ2));
  101. PixelUtil::unpackColor(&x2y2z2, source.getFormat(), GETSOURCEDATA(sampleCoordX2, sampleCoordY2, sampleCoordZ2));
  102. #undef GETSOURCEDATA
  103. Color accum =
  104. x1y1z1 * ((1.0f - sampleWeightX)*(1.0f - sampleWeightY)*(1.0f - sampleWeightZ)) +
  105. x2y1z1 * ( sampleWeightX *(1.0f - sampleWeightY)*(1.0f - sampleWeightZ)) +
  106. x1y2z1 * ((1.0f - sampleWeightX)* sampleWeightY *(1.0f - sampleWeightZ)) +
  107. x2y2z1 * ( sampleWeightX * sampleWeightY *(1.0f - sampleWeightZ)) +
  108. x1y1z2 * ((1.0f - sampleWeightX)*(1.0f - sampleWeightY)* sampleWeightZ ) +
  109. x2y1z2 * ( sampleWeightX *(1.0f - sampleWeightY)* sampleWeightZ ) +
  110. x1y2z2 * ((1.0f - sampleWeightX)* sampleWeightY * sampleWeightZ ) +
  111. x2y2z2 * ( sampleWeightX * sampleWeightY * sampleWeightZ );
  112. PixelUtil::packColor(accum, dest.getFormat(), destPtr);
  113. destPtr += destElemSize;
  114. }
  115. destPtr += destElemSize * dest.getRowSkip();
  116. }
  117. destPtr += destElemSize * dest.getSliceSkip();
  118. }
  119. }
  120. };
  121. /**
  122. * @brief Performs pixel data resampling using the box filter (linear).
  123. * Only handles float RGB or RGBA pixel data (32 bits per channel).
  124. */
  125. struct LinearResampler_Float32
  126. {
  127. static void scale(const PixelData& source, const PixelData& dest)
  128. {
  129. UINT32 numSourceChannels = PixelUtil::getNumElemBytes(source.getFormat()) / sizeof(float);
  130. UINT32 numDestChannels = PixelUtil::getNumElemBytes(dest.getFormat()) / sizeof(float);
  131. float* sourceData = (float*)source.getData();
  132. float* destPtr = (float*)dest.getData();
  133. // Get steps for traversing source data in 16/48 fixed point precision format
  134. UINT64 stepX = ((UINT64)source.getWidth() << 48) / dest.getWidth();
  135. UINT64 stepY = ((UINT64)source.getHeight() << 48) / dest.getHeight();
  136. UINT64 stepZ = ((UINT64)source.getDepth() << 48) / dest.getDepth();
  137. // Contains 16/16 fixed point precision format. Most significant
  138. // 16 bits will contain the coordinate in the source image, and the
  139. // least significant 16 bits will contain the fractional part of the coordinate
  140. // that will be used for determining the blend amount.
  141. UINT32 temp = 0;
  142. UINT64 curZ = (stepZ >> 1) - 1; // Offset half a pixel to start at pixel center
  143. for (UINT32 z = dest.getFront(); z < dest.getBack(); z++, curZ += stepZ)
  144. {
  145. temp = (UINT32)(curZ >> 32);
  146. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  147. UINT32 sampleCoordZ1 = temp >> 16;
  148. UINT32 sampleCoordZ2 = std::min(sampleCoordZ1 + 1, (UINT32)source.getDepth() - 1);
  149. float sampleWeightZ = (temp & 0xFFFF) / 65536.0f;
  150. UINT64 curY = (stepY >> 1) - 1; // Offset half a pixel to start at pixel center
  151. for (UINT32 y = dest.getTop(); y < dest.getBottom(); y++, curY += stepY)
  152. {
  153. temp = (UINT32)(curY >> 32);
  154. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  155. UINT32 sampleCoordY1 = temp >> 16;
  156. UINT32 sampleCoordY2 = std::min(sampleCoordY1 + 1, (UINT32)source.getHeight() - 1);
  157. float sampleWeightY = (temp & 0xFFFF) / 65536.0f;
  158. UINT64 curX = (stepX >> 1) - 1; // Offset half a pixel to start at pixel center
  159. for (UINT32 x = dest.getLeft(); x < dest.getRight(); x++, curX += stepX)
  160. {
  161. temp = (UINT32)(curX >> 32);
  162. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  163. UINT32 sampleCoordX1 = temp >> 16;
  164. UINT32 sampleCoordX2 = std::min(sampleCoordX1 + 1, (UINT32)source.getWidth() - 1);
  165. float sampleWeightX = (temp & 0xFFFF) / 65536.0f;
  166. // process R,G,B,A simultaneously for cache coherence?
  167. float accum[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  168. #define ACCUM3(x,y,z,factor) \
  169. { float f = factor; \
  170. UINT32 offset = (x + y*source.getRowPitch() + z*source.getSlicePitch())*numSourceChannels; \
  171. accum[0] += sourceData[offset + 0] * f; accum[1] += sourceData[offset + 1] * f; \
  172. accum[2] += sourceData[offset + 2] * f; }
  173. #define ACCUM4(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; accum[3] += sourceData[offset + 3] * f; }
  178. if (numSourceChannels == 3 || numDestChannels == 3)
  179. {
  180. // RGB
  181. ACCUM3(sampleCoordX1, sampleCoordY1, sampleCoordZ1, (1.0f - sampleWeightX) * (1.0f - sampleWeightY) * (1.0f - sampleWeightZ));
  182. ACCUM3(sampleCoordX2, sampleCoordY1, sampleCoordZ1, sampleWeightX * (1.0f - sampleWeightY) * (1.0f - sampleWeightZ));
  183. ACCUM3(sampleCoordX1, sampleCoordY2, sampleCoordZ1, (1.0f - sampleWeightX) * sampleWeightY * (1.0f - sampleWeightZ));
  184. ACCUM3(sampleCoordX2, sampleCoordY2, sampleCoordZ1, sampleWeightX * sampleWeightY * (1.0f - sampleWeightZ));
  185. ACCUM3(sampleCoordX1, sampleCoordY1, sampleCoordZ2, (1.0f - sampleWeightX) * (1.0f - sampleWeightY) * sampleWeightZ);
  186. ACCUM3(sampleCoordX2, sampleCoordY1, sampleCoordZ2, sampleWeightX * (1.0f - sampleWeightY) * sampleWeightZ);
  187. ACCUM3(sampleCoordX1, sampleCoordY2, sampleCoordZ2, (1.0f - sampleWeightX) * sampleWeightY * sampleWeightZ);
  188. ACCUM3(sampleCoordX2, sampleCoordY2, sampleCoordZ2, sampleWeightX * sampleWeightY * sampleWeightZ);
  189. accum[3] = 1.0f;
  190. }
  191. else
  192. {
  193. // RGBA
  194. ACCUM4(sampleCoordX1, sampleCoordY1, sampleCoordZ1, (1.0f - sampleWeightX) * (1.0f - sampleWeightY) * (1.0f - sampleWeightZ));
  195. ACCUM4(sampleCoordX2, sampleCoordY1, sampleCoordZ1, sampleWeightX * (1.0f - sampleWeightY) * (1.0f - sampleWeightZ));
  196. ACCUM4(sampleCoordX1, sampleCoordY2, sampleCoordZ1, (1.0f - sampleWeightX) * sampleWeightY * (1.0f - sampleWeightZ));
  197. ACCUM4(sampleCoordX2, sampleCoordY2, sampleCoordZ1, sampleWeightX * sampleWeightY * (1.0f - sampleWeightZ));
  198. ACCUM4(sampleCoordX1, sampleCoordY1, sampleCoordZ2, (1.0f - sampleWeightX) * (1.0f - sampleWeightY) * sampleWeightZ);
  199. ACCUM4(sampleCoordX2, sampleCoordY1, sampleCoordZ2, sampleWeightX * (1.0f - sampleWeightY) * sampleWeightZ);
  200. ACCUM4(sampleCoordX1, sampleCoordY2, sampleCoordZ2, (1.0f - sampleWeightX) * sampleWeightY * sampleWeightZ);
  201. ACCUM4(sampleCoordX2, sampleCoordY2, sampleCoordZ2, sampleWeightX * sampleWeightY * sampleWeightZ);
  202. }
  203. memcpy(destPtr, accum, sizeof(float)*numDestChannels);
  204. #undef ACCUM3
  205. #undef ACCUM4
  206. destPtr += numDestChannels;
  207. }
  208. destPtr += numDestChannels*dest.getRowSkip();
  209. }
  210. destPtr += numDestChannels*dest.getSliceSkip();
  211. }
  212. }
  213. };
  214. // byte linear resampler, does not do any format conversions.
  215. // only handles pixel formats that use 1 byte per color channel.
  216. // 2D only; punts 3D pixelboxes to default LinearResampler (slow).
  217. // templated on bytes-per-pixel to allow compiler optimizations, such
  218. // as unrolling loops and replacing multiplies with bitshifts
  219. /**
  220. * @brief Performs pixel data resampling using the box filter (linear).
  221. * Only handles pixel formats with one byte per channel. Does
  222. * not perform format conversion.
  223. *
  224. * @tparam channels Number of channels in the pixel format.
  225. */
  226. template<UINT32 channels> struct LinearResampler_Byte
  227. {
  228. static void scale(const PixelData& source, const PixelData& dest)
  229. {
  230. // Only optimized for 2D
  231. if (source.getDepth() > 1 || dest.getDepth() > 1)
  232. {
  233. LinearResampler::scale(source, dest);
  234. return;
  235. }
  236. UINT8* sourceData = (UINT8*)source.getData();
  237. UINT8* destPtr = (UINT8*)dest.getData();
  238. // Get steps for traversing source data in 16/48 fixed point precision format
  239. UINT64 stepX = ((UINT64)source.getWidth() << 48) / dest.getWidth();
  240. UINT64 stepY = ((UINT64)source.getHeight() << 48) / dest.getHeight();
  241. // Contains 16/16 fixed point precision format. Most significant
  242. // 16 bits will contain the coordinate in the source image, and the
  243. // least significant 16 bits will contain the fractional part of the coordinate
  244. // that will be used for determining the blend amount.
  245. UINT32 temp;
  246. UINT64 curY = (stepY >> 1) - 1; // Offset half a pixel to start at pixel center
  247. for (UINT32 y = dest.getTop(); y < dest.getBottom(); y++, curY += stepY)
  248. {
  249. temp = (UINT32)(curY >> 36);
  250. temp = (temp > 0x800)? temp - 0x800: 0;
  251. UINT32 sampleWeightY = temp & 0xFFF;
  252. UINT32 sampleCoordY1 = temp >> 12;
  253. UINT32 sampleCoordY2 = std::min(sampleCoordY1 + 1, (UINT32)source.getBottom() - source.getTop() - 1);
  254. UINT32 sampleY1Offset = sampleCoordY1 * source.getRowPitch();
  255. UINT32 sampleY2Offset = sampleCoordY2 * source.getRowPitch();
  256. UINT64 curX = (stepX >> 1) - 1; // Offset half a pixel to start at pixel center
  257. for (UINT32 x = dest.getLeft(); x < dest.getRight(); x++, curX += stepX)
  258. {
  259. temp = (UINT32)(curX >> 36);
  260. temp = (temp > 0x800)? temp - 0x800 : 0;
  261. UINT32 sampleWeightX = temp & 0xFFF;
  262. UINT32 sampleCoordX1 = temp >> 12;
  263. UINT32 sampleCoordX2 = std::min(sampleCoordX1 + 1, (UINT32)source.getRight() - source.getLeft() - 1);
  264. UINT32 sxfsyf = sampleWeightX*sampleWeightY;
  265. for (UINT32 k = 0; k < channels; k++)
  266. {
  267. UINT32 accum =
  268. sourceData[(sampleCoordX1 + sampleY1Offset)*channels+k]*(0x1000000-(sampleWeightX<<12)-(sampleWeightY<<12)+sxfsyf) +
  269. sourceData[(sampleCoordX2 + sampleY1Offset)*channels+k]*((sampleWeightX<<12)-sxfsyf) +
  270. sourceData[(sampleCoordX1 + sampleY2Offset)*channels+k]*((sampleWeightY<<12)-sxfsyf) +
  271. sourceData[(sampleCoordX2 + sampleY2Offset)*channels+k]*sxfsyf;
  272. // Round up to byte size
  273. *destPtr = (UINT8)((accum + 0x800000) >> 24);
  274. destPtr++;
  275. }
  276. }
  277. destPtr += channels*dest.getRowSkip();
  278. }
  279. }
  280. };
  281. /**
  282. * @brief Data describing a pixel format.
  283. */
  284. struct PixelFormatDescription
  285. {
  286. const char* name; /**< Name of the format. */
  287. UINT8 elemBytes; /**< Number of bytes one element (color value) uses. */
  288. UINT32 flags; /**< PixelFormatFlags set by the pixel format. */
  289. PixelComponentType componentType; /**< Data type of a single element of the format. */
  290. UINT8 componentCount; /**< Number of elements in the format. */
  291. UINT8 rbits, gbits, bbits, abits; /**< Number of bits per element in the format. */
  292. UINT32 rmask, gmask, bmask, amask; /**< Masks used by packers/unpackers. */
  293. UINT8 rshift, gshift, bshift, ashift; /**< Shifts used by packers/unpackers. */
  294. };
  295. /**
  296. * @brief A list of all available pixel formats.
  297. */
  298. PixelFormatDescription _pixelFormats[PF_COUNT] = {
  299. {"PF_UNKNOWN",
  300. /* Bytes per element */
  301. 0,
  302. /* Flags */
  303. 0,
  304. /* Component type and count */
  305. PCT_BYTE, 0,
  306. /* rbits, gbits, bbits, abits */
  307. 0, 0, 0, 0,
  308. /* Masks and shifts */
  309. 0, 0, 0, 0, 0, 0, 0, 0
  310. },
  311. //-----------------------------------------------------------------------
  312. {"PF_R8",
  313. /* Bytes per element */
  314. 1,
  315. /* Flags */
  316. 0,
  317. /* Component type and count */
  318. PCT_BYTE, 1,
  319. /* rbits, gbits, bbits, abits */
  320. 8, 0, 0, 0,
  321. /* Masks and shifts */
  322. 0x000000FF, 0, 0, 0,
  323. 0, 0, 0, 0
  324. },
  325. //-----------------------------------------------------------------------
  326. {"PF_R8G8",
  327. /* Bytes per element */
  328. 2,
  329. /* Flags */
  330. 0,
  331. /* Component type and count */
  332. PCT_BYTE, 2,
  333. /* rbits, gbits, bbits, abits */
  334. 8, 8, 0, 0,
  335. /* Masks and shifts */
  336. 0x000000FF, 0x0000FF00, 0, 0,
  337. 0, 8, 0, 0
  338. },
  339. //-----------------------------------------------------------------------
  340. {"PF_R8G8B8",
  341. /* Bytes per element */
  342. 3, // 24 bit integer -- special
  343. /* Flags */
  344. PFF_NATIVEENDIAN,
  345. /* Component type and count */
  346. PCT_BYTE, 3,
  347. /* rbits, gbits, bbits, abits */
  348. 8, 8, 8, 0,
  349. /* Masks and shifts */
  350. 0x000000FF, 0x0000FF00, 0x00FF0000, 0,
  351. 0, 8, 16, 0
  352. },
  353. //-----------------------------------------------------------------------
  354. {"PF_B8G8R8",
  355. /* Bytes per element */
  356. 3, // 24 bit integer -- special
  357. /* Flags */
  358. PFF_NATIVEENDIAN,
  359. /* Component type and count */
  360. PCT_BYTE, 3,
  361. /* rbits, gbits, bbits, abits */
  362. 8, 8, 8, 0,
  363. /* Masks and shifts */
  364. 0x00FF0000, 0x0000FF00, 0x000000FF, 0,
  365. 16, 8, 0, 0
  366. },
  367. //-----------------------------------------------------------------------
  368. {"PF_A8R8G8B8",
  369. /* Bytes per element */
  370. 4,
  371. /* Flags */
  372. PFF_HASALPHA | PFF_NATIVEENDIAN,
  373. /* Component type and count */
  374. PCT_BYTE, 4,
  375. /* rbits, gbits, bbits, abits */
  376. 8, 8, 8, 8,
  377. /* Masks and shifts */
  378. 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF,
  379. 8, 16, 24, 0
  380. },
  381. //-----------------------------------------------------------------------
  382. {"PF_A8B8G8R8",
  383. /* Bytes per element */
  384. 4,
  385. /* Flags */
  386. PFF_HASALPHA | PFF_NATIVEENDIAN,
  387. /* Component type and count */
  388. PCT_BYTE, 4,
  389. /* rbits, gbits, bbits, abits */
  390. 8, 8, 8, 8,
  391. /* Masks and shifts */
  392. 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF,
  393. 24, 16, 8, 0,
  394. },
  395. //-----------------------------------------------------------------------
  396. {"PF_B8G8R8A8",
  397. /* Bytes per element */
  398. 4,
  399. /* Flags */
  400. PFF_HASALPHA | PFF_NATIVEENDIAN,
  401. /* Component type and count */
  402. PCT_BYTE, 4,
  403. /* rbits, gbits, bbits, abits */
  404. 8, 8, 8, 8,
  405. /* Masks and shifts */
  406. 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000,
  407. 16, 8, 0, 24
  408. },
  409. //-----------------------------------------------------------------------
  410. {"PF_R8G8B8A8",
  411. /* Bytes per element */
  412. 4,
  413. /* Flags */
  414. PFF_HASALPHA | PFF_NATIVEENDIAN,
  415. /* Component type and count */
  416. PCT_BYTE, 4,
  417. /* rbits, gbits, bbits, abits */
  418. 8, 8, 8, 8,
  419. /* Masks and shifts */
  420. 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000,
  421. 0, 8, 16, 24
  422. },
  423. //-----------------------------------------------------------------------
  424. {"PF_X8R8G8B8",
  425. /* Bytes per element */
  426. 4,
  427. /* Flags */
  428. PFF_NATIVEENDIAN,
  429. /* Component type and count */
  430. PCT_BYTE, 3,
  431. /* rbits, gbits, bbits, abits */
  432. 8, 8, 8, 0,
  433. /* Masks and shifts */
  434. 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF,
  435. 8, 16, 24, 0
  436. },
  437. //-----------------------------------------------------------------------
  438. {"PF_X8B8G8R8",
  439. /* Bytes per element */
  440. 4,
  441. /* Flags */
  442. PFF_NATIVEENDIAN,
  443. /* Component type and count */
  444. PCT_BYTE, 3,
  445. /* rbits, gbits, bbits, abits */
  446. 8, 8, 8, 0,
  447. /* Masks and shifts */
  448. 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF,
  449. 24, 16, 8, 0
  450. },
  451. //-----------------------------------------------------------------------
  452. {"PF_R8G8B8X8",
  453. /* Bytes per element */
  454. 4,
  455. /* Flags */
  456. PFF_HASALPHA | PFF_NATIVEENDIAN,
  457. /* Component type and count */
  458. PCT_BYTE, 3,
  459. /* rbits, gbits, bbits, abits */
  460. 8, 8, 8, 0,
  461. /* Masks and shifts */
  462. 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000,
  463. 0, 8, 16, 0
  464. },
  465. //-----------------------------------------------------------------------
  466. {"PF_B8G8R8X8",
  467. /* Bytes per element */
  468. 4,
  469. /* Flags */
  470. PFF_HASALPHA | PFF_NATIVEENDIAN,
  471. /* Component type and count */
  472. PCT_BYTE, 3,
  473. /* rbits, gbits, bbits, abits */
  474. 8, 8, 8, 0,
  475. /* Masks and shifts */
  476. 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000,
  477. 16, 8, 0, 0
  478. },
  479. //-----------------------------------------------------------------------
  480. {"PF_DXT1",
  481. /* Bytes per element */
  482. 0,
  483. /* Flags */
  484. PFF_COMPRESSED | PFF_HASALPHA,
  485. /* Component type and count */
  486. PCT_BYTE, 3, // No alpha
  487. /* rbits, gbits, bbits, abits */
  488. 0, 0, 0, 0,
  489. /* Masks and shifts */
  490. 0, 0, 0, 0, 0, 0, 0, 0
  491. },
  492. //-----------------------------------------------------------------------
  493. {"PF_DXT2",
  494. /* Bytes per element */
  495. 0,
  496. /* Flags */
  497. PFF_COMPRESSED | PFF_HASALPHA,
  498. /* Component type and count */
  499. PCT_BYTE, 4,
  500. /* rbits, gbits, bbits, abits */
  501. 0, 0, 0, 0,
  502. /* Masks and shifts */
  503. 0, 0, 0, 0, 0, 0, 0, 0
  504. },
  505. //-----------------------------------------------------------------------
  506. {"PF_DXT3",
  507. /* Bytes per element */
  508. 0,
  509. /* Flags */
  510. PFF_COMPRESSED | PFF_HASALPHA,
  511. /* Component type and count */
  512. PCT_BYTE, 4,
  513. /* rbits, gbits, bbits, abits */
  514. 0, 0, 0, 0,
  515. /* Masks and shifts */
  516. 0, 0, 0, 0, 0, 0, 0, 0
  517. },
  518. //-----------------------------------------------------------------------
  519. {"PF_DXT4",
  520. /* Bytes per element */
  521. 0,
  522. /* Flags */
  523. PFF_COMPRESSED | PFF_HASALPHA,
  524. /* Component type and count */
  525. PCT_BYTE, 4,
  526. /* rbits, gbits, bbits, abits */
  527. 0, 0, 0, 0,
  528. /* Masks and shifts */
  529. 0, 0, 0, 0, 0, 0, 0, 0
  530. },
  531. //-----------------------------------------------------------------------
  532. {"PF_DXT5",
  533. /* Bytes per element */
  534. 0,
  535. /* Flags */
  536. PFF_COMPRESSED | PFF_HASALPHA,
  537. /* Component type and count */
  538. PCT_BYTE, 4,
  539. /* rbits, gbits, bbits, abits */
  540. 0, 0, 0, 0,
  541. /* Masks and shifts */
  542. 0, 0, 0, 0, 0, 0, 0, 0
  543. },
  544. //-----------------------------------------------------------------------
  545. {"PF_FLOAT16_R",
  546. /* Bytes per element */
  547. 2,
  548. /* Flags */
  549. PFF_FLOAT,
  550. /* Component type and count */
  551. PCT_FLOAT16, 1,
  552. /* rbits, gbits, bbits, abits */
  553. 16, 0, 0, 0,
  554. /* Masks and shifts */
  555. 0, 0, 0, 0, 0, 0, 0, 0
  556. },
  557. //-----------------------------------------------------------------------
  558. {"PF_FLOAT16_RG",
  559. /* Bytes per element */
  560. 4,
  561. /* Flags */
  562. PFF_FLOAT,
  563. /* Component type and count */
  564. PCT_FLOAT16, 2,
  565. /* rbits, gbits, bbits, abits */
  566. 16, 16, 0, 0,
  567. /* Masks and shifts */
  568. 0, 0, 0, 0, 0, 0, 0, 0
  569. },
  570. //-----------------------------------------------------------------------
  571. {"PF_FLOAT16_RGB",
  572. /* Bytes per element */
  573. 6,
  574. /* Flags */
  575. PFF_FLOAT,
  576. /* Component type and count */
  577. PCT_FLOAT16, 3,
  578. /* rbits, gbits, bbits, abits */
  579. 16, 16, 16, 0,
  580. /* Masks and shifts */
  581. 0, 0, 0, 0, 0, 0, 0, 0
  582. },
  583. //-----------------------------------------------------------------------
  584. {"PF_FLOAT16_RGBA",
  585. /* Bytes per element */
  586. 8,
  587. /* Flags */
  588. PFF_FLOAT | PFF_HASALPHA,
  589. /* Component type and count */
  590. PCT_FLOAT16, 4,
  591. /* rbits, gbits, bbits, abits */
  592. 16, 16, 16, 16,
  593. /* Masks and shifts */
  594. 0, 0, 0, 0, 0, 0, 0, 0
  595. },
  596. //-----------------------------------------------------------------------
  597. {"PF_FLOAT32_R",
  598. /* Bytes per element */
  599. 4,
  600. /* Flags */
  601. PFF_FLOAT,
  602. /* Component type and count */
  603. PCT_FLOAT32, 1,
  604. /* rbits, gbits, bbits, abits */
  605. 32, 0, 0, 0,
  606. /* Masks and shifts */
  607. 0, 0, 0, 0, 0, 0, 0, 0
  608. },
  609. //-----------------------------------------------------------------------
  610. {"PF_FLOAT32_RG",
  611. /* Bytes per element */
  612. 8,
  613. /* Flags */
  614. PFF_FLOAT,
  615. /* Component type and count */
  616. PCT_FLOAT32, 2,
  617. /* rbits, gbits, bbits, abits */
  618. 32, 32, 0, 0,
  619. /* Masks and shifts */
  620. 0, 0, 0, 0, 0, 0, 0, 0
  621. },
  622. //-----------------------------------------------------------------------
  623. {"PF_FLOAT32_RGB",
  624. /* Bytes per element */
  625. 12,
  626. /* Flags */
  627. PFF_FLOAT,
  628. /* Component type and count */
  629. PCT_FLOAT32, 3,
  630. /* rbits, gbits, bbits, abits */
  631. 32, 32, 32, 0,
  632. /* Masks and shifts */
  633. 0, 0, 0, 0, 0, 0, 0, 0
  634. },
  635. //-----------------------------------------------------------------------
  636. {"PF_FLOAT32_RGBA",
  637. /* Bytes per element */
  638. 16,
  639. /* Flags */
  640. PFF_FLOAT | PFF_HASALPHA,
  641. /* Component type and count */
  642. PCT_FLOAT32, 4,
  643. /* rbits, gbits, bbits, abits */
  644. 32, 32, 32, 32,
  645. /* Masks and shifts */
  646. 0, 0, 0, 0, 0, 0, 0, 0
  647. },
  648. //-----------------------------------------------------------------------
  649. {"PF_D32_S8X24",
  650. /* Bytes per element */
  651. 4,
  652. /* Flags */
  653. PFF_DEPTH | PFF_FLOAT,
  654. /* Component type and count */
  655. PCT_FLOAT32, 1,
  656. /* rbits, gbits, bbits, abits */
  657. 0, 0, 0, 0,
  658. /* Masks and shifts */
  659. 0, 0, 0, 0, 0, 0, 0, 0
  660. },
  661. //-----------------------------------------------------------------------
  662. {"PF_D24_S8",
  663. /* Bytes per element */
  664. 8,
  665. /* Flags */
  666. PFF_DEPTH | PFF_FLOAT,
  667. /* Component type and count */
  668. PCT_FLOAT32, 2,
  669. /* rbits, gbits, bbits, abits */
  670. 0, 0, 0, 0,
  671. /* Masks and shifts */
  672. 0, 0, 0, 0, 0, 0, 0, 0
  673. },
  674. //-----------------------------------------------------------------------
  675. {"PF_D32",
  676. /* Bytes per element */
  677. 4,
  678. /* Flags */
  679. PFF_DEPTH | PFF_FLOAT,
  680. /* Component type and count */
  681. PCT_FLOAT32, 1,
  682. /* rbits, gbits, bbits, abits */
  683. 0, 0, 0, 0,
  684. /* Masks and shifts */
  685. 0, 0, 0, 0, 0, 0, 0, 0
  686. },
  687. //-----------------------------------------------------------------------
  688. {"PF_D16",
  689. /* Bytes per element */
  690. 2,
  691. /* Flags */
  692. PFF_DEPTH | PFF_FLOAT,
  693. /* Component type and count */
  694. PCT_FLOAT16, 1,
  695. /* rbits, gbits, bbits, abits */
  696. 0, 0, 0, 0,
  697. /* Masks and shifts */
  698. 0, 0, 0, 0, 0, 0, 0, 0
  699. },
  700. };
  701. static inline const PixelFormatDescription &getDescriptionFor(const PixelFormat fmt)
  702. {
  703. const int ord = (int)fmt;
  704. assert(ord>=0 && ord<PF_COUNT);
  705. return _pixelFormats[ord];
  706. }
  707. UINT32 PixelUtil::getNumElemBytes(PixelFormat format)
  708. {
  709. return getDescriptionFor(format).elemBytes;
  710. }
  711. UINT32 PixelUtil::getMemorySize(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  712. {
  713. if(isCompressed(format))
  714. {
  715. switch(format)
  716. {
  717. // DXT formats work by dividing the image into 4x4 blocks, then encoding each
  718. // 4x4 block with a certain number of bytes.
  719. case PF_DXT1:
  720. return ((width+3)/4)*((height+3)/4)*8 * depth;
  721. case PF_DXT2:
  722. case PF_DXT3:
  723. case PF_DXT4:
  724. case PF_DXT5:
  725. return ((width+3)/4)*((height+3)/4)*16 * depth;
  726. default:
  727. BS_EXCEPT(InvalidParametersException, "Invalid compressed pixel format");
  728. }
  729. }
  730. else
  731. {
  732. return width*height*depth*getNumElemBytes(format);
  733. }
  734. }
  735. UINT32 PixelUtil::getNumElemBits(PixelFormat format)
  736. {
  737. return getDescriptionFor(format).elemBytes * 8;
  738. }
  739. UINT32 PixelUtil::getFlags(PixelFormat format)
  740. {
  741. return getDescriptionFor(format).flags;
  742. }
  743. bool PixelUtil::hasAlpha(PixelFormat format)
  744. {
  745. return (PixelUtil::getFlags(format) & PFF_HASALPHA) > 0;
  746. }
  747. bool PixelUtil::isFloatingPoint(PixelFormat format)
  748. {
  749. return (PixelUtil::getFlags(format) & PFF_FLOAT) > 0;
  750. }
  751. bool PixelUtil::isCompressed(PixelFormat format)
  752. {
  753. return (PixelUtil::getFlags(format) & PFF_COMPRESSED) > 0;
  754. }
  755. bool PixelUtil::isDepth(PixelFormat format)
  756. {
  757. return (PixelUtil::getFlags(format) & PFF_DEPTH) > 0;
  758. }
  759. bool PixelUtil::isNativeEndian(PixelFormat format)
  760. {
  761. return (PixelUtil::getFlags(format) & PFF_NATIVEENDIAN) > 0;
  762. }
  763. bool PixelUtil::isValidExtent(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  764. {
  765. if(isCompressed(format))
  766. {
  767. switch(format)
  768. {
  769. case PF_DXT1:
  770. case PF_DXT2:
  771. case PF_DXT3:
  772. case PF_DXT4:
  773. case PF_DXT5:
  774. return ((width & 3) == 0 && (height & 3) == 0 && depth == 1);
  775. default:
  776. return true;
  777. }
  778. }
  779. else
  780. {
  781. return true;
  782. }
  783. }
  784. void PixelUtil::getBitDepths(PixelFormat format, int rgba[4])
  785. {
  786. const PixelFormatDescription& des = getDescriptionFor(format);
  787. rgba[0] = des.rbits;
  788. rgba[1] = des.gbits;
  789. rgba[2] = des.bbits;
  790. rgba[3] = des.abits;
  791. }
  792. void PixelUtil::getBitMasks(PixelFormat format, UINT32 rgba[4])
  793. {
  794. const PixelFormatDescription& des = getDescriptionFor(format);
  795. rgba[0] = des.rmask;
  796. rgba[1] = des.gmask;
  797. rgba[2] = des.bmask;
  798. rgba[3] = des.amask;
  799. }
  800. void PixelUtil::getBitShifts(PixelFormat format, UINT8 rgba[4])
  801. {
  802. const PixelFormatDescription& des = getDescriptionFor(format);
  803. rgba[0] = des.rshift;
  804. rgba[1] = des.gshift;
  805. rgba[2] = des.bshift;
  806. rgba[3] = des.ashift;
  807. }
  808. String PixelUtil::getFormatName(PixelFormat srcformat)
  809. {
  810. return getDescriptionFor(srcformat).name;
  811. }
  812. bool PixelUtil::isAccessible(PixelFormat srcformat)
  813. {
  814. if (srcformat == PF_UNKNOWN)
  815. return false;
  816. UINT32 flags = getFlags(srcformat);
  817. return !((flags & PFF_COMPRESSED) || (flags & PFF_DEPTH));
  818. }
  819. PixelComponentType PixelUtil::getElementType(PixelFormat format)
  820. {
  821. const PixelFormatDescription& des = getDescriptionFor(format);
  822. return des.componentType;
  823. }
  824. UINT32 PixelUtil::getNumElements(PixelFormat format)
  825. {
  826. const PixelFormatDescription& des = getDescriptionFor(format);
  827. return des.componentCount;
  828. }
  829. UINT32 PixelUtil::getMaxMipmaps(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  830. {
  831. UINT32 count = 0;
  832. if((width > 0) && (height > 0))
  833. {
  834. do {
  835. if(width>1) width = width/2;
  836. if(height>1) height = height/2;
  837. if(depth>1) depth = depth/2;
  838. count ++;
  839. } while(!(width == 1 && height == 1 && depth == 1));
  840. }
  841. return count;
  842. }
  843. void PixelUtil::packColor(const Color& color, PixelFormat format, void* dest)
  844. {
  845. packColor(color.r, color.g, color.b, color.a, format, dest);
  846. }
  847. void PixelUtil::packColor(UINT8 r, UINT8 g, UINT8 b, UINT8 a, PixelFormat format, void* dest)
  848. {
  849. const PixelFormatDescription &des = getDescriptionFor(format);
  850. if(des.flags & PFF_NATIVEENDIAN)
  851. {
  852. // Shortcut for integer formats packing
  853. UINT32 value = ((Bitwise::fixedToFixed(r, 8, des.rbits)<<des.rshift) & des.rmask) |
  854. ((Bitwise::fixedToFixed(g, 8, des.gbits)<<des.gshift) & des.gmask) |
  855. ((Bitwise::fixedToFixed(b, 8, des.bbits)<<des.bshift) & des.bmask) |
  856. ((Bitwise::fixedToFixed(a, 8, des.abits)<<des.ashift) & des.amask);
  857. // And write to memory
  858. Bitwise::intWrite(dest, des.elemBytes, value);
  859. }
  860. else
  861. {
  862. // Convert to float
  863. packColor((float)r/255.0f,(float)g/255.0f,(float)b/255.0f,(float)a/255.0f, format, dest);
  864. }
  865. }
  866. void PixelUtil::packColor(float r, float g, float b, float a, const PixelFormat format, void* dest)
  867. {
  868. const PixelFormatDescription& des = getDescriptionFor(format);
  869. if(des.flags & PFF_NATIVEENDIAN)
  870. {
  871. // Do the packing
  872. const unsigned int value = ((Bitwise::floatToFixed(r, des.rbits)<<des.rshift) & des.rmask) |
  873. ((Bitwise::floatToFixed(g, des.gbits)<<des.gshift) & des.gmask) |
  874. ((Bitwise::floatToFixed(b, des.bbits)<<des.bshift) & des.bmask) |
  875. ((Bitwise::floatToFixed(a, des.abits)<<des.ashift) & des.amask);
  876. // And write to memory
  877. Bitwise::intWrite(dest, des.elemBytes, value);
  878. }
  879. else
  880. {
  881. switch(format)
  882. {
  883. case PF_FLOAT32_R:
  884. ((float*)dest)[0] = r;
  885. break;
  886. case PF_FLOAT32_RG:
  887. ((float*)dest)[0] = r;
  888. ((float*)dest)[1] = g;
  889. break;
  890. case PF_FLOAT32_RGB:
  891. ((float*)dest)[0] = r;
  892. ((float*)dest)[1] = g;
  893. ((float*)dest)[2] = b;
  894. break;
  895. case PF_FLOAT32_RGBA:
  896. ((float*)dest)[0] = r;
  897. ((float*)dest)[1] = g;
  898. ((float*)dest)[2] = b;
  899. ((float*)dest)[3] = a;
  900. break;
  901. case PF_FLOAT16_R:
  902. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  903. break;
  904. case PF_FLOAT16_RG:
  905. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  906. ((UINT16*)dest)[1] = Bitwise::floatToHalf(g);
  907. break;
  908. case PF_FLOAT16_RGB:
  909. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  910. ((UINT16*)dest)[1] = Bitwise::floatToHalf(g);
  911. ((UINT16*)dest)[2] = Bitwise::floatToHalf(b);
  912. break;
  913. case PF_FLOAT16_RGBA:
  914. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  915. ((UINT16*)dest)[1] = Bitwise::floatToHalf(g);
  916. ((UINT16*)dest)[2] = Bitwise::floatToHalf(b);
  917. ((UINT16*)dest)[3] = Bitwise::floatToHalf(a);
  918. break;
  919. case PF_R8G8:
  920. ((UINT8*)dest)[0] = (UINT8)Bitwise::floatToFixed(r, 8);
  921. ((UINT8*)dest)[1] = (UINT8)Bitwise::floatToFixed(g, 8);
  922. break;
  923. case PF_R8:
  924. ((UINT8*)dest)[0] = (UINT8)Bitwise::floatToFixed(r, 8);
  925. break;
  926. default:
  927. BS_EXCEPT(NotImplementedException, "Pack to " + getFormatName(format) + " not implemented");
  928. break;
  929. }
  930. }
  931. }
  932. void PixelUtil::unpackColor(Color* color, PixelFormat format, const void* src)
  933. {
  934. unpackColor(&color->r, &color->g, &color->b, &color->a, format, src);
  935. }
  936. void PixelUtil::unpackColor(UINT8* r, UINT8* g, UINT8* b, UINT8* a, PixelFormat format, const void* src)
  937. {
  938. const PixelFormatDescription &des = getDescriptionFor(format);
  939. if(des.flags & PFF_NATIVEENDIAN)
  940. {
  941. // Shortcut for integer formats unpacking
  942. const UINT32 value = Bitwise::intRead(src, des.elemBytes);
  943. *r = (UINT8)Bitwise::fixedToFixed((value & des.rmask)>>des.rshift, des.rbits, 8);
  944. *g = (UINT8)Bitwise::fixedToFixed((value & des.gmask)>>des.gshift, des.gbits, 8);
  945. *b = (UINT8)Bitwise::fixedToFixed((value & des.bmask)>>des.bshift, des.bbits, 8);
  946. if(des.flags & PFF_HASALPHA)
  947. {
  948. *a = (UINT8)Bitwise::fixedToFixed((value & des.amask)>>des.ashift, des.abits, 8);
  949. }
  950. else
  951. {
  952. *a = 255; // No alpha, default a component to full
  953. }
  954. }
  955. else
  956. {
  957. // Do the operation with the more generic floating point
  958. float rr, gg, bb, aa;
  959. unpackColor(&rr,&gg,&bb,&aa, format, src);
  960. *r = (UINT8)Bitwise::floatToFixed(rr, 8);
  961. *g = (UINT8)Bitwise::floatToFixed(gg, 8);
  962. *b = (UINT8)Bitwise::floatToFixed(bb, 8);
  963. *a = (UINT8)Bitwise::floatToFixed(aa, 8);
  964. }
  965. }
  966. void PixelUtil::unpackColor(float* r, float* g, float* b, float* a, PixelFormat format, const void* src)
  967. {
  968. const PixelFormatDescription &des = getDescriptionFor(format);
  969. if(des.flags & PFF_NATIVEENDIAN)
  970. {
  971. // Shortcut for integer formats unpacking
  972. const unsigned int value = Bitwise::intRead(src, des.elemBytes);
  973. *r = Bitwise::fixedToFloat((value & des.rmask)>>des.rshift, des.rbits);
  974. *g = Bitwise::fixedToFloat((value & des.gmask)>>des.gshift, des.gbits);
  975. *b = Bitwise::fixedToFloat((value & des.bmask)>>des.bshift, des.bbits);
  976. if(des.flags & PFF_HASALPHA)
  977. {
  978. *a = Bitwise::fixedToFloat((value & des.amask)>>des.ashift, des.abits);
  979. }
  980. else
  981. {
  982. *a = 1.0f; // No alpha, default a component to full
  983. }
  984. }
  985. else
  986. {
  987. switch(format)
  988. {
  989. case PF_FLOAT32_R:
  990. *r = *g = *b = ((float*)src)[0];
  991. *a = 1.0f;
  992. break;
  993. case PF_FLOAT32_RG:
  994. *r = ((float*)src)[0];
  995. *g = *b = ((float*)src)[1];
  996. *a = 1.0f;
  997. break;
  998. case PF_FLOAT32_RGB:
  999. *r = ((float*)src)[0];
  1000. *g = ((float*)src)[1];
  1001. *b = ((float*)src)[2];
  1002. *a = 1.0f;
  1003. break;
  1004. case PF_FLOAT32_RGBA:
  1005. *r = ((float*)src)[0];
  1006. *g = ((float*)src)[1];
  1007. *b = ((float*)src)[2];
  1008. *a = ((float*)src)[3];
  1009. break;
  1010. case PF_FLOAT16_R:
  1011. *r = *g = *b = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1012. *a = 1.0f;
  1013. break;
  1014. case PF_FLOAT16_RG:
  1015. *r = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1016. *g = *b = Bitwise::halfToFloat(((UINT16*)src)[1]);
  1017. *a = 1.0f;
  1018. break;
  1019. case PF_FLOAT16_RGB:
  1020. *r = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1021. *g = Bitwise::halfToFloat(((UINT16*)src)[1]);
  1022. *b = Bitwise::halfToFloat(((UINT16*)src)[2]);
  1023. *a = 1.0f;
  1024. break;
  1025. case PF_FLOAT16_RGBA:
  1026. *r = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1027. *g = Bitwise::halfToFloat(((UINT16*)src)[1]);
  1028. *b = Bitwise::halfToFloat(((UINT16*)src)[2]);
  1029. *a = Bitwise::halfToFloat(((UINT16*)src)[3]);
  1030. break;
  1031. case PF_R8G8:
  1032. *r = Bitwise::fixedToFloat(((UINT8*)src)[0], 8);
  1033. *g = Bitwise::fixedToFloat(((UINT8*)src)[1], 8);
  1034. *b = 0.0f;
  1035. *a = 1.0f;
  1036. break;
  1037. case PF_R8:
  1038. *r = Bitwise::fixedToFloat(((UINT8*)src)[0], 8);
  1039. *g = 0.0f;
  1040. *b = 0.0f;
  1041. *a = 1.0f;
  1042. break;
  1043. default:
  1044. BS_EXCEPT(NotImplementedException, "Unpack from " + getFormatName(format) + " not implemented");
  1045. break;
  1046. }
  1047. }
  1048. }
  1049. void PixelUtil::bulkPixelConversion(const PixelData &src, const PixelData &dst)
  1050. {
  1051. assert(src.getWidth() == dst.getWidth() &&
  1052. src.getHeight() == dst.getHeight() &&
  1053. src.getDepth() == dst.getDepth());
  1054. // Check for compressed formats, we don't support decompression, compression or recoding
  1055. if(PixelUtil::isCompressed(src.getFormat()) || PixelUtil::isCompressed(dst.getFormat()))
  1056. {
  1057. if(src.getFormat() == dst.getFormat())
  1058. {
  1059. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1060. return;
  1061. }
  1062. else
  1063. {
  1064. BS_EXCEPT(NotImplementedException, "This method can not be used to compress or decompress images");
  1065. }
  1066. }
  1067. // The easy case
  1068. if(src.getFormat() == dst.getFormat())
  1069. {
  1070. // Everything consecutive?
  1071. if(src.isConsecutive() && dst.isConsecutive())
  1072. {
  1073. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1074. return;
  1075. }
  1076. const UINT32 srcPixelSize = PixelUtil::getNumElemBytes(src.getFormat());
  1077. const UINT32 dstPixelSize = PixelUtil::getNumElemBytes(dst.getFormat());
  1078. UINT8 *srcptr = static_cast<UINT8*>(src.getData())
  1079. + (src.getLeft() + src.getTop() * src.getRowPitch() + src.getFront() * src.getSlicePitch()) * srcPixelSize;
  1080. UINT8 *dstptr = static_cast<UINT8*>(dst.getData())
  1081. + (dst.getLeft() + dst.getTop() * dst.getRowPitch() + dst.getFront() * dst.getSlicePitch()) * dstPixelSize;
  1082. // Calculate pitches+skips in bytes
  1083. const UINT32 srcRowPitchBytes = src.getRowPitch()*srcPixelSize;
  1084. const UINT32 srcSliceSkipBytes = src.getSliceSkip()*srcPixelSize;
  1085. const UINT32 dstRowPitchBytes = dst.getRowPitch()*dstPixelSize;
  1086. const UINT32 dstSliceSkipBytes = dst.getSliceSkip()*dstPixelSize;
  1087. // Otherwise, copy per row
  1088. const UINT32 rowSize = src.getWidth()*srcPixelSize;
  1089. for (UINT32 z = src.getFront(); z < src.getBack(); z++)
  1090. {
  1091. for(UINT32 y = src.getTop(); y < src.getBottom(); y++)
  1092. {
  1093. memcpy(dstptr, srcptr, rowSize);
  1094. srcptr += srcRowPitchBytes;
  1095. dstptr += dstRowPitchBytes;
  1096. }
  1097. srcptr += srcSliceSkipBytes;
  1098. dstptr += dstSliceSkipBytes;
  1099. }
  1100. return;
  1101. }
  1102. // Converting to PF_X8R8G8B8 is exactly the same as converting to
  1103. // PF_A8R8G8B8. (same with PF_X8B8G8R8 and PF_A8B8G8R8)
  1104. if(dst.getFormat() == PF_X8R8G8B8 || dst.getFormat() == PF_X8B8G8R8)
  1105. {
  1106. // Do the same conversion, with PF_A8R8G8B8, which has a lot of
  1107. // optimized conversions
  1108. PixelFormat tempFormat = dst.getFormat() == PF_X8R8G8B8?PF_A8R8G8B8:PF_A8B8G8R8;
  1109. PixelData tempdst(dst.getWidth(), dst.getHeight(), dst.getDepth(), tempFormat);
  1110. bulkPixelConversion(src, tempdst);
  1111. return;
  1112. }
  1113. // Converting from PF_X8R8G8B8 is exactly the same as converting from
  1114. // PF_A8R8G8B8, given that the destination format does not have alpha.
  1115. if((src.getFormat() == PF_X8R8G8B8 || src.getFormat() == PF_X8B8G8R8) && !hasAlpha(dst.getFormat()))
  1116. {
  1117. // Do the same conversion, with PF_A8R8G8B8, which has a lot of
  1118. // optimized conversions
  1119. PixelFormat tempFormat = src.getFormat()==PF_X8R8G8B8?PF_A8R8G8B8:PF_A8B8G8R8;
  1120. PixelData tempsrc(src.getWidth(), src.getHeight(), src.getDepth(), tempFormat);
  1121. tempsrc.setExternalBuffer(src.getData());
  1122. bulkPixelConversion(tempsrc, dst);
  1123. return;
  1124. }
  1125. const UINT32 srcPixelSize = PixelUtil::getNumElemBytes(src.getFormat());
  1126. const UINT32 dstPixelSize = PixelUtil::getNumElemBytes(dst.getFormat());
  1127. UINT8 *srcptr = static_cast<UINT8*>(src.getData())
  1128. + (src.getLeft() + src.getTop() * src.getRowPitch() + src.getFront() * src.getSlicePitch()) * srcPixelSize;
  1129. UINT8 *dstptr = static_cast<UINT8*>(dst.getData())
  1130. + (dst.getLeft() + dst.getTop() * dst.getRowPitch() + dst.getFront() * dst.getSlicePitch()) * dstPixelSize;
  1131. // Calculate pitches+skips in bytes
  1132. const UINT32 srcRowSkipBytes = src.getRowSkip()*srcPixelSize;
  1133. const UINT32 srcSliceSkipBytes = src.getSliceSkip()*srcPixelSize;
  1134. const UINT32 dstRowSkipBytes = dst.getRowSkip()*dstPixelSize;
  1135. const UINT32 dstSliceSkipBytes = dst.getSliceSkip()*dstPixelSize;
  1136. // The brute force fallback
  1137. float r,g,b,a;
  1138. for (UINT32 z = src.getFront(); z<src.getBack(); z++)
  1139. {
  1140. for (UINT32 y = src.getTop(); y < src.getBottom(); y++)
  1141. {
  1142. for (UINT32 x = src.getLeft(); x<src.getRight(); x++)
  1143. {
  1144. unpackColor(&r, &g, &b, &a, src.getFormat(), srcptr);
  1145. packColor(r, g, b, a, dst.getFormat(), dstptr);
  1146. srcptr += srcPixelSize;
  1147. dstptr += dstPixelSize;
  1148. }
  1149. srcptr += srcRowSkipBytes;
  1150. dstptr += dstRowSkipBytes;
  1151. }
  1152. srcptr += srcSliceSkipBytes;
  1153. dstptr += dstSliceSkipBytes;
  1154. }
  1155. }
  1156. void PixelUtil::scale(const PixelData& src, const PixelData& scaled, Filter filter)
  1157. {
  1158. assert(PixelUtil::isAccessible(src.getFormat()));
  1159. assert(PixelUtil::isAccessible(scaled.getFormat()));
  1160. PixelData temp;
  1161. switch (filter)
  1162. {
  1163. default:
  1164. case FILTER_NEAREST:
  1165. if(src.getFormat() == scaled.getFormat())
  1166. {
  1167. // No intermediate buffer needed
  1168. temp = scaled;
  1169. }
  1170. else
  1171. {
  1172. // Allocate temporary buffer of destination size in source format
  1173. temp = PixelData(scaled.getWidth(), scaled.getHeight(), scaled.getDepth(), src.getFormat());
  1174. temp.allocateInternalBuffer();
  1175. }
  1176. // No conversion
  1177. switch (PixelUtil::getNumElemBytes(src.getFormat()))
  1178. {
  1179. case 1: NearestResampler<1>::scale(src, temp); break;
  1180. case 2: NearestResampler<2>::scale(src, temp); break;
  1181. case 3: NearestResampler<3>::scale(src, temp); break;
  1182. case 4: NearestResampler<4>::scale(src, temp); break;
  1183. case 6: NearestResampler<6>::scale(src, temp); break;
  1184. case 8: NearestResampler<8>::scale(src, temp); break;
  1185. case 12: NearestResampler<12>::scale(src, temp); break;
  1186. case 16: NearestResampler<16>::scale(src, temp); break;
  1187. default:
  1188. // Never reached
  1189. assert(false);
  1190. }
  1191. if(temp.getData() != scaled.getData())
  1192. {
  1193. // Blit temp buffer
  1194. PixelUtil::bulkPixelConversion(temp, scaled);
  1195. temp.freeInternalBuffer();
  1196. }
  1197. break;
  1198. case FILTER_LINEAR:
  1199. switch (src.getFormat())
  1200. {
  1201. case PF_R8G8:
  1202. case PF_R8G8B8: case PF_B8G8R8:
  1203. case PF_R8G8B8A8: case PF_B8G8R8A8:
  1204. case PF_A8B8G8R8: case PF_A8R8G8B8:
  1205. case PF_X8B8G8R8: case PF_X8R8G8B8:
  1206. if(src.getFormat() == scaled.getFormat())
  1207. {
  1208. // No intermediate buffer needed
  1209. temp = scaled;
  1210. }
  1211. else
  1212. {
  1213. // Allocate temp buffer of destination size in source format
  1214. temp = PixelData(scaled.getWidth(), scaled.getHeight(), scaled.getDepth(), src.getFormat());
  1215. temp.allocateInternalBuffer();
  1216. }
  1217. // No conversion
  1218. switch (PixelUtil::getNumElemBytes(src.getFormat()))
  1219. {
  1220. case 1: LinearResampler_Byte<1>::scale(src, temp); break;
  1221. case 2: LinearResampler_Byte<2>::scale(src, temp); break;
  1222. case 3: LinearResampler_Byte<3>::scale(src, temp); break;
  1223. case 4: LinearResampler_Byte<4>::scale(src, temp); break;
  1224. default:
  1225. // Never reached
  1226. assert(false);
  1227. }
  1228. if(temp.getData() != scaled.getData())
  1229. {
  1230. // Blit temp buffer
  1231. PixelUtil::bulkPixelConversion(temp, scaled);
  1232. temp.freeInternalBuffer();
  1233. }
  1234. break;
  1235. case PF_FLOAT32_RGB:
  1236. case PF_FLOAT32_RGBA:
  1237. if (scaled.getFormat() == PF_FLOAT32_RGB || scaled.getFormat() == PF_FLOAT32_RGBA)
  1238. {
  1239. // float32 to float32, avoid unpack/repack overhead
  1240. LinearResampler_Float32::scale(src, scaled);
  1241. break;
  1242. }
  1243. // Else, fall through
  1244. default:
  1245. // Fallback case, slow but works
  1246. LinearResampler::scale(src, scaled);
  1247. }
  1248. break;
  1249. }
  1250. }
  1251. void PixelUtil::applyGamma(UINT8* buffer, float gamma, UINT32 size, UINT8 bpp)
  1252. {
  1253. if(gamma == 1.0f)
  1254. return;
  1255. UINT32 stride = bpp >> 3;
  1256. for(size_t i = 0, j = size / stride; i < j; i++, buffer += stride)
  1257. {
  1258. float r = (float)buffer[0];
  1259. float g = (float)buffer[1];
  1260. float b = (float)buffer[2];
  1261. r = r * gamma;
  1262. g = g * gamma;
  1263. b = b * gamma;
  1264. float scale = 1.0f;
  1265. float tmp = 0.0f;
  1266. if(r > 255.0f && (tmp=(255.0f/r)) < scale)
  1267. scale = tmp;
  1268. if(g > 255.0f && (tmp=(255.0f/g)) < scale)
  1269. scale = tmp;
  1270. if(b > 255.0f && (tmp=(255.0f/b)) < scale)
  1271. scale = tmp;
  1272. r *= scale;
  1273. g *= scale;
  1274. b *= scale;
  1275. buffer[0] = (UINT8)r;
  1276. buffer[1] = (UINT8)g;
  1277. buffer[2] = (UINT8)b;
  1278. }
  1279. }
  1280. }