mirror of
https://github.com/unidoc/unipdf.git
synced 2025-05-05 19:30:30 +08:00
73 lines
1.9 KiB
Go
73 lines
1.9 KiB
Go
/*
|
|
* This file is subject to the terms and conditions defined in
|
|
* file 'LICENSE.md', which is part of this source code package.
|
|
*/
|
|
|
|
package core
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/unidoc/unidoc/common"
|
|
)
|
|
|
|
// Creates the encoder from the stream's dictionary.
|
|
func NewEncoderFromStream(streamObj *PdfObjectStream) (StreamEncoder, error) {
|
|
filterObj, hasFilter := (*(streamObj.PdfObjectDictionary))["Filter"]
|
|
if !hasFilter {
|
|
// No filter, return raw data back.
|
|
return NewRawEncoder(), nil
|
|
}
|
|
|
|
// The filter should be a name or an array with a list of filter names.
|
|
// Currently only supporting a single filter.
|
|
method, ok := filterObj.(*PdfObjectName)
|
|
if !ok {
|
|
array, ok := filterObj.(*PdfObjectArray)
|
|
if !ok {
|
|
return nil, fmt.Errorf("Filter not a Name or Array object")
|
|
}
|
|
if len(*array) != 1 {
|
|
return nil, fmt.Errorf("Currently not supporting serial multi filter decoding")
|
|
}
|
|
filterObj = (*array)[0]
|
|
method, ok = filterObj.(*PdfObjectName)
|
|
if !ok {
|
|
return nil, fmt.Errorf("Filter array member not a Name object")
|
|
}
|
|
}
|
|
|
|
if *method == "FlateDecode" {
|
|
return newFlateEncoderFromStream(streamObj)
|
|
} else if *method == "ASCIIHexDecode" {
|
|
return NewASCIIHexEncoder(), nil
|
|
} else if *method == "LZWDecode" {
|
|
return newLZWEncoderFromStream(streamObj)
|
|
}
|
|
|
|
common.Log.Debug("ERROR: Unsupported encoding method!")
|
|
return nil, fmt.Errorf("Unsupported encoding method (%s)", *method)
|
|
}
|
|
|
|
// Decodes the stream.
|
|
// Supports FlateDecode, ASCIIHexDecode, LZW.
|
|
func DecodeStream(streamObj *PdfObjectStream) ([]byte, error) {
|
|
common.Log.Debug("Decode stream")
|
|
|
|
encoder, err := NewEncoderFromStream(streamObj)
|
|
if err != nil {
|
|
common.Log.Debug("Stream decoding failed: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
common.Log.Debug("Encoder: %+v\n", encoder)
|
|
|
|
decoded, err := encoder.DecodeStream(streamObj)
|
|
if err != nil {
|
|
common.Log.Debug("Stream decoding failed: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
return decoded, nil
|
|
}
|