encoding

package
v0.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package encoding implements PDF stream encoding and decoding filters.

Package encoding implements PDF stream encoding and decoding filters.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DCTDecoder

type DCTDecoder struct {
	// ColorTransform specifies color transformation:
	// 0 = no transform
	// 1 = YCbCr to RGB (default for RGB images)
	ColorTransform int
}

DCTDecoder implements DCTDecode (JPEG) stream decompression.

DCTDecode is used for JPEG-compressed image data in PDF files. The decoder converts JPEG data to raw RGB/Gray pixel data.

Reference: PDF 1.7 specification, Section 7.4.8 (DCTDecode Filter).

func NewDCTDecoder

func NewDCTDecoder() *DCTDecoder

NewDCTDecoder creates a new DCT (JPEG) decoder.

func NewDCTDecoderWithParams

func NewDCTDecoderWithParams(colorTransform int) *DCTDecoder

NewDCTDecoderWithParams creates a DCT decoder with specific parameters.

func (*DCTDecoder) Decode

func (d *DCTDecoder) Decode(data []byte) ([]byte, error)

Decode decompresses JPEG-encoded data to raw pixels.

Returns raw RGB or grayscale pixel data. For RGB images: 3 bytes per pixel (R, G, B). For grayscale images: 1 byte per pixel.

Parameters:

  • data: JPEG-compressed image data

Returns: Raw pixel data bytes, or error if decoding fails.

func (*DCTDecoder) DecodeToImage

func (d *DCTDecoder) DecodeToImage(data []byte) (image.Image, error)

DecodeToImage decodes JPEG data to a Go image.Image.

This is useful when you need the image in Go's standard format for further processing or saving in a different format.

func (*DCTDecoder) DecodeWithMetadata

func (d *DCTDecoder) DecodeWithMetadata(data []byte) (*DCTResult, error)

DecodeWithMetadata decodes JPEG data and returns both pixel data and metadata.

This is useful when you need to know the image dimensions and color space.

func (*DCTDecoder) Encode

func (d *DCTDecoder) Encode(data []byte, width, height, quality int) ([]byte, error)

Encode compresses raw pixel data to JPEG format.

This enables creating JPEG streams for PDF writing.

Parameters:

  • data: Raw RGB pixel data (3 bytes per pixel)
  • width: Image width in pixels
  • height: Image height in pixels
  • quality: JPEG quality (1-100, 0 for default 75)

Returns: JPEG-compressed data, or error if encoding fails.

func (*DCTDecoder) EncodeGray

func (d *DCTDecoder) EncodeGray(data []byte, width, height, quality int) ([]byte, error)

EncodeGray compresses grayscale pixel data to JPEG format.

Parameters:

  • data: Raw grayscale pixel data (1 byte per pixel)
  • width: Image width in pixels
  • height: Image height in pixels
  • quality: JPEG quality (1-100, 0 for default 75)

Returns: JPEG-compressed data, or error if encoding fails.

type DCTResult

type DCTResult struct {
	// Data contains raw pixel data in RGB (3 bytes per pixel) or Gray (1 byte per pixel).
	Data []byte

	// Width is the image width in pixels.
	Width int

	// Height is the image height in pixels.
	Height int

	// Components is the number of color components (1 for grayscale, 3 for RGB).
	Components int

	// BitsPerComponent is always 8 for JPEG.
	BitsPerComponent int
}

DCTResult contains decoded image data and metadata.

type FlateDecoder

type FlateDecoder struct{}

FlateDecoder implements FlateDecode (zlib/deflate) stream decompression.

FlateDecode is the most common compression filter in PDF files, using the zlib/deflate algorithm (RFC 1950/1951).

Reference: PDF 1.7 specification, Section 7.4.4 (FlateDecode Filter).

func NewFlateDecoder

func NewFlateDecoder() *FlateDecoder

NewFlateDecoder creates a new Flate decoder.

func (*FlateDecoder) Decode

func (d *FlateDecoder) Decode(data []byte) (result []byte, err error)

Decode decompresses Flate-encoded data.

This is a straightforward zlib decompression without predictor support. Predictors (like PNG filters) are typically not used for xref streams.

Parameters:

  • data: Compressed data bytes

Returns: Decompressed data bytes, or error if decompression fails.

func (*FlateDecoder) Encode

func (d *FlateDecoder) Encode(data []byte) ([]byte, error)

Encode compresses data using Flate encoding.

This is for future PDF writing support.

Jump to

Keyboard shortcuts

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