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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.pdf;
  8. import java.io.ByteArrayOutputStream;
  9. import java.io.IOException;
  10. import java.util.zip.DeflaterOutputStream;
  11. /**
  12. * A filter to deflate a stream. Note that the attributes for
  13. * prediction, colors, bitsPerComponent, and columns are not supported
  14. * when this filter is used to handle the data compression. They are
  15. * only valid for externally encoded data such as that from a graphics
  16. * file.
  17. */
  18. public class FlateFilter extends PDFFilter {
  19. public static final int PREDICTION_NONE = 1;
  20. public static final int PREDICTION_TIFF2 = 2;
  21. public static final int PREDICTION_PNG_NONE = 10;
  22. public static final int PREDICTION_PNG_SUB = 11;
  23. public static final int PREDICTION_PNG_UP = 12;
  24. public static final int PREDICTION_PNG_AVG = 13;
  25. public static final int PREDICTION_PNG_PAETH = 14;
  26. public static final int PREDICTION_PNG_OPT = 15;
  27. private int _predictor = PREDICTION_NONE;
  28. private int _colors;
  29. private int _bitsPerComponent;
  30. private int _columns;
  31. public String getName() {
  32. return "/FlateDecode";
  33. }
  34. public String getDecodeParms() {
  35. if (_predictor > PREDICTION_NONE) {
  36. StringBuffer sb = new StringBuffer();
  37. sb.append("<< /Predictor ");
  38. sb.append(_predictor);
  39. if (_colors > 0) {
  40. sb.append(" /Colors " + _colors);
  41. }
  42. if (_bitsPerComponent > 0) {
  43. sb.append(" /BitsPerComponent " + _bitsPerComponent);
  44. }
  45. if (_columns > 0) {
  46. sb.append(" /Columns " + _columns);
  47. }
  48. sb.append(" >> ");
  49. return sb.toString();
  50. }
  51. return null;
  52. }
  53. /**
  54. * Encode the given data and return it. Note: a side effect of
  55. * this method is that it resets the prediction to the default
  56. * because these attributes are not supported. So the DecodeParms
  57. * should be retrieved after calling this method.
  58. */
  59. public byte[] encode(byte[] data) {
  60. ByteArrayOutputStream outArrayStream = new ByteArrayOutputStream();
  61. _predictor = PREDICTION_NONE;
  62. try {
  63. DeflaterOutputStream compressedStream =
  64. new DeflaterOutputStream(outArrayStream);
  65. compressedStream.write(data, 0, data.length);
  66. compressedStream.flush();
  67. compressedStream.close();
  68. } catch (IOException e) {
  69. //log.error("Fatal error: "
  70. // + e.getMessage(), e);
  71. }
  72. return outArrayStream.toByteArray();
  73. }
  74. public void setPredictor(int predictor) throws PDFFilterException {
  75. _predictor = predictor;
  76. }
  77. public int getPredictor() {
  78. return _predictor;
  79. }
  80. public void setColors(int colors) throws PDFFilterException {
  81. if (_predictor != PREDICTION_NONE) {
  82. _colors = colors;
  83. } else {
  84. throw new PDFFilterException("Prediction must not be PREDICTION_NONE in order to set Colors");
  85. }
  86. }
  87. public int getColors() {
  88. return _colors;
  89. }
  90. public void setBitsPerComponent(int bits) throws PDFFilterException {
  91. if (_predictor != PREDICTION_NONE) {
  92. _bitsPerComponent = bits;
  93. } else {
  94. throw new PDFFilterException("Prediction must not be PREDICTION_NONE in order to set bitsPerComponent");
  95. }
  96. }
  97. public int getBitsPerComponent() {
  98. return _bitsPerComponent;
  99. }
  100. public void setColumns(int columns) throws PDFFilterException {
  101. if (_predictor != PREDICTION_NONE) {
  102. _columns = columns;
  103. } else {
  104. throw new PDFFilterException("Prediction must not be PREDICTION_NONE in order to set Columns");
  105. }
  106. }
  107. public int getColumns() {
  108. return _columns;
  109. }
  110. }