resize

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2015 License: MIT Imports: 6 Imported by: 1

README

resize

Image resizing for the Go programming language with common interpolation methods.

Installation

$ go get github.com/coraldane/resize

It's that easy!

Usage

Import package with

import "github.com/coraldane/resize"

The resize package provides 3 functions:

  • resize.Resize creates a scaled image with new dimensions (width, height) using the interpolation function interp. If either width or height is set to 0, it will be set to an aspect ratio preserving value.
  • resize.Thumbnail downscales an image preserving its aspect ratio to the maximum dimensions (maxWidth, maxHeight). It will return the original image if original sizes are smaller than the provided dimensions.
resize.Resize(width, height uint, img image.Image, interp resize.InterpolationFunction) image.Image
resize.Thumbnail(maxWidth, maxHeight uint, img image.Image, interp resize.InterpolationFunction) image.Image
resize.GaussianSmooth(srcImg image.Image, sigma float64, radius int) image.Image

The provided interpolation functions are (from fast to slow execution time)

Which of these methods gives the best results depends on your use case.

Sample usage:

package main

import (
	"github.com/coraldane/resize"
	"image/jpeg"
	"log"
	"os"
)

func main() {
	// open "test.jpg"
	file, err := os.Open("test.jpg")
	if err != nil {
		log.Fatal(err)
	}

	// decode jpeg into image.Image
	img, err := jpeg.Decode(file)
	if err != nil {
		log.Fatal(err)
	}
	file.Close()

	// resize to width 1000 using cubic resampling
	// and preserve aspect ratio
	m := resize.Resize(1000, 0, img, resize.Bicubic)

	out, err := os.Create("test_resized.jpg")
	if err != nil {
		log.Fatal(err)
	}
	defer out.Close()

	// write new image to file
	jpeg.Encode(out, m, nil)
}

Caveats

  • Optimized access routines are used for image.RGBA, image.RGBA64, image.YCbCr, image.Gray, and image.Gray16 types. All other image types are accessed in a generic way that will result in slow processing speed.
  • JPEG images are stored in image.YCbCr. This image format stores data in a way that will decrease processing speed. A resize may be up to 2 times slower than with image.RGBA.

Downsizing Samples

Downsizing is not as simple as it might look like. Images have to be filtered before they are scaled down, otherwise aliasing might occur. Filtering is highly subjective: Applying too much will blur the whole image, too little will make aliasing become apparent. Resize tries to provide sane defaults that should suffice in most cases.

License

Copyright (c) 2014 coraldane coraldane@gmail.com Resize is released under a MIT style license.

Documentation

Overview

Package resize implements various image resizing methods.

The package works with the Image interface described in the image package. Various interpolation methods are provided and multiple processors may be utilized in the computations.

Example:

imgResized := resize.Resize(1000, 0, imgOld, resize.MitchellNetravali)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Gaussian

func Gaussian(sigma float64, in int) float64

func GaussianSmooth

func GaussianSmooth(srcImg image.Image, sigma float64, radius int) image.Image

func Resize

func Resize(width, height int, img image.Image, interp InterpolationFunction) image.Image

Resize scales an image to new width and height using the interpolation function interp. A new image with the given dimensions will be returned. If one of the parameters width or height is set to 0, its size will be calculated so that the aspect ratio is that of the originating image. The resizing algorithm uses channels for parallel computation.

func Thumbnail

func Thumbnail(maxWidth, maxHeight int, img image.Image, interp InterpolationFunction) image.Image

Thumbnail will downscale provided image to max width and height preserving original aspect ratio and using the interpolation function interp. It will return original image, without processing it, if original sizes are already smaller than provided constraints.

Types

type InterpolationFunction

type InterpolationFunction int

An InterpolationFunction provides the parameters that describe an interpolation kernel. It returns the number of samples to take and the kernel function to use for sampling.

const (
	// Nearest-neighbor interpolation
	NearestNeighbor InterpolationFunction = iota
	// Bilinear interpolation
	Bilinear
	// Bicubic interpolation (with cubic hermite spline)
	Bicubic
	// Mitchell-Netravali interpolation
	MitchellNetravali
	// Lanczos interpolation (a=2)
	Lanczos2
	// Lanczos interpolation (a=3)
	Lanczos3
)

InterpolationFunction constants

type Ycc

type Ycc struct {
	// Pix holds the image's pixels, in Y, Cb, Cr order. The pixel at
	// (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*3].
	Pix []uint8
	// Stride is the Pix stride (in bytes) between vertically adjacent pixels.
	Stride int
	// Rect is the image's bounds.
	Rect image.Rectangle
	// SubsampleRatio is the subsample ratio of the original YCbCr image.
	SubsampleRatio image.YCbCrSubsampleRatio
}

Ycc is an in memory YCbCr image. The Y, Cb and Cr samples are held in a single slice to increase resizing performance.

func ImageYCbCrToYcc

func ImageYCbCrToYcc(in *image.YCbCr) *Ycc

ImageYCbCrToYcc converts a YCbCr image to a Ycc image for resizing.

func (*Ycc) At

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

func (*Ycc) Bounds

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

func (*Ycc) ColorModel

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

func (*Ycc) Opaque

func (p *Ycc) Opaque() bool

func (*Ycc) PixOffset

func (p *Ycc) PixOffset(x, y int) int

PixOffset returns the index of the first element of Pix that corresponds to the pixel at (x, y).

func (*Ycc) SubImage

func (p *Ycc) SubImage(r image.Rectangle) image.Image

SubImage returns an image representing the portion of the image p visible through r. The returned value shares pixels with the original image.

func (*Ycc) YCbCr

func (p *Ycc) YCbCr() *image.YCbCr

YCbCr converts Ycc to a YCbCr image with the same subsample ratio as the YCbCr image that Ycc was generated from.

Jump to

Keyboard shortcuts

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