ZipPredict32Image.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. // See LICENSE.txt for complete licensing and attribution information.
  11. //
  12. /////////////////////////////////////////////////////////////////////////////////
  13. using System;
  14. using System.Diagnostics;
  15. using System.Drawing;
  16. namespace PhotoshopFile.Compression
  17. {
  18. public class ZipPredict32Image : ImageData
  19. {
  20. private ZipImage zipImage;
  21. // Prediction will pack the data into a temporary buffer, so the
  22. // original data will remain unchanged.
  23. protected override bool AltersWrittenData => false;
  24. public ZipPredict32Image(byte[] zipData, Size size)
  25. : base(size, 32)
  26. {
  27. zipImage = new ZipImage(zipData, size, 32);
  28. }
  29. internal override void Read(byte[] buffer)
  30. {
  31. if (buffer.Length == 0)
  32. {
  33. return;
  34. }
  35. var predictedData = new byte[buffer.Length];
  36. zipImage.Read(predictedData);
  37. unsafe
  38. {
  39. fixed (byte* ptrData = &predictedData[0])
  40. fixed (byte* ptrOutput = &buffer[0])
  41. {
  42. Unpredict(ptrData, (Int32*)ptrOutput);
  43. }
  44. }
  45. }
  46. public override byte[] ReadCompressed()
  47. {
  48. return zipImage.ReadCompressed();
  49. }
  50. internal override void WriteInternal(byte[] array)
  51. {
  52. if (array.Length == 0)
  53. {
  54. return;
  55. }
  56. var predictedData = new byte[array.Length];
  57. unsafe
  58. {
  59. fixed (byte* ptrData = &array[0])
  60. fixed (byte* ptrOutput = &predictedData[0])
  61. {
  62. Predict((Int32*)ptrData, ptrOutput);
  63. }
  64. }
  65. zipImage.WriteInternal(predictedData);
  66. }
  67. unsafe private void Predict(Int32* ptrData, byte* ptrOutput)
  68. {
  69. for (int i = 0; i < Size.Height; i++)
  70. {
  71. // Pack together the individual bytes of the 32-bit words, high-order
  72. // bytes before low-order bytes.
  73. int offset1 = Size.Width;
  74. int offset2 = 2 * offset1;
  75. int offset3 = 3 * offset1;
  76. Int32* ptrDataRow = ptrData;
  77. Int32* ptrDataRowEnd = ptrDataRow + Size.Width;
  78. byte* ptrOutputRow = ptrOutput;
  79. byte* ptrOutputRowEnd = ptrOutputRow + BytesPerRow;
  80. while (ptrData < ptrDataRowEnd)
  81. {
  82. *(ptrOutput) = (byte)(*ptrData >> 24);
  83. *(ptrOutput + offset1) = (byte)(*ptrData >> 16);
  84. *(ptrOutput + offset2) = (byte)(*ptrData >> 8);
  85. *(ptrOutput + offset3) = (byte)(*ptrData);
  86. ptrData++;
  87. ptrOutput++;
  88. }
  89. // Delta-encode the row
  90. ptrOutput = ptrOutputRowEnd - 1;
  91. while (ptrOutput > ptrOutputRow)
  92. {
  93. *ptrOutput -= *(ptrOutput - 1);
  94. ptrOutput--;
  95. }
  96. // Advance pointer to next row
  97. ptrOutput = ptrOutputRowEnd;
  98. Debug.Assert(ptrData == ptrDataRowEnd);
  99. }
  100. }
  101. /// <summary>
  102. /// Unpredicts the raw decompressed image data into a 32-bpp bitmap with
  103. /// native endianness.
  104. /// </summary>
  105. unsafe private void Unpredict(byte* ptrData, Int32* ptrOutput)
  106. {
  107. for (int i = 0; i < Size.Height; i++)
  108. {
  109. byte* ptrDataRow = ptrData;
  110. byte* ptrDataRowEnd = ptrDataRow + BytesPerRow;
  111. // Delta-decode each row
  112. ptrData++;
  113. while (ptrData < ptrDataRowEnd)
  114. {
  115. *ptrData += *(ptrData - 1);
  116. ptrData++;
  117. }
  118. // Within each row, the individual bytes of the 32-bit words are
  119. // packed together, high-order bytes before low-order bytes.
  120. // We now unpack them into words.
  121. int offset1 = Size.Width;
  122. int offset2 = 2 * offset1;
  123. int offset3 = 3 * offset1;
  124. ptrData = ptrDataRow;
  125. Int32* ptrOutputRowEnd = ptrOutput + Size.Width;
  126. while (ptrOutput < ptrOutputRowEnd)
  127. {
  128. *ptrOutput = *(ptrData) << 24
  129. | *(ptrData + offset1) << 16
  130. | *(ptrData + offset2) << 8
  131. | *(ptrData + offset3);
  132. ptrData++;
  133. ptrOutput++;
  134. }
  135. // Advance pointer to next row
  136. ptrData = ptrDataRowEnd;
  137. Debug.Assert(ptrOutput == ptrOutputRowEnd);
  138. }
  139. }
  140. }
  141. }