func main() { var buffer bytes.Buffer enc := ascii85.NewEncoder(io.MultiWriter(os.Stdout, &buffer)) log.Println("encoding to stdout") _, err := enc.Write(data()) enc.Close() if err != nil { log.Fatalf("failed encoding: %s", err) } println() dec := ascii85.NewDecoder(&buffer) log.Println("decoding to stdout") io.Copy(os.Stdout, dec) }
// pd.DecodedStream() returns decoded contents of a stream. func (pd *PdfReaderT) DecodedStream(reference []byte) (DictionaryT, []byte) { dic, data := pd.Stream(reference) if f, ok := dic["/Filter"]; ok { filter := pd.ForcedArray(f) var decos [][]byte if d, ok := dic["/DecodeParams"]; ok { decos = pd.ForcedArray(d) } else { decos = make([][]byte, len(filter)) } for ff := range filter { deco := pd.Dic(decos[ff]) switch string(filter[ff]) { case "/FlateDecode": data = fancy.ReadAndClose(zlib.NewInflater(fancy.SliceReader(data))) case "/LZWDecode": early := true if deco != nil { if s, ok := deco["/EarlyChange"]; ok { early = pd.Num(s) == 1 } } data = lzw.Decode(data, early) case "/ASCII85Decode": ds := data for len(ds) > 1 && ds[len(ds)-1] < 33 { ds = ds[0 : len(ds)-1] } if len(ds) >= 2 && ds[len(ds)-1] == '>' && ds[len(ds)-2] == '~' { ds = ds[0 : len(ds)-2] } data = fancy.ReadAll(ascii85.NewDecoder(fancy.SliceReader(ds))) case "/ASCIIHexDecode": data = hex.Decode(string(data)) default: data = []byte{} } } } return dic, data }
func decodeStream(dic DictionaryT, data []byte) []byte { if f, ok := dic["/Filter"]; ok { filter := ForcedArray(f) var decos [][]byte if d, ok := dic["/DecodeParms"]; ok { decos = ForcedArray(d) } else { decos = make([][]byte, len(filter)) } for ff := range filter { // XXX: if there are multiple filters but only one DecodeParams, // it should be used for all filters deco := Dictionary(decos[ff]) switch string(filter[ff]) { case "/FlateDecode": data = fancy.ReadAndClose(zlib.NewReader(fancy.SliceReader(data))) case "/LZWDecode": early := true if deco != nil { if s, ok := deco["/EarlyChange"]; ok { early = num(s) == 1 } } data = lzw.Decode(data, early) case "/ASCII85Decode": ds := data for len(ds) > 1 && ds[len(ds)-1] < 33 { ds = ds[0 : len(ds)-1] } if len(ds) >= 2 && ds[len(ds)-1] == '>' && ds[len(ds)-2] == '~' { ds = ds[0 : len(ds)-2] } data = fancy.ReadAll(ascii85.NewDecoder(fancy.SliceReader(ds))) case "/ASCIIHexDecode": data, _ = hex.DecodeString(string(data)) default: util.Log("Unsupported filter", string(filter[ff])) data = []byte{} } if s, ok := deco["/Predictor"]; ok { pred := num(s) switch { case pred == 1: // no predictor case pred > 10: colors := numdef(deco["/Colors"], 1) columns := numdef(deco["/Columns"], 1) bitspercomponent := numdef(deco["/BitsPerComponent"], 8) util.Log("applying predictor", pred, colors, columns, bitspercomponent) data = util.ApplyPNGPredictor(pred, colors, columns, bitspercomponent, data) default: util.Log("Unsupported predictor", pred) return nil } } } } return data }
} parameter := Dictionary{} if i < len(parameters) { parameter = parameters[i] } var err error stream, err = decoder(stream, parameter) if err != nil { return nil, errors.New(string(filter) + ": " + err.Error()) } } return stream, nil } var decoders = map[Name]func([]byte, Dictionary) ([]byte, error){ Name("ASCII85Decode"): func(encoded []byte, dict Dictionary) ([]byte, error) { // the -3 strips the end of data marker return ioutil.ReadAll(ascii85.NewDecoder(bytes.NewBuffer(encoded[:len(encoded)-3]))) }, Name("FlateDecode"): func(encoded []byte, dict Dictionary) ([]byte, error) { return ioutil.ReadAll(flate.NewReader(bytes.NewBuffer(encoded[2:]))) }, // There is some problem with LZWDecode and TestFilterExample3 // Name("LZWDecode"): func(encoded []byte, dict Dictionary) ([]byte, error) { // return ioutil.ReadAll(lzw.NewReader(bytes.NewBuffer(encoded[:len(encoded)-3]), lzw.MSB, 8)) // }, }