imagecoding

package module
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2024 License: MIT Imports: 22 Imported by: 0

README

Go Image Coding

Go Report Card go-doc

Go (bindings for) image en-/de-coding using libraries found on debian and other common systems. Go comes with image processing built-in, written in go, but often they can't quite match the performance of established image processing libraries such as jpeg-turbo etc.

While general purpose, this project is opinionated and tailor to our needs.

Install (debian)

See Dockerfile

Install (OS X)
brew install jpeg-turbo libpng libheif webp pkg-config
HEIF/HEIC

This package optionally supports heif, to include heif; add -tags heif to your gobuild. It's enabled by default on darwin (macOS).

Documentation

Overview

Package imagecoding provides go bindings for en-/de-coding images using image processing c libraries found in common systems

Package imagecoding provides go bindings for en-/de-coding images using image processing c libraries found in common systems

Index

Constants

View Source
const (
	A4Short = 210 / 25.4 * 150 // 210 mm / 25.4 mm/inch * 150 ppi ≈ 1204 pixels
	A4Long  = 297 / 25.4 * 150 // 297 mm / 25.4 mm/inch * 150 ppi ≈ 1754 pixels
)

Variables

View Source
var ErrEmptyInput = errors.New("empty input data")
View Source
var RGBModel = color.ModelFunc(rgbModel)

RGBModel is RGB color model instance

Functions

func ConfigHeif

func ConfigHeif(data []byte) (image.Config, string, error)

func ConfigJpeg

func ConfigJpeg(data []byte) (image.Config, string, error)

func DecodeConfig

func DecodeConfig(content []byte) (image.Config, string, error)

DecodeConfig is like image.DecodeConfig but supports additional formats For JPEGs it uses jpeg-turbos internal function for compatibility

func DefaultScale

func DefaultScale(pageWidth, pageHeight int) (imgWidth, imgHeight int, scaleFactor float64)

Calculcate at what scale to use for OCR optimized pages We prefer maximum what would be the equivalent for a A4 page at 150 ppi

func EncodeJpeg

func EncodeJpeg(buf *bytes.Buffer, img image.Image, quality int) ([]byte, error)

func EncodePng

func EncodePng(buf *bytes.Buffer, img image.Image) ([]byte, error)

EncodePng will encode an image.Gray to PNG bytes, using libpng's simplified API for performance

func EncodeWebP

func EncodeWebP(buf *bytes.Buffer, img image.Image) ([]byte, error)

func FixOrientation

func FixOrientation(img image.Image, orient Orientation) image.Image

FixOrientation uses the imaging library to correct for orientation

func ReOrientJpeg

func ReOrientJpeg(file []byte, orient Orientation) ([]byte, error)

ReOrientJpeg will transform a JPEG into a top left (normal) orientation It returns a buffer with JPEG encoding

func Transform

func Transform(data []byte, grayscale bool, scale ScaleFunc) (out image.Image, width, height int, scaleFactor float64, err error)

Transform scales, colormaps and orients an image according to input param

func TransformHeif

func TransformHeif(data []byte, grayscale bool, scale ScaleFunc) (out image.Image, width, height int, scaleFactor float64, err error)

func TransformJpeg

func TransformJpeg(data []byte, grayscale bool, scale ScaleFunc) (out image.Image, width, height int, scaleFactor float64, err error)

TransformJpeg will scale and colormap an input JPEG file to an image.Gray or RGBImage This will use libjpeg-turbo to do it as efficiently as possible, utilizing DCT factors for fast scaling

Types

type ImgFormat

type ImgFormat string
const (
	Bmp  ImgFormat = "bmp"
	Gif  ImgFormat = "gif"
	Png  ImgFormat = "png"
	Jpeg ImgFormat = "jpg"
	Tiff ImgFormat = "tif"
	Webp ImgFormat = "webp"
	Heif ImgFormat = "heif"
)

type Orientation

type Orientation uint8

Enum representation for Exif Orientation

const (
	TopLeft     Orientation = 1
	TopRight    Orientation = 2
	BottomRight Orientation = 3
	BottomLeft  Orientation = 4
	LeftTop     Orientation = 5
	RightTop    Orientation = 6
	RightBottom Orientation = 7
	LeftBottom  Orientation = 8
)

func GetOrientation

func GetOrientation(reader io.Reader) Orientation

GetOrientation returns the image orientation from EXIF data https://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/

TopLeft 1: 0 degrees – the correct orientation, no adjustment is required. TopRight 2: 0 degrees, mirrored – image has been flipped back-to-front. BottomRight 3: 180 degrees – image is upside down. BottomLeft 4: 180 degrees, mirrored – image is upside down and flipped back-to-front. LeftTop 5: 90 degrees – image is on its side. RightTop 6: 90 degrees, mirrored – image is on its side and flipped back-to-front. RightBottom 7: 270 degrees – image is on its far side. LeftBottom 8: 270 degrees, mirrored – image is on its far side and flipped back-to-front.

type RGB

type RGB struct {
	R, G, B uint8
}

RGB color

func (RGB) RGBA added in v0.0.14

func (c RGB) RGBA() (r, g, b, a uint32)

RGBA implements Color.RGBA

type RGBImage

type RGBImage struct {
	// Pix holds the image's stream, in R, G, B order.
	Pix []uint8
	// Stride is the Pix stride (in bytes) between vertically adjacent pixels.
	Stride int
	// Rect is the image's bounds.
	Rect image.Rectangle
}

RGBImage represent image data which has RGB colors. RGBImage is compatible with image.RGBA, but does not have alpha channel to reduce using memory.

func NewRGBImage added in v0.0.14

func NewRGBImage(r image.Rectangle) *RGBImage

NewRGBImage allocates and returns RGB image

func (*RGBImage) At added in v0.0.14

func (p *RGBImage) At(x, y int) color.Color

At implements image.Image.At

func (*RGBImage) Bounds added in v0.0.14

func (p *RGBImage) Bounds() image.Rectangle

Bounds implements image.Image.At

func (*RGBImage) ColorModel added in v0.0.14

func (p *RGBImage) ColorModel() color.Model

ColorModel returns RGB color model.

func (*RGBImage) RGBAAt added in v0.0.14

func (p *RGBImage) RGBAAt(x, y int) color.RGBA

RGBAAt returns the color of the pixel at (x, y) as RGBA.

type ScaleFunc

type ScaleFunc func(pageWidth, pageHeight int) (imgWidth, imgHeight int, scaleFactor float64)

type TurboJpegOperation

type TurboJpegOperation C.int

Jump to

Keyboard shortcuts

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