PSDReader.cpp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071
  1. #include "PSDReader.h"
  2. #include "FileStream.h"
  3. #include "BFApp.h"
  4. #include "img/ImageData.h"
  5. #include "gfx/Texture.h"
  6. #include "gfx/RenderDevice.h"
  7. #include "ImgEffects.h"
  8. #include "ImageUtils.h"
  9. #include "ImageAdjustments.h"
  10. #include "util/PerfTimer.h"
  11. #include <set>
  12. /*
  13. #include "agg_rendering_buffer.h"
  14. #include "agg_trans_viewport.h"
  15. #include "agg_path_storage.h"
  16. #include "agg_conv_transform.h"
  17. #include "agg_conv_curve.h"
  18. #include "agg_conv_stroke.h"
  19. #include "agg_gsv_text.h"
  20. #include "agg_scanline_u.h"
  21. #include "agg_scanline_bin.h"
  22. #include "agg_renderer_scanline.h"
  23. #include "agg_rasterizer_scanline_aa.h"
  24. #include "agg_rasterizer_compound_aa.h"
  25. #include "agg_span_allocator.h"
  26. #include "agg_gamma_lut.h"
  27. #include "agg_pixfmt_rgba.h"
  28. #include "agg_pixfmt_gray.h"
  29. #include "agg_bounding_rect.h"
  30. #include "agg_color_gray.h"
  31. #include "agg_ellipse.h"
  32. #include "agg_scanline_u.h"
  33. #include "agg_scanline_p.h"
  34. */
  35. USING_NS_BF;
  36. PSDValue::PSDValue()
  37. {
  38. mType = PSDVal_None;
  39. mDescriptor = NULL;
  40. mList = NULL;
  41. mInteger = 0;
  42. mDouble = 0;
  43. }
  44. PSDValue::~PSDValue()
  45. {
  46. if (mType == PSDVal_Descriptor)
  47. delete mDescriptor;
  48. else if (mType == PSDVal_List)
  49. delete [] mList;
  50. }
  51. int32 PSDValue::GetMulticharInt()
  52. {
  53. if (mString.length() < 4)
  54. return 0;
  55. char chars [] = {(char) mString[3], (char) mString[2], (char) mString[1], (char) mString[0]};
  56. return *((int*) chars);
  57. }
  58. bool PSDDescriptor::IsEmpty()
  59. {
  60. return mPSDValueMap.empty();
  61. }
  62. bool PSDDescriptor::Contains(const StringImpl& value)
  63. {
  64. return mPSDValueMap.find(value) != mPSDValueMap.end();
  65. }
  66. PSDValue* PSDDescriptor::Get(const StringImpl& value)
  67. {
  68. static PSDValue defaultValue;
  69. if (mPSDValueMap.find(value) == mPSDValueMap.end())
  70. return &defaultValue;
  71. return &mPSDValueMap[value];
  72. }
  73. PSDDescriptor* PSDDescriptor::GetDescriptor(const StringImpl& value)
  74. {
  75. static PSDDescriptor defaultDescriptor;
  76. if (mPSDValueMap.find(value) == mPSDValueMap.end())
  77. return &defaultDescriptor;
  78. PSDValue* aValue = &mPSDValueMap[value];
  79. if (aValue == NULL)
  80. return &defaultDescriptor;
  81. return aValue->mDescriptor;
  82. }
  83. ///
  84. PSDPattern::PSDPattern()
  85. {
  86. mIntensityBits = NULL;
  87. mNextMipLevel = NULL;
  88. }
  89. PSDPattern::~PSDPattern()
  90. {
  91. delete mIntensityBits;
  92. delete mNextMipLevel;
  93. }
  94. PSDPattern* PSDPattern::GetNextMipLevel()
  95. {
  96. PSDPattern* mip = new PSDPattern();
  97. int mw = (mWidth + 1) / 2;
  98. int mh = (mHeight + 1) / 2;
  99. mip->mWidth = mw;
  100. mip->mHeight = mh;
  101. mip->mBits = new uint32[mw*mh];
  102. mip->mIntensityBits = new uint8[mw*mh];
  103. for (int y = 0; y < mh; y++)
  104. {
  105. uint8* destI = mip->mIntensityBits + y*mw;
  106. uint8* srcI1 = mIntensityBits + y*2*mWidth;
  107. uint8* srcI2 = (y + 1 >= mh) ? mIntensityBits : (srcI1 + mWidth);
  108. for (int x = 0; x < mw; x++)
  109. {
  110. int srcX1 = x*2;
  111. int srcX2 = (srcX1+1)%mWidth;
  112. *(destI++) = (uint8) (((int) srcI1[srcX1] + (int) srcI1[srcX2] +
  113. (int) srcI2[srcX1] + (int) (srcI2[srcX2])) / 4);
  114. }
  115. }
  116. if (mBits != NULL)
  117. {
  118. for (int y = 0; y < mh; y++)
  119. {
  120. uint32* destBits = mip->mBits + y*mw;
  121. uint32* srcBits1 = mBits + y*2*mWidth;
  122. uint32* srcBits2 = (y + 1 >= mh) ? mBits : (srcBits1 + mWidth);
  123. for (int x = 0; x < mw; x++)
  124. {
  125. int srcX1 = x*2;
  126. int srcX2 = (srcX1+1)%mWidth;
  127. *(destBits++) =
  128. (((
  129. (srcBits1[srcX1] & 0x00FF00FF) +
  130. (srcBits1[srcX2] & 0x00FF00FF) +
  131. (srcBits2[srcX1] & 0x00FF00FF) +
  132. (srcBits2[srcX2] & 0x00FF00FF)) / 4) & 0x00FF00FF) |
  133. (((
  134. ((srcBits1[srcX1] >> 8) & 0x00FF00FF) +
  135. ((srcBits1[srcX2] >> 8) & 0x00FF00FF) +
  136. ((srcBits2[srcX1] >> 8) & 0x00FF00FF) +
  137. ((srcBits2[srcX2] >> 8) & 0x00FF00FF)) / 4) & 0x00FF00FF) << 8;
  138. }
  139. }
  140. }
  141. return mip;
  142. }
  143. ///
  144. PSDReader::PSDReader()
  145. {
  146. mFS = NULL;
  147. mGlobalAltitude = 0;
  148. mGlobalAngle = 30;
  149. }
  150. PSDReader::~PSDReader()
  151. {
  152. if (mFS != NULL)
  153. delete mFS;
  154. for (int i = 0; i < (int) mPSDLayerInfoVector.size(); i++)
  155. delete mPSDLayerInfoVector[i];
  156. PSDPatternMap::iterator itr = mPSDPatternMap.begin();
  157. while (itr != mPSDPatternMap.end())
  158. {
  159. delete itr->second;
  160. ++itr;
  161. }
  162. }
  163. String PSDReader::ReadIdString()
  164. {
  165. String idStr = mFS->ReadAscii32SizedString();
  166. int id = 0;
  167. if (idStr.length() == 0)
  168. {
  169. id = mFS->ReadInt32();
  170. char str[5] = {
  171. (char)((id & 0xFF000000) >> 24),
  172. (char)((id & 0x00FF0000) >> 16),
  173. (char)((id & 0x0000FF00) >> 8),
  174. (char)(id & 0x000000FF), 0};
  175. idStr = str;
  176. }
  177. return idStr;
  178. }
  179. void PSDReader::ReadPSDValue(PSDValue* value)
  180. {
  181. int aType = mFS->ReadInt32();
  182. switch (aType)
  183. {
  184. case 'obj ':
  185. {
  186. int32 numItems = mFS->ReadInt32();
  187. for (int itemIdx = 0; itemIdx < numItems; itemIdx++)
  188. {
  189. int refType = mFS->ReadInt32();
  190. switch (refType)
  191. {
  192. case 'prop':
  193. {
  194. String aName = mFS->ReadUnicode32SizedString();
  195. String classId = ReadIdString();
  196. String keyId = ReadIdString();
  197. }
  198. break;
  199. case 'Clss':
  200. {
  201. String aName = mFS->ReadUnicode32SizedString();
  202. String classId = ReadIdString();
  203. }
  204. break;
  205. case 'Enmr':
  206. {
  207. String aName = mFS->ReadUnicode32SizedString();
  208. String classId = ReadIdString();
  209. String typeId = ReadIdString();
  210. String anEnum = ReadIdString();
  211. }
  212. break;
  213. case 'rele':
  214. {
  215. String aName = mFS->ReadUnicode32SizedString();
  216. String classId = ReadIdString();
  217. int32 offset = mFS->ReadInt32();
  218. }
  219. break;
  220. case 'Idnt':
  221. {
  222. int32 ident = mFS->ReadInt32();
  223. }
  224. break;
  225. case 'indx':
  226. {
  227. int32 ident = mFS->ReadInt32();
  228. }
  229. break;
  230. case 'name':
  231. {
  232. String aName = mFS->ReadAscii8SizedString();
  233. }
  234. break;
  235. }
  236. }
  237. }
  238. break;
  239. case 'GlbO':
  240. case 'Objc':
  241. {
  242. value->mType = PSDVal_Descriptor;
  243. value->mDescriptor = new PSDDescriptor();
  244. ReadPSDDescriptor(value->mDescriptor);
  245. }
  246. break;
  247. case 'TEXT':
  248. value->mType = PSDVal_String;
  249. value->mString = mFS->ReadUnicode32SizedString();
  250. break;
  251. case 'VlLs':
  252. {
  253. value->mType = PSDVal_List;
  254. int count = mFS->ReadInt32();
  255. value->mInteger = count;
  256. value->mList = new PSDValue[count];
  257. for (int idx = 0; idx < count; idx++)
  258. ReadPSDValue(value->mList + idx);
  259. }
  260. break;
  261. case 'doub':
  262. value->mType = PSDVal_Double;
  263. value->mDouble = mFS->ReadDouble();
  264. break;
  265. case 'UntF':
  266. value->mType = PSDVal_UnitFloat;
  267. value->mInteger = mFS->ReadInt32();
  268. value->mDouble = mFS->ReadDouble();
  269. break;
  270. case 'enum':
  271. value->mType = PSDVal_KeyedString;
  272. value->mKey = ReadIdString();
  273. value->mString = ReadIdString();
  274. break;
  275. case 'long':
  276. value->mType = PSDVal_Integer;
  277. value->mInteger = mFS->ReadInt32();
  278. break;
  279. case 'bool':
  280. value->mType = PSDVal_Integer;
  281. value->mInteger = mFS->ReadInt8();
  282. break;
  283. case 'type':
  284. case 'GlbC':
  285. value->mType = PSDVal_String;
  286. value->mString = ReadIdString();
  287. break;
  288. case 'alis':
  289. {
  290. int aSize = mFS->ReadInt32();
  291. mFS->Seek(aSize);
  292. }
  293. break;
  294. case 'tdta':
  295. {
  296. int aSize = mFS->ReadInt32();
  297. mFS->Seek(aSize);
  298. }
  299. break;
  300. }
  301. }
  302. void PSDReader::ReadPSDDescriptor(PSDDescriptor* descriptor)
  303. {
  304. AutoPerf gPerf("PSDReader::ReadPSDDescriptor");
  305. PSDDescriptor* aDescriptor = descriptor;
  306. String textString;
  307. int pos = mFS->GetPos();
  308. String aName = mFS->ReadUnicode32SizedString();
  309. String classIdStr = mFS->ReadAscii32SizedString();
  310. int classIdType = 0;
  311. if (classIdStr.length() == 0)
  312. classIdType = mFS->ReadInt32();
  313. String aString;
  314. int numItems = mFS->ReadInt32();
  315. for (int itemIdx = 0; itemIdx < numItems; itemIdx++)
  316. {
  317. String keyStr = ReadIdString();
  318. PSDValue* aValue = NULL;
  319. {
  320. //AutoPerf gPerf("PSDReader::ReadPSDDescriptor - insert");
  321. aValue = &(aDescriptor->mPSDValueMap.insert(PSDValueMap::value_type(keyStr, PSDValue())).first->second);
  322. }
  323. ReadPSDValue(aValue);
  324. }
  325. }
  326. void PSDReader::ReadEffectColor(PSDDescriptor* colorDesc, uint32* color)
  327. {
  328. int vals[3];
  329. vals[0] = (int) colorDesc->Get("Rd ")->mDouble;
  330. vals[1] = (int) colorDesc->Get("Grn ")->mDouble;
  331. vals[2] = (int) colorDesc->Get("Bl ")->mDouble;
  332. *color = vals[0] | (vals[1] << 8) | (vals[2] << 16) | 0xFF000000;
  333. }
  334. bool PSDReader::ReadEffectGradient(PSDDescriptor* descriptor, ImageGradient* colorGradient)
  335. {
  336. if (descriptor->Contains("Grad"))
  337. {
  338. PSDDescriptor* gradDesc = descriptor->GetDescriptor("Grad");
  339. int gridF = gradDesc->Get("GrdF")->GetMulticharInt(); // CstS
  340. double intr = gradDesc->Get("Intr")->mDouble;
  341. int trns = gradDesc->Get("Trns")->mInteger;
  342. PSDValue* colors = gradDesc->Get("Clrs");
  343. for (int colorIdx = 0; colorIdx < colors->mInteger; colorIdx++)
  344. {
  345. PSDDescriptor* colorDataDesc = colors->mList[colorIdx].mDescriptor;
  346. PSDDescriptor* aColorDesc = colorDataDesc->GetDescriptor("Clr ");
  347. int vals[3];
  348. vals[0] = (int) aColorDesc->Get("Rd ")->mDouble;
  349. vals[1] = (int) aColorDesc->Get("Grn ")->mDouble;
  350. vals[2] = (int) aColorDesc->Get("Bl ")->mDouble;
  351. int loc = colorDataDesc->Get("Lctn")->mInteger;
  352. int mdPn = colorDataDesc->Get("Mdpn")->mInteger;
  353. int aType = colorDataDesc->Get("Type")->GetMulticharInt();
  354. for (int i = 0; i < 3; i++)
  355. {
  356. ImageGradientPoint pt;
  357. pt.mX = (float) loc;
  358. pt.mValue = vals[i];
  359. if ((colorGradient[i].mPoints.size() != 0) && (colorGradient[i].mPoints.back().mX == pt.mX))
  360. colorGradient[i].mPoints.back() = pt;
  361. else
  362. colorGradient[i].mPoints.push_back(pt);
  363. }
  364. }
  365. for (int i = 0; i < 4; i++)
  366. {
  367. colorGradient[i].mXSize = 4096;
  368. colorGradient[i].mSmoothness = (float) intr / 4096.0f;
  369. }
  370. PSDValue* transDesc = gradDesc->Get("Trns");
  371. for (int transIdx = 0; transIdx < transDesc->mInteger; transIdx++)
  372. {
  373. ImageGradientPoint pt;
  374. PSDDescriptor* transDataDesc = transDesc->mList[transIdx].mDescriptor;
  375. int loc = transDataDesc->Get("Lctn")->mInteger;
  376. double opacity = transDataDesc->Get("Opct")->mDouble;
  377. pt.mX = (float) loc;
  378. pt.mValue = (int) (opacity * 255 / 100.0f + 0.5f);
  379. colorGradient[3].mPoints.push_back(pt);
  380. }
  381. return true;
  382. }
  383. else
  384. {
  385. PSDDescriptor* aColorDesc = descriptor->GetDescriptor("Clr ");
  386. int vals[4];
  387. vals[0] = (int) aColorDesc->Get("Rd ")->mDouble;
  388. vals[1] = (int) aColorDesc->Get("Grn ")->mDouble;
  389. vals[2] = (int) aColorDesc->Get("Bl ")->mDouble;
  390. vals[3] = 255;
  391. for (int i = 0; i < 4; i++)
  392. {
  393. ImageGradientPoint pt;
  394. pt.mX = 0;
  395. pt.mValue = vals[i];
  396. colorGradient[i].mPoints.push_back(pt);
  397. colorGradient[i].mXSize = 4096;
  398. colorGradient[i].mSmoothness = 0;
  399. }
  400. // Alpha out
  401. ImageGradientPoint pt;
  402. pt.mX = 4096;
  403. pt.mValue = 0;
  404. colorGradient[3].mPoints.push_back(pt);
  405. return false;
  406. }
  407. }
  408. void PSDReader::ReadEffectContour(PSDDescriptor* descriptor, ImageCurve* curve)
  409. {
  410. PSDDescriptor* transDesc = descriptor;
  411. PSDValue* curvePts = transDesc->Get("Crv ");
  412. curve->mInterpType = transDesc->Get("Nm ")->mString;
  413. if (curvePts->mList != NULL)
  414. {
  415. for (int ptIdx = 0; ptIdx < curvePts->mInteger; ptIdx++)
  416. {
  417. ImageCurvePoint pt;
  418. PSDDescriptor* ptDesc = curvePts->mList[ptIdx].mDescriptor;
  419. if (ptDesc != NULL)
  420. {
  421. pt.mX = (float) ptDesc->Get("Hrzn")->mDouble;
  422. pt.mY = (float) ptDesc->Get("Vrtc")->mDouble;
  423. pt.mIsCorner = (ptDesc->Contains("Cnty")) && (ptDesc->Get("Cnty")->mInteger == 0);
  424. curve->mPoints.push_back(pt);
  425. }
  426. }
  427. curve->Init();
  428. }
  429. }
  430. int32 PSDReader::ReadBlendMode(PSDValue* value)
  431. {
  432. if (value->mString.length() > 4)
  433. {
  434. if (value->mString == "linearBurn")
  435. return 'lnBn';
  436. if (value->mString == "linearDodge")
  437. return 'lnDg';
  438. if (value->mString == "darkerColor")
  439. return 'dkCl';
  440. if (value->mString == "lighterColor")
  441. return 'ltCl';
  442. if (value->mString == "vividLight")
  443. return 'vivL';
  444. if (value->mString == "linearLight")
  445. return 'linL';
  446. if (value->mString == "pinLight")
  447. return 'pinL';
  448. if (value->mString == "hardMix")
  449. return 'hdMx';
  450. if (value->mString == "blendSubtraction")
  451. return 'subt';
  452. if (value->mString == "blendDivide")
  453. return 'divi';
  454. BF_ASSERT("Unknown blend mode" == 0);
  455. }
  456. return value->GetMulticharInt();
  457. }
  458. void PSDReader::ReadExtraInfo(int endPos)
  459. {
  460. while (mFS->GetPos() < endPos)
  461. {
  462. int addlSignature = mFS->ReadInt32();
  463. BF_ASSERT(addlSignature == '8BIM');
  464. int addlType = mFS->ReadInt32();
  465. int addlSize = mFS->ReadInt32();
  466. int addlStart = mFS->GetPos();
  467. if (addlSize == 0)
  468. continue;
  469. if ((addlType == 'Patt') || (addlType == 'Pat2') || (addlType == 'Pat3'))
  470. {
  471. while (true)
  472. {
  473. int pos = mFS->GetPos();
  474. PSDPattern* pattern = new PSDPattern();
  475. int aLength = mFS->ReadInt32();
  476. int patternStartPos = mFS->GetPos();
  477. int version = mFS->ReadInt32();
  478. BF_ASSERT(version == 1);
  479. int aMode = mFS->ReadInt32();
  480. int vert = mFS->ReadInt16();
  481. int horz = mFS->ReadInt16();
  482. String aName = mFS->ReadUnicode32SizedString();
  483. String id = mFS->ReadAscii8SizedString();
  484. int patVersion = mFS->ReadInt32();
  485. int patLength = mFS->ReadInt32();
  486. pos = mFS->GetPos();
  487. int patStart = mFS->GetPos();
  488. pattern->mTop = mFS->ReadInt32();
  489. pattern->mLeft = mFS->ReadInt32();
  490. pattern->mBottom = mFS->ReadInt32();
  491. pattern->mRight = mFS->ReadInt32();
  492. int maxChannels = mFS->ReadInt32();
  493. int aSize = horz*vert;
  494. pattern->mBits = new uint32[aSize];
  495. pattern->mIntensityBits = new uint8[aSize];
  496. pattern->mWidth = horz;
  497. pattern->mHeight = vert;
  498. for (int i = 0; i < aSize; i++)
  499. pattern->mBits[i] = 0x00000000;
  500. int16* rowLengths = NULL;
  501. int usedChannels = 0;
  502. for (int channel = 0; channel < maxChannels + 2; channel++)
  503. {
  504. int shift = channel * 8;
  505. if (channel == maxChannels + 1)
  506. shift = 24;
  507. bool isWritten = mFS->ReadInt32() != 0;
  508. if (isWritten)
  509. {
  510. usedChannels++;
  511. int channelLen = mFS->ReadInt32();
  512. int channelStart = mFS->GetPos();
  513. int pixelDepth = mFS->ReadInt32();
  514. int compression = mFS->ReadInt32();
  515. int val = mFS->ReadInt32();
  516. int width = mFS->ReadInt32();
  517. int height = mFS->ReadInt32();
  518. int bits = mFS->ReadInt16();
  519. compression = mFS->ReadInt8();
  520. if (compression == 0)
  521. {
  522. for (int pos = 0; pos < aSize; pos++)
  523. pattern->mBits[pos] |= ((uint32) (uint8) mFS->ReadInt8()) << shift;
  524. }
  525. else
  526. {
  527. if (rowLengths == NULL)
  528. rowLengths = new short[vert];
  529. for (int aY = 0; aY < vert; aY++)
  530. rowLengths[aY] = mFS->ReadInt16();
  531. for (int aY = 0; aY < vert; aY++)
  532. {
  533. int pos = aY * horz;
  534. int readSize = rowLengths[aY];
  535. while (readSize > 0)
  536. {
  537. int chunkSize = mFS->ReadInt8();
  538. readSize--;
  539. if (chunkSize >= 0)
  540. {
  541. // String of literal data
  542. chunkSize++;
  543. readSize -= chunkSize;
  544. while (chunkSize > 0)
  545. {
  546. pattern->mBits[pos++] |= ((uint32) (uint8) mFS->ReadInt8()) << shift;
  547. chunkSize--;
  548. }
  549. }
  550. else if (chunkSize > -128)
  551. {
  552. // One byte repeated
  553. chunkSize = 1 - chunkSize;
  554. uint32 aData = ((uint32) (uint8) mFS->ReadInt8()) << shift;
  555. readSize--;
  556. while (chunkSize > 0)
  557. {
  558. pattern->mBits[pos++] |= aData;
  559. chunkSize--;
  560. }
  561. }
  562. }
  563. }
  564. }
  565. mFS->SetPos(channelStart + channelLen);
  566. }
  567. }
  568. pos = mFS->GetPos();
  569. if (usedChannels == 1)
  570. {
  571. for (int i = 0; i < aSize; i++)
  572. {
  573. uint8 intensity = (uint8) (pattern->mBits[i] & 0xFF);
  574. pattern->mIntensityBits[i] = intensity;
  575. pattern->mBits[i] |= 0xFF000000 | (((uint32) intensity) << 8) | (((uint32) intensity) << 16);
  576. }
  577. }
  578. else if (usedChannels == 2)
  579. {
  580. for (int i = 0; i < aSize; i++)
  581. {
  582. uint8 aColor = (uint8) (pattern->mBits[i] & 0xFF);
  583. uint8 anAlpha = (uint8) ((pattern->mBits[i] >> 8) & 0xFF);
  584. pattern->mIntensityBits[i] = aColor * anAlpha / 255;
  585. pattern->mBits[i] = ((uint32) anAlpha << 24) | aColor | (((uint32) aColor) << 8) | (((uint32) aColor) << 16);
  586. }
  587. }
  588. else if (usedChannels == 3)
  589. {
  590. for (int i = 0; i < aSize; i++)
  591. {
  592. pattern->mBits[i] |= 0xFF000000;
  593. uint8 r = (uint8) ((pattern->mBits[i]) & 0xFF);
  594. uint8 g = (uint8) ((pattern->mBits[i] >> 8) & 0xFF);
  595. uint8 b = (uint8) ((pattern->mBits[i] >> 16) & 0xFF);
  596. pattern->mIntensityBits[i] = (int) ((r * 0.299f) + (g * 0.587f) + (b * 0.114f) + 0.5f);
  597. }
  598. }
  599. else
  600. {
  601. for (int i = 0; i < aSize; i++)
  602. {
  603. uint8 r = (uint8) ((pattern->mBits[i]) & 0xFF);
  604. uint8 g = (uint8) ((pattern->mBits[i] >> 8) & 0xFF);
  605. uint8 b = (uint8) ((pattern->mBits[i] >> 16) & 0xFF);
  606. uint8 a = (uint8) ((pattern->mBits[i] >> 24) & 0xFF);
  607. pattern->mIntensityBits[i] = (int) ((((r * 0.299f) + (g * 0.587f) + (b * 0.114f)) * a / 255.0f) + (255.0f - a) + 0.5f);
  608. }
  609. }
  610. delete rowLengths;
  611. mPSDPatternMap[id] = pattern;
  612. int newPos = patternStartPos + ((aLength + 3) & ~3);
  613. mFS->SetPos(newPos);
  614. pos = mFS->GetPos();
  615. if (mFS->GetPos() + 3 >= addlStart + addlSize)
  616. break;
  617. //int padding = mFS->ReadInt8();
  618. }
  619. }
  620. addlSize = (addlSize + 3) & ~3;
  621. mFS->SetPos(addlStart + addlSize);
  622. }
  623. }
  624. void PSDReader::ReadEffectSection(ImageEffects* imageEffects, PSDDescriptor* desc)
  625. {
  626. ImageEffects* anImageEffects = imageEffects;
  627. if (desc->Get("masterFXSwitch")->mInteger == 0)
  628. return;
  629. // Drop shadow
  630. PSDDescriptor* effectDesc = desc->GetDescriptor("DrSh");
  631. if (effectDesc->Get("enab")->mInteger != 0)
  632. {
  633. ImageDropShadowEffect* effect = new ImageDropShadowEffect();
  634. effect->mBlendMode = ReadBlendMode(effectDesc->Get("Md "));
  635. effect->mOpacity = effectDesc->Get("Opct")->mDouble;
  636. ReadEffectColor(effectDesc->GetDescriptor("Clr "), &effect->mColor);
  637. effect->mLocalAngle = effectDesc->Get("lagl")->mDouble;
  638. effect->mUseGlobalLight = effectDesc->Get("uglg")->mInteger != 0;
  639. effect->mDistance = effectDesc->Get("Dstn")->mDouble;
  640. effect->mSpread = effectDesc->Get("Ckmt")->mDouble;
  641. effect->mSize = effectDesc->Get("blur")->mDouble;
  642. ReadEffectContour(effectDesc->GetDescriptor("TrnS"), &effect->mContour);
  643. effect->mNoise = effectDesc->Get("Nose")->mDouble;
  644. effect->mAntiAliased = effectDesc->Get("AntA")->mInteger != 0;
  645. effect->mLayerKnocksOut = effectDesc->Get("layerConceals")->mInteger != 0;
  646. anImageEffects->AddEffect(effect);
  647. }
  648. // Outer glow
  649. effectDesc = desc->GetDescriptor("OrGl");
  650. if (effectDesc->Get("enab")->mInteger != 0)
  651. {
  652. ImageOuterGlowEffect* effect = new ImageOuterGlowEffect();
  653. effect->mBlendMode = ReadBlendMode(effectDesc->Get("Md "));
  654. effect->mOpacity = effectDesc->Get("Opct")->mDouble;
  655. effect->mNoise = effectDesc->Get("Nose")->mDouble;
  656. effect->mHasGradient = ReadEffectGradient(effectDesc, effect->mColorGradient);
  657. effect->mTechnique = effectDesc->Get("GlwT")->GetMulticharInt();
  658. effect->mSpread = effectDesc->Get("Ckmt")->mDouble;
  659. effect->mSize = effectDesc->Get("blur")->mDouble;
  660. ReadEffectContour(effectDesc->GetDescriptor("TrnS"), &effect->mContour);
  661. effect->mAntiAliased = effectDesc->Get("AntA")->mInteger != 0;
  662. effect->mRange = effectDesc->Get("Inpr")->mDouble;
  663. effect->mJitter = effectDesc->Get("ShdN")->mDouble;
  664. anImageEffects->AddEffect(effect);
  665. }
  666. // Pattern Overlay
  667. effectDesc = desc->GetDescriptor("patternFill");
  668. if (effectDesc->Get("enab")->mInteger != 0)
  669. {
  670. ImagePatternOverlayEffect* effect = new ImagePatternOverlayEffect();
  671. effect->mBlendMode = ReadBlendMode(effectDesc->Get("Md "));
  672. effect->mOpacity = effectDesc->Get("Opct")->mDouble;
  673. ReadPatternFill(effectDesc, &effect->mPattern);
  674. anImageEffects->AddEffect(effect);
  675. }
  676. // Gradient Overlay
  677. effectDesc = desc->GetDescriptor("GrFl");
  678. if (effectDesc->Get("enab")->mInteger != 0)
  679. {
  680. ImageGradientOverlayEffect* effect = new ImageGradientOverlayEffect();
  681. effect->mBlendMode = ReadBlendMode(effectDesc->Get("Md "));
  682. effect->mOpacity = effectDesc->Get("Opct")->mDouble;
  683. ReadGradientFill(effectDesc, &effect->mGradientFill);
  684. anImageEffects->AddEffect(effect);
  685. }
  686. // Color Overlay
  687. effectDesc = desc->GetDescriptor("SoFi");
  688. if (effectDesc->Get("enab")->mInteger != 0)
  689. {
  690. ImageColorOverlayEffect* effect = new ImageColorOverlayEffect();
  691. effect->mBlendMode = ReadBlendMode(effectDesc->Get("Md "));
  692. effect->mOpacity = effectDesc->Get("Opct")->mDouble;
  693. ReadEffectColor(effectDesc->GetDescriptor("Clr "), &effect->mColorFill.mColor);
  694. anImageEffects->AddEffect(effect);
  695. }
  696. // Satin
  697. effectDesc = desc->GetDescriptor("ChFX");
  698. if (effectDesc->Get("enab")->mInteger != 0)
  699. {
  700. ImageSatinEffect* effect = new ImageSatinEffect();
  701. effect->mBlendMode = ReadBlendMode(effectDesc->Get("Md "));
  702. effect->mOpacity = effectDesc->Get("Opct")->mDouble;
  703. effect->mAngle = effectDesc->Get("lagl")->mDouble;
  704. effect->mDistance = effectDesc->Get("Dstn")->mDouble;
  705. effect->mSize = effectDesc->Get("blur")->mDouble;
  706. ReadEffectColor(effectDesc->GetDescriptor("Clr "), &effect->mColor);
  707. ReadEffectContour(effectDesc->GetDescriptor("MpgS"), &effect->mContour);
  708. effect->mInvert = effectDesc->Get("Invr")->mInteger != 0;
  709. effect->mAntiAliased = effectDesc->Get("AntA")->mInteger != 0;
  710. anImageEffects->AddEffect(effect);
  711. }
  712. // Inner glow
  713. effectDesc = desc->GetDescriptor("IrGl");
  714. if (effectDesc->Get("enab")->mInteger != 0)
  715. {
  716. ImageInnerGlowEffect* effect = new ImageInnerGlowEffect();
  717. effect->mBlendMode = ReadBlendMode(effectDesc->Get("Md "));
  718. effect->mOpacity = effectDesc->Get("Opct")->mDouble;
  719. effect->mNoise = effectDesc->Get("Nose")->mDouble;
  720. effect->mHasGradient = ReadEffectGradient(effectDesc, effect->mColorGradient);
  721. effect->mIsCenter = effectDesc->Get("glwS")->mString == "SrcC";
  722. effect->mTechnique = effectDesc->Get("GlwT")->GetMulticharInt();
  723. effect->mChoke = effectDesc->Get("Ckmt")->mDouble;
  724. effect->mSize = effectDesc->Get("blur")->mDouble;
  725. ReadEffectContour(effectDesc->GetDescriptor("TrnS"), &effect->mContour);
  726. effect->mAntiAliased = effectDesc->Get("AntA")->mInteger != 0;
  727. effect->mRange = effectDesc->Get("Inpr")->mDouble;
  728. effect->mJitter = effectDesc->Get("ShdN")->mDouble;
  729. anImageEffects->AddEffect(effect);
  730. }
  731. // Inner shadow
  732. effectDesc = desc->GetDescriptor("IrSh");
  733. if (effectDesc->Get("enab")->mInteger != 0)
  734. {
  735. ImageInnerShadowEffect* effect = new ImageInnerShadowEffect();
  736. effect->mBlendMode = ReadBlendMode(effectDesc->Get("Md "));
  737. effect->mOpacity = effectDesc->Get("Opct")->mDouble;
  738. ReadEffectColor(effectDesc->GetDescriptor("Clr "), &effect->mColor);
  739. effect->mLocalAngle = effectDesc->Get("lagl")->mDouble;
  740. effect->mUseGlobalLight = effectDesc->Get("uglg")->mInteger != 0;
  741. effect->mDistance = effectDesc->Get("Dstn")->mDouble;
  742. effect->mSpread = effectDesc->Get("Ckmt")->mDouble;
  743. effect->mSize = effectDesc->Get("blur")->mDouble;
  744. ReadEffectContour(effectDesc->GetDescriptor("TrnS"), &effect->mContour);
  745. effect->mNoise = effectDesc->Get("Nose")->mDouble;
  746. effect->mAntiAliased = effectDesc->Get("AntA")->mInteger != 0;
  747. anImageEffects->AddEffect(effect);
  748. }
  749. // Stroke
  750. effectDesc = desc->GetDescriptor("FrFX");
  751. if (effectDesc->Get("enab")->mInteger != 0)
  752. {
  753. ImageStrokeEffect* effect = new ImageStrokeEffect();
  754. effect->mBlendMode = ReadBlendMode(effectDesc->Get("Md "));
  755. effect->mOpacity = effectDesc->Get("Opct")->mDouble;
  756. effect->mSize = effectDesc->Get("Sz ")->mDouble;
  757. effect->mPosition = effectDesc->Get("Styl")->GetMulticharInt();
  758. effect->mFillType = effectDesc->Get("PntT")->GetMulticharInt();
  759. ReadEffectColor(effectDesc->GetDescriptor("Clr "), &effect->mColorFill.mColor);
  760. ReadGradientFill(effectDesc, &effect->mGradientFill);
  761. ReadPatternFill(effectDesc, &effect->mPatternFill);
  762. anImageEffects->AddEffect(effect);
  763. }
  764. // Bevel
  765. effectDesc = desc->GetDescriptor("ebbl");
  766. if (effectDesc->Get("enab")->mInteger != 0)
  767. {
  768. ImageBevelEffect* effect = new ImageBevelEffect();
  769. effect->mBlendMode = ReadBlendMode(effectDesc->Get("Md "));
  770. effect->mOpacity = effectDesc->Get("Opct")->mDouble;
  771. effect->mStyle = effectDesc->Get("bvlS")->GetMulticharInt();
  772. effect->mTechnique = effectDesc->Get("bvlT")->GetMulticharInt();
  773. effect->mDepth = effectDesc->Get("srgR")->mDouble;
  774. effect->mDirectionUp = effectDesc->Get("bvlD")->mString == "In ";
  775. effect->mSize = effectDesc->Get("blur")->mDouble;;
  776. effect->mSoften = effectDesc->Get("Sftn")->mDouble;
  777. effect->mLocalAngle = effectDesc->Get("lagl")->mDouble;
  778. effect->mUseGlobalLight = effectDesc->Get("uglg")->mInteger != 0;
  779. effect->mLocalAltitude = effectDesc->Get("Lald")->mDouble;
  780. ReadEffectContour(effectDesc->GetDescriptor("TrnS"), &effect->mGlossContour);
  781. effect->mAntiAliased = effectDesc->Get("antialiasGloss")->mInteger != 0;
  782. effect->mHiliteMode = ReadBlendMode(effectDesc->Get("hglM"));
  783. ReadEffectColor(effectDesc->GetDescriptor("hglC"), &effect->mHiliteColor);
  784. effect->mHiliteOpacity = effectDesc->Get("hglO")->mDouble;
  785. effect->mShadowMode = ReadBlendMode(effectDesc->Get("sdwM"));
  786. ReadEffectColor(effectDesc->GetDescriptor("sdwC"), &effect->mShadowColor);
  787. effect->mShadowOpacity = effectDesc->Get("sdwO")->mDouble;
  788. effect->mUseContour = effectDesc->Get("useShape")->mInteger != 0;
  789. effect->mUseTexture = effectDesc->Get("useTexture")->mInteger != 0;
  790. ReadEffectContour(effectDesc->GetDescriptor("MpgS"), &effect->mBevelContour);
  791. effect->mBevelContourRange = effectDesc->Get("Inpr")->mDouble;
  792. effect->mTextureDepth = effectDesc->Get("textureDepth")->mDouble;
  793. ReadPatternFill(effectDesc, &effect->mTexture);
  794. effect->mTextureInvert = effectDesc->Get("InvT")->mInteger != 0;
  795. anImageEffects->AddEffect(effect);
  796. }
  797. }
  798. void PSDReader::ReadGradientFill(PSDDescriptor* descriptor, ImageGradientFill* gradientFill)
  799. {
  800. ReadEffectGradient(descriptor, gradientFill->mColorGradient);
  801. gradientFill->mReverse = descriptor->Get("Rvrs")->mInteger != 0;
  802. gradientFill->mStyle = descriptor->Get("Type")->GetMulticharInt();
  803. gradientFill->mAlignWithLayer = descriptor->Get("Algn")->mInteger != 0;
  804. gradientFill->mAngle = descriptor->Get("Angl")->mDouble;
  805. gradientFill->mScale = descriptor->Get("Scl ")->mDouble;
  806. PSDDescriptor* offsetDesc = descriptor->GetDescriptor("Ofst");
  807. gradientFill->mOffsetX = offsetDesc->Get("Hrzn")->mDouble;
  808. gradientFill->mOffsetY = offsetDesc->Get("Vrtc")->mDouble;
  809. }
  810. void PSDReader::ReadPatternFill(PSDDescriptor* descriptor, ImagePatternFill* patternFill)
  811. {
  812. PSDDescriptor* phaseDesc = descriptor->GetDescriptor("phase");
  813. patternFill->mPhaseX = phaseDesc->Get("Hrzn")->mDouble;
  814. patternFill->mPhaseY = phaseDesc->Get("Vrtc")->mDouble;
  815. patternFill->mLinkWithLayer = true;
  816. if (descriptor->Contains("Algn"))
  817. patternFill->mLinkWithLayer = descriptor->Get("Algn")->mInteger != 0;
  818. if (descriptor->Contains("Lnkd"))
  819. patternFill->mLinkWithLayer = descriptor->Get("Lnkd")->mInteger != 0;
  820. patternFill->mScale = descriptor->Get("Scl ")->mDouble;
  821. if (patternFill->mScale == 0)
  822. patternFill->mScale = 100.0;
  823. PSDDescriptor* patternDesc = descriptor->GetDescriptor("Ptrn");
  824. patternFill->mPatternName = patternDesc->Get("Idnt")->mString;
  825. }
  826. bool PSDReader::Init(const StringImpl& fileName)
  827. {
  828. AutoPerf gPerf("PSDReader::Init");
  829. /*mFS = gBFApp->OpenBinaryFile(fileName);
  830. if (mFS == NULL)
  831. return false;*/
  832. mFS = new FileStream();
  833. mFS->Open(fileName, "rb");
  834. if (!mFS->IsOpen())
  835. {
  836. delete mFS;
  837. mFS = NULL;
  838. return false;
  839. }
  840. //mFS->SetCacheSize(4096);
  841. mFS->mBigEndian = true;
  842. int32 header = mFS->ReadInt32();
  843. if (header != '8BPS')
  844. return false;
  845. mVersion = mFS->ReadInt16();
  846. mFS->Seek(6);
  847. mChannels = mFS->ReadInt16();
  848. mHeight = mFS->ReadInt32();
  849. mWidth = mFS->ReadInt32();
  850. mBitDepthPerChannel = mFS->ReadInt16();
  851. mMode = mFS->ReadInt16();
  852. int colorModeLen = mFS->ReadInt32();
  853. if (colorModeLen > 0)
  854. mFS->Seek(colorModeLen);
  855. int imageResourcesLen = mFS->ReadInt32();
  856. int resourceStartPos = mFS->GetPos();
  857. if (imageResourcesLen > 0)
  858. {
  859. while (mFS->GetPos() < resourceStartPos + imageResourcesLen)
  860. {
  861. int signature = mFS->ReadInt32();
  862. BF_ASSERT(signature == '8BIM');
  863. int aType = mFS->ReadInt16();
  864. std::string aName = mFS->ReadAscii8SizedString();
  865. int extra = (int)(((aName.length() + 2) & ~1) - (aName.length() + 1));
  866. mFS->Seek(extra);
  867. int dataSize = mFS->ReadInt32();
  868. //BF_ASSERT(dataSize < 100000);
  869. int curPos = mFS->GetPos();
  870. if (aType == 0x40D)
  871. {
  872. mGlobalAngle = mFS->ReadInt32();
  873. }
  874. else if (aType == 0x419)
  875. {
  876. mGlobalAltitude = mFS->ReadInt32();
  877. }
  878. mFS->SetPos(curPos + (dataSize + 1) & ~1);
  879. }
  880. mFS->SetPos(resourceStartPos + imageResourcesLen);
  881. }
  882. int layerAndMaskLen = mFS->ReadInt32();
  883. int startPos = mFS->GetPos();
  884. if (layerAndMaskLen > 0)
  885. {
  886. int layersLen = mFS->ReadInt32();
  887. int layerStart = mFS->GetPos();
  888. if (layersLen > 0)
  889. {
  890. int numLayers = abs(mFS->ReadInt16());
  891. for (int layerNum = 0; layerNum < numLayers; layerNum++)
  892. {
  893. PSDLayerInfo* pSDLayerInfo = new PSDLayerInfo();
  894. pSDLayerInfo->mPSDReader = this;
  895. pSDLayerInfo->mY = mFS->ReadInt32();
  896. pSDLayerInfo->mX = mFS->ReadInt32();
  897. pSDLayerInfo->mHeight = mFS->ReadInt32() - pSDLayerInfo->mY;
  898. pSDLayerInfo->mWidth = mFS->ReadInt32() - pSDLayerInfo->mX;
  899. int channelCount = mFS->ReadInt16();
  900. for (int channelIdx = 0; channelIdx < channelCount; channelIdx++)
  901. {
  902. PSDChannelInfo pSDChannelInfo;
  903. pSDChannelInfo.mId = mFS->ReadInt16();
  904. pSDChannelInfo.mLength = mFS->ReadInt32();
  905. pSDLayerInfo->mChannels.push_back(pSDChannelInfo);
  906. }
  907. int blendModeSig = mFS->ReadInt32(); // Should be '8BIM'
  908. int blendMode = mFS->ReadInt32();
  909. pSDLayerInfo->mIsAdditive = (blendMode == 'lddg');
  910. pSDLayerInfo->mBlendMode = blendMode;
  911. pSDLayerInfo->mOpacity = mFS->ReadInt8();
  912. pSDLayerInfo->mBaseClipping = mFS->ReadInt8() == 0;
  913. int flags = mFS->ReadInt8();
  914. int filler = mFS->ReadInt8();
  915. pSDLayerInfo->mVisible = (flags & (1<<1)) == 0;
  916. int extraDataLen = mFS->ReadInt32();
  917. int extraDataStart = mFS->GetPos();
  918. int layerMaskDataLen = mFS->ReadInt32();
  919. int layerMaskStart = mFS->GetPos();
  920. if (layerMaskDataLen != 0)
  921. {
  922. pSDLayerInfo->mLayerMaskY = mFS->ReadInt32();
  923. pSDLayerInfo->mLayerMaskX = mFS->ReadInt32();
  924. pSDLayerInfo->mLayerMaskHeight = mFS->ReadInt32() - pSDLayerInfo->mLayerMaskY;
  925. pSDLayerInfo->mLayerMaskWidth = mFS->ReadInt32() - pSDLayerInfo->mLayerMaskX;
  926. uint8 aColor = (uint8) mFS->ReadInt8();
  927. int flags = (uint8) mFS->ReadInt8();
  928. pSDLayerInfo->mLayerMaskEnabled = (flags & 2) == 0;
  929. pSDLayerInfo->mLayerMaskInverted = (flags & 4) == 0;
  930. pSDLayerInfo->mLayerMaskDefault = aColor;
  931. mFS->SetPos(layerMaskStart + layerMaskDataLen);
  932. }
  933. int layerBlendingRangesLen = mFS->ReadInt32();
  934. int layerBlendingRangesStart = mFS->GetPos();
  935. if (layerBlendingRangesLen > 0)
  936. {
  937. int idx = 0;
  938. // What's the 5th range value for?
  939. while ((mFS->GetPos() < layerBlendingRangesStart + layerBlendingRangesLen) && (idx < 4))
  940. {
  941. int source = mFS->ReadInt32();
  942. int dest = mFS->ReadInt32();
  943. int shift = ((idx + 3) % 4) * 8;
  944. pSDLayerInfo->mBlendingRangeSourceStart = (pSDLayerInfo->mBlendingRangeSourceStart & ~(0xFF<<shift)) | ((source>>16 & 0xFF)<<shift);
  945. pSDLayerInfo->mBlendingRangeSourceEnd = (pSDLayerInfo->mBlendingRangeSourceEnd & ~(0xFF<<shift)) | ((source & 0xFF)<<shift);
  946. pSDLayerInfo->mBlendingRangeDestStart = (pSDLayerInfo->mBlendingRangeDestStart & ~(0xFF<<shift)) | ((dest>>16 & 0xFF)<<shift);
  947. pSDLayerInfo->mBlendingRangeDestEnd = (pSDLayerInfo->mBlendingRangeDestEnd & ~(0xFF<<shift)) | ((dest & 0xFF)<<shift);
  948. idx++;
  949. }
  950. mFS->SetPos(layerBlendingRangesStart + layerBlendingRangesLen);
  951. }
  952. pSDLayerInfo->mName = mFS->ReadAscii8SizedString();
  953. int extra = (int)(((pSDLayerInfo->mName.length() + 4) & ~3) - (pSDLayerInfo->mName.length() + 1));
  954. mFS->Seek(extra);
  955. AutoPerf gPerf("PSDReader::Init - Extras");
  956. while (mFS->GetPos() < extraDataStart + extraDataLen)
  957. {
  958. int addlSignature = mFS->ReadInt32();
  959. BF_ASSERT(addlSignature == '8BIM');
  960. int addlType = mFS->ReadInt32();
  961. int addlSize = mFS->ReadInt32();
  962. int addlStart = mFS->GetPos();
  963. if (addlSignature == '8BIM')
  964. {
  965. if (addlType == 'lfx2')
  966. {
  967. int32 version = mFS->ReadInt32();
  968. int descriptorVersion = mFS->ReadInt32();
  969. PSDDescriptor* aDesc = new PSDDescriptor();
  970. ReadPSDDescriptor(aDesc);
  971. ReadEffectSection(pSDLayerInfo->mImageEffects, aDesc);
  972. delete aDesc;
  973. }
  974. else if (addlType == 'lyid')
  975. {
  976. pSDLayerInfo->mLayerId = mFS->ReadInt32();
  977. }
  978. else if (addlType == 'lrFX')
  979. {
  980. int version = mFS->ReadInt16();
  981. int countCount = mFS->ReadInt16();
  982. }
  983. else if (addlType == 'TySh')
  984. {
  985. int typeVersion = mFS->ReadInt16();
  986. double a = mFS->ReadDouble();
  987. double b = mFS->ReadDouble();
  988. double c = mFS->ReadDouble();
  989. double d = mFS->ReadDouble();
  990. double tx = mFS->ReadDouble();
  991. double ty = mFS->ReadDouble();
  992. int textVersion = mFS->ReadInt16();
  993. int descriptorVersion = mFS->ReadInt32();
  994. PSDDescriptor* aDescriptor = new PSDDescriptor();
  995. ReadPSDDescriptor(aDescriptor);
  996. delete aDescriptor;
  997. }
  998. else if (addlType == 'luni')
  999. {
  1000. NOP;
  1001. }
  1002. else if (addlType == 'lnsr')
  1003. {
  1004. NOP;
  1005. }
  1006. else if (addlType == 'clbl')
  1007. {
  1008. pSDLayerInfo->mBlendClippedElementsAsGroup = mFS->ReadInt8() != 0;
  1009. }
  1010. else if (addlType == 'infx')
  1011. {
  1012. pSDLayerInfo->mBlendInteriorEffectsAsGroup = mFS->ReadInt8() != 0;
  1013. }
  1014. else if (addlType == 'knko')
  1015. {
  1016. pSDLayerInfo->mKnockout = mFS->ReadInt8();
  1017. }
  1018. else if (addlType == 'lspf')
  1019. {
  1020. NOP;
  1021. }
  1022. else if (addlType == 'tsly')
  1023. {
  1024. pSDLayerInfo->mTransparencyShapesLayer = mFS->ReadInt8() != 0;
  1025. }
  1026. else if (addlType == 'lmgm')
  1027. {
  1028. pSDLayerInfo->mLayerMaskHidesEffects = mFS->ReadInt8() != 0;
  1029. }
  1030. else if (addlType == 'vmgm')
  1031. {
  1032. pSDLayerInfo->mVectorMaskHidesEffects = mFS->ReadInt8() != 0;
  1033. }
  1034. else if (addlType == 'lyvr')
  1035. {
  1036. NOP;
  1037. }
  1038. else if (addlType == 'lclr')
  1039. {
  1040. NOP;
  1041. }
  1042. else if (addlType == 'shmd') // Metadata
  1043. {
  1044. NOP;
  1045. }
  1046. else if (addlType == 'fxrp')
  1047. {
  1048. pSDLayerInfo->mRefX = mFS->ReadDouble();
  1049. pSDLayerInfo->mRefY = mFS->ReadDouble();
  1050. }
  1051. else if (addlType == 'iOpa')
  1052. {
  1053. pSDLayerInfo->mFillOpacity = mFS->ReadInt8();
  1054. }
  1055. else if (addlType == 'lsct')
  1056. {
  1057. pSDLayerInfo->mSectionDividerType = mFS->ReadInt32();
  1058. int sig = mFS->ReadInt32();
  1059. pSDLayerInfo->mSectionBlendMode = mFS->ReadInt32();
  1060. }
  1061. else if (addlType == 'brst')
  1062. {
  1063. for (int i = 0; i < addlSize/4; i++)
  1064. {
  1065. int channel = mFS->ReadInt32();
  1066. pSDLayerInfo->mChannelMask &= ~(0xFF << (8*channel));
  1067. }
  1068. // Turn off alpha if RGB is all off
  1069. if (pSDLayerInfo->mChannelMask == 0xFF000000)
  1070. pSDLayerInfo->mChannelMask = 0;
  1071. }
  1072. else if (addlType == 'nvrt')
  1073. {
  1074. pSDLayerInfo->mImageAdjustment = new InvertImageAdjustement();
  1075. }
  1076. else if (addlType == 'SoCo')
  1077. {
  1078. int version = mFS->ReadInt32();
  1079. PSDDescriptor aDescriptor;
  1080. ReadPSDDescriptor(&aDescriptor);
  1081. SolidColorImageAdjustement* adjustment = new SolidColorImageAdjustement();
  1082. ReadEffectColor(aDescriptor.GetDescriptor("Clr "), &adjustment->mColor);
  1083. pSDLayerInfo->mImageAdjustment = adjustment;
  1084. }
  1085. else if (addlType == 'GdFl')
  1086. {
  1087. int version = mFS->ReadInt32();
  1088. PSDDescriptor aDescriptor;
  1089. ReadPSDDescriptor(&aDescriptor);
  1090. GradientImageAdjustement* adjustment = new GradientImageAdjustement();
  1091. adjustment->mFill = new ImageGradientFill();
  1092. adjustment->mFill->mAlignWithLayer = true;
  1093. ReadGradientFill(&aDescriptor, adjustment->mFill);
  1094. pSDLayerInfo->mImageAdjustment = adjustment;
  1095. }
  1096. else if (addlType == 'PtFl')
  1097. {
  1098. int version = mFS->ReadInt32();
  1099. PSDDescriptor aDescriptor;
  1100. ReadPSDDescriptor(&aDescriptor);
  1101. PatternImageAdjustement* adjustment = new PatternImageAdjustement();
  1102. adjustment->mFill = new ImagePatternFill();
  1103. ReadPatternFill(&aDescriptor, adjustment->mFill);
  1104. pSDLayerInfo->mImageAdjustment = adjustment;
  1105. }
  1106. else if (addlType == 'brit')
  1107. {
  1108. BrightnessContrastImageAdjustment* adjustment = new BrightnessContrastImageAdjustment();
  1109. adjustment->mBrightness = mFS->ReadInt16();
  1110. adjustment->mContrast = mFS->ReadInt16();
  1111. adjustment->mMeanValue = mFS->ReadInt16();
  1112. adjustment->mLabColorOnly = mFS->ReadInt8() != 0;
  1113. pSDLayerInfo->mImageAdjustment = adjustment;
  1114. }
  1115. else if (addlType == 'CgEd')
  1116. {
  1117. int version = mFS->ReadInt32();
  1118. PSDDescriptor aDescriptor;
  1119. ReadPSDDescriptor(&aDescriptor);
  1120. BrightnessContrastImageAdjustment* brightnessAdjustment = dynamic_cast<BrightnessContrastImageAdjustment*>(pSDLayerInfo->mImageAdjustment);
  1121. if (brightnessAdjustment != NULL)
  1122. {
  1123. brightnessAdjustment->mBrightness = aDescriptor.Get("Brgh")->mInteger;
  1124. brightnessAdjustment->mContrast = aDescriptor.Get("Cntr")->mInteger;
  1125. brightnessAdjustment->mMeanValue = aDescriptor.Get("means")->mInteger;
  1126. brightnessAdjustment->mLabColorOnly = aDescriptor.Get("Lab")->mInteger != 0;
  1127. }
  1128. }
  1129. else if (addlType == 'vmsk')
  1130. {
  1131. pSDLayerInfo->mVectorMask = new PSDPath();
  1132. int version = mFS->ReadInt32();
  1133. int flags = mFS->ReadInt32();
  1134. int count = addlSize / 26;
  1135. for (int i = 0; i < count; i++)
  1136. {
  1137. int aType = mFS->ReadInt16();
  1138. if (aType == 0)
  1139. {
  1140. pSDLayerInfo->mVectorMask->mClosed = true;
  1141. mFS->Seek(24);
  1142. }
  1143. else if (aType == 3)
  1144. {
  1145. pSDLayerInfo->mVectorMask->mClosed = false;
  1146. mFS->Seek(24);
  1147. }
  1148. else if ((aType == 1) || (aType == 2))
  1149. {
  1150. PSDPathPoint point;
  1151. point.mCtlEnterX = (mFS->ReadInt32() / (double) 0x1000000) * mWidth;
  1152. point.mCtlEnterY = (mFS->ReadInt32() / (double) 0x1000000) * mHeight;
  1153. point.mAnchorX = (mFS->ReadInt32() / (double) 0x1000000) * mWidth;
  1154. point.mAnchorY = (mFS->ReadInt32() / (double) 0x1000000) * mHeight;
  1155. point.mCtlLeaveX = (mFS->ReadInt32() / (double) 0x1000000) * mWidth;
  1156. point.mCtlLeaveY = (mFS->ReadInt32() / (double) 0x1000000) * mHeight;
  1157. pSDLayerInfo->mVectorMask->mPoints.push_back(point);
  1158. NOP;
  1159. }
  1160. else
  1161. {
  1162. mFS->Seek(24);
  1163. }
  1164. }
  1165. }
  1166. else
  1167. {
  1168. NOP;
  1169. }
  1170. }
  1171. mFS->SetPos(addlStart + addlSize);
  1172. }
  1173. int pos = mFS->GetPos();
  1174. mFS->SetPos(extraDataStart + extraDataLen);
  1175. mPSDLayerInfoVector.insert(mPSDLayerInfoVector.begin(), pSDLayerInfo);
  1176. }
  1177. int filePos = mFS->GetPos();
  1178. int channelsStart = filePos;
  1179. int channelsLen = 0;
  1180. for (int layerNum = (int) mPSDLayerInfoVector.size() - 1; layerNum >= 0; layerNum--)
  1181. {
  1182. PSDLayerInfo* pSDLayerInfo = mPSDLayerInfoVector[layerNum];
  1183. pSDLayerInfo->mImageDataStart = filePos;
  1184. for (int channelIdx = 0; channelIdx < (int) pSDLayerInfo->mChannels.size(); channelIdx++)
  1185. {
  1186. PSDChannelInfo* channel = &pSDLayerInfo->mChannels[channelIdx];
  1187. filePos += channel->mLength;
  1188. channelsLen += channel->mLength;
  1189. }
  1190. }
  1191. PSDLayerInfo* parent = NULL;
  1192. for (int idx = 0; idx < (int) mPSDLayerInfoVector.size(); idx++)
  1193. {
  1194. PSDLayerInfo* aLayer = mPSDLayerInfoVector[idx];
  1195. aLayer->mParent = parent;
  1196. aLayer->mIdx = idx;
  1197. if ((aLayer->mSectionDividerType == PSDDIVIDER_OPEN_FOLDER) || (aLayer->mSectionDividerType == PSDDIVIDER_CLOSED_FOLDER))
  1198. parent = aLayer;
  1199. else if (aLayer->mSectionDividerType == PSDDIVIDER_SECTION_END)
  1200. {
  1201. parent = parent->mParent;
  1202. delete aLayer;
  1203. mPSDLayerInfoVector.erase(mPSDLayerInfoVector.begin() + idx);
  1204. idx--;
  1205. }
  1206. }
  1207. mFS->SetPos(layerStart + layersLen);
  1208. int posThing = mFS->GetPos();
  1209. int extraVersion = mFS->ReadInt32();
  1210. mFS->Seek(extraVersion);
  1211. ReadExtraInfo(startPos + layerAndMaskLen);
  1212. }
  1213. }
  1214. int pos = mFS->GetPos();
  1215. mFS->SetPos(startPos + layerAndMaskLen);
  1216. pos = mFS->GetPos();
  1217. return true;
  1218. }
  1219. Texture* PSDReader::LoadLayerTexture(int layerIdx, int* ofsX, int* ofsY) // -1 = composited image
  1220. {
  1221. if ((layerIdx < 0) || (layerIdx >= (int) mPSDLayerInfoVector.size()))
  1222. return NULL;
  1223. //TODO: This is a special case for flattening here, just on the first layer...
  1224. PSDLayerInfo* prevLayer = (layerIdx == 1) ? mPSDLayerInfoVector[layerIdx - 1] : NULL;
  1225. if (prevLayer != NULL)
  1226. {
  1227. if (prevLayer->mBits == NULL)
  1228. prevLayer->ReadData();
  1229. }
  1230. PSDLayerInfo* aLayer = mPSDLayerInfoVector[layerIdx];
  1231. if (aLayer->mBits == NULL)
  1232. aLayer->ReadData();
  1233. ImageData* anImageData = aLayer;
  1234. anImageData = aLayer->mImageEffects->FlattenInto(prevLayer, aLayer, aLayer, NULL, NULL);
  1235. Texture* texture = gBFApp->mRenderDevice->LoadTexture(anImageData, false);
  1236. *ofsX = anImageData->mX;
  1237. *ofsY = anImageData->mY;
  1238. if (anImageData != aLayer)
  1239. delete anImageData;
  1240. return texture;
  1241. }
  1242. typedef std::vector<PSDLayerInfo*> PSDLayerVector;
  1243. typedef std::map<int, PSDLayerVector> LayerInfoMap;
  1244. typedef std::set<int> IntSet;
  1245. ImageData* PSDReader::MergeLayers(PSDLayerInfo* group, const std::vector<int>& layerIndices, ImageData* bottomImage)
  1246. {
  1247. AutoPerf gPerf("PSDReader::MergeLayers");
  1248. bool isImageAllocated = false;
  1249. ImageData* combinedImage = bottomImage;
  1250. ImageData* prevBottom = NULL;
  1251. bool doPostGroupBlend = false;
  1252. bool needsPrevBottom = (group != NULL) && (group->mOpacity != 255);
  1253. for (int indexIdx = 0; indexIdx < (int) layerIndices.size(); indexIdx++)
  1254. {
  1255. PSDLayerInfo* layerInfo = mPSDLayerInfoVector[layerIndices[indexIdx]];
  1256. if (layerInfo->mBits == NULL)
  1257. layerInfo->ReadData();
  1258. if (layerInfo->mKnockout == 1) // Shallow
  1259. needsPrevBottom = true;
  1260. }
  1261. if ((group != NULL) && (bottomImage != NULL) && (group->mSectionBlendMode != 'pass'))
  1262. {
  1263. doPostGroupBlend = true;
  1264. combinedImage = NULL;
  1265. }
  1266. if (needsPrevBottom && (bottomImage != NULL))
  1267. prevBottom = bottomImage->Duplicate();
  1268. bool hasAllocatedClipBase = false;
  1269. PSDLayerInfo* clipBaseLayer = NULL;
  1270. ImageData* clipBaseImage = NULL;
  1271. for (int indexIdx = 0; indexIdx <= (int) layerIndices.size(); indexIdx++)
  1272. {
  1273. PSDLayerInfo* layerInfo = NULL;
  1274. if (indexIdx < (int) layerIndices.size())
  1275. layerInfo = mPSDLayerInfoVector[layerIndices[indexIdx]];
  1276. PSDLayerInfo* pendingFlushLayer = NULL;
  1277. ImageData* pendingFlushImage = NULL;
  1278. if ((layerInfo != NULL) && (!layerInfo->mBaseClipping))
  1279. {
  1280. if (clipBaseLayer != NULL)
  1281. {
  1282. if (clipBaseLayer->mBlendClippedElementsAsGroup)
  1283. {
  1284. ImageData* newImage = layerInfo->mImageEffects->FlattenInto(clipBaseImage, layerInfo, layerInfo, prevBottom, NULL);
  1285. SetImageAlpha(newImage, 255);
  1286. if (hasAllocatedClipBase)
  1287. delete clipBaseImage;
  1288. hasAllocatedClipBase = true;
  1289. clipBaseImage = newImage;
  1290. }
  1291. else
  1292. {
  1293. if (clipBaseImage == clipBaseLayer)
  1294. {
  1295. /*clipBaseImage = clipBaseImage->Duplicate();
  1296. //SetImageAlpha(clipBaseImage, clipBaseLayer->mFillOpacity);
  1297. SetImageAlpha(clipBaseImage, 0);
  1298. hasAllocatedClipBase = true;*/
  1299. //clipBaseImage = layerInfo->mImageEffects->FlattenInto(combinedImage, clipBaseLayer, clipBaseLayer, prevBottom, NULL);
  1300. //hasAllocatedClipBase = true;
  1301. //combinedImage = combinedImage->Duplicate();
  1302. ImageData* newImage = NULL;
  1303. if (clipBaseLayer->mKnockout == 0) // No Knockout
  1304. {
  1305. newImage = CreateResizedImageUnion(combinedImage, clipBaseLayer->mX, clipBaseLayer->mY, clipBaseLayer->mWidth, clipBaseLayer->mHeight);
  1306. }
  1307. else if (clipBaseLayer->mKnockout == 1) // Shallow Knockout
  1308. {
  1309. newImage = CreateResizedImageUnion(prevBottom, clipBaseLayer->mX, clipBaseLayer->mY, clipBaseLayer->mWidth, clipBaseLayer->mHeight);
  1310. }
  1311. else // Deep Knockout
  1312. {
  1313. newImage = CreateEmptyResizedImageUnion(combinedImage, clipBaseLayer->mX, clipBaseLayer->mY, clipBaseLayer->mWidth, clipBaseLayer->mHeight);
  1314. }
  1315. if (combinedImage != NULL)
  1316. BlendImage(newImage, clipBaseImage, clipBaseImage->mX - combinedImage->mX, clipBaseImage->mY - combinedImage->mY, clipBaseLayer->mFillOpacity / 255.0f, clipBaseLayer->mBlendMode, true);
  1317. if (hasAllocatedClipBase)
  1318. delete clipBaseImage;
  1319. hasAllocatedClipBase = true;
  1320. clipBaseImage = newImage;
  1321. }
  1322. //ImageData* tempImage = layerInfo->Duplicate();
  1323. //SetImageAlpha(tempImage, 255);
  1324. //ImageData* newImage = layerInfo->mImageEffects->FlattenInto(clipBaseImage, layerInfo, tempImage, prevBottom);
  1325. ImageData* newImage = layerInfo->mImageEffects->FlattenInto(clipBaseImage, layerInfo, layerInfo, prevBottom, NULL);
  1326. if (hasAllocatedClipBase)
  1327. delete clipBaseImage;
  1328. hasAllocatedClipBase = true;
  1329. clipBaseImage = newImage;
  1330. }
  1331. }
  1332. }
  1333. else
  1334. {
  1335. pendingFlushImage = clipBaseImage;
  1336. pendingFlushLayer = clipBaseLayer;
  1337. clipBaseImage = layerInfo;
  1338. clipBaseLayer = layerInfo;
  1339. }
  1340. if (pendingFlushImage != NULL)
  1341. {
  1342. ImageData* newImage = NULL;
  1343. if ((pendingFlushLayer != pendingFlushImage) && (!pendingFlushLayer->mBlendClippedElementsAsGroup))
  1344. {
  1345. /*hasAllocatedClipBase = false;
  1346. BlendImagesTogether(combinedImage, pendingFlushImage, pendingFlushLayer);
  1347. newImage = combinedImage;*/
  1348. newImage = pendingFlushLayer->mImageEffects->FlattenInto(combinedImage, pendingFlushLayer, pendingFlushLayer, prevBottom, pendingFlushImage);
  1349. }
  1350. else
  1351. {
  1352. if ((pendingFlushImage != pendingFlushLayer) && (pendingFlushLayer->mBlendClippedElementsAsGroup))
  1353. SetImageAlpha(pendingFlushImage, pendingFlushLayer);
  1354. newImage = pendingFlushLayer->mImageEffects->FlattenInto(combinedImage, pendingFlushLayer, pendingFlushImage, prevBottom, NULL);
  1355. }
  1356. if (isImageAllocated)
  1357. delete combinedImage;
  1358. isImageAllocated = newImage == pendingFlushLayer;
  1359. combinedImage = newImage;
  1360. if (hasAllocatedClipBase)
  1361. delete pendingFlushImage;
  1362. }
  1363. // Is this a group?
  1364. if ((layerInfo != NULL) && (layerInfo->mParent != group))
  1365. {
  1366. std::vector<int> aChildren;
  1367. //indexIdx--;
  1368. for (; indexIdx < (int) layerIndices.size(); indexIdx++)
  1369. {
  1370. layerInfo = mPSDLayerInfoVector[layerIndices[indexIdx]];
  1371. if (layerInfo->mParent == group)
  1372. break;
  1373. aChildren.push_back(layerInfo->mIdx);
  1374. }
  1375. ImageData* newImage = MergeLayers(layerInfo, aChildren, combinedImage);
  1376. if (isImageAllocated)
  1377. delete combinedImage;
  1378. isImageAllocated = newImage == layerInfo;
  1379. combinedImage = newImage;
  1380. if (hasAllocatedClipBase)
  1381. delete pendingFlushImage;
  1382. clipBaseImage = NULL;
  1383. clipBaseLayer = NULL;
  1384. }
  1385. }
  1386. if (hasAllocatedClipBase)
  1387. delete clipBaseImage;
  1388. if (doPostGroupBlend)
  1389. {
  1390. ImageData* blendInto = CreateResizedImageUnion(bottomImage, combinedImage->mX, combinedImage->mY, combinedImage->mWidth, combinedImage->mHeight);
  1391. BlendImage(blendInto, combinedImage, combinedImage->mX - blendInto->mX, combinedImage->mY - blendInto->mY,
  1392. 1.0f, group->mSectionBlendMode);
  1393. if (isImageAllocated)
  1394. delete combinedImage;
  1395. combinedImage = blendInto;
  1396. }
  1397. if ((group != NULL) && (group->mOpacity != 255))
  1398. {
  1399. // Crossfade together with previous bottom
  1400. CrossfadeImage(prevBottom, combinedImage, group->mOpacity / 255.0f);
  1401. delete prevBottom;
  1402. }
  1403. return combinedImage;
  1404. }
  1405. Texture* PSDReader::LoadMergedLayerTexture(const std::vector<int>& layerIndices, int* ofsX, int* ofsY)
  1406. {
  1407. AutoPerf gPerf("PSDReader::LoadMergedLayerTexture");
  1408. LayerInfoMap groupMap;
  1409. if (layerIndices.size() == 0)
  1410. return NULL;
  1411. IntSet layerSet;
  1412. // Fix up the layer set - add parents and remove children whose parents don't appear in the list
  1413. for (int indexIdx = 0; indexIdx < (int) layerIndices.size(); indexIdx++)
  1414. {
  1415. PSDLayerInfo* layerInfo = mPSDLayerInfoVector[layerIndices[indexIdx]];
  1416. layerSet.insert(layerInfo->mIdx);
  1417. }
  1418. for (int indexIdx = 0; indexIdx < (int) layerIndices.size(); indexIdx++)
  1419. {
  1420. PSDLayerInfo* layerInfo = mPSDLayerInfoVector[layerIndices[indexIdx]];
  1421. PSDLayerInfo* parentCheck = layerInfo->mParent;
  1422. bool includeLayer = true;
  1423. if (!layerInfo->mBaseClipping)
  1424. {
  1425. int baseIdx = layerInfo->mIdx + 1;
  1426. while (baseIdx < (int) mPSDLayerInfoVector.size())
  1427. {
  1428. PSDLayerInfo* baseCheck = mPSDLayerInfoVector[baseIdx];
  1429. if (baseCheck->mBaseClipping)
  1430. {
  1431. includeLayer &= baseCheck->mVisible;
  1432. break;
  1433. }
  1434. baseIdx++;
  1435. }
  1436. }
  1437. while (parentCheck != NULL)
  1438. {
  1439. includeLayer &= layerSet.find(parentCheck->mIdx) != layerSet.end();
  1440. parentCheck = parentCheck->mParent;
  1441. }
  1442. if (!includeLayer)
  1443. layerSet.erase(layerSet.find(layerInfo->mIdx));
  1444. }
  1445. std::vector<int> aLayerIndices;
  1446. // Composite
  1447. IntSet::reverse_iterator revItr = layerSet.rbegin();
  1448. while (revItr != layerSet.rend())
  1449. {
  1450. //PSDLayerInfo* layerInfo = mPSDLayerInfoVector[*revItr];
  1451. aLayerIndices.push_back(*revItr);
  1452. revItr++;
  1453. }
  1454. ImageData* combinedImage = MergeLayers(NULL, aLayerIndices, NULL);
  1455. Texture* texture = gBFApp->mRenderDevice->LoadTexture(combinedImage, false);
  1456. *ofsX = combinedImage->mX;
  1457. *ofsY = combinedImage->mY;
  1458. if (dynamic_cast<PSDLayerInfo*>(combinedImage) == NULL)
  1459. delete combinedImage;
  1460. return texture;
  1461. }
  1462. ///
  1463. PSDLayerInfo::PSDLayerInfo()
  1464. {
  1465. mLayerId = 0;
  1466. mSectionDividerType = 0;
  1467. mSectionBlendMode = 0;
  1468. mPSDDescriptor = NULL;
  1469. mRefX = 0;
  1470. mRefY = 0;
  1471. mFillOpacity = 255;
  1472. mImageEffects = new ImageEffects();
  1473. mKnockout = 0;
  1474. mLayerMask = NULL;
  1475. mLayerMaskEnabled = false;
  1476. mLayerMaskInverted = false;
  1477. mLayerMaskDefault = 255;
  1478. mLayerMaskX = 0;
  1479. mLayerMaskY = 0;
  1480. mLayerMaskWidth = 0;
  1481. mLayerMaskHeight = 0;
  1482. mBlendInteriorEffectsAsGroup = false;
  1483. mBlendClippedElementsAsGroup = true;
  1484. mTransparencyShapesLayer = true;
  1485. mLayerMaskHidesEffects = false;
  1486. mVectorMaskHidesEffects = false;
  1487. mBlendingRangeSourceStart = 0;
  1488. mBlendingRangeSourceEnd = 0xFFFFFFFF;
  1489. mBlendingRangeDestStart = 0;
  1490. mBlendingRangeDestEnd = 0xFFFFFFFF;
  1491. mChannelMask = 0xFFFFFFFF;
  1492. mImageAdjustment = NULL;
  1493. mVectorMask = NULL;
  1494. }
  1495. PSDLayerInfo::~PSDLayerInfo()
  1496. {
  1497. delete mPSDDescriptor;
  1498. delete mLayerMask;
  1499. delete mVectorMask;
  1500. delete mImageEffects;
  1501. }
  1502. bool PSDLayerInfo::ReadData()
  1503. {
  1504. AutoPerf gPerf("PSDLayerInfo::ReadData");
  1505. FileStream* fS = mPSDReader->mFS;
  1506. fS->SetPos(mImageDataStart);
  1507. mBits = new uint32[mWidth*mHeight];
  1508. memset(mBits, 0, mWidth*mHeight*sizeof(uint32));
  1509. int aSize = mWidth*mHeight;
  1510. if (mChannels.size() < 4)
  1511. {
  1512. for (int i = 0; i < aSize; i++)
  1513. mBits[i] = 0xFF000000;
  1514. }
  1515. else
  1516. {
  1517. for (int i = 0; i < aSize; i++)
  1518. mBits[i] = 0x00000000;
  1519. }
  1520. short* rowLengths = new short[std::max(mLayerMaskHeight, mHeight)];
  1521. for (int channelIdx = 0; channelIdx < (int) mChannels.size(); channelIdx++)
  1522. {
  1523. PSDChannelInfo* channel = &mChannels[channelIdx];
  1524. if (channel->mId == -2)
  1525. {
  1526. mLayerMask = new uint8[mLayerMaskWidth*mLayerMaskHeight];
  1527. int compression = fS->ReadInt16();
  1528. if (compression == 0)
  1529. {
  1530. for (int pos = 0; pos < aSize; pos++)
  1531. mLayerMask[pos] = fS->ReadInt8();
  1532. }
  1533. else
  1534. {
  1535. for (int aY = 0; aY < mLayerMaskHeight; aY++)
  1536. rowLengths[aY] = fS->ReadInt16();
  1537. for (int aY = 0; aY < mLayerMaskHeight; aY++)
  1538. {
  1539. int pos = aY * mLayerMaskWidth;
  1540. int readSize = rowLengths[aY];
  1541. while (readSize > 0)
  1542. {
  1543. int chunkSize = fS->ReadInt8();
  1544. readSize--;
  1545. if (chunkSize >= 0)
  1546. {
  1547. // String of literal data
  1548. chunkSize++;
  1549. readSize -= chunkSize;
  1550. while (chunkSize > 0)
  1551. {
  1552. mLayerMask[pos++] = fS->ReadInt8();
  1553. chunkSize--;
  1554. }
  1555. }
  1556. else if (chunkSize > -128)
  1557. {
  1558. // One byte repeated
  1559. chunkSize = 1 - chunkSize;
  1560. uint8 aData = fS->ReadInt8();
  1561. readSize--;
  1562. while (chunkSize > 0)
  1563. {
  1564. mLayerMask[pos++] = aData;
  1565. chunkSize--;
  1566. }
  1567. }
  1568. }
  1569. }
  1570. }
  1571. if (!mLayerMaskEnabled)
  1572. {
  1573. delete mLayerMask;
  1574. mLayerMask = NULL;
  1575. }
  1576. }
  1577. else
  1578. {
  1579. int shift = (2 - channel->mId) * 8;
  1580. int compression = fS->ReadInt16();
  1581. if (compression == 0)
  1582. {
  1583. for (int pos = 0; pos < aSize; pos++)
  1584. mBits[pos] |= ((uint32) (uint8) fS->ReadInt8()) << shift;
  1585. }
  1586. else
  1587. {
  1588. int maxSize = 0;
  1589. for (int aY = 0; aY < mHeight; aY++)
  1590. {
  1591. rowLengths[aY] = fS->ReadInt16();
  1592. maxSize = std::max(maxSize, (int) rowLengths[aY]);
  1593. }
  1594. uint8* rowData = new uint8[maxSize];
  1595. for (int aY = 0; aY < mHeight; aY++)
  1596. {
  1597. int pos = aY * mWidth;
  1598. int readSize = rowLengths[aY];
  1599. int readPos = 0;
  1600. int fileOfs = fS->GetPos();
  1601. fS->Read(rowData, readSize);
  1602. /*auto FixAlpha = [&]()
  1603. {
  1604. if (channel->mId == -1)
  1605. {
  1606. int fixedAlpha = std::min(255, rowData[readPos] * 0xFF / 0xB8);
  1607. fS->SetPos(fileOfs + readPos);
  1608. fS->Write((uint8)fixedAlpha);
  1609. fS->SetPos(fileOfs + readSize);
  1610. }
  1611. };*/
  1612. while (readPos < readSize)
  1613. {
  1614. int chunkSize = (int8) rowData[readPos++];
  1615. if (chunkSize >= 0)
  1616. {
  1617. // String of literal data
  1618. chunkSize++;
  1619. while (chunkSize > 0)
  1620. {
  1621. //FixAlpha();
  1622. mBits[pos++] |= ((uint32) rowData[readPos++]) << shift;
  1623. chunkSize--;
  1624. }
  1625. }
  1626. else if (chunkSize > -128)
  1627. {
  1628. // One byte repeated
  1629. chunkSize = 1 - chunkSize;
  1630. //FixAlpha();
  1631. uint32 aData = ((uint32) rowData[readPos++]) << shift;
  1632. while (chunkSize > 0)
  1633. {
  1634. mBits[pos++] |= aData;
  1635. chunkSize--;
  1636. }
  1637. }
  1638. }
  1639. }
  1640. delete [] rowData;
  1641. }
  1642. }
  1643. }
  1644. delete [] rowLengths;
  1645. if (((mLayerMask != NULL) || (mVectorMask != NULL)) && (!mLayerMaskHidesEffects))
  1646. ApplyMask(this);
  1647. uint32* ptr = mBits;
  1648. for (int i = 0; i < aSize; i++)
  1649. {
  1650. uint32 aColor = *ptr;
  1651. int a = (aColor & 0xFF000000) >> 24;
  1652. int r = (aColor & 0x00FF0000) >> 16;
  1653. int g = (aColor & 0x0000FF00) >> 8 ;
  1654. int b = (aColor & 0x000000FF) ;
  1655. *(ptr++) = (a << 24) | (b << 16) | (g << 8) | r;
  1656. }
  1657. return true;
  1658. }
  1659. void PSDLayerInfo::ApplyVectorMask(ImageData* imageData)
  1660. {
  1661. /*if (mVectorMask == NULL)
  1662. return;
  1663. agg::rendering_buffer g_alpha_mask_rbuf;
  1664. uint8* aBuffer = new uint8[mWidth*mHeight];
  1665. g_alpha_mask_rbuf.attach(aBuffer, mWidth, mHeight, mWidth);
  1666. typedef agg::pixfmt_gray8 pixfmt;
  1667. typedef agg::renderer_base<pixfmt> renderer_base;
  1668. typedef agg::renderer_scanline_aa_solid<renderer_base> renderer_scanline;
  1669. typedef agg::scanline_u8 scanline;
  1670. agg::rasterizer_scanline_aa<> g_rasterizer;
  1671. pixfmt pixf(g_alpha_mask_rbuf);
  1672. agg::path_storage m_path;
  1673. agg::gamma_lut<> m_gamma;
  1674. agg::renderer_base<pixfmt> ren_base(pixf);
  1675. ren_base.clear(agg::gray8(0));
  1676. renderer_scanline ren(ren_base);
  1677. agg::scanline_p8 sl;
  1678. agg::ellipse ell;
  1679. //renderer r(rb);
  1680. int i;
  1681. for(i = 0; i < 10; i++)
  1682. {
  1683. ell.init(rand() % mWidth,
  1684. rand() % mHeight,
  1685. rand() % 100 + 20,
  1686. rand() % 100 + 20,
  1687. 100);
  1688. g_rasterizer.add_path(ell);
  1689. ren.color(agg::gray8(rand() & 0xFF, rand() & 0xFF));
  1690. agg::render_scanlines(g_rasterizer, sl, ren);
  1691. }
  1692. int aSize = mWidth*mHeight;
  1693. for (int i = 0; i < aSize; i++)
  1694. {
  1695. uint8 aMask = aBuffer[i];
  1696. PackedColor* aColor = (PackedColor*) (&mBits[i]);
  1697. aColor->a = (aColor->a * aMask) / 255;
  1698. aColor->a = 255;
  1699. aColor->g = 128;
  1700. }
  1701. delete aBuffer;*/
  1702. }
  1703. void PSDLayerInfo::ApplyMask(ImageData* imageData)
  1704. {
  1705. AutoPerf gPerf("PSDLayerInfo::ApplyMask");
  1706. ApplyVectorMask(imageData);
  1707. if (mLayerMask == NULL)
  1708. return;
  1709. int maskStartX = std::max(mLayerMaskX, imageData->mX);
  1710. int maskStartY = std::max(mLayerMaskY, imageData->mY);
  1711. int maskEndX = std::min(mLayerMaskX + mLayerMaskWidth, imageData->mX + imageData->mWidth);
  1712. int maskEndY = std::min(mLayerMaskY + mLayerMaskHeight, imageData->mY + imageData->mHeight);
  1713. for (int y = maskStartY; y < maskEndY; y++)
  1714. {
  1715. uint32* colorBits = imageData->mBits + (y - imageData->mY)*imageData->mWidth;
  1716. uint8* maskBits = mLayerMask + (y - mLayerMaskY)*mLayerMaskWidth;
  1717. for (int x = maskStartX; x < maskEndX; x++)
  1718. {
  1719. uint8 aMask = maskBits[x - mLayerMaskX];
  1720. PackedColor* aColor = (PackedColor*) (&colorBits[x - imageData->mX]);
  1721. aColor->a = (aColor->a * aMask) / 255;
  1722. }
  1723. }
  1724. if (mLayerMaskDefault != 255)
  1725. {
  1726. // Clear out alpha on borders
  1727. // Top
  1728. for (int y = imageData->mY; y < maskStartY; y++)
  1729. {
  1730. uint32* colorBits = imageData->mBits + (y - imageData->mY)*imageData->mWidth;
  1731. for (int x = imageData->mX; x < imageData->mX + imageData->mWidth; x++)
  1732. {
  1733. PackedColor* aColor = (PackedColor*) (&colorBits[x - imageData->mX]);
  1734. aColor->a = 0;
  1735. }
  1736. }
  1737. // Bottom
  1738. for (int y = mLayerMaskY + mLayerMaskHeight; y < imageData->mY + imageData->mHeight; y++)
  1739. {
  1740. uint32* colorBits = imageData->mBits + (y - imageData->mY)*imageData->mWidth;
  1741. for (int x = imageData->mX; x < imageData->mX + imageData->mWidth; x++)
  1742. {
  1743. PackedColor* aColor = (PackedColor*) (&colorBits[x - imageData->mX]);
  1744. aColor->a = 0;
  1745. }
  1746. }
  1747. // Left
  1748. for (int y = imageData->mY; y < imageData->mY + imageData->mHeight; y++)
  1749. {
  1750. uint32* colorBits = imageData->mBits + (y - imageData->mY)*imageData->mWidth;
  1751. for (int x = imageData->mX; x < mLayerMaskX; x++)
  1752. {
  1753. PackedColor* aColor = (PackedColor*) (&colorBits[x - imageData->mX]);
  1754. aColor->a = 0;
  1755. }
  1756. }
  1757. // Right
  1758. for (int y = imageData->mY; y < imageData->mY + imageData->mHeight; y++)
  1759. {
  1760. uint32* colorBits = imageData->mBits + (y - imageData->mY)*imageData->mWidth;
  1761. for (int x = mLayerMaskX + mLayerMaskWidth; x < imageData->mX + imageData->mWidth; x++)
  1762. {
  1763. PackedColor* aColor = (PackedColor*) (&colorBits[x - imageData->mX]);
  1764. aColor->a = 0;
  1765. }
  1766. }
  1767. }
  1768. }