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 5.8KB

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