Channel.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.Linq;
  21. using Unity.Collections;
  22. using PhotoshopFile.Compression;
  23. namespace PhotoshopFile
  24. {
  25. internal class ChannelList : List<Channel>
  26. {
  27. /// <summary>
  28. /// Returns channels with nonnegative IDs as an array, so that accessing
  29. /// a channel by Id can be optimized into pointer arithmetic rather than
  30. /// being implemented as a List scan.
  31. /// </summary>
  32. /// <remarks>
  33. /// This optimization is crucial for blitting lots of pixels back and
  34. /// forth between Photoshop's per-channel representation, and Paint.NET's
  35. /// per-pixel BGRA representation.
  36. /// </remarks>
  37. public Channel[] ToIdArray()
  38. {
  39. var maxId = this.Max(x => x.ID);
  40. var idArray = new Channel[maxId + 1];
  41. foreach (var channel in this)
  42. {
  43. if (channel.ID >= 0)
  44. idArray[channel.ID] = channel;
  45. }
  46. return idArray;
  47. }
  48. public ChannelList()
  49. : base()
  50. {
  51. }
  52. public Channel GetId(int id)
  53. {
  54. return this.Single(x => x.ID == id);
  55. }
  56. public bool ContainsId(int id)
  57. {
  58. return this.Exists(x => x.ID == id);
  59. }
  60. }
  61. ///////////////////////////////////////////////////////////////////////////
  62. [DebuggerDisplay("ID = {ID}")]
  63. internal class Channel
  64. {
  65. /// <summary>
  66. /// The layer to which this channel belongs
  67. /// </summary>
  68. public Layer Layer { get; private set; }
  69. /// <summary>
  70. /// Channel ID.
  71. /// <list type="bullet">
  72. /// <item>-1 = transparency mask</item>
  73. /// <item>-2 = user-supplied layer mask, or vector mask</item>
  74. /// <item>-3 = user-supplied layer mask, if channel -2 contains a vector mask</item>
  75. /// <item>
  76. /// Nonnegative channel IDs give the actual image channels, in the
  77. /// order defined by the colormode. For example, 0, 1, 2 = R, G, B.
  78. /// </item>
  79. /// </list>
  80. /// </summary>
  81. public short ID { get; set; }
  82. public Rectangle Rect
  83. {
  84. get
  85. {
  86. switch (ID)
  87. {
  88. case -2:
  89. return Layer.Masks.LayerMask.Rect;
  90. case -3:
  91. return Layer.Masks.UserMask.Rect;
  92. default:
  93. return Layer.Rect;
  94. }
  95. }
  96. }
  97. /// <summary>
  98. /// Total length of the channel data, including compression headers.
  99. /// </summary>
  100. public long Length { get; set; }
  101. /// <summary>
  102. /// Raw image data for this color channel, in compressed on-disk format.
  103. /// </summary>
  104. /// <remarks>
  105. /// If null, the ImageData will be automatically compressed during save.
  106. /// </remarks>
  107. public byte[] ImageDataRaw { get; set; }
  108. /// <summary>
  109. /// Decompressed image data for this color channel.
  110. /// </summary>
  111. /// <remarks>
  112. /// When making changes to the ImageData, set ImageDataRaw to null so that
  113. /// the correct data will be compressed during save.
  114. /// </remarks>
  115. public NativeArray<byte> ImageData { get; set; }
  116. /// <summary>
  117. /// Image compression method used.
  118. /// </summary>
  119. public ImageCompression ImageCompression { get; set; }
  120. /// <summary>
  121. /// RLE-compressed length of each row.
  122. /// </summary>
  123. public RleRowLengths RleRowLengths { get; set; }
  124. //////////////////////////////////////////////////////////////////
  125. internal Channel(short id, Layer layer)
  126. {
  127. ID = id;
  128. Layer = layer;
  129. }
  130. internal Channel(PsdBinaryReader reader, Layer layer)
  131. {
  132. Util.DebugMessage(reader.BaseStream, "Load, Begin, Channel");
  133. ID = reader.ReadInt16();
  134. Length = (layer.PsdFile.IsLargeDocument)
  135. ? reader.ReadInt64()
  136. : reader.ReadInt32();
  137. Layer = layer;
  138. Util.DebugMessage(reader.BaseStream, "Load, End, Channel, {0}", ID);
  139. }
  140. internal void Cleanup()
  141. {
  142. if (ImageData.IsCreated)
  143. ImageData.Dispose();
  144. }
  145. //////////////////////////////////////////////////////////////////
  146. internal void LoadPixelData(PsdBinaryReader reader)
  147. {
  148. Util.DebugMessage(reader.BaseStream, "Load, Begin, Channel image");
  149. if (Length == 0)
  150. {
  151. ImageCompression = ImageCompression.Raw;
  152. ImageDataRaw = new byte[0];
  153. return;
  154. }
  155. var endPosition = reader.BaseStream.Position + this.Length;
  156. ImageCompression = (ImageCompression)reader.ReadInt16();
  157. var longDataLength = this.Length - 2;
  158. Util.CheckByteArrayLength(longDataLength);
  159. var dataLength = (int)longDataLength;
  160. switch (ImageCompression)
  161. {
  162. case ImageCompression.Raw:
  163. ImageDataRaw = reader.ReadBytes(dataLength);
  164. break;
  165. case ImageCompression.Rle:
  166. // RLE row lengths
  167. RleRowLengths = new RleRowLengths(reader, Rect.Height, Layer.PsdFile.IsLargeDocument);
  168. var rleDataLength = (int)(endPosition - reader.BaseStream.Position);
  169. Debug.Assert(rleDataLength == RleRowLengths.Total,
  170. "RLE row lengths do not sum to length of channel image data.");
  171. // The PSD specification states that rows are padded to even sizes.
  172. // However, Photoshop doesn't actually do this. RLE rows can have
  173. // odd lengths in the header, and there is no padding between rows.
  174. ImageDataRaw = reader.ReadBytes(rleDataLength);
  175. break;
  176. case ImageCompression.Zip:
  177. case ImageCompression.ZipPrediction:
  178. ImageDataRaw = reader.ReadBytes(dataLength);
  179. break;
  180. }
  181. Util.DebugMessage(reader.BaseStream, "Load, End, Channel image, {0}", ID, Layer.Name);
  182. Debug.Assert(reader.BaseStream.Position == endPosition, "Pixel data was not fully read in.");
  183. }
  184. /// <summary>
  185. /// Decodes the raw image data from the compressed on-disk format into
  186. /// an uncompressed bitmap, in native byte order.
  187. /// </summary>
  188. public void DecodeImageData()
  189. {
  190. if ((ImageCompression == ImageCompression.Raw) && (Layer.PsdFile.BitDepth <= 8))
  191. {
  192. ImageData = new NativeArray<byte>(ImageDataRaw, Allocator.TempJob);
  193. return;
  194. }
  195. var image = ImageDataFactory.Create(this, ImageDataRaw);
  196. var longLength = (long)image.BytesPerRow * Rect.Height;
  197. Util.CheckByteArrayLength(longLength);
  198. var LocalImageData = new byte[longLength];
  199. image.Read(LocalImageData);
  200. ImageData = new NativeArray<byte>(LocalImageData, Allocator.TempJob);
  201. ImageDataRaw = null; // no longer needed.
  202. }
  203. }
  204. }