You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FlateFilter.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.pdf;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import org.apache.xmlgraphics.util.io.FlateEncodeOutputStream;
  22. /**
  23. * A filter to deflate a stream.
  24. * <p>
  25. * <b>Note</b> that the attributes for
  26. * prediction, colors, bitsPerComponent, and columns are not supported
  27. * when this filter is used to handle the data compression. They are
  28. * only valid for externally encoded data such as that from a graphics
  29. * file.
  30. */
  31. public class FlateFilter extends PDFFilter {
  32. /**
  33. * The supported mode when this filter is used for data compression
  34. */
  35. public static final int PREDICTION_NONE = 1;
  36. /**
  37. * Mode for externally encoded data.
  38. */
  39. public static final int PREDICTION_TIFF2 = 2;
  40. /**
  41. * Mode for externally encoded data.
  42. */
  43. public static final int PREDICTION_PNG_NONE = 10;
  44. /**
  45. * Mode for externally encoded data.
  46. */
  47. public static final int PREDICTION_PNG_SUB = 11;
  48. /**
  49. * Mode for externally encoded data.
  50. */
  51. public static final int PREDICTION_PNG_UP = 12;
  52. /**
  53. * Mode for externally encoded data.
  54. */
  55. public static final int PREDICTION_PNG_AVG = 13;
  56. /**
  57. * Mode for externally encoded data.
  58. */
  59. public static final int PREDICTION_PNG_PAETH = 14;
  60. /**
  61. * Mode for externally encoded data.
  62. */
  63. public static final int PREDICTION_PNG_OPT = 15;
  64. private int predictor = PREDICTION_NONE;
  65. private int colors;
  66. private int bitsPerComponent;
  67. private int columns;
  68. /**
  69. * Get the name of this filter.
  70. *
  71. * @return the pdf name of the flate decode filter
  72. */
  73. public String getName() {
  74. return "/FlateDecode";
  75. }
  76. /**
  77. * Get the decode params for this filter.
  78. *
  79. * @return a string containing the decode params for this filter
  80. */
  81. public PDFObject getDecodeParms() {
  82. if (predictor > PREDICTION_NONE) {
  83. PDFDictionary dict = new PDFDictionary();
  84. dict.put("Predictor", predictor);
  85. if (colors > 1) {
  86. dict.put("Colors", colors);
  87. }
  88. if (bitsPerComponent > 0 && bitsPerComponent != 8) {
  89. dict.put("BitsPerComponent", bitsPerComponent);
  90. }
  91. if (columns > 1) {
  92. dict.put("Columns", columns);
  93. }
  94. return dict;
  95. }
  96. return null;
  97. }
  98. /**
  99. * Set the predictor for this filter.
  100. *
  101. * @param predictor the predictor to use
  102. * @throws PDFFilterException if there is an error with the predictor
  103. */
  104. public void setPredictor(int predictor) throws PDFFilterException {
  105. this.predictor = predictor;
  106. }
  107. /**
  108. * Get the predictor for this filter.
  109. *
  110. * @return the predictor used for this filter
  111. */
  112. public int getPredictor() {
  113. return predictor;
  114. }
  115. /**
  116. * Set the colors for this filter.
  117. *
  118. * @param colors the colors to use
  119. * @throws PDFFilterException if predictor is not PREDICTION_NONE
  120. */
  121. public void setColors(int colors) throws PDFFilterException {
  122. if (predictor != PREDICTION_NONE) {
  123. this.colors = colors;
  124. } else {
  125. throw new PDFFilterException(
  126. "Prediction must not be PREDICTION_NONE in"
  127. + " order to set Colors");
  128. }
  129. }
  130. /**
  131. * Get the colors for this filter.
  132. *
  133. * @return the colors for this filter
  134. */
  135. public int getColors() {
  136. return colors;
  137. }
  138. /**
  139. * Set the number of bits per component.
  140. *
  141. * @param bits the number of bits per component
  142. * @throws PDFFilterException if predictor is not PREDICTION_NONE
  143. */
  144. public void setBitsPerComponent(int bits) throws PDFFilterException {
  145. if (predictor != PREDICTION_NONE) {
  146. bitsPerComponent = bits;
  147. } else {
  148. throw new PDFFilterException(
  149. "Prediction must not be PREDICTION_NONE in order"
  150. + " to set bitsPerComponent");
  151. }
  152. }
  153. /**
  154. * Get the number of bits per component.
  155. *
  156. * @return the number of bits per component
  157. */
  158. public int getBitsPerComponent() {
  159. return bitsPerComponent;
  160. }
  161. /**
  162. * Set the number of columns for this filter.
  163. *
  164. * @param columns the number of columns to use for the filter
  165. * @throws PDFFilterException if predictor is not PREDICTION_NONE
  166. */
  167. public void setColumns(int columns) throws PDFFilterException {
  168. if (predictor != PREDICTION_NONE) {
  169. this.columns = columns;
  170. } else {
  171. throw new PDFFilterException(
  172. "Prediction must not be PREDICTION_NONE in"
  173. + " order to set Columns");
  174. }
  175. }
  176. /**
  177. * Get the number of columns for this filter.
  178. *
  179. * @return the number of columns
  180. */
  181. public int getColumns() {
  182. return columns;
  183. }
  184. /** {@inheritDoc} */
  185. public OutputStream applyFilter(OutputStream out) throws IOException {
  186. if (isApplied()) {
  187. return out;
  188. } else {
  189. return new FlateEncodeOutputStream(out);
  190. }
  191. }
  192. }