/PDFsharp/code/PdfSharp/PdfSharp.Pdf.Filters/Filtering.cs

# · C# · 271 lines · 150 code · 20 blank · 101 comment · 36 complexity · 036bb73baa9cd5a27c9167df7718ee58 MD5 · raw file

  1. #region PDFsharp - A .NET library for processing PDF
  2. //
  3. // Authors:
  4. // Stefan Lange (mailto:Stefan.Lange@pdfsharp.com)
  5. //
  6. // Copyright (c) 2005-2009 empira Software GmbH, Cologne (Germany)
  7. //
  8. // http://www.pdfsharp.com
  9. // http://sourceforge.net/projects/pdfsharp
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining a
  12. // copy of this software and associated documentation files (the "Software"),
  13. // to deal in the Software without restriction, including without limitation
  14. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15. // and/or sell copies of the Software, and to permit persons to whom the
  16. // Software is furnished to do so, subject to the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be included
  19. // in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  24. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  26. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  27. // DEALINGS IN THE SOFTWARE.
  28. #endregion
  29. using System;
  30. using System.Diagnostics;
  31. using PdfSharp.Internal;
  32. using PdfSharp.Pdf.IO;
  33. using PdfSharp.Pdf.Internal;
  34. namespace PdfSharp.Pdf.Filters
  35. {
  36. /// <summary>
  37. /// Applies standard filters to streams.
  38. /// </summary>
  39. public static class Filtering
  40. {
  41. /// <summary>
  42. /// Gets the filter specified by the case sensitive name.
  43. /// </summary>
  44. public static Filter GetFilter(string filterName)
  45. {
  46. if (filterName.StartsWith("/"))
  47. filterName = filterName.Substring(1);
  48. // Some tools use abbreviations
  49. switch (filterName)
  50. {
  51. case "ASCIIHexDecode":
  52. case "AHx":
  53. if (Filtering.asciiHexDecode == null)
  54. Filtering.asciiHexDecode = new ASCIIHexDecode();
  55. return Filtering.asciiHexDecode;
  56. case "ASCII85Decode":
  57. case "A85":
  58. if (Filtering.ascii85Decode == null)
  59. Filtering.ascii85Decode = new ASCII85Decode();
  60. return Filtering.ascii85Decode;
  61. case "LZWDecode":
  62. case "LZW":
  63. if (Filtering.lzwDecode == null)
  64. Filtering.lzwDecode = new LzwDecode();
  65. return Filtering.lzwDecode;
  66. case "FlateDecode":
  67. case "Fl":
  68. if (Filtering.flateDecode == null)
  69. Filtering.flateDecode = new FlateDecode();
  70. return Filtering.flateDecode;
  71. //case "RunLengthDecode":
  72. // if (Filtering.RunLengthDecode == null)
  73. // Filtering.RunLengthDecode = new RunLengthDecode();
  74. // return Filtering.RunLengthDecode;
  75. //
  76. //case "CCITTFaxDecode":
  77. // if (Filtering.CCITTFaxDecode == null)
  78. // Filtering.CCITTFaxDecode = new CCITTFaxDecode();
  79. // return Filtering.CCITTFaxDecode;
  80. //
  81. //case "JBIG2Decode":
  82. // if (Filtering.JBIG2Decode == null)
  83. // Filtering.JBIG2Decode = new JBIG2Decode();
  84. // return Filtering.JBIG2Decode;
  85. //
  86. //case "DCTDecode":
  87. // if (Filtering.DCTDecode == null)
  88. // Filtering.DCTDecode = new DCTDecode();
  89. // return Filtering.DCTDecode;
  90. //
  91. //case "JPXDecode":
  92. // if (Filtering.JPXDecode == null)
  93. // Filtering.JPXDecode = new JPXDecode();
  94. // return Filtering.JPXDecode;
  95. //
  96. //case "Crypt":
  97. // if (Filtering.Crypt == null)
  98. // Filtering.Crypt = new Crypt();
  99. // return Filtering.Crypt;
  100. case "RunLengthDecode":
  101. case "CCITTFaxDecode":
  102. case "JBIG2Decode":
  103. case "DCTDecode":
  104. case "JPXDecode":
  105. case "Crypt":
  106. Debug.WriteLine("Filter not implemented: " + filterName);
  107. return null;
  108. }
  109. throw new NotImplementedException("Unknown filter: " + filterName);
  110. }
  111. /// <summary>
  112. /// Gets the filter singleton.
  113. /// </summary>
  114. public static ASCIIHexDecode ASCIIHexDecode
  115. {
  116. get
  117. {
  118. if (Filtering.asciiHexDecode == null)
  119. Filtering.asciiHexDecode = new ASCIIHexDecode();
  120. return Filtering.asciiHexDecode;
  121. }
  122. }
  123. static ASCIIHexDecode asciiHexDecode;
  124. /// <summary>
  125. /// Gets the filter singleton.
  126. /// </summary>
  127. public static ASCII85Decode ASCII85Decode
  128. {
  129. get
  130. {
  131. if (Filtering.ascii85Decode == null)
  132. Filtering.ascii85Decode = new ASCII85Decode();
  133. return Filtering.ascii85Decode;
  134. }
  135. }
  136. static ASCII85Decode ascii85Decode;
  137. /// <summary>
  138. /// Gets the filter singleton.
  139. /// </summary>
  140. public static LzwDecode LzwDecode
  141. {
  142. get
  143. {
  144. if (Filtering.lzwDecode == null)
  145. Filtering.lzwDecode = new LzwDecode();
  146. return Filtering.lzwDecode;
  147. }
  148. }
  149. static LzwDecode lzwDecode;
  150. /// <summary>
  151. /// Gets the filter singleton.
  152. /// </summary>
  153. public static FlateDecode FlateDecode
  154. {
  155. get
  156. {
  157. if (Filtering.flateDecode == null)
  158. Filtering.flateDecode = new FlateDecode();
  159. return Filtering.flateDecode;
  160. }
  161. }
  162. static FlateDecode flateDecode;
  163. //runLengthDecode
  164. //ccittFaxDecode
  165. //jbig2Decode
  166. //dctDecode
  167. //jpxDecode
  168. //crypt
  169. /// <summary>
  170. /// Encodes the data with the specified filter.
  171. /// </summary>
  172. public static byte[] Encode(byte[] data, string filterName)
  173. {
  174. Filter filter = GetFilter(filterName);
  175. if (filter != null)
  176. return filter.Encode(data);
  177. return null;
  178. }
  179. /// <summary>
  180. /// Encodes a raw string with the specified filter.
  181. /// </summary>
  182. public static byte[] Encode(string rawString, string filterName)
  183. {
  184. Filter filter = GetFilter(filterName);
  185. if (filter != null)
  186. return filter.Encode(rawString);
  187. return null;
  188. }
  189. /// <summary>
  190. /// Decodes the data with the specified filter.
  191. /// </summary>
  192. public static byte[] Decode(byte[] data, string filterName, FilterParms parms)
  193. {
  194. Filter filter = GetFilter(filterName);
  195. if (filter != null)
  196. return filter.Decode(data, parms);
  197. return null;
  198. }
  199. /// <summary>
  200. /// Decodes the data with the specified filter.
  201. /// </summary>
  202. public static byte[] Decode(byte[] data, string filterName)
  203. {
  204. Filter filter = GetFilter(filterName);
  205. if (filter != null)
  206. return filter.Decode(data, null);
  207. return null;
  208. }
  209. /// <summary>
  210. /// Decodes the data with the specified filter.
  211. /// </summary>
  212. public static byte[] Decode(byte[] data, PdfItem filterItem)
  213. {
  214. byte[] result = null;
  215. if (filterItem is PdfName)
  216. {
  217. Filter filter = GetFilter(filterItem.ToString());
  218. if (filter != null)
  219. result = filter.Decode(data);
  220. }
  221. else if (filterItem is PdfArray)
  222. {
  223. PdfArray array = (PdfArray)filterItem;
  224. foreach (PdfItem item in array)
  225. data = Filtering.Decode(data, item);
  226. result = data;
  227. }
  228. return result;
  229. }
  230. /// <summary>
  231. /// Decodes to a raw string with the specified filter.
  232. /// </summary>
  233. public static string DecodeToString(byte[] data, string filterName, FilterParms parms)
  234. {
  235. Filter filter = GetFilter(filterName);
  236. if (filter != null)
  237. return filter.DecodeToString(data, parms);
  238. return null;
  239. }
  240. /// <summary>
  241. /// Decodes to a raw string with the specified filter.
  242. /// </summary>
  243. public static string DecodeToString(byte[] data, string filterName)
  244. {
  245. Filter filter = GetFilter(filterName);
  246. if (filter != null)
  247. return filter.DecodeToString(data, null);
  248. return null;
  249. }
  250. }
  251. }