imgconv

package
v0.0.0-...-277146c Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2026 License: BSD-2-Clause Imports: 13 Imported by: 0

README

Abstract definitions for printer and scanner interfaces

import "github.com/OpenPrinting/go-mfp/imgconv"

This package implements image conversions.

Documentation

Index

Constants

View Source
const (
	MIMETypeBMP  = "image/bmp"
	MIMETypeJPEG = "image/jpeg"
	MIMETypePDF  = "application/pdf"
	MIMETypePNG  = "image/png"
	MIMETypeTIFF = "image/tiff"
	MIMETypeData = "application/octet-stream"
)

MIME types for known document formats:

Variables

This section is empty.

Functions

func MIMETypeDetect

func MIMETypeDetect(image []byte) string

MIMETypeDetect detects image type by its few starting bytes and returns its MIME type.

If format cannot be guessed, "" is returned.

Types

type Decoder

type Decoder interface {
	Reader

	// MIMEType returns the MIME type of the image being decoded.
	MIMEType() string
}

Decoder is the Reader with few additional methods, intended for decoding image formats, such as JPEG, PNG, ...

func NewJPEGReader

func NewJPEGReader(input io.Reader) (r Decoder, err error)

NewJPEGReader creates a new Reader for JPEG images.

func NewPNGReader

func NewPNGReader(input io.Reader) (Decoder, error)

NewPNGReader creates a new Decoder for PNG images

type Encoder

type Encoder interface {
	Writer

	// MIMEType returns the MIME type of the image being decoded.
	MIMEType() string
}

Encoder is the Writer with few additional methods, intended for encoding image formats, such as JPEG, PNG, ...

func NewJPEGWriter

func NewJPEGWriter(output io.Writer,
	wid, hei int, model color.Model, quality int) (w Encoder, err error)

NewJPEGWriter creates a new Writer for JPEG images. Supported color models are following:

  • color.GrayModel
  • color.RGBAModel

The following color models are also supported, but resulting image depth is 8 bit (this is the JPEG format limitation):

  • color.Gray16Model
  • color.RGBA64Model

The quality is the [0...100] integer that defines the tradeoff between level of compression and image quality. 0 is the best compression, lowest quality, 100 is the best quality, lowest compression.

func NewPNGWriter

func NewPNGWriter(output io.Writer,
	wid, hei int, model color.Model) (Encoder, error)

NewPNGWriter creates a new Writer for PNG images. Supported color models are following:

  • color.GrayModel
  • color.Gray16Model
  • color.RGBAModel
  • color.RGBA64Model

type Reader

type Reader interface {
	// ColorModel returns the [color.Model] of image being decoded.
	ColorModel() color.Model

	// Size returns the image size.
	Size() (wid, hei int)

	// NewRow allocates a [Row] of the appropriate type and width for
	// use with the [Reader.Read] function.
	NewRow() Row

	// Read returns the next image [Row].
	// It returns the resulting row length, in pixels, or an error.
	Read(Row) (int, error)

	// Close closes the reader.
	Close()
}

Reader implements streaming image reader. It allows to read image line by line and doesn't use the whole image internal buffer, so even the large images can be processed using very small amount of memory.

There are two kinds of readers:

  • image format reader (for example, PNG reader). They work on a top of io.Reader.
  • image filter (for example, image resizer). They work on a top of existent Reader and also implement the Reader interface, which allows filters to be stacked.

[Reader.Read] may block or fail, if its source io.Reader blocks of fails, and this condition propagates over the entire reader stack.

Once Reader.Read fails, all subsequent calls to Reader.Read will return the same error.

Attempt to Read after the last line returns io.EOF. However, if underlying io.Reader unexpectedly returns io.EOF (i.e., if image is truncated), Reader.Read will return some other error code (typically, io.ErrUnexpectedEOF).

Reader may own some resources associated with it, which will not be automatically garbage-collected. So Reader needs to be explicitly closed after use (with the Reader.Close call).

Closing the input format reader doesn't close the underlying io.Reader. However, closing the image filter closes its underlying input Reader. So the entire filter chain can be closed by closing the top-level filter, but the bottom io.Reader still needs to be closed explicitly.

Reader doesn't guarantee that its input stream (io.Reader or underlying Reader) will be consumed till the end or even that at least some data will be consumed from it.

func NewColorModelFilter

func NewColorModelFilter(in Reader, model color.Model) Reader

NewColorModelFilter creates a new image filter on a top of the existent Reader.

This filter converts image's color.Model.

func NewDetectReader

func NewDetectReader(input io.Reader) (r Reader, err error)

NewDetectReader automatically detects image format and returns the appropriate reader.

func NewGrayScale

func NewGrayScale(in Reader) Reader

NewGrayScale creates a new image filter on a top of the existent Reader.

This filter converts color images into the Grayscale without changing the image color.Model. For example, the full-color RGB24 image will be converted into the RGB24 with all color channels set to the same value, effectively making it Grayscale.

func NewResizer

func NewResizer(in Reader, rect image.Rectangle) Reader

NewResizer creates a new image resize filter on a top of the existent Reader.

Resizer works by either clipping or expanding image to fit the specified region.

Resizer implements the Reader interface, which allows to build a chain of image filters.

When resizer is closed, its input Reader is also closed.

func NewScaler

func NewScaler(in Reader, wid, hei int) Reader

NewScaler creates a new image resize filter on a top of the existent Reader.

This filter scales the input image into the new dimensions, defined by the wid and hei parameters.

type Row

type Row interface {
	// Width returns the row width, in pixels.
	Width() int

	// At returns the pixel at the specified position.
	At(x int) color.Color

	// Set sets the pixel at the specified position.
	Set(x int, c color.Color)

	// Slice returns a [low:high] sub-slice of the original Row.
	Slice(low, high int) Row

	// Fill fills Row with the pixels of the specified color
	Fill(c color.Color)

	// Copy copies content of the r2 into the receiver Row.
	// If they have a different color model, pixels are converted.
	// Rows may be of the different size and may overlap.
	//
	// It returns number of pixels copied.
	Copy(r2 Row) int
}

Row represents a single row of image

func NewRow

func NewRow(model color.Model, width int) (row Row)

NewRow returns the new Row of the specified width and color.Model. The following color models are supported:

  • color.GrayModel
  • color.Gray16Model
  • color.RGBAModel
  • color.RGBA64Model

For unknown (unsupported) model nil is returned.

type RowEmpty

type RowEmpty struct{}

RowEmpty represents an empty row

func (RowEmpty) At

func (r RowEmpty) At(x int) color.Color

At returns the pixel at the specified position as color.Color.

func (RowEmpty) Copy

func (r RowEmpty) Copy(r2 Row) int

Copy copies content of the r2 into the receiver Row.

func (RowEmpty) Fill

func (r RowEmpty) Fill(c color.Color)

Fill fills Row with the pixels of the specified color

func (RowEmpty) Set

func (r RowEmpty) Set(x int, c color.Color)

Set sets the pixel at the specified position.

func (RowEmpty) Slice

func (r RowEmpty) Slice(low, high int) Row

Slice returns a [low:high] sub-slice of the original Row.

func (RowEmpty) Width

func (r RowEmpty) Width() int

Width returns the row width, in pixels.

type RowFP

type RowFP interface {
	Row

	// ZeroFill fills the row with all-zero values
	ZeroFill()

	// MultiplyAccumulate performs the multiply-accumulate operation on
	// the entire row:
	//
	//	r += r2 * w
	//
	// Rows must be of the same type and size.
	MultiplyAccumulate(r2 Row, w float32)
	// contains filtered or unexported methods
}

RowFP extends the Row interface with the operations, required for image processing.

func NewRowFP

func NewRowFP(model color.Model, width int) (row RowFP)

NewRowFP returns the new Row with the RowGrayFP32 or RowRGBAFP32, compatible with the color.Model (grayscale or RGBA).

The following color models are supported:

  • color.GrayModel
  • color.Gray16Model
  • color.RGBAModel
  • color.RGBA64Model

For unknown (unsupported) model nil is returned.

type RowGray16

type RowGray16 []color.Gray16

RowGray16 represents a row of 16-bit grayscale image.

func (RowGray16) At

func (r RowGray16) At(x int) color.Color

At returns pixel at the specified position as color.Color.

func (RowGray16) Copy

func (r RowGray16) Copy(r2 Row) int

Copy copies content of the r2 into the receiver Row.

func (RowGray16) Fill

func (r RowGray16) Fill(c color.Color)

Fill fills Row with the pixels of the specified color

func (RowGray16) Gray16At

func (r RowGray16) Gray16At(x int) color.Gray16

Gray16At returns pixel at the specified position as color.Gray16.

func (RowGray16) Set

func (r RowGray16) Set(x int, c color.Color)

Set sets the pixel at the specified position.

func (RowGray16) Slice

func (r RowGray16) Slice(low, high int) Row

Slice returns a [low:high] sub-slice of the original Row.

func (RowGray16) Width

func (r RowGray16) Width() int

Width returns the row width, in pixels.

type RowGray8

type RowGray8 []color.Gray

RowGray8 represents a row of 8-bit grayscale image.

func (RowGray8) At

func (r RowGray8) At(x int) color.Color

At returns pixel at the specified position as color.Color.

func (RowGray8) Copy

func (r RowGray8) Copy(r2 Row) int

Copy copies content of the r2 into the receiver Row.

func (RowGray8) Fill

func (r RowGray8) Fill(c color.Color)

Fill fills Row with the pixels of the specified color

func (RowGray8) GrayAt

func (r RowGray8) GrayAt(x int) color.Gray

GrayAt returns pixel at the specified position as color.Gray.

func (RowGray8) Set

func (r RowGray8) Set(x int, c color.Color)

Set sets the pixel at the specified position.

func (RowGray8) Slice

func (r RowGray8) Slice(low, high int) Row

Slice returns a [low:high] sub-slice of the original Row.

func (RowGray8) Width

func (r RowGray8) Width() int

Width returns the row width, in pixels.

type RowGrayFP32

type RowGrayFP32 []float32

RowGrayFP32 represents a row of the grayscale image as a sequence of the 32-bit floating point numbers in range [0...1.0].

func (RowGrayFP32) At

func (r RowGrayFP32) At(x int) color.Color

At returns pixel at the specified position as color.Color.

func (RowGrayFP32) Copy

func (r RowGrayFP32) Copy(r2 Row) int

Copy copies content of the r2 into the receiver Row.

func (RowGrayFP32) Fill

func (r RowGrayFP32) Fill(c color.Color)

Fill fills Row with the pixels of the specified color

func (RowGrayFP32) Gray16At

func (r RowGrayFP32) Gray16At(x int) color.Gray16

Gray16At returns pixel at the specified position as color.Gray16.

func (RowGrayFP32) GrayAt

func (r RowGrayFP32) GrayAt(x int) color.Gray

GrayAt returns pixel at the specified position as color.Gray.

func (RowGrayFP32) MultiplyAccumulate

func (r RowGrayFP32) MultiplyAccumulate(r2 Row, w float32)

MultiplyAccumulate performs the multiply-accumulate operation on the entire row:

r += r2 * w

Rows must be of the same type and size.

func (RowGrayFP32) Set

func (r RowGrayFP32) Set(x int, c color.Color)

Set sets the pixel at the specified position.

func (RowGrayFP32) Slice

func (r RowGrayFP32) Slice(low, high int) Row

Slice returns a [low:high] sub-slice of the original Row.

func (RowGrayFP32) Width

func (r RowGrayFP32) Width() int

Width returns the row width, in pixels.

func (RowGrayFP32) ZeroFill

func (r RowGrayFP32) ZeroFill()

ZeroFill fills the row with all-zero values

type RowRGBA32

type RowRGBA32 []color.RGBA

RowRGBA32 represents a row of 8-bit RGBA image.

func (RowRGBA32) At

func (r RowRGBA32) At(x int) color.Color

At returns pixel at the specified position as color.Color.

func (RowRGBA32) Copy

func (r RowRGBA32) Copy(r2 Row) int

Copy copies content of the r2 into the receiver Row.

func (RowRGBA32) Fill

func (r RowRGBA32) Fill(c color.Color)

Fill fills Row with the pixels of the specified color

func (RowRGBA32) RGBAAt

func (r RowRGBA32) RGBAAt(x int) color.RGBA

RGBAAt returns pixel at the specified position as color.RGBA.

func (RowRGBA32) Set

func (r RowRGBA32) Set(x int, c color.Color)

Set sets the pixel at the specified position.

func (RowRGBA32) Slice

func (r RowRGBA32) Slice(low, high int) Row

Slice returns a [low:high] sub-slice of the original Row.

func (RowRGBA32) Width

func (r RowRGBA32) Width() int

Width returns the row width, in pixels.

type RowRGBA64

type RowRGBA64 []color.RGBA64

RowRGBA64 represents a row of 16-bit RGBA image.

func (RowRGBA64) At

func (r RowRGBA64) At(x int) color.Color

At returns pixel at the specified position as color.Color.

func (RowRGBA64) Copy

func (r RowRGBA64) Copy(r2 Row) int

Copy copies content of the r2 into the receiver Row.

func (RowRGBA64) Fill

func (r RowRGBA64) Fill(c color.Color)

Fill fills Row with the pixels of the specified color

func (RowRGBA64) RGBA64At

func (r RowRGBA64) RGBA64At(x int) color.RGBA64

RGBA64At returns pixel at the specified position as color.RGBA64.

func (RowRGBA64) Set

func (r RowRGBA64) Set(x int, c color.Color)

Set sets the pixel at the specified position.

func (RowRGBA64) Slice

func (r RowRGBA64) Slice(low, high int) Row

Slice returns a [low:high] sub-slice of the original Row.

func (RowRGBA64) Width

func (r RowRGBA64) Width() int

Width returns the row width, in pixels.

type RowRGBAFP32

type RowRGBAFP32 []float32

RowRGBAFP32 represents a row of the RGBA image as a sequence of the 32-bit floating point numbers in range [0...1.0]. The sequence is ordered as follows: R-G-B-A-R-G-B-A-...

func (RowRGBAFP32) At

func (r RowRGBAFP32) At(x int) color.Color

At returns pixel at the specified position as color.Color.

func (RowRGBAFP32) Copy

func (r RowRGBAFP32) Copy(r2 Row) int

Copy copies content of the r2 into the receiver Row.

func (RowRGBAFP32) Fill

func (r RowRGBAFP32) Fill(c color.Color)

Fill fills Row with the pixels of the specified color

func (RowRGBAFP32) MultiplyAccumulate

func (r RowRGBAFP32) MultiplyAccumulate(r2 Row, w float32)

MultiplyAccumulate performs the multiply-accumulate operation on the entire row:

r += r2 * w

Rows must be of the same type and size.

func (RowRGBAFP32) RGBA64At

func (r RowRGBAFP32) RGBA64At(x int) color.RGBA64

RGBA64At returns pixel at the specified position as color.RGBA64.

func (RowRGBAFP32) RGBAAt

func (r RowRGBAFP32) RGBAAt(x int) color.RGBA

RGBAAt returns pixel at the specified position as color.RGBA.

func (RowRGBAFP32) Set

func (r RowRGBAFP32) Set(x int, c color.Color)

Set sets the pixel at the specified position.

func (RowRGBAFP32) Slice

func (r RowRGBAFP32) Slice(low, high int) Row

Slice returns a [low:high] sub-slice of the original Row.

func (RowRGBAFP32) Width

func (r RowRGBAFP32) Width() int

Width returns the row width, in pixels.

func (RowRGBAFP32) ZeroFill

func (r RowRGBAFP32) ZeroFill()

ZeroFill fills the row with all-zero values

type Writer

type Writer interface {
	// ColorModel returns the [color.Model] of image being written.
	ColorModel() color.Model

	// Size returns the image size.
	Size() (wid, hei int)

	// Write writes the next image [Row].
	Write(Row) error

	// Close flushes the buffered data and then closes the Writer
	Close() error
}

Writer implements streaming image writer.

It writes image row by row into the supplied io.Writer.

Writer may own some resources associated with it, which will not be automatically garbage-collected. So Writer needs to be explicitly closed after use (with the Writer.Close call).

Jump to

Keyboard shortcuts

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