No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PsdFile.cs 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Photoshop PSD FileType Plugin for Paint.NET
  4. // http://psdplugin.codeplex.com/
  5. //
  6. // This software is provided under the MIT License:
  7. // Copyright (c) 2006-2007 Frank Blumenberg
  8. // Copyright (c) 2010-2016 Tao Yue
  9. //
  10. // Portions of this file are provided under the BSD 3-clause License:
  11. // Copyright (c) 2006, Jonas Beckeman
  12. //
  13. // See LICENSE.txt for complete licensing and attribution information.
  14. //
  15. /////////////////////////////////////////////////////////////////////////////////
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Diagnostics;
  19. using PDNWrapper;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Text;
  23. using PaintDotNet.Data.PhotoshopFileType;
  24. namespace PhotoshopFile
  25. {
  26. [Flags]
  27. internal enum ELoadFlag
  28. {
  29. Header = 1,
  30. ColorMode = Header | 1 << 2,
  31. ImageData = ColorMode | 1 << 3,
  32. All = Header | ColorMode | ImageData
  33. }
  34. internal enum PsdColorMode
  35. {
  36. Bitmap = 0,
  37. Grayscale = 1,
  38. Indexed = 2,
  39. RGB = 3,
  40. CMYK = 4,
  41. Multichannel = 7,
  42. Duotone = 8,
  43. Lab = 9
  44. };
  45. internal enum PsdFileVersion : short
  46. {
  47. Psd = 1,
  48. PsbLargeDocument = 2
  49. }
  50. internal class PsdFile
  51. {
  52. #region Constructors
  53. ELoadFlag m_LoadFlag;
  54. public PsdFile(PsdFileVersion version = PsdFileVersion.Psd)
  55. {
  56. Version = version;
  57. BaseLayer = new Layer(this);
  58. ImageResources = new ImageResources();
  59. Layers = new List<Layer>();
  60. AdditionalInfo = new List<LayerInfo>();
  61. }
  62. public PsdFile(string filename, LoadContext loadContext, ELoadFlag loadFlag = ELoadFlag.All)
  63. : this()
  64. {
  65. using (var stream = new FileStream(filename, FileMode.Open))
  66. {
  67. Load(stream, loadContext, loadFlag);
  68. }
  69. }
  70. public PsdFile(Stream stream, LoadContext loadContext, ELoadFlag loadFlag = ELoadFlag.All)
  71. : this()
  72. {
  73. Load(stream, loadContext, loadFlag);
  74. }
  75. #endregion
  76. #region Load and save
  77. internal LoadContext LoadContext { get; private set; }
  78. private void Load(Stream stream, LoadContext loadContext, ELoadFlag loadFlag)
  79. {
  80. LoadContext = loadContext;
  81. var reader = new PsdBinaryReader(stream, loadContext.Encoding);
  82. if ((loadFlag & ELoadFlag.Header) == ELoadFlag.Header)
  83. LoadHeader(reader);
  84. if ((loadFlag & ELoadFlag.ColorMode) == ELoadFlag.ColorMode)
  85. LoadColorModeData(reader);
  86. if ((loadFlag & ELoadFlag.ImageData) == ELoadFlag.ImageData)
  87. {
  88. LoadImageResources(reader);
  89. LoadLayerAndMaskInfo(reader);
  90. LoadImage(reader);
  91. DecompressImages();
  92. }
  93. reader.Dispose();
  94. reader = null;
  95. }
  96. #endregion
  97. #region Header
  98. /// <summary>
  99. /// Photoshop file format version.
  100. /// </summary>
  101. public PsdFileVersion Version { get; private set; }
  102. public bool IsLargeDocument
  103. {
  104. get { return Version == PsdFileVersion.PsbLargeDocument; }
  105. }
  106. private Int16 channelCount;
  107. /// <summary>
  108. /// The number of channels in the image, including any alpha channels.
  109. /// </summary>
  110. public Int16 ChannelCount
  111. {
  112. get { return channelCount; }
  113. set
  114. {
  115. if (value < 1 || value > 56)
  116. throw new ArgumentException("Number of channels must be from 1 to 56.");
  117. channelCount = value;
  118. }
  119. }
  120. private void CheckDimension(int dimension)
  121. {
  122. if (dimension < 1)
  123. {
  124. throw new ArgumentException("Image dimension must be at least 1.");
  125. }
  126. if ((Version == PsdFileVersion.Psd) && (dimension > 30000))
  127. {
  128. throw new ArgumentException("PSD image dimension cannot exceed 30000.");
  129. }
  130. if ((Version == PsdFileVersion.PsbLargeDocument) && (dimension > 300000))
  131. {
  132. throw new ArgumentException("PSB image dimension cannot exceed 300000.");
  133. }
  134. }
  135. /// <summary>
  136. /// The height of the image in pixels.
  137. /// </summary>
  138. public int RowCount
  139. {
  140. get { return this.BaseLayer.Rect.Height; }
  141. set
  142. {
  143. CheckDimension(value);
  144. BaseLayer.Rect = new Rectangle(0, 0, BaseLayer.Rect.Width, value);
  145. }
  146. }
  147. /// <summary>
  148. /// The width of the image in pixels.
  149. /// </summary>
  150. public int ColumnCount
  151. {
  152. get { return this.BaseLayer.Rect.Width; }
  153. set
  154. {
  155. CheckDimension(value);
  156. BaseLayer.Rect = new Rectangle(0, 0, value, BaseLayer.Rect.Height);
  157. }
  158. }
  159. private int bitDepth;
  160. /// <summary>
  161. /// The number of bits per channel. Supported values are 1, 8, 16, and 32.
  162. /// </summary>
  163. public int BitDepth
  164. {
  165. get { return bitDepth; }
  166. set
  167. {
  168. switch (value)
  169. {
  170. case 1:
  171. case 8:
  172. case 16:
  173. case 32:
  174. bitDepth = value;
  175. break;
  176. default:
  177. throw new NotImplementedException("Invalid bit depth.");
  178. }
  179. }
  180. }
  181. /// <summary>
  182. /// The color mode of the file.
  183. /// </summary>
  184. public PsdColorMode ColorMode { get; set; }
  185. ///////////////////////////////////////////////////////////////////////////
  186. private void LoadHeader(PsdBinaryReader reader)
  187. {
  188. Util.DebugMessage(reader.BaseStream, "Load, Begin, File header");
  189. var signature = reader.ReadAsciiChars(4);
  190. if (signature != "8BPS")
  191. throw new PsdInvalidException("The given stream is not a valid PSD file");
  192. Version = (PsdFileVersion)reader.ReadInt16();
  193. Util.DebugMessage(reader.BaseStream, "Load, Info, Version {0}", (int)Version);
  194. if ((Version != PsdFileVersion.Psd)
  195. && (Version != PsdFileVersion.PsbLargeDocument))
  196. {
  197. throw new PsdInvalidException("The PSD file has an unknown version");
  198. }
  199. //6 bytes reserved
  200. reader.BaseStream.Position += 6;
  201. this.ChannelCount = reader.ReadInt16();
  202. this.RowCount = reader.ReadInt32();
  203. this.ColumnCount = reader.ReadInt32();
  204. BitDepth = reader.ReadInt16();
  205. ColorMode = (PsdColorMode)reader.ReadInt16();
  206. Util.DebugMessage(reader.BaseStream, "Load, End, File header");
  207. }
  208. #endregion
  209. ///////////////////////////////////////////////////////////////////////////
  210. #region ColorModeData
  211. /// <summary>
  212. /// If ColorMode is ColorModes.Indexed, the following 768 bytes will contain
  213. /// a 256-color palette. If the ColorMode is ColorModes.Duotone, the data
  214. /// following presumably consists of screen parameters and other related information.
  215. /// Unfortunately, it is intentionally not documented by Adobe, and non-Photoshop
  216. /// readers are advised to treat duotone images as gray-scale images.
  217. /// </summary>
  218. public byte[] ColorModeData = new byte[0];
  219. private void LoadColorModeData(PsdBinaryReader reader)
  220. {
  221. Util.DebugMessage(reader.BaseStream, "Load, Begin, ColorModeData");
  222. var paletteLength = reader.ReadUInt32();
  223. if (paletteLength > 0)
  224. {
  225. ColorModeData = reader.ReadBytes((int)paletteLength);
  226. }
  227. Util.DebugMessage(reader.BaseStream, "Load, End, ColorModeData");
  228. }
  229. #endregion
  230. ///////////////////////////////////////////////////////////////////////////
  231. #region ImageResources
  232. /// <summary>
  233. /// The Image resource blocks for the file
  234. /// </summary>
  235. public ImageResources ImageResources { get; set; }
  236. public ResolutionInfo Resolution
  237. {
  238. get
  239. {
  240. return (ResolutionInfo)ImageResources.Get(ResourceID.ResolutionInfo);
  241. }
  242. set
  243. {
  244. ImageResources.Set(value);
  245. }
  246. }
  247. ///////////////////////////////////////////////////////////////////////////
  248. private void LoadImageResources(PsdBinaryReader reader)
  249. {
  250. Util.DebugMessage(reader.BaseStream, "Load, Begin, ImageResources");
  251. var imageResourcesLength = reader.ReadUInt32();
  252. if (imageResourcesLength <= 0)
  253. return;
  254. var startPosition = reader.BaseStream.Position;
  255. var endPosition = startPosition + imageResourcesLength;
  256. while (reader.BaseStream.Position < endPosition)
  257. {
  258. var imageResource = ImageResourceFactory.CreateImageResource(reader);
  259. ImageResources.Add(imageResource);
  260. }
  261. Util.DebugMessage(reader.BaseStream, "Load, End, ImageResources");
  262. //-----------------------------------------------------------------------
  263. // make sure we are not on a wrong offset, so set the stream position
  264. // manually
  265. reader.BaseStream.Position = startPosition + imageResourcesLength;
  266. }
  267. #endregion
  268. ///////////////////////////////////////////////////////////////////////////
  269. #region LayerAndMaskInfo
  270. public List<Layer> Layers { get; private set; }
  271. public List<LayerInfo> AdditionalInfo { get; private set; }
  272. public bool AbsoluteAlpha { get; set; }
  273. ///////////////////////////////////////////////////////////////////////////
  274. private void LoadLayerAndMaskInfo(PsdBinaryReader reader)
  275. {
  276. Util.DebugMessage(reader.BaseStream, "Load, Begin, Layer and mask info");
  277. var layersAndMaskLength = IsLargeDocument
  278. ? reader.ReadInt64()
  279. : reader.ReadUInt32();
  280. if (layersAndMaskLength <= 0)
  281. return;
  282. var startPosition = reader.BaseStream.Position;
  283. var endPosition = startPosition + layersAndMaskLength;
  284. LoadLayers(reader, true);
  285. LoadGlobalLayerMask(reader);
  286. //-----------------------------------------------------------------------
  287. // Load Additional Layer Information
  288. while (reader.BaseStream.Position < endPosition)
  289. {
  290. var info = LayerInfoFactory.Load(reader, this, true, endPosition);
  291. AdditionalInfo.Add(info);
  292. if (info is RawLayerInfo)
  293. {
  294. var layerInfo = (RawLayerInfo)info;
  295. switch (info.Key)
  296. {
  297. case "LMsk":
  298. m_GlobalLayerMaskData = layerInfo.Data;
  299. break;
  300. }
  301. }
  302. }
  303. Util.DebugMessage(reader.BaseStream, "Load, End, Layer and mask info");
  304. //-----------------------------------------------------------------------
  305. // make sure we are not on a wrong offset, so set the stream position
  306. // manually
  307. reader.BaseStream.Position = startPosition + layersAndMaskLength;
  308. }
  309. ///////////////////////////////////////////////////////////////////////////
  310. /// <summary>
  311. /// Load Layers Info section, including image data.
  312. /// </summary>
  313. /// <param name="reader">PSD reader.</param>
  314. /// <param name="hasHeader">Whether the Layers Info section has a length header.</param>
  315. internal void LoadLayers(PsdBinaryReader reader, bool hasHeader)
  316. {
  317. Util.DebugMessage(reader.BaseStream, "Load, Begin, Layers Info section");
  318. long sectionLength = 0;
  319. if (hasHeader)
  320. {
  321. sectionLength = IsLargeDocument
  322. ? reader.ReadInt64()
  323. : reader.ReadUInt32();
  324. if (sectionLength <= 0)
  325. {
  326. // The callback may take action when there are 0 layers, so it must
  327. // be called even though the Layers Info section is empty.
  328. LoadContext.OnLoadLayersHeader(this);
  329. Util.DebugMessage(reader.BaseStream, "Load, End, Layers Info section");
  330. return;
  331. }
  332. }
  333. var startPosition = reader.BaseStream.Position;
  334. var numLayers = reader.ReadInt16();
  335. // If numLayers < 0, then number of layers is absolute value,
  336. // and the first alpha channel contains the transparency data for
  337. // the merged result.
  338. if (numLayers < 0)
  339. {
  340. AbsoluteAlpha = true;
  341. numLayers = Math.Abs(numLayers);
  342. }
  343. for (int i = 0; i < numLayers; i++)
  344. {
  345. var layer = new Layer(reader, this);
  346. Layers.Add(layer);
  347. }
  348. // Header is complete just before loading pixel data
  349. LoadContext.OnLoadLayersHeader(this);
  350. //-----------------------------------------------------------------------
  351. // Load image data for all channels.
  352. foreach (var layer in Layers)
  353. {
  354. Util.DebugMessage(reader.BaseStream,
  355. "Load, Begin, Layer image, layer.Name");
  356. foreach (var channel in layer.Channels)
  357. {
  358. channel.LoadPixelData(reader);
  359. }
  360. Util.DebugMessage(reader.BaseStream,
  361. "Load, End, Layer image, layer.Name");
  362. }
  363. // Length is set to 0 when called on higher bitdepth layers.
  364. if (sectionLength > 0)
  365. {
  366. // Layers Info section is documented to be even-padded, but Photoshop
  367. // actually pads to 4 bytes.
  368. var endPosition = startPosition + sectionLength;
  369. var positionOffset = reader.BaseStream.Position - endPosition;
  370. Debug.Assert(positionOffset > -4,
  371. "LoadLayers did not read the full length of the Layers Info section.");
  372. Debug.Assert(positionOffset <= 0,
  373. "LoadLayers read past the end of the Layers Info section.");
  374. if (reader.BaseStream.Position < endPosition)
  375. reader.BaseStream.Position = endPosition;
  376. }
  377. Util.DebugMessage(reader.BaseStream, "Load, End, Layers");
  378. }
  379. ///////////////////////////////////////////////////////////////////////////
  380. /// <summary>
  381. /// Cleanup
  382. /// </summary>
  383. public void Cleanup()
  384. {
  385. var layersAndComposite = Layers.Concat(new[] { BaseLayer });
  386. foreach (var lac in layersAndComposite)
  387. {
  388. foreach (var c in lac.Channels)
  389. {
  390. c.Cleanup();
  391. }
  392. }
  393. }
  394. ///////////////////////////////////////////////////////////////////////////
  395. /// <summary>
  396. /// Decompress the document image data and all the layers' image data, in parallel.
  397. /// </summary>
  398. private void DecompressImages()
  399. {
  400. var layersAndComposite = Layers.Concat(new[] { BaseLayer });
  401. //var channels = layersAndComposite.SelectMany(x => x.Channels);
  402. //Parallel.ForEach(channels, channel =>
  403. //{
  404. // channel.DecodeImageData();
  405. //});
  406. foreach (var lac in layersAndComposite)
  407. {
  408. foreach (var c in lac.Channels)
  409. {
  410. c.DecodeImageData();
  411. }
  412. }
  413. foreach (var layer in Layers)
  414. {
  415. foreach (var channel in layer.Channels)
  416. {
  417. if (channel.ID == -2)
  418. layer.Masks.LayerMask.ImageData = channel.ImageData;
  419. else if (channel.ID == -3)
  420. layer.Masks.UserMask.ImageData = channel.ImageData;
  421. }
  422. }
  423. }
  424. /// <summary>
  425. /// Verifies that any Additional Info layers are consistent.
  426. /// </summary>
  427. private void VerifyInfoLayers()
  428. {
  429. var infoLayersCount = AdditionalInfo.Count(x => x is InfoLayers);
  430. if (infoLayersCount > 1)
  431. {
  432. throw new PsdInvalidException(
  433. "Cannot have more than one InfoLayers in a PSD file.");
  434. }
  435. if ((infoLayersCount > 0) && (Layers.Count == 0))
  436. {
  437. throw new PsdInvalidException(
  438. "InfoLayers cannot exist when there are 0 layers.");
  439. }
  440. }
  441. /// <summary>
  442. /// Verify validity of layer sections. Each start marker should have a
  443. /// matching end marker.
  444. /// </summary>
  445. internal void VerifyLayerSections()
  446. {
  447. int depth = 0;
  448. foreach (var layer in Enumerable.Reverse(Layers))
  449. {
  450. var layerSectionInfo = layer.AdditionalInfo.SingleOrDefault(
  451. x => x is LayerSectionInfo);
  452. if (layerSectionInfo == null)
  453. continue;
  454. var sectionInfo = (LayerSectionInfo)layerSectionInfo;
  455. switch (sectionInfo.SectionType)
  456. {
  457. case LayerSectionType.OpenFolder:
  458. case LayerSectionType.ClosedFolder:
  459. depth++;
  460. break;
  461. case LayerSectionType.SectionDivider:
  462. depth--;
  463. if (depth < 0)
  464. throw new PsdInvalidException("Layer section ended without matching start marker.");
  465. break;
  466. case LayerSectionType.Layer: // Nothing to do here yet.
  467. break;
  468. default:
  469. throw new PsdInvalidException("Unrecognized layer section type.");
  470. }
  471. }
  472. if (depth != 0)
  473. throw new PsdInvalidException("Layer section not closed by end marker.");
  474. }
  475. /// <summary>
  476. /// Set the VersionInfo resource on the file.
  477. /// </summary>
  478. public void SetVersionInfo()
  479. {
  480. var versionInfo = (VersionInfo)ImageResources.Get(ResourceID.VersionInfo);
  481. if (versionInfo == null)
  482. {
  483. versionInfo = new VersionInfo();
  484. ImageResources.Set(versionInfo);
  485. // Get the version string. We don't use the fourth part (revision).
  486. var assembly = System.Reflection.Assembly.GetExecutingAssembly();
  487. var version = assembly.GetName().Version;
  488. var versionString = version.Major + "." + version.Minor + "." + version.Build;
  489. // Strings are not localized since they are not shown to the user.
  490. versionInfo.Version = 1;
  491. versionInfo.HasRealMergedData = true;
  492. versionInfo.ReaderName = "Paint.NET PSD Plugin";
  493. versionInfo.WriterName = "Paint.NET PSD Plugin " + versionString;
  494. versionInfo.FileVersion = 1;
  495. }
  496. }
  497. ///////////////////////////////////////////////////////////////////////////
  498. byte[] m_GlobalLayerMaskData;
  499. private void LoadGlobalLayerMask(PsdBinaryReader reader)
  500. {
  501. Util.DebugMessage(reader.BaseStream, "Load, Begin, GlobalLayerMask");
  502. var maskLength = reader.ReadUInt32();
  503. if (maskLength <= 0)
  504. {
  505. Util.DebugMessage(reader.BaseStream, "Load, End, GlobalLayerMask");
  506. return;
  507. }
  508. m_GlobalLayerMaskData = reader.ReadBytes((int)maskLength);
  509. Util.DebugMessage(reader.BaseStream, "Load, End, GlobalLayerMask");
  510. }
  511. public byte[] globalLayerMaskData
  512. {
  513. get { return m_GlobalLayerMaskData; }
  514. }
  515. #endregion
  516. ///////////////////////////////////////////////////////////////////////////
  517. #region Composite image
  518. /// <summary>
  519. /// Represents the composite image.
  520. /// </summary>
  521. public Layer BaseLayer { get; set; }
  522. public ImageCompression ImageCompression { get; set; }
  523. private void LoadImage(PsdBinaryReader reader)
  524. {
  525. Util.DebugMessage(reader.BaseStream, "Load, Begin, Composite image");
  526. ImageCompression = (ImageCompression)reader.ReadInt16();
  527. // Create channels
  528. for (Int16 i = 0; i < ChannelCount; i++)
  529. {
  530. Util.DebugMessage(reader.BaseStream, "Load, Begin, Channel image data");
  531. var channel = new Channel(i, this.BaseLayer);
  532. channel.ImageCompression = ImageCompression;
  533. channel.Length = this.RowCount
  534. * Util.BytesPerRow(BaseLayer.Rect.Size, BitDepth);
  535. // The composite image stores all RLE headers up-front, rather than
  536. // with each channel.
  537. if (ImageCompression == ImageCompression.Rle)
  538. {
  539. channel.RleRowLengths = new RleRowLengths(reader, RowCount, IsLargeDocument);
  540. channel.Length = channel.RleRowLengths.Total;
  541. }
  542. BaseLayer.Channels.Add(channel);
  543. Util.DebugMessage(reader.BaseStream, "Load, End, Channel image data");
  544. }
  545. foreach (var channel in this.BaseLayer.Channels)
  546. {
  547. Util.DebugMessage(reader.BaseStream, "Load, Begin, Channel image data");
  548. Util.CheckByteArrayLength(channel.Length);
  549. channel.ImageDataRaw = reader.ReadBytes((int)channel.Length);
  550. Util.DebugMessage(reader.BaseStream, "Load, End, Channel image data");
  551. }
  552. // If there is exactly one more channel than we need, then it is the
  553. // alpha channel.
  554. if ((ColorMode != PsdColorMode.Multichannel)
  555. && (ChannelCount == ColorMode.MinChannelCount() + 1))
  556. {
  557. var alphaChannel = BaseLayer.Channels.Last();
  558. alphaChannel.ID = -1;
  559. }
  560. Util.DebugMessage(reader.BaseStream, "Load, End, Composite image");
  561. }
  562. #endregion
  563. }
  564. /// <summary>
  565. /// The possible Compression methods.
  566. /// </summary>
  567. internal enum ImageCompression
  568. {
  569. /// <summary>
  570. /// Raw data
  571. /// </summary>
  572. Raw = 0,
  573. /// <summary>
  574. /// RLE compressed
  575. /// </summary>
  576. Rle = 1,
  577. /// <summary>
  578. /// ZIP without prediction.
  579. /// </summary>
  580. Zip = 2,
  581. /// <summary>
  582. /// ZIP with prediction.
  583. /// </summary>
  584. ZipPrediction = 3
  585. }
  586. }