OgrePixelFormat.cpp 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "OgrePixelFormat.h"
  25. #include "OgreBitwise.h"
  26. #include "OgreColourValue.h"
  27. #include "OgreImageResampler.h"
  28. #include "OgreException.h"
  29. namespace Ogre {
  30. //-----------------------------------------------------------------------
  31. /**
  32. * A record that describes a pixel format in detail.
  33. */
  34. struct PixelFormatDescription {
  35. /* Name of the format, as in the enum */
  36. const char *name;
  37. /* Number of bytes one element (colour value) takes. */
  38. unsigned char elemBytes;
  39. /* Pixel format flags, see enum PixelFormatFlags for the bit field
  40. * definitions
  41. */
  42. uint32 flags;
  43. /** Component type
  44. */
  45. PixelComponentType componentType;
  46. /** Component count
  47. */
  48. unsigned char componentCount;
  49. /* Number of bits for red(or luminance), green, blue, alpha
  50. */
  51. unsigned char rbits,gbits,bbits,abits; /*, ibits, dbits, ... */
  52. /* Masks and shifts as used by packers/unpackers */
  53. uint32 rmask, gmask, bmask, amask;
  54. unsigned char rshift, gshift, bshift, ashift;
  55. };
  56. //-----------------------------------------------------------------------
  57. /** Pixel format database */
  58. PixelFormatDescription _pixelFormats[PF_COUNT] = {
  59. //-----------------------------------------------------------------------
  60. {"PF_UNKNOWN",
  61. /* Bytes per element */
  62. 0,
  63. /* Flags */
  64. 0,
  65. /* Component type and count */
  66. PCT_BYTE, 0,
  67. /* rbits, gbits, bbits, abits */
  68. 0, 0, 0, 0,
  69. /* Masks and shifts */
  70. 0, 0, 0, 0, 0, 0, 0, 0
  71. },
  72. //-----------------------------------------------------------------------
  73. {"PF_L8",
  74. /* Bytes per element */
  75. 1,
  76. /* Flags */
  77. PFF_LUMINANCE | PFF_NATIVEENDIAN,
  78. /* Component type and count */
  79. PCT_BYTE, 1,
  80. /* rbits, gbits, bbits, abits */
  81. 8, 0, 0, 0,
  82. /* Masks and shifts */
  83. 0xFF, 0, 0, 0, 0, 0, 0, 0
  84. },
  85. //-----------------------------------------------------------------------
  86. {"PF_L16",
  87. /* Bytes per element */
  88. 2,
  89. /* Flags */
  90. PFF_LUMINANCE | PFF_NATIVEENDIAN,
  91. /* Component type and count */
  92. PCT_SHORT, 1,
  93. /* rbits, gbits, bbits, abits */
  94. 16, 0, 0, 0,
  95. /* Masks and shifts */
  96. 0xFFFF, 0, 0, 0, 0, 0, 0, 0
  97. },
  98. //-----------------------------------------------------------------------
  99. {"PF_A8",
  100. /* Bytes per element */
  101. 1,
  102. /* Flags */
  103. PFF_HASALPHA | PFF_NATIVEENDIAN,
  104. /* Component type and count */
  105. PCT_BYTE, 1,
  106. /* rbits, gbits, bbits, abits */
  107. 0, 0, 0, 8,
  108. /* Masks and shifts */
  109. 0, 0, 0, 0xFF, 0, 0, 0, 0
  110. },
  111. //-----------------------------------------------------------------------
  112. {"PF_A4L4",
  113. /* Bytes per element */
  114. 1,
  115. /* Flags */
  116. PFF_HASALPHA | PFF_LUMINANCE | PFF_NATIVEENDIAN,
  117. /* Component type and count */
  118. PCT_BYTE, 2,
  119. /* rbits, gbits, bbits, abits */
  120. 4, 0, 0, 4,
  121. /* Masks and shifts */
  122. 0x0F, 0, 0, 0xF0, 0, 0, 0, 4
  123. },
  124. //-----------------------------------------------------------------------
  125. {"PF_BYTE_LA",
  126. /* Bytes per element */
  127. 2,
  128. /* Flags */
  129. PFF_HASALPHA | PFF_LUMINANCE,
  130. /* Component type and count */
  131. PCT_BYTE, 2,
  132. /* rbits, gbits, bbits, abits */
  133. 8, 0, 0, 8,
  134. /* Masks and shifts */
  135. 0,0,0,0,0,0,0,0
  136. },
  137. //-----------------------------------------------------------------------
  138. {"PF_R5G6B5",
  139. /* Bytes per element */
  140. 2,
  141. /* Flags */
  142. PFF_NATIVEENDIAN,
  143. /* Component type and count */
  144. PCT_BYTE, 3,
  145. /* rbits, gbits, bbits, abits */
  146. 5, 6, 5, 0,
  147. /* Masks and shifts */
  148. 0xF800, 0x07E0, 0x001F, 0,
  149. 11, 5, 0, 0
  150. },
  151. //-----------------------------------------------------------------------
  152. {"PF_B5G6R5",
  153. /* Bytes per element */
  154. 2,
  155. /* Flags */
  156. PFF_NATIVEENDIAN,
  157. /* Component type and count */
  158. PCT_BYTE, 3,
  159. /* rbits, gbits, bbits, abits */
  160. 5, 6, 5, 0,
  161. /* Masks and shifts */
  162. 0x001F, 0x07E0, 0xF800, 0,
  163. 0, 5, 11, 0
  164. },
  165. //-----------------------------------------------------------------------
  166. {"PF_A4R4G4B4",
  167. /* Bytes per element */
  168. 2,
  169. /* Flags */
  170. PFF_HASALPHA | PFF_NATIVEENDIAN,
  171. /* Component type and count */
  172. PCT_BYTE, 4,
  173. /* rbits, gbits, bbits, abits */
  174. 4, 4, 4, 4,
  175. /* Masks and shifts */
  176. 0x0F00, 0x00F0, 0x000F, 0xF000,
  177. 8, 4, 0, 12
  178. },
  179. //-----------------------------------------------------------------------
  180. {"PF_A1R5G5B5",
  181. /* Bytes per element */
  182. 2,
  183. /* Flags */
  184. PFF_HASALPHA | PFF_NATIVEENDIAN,
  185. /* Component type and count */
  186. PCT_BYTE, 4,
  187. /* rbits, gbits, bbits, abits */
  188. 5, 5, 5, 1,
  189. /* Masks and shifts */
  190. 0x7C00, 0x03E0, 0x001F, 0x8000,
  191. 10, 5, 0, 15,
  192. },
  193. //-----------------------------------------------------------------------
  194. {"PF_R8G8B8",
  195. /* Bytes per element */
  196. 3, // 24 bit integer -- special
  197. /* Flags */
  198. PFF_NATIVEENDIAN,
  199. /* Component type and count */
  200. PCT_BYTE, 3,
  201. /* rbits, gbits, bbits, abits */
  202. 8, 8, 8, 0,
  203. /* Masks and shifts */
  204. 0xFF0000, 0x00FF00, 0x0000FF, 0,
  205. 16, 8, 0, 0
  206. },
  207. //-----------------------------------------------------------------------
  208. {"PF_B8G8R8",
  209. /* Bytes per element */
  210. 3, // 24 bit integer -- special
  211. /* Flags */
  212. PFF_NATIVEENDIAN,
  213. /* Component type and count */
  214. PCT_BYTE, 3,
  215. /* rbits, gbits, bbits, abits */
  216. 8, 8, 8, 0,
  217. /* Masks and shifts */
  218. 0x0000FF, 0x00FF00, 0xFF0000, 0,
  219. 0, 8, 16, 0
  220. },
  221. //-----------------------------------------------------------------------
  222. {"PF_A8R8G8B8",
  223. /* Bytes per element */
  224. 4,
  225. /* Flags */
  226. PFF_HASALPHA | PFF_NATIVEENDIAN,
  227. /* Component type and count */
  228. PCT_BYTE, 4,
  229. /* rbits, gbits, bbits, abits */
  230. 8, 8, 8, 8,
  231. /* Masks and shifts */
  232. 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000,
  233. 16, 8, 0, 24
  234. },
  235. //-----------------------------------------------------------------------
  236. {"PF_A8B8G8R8",
  237. /* Bytes per element */
  238. 4,
  239. /* Flags */
  240. PFF_HASALPHA | PFF_NATIVEENDIAN,
  241. /* Component type and count */
  242. PCT_BYTE, 4,
  243. /* rbits, gbits, bbits, abits */
  244. 8, 8, 8, 8,
  245. /* Masks and shifts */
  246. 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000,
  247. 0, 8, 16, 24,
  248. },
  249. //-----------------------------------------------------------------------
  250. {"PF_B8G8R8A8",
  251. /* Bytes per element */
  252. 4,
  253. /* Flags */
  254. PFF_HASALPHA | PFF_NATIVEENDIAN,
  255. /* Component type and count */
  256. PCT_BYTE, 4,
  257. /* rbits, gbits, bbits, abits */
  258. 8, 8, 8, 8,
  259. /* Masks and shifts */
  260. 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF,
  261. 8, 16, 24, 0
  262. },
  263. //-----------------------------------------------------------------------
  264. {"PF_A2R10G10B10",
  265. /* Bytes per element */
  266. 4,
  267. /* Flags */
  268. PFF_HASALPHA | PFF_NATIVEENDIAN,
  269. /* Component type and count */
  270. PCT_BYTE, 4,
  271. /* rbits, gbits, bbits, abits */
  272. 10, 10, 10, 2,
  273. /* Masks and shifts */
  274. 0x3FF00000, 0x000FFC00, 0x000003FF, 0xC0000000,
  275. 20, 10, 0, 30
  276. },
  277. //-----------------------------------------------------------------------
  278. {"PF_A2B10G10R10",
  279. /* Bytes per element */
  280. 4,
  281. /* Flags */
  282. PFF_HASALPHA | PFF_NATIVEENDIAN,
  283. /* Component type and count */
  284. PCT_BYTE, 4,
  285. /* rbits, gbits, bbits, abits */
  286. 10, 10, 10, 2,
  287. /* Masks and shifts */
  288. 0x000003FF, 0x000FFC00, 0x3FF00000, 0xC0000000,
  289. 0, 10, 20, 30
  290. },
  291. //-----------------------------------------------------------------------
  292. {"PF_DXT1",
  293. /* Bytes per element */
  294. 0,
  295. /* Flags */
  296. PFF_COMPRESSED | PFF_HASALPHA,
  297. /* Component type and count */
  298. PCT_BYTE, 3, // No alpha
  299. /* rbits, gbits, bbits, abits */
  300. 0, 0, 0, 0,
  301. /* Masks and shifts */
  302. 0, 0, 0, 0, 0, 0, 0, 0
  303. },
  304. //-----------------------------------------------------------------------
  305. {"PF_DXT2",
  306. /* Bytes per element */
  307. 0,
  308. /* Flags */
  309. PFF_COMPRESSED | PFF_HASALPHA,
  310. /* Component type and count */
  311. PCT_BYTE, 4,
  312. /* rbits, gbits, bbits, abits */
  313. 0, 0, 0, 0,
  314. /* Masks and shifts */
  315. 0, 0, 0, 0, 0, 0, 0, 0
  316. },
  317. //-----------------------------------------------------------------------
  318. {"PF_DXT3",
  319. /* Bytes per element */
  320. 0,
  321. /* Flags */
  322. PFF_COMPRESSED | PFF_HASALPHA,
  323. /* Component type and count */
  324. PCT_BYTE, 4,
  325. /* rbits, gbits, bbits, abits */
  326. 0, 0, 0, 0,
  327. /* Masks and shifts */
  328. 0, 0, 0, 0, 0, 0, 0, 0
  329. },
  330. //-----------------------------------------------------------------------
  331. {"PF_DXT4",
  332. /* Bytes per element */
  333. 0,
  334. /* Flags */
  335. PFF_COMPRESSED | PFF_HASALPHA,
  336. /* Component type and count */
  337. PCT_BYTE, 4,
  338. /* rbits, gbits, bbits, abits */
  339. 0, 0, 0, 0,
  340. /* Masks and shifts */
  341. 0, 0, 0, 0, 0, 0, 0, 0
  342. },
  343. //-----------------------------------------------------------------------
  344. {"PF_DXT5",
  345. /* Bytes per element */
  346. 0,
  347. /* Flags */
  348. PFF_COMPRESSED | PFF_HASALPHA,
  349. /* Component type and count */
  350. PCT_BYTE, 4,
  351. /* rbits, gbits, bbits, abits */
  352. 0, 0, 0, 0,
  353. /* Masks and shifts */
  354. 0, 0, 0, 0, 0, 0, 0, 0
  355. },
  356. //-----------------------------------------------------------------------
  357. {"PF_FLOAT16_RGB",
  358. /* Bytes per element */
  359. 6,
  360. /* Flags */
  361. PFF_FLOAT,
  362. /* Component type and count */
  363. PCT_FLOAT16, 3,
  364. /* rbits, gbits, bbits, abits */
  365. 16, 16, 16, 0,
  366. /* Masks and shifts */
  367. 0, 0, 0, 0, 0, 0, 0, 0
  368. },
  369. //-----------------------------------------------------------------------
  370. {"PF_FLOAT16_RGBA",
  371. /* Bytes per element */
  372. 8,
  373. /* Flags */
  374. PFF_FLOAT | PFF_HASALPHA,
  375. /* Component type and count */
  376. PCT_FLOAT16, 4,
  377. /* rbits, gbits, bbits, abits */
  378. 16, 16, 16, 16,
  379. /* Masks and shifts */
  380. 0, 0, 0, 0, 0, 0, 0, 0
  381. },
  382. //-----------------------------------------------------------------------
  383. {"PF_FLOAT32_RGB",
  384. /* Bytes per element */
  385. 12,
  386. /* Flags */
  387. PFF_FLOAT,
  388. /* Component type and count */
  389. PCT_FLOAT32, 3,
  390. /* rbits, gbits, bbits, abits */
  391. 32, 32, 32, 0,
  392. /* Masks and shifts */
  393. 0, 0, 0, 0, 0, 0, 0, 0
  394. },
  395. //-----------------------------------------------------------------------
  396. {"PF_FLOAT32_RGBA",
  397. /* Bytes per element */
  398. 16,
  399. /* Flags */
  400. PFF_FLOAT | PFF_HASALPHA,
  401. /* Component type and count */
  402. PCT_FLOAT32, 4,
  403. /* rbits, gbits, bbits, abits */
  404. 32, 32, 32, 32,
  405. /* Masks and shifts */
  406. 0, 0, 0, 0, 0, 0, 0, 0
  407. },
  408. //-----------------------------------------------------------------------
  409. {"PF_X8R8G8B8",
  410. /* Bytes per element */
  411. 4,
  412. /* Flags */
  413. PFF_NATIVEENDIAN,
  414. /* Component type and count */
  415. PCT_BYTE, 3,
  416. /* rbits, gbits, bbits, abits */
  417. 8, 8, 8, 0,
  418. /* Masks and shifts */
  419. 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000,
  420. 16, 8, 0, 24
  421. },
  422. //-----------------------------------------------------------------------
  423. {"PF_X8B8G8R8",
  424. /* Bytes per element */
  425. 4,
  426. /* Flags */
  427. PFF_NATIVEENDIAN,
  428. /* Component type and count */
  429. PCT_BYTE, 3,
  430. /* rbits, gbits, bbits, abits */
  431. 8, 8, 8, 0,
  432. /* Masks and shifts */
  433. 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000,
  434. 0, 8, 16, 24
  435. },
  436. //-----------------------------------------------------------------------
  437. {"PF_R8G8B8A8",
  438. /* Bytes per element */
  439. 4,
  440. /* Flags */
  441. PFF_HASALPHA | PFF_NATIVEENDIAN,
  442. /* Component type and count */
  443. PCT_BYTE, 4,
  444. /* rbits, gbits, bbits, abits */
  445. 8, 8, 8, 8,
  446. /* Masks and shifts */
  447. 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF,
  448. 24, 16, 8, 0
  449. },
  450. //-----------------------------------------------------------------------
  451. {"PF_DEPTH",
  452. /* Bytes per element */
  453. 4,
  454. /* Flags */
  455. PFF_DEPTH,
  456. /* Component type and count */
  457. PCT_FLOAT32, 1, // ?
  458. /* rbits, gbits, bbits, abits */
  459. 0, 0, 0, 0,
  460. /* Masks and shifts */
  461. 0, 0, 0, 0, 0, 0, 0, 0
  462. },
  463. //-----------------------------------------------------------------------
  464. {"PF_SHORT_RGBA",
  465. /* Bytes per element */
  466. 8,
  467. /* Flags */
  468. PFF_HASALPHA,
  469. /* Component type and count */
  470. PCT_SHORT, 4,
  471. /* rbits, gbits, bbits, abits */
  472. 16, 16, 16, 16,
  473. /* Masks and shifts */
  474. 0, 0, 0, 0, 0, 0, 0, 0
  475. },
  476. //-----------------------------------------------------------------------
  477. {"PF_R3G3B2",
  478. /* Bytes per element */
  479. 1,
  480. /* Flags */
  481. PFF_NATIVEENDIAN,
  482. /* Component type and count */
  483. PCT_BYTE, 3,
  484. /* rbits, gbits, bbits, abits */
  485. 3, 3, 2, 0,
  486. /* Masks and shifts */
  487. 0xE0, 0x1C, 0x03, 0,
  488. 5, 2, 0, 0
  489. },
  490. //-----------------------------------------------------------------------
  491. {"PF_FLOAT16_R",
  492. /* Bytes per element */
  493. 2,
  494. /* Flags */
  495. PFF_FLOAT,
  496. /* Component type and count */
  497. PCT_FLOAT16, 1,
  498. /* rbits, gbits, bbits, abits */
  499. 16, 0, 0, 0,
  500. /* Masks and shifts */
  501. 0, 0, 0, 0, 0, 0, 0, 0
  502. },
  503. //-----------------------------------------------------------------------
  504. {"PF_FLOAT32_R",
  505. /* Bytes per element */
  506. 4,
  507. /* Flags */
  508. PFF_FLOAT,
  509. /* Component type and count */
  510. PCT_FLOAT32, 1,
  511. /* rbits, gbits, bbits, abits */
  512. 32, 0, 0, 0,
  513. /* Masks and shifts */
  514. 0, 0, 0, 0, 0, 0, 0, 0
  515. },
  516. //-----------------------------------------------------------------------
  517. {"PF_SHORT_GR",
  518. /* Bytes per element */
  519. 4,
  520. /* Flags */
  521. PFF_NATIVEENDIAN,
  522. /* Component type and count */
  523. PCT_SHORT, 2,
  524. /* rbits, gbits, bbits, abits */
  525. 16, 16, 0, 0,
  526. /* Masks and shifts */
  527. 0x0000FFFF, 0xFFFF0000, 0, 0,
  528. 0, 16, 0, 0
  529. },
  530. //-----------------------------------------------------------------------
  531. {"PF_FLOAT16_GR",
  532. /* Bytes per element */
  533. 4,
  534. /* Flags */
  535. PFF_FLOAT,
  536. /* Component type and count */
  537. PCT_FLOAT16, 2,
  538. /* rbits, gbits, bbits, abits */
  539. 16, 16, 0, 0,
  540. /* Masks and shifts */
  541. 0, 0, 0, 0, 0, 0, 0, 0
  542. },
  543. //-----------------------------------------------------------------------
  544. {"PF_FLOAT32_GR",
  545. /* Bytes per element */
  546. 8,
  547. /* Flags */
  548. PFF_FLOAT,
  549. /* Component type and count */
  550. PCT_FLOAT32, 2,
  551. /* rbits, gbits, bbits, abits */
  552. 32, 32, 0, 0,
  553. /* Masks and shifts */
  554. 0, 0, 0, 0, 0, 0, 0, 0
  555. },
  556. //-----------------------------------------------------------------------
  557. {"PF_SHORT_RGB",
  558. /* Bytes per element */
  559. 6,
  560. /* Flags */
  561. 0,
  562. /* Component type and count */
  563. PCT_SHORT, 3,
  564. /* rbits, gbits, bbits, abits */
  565. 16, 16, 16, 0,
  566. /* Masks and shifts */
  567. 0, 0, 0, 0, 0, 0, 0, 0
  568. },
  569. //-----------------------------------------------------------------------
  570. {"PF_PVRTC_RGB2",
  571. /* Bytes per element */
  572. 0,
  573. /* Flags */
  574. PFF_COMPRESSED,
  575. /* Component type and count */
  576. PCT_BYTE, 3,
  577. /* rbits, gbits, bbits, abits */
  578. 0, 0, 0, 0,
  579. /* Masks and shifts */
  580. 0, 0, 0, 0, 0, 0, 0, 0
  581. },
  582. //-----------------------------------------------------------------------
  583. {"PF_PVRTC_RGBA2",
  584. /* Bytes per element */
  585. 0,
  586. /* Flags */
  587. PFF_COMPRESSED | PFF_HASALPHA,
  588. /* Component type and count */
  589. PCT_BYTE, 4,
  590. /* rbits, gbits, bbits, abits */
  591. 0, 0, 0, 0,
  592. /* Masks and shifts */
  593. 0, 0, 0, 0, 0, 0, 0, 0
  594. },
  595. //-----------------------------------------------------------------------
  596. {"PF_PVRTC_RGB4",
  597. /* Bytes per element */
  598. 0,
  599. /* Flags */
  600. PFF_COMPRESSED,
  601. /* Component type and count */
  602. PCT_BYTE, 3,
  603. /* rbits, gbits, bbits, abits */
  604. 0, 0, 0, 0,
  605. /* Masks and shifts */
  606. 0, 0, 0, 0, 0, 0, 0, 0
  607. },
  608. //-----------------------------------------------------------------------
  609. {"PF_PVRTC_RGBA4",
  610. /* Bytes per element */
  611. 0,
  612. /* Flags */
  613. PFF_COMPRESSED | PFF_HASALPHA,
  614. /* Component type and count */
  615. PCT_BYTE, 4,
  616. /* rbits, gbits, bbits, abits */
  617. 0, 0, 0, 0,
  618. /* Masks and shifts */
  619. 0, 0, 0, 0, 0, 0, 0, 0
  620. },
  621. };
  622. //-----------------------------------------------------------------------
  623. size_t PixelBox::getConsecutiveSize() const
  624. {
  625. return PixelUtil::getMemorySize(getWidth(), getHeight(), getDepth(), format);
  626. }
  627. PixelBox PixelBox::getSubVolume(const Box &def) const
  628. {
  629. if(PixelUtil::isCompressed(format))
  630. {
  631. if(def.left == left && def.top == top && def.front == front &&
  632. def.right == right && def.bottom == bottom && def.back == back)
  633. {
  634. // Entire buffer is being queried
  635. return *this;
  636. }
  637. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Cannot return subvolume of compressed PixelBuffer", "PixelBox::getSubVolume");
  638. }
  639. if(!contains(def))
  640. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Bounds out of range", "PixelBox::getSubVolume");
  641. const size_t elemSize = PixelUtil::getNumElemBytes(format);
  642. // Calculate new data origin
  643. // Notice how we do not propagate left/top/front from the incoming box, since
  644. // the returned pointer is already offset
  645. PixelBox rval(def.getWidth(), def.getHeight(), def.getDepth(), format,
  646. ((uint8*)data) + ((def.left-left)*elemSize)
  647. + ((def.top-top)*rowPitch*elemSize)
  648. + ((def.front-front)*slicePitch*elemSize)
  649. );
  650. rval.rowPitch = rowPitch;
  651. rval.slicePitch = slicePitch;
  652. rval.format = format;
  653. return rval;
  654. }
  655. //-----------------------------------------------------------------------
  656. /**
  657. * Directly get the description record for provided pixel format. For debug builds,
  658. * this checks the bounds of fmt with an assertion.
  659. */
  660. static inline const PixelFormatDescription &getDescriptionFor(const PixelFormat fmt)
  661. {
  662. const int ord = (int)fmt;
  663. assert(ord>=0 && ord<PF_COUNT);
  664. return _pixelFormats[ord];
  665. }
  666. //-----------------------------------------------------------------------
  667. size_t PixelUtil::getNumElemBytes( PixelFormat format )
  668. {
  669. return getDescriptionFor(format).elemBytes;
  670. }
  671. //-----------------------------------------------------------------------
  672. size_t PixelUtil::getMemorySize(size_t width, size_t height, size_t depth, PixelFormat format)
  673. {
  674. if(isCompressed(format))
  675. {
  676. switch(format)
  677. {
  678. // DXT formats work by dividing the image into 4x4 blocks, then encoding each
  679. // 4x4 block with a certain number of bytes.
  680. case PF_DXT1:
  681. return ((width+3)/4)*((height+3)/4)*8 * depth;
  682. case PF_DXT2:
  683. case PF_DXT3:
  684. case PF_DXT4:
  685. case PF_DXT5:
  686. return ((width+3)/4)*((height+3)/4)*16 * depth;
  687. // Size calculations from the PVRTC OpenGL extension spec
  688. // http://www.khronos.org/registry/gles/extensions/IMG/IMG_texture_compression_pvrtc.txt
  689. // Basically, 32 bytes is the minimum texture size. Smaller textures are padded up to 32 bytes
  690. case PF_PVRTC_RGB2:
  691. case PF_PVRTC_RGBA2:
  692. assert(depth == 1);
  693. return (std::max((int)width, 16) * std::max((int)height, 8) * 2 + 7) / 8;
  694. case PF_PVRTC_RGB4:
  695. case PF_PVRTC_RGBA4:
  696. assert(depth == 1);
  697. return (std::max((int)width, 8) * std::max((int)height, 8) * 4 + 7) / 8;
  698. default:
  699. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Invalid compressed pixel format",
  700. "PixelUtil::getMemorySize");
  701. }
  702. }
  703. else
  704. {
  705. return width*height*depth*getNumElemBytes(format);
  706. }
  707. }
  708. //-----------------------------------------------------------------------
  709. size_t PixelUtil::getNumElemBits( PixelFormat format )
  710. {
  711. return getDescriptionFor(format).elemBytes * 8;
  712. }
  713. //-----------------------------------------------------------------------
  714. unsigned int PixelUtil::getFlags( PixelFormat format )
  715. {
  716. return getDescriptionFor(format).flags;
  717. }
  718. //-----------------------------------------------------------------------
  719. bool PixelUtil::hasAlpha(PixelFormat format)
  720. {
  721. return (PixelUtil::getFlags(format) & PFF_HASALPHA) > 0;
  722. }
  723. //-----------------------------------------------------------------------
  724. bool PixelUtil::isFloatingPoint(PixelFormat format)
  725. {
  726. return (PixelUtil::getFlags(format) & PFF_FLOAT) > 0;
  727. }
  728. //-----------------------------------------------------------------------
  729. bool PixelUtil::isCompressed(PixelFormat format)
  730. {
  731. return (PixelUtil::getFlags(format) & PFF_COMPRESSED) > 0;
  732. }
  733. //-----------------------------------------------------------------------
  734. bool PixelUtil::isDepth(PixelFormat format)
  735. {
  736. return (PixelUtil::getFlags(format) & PFF_DEPTH) > 0;
  737. }
  738. //-----------------------------------------------------------------------
  739. bool PixelUtil::isNativeEndian(PixelFormat format)
  740. {
  741. return (PixelUtil::getFlags(format) & PFF_NATIVEENDIAN) > 0;
  742. }
  743. //-----------------------------------------------------------------------
  744. bool PixelUtil::isLuminance(PixelFormat format)
  745. {
  746. return (PixelUtil::getFlags(format) & PFF_LUMINANCE) > 0;
  747. }
  748. //-----------------------------------------------------------------------
  749. bool PixelUtil::isValidExtent(size_t width, size_t height, size_t depth, PixelFormat format)
  750. {
  751. if(isCompressed(format))
  752. {
  753. switch(format)
  754. {
  755. case PF_DXT1:
  756. case PF_DXT2:
  757. case PF_DXT3:
  758. case PF_DXT4:
  759. case PF_DXT5:
  760. return ((width&3)==0 && (height&3)==0 && depth==1);
  761. default:
  762. return true;
  763. }
  764. }
  765. else
  766. {
  767. return true;
  768. }
  769. }
  770. //-----------------------------------------------------------------------
  771. void PixelUtil::getBitDepths(PixelFormat format, int rgba[4])
  772. {
  773. const PixelFormatDescription &des = getDescriptionFor(format);
  774. rgba[0] = des.rbits;
  775. rgba[1] = des.gbits;
  776. rgba[2] = des.bbits;
  777. rgba[3] = des.abits;
  778. }
  779. //-----------------------------------------------------------------------
  780. void PixelUtil::getBitMasks(PixelFormat format, uint32 rgba[4])
  781. {
  782. const PixelFormatDescription &des = getDescriptionFor(format);
  783. rgba[0] = des.rmask;
  784. rgba[1] = des.gmask;
  785. rgba[2] = des.bmask;
  786. rgba[3] = des.amask;
  787. }
  788. //---------------------------------------------------------------------
  789. void PixelUtil::getBitShifts(PixelFormat format, unsigned char rgba[4])
  790. {
  791. const PixelFormatDescription &des = getDescriptionFor(format);
  792. rgba[0] = des.rshift;
  793. rgba[1] = des.gshift;
  794. rgba[2] = des.bshift;
  795. rgba[3] = des.ashift;
  796. }
  797. //-----------------------------------------------------------------------
  798. String PixelUtil::getFormatName(PixelFormat srcformat)
  799. {
  800. return getDescriptionFor(srcformat).name;
  801. }
  802. //-----------------------------------------------------------------------
  803. bool PixelUtil::isAccessible(PixelFormat srcformat)
  804. {
  805. if (srcformat == PF_UNKNOWN)
  806. return false;
  807. unsigned int flags = getFlags(srcformat);
  808. return !((flags & PFF_COMPRESSED) || (flags & PFF_DEPTH));
  809. }
  810. //-----------------------------------------------------------------------
  811. PixelComponentType PixelUtil::getComponentType(PixelFormat fmt)
  812. {
  813. const PixelFormatDescription &des = getDescriptionFor(fmt);
  814. return des.componentType;
  815. }
  816. //-----------------------------------------------------------------------
  817. size_t PixelUtil::getComponentCount(PixelFormat fmt)
  818. {
  819. const PixelFormatDescription &des = getDescriptionFor(fmt);
  820. return des.componentCount;
  821. }
  822. //-----------------------------------------------------------------------
  823. PixelFormat PixelUtil::getFormatFromName(const String& name, bool accessibleOnly, bool caseSensitive)
  824. {
  825. String tmp = name;
  826. if (!caseSensitive)
  827. {
  828. // We are stored upper-case format names.
  829. StringUtil::toUpperCase(tmp);
  830. }
  831. for (int i = 0; i < PF_COUNT; ++i)
  832. {
  833. PixelFormat pf = static_cast<PixelFormat>(i);
  834. if (!accessibleOnly || isAccessible(pf))
  835. {
  836. if (tmp == getFormatName(pf))
  837. return pf;
  838. }
  839. }
  840. return PF_UNKNOWN;
  841. }
  842. //-----------------------------------------------------------------------
  843. String PixelUtil::getBNFExpressionOfPixelFormats(bool accessibleOnly)
  844. {
  845. // Collect format names sorted by length, it's required by BNF compiler
  846. // that similar tokens need longer ones comes first.
  847. typedef multimap<String::size_type, String>::type FormatNameMap;
  848. FormatNameMap formatNames;
  849. for (size_t i = 0; i < PF_COUNT; ++i)
  850. {
  851. PixelFormat pf = static_cast<PixelFormat>(i);
  852. if (!accessibleOnly || isAccessible(pf))
  853. {
  854. String formatName = getFormatName(pf);
  855. formatNames.insert(std::make_pair(formatName.length(), formatName));
  856. }
  857. }
  858. // Populate the BNF expression in reverse order
  859. String result;
  860. // Note: Stupid M$ VC7.1 can't dealing operator!= with FormatNameMap::const_reverse_iterator.
  861. for (FormatNameMap::reverse_iterator j = formatNames.rbegin(); j != formatNames.rend(); ++j)
  862. {
  863. if (!result.empty())
  864. result += " | ";
  865. result += "'" + j->second + "'";
  866. }
  867. return result;
  868. }
  869. //-----------------------------------------------------------------------
  870. PixelFormat PixelUtil::getFormatForBitDepths(PixelFormat fmt, ushort integerBits, ushort floatBits)
  871. {
  872. switch (integerBits)
  873. {
  874. case 16:
  875. switch (fmt)
  876. {
  877. case PF_R8G8B8:
  878. case PF_X8R8G8B8:
  879. return PF_R5G6B5;
  880. case PF_B8G8R8:
  881. case PF_X8B8G8R8:
  882. return PF_B5G6R5;
  883. case PF_A8R8G8B8:
  884. case PF_R8G8B8A8:
  885. case PF_A8B8G8R8:
  886. case PF_B8G8R8A8:
  887. return PF_A4R4G4B4;
  888. case PF_A2R10G10B10:
  889. case PF_A2B10G10R10:
  890. return PF_A1R5G5B5;
  891. default:
  892. // use original image format
  893. break;
  894. }
  895. break;
  896. case 32:
  897. switch (fmt)
  898. {
  899. case PF_R5G6B5:
  900. return PF_X8R8G8B8;
  901. case PF_B5G6R5:
  902. return PF_X8B8G8R8;
  903. case PF_A4R4G4B4:
  904. return PF_A8R8G8B8;
  905. case PF_A1R5G5B5:
  906. return PF_A2R10G10B10;
  907. default:
  908. // use original image format
  909. break;
  910. }
  911. break;
  912. default:
  913. // use original image format
  914. break;
  915. }
  916. switch (floatBits)
  917. {
  918. case 16:
  919. switch (fmt)
  920. {
  921. case PF_FLOAT32_R:
  922. return PF_FLOAT16_R;
  923. case PF_FLOAT32_RGB:
  924. return PF_FLOAT16_RGB;
  925. case PF_FLOAT32_RGBA:
  926. return PF_FLOAT16_RGBA;
  927. default:
  928. // use original image format
  929. break;
  930. }
  931. break;
  932. case 32:
  933. switch (fmt)
  934. {
  935. case PF_FLOAT16_R:
  936. return PF_FLOAT32_R;
  937. case PF_FLOAT16_RGB:
  938. return PF_FLOAT32_RGB;
  939. case PF_FLOAT16_RGBA:
  940. return PF_FLOAT32_RGBA;
  941. default:
  942. // use original image format
  943. break;
  944. }
  945. break;
  946. default:
  947. // use original image format
  948. break;
  949. }
  950. return fmt;
  951. }
  952. //-----------------------------------------------------------------------
  953. /*************************************************************************
  954. * Pixel packing/unpacking utilities
  955. */
  956. void PixelUtil::packColour(const ColourValue &colour, const PixelFormat pf, void* dest)
  957. {
  958. packColour(colour.r, colour.g, colour.b, colour.a, pf, dest);
  959. }
  960. //-----------------------------------------------------------------------
  961. void PixelUtil::packColour(const uint8 r, const uint8 g, const uint8 b, const uint8 a, const PixelFormat pf, void* dest)
  962. {
  963. const PixelFormatDescription &des = getDescriptionFor(pf);
  964. if(des.flags & PFF_NATIVEENDIAN) {
  965. // Shortcut for integer formats packing
  966. unsigned int value = ((Bitwise::fixedToFixed(r, 8, des.rbits)<<des.rshift) & des.rmask) |
  967. ((Bitwise::fixedToFixed(g, 8, des.gbits)<<des.gshift) & des.gmask) |
  968. ((Bitwise::fixedToFixed(b, 8, des.bbits)<<des.bshift) & des.bmask) |
  969. ((Bitwise::fixedToFixed(a, 8, des.abits)<<des.ashift) & des.amask);
  970. // And write to memory
  971. Bitwise::intWrite(dest, des.elemBytes, value);
  972. } else {
  973. // Convert to float
  974. packColour((float)r/255.0f,(float)g/255.0f,(float)b/255.0f,(float)a/255.0f, pf, dest);
  975. }
  976. }
  977. //-----------------------------------------------------------------------
  978. void PixelUtil::packColour(const float r, const float g, const float b, const float a, const PixelFormat pf, void* dest)
  979. {
  980. // Catch-it-all here
  981. const PixelFormatDescription &des = getDescriptionFor(pf);
  982. if(des.flags & PFF_NATIVEENDIAN) {
  983. // Do the packing
  984. //std::cerr << dest << " " << r << " " << g << " " << b << " " << a << std::endl;
  985. const unsigned int value = ((Bitwise::floatToFixed(r, des.rbits)<<des.rshift) & des.rmask) |
  986. ((Bitwise::floatToFixed(g, des.gbits)<<des.gshift) & des.gmask) |
  987. ((Bitwise::floatToFixed(b, des.bbits)<<des.bshift) & des.bmask) |
  988. ((Bitwise::floatToFixed(a, des.abits)<<des.ashift) & des.amask);
  989. // And write to memory
  990. Bitwise::intWrite(dest, des.elemBytes, value);
  991. } else {
  992. switch(pf)
  993. {
  994. case PF_FLOAT32_R:
  995. ((float*)dest)[0] = r;
  996. break;
  997. case PF_FLOAT32_GR:
  998. ((float*)dest)[0] = g;
  999. ((float*)dest)[1] = r;
  1000. break;
  1001. case PF_FLOAT32_RGB:
  1002. ((float*)dest)[0] = r;
  1003. ((float*)dest)[1] = g;
  1004. ((float*)dest)[2] = b;
  1005. break;
  1006. case PF_FLOAT32_RGBA:
  1007. ((float*)dest)[0] = r;
  1008. ((float*)dest)[1] = g;
  1009. ((float*)dest)[2] = b;
  1010. ((float*)dest)[3] = a;
  1011. break;
  1012. case PF_FLOAT16_R:
  1013. ((uint16*)dest)[0] = Bitwise::floatToHalf(r);
  1014. break;
  1015. case PF_FLOAT16_GR:
  1016. ((uint16*)dest)[0] = Bitwise::floatToHalf(g);
  1017. ((uint16*)dest)[1] = Bitwise::floatToHalf(r);
  1018. break;
  1019. case PF_FLOAT16_RGB:
  1020. ((uint16*)dest)[0] = Bitwise::floatToHalf(r);
  1021. ((uint16*)dest)[1] = Bitwise::floatToHalf(g);
  1022. ((uint16*)dest)[2] = Bitwise::floatToHalf(b);
  1023. break;
  1024. case PF_FLOAT16_RGBA:
  1025. ((uint16*)dest)[0] = Bitwise::floatToHalf(r);
  1026. ((uint16*)dest)[1] = Bitwise::floatToHalf(g);
  1027. ((uint16*)dest)[2] = Bitwise::floatToHalf(b);
  1028. ((uint16*)dest)[3] = Bitwise::floatToHalf(a);
  1029. break;
  1030. case PF_SHORT_RGB:
  1031. ((uint16*)dest)[0] = (uint16)Bitwise::floatToFixed(r, 16);
  1032. ((uint16*)dest)[1] = (uint16)Bitwise::floatToFixed(g, 16);
  1033. ((uint16*)dest)[2] = (uint16)Bitwise::floatToFixed(b, 16);
  1034. break;
  1035. case PF_SHORT_RGBA:
  1036. ((uint16*)dest)[0] = (uint16)Bitwise::floatToFixed(r, 16);
  1037. ((uint16*)dest)[1] = (uint16)Bitwise::floatToFixed(g, 16);
  1038. ((uint16*)dest)[2] = (uint16)Bitwise::floatToFixed(b, 16);
  1039. ((uint16*)dest)[3] = (uint16)Bitwise::floatToFixed(a, 16);
  1040. break;
  1041. case PF_BYTE_LA:
  1042. ((uint8*)dest)[0] = (uint8)Bitwise::floatToFixed(r, 8);
  1043. ((uint8*)dest)[1] = (uint8)Bitwise::floatToFixed(a, 8);
  1044. break;
  1045. default:
  1046. // Not yet supported
  1047. OGRE_EXCEPT(
  1048. Exception::ERR_NOT_IMPLEMENTED,
  1049. "pack to "+getFormatName(pf)+" not implemented",
  1050. "PixelUtil::packColour");
  1051. break;
  1052. }
  1053. }
  1054. }
  1055. //-----------------------------------------------------------------------
  1056. void PixelUtil::unpackColour(ColourValue *colour, PixelFormat pf, const void* src)
  1057. {
  1058. unpackColour(&colour->r, &colour->g, &colour->b, &colour->a, pf, src);
  1059. }
  1060. //-----------------------------------------------------------------------
  1061. void PixelUtil::unpackColour(uint8 *r, uint8 *g, uint8 *b, uint8 *a, PixelFormat pf, const void* src)
  1062. {
  1063. const PixelFormatDescription &des = getDescriptionFor(pf);
  1064. if(des.flags & PFF_NATIVEENDIAN) {
  1065. // Shortcut for integer formats unpacking
  1066. const unsigned int value = Bitwise::intRead(src, des.elemBytes);
  1067. if(des.flags & PFF_LUMINANCE)
  1068. {
  1069. // Luminance format -- only rbits used
  1070. *r = *g = *b = (uint8)Bitwise::fixedToFixed(
  1071. (value & des.rmask)>>des.rshift, des.rbits, 8);
  1072. }
  1073. else
  1074. {
  1075. *r = (uint8)Bitwise::fixedToFixed((value & des.rmask)>>des.rshift, des.rbits, 8);
  1076. *g = (uint8)Bitwise::fixedToFixed((value & des.gmask)>>des.gshift, des.gbits, 8);
  1077. *b = (uint8)Bitwise::fixedToFixed((value & des.bmask)>>des.bshift, des.bbits, 8);
  1078. }
  1079. if(des.flags & PFF_HASALPHA)
  1080. {
  1081. *a = (uint8)Bitwise::fixedToFixed((value & des.amask)>>des.ashift, des.abits, 8);
  1082. }
  1083. else
  1084. {
  1085. *a = 255; // No alpha, default a component to full
  1086. }
  1087. } else {
  1088. // Do the operation with the more generic floating point
  1089. float rr, gg, bb, aa;
  1090. unpackColour(&rr,&gg,&bb,&aa, pf, src);
  1091. *r = (uint8)Bitwise::floatToFixed(rr, 8);
  1092. *g = (uint8)Bitwise::floatToFixed(gg, 8);
  1093. *b = (uint8)Bitwise::floatToFixed(bb, 8);
  1094. *a = (uint8)Bitwise::floatToFixed(aa, 8);
  1095. }
  1096. }
  1097. //-----------------------------------------------------------------------
  1098. void PixelUtil::unpackColour(float *r, float *g, float *b, float *a,
  1099. PixelFormat pf, const void* src)
  1100. {
  1101. const PixelFormatDescription &des = getDescriptionFor(pf);
  1102. if(des.flags & PFF_NATIVEENDIAN) {
  1103. // Shortcut for integer formats unpacking
  1104. const unsigned int value = Bitwise::intRead(src, des.elemBytes);
  1105. if(des.flags & PFF_LUMINANCE)
  1106. {
  1107. // Luminance format -- only rbits used
  1108. *r = *g = *b = Bitwise::fixedToFloat(
  1109. (value & des.rmask)>>des.rshift, des.rbits);
  1110. }
  1111. else
  1112. {
  1113. *r = Bitwise::fixedToFloat((value & des.rmask)>>des.rshift, des.rbits);
  1114. *g = Bitwise::fixedToFloat((value & des.gmask)>>des.gshift, des.gbits);
  1115. *b = Bitwise::fixedToFloat((value & des.bmask)>>des.bshift, des.bbits);
  1116. }
  1117. if(des.flags & PFF_HASALPHA)
  1118. {
  1119. *a = Bitwise::fixedToFloat((value & des.amask)>>des.ashift, des.abits);
  1120. }
  1121. else
  1122. {
  1123. *a = 1.0f; // No alpha, default a component to full
  1124. }
  1125. } else {
  1126. switch(pf)
  1127. {
  1128. case PF_FLOAT32_R:
  1129. *r = *g = *b = ((float*)src)[0];
  1130. *a = 1.0f;
  1131. break;
  1132. case PF_FLOAT32_GR:
  1133. *g = ((float*)src)[0];
  1134. *r = *b = ((float*)src)[1];
  1135. *a = 1.0f;
  1136. break;
  1137. case PF_FLOAT32_RGB:
  1138. *r = ((float*)src)[0];
  1139. *g = ((float*)src)[1];
  1140. *b = ((float*)src)[2];
  1141. *a = 1.0f;
  1142. break;
  1143. case PF_FLOAT32_RGBA:
  1144. *r = ((float*)src)[0];
  1145. *g = ((float*)src)[1];
  1146. *b = ((float*)src)[2];
  1147. *a = ((float*)src)[3];
  1148. break;
  1149. case PF_FLOAT16_R:
  1150. *r = *g = *b = Bitwise::halfToFloat(((uint16*)src)[0]);
  1151. *a = 1.0f;
  1152. break;
  1153. case PF_FLOAT16_GR:
  1154. *g = Bitwise::halfToFloat(((uint16*)src)[0]);
  1155. *r = *b = Bitwise::halfToFloat(((uint16*)src)[1]);
  1156. *a = 1.0f;
  1157. break;
  1158. case PF_FLOAT16_RGB:
  1159. *r = Bitwise::halfToFloat(((uint16*)src)[0]);
  1160. *g = Bitwise::halfToFloat(((uint16*)src)[1]);
  1161. *b = Bitwise::halfToFloat(((uint16*)src)[2]);
  1162. *a = 1.0f;
  1163. break;
  1164. case PF_FLOAT16_RGBA:
  1165. *r = Bitwise::halfToFloat(((uint16*)src)[0]);
  1166. *g = Bitwise::halfToFloat(((uint16*)src)[1]);
  1167. *b = Bitwise::halfToFloat(((uint16*)src)[2]);
  1168. *a = Bitwise::halfToFloat(((uint16*)src)[3]);
  1169. break;
  1170. case PF_SHORT_RGB:
  1171. *r = Bitwise::fixedToFloat(((uint16*)src)[0], 16);
  1172. *g = Bitwise::fixedToFloat(((uint16*)src)[1], 16);
  1173. *b = Bitwise::fixedToFloat(((uint16*)src)[2], 16);
  1174. *a = 1.0f;
  1175. break;
  1176. case PF_SHORT_RGBA:
  1177. *r = Bitwise::fixedToFloat(((uint16*)src)[0], 16);
  1178. *g = Bitwise::fixedToFloat(((uint16*)src)[1], 16);
  1179. *b = Bitwise::fixedToFloat(((uint16*)src)[2], 16);
  1180. *a = Bitwise::fixedToFloat(((uint16*)src)[3], 16);
  1181. break;
  1182. case PF_BYTE_LA:
  1183. *r = *g = *b = Bitwise::fixedToFloat(((uint8*)src)[0], 8);
  1184. *a = Bitwise::fixedToFloat(((uint8*)src)[1], 8);
  1185. break;
  1186. default:
  1187. // Not yet supported
  1188. OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED,
  1189. "unpack from "+getFormatName(pf)+" not implemented",
  1190. "PixelUtil::unpackColour");
  1191. break;
  1192. }
  1193. }
  1194. }
  1195. //-----------------------------------------------------------------------
  1196. /* Convert pixels from one format to another */
  1197. void PixelUtil::bulkPixelConversion(void *srcp, PixelFormat srcFormat,
  1198. void *destp, PixelFormat dstFormat, unsigned int count)
  1199. {
  1200. PixelBox src(count, 1, 1, srcFormat, srcp),
  1201. dst(count, 1, 1, dstFormat, destp);
  1202. bulkPixelConversion(src, dst);
  1203. }
  1204. //-----------------------------------------------------------------------
  1205. void PixelUtil::bulkPixelConversion(const PixelBox &src, const PixelBox &dst)
  1206. {
  1207. assert(src.getWidth() == dst.getWidth() &&
  1208. src.getHeight() == dst.getHeight() &&
  1209. src.getDepth() == dst.getDepth());
  1210. // Check for compressed formats, we don't support decompression, compression or recoding
  1211. if(PixelUtil::isCompressed(src.format) || PixelUtil::isCompressed(dst.format))
  1212. {
  1213. if(src.format == dst.format)
  1214. {
  1215. memcpy(dst.data, src.data, src.getConsecutiveSize());
  1216. return;
  1217. }
  1218. else
  1219. {
  1220. OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED,
  1221. "This method can not be used to compress or decompress images",
  1222. "PixelUtil::bulkPixelConversion");
  1223. }
  1224. }
  1225. // The easy case
  1226. if(src.format == dst.format) {
  1227. // Everything consecutive?
  1228. if(src.isConsecutive() && dst.isConsecutive())
  1229. {
  1230. memcpy(dst.data, src.data, src.getConsecutiveSize());
  1231. return;
  1232. }
  1233. const size_t srcPixelSize = PixelUtil::getNumElemBytes(src.format);
  1234. const size_t dstPixelSize = PixelUtil::getNumElemBytes(dst.format);
  1235. uint8 *srcptr = static_cast<uint8*>(src.data)
  1236. + (src.left + src.top * src.rowPitch + src.front * src.slicePitch) * srcPixelSize;
  1237. uint8 *dstptr = static_cast<uint8*>(dst.data)
  1238. + (dst.left + dst.top * dst.rowPitch + dst.front * dst.slicePitch) * dstPixelSize;
  1239. // Calculate pitches+skips in bytes
  1240. const size_t srcRowPitchBytes = src.rowPitch*srcPixelSize;
  1241. //const size_t srcRowSkipBytes = src.getRowSkip()*srcPixelSize;
  1242. const size_t srcSliceSkipBytes = src.getSliceSkip()*srcPixelSize;
  1243. const size_t dstRowPitchBytes = dst.rowPitch*dstPixelSize;
  1244. //const size_t dstRowSkipBytes = dst.getRowSkip()*dstPixelSize;
  1245. const size_t dstSliceSkipBytes = dst.getSliceSkip()*dstPixelSize;
  1246. // Otherwise, copy per row
  1247. const size_t rowSize = src.getWidth()*srcPixelSize;
  1248. for(size_t z=src.front; z<src.back; z++)
  1249. {
  1250. for(size_t y=src.top; y<src.bottom; y++)
  1251. {
  1252. memcpy(dstptr, srcptr, rowSize);
  1253. srcptr += srcRowPitchBytes;
  1254. dstptr += dstRowPitchBytes;
  1255. }
  1256. srcptr += srcSliceSkipBytes;
  1257. dstptr += dstSliceSkipBytes;
  1258. }
  1259. return;
  1260. }
  1261. // Converting to PF_X8R8G8B8 is exactly the same as converting to
  1262. // PF_A8R8G8B8. (same with PF_X8B8G8R8 and PF_A8B8G8R8)
  1263. if(dst.format == PF_X8R8G8B8 || dst.format == PF_X8B8G8R8)
  1264. {
  1265. // Do the same conversion, with PF_A8R8G8B8, which has a lot of
  1266. // optimized conversions
  1267. PixelBox tempdst = dst;
  1268. tempdst.format = dst.format==PF_X8R8G8B8?PF_A8R8G8B8:PF_A8B8G8R8;
  1269. bulkPixelConversion(src, tempdst);
  1270. return;
  1271. }
  1272. // Converting from PF_X8R8G8B8 is exactly the same as converting from
  1273. // PF_A8R8G8B8, given that the destination format does not have alpha.
  1274. if((src.format == PF_X8R8G8B8||src.format == PF_X8B8G8R8) && !hasAlpha(dst.format))
  1275. {
  1276. // Do the same conversion, with PF_A8R8G8B8, which has a lot of
  1277. // optimized conversions
  1278. PixelBox tempsrc = src;
  1279. tempsrc.format = src.format==PF_X8R8G8B8?PF_A8R8G8B8:PF_A8B8G8R8;
  1280. bulkPixelConversion(tempsrc, dst);
  1281. return;
  1282. }
  1283. const size_t srcPixelSize = PixelUtil::getNumElemBytes(src.format);
  1284. const size_t dstPixelSize = PixelUtil::getNumElemBytes(dst.format);
  1285. uint8 *srcptr = static_cast<uint8*>(src.data)
  1286. + (src.left + src.top * src.rowPitch + src.front * src.slicePitch) * srcPixelSize;
  1287. uint8 *dstptr = static_cast<uint8*>(dst.data)
  1288. + (dst.left + dst.top * dst.rowPitch + dst.front * dst.slicePitch) * dstPixelSize;
  1289. // Old way, not taking into account box dimensions
  1290. //uint8 *srcptr = static_cast<uint8*>(src.data), *dstptr = static_cast<uint8*>(dst.data);
  1291. // Calculate pitches+skips in bytes
  1292. const size_t srcRowSkipBytes = src.getRowSkip()*srcPixelSize;
  1293. const size_t srcSliceSkipBytes = src.getSliceSkip()*srcPixelSize;
  1294. const size_t dstRowSkipBytes = dst.getRowSkip()*dstPixelSize;
  1295. const size_t dstSliceSkipBytes = dst.getSliceSkip()*dstPixelSize;
  1296. // The brute force fallback
  1297. float r,g,b,a;
  1298. for(size_t z=src.front; z<src.back; z++)
  1299. {
  1300. for(size_t y=src.top; y<src.bottom; y++)
  1301. {
  1302. for(size_t x=src.left; x<src.right; x++)
  1303. {
  1304. unpackColour(&r, &g, &b, &a, src.format, srcptr);
  1305. packColour(r, g, b, a, dst.format, dstptr);
  1306. srcptr += srcPixelSize;
  1307. dstptr += dstPixelSize;
  1308. }
  1309. srcptr += srcRowSkipBytes;
  1310. dstptr += dstRowSkipBytes;
  1311. }
  1312. srcptr += srcSliceSkipBytes;
  1313. dstptr += dstSliceSkipBytes;
  1314. }
  1315. }
  1316. void PixelUtil::scale(const PixelBox &src, const PixelBox &scaled, Filter filter)
  1317. {
  1318. assert(PixelUtil::isAccessible(src.format));
  1319. assert(PixelUtil::isAccessible(scaled.format));
  1320. PixelBox temp;
  1321. switch (filter)
  1322. {
  1323. default:
  1324. case FILTER_NEAREST:
  1325. if(src.format == scaled.format)
  1326. {
  1327. // No intermediate buffer needed
  1328. temp = scaled;
  1329. }
  1330. else
  1331. {
  1332. // Allocate temporary buffer of destination size in source format
  1333. temp = PixelBox(scaled.getWidth(), scaled.getHeight(), scaled.getDepth(), src.format);
  1334. temp.data = OGRE_MALLOC(temp.getConsecutiveSize(), 0);
  1335. }
  1336. // super-optimized: no conversion
  1337. switch (PixelUtil::getNumElemBytes(src.format))
  1338. {
  1339. case 1: NearestResampler<1>::scale(src, temp); break;
  1340. case 2: NearestResampler<2>::scale(src, temp); break;
  1341. case 3: NearestResampler<3>::scale(src, temp); break;
  1342. case 4: NearestResampler<4>::scale(src, temp); break;
  1343. case 6: NearestResampler<6>::scale(src, temp); break;
  1344. case 8: NearestResampler<8>::scale(src, temp); break;
  1345. case 12: NearestResampler<12>::scale(src, temp); break;
  1346. case 16: NearestResampler<16>::scale(src, temp); break;
  1347. default:
  1348. // never reached
  1349. assert(false);
  1350. }
  1351. if(temp.data != scaled.data)
  1352. {
  1353. // Blit temp buffer
  1354. PixelUtil::bulkPixelConversion(temp, scaled);
  1355. OGRE_FREE(temp.data, 0);
  1356. }
  1357. break;
  1358. case FILTER_LINEAR:
  1359. case FILTER_BILINEAR:
  1360. switch (src.format)
  1361. {
  1362. case PF_L8: case PF_A8: case PF_BYTE_LA:
  1363. case PF_R8G8B8: case PF_B8G8R8:
  1364. case PF_R8G8B8A8: case PF_B8G8R8A8:
  1365. case PF_A8B8G8R8: case PF_A8R8G8B8:
  1366. case PF_X8B8G8R8: case PF_X8R8G8B8:
  1367. if(src.format == scaled.format)
  1368. {
  1369. // No intermediate buffer needed
  1370. temp = scaled;
  1371. }
  1372. else
  1373. {
  1374. // Allocate temp buffer of destination size in source format
  1375. temp = PixelBox(scaled.getWidth(), scaled.getHeight(), scaled.getDepth(), src.format);
  1376. temp.data = OGRE_MALLOC(temp.getConsecutiveSize(), 0);
  1377. }
  1378. // super-optimized: byte-oriented math, no conversion
  1379. switch (PixelUtil::getNumElemBytes(src.format))
  1380. {
  1381. case 1: LinearResampler_Byte<1>::scale(src, temp); break;
  1382. case 2: LinearResampler_Byte<2>::scale(src, temp); break;
  1383. case 3: LinearResampler_Byte<3>::scale(src, temp); break;
  1384. case 4: LinearResampler_Byte<4>::scale(src, temp); break;
  1385. default:
  1386. // never reached
  1387. assert(false);
  1388. }
  1389. if(temp.data != scaled.data)
  1390. {
  1391. // Blit temp buffer
  1392. PixelUtil::bulkPixelConversion(temp, scaled);
  1393. OGRE_FREE(temp.data, 0);
  1394. }
  1395. break;
  1396. case PF_FLOAT32_RGB:
  1397. case PF_FLOAT32_RGBA:
  1398. if (scaled.format == PF_FLOAT32_RGB || scaled.format == PF_FLOAT32_RGBA)
  1399. {
  1400. // float32 to float32, avoid unpack/repack overhead
  1401. LinearResampler_Float32::scale(src, scaled);
  1402. break;
  1403. }
  1404. // else, fall through
  1405. default:
  1406. // non-optimized: floating-point math, performs conversion but always works
  1407. LinearResampler::scale(src, scaled);
  1408. }
  1409. break;
  1410. }
  1411. }
  1412. ColourValue PixelBox::getColourAt(size_t x, size_t y, size_t z)
  1413. {
  1414. ColourValue cv;
  1415. unsigned char pixelSize = PixelUtil::getNumElemBytes(format);
  1416. size_t pixelOffset = pixelSize * (z * slicePitch + y * rowPitch + x);
  1417. PixelUtil::unpackColour(&cv, format, (unsigned char *)data + pixelOffset);
  1418. return cv;
  1419. }
  1420. void PixelBox::setColourAt(ColourValue const &cv, size_t x, size_t y, size_t z)
  1421. {
  1422. unsigned char pixelSize = PixelUtil::getNumElemBytes(format);
  1423. size_t pixelOffset = pixelSize * (z * slicePitch + y * rowPitch + x);
  1424. PixelUtil::packColour(cv, format, (unsigned char *)data + pixelOffset);
  1425. }
  1426. }