PSDReader.cpp 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175
  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. mImageDataSectStart = -1;
  150. }
  151. PSDReader::~PSDReader()
  152. {
  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. mImageDataSectStart = mFS->GetPos();
  1218. return true;
  1219. }
  1220. ImageData* PSDReader::ReadImageData()
  1221. {
  1222. mFS->SetPos(mImageDataSectStart);
  1223. int compression = mFS->ReadInt16();
  1224. if ((compression != 0) && (compression != 1))
  1225. return NULL;
  1226. ImageData* imageData = new ImageData();
  1227. imageData->CreateNew(mWidth, mHeight);
  1228. if (mChannels < 4)
  1229. imageData->Fill(0xFF000000);
  1230. int16** rowLengths = NULL;
  1231. int horz = mWidth;
  1232. int vert = mHeight;
  1233. int aSize = horz*vert;
  1234. if (compression == 1)
  1235. {
  1236. rowLengths = new int16*[mChannels];
  1237. for (int channel = 0; channel < mChannels; channel++)
  1238. {
  1239. rowLengths[channel] = new int16[vert];
  1240. for (int aY = 0; aY < vert; aY++)
  1241. rowLengths[channel][aY] = mFS->ReadInt16();
  1242. }
  1243. }
  1244. for (int channel = 0; channel < mChannels; channel++)
  1245. {
  1246. int shift = channel * 8;
  1247. // if (channel == mC + 1)
  1248. // shift = 24;
  1249. if (channel == 4)
  1250. break;
  1251. if (compression == 0)
  1252. {
  1253. for (int pos = 0; pos < aSize; pos++)
  1254. imageData->mBits[pos] |= ((uint32)(uint8)mFS->ReadInt8()) << shift;
  1255. }
  1256. else
  1257. {
  1258. for (int aY = 0; aY < vert; aY++)
  1259. {
  1260. int pos = aY * horz;
  1261. int readSize = rowLengths[channel][aY];
  1262. while (readSize > 0)
  1263. {
  1264. int chunkSize = mFS->ReadInt8();
  1265. readSize--;
  1266. if (chunkSize >= 0)
  1267. {
  1268. // String of literal data
  1269. chunkSize++;
  1270. readSize -= chunkSize;
  1271. while (chunkSize > 0)
  1272. {
  1273. imageData->mBits[pos++] |= ((uint32)(uint8)mFS->ReadInt8()) << shift;
  1274. chunkSize--;
  1275. }
  1276. }
  1277. else if (chunkSize > -128)
  1278. {
  1279. // One byte repeated
  1280. chunkSize = 1 - chunkSize;
  1281. uint32 aData = ((uint32)(uint8)mFS->ReadInt8()) << shift;
  1282. readSize--;
  1283. while (chunkSize > 0)
  1284. {
  1285. imageData->mBits[pos++] |= aData;
  1286. chunkSize--;
  1287. }
  1288. }
  1289. }
  1290. }
  1291. }
  1292. }
  1293. for (int i = 0; i < aSize; i++)
  1294. {
  1295. PackedColor& packedColor = *(PackedColor*)(&imageData->mBits[i]);
  1296. if (packedColor.a != 0)
  1297. {
  1298. packedColor.r = BF_CLAMP((packedColor.r - (0xFF - packedColor.a)) * 0xFF / packedColor.a, 0, 0xFF);
  1299. packedColor.g = BF_CLAMP((packedColor.g - (0xFF - packedColor.a)) * 0xFF / packedColor.a, 0, 0xFF);
  1300. packedColor.b = BF_CLAMP((packedColor.b - (0xFF - packedColor.a)) * 0xFF / packedColor.a, 0, 0xFF);
  1301. }
  1302. }
  1303. if (rowLengths != NULL)
  1304. {
  1305. for (int i = 0; i < mChannels; i++)
  1306. delete rowLengths[i];
  1307. }
  1308. return imageData;
  1309. }
  1310. Texture* PSDReader::LoadLayerTexture(int layerIdx, int* ofsX, int* ofsY) // -1 = composited image
  1311. {
  1312. if ((layerIdx < 0) || (layerIdx >= (int) mPSDLayerInfoVector.size()))
  1313. return NULL;
  1314. //TODO: This is a special case for flattening here, just on the first layer...
  1315. PSDLayerInfo* prevLayer = (layerIdx == 1) ? mPSDLayerInfoVector[layerIdx - 1] : NULL;
  1316. if (prevLayer != NULL)
  1317. {
  1318. if (prevLayer->mBits == NULL)
  1319. prevLayer->ReadData();
  1320. }
  1321. PSDLayerInfo* aLayer = mPSDLayerInfoVector[layerIdx];
  1322. if (aLayer->mBits == NULL)
  1323. aLayer->ReadData();
  1324. ImageData* anImageData = aLayer;
  1325. anImageData = aLayer->mImageEffects->FlattenInto(prevLayer, aLayer, aLayer, NULL, NULL);
  1326. Texture* texture = gBFApp->mRenderDevice->LoadTexture(anImageData, false);
  1327. *ofsX = anImageData->mX;
  1328. *ofsY = anImageData->mY;
  1329. if (anImageData != aLayer)
  1330. delete anImageData;
  1331. return texture;
  1332. }
  1333. typedef std::vector<PSDLayerInfo*> PSDLayerVector;
  1334. typedef std::map<int, PSDLayerVector> LayerInfoMap;
  1335. typedef std::set<int> IntSet;
  1336. ImageData* PSDReader::MergeLayers(PSDLayerInfo* group, const std::vector<int>& layerIndices, ImageData* bottomImage)
  1337. {
  1338. AutoPerf gPerf("PSDReader::MergeLayers");
  1339. bool isImageAllocated = false;
  1340. ImageData* combinedImage = bottomImage;
  1341. ImageData* prevBottom = NULL;
  1342. bool doPostGroupBlend = false;
  1343. bool needsPrevBottom = (group != NULL) && (group->mOpacity != 255);
  1344. for (int indexIdx = 0; indexIdx < (int) layerIndices.size(); indexIdx++)
  1345. {
  1346. PSDLayerInfo* layerInfo = mPSDLayerInfoVector[layerIndices[indexIdx]];
  1347. if (layerInfo->mBits == NULL)
  1348. layerInfo->ReadData();
  1349. if (layerInfo->mKnockout == 1) // Shallow
  1350. needsPrevBottom = true;
  1351. }
  1352. if ((group != NULL) && (bottomImage != NULL) && (group->mSectionBlendMode != 'pass'))
  1353. {
  1354. doPostGroupBlend = true;
  1355. combinedImage = NULL;
  1356. }
  1357. if (needsPrevBottom && (bottomImage != NULL))
  1358. prevBottom = bottomImage->Duplicate();
  1359. bool hasAllocatedClipBase = false;
  1360. PSDLayerInfo* clipBaseLayer = NULL;
  1361. ImageData* clipBaseImage = NULL;
  1362. for (int indexIdx = 0; indexIdx <= (int) layerIndices.size(); indexIdx++)
  1363. {
  1364. PSDLayerInfo* layerInfo = NULL;
  1365. if (indexIdx < (int) layerIndices.size())
  1366. layerInfo = mPSDLayerInfoVector[layerIndices[indexIdx]];
  1367. PSDLayerInfo* pendingFlushLayer = NULL;
  1368. ImageData* pendingFlushImage = NULL;
  1369. if ((layerInfo != NULL) && (!layerInfo->mBaseClipping))
  1370. {
  1371. if (clipBaseLayer != NULL)
  1372. {
  1373. if (clipBaseLayer->mBlendClippedElementsAsGroup)
  1374. {
  1375. ImageData* newImage = layerInfo->mImageEffects->FlattenInto(clipBaseImage, layerInfo, layerInfo, prevBottom, NULL);
  1376. SetImageAlpha(newImage, 255);
  1377. if (hasAllocatedClipBase)
  1378. delete clipBaseImage;
  1379. hasAllocatedClipBase = true;
  1380. clipBaseImage = newImage;
  1381. }
  1382. else
  1383. {
  1384. if (clipBaseImage == clipBaseLayer)
  1385. {
  1386. /*clipBaseImage = clipBaseImage->Duplicate();
  1387. //SetImageAlpha(clipBaseImage, clipBaseLayer->mFillOpacity);
  1388. SetImageAlpha(clipBaseImage, 0);
  1389. hasAllocatedClipBase = true;*/
  1390. //clipBaseImage = layerInfo->mImageEffects->FlattenInto(combinedImage, clipBaseLayer, clipBaseLayer, prevBottom, NULL);
  1391. //hasAllocatedClipBase = true;
  1392. //combinedImage = combinedImage->Duplicate();
  1393. ImageData* newImage = NULL;
  1394. if (clipBaseLayer->mKnockout == 0) // No Knockout
  1395. {
  1396. newImage = CreateResizedImageUnion(combinedImage, clipBaseLayer->mX, clipBaseLayer->mY, clipBaseLayer->mWidth, clipBaseLayer->mHeight);
  1397. }
  1398. else if (clipBaseLayer->mKnockout == 1) // Shallow Knockout
  1399. {
  1400. newImage = CreateResizedImageUnion(prevBottom, clipBaseLayer->mX, clipBaseLayer->mY, clipBaseLayer->mWidth, clipBaseLayer->mHeight);
  1401. }
  1402. else // Deep Knockout
  1403. {
  1404. newImage = CreateEmptyResizedImageUnion(combinedImage, clipBaseLayer->mX, clipBaseLayer->mY, clipBaseLayer->mWidth, clipBaseLayer->mHeight);
  1405. }
  1406. if (combinedImage != NULL)
  1407. BlendImage(newImage, clipBaseImage, clipBaseImage->mX - combinedImage->mX, clipBaseImage->mY - combinedImage->mY, clipBaseLayer->mFillOpacity / 255.0f, clipBaseLayer->mBlendMode, true);
  1408. if (hasAllocatedClipBase)
  1409. delete clipBaseImage;
  1410. hasAllocatedClipBase = true;
  1411. clipBaseImage = newImage;
  1412. }
  1413. //ImageData* tempImage = layerInfo->Duplicate();
  1414. //SetImageAlpha(tempImage, 255);
  1415. //ImageData* newImage = layerInfo->mImageEffects->FlattenInto(clipBaseImage, layerInfo, tempImage, prevBottom);
  1416. ImageData* newImage = layerInfo->mImageEffects->FlattenInto(clipBaseImage, layerInfo, layerInfo, prevBottom, NULL);
  1417. if (hasAllocatedClipBase)
  1418. delete clipBaseImage;
  1419. hasAllocatedClipBase = true;
  1420. clipBaseImage = newImage;
  1421. }
  1422. }
  1423. }
  1424. else
  1425. {
  1426. pendingFlushImage = clipBaseImage;
  1427. pendingFlushLayer = clipBaseLayer;
  1428. clipBaseImage = layerInfo;
  1429. clipBaseLayer = layerInfo;
  1430. }
  1431. if (pendingFlushImage != NULL)
  1432. {
  1433. ImageData* newImage = NULL;
  1434. if ((pendingFlushLayer != pendingFlushImage) && (!pendingFlushLayer->mBlendClippedElementsAsGroup))
  1435. {
  1436. /*hasAllocatedClipBase = false;
  1437. BlendImagesTogether(combinedImage, pendingFlushImage, pendingFlushLayer);
  1438. newImage = combinedImage;*/
  1439. newImage = pendingFlushLayer->mImageEffects->FlattenInto(combinedImage, pendingFlushLayer, pendingFlushLayer, prevBottom, pendingFlushImage);
  1440. }
  1441. else
  1442. {
  1443. if ((pendingFlushImage != pendingFlushLayer) && (pendingFlushLayer->mBlendClippedElementsAsGroup))
  1444. SetImageAlpha(pendingFlushImage, pendingFlushLayer);
  1445. newImage = pendingFlushLayer->mImageEffects->FlattenInto(combinedImage, pendingFlushLayer, pendingFlushImage, prevBottom, NULL);
  1446. }
  1447. if (isImageAllocated)
  1448. delete combinedImage;
  1449. isImageAllocated = newImage == pendingFlushLayer;
  1450. combinedImage = newImage;
  1451. if (hasAllocatedClipBase)
  1452. delete pendingFlushImage;
  1453. }
  1454. // Is this a group?
  1455. if ((layerInfo != NULL) && (layerInfo->mParent != group))
  1456. {
  1457. std::vector<int> aChildren;
  1458. //indexIdx--;
  1459. for (; indexIdx < (int) layerIndices.size(); indexIdx++)
  1460. {
  1461. layerInfo = mPSDLayerInfoVector[layerIndices[indexIdx]];
  1462. if (layerInfo->mParent == group)
  1463. break;
  1464. aChildren.push_back(layerInfo->mIdx);
  1465. }
  1466. ImageData* newImage = MergeLayers(layerInfo, aChildren, combinedImage);
  1467. if (isImageAllocated)
  1468. delete combinedImage;
  1469. isImageAllocated = newImage == layerInfo;
  1470. combinedImage = newImage;
  1471. if (hasAllocatedClipBase)
  1472. delete pendingFlushImage;
  1473. clipBaseImage = NULL;
  1474. clipBaseLayer = NULL;
  1475. }
  1476. }
  1477. if (hasAllocatedClipBase)
  1478. delete clipBaseImage;
  1479. if (doPostGroupBlend)
  1480. {
  1481. ImageData* blendInto = CreateResizedImageUnion(bottomImage, combinedImage->mX, combinedImage->mY, combinedImage->mWidth, combinedImage->mHeight);
  1482. BlendImage(blendInto, combinedImage, combinedImage->mX - blendInto->mX, combinedImage->mY - blendInto->mY,
  1483. 1.0f, group->mSectionBlendMode);
  1484. if (isImageAllocated)
  1485. delete combinedImage;
  1486. combinedImage = blendInto;
  1487. }
  1488. if ((group != NULL) && (group->mOpacity != 255))
  1489. {
  1490. // Crossfade together with previous bottom
  1491. CrossfadeImage(prevBottom, combinedImage, group->mOpacity / 255.0f);
  1492. delete prevBottom;
  1493. }
  1494. return combinedImage;
  1495. }
  1496. Texture* PSDReader::LoadMergedLayerTexture(const std::vector<int>& layerIndices, int* ofsX, int* ofsY)
  1497. {
  1498. AutoPerf gPerf("PSDReader::LoadMergedLayerTexture");
  1499. LayerInfoMap groupMap;
  1500. if (layerIndices.size() == 0)
  1501. return NULL;
  1502. IntSet layerSet;
  1503. // Fix up the layer set - add parents and remove children whose parents don't appear in the list
  1504. for (int indexIdx = 0; indexIdx < (int) layerIndices.size(); indexIdx++)
  1505. {
  1506. PSDLayerInfo* layerInfo = mPSDLayerInfoVector[layerIndices[indexIdx]];
  1507. layerSet.insert(layerInfo->mIdx);
  1508. }
  1509. for (int indexIdx = 0; indexIdx < (int) layerIndices.size(); indexIdx++)
  1510. {
  1511. PSDLayerInfo* layerInfo = mPSDLayerInfoVector[layerIndices[indexIdx]];
  1512. PSDLayerInfo* parentCheck = layerInfo->mParent;
  1513. bool includeLayer = true;
  1514. if (!layerInfo->mBaseClipping)
  1515. {
  1516. int baseIdx = layerInfo->mIdx + 1;
  1517. while (baseIdx < (int) mPSDLayerInfoVector.size())
  1518. {
  1519. PSDLayerInfo* baseCheck = mPSDLayerInfoVector[baseIdx];
  1520. if (baseCheck->mBaseClipping)
  1521. {
  1522. includeLayer &= baseCheck->mVisible;
  1523. break;
  1524. }
  1525. baseIdx++;
  1526. }
  1527. }
  1528. while (parentCheck != NULL)
  1529. {
  1530. includeLayer &= layerSet.find(parentCheck->mIdx) != layerSet.end();
  1531. parentCheck = parentCheck->mParent;
  1532. }
  1533. if (!includeLayer)
  1534. layerSet.erase(layerSet.find(layerInfo->mIdx));
  1535. }
  1536. std::vector<int> aLayerIndices;
  1537. // Composite
  1538. IntSet::reverse_iterator revItr = layerSet.rbegin();
  1539. while (revItr != layerSet.rend())
  1540. {
  1541. //PSDLayerInfo* layerInfo = mPSDLayerInfoVector[*revItr];
  1542. aLayerIndices.push_back(*revItr);
  1543. revItr++;
  1544. }
  1545. ImageData* combinedImage = MergeLayers(NULL, aLayerIndices, NULL);
  1546. Texture* texture = gBFApp->mRenderDevice->LoadTexture(combinedImage, false);
  1547. *ofsX = combinedImage->mX;
  1548. *ofsY = combinedImage->mY;
  1549. if (dynamic_cast<PSDLayerInfo*>(combinedImage) == NULL)
  1550. delete combinedImage;
  1551. return texture;
  1552. }
  1553. ///
  1554. PSDLayerInfo::PSDLayerInfo()
  1555. {
  1556. mLayerId = 0;
  1557. mSectionDividerType = 0;
  1558. mSectionBlendMode = 0;
  1559. mPSDDescriptor = NULL;
  1560. mRefX = 0;
  1561. mRefY = 0;
  1562. mFillOpacity = 255;
  1563. mImageEffects = new ImageEffects();
  1564. mKnockout = 0;
  1565. mLayerMask = NULL;
  1566. mLayerMaskEnabled = false;
  1567. mLayerMaskInverted = false;
  1568. mLayerMaskDefault = 255;
  1569. mLayerMaskX = 0;
  1570. mLayerMaskY = 0;
  1571. mLayerMaskWidth = 0;
  1572. mLayerMaskHeight = 0;
  1573. mBlendInteriorEffectsAsGroup = false;
  1574. mBlendClippedElementsAsGroup = true;
  1575. mTransparencyShapesLayer = true;
  1576. mLayerMaskHidesEffects = false;
  1577. mVectorMaskHidesEffects = false;
  1578. mBlendingRangeSourceStart = 0;
  1579. mBlendingRangeSourceEnd = 0xFFFFFFFF;
  1580. mBlendingRangeDestStart = 0;
  1581. mBlendingRangeDestEnd = 0xFFFFFFFF;
  1582. mChannelMask = 0xFFFFFFFF;
  1583. mImageAdjustment = NULL;
  1584. mVectorMask = NULL;
  1585. }
  1586. PSDLayerInfo::~PSDLayerInfo()
  1587. {
  1588. delete mPSDDescriptor;
  1589. delete mLayerMask;
  1590. delete mVectorMask;
  1591. delete mImageEffects;
  1592. }
  1593. bool PSDLayerInfo::ReadData()
  1594. {
  1595. AutoPerf gPerf("PSDLayerInfo::ReadData");
  1596. FileStream* fS = mPSDReader->mFS;
  1597. fS->SetPos(mImageDataStart);
  1598. mBits = new uint32[mWidth*mHeight];
  1599. memset(mBits, 0, mWidth*mHeight*sizeof(uint32));
  1600. int aSize = mWidth*mHeight;
  1601. if (mChannels.size() < 4)
  1602. {
  1603. for (int i = 0; i < aSize; i++)
  1604. mBits[i] = 0xFF000000;
  1605. }
  1606. else
  1607. {
  1608. for (int i = 0; i < aSize; i++)
  1609. mBits[i] = 0x00000000;
  1610. }
  1611. short* rowLengths = new short[std::max(mLayerMaskHeight, mHeight)];
  1612. for (int channelIdx = 0; channelIdx < (int) mChannels.size(); channelIdx++)
  1613. {
  1614. PSDChannelInfo* channel = &mChannels[channelIdx];
  1615. if (channel->mId == -2)
  1616. {
  1617. mLayerMask = new uint8[mLayerMaskWidth*mLayerMaskHeight];
  1618. int compression = fS->ReadInt16();
  1619. if (compression == 0)
  1620. {
  1621. for (int pos = 0; pos < aSize; pos++)
  1622. mLayerMask[pos] = fS->ReadInt8();
  1623. }
  1624. else
  1625. {
  1626. for (int aY = 0; aY < mLayerMaskHeight; aY++)
  1627. rowLengths[aY] = fS->ReadInt16();
  1628. for (int aY = 0; aY < mLayerMaskHeight; aY++)
  1629. {
  1630. int pos = aY * mLayerMaskWidth;
  1631. int readSize = rowLengths[aY];
  1632. while (readSize > 0)
  1633. {
  1634. int chunkSize = fS->ReadInt8();
  1635. readSize--;
  1636. if (chunkSize >= 0)
  1637. {
  1638. // String of literal data
  1639. chunkSize++;
  1640. readSize -= chunkSize;
  1641. while (chunkSize > 0)
  1642. {
  1643. mLayerMask[pos++] = fS->ReadInt8();
  1644. chunkSize--;
  1645. }
  1646. }
  1647. else if (chunkSize > -128)
  1648. {
  1649. // One byte repeated
  1650. chunkSize = 1 - chunkSize;
  1651. uint8 aData = fS->ReadInt8();
  1652. readSize--;
  1653. while (chunkSize > 0)
  1654. {
  1655. mLayerMask[pos++] = aData;
  1656. chunkSize--;
  1657. }
  1658. }
  1659. }
  1660. }
  1661. }
  1662. if (!mLayerMaskEnabled)
  1663. {
  1664. delete mLayerMask;
  1665. mLayerMask = NULL;
  1666. }
  1667. }
  1668. else
  1669. {
  1670. int shift = (2 - channel->mId) * 8;
  1671. int compression = fS->ReadInt16();
  1672. if (compression == 0)
  1673. {
  1674. for (int pos = 0; pos < aSize; pos++)
  1675. mBits[pos] |= ((uint32) (uint8) fS->ReadInt8()) << shift;
  1676. }
  1677. else
  1678. {
  1679. int maxSize = 0;
  1680. for (int aY = 0; aY < mHeight; aY++)
  1681. {
  1682. rowLengths[aY] = fS->ReadInt16();
  1683. maxSize = std::max(maxSize, (int) rowLengths[aY]);
  1684. }
  1685. uint8* rowData = new uint8[maxSize];
  1686. for (int aY = 0; aY < mHeight; aY++)
  1687. {
  1688. int pos = aY * mWidth;
  1689. int readSize = rowLengths[aY];
  1690. int readPos = 0;
  1691. int fileOfs = fS->GetPos();
  1692. fS->Read(rowData, readSize);
  1693. /*auto FixAlpha = [&]()
  1694. {
  1695. if (channel->mId == -1)
  1696. {
  1697. int fixedAlpha = std::min(255, rowData[readPos] * 0xFF / 0xB8);
  1698. fS->SetPos(fileOfs + readPos);
  1699. fS->Write((uint8)fixedAlpha);
  1700. fS->SetPos(fileOfs + readSize);
  1701. }
  1702. };*/
  1703. while (readPos < readSize)
  1704. {
  1705. int chunkSize = (int8) rowData[readPos++];
  1706. if (chunkSize >= 0)
  1707. {
  1708. // String of literal data
  1709. chunkSize++;
  1710. while (chunkSize > 0)
  1711. {
  1712. //FixAlpha();
  1713. mBits[pos++] |= ((uint32) rowData[readPos++]) << shift;
  1714. chunkSize--;
  1715. }
  1716. }
  1717. else if (chunkSize > -128)
  1718. {
  1719. // One byte repeated
  1720. chunkSize = 1 - chunkSize;
  1721. //FixAlpha();
  1722. uint32 aData = ((uint32) rowData[readPos++]) << shift;
  1723. while (chunkSize > 0)
  1724. {
  1725. mBits[pos++] |= aData;
  1726. chunkSize--;
  1727. }
  1728. }
  1729. }
  1730. }
  1731. delete [] rowData;
  1732. }
  1733. }
  1734. }
  1735. delete [] rowLengths;
  1736. if (((mLayerMask != NULL) || (mVectorMask != NULL)) && (!mLayerMaskHidesEffects))
  1737. ApplyMask(this);
  1738. uint32* ptr = mBits;
  1739. for (int i = 0; i < aSize; i++)
  1740. {
  1741. uint32 aColor = *ptr;
  1742. int a = (aColor & 0xFF000000) >> 24;
  1743. int r = (aColor & 0x00FF0000) >> 16;
  1744. int g = (aColor & 0x0000FF00) >> 8 ;
  1745. int b = (aColor & 0x000000FF) ;
  1746. *(ptr++) = (a << 24) | (b << 16) | (g << 8) | r;
  1747. }
  1748. return true;
  1749. }
  1750. void PSDLayerInfo::ApplyVectorMask(ImageData* imageData)
  1751. {
  1752. /*if (mVectorMask == NULL)
  1753. return;
  1754. agg::rendering_buffer g_alpha_mask_rbuf;
  1755. uint8* aBuffer = new uint8[mWidth*mHeight];
  1756. g_alpha_mask_rbuf.attach(aBuffer, mWidth, mHeight, mWidth);
  1757. typedef agg::pixfmt_gray8 pixfmt;
  1758. typedef agg::renderer_base<pixfmt> renderer_base;
  1759. typedef agg::renderer_scanline_aa_solid<renderer_base> renderer_scanline;
  1760. typedef agg::scanline_u8 scanline;
  1761. agg::rasterizer_scanline_aa<> g_rasterizer;
  1762. pixfmt pixf(g_alpha_mask_rbuf);
  1763. agg::path_storage m_path;
  1764. agg::gamma_lut<> m_gamma;
  1765. agg::renderer_base<pixfmt> ren_base(pixf);
  1766. ren_base.clear(agg::gray8(0));
  1767. renderer_scanline ren(ren_base);
  1768. agg::scanline_p8 sl;
  1769. agg::ellipse ell;
  1770. //renderer r(rb);
  1771. int i;
  1772. for(i = 0; i < 10; i++)
  1773. {
  1774. ell.init(rand() % mWidth,
  1775. rand() % mHeight,
  1776. rand() % 100 + 20,
  1777. rand() % 100 + 20,
  1778. 100);
  1779. g_rasterizer.add_path(ell);
  1780. ren.color(agg::gray8(rand() & 0xFF, rand() & 0xFF));
  1781. agg::render_scanlines(g_rasterizer, sl, ren);
  1782. }
  1783. int aSize = mWidth*mHeight;
  1784. for (int i = 0; i < aSize; i++)
  1785. {
  1786. uint8 aMask = aBuffer[i];
  1787. PackedColor* aColor = (PackedColor*) (&mBits[i]);
  1788. aColor->a = (aColor->a * aMask) / 255;
  1789. aColor->a = 255;
  1790. aColor->g = 128;
  1791. }
  1792. delete aBuffer;*/
  1793. }
  1794. void PSDLayerInfo::ApplyMask(ImageData* imageData)
  1795. {
  1796. AutoPerf gPerf("PSDLayerInfo::ApplyMask");
  1797. ApplyVectorMask(imageData);
  1798. if (mLayerMask == NULL)
  1799. return;
  1800. int maskStartX = std::max(mLayerMaskX, imageData->mX);
  1801. int maskStartY = std::max(mLayerMaskY, imageData->mY);
  1802. int maskEndX = std::min(mLayerMaskX + mLayerMaskWidth, imageData->mX + imageData->mWidth);
  1803. int maskEndY = std::min(mLayerMaskY + mLayerMaskHeight, imageData->mY + imageData->mHeight);
  1804. for (int y = maskStartY; y < maskEndY; y++)
  1805. {
  1806. uint32* colorBits = imageData->mBits + (y - imageData->mY)*imageData->mWidth;
  1807. uint8* maskBits = mLayerMask + (y - mLayerMaskY)*mLayerMaskWidth;
  1808. for (int x = maskStartX; x < maskEndX; x++)
  1809. {
  1810. uint8 aMask = maskBits[x - mLayerMaskX];
  1811. PackedColor* aColor = (PackedColor*) (&colorBits[x - imageData->mX]);
  1812. aColor->a = (aColor->a * aMask) / 255;
  1813. }
  1814. }
  1815. if (mLayerMaskDefault != 255)
  1816. {
  1817. // Clear out alpha on borders
  1818. // Top
  1819. for (int y = imageData->mY; y < maskStartY; y++)
  1820. {
  1821. uint32* colorBits = imageData->mBits + (y - imageData->mY)*imageData->mWidth;
  1822. for (int x = imageData->mX; x < imageData->mX + imageData->mWidth; x++)
  1823. {
  1824. PackedColor* aColor = (PackedColor*) (&colorBits[x - imageData->mX]);
  1825. aColor->a = 0;
  1826. }
  1827. }
  1828. // Bottom
  1829. for (int y = mLayerMaskY + mLayerMaskHeight; y < imageData->mY + imageData->mHeight; y++)
  1830. {
  1831. uint32* colorBits = imageData->mBits + (y - imageData->mY)*imageData->mWidth;
  1832. for (int x = imageData->mX; x < imageData->mX + imageData->mWidth; x++)
  1833. {
  1834. PackedColor* aColor = (PackedColor*) (&colorBits[x - imageData->mX]);
  1835. aColor->a = 0;
  1836. }
  1837. }
  1838. // Left
  1839. for (int y = imageData->mY; y < imageData->mY + imageData->mHeight; y++)
  1840. {
  1841. uint32* colorBits = imageData->mBits + (y - imageData->mY)*imageData->mWidth;
  1842. for (int x = imageData->mX; x < mLayerMaskX; x++)
  1843. {
  1844. PackedColor* aColor = (PackedColor*) (&colorBits[x - imageData->mX]);
  1845. aColor->a = 0;
  1846. }
  1847. }
  1848. // Right
  1849. for (int y = imageData->mY; y < imageData->mY + imageData->mHeight; y++)
  1850. {
  1851. uint32* colorBits = imageData->mBits + (y - imageData->mY)*imageData->mWidth;
  1852. for (int x = mLayerMaskX + mLayerMaskWidth; x < imageData->mX + imageData->mWidth; x++)
  1853. {
  1854. PackedColor* aColor = (PackedColor*) (&colorBits[x - imageData->mX]);
  1855. aColor->a = 0;
  1856. }
  1857. }
  1858. }
  1859. }