gBitmap.cpp 33 KB

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