gBitmap.cc 27 KB

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