ImageData.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // See LICENSE.txt for complete licensing and attribution information.
  11. //
  12. /////////////////////////////////////////////////////////////////////////////////
  13. using System;
  14. using System.Drawing;
  15. using System.IO.Compression;
  16. namespace PhotoshopFile.Compression
  17. {
  18. public abstract class ImageData
  19. {
  20. public int BitDepth { get; private set; }
  21. public int BytesPerRow { get; private set; }
  22. public Size Size { get; private set; }
  23. protected abstract bool AltersWrittenData { get; }
  24. protected ImageData(Size size, int bitDepth)
  25. {
  26. Size = size;
  27. BitDepth = bitDepth;
  28. BytesPerRow = Util.BytesPerRow(size, bitDepth);
  29. }
  30. /// <summary>
  31. /// Reads decompressed image data.
  32. /// </summary>
  33. public virtual byte[] Read()
  34. {
  35. var imageLongLength = (long)BytesPerRow * Size.Height;
  36. Util.CheckByteArrayLength(imageLongLength);
  37. var buffer = new byte[imageLongLength];
  38. Read(buffer);
  39. return buffer;
  40. }
  41. internal abstract void Read(byte[] buffer);
  42. /// <summary>
  43. /// Reads compressed image data.
  44. /// </summary>
  45. public abstract byte[] ReadCompressed();
  46. /// <summary>
  47. /// Writes rows of image data into compressed format.
  48. /// </summary>
  49. /// <param name="array">An array containing the data to be compressed.</param>
  50. public void Write(byte[] array)
  51. {
  52. var imageLength = (long)BytesPerRow * Size.Height;
  53. if (array.Length != imageLength)
  54. {
  55. throw new ArgumentException(
  56. "Array length is not equal to image length.",
  57. nameof(array));
  58. }
  59. if (AltersWrittenData)
  60. {
  61. array = (byte[])array.Clone();
  62. }
  63. WriteInternal(array);
  64. }
  65. internal abstract void WriteInternal(byte[] array);
  66. }
  67. }