bitmapUtils.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "gfx/bitmap/bitmapUtils.h"
  23. #include "platform/platform.h"
  24. void bitmapExtrude5551_c(const void *srcMip, void *mip, U32 srcHeight, U32 srcWidth)
  25. {
  26. const U16 *src = (const U16 *) srcMip;
  27. U16 *dst = (U16 *) mip;
  28. U32 stride = srcHeight != 1 ? srcWidth : 0;
  29. U32 width = srcWidth >> 1;
  30. U32 height = srcHeight >> 1;
  31. if (width == 0) width = 1;
  32. if (height == 0) height = 1;
  33. if (srcWidth != 1)
  34. {
  35. for(U32 y = 0; y < height; y++)
  36. {
  37. for(U32 x = 0; x < width; x++)
  38. {
  39. U32 a = src[0];
  40. U32 b = src[1];
  41. U32 c = src[stride];
  42. U32 d = src[stride+1];
  43. #if defined(TORQUE_BIG_ENDIAN)
  44. dst[x] = ((( (a >> 10) + (b >> 10) + (c >> 10) + (d >> 10)) >> 2) << 10) |
  45. ((( ((a >> 5) & 0x1F) + ((b >> 5) & 0x1F) + ((c >> 5) & 0x1F) + ((d >> 5) & 0x1F)) >> 2) << 5) |
  46. ((( ((a >> 0) & 0x1F) + ((b >> 0) & 0x1F) + ((c >> 0) & 0x1F) + ((d >> 0) & 0x1F)) >> 2) << 0);
  47. #else
  48. dst[x] = ((( (a >> 11) + (b >> 11) + (c >> 11) + (d >> 11)) >> 2) << 11) |
  49. ((( ((a >> 6) & 0x1F) + ((b >> 6) & 0x1F) + ((c >> 6) & 0x1F) + ((d >> 6) & 0x1F)) >> 2) << 6) |
  50. ((( ((a >> 1) & 0x1F) + ((b >> 1) & 0x1F) + ((c >> 1) & 0x1F) + ((d >> 1) & 0x1F)) >> 2) << 1);
  51. #endif
  52. src += 2;
  53. }
  54. src += stride;
  55. dst += width;
  56. }
  57. }
  58. else
  59. {
  60. for(U32 y = 0; y < height; y++)
  61. {
  62. U32 a = src[0];
  63. U32 c = src[stride];
  64. #if defined(TORQUE_OS_MAC)
  65. dst[y] = ((( (a >> 10) + (c >> 10)) >> 1) << 10) |
  66. ((( ((a >> 5) & 0x1F) + ((c >> 5) & 0x1f)) >> 1) << 5) |
  67. ((( ((a >> 0) & 0x1F) + ((c >> 0) & 0x1f)) >> 1) << 0);
  68. #else
  69. dst[y] = ((( (a >> 11) + (c >> 11)) >> 1) << 11) |
  70. ((( ((a >> 6) & 0x1f) + ((c >> 6) & 0x1f)) >> 1) << 6) |
  71. ((( ((a >> 1) & 0x1F) + ((c >> 1) & 0x1f)) >> 1) << 1);
  72. #endif
  73. src += 1 + stride;
  74. }
  75. }
  76. }
  77. //--------------------------------------------------------------------------
  78. void bitmapExtrudeRGB_c(const void *srcMip, void *mip, U32 srcHeight, U32 srcWidth)
  79. {
  80. const U8 *src = (const U8 *) srcMip;
  81. U8 *dst = (U8 *) mip;
  82. U32 stride = srcHeight != 1 ? (srcWidth) * 3 : 0;
  83. U32 width = srcWidth >> 1;
  84. U32 height = srcHeight >> 1;
  85. if (width == 0) width = 1;
  86. if (height == 0) height = 1;
  87. if (srcWidth != 1)
  88. {
  89. for(U32 y = 0; y < height; y++)
  90. {
  91. for(U32 x = 0; x < width; x++)
  92. {
  93. *dst++ = (U32(*src) + U32(src[3]) + U32(src[stride]) + U32(src[stride+3]) + 2) >> 2;
  94. src++;
  95. *dst++ = (U32(*src) + U32(src[3]) + U32(src[stride]) + U32(src[stride+3]) + 2) >> 2;
  96. src++;
  97. *dst++ = (U32(*src) + U32(src[3]) + U32(src[stride]) + U32(src[stride+3]) + 2) >> 2;
  98. src += 4;
  99. }
  100. src += stride; // skip
  101. }
  102. }
  103. else
  104. {
  105. for(U32 y = 0; y < height; y++)
  106. {
  107. *dst++ = (U32(*src) + U32(src[stride]) + 1) >> 1;
  108. src++;
  109. *dst++ = (U32(*src) + U32(src[stride]) + 1) >> 1;
  110. src++;
  111. *dst++ = (U32(*src) + U32(src[stride]) + 1) >> 1;
  112. src += 4;
  113. src += stride; // skip
  114. }
  115. }
  116. }
  117. //--------------------------------------------------------------------------
  118. void bitmapExtrudeRGBA_c(const void *srcMip, void *mip, U32 srcHeight, U32 srcWidth)
  119. {
  120. const U8 *src = (const U8 *) srcMip;
  121. U8 *dst = (U8 *) mip;
  122. U32 stride = srcHeight != 1 ? (srcWidth) * 4 : 0;
  123. U32 width = srcWidth >> 1;
  124. U32 height = srcHeight >> 1;
  125. if (width == 0) width = 1;
  126. if (height == 0) height = 1;
  127. if (srcWidth != 1)
  128. {
  129. for(U32 y = 0; y < height; y++)
  130. {
  131. for(U32 x = 0; x < width; x++)
  132. {
  133. *dst++ = (U32(*src) + U32(src[4]) + U32(src[stride]) + U32(src[stride+4]) + 2) >> 2;
  134. src++;
  135. *dst++ = (U32(*src) + U32(src[4]) + U32(src[stride]) + U32(src[stride+4]) + 2) >> 2;
  136. src++;
  137. *dst++ = (U32(*src) + U32(src[4]) + U32(src[stride]) + U32(src[stride+4]) + 2) >> 2;
  138. src++;
  139. *dst++ = (U32(*src) + U32(src[4]) + U32(src[stride]) + U32(src[stride+4]) + 2) >> 2;
  140. src += 5;
  141. }
  142. src += stride; // skip
  143. }
  144. }
  145. else
  146. {
  147. for(U32 y = 0; y < height; y++)
  148. {
  149. *dst++ = (U32(*src) + U32(src[stride]) + 1) >> 1;
  150. src++;
  151. *dst++ = (U32(*src) + U32(src[stride]) + 1) >> 1;
  152. src++;
  153. *dst++ = (U32(*src) + U32(src[stride]) + 1) >> 1;
  154. src++;
  155. *dst++ = (U32(*src) + U32(src[stride]) + 1) >> 1;
  156. src += 5;
  157. src += stride; // skip
  158. }
  159. }
  160. }
  161. void bitmapExtrudeFPRGBA_c(const void *srcMip, void *mip, U32 srcHeight, U32 srcWidth)
  162. {
  163. const U16 *src = (const U16 *)srcMip;
  164. U16 *dst = (U16 *)mip;
  165. U32 stride = srcHeight != 1 ? (srcWidth) * 8 : 0;
  166. U32 width = srcWidth >> 1;
  167. U32 height = srcHeight >> 1;
  168. if (width == 0) width = 1;
  169. if (height == 0) height = 1;
  170. if (srcWidth != 1)
  171. {
  172. for (U32 y = 0; y < height; y++)
  173. {
  174. for (U32 x = 0; x < width; x++)
  175. {
  176. *dst++ = (U32(*src) + U32(src[4]) + U32(src[stride]) + U32(src[stride + 4]) + 2) >> 2;
  177. src++;
  178. *dst++ = (U32(*src) + U32(src[4]) + U32(src[stride]) + U32(src[stride + 4]) + 2) >> 2;
  179. src++;
  180. *dst++ = (U32(*src) + U32(src[4]) + U32(src[stride]) + U32(src[stride + 4]) + 2) >> 2;
  181. src++;
  182. *dst++ = (U32(*src) + U32(src[4]) + U32(src[stride]) + U32(src[stride + 4]) + 2) >> 2;
  183. src += 5;
  184. }
  185. src += stride; // skip
  186. }
  187. }
  188. else
  189. {
  190. for (U32 y = 0; y < height; y++)
  191. {
  192. *dst++ = (U32(*src) + U32(src[stride]) + 1) >> 1;
  193. src++;
  194. *dst++ = (U32(*src) + U32(src[stride]) + 1) >> 1;
  195. src++;
  196. *dst++ = (U32(*src) + U32(src[stride]) + 1) >> 1;
  197. src++;
  198. *dst++ = (U32(*src) + U32(src[stride]) + 1) >> 1;
  199. src += 5;
  200. src += stride; // skip
  201. }
  202. }
  203. }
  204. void (*bitmapExtrude5551)(const void *srcMip, void *mip, U32 height, U32 width) = bitmapExtrude5551_c;
  205. void (*bitmapExtrudeRGB)(const void *srcMip, void *mip, U32 srcHeight, U32 srcWidth) = bitmapExtrudeRGB_c;
  206. void (*bitmapExtrudeRGBA)(const void *srcMip, void *mip, U32 srcHeight, U32 srcWidth) = bitmapExtrudeRGBA_c;
  207. void (*bitmapExtrudeFPRGBA)(const void *srcMip, void *mip, U32 srcHeight, U32 srcWidth) = bitmapExtrudeFPRGBA_c;
  208. //--------------------------------------------------------------------------
  209. void bitmapConvertRGB_to_1555_c(U8 *src, U32 pixels)
  210. {
  211. U16 *dst = (U16 *)src;
  212. for(U32 j = 0; j < pixels; j++)
  213. {
  214. U32 r = src[0] >> 3;
  215. U32 g = src[1] >> 3;
  216. U32 b = src[2] >> 3;
  217. #if defined(TORQUE_OS_MAC)
  218. *dst++ = 0x8000 | (b << 10) | (g << 5) | (r << 0);
  219. #else
  220. *dst++ = b | (g << 5) | (r << 10) | 0x8000;
  221. #endif
  222. src += 3;
  223. }
  224. }
  225. void (*bitmapConvertRGB_to_1555)(U8 *src, U32 pixels) = bitmapConvertRGB_to_1555_c;
  226. //------------------------------------------------------------------------------
  227. void bitmapConvertRGB_to_5551_c(U8 *src, U32 pixels)
  228. {
  229. U16 *dst = (U16 *)src;
  230. for(U32 j = 0; j < pixels; j++)
  231. {
  232. U32 r = src[0] >> 3;
  233. U32 g = src[1] >> 3;
  234. U32 b = src[2] >> 3;
  235. #if defined(TORQUE_OS_MAC)
  236. *dst++ = (1 << 15) | (b << 10) | (g << 5) | (r << 0);
  237. #else
  238. *dst++ = (b << 1) | (g << 6) | (r << 11) | 1;
  239. #endif
  240. src += 3;
  241. }
  242. }
  243. void (*bitmapConvertRGB_to_5551)(U8 *src, U32 pixels) = bitmapConvertRGB_to_5551_c;
  244. //------------------------------------------------------------------------------
  245. void bitmapConvertRGB_to_RGBX_c( U8 **src, U32 pixels )
  246. {
  247. const U8 *oldBits = *src;
  248. U8 *newBits = new U8[pixels * 4];
  249. dMemset( newBits, 0xFF, pixels * 4 ); // This is done to set alpha values -patw
  250. // Copy the bits over to the new memory
  251. for( U32 i = 0; i < pixels; i++ )
  252. dMemcpy( &newBits[i * 4], &oldBits[i * 3], sizeof(U8) * 3 );
  253. // Now hose the old bits
  254. delete [] *src;
  255. *src = newBits;
  256. }
  257. void (*bitmapConvertRGB_to_RGBX)( U8 **src, U32 pixels ) = bitmapConvertRGB_to_RGBX_c;
  258. //------------------------------------------------------------------------------
  259. void bitmapConvertRGBX_to_RGB_c( U8 **src, U32 pixels )
  260. {
  261. const U8 *oldBits = *src;
  262. U8 *newBits = new U8[pixels * 3];
  263. // Copy the bits over to the new memory
  264. for( U32 i = 0; i < pixels; i++ )
  265. dMemcpy( &newBits[i * 3], &oldBits[i * 4], sizeof(U8) * 3 );
  266. // Now hose the old bits
  267. delete [] *src;
  268. *src = newBits;
  269. }
  270. void (*bitmapConvertRGBX_to_RGB)( U8 **src, U32 pixels ) = bitmapConvertRGBX_to_RGB_c;
  271. //------------------------------------------------------------------------------
  272. void bitmapConvertA8_to_RGBA_c( U8 **src, U32 pixels )
  273. {
  274. const U8 *oldBits = *src;
  275. U8 *newBits = new U8[pixels * 4];
  276. // Zero new bits
  277. dMemset( newBits, 0, pixels * 4 );
  278. // Copy Alpha values
  279. for( U32 i = 0; i < pixels; i++ )
  280. newBits[i * 4 + 3] = oldBits[i];
  281. // Now hose the old bits
  282. delete [] *src;
  283. *src = newBits;
  284. }
  285. void (*bitmapConvertA8_to_RGBA)( U8 **src, U32 pixels ) = bitmapConvertA8_to_RGBA_c;