170 lines
4.6 KiB
Go
Raw Normal View History

JBIG2 Generic Encoder (#264) * Prepared skeleton and basic component implementations for the jbig2 encoding. * Added Bitset. Implemented Bitmap. * Decoder with old Arithmetic Decoder * Partly working arithmetic * Working arithmetic decoder. * MMR patched. * rebuild to apache. * Working generic * Working generic * Decoded full document * Update Jenkinsfile go version [master] (#398) * Update Jenkinsfile go version * Decoded AnnexH document * Minor issues fixed. * Update README.md * Fixed generic region errors. Added benchmark. Added bitmap unpadder. Added Bitmap toImage method. * Fixed endofpage error * Added integration test. * Decoded all test files without errors. Implemented JBIG2Global. * Merged with v3 version * Fixed the EOF in the globals issue * Fixed the JBIG2 ChocolateData Decode * JBIG2 Added license information * Minor fix in jbig2 encoding. * Applied the logging convention * Cleaned unnecessary imports * Go modules clear unused imports * checked out the README.md * Moved trace to Debug. Fixed the build integrate tag in the document_decode_test.go * Initial encoder skeleton * Applied UniPDF Developer Guide. Fixed lint issues. * Cleared documentation, fixed style issues. * Added jbig2 doc.go files. Applied unipdf guide style. * Minor code style changes. * Minor naming and style issues fixes. * Minor naming changes. Style issues fixed. * Review r11 fixes. * Added JBIG2 Encoder skeleton. * Moved Document and Page to jbig2/document package. Created decoder package responsible for decoding jbig2 stream. * Implemented raster functions. * Added raster uni low test funcitons. * Added raster low test functions * untracked files on jbig2-encoder: c869089 Added raster low test functions * index on jbig2-encoder: c869089 Added raster low test functions * Added morph files. * implemented jbig2 encoder basics * JBIG2 Encoder - Generic method * Added jbig2 image encode ttests, black/white image tests * cleaned and tested jbig2 package * unfinished jbig2 classified encoder * jbig2 minor style changes * minor jbig2 encoder changes * prepared JBIG2 Encoder * Style and lint fixes * Minor changes and lints * Fixed shift unsinged value build errors * Minor naming change * Added jbig2 encode, image gondels. Fixed jbig2 decode bug. * Provided jbig2 core.DecodeGlobals function. * Fixed JBIG2Encoder `r6` revision issues. * Removed public JBIG2Encoder document. * Minor style changes * added NewJBIG2Encoder function. * fixed JBIG2Encoder 'r9' revision issues. * Cleared 'r9' commented code. * Updated ACKNOWLEDGEMENETS. Fixed JBIG2Encoder 'r10' revision issues. Co-authored-by: Gunnsteinn Hall <gunnsteinn.hall@gmail.com>
2020-03-27 12:47:41 +01:00
/*
* This file is subject to the terms and conditions defined in
* file 'LICENSE.md', which is part of this source code package.
*/
package segments
import (
"encoding/binary"
"fmt"
"math"
"strings"
"github.com/unidoc/unipdf/v3/common"
"github.com/unidoc/unipdf/v3/internal/jbig2/bitmap"
"github.com/unidoc/unipdf/v3/internal/jbig2/errors"
"github.com/unidoc/unipdf/v3/internal/jbig2/reader"
"github.com/unidoc/unipdf/v3/internal/jbig2/writer"
)
// RegionSegment is the model representing base jbig2 segment region - see 7.4.1.
type RegionSegment struct {
r reader.StreamReader
// Region segment bitmap width, 7.4.1.1
BitmapWidth uint32
// Region segment bitmap height, 7.4.1.2
BitmapHeight uint32
// Region segment bitmap X location, 7.4.1.3
XLocation uint32
// Region segment bitmap Y location, 7.4.1.4
YLocation uint32
// Region segment flags, 7.4.1.5
CombinaionOperator bitmap.CombinationOperator
}
// NewRegionSegment creates new Region segment model.
func NewRegionSegment(r reader.StreamReader) *RegionSegment {
return &RegionSegment{r: r}
}
// compile time check for the SegmentEncoder interface.
var _ SegmentEncoder = &RegionSegment{}
// Encode implements the SegmentEncoder interface.
func (r *RegionSegment) Encode(w writer.BinaryWriter) (n int, err error) {
const processName = "RegionSegment.Encode"
// the region segment encodes as follows:
// segment bitmap width - big endian uint32
// segment bitmap height - big endian uint32
// segment bitmap x location - big endian uint32
// segment bitmap y location - big endian uint32
// segment flags - 1 byte - 5 empty bits + 3 combination operator
// prepare 4 bytes temporary slice.
temp := make([]byte, 4)
// encode region segment width
binary.BigEndian.PutUint32(temp, r.BitmapWidth)
n, err = w.Write(temp)
if err != nil {
return 0, errors.Wrap(err, processName, "Width")
}
// encode region segment height
binary.BigEndian.PutUint32(temp, r.BitmapHeight)
var tempCount int
tempCount, err = w.Write(temp)
if err != nil {
return 0, errors.Wrap(err, processName, "Height")
}
n += tempCount
// encode region segment x location
binary.BigEndian.PutUint32(temp, r.XLocation)
tempCount, err = w.Write(temp)
if err != nil {
return 0, errors.Wrap(err, processName, "XLocation")
}
n += tempCount
// encode region segment y location
binary.BigEndian.PutUint32(temp, r.YLocation)
tempCount, err = w.Write(temp)
if err != nil {
return 0, errors.Wrap(err, processName, "YLocation")
}
n += tempCount
// the region segment flags is composed of:
// 5 zero bits + 3 bits for the external combination operator
if err = w.WriteByte(byte(r.CombinaionOperator) & 0x07); err != nil {
return 0, errors.Wrap(err, processName, "combination operator")
}
n++
return n, nil
}
// Size returns the bytewise size of the region segment.
func (r *RegionSegment) Size() int {
// width + height + xlocation + ylocation + flags = 17
// 4 + 4 + 4 + 4 + 1 = 17
return 17
}
// String implements the Stringer interface.
func (r *RegionSegment) String() string {
sb := &strings.Builder{}
sb.WriteString("\t[REGION SEGMENT]\n")
sb.WriteString(fmt.Sprintf("\t\t- Bitmap (width, height) [%dx%d]\n", r.BitmapWidth, r.BitmapHeight))
sb.WriteString(fmt.Sprintf("\t\t- Location (x,y): [%d,%d]\n", r.XLocation, r.YLocation))
sb.WriteString(fmt.Sprintf("\t\t- CombinationOperator: %s", r.CombinaionOperator))
return sb.String()
}
// parseHeader parses the RegionSegment header.
func (r *RegionSegment) parseHeader() error {
const processName = "parseHeader"
common.Log.Trace("[REGION][PARSE-HEADER] Begin")
defer func() {
common.Log.Trace("[REGION][PARSE-HEADER] Finished")
}()
temp, err := r.r.ReadBits(32)
if err != nil {
return errors.Wrap(err, processName, "width")
}
r.BitmapWidth = uint32(temp & math.MaxUint32)
temp, err = r.r.ReadBits(32)
if err != nil {
return errors.Wrap(err, processName, "height")
}
r.BitmapHeight = uint32(temp & math.MaxUint32)
temp, err = r.r.ReadBits(32)
if err != nil {
return errors.Wrap(err, processName, "x location")
}
r.XLocation = uint32(temp & math.MaxUint32)
temp, err = r.r.ReadBits(32)
if err != nil {
return errors.Wrap(err, processName, "y location")
}
r.YLocation = uint32(temp & math.MaxUint32)
// Bit 3-7
if _, err = r.r.ReadBits(5); err != nil {
return errors.Wrap(err, processName, "diry read")
}
// Bit 0-2
if err = r.readCombinationOperator(); err != nil {
return errors.Wrap(err, processName, "combination operator")
}
return nil
}
func (r *RegionSegment) readCombinationOperator() error {
temp, err := r.r.ReadBits(3)
if err != nil {
return err
}
r.CombinaionOperator = bitmap.CombinationOperator(temp & 0xF)
return nil
}