jpm

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package jpm provides JPEG 2000 Part 6 (JPM) compound image support per ISO/IEC 15444-6.

JPM is designed for compound document images that combine raster and mask layers, such as scanned documents with text, graphics, and photographic content. The format extends JP2 with features specific to document imaging:

Key features:

  • Compound image model with multiple layers
  • Mixed raster (continuous-tone) and mask (bi-level) layers
  • Page layout and object positioning
  • Multi-page document support
  • MRC (Mixed Raster Content) model compatibility

JPM uses an ISO base media file format container with specialized boxes for compound document structure.

Index

Constants

View Source
const (
	// Base JP2/JPX box types (for reference)
	BoxTypeSignature   = 0x6A502020 // 'jP  ' - JP2 Signature
	BoxTypeFileType    = 0x66747970 // 'ftyp' - File Type
	BoxTypeJP2Header   = 0x6A703268 // 'jp2h' - JP2 Header
	BoxTypeImageHeader = 0x69686472 // 'ihdr' - Image Header
	BoxTypeColourSpec  = 0x636F6C72 // 'colr' - Colour Specification
	BoxTypeCodestream  = 0x6A703263 // 'jp2c' - Contiguous Codestream

	// JPM-specific box types
	BoxTypeCompoundImageHeader = 0x6A706D68 // 'jpmh' - Compound Image Header
	BoxTypePageCollection      = 0x70636F6C // 'pcol' - Page Collection
	BoxTypePage                = 0x70616765 // 'page' - Page Box
	BoxTypeLayoutObject        = 0x6C6F626A // 'lobj' - Layout Object
	BoxTypeLayoutObjectHeader  = 0x6C686472 // 'lhdr' - Layout Object Header
	BoxTypeObjectData          = 0x6F626A64 // 'objd' - Object Data
	BoxTypeMask                = 0x6D736B20 // 'msk ' - Mask
	BoxTypeMaskEntry           = 0x6D656E74 // 'ment' - Mask Entry

	// Additional boxes for compound image structure
	BoxTypeObjectInfo    = 0x6F62696E // 'obin' - Object Information
	BoxTypeLayoutInfo    = 0x6C696E66 // 'linf' - Layout Information
	BoxTypeRasterInfo    = 0x72696E66 // 'rinf' - Raster Information
	BoxTypeComposingInfo = 0x63696E66 // 'cinf' - Composing Information
	BoxTypeFree          = 0x66726565 // 'free' - Free space
	BoxTypeMediaData     = 0x6D646174 // 'mdat' - Media Data
)

JPM box types as per ISO/IEC 15444-6. These extend the base JP2 box types for compound document images.

View Source
const (
	LayoutObjectTypeRaster  = 0 // Continuous-tone raster image
	LayoutObjectTypeMask    = 1 // Bi-level mask image
	LayoutObjectTypeText    = 2 // Text object (rendered as mask)
	LayoutObjectTypeGraphic = 3 // Graphic/vector object
)

Layout object types

View Source
const (
	MaskTypeHard = 0 // Hard-edged mask (bi-level)
	MaskTypeSoft = 1 // Soft mask (grayscale)
)

Mask types for compound images

View Source
const (
	CompositeOpOver     = 0 // Standard alpha compositing
	CompositeOpMaskOnly = 1 // Use mask without background
	CompositeOpReplace  = 2 // Replace background completely
)

Compositing operations

View Source
const (
	MaxBoxNestingDepth = 32      // Maximum superbox nesting depth
	MaxBoxCount        = 10000   // Maximum number of boxes in file
	MaxBoxSize         = 1 << 30 // Maximum box size (1 GB)
	MaxPages           = 10000   // Maximum pages in document
	MaxLayersPerPage   = 1000    // Maximum layers per page
	MaxLayoutObjects   = 100000  // Maximum layout objects in document
)

Security limits for JPM parsing

Variables

View Source
var (
	BrandJPM = [4]byte{'j', 'p', 'm', ' '} // JPM compound image brand
	BrandJP2 = [4]byte{'j', 'p', '2', ' '} // JPEG 2000 brand (for compatibility)
)

JPM file type brands

View Source
var (
	ErrEmptyData           = errors.New("jpm: empty data")
	ErrInvalidBox          = errors.New("jpm: invalid box structure")
	ErrBoxNestingTooDeep   = errors.New("jpm: box nesting exceeds maximum depth")
	ErrBoxTooLarge         = errors.New("jpm: box size exceeds maximum")
	ErrTruncatedData       = errors.New("jpm: truncated data")
	ErrInvalidSignature    = errors.New("jpm: invalid file signature")
	ErrMissingHeader       = errors.New("jpm: missing compound image header")
	ErrMissingCodestream   = errors.New("jpm: missing codestream data")
	ErrTooManyPages        = errors.New("jpm: too many pages")
	ErrTooManyLayers       = errors.New("jpm: too many layers per page")
	ErrTooManyObjects      = errors.New("jpm: too many layout objects")
	ErrInvalidPage         = errors.New("jpm: invalid page structure")
	ErrInvalidLayoutObject = errors.New("jpm: invalid layout object")
	ErrInvalidMask         = errors.New("jpm: invalid mask data")
	ErrLayerCompositeError = errors.New("jpm: layer composition error")
	ErrUnsupportedBrand    = errors.New("jpm: unsupported file brand")
)

Errors for JPM parsing

View Source
var Signature = [4]byte{0x0D, 0x0A, 0x87, 0x0A}

JP2/JPM signature bytes

Functions

func Validate

func Validate(data []byte) error

Validate checks if box size is within security limits.

Types

type Box

type Box struct {
	Type     uint32 // Box type (4-byte identifier)
	Length   uint64 // Box length including header
	Data     []byte // Box content
	Children []*Box // For superboxes
	Offset   int64  // Byte offset in file
}

Box represents a generic JPM box structure.

func (*Box) TypeString

func (b *Box) TypeString() string

TypeString returns the box type as a 4-character string.

type Composer

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

Composer handles layer composition for compound images.

func NewComposer

func NewComposer() *Composer

NewComposer creates a new layer composer.

func (*Composer) CompositePage

func (c *Composer) CompositePage(page *Page) (*CompositionResult, error)

CompositePage composes all layers of a page into a single image.

func (*Composer) SetBackgroundColor

func (c *Composer) SetBackgroundColor(r, g, b, a uint8)

SetBackgroundColor sets the background color for composition.

type CompositionResult

type CompositionResult struct {
	Width      int     // Result width in pixels
	Height     int     // Result height in pixels
	Pixels     []uint8 // Composed pixel data (RGBA format)
	Components int     // Number of components (typically 4 for RGBA)
}

CompositionResult holds the result of layer composition.

type CompoundHeader

type CompoundHeader struct {
	Width          uint32 // Overall document width
	Height         uint32 // Overall document height
	NumPages       uint32 // Number of pages
	DefaultColorID uint8  // Default color space identifier
	DefaultBPC     uint8  // Default bits per component
}

CompoundHeader contains the compound image header (jpmh) data.

type JPMFile

type JPMFile struct {
	Brand        [4]byte         // File brand (jpm or jp2)
	MinorVersion uint32          // Minor version
	Compat       [][4]byte       // Compatibility list
	Header       *CompoundHeader // Compound image header
	Pages        []*Page         // Pages in document
	Codestreams  [][]byte        // Embedded codestreams
}

JPMFile represents a parsed JPM compound image file.

func (*JPMFile) GetLayoutObject

func (f *JPMFile) GetLayoutObject(pageIdx int, objectID uint32) (*LayoutObject, error)

GetLayoutObject returns a layout object by ID from a specific page.

func (*JPMFile) GetPageInfo

func (f *JPMFile) GetPageInfo(pageIdx int) (*PageInfo, error)

GetPageInfo returns information about a specific page.

type LayoutObject

type LayoutObject struct {
	ID            uint32 // Object identifier
	Type          uint8  // Object type (raster, mask, text, graphic)
	OffsetX       int32  // X position on page
	OffsetY       int32  // Y position on page
	Width         uint32 // Object width
	Height        uint32 // Object height
	Opacity       uint8  // Object opacity (0-255)
	CompositeOp   uint8  // Compositing operation
	CodestreamIdx int    // Index to codestream data (-1 if none)
	MaskIdx       int    // Index to mask object (-1 if none)
	ZOrder        int    // Z-order for compositing (higher = on top)
	Data          []byte // Raw object data (if inline)
}

LayoutObject represents a layout object within a page. Layout objects can be raster images, masks, text, or graphics.

type Mask

type Mask struct {
	ID       uint32 // Mask identifier
	Type     uint8  // Mask type (hard or soft)
	Width    uint32 // Mask width
	Height   uint32 // Mask height
	Data     []byte // Mask bitmap data
	Inverted bool   // If true, mask is inverted
}

Mask represents a mask layer for compound images.

type Page

type Page struct {
	Index           int             // Page index (0-based)
	Width           uint32          // Page width in pixels
	Height          uint32          // Page height in pixels
	LayoutObjects   []*LayoutObject // Layout objects on this page
	BackgroundColor []uint8         // Background color (RGB or RGBA)
}

Page represents a single page in the compound document.

type PageInfo

type PageInfo struct {
	Index       int
	Width       uint32
	Height      uint32
	ObjectCount int
	HasMasks    bool
	HasRasters  bool
}

PageInfo contains summarized information about a page.

type Reader

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

Reader reads and parses JPM compound image files.

func NewReader

func NewReader(data []byte) *Reader

NewReader creates a new JPM reader from data.

func (*Reader) ExtractCodestream

func (r *Reader) ExtractCodestream() ([]byte, error)

ExtractCodestream extracts the first codestream from the JPM file.

func (*Reader) GetJPMFile

func (r *Reader) GetJPMFile() *JPMFile

GetJPMFile returns the parsed JPM file structure.

func (*Reader) GetPage

func (r *Reader) GetPage(index int) (*Page, error)

GetPage returns a specific page from the parsed JPM file.

func (*Reader) Parse

func (r *Reader) Parse() error

Parse parses the JPM file.

func (*Reader) Verify

func (r *Reader) Verify() error

Verify verifies that data is a valid JPM file.

Jump to

Keyboard shortcuts

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