Channel.cs 8.3 KB

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