1 51 package org.apache.fop.pdf; 52 53 import java.io.ByteArrayOutputStream ; 54 import java.io.IOException ; 55 import java.util.zip.DeflaterOutputStream ; 56 57 64 public class FlateFilter extends PDFFilter { 65 66 public static final int PREDICTION_NONE = 1; 67 public static final int PREDICTION_TIFF2 = 2; 68 public static final int PREDICTION_PNG_NONE = 10; 69 public static final int PREDICTION_PNG_SUB = 11; 70 public static final int PREDICTION_PNG_UP = 12; 71 public static final int PREDICTION_PNG_AVG = 13; 72 public static final int PREDICTION_PNG_PAETH = 14; 73 public static final int PREDICTION_PNG_OPT = 15; 74 75 76 private int _predictor = PREDICTION_NONE; 77 private int _colors; 78 private int _bitsPerComponent; 79 private int _columns; 80 81 public String getName() { 82 return "/FlateDecode"; 83 } 84 85 public String getDecodeParms() { 86 if (_predictor > PREDICTION_NONE) { 87 StringBuffer sb = new StringBuffer (); 88 sb.append("<< /Predictor "); 89 sb.append(_predictor); 90 if (_colors > 0) { 91 sb.append(" /Colors " + _colors); 92 } 93 if (_bitsPerComponent > 0) { 94 sb.append(" /BitsPerComponent " + _bitsPerComponent); 95 } 96 if (_columns > 0) { 97 sb.append(" /Columns " + _columns); 98 } 99 sb.append(" >> "); 100 return sb.toString(); 101 } 102 return null; 103 } 104 105 106 112 public byte[] encode(byte[] data) { 113 ByteArrayOutputStream outArrayStream = new ByteArrayOutputStream (); 114 _predictor = PREDICTION_NONE; 115 try { 116 DeflaterOutputStream compressedStream = 117 new DeflaterOutputStream (outArrayStream); 118 compressedStream.write(data, 0, data.length); 119 compressedStream.flush(); 120 compressedStream.close(); 121 } catch (IOException e) { 122 org.apache.fop.messaging.MessageHandler.error("Fatal error: " 123 + e.getMessage()); 124 e.printStackTrace(); 125 } 126 127 return outArrayStream.toByteArray(); 128 } 129 130 public void setPredictor(int predictor) throws PDFFilterException { 131 _predictor = predictor; 132 133 } 134 135 public int getPredictor() { 136 return _predictor; 137 } 138 139 140 public void setColors(int colors) throws PDFFilterException { 141 if (_predictor != PREDICTION_NONE) { 142 _colors = colors; 143 } else { 144 throw new PDFFilterException("Prediction must not be PREDICTION_NONE in order to set Colors"); 145 } 146 } 147 148 public int getColors() { 149 return _colors; 150 } 151 152 153 public void setBitsPerComponent(int bits) throws PDFFilterException { 154 if (_predictor != PREDICTION_NONE) { 155 _bitsPerComponent = bits; 156 } else { 157 throw new PDFFilterException("Prediction must not be PREDICTION_NONE in order to set bitsPerComponent"); 158 } 159 } 160 161 public int getBitsPerComponent() { 162 return _bitsPerComponent; 163 } 164 165 166 public void setColumns(int columns) throws PDFFilterException { 167 if (_predictor != PREDICTION_NONE) { 168 _columns = columns; 169 } else { 170 throw new PDFFilterException("Prediction must not be PREDICTION_NONE in order to set Columns"); 171 } 172 } 173 174 public int getColumns() { 175 return _columns; 176 } 177 178 179 } 180 | Popular Tags |