gBitmap.cc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "io/stream.h"
  23. #include "io/fileStream.h"
  24. #include "graphics/gBitmap.h"
  25. #include "graphics/gPalette.h"
  26. #include "io/resource/resourceManager.h"
  27. #include "platform/platform.h"
  28. #include "memory/safeDelete.h"
  29. #include "math/mRect.h"
  30. #include "console/console.h"
  31. #ifndef _TORQUECONFIG_H_
  32. #include "torqueConfig.h"//for PNG loading setting
  33. #endif
  34. const U32 GBitmap::csFileVersion = 3;
  35. U32 GBitmap::sBitmapIdSource = 0;
  36. GBitmap::GBitmap()
  37. : internalFormat(RGB),
  38. pBits(NULL),
  39. byteSize(0),
  40. width(0),
  41. height(0),
  42. numMipLevels(0),
  43. bytesPerPixel(0),
  44. pPalette(NULL),
  45. mForce16Bit(false)
  46. {
  47. for (U32 i = 0; i < c_maxMipLevels; i++)
  48. mipLevelOffsets[i] = 0xffffffff;
  49. }
  50. GBitmap::GBitmap(const GBitmap& rCopy)
  51. {
  52. if (rCopy.pPalette)
  53. {
  54. pPalette = new GPalette;
  55. pPalette->setPaletteType(rCopy.pPalette->getPaletteType());
  56. dMemcpy(rCopy.pPalette->getColors(), pPalette->getColors(), sizeof(ColorI)*256);
  57. }
  58. else
  59. pPalette = NULL;
  60. internalFormat = rCopy.internalFormat;
  61. mForce16Bit = rCopy.mForce16Bit;
  62. byteSize = rCopy.byteSize;
  63. pBits = new U8[byteSize];
  64. dMemcpy(pBits, rCopy.pBits, byteSize);
  65. width = rCopy.width;
  66. height = rCopy.height;
  67. bytesPerPixel = rCopy.bytesPerPixel;
  68. numMipLevels = rCopy.numMipLevels;
  69. dMemcpy(mipLevelOffsets, rCopy.mipLevelOffsets, sizeof(mipLevelOffsets));
  70. }
  71. GBitmap::GBitmap(const U32 in_width,
  72. const U32 in_height,
  73. const bool in_extrudeMipLevels,
  74. const BitmapFormat in_format)
  75. : pBits(NULL),
  76. byteSize(0),
  77. pPalette(NULL),
  78. mForce16Bit(false)
  79. {
  80. for (U32 i = 0; i < c_maxMipLevels; i++)
  81. mipLevelOffsets[i] = 0xffffffff;
  82. allocateBitmap(in_width, in_height, in_extrudeMipLevels, in_format);
  83. }
  84. //--------------------------------------------------------------------------
  85. GBitmap::~GBitmap()
  86. {
  87. deleteImage();
  88. }
  89. //--------------------------------------------------------------------------
  90. void GBitmap::deleteImage()
  91. {
  92. delete [] pBits;
  93. pBits = NULL;
  94. byteSize = 0;
  95. width = 0;
  96. height = 0;
  97. numMipLevels = 0;
  98. SAFE_DELETE(pPalette);
  99. }
  100. //--------------------------------------------------------------------------
  101. void GBitmap::setPalette(GPalette* in_pPalette)
  102. {
  103. SAFE_DELETE(pPalette);
  104. pPalette = in_pPalette;
  105. }
  106. void GBitmap::copyRect(const GBitmap *src, const RectI &srcRect, const Point2I &dstPt)
  107. {
  108. if(src->getFormat() != getFormat())
  109. return;
  110. if(srcRect.extent.x + srcRect.point.x > (S32)src->getWidth() || srcRect.extent.y + srcRect.point.y > (S32)src->getHeight())
  111. return;
  112. if(srcRect.extent.x + dstPt.x > (S32)getWidth() || srcRect.extent.y + dstPt.y > (S32)getHeight())
  113. return;
  114. for(U32 i = 0; i < (U32)srcRect.extent.y; i++)
  115. {
  116. dMemcpy(getAddress(dstPt.x, dstPt.y + i),
  117. src->getAddress(srcRect.point.x, srcRect.point.y + i),
  118. bytesPerPixel * srcRect.extent.x);
  119. }
  120. }
  121. //--------------------------------------------------------------------------
  122. void GBitmap::allocateBitmap(const U32 in_width, const U32 in_height, const bool in_extrudeMipLevels, const BitmapFormat in_format)
  123. {
  124. //-------------------------------------- Some debug checks...
  125. U32 svByteSize = byteSize;
  126. U8 *svBits = pBits;
  127. AssertFatal(in_width != 0 && in_height != 0, "GBitmap::allocateBitmap: width or height is 0");
  128. if (in_extrudeMipLevels == true) {
  129. //AssertFatal(in_width <= 256 && in_height <= 256, "GBitmap::allocateBitmap: width or height is too large");
  130. AssertFatal(isPow2(in_width) == true && isPow2(in_height) == true, "GBitmap::GBitmap: in order to extrude miplevels, bitmap w/h must be pow2");
  131. }
  132. internalFormat = in_format;
  133. width = in_width;
  134. height = in_height;
  135. bytesPerPixel = 1;
  136. switch (internalFormat) {
  137. case Alpha:
  138. case Palettized:
  139. case Luminance:
  140. case Intensity: bytesPerPixel = 1;
  141. break;
  142. case RGB: bytesPerPixel = 3;
  143. break;
  144. case RGBA: bytesPerPixel = 4;
  145. break;
  146. case RGB565:
  147. case RGB5551: bytesPerPixel = 2;
  148. break;
  149. #ifdef TORQUE_OS_IOS
  150. case PVR2:
  151. case PVR2A:
  152. case PVR4:
  153. case PVR4A:
  154. // compressed textures can't be allocated!
  155. return;
  156. #endif //-Mat
  157. default:
  158. AssertFatal(false, "GBitmap::GBitmap: misunderstood format specifier");
  159. break;
  160. }
  161. // Set up the mip levels, if necessary...
  162. numMipLevels = 1;
  163. U32 allocPixels = in_width * in_height * bytesPerPixel;
  164. mipLevelOffsets[0] = 0;
  165. if (in_extrudeMipLevels == true)
  166. {
  167. U32 currWidth = in_width;
  168. U32 currHeight = in_height;
  169. do
  170. {
  171. mipLevelOffsets[numMipLevels] = mipLevelOffsets[numMipLevels - 1] +
  172. (currWidth * currHeight * bytesPerPixel);
  173. currWidth >>= 1;
  174. currHeight >>= 1;
  175. if (currWidth == 0) currWidth = 1;
  176. if (currHeight == 0) currHeight = 1;
  177. numMipLevels++;
  178. allocPixels += currWidth * currHeight * bytesPerPixel;
  179. } while (currWidth != 1 || currHeight != 1);
  180. }
  181. AssertFatal(numMipLevels <= c_maxMipLevels, "GBitmap::allocateBitmap: too many miplevels");
  182. // Set up the memory...
  183. byteSize = allocPixels;
  184. pBits = new U8[byteSize];
  185. dMemset(pBits, 0xFF, byteSize);
  186. if(svBits != NULL)
  187. {
  188. dMemcpy(pBits, svBits, getMin(byteSize, svByteSize));
  189. delete[] svBits;
  190. }
  191. }
  192. //--------------------------------------------------------------------------
  193. void bitmapExtrude5551_c(const void *srcMip, void *mip, U32 srcHeight, U32 srcWidth)
  194. {
  195. const U16 *src = (const U16 *) srcMip;
  196. U16 *dst = (U16 *) mip;
  197. U32 stride = srcHeight != 1 ? srcWidth : 0;
  198. U32 width = srcWidth >> 1;
  199. U32 height = srcHeight >> 1;
  200. if (width == 0) width = 1;
  201. if (height == 0) height = 1;
  202. if (srcWidth != 1)
  203. {
  204. for(U32 y = 0; y < height; y++)
  205. {
  206. for(U32 x = 0; x < width; x++)
  207. {
  208. U32 a = src[0];
  209. U32 b = src[1];
  210. U32 c = src[stride];
  211. U32 d = src[stride+1];
  212. #if defined(TORQUE_BIG_ENDIAN)
  213. dst[x] = ((( (a >> 10) + (b >> 10) + (c >> 10) + (d >> 10)) >> 2) << 10) |
  214. ((( ((a >> 5) & 0x1F) + ((b >> 5) & 0x1F) + ((c >> 5) & 0x1F) + ((d >> 5) & 0x1F)) >> 2) << 5) |
  215. ((( ((a >> 0) & 0x1F) + ((b >> 0) & 0x1F) + ((c >> 0) & 0x1F) + ((d >> 0) & 0x1F)) >> 2) << 0);
  216. #else
  217. dst[x] = ((( (a >> 11) + (b >> 11) + (c >> 11) + (d >> 11)) >> 2) << 11) |
  218. ((( ((a >> 6) & 0x1F) + ((b >> 6) & 0x1F) + ((c >> 6) & 0x1F) + ((d >> 6) & 0x1F)) >> 2) << 6) |
  219. ((( ((a >> 1) & 0x1F) + ((b >> 1) & 0x1F) + ((c >> 1) & 0x1F) + ((d >> 1) & 0x1F)) >> 2) << 1);
  220. #endif
  221. src += 2;
  222. }
  223. src += stride;
  224. dst += width;
  225. }
  226. }
  227. else
  228. {
  229. for(U32 y = 0; y < height; y++)
  230. {
  231. U32 a = src[0];
  232. U32 c = src[stride];
  233. #if defined(TORQUE_BIG_ENDIAN)
  234. dst[y] = ((( (a >> 10) + (c >> 10)) >> 1) << 10) |
  235. ((( ((a >> 5) & 0x1F) + ((c >> 5) & 0x1f)) >> 1) << 5) |
  236. ((( ((a >> 0) & 0x1F) + ((c >> 0) & 0x1f)) >> 1) << 0);
  237. #else
  238. dst[y] = ((( (a >> 11) + (c >> 11)) >> 1) << 11) |
  239. ((( ((a >> 6) & 0x1f) + ((c >> 6) & 0x1f)) >> 1) << 6) |
  240. ((( ((a >> 1) & 0x1F) + ((c >> 1) & 0x1f)) >> 1) << 1);
  241. #endif
  242. src += 1 + stride;
  243. }
  244. }
  245. }
  246. //--------------------------------------------------------------------------
  247. void bitmapExtrudeRGB_c(const void *srcMip, void *mip, U32 srcHeight, U32 srcWidth)
  248. {
  249. const U8 *src = (const U8 *) srcMip;
  250. U8 *dst = (U8 *) mip;
  251. U32 stride = srcHeight != 1 ? (srcWidth) * 3 : 0;
  252. U32 width = srcWidth >> 1;
  253. U32 height = srcHeight >> 1;
  254. if (width == 0) width = 1;
  255. if (height == 0) height = 1;
  256. if (srcWidth != 1)
  257. {
  258. for(U32 y = 0; y < height; y++)
  259. {
  260. for(U32 x = 0; x < width; x++)
  261. {
  262. *dst++ = (U32(*src) + U32(src[3]) + U32(src[stride]) + U32(src[stride+3]) + 2) >> 2;
  263. src++;
  264. *dst++ = (U32(*src) + U32(src[3]) + U32(src[stride]) + U32(src[stride+3]) + 2) >> 2;
  265. src++;
  266. *dst++ = (U32(*src) + U32(src[3]) + U32(src[stride]) + U32(src[stride+3]) + 2) >> 2;
  267. src += 4;
  268. }
  269. src += stride; // skip
  270. }
  271. }
  272. else
  273. {
  274. for(U32 y = 0; y < height; y++)
  275. {
  276. *dst++ = (U32(*src) + U32(src[stride]) + 1) >> 1;
  277. src++;
  278. *dst++ = (U32(*src) + U32(src[stride]) + 1) >> 1;
  279. src++;
  280. *dst++ = (U32(*src) + U32(src[stride]) + 1) >> 1;
  281. src += 4;
  282. src += stride; // skip
  283. }
  284. }
  285. }
  286. //--------------------------------------------------------------------------
  287. void bitmapExtrudePaletted_c(const void *srcMip, void *mip, U32 srcHeight, U32 srcWidth)
  288. {
  289. U32 width = srcWidth >> 1;
  290. U32 height = srcHeight >> 1;
  291. if (width == 0) width = 1;
  292. if (height == 0) height = 1;
  293. dMemset(mip, 0, width * height);
  294. }
  295. //--------------------------------------------------------------------------
  296. void bitmapExtrudeRGBA_c(const void *srcMip, void *mip, U32 srcHeight, U32 srcWidth)
  297. {
  298. const U8 *src = (const U8 *) srcMip;
  299. U8 *dst = (U8 *) mip;
  300. U32 stride = srcHeight != 1 ? (srcWidth) * 4 : 0;
  301. U32 width = srcWidth >> 1;
  302. U32 height = srcHeight >> 1;
  303. if (width == 0) width = 1;
  304. if (height == 0) height = 1;
  305. if (srcWidth != 1)
  306. {
  307. for(U32 y = 0; y < height; y++)
  308. {
  309. for(U32 x = 0; x < width; x++)
  310. {
  311. *dst++ = (U32(*src) + U32(src[4]) + U32(src[stride]) + U32(src[stride+4]) + 2) >> 2;
  312. src++;
  313. *dst++ = (U32(*src) + U32(src[4]) + U32(src[stride]) + U32(src[stride+4]) + 2) >> 2;
  314. src++;
  315. *dst++ = (U32(*src) + U32(src[4]) + U32(src[stride]) + U32(src[stride+4]) + 2) >> 2;
  316. src++;
  317. *dst++ = (U32(*src) + U32(src[4]) + U32(src[stride]) + U32(src[stride+4]) + 2) >> 2;
  318. src += 5;
  319. }
  320. src += stride; // skip
  321. }
  322. }
  323. else
  324. {
  325. for(U32 y = 0; y < height; y++)
  326. {
  327. *dst++ = (U32(*src) + U32(src[stride]) + 1) >> 1;
  328. src++;
  329. *dst++ = (U32(*src) + U32(src[stride]) + 1) >> 1;
  330. src++;
  331. *dst++ = (U32(*src) + U32(src[stride]) + 1) >> 1;
  332. src++;
  333. *dst++ = (U32(*src) + U32(src[stride]) + 1) >> 1;
  334. src += 5;
  335. src += stride; // skip
  336. }
  337. }
  338. }
  339. void (*bitmapExtrude5551)(const void *srcMip, void *mip, U32 height, U32 width) = bitmapExtrude5551_c;
  340. void (*bitmapExtrudeRGB)(const void *srcMip, void *mip, U32 srcHeight, U32 srcWidth) = bitmapExtrudeRGB_c;
  341. void (*bitmapExtrudeRGBA)(const void *srcMip, void *mip, U32 srcHeight, U32 srcWidth) = bitmapExtrudeRGBA_c;
  342. void (*bitmapExtrudePaletted)(const void *srcMip, void *mip, U32 srcHeight, U32 srcWidth) = bitmapExtrudePaletted_c;
  343. //--------------------------------------------------------------------------
  344. void GBitmap::extrudeMipLevels(bool clearBorders)
  345. {
  346. if(numMipLevels == 1)
  347. allocateBitmap(getWidth(), getHeight(), true, getFormat());
  348. switch (getFormat())
  349. {
  350. case RGB5551:
  351. {
  352. for(U32 i = 1; i < numMipLevels; i++)
  353. bitmapExtrude5551(getBits(i - 1), getWritableBits(i), getHeight(i), getWidth(i));
  354. break;
  355. }
  356. case RGB:
  357. {
  358. for(U32 i = 1; i < numMipLevels; i++)
  359. bitmapExtrudeRGB(getBits(i - 1), getWritableBits(i), getHeight(i-1), getWidth(i-1));
  360. break;
  361. }
  362. case RGBA:
  363. {
  364. for(U32 i = 1; i < numMipLevels; i++)
  365. bitmapExtrudeRGBA(getBits(i - 1), getWritableBits(i), getHeight(i-1), getWidth(i-1));
  366. break;
  367. }
  368. case Palettized:
  369. {
  370. for(U32 i = 1; i < numMipLevels; i++)
  371. bitmapExtrudePaletted(getBits(i - 1), getWritableBits(i), getHeight(i-1), getWidth(i-1));
  372. break;
  373. }
  374. case Intensity:
  375. case Luminance:
  376. case Alpha:
  377. case RGB565:
  378. #ifdef TORQUE_OS_IOS
  379. case PVR2:
  380. case PVR2A:
  381. case PVR4:
  382. case PVR4A:
  383. #endif
  384. break;
  385. }
  386. if (clearBorders)
  387. {
  388. for (U32 i = 1; i<numMipLevels; i++)
  389. {
  390. U32 width = getWidth(i);
  391. U32 height = getHeight(i);
  392. if (height<3 || width<3)
  393. // bmp is all borders at this mip level
  394. dMemset(getWritableBits(i),0,width*height*bytesPerPixel);
  395. else
  396. {
  397. width *= bytesPerPixel;
  398. U8 * bytes = getWritableBits(i);
  399. U8 * end = bytes + (height-1)*width - bytesPerPixel; // end = last row, 2nd column
  400. // clear first row sans the last pixel
  401. dMemset(bytes,0,width-bytesPerPixel);
  402. bytes -= bytesPerPixel;
  403. while (bytes<end)
  404. {
  405. // clear last pixel of row N-1 and first pixel of row N
  406. bytes += width;
  407. dMemset(bytes,0,bytesPerPixel*2);
  408. }
  409. // clear last row sans the first pixel
  410. dMemset(bytes+2*bytesPerPixel,0,width-bytesPerPixel);
  411. }
  412. }
  413. }
  414. }
  415. //--------------------------------------------------------------------------
  416. void GBitmap::extrudeMipLevelsDetail()
  417. {
  418. AssertFatal(getFormat() == GBitmap::RGB, "Error, only handles RGB for now...");
  419. U32 i,j;
  420. if(numMipLevels == 1)
  421. allocateBitmap(getWidth(), getHeight(), true, getFormat());
  422. for (i = 1; i < numMipLevels; i++) {
  423. bitmapExtrudeRGB(getBits(i - 1), getWritableBits(i), getHeight(i-1), getWidth(i-1));
  424. }
  425. // Ok, now that we have the levels extruded, we need to move the lower miplevels
  426. // closer to 0.5.
  427. for (i = 1; i < numMipLevels - 1; i++) {
  428. U8* pMipBits = (U8*)getWritableBits(i);
  429. U32 numBytes = getWidth(i) * getHeight(i) * 3;
  430. U32 shift = i;
  431. U32 start = ((1 << i) - 1) * 0x80;
  432. for (j = 0; j < numBytes; j++) {
  433. U32 newVal = (start + pMipBits[j]) >> shift;
  434. AssertFatal(newVal <= 255, "Error, oob");
  435. pMipBits[j] = U8(newVal);
  436. }
  437. }
  438. AssertFatal(getWidth(numMipLevels - 1) == 1 && getHeight(numMipLevels - 1) == 1,
  439. "Error, last miplevel should be 1x1!");
  440. ((U8*)getWritableBits(numMipLevels - 1))[0] = 0x80;
  441. ((U8*)getWritableBits(numMipLevels - 1))[1] = 0x80;
  442. ((U8*)getWritableBits(numMipLevels - 1))[2] = 0x80;
  443. }
  444. //--------------------------------------------------------------------------
  445. void bitmapConvertRGB_to_5551_c(U8 *src, U32 pixels)
  446. {
  447. U16 *dst = (U16 *)src;
  448. for(U32 j = 0; j < pixels; j++)
  449. {
  450. U32 r = src[0] >> 3;
  451. U32 g = src[1] >> 3;
  452. U32 b = src[2] >> 3;
  453. #if defined(TORQUE_BIG_ENDIAN)
  454. *dst++ = (1 << 15) | (b << 10) | (g << 5) | (r << 0);
  455. #else
  456. *dst++ = (b << 1) | (g << 6) | (r << 11) | 1;
  457. #endif
  458. src += 3;
  459. }
  460. }
  461. void (*bitmapConvertRGB_to_5551)(U8 *src, U32 pixels) = bitmapConvertRGB_to_5551_c;
  462. //--------------------------------------------------------------------------
  463. bool GBitmap::setFormat(BitmapFormat fmt)
  464. {
  465. if (getFormat() == fmt)
  466. return true;
  467. // this is a nasty pointer math hack
  468. // is there a quick way to calc pixels of a fully mipped bitmap?
  469. U32 pixels = 0;
  470. for (U32 i=0; i < numMipLevels; i++)
  471. pixels += getHeight(i) * getWidth(i);
  472. switch (getFormat())
  473. {
  474. case RGB:
  475. if ( fmt == RGB5551 )
  476. {
  477. bitmapConvertRGB_to_5551(pBits, pixels);
  478. internalFormat = RGB5551;
  479. bytesPerPixel = 2;
  480. }
  481. break;
  482. default:
  483. AssertWarn(0, "GBitmap::setFormat: unable to convert bitmap to requested format.");
  484. return false;
  485. }
  486. U32 offset = 0;
  487. for (U32 j=0; j < numMipLevels; j++)
  488. {
  489. mipLevelOffsets[j] = offset;
  490. offset += getHeight(j) * getWidth(j) * bytesPerPixel;
  491. }
  492. return true;
  493. }
  494. //--------------------------------------------------------------------------
  495. bool GBitmap::getColorBGRA(const U32 x, const U32 y, ColorI& rColor) const
  496. {
  497. if(!getColor(x, y, rColor))
  498. return false;
  499. //jk - swap red and blue...
  500. U8 r = rColor.red;
  501. rColor.red = rColor.blue;
  502. rColor.blue = r;
  503. return true;
  504. }
  505. bool GBitmap::setColorBGRA(const U32 x, const U32 y, ColorI& rColor)
  506. {
  507. //jk - copy then swap red and blue...
  508. //jk - using a copy so the color object provided by the caller isn't swapped...
  509. ColorI temp = rColor;
  510. U8 r = temp.red;
  511. temp.red = temp.blue;
  512. temp.blue = r;
  513. return setColor(x, y, temp);
  514. }
  515. bool GBitmap::getColor(const U32 x, const U32 y, ColorI& rColor) const
  516. {
  517. if (x >= width || y >= height)
  518. return false;
  519. if (internalFormat == Palettized && pPalette == NULL)
  520. return false;
  521. const U8* pLoc = getAddress(x, y);
  522. switch (internalFormat) {
  523. case Palettized:
  524. rColor = pPalette->getColor(*pLoc);
  525. break;
  526. case Alpha:
  527. case Intensity:
  528. case Luminance:
  529. rColor.set( *pLoc, *pLoc, *pLoc, *pLoc );
  530. break;
  531. case RGB:
  532. rColor.set( pLoc[0], pLoc[1], pLoc[2], 255 );
  533. break;
  534. case RGBA:
  535. rColor.set( pLoc[0], pLoc[1], pLoc[2], pLoc[3] );
  536. break;
  537. case RGB5551:
  538. #if defined(TORQUE_BIG_ENDIAN)
  539. rColor.red = (*((U16*)pLoc) >> 0) & 0x1F;
  540. rColor.green = (*((U16*)pLoc) >> 5) & 0x1F;
  541. rColor.blue = (*((U16*)pLoc) >> 10) & 0x1F;
  542. rColor.alpha = ((*((U16*)pLoc) >> 15) & 0x01) ? 255 : 0;
  543. #else
  544. rColor.red = *((U16*)pLoc) >> 11;
  545. rColor.green = (*((U16*)pLoc) >> 6) & 0x1f;
  546. rColor.blue = (*((U16*)pLoc) >> 1) & 0x1f;
  547. rColor.alpha = (*((U16*)pLoc) & 1) ? 255 : 0;
  548. #endif
  549. break;
  550. default:
  551. AssertFatal(false, "Bad internal format");
  552. return false;
  553. }
  554. return true;
  555. }
  556. //--------------------------------------------------------------------------
  557. bool GBitmap::setColor(const U32 x, const U32 y, ColorI& rColor)
  558. {
  559. if (x >= width || y >= height)
  560. return false;
  561. if (internalFormat == Palettized && pPalette == NULL)
  562. return false;
  563. U8* pLoc = getAddress(x, y);
  564. switch (internalFormat) {
  565. case Palettized:
  566. rColor = pPalette->getColor(*pLoc);
  567. break;
  568. case Alpha:
  569. case Intensity:
  570. case Luminance:
  571. *pLoc = rColor.alpha;
  572. break;
  573. case RGB:
  574. dMemcpy( pLoc, &rColor, 3 * sizeof( U8 ) );
  575. break;
  576. case RGBA:
  577. dMemcpy( pLoc, &rColor, 4 * sizeof( U8 ) );
  578. break;
  579. case RGB5551:
  580. #if defined(TORQUE_BIG_ENDIAN)
  581. *((U16*)pLoc) = (((rColor.alpha>0) ? 1 : 0)<<15) | (rColor.blue << 10) | (rColor.green << 5) | (rColor.red << 0);
  582. #else
  583. *((U16*)pLoc) = (rColor.blue << 1) | (rColor.green << 6) | (rColor.red << 11) | ((rColor.alpha>0) ? 1 : 0);
  584. #endif
  585. break;
  586. default:
  587. AssertFatal(false, "Bad internal format");
  588. return false;
  589. }
  590. return true;
  591. }
  592. //-----------------------------------------------------------------------------
  593. GBitmap* GBitmap::createPowerOfTwoBitmap()
  594. {
  595. if (isPow2(getWidth()) && isPow2(getHeight()))
  596. return NULL;
  597. AssertFatal(getNumMipLevels() == 1,
  598. "Cannot have non-pow2 bitmap with miplevels");
  599. U32 width = getWidth();
  600. U32 height = getHeight();
  601. U32 newWidth = getNextPow2(getWidth());
  602. U32 newHeight = getNextPow2(getHeight());
  603. GBitmap* pReturn = new GBitmap(newWidth, newHeight, false, getFormat());
  604. for (U32 i = 0; i < height; i++)
  605. {
  606. U8* pDest = (U8*)pReturn->getAddress(0, i);
  607. const U8* pSrc = (const U8*)getAddress(0, i);
  608. dMemcpy(pDest, pSrc, width * bytesPerPixel);
  609. pDest += width * bytesPerPixel;
  610. // set the src pixel to the last pixel in the row
  611. const U8 *pSrcPixel = pDest - bytesPerPixel;
  612. for(U32 j = width; j < newWidth; j++)
  613. for(U32 k = 0; k < bytesPerPixel; k++)
  614. *pDest++ = pSrcPixel[k];
  615. }
  616. for(U32 i = height; i < newHeight; i++)
  617. {
  618. U8* pDest = (U8*)pReturn->getAddress(0, i);
  619. U8* pSrc = (U8*)pReturn->getAddress(0, height-1);
  620. dMemcpy(pDest, pSrc, newWidth * bytesPerPixel);
  621. }
  622. return pReturn;
  623. }
  624. //------------------------------------------------------------------------------
  625. //-------------------------------------- Persistent I/O
  626. //
  627. #ifdef TORQUE_OS_IOS
  628. #define EXT_ARRAY_SIZE 4
  629. static const char* extArray[EXT_ARRAY_SIZE] = { "", ".pvr", ".jpg", ".png"};
  630. #else
  631. #define EXT_ARRAY_SIZE 3
  632. static const char* extArray[EXT_ARRAY_SIZE] = { "", ".jpg", ".png"};
  633. #endif
  634. ResourceObject * GBitmap::findBmpResource(const char * path)
  635. {
  636. char fileNameBuffer[512];
  637. dStrcpy( fileNameBuffer, path );
  638. // Try some different possible filenames.
  639. U32 len = dStrlen( fileNameBuffer );
  640. for( U32 i = 0; i < EXT_ARRAY_SIZE; i++ )
  641. {
  642. dStrcpy( fileNameBuffer + len, extArray[i] );
  643. ResourceObject * ret = ResourceManager->find( fileNameBuffer );
  644. if (ret)
  645. return ret;
  646. }
  647. return NULL;
  648. }
  649. GBitmap *GBitmap::load(const char *path)
  650. {
  651. ResourceObject * ro = findBmpResource(path);
  652. if (ro)
  653. {
  654. GBitmap *bmp = (GBitmap*)ResourceManager->loadInstance(ro);
  655. return bmp;
  656. }
  657. // If unable to load texture in current directory
  658. // look in the parent directory. But never look in the root.
  659. char fileNameBuffer[512];
  660. dStrcpy( fileNameBuffer, path );
  661. char *name = dStrrchr( fileNameBuffer, '/' );
  662. if( name )
  663. {
  664. *name++ = 0;
  665. char *parent = dStrrchr( fileNameBuffer, '/' );
  666. if( parent )
  667. {
  668. parent[1] = 0;
  669. dStrcat( fileNameBuffer, name );
  670. return load( fileNameBuffer );
  671. }
  672. }
  673. return NULL;
  674. }
  675. bool GBitmap::read(Stream& io_rStream)
  676. {
  677. // Handle versioning
  678. U32 version;
  679. io_rStream.read(&version);
  680. AssertFatal(version == csFileVersion, "Bitmap::read: incorrect file version");
  681. //-------------------------------------- Read the object
  682. U32 fmt;
  683. io_rStream.read(&fmt);
  684. internalFormat = BitmapFormat(fmt);
  685. bytesPerPixel = 1;
  686. switch (internalFormat) {
  687. case Alpha:
  688. case Palettized:
  689. case Luminance:
  690. case Intensity: bytesPerPixel = 1;
  691. break;
  692. case RGB: bytesPerPixel = 3;
  693. break;
  694. case RGBA: bytesPerPixel = 4;
  695. break;
  696. case RGB565:
  697. case RGB5551: bytesPerPixel = 2;
  698. break;
  699. default:
  700. AssertFatal(false, "GBitmap::GBitmap: misunderstood format specifier");
  701. break;
  702. }
  703. io_rStream.read(&byteSize);
  704. pBits = new U8[byteSize];
  705. io_rStream.read(byteSize, pBits);
  706. io_rStream.read(&width);
  707. io_rStream.read(&height);
  708. io_rStream.read(&numMipLevels);
  709. for (U32 i = 0; i < c_maxMipLevels; i++)
  710. io_rStream.read(&mipLevelOffsets[i]);
  711. if (internalFormat == Palettized) {
  712. pPalette = new GPalette;
  713. pPalette->read(io_rStream);
  714. }
  715. return (io_rStream.getStatus() == Stream::Ok);
  716. }
  717. bool GBitmap::write(Stream& io_rStream) const
  718. {
  719. // Handle versioning
  720. io_rStream.write(csFileVersion);
  721. //-------------------------------------- Write the object
  722. io_rStream.write(U32(internalFormat));
  723. io_rStream.write(byteSize);
  724. io_rStream.write(byteSize, pBits);
  725. io_rStream.write(width);
  726. io_rStream.write(height);
  727. io_rStream.write(numMipLevels);
  728. for (U32 i = 0; i < c_maxMipLevels; i++)
  729. io_rStream.write(mipLevelOffsets[i]);
  730. if (internalFormat == Palettized) {
  731. AssertFatal(pPalette != NULL,
  732. "GBitmap::write: cannot write a palettized bitmap wo/ a palette");
  733. pPalette->write(io_rStream);
  734. }
  735. return true;
  736. }
  737. //-------------------------------------- GFXBitmap
  738. ResourceInstance* constructBitmapJPEG(Stream &stream)
  739. {
  740. GBitmap* bmp = new GBitmap;
  741. if (bmp->readJPEG(stream))
  742. return bmp;
  743. else
  744. {
  745. delete bmp;
  746. return NULL;
  747. }
  748. }
  749. ResourceInstance* constructBitmapPNG(Stream &stream)
  750. {
  751. GBitmap* bmp = new GBitmap;
  752. //PUAP -Mat uless you compile with a custom build step 'IPHONE_OPTIMIZE_OPTIONS' set to '-skip-PNGs', you're PNGs will be altered(optimized)
  753. //So either deal with that youself or define this so that we load it using apple iPhone functions to get the PNG data to Torque
  754. #ifdef USE_APPLE_OPTIMIZED_PNGS
  755. if (bmp->readPNGiPhone(stream)){
  756. #else
  757. if (bmp->readPNG(stream)){
  758. #endif
  759. return bmp;
  760. }
  761. else
  762. {
  763. delete bmp;
  764. return NULL;
  765. }
  766. }
  767. ResourceInstance* constructBitmapBMP(Stream &stream)
  768. {
  769. GBitmap *bmp = new GBitmap;
  770. if(bmp->readMSBmp(stream))
  771. return bmp;
  772. else
  773. {
  774. delete bmp;
  775. return NULL;
  776. }
  777. }
  778. #ifdef TORQUE_OS_IOS
  779. ResourceInstance* constructBitmapPVR(Stream &stream)
  780. {
  781. GBitmap *bmp = new GBitmap;
  782. if(bmp->readPvr(stream))
  783. return bmp;
  784. else
  785. {
  786. delete bmp;
  787. return NULL;
  788. }
  789. }
  790. #endif