Permalink
Browse files

Add missing godoc documentation, making golint happy. Add golint chec…

…k to travis.
  • Loading branch information...
aaron42net committed Nov 13, 2016
1 parent 4583478 commit a9b9a80baf8d50f4bd619ffe527cbaa77688c908
View
@@ -17,10 +17,11 @@ install:
script:
- rm -rf $GOPATH/{bin,pkg}/*
- go get -t -v ./... github.com/mattn/goveralls
- go get -t -v ./... github.com/mattn/goveralls github.com/golang/lint/golint
- go test -v -short -race ./...
- diff -u <(echo -n) <(gofmt -d -s .)
- go vet ./...
- golint -set_exit_status ./...
after_success:
- for dir in ./format ./thumbnail ./cmd/fotomat; do go test -short -cover -covermode=count -coverprofile=$dir/profile.out $dir; done
View
@@ -8,7 +8,7 @@ import (
const (
// FotomatVersion is updated by git-hooks/pre-commit
FotomatVersion = "2.6.203"
FotomatVersion = "2.6.204"
)
var (
View
@@ -7,12 +7,16 @@ import (
)
var (
// ErrInvalidOperation is returned for an invalid operation on this image format.
ErrInvalidOperation = errors.New("Invalid operation")
ErrUnknownFormat = errors.New("Unknown image format")
// ErrUnknownFormat is returned when the given image is in an unknown format.
ErrUnknownFormat = errors.New("Unknown image format")
)
// Format of compressed image.
type Format int
// Format of compressed image.
const (
Unknown Format = iota
Jpeg
@@ -33,6 +37,7 @@ var formatInfo = []struct {
{mime: "image/webp", loadFile: vips.Webpload, loadBytes: vips.WebploadBuffer},
}
// DetectFormat detects the Format of the supplied byte slice.
func DetectFormat(blob []byte) Format {
mime := http.DetectContentType(blob)
@@ -45,18 +50,22 @@ func DetectFormat(blob []byte) Format {
return Unknown
}
// String returns the mime type of given image format.
func (format Format) String() string {
return formatInfo[format].mime
}
// CanLoadFile returns true if we know how to load this format from a file.
func (format Format) CanLoadFile() bool {
return formatInfo[format].loadFile != nil
}
// CanLoadBytes returns true if we know how to load this format from a byte slice.
func (format Format) CanLoadBytes() bool {
return formatInfo[format].loadBytes != nil
}
// LoadFile loads a file in a given Format and returns an Image.
func (format Format) LoadFile(filename string) (*vips.Image, error) {
loadFile := formatInfo[format].loadFile
if loadFile == nil {
@@ -66,6 +75,7 @@ func (format Format) LoadFile(filename string) (*vips.Image, error) {
return loadFile(filename)
}
// LoadBytes loads byte slice in a given format and returns an Image.
func (format Format) LoadBytes(blob []byte) (*vips.Image, error) {
loadBytes := formatInfo[format].loadBytes
if loadBytes == nil {
View
@@ -4,6 +4,7 @@ import (
"github.com/die-net/fotomat/vips"
)
// Metadata is the currently-known metadata about an Image.
type Metadata struct {
Width int
Height int
@@ -12,6 +13,7 @@ type Metadata struct {
HasAlpha bool
}
// MetadataBytes parses an image byte slice and returns Metadata or an error.
func MetadataBytes(blob []byte) (Metadata, error) {
format := DetectFormat(blob)
if format == Unknown {
@@ -21,6 +23,7 @@ func MetadataBytes(blob []byte) (Metadata, error) {
return format.MetadataBytes(blob)
}
// MetadataBytes parses an image byte slice in known format and returns Metadata or an error.
func (format Format) MetadataBytes(blob []byte) (Metadata, error) {
image, err := format.LoadBytes(blob)
if err != nil {
@@ -32,6 +35,7 @@ func (format Format) MetadataBytes(blob []byte) (Metadata, error) {
return metadataImageFormat(image, format), nil
}
// MetadataFile parses an image file in known format and returns Metadata or an error.
func (format Format) MetadataFile(filename string) (Metadata, error) {
image, err := format.LoadFile(filename)
if err != nil {
@@ -49,6 +53,7 @@ func metadataImageFormat(image *vips.Image, format Format) Metadata {
return m
}
// MetadataImage returns Metadata from an Image. Format is always unset.
func MetadataImage(image *vips.Image) Metadata {
o := DetectOrientation(image)
w, h := o.Dimensions(image.Xsize(), image.Ysize())
View
@@ -5,8 +5,10 @@ import (
"strconv"
)
// Orientation is the current Image orientation, as stored by a camera.
type Orientation int
// Orientation values as defined by EXIF.
const (
Undefined Orientation = iota
TopLeft
@@ -36,6 +38,7 @@ var orientationInfo = []struct {
{swapXY: true, flipX: true, flipY: false, apply: rot270},
}
// DetectOrientation detects the current Image Orientation from the EXIF header.
func DetectOrientation(image *vips.Image) Orientation {
o, ok := image.ImageGetAsString(vips.ExifOrientation)
if !ok || o == "" {
@@ -50,13 +53,15 @@ func DetectOrientation(image *vips.Image) Orientation {
return Orientation(orientation)
}
// Dimensions translates a virtual width and height to match the current physical Orientation.
func (orientation Orientation) Dimensions(width, height int) (int, int) {
if orientationInfo[orientation].swapXY {
return height, width
}
return width, height
}
// Crop translates crop parameters from virtual coordinates to match the current physical Orientation.
func (orientation Orientation) Crop(ow, oh int, x, y int, iw, ih int) (int, int, int, int) {
oi := &orientationInfo[orientation]
@@ -74,6 +79,8 @@ func (orientation Orientation) Crop(ow, oh int, x, y int, iw, ih int) (int, int,
return x, y, ow, oh
}
// Apply executes a set of operations to change the pixel ordering from
// orientation to TopLeft.
func (orientation Orientation) Apply(image *vips.Image) error {
oi := &orientationInfo[orientation]
View
@@ -6,21 +6,32 @@ import (
)
const (
DefaultQuality = 85
// DefaultQuality is used when SaveOptions.Quality is unspecified.
DefaultQuality = 85
// DefaultCompression is used when SaveOptions.Compression is unspecified.
DefaultCompression = 6
)
// ErrInvalidSaveFormat is returned if the specified Format can't be written to.
var ErrInvalidSaveFormat = errors.New("Invalid save format")
// SaveOptions specifies how an image should be saved.
type SaveOptions struct {
Format Format
Quality int
Compression int
AllowWebp bool
Lossless bool
// Format is the Format that an image is saved in. If unspecified, the best output format for a given input image is selected.
Format Format
// JPEG or WebP quality for an output image (1-100).
Quality int
// Compress is the GZIP compression setting to use for PNG images (1-9).
Compression int
// AllowWebp allows automatic selection of WebP format, if reader can support it.
AllowWebp bool
// Lossless allows selection of a lossless output format.
Lossless bool
// LossyIfPhoto uses a lossy format if it detects that an image is a photo.
LossyIfPhoto bool
}
// Save returns an Image compressed using the given SaveOptions as a byte slice.
func Save(image *vips.Image, options SaveOptions) ([]byte, error) {
if options.Quality < 1 || options.Quality > 100 {
options.Quality = DefaultQuality
View
@@ -9,29 +9,50 @@ import (
)
var (
// ErrBadOption is returned when option values are out of range.
ErrBadOption = errors.New("Bad option specified")
ErrTooBig = errors.New("Image is too wide or tall")
ErrTooSmall = errors.New("Image is too small")
// ErrTooBig is returned when an image is too wide or tall.
ErrTooBig = errors.New("Image is too wide or tall")
// ErrTooSmall is returned when an image is too small.
ErrTooSmall = errors.New("Image is too small")
)
const (
minDimension = 2 // Avoid off-by-one divide-by-zero errors.
maxDimension = (1 << 15) - 2 // Avoid signed int16 overflows.
)
// Options specifies how a Thumbnail operation should modify an image.
type Options struct {
Width int
Height int
Crop bool
MaxBufferPixels int
Sharpen bool
BlurSigma float64
FastResize bool
MaxQueueDuration time.Duration
// Width and Height are the optional maximum sizes of output image,
// in pixels. If Crop is false, the original aspect ratio is
// preserved and the more restrictive of Width or Height are used.
Width int
Height int
// Crop enables crop mode, where exact supplied Width:Height aspect
// ratio is preserved and excess pixels are trimmed from the sides.
Crop bool
// MaxBufferPixels specifies how large of an intermediate image
// buffer to allow, in pixels. RAM usage will be a few bytes per pixel.
MaxBufferPixels int
// Sharpen runs a mild sharpening pass on downsampled images.
Sharpen bool
// BlurSigma performs a gaussian blur with specified sigma.
BlurSigma float64
// FastResize reduces output image quality in some cases in favor of speed.
FastResize bool
// MaxQueueDuration limits the amount of time spent in a queue before processing starts.
MaxQueueDuration time.Duration
// MaxProcessingDuration limits the amount of time processing an
// image, after which it is assumed the operation has crashed and
// the server aborts, killing all outstanding requests.
MaxProcessingDuration time.Duration
Save format.SaveOptions
// Save specifies the format.SaveOptions to use when compressing the modified image.
Save format.SaveOptions
}
// Check verifies Options against Metadata and returns a modified
// Options or an error.
func (o Options) Check(m format.Metadata) (Options, error) {
// Input format must be set.
if m.Format == format.Unknown {
View
@@ -8,6 +8,8 @@ import (
)
var (
// ErrAborted means the operation wasn't executed because the
// Request.Aborted channel was closed by the caller.
ErrAborted = errors.New("Thumbnail request aborted")
)
View
@@ -12,13 +12,16 @@ import (
)
const (
DefaultAccept = "image/jpeg,image/*;q=0.6"
DefaultServer = "Fotomat"
// DefaultAccept is the default Accept header sent on upstream requests.
DefaultAccept = "image/jpeg,image/*;q=0.6"
// DefaultServer is the default Server header sent with responses.
DefaultServer = "Fotomat"
// DefaultUserAgent is the default User-Agent header sent on upstream requests.
DefaultUserAgent = "Fotomat (http://fotomat.org)"
)
// Proxy represents an HTTP proxy that can optionally run its contents
// through Thumbnail.
// through Thumbnail. Must be created with NewProxy.
type Proxy struct {
Director func(*http.Request) (Options, int)
Client *http.Client
@@ -29,6 +32,8 @@ type Proxy struct {
active chan bool
}
// NewProxy creates a Proxy object, with a given Director, Pool, upper limit
// on images held in RAM, and http.Client.
func NewProxy(director func(*http.Request) (Options, int), pool *Pool, maxActive int, client *http.Client) *Proxy {
if director == nil || pool == nil || client == nil || maxActive <= 0 {
return nil
@@ -51,6 +56,9 @@ func NewProxy(director func(*http.Request) (Options, int), pool *Pool, maxActive
return p
}
// ServeHTTP serves an HTTP request for a given Proxy, using Director to
// parse the request, fetching an image, calling pool.Thumbnail on it, and
// returning the result.
func (p *Proxy) ServeHTTP(w http.ResponseWriter, or *http.Request) {
aborted := w.(http.CloseNotifier).CloseNotify()
@@ -134,6 +142,7 @@ func (p *Proxy) get(url string, header http.Header) ([]byte, http.Header, int, e
return orig, resp.Header, resp.StatusCode, err
}
// Close shuts down a Proxy.
func (p *Proxy) Close() {
close(p.active)
p.pool.Close()
View
@@ -8,6 +8,9 @@ import (
"time"
)
// Thumbnail scales or crops a compressed image blob according to the
// Options specified in o and returns a compressed image.
// Should be called from a thread pool with runtime.LockOSThread() locked.
func Thumbnail(blob []byte, o Options) ([]byte, error) {
if o.MaxProcessingDuration > 0 {
timer := time.AfterFunc(o.MaxProcessingDuration, func() {
View
@@ -6,6 +6,7 @@ package vips
*/
import "C"
// Min finds the single smallest value in all bands of the input image.
func (in *Image) Min() (float64, error) {
var out C.double
err := vipsError(C.cgo_vips_min(in.vi, &out))
View
@@ -37,13 +37,15 @@ const (
InterpretationScRGB Interpretation = C.VIPS_INTERPRETATION_scRGB
)
// Intent is the color management system rendering intent.
type Intent int
// Various Intent values understood by VIPS.
const (
IntentPerceptual Intent = C.VIPS_INTENT_PERCEPTUAL
IntentRelative Intent = C.VIPS_INTENT_RELATIVE
IntentSaturation Intent = C.VIPS_INTENT_SATURATION
IntentAbsolute Intent = C.VIPS_INTENT_ABSOLUTE
IntentPerceptual Intent = C.VIPS_INTENT_PERCEPTUAL // best for business graphics
IntentRelative Intent = C.VIPS_INTENT_RELATIVE // best for accurate communication with other imaging libraries
IntentSaturation Intent = C.VIPS_INTENT_SATURATION // best for business graphics
IntentAbsolute Intent = C.VIPS_INTENT_ABSOLUTE // best for scientific work
)
// Colourspace moves an image to a target colourspace using the best sequence of colour transform operations.
Oops, something went wrong.

0 comments on commit a9b9a80

Please sign in to comment.