segments

package
v0.0.0-...-73e6ce1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 27, 2023 License: AGPL-3.0, AGPL-3.0-only Imports: 18 Imported by: 0

Documentation

Overview

Package segments provides jbig2 segments models used in the jbig2 document decoding process.

Index

Constants

View Source
const BorderSize = 6

BorderSize is the constant border size for symbols.

Variables

This section is empty.

Functions

This section is empty.

Types

type Documenter

type Documenter interface {
	// GetPage gets the page at given page number.
	GetPage(int) (Pager, error)
	// GetGlobalSegment gets the global segment header at given segment number.
	GetGlobalSegment(int) (*Header, error)
}

Documenter is the interface used for the document model.

type EncodeInitializer

type EncodeInitializer interface {
	// InitEncode initializes the segment for the encode method purpose.
	InitEncode()
}

EncodeInitializer is the interface used to initialize the segments for the encode process.

type EndOfStripe

type EndOfStripe struct {
	// contains filtered or unexported fields
}

EndOfStripe flags an end of stripe - see 7.4.9.

func (*EndOfStripe) Init

func (e *EndOfStripe) Init(h *Header, r reader.StreamReader) error

Init implements Segmenter interface.

func (*EndOfStripe) LineNumber

func (e *EndOfStripe) LineNumber() int

LineNumber gets the EndOfStripe line number.

type GenericRefinementRegion

type GenericRefinementRegion struct {

	// Region segment information flags 7.4.1.
	RegionInfo *RegionSegment

	// Generic refinement region segment flags 7.4.7.2.
	IsTPGROn   bool
	TemplateID int8

	Template templater

	// Generic refinement region segment AT flags 7.4.7.3.
	GrAtX []int8
	GrAtY []int8

	// Decoded data as pixel values (use row stride/width to wrap line).
	RegionBitmap *bitmap.Bitmap

	// Variables for decoding.
	ReferenceBitmap *bitmap.Bitmap
	ReferenceDX     int32
	ReferenceDY     int32
	// contains filtered or unexported fields
}

GenericRefinementRegion represtents jbig2 generic refinement region segment - 7.4.7.

func (*GenericRefinementRegion) GetRegionBitmap

func (g *GenericRefinementRegion) GetRegionBitmap() (*bitmap.Bitmap, error)

GetRegionBitmap implements Regioner interface.

func (*GenericRefinementRegion) GetRegionInfo

func (g *GenericRefinementRegion) GetRegionInfo() *RegionSegment

GetRegionInfo implements Regioner interface.

func (*GenericRefinementRegion) Init

Init implements Segmenter interface.

func (*GenericRefinementRegion) String

func (g *GenericRefinementRegion) String() string

String implements the Stringer interface.

type GenericRegion

type GenericRegion struct {
	DataHeaderOffset int64
	DataHeaderLength int64
	DataOffset       int64
	DataLength       int64

	// Region segment information field, 7.4.1
	RegionSegment *RegionSegment

	// Generic region segment flags, 7.4.6.2
	UseExtTemplates bool
	IsTPGDon        bool
	GBTemplate      byte
	IsMMREncoded    bool
	UseMMR          bool

	// Generic region segment AT flags, 7.4.6.3
	GBAtX        []int8
	GBAtY        []int8
	GBAtOverride []bool

	// Bitmap is the decoded generic region image.
	Bitmap *bitmap.Bitmap
	// contains filtered or unexported fields
}

GenericRegion represents a generic region segment. Parsing is done as described in 7.4.5. Decoding procedure is done as described in 6.2.5.7 and 7.4.6.4.

func NewGenericRegion

func NewGenericRegion(r reader.StreamReader) *GenericRegion

NewGenericRegion creates new GenericRegion segment.

func (*GenericRegion) Encode

func (g *GenericRegion) Encode(w writer.BinaryWriter) (n int, err error)

Encode implements SegmentEncoder interface.

func (*GenericRegion) GetRegionBitmap

func (g *GenericRegion) GetRegionBitmap() (bm *bitmap.Bitmap, err error)

GetRegionBitmap gets the bitmap for the generic region segment.

func (*GenericRegion) GetRegionInfo

func (g *GenericRegion) GetRegionInfo() *RegionSegment

GetRegionInfo implements Regioner interface.

func (*GenericRegion) Init

func (g *GenericRegion) Init(h *Header, r reader.StreamReader) error

Init implements Segmenter interface.

func (*GenericRegion) InitEncode

func (g *GenericRegion) InitEncode(bm *bitmap.Bitmap, xLoc, yLoc, template int, duplicateLineRemoval bool) error

InitEncode initializes the generic region for the provided bitmap 'bm', it's 'xLoc', 'yLoc' locations and if it has to remove duplicated lines.

func (*GenericRegion) Size

func (g *GenericRegion) Size() int

Size returns the byte size of the generic region.

func (*GenericRegion) String

func (g *GenericRegion) String() string

String implements Stringer interface.

type HalftoneRegion

type HalftoneRegion struct {
	DataHeaderOffset int64
	DataHeaderLength int64
	DataOffset       int64
	DataLength       int64

	// Region segment information field, 7.4.1.
	RegionSegment *RegionSegment

	// Halftone segment information field, 7.4.5.1.1.
	HDefaultPixel       int8
	CombinationOperator bitmap.CombinationOperator
	HSkipEnabled        bool
	HTemplate           byte
	IsMMREncoded        bool

	// Halftone grid position and size, 7.4.5.1.2
	// Width of the gray-scale image, 7.4.5.1.2.1
	HGridWidth uint32
	// Height of the gray-scale image, 7.4.5.1.2.2
	HGridHeight uint32
	// Horizontal offset of the grid, 7.4.5.1.2.3
	HGridX int32
	// Vertical offset of the grid, 7.4.5.1.2.4
	HGridY int32

	// Halftone grid vector, 7.4.5.1.3
	// Horizontal coordinate of the halftone grid vector, 7.4.5.1.3.1
	HRegionX uint16
	// Vertical coordinate of the halftone grod vector, 7.4.5.1.3.2
	HRegionY uint16

	// Decoded data
	HalftoneRegionBitmap *bitmap.Bitmap

	// Previously decoded data from other regions or dictionaries, stored to use as patterns in this region.
	Patterns []*bitmap.Bitmap
	// contains filtered or unexported fields
}

HalftoneRegion is the model for the jbig2 halftone region segment implementation - 7.4.5.1.

func (*HalftoneRegion) GetPatterns

func (h *HalftoneRegion) GetPatterns() ([]*bitmap.Bitmap, error)

GetPatterns gets the HalftoneRegion patterns.

func (*HalftoneRegion) GetRegionBitmap

func (h *HalftoneRegion) GetRegionBitmap() (*bitmap.Bitmap, error)

GetRegionBitmap implements Regioner interface.

func (*HalftoneRegion) GetRegionInfo

func (h *HalftoneRegion) GetRegionInfo() *RegionSegment

GetRegionInfo implements Regioner interface.

func (*HalftoneRegion) Init

func (h *HalftoneRegion) Init(hd *Header, r reader.StreamReader) error

Init implements Segmenter interface.

type Header struct {
	SegmentNumber            uint32
	Type                     Type
	RetainFlag               bool
	PageAssociation          int
	PageAssociationFieldSize bool
	RTSegments               []*Header
	HeaderLength             int64
	SegmentDataLength        uint64
	SegmentDataStartOffset   uint64
	Reader                   reader.StreamReader
	SegmentData              Segmenter
	// RTSNumbers is the list of numbers where the segment is referred to.
	RTSNumbers []int
	// RetainBits are the  flags for the given segment.
	RetainBits []uint8
}

Header is the segment header used to define the segment parameters - see 7.2.

func NewHeader

func NewHeader(d Documenter, r reader.StreamReader, offset int64, organizationType OrganizationType) (*Header, error)

NewHeader creates new segment header for the provided document from the stream reader.

func (*Header) CleanSegmentData

func (h *Header) CleanSegmentData()

CleanSegmentData cleans the segment's data setting its segment data to nil.

func (*Header) Encode

func (h *Header) Encode(w writer.BinaryWriter) (n int, err error)

Encode encodes the jbi2 header structure to the provided 'w' BinaryWriter.

func (*Header) GetSegmentData

func (h *Header) GetSegmentData() (Segmenter, error)

GetSegmentData gets the segment's data returning the Segmenter instance.

func (*Header) String

func (h *Header) String() string

String implements Stringer interface.

type OrganizationType

type OrganizationType uint8

OrganizationType is the enum for the stream sequence organization.

const (
	ORandom OrganizationType = iota
	OSequential
)

Organization types defined in D.4.2. - File header bit 0 defines the stream sequence organization.

type PageInformationSegment

type PageInformationSegment struct {

	// Page bitmap height, four byte, 7.4.8.1
	PageBMHeight int
	// Page bitmap width, four byte, 7.4.8.1
	PageBMWidth int
	// Page X resolution, four byte 7.4.8.3
	ResolutionX int
	// Page Y resolution, four byte 7.4.8.4
	ResolutionY int

	IsLossless bool

	// Page striping information, two byte 7.4.8.6
	IsStripe      bool
	MaxStripeSize uint16
	// contains filtered or unexported fields
}

PageInformationSegment represents the segment type Page Information 7.4.8.

func (*PageInformationSegment) CombinationOperator

func (p *PageInformationSegment) CombinationOperator() bitmap.CombinationOperator

CombinationOperator gets the combination operator used by the page information segment.

func (*PageInformationSegment) CombinationOperatorOverrideAllowed

func (p *PageInformationSegment) CombinationOperatorOverrideAllowed() bool

CombinationOperatorOverrideAllowed defines if the Page segment has allowed override.

func (*PageInformationSegment) DefaultPixelValue

func (p *PageInformationSegment) DefaultPixelValue() uint8

DefaultPixelValue returns page segment default pixel.

func (*PageInformationSegment) Encode

func (p *PageInformationSegment) Encode(w writer.BinaryWriter) (n int, err error)

Encode implements SegmentEncoder interface.

func (*PageInformationSegment) Init

func (p *PageInformationSegment) Init(h *Header, r reader.StreamReader) (err error)

Init implements Segmenter interface.

func (*PageInformationSegment) Size

func (p *PageInformationSegment) Size() int

Size returns the byte size of the page information segment data. The page information segment is composed of: - bitmap width - 4 bytes - bitmap height - 4 bytes - x resolution - 4 bytes - y resolution - 4 bytes - flags - 1 byte - stripping information - 2 bytes Total 19 bytes

func (*PageInformationSegment) String

func (p *PageInformationSegment) String() string

String implements Stringer interface.

type Pager

type Pager interface {
	// GetSegment gets the segment Header with the given segment number.
	GetSegment(int) (*Header, error)
	// GetBitmap gets the decoded bitmap.Bitmap.
	GetBitmap() (*bitmap.Bitmap, error)
}

Pager is the interface used as a Page model.

type PatternDictionary

type PatternDictionary struct {
	DataHeaderOffset int64
	DataHeaderLength int64
	DataOffset       int64
	DataLength       int64

	GBAtX []int8
	GBAtY []int8

	// Flags 7.4.4.1.1
	IsMMREncoded bool
	HDTemplate   byte

	// Width of the patterns in the pattern dictionary
	HdpWidth byte
	// Height of the patterns in the pattern dictionary
	HdpHeight byte

	// Decoded bitmaps stored to be used by segments that refer to it
	Patterns []*bitmap.Bitmap

	// Largest gray-scale value 7.4.4.1.4
	GrayMax uint32
	// contains filtered or unexported fields
}

PatternDictionary is the jbig2 model for the pattern dictionary segment - 7.4.4.

func (*PatternDictionary) GetDictionary

func (p *PatternDictionary) GetDictionary() ([]*bitmap.Bitmap, error)

GetDictionary gets the PatternDictionary segment Dictionary bitmaps.

func (*PatternDictionary) Init

Init implements Segmenter interface.

type RegionSegment

type RegionSegment struct {

	// 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
	// contains filtered or unexported fields
}

RegionSegment is the model representing base jbig2 segment region - see 7.4.1.

func NewRegionSegment

func NewRegionSegment(r reader.StreamReader) *RegionSegment

NewRegionSegment creates new Region segment model.

func (*RegionSegment) Encode

func (r *RegionSegment) Encode(w writer.BinaryWriter) (n int, err error)

Encode implements the SegmentEncoder interface.

func (*RegionSegment) Size

func (r *RegionSegment) Size() int

Size returns the bytewise size of the region segment.

func (*RegionSegment) String

func (r *RegionSegment) String() string

String implements the Stringer interface.

type Regioner

type Regioner interface {
	// GetRegionBitmap decodes and returns a regions content.
	GetRegionBitmap() (*bitmap.Bitmap, error)
	// GetRegionInfo returns RegionSegment information.
	GetRegionInfo() *RegionSegment
}

Regioner is the interface for all JBIG2 region segments.

type SegmentEncoder

type SegmentEncoder interface {
	// Encode encodes the segment and write into 'w' writer.
	Encode(w writer.BinaryWriter) (n int, err error)
}

SegmentEncoder is the interface used for encoding single segment instances.

type Segmenter

type Segmenter interface {
	// Init initializes the segment from the provided data stream 'r'.
	Init(header *Header, r reader.StreamReader) error
}

Segmenter is the interface for all data pars of segments.

type SymbolDictionary

type SymbolDictionary struct {

	// Symbol Dictionary flags, 7.4.2.1.1
	SdrTemplate int8
	SdTemplate  int8

	SdHuffAggInstanceSelection  bool
	SdHuffBMSizeSelection       int8
	SdHuffDecodeWidthSelection  int8
	SdHuffDecodeHeightSelection int8
	UseRefinementAggregation    bool
	IsHuffmanEncoded            bool

	// Symbol Dictionary AT flags 7.4.2.1.2
	SdATX []int8
	SdATY []int8

	// Symbol Dictionary refinement AT flags 7.4.2.1.3
	SdrATX []int8
	SdrATY []int8

	// Number of exported symbols, 7.4.2.1.4
	NumberOfExportedSymbols uint32

	// Number of new symbols 7.4.2.1.5
	NumberOfNewSymbols uint32

	// Further parameters
	Header *Header
	// contains filtered or unexported fields
}

SymbolDictionary is the model for the JBIG2 Symbol Dictionary Segment - see 7.4.2.

func (*SymbolDictionary) Encode

func (s *SymbolDictionary) Encode(w writer.BinaryWriter) (n int, err error)

Encode encodes the symbol dictionary structure into 'w' writer. Returns number of bytes written and the error if occurs.

func (*SymbolDictionary) GetDictionary

func (s *SymbolDictionary) GetDictionary() ([]*bitmap.Bitmap, error)

GetDictionary gets the decoded dictionary symbols as a bitmap slice.

func (*SymbolDictionary) Init

Init implements Segmenter interface.

func (*SymbolDictionary) InitEncode

func (s *SymbolDictionary) InitEncode(symbols *bitmap.Bitmaps, symbolList []int, symbolMap map[int]int, unborderSymbols bool) error

InitEncode initializes the symbol dictionary for the encode method.

func (*SymbolDictionary) String

func (s *SymbolDictionary) String() string

String implements the Stringer interface.

type TableSegment

type TableSegment struct {
	// contains filtered or unexported fields
}

TableSegment is the model used for user defined Huffman Table Segment - see 7.4.13 and appendix B.

func (*TableSegment) HtHigh

func (t *TableSegment) HtHigh() int32

HtHigh implements huffman.BasicTabler interface.

func (*TableSegment) HtLow

func (t *TableSegment) HtLow() int32

HtLow implements huffman.BasicTabler interface.

func (*TableSegment) HtOOB

func (t *TableSegment) HtOOB() int32

HtOOB implements huffman.BasicTabler interface.

func (*TableSegment) HtPS

func (t *TableSegment) HtPS() int32

HtPS implements huffman.BasicTabler interface.

func (*TableSegment) HtRS

func (t *TableSegment) HtRS() int32

HtRS implements huffman.BasicTabler interface.

func (*TableSegment) Init

func (t *TableSegment) Init(h *Header, r reader.StreamReader) error

Init initializes the table segment. Implements Segmenter interface.

func (*TableSegment) StreamReader

func (t *TableSegment) StreamReader() reader.StreamReader

StreamReader implements huffman.BasicTabler interface.

type TextRegion

type TextRegion struct {

	// Region segment information field 7.4.1.
	RegionInfo *RegionSegment

	// Text Region segment flags 7.4.3.1.1.
	SbrTemplate         int8
	SbdsOffset          int8
	DefaultPixel        int8
	CombinationOperator bitmap.CombinationOperator
	IsTransposed        int8
	ReferenceCorner     int16
	LogSBStrips         int16
	UseRefinement       bool
	IsHuffmanEncoded    bool

	// Text region segment huffman flags 7.4.3.1.2.
	SbHuffRSize    int8
	SbHuffRDY      int8
	SbHuffRDX      int8
	SbHuffRDHeight int8
	SbHuffRDWidth  int8
	SbHuffDT       int8
	SbHuffDS       int8
	SbHuffFS       int8

	// Text region refinement AT flags 7.4.3.1.3.
	SbrATX []int8
	SbrATY []int8

	// Number of symbol instances 7.4.3.1.3.
	NumberOfSymbolInstances uint32

	SbStrips        int8
	NumberOfSymbols uint32

	RegionBitmap *bitmap.Bitmap
	Symbols      []*bitmap.Bitmap

	Header *Header
	// contains filtered or unexported fields
}

TextRegion is the model for the jbig2 text region segment - see 7.4.1.

func (*TextRegion) Encode

func (t *TextRegion) Encode(w writer.BinaryWriter) (n int, err error)

Encode writes the TextRegion segment data into 'w' binary writer.

func (*TextRegion) GetRegionBitmap

func (t *TextRegion) GetRegionBitmap() (*bitmap.Bitmap, error)

GetRegionBitmap implements Regioner interface.

func (*TextRegion) GetRegionInfo

func (t *TextRegion) GetRegionInfo() *RegionSegment

GetRegionInfo implements Regioner interface.

func (*TextRegion) Init

func (t *TextRegion) Init(header *Header, r reader.StreamReader) error

Init implements Segmenter interface.

func (*TextRegion) InitEncode

func (t *TextRegion) InitEncode(globalSymbolsMap, localSymbolsMap map[int]int, comps []int, inLL *bitmap.Points, symbols *bitmap.Bitmaps, classIDs *basic.IntSlice, boxes *bitmap.Boxes, width, height, symbits int)

InitEncode initializes text region for the Encode method.

func (*TextRegion) String

func (t *TextRegion) String() string

String implements the Stringer interface.

type Type

type Type int

Type defines the jbig2 segment type - see 7.3.

const (
	TSymbolDictionary                         Type = 0
	TIntermediateTextRegion                   Type = 4
	TImmediateTextRegion                      Type = 6
	TImmediateLosslessTextRegion              Type = 7
	TPatternDictionary                        Type = 16
	TIntermediateHalftoneRegion               Type = 20
	TImmediateHalftoneRegion                  Type = 22
	TImmediateLosslessHalftoneRegion          Type = 23
	TIntermediateGenericRegion                Type = 36
	TImmediateGenericRegion                   Type = 38
	TImmediateLosslessGenericRegion           Type = 39
	TIntermediateGenericRefinementRegion      Type = 40
	TImmediateGenericRefinementRegion         Type = 42
	TImmediateLosslessGenericRefinementRegion Type = 43
	TPageInformation                          Type = 48
	TEndOfPage                                Type = 49
	TEndOfStrip                               Type = 50
	TEndOfFile                                Type = 51
	TProfiles                                 Type = 52
	TTables                                   Type = 53
	TExtension                                Type = 62
	TBitmap                                   Type = 70
)

Enumerate segment type definitions.

func (Type) String

func (k Type) String() string

String implements Stringer interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL