gBitmap.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  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 "platform/platform.h"
  23. #include "gfx/bitmap/gBitmap.h"
  24. #include "core/resourceManager.h"
  25. #include "core/stream/fileStream.h"
  26. #include "core/strings/stringFunctions.h"
  27. #include "core/color.h"
  28. #include "gfx/bitmap/bitmapUtils.h"
  29. #include "math/mRect.h"
  30. #include "console/console.h"
  31. #include "platform/profiler.h"
  32. #include "console/engineAPI.h"
  33. using namespace Torque;
  34. const U32 GBitmap::csFileVersion = 3;
  35. Vector<GBitmap::Registration> GBitmap::sRegistrations( __FILE__, __LINE__ );
  36. GBitmap::GBitmap()
  37. : mInternalFormat(GFXFormatR8G8B8),
  38. mBits(NULL),
  39. mByteSize(0),
  40. mWidth(0),
  41. mHeight(0),
  42. mBytesPerPixel(0),
  43. mNumMipLevels(0),
  44. mHasTransparency(false)
  45. {
  46. for (U32 i = 0; i < c_maxMipLevels; i++)
  47. mMipLevelOffsets[i] = 0xffffffff;
  48. }
  49. GBitmap::GBitmap(const GBitmap& rCopy)
  50. {
  51. mInternalFormat = rCopy.mInternalFormat;
  52. mByteSize = rCopy.mByteSize;
  53. mBits = new U8[mByteSize];
  54. dMemcpy(mBits, rCopy.mBits, mByteSize);
  55. mWidth = rCopy.mWidth;
  56. mHeight = rCopy.mHeight;
  57. mBytesPerPixel = rCopy.mBytesPerPixel;
  58. mNumMipLevels = rCopy.mNumMipLevels;
  59. dMemcpy(mMipLevelOffsets, rCopy.mMipLevelOffsets, sizeof(mMipLevelOffsets));
  60. mHasTransparency = rCopy.mHasTransparency;
  61. }
  62. GBitmap::GBitmap(const U32 in_width,
  63. const U32 in_height,
  64. const bool in_extrudeMipLevels,
  65. const GFXFormat in_format)
  66. : mBits(NULL),
  67. mByteSize(0)
  68. {
  69. for (U32 i = 0; i < c_maxMipLevels; i++)
  70. mMipLevelOffsets[i] = 0xffffffff;
  71. allocateBitmap(in_width, in_height, in_extrudeMipLevels, in_format);
  72. mHasTransparency = false;
  73. }
  74. GBitmap::GBitmap(const U32 in_width,
  75. const U32 in_height,
  76. const U8* data )
  77. : mBits(NULL),
  78. mByteSize(0)
  79. {
  80. allocateBitmap(in_width, in_height, false, GFXFormatR8G8B8A8);
  81. mHasTransparency = false;
  82. for (U32 x = 0; x < in_width; x++)
  83. {
  84. for (U32 y = 0; y < in_height; y++)
  85. {
  86. U32 offset = (x + y * in_width) * 4;
  87. ColorI color(data[offset],
  88. data[offset + 1],
  89. data[offset + 2],
  90. data[offset + 3]);
  91. if (color.alpha < 255)
  92. mHasTransparency = true;
  93. setColor(x, y, color);
  94. }
  95. }
  96. }
  97. //--------------------------------------------------------------------------
  98. GBitmap::~GBitmap()
  99. {
  100. deleteImage();
  101. }
  102. //--------------------------------------------------------------------------
  103. void GBitmap::sRegisterFormat( const GBitmap::Registration &reg )
  104. {
  105. U32 insert = sRegistrations.size();
  106. for ( U32 i = 0; i < sRegistrations.size(); i++ )
  107. {
  108. if ( sRegistrations[i].priority <= reg.priority )
  109. {
  110. insert = i;
  111. break;
  112. }
  113. }
  114. sRegistrations.insert( insert, reg );
  115. }
  116. const GBitmap::Registration *GBitmap::sFindRegInfo( const String &extension )
  117. {
  118. for ( U32 i = 0; i < GBitmap::sRegistrations.size(); i++ )
  119. {
  120. const GBitmap::Registration &reg = GBitmap::sRegistrations[i];
  121. const Vector<String> &extensions = reg.extensions;
  122. for ( U32 j = 0; j < extensions.size(); ++j )
  123. {
  124. if ( extensions[j].equal( extension, String::NoCase ) )
  125. return &reg;
  126. }
  127. }
  128. return NULL;
  129. }
  130. bool GBitmap::sFindFile( const Path &path, Path *outPath )
  131. {
  132. PROFILE_SCOPE( GBitmap_sFindFile );
  133. const String origExt( String::ToLower( path.getExtension() ) );
  134. Path tryPath( path );
  135. for ( U32 i = 0; i < sRegistrations.size(); i++ )
  136. {
  137. const Registration &reg = sRegistrations[i];
  138. const Vector<String> &extensions = reg.extensions;
  139. for ( U32 j = 0; j < extensions.size(); ++j )
  140. {
  141. // We've already tried this one.
  142. if ( extensions[j] == origExt )
  143. continue;
  144. tryPath.setExtension( extensions[j] );
  145. if ( !Torque::FS::IsFile( tryPath ) )
  146. continue;
  147. if ( outPath )
  148. *outPath = tryPath;
  149. return true;
  150. }
  151. }
  152. return false;
  153. }
  154. bool GBitmap::sFindFiles( const Path &path, Vector<Path> *outFoundPaths )
  155. {
  156. PROFILE_SCOPE( GBitmap_sFindFiles );
  157. Path tryPath( path );
  158. for ( U32 i = 0; i < GBitmap::sRegistrations.size(); i++ )
  159. {
  160. const GBitmap::Registration &reg = GBitmap::sRegistrations[i];
  161. const Vector<String> &extensions = reg.extensions;
  162. for ( U32 j = 0; j < extensions.size(); ++j )
  163. {
  164. tryPath.setExtension( extensions[j] );
  165. if ( Torque::FS::IsFile( tryPath ) )
  166. {
  167. if ( outFoundPaths )
  168. outFoundPaths->push_back( tryPath );
  169. else
  170. return true;
  171. }
  172. }
  173. }
  174. return outFoundPaths ? outFoundPaths->size() > 0 : false;
  175. }
  176. String GBitmap::sGetExtensionList()
  177. {
  178. String list;
  179. for ( U32 i = 0; i < sRegistrations.size(); i++ )
  180. {
  181. const Registration &reg = sRegistrations[i];
  182. for ( U32 j = 0; j < reg.extensions.size(); j++ )
  183. {
  184. list += reg.extensions[j];
  185. list += " ";
  186. }
  187. }
  188. return list;
  189. }
  190. //--------------------------------------------------------------------------
  191. void GBitmap::deleteImage()
  192. {
  193. delete [] mBits;
  194. mBits = NULL;
  195. mByteSize = 0;
  196. mWidth = 0;
  197. mHeight = 0;
  198. mNumMipLevels = 0;
  199. }
  200. //--------------------------------------------------------------------------
  201. void GBitmap::copyRect(const GBitmap *src, const RectI &srcRect, const Point2I &dstPt, const U32 srcMipLevel, const U32 dstMipLevel)
  202. {
  203. if(src->getFormat() != getFormat())
  204. return;
  205. if(srcRect.extent.x + srcRect.point.x > src->getWidth(srcMipLevel) || srcRect.extent.y + srcRect.point.y > src->getHeight(srcMipLevel))
  206. return;
  207. if(srcRect.extent.x + dstPt.x > getWidth(dstMipLevel) || srcRect.extent.y + dstPt.y > getHeight(dstMipLevel))
  208. return;
  209. for(U32 i = 0; i < srcRect.extent.y; i++)
  210. {
  211. dMemcpy(getAddress(dstPt.x, dstPt.y + i, dstMipLevel),
  212. src->getAddress(srcRect.point.x, srcRect.point.y + i, srcMipLevel),
  213. mBytesPerPixel * srcRect.extent.x);
  214. }
  215. }
  216. //--------------------------------------------------------------------------
  217. void GBitmap::allocateBitmap(const U32 in_width, const U32 in_height, const bool in_extrudeMipLevels, const GFXFormat in_format )
  218. {
  219. //-------------------------------------- Some debug checks...
  220. U32 svByteSize = mByteSize;
  221. U8 *svBits = mBits;
  222. AssertFatal(in_width != 0 && in_height != 0, "GBitmap::allocateBitmap: width or height is 0");
  223. if (in_extrudeMipLevels == true)
  224. {
  225. AssertFatal(isPow2(in_width) == true && isPow2(in_height) == true, "GBitmap::GBitmap: in order to extrude mip levels, bitmap w/h must be pow2");
  226. }
  227. mInternalFormat = in_format;
  228. mWidth = in_width;
  229. mHeight = in_height;
  230. mBytesPerPixel = 1;
  231. switch (mInternalFormat)
  232. {
  233. case GFXFormatA8:
  234. case GFXFormatL8: mBytesPerPixel = 1;
  235. break;
  236. case GFXFormatR8G8B8: mBytesPerPixel = 3;
  237. break;
  238. case GFXFormatR8G8B8A8_LINEAR_FORCE:
  239. case GFXFormatR8G8B8X8:
  240. case GFXFormatR8G8B8A8: mBytesPerPixel = 4;
  241. break;
  242. case GFXFormatR5G6B5:
  243. case GFXFormatR5G5B5A1: mBytesPerPixel = 2;
  244. break;
  245. default:
  246. AssertFatal(false, "GBitmap::GBitmap: misunderstood format specifier");
  247. break;
  248. }
  249. // Set up the mip levels, if necessary...
  250. mNumMipLevels = 1;
  251. U32 allocPixels = in_width * in_height * mBytesPerPixel;
  252. mMipLevelOffsets[0] = 0;
  253. if (in_extrudeMipLevels == true)
  254. {
  255. U32 currWidth = in_width;
  256. U32 currHeight = in_height;
  257. do
  258. {
  259. mMipLevelOffsets[mNumMipLevels] = mMipLevelOffsets[mNumMipLevels - 1] +
  260. (currWidth * currHeight * mBytesPerPixel);
  261. currWidth >>= 1;
  262. currHeight >>= 1;
  263. if (currWidth == 0) currWidth = 1;
  264. if (currHeight == 0) currHeight = 1;
  265. mNumMipLevels++;
  266. allocPixels += currWidth * currHeight * mBytesPerPixel;
  267. } while (currWidth != 1 || currHeight != 1);
  268. U32 expectedMips = mFloor(mLog2(mMax(in_width, in_height))) + 1;
  269. AssertFatal(mNumMipLevels == expectedMips, "GBitmap::allocateBitmap: mipmap count wrong");
  270. }
  271. AssertFatal(mNumMipLevels <= c_maxMipLevels, "GBitmap::allocateBitmap: too many miplevels");
  272. // Set up the memory...
  273. mByteSize = allocPixels;
  274. mBits = new U8[mByteSize];
  275. dMemset(mBits, 0xFF, mByteSize);
  276. if(svBits != NULL)
  277. {
  278. dMemcpy(mBits, svBits, getMin(mByteSize, svByteSize));
  279. delete[] svBits;
  280. }
  281. }
  282. //--------------------------------------------------------------------------
  283. void GBitmap::extrudeMipLevels(bool clearBorders)
  284. {
  285. if(mNumMipLevels == 1)
  286. allocateBitmap(getWidth(), getHeight(), true, getFormat());
  287. switch (getFormat())
  288. {
  289. case GFXFormatR5G5B5A1:
  290. {
  291. for(U32 i = 1; i < mNumMipLevels; i++)
  292. bitmapExtrude5551(getBits(i - 1), getWritableBits(i), getHeight(i), getWidth(i));
  293. break;
  294. }
  295. case GFXFormatR8G8B8:
  296. {
  297. for(U32 i = 1; i < mNumMipLevels; i++)
  298. bitmapExtrudeRGB(getBits(i - 1), getWritableBits(i), getHeight(i-1), getWidth(i-1));
  299. break;
  300. }
  301. case GFXFormatR8G8B8A8:
  302. case GFXFormatR8G8B8X8:
  303. {
  304. for(U32 i = 1; i < mNumMipLevels; i++)
  305. bitmapExtrudeRGBA(getBits(i - 1), getWritableBits(i), getHeight(i-1), getWidth(i-1));
  306. break;
  307. }
  308. default:
  309. break;
  310. }
  311. if (clearBorders)
  312. {
  313. for (U32 i = 1; i<mNumMipLevels; i++)
  314. {
  315. U32 width = getWidth(i);
  316. U32 height = getHeight(i);
  317. if (height<3 || width<3)
  318. // bmp is all borders at this mip level
  319. dMemset(getWritableBits(i),0,width*height*mBytesPerPixel);
  320. else
  321. {
  322. width *= mBytesPerPixel;
  323. U8 * bytes = getWritableBits(i);
  324. U8 * end = bytes + (height-1)*width - mBytesPerPixel; // end = last row, 2nd column
  325. // clear first row sans the last pixel
  326. dMemset(bytes,0,width-mBytesPerPixel);
  327. bytes -= mBytesPerPixel;
  328. while (bytes<end)
  329. {
  330. // clear last pixel of row N-1 and first pixel of row N
  331. bytes += width;
  332. dMemset(bytes,0,mBytesPerPixel*2);
  333. }
  334. // clear last row sans the first pixel
  335. dMemset(bytes+2*mBytesPerPixel,0,width-mBytesPerPixel);
  336. }
  337. }
  338. }
  339. }
  340. //--------------------------------------------------------------------------
  341. void GBitmap::extrudeMipLevelsDetail()
  342. {
  343. AssertFatal(getFormat() == GFXFormatR8G8B8, "Error, only handles RGB for now...");
  344. U32 i,j;
  345. if(mNumMipLevels == 1)
  346. allocateBitmap(getWidth(), getHeight(), true, getFormat());
  347. for (i = 1; i < mNumMipLevels; i++) {
  348. bitmapExtrudeRGB(getBits(i - 1), getWritableBits(i), getHeight(i-1), getWidth(i-1));
  349. }
  350. // Ok, now that we have the levels extruded, we need to move the lower miplevels
  351. // closer to 0.5.
  352. for (i = 1; i < mNumMipLevels - 1; i++) {
  353. U8* pMipBits = (U8*)getWritableBits(i);
  354. U32 numBytes = getWidth(i) * getHeight(i) * 3;
  355. U32 shift = i;
  356. U32 start = ((1 << i) - 1) * 0x80;
  357. for (j = 0; j < numBytes; j++) {
  358. U32 newVal = (start + pMipBits[j]) >> shift;
  359. AssertFatal(newVal <= 255, "Error, oob");
  360. pMipBits[j] = U8(newVal);
  361. }
  362. }
  363. AssertFatal(getWidth(mNumMipLevels - 1) == 1 && getHeight(mNumMipLevels - 1) == 1,
  364. "Error, last miplevel should be 1x1!");
  365. ((U8*)getWritableBits(mNumMipLevels - 1))[0] = 0x80;
  366. ((U8*)getWritableBits(mNumMipLevels - 1))[1] = 0x80;
  367. ((U8*)getWritableBits(mNumMipLevels - 1))[2] = 0x80;
  368. }
  369. //--------------------------------------------------------------------------
  370. bool GBitmap::setFormat(GFXFormat fmt)
  371. {
  372. if (getFormat() == fmt)
  373. return true;
  374. PROFILE_SCOPE(GBitmap_setFormat);
  375. // this is a nasty pointer math hack
  376. // is there a quick way to calc pixels of a fully mipped bitmap?
  377. U32 pixels = 0;
  378. for (U32 i=0; i < mNumMipLevels; i++)
  379. pixels += getHeight(i) * getWidth(i);
  380. switch( getFormat() )
  381. {
  382. case GFXFormatR8G8B8:
  383. switch ( fmt )
  384. {
  385. case GFXFormatR5G5B5A1:
  386. #ifdef _XBOX
  387. bitmapConvertRGB_to_1555(mBits, pixels);
  388. #else
  389. bitmapConvertRGB_to_5551(mBits, pixels);
  390. #endif
  391. mInternalFormat = GFXFormatR5G5B5A1;
  392. mBytesPerPixel = 2;
  393. break;
  394. case GFXFormatR8G8B8A8:
  395. case GFXFormatR8G8B8X8:
  396. // Took this out, it may crash -patw
  397. //AssertFatal( mNumMipLevels == 1, "Do the mip-mapping in hardware." );
  398. bitmapConvertRGB_to_RGBX( &mBits, pixels );
  399. mInternalFormat = fmt;
  400. mBytesPerPixel = 4;
  401. mByteSize = pixels * 4;
  402. break;
  403. default:
  404. AssertWarn(0, "GBitmap::setFormat: unable to convert bitmap to requested format.");
  405. return false;
  406. }
  407. break;
  408. case GFXFormatR8G8B8X8:
  409. switch( fmt )
  410. {
  411. // No change needed for this
  412. case GFXFormatR8G8B8A8:
  413. mInternalFormat = GFXFormatR8G8B8A8;
  414. break;
  415. case GFXFormatR8G8B8:
  416. bitmapConvertRGBX_to_RGB( &mBits, pixels );
  417. mInternalFormat = GFXFormatR8G8B8;
  418. mBytesPerPixel = 3;
  419. mByteSize = pixels * 3;
  420. break;
  421. default:
  422. AssertWarn(0, "GBitmap::setFormat: unable to convert bitmap to requested format.");
  423. return false;
  424. }
  425. break;
  426. case GFXFormatR8G8B8A8:
  427. switch( fmt )
  428. {
  429. // No change needed for this
  430. case GFXFormatR8G8B8X8:
  431. mInternalFormat = GFXFormatR8G8B8X8;
  432. break;
  433. case GFXFormatR8G8B8:
  434. bitmapConvertRGBX_to_RGB( &mBits, pixels );
  435. mInternalFormat = GFXFormatR8G8B8;
  436. mBytesPerPixel = 3;
  437. mByteSize = pixels * 3;
  438. break;
  439. default:
  440. AssertWarn(0, "GBitmap::setFormat: unable to convert bitmap to requested format.");
  441. return false;
  442. }
  443. break;
  444. case GFXFormatA8:
  445. switch( fmt )
  446. {
  447. case GFXFormatR8G8B8A8:
  448. mInternalFormat = GFXFormatR8G8B8A8;
  449. bitmapConvertA8_to_RGBA( &mBits, pixels );
  450. mBytesPerPixel = 4;
  451. mByteSize = pixels * 4;
  452. break;
  453. default:
  454. AssertWarn(0, "GBitmap::setFormat: unable to convert bitmap to requested format.");
  455. return false;
  456. }
  457. break;
  458. default:
  459. AssertWarn(0, "GBitmap::setFormat: unable to convert bitmap to requested format.");
  460. return false;
  461. }
  462. U32 offset = 0;
  463. for (U32 j=0; j < mNumMipLevels; j++)
  464. {
  465. mMipLevelOffsets[j] = offset;
  466. offset += getHeight(j) * getWidth(j) * mBytesPerPixel;
  467. }
  468. return true;
  469. }
  470. //------------------------------------------------------------------------------
  471. bool GBitmap::checkForTransparency()
  472. {
  473. mHasTransparency = false;
  474. ColorI pixel(255, 255, 255, 255);
  475. switch (mInternalFormat)
  476. {
  477. // Non-transparent formats
  478. case GFXFormatL8:
  479. case GFXFormatR8G8B8:
  480. case GFXFormatR5G6B5:
  481. break;
  482. // Transparent formats
  483. case GFXFormatA8:
  484. case GFXFormatR8G8B8A8:
  485. case GFXFormatR5G5B5A1:
  486. // Let getColor() do the heavy lifting
  487. for (U32 x = 0; x < mWidth; x++)
  488. {
  489. for (U32 y = 0; y < mHeight; y++)
  490. {
  491. if (getColor(x, y, pixel))
  492. {
  493. if (pixel.alpha < 255)
  494. {
  495. mHasTransparency = true;
  496. break;
  497. }
  498. }
  499. }
  500. }
  501. break;
  502. default:
  503. AssertFatal(false, "GBitmap::checkForTransparency: misunderstood format specifier");
  504. break;
  505. }
  506. return mHasTransparency;
  507. }
  508. //------------------------------------------------------------------------------
  509. ColorF GBitmap::sampleTexel(F32 u, F32 v) const
  510. {
  511. ColorF col(0.5f, 0.5f, 0.5f);
  512. // normally sampling wraps all the way around at 1.0,
  513. // but locking doesn't support this, and we seem to calc
  514. // the uv based on a clamped 0 - 1...
  515. Point2F max((F32)(getWidth()-1), (F32)(getHeight()-1));
  516. Point2F posf;
  517. posf.x = mClampF(((u) * max.x), 0.0f, max.x);
  518. posf.y = mClampF(((v) * max.y), 0.0f, max.y);
  519. Point2I posi((S32)posf.x, (S32)posf.y);
  520. const U8 *buffer = getBits();
  521. U32 lexelindex = ((posi.y * getWidth()) + posi.x) * mBytesPerPixel;
  522. if(mBytesPerPixel == 2)
  523. {
  524. //U16 *buffer = (U16 *)lockrect->pBits;
  525. }
  526. else if(mBytesPerPixel > 2)
  527. {
  528. col.red = F32(buffer[lexelindex + 0]) / 255.0f;
  529. col.green = F32(buffer[lexelindex + 1]) / 255.0f;
  530. col.blue = F32(buffer[lexelindex + 2]) / 255.0f;
  531. }
  532. return col;
  533. }
  534. //--------------------------------------------------------------------------
  535. bool GBitmap::getColor(const U32 x, const U32 y, ColorI& rColor) const
  536. {
  537. if (x >= mWidth || y >= mHeight)
  538. return false;
  539. const U8* pLoc = getAddress(x, y);
  540. switch (mInternalFormat) {
  541. case GFXFormatA8:
  542. case GFXFormatL8:
  543. rColor.set( *pLoc, *pLoc, *pLoc, *pLoc );
  544. break;
  545. case GFXFormatR8G8B8:
  546. case GFXFormatR8G8B8X8:
  547. rColor.set( pLoc[0], pLoc[1], pLoc[2], 255 );
  548. break;
  549. case GFXFormatR8G8B8A8:
  550. rColor.set( pLoc[0], pLoc[1], pLoc[2], pLoc[3] );
  551. break;
  552. case GFXFormatR5G5B5A1:
  553. #if defined(TORQUE_OS_MAC)
  554. rColor.set( (*((U16*)pLoc) >> 0) & 0x1F,
  555. (*((U16*)pLoc) >> 5) & 0x1F,
  556. (*((U16*)pLoc) >> 10) & 0x1F,
  557. ((*((U16*)pLoc) >> 15) & 0x01) ? 255 : 0 );
  558. #else
  559. rColor.set( *((U16*)pLoc) >> 11,
  560. (*((U16*)pLoc) >> 6) & 0x1f,
  561. (*((U16*)pLoc) >> 1) & 0x1f,
  562. (*((U16*)pLoc) & 1) ? 255 : 0 );
  563. #endif
  564. break;
  565. default:
  566. AssertFatal(false, "Bad internal format");
  567. return false;
  568. }
  569. return true;
  570. }
  571. //--------------------------------------------------------------------------
  572. bool GBitmap::setColor(const U32 x, const U32 y, const ColorI& rColor)
  573. {
  574. if (x >= mWidth || y >= mHeight)
  575. return false;
  576. U8* pLoc = getAddress(x, y);
  577. switch (mInternalFormat) {
  578. case GFXFormatA8:
  579. case GFXFormatL8:
  580. *pLoc = rColor.alpha;
  581. break;
  582. case GFXFormatR8G8B8:
  583. dMemcpy( pLoc, &rColor, 3 * sizeof( U8 ) );
  584. break;
  585. case GFXFormatR8G8B8A8:
  586. case GFXFormatR8G8B8X8:
  587. dMemcpy( pLoc, &rColor, 4 * sizeof( U8 ) );
  588. break;
  589. case GFXFormatR5G6B5:
  590. #ifdef TORQUE_OS_MAC
  591. *((U16*)pLoc) = (rColor.red << 11) | (rColor.green << 5) | (rColor.blue << 0) ;
  592. #else
  593. *((U16*)pLoc) = (rColor.blue << 0) | (rColor.green << 5) | (rColor.red << 11);
  594. #endif
  595. break;
  596. case GFXFormatR5G5B5A1:
  597. #ifdef TORQUE_OS_MAC
  598. *((U16*)pLoc) = (((rColor.alpha>0) ? 1 : 0)<<15) | (rColor.blue << 10) | (rColor.green << 5) | (rColor.red << 0);
  599. #else
  600. *((U16*)pLoc) = (rColor.blue << 1) | (rColor.green << 6) | (rColor.red << 11) | ((rColor.alpha>0) ? 1 : 0);
  601. #endif
  602. break;
  603. default:
  604. AssertFatal(false, "Bad internal format");
  605. return false;
  606. }
  607. return true;
  608. }
  609. //-----------------------------------------------------------------------------
  610. bool GBitmap::combine( const GBitmap *bitmapA, const GBitmap *bitmapB, const GFXTextureOp combineOp )
  611. {
  612. // Check valid texture ops
  613. switch( combineOp )
  614. {
  615. case GFXTOPAdd:
  616. case GFXTOPSubtract:
  617. break;
  618. default:
  619. Con::errorf( "GBitmap::combine - Invalid op type" );
  620. return false;
  621. }
  622. // Check bitmapA format
  623. switch( bitmapA->getFormat() )
  624. {
  625. case GFXFormatR8G8B8:
  626. case GFXFormatR8G8B8X8:
  627. case GFXFormatR8G8B8A8:
  628. break;
  629. default:
  630. Con::errorf( "GBitmap::combine - invalid format for bitmapA" );
  631. return false;
  632. }
  633. // Check bitmapB format
  634. switch( bitmapB->getFormat() )
  635. {
  636. case GFXFormatR8G8B8:
  637. case GFXFormatR8G8B8X8:
  638. case GFXFormatR8G8B8A8:
  639. break;
  640. default:
  641. Con::errorf( "GBitmap::combine - invalid format for bitmapB" );
  642. return false;
  643. }
  644. // Determine format of result texture
  645. // CodeReview: This is dependent on the order of the GFXFormat enum. [5/11/2007 Pat]
  646. GFXFormat resFmt = static_cast<GFXFormat>( getMax( bitmapA->getFormat(), bitmapB->getFormat() ) );
  647. U32 resWidth = getMax( bitmapA->getWidth(), bitmapB->getWidth() );
  648. U32 resHeight = getMax( bitmapA->getHeight(), bitmapB->getHeight() );
  649. // Adjust size OF bitmap based on the biggest one
  650. if( bitmapA->getWidth() != bitmapB->getWidth() ||
  651. bitmapA->getHeight() != bitmapB->getHeight() )
  652. {
  653. // Delete old bitmap
  654. deleteImage();
  655. // Allocate new one
  656. allocateBitmap( resWidth, resHeight, false, resFmt );
  657. }
  658. // Adjust format of result bitmap (if resFmt == getFormat() it will not perform the format convert)
  659. setFormat( resFmt );
  660. // Perform combine
  661. U8 *destBits = getWritableBits();
  662. const U8 *aBits = bitmapA->getBits();
  663. const U8 *bBits = bitmapB->getBits();
  664. for( S32 y = 0; y < getHeight(); y++ )
  665. {
  666. for( S32 x = 0; x < getWidth(); x++ )
  667. {
  668. for( S32 _byte = 0; _byte < mBytesPerPixel; _byte++ )
  669. {
  670. U8 pxA = 0;
  671. U8 pxB = 0;
  672. // Get contributions from A and B
  673. if( y < bitmapA->getHeight() &&
  674. x < bitmapA->getWidth() &&
  675. _byte < bitmapA->mBytesPerPixel )
  676. pxA = *aBits++;
  677. if( y < bitmapB->getHeight() &&
  678. x < bitmapB->getWidth() &&
  679. _byte < bitmapB->mBytesPerPixel )
  680. pxB = *bBits++;
  681. // Combine them (clamp values 0-U8_MAX)
  682. switch( combineOp )
  683. {
  684. case GFXTOPAdd:
  685. *destBits++ = getMin( U8( pxA + pxB ), U8_MAX );
  686. break;
  687. case GFXTOPSubtract:
  688. *destBits++ = getMax( U8( pxA - pxB ), U8( 0 ) );
  689. break;
  690. default:
  691. AssertFatal(false, "GBitmap::combine - Invalid combineOp");
  692. break;
  693. }
  694. }
  695. }
  696. }
  697. return true;
  698. }
  699. void GBitmap::fill( const ColorI &rColor )
  700. {
  701. // Set the first pixel using the slow
  702. // but proper method.
  703. setColor( 0, 0, rColor );
  704. mHasTransparency = rColor.alpha < 255;
  705. // Now fill the first row of the bitmap by
  706. // copying the first pixel across the row.
  707. const U32 stride = getWidth() * mBytesPerPixel;
  708. const U8 *src = getBits();
  709. U8 *dest = getWritableBits() + mBytesPerPixel;
  710. const U8 *end = src + stride;
  711. for ( ; dest != end; dest += mBytesPerPixel )
  712. dMemcpy( dest, src, mBytesPerPixel );
  713. // Now copy the first row to all the others.
  714. //
  715. // TODO: This could adaptively size the copy
  716. // amount to copy more rows from the source
  717. // and reduce the total number of memcpy calls.
  718. //
  719. dest = getWritableBits() + stride;
  720. end = src + ( stride * getHeight() );
  721. for ( ; dest != end; dest += stride )
  722. dMemcpy( dest, src, stride );
  723. }
  724. void GBitmap::fillWhite()
  725. {
  726. dMemset( getWritableBits(), 255, mByteSize );
  727. mHasTransparency = false;
  728. }
  729. GBitmap* GBitmap::createPaddedBitmap() const
  730. {
  731. if (isPow2(getWidth()) && isPow2(getHeight()))
  732. return NULL;
  733. AssertFatal(getNumMipLevels() == 1,
  734. "Cannot have non-pow2 bitmap with miplevels");
  735. U32 width = getWidth();
  736. U32 height = getHeight();
  737. U32 newWidth = getNextPow2(getWidth());
  738. U32 newHeight = getNextPow2(getHeight());
  739. GBitmap* pReturn = new GBitmap(newWidth, newHeight, false, getFormat());
  740. for (U32 i = 0; i < height; i++)
  741. {
  742. U8* pDest = (U8*)pReturn->getAddress(0, i);
  743. const U8* pSrc = (const U8*)getAddress(0, i);
  744. dMemcpy(pDest, pSrc, width * mBytesPerPixel);
  745. pDest += width * mBytesPerPixel;
  746. // set the src pixel to the last pixel in the row
  747. const U8 *pSrcPixel = pDest - mBytesPerPixel;
  748. for(U32 j = width; j < newWidth; j++)
  749. for(U32 k = 0; k < mBytesPerPixel; k++)
  750. *pDest++ = pSrcPixel[k];
  751. }
  752. for(U32 i = height; i < newHeight; i++)
  753. {
  754. U8* pDest = (U8*)pReturn->getAddress(0, i);
  755. U8* pSrc = (U8*)pReturn->getAddress(0, height-1);
  756. dMemcpy(pDest, pSrc, newWidth * mBytesPerPixel);
  757. }
  758. return pReturn;
  759. }
  760. GBitmap* GBitmap::createPow2Bitmap() const
  761. {
  762. if (isPow2(getWidth()) && isPow2(getHeight()))
  763. return NULL;
  764. AssertFatal(getNumMipLevels() == 1,
  765. "Cannot have non-pow2 bitmap with miplevels");
  766. U32 width = getWidth();
  767. U32 height = getHeight();
  768. U32 newWidth = getNextPow2(getWidth());
  769. U32 newHeight = getNextPow2(getHeight());
  770. GBitmap* pReturn = new GBitmap(newWidth, newHeight, false, getFormat());
  771. U8* pDest = (U8*)pReturn->getAddress(0, 0);
  772. const U8* pSrc = (const U8*)getAddress(0, 0);
  773. F32 yCoeff = (F32) height / (F32) newHeight;
  774. F32 xCoeff = (F32) width / (F32) newWidth;
  775. F32 currY = 0.0f;
  776. for (U32 y = 0; y < newHeight; y++)
  777. {
  778. F32 currX = 0.0f;
  779. //U32 yDestOffset = (pReturn->mWidth * pReturn->mBytesPerPixel) * y;
  780. //U32 xDestOffset = 0;
  781. //U32 ySourceOffset = (U32)((mWidth * mBytesPerPixel) * currY);
  782. //F32 xSourceOffset = 0.0f;
  783. for (U32 x = 0; x < newWidth; x++)
  784. {
  785. pDest = (U8*) pReturn->getAddress(x, y);
  786. pSrc = (U8*) getAddress((S32)currX, (S32)currY);
  787. for (U32 p = 0; p < pReturn->mBytesPerPixel; p++)
  788. {
  789. pDest[p] = pSrc[p];
  790. }
  791. currX += xCoeff;
  792. }
  793. currY += yCoeff;
  794. }
  795. return pReturn;
  796. }
  797. void GBitmap::copyChannel( U32 index, GBitmap *outBitmap ) const
  798. {
  799. AssertFatal( index < mBytesPerPixel, "GBitmap::copyChannel() - Bad channel offset!" );
  800. AssertFatal( outBitmap, "GBitmap::copyChannel() - Null output bitmap!" );
  801. AssertFatal( outBitmap->getWidth() == getWidth(), "GBitmap::copyChannel() - Width mismatch!" );
  802. AssertFatal( outBitmap->getHeight() == getHeight(), "GBitmap::copyChannel() - Height mismatch!" );
  803. U8 *outBits = outBitmap->getWritableBits();
  804. const U32 outBytesPerPixel = outBitmap->getBytesPerPixel();
  805. const U8 *srcBits = getBits() + index;
  806. const U8 *endBits = getBits() + mByteSize;
  807. for ( ; srcBits < endBits; )
  808. {
  809. *outBits = *srcBits;
  810. outBits += outBytesPerPixel;
  811. srcBits += mBytesPerPixel;
  812. }
  813. }
  814. //------------------------------------------------------------------------------
  815. bool GBitmap::read(Stream& io_rStream)
  816. {
  817. // Handle versioning
  818. U32 version;
  819. io_rStream.read(&version);
  820. AssertFatal(version == csFileVersion, "Bitmap::read: incorrect file version");
  821. //-------------------------------------- Read the object
  822. U32 fmt;
  823. io_rStream.read(&fmt);
  824. mInternalFormat = GFXFormat(fmt);
  825. mBytesPerPixel = 1;
  826. switch (mInternalFormat) {
  827. case GFXFormatA8:
  828. case GFXFormatL8: mBytesPerPixel = 1;
  829. break;
  830. case GFXFormatR8G8B8: mBytesPerPixel = 3;
  831. break;
  832. case GFXFormatR8G8B8A8: mBytesPerPixel = 4;
  833. break;
  834. case GFXFormatR5G6B5:
  835. case GFXFormatR5G5B5A1: mBytesPerPixel = 2;
  836. break;
  837. default:
  838. AssertFatal(false, "GBitmap::read: misunderstood format specifier");
  839. break;
  840. }
  841. io_rStream.read(&mByteSize);
  842. mBits = new U8[mByteSize];
  843. io_rStream.read(mByteSize, mBits);
  844. io_rStream.read(&mWidth);
  845. io_rStream.read(&mHeight);
  846. io_rStream.read(&mNumMipLevels);
  847. for (U32 i = 0; i < c_maxMipLevels; i++)
  848. io_rStream.read(&mMipLevelOffsets[i]);
  849. checkForTransparency();
  850. return (io_rStream.getStatus() == Stream::Ok);
  851. }
  852. bool GBitmap::write(Stream& io_rStream) const
  853. {
  854. // Handle versioning
  855. io_rStream.write(csFileVersion);
  856. //-------------------------------------- Write the object
  857. io_rStream.write(U32(mInternalFormat));
  858. io_rStream.write(mByteSize);
  859. io_rStream.write(mByteSize, mBits);
  860. io_rStream.write(mWidth);
  861. io_rStream.write(mHeight);
  862. io_rStream.write(mNumMipLevels);
  863. for (U32 i = 0; i < c_maxMipLevels; i++)
  864. io_rStream.write(mMipLevelOffsets[i]);
  865. return (io_rStream.getStatus() == Stream::Ok);
  866. }
  867. //------------------------------------------------------------------------------
  868. //-------------------------------------- Persistent I/O
  869. //
  870. bool GBitmap::readBitmap( const String &bmType, Stream &ioStream )
  871. {
  872. const GBitmap::Registration *regInfo = GBitmap::sFindRegInfo( bmType );
  873. if ( regInfo == NULL )
  874. {
  875. Con::errorf( "[GBitmap::readBitmap] unable to find registration for extension [%s]", bmType.c_str() );
  876. return NULL;
  877. }
  878. return regInfo->readFunc( ioStream, this );
  879. }
  880. bool GBitmap::writeBitmap( const String &bmType, Stream &ioStream, U32 compressionLevel )
  881. {
  882. const GBitmap::Registration *regInfo = GBitmap::sFindRegInfo( bmType );
  883. if ( regInfo == NULL )
  884. {
  885. Con::errorf( "[GBitmap::writeBitmap] unable to find registration for extension [%s]", bmType.c_str() );
  886. return NULL;
  887. }
  888. return regInfo->writeFunc( this, ioStream, (compressionLevel == U32_MAX) ? regInfo->defaultCompression : compressionLevel );
  889. }
  890. template<> void *Resource<GBitmap>::create(const Torque::Path &path)
  891. {
  892. PROFILE_SCOPE( ResourceGBitmap_create );
  893. #ifdef TORQUE_DEBUG_RES_MANAGER
  894. Con::printf( "Resource<GBitmap>::create - [%s]", path.getFullPath().c_str() );
  895. #endif
  896. FileStream stream;
  897. stream.open( path.getFullPath(), Torque::FS::File::Read );
  898. if ( stream.getStatus() != Stream::Ok )
  899. {
  900. Con::errorf( "Resource<GBitmap>::create - failed to open '%s'", path.getFullPath().c_str() );
  901. return NULL;
  902. }
  903. GBitmap *bmp = new GBitmap;
  904. const String extension = path.getExtension();
  905. if( !bmp->readBitmap( extension, stream ) )
  906. {
  907. Con::errorf( "Resource<GBitmap>::create - error reading '%s'", path.getFullPath().c_str() );
  908. delete bmp;
  909. bmp = NULL;
  910. }
  911. return bmp;
  912. }
  913. template<> ResourceBase::Signature Resource<GBitmap>::signature()
  914. {
  915. return MakeFourCC('b','i','t','m');
  916. }
  917. Resource<GBitmap> GBitmap::load(const Torque::Path &path)
  918. {
  919. Resource<GBitmap> ret = _load( path );
  920. if ( ret != NULL )
  921. return ret;
  922. // Do a recursive search.
  923. return _search( path );
  924. }
  925. Resource<GBitmap> GBitmap::_load(const Torque::Path &path)
  926. {
  927. PROFILE_SCOPE( GBitmap_load );
  928. if ( Torque::FS::IsFile( path ) )
  929. return ResourceManager::get().load( path );
  930. Path foundPath;
  931. if ( GBitmap::sFindFile( path, &foundPath ) )
  932. {
  933. Resource<GBitmap> ret = ResourceManager::get().load( foundPath );
  934. if ( ret != NULL )
  935. return ret;
  936. }
  937. return Resource< GBitmap >( NULL );
  938. }
  939. Resource<GBitmap> GBitmap::_search(const Torque::Path &path)
  940. {
  941. PROFILE_SCOPE( GBitmap_search );
  942. // If unable to load texture in current directory
  943. // look in the parent directory. But never look in the root.
  944. Path newPath( path );
  945. while ( true )
  946. {
  947. String filePath = newPath.getPath();
  948. String::SizeType slash = filePath.find( '/', filePath.length(), String::Right );
  949. if ( slash == String::NPos )
  950. break;
  951. slash = filePath.find( '/', filePath.length(), String::Right );
  952. if ( slash == String::NPos )
  953. break;
  954. String truncPath = filePath.substr( 0, slash );
  955. newPath.setPath( truncPath );
  956. Resource<GBitmap> ret = _load( newPath );
  957. if ( ret != NULL )
  958. return ret;
  959. }
  960. return Resource< GBitmap >( NULL );
  961. }
  962. DefineEngineFunction( getBitmapInfo, String, ( const char *filename ),,
  963. "Returns image info in the following format: width TAB height TAB bytesPerPixel. "
  964. "It will return an empty string if the file is not found.\n"
  965. "@ingroup Rendering\n" )
  966. {
  967. Resource<GBitmap> image = GBitmap::load( filename );
  968. if ( !image )
  969. return String::EmptyString;
  970. return String::ToString( "%d\t%d\t%d", image->getWidth(),
  971. image->getHeight(),
  972. image->getBytesPerPixel() );
  973. }