processor

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package processor provides image processing interfaces and types.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrPreserveMetadataNotSupported is returned when PreserveMetadata is set to true.
	// Metadata preservation is not yet implemented.
	ErrPreserveMetadataNotSupported = errors.New("preserve metadata is not yet supported")

	// ErrFileTooLarge is returned when the input file exceeds the MaxFileSize limit.
	ErrFileTooLarge = errors.New("file size exceeds maximum allowed size")
)

Errors returned by processor operations.

Functions

This section is empty.

Types

type BatchConvertItem

type BatchConvertItem struct {
	// InputPath is the path to the input image file.
	InputPath string

	// OutputPath is the path to the output image file.
	OutputPath string

	// Options contains the conversion options for this item.
	Options ConvertOptions
}

BatchConvertItem represents a single item in batch format conversion.

func ScanDirectoryForConvert

func ScanDirectoryForConvert(inputDir, outputDir string, targetFormat ImageFormat, opts ...ScanDirectoryForConvertOption) ([]BatchConvertItem, error)

ScanDirectoryForConvert scans a directory for supported image files and returns BatchConvertItems. Files that are already in the target format are skipped.

type BatchConvertResult

type BatchConvertResult struct {
	// Item is the original batch convert item.
	Item BatchConvertItem

	// Result contains the processing result, nil if error occurred.
	Result *Result

	// Error contains any error that occurred during processing.
	Error error
}

BatchConvertResult represents the result of processing a single batch convert item.

func (*BatchConvertResult) IsSuccess

func (br *BatchConvertResult) IsSuccess() bool

IsSuccess returns true if the batch convert item was processed successfully.

type BatchItem

type BatchItem struct {
	// InputPath is the path to the input image file.
	InputPath string

	// OutputPath is the path to the output image file.
	OutputPath string

	// Options contains the compression options for this item.
	Options CompressOptions
}

BatchItem represents a single item in batch processing.

func ScanDirectory

func ScanDirectory(inputDir, outputDir string, opts ...ScanDirectoryOption) ([]BatchItem, error)

ScanDirectory scans a directory for supported image files and returns BatchItems.

type BatchProcessor

type BatchProcessor interface {
	// ProcessBatch processes multiple images in batch.
	ProcessBatch(ctx context.Context, items []BatchItem) ([]BatchResult, error)
}

BatchProcessor defines the interface for batch image processing operations. This interface is designed for FRO-61 batch processing implementation.

type BatchProcessorOption

type BatchProcessorOption func(*DefaultBatchProcessor)

BatchProcessorOption is a functional option for DefaultBatchProcessor.

func WithMaxWorkers

func WithMaxWorkers(n int) BatchProcessorOption

WithMaxWorkers sets the maximum number of concurrent workers.

func WithProgressCallback

func WithProgressCallback(cb func(Progress)) BatchProcessorOption

WithProgressCallback sets a callback function that is called on progress updates.

type BatchResult

type BatchResult struct {
	// Item is the original batch item.
	Item BatchItem

	// Result contains the processing result, nil if error occurred.
	Result *Result

	// Error contains any error that occurred during processing.
	Error error
}

BatchResult represents the result of processing a single batch item.

func (*BatchResult) IsSuccess

func (br *BatchResult) IsSuccess() bool

IsSuccess returns true if the batch item was processed successfully.

type CompressOptions

type CompressOptions struct {
	// Quality specifies the JPEG quality (1-100). 0 means use default.
	// This setting is only applied to JPEG and is ignored for PNG,
	// which uses Level to control compression.
	Quality int

	// Level specifies the compression level.
	// For JPEG: used when Quality is 0 (Low=60, Medium=75, High=90).
	// For PNG: directly controls compression (BestSpeed, Default, BestCompression).
	Level CompressionLevel

	// PreserveMetadata indicates whether to preserve image metadata.
	// Not yet implemented. Setting this to true will cause Validate() to return an error.
	PreserveMetadata bool

	// MaxFileSize specifies the maximum allowed input file size in bytes.
	// 0 means no limit.
	MaxFileSize int64
}

CompressOptions contains options for image compression.

func DefaultCompressOptions

func DefaultCompressOptions() CompressOptions

DefaultCompressOptions returns the default compression options.

func (CompressOptions) Validate

func (o CompressOptions) Validate() error

Validate validates the CompressOptions and returns an error if any option is unsupported.

type CompressionLevel

type CompressionLevel int

CompressionLevel represents the compression level.

const (
	// CompressionLow provides fast compression with larger file size.
	CompressionLow CompressionLevel = iota
	// CompressionMedium provides balanced compression.
	CompressionMedium
	// CompressionHigh provides slower compression with smaller file size.
	CompressionHigh
)

func (CompressionLevel) IsValid

func (c CompressionLevel) IsValid() bool

IsValid returns true if the CompressionLevel is a valid value.

func (CompressionLevel) String

func (c CompressionLevel) String() string

String returns the string representation of the CompressionLevel.

func (CompressionLevel) ToJPEGQuality

func (c CompressionLevel) ToJPEGQuality() int

ToJPEGQuality converts CompressionLevel to JPEG quality value (1-100). Low=60, Medium=75, High=90.

func (CompressionLevel) ToPNGCompressionLevel

func (c CompressionLevel) ToPNGCompressionLevel() png.CompressionLevel

ToPNGCompressionLevel converts CompressionLevel to png.CompressionLevel.

func (CompressionLevel) ToWebPQuality

func (c CompressionLevel) ToWebPQuality() float32

ToWebPQuality converts CompressionLevel to WebP quality value (float32). Low=60, Medium=75, High=90.

type ConvertOptions

type ConvertOptions struct {
	// Format specifies the target image format.
	Format ImageFormat

	// CompressOptions contains compression settings for the output.
	CompressOptions
}

ConvertOptions contains options for image format conversion.

func DefaultConvertOptions

func DefaultConvertOptions(format ImageFormat) ConvertOptions

DefaultConvertOptions returns the default conversion options.

type DefaultBatchProcessor

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

DefaultBatchProcessor implements the BatchProcessor interface with parallel processing.

func NewDefaultBatchProcessor

func NewDefaultBatchProcessor(opts ...BatchProcessorOption) *DefaultBatchProcessor

NewDefaultBatchProcessor creates a new DefaultBatchProcessor with the given options.

func (*DefaultBatchProcessor) ProcessBatch

func (bp *DefaultBatchProcessor) ProcessBatch(ctx context.Context, items []BatchItem) ([]BatchResult, error)

ProcessBatch processes multiple images in batch with parallel workers.

func (*DefaultBatchProcessor) ProcessBatchConvert

func (bp *DefaultBatchProcessor) ProcessBatchConvert(ctx context.Context, items []BatchConvertItem) ([]BatchConvertResult, error)

ProcessBatchConvert processes multiple images in batch format conversion with parallel workers.

type ImageFormat

type ImageFormat int

ImageFormat represents supported image formats.

const (
	// FormatJPEG represents JPEG image format.
	FormatJPEG ImageFormat = iota
	// FormatPNG represents PNG image format.
	FormatPNG
	// FormatWEBP represents WebP image format.
	FormatWEBP
)

func (ImageFormat) Extension

func (f ImageFormat) Extension() string

Extension returns the file extension for the ImageFormat.

func (ImageFormat) IsValid

func (f ImageFormat) IsValid() bool

IsValid returns true if the ImageFormat is a valid value.

func (ImageFormat) MIMEType

func (f ImageFormat) MIMEType() string

MIMEType returns the MIME type for the ImageFormat.

func (ImageFormat) String

func (f ImageFormat) String() string

String returns the string representation of the ImageFormat.

type JPEGProcessor

type JPEGProcessor struct{}

JPEGProcessor implements the Processor interface for JPEG images.

func NewJPEGProcessor

func NewJPEGProcessor() *JPEGProcessor

NewJPEGProcessor creates a new JPEGProcessor.

func (*JPEGProcessor) Compress

func (p *JPEGProcessor) Compress(ctx context.Context, r io.Reader, w io.Writer, opts CompressOptions) (*Result, error)

Compress compresses a JPEG image.

func (*JPEGProcessor) Convert

func (p *JPEGProcessor) Convert(ctx context.Context, r io.Reader, w io.Writer, opts ConvertOptions) (*Result, error)

Convert converts an image to JPEG format.

func (*JPEGProcessor) SupportedFormats

func (p *JPEGProcessor) SupportedFormats() []ImageFormat

SupportedFormats returns the formats supported by this processor.

type PNGProcessor

type PNGProcessor struct{}

PNGProcessor implements the Processor interface for PNG images.

func NewPNGProcessor

func NewPNGProcessor() *PNGProcessor

NewPNGProcessor creates a new PNGProcessor.

func (*PNGProcessor) Compress

func (p *PNGProcessor) Compress(ctx context.Context, r io.Reader, w io.Writer, opts CompressOptions) (*Result, error)

Compress compresses a PNG image.

func (*PNGProcessor) Convert

func (p *PNGProcessor) Convert(ctx context.Context, r io.Reader, w io.Writer, opts ConvertOptions) (*Result, error)

Convert converts an image to PNG format.

func (*PNGProcessor) SupportedFormats

func (p *PNGProcessor) SupportedFormats() []ImageFormat

SupportedFormats returns the formats supported by this processor.

type Processor

type Processor interface {
	// Compress compresses an image from the reader and writes to the writer.
	Compress(ctx context.Context, r io.Reader, w io.Writer, opts CompressOptions) (*Result, error)

	// Convert converts an image format from the reader and writes to the writer.
	Convert(ctx context.Context, r io.Reader, w io.Writer, opts ConvertOptions) (*Result, error)

	// SupportedFormats returns the list of supported image formats.
	SupportedFormats() []ImageFormat
}

Processor defines the interface for image processing operations.

type Progress

type Progress struct {
	// Total is the total number of items to process.
	Total int
	// Completed is the number of items that have been processed successfully.
	Completed int
	// Failed is the number of items that have failed.
	Failed int
	// Current is the path of the item currently being processed.
	Current string
}

Progress represents the progress of batch processing.

type Result

type Result struct {
	// OriginalSize is the size of the original image in bytes.
	OriginalSize int64

	// CompressedSize is the size of the processed image in bytes.
	CompressedSize int64

	// Format is the format of the output image.
	Format ImageFormat
}

Result contains the result of an image processing operation.

func (*Result) CompressionRatio

func (r *Result) CompressionRatio() float64

CompressionRatio returns the compression ratio as a percentage. Returns 0 if original size is 0.

func (*Result) SavedBytes

func (r *Result) SavedBytes() int64

SavedBytes returns the number of bytes saved by compression.

func (*Result) SavedPercentage

func (r *Result) SavedPercentage() float64

SavedPercentage returns the percentage of bytes saved. Returns 0 if original size is 0.

type ScanDirectoryForConvertOption

type ScanDirectoryForConvertOption func(*scanConvertConfig)

ScanDirectoryForConvertOption is a functional option for ScanDirectoryForConvert.

func WithConvertOptions

func WithConvertOptions(opts ConvertOptions) ScanDirectoryForConvertOption

WithConvertOptions sets the conversion options for scanned items.

type ScanDirectoryOption

type ScanDirectoryOption func(*scanConfig)

ScanDirectoryOption is a functional option for ScanDirectory.

func WithCompressOptions

func WithCompressOptions(opts CompressOptions) ScanDirectoryOption

WithCompressOptions sets the compression options for scanned items.

type WEBPProcessor

type WEBPProcessor struct{}

WEBPProcessor implements the Processor interface for WebP images.

func NewWEBPProcessor

func NewWEBPProcessor() *WEBPProcessor

NewWEBPProcessor creates a new WEBPProcessor.

func (*WEBPProcessor) Compress

func (p *WEBPProcessor) Compress(ctx context.Context, r io.Reader, w io.Writer, opts CompressOptions) (*Result, error)

Compress compresses a WebP image.

func (*WEBPProcessor) Convert

func (p *WEBPProcessor) Convert(ctx context.Context, r io.Reader, w io.Writer, opts ConvertOptions) (*Result, error)

Convert converts an image to WebP format.

func (*WEBPProcessor) SupportedFormats

func (p *WEBPProcessor) SupportedFormats() []ImageFormat

SupportedFormats returns the formats supported by this processor.

Jump to

Keyboard shortcuts

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