thumbnails: drop the enable_vips build flag (#3985)
The thumbnails service now always resizes in pure Go. Deployments that
want more formats, webp output or an attention crop run a real imagor
and point WEBDAV_THUMBNAILS_GENERATOR_URL at it. The built-in service
stays as a minimal fallback that keeps working when imagor or libvips
have to be taken out, for example during a security issue.
Without cgo dependencies the docker images link statically again and no
longer need the vips packages. The vips parity run in CI goes away, the
regular test run already covers the pure Go build.
The preview fixtures of the acceptance tests were rendered by libvips,
so they are regenerated with the pure Go build. Text previews now lose
their left edge: the 640x480 text canvas is filled into a square box
and the pure Go build crops the centre, where libvips' attention crop
kept the left side with the text.
part of https://github.com/opencloud-eu/opencloud/issues/3912
Assisted-by: Claude Code:claude-opus-5-5
(cherry picked from commit 0d969c3179)
Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
Co-authored-by: Jörn Friedrich Dreyer <jfd@butonic.de>
No files matched your search
@@ -9,11 +9,7 @@ release-dirs:
|
||||
DOCKER_LDFLAGS += -X "$(OC_REPO)/pkg/config/defaults.BaseDataPathType=path" -X "$(OC_REPO)/pkg/config/defaults.BaseDataPathValue=/var/lib/opencloud"
|
||||
DOCKER_LDFLAGS += -X "$(OC_REPO)/pkg/config/defaults.BaseConfigPathType=path" -X "$(OC_REPO)/pkg/config/defaults.BaseConfigPathValue=/etc/opencloud"
|
||||
|
||||
# We can't link statically when vips is enabled but we still
|
||||
# prefer static linking where possible
|
||||
ifndef ENABLE_VIPS
|
||||
DOCKER_LDFLAGS += -extldflags "-static"
|
||||
endif
|
||||
DOCKER_LDFLAGS += -extldflags "-static"
|
||||
|
||||
release-linux-docker-amd64: release-dirs
|
||||
GOOS=linux \
|
||||
|
||||
@@ -19,9 +19,6 @@
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "debug",
|
||||
"buildFlags": [
|
||||
// "-tags", "enable_vips"
|
||||
],
|
||||
"program": "${workspaceFolder}/opencloud/cmd/opencloud",
|
||||
"args": ["server"],
|
||||
"env": {
|
||||
|
||||
@@ -806,20 +806,6 @@ def testOpencloud(ctx):
|
||||
"mv coverage.out unit.coverage.out",
|
||||
],
|
||||
},
|
||||
{
|
||||
# the only run that covers the resize code the official docker
|
||||
# images ship
|
||||
# FIXME: run it with the libvips those images ship, see issue 3746
|
||||
"name": "test-thumbnail-parity-vips",
|
||||
"image": OC_CI_GOLANG,
|
||||
"environment": {
|
||||
"THUMBNAILS_PARITY_IMAGOR_URL": "http://imagor:8000",
|
||||
"THUMBNAILS_PARITY_IMAGOR_MAGICK_URL": "http://imagor-magick:8000",
|
||||
},
|
||||
"commands": [
|
||||
"go test -tags enable_vips ./services/thumbnails/pkg/parity/...",
|
||||
],
|
||||
},
|
||||
{
|
||||
"name": "codacy-coverage",
|
||||
"image": CODACY_COVERAGE_REPORTER,
|
||||
@@ -2650,7 +2636,7 @@ def build():
|
||||
"name": "build",
|
||||
"image": OC_CI_GOLANG,
|
||||
"commands": [
|
||||
"for i in $(seq 3); do make -C opencloud build ENABLE_VIPS=1 && break || sleep 1; done",
|
||||
"for i in $(seq 3); do make -C opencloud build && break || sleep 1; done",
|
||||
],
|
||||
"environment": CI_HTTP_PROXY_ENV,
|
||||
},
|
||||
|
||||
@@ -26,11 +26,11 @@ FROM quay.io/opencloudeu/golang-ci:1.26 AS build
|
||||
COPY --from=generate /opencloud /opencloud
|
||||
|
||||
WORKDIR /opencloud/opencloud
|
||||
RUN make go-generate build ENABLE_VIPS=true
|
||||
RUN make go-generate build
|
||||
|
||||
FROM alpine:3.24
|
||||
|
||||
RUN apk add --no-cache attr ca-certificates curl mailcap tree vips && \
|
||||
RUN apk add --no-cache attr ca-certificates curl mailcap tree && \
|
||||
echo 'hosts: files dns' >| /etc/nsswitch.conf
|
||||
|
||||
LABEL maintainer="OpenCloud GmbH <devops@opencloud.eu>" \
|
||||
|
||||
@@ -16,7 +16,6 @@ require (
|
||||
github.com/cenkalti/backoff v2.2.1+incompatible
|
||||
github.com/coreos/go-oidc/v3 v3.21.0
|
||||
github.com/cs3org/go-cs3apis v0.0.0-20260424072047-8d9ef7076ae9
|
||||
github.com/davidbyttow/govips/v2 v2.20.0
|
||||
github.com/dhowden/tag v0.0.0-20240417053706-3d75831295e8
|
||||
github.com/dutchcoders/go-clamd v0.0.0-20170520113014-b970184f4d9e
|
||||
github.com/gabriel-vasile/mimetype v1.4.15
|
||||
|
||||
@@ -223,8 +223,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davidbyttow/govips/v2 v2.20.0 h1:sftLOcLeI+sMPVrBiw/ulWHuMm0kKyTQ2hfnoIYSxwc=
|
||||
github.com/davidbyttow/govips/v2 v2.20.0/go.mod h1:QK5liLrx7YaHC7xPcYnm4KaQIYvu9xSGrBt9goXrfgo=
|
||||
github.com/deckarep/golang-set v1.8.0 h1:sk9/l/KqpunDwP7pSjUg0keiOOLEnOBHzykLrsPppp4=
|
||||
github.com/deckarep/golang-set v1.8.0/go.mod h1:5nI87KwE7wgsBU1F4GKAw2Qod7p5kyS383rP6+o6qqo=
|
||||
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.1 h1:5RVFMOWjMyRy8cARdy79nAmgYw3hK/4HUq48LQ6Wwqo=
|
||||
|
||||
@@ -3,9 +3,6 @@ NAME := opencloud
|
||||
TAGS := disable_crypt
|
||||
GOARCH := $(shell go env GOARCH)
|
||||
CONFIG_DOCS_BASE_PATH := ../docs
|
||||
ifdef ENABLE_VIPS
|
||||
TAGS := ${TAGS},enable_vips
|
||||
endif
|
||||
|
||||
ifneq (, $(shell command -v go 2> /dev/null)) # suppress `command not found warnings` for non go targets in CI
|
||||
include ../.bingo/Variables.mk
|
||||
|
||||
@@ -3,7 +3,7 @@ FROM amd64/alpine:edge
|
||||
ARG VERSION=""
|
||||
ARG REVISION=""
|
||||
|
||||
RUN apk add --no-cache attr bash ca-certificates curl delve inotify-tools libc6-compat mailcap tree vips patch && \
|
||||
RUN apk add --no-cache attr bash ca-certificates curl delve inotify-tools libc6-compat mailcap tree patch && \
|
||||
echo 'hosts: files dns' >| /etc/nsswitch.conf
|
||||
|
||||
LABEL maintainer="OpenCloud GmbH <devops@opencloud.eu>" \
|
||||
|
||||
@@ -3,7 +3,7 @@ FROM arm64v8/alpine:edge
|
||||
ARG VERSION=""
|
||||
ARG REVISION=""
|
||||
|
||||
RUN apk add --no-cache attr bash ca-certificates curl delve inotify-tools libc6-compat mailcap tree vips patch && \
|
||||
RUN apk add --no-cache attr bash ca-certificates curl delve inotify-tools libc6-compat mailcap tree patch && \
|
||||
echo 'hosts: files dns' >| /etc/nsswitch.conf
|
||||
|
||||
LABEL maintainer="OpenCloud GmbH <devops@opencloud.eu>" \
|
||||
|
||||
@@ -12,7 +12,7 @@ RUN --mount=type=bind,target=/build,rw \
|
||||
--mount=type=cache,target=/root/.cache \
|
||||
GOOS="${TARGETOS:-linux}" GOARCH="${TARGETARCH:-amd64}" ; \
|
||||
make -C ${SRCDIR:-.}/opencloud release-linux-docker-${TARGETARCH} \
|
||||
ENABLE_VIPS=true DIST=/dist \
|
||||
DIST=/dist \
|
||||
VERSION="${VERSION}" EDITION="${EDITION}" \
|
||||
${STRING:+STRING="${STRING}"}
|
||||
|
||||
@@ -23,8 +23,7 @@ ARG TARGETOS
|
||||
ARG TARGETARCH
|
||||
|
||||
RUN apk add --no-cache attr bash ca-certificates curl imagemagick \
|
||||
inotify-tools libc6-compat mailcap tree vips \
|
||||
vips-magick patch && \
|
||||
inotify-tools libc6-compat mailcap tree patch && \
|
||||
echo 'hosts: files dns' >| /etc/nsswitch.conf
|
||||
|
||||
LABEL maintainer="openCloud GmbH <devops@opencloud.eu>" \
|
||||
|
||||
@@ -22,7 +22,7 @@ source file to this endpoint and receives the processed image back.
|
||||
| `POST /unsafe/fit-in/{width}x{height}/filters:{filters}` | Scale to fit within width x height, preserving aspect ratio and never upscaling |
|
||||
| `POST /unsafe/stretch/{width}x{height}/filters:{filters}` | Resize to the exact width x height without preserving aspect ratio (distorts) |
|
||||
|
||||
A `smart` segment behind the box, `POST /unsafe/{width}x{height}/smart/filters:{filters}`, asks for the crop that keeps the region carrying the image rather than the middle, which is what webdav sends for every cropping resize. With libvips that is an attention crop; the pure Go build has nothing of the kind and crops the centre, as the thumbnails service has always done without libvips.
|
||||
A `smart` segment behind the box, `POST /unsafe/{width}x{height}/smart/filters:{filters}`, asks for the crop that keeps the region carrying the image rather than the middle, which is what webdav sends for every cropping resize. A real imagor answers it with an attention crop; this service has nothing of the kind and crops the centre.
|
||||
|
||||
The path is imagor's. The filters segment is optional and may be empty
|
||||
(`filters:`), and an image key behind it is accepted and ignored, because the
|
||||
@@ -34,11 +34,10 @@ resized, while this service treats it as the box. Inside the filters segment,
|
||||
filter is ignored. `format` itself is optional: without it the input's own format is
|
||||
preserved, the same way imagor does it. An input this service cannot write
|
||||
(tiff, bmp) becomes a JPEG; imagor writes tiff back in its own format and only
|
||||
falls back to JPEG for the ones it cannot write either. Which formats those are
|
||||
depends on the build: with libvips a webp source stays a webp, the pure Go build
|
||||
answers it as a JPEG because it has no webp encoder.
|
||||
falls back to JPEG for the ones it cannot write either. A webp source is
|
||||
answered as a JPEG, because this service has no webp encoder.
|
||||
|
||||
What a build can read is its own backend's answer rather than a list: the library
|
||||
What this service can read is its decoders' answer rather than a list: the library
|
||||
that will decode the image reads the header first, so a source it cannot read
|
||||
answers `406` before anything is decoded, and the maximum dimensions are checked
|
||||
on that same header.
|
||||
@@ -61,13 +60,11 @@ decides it, are the webdav service's business and documented in
|
||||
|
||||
The request body is a `multipart/form-data` upload with a single file field
|
||||
named `image`, typed `image/*`; an untyped part is refused the way imagor's
|
||||
upload loader refuses it. Output formats are `jpg`, `png`, `gif` and, with
|
||||
libvips, `webp`; the pure Go build answers `406` for `format(webp)` because it
|
||||
has no webp encoder. An animated gif asked for a still format is flattened to
|
||||
upload loader refuses it. Output formats are `jpg`, `png` and `gif`; `format(webp)`
|
||||
is answered with `406` because this service has no webp encoder. An animated gif asked for a still format is flattened to
|
||||
its first frame, and a still source asked for `format(gif)` is palettised.
|
||||
|
||||
Reading an ICO needs libvips with its magick loader; the pure Go build answers
|
||||
`406` for one.
|
||||
An ICO is answered with `406`; imagor reads one only with its magick loader.
|
||||
|
||||
An animation is the one source whose frames are all decoded here, so what its
|
||||
blocks declare is read before any of them is: a gif with more frames than
|
||||
@@ -82,7 +79,7 @@ none.
|
||||
The answers are imagor's: `400` for a malformed request, which includes a
|
||||
`format` nobody writes (anything but `jpg`, `png`, `gif` and `webp`); `406` for a
|
||||
source it cannot read, for an upload part that is not typed `image/*`, and for a
|
||||
format this build has no encoder for (`webp` without libvips); `422` for an image
|
||||
format this service has no encoder for (`webp`); `422` for an image
|
||||
above the configured maximum dimensions and for an animation above the frame
|
||||
limit; `429` when the concurrency limit is reached; `413` for a body above
|
||||
`THUMBNAILS_MAX_INPUT_IMAGE_FILE_SIZE`.
|
||||
@@ -107,41 +104,12 @@ The source and the thumbnail have ceilings of their own, for different reasons.
|
||||
|
||||
A real imagor has one pair of knobs for both, `-vips-max-width` and `-vips-max-height`, which bound every image in its pipeline, the loaded source as well as the result. Set them to the source ceiling. They are also more forgiving than this service for formats libvips can shrink while loading: a 9000 pixel wide JPEG passes a 2048 limit there, where this service refuses it on the header.
|
||||
|
||||
## Using libvips for Image Processing
|
||||
## Supported Formats
|
||||
|
||||
To improve performance and to support a wider range of image formats, the
|
||||
thumbnails service is able to utilize the
|
||||
[libvips library](https://www.libvips.org/) for image processing. Support for
|
||||
libvips needs to be enabled at buildtime and has a couple of implications:
|
||||
|
||||
- With libvips support enabled, it is not possible to create a statically
|
||||
linked OpenCloud binary.
|
||||
- Therefore, the libvips shared libraries need to be available at runtime in
|
||||
the same release that was used to build the OpenCloud binary.
|
||||
- When using the OpenCloud docker images, the libvips shared libraries are
|
||||
included in the image and are correctly embedded.
|
||||
|
||||
Support of libvips is disabled by default. To enable it, make sure libvips
|
||||
and its buildtime dependencies are installed in your build environment. For
|
||||
macOS users, add the build time dependencies via:
|
||||
|
||||
```shell
|
||||
brew install vips pkg-config
|
||||
export PKG_CONFIG_PATH="/usr/local/opt/libffi/lib/pkgconfig"
|
||||
```
|
||||
|
||||
Then you just need to set the `ENABLE_VIPS` variable on the `make` command:
|
||||
|
||||
```shell
|
||||
make -C opencloud build ENABLE_VIPS=1
|
||||
```
|
||||
|
||||
Or include the `enable_vips` build tag in the `go build` command:
|
||||
|
||||
```shell
|
||||
go build -tags enable_vips -o opencloud -o bin/opencloud ./cmd/opencloud
|
||||
```
|
||||
|
||||
When building a docker image using the Dockerfile in the top-level directory of
|
||||
OpenCloud, libvips support is enabled and the libvips shared libraries are
|
||||
included in the resulting docker image.
|
||||
This service deliberately supports a small set of formats, decoded and encoded in
|
||||
pure Go: it reads `jpeg`, `png`, `gif`, `bmp`, `tiff` and `webp` and writes `jpg`,
|
||||
`png` and `gif`. It is meant as a fallback generator that keeps working when
|
||||
libvips or imagor need to be taken out of a deployment, for example while a
|
||||
security issue in them is being fixed. A deployment that wants more formats,
|
||||
webp output or an attention crop runs a real [imagor](https://imagor.net) and
|
||||
points `WEBDAV_THUMBNAILS_GENERATOR_URL` at it.
|
||||
@@ -1,5 +1,3 @@
|
||||
//go:build !enable_vips
|
||||
|
||||
package parity
|
||||
|
||||
const builtinBuild = "imaging"
|
||||
@@ -1,7 +0,0 @@
|
||||
//go:build enable_vips
|
||||
|
||||
package parity
|
||||
|
||||
// builtinBuild is the resize implementation compiled into this run, and the
|
||||
// override key for a divergence only it has. The container images ship vips.
|
||||
const builtinBuild = "vips"
|
||||
@@ -141,10 +141,9 @@ func (s Thumbnails) pushHandler(w http.ResponseWriter, r *http.Request) {
|
||||
MaxOutWidth: s.maxOutWidth, MaxOutHeight: s.maxOutHeight,
|
||||
}
|
||||
|
||||
// An animated gif is the one source both builds treat the same way: neither
|
||||
// libvips nor the pure Go path has anything better for a paletted animation,
|
||||
// so it never reaches a backend. Asking for a still format flattens it, and
|
||||
// that is a backend's job again.
|
||||
// An animated gif never reaches the still image backend, it is resized
|
||||
// frame by frame. Asking for a still format flattens it, and that is the
|
||||
// backend's job again.
|
||||
if bytes.HasPrefix(imgData, gifMagic) && (ext == "gif" || !hasFormat) {
|
||||
s.serveAnimation(w, imgData, req)
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
//go:build !enable_vips
|
||||
|
||||
package svc
|
||||
|
||||
import (
|
||||
@@ -15,9 +13,7 @@ import (
|
||||
"github.com/kovidgoyal/imaging/prism/meta/autometa"
|
||||
)
|
||||
|
||||
// The backend this build resizes with. The implementations keep their own names
|
||||
// so a symbol search finds both builds' versions side by side, the variables are
|
||||
// what the shared handler calls.
|
||||
// The backend the service resizes with, as variables the shared handler calls.
|
||||
var (
|
||||
loadSource = loadSourceImaging
|
||||
extForSourceFormat = extForSourceFormatImaging
|
||||
@@ -28,7 +24,7 @@ var (
|
||||
encodeWebP = encodeWebPImaging
|
||||
)
|
||||
|
||||
// imagingSource is a request's body as this build holds it between the header
|
||||
// imagingSource is a request's body as the service holds it between the header
|
||||
// read and the resize: the bytes plus what the header said about them. The
|
||||
// pixels are decoded in resizeSourceImaging, so the dimension guard has its say
|
||||
// before anything allocates a frame buffer.
|
||||
@@ -38,8 +34,8 @@ type imagingSource struct {
|
||||
format string
|
||||
}
|
||||
|
||||
// loadSourceImaging reads the header through the stdlib decoder registry, which this
|
||||
// build also decodes with: png, jpeg and gif from the stdlib, bmp, tiff and webp
|
||||
// loadSourceImaging reads the header through the stdlib decoder registry, which the
|
||||
// service also decodes with: png, jpeg and gif from the stdlib, bmp, tiff and webp
|
||||
// from the imaging library.
|
||||
func loadSourceImaging(data []byte) (*imagingSource, error) {
|
||||
config, format, err := image.DecodeConfig(bytes.NewReader(data))
|
||||
@@ -51,14 +47,14 @@ func loadSourceImaging(data []byte) (*imagingSource, error) {
|
||||
}
|
||||
|
||||
// Width and Height are what the header declared, so the dimension guard can ask
|
||||
// before the pixels exist. Close is there because the libvips build's image has
|
||||
// one; this build holds nothing that needs releasing.
|
||||
// before the pixels exist. Close is a no-op, the source holds nothing that
|
||||
// needs releasing.
|
||||
func (s *imagingSource) Width() int { return s.config.Width }
|
||||
func (s *imagingSource) Height() int { return s.config.Height }
|
||||
func (s *imagingSource) Close() {}
|
||||
|
||||
// extForSourceFormatImaging is the output format for a request that asked for none: the
|
||||
// input's own where this build can write it, a JPEG for the rest, which is how
|
||||
// input's own where the service can write it, a JPEG for the rest, which is how
|
||||
// imagor answers a format it cannot write either.
|
||||
func extForSourceFormatImaging(src *imagingSource) string {
|
||||
switch src.format {
|
||||
@@ -160,8 +156,8 @@ func resizeSourceImaging(src *imagingSource, req resizeRequest) (image.Image, er
|
||||
return img, nil
|
||||
}
|
||||
|
||||
// smart() asks for the most interesting window, which needs libvips;
|
||||
// this build crops the centre for it, the way main always did
|
||||
// smart() asks for the most interesting window, which real imagor
|
||||
// finds with libvips; this service crops the centre for it.
|
||||
|
||||
return imaging.Thumbnail(img, req.Width, req.Height, imaging.Lanczos), nil
|
||||
}
|
||||
@@ -176,6 +172,6 @@ func encodeGIFImaging(w io.Writer, img image.Image) error {
|
||||
return gif.Encode(w, palettedAtOrigin(img, palette.Plan9), nil)
|
||||
}
|
||||
|
||||
// encodeWebPImaging refuses: there is no webp encoder in the pure Go build, so a
|
||||
// deployment that needs webp output runs the libvips build or a real imagor.
|
||||
// encodeWebPImaging refuses: there is no webp encoder in pure Go, so a
|
||||
// deployment that needs webp output runs a real imagor.
|
||||
func encodeWebPImaging(io.Writer, image.Image) error { return errFormatUnsupported }
|
||||
@@ -226,7 +226,7 @@ var _ = Describe("outputFormatFromFilters", func() {
|
||||
Entry("jpeg maps onto our jpg", "format(jpeg)", "jpg", true, false),
|
||||
Entry("png", "format(png)", "png", true, false),
|
||||
Entry("gif", "format(gif)", "gif", true, false),
|
||||
Entry("webp, which only the libvips build writes", "format(webp)", "webp", true, false),
|
||||
Entry("webp, which only imagor writes", "format(webp)", "webp", true, false),
|
||||
Entry("behind another filter", "no_upscale():format(png)", "png", true, false),
|
||||
Entry("no format filter at all", "no_upscale()", "", false, false),
|
||||
Entry("an empty segment", "", "", false, false),
|
||||
|
||||
@@ -1,148 +0,0 @@
|
||||
//go:build enable_vips
|
||||
|
||||
package svc
|
||||
|
||||
import (
|
||||
"image/color/palette"
|
||||
"image/gif"
|
||||
"io"
|
||||
|
||||
"github.com/davidbyttow/govips/v2/vips"
|
||||
)
|
||||
|
||||
// The backend this build resizes with, as variables the shared handler calls, so
|
||||
// a symbol search finds both builds' implementations side by side. libvips is
|
||||
// demand-driven: loading a buffer parses the header and leaves the pixels alone
|
||||
// until something pulls them, so the dimension guard runs before any frame buffer
|
||||
// exists.
|
||||
var (
|
||||
loadSource = loadSourceVips
|
||||
extForSourceFormat = extForSourceFormatVips
|
||||
resizeSource = resizeSourceVips
|
||||
encodeJPEG = encodeJPEGVips
|
||||
encodePNG = encodePNGVips
|
||||
encodeGIF = encodeGIFVips
|
||||
encodeWebP = encodeWebPVips
|
||||
)
|
||||
|
||||
// loadSourceVips reads the header through libvips, which this build also decodes
|
||||
// with, so whatever the deployed libvips reads is what this service accepts.
|
||||
func loadSourceVips(data []byte) (*vips.ImageRef, error) { return vips.NewImageFromBuffer(data) }
|
||||
|
||||
// extForSourceFormatVips is the output format for a request that asked for none: the
|
||||
// input's own where this build can write it, a JPEG for the rest, which is how
|
||||
// imagor answers a format it cannot write either.
|
||||
func extForSourceFormatVips(src *vips.ImageRef) string {
|
||||
switch src.Format() {
|
||||
case vips.ImageTypePNG:
|
||||
return "png"
|
||||
case vips.ImageTypeGIF:
|
||||
return "gif"
|
||||
case vips.ImageTypeWEBP:
|
||||
return "webp"
|
||||
default: // jpeg, tiff, bmp, heif, ...
|
||||
return "jpg"
|
||||
}
|
||||
}
|
||||
|
||||
// resizeSourceVips resizes with libvips. The operation selects the resize/crop mode:
|
||||
// fill (center-crop to the box, upscaling by default like real imagor's default
|
||||
// resize), fit-in (fit within the box without cropping and without ever enlarging
|
||||
// the source, like imagor without its upscale() filter), or stretch (resize to
|
||||
// the exact box). noUpscale caps the default fill at the source size, mirroring
|
||||
// imagor's no_upscale() filter.
|
||||
func resizeSourceVips(src *vips.ImageRef, req resizeRequest) (*vips.ImageRef, error) {
|
||||
if req.Width == 0 || req.Height == 0 {
|
||||
boxW, boxH, resize, err := req.freeAxis(src.Width(), src.Height())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resize {
|
||||
if err := scaleTo(src, boxW, boxH); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return src, src.RemoveMetadata()
|
||||
}
|
||||
|
||||
switch req.Operation {
|
||||
case OpStretch:
|
||||
if err := scaleTo(src, req.Width, req.Height); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case OpFitIn:
|
||||
// imagor's fit-in only enlarges with the upscale() filter, which webdav
|
||||
// never sends, so it always caps at the source size.
|
||||
if err := src.ThumbnailWithSize(req.Width, req.Height, vips.InterestingNone, vips.SizeDown); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
default: // OpFill
|
||||
// SizeBoth upscales a small source the way imagor's default resize does,
|
||||
// no_upscale() caps it at the source size via SizeDown.
|
||||
size := vips.SizeBoth
|
||||
if req.NoUpscale {
|
||||
size = vips.SizeDown
|
||||
}
|
||||
crop := vips.InterestingCentre
|
||||
if req.Smart {
|
||||
crop = vips.InterestingAttention
|
||||
}
|
||||
if err := src.ThumbnailWithSize(req.Width, req.Height, crop, size); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return src, src.RemoveMetadata()
|
||||
}
|
||||
|
||||
// scaleTo resizes to the exact box without preserving the aspect ratio.
|
||||
func scaleTo(src *vips.ImageRef, width, height int) error {
|
||||
hScale := float64(width) / float64(src.Width())
|
||||
vScale := float64(height) / float64(src.Height())
|
||||
|
||||
return src.ResizeWithVScale(hScale, vScale, vips.KernelLanczos3)
|
||||
}
|
||||
|
||||
// The encoders keep the image in libvips form up to here, so a JPEG comes out of
|
||||
// libvips (progressive, quality 80) instead of a stdlib re-encode.
|
||||
func encodeJPEGVips(w io.Writer, img *vips.ImageRef) error {
|
||||
buf, _, err := img.ExportJpeg(vips.NewJpegExportParams())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = w.Write(buf)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func encodePNGVips(w io.Writer, img *vips.ImageRef) error {
|
||||
buf, _, err := img.ExportPng(vips.NewPngExportParams())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = w.Write(buf)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// encodeGIFVips goes through the same palette the animated path uses, as libvips has
|
||||
// no gif export.
|
||||
func encodeGIFVips(w io.Writer, img *vips.ImageRef) error {
|
||||
still, err := img.ToImage(vips.NewDefaultPNGExportParams())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return gif.Encode(w, palettedAtOrigin(still, palette.Plan9), nil)
|
||||
}
|
||||
|
||||
func encodeWebPVips(w io.Writer, img *vips.ImageRef) error {
|
||||
buf, _, err := img.ExportWebp(vips.NewWebpExportParams())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = w.Write(buf)
|
||||
|
||||
return err
|
||||
}
|
||||
@@ -10,9 +10,7 @@ import (
|
||||
|
||||
// resizeGIF resizes every frame of an animated gif, compositing each frame onto
|
||||
// a running canvas to honour the gif disposal method and re-palletting with
|
||||
// Floyd-Steinberg dithering. Both backends share it: the frames go through
|
||||
// imaging either way, because neither libvips nor the pure Go path has anything
|
||||
// better for a paletted animation. Code adapted from
|
||||
// Floyd-Steinberg dithering. Code adapted from
|
||||
// https://github.com/willnorris/gifresize. The only error is errBoxTooLarge.
|
||||
func resizeGIF(m *gif.GIF, req resizeRequest) (*gif.GIF, error) {
|
||||
srcX, srcY := m.Config.Width, m.Config.Height
|
||||
|
||||
@@ -64,7 +64,7 @@ Without a processor two things decide, in this order. An unconstrained axis is a
|
||||
|
||||
#### Thumbnail Generator
|
||||
|
||||
The thumbnails themselves are made by a generator the webdav service POSTs to, configured with `WEBDAV_THUMBNAILS_GENERATOR_URL` (and `WEBDAV_THUMBNAILS_GENERATOR_TIMEOUT`). The built-in thumbnails service speaks the imagor image endpoint, so a real [imagor](https://imagor.net) can be put in its place without changing anything else. A source the generator cannot read answers `404`, one that is too large `403`, and `WEBDAV_THUMBNAILS_MAX_INPUT_IMAGE_FILE_SIZE` caps what is sent at all; `OC_THUMBNAILS_MAX_INPUT_IMAGE_FILE_SIZE` sets this and the generator's own cap together, so one does not send what the other refuses. A generator that refuses because it is busy answers `429` with its own `Retry-After`, so a client can come back instead of treating it as a missing preview. The same cap bounds an image webdav extracts itself, the cover of an audio file or the thumbnail inside a geogebra container, because a compressed entry says nothing about its size until it is read. A cropping resize asks the generator for a smart crop, so what survives is the region that carries the image; a generator without libvips answers the same request with a centre crop.
|
||||
The thumbnails themselves are made by a generator the webdav service POSTs to, configured with `WEBDAV_THUMBNAILS_GENERATOR_URL` (and `WEBDAV_THUMBNAILS_GENERATOR_TIMEOUT`). The built-in thumbnails service speaks the imagor image endpoint, so a real [imagor](https://imagor.net) can be put in its place without changing anything else. A source the generator cannot read answers `404`, one that is too large `403`, and `WEBDAV_THUMBNAILS_MAX_INPUT_IMAGE_FILE_SIZE` caps what is sent at all; `OC_THUMBNAILS_MAX_INPUT_IMAGE_FILE_SIZE` sets this and the generator's own cap together, so one does not send what the other refuses. A generator that refuses because it is busy answers `429` with its own `Retry-After`, so a client can come back instead of treating it as a missing preview. The same cap bounds an image webdav extracts itself, the cover of an audio file or the thumbnail inside a geogebra container, because a compressed entry says nothing about its size until it is read. A cropping resize asks the generator for a smart crop, so what survives is the region that carries the image; the built-in thumbnails service answers the same request with a centre crop.
|
||||
|
||||
Three settings say what is made of what. Each one is merged onto the built-in defaults, which is what their `ADDITIONAL` says: naming one type does not take the answer away from the others, and an entry for a type the defaults already answer for replaces that one entry. The first takes bare mime types, the other two take `key:value` entries:
|
||||
|
||||
|
||||
@@ -42,8 +42,8 @@ func operationSegment(op string) string {
|
||||
//
|
||||
// A cropping resize also asks for the smart crop, so the generator keeps the
|
||||
// region that carries the image instead of whatever sits in the middle. What a
|
||||
// generator makes of it is its own: real imagor and the libvips build crop on
|
||||
// attention, the pure Go build has no such thing and crops the centre.
|
||||
// generator makes of it is its own: real imagor crops on attention, the
|
||||
// built-in thumbnails service has no such thing and crops the centre.
|
||||
func BuildURL(baseURL string, width, height int32, operation, format string, noUpscale bool) string {
|
||||
base := strings.TrimRight(baseURL, "/")
|
||||
|
||||
|
||||
@@ -236,7 +236,7 @@ Feature: previews of files downloaded through the webdav API
|
||||
| new |
|
||||
| spaces |
|
||||
|
||||
@need-build-with-VIPS @issue-1451
|
||||
@issue-1451
|
||||
Scenario Outline: it should update the preview content if the file content is updated (content with UTF chars)
|
||||
Given using <dav-path-version> DAV path
|
||||
And user "Alice" has uploaded file "filesForUpload/lorem.txt" to "/lorem.txt"
|
||||
@@ -250,7 +250,7 @@ Feature: previews of files downloaded through the webdav API
|
||||
| new |
|
||||
| spaces |
|
||||
|
||||
@need-build-with-VIPS @issue-1452
|
||||
@issue-1452
|
||||
Scenario Outline: download preview of the text file
|
||||
Given using <dav-path-version> DAV path
|
||||
And user "Alice" has uploaded file "filesForUpload/lorem.txt" to "/lorem.txt"
|
||||
@@ -359,7 +359,7 @@ Feature: previews of files downloaded through the webdav API
|
||||
| new |
|
||||
| spaces |
|
||||
|
||||
@need-build-with-VIPS
|
||||
|
||||
Scenario Outline: download previews of an image with different processors
|
||||
Given using <dav-path-version> DAV path
|
||||
And user "Alice" has uploaded file "filesForUpload/testavatar.jpg" to "/testimage.jpg"
|
||||
|
||||
|
Before Width: | Height: | Size: 967 B After Width: | Height: | Size: 836 B |
|
Before Width: | Height: | Size: 804 B After Width: | Height: | Size: 666 B |
|
Before Width: | Height: | Size: 944 B After Width: | Height: | Size: 797 B |
|
Before Width: | Height: | Size: 67 KiB After Width: | Height: | Size: 62 KiB |
|
Before Width: | Height: | Size: 967 B After Width: | Height: | Size: 836 B |
|
Before Width: | Height: | Size: 9.8 KiB After Width: | Height: | Size: 18 KiB |
@@ -1,24 +0,0 @@
|
||||
The MIT License
|
||||
|
||||
Copyright (c) Simple Things LLC and contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person
|
||||
obtaining a copy of this software and associated documentation
|
||||
files (the "Software"), to deal in the Software without
|
||||
restriction, including without limitation the rights to use,
|
||||
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
OTHER DEALINGS IN THE SOFTWARE.
|
||||
@@ -1,39 +0,0 @@
|
||||
package vips
|
||||
|
||||
// #include <vips/vips.h>
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
dbg "runtime/debug"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrUnsupportedImageFormat when image type is unsupported
|
||||
ErrUnsupportedImageFormat = errors.New("unsupported image format")
|
||||
)
|
||||
|
||||
func handleImageError(out *C.VipsImage) error {
|
||||
if out != nil {
|
||||
clearImage(out)
|
||||
}
|
||||
|
||||
return handleVipsError()
|
||||
}
|
||||
|
||||
func handleSaveBufferError(out unsafe.Pointer) error {
|
||||
if out != nil {
|
||||
gFreePointer(out)
|
||||
}
|
||||
|
||||
return handleVipsError()
|
||||
}
|
||||
|
||||
func handleVipsError() error {
|
||||
s := C.GoString(C.vips_error_buffer())
|
||||
C.vips_error_clear()
|
||||
|
||||
return fmt.Errorf("%v\nStack:\n%s", s, dbg.Stack())
|
||||
}
|
||||
@@ -1,628 +0,0 @@
|
||||
#include "foreign.h"
|
||||
|
||||
#include "lang.h"
|
||||
|
||||
void set_bool_param(Param *p, gboolean b) {
|
||||
p->type = PARAM_TYPE_BOOL;
|
||||
p->value.b = b;
|
||||
p->is_set = TRUE;
|
||||
}
|
||||
|
||||
void set_int_param(Param *p, gint i) {
|
||||
p->type = PARAM_TYPE_INT;
|
||||
p->value.i = i;
|
||||
p->is_set = TRUE;
|
||||
}
|
||||
|
||||
void set_double_param(Param *p, gdouble d) {
|
||||
p->type = PARAM_TYPE_DOUBLE;
|
||||
p->value.d = d;
|
||||
p->is_set = TRUE;
|
||||
}
|
||||
|
||||
int load_image_buffer(LoadParams *params, void *buf, size_t len,
|
||||
VipsImage **out) {
|
||||
int code = 1;
|
||||
ImageType imageType = params->inputFormat;
|
||||
|
||||
if (imageType == JPEG) {
|
||||
// shrink: int, fail: bool, autorotate: bool
|
||||
code = vips_jpegload_buffer(buf, len, out, "fail", params->fail,
|
||||
"autorotate", params->autorotate, "shrink",
|
||||
params->jpegShrink, "access", params->access, NULL);
|
||||
} else if (imageType == PNG) {
|
||||
code = vips_pngload_buffer(buf, len, out, "access", params->access, NULL);
|
||||
} else if (imageType == WEBP) {
|
||||
// page: int, n: int, scale: double
|
||||
code = vips_webpload_buffer(buf, len, out, "page", params->page, "n",
|
||||
params->n, "access", params->access, NULL);
|
||||
} else if (imageType == TIFF) {
|
||||
// page: int, n: int, autorotate: bool, subifd: int
|
||||
code =
|
||||
vips_tiffload_buffer(buf, len, out, "page", params->page, "n",
|
||||
params->n, "autorotate", params->autorotate, "access", params->access, NULL);
|
||||
} else if (imageType == GIF) {
|
||||
// page: int, n: int, scale: double
|
||||
code = vips_gifload_buffer(buf, len, out, "page", params->page, "n",
|
||||
params->n, "access", params->access, NULL);
|
||||
} else if (imageType == PDF) {
|
||||
// page: int, n: int, dpi: double, scale: double, background: color
|
||||
code = vips_pdfload_buffer(buf, len, out, "page", params->page, "n",
|
||||
params->n, "dpi", params->dpi, "access", params->access, NULL);
|
||||
} else if (imageType == SVG) {
|
||||
// dpi: double, scale: double, unlimited: bool
|
||||
code = vips_svgload_buffer(buf, len, out, "dpi", params->dpi, "unlimited",
|
||||
params->svgUnlimited, "access", params->access, NULL);
|
||||
} else if (imageType == HEIF) {
|
||||
// added autorotate on load as currently it addresses orientation issues
|
||||
// https://github.com/libvips/libvips/pull/1680
|
||||
// page: int, n: int, thumbnail: bool
|
||||
code = vips_heifload_buffer(buf, len, out, "page", params->page, "n",
|
||||
params->n, "thumbnail", params->heifThumbnail,
|
||||
"autorotate", TRUE, "access", params->access, NULL);
|
||||
} else if (imageType == MAGICK) {
|
||||
// page: int, n: int, density: string
|
||||
code = vips_magickload_buffer(buf, len, out, "page", params->page, "n",
|
||||
params->n, "access", params->access, NULL);
|
||||
} else if (imageType == AVIF) {
|
||||
code = vips_heifload_buffer(buf, len, out, "page", params->page, "n",
|
||||
params->n, "thumbnail", params->heifThumbnail,
|
||||
"autorotate", params->autorotate, "access", params->access, NULL);
|
||||
|
||||
}
|
||||
#if (VIPS_MAJOR_VERSION >= 8) && (VIPS_MINOR_VERSION >= 11)
|
||||
else if (imageType == JP2K) {
|
||||
code = vips_jp2kload_buffer(buf, len, out, "page", params->page, "access", params->access, NULL);
|
||||
}
|
||||
#endif
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
#define MAYBE_SET_BOOL(OP, PARAM, NAME) \
|
||||
if (PARAM.is_set) { \
|
||||
vips_object_set(VIPS_OBJECT(OP), NAME, PARAM.value.b, NULL); \
|
||||
}
|
||||
|
||||
#define MAYBE_SET_INT(OP, PARAM, NAME) \
|
||||
if (PARAM.is_set) { \
|
||||
vips_object_set(VIPS_OBJECT(OP), NAME, PARAM.value.i, NULL); \
|
||||
}
|
||||
|
||||
#define MAYBE_SET_DOUBLE(OP, PARAM, NAME) \
|
||||
if (PARAM.is_set) { \
|
||||
vips_object_set(VIPS_OBJECT(OP), NAME, PARAM.value.d, NULL); \
|
||||
}
|
||||
|
||||
typedef int (*SetLoadOptionsFn)(VipsOperation *operation, LoadParams *params);
|
||||
|
||||
int set_jpegload_options(VipsOperation *operation, LoadParams *params) {
|
||||
MAYBE_SET_BOOL(operation, params->autorotate, "autorotate");
|
||||
MAYBE_SET_BOOL(operation, params->fail, "fail");
|
||||
MAYBE_SET_INT(operation, params->jpegShrink, "shrink");
|
||||
MAYBE_SET_INT(operation, params->access, "access");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int set_pngload_options(VipsOperation *operation, LoadParams *params) {
|
||||
MAYBE_SET_BOOL(operation, params->fail, "fail");
|
||||
MAYBE_SET_INT(operation, params->access, "access");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int set_webpload_options(VipsOperation *operation, LoadParams *params) {
|
||||
MAYBE_SET_INT(operation, params->page, "page");
|
||||
MAYBE_SET_INT(operation, params->n, "n");
|
||||
MAYBE_SET_INT(operation, params->access, "access");
|
||||
MAYBE_SET_DOUBLE(operation, params->webpScale, "scale");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int set_tiffload_options(VipsOperation *operation, LoadParams *params) {
|
||||
MAYBE_SET_BOOL(operation, params->autorotate, "autorotate");
|
||||
MAYBE_SET_INT(operation, params->page, "page");
|
||||
MAYBE_SET_INT(operation, params->n, "n");
|
||||
MAYBE_SET_INT(operation, params->access, "access");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int set_gifload_options(VipsOperation *operation, LoadParams *params) {
|
||||
MAYBE_SET_INT(operation, params->page, "page");
|
||||
MAYBE_SET_INT(operation, params->n, "n");
|
||||
MAYBE_SET_INT(operation, params->access, "access");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int set_pdfload_options(VipsOperation *operation, LoadParams *params) {
|
||||
MAYBE_SET_INT(operation, params->page, "page");
|
||||
MAYBE_SET_INT(operation, params->n, "n");
|
||||
MAYBE_SET_DOUBLE(operation, params->dpi, "dpi");
|
||||
MAYBE_SET_INT(operation, params->access, "access");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int set_svgload_options(VipsOperation *operation, LoadParams *params) {
|
||||
MAYBE_SET_BOOL(operation, params->svgUnlimited, "unlimited");
|
||||
MAYBE_SET_DOUBLE(operation, params->dpi, "dpi");
|
||||
MAYBE_SET_INT(operation, params->access, "access");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int set_heifload_options(VipsOperation *operation, LoadParams *params) {
|
||||
MAYBE_SET_BOOL(operation, params->autorotate, "autorotate");
|
||||
MAYBE_SET_BOOL(operation, params->heifThumbnail, "thumbnail");
|
||||
MAYBE_SET_INT(operation, params->page, "page");
|
||||
MAYBE_SET_INT(operation, params->n, "n");
|
||||
MAYBE_SET_INT(operation, params->access, "access");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int set_jp2kload_options(VipsOperation *operation, LoadParams *params) {
|
||||
MAYBE_SET_INT(operation, params->page, "page");
|
||||
MAYBE_SET_INT(operation, params->access, "access");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int set_jxlload_options(VipsOperation *operation, LoadParams *params) {
|
||||
// nothing need to do
|
||||
return 0;
|
||||
}
|
||||
|
||||
int set_magickload_options(VipsOperation *operation, LoadParams *params) {
|
||||
MAYBE_SET_INT(operation, params->page, "page");
|
||||
MAYBE_SET_INT(operation, params->n, "n");
|
||||
MAYBE_SET_INT(operation, params->access, "access");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int load_buffer(const char *operationName, void *buf, size_t len,
|
||||
LoadParams *params, SetLoadOptionsFn setLoadOptions) {
|
||||
VipsBlob *blob = vips_blob_new(NULL, buf, len);
|
||||
|
||||
VipsOperation *operation = vips_operation_new(operationName);
|
||||
if (!operation) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (vips_object_set(VIPS_OBJECT(operation), "buffer", blob, NULL)) {
|
||||
vips_area_unref(VIPS_AREA(blob));
|
||||
return 1;
|
||||
}
|
||||
|
||||
vips_area_unref(VIPS_AREA(blob));
|
||||
|
||||
if (setLoadOptions(operation, params)) {
|
||||
vips_object_unref_outputs(VIPS_OBJECT(operation));
|
||||
g_object_unref(operation);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (vips_cache_operation_buildp(&operation)) {
|
||||
vips_object_unref_outputs(VIPS_OBJECT(operation));
|
||||
g_object_unref(operation);
|
||||
return 1;
|
||||
}
|
||||
|
||||
g_object_get(VIPS_OBJECT(operation), "out", ¶ms->outputImage, NULL);
|
||||
|
||||
vips_object_unref_outputs(VIPS_OBJECT(operation));
|
||||
g_object_unref(operation);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef int (*SetSaveOptionsFn)(VipsOperation *operation, SaveParams *params);
|
||||
|
||||
int save_buffer(const char *operationName, SaveParams *params,
|
||||
SetSaveOptionsFn setSaveOptions) {
|
||||
VipsBlob *blob;
|
||||
VipsOperation *operation = vips_operation_new(operationName);
|
||||
if (!operation) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (vips_object_set(VIPS_OBJECT(operation), "in", params->inputImage, NULL)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (setSaveOptions(operation, params)) {
|
||||
g_object_unref(operation);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// "keep" is set here rather than in each per-format setter so every
|
||||
// saver gets it the same way. It overrides the deprecated "strip".
|
||||
if (params->keepSet &&
|
||||
vips_object_set(VIPS_OBJECT(operation), "keep", params->keep, NULL)) {
|
||||
g_object_unref(operation);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (vips_cache_operation_buildp(&operation)) {
|
||||
vips_object_unref_outputs(VIPS_OBJECT(operation));
|
||||
g_object_unref(operation);
|
||||
return 1;
|
||||
}
|
||||
|
||||
g_object_get(VIPS_OBJECT(operation), "buffer", &blob, NULL);
|
||||
g_object_unref(operation);
|
||||
|
||||
VipsArea *area = VIPS_AREA(blob);
|
||||
|
||||
params->outputBuffer = (char *)(area->data);
|
||||
params->outputLen = area->length;
|
||||
area->free_fn = NULL;
|
||||
vips_area_unref(area);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// https://libvips.github.io/libvips/API/current/VipsForeignSave.html#vips-jpegsave-buffer
|
||||
int set_jpegsave_options(VipsOperation *operation, SaveParams *params) {
|
||||
int ret = vips_object_set(
|
||||
VIPS_OBJECT(operation), "strip", params->stripMetadata, "optimize_coding",
|
||||
params->jpegOptimizeCoding, "interlace", params->interlace,
|
||||
"subsample_mode", params->jpegSubsample, "trellis_quant",
|
||||
params->jpegTrellisQuant, "overshoot_deringing",
|
||||
params->jpegOvershootDeringing, "optimize_scans",
|
||||
params->jpegOptimizeScans, "quant_table", params->jpegQuantTable, NULL);
|
||||
|
||||
if (!ret && params->quality) {
|
||||
ret = vips_object_set(VIPS_OBJECT(operation), "Q", params->quality, NULL);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// https://libvips.github.io/libvips/API/current/VipsForeignSave.html#vips-pngsave-buffer
|
||||
int set_pngsave_options(VipsOperation *operation, SaveParams *params) {
|
||||
int ret =
|
||||
vips_object_set(VIPS_OBJECT(operation), "strip", params->stripMetadata,
|
||||
"compression", params->pngCompression, "interlace",
|
||||
params->interlace, "filter", params->pngFilter, "palette",
|
||||
params->pngPalette, NULL);
|
||||
|
||||
if (!ret && params->quality) {
|
||||
ret = vips_object_set(VIPS_OBJECT(operation), "Q", params->quality, NULL);
|
||||
}
|
||||
|
||||
if (!ret && params->pngDither) {
|
||||
ret = vips_object_set(VIPS_OBJECT(operation), "dither", params->pngDither, NULL);
|
||||
}
|
||||
|
||||
if (!ret && params->pngBitdepth) {
|
||||
ret = vips_object_set(VIPS_OBJECT(operation), "bitdepth", params->pngBitdepth, NULL);
|
||||
}
|
||||
|
||||
// TODO: Handle `profile` param.
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// https://github.com/libvips/libvips/blob/master/libvips/foreign/webpsave.c#L524
|
||||
// https://libvips.github.io/libvips/API/current/VipsForeignSave.html#vips-webpsave-buffer
|
||||
int set_webpsave_options(VipsOperation *operation, SaveParams *params) {
|
||||
int ret =
|
||||
vips_object_set(VIPS_OBJECT(operation),
|
||||
"strip", params->stripMetadata,
|
||||
"lossless", params->webpLossless,
|
||||
"near_lossless", params->webpNearLossless,
|
||||
"reduction_effort", params->webpReductionEffort,
|
||||
"min_size", params->webpMinSize,
|
||||
"kmin", params->webpKMin,
|
||||
"kmax", params->webpKMax,
|
||||
NULL);
|
||||
|
||||
// Only pass a profile the caller chose; leaving "profile" unset lets
|
||||
// strip/keep decide about the source image's own ICC, as for the other
|
||||
// savers. With StripMetadata and no Keep, still pass "none": webpsave in
|
||||
// libvips 8.14.0-8.14.1 ignores "strip" (a shadowed field, fixed in
|
||||
// 8.14.2), and "none" is what keeps the ICC out there. Elsewhere strip
|
||||
// already drops it.
|
||||
if (!ret && params->webpIccProfile) {
|
||||
ret = vips_object_set(VIPS_OBJECT(operation), "profile",
|
||||
params->webpIccProfile, NULL);
|
||||
} else if (!ret && params->stripMetadata && !params->keepSet) {
|
||||
ret = vips_object_set(VIPS_OBJECT(operation), "profile", "none", NULL);
|
||||
}
|
||||
|
||||
if (!ret && params->quality) {
|
||||
ret = vips_object_set(VIPS_OBJECT(operation), "Q", params->quality, NULL);
|
||||
}
|
||||
|
||||
if (!ret && params->webpTargetSize) {
|
||||
ret = vips_object_set(VIPS_OBJECT(operation), "target_size", params->webpTargetSize, NULL);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// https://libvips.github.io/libvips/API/current/VipsForeignSave.html#vips-tiffsave-buffer
|
||||
int set_tiffsave_options(VipsOperation *operation, SaveParams *params) {
|
||||
int ret = vips_object_set(
|
||||
VIPS_OBJECT(operation), "strip", params->stripMetadata, "compression",
|
||||
params->tiffCompression, "predictor", params->tiffPredictor, "pyramid",
|
||||
params->tiffPyramid, "tile_height", params->tiffTileHeight, "tile_width",
|
||||
params->tiffTileWidth, "tile", params->tiffTile, NULL);
|
||||
|
||||
if (!ret && params->quality) {
|
||||
ret = vips_object_set(VIPS_OBJECT(operation), "Q", params->quality, NULL);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// https://libvips.github.io/libvips/API/current/VipsForeignSave.html#vips-magicksave-buffer
|
||||
int set_magicksave_options(VipsOperation *operation, SaveParams *params) {
|
||||
int ret = vips_object_set(VIPS_OBJECT(operation), "format", params->magickFormat,
|
||||
"optimize_gif_frames", params->magickOptimizeGifFrames,
|
||||
"optimize_gif_transparency", params->magickOptimizeGifTransparency,
|
||||
"bitdepth", params->magickBitDepth, NULL);
|
||||
|
||||
if (!ret && params->quality) {
|
||||
ret = vips_object_set(VIPS_OBJECT(operation), "quality", params->quality,
|
||||
NULL);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// https://libvips.github.io/libvips/API/current/VipsForeignSave.html#vips-gifsave-buffer
|
||||
int set_gifsave_options(VipsOperation *operation, SaveParams *params) {
|
||||
int ret = 0;
|
||||
// See for argument values: https://www.libvips.org/API/current/VipsForeignSave.html#vips-gifsave
|
||||
if (params->gifDither > 0.0 && params->gifDither <= 10) {
|
||||
ret = vips_object_set(VIPS_OBJECT(operation), "dither", params->gifDither, NULL);
|
||||
}
|
||||
if (params->gifEffort >= 1 && params->gifEffort <= 10) {
|
||||
ret = vips_object_set(VIPS_OBJECT(operation), "effort", params->gifEffort, NULL);
|
||||
}
|
||||
if (params->gifBitdepth >= 1 && params->gifBitdepth <= 8) {
|
||||
ret = vips_object_set(VIPS_OBJECT(operation), "bitdepth", params->gifBitdepth, NULL);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// https://github.com/libvips/libvips/blob/master/libvips/foreign/heifsave.c#L653
|
||||
int set_heifsave_options(VipsOperation *operation, SaveParams *params) {
|
||||
int ret = vips_object_set(VIPS_OBJECT(operation), "lossless",
|
||||
params->heifLossless, NULL);
|
||||
|
||||
#if (VIPS_MAJOR_VERSION >= 8) && (VIPS_MINOR_VERSION >= 13)
|
||||
if (!ret && params->heifBitdepth && params->heifEffort) {
|
||||
ret = vips_object_set(VIPS_OBJECT(operation), "bitdepth",
|
||||
params->heifBitdepth, "effort", params->heifEffort,
|
||||
NULL);
|
||||
}
|
||||
#else
|
||||
if (!ret && params->heifEffort) {
|
||||
ret = vips_object_set(VIPS_OBJECT(operation), "speed", params->heifEffort,
|
||||
NULL);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!ret && params->quality) {
|
||||
ret = vips_object_set(VIPS_OBJECT(operation), "Q", params->quality, NULL);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// https://github.com/libvips/libvips/blob/master/libvips/foreign/heifsave.c#L653
|
||||
int set_avifsave_options(VipsOperation *operation, SaveParams *params) {
|
||||
int ret = vips_object_set(VIPS_OBJECT(operation), "strip", params->stripMetadata, "compression",
|
||||
VIPS_FOREIGN_HEIF_COMPRESSION_AV1, "lossless",
|
||||
params->heifLossless, NULL);
|
||||
|
||||
#if (VIPS_MAJOR_VERSION >= 8) && (VIPS_MINOR_VERSION >= 13)
|
||||
if (!ret && params->heifBitdepth && params->heifEffort) {
|
||||
ret = vips_object_set(VIPS_OBJECT(operation), "bitdepth",
|
||||
params->heifBitdepth, "effort", params->heifEffort,
|
||||
NULL);
|
||||
}
|
||||
#else
|
||||
if (!ret && params->heifEffort) {
|
||||
ret = vips_object_set(VIPS_OBJECT(operation), "speed", params->heifEffort,
|
||||
NULL);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!ret && params->quality) {
|
||||
ret = vips_object_set(VIPS_OBJECT(operation), "Q", params->quality, NULL);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int set_jp2ksave_options(VipsOperation *operation, SaveParams *params) {
|
||||
int ret = vips_object_set(
|
||||
VIPS_OBJECT(operation), "subsample_mode", params->jpegSubsample,
|
||||
"tile_height", params->jp2kTileHeight, "tile_width", params->jp2kTileWidth,
|
||||
"lossless", params->jp2kLossless, NULL);
|
||||
|
||||
if (!ret && params->quality) {
|
||||
ret = vips_object_set(VIPS_OBJECT(operation), "Q", params->quality, NULL);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int set_jxlsave_options(VipsOperation *operation, SaveParams *params) {
|
||||
int ret = vips_object_set(
|
||||
VIPS_OBJECT(operation), "tier", params->jxlTier,
|
||||
"distance", params->jxlDistance, "effort", params->jxlEffort,
|
||||
"lossless", params->jxlLossless, NULL);
|
||||
|
||||
if (!ret && params->quality) {
|
||||
ret = vips_object_set(VIPS_OBJECT(operation), "Q", params->quality, NULL);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int load_from_buffer(LoadParams *params, void *buf, size_t len) {
|
||||
switch (params->inputFormat) {
|
||||
case JPEG:
|
||||
return load_buffer("jpegload_buffer", buf, len, params,
|
||||
set_jpegload_options);
|
||||
case PNG:
|
||||
return load_buffer("pngload_buffer", buf, len, params,
|
||||
set_pngload_options);
|
||||
case WEBP:
|
||||
return load_buffer("webpload_buffer", buf, len, params,
|
||||
set_webpload_options);
|
||||
case HEIF:
|
||||
return load_buffer("heifload_buffer", buf, len, params,
|
||||
set_heifload_options);
|
||||
case TIFF:
|
||||
return load_buffer("tiffload_buffer", buf, len, params,
|
||||
set_tiffload_options);
|
||||
case SVG:
|
||||
return load_buffer("svgload_buffer", buf, len, params,
|
||||
set_svgload_options);
|
||||
case GIF:
|
||||
return load_buffer("gifload_buffer", buf, len, params,
|
||||
set_gifload_options);
|
||||
case PDF:
|
||||
return load_buffer("pdfload_buffer", buf, len, params,
|
||||
set_pdfload_options);
|
||||
case MAGICK:
|
||||
return load_buffer("magickload_buffer", buf, len, params,
|
||||
set_magickload_options);
|
||||
case AVIF:
|
||||
return load_buffer("heifload_buffer", buf, len, params,
|
||||
set_heifload_options);
|
||||
case JP2K:
|
||||
return load_buffer("jp2kload_buffer", buf, len, params,
|
||||
set_jp2kload_options);
|
||||
case JXL:
|
||||
return load_buffer("jxlload_buffer", buf, len, params,
|
||||
set_jxlload_options);
|
||||
default:
|
||||
g_warning("Unsupported input type given: %d", params->inputFormat);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int save_to_buffer(SaveParams *params) {
|
||||
switch (params->outputFormat) {
|
||||
case JPEG:
|
||||
return save_buffer("jpegsave_buffer", params, set_jpegsave_options);
|
||||
case PNG:
|
||||
return save_buffer("pngsave_buffer", params, set_pngsave_options);
|
||||
case WEBP:
|
||||
return save_buffer("webpsave_buffer", params, set_webpsave_options);
|
||||
case HEIF:
|
||||
return save_buffer("heifsave_buffer", params, set_heifsave_options);
|
||||
case TIFF:
|
||||
return save_buffer("tiffsave_buffer", params, set_tiffsave_options);
|
||||
case GIF:
|
||||
#if (VIPS_MAJOR_VERSION >= 8) && (VIPS_MINOR_VERSION >= 12)
|
||||
return save_buffer("gifsave_buffer", params, set_gifsave_options);
|
||||
#else
|
||||
// Gif save through ImageMagick below Vips Version 8.12
|
||||
params->magickFormat="GIF";
|
||||
params->magickOptimizeGifFrames=FALSE;
|
||||
params->magickOptimizeGifTransparency=FALSE;
|
||||
params->magickBitDepth=params->gifBitdepth;
|
||||
return save_buffer("magicksave_buffer", params, set_magicksave_options);
|
||||
#endif
|
||||
case AVIF:
|
||||
return save_buffer("heifsave_buffer", params, set_avifsave_options);
|
||||
case JP2K:
|
||||
return save_buffer("jp2ksave_buffer", params, set_jp2ksave_options);
|
||||
case JXL:
|
||||
return save_buffer("jxlsave_buffer", params, set_jxlsave_options);
|
||||
case MAGICK:
|
||||
return save_buffer("magicksave_buffer", params, set_magicksave_options);
|
||||
default:
|
||||
g_warning("Unsupported output type given: %d", params->outputFormat);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
LoadParams create_load_params(ImageType inputFormat) {
|
||||
Param defaultParam = {};
|
||||
LoadParams p = {
|
||||
.inputFormat = inputFormat,
|
||||
.inputBlob = NULL,
|
||||
.outputImage = NULL,
|
||||
.autorotate = defaultParam,
|
||||
.fail = defaultParam,
|
||||
.page = defaultParam,
|
||||
.n = defaultParam,
|
||||
.dpi = defaultParam,
|
||||
.jpegShrink = defaultParam,
|
||||
.webpScale = defaultParam,
|
||||
.heifThumbnail = defaultParam,
|
||||
.svgUnlimited = defaultParam,
|
||||
.access = defaultParam,
|
||||
};
|
||||
return p;
|
||||
}
|
||||
|
||||
// TODO: Change to same pattern as ImportParams
|
||||
|
||||
static SaveParams defaultSaveParams = {
|
||||
.inputImage = NULL,
|
||||
.outputBuffer = NULL,
|
||||
.outputFormat = JPEG,
|
||||
.outputLen = 0,
|
||||
|
||||
.interlace = FALSE,
|
||||
.quality = 0,
|
||||
.stripMetadata = FALSE,
|
||||
.keep = 0,
|
||||
.keepSet = FALSE,
|
||||
|
||||
.jpegOptimizeCoding = FALSE,
|
||||
.jpegSubsample = VIPS_FOREIGN_SUBSAMPLE_ON,
|
||||
.jpegTrellisQuant = FALSE,
|
||||
.jpegOvershootDeringing = FALSE,
|
||||
.jpegOptimizeScans = FALSE,
|
||||
.jpegQuantTable = 0,
|
||||
|
||||
.pngCompression = 6,
|
||||
.pngPalette = FALSE,
|
||||
.pngBitdepth = 0,
|
||||
.pngDither = 0,
|
||||
.pngFilter = VIPS_FOREIGN_PNG_FILTER_NONE,
|
||||
|
||||
.gifDither = 0.0,
|
||||
.gifEffort = 0,
|
||||
.gifBitdepth = 0,
|
||||
|
||||
.webpLossless = FALSE,
|
||||
.webpNearLossless = FALSE,
|
||||
.webpReductionEffort = 4,
|
||||
.webpIccProfile = NULL,
|
||||
.webpKMax = 0,
|
||||
.webpKMin = 0,
|
||||
.webpMinSize = FALSE,
|
||||
.webpTargetSize = 0,
|
||||
|
||||
.heifBitdepth = 8,
|
||||
.heifLossless = FALSE,
|
||||
.heifEffort = 5,
|
||||
|
||||
.tiffCompression = VIPS_FOREIGN_TIFF_COMPRESSION_LZW,
|
||||
.tiffPredictor = VIPS_FOREIGN_TIFF_PREDICTOR_HORIZONTAL,
|
||||
.tiffPyramid = FALSE,
|
||||
.tiffTile = FALSE,
|
||||
.tiffTileHeight = 256,
|
||||
.tiffTileWidth = 256,
|
||||
|
||||
.jp2kLossless = FALSE,
|
||||
.jp2kTileHeight = 512,
|
||||
.jp2kTileWidth = 512,
|
||||
|
||||
.jxlTier = 0,
|
||||
.jxlDistance = 1.0,
|
||||
.jxlEffort = 7,
|
||||
.jxlLossless = FALSE,
|
||||
};
|
||||
|
||||
SaveParams create_save_params(ImageType outputFormat) {
|
||||
SaveParams params = defaultSaveParams;
|
||||
params.outputFormat = outputFormat;
|
||||
return params;
|
||||
}
|
||||
@@ -1,759 +0,0 @@
|
||||
package vips
|
||||
|
||||
// #include "foreign.h"
|
||||
import "C"
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/net/html/charset"
|
||||
)
|
||||
|
||||
// SubsampleMode correlates to a libvips subsample mode
|
||||
type SubsampleMode int
|
||||
|
||||
// SubsampleMode enum correlating to libvips subsample modes
|
||||
const (
|
||||
VipsForeignSubsampleAuto SubsampleMode = C.VIPS_FOREIGN_SUBSAMPLE_AUTO
|
||||
VipsForeignSubsampleOn SubsampleMode = C.VIPS_FOREIGN_SUBSAMPLE_ON
|
||||
VipsForeignSubsampleOff SubsampleMode = C.VIPS_FOREIGN_SUBSAMPLE_OFF
|
||||
VipsForeignSubsampleLast SubsampleMode = C.VIPS_FOREIGN_SUBSAMPLE_LAST
|
||||
)
|
||||
|
||||
// ForeignKeep flags select which metadata a saver retains (libvips's "keep"
|
||||
// save option, libvips 8.15+). Values may be combined with |.
|
||||
//
|
||||
// The values are libvips's VipsForeignKeep bits written as literals rather
|
||||
// than C.VIPS_FOREIGN_KEEP_* because those macros don't exist in the
|
||||
// libvips 8.14 headers govips still builds against.
|
||||
const (
|
||||
// ForeignKeepNone keeps no metadata. It is a Go-only sentinel (like
|
||||
// KernelAuto) translated to VIPS_FOREIGN_KEEP_NONE (0) before reaching
|
||||
// libvips, because the zero value of the Keep fields means "unset".
|
||||
// It is a high bit rather than -1 so that it still composes with |.
|
||||
ForeignKeepNone ForeignKeep = 1 << 30
|
||||
ForeignKeepExif ForeignKeep = 1 << 0
|
||||
ForeignKeepXmp ForeignKeep = 1 << 1
|
||||
ForeignKeepIptc ForeignKeep = 1 << 2
|
||||
ForeignKeepIcc ForeignKeep = 1 << 3
|
||||
ForeignKeepOther ForeignKeep = 1 << 4
|
||||
// ForeignKeepGainmap keeps the UltraHDR gain map (libvips 8.18+). Older
|
||||
// libvips cannot load gain maps, so the bit is dropped there: combined
|
||||
// with other flags it changes nothing, but on its own it keeps no
|
||||
// metadata at all, exactly like ForeignKeepNone.
|
||||
ForeignKeepGainmap ForeignKeep = 1 << 5
|
||||
// ForeignKeepAll keeps all metadata the running libvips supports.
|
||||
ForeignKeepAll = ForeignKeepExif | ForeignKeepXmp | ForeignKeepIptc |
|
||||
ForeignKeepIcc | ForeignKeepOther | ForeignKeepGainmap
|
||||
)
|
||||
|
||||
// errKeepUnsupported is returned by resolveKeep when the libvips version
|
||||
// predates the "keep" save option.
|
||||
var errKeepUnsupported = errors.New("keep requires libvips 8.15+")
|
||||
|
||||
// resolveKeep maps a ForeignKeep value to the libvips "keep" flags for the
|
||||
// given libvips version. set is false when k is unset (zero), in which case
|
||||
// the caller must not pass "keep" at all and StripMetadata decides.
|
||||
func resolveKeep(k ForeignKeep, major, minor int) (flags int, set bool, err error) {
|
||||
if k == 0 {
|
||||
return 0, false, nil
|
||||
}
|
||||
if major < 8 || (major == 8 && minor < 15) {
|
||||
return 0, false, errKeepUnsupported
|
||||
}
|
||||
if unknown := k &^ (ForeignKeepAll | ForeignKeepNone); unknown != 0 {
|
||||
return 0, false, fmt.Errorf("unknown ForeignKeep bits: %#x", int(unknown))
|
||||
}
|
||||
k &^= ForeignKeepNone
|
||||
if major == 8 && minor < 18 {
|
||||
// libvips before 8.18 doesn't know VIPS_FOREIGN_KEEP_GAINMAP.
|
||||
k &^= ForeignKeepGainmap
|
||||
}
|
||||
return int(k), true, nil
|
||||
}
|
||||
|
||||
// applyKeep validates k against the running libvips and stores it in p.
|
||||
// Every save-param builder calls it, so the buffer and streaming paths
|
||||
// share one version guard.
|
||||
func applyKeep(p *C.struct_SaveParams, k ForeignKeep) error {
|
||||
flags, set, err := resolveKeep(k, MajorVersion, MinorVersion)
|
||||
if errors.Is(err, errKeepUnsupported) {
|
||||
return fmt.Errorf("Keep requires libvips 8.15+, found %s", Version)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.keep = C.int(flags)
|
||||
p.keepSet = C.int(boolToInt(set))
|
||||
return nil
|
||||
}
|
||||
|
||||
// ImageType represents an image type
|
||||
type ImageType int
|
||||
|
||||
// ImageType enum
|
||||
const (
|
||||
ImageTypeUnknown ImageType = C.UNKNOWN
|
||||
ImageTypeGIF ImageType = C.GIF
|
||||
ImageTypeJPEG ImageType = C.JPEG
|
||||
ImageTypeMagick ImageType = C.MAGICK
|
||||
ImageTypePDF ImageType = C.PDF
|
||||
ImageTypePNG ImageType = C.PNG
|
||||
ImageTypeSVG ImageType = C.SVG
|
||||
ImageTypeTIFF ImageType = C.TIFF
|
||||
ImageTypeWEBP ImageType = C.WEBP
|
||||
ImageTypeHEIF ImageType = C.HEIF
|
||||
ImageTypeBMP ImageType = C.BMP
|
||||
ImageTypeAVIF ImageType = C.AVIF
|
||||
ImageTypeJP2K ImageType = C.JP2K
|
||||
ImageTypeJXL ImageType = C.JXL
|
||||
ImageTypePSD ImageType = C.PSD
|
||||
)
|
||||
|
||||
// Types which should be deligated to ImageMagick loader
|
||||
var imageMagickTypes = map[ImageType]bool{
|
||||
ImageTypeBMP: true,
|
||||
ImageTypePSD: true,
|
||||
}
|
||||
|
||||
var imageTypeExtensionMap = map[ImageType]string{
|
||||
ImageTypeGIF: ".gif",
|
||||
ImageTypeJPEG: ".jpeg",
|
||||
ImageTypeMagick: ".magick",
|
||||
ImageTypePDF: ".pdf",
|
||||
ImageTypePNG: ".png",
|
||||
ImageTypeSVG: ".svg",
|
||||
ImageTypeTIFF: ".tiff",
|
||||
ImageTypeWEBP: ".webp",
|
||||
ImageTypeHEIF: ".heic",
|
||||
ImageTypeBMP: ".bmp",
|
||||
ImageTypeAVIF: ".avif",
|
||||
ImageTypeJP2K: ".jp2",
|
||||
ImageTypeJXL: ".jxl",
|
||||
ImageTypePSD: ".psd",
|
||||
}
|
||||
|
||||
// ImageTypes defines the various image types supported by govips
|
||||
var ImageTypes = map[ImageType]string{
|
||||
ImageTypeGIF: "gif",
|
||||
ImageTypeJPEG: "jpeg",
|
||||
ImageTypeMagick: "magick",
|
||||
ImageTypePDF: "pdf",
|
||||
ImageTypePNG: "png",
|
||||
ImageTypeSVG: "svg",
|
||||
ImageTypeTIFF: "tiff",
|
||||
ImageTypeWEBP: "webp",
|
||||
ImageTypeHEIF: "heif",
|
||||
ImageTypeBMP: "bmp",
|
||||
ImageTypeAVIF: "heif",
|
||||
ImageTypeJP2K: "jp2k",
|
||||
ImageTypeJXL: "jxl",
|
||||
ImageTypePSD: "psd",
|
||||
}
|
||||
|
||||
// TiffCompression represents method for compressing a tiff at export
|
||||
type TiffCompression int
|
||||
|
||||
// TiffCompression enum
|
||||
const (
|
||||
TiffCompressionNone TiffCompression = C.VIPS_FOREIGN_TIFF_COMPRESSION_NONE
|
||||
TiffCompressionJpeg TiffCompression = C.VIPS_FOREIGN_TIFF_COMPRESSION_JPEG
|
||||
TiffCompressionDeflate TiffCompression = C.VIPS_FOREIGN_TIFF_COMPRESSION_DEFLATE
|
||||
TiffCompressionPackbits TiffCompression = C.VIPS_FOREIGN_TIFF_COMPRESSION_PACKBITS
|
||||
TiffCompressionFax4 TiffCompression = C.VIPS_FOREIGN_TIFF_COMPRESSION_CCITTFAX4
|
||||
TiffCompressionLzw TiffCompression = C.VIPS_FOREIGN_TIFF_COMPRESSION_LZW
|
||||
TiffCompressionWebp TiffCompression = C.VIPS_FOREIGN_TIFF_COMPRESSION_WEBP
|
||||
TiffCompressionZstd TiffCompression = C.VIPS_FOREIGN_TIFF_COMPRESSION_ZSTD
|
||||
)
|
||||
|
||||
// TiffPredictor represents method for compressing a tiff at export
|
||||
type TiffPredictor int
|
||||
|
||||
// TiffPredictor enum
|
||||
const (
|
||||
TiffPredictorNone TiffPredictor = C.VIPS_FOREIGN_TIFF_PREDICTOR_NONE
|
||||
TiffPredictorHorizontal TiffPredictor = C.VIPS_FOREIGN_TIFF_PREDICTOR_HORIZONTAL
|
||||
TiffPredictorFloat TiffPredictor = C.VIPS_FOREIGN_TIFF_PREDICTOR_FLOAT
|
||||
)
|
||||
|
||||
// PngFilter represents filter algorithms that can be applied before compression.
|
||||
// See https://www.w3.org/TR/PNG-Filters.html
|
||||
type PngFilter int
|
||||
|
||||
// PngFilter enum
|
||||
const (
|
||||
PngFilterNone PngFilter = C.VIPS_FOREIGN_PNG_FILTER_NONE
|
||||
PngFilterSub PngFilter = C.VIPS_FOREIGN_PNG_FILTER_SUB
|
||||
PngFilterUo PngFilter = C.VIPS_FOREIGN_PNG_FILTER_UP
|
||||
PngFilterAvg PngFilter = C.VIPS_FOREIGN_PNG_FILTER_AVG
|
||||
PngFilterPaeth PngFilter = C.VIPS_FOREIGN_PNG_FILTER_PAETH
|
||||
PngFilterAll PngFilter = C.VIPS_FOREIGN_PNG_FILTER_ALL
|
||||
)
|
||||
|
||||
// Access represents how libvips opens files.
|
||||
// See https://www.libvips.org/API/current/How-it-opens-files.html
|
||||
const (
|
||||
AccessRandom int = C.VIPS_ACCESS_RANDOM
|
||||
AccessSequential int = C.VIPS_ACCESS_SEQUENTIAL
|
||||
AccessSequentialUnbuffered int = C.VIPS_ACCESS_SEQUENTIAL_UNBUFFERED
|
||||
AccessLast int = C.VIPS_ACCESS_LAST
|
||||
)
|
||||
|
||||
// FileExt returns the canonical extension for the ImageType
|
||||
func (i ImageType) FileExt() string {
|
||||
if ext, ok := imageTypeExtensionMap[i]; ok {
|
||||
return ext
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// IsTypeSupported checks whether given image type is supported by govips
|
||||
func IsTypeSupported(imageType ImageType) bool {
|
||||
if err := startupIfNeeded(); err != nil {
|
||||
govipsLog("govips", LogLevelError, fmt.Sprintf("failed to start vips: %v", err))
|
||||
return false
|
||||
}
|
||||
|
||||
// BMP is supported via the magick loader
|
||||
if imageType == ImageTypeBMP {
|
||||
return supportedImageTypes[ImageTypeMagick]
|
||||
}
|
||||
|
||||
return supportedImageTypes[imageType]
|
||||
}
|
||||
|
||||
// DetermineImageType attempts to determine the image type of the given buffer
|
||||
func DetermineImageType(buf []byte) ImageType {
|
||||
if len(buf) < 12 {
|
||||
return ImageTypeUnknown
|
||||
} else if isJPEG(buf) {
|
||||
return ImageTypeJPEG
|
||||
} else if isPNG(buf) {
|
||||
return ImageTypePNG
|
||||
} else if isGIF(buf) {
|
||||
return ImageTypeGIF
|
||||
} else if isTIFF(buf) {
|
||||
return ImageTypeTIFF
|
||||
} else if isWEBP(buf) {
|
||||
return ImageTypeWEBP
|
||||
} else if isAVIF(buf) {
|
||||
return ImageTypeAVIF
|
||||
} else if isHEIF(buf) {
|
||||
return ImageTypeHEIF
|
||||
} else if isSVG(buf) {
|
||||
return ImageTypeSVG
|
||||
} else if isBMP(buf) {
|
||||
return ImageTypeBMP
|
||||
} else if isJP2K(buf) {
|
||||
return ImageTypeJP2K
|
||||
} else if isJXL(buf) {
|
||||
return ImageTypeJXL
|
||||
} else if isPDF(buf) {
|
||||
return ImageTypePDF
|
||||
} else if isICO(buf) {
|
||||
return ImageTypeMagick
|
||||
} else if isPSD(buf) {
|
||||
return ImageTypePSD
|
||||
} else {
|
||||
return ImageTypeUnknown
|
||||
}
|
||||
}
|
||||
|
||||
var jpeg = []byte("\xFF\xD8\xFF")
|
||||
|
||||
func isJPEG(buf []byte) bool {
|
||||
return bytes.HasPrefix(buf, jpeg)
|
||||
}
|
||||
|
||||
var gifHeader = []byte("\x47\x49\x46")
|
||||
|
||||
func isGIF(buf []byte) bool {
|
||||
return bytes.HasPrefix(buf, gifHeader)
|
||||
}
|
||||
|
||||
var pngHeader = []byte("\x89\x50\x4E\x47")
|
||||
|
||||
func isPNG(buf []byte) bool {
|
||||
return bytes.HasPrefix(buf, pngHeader)
|
||||
}
|
||||
|
||||
var tifII = []byte("\x49\x49\x2A\x00")
|
||||
var tifMM = []byte("\x4D\x4D\x00\x2A")
|
||||
var bigTifII = []byte("\x49\x49\x2B\x00")
|
||||
var bigTifMM = []byte("\x4D\x4D\x00\x2B")
|
||||
|
||||
func isTIFF(buf []byte) bool {
|
||||
return bytes.HasPrefix(buf, tifII) ||
|
||||
bytes.HasPrefix(buf, tifMM) ||
|
||||
bytes.HasPrefix(buf, bigTifII) ||
|
||||
bytes.HasPrefix(buf, bigTifMM)
|
||||
}
|
||||
|
||||
var webpHeader = []byte("\x57\x45\x42\x50")
|
||||
|
||||
func isWEBP(buf []byte) bool {
|
||||
return bytes.Equal(buf[8:12], webpHeader)
|
||||
}
|
||||
|
||||
// https://github.com/strukturag/libheif/blob/master/libheif/heif.cc
|
||||
var ftyp = []byte("ftyp")
|
||||
var heic = []byte("heic")
|
||||
var heix = []byte("heix")
|
||||
var heim = []byte("heim")
|
||||
var heis = []byte("heis")
|
||||
var mif1 = []byte("mif1")
|
||||
var msf1 = []byte("msf1")
|
||||
var avif = []byte("avif")
|
||||
var avis = []byte("avis")
|
||||
|
||||
func isHEIF(buf []byte) bool {
|
||||
return bytes.Equal(buf[4:8], ftyp) && (bytes.Equal(buf[8:12], heic) ||
|
||||
bytes.Equal(buf[8:12], heix) ||
|
||||
bytes.Equal(buf[8:12], heim) ||
|
||||
bytes.Equal(buf[8:12], heis) ||
|
||||
bytes.Equal(buf[8:12], mif1) ||
|
||||
bytes.Equal(buf[8:12], msf1)) ||
|
||||
isAVIF(buf)
|
||||
}
|
||||
|
||||
func isAVIF(buf []byte) bool {
|
||||
return bytes.Equal(buf[4:8], ftyp) &&
|
||||
(bytes.Equal(buf[8:12], avif) || bytes.Equal(buf[8:12], avis))
|
||||
}
|
||||
|
||||
var svg = []byte("<svg")
|
||||
|
||||
func isSVG(buf []byte) bool {
|
||||
sub := buf[:int(math.Min(1024.0, float64(len(buf))))]
|
||||
if bytes.Contains(sub, svg) {
|
||||
data := &struct {
|
||||
XMLName xml.Name `xml:"svg"`
|
||||
}{}
|
||||
reader := bytes.NewReader(buf)
|
||||
decoder := xml.NewDecoder(reader)
|
||||
decoder.Strict = false
|
||||
decoder.CharsetReader = charset.NewReaderLabel
|
||||
|
||||
err := decoder.Decode(data)
|
||||
|
||||
return err == nil && data.XMLName.Local == "svg"
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
var pdf = []byte("\x25\x50\x44\x46")
|
||||
|
||||
func isPDF(buf []byte) bool {
|
||||
if len(buf) <= 1024 {
|
||||
return bytes.Contains(buf, pdf)
|
||||
}
|
||||
return bytes.Contains(buf[:1024], pdf)
|
||||
}
|
||||
|
||||
var bmpHeader = []byte("BM")
|
||||
|
||||
func isBMP(buf []byte) bool {
|
||||
return bytes.HasPrefix(buf, bmpHeader)
|
||||
}
|
||||
|
||||
// X'0000 000C 6A50 2020 0D0A 870A'
|
||||
var jp2kHeader = []byte("\x00\x00\x00\x0C\x6A\x50\x20\x20\x0D\x0A\x87\x0A")
|
||||
|
||||
// https://datatracker.ietf.org/doc/html/rfc3745
|
||||
func isJP2K(buf []byte) bool {
|
||||
return bytes.HasPrefix(buf, jp2kHeader)
|
||||
}
|
||||
|
||||
// As a 'naked' codestream
|
||||
var jxlHeader = []byte("\xff\x0a")
|
||||
|
||||
// As an ISOBMFF-based container: 0x0000000C 4A584C20 0D0A870A
|
||||
var jxlHeaderISOBMFF = []byte("\x00\x00\x00\x0C\x4A\x58\x4C\x20\x0D\x0A\x87\x0A")
|
||||
|
||||
func isJXL(buf []byte) bool {
|
||||
if len(buf) >= 2 && buf[0] == 0xFF && buf[1] == 0x0A {
|
||||
return true
|
||||
}
|
||||
if len(buf) >= 8 && bytes.Equal(buf[4:8], []byte("JXL ")) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
var icoHeader = []byte("\x00\x00\x01\x00")
|
||||
|
||||
func isICO(buf []byte) bool {
|
||||
return bytes.HasPrefix(buf, icoHeader)
|
||||
}
|
||||
|
||||
var psdHeader = []byte("\x38\x42\x50\x53")
|
||||
|
||||
func isPSD(buf []byte) bool {
|
||||
return bytes.HasPrefix(buf, psdHeader)
|
||||
}
|
||||
|
||||
func isNeedToChangeLoaderToMagick(t ImageType) bool {
|
||||
return imageMagickTypes[t]
|
||||
}
|
||||
|
||||
func vipsLoadFromBuffer(buf []byte, params *ImportParams) (*C.VipsImage, ImageType, ImageType, error) {
|
||||
src := buf
|
||||
// Reference src here so it's not garbage collected during image initialization.
|
||||
defer runtime.KeepAlive(src)
|
||||
|
||||
originalType := DetermineImageType(src)
|
||||
currentType := originalType
|
||||
|
||||
// Map image types which are not supported by libvips itself to ImageMagick
|
||||
if isNeedToChangeLoaderToMagick(originalType) {
|
||||
currentType = ImageTypeMagick
|
||||
}
|
||||
|
||||
if !IsTypeSupported(currentType) {
|
||||
govipsLog("govips", LogLevelInfo, fmt.Sprintf("failed to understand image format size=%d", len(src)))
|
||||
return nil, currentType, originalType, ErrUnsupportedImageFormat
|
||||
}
|
||||
|
||||
importParams := createImportParams(currentType, params)
|
||||
|
||||
if err := C.load_from_buffer(&importParams, unsafe.Pointer(&src[0]), C.size_t(len(src))); err != 0 {
|
||||
return nil, currentType, originalType, handleImageError(importParams.outputImage)
|
||||
}
|
||||
|
||||
return importParams.outputImage, currentType, originalType, nil
|
||||
}
|
||||
|
||||
func maybeSetBoolParam(p BoolParameter, cp *C.Param) {
|
||||
if p.IsSet() {
|
||||
C.set_bool_param(cp, toGboolean(p.Get()))
|
||||
}
|
||||
}
|
||||
|
||||
func maybeSetIntParam(p IntParameter, cp *C.Param) {
|
||||
if p.IsSet() {
|
||||
C.set_int_param(cp, C.int(p.Get()))
|
||||
}
|
||||
}
|
||||
|
||||
func maybeSetDoubleParam(p Float64Parameter, cp *C.Param) {
|
||||
if p.IsSet() {
|
||||
C.set_double_param(cp, C.gdouble(p.Get()))
|
||||
}
|
||||
}
|
||||
|
||||
func createImportParams(format ImageType, params *ImportParams) C.LoadParams {
|
||||
p := C.create_load_params(C.ImageType(format))
|
||||
|
||||
maybeSetBoolParam(params.AutoRotate, &p.autorotate)
|
||||
maybeSetBoolParam(params.FailOnError, &p.fail)
|
||||
maybeSetIntParam(params.Page, &p.page)
|
||||
maybeSetIntParam(params.NumPages, &p.n)
|
||||
maybeSetIntParam(params.JpegShrinkFactor, &p.jpegShrink)
|
||||
maybeSetDoubleParam(params.WebpScaleFactor, &p.webpScale)
|
||||
maybeSetBoolParam(params.HeifThumbnail, &p.heifThumbnail)
|
||||
maybeSetBoolParam(params.SvgUnlimited, &p.svgUnlimited)
|
||||
maybeSetIntParam(params.Access, &p.access)
|
||||
|
||||
if params.Density.IsSet() {
|
||||
C.set_double_param(&p.dpi, C.gdouble(params.Density.Get()))
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func newSaveParamsJPEG(in *C.VipsImage, params JpegExportParams) (C.struct_SaveParams, error) {
|
||||
p := C.create_save_params(C.JPEG)
|
||||
p.inputImage = in
|
||||
p.stripMetadata = C.int(boolToInt(params.StripMetadata))
|
||||
p.quality = C.int(params.Quality)
|
||||
p.interlace = C.int(boolToInt(params.Interlace))
|
||||
p.jpegOptimizeCoding = C.int(boolToInt(params.OptimizeCoding))
|
||||
p.jpegSubsample = C.VipsForeignSubsample(params.SubsampleMode)
|
||||
p.jpegTrellisQuant = C.int(boolToInt(params.TrellisQuant))
|
||||
p.jpegOvershootDeringing = C.int(boolToInt(params.OvershootDeringing))
|
||||
p.jpegOptimizeScans = C.int(boolToInt(params.OptimizeScans))
|
||||
p.jpegQuantTable = C.int(params.QuantTable)
|
||||
if err := applyKeep(&p, params.Keep); err != nil {
|
||||
return p, err
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func vipsSaveJPEGToBuffer(in *C.VipsImage, params JpegExportParams) ([]byte, error) {
|
||||
incOpCounter("save_jpeg_buffer")
|
||||
|
||||
p, err := newSaveParamsJPEG(in, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return vipsSaveToBuffer(p)
|
||||
}
|
||||
|
||||
func newSaveParamsPNG(in *C.VipsImage, params PngExportParams) (C.struct_SaveParams, error) {
|
||||
p := C.create_save_params(C.PNG)
|
||||
p.inputImage = in
|
||||
p.quality = C.int(params.Quality)
|
||||
p.stripMetadata = C.int(boolToInt(params.StripMetadata))
|
||||
p.interlace = C.int(boolToInt(params.Interlace))
|
||||
p.pngCompression = C.int(params.Compression)
|
||||
p.pngFilter = C.VipsForeignPngFilter(params.Filter)
|
||||
p.pngPalette = C.int(boolToInt(params.Palette))
|
||||
p.pngDither = C.double(params.Dither)
|
||||
p.pngBitdepth = C.int(params.Bitdepth)
|
||||
if err := applyKeep(&p, params.Keep); err != nil {
|
||||
return p, err
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func vipsSavePNGToBuffer(in *C.VipsImage, params PngExportParams) ([]byte, error) {
|
||||
incOpCounter("save_png_buffer")
|
||||
|
||||
p, err := newSaveParamsPNG(in, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return vipsSaveToBuffer(p)
|
||||
}
|
||||
|
||||
// newSaveParamsWebP returns the populated params and a cleanup function
|
||||
// that must be called once the save has completed; it frees the ICC
|
||||
// profile C string, if one was allocated. Both the buffer and streaming
|
||||
// WebP savers go through here, so the TargetSize version guard covers
|
||||
// both paths.
|
||||
func newSaveParamsWebP(in *C.VipsImage, params WebpExportParams) (C.struct_SaveParams, func(), error) {
|
||||
noop := func() {}
|
||||
p := C.create_save_params(C.WEBP)
|
||||
p.inputImage = in
|
||||
p.stripMetadata = C.int(boolToInt(params.StripMetadata))
|
||||
p.quality = C.int(params.Quality)
|
||||
p.webpLossless = C.int(boolToInt(params.Lossless))
|
||||
p.webpNearLossless = C.int(boolToInt(params.NearLossless))
|
||||
p.webpReductionEffort = C.int(params.ReductionEffort)
|
||||
p.webpMinSize = C.int(boolToInt(params.MinSize))
|
||||
p.webpKMin = C.int(params.MinKeyFrames)
|
||||
p.webpKMax = C.int(params.MaxKeyFrames)
|
||||
|
||||
if params.TargetSize > 0 {
|
||||
// webpsave's "target_size" property was added in libvips 8.17.4; setting
|
||||
// it on an older libvips fails at the C layer with an opaque "no property
|
||||
// named `target_size'" error. Fail clearly instead.
|
||||
if MajorVersion < 8 || (MajorVersion == 8 && MinorVersion < 17) ||
|
||||
(MajorVersion == 8 && MinorVersion == 17 && MicroVersion < 4) {
|
||||
return p, noop, fmt.Errorf("WebpExportParams.TargetSize requires libvips 8.17.4+, found %s", Version)
|
||||
}
|
||||
p.webpTargetSize = C.int(params.TargetSize)
|
||||
}
|
||||
|
||||
if err := applyKeep(&p, params.Keep); err != nil {
|
||||
return p, noop, err
|
||||
}
|
||||
|
||||
iccProfile := params.IccProfile
|
||||
if p.keepSet != 0 && ForeignKeep(p.keep)&ForeignKeepIcc == 0 {
|
||||
// An explicit Keep without ICC wins over any profile: libvips
|
||||
// would otherwise embed a supplied profile regardless of keep.
|
||||
iccProfile = ""
|
||||
}
|
||||
|
||||
cleanup := noop
|
||||
if iccProfile != "" {
|
||||
p.webpIccProfile = C.CString(iccProfile)
|
||||
profile := p.webpIccProfile
|
||||
cleanup = func() { C.free(unsafe.Pointer(profile)) }
|
||||
}
|
||||
return p, cleanup, nil
|
||||
}
|
||||
|
||||
func vipsSaveWebPToBuffer(in *C.VipsImage, params WebpExportParams) ([]byte, error) {
|
||||
incOpCounter("save_webp_buffer")
|
||||
|
||||
p, cleanup, err := newSaveParamsWebP(in, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cleanup()
|
||||
|
||||
return vipsSaveToBuffer(p)
|
||||
}
|
||||
|
||||
func newSaveParamsTIFF(in *C.VipsImage, params TiffExportParams) (C.struct_SaveParams, error) {
|
||||
p := C.create_save_params(C.TIFF)
|
||||
p.inputImage = in
|
||||
p.stripMetadata = C.int(boolToInt(params.StripMetadata))
|
||||
p.quality = C.int(params.Quality)
|
||||
p.tiffCompression = C.VipsForeignTiffCompression(params.Compression)
|
||||
p.tiffPyramid = C.int(boolToInt(params.Pyramid))
|
||||
p.tiffTile = C.int(boolToInt(params.Tile))
|
||||
tileHeight := params.TileHeight
|
||||
tileWidth := params.TileWidth
|
||||
if tileHeight <= 0 {
|
||||
tileHeight = 256
|
||||
}
|
||||
if tileWidth <= 0 {
|
||||
tileWidth = 256
|
||||
}
|
||||
p.tiffTileHeight = C.int(tileHeight)
|
||||
p.tiffTileWidth = C.int(tileWidth)
|
||||
if err := applyKeep(&p, params.Keep); err != nil {
|
||||
return p, err
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func vipsSaveTIFFToBuffer(in *C.VipsImage, params TiffExportParams) ([]byte, error) {
|
||||
incOpCounter("save_tiff_buffer")
|
||||
|
||||
p, err := newSaveParamsTIFF(in, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return vipsSaveToBuffer(p)
|
||||
}
|
||||
|
||||
func newSaveParamsHEIF(in *C.VipsImage, params HeifExportParams) (C.struct_SaveParams, error) {
|
||||
p := C.create_save_params(C.HEIF)
|
||||
p.inputImage = in
|
||||
p.outputFormat = C.HEIF
|
||||
p.quality = C.int(params.Quality)
|
||||
p.heifLossless = C.int(boolToInt(params.Lossless))
|
||||
p.heifBitdepth = C.int(params.Bitdepth)
|
||||
p.heifEffort = C.int(params.Effort)
|
||||
if err := applyKeep(&p, params.Keep); err != nil {
|
||||
return p, err
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func vipsSaveHEIFToBuffer(in *C.VipsImage, params HeifExportParams) ([]byte, error) {
|
||||
incOpCounter("save_heif_buffer")
|
||||
|
||||
p, err := newSaveParamsHEIF(in, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return vipsSaveToBuffer(p)
|
||||
}
|
||||
|
||||
func vipsSaveAVIFToBuffer(in *C.VipsImage, params AvifExportParams) ([]byte, error) {
|
||||
incOpCounter("save_heif_buffer")
|
||||
|
||||
// Speed was deprecated but we want to avoid breaking code that still uses it:
|
||||
effort := params.Effort
|
||||
if params.Speed != 0 {
|
||||
effort = params.Speed
|
||||
}
|
||||
|
||||
p := C.create_save_params(C.AVIF)
|
||||
p.inputImage = in
|
||||
p.stripMetadata = C.int(boolToInt(params.StripMetadata))
|
||||
p.outputFormat = C.AVIF
|
||||
p.quality = C.int(params.Quality)
|
||||
p.heifLossless = C.int(boolToInt(params.Lossless))
|
||||
p.heifBitdepth = C.int(params.Bitdepth)
|
||||
p.heifEffort = C.int(effort)
|
||||
if err := applyKeep(&p, params.Keep); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return vipsSaveToBuffer(p)
|
||||
}
|
||||
|
||||
func vipsSaveJP2KToBuffer(in *C.VipsImage, params Jp2kExportParams) ([]byte, error) {
|
||||
incOpCounter("save_jp2k_buffer")
|
||||
|
||||
p := C.create_save_params(C.JP2K)
|
||||
p.inputImage = in
|
||||
p.outputFormat = C.JP2K
|
||||
p.quality = C.int(params.Quality)
|
||||
p.jp2kLossless = C.int(boolToInt(params.Lossless))
|
||||
p.jp2kTileWidth = C.int(params.TileWidth)
|
||||
p.jp2kTileHeight = C.int(params.TileHeight)
|
||||
p.jpegSubsample = C.VipsForeignSubsample(params.SubsampleMode)
|
||||
if err := applyKeep(&p, params.Keep); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return vipsSaveToBuffer(p)
|
||||
}
|
||||
|
||||
func newSaveParamsGIF(in *C.VipsImage, params GifExportParams) (C.struct_SaveParams, error) {
|
||||
p := C.create_save_params(C.GIF)
|
||||
p.inputImage = in
|
||||
p.quality = C.int(params.Quality)
|
||||
p.gifDither = C.double(params.Dither)
|
||||
p.gifEffort = C.int(params.Effort)
|
||||
p.gifBitdepth = C.int(params.Bitdepth)
|
||||
if err := applyKeep(&p, params.Keep); err != nil {
|
||||
return p, err
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func vipsSaveGIFToBuffer(in *C.VipsImage, params GifExportParams) ([]byte, error) {
|
||||
incOpCounter("save_gif_buffer")
|
||||
|
||||
p, err := newSaveParamsGIF(in, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return vipsSaveToBuffer(p)
|
||||
}
|
||||
|
||||
func vipsSaveJxlToBuffer(in *C.VipsImage, params JxlExportParams) ([]byte, error) {
|
||||
incOpCounter("save_jxl_buffer")
|
||||
|
||||
p := C.create_save_params(C.JXL)
|
||||
p.inputImage = in
|
||||
p.outputFormat = C.JXL
|
||||
p.quality = C.int(params.Quality)
|
||||
p.jxlLossless = C.int(boolToInt(params.Lossless))
|
||||
p.jxlTier = C.int(params.Tier)
|
||||
p.jxlDistance = C.double(params.Distance)
|
||||
p.jxlEffort = C.int(params.Effort)
|
||||
if err := applyKeep(&p, params.Keep); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return vipsSaveToBuffer(p)
|
||||
}
|
||||
|
||||
func vipsSaveMagickToBuffer(in *C.VipsImage, params MagickExportParams) ([]byte, error) {
|
||||
incOpCounter("save_magick_buffer")
|
||||
|
||||
if params.Format == "" {
|
||||
return nil, errors.New("magick format required")
|
||||
}
|
||||
p := C.create_save_params(C.MAGICK)
|
||||
p.inputImage = in
|
||||
p.outputFormat = C.MAGICK
|
||||
p.quality = C.int(params.Quality)
|
||||
// libvips copies the string into the operation; nothing else frees it.
|
||||
cFormat := C.CString(params.Format)
|
||||
defer freeCString(cFormat)
|
||||
p.magickFormat = cFormat
|
||||
p.magickOptimizeGifFrames = C.int(boolToInt(params.OptimizeGifFrames))
|
||||
p.magickOptimizeGifTransparency = C.int(boolToInt(params.OptimizeGifTransparency))
|
||||
p.magickBitDepth = C.int(params.BitDepth)
|
||||
if err := applyKeep(&p, params.Keep); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return vipsSaveToBuffer(p)
|
||||
}
|
||||
|
||||
func vipsSaveToBuffer(params C.struct_SaveParams) ([]byte, error) {
|
||||
if err := C.save_to_buffer(¶ms); err != 0 {
|
||||
return nil, handleSaveBufferError(params.outputBuffer)
|
||||
}
|
||||
|
||||
buf := C.GoBytes(params.outputBuffer, C.int(params.outputLen))
|
||||
defer gFreePointer(params.outputBuffer)
|
||||
|
||||
return buf, nil
|
||||
}
|
||||
@@ -1,155 +0,0 @@
|
||||
// https://libvips.github.io/libvips/API/current/VipsForeignSave.html
|
||||
|
||||
// clang-format off
|
||||
// include order matters
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <vips/vips.h>
|
||||
#include <vips/foreign.h>
|
||||
// clang-format n
|
||||
|
||||
#ifndef BOOL
|
||||
#define BOOL int
|
||||
#endif
|
||||
|
||||
typedef enum types {
|
||||
UNKNOWN = 0,
|
||||
JPEG,
|
||||
WEBP,
|
||||
PNG,
|
||||
TIFF,
|
||||
GIF,
|
||||
PDF,
|
||||
SVG,
|
||||
MAGICK,
|
||||
HEIF,
|
||||
BMP,
|
||||
AVIF,
|
||||
JP2K,
|
||||
JXL,
|
||||
PSD,
|
||||
} ImageType;
|
||||
|
||||
typedef enum ParamType {
|
||||
PARAM_TYPE_NULL,
|
||||
PARAM_TYPE_BOOL,
|
||||
PARAM_TYPE_INT,
|
||||
PARAM_TYPE_DOUBLE,
|
||||
} ParamType;
|
||||
|
||||
typedef struct Param {
|
||||
ParamType type;
|
||||
|
||||
union Value {
|
||||
gboolean b;
|
||||
gint i;
|
||||
gdouble d;
|
||||
} value;
|
||||
|
||||
gboolean is_set;
|
||||
|
||||
} Param;
|
||||
|
||||
void set_bool_param(Param *p, gboolean b);
|
||||
void set_int_param(Param *p, gint i);
|
||||
void set_double_param(Param *p, gdouble d);
|
||||
|
||||
typedef struct LoadParams {
|
||||
ImageType inputFormat;
|
||||
VipsBlob *inputBlob;
|
||||
VipsImage *outputImage;
|
||||
|
||||
Param autorotate;
|
||||
Param fail;
|
||||
Param page;
|
||||
Param n;
|
||||
Param dpi;
|
||||
Param jpegShrink;
|
||||
Param webpScale;
|
||||
Param heifThumbnail;
|
||||
Param svgUnlimited;
|
||||
Param access;
|
||||
|
||||
} LoadParams;
|
||||
|
||||
LoadParams create_load_params(ImageType inputFormat);
|
||||
int load_from_buffer(LoadParams *params, void *buf, size_t len);
|
||||
|
||||
typedef struct SaveParams {
|
||||
VipsImage *inputImage;
|
||||
void *outputBuffer;
|
||||
ImageType outputFormat;
|
||||
size_t outputLen;
|
||||
|
||||
BOOL stripMetadata;
|
||||
// libvips "keep" flags (VipsForeignKeep, 8.15+). Plain int so the header
|
||||
// still compiles against libvips 8.14; only applied when keepSet.
|
||||
int keep;
|
||||
BOOL keepSet;
|
||||
int quality;
|
||||
BOOL interlace;
|
||||
|
||||
// JPEG
|
||||
BOOL jpegOptimizeCoding;
|
||||
VipsForeignSubsample jpegSubsample;
|
||||
BOOL jpegTrellisQuant;
|
||||
BOOL jpegOvershootDeringing;
|
||||
BOOL jpegOptimizeScans;
|
||||
int jpegQuantTable;
|
||||
|
||||
// PNG
|
||||
int pngCompression;
|
||||
VipsForeignPngFilter pngFilter;
|
||||
BOOL pngPalette;
|
||||
double pngDither;
|
||||
int pngBitdepth;
|
||||
|
||||
// GIF (with CGIF)
|
||||
double gifDither;
|
||||
int gifEffort;
|
||||
int gifBitdepth;
|
||||
|
||||
// WEBP
|
||||
BOOL webpLossless;
|
||||
BOOL webpNearLossless;
|
||||
int webpReductionEffort;
|
||||
char *webpIccProfile;
|
||||
BOOL webpMinSize;
|
||||
int webpKMin;
|
||||
int webpKMax;
|
||||
int webpTargetSize;
|
||||
|
||||
// HEIF - https://github.com/libvips/libvips/blob/master/libvips/foreign/heifsave.c#L71
|
||||
int heifBitdepth; // Bitdepth to save at for >8 bit images
|
||||
BOOL heifLossless; // Lossless compression
|
||||
int heifEffort; // CPU effort (0 - 9)
|
||||
|
||||
// TIFF
|
||||
VipsForeignTiffCompression tiffCompression;
|
||||
VipsForeignTiffPredictor tiffPredictor;
|
||||
BOOL tiffPyramid;
|
||||
BOOL tiffTile;
|
||||
int tiffTileHeight;
|
||||
int tiffTileWidth;
|
||||
|
||||
// JPEG2000
|
||||
BOOL jp2kLossless;
|
||||
int jp2kTileWidth;
|
||||
int jp2kTileHeight;
|
||||
|
||||
// JXL
|
||||
int jxlTier;
|
||||
double jxlDistance;
|
||||
int jxlEffort;
|
||||
BOOL jxlLossless;
|
||||
|
||||
// MAGICK
|
||||
char *magickFormat;
|
||||
BOOL magickOptimizeGifFrames;
|
||||
BOOL magickOptimizeGifTransparency;
|
||||
int magickBitDepth;
|
||||
} SaveParams;
|
||||
|
||||
SaveParams create_save_params(ImageType outputFormat);
|
||||
int save_to_buffer(SaveParams *params);
|
||||
|
||||
@@ -1,139 +0,0 @@
|
||||
// Code generated by vipsgen. DO NOT EDIT.
|
||||
//
|
||||
// Enum types needed by generated code that are not yet defined in the
|
||||
// hand-written codebase. These will move to gen_enums.go in Phase 4.
|
||||
|
||||
package vips
|
||||
|
||||
// #include <vips/vips.h>
|
||||
import "C"
|
||||
|
||||
// Precision represents VipsPrecision for controlling computation precision.
|
||||
type Precision int
|
||||
|
||||
const (
|
||||
PrecisionInteger Precision = C.VIPS_PRECISION_INTEGER
|
||||
PrecisionFloat Precision = C.VIPS_PRECISION_FLOAT
|
||||
PrecisionApproximate Precision = C.VIPS_PRECISION_APPROXIMATE
|
||||
)
|
||||
|
||||
// Combine represents VipsCombine for combining operations.
|
||||
type Combine int
|
||||
|
||||
const (
|
||||
CombineMax Combine = C.VIPS_COMBINE_MAX
|
||||
CombineSum Combine = C.VIPS_COMBINE_SUM
|
||||
CombineMin Combine = C.VIPS_COMBINE_MIN
|
||||
)
|
||||
|
||||
// CombineMode represents VipsCombineMode for draw combine operations.
|
||||
type CombineMode int
|
||||
|
||||
const (
|
||||
CombineModeSet CombineMode = C.VIPS_COMBINE_MODE_SET
|
||||
CombineModeAdd CombineMode = C.VIPS_COMBINE_MODE_ADD
|
||||
)
|
||||
|
||||
// OperationBoolean represents VipsOperationBoolean for boolean operations.
|
||||
type OperationBoolean int
|
||||
|
||||
const (
|
||||
OperationBooleanAnd OperationBoolean = C.VIPS_OPERATION_BOOLEAN_AND
|
||||
OperationBooleanOr OperationBoolean = C.VIPS_OPERATION_BOOLEAN_OR
|
||||
OperationBooleanEor OperationBoolean = C.VIPS_OPERATION_BOOLEAN_EOR
|
||||
OperationBooleanLshift OperationBoolean = C.VIPS_OPERATION_BOOLEAN_LSHIFT
|
||||
OperationBooleanRshift OperationBoolean = C.VIPS_OPERATION_BOOLEAN_RSHIFT
|
||||
)
|
||||
|
||||
// OperationComplex represents VipsOperationComplex for complex operations.
|
||||
type OperationComplex int
|
||||
|
||||
const (
|
||||
OperationComplexPolar OperationComplex = C.VIPS_OPERATION_COMPLEX_POLAR
|
||||
OperationComplexRect OperationComplex = C.VIPS_OPERATION_COMPLEX_RECT
|
||||
OperationComplexConj OperationComplex = C.VIPS_OPERATION_COMPLEX_CONJ
|
||||
)
|
||||
|
||||
// OperationComplex2 represents VipsOperationComplex2 for binary complex operations.
|
||||
type OperationComplex2 int
|
||||
|
||||
const (
|
||||
OperationComplex2CrossPhase OperationComplex2 = C.VIPS_OPERATION_COMPLEX2_CROSS_PHASE
|
||||
)
|
||||
|
||||
// OperationComplexget represents VipsOperationComplexget for getting complex components.
|
||||
type OperationComplexget int
|
||||
|
||||
const (
|
||||
OperationComplexgetReal OperationComplexget = C.VIPS_OPERATION_COMPLEXGET_REAL
|
||||
OperationComplexgetImag OperationComplexget = C.VIPS_OPERATION_COMPLEXGET_IMAG
|
||||
)
|
||||
|
||||
// OperationMath represents VipsOperationMath for math operations.
|
||||
type OperationMath int
|
||||
|
||||
const (
|
||||
OperationMathSin OperationMath = C.VIPS_OPERATION_MATH_SIN
|
||||
OperationMathCos OperationMath = C.VIPS_OPERATION_MATH_COS
|
||||
OperationMathTan OperationMath = C.VIPS_OPERATION_MATH_TAN
|
||||
OperationMathAsin OperationMath = C.VIPS_OPERATION_MATH_ASIN
|
||||
OperationMathAcos OperationMath = C.VIPS_OPERATION_MATH_ACOS
|
||||
OperationMathAtan OperationMath = C.VIPS_OPERATION_MATH_ATAN
|
||||
OperationMathLog OperationMath = C.VIPS_OPERATION_MATH_LOG
|
||||
OperationMathLog10 OperationMath = C.VIPS_OPERATION_MATH_LOG10
|
||||
OperationMathExp OperationMath = C.VIPS_OPERATION_MATH_EXP
|
||||
OperationMathExp10 OperationMath = C.VIPS_OPERATION_MATH_EXP10
|
||||
)
|
||||
|
||||
// OperationMath2 represents VipsOperationMath2 for binary math operations.
|
||||
type OperationMath2 int
|
||||
|
||||
const (
|
||||
OperationMath2Pow OperationMath2 = C.VIPS_OPERATION_MATH2_POW
|
||||
OperationMath2Wop OperationMath2 = C.VIPS_OPERATION_MATH2_WOP
|
||||
OperationMath2Atan2 OperationMath2 = C.VIPS_OPERATION_MATH2_ATAN2
|
||||
)
|
||||
|
||||
// OperationRelational represents VipsOperationRelational for relational operations.
|
||||
type OperationRelational int
|
||||
|
||||
const (
|
||||
OperationRelationalEqual OperationRelational = C.VIPS_OPERATION_RELATIONAL_EQUAL
|
||||
OperationRelationalNoteq OperationRelational = C.VIPS_OPERATION_RELATIONAL_NOTEQ
|
||||
OperationRelationalLess OperationRelational = C.VIPS_OPERATION_RELATIONAL_LESS
|
||||
OperationRelationalLesseq OperationRelational = C.VIPS_OPERATION_RELATIONAL_LESSEQ
|
||||
OperationRelationalMore OperationRelational = C.VIPS_OPERATION_RELATIONAL_MORE
|
||||
OperationRelationalMoreeq OperationRelational = C.VIPS_OPERATION_RELATIONAL_MOREEQ
|
||||
)
|
||||
|
||||
// OperationRound represents VipsOperationRound for rounding operations.
|
||||
type OperationRound int
|
||||
|
||||
const (
|
||||
OperationRoundRint OperationRound = C.VIPS_OPERATION_ROUND_RINT
|
||||
OperationRoundCeil OperationRound = C.VIPS_OPERATION_ROUND_CEIL
|
||||
OperationRoundFloor OperationRound = C.VIPS_OPERATION_ROUND_FLOOR
|
||||
)
|
||||
|
||||
// OperationMorphology represents VipsOperationMorphology for morphological operations.
|
||||
type OperationMorphology int
|
||||
|
||||
const (
|
||||
OperationMorphologyErode OperationMorphology = C.VIPS_OPERATION_MORPHOLOGY_ERODE
|
||||
OperationMorphologyDilate OperationMorphology = C.VIPS_OPERATION_MORPHOLOGY_DILATE
|
||||
)
|
||||
|
||||
// FailOn represents VipsFailOn for controlling error handling on load.
|
||||
type FailOn int
|
||||
|
||||
const (
|
||||
FailOnNone FailOn = C.VIPS_FAIL_ON_NONE
|
||||
FailOnTruncated FailOn = C.VIPS_FAIL_ON_TRUNCATED
|
||||
FailOnError FailOn = C.VIPS_FAIL_ON_ERROR
|
||||
FailOnWarning FailOn = C.VIPS_FAIL_ON_WARNING
|
||||
)
|
||||
|
||||
// ForeignKeep represents VipsForeignKeep flags for controlling metadata retention.
|
||||
type ForeignKeep int
|
||||
|
||||
// Note: ForeignKeep is a flags (bitmask) type. Values combined with |.
|
||||
@@ -1,3 +0,0 @@
|
||||
package vips
|
||||
|
||||
//go:generate go run ../cmd/vipsgen --generate --output=.
|
||||
@@ -1,27 +0,0 @@
|
||||
#include "govips.h"
|
||||
|
||||
static void govips_logging_handler(const gchar *log_domain,
|
||||
GLogLevelFlags log_level,
|
||||
const gchar *message, gpointer user_data) {
|
||||
govipsLoggingHandler((char *)log_domain, (int)log_level, (char *)message);
|
||||
}
|
||||
|
||||
static void null_logging_handler(const gchar *log_domain,
|
||||
GLogLevelFlags log_level, const gchar *message,
|
||||
gpointer user_data) {}
|
||||
|
||||
void vips_set_logging_handler(void) {
|
||||
g_log_set_default_handler(govips_logging_handler, NULL);
|
||||
}
|
||||
|
||||
void vips_unset_logging_handler(void) {
|
||||
g_log_set_default_handler(null_logging_handler, NULL);
|
||||
}
|
||||
|
||||
/* This function skips the Govips logging handler and logs
|
||||
directly to stdout. To be used only for testing and debugging.
|
||||
Needed for CI because of a Go macOS bug which doesn't clean cgo callbacks on
|
||||
exit. */
|
||||
void vips_default_logging_handler(void) {
|
||||
g_log_set_default_handler(g_log_default_handler, NULL);
|
||||
}
|
||||
@@ -1,299 +0,0 @@
|
||||
// Package vips provides go bindings for libvips, a fast image processing library.
|
||||
package vips
|
||||
|
||||
// #cgo pkg-config: vips
|
||||
// #include <vips/vips.h>
|
||||
// #include "govips.h"
|
||||
import "C"
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultConcurrencyLevel = 1
|
||||
defaultMaxCacheMem = 50 * 1024 * 1024
|
||||
defaultMaxCacheSize = 100
|
||||
defaultMaxCacheFiles = 0
|
||||
)
|
||||
|
||||
var (
|
||||
// Version is the full libvips version string (x.y.z)
|
||||
Version = C.GoString(C.vips_version_string())
|
||||
|
||||
// MajorVersion is the libvips major component of the version string (x in x.y.z)
|
||||
MajorVersion = int(C.vips_version(0))
|
||||
|
||||
// MinorVersion is the libvips minor component of the version string (y in x.y.z)
|
||||
MinorVersion = int(C.vips_version(1))
|
||||
|
||||
// MicroVersion is the libvips micro component of the version string (z in x.y.z)
|
||||
// Also known as patch version
|
||||
MicroVersion = int(C.vips_version(2))
|
||||
|
||||
running = false
|
||||
hasShutdown = false
|
||||
initLock sync.Mutex
|
||||
statCollectorDone chan struct{}
|
||||
once sync.Once
|
||||
typeLoaders = make(map[string]ImageType)
|
||||
supportedImageTypes = make(map[ImageType]bool)
|
||||
)
|
||||
|
||||
// Config allows fine-tuning of libvips library
|
||||
type Config struct {
|
||||
ConcurrencyLevel int
|
||||
MaxCacheFiles int
|
||||
MaxCacheMem int
|
||||
MaxCacheSize int
|
||||
ReportLeaks bool
|
||||
CacheTrace bool
|
||||
CollectStats bool
|
||||
}
|
||||
|
||||
// Startup sets up the libvips support and ensures the versions are correct. Pass in nil for
|
||||
// default configuration.
|
||||
func Startup(config *Config) error {
|
||||
if hasShutdown {
|
||||
return errors.New("govips cannot be stopped and restarted")
|
||||
}
|
||||
|
||||
initLock.Lock()
|
||||
defer initLock.Unlock()
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
if running {
|
||||
govipsLog("govips", LogLevelInfo, "warning libvips already started")
|
||||
return nil
|
||||
}
|
||||
|
||||
if MajorVersion < 8 {
|
||||
return errors.New("govips requires libvips version 8.10+")
|
||||
}
|
||||
|
||||
if MajorVersion == 8 && MinorVersion < 10 {
|
||||
return errors.New("govips requires libvips version 8.10+")
|
||||
}
|
||||
|
||||
cName := C.CString("govips")
|
||||
defer freeCString(cName)
|
||||
|
||||
// Initialize govips logging handler and verbosity filter to historical default
|
||||
if !currentLoggingOverridden {
|
||||
govipsLoggingSettings(nil, LogLevelInfo)
|
||||
}
|
||||
|
||||
// Override default glib logging handler to intercept logging messages
|
||||
enableLogging()
|
||||
|
||||
err := C.vips_init(cName)
|
||||
if err != 0 {
|
||||
return fmt.Errorf("failed to start vips code=%v", err)
|
||||
}
|
||||
|
||||
running = true
|
||||
|
||||
if config != nil {
|
||||
if config.CollectStats {
|
||||
statCollectorDone = collectStats()
|
||||
}
|
||||
|
||||
C.vips_leak_set(toGboolean(config.ReportLeaks))
|
||||
|
||||
if config.ConcurrencyLevel >= 0 {
|
||||
C.vips_concurrency_set(C.int(config.ConcurrencyLevel))
|
||||
} else {
|
||||
C.vips_concurrency_set(defaultConcurrencyLevel)
|
||||
}
|
||||
|
||||
if config.MaxCacheFiles >= 0 {
|
||||
C.vips_cache_set_max_files(C.int(config.MaxCacheFiles))
|
||||
} else {
|
||||
C.vips_cache_set_max_files(defaultMaxCacheFiles)
|
||||
}
|
||||
|
||||
if config.MaxCacheMem >= 0 {
|
||||
C.vips_cache_set_max_mem(C.size_t(config.MaxCacheMem))
|
||||
} else {
|
||||
C.vips_cache_set_max_mem(defaultMaxCacheMem)
|
||||
}
|
||||
|
||||
if config.MaxCacheSize >= 0 {
|
||||
C.vips_cache_set_max(C.int(config.MaxCacheSize))
|
||||
} else {
|
||||
C.vips_cache_set_max(defaultMaxCacheSize)
|
||||
}
|
||||
|
||||
C.vips_cache_set_trace(toGboolean(config.CacheTrace))
|
||||
} else {
|
||||
C.vips_concurrency_set(defaultConcurrencyLevel)
|
||||
C.vips_cache_set_max(defaultMaxCacheSize)
|
||||
C.vips_cache_set_max_mem(defaultMaxCacheMem)
|
||||
C.vips_cache_set_max_files(defaultMaxCacheFiles)
|
||||
C.vips_cache_set_trace(toGboolean(false))
|
||||
}
|
||||
|
||||
govipsLog("govips", LogLevelInfo, fmt.Sprintf("vips %s started with concurrency=%d cache_max_files=%d cache_max_mem=%d cache_max=%d",
|
||||
Version,
|
||||
int(C.vips_concurrency_get()),
|
||||
int(C.vips_cache_get_max_files()),
|
||||
int(C.vips_cache_get_max_mem()),
|
||||
int(C.vips_cache_get_max())))
|
||||
|
||||
initTypes()
|
||||
return nil
|
||||
}
|
||||
|
||||
func enableLogging() {
|
||||
C.vips_set_logging_handler()
|
||||
}
|
||||
|
||||
func disableLogging() {
|
||||
C.vips_unset_logging_handler()
|
||||
}
|
||||
|
||||
// consoleLogging overrides the Govips logging handler and makes glib
|
||||
// use its default logging handler which outputs everything to console.
|
||||
// Needed for CI unit testing due to a macOS bug in Go (doesn't clean cgo callbacks on exit)
|
||||
func consoleLogging() {
|
||||
C.vips_default_logging_handler()
|
||||
}
|
||||
|
||||
// Shutdown libvips
|
||||
func Shutdown() {
|
||||
hasShutdown = true
|
||||
|
||||
if statCollectorDone != nil {
|
||||
statCollectorDone <- struct{}{}
|
||||
}
|
||||
|
||||
initLock.Lock()
|
||||
defer initLock.Unlock()
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
if !running {
|
||||
govipsLog("govips", LogLevelInfo, "warning libvips not started")
|
||||
return
|
||||
}
|
||||
|
||||
if temporaryDirectory != "" {
|
||||
os.RemoveAll(temporaryDirectory)
|
||||
}
|
||||
|
||||
C.vips_shutdown()
|
||||
disableLogging()
|
||||
running = false
|
||||
}
|
||||
|
||||
// ShutdownThread clears the cache for for the given thread. This needs to be
|
||||
// called when a thread using vips exits.
|
||||
func ShutdownThread() {
|
||||
C.vips_thread_shutdown()
|
||||
}
|
||||
|
||||
// ClearCache drops the whole operation cache, handy for leak tracking.
|
||||
func ClearCache() {
|
||||
C.vips_cache_drop_all()
|
||||
}
|
||||
|
||||
// SetPipeReadLimit sets the maximum number of bytes libvips will buffer
|
||||
// when loading from a non-seekable source (a reader without io.Seeker
|
||||
// passed to LoadImageFromReader). The default is approximately 1 GB.
|
||||
// Callers processing large non-seekable streams can lower this to bound
|
||||
// memory usage.
|
||||
//
|
||||
// Must be called before any streaming load operations.
|
||||
func SetPipeReadLimit(bytes int64) {
|
||||
C.vips_pipe_read_limit_set(C.gint64(bytes))
|
||||
}
|
||||
|
||||
// PrintCache prints the whole operation cache to stdout for debugging purposes.
|
||||
func PrintCache() {
|
||||
C.vips_cache_print()
|
||||
}
|
||||
|
||||
// PrintObjectReport outputs all of the current internal objects in libvips
|
||||
func PrintObjectReport(label string) {
|
||||
govipsLog("govips", LogLevelInfo, fmt.Sprintf("\n=======================================\nvips live objects: %s...\n", label))
|
||||
C.vips_object_print_all()
|
||||
govipsLog("govips", LogLevelInfo, "=======================================\n\n")
|
||||
}
|
||||
|
||||
// MemoryStats is a data structure that houses various memory statistics from ReadVipsMemStats()
|
||||
type MemoryStats struct {
|
||||
Mem int64
|
||||
MemHigh int64
|
||||
Files int64
|
||||
Allocs int64
|
||||
}
|
||||
|
||||
var openImageRefs atomic.Int64
|
||||
|
||||
// OpenImageRefs returns the number of ImageRef instances that haven't been closed.
|
||||
func OpenImageRefs() int64 {
|
||||
return openImageRefs.Load()
|
||||
}
|
||||
|
||||
// AssertNoLeaks fails the test if any ImageRef instances remain open.
|
||||
// Call this at the end of tests to catch leaked images.
|
||||
func AssertNoLeaks(t testing.TB) {
|
||||
t.Helper()
|
||||
n := openImageRefs.Load()
|
||||
if n != 0 {
|
||||
t.Errorf("govips: %d ImageRef(s) not closed (leaked)", n)
|
||||
}
|
||||
}
|
||||
|
||||
// ReadVipsMemStats returns various memory statistics such as allocated memory and open files.
|
||||
func ReadVipsMemStats(stats *MemoryStats) {
|
||||
stats.Mem = int64(C.vips_tracked_get_mem())
|
||||
stats.MemHigh = int64(C.vips_tracked_get_mem_highwater())
|
||||
stats.Allocs = int64(C.vips_tracked_get_allocs())
|
||||
stats.Files = int64(C.vips_tracked_get_files())
|
||||
}
|
||||
|
||||
func startupIfNeeded() error {
|
||||
if !running {
|
||||
govipsLog("govips", LogLevelInfo, "libvips was forcibly started automatically, consider calling Startup/Shutdown yourself")
|
||||
if err := Startup(nil); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// InitTypes initializes caches and figures out which image types are supported
|
||||
func initTypes() {
|
||||
once.Do(func() {
|
||||
cType := C.CString("VipsOperation")
|
||||
defer freeCString(cType)
|
||||
|
||||
for k, v := range ImageTypes {
|
||||
name := strings.ToLower("VipsForeignLoad" + v)
|
||||
typeLoaders[name] = k
|
||||
typeLoaders[name+"buffer"] = k
|
||||
|
||||
cFunc := C.CString(v + "load")
|
||||
//noinspection GoDeferInLoop
|
||||
defer freeCString(cFunc)
|
||||
|
||||
ret := C.vips_type_find(cType, cFunc)
|
||||
|
||||
supportedImageTypes[k] = int(ret) != 0
|
||||
|
||||
if supportedImageTypes[k] {
|
||||
govipsLog("govips", LogLevelInfo, fmt.Sprintf("registered image type loader type=%s", v))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
|
||||
// clang-format off
|
||||
// include order matters
|
||||
#include <stdlib.h>
|
||||
#include <glib.h>
|
||||
#include <vips/vips.h>
|
||||
// clang-format on
|
||||
|
||||
#if (VIPS_MAJOR_VERSION < 8)
|
||||
error_requires_version_8
|
||||
#endif
|
||||
|
||||
extern void
|
||||
govipsLoggingHandler(char *log_domain, int log_level, char *message);
|
||||
|
||||
static void govips_logging_handler(const gchar *log_domain,
|
||||
GLogLevelFlags log_level,
|
||||
const gchar *message, gpointer user_data);
|
||||
|
||||
static void null_logging_handler(const gchar *log_domain,
|
||||
GLogLevelFlags log_level, const gchar *message,
|
||||
gpointer user_data);
|
||||
|
||||
void vips_set_logging_handler(void);
|
||||
void vips_unset_logging_handler(void);
|
||||
void vips_default_logging_handler(void);
|
||||
@@ -1,724 +0,0 @@
|
||||
package vips
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
// ATTRIBUTION:
|
||||
// The following micro icc profile taken from: https://github.com/saucecontrol/Compact-ICC-Profiles.
|
||||
// Read more (very interesting): https://photosauce.net/blog/post/making-a-minimal-srgb-icc-profile-part-1-trim-the-fat-abuse-the-spec
|
||||
sRGBV2MicroICCProfile = []byte{
|
||||
0x00, 0x00, 0x01, 0xc8, 0x6c, 0x63, 0x6d, 0x73, 0x02, 0x10, 0x00, 0x00,
|
||||
0x6d, 0x6e, 0x74, 0x72, 0x52, 0x47, 0x42, 0x20, 0x58, 0x59, 0x5a, 0x20,
|
||||
0x07, 0xe2, 0x00, 0x03, 0x00, 0x14, 0x00, 0x09, 0x00, 0x0e, 0x00, 0x1d,
|
||||
0x61, 0x63, 0x73, 0x70, 0x4d, 0x53, 0x46, 0x54, 0x00, 0x00, 0x00, 0x00,
|
||||
0x73, 0x61, 0x77, 0x73, 0x63, 0x74, 0x72, 0x6c, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf6, 0xd6,
|
||||
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x2d, 0x68, 0x61, 0x6e, 0x64,
|
||||
0x9d, 0x91, 0x00, 0x3d, 0x40, 0x80, 0xb0, 0x3d, 0x40, 0x74, 0x2c, 0x81,
|
||||
0x9e, 0xa5, 0x22, 0x8e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09,
|
||||
0x64, 0x65, 0x73, 0x63, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x5f,
|
||||
0x63, 0x70, 0x72, 0x74, 0x00, 0x00, 0x01, 0x0c, 0x00, 0x00, 0x00, 0x0c,
|
||||
0x77, 0x74, 0x70, 0x74, 0x00, 0x00, 0x01, 0x18, 0x00, 0x00, 0x00, 0x14,
|
||||
0x72, 0x58, 0x59, 0x5a, 0x00, 0x00, 0x01, 0x2c, 0x00, 0x00, 0x00, 0x14,
|
||||
0x67, 0x58, 0x59, 0x5a, 0x00, 0x00, 0x01, 0x40, 0x00, 0x00, 0x00, 0x14,
|
||||
0x62, 0x58, 0x59, 0x5a, 0x00, 0x00, 0x01, 0x54, 0x00, 0x00, 0x00, 0x14,
|
||||
0x72, 0x54, 0x52, 0x43, 0x00, 0x00, 0x01, 0x68, 0x00, 0x00, 0x00, 0x60,
|
||||
0x67, 0x54, 0x52, 0x43, 0x00, 0x00, 0x01, 0x68, 0x00, 0x00, 0x00, 0x60,
|
||||
0x62, 0x54, 0x52, 0x43, 0x00, 0x00, 0x01, 0x68, 0x00, 0x00, 0x00, 0x60,
|
||||
0x64, 0x65, 0x73, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
|
||||
0x75, 0x52, 0x47, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x74, 0x65, 0x78, 0x74, 0x00, 0x00, 0x00, 0x00,
|
||||
0x43, 0x43, 0x30, 0x00, 0x58, 0x59, 0x5a, 0x20, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0xf3, 0x54, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x16, 0xc9,
|
||||
0x58, 0x59, 0x5a, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6f, 0xa0,
|
||||
0x00, 0x00, 0x38, 0xf2, 0x00, 0x00, 0x03, 0x8f, 0x58, 0x59, 0x5a, 0x20,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x96, 0x00, 0x00, 0xb7, 0x89,
|
||||
0x00, 0x00, 0x18, 0xda, 0x58, 0x59, 0x5a, 0x20, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x24, 0xa0, 0x00, 0x00, 0x0f, 0x85, 0x00, 0x00, 0xb6, 0xc4,
|
||||
0x63, 0x75, 0x72, 0x76, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a,
|
||||
0x00, 0x00, 0x00, 0x7c, 0x00, 0xf8, 0x01, 0x9c, 0x02, 0x75, 0x03, 0x83,
|
||||
0x04, 0xc9, 0x06, 0x4e, 0x08, 0x12, 0x0a, 0x18, 0x0c, 0x62, 0x0e, 0xf4,
|
||||
0x11, 0xcf, 0x14, 0xf6, 0x18, 0x6a, 0x1c, 0x2e, 0x20, 0x43, 0x24, 0xac,
|
||||
0x29, 0x6a, 0x2e, 0x7e, 0x33, 0xeb, 0x39, 0xb3, 0x3f, 0xd6, 0x46, 0x57,
|
||||
0x4d, 0x36, 0x54, 0x76, 0x5c, 0x17, 0x64, 0x1d, 0x6c, 0x86, 0x75, 0x56,
|
||||
0x7e, 0x8d, 0x88, 0x2c, 0x92, 0x36, 0x9c, 0xab, 0xa7, 0x8c, 0xb2, 0xdb,
|
||||
0xbe, 0x99, 0xca, 0xc7, 0xd7, 0x65, 0xe4, 0x77, 0xf1, 0xf9, 0xff, 0xff,
|
||||
}
|
||||
|
||||
// ATTRIBUTION:
|
||||
// The following micro icc profile taken from: https://github.com/saucecontrol/Compact-ICC-Profiles.
|
||||
// Read more (very interesting): https://photosauce.net/blog/post/making-a-minimal-srgb-icc-profile-part-1-trim-the-fat-abuse-the-spec
|
||||
sGrayV2MicroICCProfile = []byte{
|
||||
0x00, 0x00, 0x01, 0x50, 0x6c, 0x63, 0x6d, 0x73, 0x02, 0x10, 0x00, 0x00,
|
||||
0x6d, 0x6e, 0x74, 0x72, 0x47, 0x52, 0x41, 0x59, 0x58, 0x59, 0x5a, 0x20,
|
||||
0x07, 0xe2, 0x00, 0x03, 0x00, 0x14, 0x00, 0x09, 0x00, 0x0e, 0x00, 0x1d,
|
||||
0x61, 0x63, 0x73, 0x70, 0x4d, 0x53, 0x46, 0x54, 0x00, 0x00, 0x00, 0x00,
|
||||
0x73, 0x61, 0x77, 0x73, 0x63, 0x74, 0x72, 0x6c, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf6, 0xd6,
|
||||
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x2d, 0x68, 0x61, 0x6e, 0x64,
|
||||
0x05, 0xd2, 0x02, 0xa7, 0xf9, 0xdd, 0x47, 0x94, 0xc7, 0x4f, 0x4c, 0x5f,
|
||||
0x26, 0x82, 0x3a, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
|
||||
0x64, 0x65, 0x73, 0x63, 0x00, 0x00, 0x00, 0xb4, 0x00, 0x00, 0x00, 0x5f,
|
||||
0x63, 0x70, 0x72, 0x74, 0x00, 0x00, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x0c,
|
||||
0x77, 0x74, 0x70, 0x74, 0x00, 0x00, 0x00, 0xdc, 0x00, 0x00, 0x00, 0x14,
|
||||
0x6b, 0x54, 0x52, 0x43, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x60,
|
||||
0x64, 0x65, 0x73, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
|
||||
0x75, 0x47, 0x72, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x74, 0x65, 0x78, 0x74, 0x00, 0x00, 0x00, 0x00,
|
||||
0x43, 0x43, 0x30, 0x00, 0x58, 0x59, 0x5a, 0x20, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0xf3, 0x54, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x16, 0xc9,
|
||||
0x63, 0x75, 0x72, 0x76, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a,
|
||||
0x00, 0x00, 0x00, 0x7c, 0x00, 0xf8, 0x01, 0x9c, 0x02, 0x75, 0x03, 0x83,
|
||||
0x04, 0xc9, 0x06, 0x4e, 0x08, 0x12, 0x0a, 0x18, 0x0c, 0x62, 0x0e, 0xf4,
|
||||
0x11, 0xcf, 0x14, 0xf6, 0x18, 0x6a, 0x1c, 0x2e, 0x20, 0x43, 0x24, 0xac,
|
||||
0x29, 0x6a, 0x2e, 0x7e, 0x33, 0xeb, 0x39, 0xb3, 0x3f, 0xd6, 0x46, 0x57,
|
||||
0x4d, 0x36, 0x54, 0x76, 0x5c, 0x17, 0x64, 0x1d, 0x6c, 0x86, 0x75, 0x56,
|
||||
0x7e, 0x8d, 0x88, 0x2c, 0x92, 0x36, 0x9c, 0xab, 0xa7, 0x8c, 0xb2, 0xdb,
|
||||
0xbe, 0x99, 0xca, 0xc7, 0xd7, 0x65, 0xe4, 0x77, 0xf1, 0xf9, 0xff, 0xff,
|
||||
}
|
||||
|
||||
sRGBIEC6196621ICCProfile = []byte{
|
||||
0x00, 0x00, 0x0b, 0xe8, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
|
||||
0x6d, 0x6e, 0x74, 0x72, 0x52, 0x47, 0x42, 0x20, 0x58, 0x59, 0x5a, 0x20,
|
||||
0x07, 0xd9, 0x00, 0x03, 0x00, 0x1b, 0x00, 0x15, 0x00, 0x24, 0x00, 0x1f,
|
||||
0x61, 0x63, 0x73, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf6, 0xd6,
|
||||
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x2d, 0x00, 0x00, 0x00, 0x00,
|
||||
0x29, 0xf8, 0x3d, 0xde, 0xaf, 0xf2, 0x55, 0xae, 0x78, 0x42, 0xfa, 0xe4,
|
||||
0xca, 0x83, 0x39, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
|
||||
0x64, 0x65, 0x73, 0x63, 0x00, 0x00, 0x01, 0x44, 0x00, 0x00, 0x00, 0x79,
|
||||
0x62, 0x58, 0x59, 0x5a, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x14,
|
||||
0x62, 0x54, 0x52, 0x43, 0x00, 0x00, 0x01, 0xd4, 0x00, 0x00, 0x08, 0x0c,
|
||||
0x64, 0x6d, 0x64, 0x64, 0x00, 0x00, 0x09, 0xe0, 0x00, 0x00, 0x00, 0x88,
|
||||
0x67, 0x58, 0x59, 0x5a, 0x00, 0x00, 0x0a, 0x68, 0x00, 0x00, 0x00, 0x14,
|
||||
0x67, 0x54, 0x52, 0x43, 0x00, 0x00, 0x01, 0xd4, 0x00, 0x00, 0x08, 0x0c,
|
||||
0x6c, 0x75, 0x6d, 0x69, 0x00, 0x00, 0x0a, 0x7c, 0x00, 0x00, 0x00, 0x14,
|
||||
0x6d, 0x65, 0x61, 0x73, 0x00, 0x00, 0x0a, 0x90, 0x00, 0x00, 0x00, 0x24,
|
||||
0x62, 0x6b, 0x70, 0x74, 0x00, 0x00, 0x0a, 0xb4, 0x00, 0x00, 0x00, 0x14,
|
||||
0x72, 0x58, 0x59, 0x5a, 0x00, 0x00, 0x0a, 0xc8, 0x00, 0x00, 0x00, 0x14,
|
||||
0x72, 0x54, 0x52, 0x43, 0x00, 0x00, 0x01, 0xd4, 0x00, 0x00, 0x08, 0x0c,
|
||||
0x74, 0x65, 0x63, 0x68, 0x00, 0x00, 0x0a, 0xdc, 0x00, 0x00, 0x00, 0x0c,
|
||||
0x76, 0x75, 0x65, 0x64, 0x00, 0x00, 0x0a, 0xe8, 0x00, 0x00, 0x00, 0x87,
|
||||
0x77, 0x74, 0x70, 0x74, 0x00, 0x00, 0x0b, 0x70, 0x00, 0x00, 0x00, 0x14,
|
||||
0x63, 0x70, 0x72, 0x74, 0x00, 0x00, 0x0b, 0x84, 0x00, 0x00, 0x00, 0x37,
|
||||
0x63, 0x68, 0x61, 0x64, 0x00, 0x00, 0x0b, 0xbc, 0x00, 0x00, 0x00, 0x2c,
|
||||
0x64, 0x65, 0x73, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f,
|
||||
0x73, 0x52, 0x47, 0x42, 0x20, 0x49, 0x45, 0x43, 0x36, 0x31, 0x39, 0x36,
|
||||
0x36, 0x2d, 0x32, 0x2d, 0x31, 0x20, 0x62, 0x6c, 0x61, 0x63, 0x6b, 0x20,
|
||||
0x73, 0x63, 0x61, 0x6c, 0x65, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x58, 0x59, 0x5a, 0x20, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x24, 0xa0, 0x00, 0x00, 0x0f, 0x84, 0x00, 0x00, 0xb6, 0xcf,
|
||||
0x63, 0x75, 0x72, 0x76, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
|
||||
0x00, 0x00, 0x00, 0x05, 0x00, 0x0a, 0x00, 0x0f, 0x00, 0x14, 0x00, 0x19,
|
||||
0x00, 0x1e, 0x00, 0x23, 0x00, 0x28, 0x00, 0x2d, 0x00, 0x32, 0x00, 0x37,
|
||||
0x00, 0x3b, 0x00, 0x40, 0x00, 0x45, 0x00, 0x4a, 0x00, 0x4f, 0x00, 0x54,
|
||||
0x00, 0x59, 0x00, 0x5e, 0x00, 0x63, 0x00, 0x68, 0x00, 0x6d, 0x00, 0x72,
|
||||
0x00, 0x77, 0x00, 0x7c, 0x00, 0x81, 0x00, 0x86, 0x00, 0x8b, 0x00, 0x90,
|
||||
0x00, 0x95, 0x00, 0x9a, 0x00, 0x9f, 0x00, 0xa4, 0x00, 0xa9, 0x00, 0xae,
|
||||
0x00, 0xb2, 0x00, 0xb7, 0x00, 0xbc, 0x00, 0xc1, 0x00, 0xc6, 0x00, 0xcb,
|
||||
0x00, 0xd0, 0x00, 0xd5, 0x00, 0xdb, 0x00, 0xe0, 0x00, 0xe5, 0x00, 0xeb,
|
||||
0x00, 0xf0, 0x00, 0xf6, 0x00, 0xfb, 0x01, 0x01, 0x01, 0x07, 0x01, 0x0d,
|
||||
0x01, 0x13, 0x01, 0x19, 0x01, 0x1f, 0x01, 0x25, 0x01, 0x2b, 0x01, 0x32,
|
||||
0x01, 0x38, 0x01, 0x3e, 0x01, 0x45, 0x01, 0x4c, 0x01, 0x52, 0x01, 0x59,
|
||||
0x01, 0x60, 0x01, 0x67, 0x01, 0x6e, 0x01, 0x75, 0x01, 0x7c, 0x01, 0x83,
|
||||
0x01, 0x8b, 0x01, 0x92, 0x01, 0x9a, 0x01, 0xa1, 0x01, 0xa9, 0x01, 0xb1,
|
||||
0x01, 0xb9, 0x01, 0xc1, 0x01, 0xc9, 0x01, 0xd1, 0x01, 0xd9, 0x01, 0xe1,
|
||||
0x01, 0xe9, 0x01, 0xf2, 0x01, 0xfa, 0x02, 0x03, 0x02, 0x0c, 0x02, 0x14,
|
||||
0x02, 0x1d, 0x02, 0x26, 0x02, 0x2f, 0x02, 0x38, 0x02, 0x41, 0x02, 0x4b,
|
||||
0x02, 0x54, 0x02, 0x5d, 0x02, 0x67, 0x02, 0x71, 0x02, 0x7a, 0x02, 0x84,
|
||||
0x02, 0x8e, 0x02, 0x98, 0x02, 0xa2, 0x02, 0xac, 0x02, 0xb6, 0x02, 0xc1,
|
||||
0x02, 0xcb, 0x02, 0xd5, 0x02, 0xe0, 0x02, 0xeb, 0x02, 0xf5, 0x03, 0x00,
|
||||
0x03, 0x0b, 0x03, 0x16, 0x03, 0x21, 0x03, 0x2d, 0x03, 0x38, 0x03, 0x43,
|
||||
0x03, 0x4f, 0x03, 0x5a, 0x03, 0x66, 0x03, 0x72, 0x03, 0x7e, 0x03, 0x8a,
|
||||
0x03, 0x96, 0x03, 0xa2, 0x03, 0xae, 0x03, 0xba, 0x03, 0xc7, 0x03, 0xd3,
|
||||
0x03, 0xe0, 0x03, 0xec, 0x03, 0xf9, 0x04, 0x06, 0x04, 0x13, 0x04, 0x20,
|
||||
0x04, 0x2d, 0x04, 0x3b, 0x04, 0x48, 0x04, 0x55, 0x04, 0x63, 0x04, 0x71,
|
||||
0x04, 0x7e, 0x04, 0x8c, 0x04, 0x9a, 0x04, 0xa8, 0x04, 0xb6, 0x04, 0xc4,
|
||||
0x04, 0xd3, 0x04, 0xe1, 0x04, 0xf0, 0x04, 0xfe, 0x05, 0x0d, 0x05, 0x1c,
|
||||
0x05, 0x2b, 0x05, 0x3a, 0x05, 0x49, 0x05, 0x58, 0x05, 0x67, 0x05, 0x77,
|
||||
0x05, 0x86, 0x05, 0x96, 0x05, 0xa6, 0x05, 0xb5, 0x05, 0xc5, 0x05, 0xd5,
|
||||
0x05, 0xe5, 0x05, 0xf6, 0x06, 0x06, 0x06, 0x16, 0x06, 0x27, 0x06, 0x37,
|
||||
0x06, 0x48, 0x06, 0x59, 0x06, 0x6a, 0x06, 0x7b, 0x06, 0x8c, 0x06, 0x9d,
|
||||
0x06, 0xaf, 0x06, 0xc0, 0x06, 0xd1, 0x06, 0xe3, 0x06, 0xf5, 0x07, 0x07,
|
||||
0x07, 0x19, 0x07, 0x2b, 0x07, 0x3d, 0x07, 0x4f, 0x07, 0x61, 0x07, 0x74,
|
||||
0x07, 0x86, 0x07, 0x99, 0x07, 0xac, 0x07, 0xbf, 0x07, 0xd2, 0x07, 0xe5,
|
||||
0x07, 0xf8, 0x08, 0x0b, 0x08, 0x1f, 0x08, 0x32, 0x08, 0x46, 0x08, 0x5a,
|
||||
0x08, 0x6e, 0x08, 0x82, 0x08, 0x96, 0x08, 0xaa, 0x08, 0xbe, 0x08, 0xd2,
|
||||
0x08, 0xe7, 0x08, 0xfb, 0x09, 0x10, 0x09, 0x25, 0x09, 0x3a, 0x09, 0x4f,
|
||||
0x09, 0x64, 0x09, 0x79, 0x09, 0x8f, 0x09, 0xa4, 0x09, 0xba, 0x09, 0xcf,
|
||||
0x09, 0xe5, 0x09, 0xfb, 0x0a, 0x11, 0x0a, 0x27, 0x0a, 0x3d, 0x0a, 0x54,
|
||||
0x0a, 0x6a, 0x0a, 0x81, 0x0a, 0x98, 0x0a, 0xae, 0x0a, 0xc5, 0x0a, 0xdc,
|
||||
0x0a, 0xf3, 0x0b, 0x0b, 0x0b, 0x22, 0x0b, 0x39, 0x0b, 0x51, 0x0b, 0x69,
|
||||
0x0b, 0x80, 0x0b, 0x98, 0x0b, 0xb0, 0x0b, 0xc8, 0x0b, 0xe1, 0x0b, 0xf9,
|
||||
0x0c, 0x12, 0x0c, 0x2a, 0x0c, 0x43, 0x0c, 0x5c, 0x0c, 0x75, 0x0c, 0x8e,
|
||||
0x0c, 0xa7, 0x0c, 0xc0, 0x0c, 0xd9, 0x0c, 0xf3, 0x0d, 0x0d, 0x0d, 0x26,
|
||||
0x0d, 0x40, 0x0d, 0x5a, 0x0d, 0x74, 0x0d, 0x8e, 0x0d, 0xa9, 0x0d, 0xc3,
|
||||
0x0d, 0xde, 0x0d, 0xf8, 0x0e, 0x13, 0x0e, 0x2e, 0x0e, 0x49, 0x0e, 0x64,
|
||||
0x0e, 0x7f, 0x0e, 0x9b, 0x0e, 0xb6, 0x0e, 0xd2, 0x0e, 0xee, 0x0f, 0x09,
|
||||
0x0f, 0x25, 0x0f, 0x41, 0x0f, 0x5e, 0x0f, 0x7a, 0x0f, 0x96, 0x0f, 0xb3,
|
||||
0x0f, 0xcf, 0x0f, 0xec, 0x10, 0x09, 0x10, 0x26, 0x10, 0x43, 0x10, 0x61,
|
||||
0x10, 0x7e, 0x10, 0x9b, 0x10, 0xb9, 0x10, 0xd7, 0x10, 0xf5, 0x11, 0x13,
|
||||
0x11, 0x31, 0x11, 0x4f, 0x11, 0x6d, 0x11, 0x8c, 0x11, 0xaa, 0x11, 0xc9,
|
||||
0x11, 0xe8, 0x12, 0x07, 0x12, 0x26, 0x12, 0x45, 0x12, 0x64, 0x12, 0x84,
|
||||
0x12, 0xa3, 0x12, 0xc3, 0x12, 0xe3, 0x13, 0x03, 0x13, 0x23, 0x13, 0x43,
|
||||
0x13, 0x63, 0x13, 0x83, 0x13, 0xa4, 0x13, 0xc5, 0x13, 0xe5, 0x14, 0x06,
|
||||
0x14, 0x27, 0x14, 0x49, 0x14, 0x6a, 0x14, 0x8b, 0x14, 0xad, 0x14, 0xce,
|
||||
0x14, 0xf0, 0x15, 0x12, 0x15, 0x34, 0x15, 0x56, 0x15, 0x78, 0x15, 0x9b,
|
||||
0x15, 0xbd, 0x15, 0xe0, 0x16, 0x03, 0x16, 0x26, 0x16, 0x49, 0x16, 0x6c,
|
||||
0x16, 0x8f, 0x16, 0xb2, 0x16, 0xd6, 0x16, 0xfa, 0x17, 0x1d, 0x17, 0x41,
|
||||
0x17, 0x65, 0x17, 0x89, 0x17, 0xae, 0x17, 0xd2, 0x17, 0xf7, 0x18, 0x1b,
|
||||
0x18, 0x40, 0x18, 0x65, 0x18, 0x8a, 0x18, 0xaf, 0x18, 0xd5, 0x18, 0xfa,
|
||||
0x19, 0x20, 0x19, 0x45, 0x19, 0x6b, 0x19, 0x91, 0x19, 0xb7, 0x19, 0xdd,
|
||||
0x1a, 0x04, 0x1a, 0x2a, 0x1a, 0x51, 0x1a, 0x77, 0x1a, 0x9e, 0x1a, 0xc5,
|
||||
0x1a, 0xec, 0x1b, 0x14, 0x1b, 0x3b, 0x1b, 0x63, 0x1b, 0x8a, 0x1b, 0xb2,
|
||||
0x1b, 0xda, 0x1c, 0x02, 0x1c, 0x2a, 0x1c, 0x52, 0x1c, 0x7b, 0x1c, 0xa3,
|
||||
0x1c, 0xcc, 0x1c, 0xf5, 0x1d, 0x1e, 0x1d, 0x47, 0x1d, 0x70, 0x1d, 0x99,
|
||||
0x1d, 0xc3, 0x1d, 0xec, 0x1e, 0x16, 0x1e, 0x40, 0x1e, 0x6a, 0x1e, 0x94,
|
||||
0x1e, 0xbe, 0x1e, 0xe9, 0x1f, 0x13, 0x1f, 0x3e, 0x1f, 0x69, 0x1f, 0x94,
|
||||
0x1f, 0xbf, 0x1f, 0xea, 0x20, 0x15, 0x20, 0x41, 0x20, 0x6c, 0x20, 0x98,
|
||||
0x20, 0xc4, 0x20, 0xf0, 0x21, 0x1c, 0x21, 0x48, 0x21, 0x75, 0x21, 0xa1,
|
||||
0x21, 0xce, 0x21, 0xfb, 0x22, 0x27, 0x22, 0x55, 0x22, 0x82, 0x22, 0xaf,
|
||||
0x22, 0xdd, 0x23, 0x0a, 0x23, 0x38, 0x23, 0x66, 0x23, 0x94, 0x23, 0xc2,
|
||||
0x23, 0xf0, 0x24, 0x1f, 0x24, 0x4d, 0x24, 0x7c, 0x24, 0xab, 0x24, 0xda,
|
||||
0x25, 0x09, 0x25, 0x38, 0x25, 0x68, 0x25, 0x97, 0x25, 0xc7, 0x25, 0xf7,
|
||||
0x26, 0x27, 0x26, 0x57, 0x26, 0x87, 0x26, 0xb7, 0x26, 0xe8, 0x27, 0x18,
|
||||
0x27, 0x49, 0x27, 0x7a, 0x27, 0xab, 0x27, 0xdc, 0x28, 0x0d, 0x28, 0x3f,
|
||||
0x28, 0x71, 0x28, 0xa2, 0x28, 0xd4, 0x29, 0x06, 0x29, 0x38, 0x29, 0x6b,
|
||||
0x29, 0x9d, 0x29, 0xd0, 0x2a, 0x02, 0x2a, 0x35, 0x2a, 0x68, 0x2a, 0x9b,
|
||||
0x2a, 0xcf, 0x2b, 0x02, 0x2b, 0x36, 0x2b, 0x69, 0x2b, 0x9d, 0x2b, 0xd1,
|
||||
0x2c, 0x05, 0x2c, 0x39, 0x2c, 0x6e, 0x2c, 0xa2, 0x2c, 0xd7, 0x2d, 0x0c,
|
||||
0x2d, 0x41, 0x2d, 0x76, 0x2d, 0xab, 0x2d, 0xe1, 0x2e, 0x16, 0x2e, 0x4c,
|
||||
0x2e, 0x82, 0x2e, 0xb7, 0x2e, 0xee, 0x2f, 0x24, 0x2f, 0x5a, 0x2f, 0x91,
|
||||
0x2f, 0xc7, 0x2f, 0xfe, 0x30, 0x35, 0x30, 0x6c, 0x30, 0xa4, 0x30, 0xdb,
|
||||
0x31, 0x12, 0x31, 0x4a, 0x31, 0x82, 0x31, 0xba, 0x31, 0xf2, 0x32, 0x2a,
|
||||
0x32, 0x63, 0x32, 0x9b, 0x32, 0xd4, 0x33, 0x0d, 0x33, 0x46, 0x33, 0x7f,
|
||||
0x33, 0xb8, 0x33, 0xf1, 0x34, 0x2b, 0x34, 0x65, 0x34, 0x9e, 0x34, 0xd8,
|
||||
0x35, 0x13, 0x35, 0x4d, 0x35, 0x87, 0x35, 0xc2, 0x35, 0xfd, 0x36, 0x37,
|
||||
0x36, 0x72, 0x36, 0xae, 0x36, 0xe9, 0x37, 0x24, 0x37, 0x60, 0x37, 0x9c,
|
||||
0x37, 0xd7, 0x38, 0x14, 0x38, 0x50, 0x38, 0x8c, 0x38, 0xc8, 0x39, 0x05,
|
||||
0x39, 0x42, 0x39, 0x7f, 0x39, 0xbc, 0x39, 0xf9, 0x3a, 0x36, 0x3a, 0x74,
|
||||
0x3a, 0xb2, 0x3a, 0xef, 0x3b, 0x2d, 0x3b, 0x6b, 0x3b, 0xaa, 0x3b, 0xe8,
|
||||
0x3c, 0x27, 0x3c, 0x65, 0x3c, 0xa4, 0x3c, 0xe3, 0x3d, 0x22, 0x3d, 0x61,
|
||||
0x3d, 0xa1, 0x3d, 0xe0, 0x3e, 0x20, 0x3e, 0x60, 0x3e, 0xa0, 0x3e, 0xe0,
|
||||
0x3f, 0x21, 0x3f, 0x61, 0x3f, 0xa2, 0x3f, 0xe2, 0x40, 0x23, 0x40, 0x64,
|
||||
0x40, 0xa6, 0x40, 0xe7, 0x41, 0x29, 0x41, 0x6a, 0x41, 0xac, 0x41, 0xee,
|
||||
0x42, 0x30, 0x42, 0x72, 0x42, 0xb5, 0x42, 0xf7, 0x43, 0x3a, 0x43, 0x7d,
|
||||
0x43, 0xc0, 0x44, 0x03, 0x44, 0x47, 0x44, 0x8a, 0x44, 0xce, 0x45, 0x12,
|
||||
0x45, 0x55, 0x45, 0x9a, 0x45, 0xde, 0x46, 0x22, 0x46, 0x67, 0x46, 0xab,
|
||||
0x46, 0xf0, 0x47, 0x35, 0x47, 0x7b, 0x47, 0xc0, 0x48, 0x05, 0x48, 0x4b,
|
||||
0x48, 0x91, 0x48, 0xd7, 0x49, 0x1d, 0x49, 0x63, 0x49, 0xa9, 0x49, 0xf0,
|
||||
0x4a, 0x37, 0x4a, 0x7d, 0x4a, 0xc4, 0x4b, 0x0c, 0x4b, 0x53, 0x4b, 0x9a,
|
||||
0x4b, 0xe2, 0x4c, 0x2a, 0x4c, 0x72, 0x4c, 0xba, 0x4d, 0x02, 0x4d, 0x4a,
|
||||
0x4d, 0x93, 0x4d, 0xdc, 0x4e, 0x25, 0x4e, 0x6e, 0x4e, 0xb7, 0x4f, 0x00,
|
||||
0x4f, 0x49, 0x4f, 0x93, 0x4f, 0xdd, 0x50, 0x27, 0x50, 0x71, 0x50, 0xbb,
|
||||
0x51, 0x06, 0x51, 0x50, 0x51, 0x9b, 0x51, 0xe6, 0x52, 0x31, 0x52, 0x7c,
|
||||
0x52, 0xc7, 0x53, 0x13, 0x53, 0x5f, 0x53, 0xaa, 0x53, 0xf6, 0x54, 0x42,
|
||||
0x54, 0x8f, 0x54, 0xdb, 0x55, 0x28, 0x55, 0x75, 0x55, 0xc2, 0x56, 0x0f,
|
||||
0x56, 0x5c, 0x56, 0xa9, 0x56, 0xf7, 0x57, 0x44, 0x57, 0x92, 0x57, 0xe0,
|
||||
0x58, 0x2f, 0x58, 0x7d, 0x58, 0xcb, 0x59, 0x1a, 0x59, 0x69, 0x59, 0xb8,
|
||||
0x5a, 0x07, 0x5a, 0x56, 0x5a, 0xa6, 0x5a, 0xf5, 0x5b, 0x45, 0x5b, 0x95,
|
||||
0x5b, 0xe5, 0x5c, 0x35, 0x5c, 0x86, 0x5c, 0xd6, 0x5d, 0x27, 0x5d, 0x78,
|
||||
0x5d, 0xc9, 0x5e, 0x1a, 0x5e, 0x6c, 0x5e, 0xbd, 0x5f, 0x0f, 0x5f, 0x61,
|
||||
0x5f, 0xb3, 0x60, 0x05, 0x60, 0x57, 0x60, 0xaa, 0x60, 0xfc, 0x61, 0x4f,
|
||||
0x61, 0xa2, 0x61, 0xf5, 0x62, 0x49, 0x62, 0x9c, 0x62, 0xf0, 0x63, 0x43,
|
||||
0x63, 0x97, 0x63, 0xeb, 0x64, 0x40, 0x64, 0x94, 0x64, 0xe9, 0x65, 0x3d,
|
||||
0x65, 0x92, 0x65, 0xe7, 0x66, 0x3d, 0x66, 0x92, 0x66, 0xe8, 0x67, 0x3d,
|
||||
0x67, 0x93, 0x67, 0xe9, 0x68, 0x3f, 0x68, 0x96, 0x68, 0xec, 0x69, 0x43,
|
||||
0x69, 0x9a, 0x69, 0xf1, 0x6a, 0x48, 0x6a, 0x9f, 0x6a, 0xf7, 0x6b, 0x4f,
|
||||
0x6b, 0xa7, 0x6b, 0xff, 0x6c, 0x57, 0x6c, 0xaf, 0x6d, 0x08, 0x6d, 0x60,
|
||||
0x6d, 0xb9, 0x6e, 0x12, 0x6e, 0x6b, 0x6e, 0xc4, 0x6f, 0x1e, 0x6f, 0x78,
|
||||
0x6f, 0xd1, 0x70, 0x2b, 0x70, 0x86, 0x70, 0xe0, 0x71, 0x3a, 0x71, 0x95,
|
||||
0x71, 0xf0, 0x72, 0x4b, 0x72, 0xa6, 0x73, 0x01, 0x73, 0x5d, 0x73, 0xb8,
|
||||
0x74, 0x14, 0x74, 0x70, 0x74, 0xcc, 0x75, 0x28, 0x75, 0x85, 0x75, 0xe1,
|
||||
0x76, 0x3e, 0x76, 0x9b, 0x76, 0xf8, 0x77, 0x56, 0x77, 0xb3, 0x78, 0x11,
|
||||
0x78, 0x6e, 0x78, 0xcc, 0x79, 0x2a, 0x79, 0x89, 0x79, 0xe7, 0x7a, 0x46,
|
||||
0x7a, 0xa5, 0x7b, 0x04, 0x7b, 0x63, 0x7b, 0xc2, 0x7c, 0x21, 0x7c, 0x81,
|
||||
0x7c, 0xe1, 0x7d, 0x41, 0x7d, 0xa1, 0x7e, 0x01, 0x7e, 0x62, 0x7e, 0xc2,
|
||||
0x7f, 0x23, 0x7f, 0x84, 0x7f, 0xe5, 0x80, 0x47, 0x80, 0xa8, 0x81, 0x0a,
|
||||
0x81, 0x6b, 0x81, 0xcd, 0x82, 0x30, 0x82, 0x92, 0x82, 0xf4, 0x83, 0x57,
|
||||
0x83, 0xba, 0x84, 0x1d, 0x84, 0x80, 0x84, 0xe3, 0x85, 0x47, 0x85, 0xab,
|
||||
0x86, 0x0e, 0x86, 0x72, 0x86, 0xd7, 0x87, 0x3b, 0x87, 0x9f, 0x88, 0x04,
|
||||
0x88, 0x69, 0x88, 0xce, 0x89, 0x33, 0x89, 0x99, 0x89, 0xfe, 0x8a, 0x64,
|
||||
0x8a, 0xca, 0x8b, 0x30, 0x8b, 0x96, 0x8b, 0xfc, 0x8c, 0x63, 0x8c, 0xca,
|
||||
0x8d, 0x31, 0x8d, 0x98, 0x8d, 0xff, 0x8e, 0x66, 0x8e, 0xce, 0x8f, 0x36,
|
||||
0x8f, 0x9e, 0x90, 0x06, 0x90, 0x6e, 0x90, 0xd6, 0x91, 0x3f, 0x91, 0xa8,
|
||||
0x92, 0x11, 0x92, 0x7a, 0x92, 0xe3, 0x93, 0x4d, 0x93, 0xb6, 0x94, 0x20,
|
||||
0x94, 0x8a, 0x94, 0xf4, 0x95, 0x5f, 0x95, 0xc9, 0x96, 0x34, 0x96, 0x9f,
|
||||
0x97, 0x0a, 0x97, 0x75, 0x97, 0xe0, 0x98, 0x4c, 0x98, 0xb8, 0x99, 0x24,
|
||||
0x99, 0x90, 0x99, 0xfc, 0x9a, 0x68, 0x9a, 0xd5, 0x9b, 0x42, 0x9b, 0xaf,
|
||||
0x9c, 0x1c, 0x9c, 0x89, 0x9c, 0xf7, 0x9d, 0x64, 0x9d, 0xd2, 0x9e, 0x40,
|
||||
0x9e, 0xae, 0x9f, 0x1d, 0x9f, 0x8b, 0x9f, 0xfa, 0xa0, 0x69, 0xa0, 0xd8,
|
||||
0xa1, 0x47, 0xa1, 0xb6, 0xa2, 0x26, 0xa2, 0x96, 0xa3, 0x06, 0xa3, 0x76,
|
||||
0xa3, 0xe6, 0xa4, 0x56, 0xa4, 0xc7, 0xa5, 0x38, 0xa5, 0xa9, 0xa6, 0x1a,
|
||||
0xa6, 0x8b, 0xa6, 0xfd, 0xa7, 0x6e, 0xa7, 0xe0, 0xa8, 0x52, 0xa8, 0xc4,
|
||||
0xa9, 0x37, 0xa9, 0xa9, 0xaa, 0x1c, 0xaa, 0x8f, 0xab, 0x02, 0xab, 0x75,
|
||||
0xab, 0xe9, 0xac, 0x5c, 0xac, 0xd0, 0xad, 0x44, 0xad, 0xb8, 0xae, 0x2d,
|
||||
0xae, 0xa1, 0xaf, 0x16, 0xaf, 0x8b, 0xb0, 0x00, 0xb0, 0x75, 0xb0, 0xea,
|
||||
0xb1, 0x60, 0xb1, 0xd6, 0xb2, 0x4b, 0xb2, 0xc2, 0xb3, 0x38, 0xb3, 0xae,
|
||||
0xb4, 0x25, 0xb4, 0x9c, 0xb5, 0x13, 0xb5, 0x8a, 0xb6, 0x01, 0xb6, 0x79,
|
||||
0xb6, 0xf0, 0xb7, 0x68, 0xb7, 0xe0, 0xb8, 0x59, 0xb8, 0xd1, 0xb9, 0x4a,
|
||||
0xb9, 0xc2, 0xba, 0x3b, 0xba, 0xb5, 0xbb, 0x2e, 0xbb, 0xa7, 0xbc, 0x21,
|
||||
0xbc, 0x9b, 0xbd, 0x15, 0xbd, 0x8f, 0xbe, 0x0a, 0xbe, 0x84, 0xbe, 0xff,
|
||||
0xbf, 0x7a, 0xbf, 0xf5, 0xc0, 0x70, 0xc0, 0xec, 0xc1, 0x67, 0xc1, 0xe3,
|
||||
0xc2, 0x5f, 0xc2, 0xdb, 0xc3, 0x58, 0xc3, 0xd4, 0xc4, 0x51, 0xc4, 0xce,
|
||||
0xc5, 0x4b, 0xc5, 0xc8, 0xc6, 0x46, 0xc6, 0xc3, 0xc7, 0x41, 0xc7, 0xbf,
|
||||
0xc8, 0x3d, 0xc8, 0xbc, 0xc9, 0x3a, 0xc9, 0xb9, 0xca, 0x38, 0xca, 0xb7,
|
||||
0xcb, 0x36, 0xcb, 0xb6, 0xcc, 0x35, 0xcc, 0xb5, 0xcd, 0x35, 0xcd, 0xb5,
|
||||
0xce, 0x36, 0xce, 0xb6, 0xcf, 0x37, 0xcf, 0xb8, 0xd0, 0x39, 0xd0, 0xba,
|
||||
0xd1, 0x3c, 0xd1, 0xbe, 0xd2, 0x3f, 0xd2, 0xc1, 0xd3, 0x44, 0xd3, 0xc6,
|
||||
0xd4, 0x49, 0xd4, 0xcb, 0xd5, 0x4e, 0xd5, 0xd1, 0xd6, 0x55, 0xd6, 0xd8,
|
||||
0xd7, 0x5c, 0xd7, 0xe0, 0xd8, 0x64, 0xd8, 0xe8, 0xd9, 0x6c, 0xd9, 0xf1,
|
||||
0xda, 0x76, 0xda, 0xfb, 0xdb, 0x80, 0xdc, 0x05, 0xdc, 0x8a, 0xdd, 0x10,
|
||||
0xdd, 0x96, 0xde, 0x1c, 0xde, 0xa2, 0xdf, 0x29, 0xdf, 0xaf, 0xe0, 0x36,
|
||||
0xe0, 0xbd, 0xe1, 0x44, 0xe1, 0xcc, 0xe2, 0x53, 0xe2, 0xdb, 0xe3, 0x63,
|
||||
0xe3, 0xeb, 0xe4, 0x73, 0xe4, 0xfc, 0xe5, 0x84, 0xe6, 0x0d, 0xe6, 0x96,
|
||||
0xe7, 0x1f, 0xe7, 0xa9, 0xe8, 0x32, 0xe8, 0xbc, 0xe9, 0x46, 0xe9, 0xd0,
|
||||
0xea, 0x5b, 0xea, 0xe5, 0xeb, 0x70, 0xeb, 0xfb, 0xec, 0x86, 0xed, 0x11,
|
||||
0xed, 0x9c, 0xee, 0x28, 0xee, 0xb4, 0xef, 0x40, 0xef, 0xcc, 0xf0, 0x58,
|
||||
0xf0, 0xe5, 0xf1, 0x72, 0xf1, 0xff, 0xf2, 0x8c, 0xf3, 0x19, 0xf3, 0xa7,
|
||||
0xf4, 0x34, 0xf4, 0xc2, 0xf5, 0x50, 0xf5, 0xde, 0xf6, 0x6d, 0xf6, 0xfb,
|
||||
0xf7, 0x8a, 0xf8, 0x19, 0xf8, 0xa8, 0xf9, 0x38, 0xf9, 0xc7, 0xfa, 0x57,
|
||||
0xfa, 0xe7, 0xfb, 0x77, 0xfc, 0x07, 0xfc, 0x98, 0xfd, 0x29, 0xfd, 0xba,
|
||||
0xfe, 0x4b, 0xfe, 0xdc, 0xff, 0x6d, 0xff, 0xff, 0x64, 0x65, 0x73, 0x63,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2e, 0x49, 0x45, 0x43, 0x20,
|
||||
0x36, 0x31, 0x39, 0x36, 0x36, 0x2d, 0x32, 0x2d, 0x31, 0x20, 0x44, 0x65,
|
||||
0x66, 0x61, 0x75, 0x6c, 0x74, 0x20, 0x52, 0x47, 0x42, 0x20, 0x43, 0x6f,
|
||||
0x6c, 0x6f, 0x75, 0x72, 0x20, 0x53, 0x70, 0x61, 0x63, 0x65, 0x20, 0x2d,
|
||||
0x20, 0x73, 0x52, 0x47, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x58, 0x59, 0x5a, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x99,
|
||||
0x00, 0x00, 0xb7, 0x85, 0x00, 0x00, 0x18, 0xda, 0x58, 0x59, 0x5a, 0x20,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x6d, 0x65, 0x61, 0x73, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x02, 0x58, 0x59, 0x5a, 0x20, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x03, 0x16, 0x00, 0x00, 0x03, 0x33, 0x00, 0x00, 0x02, 0xa4,
|
||||
0x58, 0x59, 0x5a, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6f, 0xa2,
|
||||
0x00, 0x00, 0x38, 0xf5, 0x00, 0x00, 0x03, 0x90, 0x73, 0x69, 0x67, 0x20,
|
||||
0x00, 0x00, 0x00, 0x00, 0x43, 0x52, 0x54, 0x20, 0x64, 0x65, 0x73, 0x63,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x52, 0x65, 0x66, 0x65,
|
||||
0x72, 0x65, 0x6e, 0x63, 0x65, 0x20, 0x56, 0x69, 0x65, 0x77, 0x69, 0x6e,
|
||||
0x67, 0x20, 0x43, 0x6f, 0x6e, 0x64, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x20,
|
||||
0x69, 0x6e, 0x20, 0x49, 0x45, 0x43, 0x20, 0x36, 0x31, 0x39, 0x36, 0x36,
|
||||
0x2d, 0x32, 0x2d, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x58, 0x59, 0x5a, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf6, 0xd6,
|
||||
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x2d, 0x74, 0x65, 0x78, 0x74,
|
||||
0x00, 0x00, 0x00, 0x00, 0x43, 0x6f, 0x70, 0x79, 0x72, 0x69, 0x67, 0x68,
|
||||
0x74, 0x20, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x74, 0x69, 0x6f,
|
||||
0x6e, 0x61, 0x6c, 0x20, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x43, 0x6f,
|
||||
0x6e, 0x73, 0x6f, 0x72, 0x74, 0x69, 0x75, 0x6d, 0x2c, 0x20, 0x32, 0x30,
|
||||
0x30, 0x39, 0x00, 0x00, 0x73, 0x66, 0x33, 0x32, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x01, 0x0c, 0x44, 0x00, 0x00, 0x05, 0xdf, 0xff, 0xff, 0xf3, 0x26,
|
||||
0x00, 0x00, 0x07, 0x94, 0x00, 0x00, 0xfd, 0x8f, 0xff, 0xff, 0xfb, 0xa1,
|
||||
0xff, 0xff, 0xfd, 0xa2, 0x00, 0x00, 0x03, 0xdb, 0x00, 0x00, 0xc0, 0x75,
|
||||
}
|
||||
|
||||
genericGrayGamma22ICCProfile = []byte{
|
||||
0x00, 0x00, 0x0e, 0x04, 0x61, 0x70, 0x70, 0x6c, 0x02, 0x00, 0x00, 0x00,
|
||||
0x6d, 0x6e, 0x74, 0x72, 0x47, 0x52, 0x41, 0x59, 0x58, 0x59, 0x5a, 0x20,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x61, 0x63, 0x73, 0x70, 0x41, 0x50, 0x50, 0x4c, 0x00, 0x00, 0x00, 0x00,
|
||||
0x6e, 0x6f, 0x6e, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf6, 0xd6,
|
||||
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x2d, 0x61, 0x70, 0x70, 0x6c,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
|
||||
0x6b, 0x54, 0x52, 0x43, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x08, 0x0c,
|
||||
0x77, 0x74, 0x70, 0x74, 0x00, 0x00, 0x08, 0xcc, 0x00, 0x00, 0x00, 0x14,
|
||||
0x63, 0x70, 0x72, 0x74, 0x00, 0x00, 0x08, 0xe0, 0x00, 0x00, 0x00, 0x23,
|
||||
0x64, 0x65, 0x73, 0x63, 0x00, 0x00, 0x09, 0x04, 0x00, 0x00, 0x00, 0x79,
|
||||
0x64, 0x73, 0x63, 0x6d, 0x00, 0x00, 0x09, 0x80, 0x00, 0x00, 0x04, 0x82,
|
||||
0x63, 0x75, 0x72, 0x76, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
|
||||
0x00, 0x00, 0x00, 0x05, 0x00, 0x0a, 0x00, 0x0f, 0x00, 0x14, 0x00, 0x19,
|
||||
0x00, 0x1e, 0x00, 0x23, 0x00, 0x28, 0x00, 0x2d, 0x00, 0x32, 0x00, 0x37,
|
||||
0x00, 0x3b, 0x00, 0x40, 0x00, 0x45, 0x00, 0x4a, 0x00, 0x4f, 0x00, 0x54,
|
||||
0x00, 0x59, 0x00, 0x5e, 0x00, 0x63, 0x00, 0x68, 0x00, 0x6d, 0x00, 0x72,
|
||||
0x00, 0x77, 0x00, 0x7c, 0x00, 0x81, 0x00, 0x86, 0x00, 0x8b, 0x00, 0x90,
|
||||
0x00, 0x95, 0x00, 0x9a, 0x00, 0x9f, 0x00, 0xa4, 0x00, 0xa9, 0x00, 0xae,
|
||||
0x00, 0xb2, 0x00, 0xb7, 0x00, 0xbc, 0x00, 0xc1, 0x00, 0xc6, 0x00, 0xcb,
|
||||
0x00, 0xd0, 0x00, 0xd5, 0x00, 0xdb, 0x00, 0xe0, 0x00, 0xe5, 0x00, 0xeb,
|
||||
0x00, 0xf0, 0x00, 0xf6, 0x00, 0xfb, 0x01, 0x01, 0x01, 0x07, 0x01, 0x0d,
|
||||
0x01, 0x13, 0x01, 0x19, 0x01, 0x1f, 0x01, 0x25, 0x01, 0x2b, 0x01, 0x32,
|
||||
0x01, 0x38, 0x01, 0x3e, 0x01, 0x45, 0x01, 0x4c, 0x01, 0x52, 0x01, 0x59,
|
||||
0x01, 0x60, 0x01, 0x67, 0x01, 0x6e, 0x01, 0x75, 0x01, 0x7c, 0x01, 0x83,
|
||||
0x01, 0x8b, 0x01, 0x92, 0x01, 0x9a, 0x01, 0xa1, 0x01, 0xa9, 0x01, 0xb1,
|
||||
0x01, 0xb9, 0x01, 0xc1, 0x01, 0xc9, 0x01, 0xd1, 0x01, 0xd9, 0x01, 0xe1,
|
||||
0x01, 0xe9, 0x01, 0xf2, 0x01, 0xfa, 0x02, 0x03, 0x02, 0x0c, 0x02, 0x14,
|
||||
0x02, 0x1d, 0x02, 0x26, 0x02, 0x2f, 0x02, 0x38, 0x02, 0x41, 0x02, 0x4b,
|
||||
0x02, 0x54, 0x02, 0x5d, 0x02, 0x67, 0x02, 0x71, 0x02, 0x7a, 0x02, 0x84,
|
||||
0x02, 0x8e, 0x02, 0x98, 0x02, 0xa2, 0x02, 0xac, 0x02, 0xb6, 0x02, 0xc1,
|
||||
0x02, 0xcb, 0x02, 0xd5, 0x02, 0xe0, 0x02, 0xeb, 0x02, 0xf5, 0x03, 0x00,
|
||||
0x03, 0x0b, 0x03, 0x16, 0x03, 0x21, 0x03, 0x2d, 0x03, 0x38, 0x03, 0x43,
|
||||
0x03, 0x4f, 0x03, 0x5a, 0x03, 0x66, 0x03, 0x72, 0x03, 0x7e, 0x03, 0x8a,
|
||||
0x03, 0x96, 0x03, 0xa2, 0x03, 0xae, 0x03, 0xba, 0x03, 0xc7, 0x03, 0xd3,
|
||||
0x03, 0xe0, 0x03, 0xec, 0x03, 0xf9, 0x04, 0x06, 0x04, 0x13, 0x04, 0x20,
|
||||
0x04, 0x2d, 0x04, 0x3b, 0x04, 0x48, 0x04, 0x55, 0x04, 0x63, 0x04, 0x71,
|
||||
0x04, 0x7e, 0x04, 0x8c, 0x04, 0x9a, 0x04, 0xa8, 0x04, 0xb6, 0x04, 0xc4,
|
||||
0x04, 0xd3, 0x04, 0xe1, 0x04, 0xf0, 0x04, 0xfe, 0x05, 0x0d, 0x05, 0x1c,
|
||||
0x05, 0x2b, 0x05, 0x3a, 0x05, 0x49, 0x05, 0x58, 0x05, 0x67, 0x05, 0x77,
|
||||
0x05, 0x86, 0x05, 0x96, 0x05, 0xa6, 0x05, 0xb5, 0x05, 0xc5, 0x05, 0xd5,
|
||||
0x05, 0xe5, 0x05, 0xf6, 0x06, 0x06, 0x06, 0x16, 0x06, 0x27, 0x06, 0x37,
|
||||
0x06, 0x48, 0x06, 0x59, 0x06, 0x6a, 0x06, 0x7b, 0x06, 0x8c, 0x06, 0x9d,
|
||||
0x06, 0xaf, 0x06, 0xc0, 0x06, 0xd1, 0x06, 0xe3, 0x06, 0xf5, 0x07, 0x07,
|
||||
0x07, 0x19, 0x07, 0x2b, 0x07, 0x3d, 0x07, 0x4f, 0x07, 0x61, 0x07, 0x74,
|
||||
0x07, 0x86, 0x07, 0x99, 0x07, 0xac, 0x07, 0xbf, 0x07, 0xd2, 0x07, 0xe5,
|
||||
0x07, 0xf8, 0x08, 0x0b, 0x08, 0x1f, 0x08, 0x32, 0x08, 0x46, 0x08, 0x5a,
|
||||
0x08, 0x6e, 0x08, 0x82, 0x08, 0x96, 0x08, 0xaa, 0x08, 0xbe, 0x08, 0xd2,
|
||||
0x08, 0xe7, 0x08, 0xfb, 0x09, 0x10, 0x09, 0x25, 0x09, 0x3a, 0x09, 0x4f,
|
||||
0x09, 0x64, 0x09, 0x79, 0x09, 0x8f, 0x09, 0xa4, 0x09, 0xba, 0x09, 0xcf,
|
||||
0x09, 0xe5, 0x09, 0xfb, 0x0a, 0x11, 0x0a, 0x27, 0x0a, 0x3d, 0x0a, 0x54,
|
||||
0x0a, 0x6a, 0x0a, 0x81, 0x0a, 0x98, 0x0a, 0xae, 0x0a, 0xc5, 0x0a, 0xdc,
|
||||
0x0a, 0xf3, 0x0b, 0x0b, 0x0b, 0x22, 0x0b, 0x39, 0x0b, 0x51, 0x0b, 0x69,
|
||||
0x0b, 0x80, 0x0b, 0x98, 0x0b, 0xb0, 0x0b, 0xc8, 0x0b, 0xe1, 0x0b, 0xf9,
|
||||
0x0c, 0x12, 0x0c, 0x2a, 0x0c, 0x43, 0x0c, 0x5c, 0x0c, 0x75, 0x0c, 0x8e,
|
||||
0x0c, 0xa7, 0x0c, 0xc0, 0x0c, 0xd9, 0x0c, 0xf3, 0x0d, 0x0d, 0x0d, 0x26,
|
||||
0x0d, 0x40, 0x0d, 0x5a, 0x0d, 0x74, 0x0d, 0x8e, 0x0d, 0xa9, 0x0d, 0xc3,
|
||||
0x0d, 0xde, 0x0d, 0xf8, 0x0e, 0x13, 0x0e, 0x2e, 0x0e, 0x49, 0x0e, 0x64,
|
||||
0x0e, 0x7f, 0x0e, 0x9b, 0x0e, 0xb6, 0x0e, 0xd2, 0x0e, 0xee, 0x0f, 0x09,
|
||||
0x0f, 0x25, 0x0f, 0x41, 0x0f, 0x5e, 0x0f, 0x7a, 0x0f, 0x96, 0x0f, 0xb3,
|
||||
0x0f, 0xcf, 0x0f, 0xec, 0x10, 0x09, 0x10, 0x26, 0x10, 0x43, 0x10, 0x61,
|
||||
0x10, 0x7e, 0x10, 0x9b, 0x10, 0xb9, 0x10, 0xd7, 0x10, 0xf5, 0x11, 0x13,
|
||||
0x11, 0x31, 0x11, 0x4f, 0x11, 0x6d, 0x11, 0x8c, 0x11, 0xaa, 0x11, 0xc9,
|
||||
0x11, 0xe8, 0x12, 0x07, 0x12, 0x26, 0x12, 0x45, 0x12, 0x64, 0x12, 0x84,
|
||||
0x12, 0xa3, 0x12, 0xc3, 0x12, 0xe3, 0x13, 0x03, 0x13, 0x23, 0x13, 0x43,
|
||||
0x13, 0x63, 0x13, 0x83, 0x13, 0xa4, 0x13, 0xc5, 0x13, 0xe5, 0x14, 0x06,
|
||||
0x14, 0x27, 0x14, 0x49, 0x14, 0x6a, 0x14, 0x8b, 0x14, 0xad, 0x14, 0xce,
|
||||
0x14, 0xf0, 0x15, 0x12, 0x15, 0x34, 0x15, 0x56, 0x15, 0x78, 0x15, 0x9b,
|
||||
0x15, 0xbd, 0x15, 0xe0, 0x16, 0x03, 0x16, 0x26, 0x16, 0x49, 0x16, 0x6c,
|
||||
0x16, 0x8f, 0x16, 0xb2, 0x16, 0xd6, 0x16, 0xfa, 0x17, 0x1d, 0x17, 0x41,
|
||||
0x17, 0x65, 0x17, 0x89, 0x17, 0xae, 0x17, 0xd2, 0x17, 0xf7, 0x18, 0x1b,
|
||||
0x18, 0x40, 0x18, 0x65, 0x18, 0x8a, 0x18, 0xaf, 0x18, 0xd5, 0x18, 0xfa,
|
||||
0x19, 0x20, 0x19, 0x45, 0x19, 0x6b, 0x19, 0x91, 0x19, 0xb7, 0x19, 0xdd,
|
||||
0x1a, 0x04, 0x1a, 0x2a, 0x1a, 0x51, 0x1a, 0x77, 0x1a, 0x9e, 0x1a, 0xc5,
|
||||
0x1a, 0xec, 0x1b, 0x14, 0x1b, 0x3b, 0x1b, 0x63, 0x1b, 0x8a, 0x1b, 0xb2,
|
||||
0x1b, 0xda, 0x1c, 0x02, 0x1c, 0x2a, 0x1c, 0x52, 0x1c, 0x7b, 0x1c, 0xa3,
|
||||
0x1c, 0xcc, 0x1c, 0xf5, 0x1d, 0x1e, 0x1d, 0x47, 0x1d, 0x70, 0x1d, 0x99,
|
||||
0x1d, 0xc3, 0x1d, 0xec, 0x1e, 0x16, 0x1e, 0x40, 0x1e, 0x6a, 0x1e, 0x94,
|
||||
0x1e, 0xbe, 0x1e, 0xe9, 0x1f, 0x13, 0x1f, 0x3e, 0x1f, 0x69, 0x1f, 0x94,
|
||||
0x1f, 0xbf, 0x1f, 0xea, 0x20, 0x15, 0x20, 0x41, 0x20, 0x6c, 0x20, 0x98,
|
||||
0x20, 0xc4, 0x20, 0xf0, 0x21, 0x1c, 0x21, 0x48, 0x21, 0x75, 0x21, 0xa1,
|
||||
0x21, 0xce, 0x21, 0xfb, 0x22, 0x27, 0x22, 0x55, 0x22, 0x82, 0x22, 0xaf,
|
||||
0x22, 0xdd, 0x23, 0x0a, 0x23, 0x38, 0x23, 0x66, 0x23, 0x94, 0x23, 0xc2,
|
||||
0x23, 0xf0, 0x24, 0x1f, 0x24, 0x4d, 0x24, 0x7c, 0x24, 0xab, 0x24, 0xda,
|
||||
0x25, 0x09, 0x25, 0x38, 0x25, 0x68, 0x25, 0x97, 0x25, 0xc7, 0x25, 0xf7,
|
||||
0x26, 0x27, 0x26, 0x57, 0x26, 0x87, 0x26, 0xb7, 0x26, 0xe8, 0x27, 0x18,
|
||||
0x27, 0x49, 0x27, 0x7a, 0x27, 0xab, 0x27, 0xdc, 0x28, 0x0d, 0x28, 0x3f,
|
||||
0x28, 0x71, 0x28, 0xa2, 0x28, 0xd4, 0x29, 0x06, 0x29, 0x38, 0x29, 0x6b,
|
||||
0x29, 0x9d, 0x29, 0xd0, 0x2a, 0x02, 0x2a, 0x35, 0x2a, 0x68, 0x2a, 0x9b,
|
||||
0x2a, 0xcf, 0x2b, 0x02, 0x2b, 0x36, 0x2b, 0x69, 0x2b, 0x9d, 0x2b, 0xd1,
|
||||
0x2c, 0x05, 0x2c, 0x39, 0x2c, 0x6e, 0x2c, 0xa2, 0x2c, 0xd7, 0x2d, 0x0c,
|
||||
0x2d, 0x41, 0x2d, 0x76, 0x2d, 0xab, 0x2d, 0xe1, 0x2e, 0x16, 0x2e, 0x4c,
|
||||
0x2e, 0x82, 0x2e, 0xb7, 0x2e, 0xee, 0x2f, 0x24, 0x2f, 0x5a, 0x2f, 0x91,
|
||||
0x2f, 0xc7, 0x2f, 0xfe, 0x30, 0x35, 0x30, 0x6c, 0x30, 0xa4, 0x30, 0xdb,
|
||||
0x31, 0x12, 0x31, 0x4a, 0x31, 0x82, 0x31, 0xba, 0x31, 0xf2, 0x32, 0x2a,
|
||||
0x32, 0x63, 0x32, 0x9b, 0x32, 0xd4, 0x33, 0x0d, 0x33, 0x46, 0x33, 0x7f,
|
||||
0x33, 0xb8, 0x33, 0xf1, 0x34, 0x2b, 0x34, 0x65, 0x34, 0x9e, 0x34, 0xd8,
|
||||
0x35, 0x13, 0x35, 0x4d, 0x35, 0x87, 0x35, 0xc2, 0x35, 0xfd, 0x36, 0x37,
|
||||
0x36, 0x72, 0x36, 0xae, 0x36, 0xe9, 0x37, 0x24, 0x37, 0x60, 0x37, 0x9c,
|
||||
0x37, 0xd7, 0x38, 0x14, 0x38, 0x50, 0x38, 0x8c, 0x38, 0xc8, 0x39, 0x05,
|
||||
0x39, 0x42, 0x39, 0x7f, 0x39, 0xbc, 0x39, 0xf9, 0x3a, 0x36, 0x3a, 0x74,
|
||||
0x3a, 0xb2, 0x3a, 0xef, 0x3b, 0x2d, 0x3b, 0x6b, 0x3b, 0xaa, 0x3b, 0xe8,
|
||||
0x3c, 0x27, 0x3c, 0x65, 0x3c, 0xa4, 0x3c, 0xe3, 0x3d, 0x22, 0x3d, 0x61,
|
||||
0x3d, 0xa1, 0x3d, 0xe0, 0x3e, 0x20, 0x3e, 0x60, 0x3e, 0xa0, 0x3e, 0xe0,
|
||||
0x3f, 0x21, 0x3f, 0x61, 0x3f, 0xa2, 0x3f, 0xe2, 0x40, 0x23, 0x40, 0x64,
|
||||
0x40, 0xa6, 0x40, 0xe7, 0x41, 0x29, 0x41, 0x6a, 0x41, 0xac, 0x41, 0xee,
|
||||
0x42, 0x30, 0x42, 0x72, 0x42, 0xb5, 0x42, 0xf7, 0x43, 0x3a, 0x43, 0x7d,
|
||||
0x43, 0xc0, 0x44, 0x03, 0x44, 0x47, 0x44, 0x8a, 0x44, 0xce, 0x45, 0x12,
|
||||
0x45, 0x55, 0x45, 0x9a, 0x45, 0xde, 0x46, 0x22, 0x46, 0x67, 0x46, 0xab,
|
||||
0x46, 0xf0, 0x47, 0x35, 0x47, 0x7b, 0x47, 0xc0, 0x48, 0x05, 0x48, 0x4b,
|
||||
0x48, 0x91, 0x48, 0xd7, 0x49, 0x1d, 0x49, 0x63, 0x49, 0xa9, 0x49, 0xf0,
|
||||
0x4a, 0x37, 0x4a, 0x7d, 0x4a, 0xc4, 0x4b, 0x0c, 0x4b, 0x53, 0x4b, 0x9a,
|
||||
0x4b, 0xe2, 0x4c, 0x2a, 0x4c, 0x72, 0x4c, 0xba, 0x4d, 0x02, 0x4d, 0x4a,
|
||||
0x4d, 0x93, 0x4d, 0xdc, 0x4e, 0x25, 0x4e, 0x6e, 0x4e, 0xb7, 0x4f, 0x00,
|
||||
0x4f, 0x49, 0x4f, 0x93, 0x4f, 0xdd, 0x50, 0x27, 0x50, 0x71, 0x50, 0xbb,
|
||||
0x51, 0x06, 0x51, 0x50, 0x51, 0x9b, 0x51, 0xe6, 0x52, 0x31, 0x52, 0x7c,
|
||||
0x52, 0xc7, 0x53, 0x13, 0x53, 0x5f, 0x53, 0xaa, 0x53, 0xf6, 0x54, 0x42,
|
||||
0x54, 0x8f, 0x54, 0xdb, 0x55, 0x28, 0x55, 0x75, 0x55, 0xc2, 0x56, 0x0f,
|
||||
0x56, 0x5c, 0x56, 0xa9, 0x56, 0xf7, 0x57, 0x44, 0x57, 0x92, 0x57, 0xe0,
|
||||
0x58, 0x2f, 0x58, 0x7d, 0x58, 0xcb, 0x59, 0x1a, 0x59, 0x69, 0x59, 0xb8,
|
||||
0x5a, 0x07, 0x5a, 0x56, 0x5a, 0xa6, 0x5a, 0xf5, 0x5b, 0x45, 0x5b, 0x95,
|
||||
0x5b, 0xe5, 0x5c, 0x35, 0x5c, 0x86, 0x5c, 0xd6, 0x5d, 0x27, 0x5d, 0x78,
|
||||
0x5d, 0xc9, 0x5e, 0x1a, 0x5e, 0x6c, 0x5e, 0xbd, 0x5f, 0x0f, 0x5f, 0x61,
|
||||
0x5f, 0xb3, 0x60, 0x05, 0x60, 0x57, 0x60, 0xaa, 0x60, 0xfc, 0x61, 0x4f,
|
||||
0x61, 0xa2, 0x61, 0xf5, 0x62, 0x49, 0x62, 0x9c, 0x62, 0xf0, 0x63, 0x43,
|
||||
0x63, 0x97, 0x63, 0xeb, 0x64, 0x40, 0x64, 0x94, 0x64, 0xe9, 0x65, 0x3d,
|
||||
0x65, 0x92, 0x65, 0xe7, 0x66, 0x3d, 0x66, 0x92, 0x66, 0xe8, 0x67, 0x3d,
|
||||
0x67, 0x93, 0x67, 0xe9, 0x68, 0x3f, 0x68, 0x96, 0x68, 0xec, 0x69, 0x43,
|
||||
0x69, 0x9a, 0x69, 0xf1, 0x6a, 0x48, 0x6a, 0x9f, 0x6a, 0xf7, 0x6b, 0x4f,
|
||||
0x6b, 0xa7, 0x6b, 0xff, 0x6c, 0x57, 0x6c, 0xaf, 0x6d, 0x08, 0x6d, 0x60,
|
||||
0x6d, 0xb9, 0x6e, 0x12, 0x6e, 0x6b, 0x6e, 0xc4, 0x6f, 0x1e, 0x6f, 0x78,
|
||||
0x6f, 0xd1, 0x70, 0x2b, 0x70, 0x86, 0x70, 0xe0, 0x71, 0x3a, 0x71, 0x95,
|
||||
0x71, 0xf0, 0x72, 0x4b, 0x72, 0xa6, 0x73, 0x01, 0x73, 0x5d, 0x73, 0xb8,
|
||||
0x74, 0x14, 0x74, 0x70, 0x74, 0xcc, 0x75, 0x28, 0x75, 0x85, 0x75, 0xe1,
|
||||
0x76, 0x3e, 0x76, 0x9b, 0x76, 0xf8, 0x77, 0x56, 0x77, 0xb3, 0x78, 0x11,
|
||||
0x78, 0x6e, 0x78, 0xcc, 0x79, 0x2a, 0x79, 0x89, 0x79, 0xe7, 0x7a, 0x46,
|
||||
0x7a, 0xa5, 0x7b, 0x04, 0x7b, 0x63, 0x7b, 0xc2, 0x7c, 0x21, 0x7c, 0x81,
|
||||
0x7c, 0xe1, 0x7d, 0x41, 0x7d, 0xa1, 0x7e, 0x01, 0x7e, 0x62, 0x7e, 0xc2,
|
||||
0x7f, 0x23, 0x7f, 0x84, 0x7f, 0xe5, 0x80, 0x47, 0x80, 0xa8, 0x81, 0x0a,
|
||||
0x81, 0x6b, 0x81, 0xcd, 0x82, 0x30, 0x82, 0x92, 0x82, 0xf4, 0x83, 0x57,
|
||||
0x83, 0xba, 0x84, 0x1d, 0x84, 0x80, 0x84, 0xe3, 0x85, 0x47, 0x85, 0xab,
|
||||
0x86, 0x0e, 0x86, 0x72, 0x86, 0xd7, 0x87, 0x3b, 0x87, 0x9f, 0x88, 0x04,
|
||||
0x88, 0x69, 0x88, 0xce, 0x89, 0x33, 0x89, 0x99, 0x89, 0xfe, 0x8a, 0x64,
|
||||
0x8a, 0xca, 0x8b, 0x30, 0x8b, 0x96, 0x8b, 0xfc, 0x8c, 0x63, 0x8c, 0xca,
|
||||
0x8d, 0x31, 0x8d, 0x98, 0x8d, 0xff, 0x8e, 0x66, 0x8e, 0xce, 0x8f, 0x36,
|
||||
0x8f, 0x9e, 0x90, 0x06, 0x90, 0x6e, 0x90, 0xd6, 0x91, 0x3f, 0x91, 0xa8,
|
||||
0x92, 0x11, 0x92, 0x7a, 0x92, 0xe3, 0x93, 0x4d, 0x93, 0xb6, 0x94, 0x20,
|
||||
0x94, 0x8a, 0x94, 0xf4, 0x95, 0x5f, 0x95, 0xc9, 0x96, 0x34, 0x96, 0x9f,
|
||||
0x97, 0x0a, 0x97, 0x75, 0x97, 0xe0, 0x98, 0x4c, 0x98, 0xb8, 0x99, 0x24,
|
||||
0x99, 0x90, 0x99, 0xfc, 0x9a, 0x68, 0x9a, 0xd5, 0x9b, 0x42, 0x9b, 0xaf,
|
||||
0x9c, 0x1c, 0x9c, 0x89, 0x9c, 0xf7, 0x9d, 0x64, 0x9d, 0xd2, 0x9e, 0x40,
|
||||
0x9e, 0xae, 0x9f, 0x1d, 0x9f, 0x8b, 0x9f, 0xfa, 0xa0, 0x69, 0xa0, 0xd8,
|
||||
0xa1, 0x47, 0xa1, 0xb6, 0xa2, 0x26, 0xa2, 0x96, 0xa3, 0x06, 0xa3, 0x76,
|
||||
0xa3, 0xe6, 0xa4, 0x56, 0xa4, 0xc7, 0xa5, 0x38, 0xa5, 0xa9, 0xa6, 0x1a,
|
||||
0xa6, 0x8b, 0xa6, 0xfd, 0xa7, 0x6e, 0xa7, 0xe0, 0xa8, 0x52, 0xa8, 0xc4,
|
||||
0xa9, 0x37, 0xa9, 0xa9, 0xaa, 0x1c, 0xaa, 0x8f, 0xab, 0x02, 0xab, 0x75,
|
||||
0xab, 0xe9, 0xac, 0x5c, 0xac, 0xd0, 0xad, 0x44, 0xad, 0xb8, 0xae, 0x2d,
|
||||
0xae, 0xa1, 0xaf, 0x16, 0xaf, 0x8b, 0xb0, 0x00, 0xb0, 0x75, 0xb0, 0xea,
|
||||
0xb1, 0x60, 0xb1, 0xd6, 0xb2, 0x4b, 0xb2, 0xc2, 0xb3, 0x38, 0xb3, 0xae,
|
||||
0xb4, 0x25, 0xb4, 0x9c, 0xb5, 0x13, 0xb5, 0x8a, 0xb6, 0x01, 0xb6, 0x79,
|
||||
0xb6, 0xf0, 0xb7, 0x68, 0xb7, 0xe0, 0xb8, 0x59, 0xb8, 0xd1, 0xb9, 0x4a,
|
||||
0xb9, 0xc2, 0xba, 0x3b, 0xba, 0xb5, 0xbb, 0x2e, 0xbb, 0xa7, 0xbc, 0x21,
|
||||
0xbc, 0x9b, 0xbd, 0x15, 0xbd, 0x8f, 0xbe, 0x0a, 0xbe, 0x84, 0xbe, 0xff,
|
||||
0xbf, 0x7a, 0xbf, 0xf5, 0xc0, 0x70, 0xc0, 0xec, 0xc1, 0x67, 0xc1, 0xe3,
|
||||
0xc2, 0x5f, 0xc2, 0xdb, 0xc3, 0x58, 0xc3, 0xd4, 0xc4, 0x51, 0xc4, 0xce,
|
||||
0xc5, 0x4b, 0xc5, 0xc8, 0xc6, 0x46, 0xc6, 0xc3, 0xc7, 0x41, 0xc7, 0xbf,
|
||||
0xc8, 0x3d, 0xc8, 0xbc, 0xc9, 0x3a, 0xc9, 0xb9, 0xca, 0x38, 0xca, 0xb7,
|
||||
0xcb, 0x36, 0xcb, 0xb6, 0xcc, 0x35, 0xcc, 0xb5, 0xcd, 0x35, 0xcd, 0xb5,
|
||||
0xce, 0x36, 0xce, 0xb6, 0xcf, 0x37, 0xcf, 0xb8, 0xd0, 0x39, 0xd0, 0xba,
|
||||
0xd1, 0x3c, 0xd1, 0xbe, 0xd2, 0x3f, 0xd2, 0xc1, 0xd3, 0x44, 0xd3, 0xc6,
|
||||
0xd4, 0x49, 0xd4, 0xcb, 0xd5, 0x4e, 0xd5, 0xd1, 0xd6, 0x55, 0xd6, 0xd8,
|
||||
0xd7, 0x5c, 0xd7, 0xe0, 0xd8, 0x64, 0xd8, 0xe8, 0xd9, 0x6c, 0xd9, 0xf1,
|
||||
0xda, 0x76, 0xda, 0xfb, 0xdb, 0x80, 0xdc, 0x05, 0xdc, 0x8a, 0xdd, 0x10,
|
||||
0xdd, 0x96, 0xde, 0x1c, 0xde, 0xa2, 0xdf, 0x29, 0xdf, 0xaf, 0xe0, 0x36,
|
||||
0xe0, 0xbd, 0xe1, 0x44, 0xe1, 0xcc, 0xe2, 0x53, 0xe2, 0xdb, 0xe3, 0x63,
|
||||
0xe3, 0xeb, 0xe4, 0x73, 0xe4, 0xfc, 0xe5, 0x84, 0xe6, 0x0d, 0xe6, 0x96,
|
||||
0xe7, 0x1f, 0xe7, 0xa9, 0xe8, 0x32, 0xe8, 0xbc, 0xe9, 0x46, 0xe9, 0xd0,
|
||||
0xea, 0x5b, 0xea, 0xe5, 0xeb, 0x70, 0xeb, 0xfb, 0xec, 0x86, 0xed, 0x11,
|
||||
0xed, 0x9c, 0xee, 0x28, 0xee, 0xb4, 0xef, 0x40, 0xef, 0xcc, 0xf0, 0x58,
|
||||
0xf0, 0xe5, 0xf1, 0x72, 0xf1, 0xff, 0xf2, 0x8c, 0xf3, 0x19, 0xf3, 0xa7,
|
||||
0xf4, 0x34, 0xf4, 0xc2, 0xf5, 0x50, 0xf5, 0xde, 0xf6, 0x6d, 0xf6, 0xfb,
|
||||
0xf7, 0x8a, 0xf8, 0x19, 0xf8, 0xa8, 0xf9, 0x38, 0xf9, 0xc7, 0xfa, 0x57,
|
||||
0xfa, 0xe7, 0xfb, 0x77, 0xfc, 0x07, 0xfc, 0x98, 0xfd, 0x29, 0xfd, 0xba,
|
||||
0xfe, 0x4b, 0xfe, 0xdc, 0xff, 0x6d, 0xff, 0xff, 0x58, 0x59, 0x5a, 0x20,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf3, 0x51, 0x00, 0x01, 0x00, 0x00,
|
||||
0x00, 0x01, 0x16, 0xcc, 0x74, 0x65, 0x78, 0x74, 0x00, 0x00, 0x00, 0x00,
|
||||
0x43, 0x6f, 0x70, 0x79, 0x72, 0x69, 0x67, 0x68, 0x74, 0x20, 0x41, 0x70,
|
||||
0x70, 0x6c, 0x65, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x2c, 0x20, 0x32, 0x30,
|
||||
0x30, 0x38, 0x00, 0x00, 0x64, 0x65, 0x73, 0x63, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x1f, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x69, 0x63, 0x20,
|
||||
0x47, 0x72, 0x61, 0x79, 0x20, 0x47, 0x61, 0x6d, 0x6d, 0x61, 0x20, 0x32,
|
||||
0x2e, 0x32, 0x20, 0x50, 0x72, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6d, 0x6c, 0x75, 0x63,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x0c,
|
||||
0x65, 0x6e, 0x55, 0x53, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0xdc,
|
||||
0x65, 0x73, 0x45, 0x53, 0x00, 0x00, 0x00, 0x4c, 0x00, 0x00, 0x01, 0x18,
|
||||
0x64, 0x61, 0x44, 0x4b, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x02, 0x2a,
|
||||
0x64, 0x65, 0x44, 0x45, 0x00, 0x00, 0x00, 0x4c, 0x00, 0x00, 0x01, 0xde,
|
||||
0x66, 0x69, 0x46, 0x49, 0x00, 0x00, 0x00, 0x46, 0x00, 0x00, 0x02, 0xa0,
|
||||
0x66, 0x72, 0x46, 0x55, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x02, 0x62,
|
||||
0x69, 0x74, 0x49, 0x54, 0x00, 0x00, 0x00, 0x54, 0x00, 0x00, 0x03, 0x70,
|
||||
0x6e, 0x6c, 0x4e, 0x4c, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x01, 0x64,
|
||||
0x6e, 0x62, 0x4e, 0x4f, 0x00, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x03, 0xc4,
|
||||
0x70, 0x74, 0x42, 0x52, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x03, 0x26,
|
||||
0x73, 0x76, 0x53, 0x45, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x02, 0x2a,
|
||||
0x6a, 0x61, 0x4a, 0x50, 0x00, 0x00, 0x00, 0x26, 0x00, 0x00, 0x03, 0xfe,
|
||||
0x6b, 0x6f, 0x4b, 0x52, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x04, 0x24,
|
||||
0x7a, 0x68, 0x54, 0x57, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x04, 0x46,
|
||||
0x7a, 0x68, 0x43, 0x4e, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x04, 0x64,
|
||||
0x72, 0x75, 0x52, 0x55, 0x00, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x01, 0xa4,
|
||||
0x70, 0x6c, 0x50, 0x4c, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x02, 0xe6,
|
||||
0x00, 0x47, 0x00, 0x65, 0x00, 0x6e, 0x00, 0x65, 0x00, 0x72, 0x00, 0x69,
|
||||
0x00, 0x63, 0x00, 0x20, 0x00, 0x47, 0x00, 0x72, 0x00, 0x61, 0x00, 0x79,
|
||||
0x00, 0x20, 0x00, 0x47, 0x00, 0x61, 0x00, 0x6d, 0x00, 0x6d, 0x00, 0x61,
|
||||
0x00, 0x20, 0x00, 0x32, 0x00, 0x2e, 0x00, 0x32, 0x00, 0x20, 0x00, 0x50,
|
||||
0x00, 0x72, 0x00, 0x6f, 0x00, 0x66, 0x00, 0x69, 0x00, 0x6c, 0x00, 0x65,
|
||||
0x00, 0x50, 0x00, 0x65, 0x00, 0x72, 0x00, 0x66, 0x00, 0x69, 0x00, 0x6c,
|
||||
0x00, 0x20, 0x00, 0x67, 0x00, 0x65, 0x00, 0x6e, 0x00, 0xe9, 0x00, 0x72,
|
||||
0x00, 0x69, 0x00, 0x63, 0x00, 0x6f, 0x00, 0x20, 0x00, 0x64, 0x00, 0x65,
|
||||
0x00, 0x20, 0x00, 0x67, 0x00, 0x61, 0x00, 0x6d, 0x00, 0x6d, 0x00, 0x61,
|
||||
0x00, 0x20, 0x00, 0x64, 0x00, 0x65, 0x00, 0x20, 0x00, 0x67, 0x00, 0x72,
|
||||
0x00, 0x69, 0x00, 0x73, 0x00, 0x65, 0x00, 0x73, 0x00, 0x20, 0x00, 0x32,
|
||||
0x00, 0x2c, 0x00, 0x32, 0x00, 0x41, 0x00, 0x6c, 0x00, 0x67, 0x00, 0x65,
|
||||
0x00, 0x6d, 0x00, 0x65, 0x00, 0x65, 0x00, 0x6e, 0x00, 0x20, 0x00, 0x67,
|
||||
0x00, 0x72, 0x00, 0x69, 0x00, 0x6a, 0x00, 0x73, 0x00, 0x20, 0x00, 0x67,
|
||||
0x00, 0x61, 0x00, 0x6d, 0x00, 0x6d, 0x00, 0x61, 0x00, 0x20, 0x00, 0x32,
|
||||
0x00, 0x2c, 0x00, 0x32, 0x00, 0x2d, 0x00, 0x70, 0x00, 0x72, 0x00, 0x6f,
|
||||
0x00, 0x66, 0x00, 0x69, 0x00, 0x65, 0x00, 0x6c, 0x04, 0x1e, 0x04, 0x31,
|
||||
0x04, 0x49, 0x04, 0x30, 0x04, 0x4f, 0x00, 0x20, 0x04, 0x41, 0x04, 0x35,
|
||||
0x04, 0x40, 0x04, 0x30, 0x04, 0x4f, 0x00, 0x20, 0x04, 0x33, 0x04, 0x30,
|
||||
0x04, 0x3c, 0x04, 0x3c, 0x04, 0x30, 0x00, 0x20, 0x00, 0x32, 0x00, 0x2c,
|
||||
0x00, 0x32, 0x00, 0x2d, 0x04, 0x3f, 0x04, 0x40, 0x04, 0x3e, 0x04, 0x44,
|
||||
0x04, 0x38, 0x04, 0x3b, 0x04, 0x4c, 0x00, 0x41, 0x00, 0x6c, 0x00, 0x6c,
|
||||
0x00, 0x67, 0x00, 0x65, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x69, 0x00, 0x6e,
|
||||
0x00, 0x65, 0x00, 0x73, 0x00, 0x20, 0x00, 0x47, 0x00, 0x72, 0x00, 0x61,
|
||||
0x00, 0x75, 0x00, 0x73, 0x00, 0x74, 0x00, 0x75, 0x00, 0x66, 0x00, 0x65,
|
||||
0x00, 0x6e, 0x00, 0x70, 0x00, 0x72, 0x00, 0x6f, 0x00, 0x66, 0x00, 0x69,
|
||||
0x00, 0x6c, 0x00, 0x20, 0x00, 0x47, 0x00, 0x61, 0x00, 0x6d, 0x00, 0x6d,
|
||||
0x00, 0x61, 0x00, 0x20, 0x00, 0x32, 0x00, 0x2c, 0x00, 0x32, 0x00, 0x47,
|
||||
0x00, 0x65, 0x00, 0x6e, 0x00, 0x65, 0x00, 0x72, 0x00, 0x69, 0x00, 0x73,
|
||||
0x00, 0x6b, 0x00, 0x20, 0x00, 0x67, 0x00, 0x72, 0x00, 0xe5, 0x00, 0x20,
|
||||
0x00, 0x32, 0x00, 0x2c, 0x00, 0x32, 0x00, 0x20, 0x00, 0x67, 0x00, 0x61,
|
||||
0x00, 0x6d, 0x00, 0x6d, 0x00, 0x61, 0x00, 0x70, 0x00, 0x72, 0x00, 0x6f,
|
||||
0x00, 0x66, 0x00, 0x69, 0x00, 0x6c, 0x00, 0x50, 0x00, 0x72, 0x00, 0x6f,
|
||||
0x00, 0x66, 0x00, 0x69, 0x00, 0x6c, 0x00, 0x20, 0x00, 0x67, 0x00, 0xe9,
|
||||
0x00, 0x6e, 0x00, 0xe9, 0x00, 0x72, 0x00, 0x69, 0x00, 0x71, 0x00, 0x75,
|
||||
0x00, 0x65, 0x00, 0x20, 0x00, 0x67, 0x00, 0x72, 0x00, 0x69, 0x00, 0x73,
|
||||
0x00, 0x20, 0x00, 0x67, 0x00, 0x61, 0x00, 0x6d, 0x00, 0x6d, 0x00, 0x61,
|
||||
0x00, 0x20, 0x00, 0x32, 0x00, 0x2c, 0x00, 0x32, 0x00, 0x59, 0x00, 0x6c,
|
||||
0x00, 0x65, 0x00, 0x69, 0x00, 0x6e, 0x00, 0x65, 0x00, 0x6e, 0x00, 0x20,
|
||||
0x00, 0x68, 0x00, 0x61, 0x00, 0x72, 0x00, 0x6d, 0x00, 0x61, 0x00, 0x61,
|
||||
0x00, 0x6e, 0x00, 0x20, 0x00, 0x67, 0x00, 0x61, 0x00, 0x6d, 0x00, 0x6d,
|
||||
0x00, 0x61, 0x00, 0x20, 0x00, 0x32, 0x00, 0x2c, 0x00, 0x32, 0x00, 0x20,
|
||||
0x00, 0x2d, 0x00, 0x70, 0x00, 0x72, 0x00, 0x6f, 0x00, 0x66, 0x00, 0x69,
|
||||
0x00, 0x69, 0x00, 0x6c, 0x00, 0x69, 0x00, 0x4f, 0x00, 0x67, 0x00, 0xf3,
|
||||
0x00, 0x6c, 0x00, 0x6e, 0x00, 0x79, 0x00, 0x20, 0x00, 0x70, 0x00, 0x72,
|
||||
0x00, 0x6f, 0x00, 0x66, 0x00, 0x69, 0x00, 0x6c, 0x00, 0x20, 0x00, 0x73,
|
||||
0x00, 0x7a, 0x00, 0x61, 0x00, 0x72, 0x00, 0x6f, 0x01, 0x5b, 0x00, 0x63,
|
||||
0x00, 0x69, 0x00, 0x20, 0x00, 0x67, 0x00, 0x61, 0x00, 0x6d, 0x00, 0x6d,
|
||||
0x00, 0x61, 0x00, 0x20, 0x00, 0x32, 0x00, 0x2c, 0x00, 0x32, 0x00, 0x50,
|
||||
0x00, 0x65, 0x00, 0x72, 0x00, 0x66, 0x00, 0x69, 0x00, 0x6c, 0x00, 0x20,
|
||||
0x00, 0x47, 0x00, 0x65, 0x00, 0x6e, 0x00, 0xe9, 0x00, 0x72, 0x00, 0x69,
|
||||
0x00, 0x63, 0x00, 0x6f, 0x00, 0x20, 0x00, 0x64, 0x00, 0x61, 0x00, 0x20,
|
||||
0x00, 0x47, 0x00, 0x61, 0x00, 0x6d, 0x00, 0x61, 0x00, 0x20, 0x00, 0x64,
|
||||
0x00, 0x65, 0x00, 0x20, 0x00, 0x43, 0x00, 0x69, 0x00, 0x6e, 0x00, 0x7a,
|
||||
0x00, 0x61, 0x00, 0x73, 0x00, 0x20, 0x00, 0x32, 0x00, 0x2c, 0x00, 0x32,
|
||||
0x00, 0x50, 0x00, 0x72, 0x00, 0x6f, 0x00, 0x66, 0x00, 0x69, 0x00, 0x6c,
|
||||
0x00, 0x6f, 0x00, 0x20, 0x00, 0x67, 0x00, 0x65, 0x00, 0x6e, 0x00, 0x65,
|
||||
0x00, 0x72, 0x00, 0x69, 0x00, 0x63, 0x00, 0x6f, 0x00, 0x20, 0x00, 0x64,
|
||||
0x00, 0x65, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x61, 0x00, 0x20, 0x00, 0x67,
|
||||
0x00, 0x61, 0x00, 0x6d, 0x00, 0x6d, 0x00, 0x61, 0x00, 0x20, 0x00, 0x64,
|
||||
0x00, 0x65, 0x00, 0x69, 0x00, 0x20, 0x00, 0x67, 0x00, 0x72, 0x00, 0x69,
|
||||
0x00, 0x67, 0x00, 0x69, 0x00, 0x20, 0x00, 0x32, 0x00, 0x2c, 0x00, 0x32,
|
||||
0x00, 0x47, 0x00, 0x65, 0x00, 0x6e, 0x00, 0x65, 0x00, 0x72, 0x00, 0x69,
|
||||
0x00, 0x73, 0x00, 0x6b, 0x00, 0x20, 0x00, 0x67, 0x00, 0x72, 0x00, 0xe5,
|
||||
0x00, 0x20, 0x00, 0x67, 0x00, 0x61, 0x00, 0x6d, 0x00, 0x6d, 0x00, 0x61,
|
||||
0x00, 0x20, 0x00, 0x32, 0x00, 0x2c, 0x00, 0x32, 0x00, 0x2d, 0x00, 0x70,
|
||||
0x00, 0x72, 0x00, 0x6f, 0x00, 0x66, 0x00, 0x69, 0x00, 0x6c, 0x4e, 0x00,
|
||||
0x82, 0x2c, 0x30, 0xb0, 0x30, 0xec, 0x30, 0xa4, 0x30, 0xac, 0x30, 0xf3,
|
||||
0x30, 0xde, 0x00, 0x20, 0x00, 0x32, 0x00, 0x2e, 0x00, 0x32, 0x00, 0x20,
|
||||
0x30, 0xd7, 0x30, 0xed, 0x30, 0xd5, 0x30, 0xa1, 0x30, 0xa4, 0x30, 0xeb,
|
||||
0xc7, 0x7c, 0xbc, 0x18, 0x00, 0x20, 0xd6, 0x8c, 0xc0, 0xc9, 0x00, 0x20,
|
||||
0xac, 0x10, 0xb9, 0xc8, 0x00, 0x20, 0x00, 0x32, 0x00, 0x2e, 0x00, 0x32,
|
||||
0x00, 0x20, 0xd5, 0x04, 0xb8, 0x5c, 0xd3, 0x0c, 0xc7, 0x7c, 0x90, 0x1a,
|
||||
0x75, 0x28, 0x70, 0x70, 0x96, 0x8e, 0x51, 0x49, 0x5e, 0xa6, 0x00, 0x20,
|
||||
0x00, 0x32, 0x00, 0x2e, 0x00, 0x32, 0x00, 0x20, 0x82, 0x72, 0x5f, 0x69,
|
||||
0x63, 0xcf, 0x8f, 0xf0, 0x90, 0x1a, 0x75, 0x28, 0x70, 0x70, 0x5e, 0xa6,
|
||||
0x7c, 0xfb, 0x65, 0x70, 0x00, 0x20, 0x00, 0x32, 0x00, 0x2e, 0x00, 0x32,
|
||||
0x00, 0x20, 0x63, 0xcf, 0x8f, 0xf0, 0x65, 0x87, 0x4e, 0xf6, 0x00, 0x00,
|
||||
}
|
||||
|
||||
sRGBV2MicroICCProfilePathToken = "\x00srgb_v2_micro.icc"
|
||||
sGrayV2MicroICCProfilePathToken = "\x00sgray_v2_micro.icc"
|
||||
sRGBIEC6196621ICCProfilePathToken = "\x00srgb_iec61966_2_1.icc"
|
||||
genericGrayGamma22ICCProfilePathToken = "\x00generic_gray_gamma_2_2.icc"
|
||||
|
||||
temporaryDirectory = ""
|
||||
SRGBV2MicroICCProfilePath = sRGBV2MicroICCProfilePathToken
|
||||
SGrayV2MicroICCProfilePath = sGrayV2MicroICCProfilePathToken
|
||||
SRGBIEC6196621ICCProfilePath = sRGBIEC6196621ICCProfilePathToken
|
||||
GenericGrayGamma22ICCProfilePath = genericGrayGamma22ICCProfilePathToken
|
||||
)
|
||||
|
||||
// Back support
|
||||
func ensureLoadICCPath(name *string) (err error) {
|
||||
if len(*name) > 0 && (*name)[0] == 0 {
|
||||
switch *name {
|
||||
case sRGBV2MicroICCProfilePathToken:
|
||||
*name, err = GetSRGBV2MicroICCProfilePath()
|
||||
return
|
||||
case sGrayV2MicroICCProfilePathToken:
|
||||
*name, err = GetSGrayV2MicroICCProfilePath()
|
||||
return
|
||||
case sRGBIEC6196621ICCProfilePathToken:
|
||||
*name, err = GetSRGBIEC6196621ICCProfilePath()
|
||||
return
|
||||
case genericGrayGamma22ICCProfilePathToken:
|
||||
*name, err = GetGenericGrayGamma22ICCProfilePath()
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
var tempDirOnce sync.Once
|
||||
var tempDirErr error
|
||||
|
||||
func getTemporaryDirectory() (string, error) {
|
||||
tempDirOnce.Do(func() {
|
||||
temporaryDirectory, tempDirErr = os.MkdirTemp("", "govips-")
|
||||
})
|
||||
return temporaryDirectory, tempDirErr
|
||||
}
|
||||
|
||||
var lockIcc sync.Mutex
|
||||
|
||||
func GetSRGBV2MicroICCProfilePath() (string, error) {
|
||||
return getOrLoad(&SRGBV2MicroICCProfilePath, "srgb_v2_micro.icc", sRGBV2MicroICCProfile)
|
||||
}
|
||||
|
||||
func GetSGrayV2MicroICCProfilePath() (string, error) {
|
||||
return getOrLoad(&SGrayV2MicroICCProfilePath, "sgray_v2_micro.icc", sGrayV2MicroICCProfile)
|
||||
}
|
||||
|
||||
func GetSRGBIEC6196621ICCProfilePath() (string, error) {
|
||||
return getOrLoad(&SRGBIEC6196621ICCProfilePath, "srgb_iec61966_2_1.icc", sRGBIEC6196621ICCProfile)
|
||||
}
|
||||
|
||||
func GetGenericGrayGamma22ICCProfilePath() (string, error) {
|
||||
return getOrLoad(&GenericGrayGamma22ICCProfilePath, "generic_gray_gamma_2_2.icc", genericGrayGamma22ICCProfile)
|
||||
}
|
||||
|
||||
func getOrLoad(pathFile *string, name string, fileBytes []byte) (string, error) {
|
||||
lockIcc.Lock()
|
||||
defer lockIcc.Unlock()
|
||||
if len(*pathFile) > 0 && (*pathFile)[0] != 0 {
|
||||
return *pathFile, nil
|
||||
}
|
||||
|
||||
if _, err := getTemporaryDirectory(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
*pathFile = filepath.Join(temporaryDirectory, name)
|
||||
if err := os.WriteFile(*pathFile, fileBytes, 0600); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return *pathFile, nil
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
#include "image.h"
|
||||
|
||||
int has_alpha_channel(VipsImage *image) { return vips_image_hasalpha(image); }
|
||||
|
||||
void clear_image(VipsImage **image) {
|
||||
// Reference-counting in libvips: https://www.libvips.org/API/current/using-from-c.html#using-C-ref
|
||||
// https://docs.gtk.org/gobject/method.Object.unref.html
|
||||
if (G_IS_OBJECT(*image)) g_object_unref(*image);
|
||||
}
|
||||
|
||||
VipsImage *create_image_from_memory_copy(const void *data, size_t size,
|
||||
int width, int height, int bands,
|
||||
VipsBandFormat format) {
|
||||
return vips_image_new_from_memory_copy(data, size, width, height, bands,
|
||||
format);
|
||||
}
|
||||
@@ -1,988 +0,0 @@
|
||||
package vips
|
||||
|
||||
// #include "image.h"
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/draw"
|
||||
_ "image/gif"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"unsafe"
|
||||
|
||||
_ "golang.org/x/image/webp"
|
||||
)
|
||||
|
||||
const GaussBlurDefaultMinAMpl = 0.2
|
||||
|
||||
// PreMultiplicationState stores the pre-multiplication band format of the image
|
||||
type PreMultiplicationState struct {
|
||||
bandFormat BandFormat
|
||||
}
|
||||
|
||||
// ImageRef contains a libvips image and manages its lifecycle.
|
||||
type ImageRef struct {
|
||||
// NOTE: We keep a reference to this so that the input buffer is
|
||||
// never garbage collected during processing. Some image loaders use random
|
||||
// access transcoding and therefore need the original buffer to be in memory.
|
||||
buf []byte
|
||||
image *C.VipsImage
|
||||
format ImageType
|
||||
originalFormat ImageType
|
||||
lock sync.Mutex
|
||||
preMultiplication *PreMultiplicationState
|
||||
optimizedIccProfile string
|
||||
|
||||
// streamSource is non-nil for sequentially stream-loaded images
|
||||
// (LoadImageFromReader with AccessSequential): the image pulls
|
||||
// pixels from the source on demand, so the C source and the Go
|
||||
// reader stay alive until Close or materialize. See stream.go.
|
||||
streamSource *streamSourceRef
|
||||
}
|
||||
|
||||
// ImageMetadata is a data structure holding the width, height, orientation and other metadata of the picture.
|
||||
type ImageMetadata struct {
|
||||
Format ImageType
|
||||
Width int
|
||||
Height int
|
||||
Colorspace Interpretation
|
||||
Orientation int
|
||||
Pages int
|
||||
}
|
||||
|
||||
type Parameter struct {
|
||||
value interface{}
|
||||
isSet bool
|
||||
}
|
||||
|
||||
func (p *Parameter) IsSet() bool {
|
||||
return p.isSet
|
||||
}
|
||||
|
||||
func (p *Parameter) set(v interface{}) {
|
||||
p.value = v
|
||||
p.isSet = true
|
||||
}
|
||||
|
||||
type BoolParameter struct {
|
||||
Parameter
|
||||
}
|
||||
|
||||
func (p *BoolParameter) Set(v bool) {
|
||||
p.set(v)
|
||||
}
|
||||
|
||||
func (p *BoolParameter) Get() bool {
|
||||
return p.value.(bool)
|
||||
}
|
||||
|
||||
type IntParameter struct {
|
||||
Parameter
|
||||
}
|
||||
|
||||
func (p *IntParameter) Set(v int) {
|
||||
p.set(v)
|
||||
}
|
||||
|
||||
func (p *IntParameter) Get() int {
|
||||
return p.value.(int)
|
||||
}
|
||||
|
||||
type Float64Parameter struct {
|
||||
Parameter
|
||||
}
|
||||
|
||||
func (p *Float64Parameter) Set(v float64) {
|
||||
p.set(v)
|
||||
}
|
||||
|
||||
func (p *Float64Parameter) Get() float64 {
|
||||
return p.value.(float64)
|
||||
}
|
||||
|
||||
// ImportParams are options for loading an image. Some are type-specific.
|
||||
// For default loading, use NewImportParams() or specify nil
|
||||
type ImportParams struct {
|
||||
AutoRotate BoolParameter
|
||||
FailOnError BoolParameter
|
||||
Page IntParameter
|
||||
NumPages IntParameter
|
||||
Density IntParameter
|
||||
|
||||
JpegShrinkFactor IntParameter
|
||||
WebpScaleFactor Float64Parameter
|
||||
HeifThumbnail BoolParameter
|
||||
SvgUnlimited BoolParameter
|
||||
Access IntParameter
|
||||
}
|
||||
|
||||
// NewImportParams creates default ImportParams
|
||||
func NewImportParams() *ImportParams {
|
||||
p := &ImportParams{}
|
||||
p.FailOnError.Set(true)
|
||||
return p
|
||||
}
|
||||
|
||||
// OptionString convert import params to option_string
|
||||
func (i *ImportParams) OptionString() string {
|
||||
var values []string
|
||||
if v := i.NumPages; v.IsSet() {
|
||||
values = append(values, "n="+strconv.Itoa(v.Get()))
|
||||
}
|
||||
if v := i.Page; v.IsSet() {
|
||||
values = append(values, "page="+strconv.Itoa(v.Get()))
|
||||
}
|
||||
if v := i.Density; v.IsSet() {
|
||||
values = append(values, "dpi="+strconv.Itoa(v.Get()))
|
||||
}
|
||||
if v := i.FailOnError; v.IsSet() {
|
||||
values = append(values, "fail="+boolToStr(v.Get()))
|
||||
}
|
||||
if v := i.JpegShrinkFactor; v.IsSet() {
|
||||
values = append(values, "shrink="+strconv.Itoa(v.Get()))
|
||||
}
|
||||
if v := i.WebpScaleFactor; v.IsSet() {
|
||||
values = append(values, "scale="+strconv.FormatFloat(v.Get(), 'f', -1, 64))
|
||||
}
|
||||
if v := i.AutoRotate; v.IsSet() {
|
||||
values = append(values, "autorotate="+boolToStr(v.Get()))
|
||||
}
|
||||
if v := i.SvgUnlimited; v.IsSet() {
|
||||
values = append(values, "unlimited="+boolToStr(v.Get()))
|
||||
}
|
||||
if v := i.HeifThumbnail; v.IsSet() {
|
||||
values = append(values, "thumbnail="+boolToStr(v.Get()))
|
||||
}
|
||||
if v := i.Access; v.IsSet() {
|
||||
values = append(values, "access="+strconv.Itoa(v.Get()))
|
||||
}
|
||||
return strings.Join(values, ",")
|
||||
}
|
||||
|
||||
func boolToStr(v bool) string {
|
||||
if v {
|
||||
return "TRUE"
|
||||
}
|
||||
return "FALSE"
|
||||
}
|
||||
|
||||
// ExportParams are options when exporting an image to file or buffer.
|
||||
// Deprecated: Use format-specific params
|
||||
type ExportParams struct {
|
||||
Format ImageType
|
||||
Quality int
|
||||
Compression int
|
||||
Interlaced bool
|
||||
Lossless bool
|
||||
Effort int
|
||||
StripMetadata bool
|
||||
OptimizeCoding bool // jpeg param
|
||||
SubsampleMode SubsampleMode // jpeg param
|
||||
TrellisQuant bool // jpeg param
|
||||
OvershootDeringing bool // jpeg param
|
||||
OptimizeScans bool // jpeg param
|
||||
QuantTable int // jpeg param
|
||||
Speed int // avif param
|
||||
}
|
||||
|
||||
// NewDefaultExportParams creates default values for an export when image type is not JPEG, PNG or WEBP.
|
||||
// By default, govips creates interlaced, lossy images with a quality of 80/100 and compression of 6/10.
|
||||
// As these are default values for a wide variety of image formats, their application varies.
|
||||
// Some formats use the quality parameters, some compression, etc.
|
||||
// Deprecated: Use format-specific params
|
||||
func NewDefaultExportParams() *ExportParams {
|
||||
return &ExportParams{
|
||||
Format: ImageTypeUnknown, // defaults to the starting encoder
|
||||
Quality: 80,
|
||||
Compression: 6,
|
||||
Interlaced: true,
|
||||
Lossless: false,
|
||||
Effort: 4,
|
||||
}
|
||||
}
|
||||
|
||||
// NewDefaultJPEGExportParams creates default values for an export of a JPEG image.
|
||||
// By default, govips creates interlaced JPEGs with a quality of 80/100.
|
||||
// Deprecated: Use NewJpegExportParams
|
||||
func NewDefaultJPEGExportParams() *ExportParams {
|
||||
return &ExportParams{
|
||||
Format: ImageTypeJPEG,
|
||||
Quality: 80,
|
||||
Interlaced: true,
|
||||
}
|
||||
}
|
||||
|
||||
// NewDefaultPNGExportParams creates default values for an export of a PNG image.
|
||||
// By default, govips creates non-interlaced PNGs with a compression of 6/10.
|
||||
// Deprecated: Use NewPngExportParams
|
||||
func NewDefaultPNGExportParams() *ExportParams {
|
||||
return &ExportParams{
|
||||
Format: ImageTypePNG,
|
||||
Compression: 6,
|
||||
Interlaced: false,
|
||||
}
|
||||
}
|
||||
|
||||
// NewDefaultWEBPExportParams creates default values for an export of a WEBP image.
|
||||
// By default, govips creates lossy images with a quality of 75/100.
|
||||
// Deprecated: Use NewWebpExportParams
|
||||
func NewDefaultWEBPExportParams() *ExportParams {
|
||||
return &ExportParams{
|
||||
Format: ImageTypeWEBP,
|
||||
Quality: 75,
|
||||
Lossless: false,
|
||||
Effort: 4,
|
||||
}
|
||||
}
|
||||
|
||||
// JpegExportParams are options when exporting a JPEG to file or buffer
|
||||
type JpegExportParams struct {
|
||||
StripMetadata bool
|
||||
Quality int
|
||||
Interlace bool
|
||||
OptimizeCoding bool
|
||||
SubsampleMode SubsampleMode
|
||||
TrellisQuant bool
|
||||
OvershootDeringing bool
|
||||
OptimizeScans bool
|
||||
QuantTable int
|
||||
|
||||
// Keep selects which metadata to retain (libvips's "keep" option; see
|
||||
// ForeignKeep). The zero value leaves it unset, so StripMetadata (where
|
||||
// present) decides as before. When set, it takes precedence over
|
||||
// StripMetadata. Requires libvips 8.15+; saving returns an error if Keep
|
||||
// is set on an older libvips.
|
||||
Keep ForeignKeep
|
||||
}
|
||||
|
||||
// NewJpegExportParams creates default values for an export of a JPEG image.
|
||||
// By default, govips creates interlaced JPEGs with a quality of 80/100.
|
||||
func NewJpegExportParams() *JpegExportParams {
|
||||
return &JpegExportParams{
|
||||
Quality: 80,
|
||||
Interlace: true,
|
||||
}
|
||||
}
|
||||
|
||||
// PngExportParams are options when exporting a PNG to file or buffer
|
||||
type PngExportParams struct {
|
||||
StripMetadata bool
|
||||
Compression int
|
||||
Filter PngFilter
|
||||
Interlace bool
|
||||
Quality int
|
||||
Palette bool
|
||||
Dither float64
|
||||
Bitdepth int
|
||||
Profile string
|
||||
|
||||
// Keep selects which metadata to retain (libvips's "keep" option; see
|
||||
// ForeignKeep). The zero value leaves it unset, so StripMetadata (where
|
||||
// present) decides as before. When set, it takes precedence over
|
||||
// StripMetadata. Requires libvips 8.15+; saving returns an error if Keep
|
||||
// is set on an older libvips.
|
||||
Keep ForeignKeep
|
||||
}
|
||||
|
||||
// NewPngExportParams creates default values for an export of a PNG image.
|
||||
// By default, govips creates non-interlaced PNGs with a compression of 6/10.
|
||||
func NewPngExportParams() *PngExportParams {
|
||||
return &PngExportParams{
|
||||
Compression: 6,
|
||||
Filter: PngFilterNone,
|
||||
Interlace: false,
|
||||
Palette: false,
|
||||
}
|
||||
}
|
||||
|
||||
// WebpExportParams are options when exporting a WEBP to file or buffer
|
||||
// see https://www.libvips.org/API/current/VipsForeignSave.html#vips-webpsave
|
||||
// for details on each parameter
|
||||
type WebpExportParams struct {
|
||||
StripMetadata bool
|
||||
Quality int
|
||||
Lossless bool
|
||||
NearLossless bool
|
||||
ReductionEffort int
|
||||
IccProfile string
|
||||
MinSize bool
|
||||
MinKeyFrames int
|
||||
MaxKeyFrames int
|
||||
// TargetSize is the desired output size in bytes (libvips's webpsave
|
||||
// "target_size" option, passed straight through to libwebp's own
|
||||
// WebPConfig.target_size). When set (> 0), it takes precedence over
|
||||
// Quality: libwebp runs its own internal search over the quality range,
|
||||
// using more encoding passes, to produce output close to this size. It
|
||||
// is a best effort: the resulting size may still land above or below
|
||||
// the target. Leave at 0 (the default) to encode at Quality instead.
|
||||
//
|
||||
// Requires libvips 8.17.4+ (when target_size was added to webpsave);
|
||||
// ExportWebp returns an error if TargetSize is set on an older libvips.
|
||||
TargetSize int
|
||||
|
||||
// Keep selects which metadata to retain (libvips's "keep" option; see
|
||||
// ForeignKeep). The zero value leaves it unset, so StripMetadata decides,
|
||||
// as for the other formats: the source image's ICC profile is kept
|
||||
// unless StripMetadata is set. A profile from IccProfile or
|
||||
// OptimizeICCProfile replaces the source profile and is embedded even
|
||||
// when StripMetadata is set. When set, Keep takes precedence over
|
||||
// StripMetadata, and it
|
||||
// alone decides whether an ICC profile is written: without
|
||||
// ForeignKeepIcc no profile is embedded, whatever its source. With
|
||||
// ForeignKeepIcc, the embedded profile is the one the path already
|
||||
// chooses: ExportWebp uses the profile picked by OptimizeICCProfile
|
||||
// (and ignores IccProfile, as before), while SaveToWriterWebp uses
|
||||
// IccProfile. With neither, the source image's profile is kept.
|
||||
// Requires libvips 8.15+; saving returns an error if Keep is set on an
|
||||
// older libvips.
|
||||
Keep ForeignKeep
|
||||
}
|
||||
|
||||
// NewWebpExportParams creates default values for an export of a WEBP image.
|
||||
// By default, govips creates lossy images with a quality of 75/100.
|
||||
func NewWebpExportParams() *WebpExportParams {
|
||||
return &WebpExportParams{
|
||||
Quality: 75,
|
||||
Lossless: false,
|
||||
NearLossless: false,
|
||||
ReductionEffort: 4,
|
||||
}
|
||||
}
|
||||
|
||||
// TiffExportParams are options when exporting a TIFF to file or buffer
|
||||
type TiffExportParams struct {
|
||||
StripMetadata bool
|
||||
Quality int
|
||||
Compression TiffCompression
|
||||
Predictor TiffPredictor
|
||||
Pyramid bool
|
||||
Tile bool
|
||||
TileHeight int
|
||||
TileWidth int
|
||||
|
||||
// Keep selects which metadata to retain (libvips's "keep" option; see
|
||||
// ForeignKeep). The zero value leaves it unset, so StripMetadata (where
|
||||
// present) decides as before. When set, it takes precedence over
|
||||
// StripMetadata. Requires libvips 8.15+; saving returns an error if Keep
|
||||
// is set on an older libvips.
|
||||
Keep ForeignKeep
|
||||
}
|
||||
|
||||
// NewTiffExportParams creates default values for an export of a TIFF image.
|
||||
func NewTiffExportParams() *TiffExportParams {
|
||||
return &TiffExportParams{
|
||||
Quality: 80,
|
||||
Compression: TiffCompressionLzw,
|
||||
Predictor: TiffPredictorHorizontal,
|
||||
Pyramid: false,
|
||||
Tile: false,
|
||||
TileHeight: 256,
|
||||
TileWidth: 256,
|
||||
}
|
||||
}
|
||||
|
||||
// GifExportParams are options when exporting a GIF to file or buffer.
|
||||
//
|
||||
// For vips 8.12+, native gifsave is used. The relevant parameters are Dither,
|
||||
// Effort, and Bitdepth. Quality is ignored because native gifsave does not
|
||||
// support a quality parameter.
|
||||
//
|
||||
// For vips below 8.12, magicksave is used as a fallback. The relevant
|
||||
// parameters are Quality and Bitdepth.
|
||||
//
|
||||
// StripMetadata has no effect on GIF images.
|
||||
type GifExportParams struct {
|
||||
StripMetadata bool
|
||||
// Quality is only used with vips < 8.12 (magicksave fallback).
|
||||
// Ignored by native gifsave in vips 8.12+.
|
||||
Quality int
|
||||
Dither float64
|
||||
Effort int
|
||||
Bitdepth int
|
||||
|
||||
// Keep selects which metadata to retain (libvips's "keep" option; see
|
||||
// ForeignKeep). The zero value leaves it unset, so StripMetadata (where
|
||||
// present) decides as before. When set, it takes precedence over
|
||||
// StripMetadata. Requires libvips 8.15+; saving returns an error if Keep
|
||||
// is set on an older libvips.
|
||||
Keep ForeignKeep
|
||||
}
|
||||
|
||||
// NewGifExportParams creates default values for an export of a GIF image.
|
||||
func NewGifExportParams() *GifExportParams {
|
||||
return &GifExportParams{
|
||||
Quality: 75,
|
||||
Effort: 7,
|
||||
Bitdepth: 8,
|
||||
}
|
||||
}
|
||||
|
||||
// HeifExportParams are options when exporting a HEIF to file or buffer
|
||||
type HeifExportParams struct {
|
||||
Quality int
|
||||
Bitdepth int
|
||||
Effort int
|
||||
Lossless bool
|
||||
|
||||
// Keep selects which metadata to retain (libvips's "keep" option; see
|
||||
// ForeignKeep). The zero value leaves it unset, so StripMetadata (where
|
||||
// present) decides as before. When set, it takes precedence over
|
||||
// StripMetadata. Requires libvips 8.15+; saving returns an error if Keep
|
||||
// is set on an older libvips.
|
||||
Keep ForeignKeep
|
||||
}
|
||||
|
||||
// NewHeifExportParams creates default values for an export of a HEIF image.
|
||||
func NewHeifExportParams() *HeifExportParams {
|
||||
return &HeifExportParams{
|
||||
Quality: 80,
|
||||
Bitdepth: 8,
|
||||
Effort: 5,
|
||||
Lossless: false,
|
||||
}
|
||||
}
|
||||
|
||||
// AvifExportParams are options when exporting an AVIF to file or buffer.
|
||||
type AvifExportParams struct {
|
||||
StripMetadata bool
|
||||
Quality int
|
||||
Bitdepth int
|
||||
Effort int
|
||||
Lossless bool
|
||||
|
||||
// DEPRECATED - Use Effort instead.
|
||||
Speed int
|
||||
|
||||
// Keep selects which metadata to retain (libvips's "keep" option; see
|
||||
// ForeignKeep). The zero value leaves it unset, so StripMetadata (where
|
||||
// present) decides as before. When set, it takes precedence over
|
||||
// StripMetadata. Requires libvips 8.15+; saving returns an error if Keep
|
||||
// is set on an older libvips.
|
||||
Keep ForeignKeep
|
||||
}
|
||||
|
||||
// NewAvifExportParams creates default values for an export of an AVIF image.
|
||||
func NewAvifExportParams() *AvifExportParams {
|
||||
return &AvifExportParams{
|
||||
Quality: 80,
|
||||
Bitdepth: 8,
|
||||
Effort: 5,
|
||||
Lossless: false,
|
||||
}
|
||||
}
|
||||
|
||||
// Jp2kExportParams are options when exporting an JPEG2000 to file or buffer.
|
||||
type Jp2kExportParams struct {
|
||||
Quality int
|
||||
Lossless bool
|
||||
TileWidth int
|
||||
TileHeight int
|
||||
SubsampleMode SubsampleMode
|
||||
|
||||
// Keep selects which metadata to retain (libvips's "keep" option; see
|
||||
// ForeignKeep). The zero value leaves it unset, so StripMetadata (where
|
||||
// present) decides as before. When set, it takes precedence over
|
||||
// StripMetadata. Requires libvips 8.15+; saving returns an error if Keep
|
||||
// is set on an older libvips.
|
||||
Keep ForeignKeep
|
||||
}
|
||||
|
||||
// NewJp2kExportParams creates default values for an export of an JPEG2000 image.
|
||||
func NewJp2kExportParams() *Jp2kExportParams {
|
||||
return &Jp2kExportParams{
|
||||
Quality: 80,
|
||||
Lossless: false,
|
||||
TileWidth: 512,
|
||||
TileHeight: 512,
|
||||
}
|
||||
}
|
||||
|
||||
// JxlExportParams are options when exporting an JXL to file or buffer.
|
||||
type JxlExportParams struct {
|
||||
Quality int
|
||||
Lossless bool
|
||||
Tier int
|
||||
Distance float64
|
||||
Effort int
|
||||
|
||||
// Keep selects which metadata to retain (libvips's "keep" option; see
|
||||
// ForeignKeep). The zero value leaves it unset, so StripMetadata (where
|
||||
// present) decides as before. When set, it takes precedence over
|
||||
// StripMetadata. Requires libvips 8.15+; saving returns an error if Keep
|
||||
// is set on an older libvips.
|
||||
Keep ForeignKeep
|
||||
}
|
||||
|
||||
// NewJxlExportParams creates default values for an export of an JXL image.
|
||||
func NewJxlExportParams() *JxlExportParams {
|
||||
return &JxlExportParams{
|
||||
Quality: 75,
|
||||
Lossless: false,
|
||||
Effort: 7,
|
||||
Distance: 1.0,
|
||||
}
|
||||
}
|
||||
|
||||
// MagickExportParams are options when exporting an image to file or buffer by ImageMagick.
|
||||
type MagickExportParams struct {
|
||||
Quality int
|
||||
Format string
|
||||
OptimizeGifFrames bool
|
||||
OptimizeGifTransparency bool
|
||||
BitDepth int
|
||||
|
||||
// Keep selects which metadata to retain (libvips's "keep" option; see
|
||||
// ForeignKeep). The zero value leaves it unset, so StripMetadata (where
|
||||
// present) decides as before. When set, it takes precedence over
|
||||
// StripMetadata. Requires libvips 8.15+; saving returns an error if Keep
|
||||
// is set on an older libvips.
|
||||
Keep ForeignKeep
|
||||
}
|
||||
|
||||
// NewMagickExportParams creates default values for an export of an image by ImageMagick.
|
||||
func NewMagickExportParams() *MagickExportParams {
|
||||
return &MagickExportParams{
|
||||
Quality: 75,
|
||||
}
|
||||
}
|
||||
|
||||
// NewImageFromReader loads an ImageRef from the given reader by reading
|
||||
// the entire stream into memory first (io.ReadAll). For large inputs
|
||||
// prefer LoadImageFromReader, which streams compressed bytes through
|
||||
// libvips without buffering the whole input in Go memory.
|
||||
func NewImageFromReader(r io.Reader) (*ImageRef, error) {
|
||||
buf, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewImageFromBuffer(buf)
|
||||
}
|
||||
|
||||
// NewImageFromFile loads an image from file and creates a new ImageRef
|
||||
func NewImageFromFile(file string) (*ImageRef, error) {
|
||||
return LoadImageFromFile(file, nil)
|
||||
}
|
||||
|
||||
// LoadImageFromFile loads an image from file and creates a new ImageRef
|
||||
func LoadImageFromFile(file string, params *ImportParams) (*ImageRef, error) {
|
||||
buf, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
govipsLog("govips", LogLevelDebug, fmt.Sprintf("creating imageRef from file %s", file))
|
||||
return LoadImageFromBuffer(buf, params)
|
||||
}
|
||||
|
||||
// NewImageFromBuffer loads an image buffer and creates a new Image
|
||||
func NewImageFromBuffer(buf []byte) (*ImageRef, error) {
|
||||
return LoadImageFromBuffer(buf, nil)
|
||||
}
|
||||
|
||||
// LoadImageFromBuffer loads an image buffer and creates a new Image
|
||||
func LoadImageFromBuffer(buf []byte, params *ImportParams) (*ImageRef, error) {
|
||||
if err := startupIfNeeded(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if params == nil {
|
||||
params = NewImportParams()
|
||||
}
|
||||
|
||||
vipsImage, currentFormat, originalFormat, err := vipsLoadFromBuffer(buf, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ref := newImageRef(vipsImage, currentFormat, originalFormat, buf)
|
||||
|
||||
govipsLog("govips", LogLevelDebug, fmt.Sprintf("created imageRef %p", ref))
|
||||
return ref, nil
|
||||
}
|
||||
|
||||
// NewThumbnailFromFile loads an image from file and creates a new ImageRef with thumbnail crop
|
||||
func NewThumbnailFromFile(file string, width, height int, crop Interesting) (*ImageRef, error) {
|
||||
return LoadThumbnailFromFile(file, width, height, crop, SizeBoth, nil)
|
||||
}
|
||||
|
||||
// NewThumbnailFromBuffer loads an image buffer and creates a new Image with thumbnail crop
|
||||
func NewThumbnailFromBuffer(buf []byte, width, height int, crop Interesting) (*ImageRef, error) {
|
||||
return LoadThumbnailFromBuffer(buf, width, height, crop, SizeBoth, nil)
|
||||
}
|
||||
|
||||
// NewThumbnailWithSizeFromFile loads an image from file and creates a new ImageRef with thumbnail crop and size
|
||||
func NewThumbnailWithSizeFromFile(file string, width, height int, crop Interesting, size Size) (*ImageRef, error) {
|
||||
return LoadThumbnailFromFile(file, width, height, crop, size, nil)
|
||||
}
|
||||
|
||||
// LoadThumbnailFromFile loads an image from file and creates a new ImageRef with thumbnail crop and size
|
||||
func LoadThumbnailFromFile(file string, width, height int, crop Interesting, size Size, params *ImportParams) (*ImageRef, error) {
|
||||
if err := startupIfNeeded(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
vipsImage, format, err := vipsThumbnailFromFile(file, width, height, crop, size, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ref := newImageRef(vipsImage, format, format, nil)
|
||||
|
||||
govipsLog("govips", LogLevelDebug, fmt.Sprintf("created imageref %p", ref))
|
||||
return ref, nil
|
||||
}
|
||||
|
||||
// NewThumbnailWithSizeFromBuffer loads an image buffer and creates a new Image with thumbnail crop and size
|
||||
func NewThumbnailWithSizeFromBuffer(buf []byte, width, height int, crop Interesting, size Size) (*ImageRef, error) {
|
||||
return LoadThumbnailFromBuffer(buf, width, height, crop, size, nil)
|
||||
}
|
||||
|
||||
// LoadThumbnailFromBuffer loads an image buffer and creates a new Image with thumbnail crop and size
|
||||
func LoadThumbnailFromBuffer(buf []byte, width, height int, crop Interesting, size Size, params *ImportParams) (*ImageRef, error) {
|
||||
if err := startupIfNeeded(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
vipsImage, format, err := vipsThumbnailFromBuffer(buf, width, height, crop, size, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ref := newImageRef(vipsImage, format, format, buf)
|
||||
|
||||
govipsLog("govips", LogLevelDebug, fmt.Sprintf("created imageref %p", ref))
|
||||
return ref, nil
|
||||
}
|
||||
|
||||
// Metadata returns the metadata (ImageMetadata struct) of the associated ImageRef
|
||||
func (r *ImageRef) Metadata() *ImageMetadata {
|
||||
return &ImageMetadata{
|
||||
Format: r.Format(),
|
||||
Width: r.Width(),
|
||||
Height: r.Height(),
|
||||
Orientation: r.Orientation(),
|
||||
Colorspace: r.ColorSpace(),
|
||||
Pages: r.Pages(),
|
||||
}
|
||||
}
|
||||
|
||||
// Copy creates a new copy of the given image.
|
||||
func (r *ImageRef) Copy() (*ImageRef, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenCopy(r.image, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return newImageRef(out, r.format, r.originalFormat, r.buf), nil
|
||||
}
|
||||
|
||||
// Copy creates a new copy of the given image with the new X and Y resolution (PPI).
|
||||
func (r *ImageRef) CopyChangingResolution(xres, yres float64) (*ImageRef, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenCopy(r.image, &CopyOptions{Xres: &xres, Yres: &yres})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return newImageRef(out, r.format, r.originalFormat, r.buf), nil
|
||||
}
|
||||
|
||||
// Copy creates a new copy of the given image with the interpretation.
|
||||
func (r *ImageRef) CopyChangingInterpretation(interpretation Interpretation) (*ImageRef, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenCopy(r.image, &CopyOptions{Interpretation: &interpretation})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return newImageRef(out, r.format, r.originalFormat, r.buf), nil
|
||||
}
|
||||
|
||||
// XYZ creates a two-band uint32 image where the elements in the first band have the value of their x coordinate
|
||||
// and elements in the second band have their y coordinate.
|
||||
func XYZ(width, height int) (*ImageRef, error) {
|
||||
vipsImage, err := vipsGenXyz(width, height, nil)
|
||||
return newImageRef(vipsImage, ImageTypeUnknown, ImageTypeUnknown, nil), err
|
||||
}
|
||||
|
||||
// Identity creates an identity lookup table, which will leave an image unchanged when applied with Maplut.
|
||||
// Each entry in the table has a value equal to its position.
|
||||
func Identity(ushort bool) (*ImageRef, error) {
|
||||
img, err := vipsGenIdentity(&IdentityOptions{Ushort: &ushort})
|
||||
return newImageRef(img, ImageTypeUnknown, ImageTypeUnknown, nil), err
|
||||
}
|
||||
|
||||
// Black creates a new black image of the specified size
|
||||
func Black(width, height int) (*ImageRef, error) {
|
||||
vipsImage, err := vipsGenBlack(width, height, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newImageRef(vipsImage, ImageTypeUnknown, ImageTypeUnknown, nil), nil
|
||||
}
|
||||
|
||||
// Grey creates a horizontal gradient image (ramp from black to white).
|
||||
// When uchar is true, pixel values are 0-255 uint8; when false, 0.0-1.0 float.
|
||||
// Useful for creating gradient overlays when combined with rotation, BandJoin, and Composite.
|
||||
func Grey(width, height int, uchar bool) (*ImageRef, error) {
|
||||
img, err := vipsGenGrey(width, height, &GreyOptions{Uchar: &uchar})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newImageRef(img, ImageTypeUnknown, ImageTypeUnknown, nil), nil
|
||||
}
|
||||
|
||||
// NewTransparentCanvas creates a fully transparent RGBA image of the given dimensions.
|
||||
// The image is in sRGB color space with 4 bands (RGBA), all channels set to 0.
|
||||
func NewTransparentCanvas(width, height int) (*ImageRef, error) {
|
||||
ref, err := Black(width, height)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := ref.ToColorSpace(InterpretationSRGB); err != nil {
|
||||
ref.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := ref.BandJoinConst([]float64{0}); err != nil {
|
||||
ref.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ref, nil
|
||||
}
|
||||
|
||||
// Text draws the string text to an image.
|
||||
func Text(params *TextParams) (*ImageRef, error) {
|
||||
img, err := vipsText(params)
|
||||
return newImageRef(img, ImageTypeUnknown, ImageTypeUnknown, nil), err
|
||||
}
|
||||
|
||||
// NewImageFromGoImage creates a new ImageRef from a Go image.Image.
|
||||
// The image is normalized to NRGBA (non-premultiplied RGBA, 8-bit) and
|
||||
// imported into libvips in sRGB color space.
|
||||
func NewImageFromGoImage(img image.Image) (*ImageRef, error) {
|
||||
if err := startupIfNeeded(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bounds := img.Bounds()
|
||||
width := bounds.Dx()
|
||||
height := bounds.Dy()
|
||||
|
||||
if width == 0 || height == 0 {
|
||||
return nil, errors.New("image has zero dimensions")
|
||||
}
|
||||
|
||||
// Normalize to NRGBA
|
||||
var nrgba *image.NRGBA
|
||||
if n, ok := img.(*image.NRGBA); ok && n.Rect.Min.X == 0 && n.Rect.Min.Y == 0 && n.Stride == width*4 {
|
||||
nrgba = n
|
||||
} else {
|
||||
nrgba = image.NewNRGBA(image.Rect(0, 0, width, height))
|
||||
draw.Draw(nrgba, nrgba.Bounds(), img, bounds.Min, draw.Src)
|
||||
}
|
||||
|
||||
// Create vips image from pixel data (copies data, safe for Go GC)
|
||||
vipsImage := C.create_image_from_memory_copy(
|
||||
unsafe.Pointer(&nrgba.Pix[0]),
|
||||
C.size_t(len(nrgba.Pix)),
|
||||
C.int(width),
|
||||
C.int(height),
|
||||
4,
|
||||
C.VIPS_FORMAT_UCHAR,
|
||||
)
|
||||
runtime.KeepAlive(nrgba)
|
||||
if vipsImage == nil {
|
||||
return nil, errors.New("failed to create vips image from memory")
|
||||
}
|
||||
|
||||
// Set interpretation to sRGB
|
||||
vipsImage.Type = C.VIPS_INTERPRETATION_sRGB
|
||||
|
||||
return newImageRef(vipsImage, ImageTypeUnknown, ImageTypeUnknown, nil), nil
|
||||
}
|
||||
|
||||
// NewImageFromMemory creates a new ImageRef from a raw, uncompressed pixel
|
||||
// buffer. The buffer is interpreted as height rows of width pixels, each pixel
|
||||
// holding bands samples of the given BandFormat, laid out contiguously (no row
|
||||
// stride padding).
|
||||
//
|
||||
// Use this for raw byte arrays from sources like ML inference outputs (ONNX,
|
||||
// TensorFlow), camera sensors, or any in-memory pixel data. For Go's
|
||||
// image.Image, use NewImageFromGoImage. For compressed bytes (PNG, JPEG, etc.),
|
||||
// use NewImageFromBuffer.
|
||||
//
|
||||
// The pixel data is copied into libvips, so the caller is free to reuse or
|
||||
// free the input slice after this call returns.
|
||||
func NewImageFromMemory(pixels []byte, width, height, bands int, format BandFormat, interpretation Interpretation) (*ImageRef, error) {
|
||||
if err := startupIfNeeded(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if width <= 0 || height <= 0 || bands <= 0 {
|
||||
return nil, fmt.Errorf("invalid dimensions: width=%d height=%d bands=%d", width, height, bands)
|
||||
}
|
||||
|
||||
bytesPerSample, ok := bandFormatSizes[format]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unsupported band format: %d", format)
|
||||
}
|
||||
|
||||
expected := width * height * bands * bytesPerSample
|
||||
if len(pixels) != expected {
|
||||
return nil, fmt.Errorf("pixel buffer size mismatch: got %d bytes, expected %d (%dx%dx%d samples of %d bytes)",
|
||||
len(pixels), expected, width, height, bands, bytesPerSample)
|
||||
}
|
||||
|
||||
vipsImage := C.create_image_from_memory_copy(
|
||||
unsafe.Pointer(&pixels[0]),
|
||||
C.size_t(len(pixels)),
|
||||
C.int(width),
|
||||
C.int(height),
|
||||
C.int(bands),
|
||||
C.VipsBandFormat(format),
|
||||
)
|
||||
runtime.KeepAlive(pixels)
|
||||
if vipsImage == nil {
|
||||
return nil, errors.New("failed to create vips image from memory")
|
||||
}
|
||||
|
||||
vipsImage.Type = C.VipsInterpretation(interpretation)
|
||||
|
||||
return newImageRef(vipsImage, ImageTypeUnknown, ImageTypeUnknown, nil), nil
|
||||
}
|
||||
|
||||
// bandFormatSizes is the byte size of one sample for each supported BandFormat.
|
||||
// Complex/DPComplex are excluded — vips_image_new_from_memory rejects them in
|
||||
// practice and the size depends on real/imag pairing semantics.
|
||||
var bandFormatSizes = map[BandFormat]int{
|
||||
BandFormatUchar: 1,
|
||||
BandFormatChar: 1,
|
||||
BandFormatUshort: 2,
|
||||
BandFormatShort: 2,
|
||||
BandFormatUint: 4,
|
||||
BandFormatInt: 4,
|
||||
BandFormatFloat: 4,
|
||||
BandFormatDouble: 8,
|
||||
}
|
||||
|
||||
func newImageRef(vipsImage *C.VipsImage, currentFormat ImageType, originalFormat ImageType, buf []byte) *ImageRef {
|
||||
imageRef := &ImageRef{
|
||||
image: vipsImage,
|
||||
format: currentFormat,
|
||||
originalFormat: originalFormat,
|
||||
buf: buf,
|
||||
}
|
||||
openImageRefs.Add(1)
|
||||
runtime.SetFinalizer(imageRef, finalizeImage)
|
||||
|
||||
return imageRef
|
||||
}
|
||||
|
||||
func finalizeImage(ref *ImageRef) {
|
||||
govipsLog("govips", LogLevelDebug, fmt.Sprintf("closing image %p", ref))
|
||||
ref.Close()
|
||||
}
|
||||
|
||||
// Close manually closes the image and frees the memory. Calling Close() is optional.
|
||||
// Images are automatically closed by GC. However, in high volume applications the GC
|
||||
// can't keep up with the amount of memory, so you might want to manually close the images.
|
||||
func (r *ImageRef) Close() {
|
||||
r.lock.Lock()
|
||||
|
||||
if r.image != nil {
|
||||
clearImage(r.image)
|
||||
r.image = nil
|
||||
openImageRefs.Add(-1)
|
||||
}
|
||||
|
||||
r.buf = nil
|
||||
|
||||
// Release the streaming source only after the image is gone: the
|
||||
// image may still hold references that read from the source.
|
||||
src := r.streamSource
|
||||
r.streamSource = nil
|
||||
|
||||
r.lock.Unlock()
|
||||
|
||||
if src != nil {
|
||||
src.release()
|
||||
}
|
||||
}
|
||||
|
||||
// SetKill sets the libvips kill flag used to stop image evaluation.
|
||||
func (r *ImageRef) SetKill(kill bool) {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
if r.image != nil {
|
||||
C.vips_image_set_kill(r.image, toGboolean(kill))
|
||||
}
|
||||
}
|
||||
|
||||
// setImage resets the image for this image and frees the previous one
|
||||
func (r *ImageRef) setImage(image *C.VipsImage) {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
if r.image == image {
|
||||
return
|
||||
}
|
||||
|
||||
if r.image != nil {
|
||||
clearImage(r.image)
|
||||
}
|
||||
|
||||
r.image = image
|
||||
}
|
||||
|
||||
func vipsHasAlpha(in *C.VipsImage) bool {
|
||||
return int(C.has_alpha_channel(in)) > 0
|
||||
}
|
||||
|
||||
func clearImage(ref *C.VipsImage) {
|
||||
C.clear_image(&ref)
|
||||
}
|
||||
|
||||
// vipsRefCount reads the GObject reference count. Test-only: it lets tests
|
||||
// catch a stray unref without relying on a use-after-free to crash.
|
||||
func vipsRefCount(in *C.VipsImage) int {
|
||||
return int((*C.GObject)(unsafe.Pointer(in)).ref_count)
|
||||
}
|
||||
|
||||
// Coding represents VIPS_CODING type
|
||||
type Coding int
|
||||
|
||||
// Coding enum
|
||||
//
|
||||
//goland:noinspection GoUnusedConst
|
||||
const (
|
||||
CodingError Coding = C.VIPS_CODING_ERROR
|
||||
CodingNone Coding = C.VIPS_CODING_NONE
|
||||
CodingLABQ Coding = C.VIPS_CODING_LABQ
|
||||
CodingRAD Coding = C.VIPS_CODING_RAD
|
||||
)
|
||||
|
||||
func (r *ImageRef) newMetadata(format ImageType) *ImageMetadata {
|
||||
return &ImageMetadata{
|
||||
Format: format,
|
||||
Width: r.Width(),
|
||||
Height: r.Height(),
|
||||
Colorspace: r.ColorSpace(),
|
||||
Orientation: r.Orientation(),
|
||||
Pages: r.Pages(),
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
// https://libvips.github.io/libvips/API/current/VipsImage.html
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <vips/vips.h>
|
||||
|
||||
int has_alpha_channel(VipsImage *image);
|
||||
|
||||
void clear_image(VipsImage **image);
|
||||
|
||||
VipsImage *create_image_from_memory_copy(const void *data, size_t size,
|
||||
int width, int height, int bands,
|
||||
VipsBandFormat format);
|
||||
@@ -1,173 +0,0 @@
|
||||
package vips
|
||||
|
||||
// #include "image.h"
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// ToColorSpace changes the color space of the image to the interpretation supplied as the parameter.
|
||||
func (r *ImageRef) ToColorSpace(interpretation Interpretation) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsToColorSpace(r.image, interpretation)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Flatten removes the alpha channel from the image and replaces it with the background color
|
||||
func (r *ImageRef) Flatten(backgroundColor *Color) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
opts := &FlattenOptions{}
|
||||
if backgroundColor != nil {
|
||||
opts.Background = []float64{float64(backgroundColor.R), float64(backgroundColor.G), float64(backgroundColor.B)}
|
||||
}
|
||||
out, err := vipsGenFlatten(r.image, opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Modulate the colors
|
||||
func (r *ImageRef) Modulate(brightness, saturation, hue float64) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
var err error
|
||||
var multiplications []float64
|
||||
var additions []float64
|
||||
|
||||
colorspace := r.ColorSpace()
|
||||
if colorspace == InterpretationRGB {
|
||||
colorspace = InterpretationSRGB
|
||||
}
|
||||
|
||||
multiplications = []float64{brightness, saturation, 1}
|
||||
additions = []float64{0, 0, hue}
|
||||
|
||||
if r.HasAlpha() {
|
||||
multiplications = append(multiplications, 1)
|
||||
additions = append(additions, 0)
|
||||
}
|
||||
|
||||
err = r.ToColorSpace(InterpretationLCH)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = r.Linear(multiplications, additions)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = r.ToColorSpace(colorspace)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ModulateHSV modulates the image HSV values based on the supplier parameters.
|
||||
func (r *ImageRef) ModulateHSV(brightness, saturation float64, hue int) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
var err error
|
||||
var multiplications []float64
|
||||
var additions []float64
|
||||
|
||||
colorspace := r.ColorSpace()
|
||||
if colorspace == InterpretationRGB {
|
||||
colorspace = InterpretationSRGB
|
||||
}
|
||||
|
||||
if r.HasAlpha() {
|
||||
multiplications = []float64{1, saturation, brightness, 1}
|
||||
additions = []float64{float64(hue), 0, 0, 0}
|
||||
} else {
|
||||
multiplications = []float64{1, saturation, brightness}
|
||||
additions = []float64{float64(hue), 0, 0}
|
||||
}
|
||||
|
||||
err = r.ToColorSpace(InterpretationHSV)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = r.Linear(multiplications, additions)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = r.ToColorSpace(colorspace)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Invert inverts the image
|
||||
func (r *ImageRef) Invert() error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenInvert(r.image)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Adjusts the image's gamma value.
|
||||
// See https://www.libvips.org/API/current/libvips-conversion.html#vips-gamma
|
||||
func (r *ImageRef) Gamma(gamma float64) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenGamma(r.image, &GammaOptions{Exponent: &gamma})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Linear passes an image through a linear transformation (i.e. output = input * a + b).
|
||||
// See https://libvips.github.io/libvips/API/current/libvips-arithmetic.html#vips-linear
|
||||
func (r *ImageRef) Linear(a, b []float64) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
if len(a) != len(b) {
|
||||
return errors.New("a and b must be of same length")
|
||||
}
|
||||
|
||||
out, err := vipsGenLinear(r.image, a, b, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Linear1 runs Linear() with a single constant.
|
||||
// See https://libvips.github.io/libvips/API/current/libvips-arithmetic.html#vips-linear1
|
||||
func (r *ImageRef) Linear1(a, b float64) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenLinear(r.image, []float64{a}, []float64{b}, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Cast converts the image to a target band format
|
||||
func (r *ImageRef) Cast(format BandFormat) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenCast(r.image, format, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
package vips
|
||||
|
||||
// #include "image.h"
|
||||
import "C"
|
||||
|
||||
import "runtime"
|
||||
|
||||
// CompositeMulti composites the given overlay image on top of the associated image with provided blending mode.
|
||||
func (r *ImageRef) CompositeMulti(ins []*ImageComposite) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
defer runtime.KeepAlive(ins)
|
||||
out, err := vipsComposite(toVipsCompositeStructs(r, ins))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Composite composites the given overlay image on top of the associated image with provided blending mode.
|
||||
func (r *ImageRef) Composite(overlay *ImageRef, mode BlendMode, x, y int) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
defer runtime.KeepAlive(overlay)
|
||||
out, err := vipsGenComposite2(r.image, overlay.image, mode, &Composite2Options{X: &x, Y: &y})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Insert draws the image on top of the associated image at the given coordinates.
|
||||
func (r *ImageRef) Insert(sub *ImageRef, x, y int, expand bool, background *ColorRGBA) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
defer runtime.KeepAlive(sub)
|
||||
insertOpts := &InsertOptions{Expand: &expand}
|
||||
if background != nil {
|
||||
insertOpts.Background = []float64{float64(background.R), float64(background.G), float64(background.B), float64(background.A)}
|
||||
}
|
||||
out, err := vipsGenInsert(r.image, sub.image, x, y, insertOpts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Join joins this image with another in the direction specified
|
||||
func (r *ImageRef) Join(in *ImageRef, dir Direction) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
defer runtime.KeepAlive(in)
|
||||
out, err := vipsJoin(r.image, in.image, dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ArrayJoin joins an array of images together wrapping at each n images
|
||||
func (r *ImageRef) ArrayJoin(images []*ImageRef, across int) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
defer runtime.KeepAlive(images)
|
||||
allImages := append([]*ImageRef{r}, images...)
|
||||
inputs := make([]*C.VipsImage, len(allImages))
|
||||
for i := range inputs {
|
||||
inputs[i] = allImages[i].image
|
||||
}
|
||||
out, err := vipsGenArrayjoin(inputs, &ArrayjoinOptions{Across: &across})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
@@ -1,439 +0,0 @@
|
||||
package vips
|
||||
|
||||
// #include "image.h"
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Export creates a byte array of the image for use.
|
||||
// The function returns a byte array that can be written to a file e.g. via os.WriteFile().
|
||||
// N.B. govips does not currently have built-in support for directly exporting to a file.
|
||||
// The function also returns a copy of the image metadata as well as an error.
|
||||
// Deprecated: Use ExportNative or format-specific Export methods
|
||||
func (r *ImageRef) Export(params *ExportParams) ([]byte, *ImageMetadata, error) {
|
||||
if params == nil || params.Format == ImageTypeUnknown {
|
||||
return r.ExportNative()
|
||||
}
|
||||
|
||||
format := params.Format
|
||||
|
||||
if !IsTypeSupported(format) {
|
||||
return nil, r.newMetadata(ImageTypeUnknown), fmt.Errorf("cannot save to %#v", ImageTypes[format])
|
||||
}
|
||||
|
||||
switch format {
|
||||
case ImageTypeGIF:
|
||||
return r.ExportGIF(gifParamsFromExport(params))
|
||||
case ImageTypeWEBP:
|
||||
return r.ExportWebp(webpParamsFromExport(params))
|
||||
case ImageTypePNG:
|
||||
return r.ExportPng(pngParamsFromExport(params))
|
||||
case ImageTypeTIFF:
|
||||
return r.ExportTiff(tiffParamsFromExport(params))
|
||||
case ImageTypeHEIF:
|
||||
return r.ExportHeif(heifParamsFromExport(params))
|
||||
case ImageTypeAVIF:
|
||||
return r.ExportAvif(&AvifExportParams{
|
||||
StripMetadata: params.StripMetadata,
|
||||
Quality: params.Quality,
|
||||
Lossless: params.Lossless,
|
||||
Speed: params.Speed,
|
||||
})
|
||||
case ImageTypeJXL:
|
||||
return r.ExportJxl(&JxlExportParams{
|
||||
Quality: params.Quality,
|
||||
Lossless: params.Lossless,
|
||||
Effort: params.Effort,
|
||||
})
|
||||
default:
|
||||
format = ImageTypeJPEG
|
||||
return r.ExportJpeg(jpegParamsFromExport(params))
|
||||
}
|
||||
}
|
||||
|
||||
// The *ParamsFromExport helpers map the generic (deprecated) ExportParams
|
||||
// onto format-specific params. They are shared between Export and the
|
||||
// streaming SaveToWriter so the two paths cannot drift. A nil params
|
||||
// yields the format's defaults.
|
||||
|
||||
func jpegParamsFromExport(params *ExportParams) *JpegExportParams {
|
||||
if params == nil {
|
||||
return NewJpegExportParams()
|
||||
}
|
||||
return &JpegExportParams{
|
||||
Quality: params.Quality,
|
||||
StripMetadata: params.StripMetadata,
|
||||
Interlace: params.Interlaced,
|
||||
OptimizeCoding: params.OptimizeCoding,
|
||||
SubsampleMode: params.SubsampleMode,
|
||||
TrellisQuant: params.TrellisQuant,
|
||||
OvershootDeringing: params.OvershootDeringing,
|
||||
OptimizeScans: params.OptimizeScans,
|
||||
QuantTable: params.QuantTable,
|
||||
}
|
||||
}
|
||||
|
||||
func pngParamsFromExport(params *ExportParams) *PngExportParams {
|
||||
if params == nil {
|
||||
return NewPngExportParams()
|
||||
}
|
||||
return &PngExportParams{
|
||||
StripMetadata: params.StripMetadata,
|
||||
Compression: params.Compression,
|
||||
Interlace: params.Interlaced,
|
||||
}
|
||||
}
|
||||
|
||||
func webpParamsFromExport(params *ExportParams) *WebpExportParams {
|
||||
if params == nil {
|
||||
return NewWebpExportParams()
|
||||
}
|
||||
return &WebpExportParams{
|
||||
StripMetadata: params.StripMetadata,
|
||||
Quality: params.Quality,
|
||||
Lossless: params.Lossless,
|
||||
ReductionEffort: params.Effort,
|
||||
}
|
||||
}
|
||||
|
||||
func tiffParamsFromExport(params *ExportParams) *TiffExportParams {
|
||||
if params == nil {
|
||||
return NewTiffExportParams()
|
||||
}
|
||||
compression := TiffCompressionLzw
|
||||
if params.Lossless {
|
||||
compression = TiffCompressionNone
|
||||
}
|
||||
return &TiffExportParams{
|
||||
StripMetadata: params.StripMetadata,
|
||||
Quality: params.Quality,
|
||||
Compression: compression,
|
||||
}
|
||||
}
|
||||
|
||||
func heifParamsFromExport(params *ExportParams) *HeifExportParams {
|
||||
if params == nil {
|
||||
return NewHeifExportParams()
|
||||
}
|
||||
return &HeifExportParams{
|
||||
Quality: params.Quality,
|
||||
Lossless: params.Lossless,
|
||||
}
|
||||
}
|
||||
|
||||
func gifParamsFromExport(params *ExportParams) *GifExportParams {
|
||||
if params == nil {
|
||||
return NewGifExportParams()
|
||||
}
|
||||
return &GifExportParams{
|
||||
Quality: params.Quality,
|
||||
}
|
||||
}
|
||||
|
||||
// ExportNative exports the image to a buffer based on its native format with default parameters.
|
||||
func (r *ImageRef) ExportNative() ([]byte, *ImageMetadata, error) {
|
||||
switch r.format {
|
||||
case ImageTypeJPEG:
|
||||
return r.ExportJpeg(NewJpegExportParams())
|
||||
case ImageTypePNG:
|
||||
return r.ExportPng(NewPngExportParams())
|
||||
case ImageTypeWEBP:
|
||||
return r.ExportWebp(NewWebpExportParams())
|
||||
case ImageTypeHEIF:
|
||||
return r.ExportHeif(NewHeifExportParams())
|
||||
case ImageTypeTIFF:
|
||||
return r.ExportTiff(NewTiffExportParams())
|
||||
case ImageTypeAVIF:
|
||||
return r.ExportAvif(NewAvifExportParams())
|
||||
case ImageTypeJP2K:
|
||||
return r.ExportJp2k(NewJp2kExportParams())
|
||||
case ImageTypeGIF:
|
||||
return r.ExportGIF(NewGifExportParams())
|
||||
case ImageTypeJXL:
|
||||
return r.ExportJxl(NewJxlExportParams())
|
||||
default:
|
||||
return r.ExportJpeg(NewJpegExportParams())
|
||||
}
|
||||
}
|
||||
|
||||
// ExportJpeg exports the image as JPEG to a buffer.
|
||||
func (r *ImageRef) ExportJpeg(params *JpegExportParams) ([]byte, *ImageMetadata, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
if params == nil {
|
||||
params = NewJpegExportParams()
|
||||
}
|
||||
|
||||
buf, err := vipsSaveJPEGToBuffer(r.image, *params)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return buf, r.newMetadata(ImageTypeJPEG), nil
|
||||
}
|
||||
|
||||
// ExportPng exports the image as PNG to a buffer.
|
||||
func (r *ImageRef) ExportPng(params *PngExportParams) ([]byte, *ImageMetadata, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
if params == nil {
|
||||
params = NewPngExportParams()
|
||||
}
|
||||
|
||||
buf, err := vipsSavePNGToBuffer(r.image, *params)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return buf, r.newMetadata(ImageTypePNG), nil
|
||||
}
|
||||
|
||||
// ExportWebp exports the image as WEBP to a buffer.
|
||||
func (r *ImageRef) ExportWebp(params *WebpExportParams) ([]byte, *ImageMetadata, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
if params == nil {
|
||||
params = NewWebpExportParams()
|
||||
}
|
||||
|
||||
paramsWithIccProfile := *params
|
||||
paramsWithIccProfile.IccProfile = r.optimizedIccProfile
|
||||
|
||||
buf, err := vipsSaveWebPToBuffer(r.image, paramsWithIccProfile)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return buf, r.newMetadata(ImageTypeWEBP), nil
|
||||
}
|
||||
|
||||
// ExportHeif exports the image as HEIF to a buffer.
|
||||
func (r *ImageRef) ExportHeif(params *HeifExportParams) ([]byte, *ImageMetadata, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
if params == nil {
|
||||
params = NewHeifExportParams()
|
||||
}
|
||||
|
||||
buf, err := vipsSaveHEIFToBuffer(r.image, *params)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return buf, r.newMetadata(ImageTypeHEIF), nil
|
||||
}
|
||||
|
||||
// ExportTiff exports the image as TIFF to a buffer.
|
||||
func (r *ImageRef) ExportTiff(params *TiffExportParams) ([]byte, *ImageMetadata, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
if params == nil {
|
||||
params = NewTiffExportParams()
|
||||
}
|
||||
|
||||
buf, err := vipsSaveTIFFToBuffer(r.image, *params)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return buf, r.newMetadata(ImageTypeTIFF), nil
|
||||
}
|
||||
|
||||
// ExportGIF exports the image as GIF to a buffer.
|
||||
func (r *ImageRef) ExportGIF(params *GifExportParams) ([]byte, *ImageMetadata, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
if params == nil {
|
||||
params = NewGifExportParams()
|
||||
}
|
||||
|
||||
buf, err := vipsSaveGIFToBuffer(r.image, *params)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return buf, r.newMetadata(ImageTypeGIF), nil
|
||||
}
|
||||
|
||||
// ExportAvif exports the image as AVIF to a buffer.
|
||||
func (r *ImageRef) ExportAvif(params *AvifExportParams) ([]byte, *ImageMetadata, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
if params == nil {
|
||||
params = NewAvifExportParams()
|
||||
}
|
||||
|
||||
buf, err := vipsSaveAVIFToBuffer(r.image, *params)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return buf, r.newMetadata(ImageTypeAVIF), nil
|
||||
}
|
||||
|
||||
// ExportJp2k exports the image as JPEG2000 to a buffer.
|
||||
func (r *ImageRef) ExportJp2k(params *Jp2kExportParams) ([]byte, *ImageMetadata, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
if params == nil {
|
||||
params = NewJp2kExportParams()
|
||||
}
|
||||
|
||||
buf, err := vipsSaveJP2KToBuffer(r.image, *params)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return buf, r.newMetadata(ImageTypeJP2K), nil
|
||||
}
|
||||
|
||||
// ExportJxl exports the image as JPEG XL to a buffer.
|
||||
func (r *ImageRef) ExportJxl(params *JxlExportParams) ([]byte, *ImageMetadata, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
if params == nil {
|
||||
params = NewJxlExportParams()
|
||||
}
|
||||
|
||||
buf, err := vipsSaveJxlToBuffer(r.image, *params)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return buf, r.newMetadata(ImageTypeJXL), nil
|
||||
}
|
||||
|
||||
// ExportMagick exports the image as Format set in param to a buffer.
|
||||
func (r *ImageRef) ExportMagick(params *MagickExportParams) ([]byte, *ImageMetadata, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
if params == nil {
|
||||
params = NewMagickExportParams()
|
||||
params.Format = "JPG"
|
||||
}
|
||||
|
||||
buf, err := vipsSaveMagickToBuffer(r.image, *params)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return buf, r.newMetadata(ImageTypeMagick), nil
|
||||
}
|
||||
|
||||
// ToBytes writes the image to memory in VIPs format and returns the raw bytes, useful for storage.
|
||||
func (r *ImageRef) ToBytes() ([]byte, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
var cSize C.size_t
|
||||
cData := C.vips_image_write_to_memory(r.image, &cSize)
|
||||
if cData == nil {
|
||||
return nil, errors.New("failed to write image to memory")
|
||||
}
|
||||
defer C.free(cData)
|
||||
|
||||
data := C.GoBytes(unsafe.Pointer(cData), C.int(cSize))
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// ToImage converts a VIPs image to a golang image.Image object, useful for interoperability with other golang libraries.
|
||||
// Deprecated: Use ToGoImage for a faster, direct conversion without encoding round-trip.
|
||||
func (r *ImageRef) ToImage(params *ExportParams) (image.Image, error) {
|
||||
imageBytes, _, err := r.ExportNative()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
reader := bytes.NewReader(imageBytes)
|
||||
img, _, err := image.Decode(reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return img, nil
|
||||
}
|
||||
|
||||
// ToGoImage converts a vips image directly to a Go image.Image without encoding.
|
||||
// This is significantly faster than ToImage() which round-trips through JPEG/PNG.
|
||||
// The resulting image will be in sRGB color space with 8-bit depth.
|
||||
func (r *ImageRef) ToGoImage() (image.Image, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
|
||||
// Work on a copy to avoid mutating the receiver
|
||||
tmp, err := vipsGenCopy(r.image, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer clearImage(tmp)
|
||||
|
||||
// Convert to sRGB if needed (keep B_W for grayscale)
|
||||
interp := Interpretation(int(tmp.Type))
|
||||
if interp != InterpretationSRGB && interp != InterpretationBW {
|
||||
out, err := vipsToColorSpace(tmp, InterpretationSRGB)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
clearImage(tmp)
|
||||
tmp = out
|
||||
}
|
||||
|
||||
// Cast to uchar if needed
|
||||
if BandFormat(int(tmp.BandFmt)) != BandFormatUchar {
|
||||
out, err := vipsGenCast(tmp, BandFormatUchar, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
clearImage(tmp)
|
||||
tmp = out
|
||||
}
|
||||
|
||||
// Extract raw pixel data
|
||||
var cSize C.size_t
|
||||
cData := C.vips_image_write_to_memory(tmp, &cSize)
|
||||
if cData == nil {
|
||||
return nil, errors.New("failed to write image to memory")
|
||||
}
|
||||
defer C.free(cData)
|
||||
|
||||
width := int(tmp.Xsize)
|
||||
height := int(tmp.Ysize)
|
||||
bands := int(tmp.Bands)
|
||||
pixels := C.GoBytes(unsafe.Pointer(cData), C.int(cSize))
|
||||
|
||||
switch bands {
|
||||
case 1:
|
||||
img := image.NewGray(image.Rect(0, 0, width, height))
|
||||
copy(img.Pix, pixels)
|
||||
return img, nil
|
||||
case 2:
|
||||
// Grayscale + alpha
|
||||
img := image.NewNRGBA(image.Rect(0, 0, width, height))
|
||||
srcIdx := 0
|
||||
dstIdx := 0
|
||||
for srcIdx+1 < len(pixels) {
|
||||
v := pixels[srcIdx]
|
||||
img.Pix[dstIdx] = v
|
||||
img.Pix[dstIdx+1] = v
|
||||
img.Pix[dstIdx+2] = v
|
||||
img.Pix[dstIdx+3] = pixels[srcIdx+1]
|
||||
srcIdx += 2
|
||||
dstIdx += 4
|
||||
}
|
||||
return img, nil
|
||||
case 3:
|
||||
// RGB, add opaque alpha
|
||||
img := image.NewNRGBA(image.Rect(0, 0, width, height))
|
||||
srcIdx := 0
|
||||
dstIdx := 0
|
||||
for srcIdx+2 < len(pixels) {
|
||||
img.Pix[dstIdx] = pixels[srcIdx]
|
||||
img.Pix[dstIdx+1] = pixels[srcIdx+1]
|
||||
img.Pix[dstIdx+2] = pixels[srcIdx+2]
|
||||
img.Pix[dstIdx+3] = 255
|
||||
srcIdx += 3
|
||||
dstIdx += 4
|
||||
}
|
||||
return img, nil
|
||||
case 4:
|
||||
img := image.NewNRGBA(image.Rect(0, 0, width, height))
|
||||
copy(img.Pix, pixels)
|
||||
return img, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported number of bands: %d", bands)
|
||||
}
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
package vips
|
||||
|
||||
// #include "image.h"
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// GetICCProfile retrieves the ICC profile data (if any) from the image.
|
||||
func (r *ImageRef) GetICCProfile() []byte {
|
||||
defer runtime.KeepAlive(r)
|
||||
bytes, _ := vipsGetICCProfile(r.image)
|
||||
return bytes
|
||||
}
|
||||
|
||||
// RemoveICCProfile removes the ICC Profile information from the image.
|
||||
// Typically, browsers and other software assume images without profile to be in the sRGB color space.
|
||||
func (r *ImageRef) RemoveICCProfile() error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenCopy(r.image, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
vipsRemoveICCProfile(out)
|
||||
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// TransformICCProfileWithFallback transforms from the embedded ICC profile of the image to the ICC profile at the given path.
|
||||
// The fallback ICC profile is used if the image does not have an embedded ICC profile.
|
||||
func (r *ImageRef) TransformICCProfileWithFallback(targetProfilePath, fallbackProfilePath string) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
if err := ensureLoadICCPath(&targetProfilePath); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ensureLoadICCPath(&fallbackProfilePath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
depth := 16
|
||||
if r.BandFormat() == BandFormatUchar || r.BandFormat() == BandFormatChar || r.BandFormat() == BandFormatNotSet {
|
||||
depth = 8
|
||||
}
|
||||
|
||||
out, err := vipsICCTransform(r.image, targetProfilePath, fallbackProfilePath, IntentPerceptual, depth, true)
|
||||
if err != nil {
|
||||
govipsLog("govips", LogLevelError, fmt.Sprintf("failed to do icc transform: %v", err.Error()))
|
||||
return err
|
||||
}
|
||||
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// TransformICCProfile transforms from the embedded ICC profile of the image to the icc profile at the given path.
|
||||
func (r *ImageRef) TransformICCProfile(outputProfilePath string) error {
|
||||
return r.TransformICCProfileWithFallback(outputProfilePath, SRGBIEC6196621ICCProfilePath)
|
||||
}
|
||||
|
||||
// OptimizeICCProfile optimizes the ICC color profile of the image.
|
||||
// For two color channel images, it sets a grayscale profile.
|
||||
// For color images, it sets a CMYK or non-CMYK profile based on the image metadata.
|
||||
func (r *ImageRef) OptimizeICCProfile() error {
|
||||
defer runtime.KeepAlive(r)
|
||||
inputProfile := r.determineInputICCProfile()
|
||||
if !r.HasICCProfile() && (inputProfile == "") {
|
||||
// No embedded ICC profile in the input image and no input profile determined, nothing to do.
|
||||
return nil
|
||||
}
|
||||
|
||||
r.optimizedIccProfile = SRGBV2MicroICCProfilePath
|
||||
if r.Bands() <= 2 {
|
||||
r.optimizedIccProfile = SGrayV2MicroICCProfilePath
|
||||
}
|
||||
|
||||
if err := ensureLoadICCPath(&r.optimizedIccProfile); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
embedded := r.HasICCProfile() && (inputProfile == "")
|
||||
|
||||
depth := 16
|
||||
if r.BandFormat() == BandFormatUchar || r.BandFormat() == BandFormatChar || r.BandFormat() == BandFormatNotSet {
|
||||
depth = 8
|
||||
}
|
||||
|
||||
out, err := vipsICCTransform(r.image, r.optimizedIccProfile, inputProfile, IntentPerceptual, depth, embedded)
|
||||
if err != nil {
|
||||
govipsLog("govips", LogLevelError, fmt.Sprintf("failed to do icc transform: %v", err.Error()))
|
||||
return err
|
||||
}
|
||||
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ImageRef) determineInputICCProfile() (inputProfile string) {
|
||||
if r.Interpretation() == InterpretationCMYK {
|
||||
if !r.HasICCProfile() {
|
||||
inputProfile = "cmyk"
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -1,338 +0,0 @@
|
||||
package vips
|
||||
|
||||
// #include "image.h"
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Format returns the current format of the vips image.
|
||||
func (r *ImageRef) Format() ImageType {
|
||||
return r.format
|
||||
}
|
||||
|
||||
// OriginalFormat returns the original format of the image when loaded.
|
||||
// In some cases the loaded image is converted on load, for example, a BMP is automatically converted to PNG
|
||||
// This method returns the format of the original buffer, as opposed to Format() with will return the format of the
|
||||
// currently held buffer content.
|
||||
func (r *ImageRef) OriginalFormat() ImageType {
|
||||
return r.originalFormat
|
||||
}
|
||||
|
||||
// Width returns the width of this image.
|
||||
func (r *ImageRef) Width() int {
|
||||
return int(r.image.Xsize)
|
||||
}
|
||||
|
||||
// Height returns the height of this image.
|
||||
func (r *ImageRef) Height() int {
|
||||
return int(r.image.Ysize)
|
||||
}
|
||||
|
||||
// Bands returns the number of bands for this image.
|
||||
func (r *ImageRef) Bands() int {
|
||||
return int(r.image.Bands)
|
||||
}
|
||||
|
||||
// HasProfile returns if the image has an ICC profile embedded.
|
||||
func (r *ImageRef) HasProfile() bool {
|
||||
defer runtime.KeepAlive(r)
|
||||
return vipsHasICCProfile(r.image)
|
||||
}
|
||||
|
||||
// HasICCProfile checks whether the image has an ICC profile embedded. Alias to HasProfile
|
||||
func (r *ImageRef) HasICCProfile() bool {
|
||||
return r.HasProfile()
|
||||
}
|
||||
|
||||
// HasIPTC returns a boolean whether the image in question has IPTC data associated with it.
|
||||
func (r *ImageRef) HasIPTC() bool {
|
||||
defer runtime.KeepAlive(r)
|
||||
return vipsHasIPTC(r.image)
|
||||
}
|
||||
|
||||
// HasAlpha returns if the image has an alpha layer.
|
||||
func (r *ImageRef) HasAlpha() bool {
|
||||
defer runtime.KeepAlive(r)
|
||||
return vipsHasAlpha(r.image)
|
||||
}
|
||||
|
||||
// Orientation returns the orientation number as it appears in the EXIF, if present
|
||||
func (r *ImageRef) Orientation() int {
|
||||
defer runtime.KeepAlive(r)
|
||||
return vipsGetMetaOrientation(r.image)
|
||||
}
|
||||
|
||||
// Deprecated: use Orientation() instead
|
||||
func (r *ImageRef) GetOrientation() int {
|
||||
return r.Orientation()
|
||||
}
|
||||
|
||||
// SetOrientation sets the orientation in the EXIF header of the associated image.
|
||||
func (r *ImageRef) SetOrientation(orientation int) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenCopy(r.image, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
vipsSetMetaOrientation(out, orientation)
|
||||
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveOrientation removes the EXIF orientation information of the image.
|
||||
func (r *ImageRef) RemoveOrientation() error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenCopy(r.image, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
vipsRemoveMetaOrientation(out)
|
||||
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ResX returns the X resolution
|
||||
func (r *ImageRef) ResX() float64 {
|
||||
return float64(r.image.Xres)
|
||||
}
|
||||
|
||||
// ResY returns the Y resolution
|
||||
func (r *ImageRef) ResY() float64 {
|
||||
return float64(r.image.Yres)
|
||||
}
|
||||
|
||||
// OffsetX returns the X offset
|
||||
func (r *ImageRef) OffsetX() int {
|
||||
return int(r.image.Xoffset)
|
||||
}
|
||||
|
||||
// OffsetY returns the Y offset
|
||||
func (r *ImageRef) OffsetY() int {
|
||||
return int(r.image.Yoffset)
|
||||
}
|
||||
|
||||
// BandFormat returns the current band format
|
||||
func (r *ImageRef) BandFormat() BandFormat {
|
||||
return BandFormat(int(r.image.BandFmt))
|
||||
}
|
||||
|
||||
// Coding returns the image coding
|
||||
func (r *ImageRef) Coding() Coding {
|
||||
return Coding(int(r.image.Coding))
|
||||
}
|
||||
|
||||
// Interpretation returns the current interpretation of the color space of the image.
|
||||
func (r *ImageRef) Interpretation() Interpretation {
|
||||
return Interpretation(int(r.image.Type))
|
||||
}
|
||||
|
||||
// ColorSpace returns the interpretation of the current color space. Alias to Interpretation().
|
||||
func (r *ImageRef) ColorSpace() Interpretation {
|
||||
return r.Interpretation()
|
||||
}
|
||||
|
||||
// IsColorSpaceSupported returns a boolean whether the image's color space is supported by libvips.
|
||||
func (r *ImageRef) IsColorSpaceSupported() bool {
|
||||
defer runtime.KeepAlive(r)
|
||||
return vipsIsColorSpaceSupported(r.image)
|
||||
}
|
||||
|
||||
// Pages returns the number of pages in the Image
|
||||
// For animated images this corresponds to the number of frames
|
||||
func (r *ImageRef) Pages() int {
|
||||
// libvips uses the same attribute (n_pages) to represent the number of pyramid layers in JP2K
|
||||
// as we interpret the attribute as frames and JP2K does not support animation we override this with 1
|
||||
if r.format == ImageTypeJP2K {
|
||||
return 1
|
||||
}
|
||||
|
||||
defer runtime.KeepAlive(r)
|
||||
return vipsGetImageNPages(r.image)
|
||||
}
|
||||
|
||||
// Deprecated: use Pages() instead
|
||||
func (r *ImageRef) GetPages() int {
|
||||
return r.Pages()
|
||||
}
|
||||
|
||||
// SetPages sets the number of pages in the Image
|
||||
// For animated images this corresponds to the number of frames
|
||||
func (r *ImageRef) SetPages(pages int) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenCopy(r.image, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
vipsSetImageNPages(out, pages)
|
||||
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// PageHeight return the height of a single page
|
||||
func (r *ImageRef) PageHeight() int {
|
||||
defer runtime.KeepAlive(r)
|
||||
return vipsGetPageHeight(r.image)
|
||||
}
|
||||
|
||||
// GetPageHeight return the height of a single page
|
||||
// Deprecated use PageHeight() instead
|
||||
func (r *ImageRef) GetPageHeight() int {
|
||||
defer runtime.KeepAlive(r)
|
||||
return vipsGetPageHeight(r.image)
|
||||
}
|
||||
|
||||
// SetPageHeight set the height of a page
|
||||
// For animated images this is used when "unrolling" back to frames
|
||||
func (r *ImageRef) SetPageHeight(height int) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenCopy(r.image, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
vipsSetPageHeight(out, height)
|
||||
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// PageDelay get the page delay array for animation
|
||||
func (r *ImageRef) PageDelay() ([]int, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
n := vipsGetImageNPages(r.image)
|
||||
if n <= 1 {
|
||||
// should not call if not multi page
|
||||
return nil, nil
|
||||
}
|
||||
return vipsImageGetDelay(r.image, n)
|
||||
}
|
||||
|
||||
// SetPageDelay set the page delay array for animation
|
||||
func (r *ImageRef) SetPageDelay(delay []int) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
var data []C.int
|
||||
for _, d := range delay {
|
||||
data = append(data, C.int(d))
|
||||
}
|
||||
return vipsImageSetDelay(r.image, data)
|
||||
}
|
||||
|
||||
// Loop returns the loop count for animated images.
|
||||
// A value of 0 means infinite looping.
|
||||
func (r *ImageRef) Loop() int {
|
||||
defer runtime.KeepAlive(r)
|
||||
return vipsImageGetLoop(r.image)
|
||||
}
|
||||
|
||||
// SetLoop sets the loop count for animated images.
|
||||
// A value of 0 means infinite looping.
|
||||
func (r *ImageRef) SetLoop(loop int) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
vipsImageSetLoop(r.image, loop)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Background get the background of image.
|
||||
func (r *ImageRef) Background() ([]float64, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsImageGetBackground(r.image)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (r *ImageRef) ImageFields() []string {
|
||||
return r.GetFields()
|
||||
}
|
||||
|
||||
func (r *ImageRef) GetFields() []string {
|
||||
defer runtime.KeepAlive(r)
|
||||
return vipsImageGetFields(r.image)
|
||||
}
|
||||
|
||||
func (r *ImageRef) SetBlob(name string, data []byte) {
|
||||
defer runtime.KeepAlive(r)
|
||||
vipsImageSetBlob(r.image, name, data)
|
||||
}
|
||||
|
||||
func (r *ImageRef) GetBlob(name string) []byte {
|
||||
defer runtime.KeepAlive(r)
|
||||
return vipsImageGetBlob(r.image, name)
|
||||
}
|
||||
|
||||
func (r *ImageRef) SetDouble(name string, f float64) {
|
||||
defer runtime.KeepAlive(r)
|
||||
vipsImageSetDouble(r.image, name, f)
|
||||
}
|
||||
|
||||
func (r *ImageRef) GetDouble(name string) float64 {
|
||||
defer runtime.KeepAlive(r)
|
||||
return vipsImageGetDouble(r.image, name)
|
||||
}
|
||||
|
||||
func (r *ImageRef) SetInt(name string, i int) {
|
||||
defer runtime.KeepAlive(r)
|
||||
vipsImageSetInt(r.image, name, i)
|
||||
}
|
||||
|
||||
func (r *ImageRef) GetInt(name string) int {
|
||||
defer runtime.KeepAlive(r)
|
||||
return vipsImageGetInt(r.image, name)
|
||||
}
|
||||
|
||||
func (r *ImageRef) SetString(name string, str string) {
|
||||
defer runtime.KeepAlive(r)
|
||||
vipsImageSetString(r.image, name, str)
|
||||
}
|
||||
|
||||
func (r *ImageRef) GetString(name string) string {
|
||||
defer runtime.KeepAlive(r)
|
||||
return vipsImageGetString(r.image, name)
|
||||
}
|
||||
|
||||
func (r *ImageRef) GetAsString(name string) string {
|
||||
defer runtime.KeepAlive(r)
|
||||
return vipsImageGetAsString(r.image, name)
|
||||
}
|
||||
|
||||
func (r *ImageRef) HasExif() bool {
|
||||
for _, field := range r.ImageFields() {
|
||||
if strings.HasPrefix(field, "exif-") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (r *ImageRef) GetExif() map[string]string {
|
||||
defer runtime.KeepAlive(r)
|
||||
return vipsImageGetExifData(r.image)
|
||||
}
|
||||
|
||||
// RemoveMetadata removes the EXIF metadata from the image.
|
||||
// N.B. this function won't remove the ICC profile, orientation and pages metadata
|
||||
// because govips needs it to correctly display the image.
|
||||
func (r *ImageRef) RemoveMetadata(keep ...string) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenCopy(r.image, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
vipsRemoveMetadata(out, keep...)
|
||||
|
||||
r.setImage(out)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,393 +0,0 @@
|
||||
package vips
|
||||
|
||||
// #include "image.h"
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// GaussianBlur blurs the image
|
||||
// add support minAmpl
|
||||
func (r *ImageRef) GaussianBlur(sigmas ...float64) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
var (
|
||||
sigma = sigmas[0]
|
||||
minAmpl = GaussBlurDefaultMinAMpl
|
||||
)
|
||||
if len(sigmas) >= 2 {
|
||||
minAmpl = sigmas[1]
|
||||
}
|
||||
out, err := vipsGenGaussblur(r.image, sigma, &GaussblurOptions{MinAmpl: &minAmpl})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Sharpen sharpens the image
|
||||
// sigma: sigma of the gaussian
|
||||
// x1: flat/jaggy threshold
|
||||
// m2: slope for jaggy areas
|
||||
func (r *ImageRef) Sharpen(sigma float64, x1 float64, m2 float64) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenSharpen(r.image, &SharpenOptions{Sigma: &sigma, X1: &x1, M2: &m2})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Apply Sobel edge detector to the image.
|
||||
func (r *ImageRef) Sobel() error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenSobel(r.image)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Rank does rank filtering on an image. A window of size width by height is passed over the image.
|
||||
// At each position, the pixels inside the window are sorted into ascending order and the pixel at position
|
||||
// index is output. index numbers from 0.
|
||||
func (r *ImageRef) Rank(width int, height int, index int) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenRank(r.image, width, height, index)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Mapim resamples an image using index to look up pixels
|
||||
func (r *ImageRef) Mapim(index *ImageRef) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
defer runtime.KeepAlive(index)
|
||||
out, err := vipsGenMapim(r.image, index.image, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Maplut maps an image through another image acting as a LUT (Look Up Table)
|
||||
func (r *ImageRef) Maplut(lut *ImageRef) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
defer runtime.KeepAlive(lut)
|
||||
out, err := vipsGenMaplut(r.image, lut.image, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ExtractBand extracts one or more bands out of the image (replacing the associated ImageRef)
|
||||
func (r *ImageRef) ExtractBand(band int, num int) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenExtractBand(r.image, band, &ExtractBandOptions{N: &num})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ExtractBandToImage extracts one or more bands out of the image to a new image
|
||||
func (r *ImageRef) ExtractBandToImage(band int, num int) (*ImageRef, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenExtractBand(r.image, band, &ExtractBandOptions{N: &num})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newImageRef(out, ImageTypeUnknown, ImageTypeUnknown, nil), nil
|
||||
}
|
||||
|
||||
// BandJoin joins a set of images together, bandwise.
|
||||
func (r *ImageRef) BandJoin(images ...*ImageRef) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
defer runtime.KeepAlive(images)
|
||||
vipsImages := []*C.VipsImage{r.image}
|
||||
for _, vipsImage := range images {
|
||||
vipsImages = append(vipsImages, vipsImage.image)
|
||||
}
|
||||
|
||||
out, err := vipsGenBandjoin(vipsImages)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// BandSplit split an n-band image into n separate images..
|
||||
func (r *ImageRef) BandSplit() ([]*ImageRef, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
var out []*ImageRef
|
||||
n := 1
|
||||
for i := 0; i < r.Bands(); i++ {
|
||||
img, err := vipsGenExtractBand(r.image, i, &ExtractBandOptions{N: &n})
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
out = append(out, &ImageRef{image: img})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// BandJoinConst appends a set of constant bands to an image.
|
||||
func (r *ImageRef) BandJoinConst(constants []float64) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
if len(constants) == 0 {
|
||||
return errors.New("BandJoinConst: empty constants slice")
|
||||
}
|
||||
out, err := vipsGenBandjoinConst(r.image, constants)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddAlpha adds an alpha channel to the associated image.
|
||||
func (r *ImageRef) AddAlpha() error {
|
||||
defer runtime.KeepAlive(r)
|
||||
if vipsHasAlpha(r.image) {
|
||||
return nil
|
||||
}
|
||||
|
||||
out, err := vipsAddAlpha(r.image)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// PremultiplyAlpha premultiplies the alpha channel.
|
||||
// See https://libvips.github.io/libvips/API/current/libvips-conversion.html#vips-premultiply
|
||||
func (r *ImageRef) PremultiplyAlpha() error {
|
||||
defer runtime.KeepAlive(r)
|
||||
if r.preMultiplication != nil || !vipsHasAlpha(r.image) {
|
||||
return nil
|
||||
}
|
||||
|
||||
band := r.BandFormat()
|
||||
|
||||
out, err := vipsGenPremultiply(r.image, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.preMultiplication = &PreMultiplicationState{
|
||||
bandFormat: band,
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnpremultiplyAlpha unpremultiplies any alpha channel.
|
||||
// See https://libvips.github.io/libvips/API/current/libvips-conversion.html#vips-unpremultiply
|
||||
func (r *ImageRef) UnpremultiplyAlpha() error {
|
||||
defer runtime.KeepAlive(r)
|
||||
if r.preMultiplication == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
unpremultiplied, err := vipsGenUnpremultiply(r.image, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer clearImage(unpremultiplied)
|
||||
|
||||
out, err := vipsGenCast(unpremultiplied, r.preMultiplication.bandFormat, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r.preMultiplication = nil
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Add calculates a sum of the image + addend and stores it back in the image
|
||||
func (r *ImageRef) Add(addend *ImageRef) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
defer runtime.KeepAlive(addend)
|
||||
out, err := vipsGenAdd(r.image, addend.image)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Multiply calculates the product of the image * multiplier and stores it back in the image
|
||||
func (r *ImageRef) Multiply(multiplier *ImageRef) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
defer runtime.KeepAlive(multiplier)
|
||||
out, err := vipsGenMultiply(r.image, multiplier.image)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Divide calculates the product of the image / denominator and stores it back in the image
|
||||
func (r *ImageRef) Divide(denominator *ImageRef) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
defer runtime.KeepAlive(denominator)
|
||||
out, err := vipsGenDivide(r.image, denominator.image)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Average finds the average value in an image
|
||||
func (r *ImageRef) Average() (float64, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenAvg(r.image)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// FindTrim returns the bounding box of the non-border part of the image
|
||||
// Returned values are left, top, width, height
|
||||
func (r *ImageRef) FindTrim(threshold float64, backgroundColor *Color) (int, int, int, int, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
return vipsFindTrim(r.image, threshold, backgroundColor)
|
||||
}
|
||||
|
||||
// GetPoint reads a single pixel on an image.
|
||||
// The pixel values are returned in a slice with one element per band.
|
||||
func (r *ImageRef) GetPoint(x int, y int) ([]float64, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
return vipsGetPoint(r.image, r.Bands(), x, y)
|
||||
}
|
||||
|
||||
// Stats find many image statistics in a single pass through the data. Image is changed into a one-band
|
||||
// `BandFormatDouble` image of at least 10 columns by n + 1 (where n is number of bands in image in)
|
||||
// rows. Columns are statistics, and are, in order: minimum, maximum, sum, sum of squares, mean,
|
||||
// standard deviation, x coordinate of minimum, y coordinate of minimum, x coordinate of maximum,
|
||||
// y coordinate of maximum.
|
||||
//
|
||||
// Row 0 has statistics for all bands together, row 1 has stats for band 1, and so on.
|
||||
//
|
||||
// If there is more than one maxima or minima, one of them will be chosen at random.
|
||||
func (r *ImageRef) Stats() error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenStats(r.image)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// HistogramFind find the histogram the image.
|
||||
// Find the histogram for all bands (producing a one-band histogram).
|
||||
// char and uchar images are cast to uchar before histogramming, all other image types are cast to ushort.
|
||||
func (r *ImageRef) HistogramFind() error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenHistFind(r.image, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// HistogramCumulative form cumulative histogram.
|
||||
func (r *ImageRef) HistogramCumulative() error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenHistCum(r.image)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// HistogramNormalise
|
||||
// The maximum of each band becomes equal to the maximum index, so for example the max for a uchar
|
||||
// image becomes 255. Normalise each band separately.
|
||||
func (r *ImageRef) HistogramNormalise() error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenHistNorm(r.image)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// HistogramEntropy estimate image entropy from a histogram. Entropy is calculated as:
|
||||
// `-sum(p * log2(p))`
|
||||
// where p is histogram-value / sum-of-histogram-values.
|
||||
func (r *ImageRef) HistogramEntropy() (float64, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
return vipsGenHistEntropy(r.image)
|
||||
}
|
||||
|
||||
// DrawRect draws an (optionally filled) rectangle with a single colour
|
||||
func (r *ImageRef) DrawRect(ink ColorRGBA, left int, top int, width int, height int, fill bool) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
err := vipsDrawRect(r.image, ink, left, top, width, height, fill)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Subtract calculate subtract operation between two images.
|
||||
func (r *ImageRef) Subtract(in2 *ImageRef) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
defer runtime.KeepAlive(in2)
|
||||
out, err := vipsGenSubtract(r.image, in2.image)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Abs calculate abs operation.
|
||||
func (r *ImageRef) Abs() error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenAbs(r.image)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Project calculate project operation.
|
||||
func (r *ImageRef) Project() (*ImageRef, *ImageRef, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
col, row, err := vipsGenProject(r.image)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return newImageRef(col, r.format, r.originalFormat, nil), newImageRef(row, r.format, r.originalFormat, nil), nil
|
||||
}
|
||||
|
||||
// Min finds the minimum value in an image.
|
||||
func (r *ImageRef) Min() (float64, int, int, error) {
|
||||
defer runtime.KeepAlive(r)
|
||||
return vipsMin(r.image)
|
||||
}
|
||||
@@ -1,458 +0,0 @@
|
||||
package vips
|
||||
|
||||
// #include "image.h"
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// GetRotationAngleFromExif returns the angle which the image is currently rotated in.
|
||||
// First returned value is the angle and second is a boolean indicating whether image is flipped.
|
||||
// This is based on the EXIF orientation tag standard.
|
||||
// If no proper orientation number is provided, the picture will be assumed to be upright.
|
||||
func GetRotationAngleFromExif(orientation int) (Angle, bool) {
|
||||
switch orientation {
|
||||
case 0, 1, 2:
|
||||
return Angle0, orientation == 2
|
||||
case 3, 4:
|
||||
return Angle180, orientation == 4
|
||||
case 5, 8:
|
||||
return Angle90, orientation == 5
|
||||
case 6, 7:
|
||||
return Angle270, orientation == 7
|
||||
}
|
||||
|
||||
return Angle0, false
|
||||
}
|
||||
|
||||
// AutoRotate rotates the image upright based on the EXIF Orientation tag,
|
||||
// including the mirrored orientations (2, 4, 5 and 7), which libvips
|
||||
// handles with a flip since 8.10.
|
||||
// It also resets the orientation information in the EXIF tag to be 1 (i.e. upright).
|
||||
// N.B. due to the HEIF image standard, HEIF images are always autorotated by default on load.
|
||||
// Thus, calling AutoRotate for HEIF images is not needed.
|
||||
// todo: use https://www.libvips.org/API/current/libvips-conversion.html#vips-autorot-remove-angle
|
||||
func (r *ImageRef) AutoRotate() error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, _, _, err := vipsGenAutorot(r.image)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ExtractArea crops the image to a specified area
|
||||
func (r *ImageRef) ExtractArea(left, top, width, height int) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
if r.Height() > r.PageHeight() {
|
||||
// use animated extract area if more than 1 pages loaded
|
||||
out, err := vipsExtractAreaMultiPage(r.image, left, top, width, height)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
} else {
|
||||
out, err := vipsExtractArea(r.image, left, top, width, height)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Resize resizes the image based on the scale, maintaining aspect ratio
|
||||
func (r *ImageRef) Resize(scale float64, kernel Kernel) error {
|
||||
return r.ResizeWithVScale(scale, -1, kernel)
|
||||
}
|
||||
|
||||
// ResizeWithVScale resizes the image with both horizontal and vertical scaling.
|
||||
// The parameters are the scaling factors.
|
||||
func (r *ImageRef) ResizeWithVScale(hScale, vScale float64, kernel Kernel) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
if err := r.PremultiplyAlpha(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pages := r.loadedPages()
|
||||
newPageHeight := 0
|
||||
if pages > 1 {
|
||||
scale := hScale
|
||||
if vScale > 0 {
|
||||
scale = vScale
|
||||
}
|
||||
newPageHeight = int(math.Round(float64(r.PageHeight()) * scale))
|
||||
if newPageHeight < 1 {
|
||||
newPageHeight = 1
|
||||
}
|
||||
// libvips resizes the stacked frames as one strip, so rounding the
|
||||
// strip and the page height separately can leave the strip a few
|
||||
// rows short of pages*newPageHeight (#350, #554). When that happens,
|
||||
// pick the vertical scale that makes the strip height exact, as
|
||||
// vips_thumbnail does. Output is unchanged when the rounding agrees.
|
||||
height := r.Height()
|
||||
if int(math.Round(float64(height)*scale)) != pages*newPageHeight {
|
||||
vScale = float64(pages*newPageHeight) / float64(height)
|
||||
}
|
||||
}
|
||||
|
||||
out, err := vipsResizeWithVScale(r.image, hScale, vScale, kernel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
|
||||
if pages > 1 {
|
||||
if err := r.SetPageHeight(newPageHeight); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return r.UnpremultiplyAlpha()
|
||||
}
|
||||
|
||||
// loadedPages is the number of pages stacked in the image. Pages() reports
|
||||
// the page count of the file, which is larger when only some were loaded.
|
||||
func (r *ImageRef) loadedPages() int {
|
||||
pageHeight := r.PageHeight()
|
||||
if pageHeight <= 0 {
|
||||
return 1
|
||||
}
|
||||
return r.Height() / pageHeight
|
||||
}
|
||||
|
||||
// Thumbnail resizes the image to the given width and height.
|
||||
// crop decides algorithm vips uses to shrink and crop to fill target,
|
||||
func (r *ImageRef) Thumbnail(width, height int, crop Interesting) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsThumbnail(r.image, width, height, crop, SizeBoth)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ThumbnailWithSize resizes the image to the given width and height.
|
||||
// crop decides algorithm vips uses to shrink and crop to fill target,
|
||||
// size controls upsize, downsize, both or force
|
||||
func (r *ImageRef) ThumbnailWithSize(width, height int, crop Interesting, size Size) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsThumbnail(r.image, width, height, crop, size)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Embed embeds the given picture in a new one, i.e. the opposite of ExtractArea
|
||||
func (r *ImageRef) Embed(left, top, width, height int, extend ExtendStrategy) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
if r.Height() > r.PageHeight() {
|
||||
out, err := vipsEmbedMultiPage(r.image, left, top, width, height, extend)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
} else {
|
||||
out, err := vipsEmbed(r.image, left, top, width, height, extend)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EmbedBackground embeds the given picture with a background color.
|
||||
// For greyscale images (1-2 bands) the R value is used as the grey level.
|
||||
func (r *ImageRef) EmbedBackground(left, top, width, height int, backgroundColor *Color) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
c := &ColorRGBA{
|
||||
R: backgroundColor.R,
|
||||
G: backgroundColor.G,
|
||||
B: backgroundColor.B,
|
||||
A: 255,
|
||||
}
|
||||
if r.Height() > r.PageHeight() {
|
||||
out, err := vipsEmbedMultiPageBackground(r.image, left, top, width, height, c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
} else {
|
||||
out, err := vipsEmbedBackground(r.image, left, top, width, height, c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EmbedBackgroundRGBA embeds the given picture with a background rgba color.
|
||||
// For greyscale images the background is built from R and A only:
|
||||
// 1 band uses {R}, 2 bands (grey+alpha) use {R, A}.
|
||||
func (r *ImageRef) EmbedBackgroundRGBA(left, top, width, height int, backgroundColor *ColorRGBA) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
if r.Height() > r.PageHeight() {
|
||||
out, err := vipsEmbedMultiPageBackground(r.image, left, top, width, height, backgroundColor)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
} else {
|
||||
out, err := vipsEmbedBackground(r.image, left, top, width, height, backgroundColor)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Zoom zooms the image by repeating pixels (fast nearest-neighbour)
|
||||
func (r *ImageRef) Zoom(xFactor int, yFactor int) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
pages := r.loadedPages()
|
||||
pageHeight := r.PageHeight()
|
||||
out, err := vipsGenZoom(r.image, xFactor, yFactor)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
if pages > 1 {
|
||||
// Zooming the strip zooms every frame; only the page height is stale.
|
||||
return r.SetPageHeight(pageHeight * yFactor)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ImageRef) Gravity(gravity Gravity, width int, height int) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenGravity(r.image, gravity, width, height, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Flip flips the image either horizontally or vertically based on the parameter
|
||||
func (r *ImageRef) Flip(direction Direction) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
var out *C.VipsImage
|
||||
var err error
|
||||
if direction == DirectionVertical && r.loadedPages() > 1 {
|
||||
// Flipping the strip would reverse the frame order.
|
||||
out, err = vipsMapPages(r.image, func(page *C.VipsImage) (*C.VipsImage, error) {
|
||||
return vipsFlip(page, direction)
|
||||
})
|
||||
} else {
|
||||
out, err = vipsFlip(r.image, direction)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Recomb recombines the image bands using the matrix provided
|
||||
func (r *ImageRef) Recomb(matrix [][]float64) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
numBands := r.Bands()
|
||||
// Ensure the provided matrix is 3x3
|
||||
if len(matrix) != 3 || len(matrix[0]) != 3 || len(matrix[1]) != 3 || len(matrix[2]) != 3 {
|
||||
return errors.New("Invalid recombination matrix")
|
||||
}
|
||||
// If the image is RGBA, expand the matrix to 4x4
|
||||
if numBands == 4 {
|
||||
matrix = append(matrix, []float64{0, 0, 0, 1})
|
||||
for i := 0; i < 3; i++ {
|
||||
matrix[i] = append(matrix[i], 0)
|
||||
}
|
||||
} else if numBands != 3 {
|
||||
return errors.New("Unsupported number of bands")
|
||||
}
|
||||
|
||||
// Flatten the matrix
|
||||
matrixValues := make([]float64, 0, numBands*numBands)
|
||||
for _, row := range matrix {
|
||||
for _, value := range row {
|
||||
matrixValues = append(matrixValues, value)
|
||||
}
|
||||
}
|
||||
|
||||
// Convert the Go slice to a C array and get its size
|
||||
matrixPtr := unsafe.Pointer(&matrixValues[0])
|
||||
matrixSize := C.size_t(len(matrixValues) * 8) // 8 bytes for each float64
|
||||
|
||||
// Create a VipsImage from the matrix in memory
|
||||
matrixImage := C.vips_image_new_from_memory(matrixPtr, matrixSize, C.int(numBands), C.int(numBands), 1, C.VIPS_FORMAT_DOUBLE)
|
||||
defer clearImage(matrixImage)
|
||||
|
||||
if matrixImage == nil {
|
||||
return handleVipsError()
|
||||
}
|
||||
|
||||
// Recombine the image using the matrix
|
||||
out, err := vipsGenRecomb(r.image, matrixImage)
|
||||
runtime.KeepAlive(matrixValues)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Rotate rotates the image by multiples of 90 degrees. To rotate by arbitrary angles use Similarity.
|
||||
func (r *ImageRef) Rotate(angle Angle) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
width := r.Width()
|
||||
pages := r.loadedPages()
|
||||
|
||||
if pages > 1 && angle == Angle180 {
|
||||
// Rotating the strip would reverse the frame order.
|
||||
out, err := vipsMapPages(r.image, func(page *C.VipsImage) (*C.VipsImage, error) {
|
||||
return vipsGenRot(page, angle)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
if pages > 1 && (angle == Angle90 || angle == Angle270) {
|
||||
if angle == Angle270 {
|
||||
if err := r.Flip(DirectionHorizontal); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := r.Grid(r.PageHeight(), pages, 1); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if angle == Angle270 {
|
||||
if err := r.Flip(DirectionHorizontal); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
out, err := vipsGenRot(r.image, angle)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
|
||||
if pages > 1 && (angle == Angle90 || angle == Angle270) {
|
||||
if err := r.SetPageHeight(width); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Similarity lets you scale, offset and rotate images by arbitrary angles in a single operation while defining the
|
||||
// color of new background pixels. If the input image has no alpha channel, the alpha on `backgroundColor` will be
|
||||
// ignored. You can add an alpha channel to an image with `BandJoinConst` (e.g. `img.BandJoinConst([]float64{255})`) or
|
||||
// AddAlpha.
|
||||
func (r *ImageRef) Similarity(scale float64, angle float64, backgroundColor *ColorRGBA,
|
||||
idx float64, idy float64, odx float64, ody float64) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsSimilarity(r.image, scale, angle, backgroundColor, idx, idy, odx, ody)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Grid tiles the image pages into a matrix across*down
|
||||
func (r *ImageRef) Grid(tileHeight, across, down int) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenGrid(r.image, tileHeight, across, down)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// SmartCrop will crop the image based on interesting factor
|
||||
func (r *ImageRef) SmartCrop(width int, height int, interesting Interesting) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsSmartCrop(r.image, width, height, interesting)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Crop will crop the image based on coordinate and box size
|
||||
func (r *ImageRef) Crop(left int, top int, width int, height int) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsCrop(r.image, left, top, width, height)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Label overlays a label on top of the image
|
||||
func (r *ImageRef) Label(labelParams *LabelParams) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := labelImage(r.image, labelParams)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Replicate repeats an image many times across and down
|
||||
func (r *ImageRef) Replicate(across int, down int) error {
|
||||
defer runtime.KeepAlive(r)
|
||||
out, err := vipsGenReplicate(r.image, across, down)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.setImage(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Pixelate applies a simple pixelate filter to the image
|
||||
func Pixelate(imageRef *ImageRef, factor float64) (err error) {
|
||||
if factor < 1 {
|
||||
return errors.New("factor must be greater then 1")
|
||||
}
|
||||
|
||||
width := imageRef.Width()
|
||||
height := imageRef.Height()
|
||||
|
||||
if err = imageRef.Resize(1/factor, KernelAuto); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
hScale := float64(width) / float64(imageRef.Width())
|
||||
vScale := float64(height) / float64(imageRef.Height())
|
||||
if err = imageRef.ResizeWithVScale(hScale, vScale, KernelNearest); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package vips
|
||||
|
||||
// #include <vips/vips.h>
|
||||
import "C"
|
||||
|
||||
import "unsafe"
|
||||
|
||||
// NewInterpolate creates a VipsInterpolate from a name string.
|
||||
// Common names: "nearest", "bilinear", "bicubic", "nohalo", "vsqbs", "lbb".
|
||||
// The caller should call g_object_unref on the result when done, or pass it
|
||||
// to a generated operation (which does not take ownership).
|
||||
func NewInterpolate(name string) (*C.VipsInterpolate, error) {
|
||||
cName := C.CString(name)
|
||||
defer C.free(unsafe.Pointer(cName))
|
||||
interp := C.vips_interpolate_new(cName)
|
||||
if interp == nil {
|
||||
return nil, handleVipsError()
|
||||
}
|
||||
return interp, nil
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package vips
|
||||
|
||||
// #include <vips/vips.h>
|
||||
// #include <stdlib.h>
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func freeCString(s *C.char) {
|
||||
C.free(unsafe.Pointer(s))
|
||||
}
|
||||
|
||||
func gFreePointer(ref unsafe.Pointer) {
|
||||
C.g_free(C.gpointer(ref))
|
||||
}
|
||||
|
||||
func boolToInt(b bool) int {
|
||||
if b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func toGboolean(b bool) C.gboolean {
|
||||
if b {
|
||||
return C.gboolean(1)
|
||||
}
|
||||
return C.gboolean(0)
|
||||
}
|
||||
|
||||
func fromGboolean(b C.gboolean) bool {
|
||||
return b != 0
|
||||
}
|
||||
|
||||
func fromCArrayInt(out *C.int, n int) []int {
|
||||
var result = make([]int, n)
|
||||
var data []C.int
|
||||
sh := (*reflect.SliceHeader)(unsafe.Pointer(&data))
|
||||
sh.Data = uintptr(unsafe.Pointer(out))
|
||||
sh.Len = n
|
||||
sh.Cap = n
|
||||
for i := range data {
|
||||
result[i] = int(data[i])
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func fromCArrayDouble(out *C.double, n int) []float64 {
|
||||
if out == nil || n <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
data := make([]float64, n)
|
||||
for i := 0; i < n; i++ {
|
||||
data[i] = float64(*(*C.double)(unsafe.Pointer(uintptr(unsafe.Pointer(out)) + uintptr(i)*unsafe.Sizeof(C.double(0)))))
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
#include <vips/vips.h>
|
||||
@@ -1,98 +0,0 @@
|
||||
package vips
|
||||
|
||||
// #include <glib.h>
|
||||
import "C"
|
||||
import (
|
||||
"log"
|
||||
)
|
||||
|
||||
// LogLevel is the enum controlling logging message verbosity.
|
||||
type LogLevel int
|
||||
|
||||
// The logging verbosity levels classify and filter logging messages.
|
||||
// From most to least verbose, they are debug, info, message, warning, critical and error.
|
||||
const (
|
||||
LogLevelError LogLevel = C.G_LOG_LEVEL_ERROR
|
||||
LogLevelCritical LogLevel = C.G_LOG_LEVEL_CRITICAL
|
||||
LogLevelWarning LogLevel = C.G_LOG_LEVEL_WARNING
|
||||
LogLevelMessage LogLevel = C.G_LOG_LEVEL_MESSAGE
|
||||
LogLevelInfo LogLevel = C.G_LOG_LEVEL_INFO
|
||||
LogLevelDebug LogLevel = C.G_LOG_LEVEL_DEBUG
|
||||
)
|
||||
|
||||
// Three global variables which keep state of the current logging handler
|
||||
// function, desired verbosity for logging and whether defaults have been
|
||||
// overridden. Set by LoggingSettings()
|
||||
var (
|
||||
currentLoggingHandlerFunction LoggingHandlerFunction
|
||||
currentLoggingVerbosity LogLevel
|
||||
currentLoggingOverridden bool
|
||||
)
|
||||
|
||||
// govipsLoggingHandler is the private bridge function exported to the C library
|
||||
// and called by glib and libvips for each logging message. It will call govipsLog
|
||||
// which in turn will filter based on verbosity and direct the messages to the
|
||||
// currently chosen LoggingHandlerFunction.
|
||||
//
|
||||
//export govipsLoggingHandler
|
||||
func govipsLoggingHandler(messageDomain *C.char, messageLevel C.int, message *C.char) {
|
||||
govipsLog(C.GoString(messageDomain), LogLevel(messageLevel), C.GoString(message))
|
||||
}
|
||||
|
||||
// LoggingHandlerFunction is a function which will be called for each log message.
|
||||
// By default, govips sends logging messages to os.Stderr. If you want to log elsewhere
|
||||
// such as to a file or to a state variable which you inspect yourself, define a new
|
||||
// logging handler function and set it via LoggingSettings().
|
||||
type LoggingHandlerFunction func(messageDomain string, messageLevel LogLevel, message string)
|
||||
|
||||
// LoggingSettings sets the logging handler and logging verbosity for govips.
|
||||
// The handler function is the function which will be called for each log message.
|
||||
// You can define one yourself to log somewhere else besides the default (stderr).
|
||||
// Use nil as handler to use standard logging handler.
|
||||
// Verbosity is the minimum logLevel you want to log. Default is logLevelInfo
|
||||
// due to backwards compatibility but it's quite verbose for a library.
|
||||
// Suggest setting it to at least logLevelWarning. Use logLevelDebug for debugging.
|
||||
func LoggingSettings(handler LoggingHandlerFunction, verbosity LogLevel) {
|
||||
currentLoggingOverridden = true
|
||||
govipsLoggingSettings(handler, verbosity)
|
||||
}
|
||||
|
||||
func govipsLoggingSettings(handler LoggingHandlerFunction, verbosity LogLevel) {
|
||||
if handler == nil {
|
||||
currentLoggingHandlerFunction = defaultLoggingHandlerFunction
|
||||
} else {
|
||||
currentLoggingHandlerFunction = handler
|
||||
}
|
||||
|
||||
currentLoggingVerbosity = verbosity
|
||||
// TODO turn on debugging in libvips and redirect to handler when setting verbosity to debug
|
||||
// This way debugging information would go to the same channel as all other logging
|
||||
}
|
||||
|
||||
func defaultLoggingHandlerFunction(messageDomain string, messageLevel LogLevel, message string) {
|
||||
var messageLevelDescription string
|
||||
switch messageLevel {
|
||||
case LogLevelError:
|
||||
messageLevelDescription = "error"
|
||||
case LogLevelCritical:
|
||||
messageLevelDescription = "critical"
|
||||
case LogLevelWarning:
|
||||
messageLevelDescription = "warning"
|
||||
case LogLevelMessage:
|
||||
messageLevelDescription = "message"
|
||||
case LogLevelInfo:
|
||||
messageLevelDescription = "info"
|
||||
case LogLevelDebug:
|
||||
messageLevelDescription = "debug"
|
||||
}
|
||||
|
||||
log.Printf("[%v.%v] %v", messageDomain, messageLevelDescription, message)
|
||||
}
|
||||
|
||||
// govipsLog is the default function used to log debug or error messages internally in govips.
|
||||
// It's used by all govips functionality directly, as well as by glib and libvips via the C bridge.
|
||||
func govipsLog(messageDomain string, messageLevel LogLevel, message string) {
|
||||
if messageLevel <= currentLoggingVerbosity {
|
||||
currentLoggingHandlerFunction(messageDomain, messageLevel, message)
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package vips
|
||||
|
||||
import "math"
|
||||
|
||||
// Scalar is the basic scalar measurement of an image's height, width or offset coordinate.
|
||||
type Scalar struct {
|
||||
Value float64
|
||||
Relative bool
|
||||
}
|
||||
|
||||
// ValueOf takes a floating point value and returns a corresponding Scalar struct
|
||||
func ValueOf(value float64) Scalar {
|
||||
return Scalar{value, false}
|
||||
}
|
||||
|
||||
// IsZero checks whether the associated Scalar's value is zero.
|
||||
func (s *Scalar) IsZero() bool {
|
||||
return s.Value == 0 && !s.Relative
|
||||
}
|
||||
|
||||
// SetInt sets an integer value for the associated Scalar.
|
||||
func (s *Scalar) SetInt(value int) {
|
||||
s.Set(float64(value))
|
||||
}
|
||||
|
||||
// Set sets a float value for the associated Scalar.
|
||||
func (s *Scalar) Set(value float64) {
|
||||
s.Value = value
|
||||
s.Relative = false
|
||||
}
|
||||
|
||||
// SetScale sets a float value for the associated Scalar and makes it relative.
|
||||
func (s *Scalar) SetScale(f float64) {
|
||||
s.Value = f
|
||||
s.Relative = true
|
||||
}
|
||||
|
||||
// Get returns the value of the scalar. Either absolute, or if relative, multiplied by the base given as parameter.
|
||||
func (s *Scalar) Get(base int) float64 {
|
||||
if s.Relative {
|
||||
return s.Value * float64(base)
|
||||
}
|
||||
return s.Value
|
||||
}
|
||||
|
||||
// GetRounded returns the value of the associated Scalar, rounded to the nearest integer, if absolute.
|
||||
// If the Scalar is relative, it will be multiplied by the supplied base parameter.
|
||||
func (s *Scalar) GetRounded(base int) int {
|
||||
return roundFloat(s.Get(base))
|
||||
}
|
||||
|
||||
func roundFloat(f float64) int {
|
||||
if f < 0 {
|
||||
return int(math.Ceil(f - 0.5))
|
||||
}
|
||||
return int(math.Floor(f + 0.5))
|
||||
}
|
||||
@@ -1,505 +0,0 @@
|
||||
// operations.c - Hand-written C bridge functions for libvips operations
|
||||
|
||||
#include "lang.h"
|
||||
#include "operations.h"
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
static int is_16bit(VipsInterpretation interpretation);
|
||||
|
||||
// Arithmetic
|
||||
|
||||
int find_trim(VipsImage *in, int *left, int *top, int *width, int *height,
|
||||
double threshold, double r, double g, double b) {
|
||||
|
||||
if (in->Type == VIPS_INTERPRETATION_RGB16 || in->Type == VIPS_INTERPRETATION_GREY16) {
|
||||
r = 65535 * r / 255;
|
||||
g = 65535 * g / 255;
|
||||
b = 65535 * b / 255;
|
||||
}
|
||||
|
||||
double background[3] = {r, g, b};
|
||||
VipsArrayDouble *vipsBackground = vips_array_double_new(background, 3);
|
||||
|
||||
int code = vips_find_trim(in, left, top, width, height, "threshold", threshold, "background", vipsBackground, NULL);
|
||||
|
||||
vips_area_unref(VIPS_AREA(vipsBackground));
|
||||
return code;
|
||||
}
|
||||
|
||||
int getpoint(VipsImage *in, double **vector, int n, int x, int y) {
|
||||
return vips_getpoint(in, vector, &n, x, y, NULL);
|
||||
}
|
||||
|
||||
int minOp(VipsImage *in, double *out, int *x, int *y, int size) {
|
||||
return vips_min(in, out, "x", x, "y", y, "size", size, NULL);
|
||||
}
|
||||
|
||||
// Color
|
||||
|
||||
int is_colorspace_supported(VipsImage *in) {
|
||||
return vips_colourspace_issupported(in) ? 1 : 0;
|
||||
}
|
||||
|
||||
int to_colorspace(VipsImage *in, VipsImage **out, VipsInterpretation space) {
|
||||
return vips_colourspace(in, out, space, NULL);
|
||||
}
|
||||
|
||||
// https://libvips.github.io/libvips/API/8.6/libvips-colour.html#vips-icc-transform
|
||||
int icc_transform(VipsImage *in, VipsImage **out, const char *output_profile, const char *input_profile, VipsIntent intent,
|
||||
int depth, gboolean embedded) {
|
||||
return vips_icc_transform(
|
||||
in, out, output_profile,
|
||||
"input_profile", input_profile ? input_profile : "none",
|
||||
"intent", intent,
|
||||
"depth", depth ? depth : 8,
|
||||
"embedded", embedded,
|
||||
NULL);
|
||||
}
|
||||
|
||||
// Conversion
|
||||
|
||||
// background_for_bands fills vec with a background vector sized to the
|
||||
// image's band count and returns the element count; libvips rejects
|
||||
// vectors longer than the band count (e.g. "linear: vector must have 1
|
||||
// or 2 elements" for greyscale images). Greyscale images take the R
|
||||
// value as the grey level: 1 band -> {r}, 2 bands -> {r, a}.
|
||||
static int background_for_bands(VipsImage *in, double vec[4], double r,
|
||||
double g, double b, double a) {
|
||||
switch (in->Bands) {
|
||||
case 1:
|
||||
vec[0] = r;
|
||||
return 1;
|
||||
case 2:
|
||||
vec[0] = r;
|
||||
vec[1] = a;
|
||||
return 2;
|
||||
case 3:
|
||||
vec[0] = r;
|
||||
vec[1] = g;
|
||||
vec[2] = b;
|
||||
return 3;
|
||||
default:
|
||||
vec[0] = r;
|
||||
vec[1] = g;
|
||||
vec[2] = b;
|
||||
vec[3] = a;
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
|
||||
int embed_image(VipsImage *in, VipsImage **out, int left, int top, int width,
|
||||
int height, int extend) {
|
||||
return vips_embed(in, out, left, top, width, height, "extend", extend, NULL);
|
||||
}
|
||||
|
||||
int embed_image_background(VipsImage *in, VipsImage **out, int left, int top, int width,
|
||||
int height, double r, double g, double b, double a) {
|
||||
|
||||
double background[4];
|
||||
int n = background_for_bands(in, background, r, g, b, a);
|
||||
VipsArrayDouble *vipsBackground = vips_array_double_new(background, n);
|
||||
|
||||
int code = vips_embed(in, out, left, top, width, height,
|
||||
"extend", VIPS_EXTEND_BACKGROUND, "background", vipsBackground, NULL);
|
||||
|
||||
vips_area_unref(VIPS_AREA(vipsBackground));
|
||||
return code;
|
||||
}
|
||||
|
||||
int embed_multi_page_image(VipsImage *in, VipsImage **out, int left, int top, int width,
|
||||
int height, int extend) {
|
||||
VipsObject *base = VIPS_OBJECT(vips_image_new());
|
||||
int page_height = vips_image_get_page_height(in);
|
||||
int in_width = in->Xsize;
|
||||
int n_pages = in->Ysize / page_height;
|
||||
|
||||
VipsImage **page = (VipsImage **) vips_object_local_array(base, n_pages);
|
||||
VipsImage **embedded_page = (VipsImage **) vips_object_local_array(base, n_pages);
|
||||
VipsImage **copy = (VipsImage **) vips_object_local_array(base, 1);
|
||||
|
||||
// split image into cropped frames
|
||||
for (int i = 0; i < n_pages; i++) {
|
||||
if (
|
||||
vips_extract_area(in, &page[i], 0, page_height * i, in_width, page_height, NULL) ||
|
||||
vips_embed(page[i], &embedded_page[i], left, top, width, height, "extend", extend, NULL)
|
||||
) {
|
||||
g_object_unref(base);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
// reassemble frames and set page height
|
||||
// copy before modifying metadata
|
||||
if(
|
||||
vips_arrayjoin(embedded_page, ©[0], n_pages, "across", 1, NULL) ||
|
||||
vips_copy(copy[0], out, NULL)
|
||||
) {
|
||||
g_object_unref(base);
|
||||
return -1;
|
||||
}
|
||||
vips_image_set_int(*out, VIPS_META_PAGE_HEIGHT, height);
|
||||
g_object_unref(base);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int embed_multi_page_image_background(VipsImage *in, VipsImage **out, int left, int top, int width,
|
||||
int height, double r, double g, double b, double a) {
|
||||
double background[4];
|
||||
int n = background_for_bands(in, background, r, g, b, a);
|
||||
VipsArrayDouble *vipsBackground = vips_array_double_new(background, n);
|
||||
|
||||
VipsObject *base = VIPS_OBJECT(vips_image_new());
|
||||
int page_height = vips_image_get_page_height(in);
|
||||
int in_width = in->Xsize;
|
||||
int n_pages = in->Ysize / page_height;
|
||||
|
||||
VipsImage **page = (VipsImage **) vips_object_local_array(base, n_pages);
|
||||
VipsImage **embedded_page = (VipsImage **) vips_object_local_array(base, n_pages);
|
||||
VipsImage **copy = (VipsImage **) vips_object_local_array(base, 1);
|
||||
|
||||
// split image into cropped frames
|
||||
for (int i = 0; i < n_pages; i++) {
|
||||
if (
|
||||
vips_extract_area(in, &page[i], 0, page_height * i, in_width, page_height, NULL) ||
|
||||
vips_embed(page[i], &embedded_page[i], left, top, width, height,
|
||||
"extend", VIPS_EXTEND_BACKGROUND, "background", vipsBackground, NULL)
|
||||
) {
|
||||
vips_area_unref(VIPS_AREA(vipsBackground));
|
||||
g_object_unref(base);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
// reassemble frames and set page height
|
||||
// copy before modifying metadata
|
||||
if(
|
||||
vips_arrayjoin(embedded_page, ©[0], n_pages, "across", 1, NULL) ||
|
||||
vips_copy(copy[0], out, NULL)
|
||||
) {
|
||||
vips_area_unref(VIPS_AREA(vipsBackground));
|
||||
g_object_unref(base);
|
||||
return -1;
|
||||
}
|
||||
vips_image_set_int(*out, VIPS_META_PAGE_HEIGHT, height);
|
||||
vips_area_unref(VIPS_AREA(vipsBackground));
|
||||
g_object_unref(base);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int similarity(VipsImage *in, VipsImage **out, double scale, double angle,
|
||||
double r, double g, double b, double a, double idx, double idy,
|
||||
double odx, double ody) {
|
||||
if (is_16bit(in->Type)) {
|
||||
r = 65535 * r / 255;
|
||||
g = 65535 * g / 255;
|
||||
b = 65535 * b / 255;
|
||||
a = 65535 * a / 255;
|
||||
}
|
||||
|
||||
double background[4];
|
||||
int n = background_for_bands(in, background, r, g, b, a);
|
||||
VipsArrayDouble *vipsBackground = vips_array_double_new(background, n);
|
||||
|
||||
int code = vips_similarity(in, out, "scale", scale, "angle", angle,
|
||||
"background", vipsBackground, "idx", idx, "idy",
|
||||
idy, "odx", odx, "ody", ody, NULL);
|
||||
|
||||
vips_area_unref(VIPS_AREA(vipsBackground));
|
||||
return code;
|
||||
}
|
||||
|
||||
int crop(VipsImage *in, VipsImage **out, int left, int top,
|
||||
int width, int height) {
|
||||
// resolve image pages
|
||||
int page_height = vips_image_get_page_height(in);
|
||||
int n_pages = in->Ysize / page_height;
|
||||
if (n_pages <= 1) {
|
||||
return vips_crop(in, out, left, top, width, height, NULL);
|
||||
}
|
||||
|
||||
int in_width = in->Xsize;
|
||||
VipsObject *base = VIPS_OBJECT(vips_image_new());
|
||||
VipsImage **page = (VipsImage **) vips_object_local_array(base, n_pages);
|
||||
VipsImage **cropped_page = (VipsImage **) vips_object_local_array(base, n_pages);
|
||||
VipsImage **copy = (VipsImage **) vips_object_local_array(base, 1);
|
||||
// split image into cropped frames
|
||||
for (int i = 0; i < n_pages; i++) {
|
||||
if (
|
||||
vips_extract_area(in, &page[i], 0, page_height * i, in_width, page_height, NULL) ||
|
||||
vips_crop(page[i], &cropped_page[i], left, top, width, height, NULL)
|
||||
) {
|
||||
g_object_unref(base);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// reassemble frames and set page height
|
||||
// copy before modifying metadata
|
||||
if(
|
||||
vips_arrayjoin(cropped_page, ©[0], n_pages, "across", 1, NULL) ||
|
||||
vips_copy(copy[0], out, NULL)
|
||||
) {
|
||||
g_object_unref(base);
|
||||
return -1;
|
||||
}
|
||||
vips_image_set_int(*out, VIPS_META_PAGE_HEIGHT, height);
|
||||
g_object_unref(base);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int is_16bit(VipsInterpretation interpretation) {
|
||||
return interpretation == VIPS_INTERPRETATION_RGB16 ||
|
||||
interpretation == VIPS_INTERPRETATION_GREY16;
|
||||
}
|
||||
|
||||
int composite_image(VipsImage **in, VipsImage **out, int n, int *mode, int *x,
|
||||
int *y) {
|
||||
VipsArrayInt *xs = vips_array_int_new(x, n - 1);
|
||||
VipsArrayInt *ys = vips_array_int_new(y, n - 1);
|
||||
|
||||
int code = vips_composite(in, out, n, mode, "x", xs, "y", ys, NULL);
|
||||
|
||||
vips_area_unref(VIPS_AREA(xs));
|
||||
vips_area_unref(VIPS_AREA(ys));
|
||||
return code;
|
||||
}
|
||||
|
||||
int join(VipsImage *in1, VipsImage *in2, VipsImage **out, int direction) {
|
||||
return vips_join(in1, in2, out, direction, NULL);
|
||||
}
|
||||
|
||||
int add_alpha(VipsImage *in, VipsImage **out) {
|
||||
return vips_addalpha(in, out, NULL);
|
||||
}
|
||||
|
||||
// Create
|
||||
|
||||
// https://libvips.github.io/libvips/API/current/libvips-create.html#vips-text
|
||||
int text(VipsImage **out, TextOptions *o) {
|
||||
return vips_text(out, o->Text, "font", o->Font, "width", o->Width, "height", o->Height, "align", o->Align,
|
||||
"dpi", o->DPI, "rgba", o->RGBA, "justify", o->Justify, "spacing", o->Spacing, "wrap", o->Wrap, NULL);
|
||||
}
|
||||
|
||||
// Draw
|
||||
|
||||
int draw_rect(VipsImage *in, double r, double g, double b, double a, int left,
|
||||
int top, int width, int height, int fill) {
|
||||
if (is_16bit(in->Type)) {
|
||||
r = 65535 * r / 255;
|
||||
g = 65535 * g / 255;
|
||||
b = 65535 * b / 255;
|
||||
a = 65535 * a / 255;
|
||||
}
|
||||
|
||||
double ink[4];
|
||||
int n = background_for_bands(in, ink, r, g, b, a);
|
||||
return vips_draw_rect(in, ink, n, left, top, width, height, "fill", fill,
|
||||
NULL);
|
||||
}
|
||||
|
||||
// Header
|
||||
|
||||
unsigned long has_icc_profile(VipsImage *in) {
|
||||
return vips_image_get_typeof(in, VIPS_META_ICC_NAME);
|
||||
}
|
||||
|
||||
unsigned long get_icc_profile(VipsImage *in, const void **data,
|
||||
size_t *dataLength) {
|
||||
return image_get_blob(in, VIPS_META_ICC_NAME, data, dataLength);
|
||||
}
|
||||
|
||||
gboolean remove_icc_profile(VipsImage *in) {
|
||||
return vips_image_remove(in, VIPS_META_ICC_NAME);
|
||||
}
|
||||
|
||||
unsigned long has_iptc(VipsImage *in) {
|
||||
return vips_image_get_typeof(in, VIPS_META_IPTC_NAME);
|
||||
}
|
||||
|
||||
char **image_get_fields(VipsImage *in) { return vips_image_get_fields(in); }
|
||||
|
||||
void image_set_string(VipsImage *in, const char *name, const char *str) {
|
||||
vips_image_set_string(in, name, str);
|
||||
}
|
||||
|
||||
unsigned long image_get_string(VipsImage *in, const char *name,
|
||||
const char **out) {
|
||||
return vips_image_get_string(in, name, out);
|
||||
}
|
||||
|
||||
unsigned long image_get_as_string(VipsImage *in, const char *name, char **out) {
|
||||
return vips_image_get_as_string(in, name, out);
|
||||
}
|
||||
|
||||
void remove_field(VipsImage *in, char *field) { vips_image_remove(in, field); }
|
||||
|
||||
int get_meta_orientation(VipsImage *in) {
|
||||
int orientation = 0;
|
||||
if (vips_image_get_typeof(in, VIPS_META_ORIENTATION) != 0) {
|
||||
vips_image_get_int(in, VIPS_META_ORIENTATION, &orientation);
|
||||
}
|
||||
|
||||
return orientation;
|
||||
}
|
||||
|
||||
void remove_meta_orientation(VipsImage *in) {
|
||||
vips_image_remove(in, VIPS_META_ORIENTATION);
|
||||
}
|
||||
|
||||
void set_meta_orientation(VipsImage *in, int orientation) {
|
||||
vips_image_set_int(in, VIPS_META_ORIENTATION, orientation);
|
||||
}
|
||||
|
||||
// https://libvips.github.io/libvips/API/current/libvips-header.html#vips-image-get-n-pages
|
||||
int get_image_n_pages(VipsImage *in) {
|
||||
int n_pages = 0;
|
||||
n_pages = vips_image_get_n_pages(in);
|
||||
return n_pages;
|
||||
}
|
||||
|
||||
void set_image_n_pages(VipsImage *in, int n_pages) {
|
||||
vips_image_set_int(in, VIPS_META_N_PAGES, n_pages);
|
||||
}
|
||||
|
||||
// https://www.libvips.org/API/current/libvips-header.html#vips-image-get-page-height
|
||||
int get_page_height(VipsImage *in) {
|
||||
int page_height = 0;
|
||||
page_height = vips_image_get_page_height(in);
|
||||
return page_height;
|
||||
}
|
||||
|
||||
void set_page_height(VipsImage *in, int height) {
|
||||
vips_image_set_int(in, VIPS_META_PAGE_HEIGHT, height);
|
||||
}
|
||||
|
||||
int get_meta_loader(const VipsImage *in, const char **out) {
|
||||
return vips_image_get_string(in, VIPS_META_LOADER, out);
|
||||
}
|
||||
|
||||
int get_background(VipsImage *in, double **out, int *n) {
|
||||
return vips_image_get_array_double(in, "background", out, n);
|
||||
}
|
||||
|
||||
int get_image_delay(VipsImage *in, int **out) {
|
||||
return vips_image_get_array_int(in, "delay", out, NULL);
|
||||
}
|
||||
|
||||
void set_image_delay(VipsImage *in, const int *array, int n) {
|
||||
vips_image_set_array_int(in, "delay", array, n);
|
||||
}
|
||||
|
||||
int get_image_loop(VipsImage *in) {
|
||||
int loop = 0;
|
||||
if (vips_image_get_typeof(in, "loop") != 0) {
|
||||
vips_image_get_int(in, "loop", &loop);
|
||||
}
|
||||
return loop;
|
||||
}
|
||||
|
||||
void set_image_loop(VipsImage *in, int loop) {
|
||||
vips_image_set_int(in, "loop", loop);
|
||||
}
|
||||
|
||||
void image_set_double(VipsImage *in, const char *name, double i) {
|
||||
vips_image_set_double(in, name, i);
|
||||
}
|
||||
|
||||
unsigned long image_get_double(VipsImage *in, const char *name, double *out) {
|
||||
return vips_image_get_double(in, name, out);
|
||||
}
|
||||
|
||||
void image_set_int(VipsImage *in, const char *name, int i) {
|
||||
vips_image_set_int(in, name, i);
|
||||
}
|
||||
|
||||
unsigned long image_get_int(VipsImage *in, const char *name, int *out) {
|
||||
return vips_image_get_int(in, name, out);
|
||||
}
|
||||
|
||||
void image_set_blob(VipsImage *in, const char *name, const void *data,
|
||||
size_t dataLength) {
|
||||
vips_image_set_blob_copy(in, name, data, dataLength);
|
||||
}
|
||||
|
||||
unsigned long image_get_blob(VipsImage *in, const char *name, const void **data,
|
||||
size_t *dataLength) {
|
||||
if (vips_image_get_typeof(in, name) == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (vips_image_get_blob(in, name, data, dataLength)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Label
|
||||
|
||||
int label(VipsImage *in, VipsImage **out, LabelOptions *o) {
|
||||
double ones[3] = {1, 1, 1};
|
||||
VipsImage *base = vips_image_new();
|
||||
VipsImage **t = (VipsImage **)vips_object_local_array(VIPS_OBJECT(base), 9);
|
||||
if (vips_text(&t[0], o->Text, "font", o->Font, "width", o->Width, "height",
|
||||
o->Height, "align", o->Align, NULL) ||
|
||||
vips_linear1(t[0], &t[1], o->Opacity, 0.0, NULL) ||
|
||||
vips_cast(t[1], &t[2], VIPS_FORMAT_UCHAR, NULL) ||
|
||||
vips_embed(t[2], &t[3], o->OffsetX, o->OffsetY, t[2]->Xsize + o->OffsetX,
|
||||
t[2]->Ysize + o->OffsetY, NULL)) {
|
||||
g_object_unref(base);
|
||||
return -1;
|
||||
}
|
||||
if (vips_black(&t[4], 1, 1, NULL) ||
|
||||
vips_linear(t[4], &t[5], ones, o->Color, 3, NULL) ||
|
||||
vips_cast(t[5], &t[6], VIPS_FORMAT_UCHAR, NULL) ||
|
||||
vips_copy(t[6], &t[7], "interpretation", in->Type, NULL) ||
|
||||
vips_embed(t[7], &t[8], 0, 0, in->Xsize, in->Ysize, "extend",
|
||||
VIPS_EXTEND_COPY, NULL)) {
|
||||
g_object_unref(base);
|
||||
return -1;
|
||||
}
|
||||
if (vips_ifthenelse(t[3], t[8], in, out, "blend", TRUE, NULL)) {
|
||||
g_object_unref(base);
|
||||
return -1;
|
||||
}
|
||||
g_object_unref(base);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Resample
|
||||
|
||||
int resize_image(VipsImage *in, VipsImage **out, double scale, gdouble vscale,
|
||||
int kernel) {
|
||||
if (vscale > 0) {
|
||||
return vips_resize(in, out, scale, "vscale", vscale, "kernel", kernel,
|
||||
NULL);
|
||||
}
|
||||
|
||||
return vips_resize(in, out, scale, "kernel", kernel, NULL);
|
||||
}
|
||||
|
||||
int thumbnail(const char *filename, VipsImage **out,
|
||||
int width, int height, int crop, int size) {
|
||||
return vips_thumbnail(filename, out, width, "height", height,
|
||||
"crop", crop, "size", size, NULL);
|
||||
}
|
||||
|
||||
int thumbnail_image(VipsImage *in, VipsImage **out, int width, int height,
|
||||
int crop, int size) {
|
||||
return vips_thumbnail_image(in, out, width, "height", height, "crop", crop,
|
||||
"size", size, NULL);
|
||||
}
|
||||
|
||||
int thumbnail_buffer_with_option(void *buf, size_t len, VipsImage **out,
|
||||
int width, int height, int crop, int size,
|
||||
const char *option_string) {
|
||||
return vips_thumbnail_buffer(buf, len, out, width, "height", height,
|
||||
"crop", crop, "size", size,
|
||||
"option_string", option_string, NULL);
|
||||
}
|
||||
|
||||
int thumbnail_buffer(void *buf, size_t len, VipsImage **out,
|
||||
int width, int height, int crop, int size) {
|
||||
return vips_thumbnail_buffer(buf, len, out, width, "height", height,
|
||||
"crop", crop, "size", size, NULL);
|
||||
}
|
||||
@@ -1,149 +0,0 @@
|
||||
// operations.h - Hand-written C bridge functions for libvips operations
|
||||
|
||||
#ifndef OPERATIONS_H
|
||||
#define OPERATIONS_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <vips/vips.h>
|
||||
#include <vips/foreign.h>
|
||||
|
||||
// Arithmetic
|
||||
// https://libvips.github.io/libvips/API/current/libvips-arithmetic.html
|
||||
|
||||
int find_trim(VipsImage *in, int *left, int *top, int *width, int *height,
|
||||
double threshold, double r, double g, double b);
|
||||
int getpoint(VipsImage *in, double **vector, int n, int x, int y);
|
||||
int minOp(VipsImage *in, double *out, int *x, int *y, int size);
|
||||
|
||||
// Color
|
||||
// https://libvips.github.io/libvips/API/current/libvips-colour.html
|
||||
|
||||
int is_colorspace_supported(VipsImage *in);
|
||||
int to_colorspace(VipsImage *in, VipsImage **out, VipsInterpretation space);
|
||||
int icc_transform(VipsImage *in, VipsImage **out, const char *output_profile,
|
||||
const char *input_profile, VipsIntent intent, int depth,
|
||||
gboolean embedded);
|
||||
|
||||
// Conversion
|
||||
// https://libvips.github.io/libvips/API/current/libvips-conversion.html
|
||||
|
||||
int embed_image(VipsImage *in, VipsImage **out, int left, int top, int width,
|
||||
int height, int extend);
|
||||
int embed_image_background(VipsImage *in, VipsImage **out, int left, int top,
|
||||
int width, int height, double r, double g, double b, double a);
|
||||
int embed_multi_page_image(VipsImage *in, VipsImage **out, int left, int top,
|
||||
int width, int height, int extend);
|
||||
int embed_multi_page_image_background(VipsImage *in, VipsImage **out, int left,
|
||||
int top, int width, int height, double r, double g, double b,
|
||||
double a);
|
||||
int crop(VipsImage *in, VipsImage **out, int left, int top, int width,
|
||||
int height);
|
||||
int similarity(VipsImage *in, VipsImage **out, double scale, double angle,
|
||||
double r, double g, double b, double a, double idx, double idy,
|
||||
double odx, double ody);
|
||||
int composite_image(VipsImage **in, VipsImage **out, int n, int *mode, int *x,
|
||||
int *y);
|
||||
int join(VipsImage *in1, VipsImage *in2, VipsImage **out, int direction);
|
||||
int add_alpha(VipsImage *in, VipsImage **out);
|
||||
|
||||
// Create
|
||||
// https://libvips.github.io/libvips/API/current/libvips-create.html
|
||||
|
||||
typedef struct {
|
||||
const char *Text;
|
||||
const char *Font;
|
||||
int Width;
|
||||
int Height;
|
||||
int DPI;
|
||||
gboolean RGBA;
|
||||
gboolean Justify;
|
||||
int Spacing;
|
||||
VipsAlign Align;
|
||||
VipsTextWrap Wrap;
|
||||
} TextOptions;
|
||||
|
||||
int text(VipsImage **out, TextOptions *o);
|
||||
|
||||
// Draw
|
||||
// https://libvips.github.io/libvips/API/current/libvips-draw.html
|
||||
|
||||
int draw_rect(VipsImage *in, double r, double g, double b, double a, int left,
|
||||
int top, int width, int height, int fill);
|
||||
|
||||
// Header
|
||||
// https://libvips.github.io/libvips/API/current/libvips-header.html
|
||||
|
||||
unsigned long has_icc_profile(VipsImage *in);
|
||||
unsigned long get_icc_profile(VipsImage *in, const void **data,
|
||||
size_t *dataLength);
|
||||
int remove_icc_profile(VipsImage *in);
|
||||
|
||||
unsigned long has_iptc(VipsImage *in);
|
||||
char **image_get_fields(VipsImage *in);
|
||||
|
||||
void image_set_string(VipsImage *in, const char *name, const char *str);
|
||||
unsigned long image_get_string(VipsImage *in, const char *name,
|
||||
const char **out);
|
||||
unsigned long image_get_as_string(VipsImage *in, const char *name, char **out);
|
||||
|
||||
void remove_field(VipsImage *in, char *field);
|
||||
|
||||
int get_meta_orientation(VipsImage *in);
|
||||
void remove_meta_orientation(VipsImage *in);
|
||||
void set_meta_orientation(VipsImage *in, int orientation);
|
||||
int get_image_n_pages(VipsImage *in);
|
||||
void set_image_n_pages(VipsImage *in, int n_pages);
|
||||
int get_page_height(VipsImage *in);
|
||||
void set_page_height(VipsImage *in, int height);
|
||||
int get_meta_loader(const VipsImage *in, const char **out);
|
||||
int get_image_delay(VipsImage *in, int **out);
|
||||
void set_image_delay(VipsImage *in, const int *array, int n);
|
||||
int get_image_loop(VipsImage *in);
|
||||
void set_image_loop(VipsImage *in, int loop);
|
||||
int get_background(VipsImage *in, double **out, int *n);
|
||||
|
||||
void image_set_blob(VipsImage *in, const char *name, const void *data,
|
||||
size_t dataLength);
|
||||
unsigned long image_get_blob(VipsImage *in, const char *name, const void **data,
|
||||
size_t *dataLength);
|
||||
|
||||
void image_set_double(VipsImage *in, const char *name, double i);
|
||||
unsigned long image_get_double(VipsImage *in, const char *name, double *out);
|
||||
|
||||
void image_set_int(VipsImage *in, const char *name, int i);
|
||||
unsigned long image_get_int(VipsImage *in, const char *name, int *out);
|
||||
|
||||
// Label
|
||||
|
||||
typedef struct {
|
||||
const char *Text;
|
||||
const char *Font;
|
||||
int Width;
|
||||
int Height;
|
||||
int OffsetX;
|
||||
int OffsetY;
|
||||
VipsAlign Align;
|
||||
int DPI;
|
||||
int Margin;
|
||||
float Opacity;
|
||||
double Color[3];
|
||||
} LabelOptions;
|
||||
|
||||
int label(VipsImage *in, VipsImage **out, LabelOptions *o);
|
||||
|
||||
// Resample
|
||||
// https://libvips.github.io/libvips/API/current/libvips-resample.html
|
||||
|
||||
int resize_image(VipsImage *in, VipsImage **out, double scale, gdouble vscale,
|
||||
int kernel);
|
||||
int thumbnail(const char *filename, VipsImage **out, int width, int height,
|
||||
int crop, int size);
|
||||
int thumbnail_image(VipsImage *in, VipsImage **out, int width, int height,
|
||||
int crop, int size);
|
||||
int thumbnail_buffer(void *buf, size_t len, VipsImage **out, int width,
|
||||
int height, int crop, int size);
|
||||
int thumbnail_buffer_with_option(void *buf, size_t len, VipsImage **out,
|
||||
int width, int height, int crop, int size,
|
||||
const char *option_string);
|
||||
|
||||
#endif // OPERATIONS_H
|
||||
@@ -1,56 +0,0 @@
|
||||
package vips
|
||||
|
||||
import "sync"
|
||||
|
||||
// RuntimeStats is a data structure to house a map of govips operation counts
|
||||
type RuntimeStats struct {
|
||||
OperationCounts map[string]int64
|
||||
}
|
||||
|
||||
var (
|
||||
operationCounter chan string
|
||||
runtimeStats *RuntimeStats
|
||||
statLock sync.RWMutex
|
||||
)
|
||||
|
||||
func incOpCounter(op string) {
|
||||
if operationCounter != nil {
|
||||
operationCounter <- op
|
||||
}
|
||||
}
|
||||
|
||||
func collectStats() chan struct{} {
|
||||
operationCounter = make(chan string, 100)
|
||||
done := make(chan struct{})
|
||||
exit := false
|
||||
go func() {
|
||||
for !exit {
|
||||
select {
|
||||
case op := <-operationCounter:
|
||||
statLock.Lock()
|
||||
runtimeStats.OperationCounts[op] = runtimeStats.OperationCounts[op] + 1
|
||||
statLock.Unlock()
|
||||
case <-done:
|
||||
exit = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}()
|
||||
return done
|
||||
}
|
||||
|
||||
// ReadRuntimeStats returns operation counts for govips
|
||||
func ReadRuntimeStats(stats *RuntimeStats) {
|
||||
statLock.RLock()
|
||||
defer statLock.RUnlock()
|
||||
stats.OperationCounts = make(map[string]int64)
|
||||
for k, v := range runtimeStats.OperationCounts {
|
||||
stats.OperationCounts[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
runtimeStats = &RuntimeStats{
|
||||
OperationCounts: make(map[string]int64),
|
||||
}
|
||||
}
|
||||
@@ -1,316 +0,0 @@
|
||||
#include "stream.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
// Option setters shared with the buffer path, defined in foreign.c.
|
||||
// Reusing them keeps streaming output identical to the buffer output.
|
||||
extern int set_jpegload_options(VipsOperation *operation, LoadParams *params);
|
||||
extern int set_pngload_options(VipsOperation *operation, LoadParams *params);
|
||||
extern int set_webpload_options(VipsOperation *operation, LoadParams *params);
|
||||
extern int set_tiffload_options(VipsOperation *operation, LoadParams *params);
|
||||
extern int set_gifload_options(VipsOperation *operation, LoadParams *params);
|
||||
extern int set_heifload_options(VipsOperation *operation, LoadParams *params);
|
||||
extern int set_svgload_options(VipsOperation *operation, LoadParams *params);
|
||||
extern int set_pdfload_options(VipsOperation *operation, LoadParams *params);
|
||||
extern int set_jp2kload_options(VipsOperation *operation, LoadParams *params);
|
||||
extern int set_jxlload_options(VipsOperation *operation, LoadParams *params);
|
||||
extern int set_magickload_options(VipsOperation *operation, LoadParams *params);
|
||||
|
||||
extern int set_jpegsave_options(VipsOperation *operation, SaveParams *params);
|
||||
extern int set_pngsave_options(VipsOperation *operation, SaveParams *params);
|
||||
extern int set_webpsave_options(VipsOperation *operation, SaveParams *params);
|
||||
extern int set_heifsave_options(VipsOperation *operation, SaveParams *params);
|
||||
extern int set_tiffsave_options(VipsOperation *operation, SaveParams *params);
|
||||
extern int set_gifsave_options(VipsOperation *operation, SaveParams *params);
|
||||
|
||||
// Trampolines: extract the registry handle from signal user_data and
|
||||
// forward to the exported Go callbacks. Buffers are owned by libvips and
|
||||
// only valid for the duration of the call.
|
||||
|
||||
static gint64 source_read_handler(VipsSourceCustom *source, void *buffer,
|
||||
gint64 length, void *user_data) {
|
||||
(void)source;
|
||||
return goSourceReadCb(GPOINTER_TO_INT(user_data), buffer, length);
|
||||
}
|
||||
|
||||
static gint64 source_seek_handler(VipsSourceCustom *source, gint64 offset,
|
||||
int whence, void *user_data) {
|
||||
(void)source;
|
||||
return goSourceSeekCb(GPOINTER_TO_INT(user_data), offset, whence);
|
||||
}
|
||||
|
||||
static gint64 target_write_handler(VipsTargetCustom *target, const void *data,
|
||||
gint64 length, void *user_data) {
|
||||
(void)target;
|
||||
return goTargetWriteCb(GPOINTER_TO_INT(user_data), (void *)data, length);
|
||||
}
|
||||
|
||||
static int target_end_handler(VipsTargetCustom *target, void *user_data) {
|
||||
(void)target;
|
||||
return goTargetEndCb(GPOINTER_TO_INT(user_data));
|
||||
}
|
||||
|
||||
VipsSourceCustom *create_source_custom(int handle, int has_seek) {
|
||||
VipsSourceCustom *source = vips_source_custom_new();
|
||||
if (!source) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
g_signal_connect(source, "read", G_CALLBACK(source_read_handler),
|
||||
GINT_TO_POINTER(handle));
|
||||
if (has_seek) {
|
||||
g_signal_connect(source, "seek", G_CALLBACK(source_seek_handler),
|
||||
GINT_TO_POINTER(handle));
|
||||
}
|
||||
|
||||
return source;
|
||||
}
|
||||
|
||||
VipsTargetCustom *create_target_custom(int handle) {
|
||||
VipsTargetCustom *target = vips_target_custom_new();
|
||||
if (!target) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
g_signal_connect(target, "write", G_CALLBACK(target_write_handler),
|
||||
GINT_TO_POINTER(handle));
|
||||
g_signal_connect(target, "end", G_CALLBACK(target_end_handler),
|
||||
GINT_TO_POINTER(handle));
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
int source_sniff_header(VipsSourceCustom *source, unsigned char *out, int len) {
|
||||
const unsigned char *p = vips_source_sniff(VIPS_SOURCE(source), (size_t) len);
|
||||
if (!p) {
|
||||
return -1;
|
||||
}
|
||||
memcpy(out, p, (size_t) len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static ImageType image_type_for_loader(const char *name) {
|
||||
if (g_str_has_prefix(name, "jpegload")) return JPEG;
|
||||
if (g_str_has_prefix(name, "pngload")) return PNG;
|
||||
if (g_str_has_prefix(name, "webpload")) return WEBP;
|
||||
if (g_str_has_prefix(name, "tiffload")) return TIFF;
|
||||
if (g_str_has_prefix(name, "gifload")) return GIF;
|
||||
if (g_str_has_prefix(name, "heifload")) return HEIF;
|
||||
if (g_str_has_prefix(name, "svgload")) return SVG;
|
||||
if (g_str_has_prefix(name, "pdfload")) return PDF;
|
||||
if (g_str_has_prefix(name, "jp2kload")) return JP2K;
|
||||
if (g_str_has_prefix(name, "jxlload")) return JXL;
|
||||
if (g_str_has_prefix(name, "magickload")) return MAGICK;
|
||||
return UNKNOWN;
|
||||
}
|
||||
|
||||
typedef int (*SetLoadOptionsFn)(VipsOperation *operation, LoadParams *params);
|
||||
|
||||
static int set_noload_options(VipsOperation *operation, LoadParams *params) {
|
||||
(void)operation;
|
||||
(void)params;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static SetLoadOptionsFn load_options_for_type(ImageType imageType) {
|
||||
switch (imageType) {
|
||||
case JPEG:
|
||||
return set_jpegload_options;
|
||||
case PNG:
|
||||
return set_pngload_options;
|
||||
case WEBP:
|
||||
return set_webpload_options;
|
||||
case TIFF:
|
||||
return set_tiffload_options;
|
||||
case GIF:
|
||||
return set_gifload_options;
|
||||
case HEIF:
|
||||
return set_heifload_options;
|
||||
case SVG:
|
||||
return set_svgload_options;
|
||||
case PDF:
|
||||
return set_pdfload_options;
|
||||
case JP2K:
|
||||
return set_jp2kload_options;
|
||||
case JXL:
|
||||
return set_jxlload_options;
|
||||
case MAGICK:
|
||||
return set_magickload_options;
|
||||
default:
|
||||
return set_noload_options;
|
||||
}
|
||||
}
|
||||
|
||||
int load_from_source(VipsSourceCustom *source, LoadParams *params) {
|
||||
// Sniffs the stream start to find a loader. This returns the GType
|
||||
// class name (e.g. "VipsForeignLoadJpegSource"); resolve it to the
|
||||
// operation nickname (e.g. "jpegload_source") for format mapping.
|
||||
const char *className = vips_foreign_find_load_source(VIPS_SOURCE(source));
|
||||
if (!className) {
|
||||
return 1;
|
||||
}
|
||||
const char *operationName = vips_nickname_find(g_type_from_name(className));
|
||||
if (!operationName) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
params->inputFormat = image_type_for_loader(operationName);
|
||||
SetLoadOptionsFn setLoadOptions = load_options_for_type(params->inputFormat);
|
||||
|
||||
VipsOperation *operation = vips_operation_new(operationName);
|
||||
if (!operation) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (vips_object_set(VIPS_OBJECT(operation), "source", source, NULL)) {
|
||||
g_object_unref(operation);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (setLoadOptions(operation, params)) {
|
||||
g_object_unref(operation);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// The deprecated "fail" bool does not make every loader reject
|
||||
// truncated input (e.g. spng pads missing rows with only a warning).
|
||||
// Streaming inputs are network-shaped, so when the caller asked for
|
||||
// fail-on-error escalate to the modern fail_on property at the
|
||||
// "truncated" level: truncated streams fail, ordinary warnings on
|
||||
// valid images do not.
|
||||
if (params->fail.is_set && params->fail.value.b &&
|
||||
g_object_class_find_property(G_OBJECT_GET_CLASS(operation),
|
||||
"fail_on")) {
|
||||
vips_object_set(VIPS_OBJECT(operation), "fail_on",
|
||||
VIPS_FAIL_ON_TRUNCATED, NULL);
|
||||
}
|
||||
|
||||
// Build uncached: the cache key includes the unique source object, so
|
||||
// a hit is impossible — caching would only pin the source and the
|
||||
// lazy image in the operation cache past their natural lifetime.
|
||||
if (vips_object_build(VIPS_OBJECT(operation))) {
|
||||
vips_object_unref_outputs(VIPS_OBJECT(operation));
|
||||
g_object_unref(operation);
|
||||
return 1;
|
||||
}
|
||||
|
||||
g_object_get(VIPS_OBJECT(operation), "out", ¶ms->outputImage, NULL);
|
||||
|
||||
vips_object_unref_outputs(VIPS_OBJECT(operation));
|
||||
g_object_unref(operation);
|
||||
|
||||
// The returned image is LAZY: libvips decodes on demand, pulling from
|
||||
// the source during later operations. The Go side decides whether to
|
||||
// materialize it now (memory or scratch disc, releasing the source
|
||||
// early) or keep the source connected for sequential streaming.
|
||||
return 0;
|
||||
}
|
||||
|
||||
gint64 image_decoded_size(VipsImage *in) {
|
||||
return (gint64)VIPS_IMAGE_SIZEOF_PEL(in) * in->Xsize * in->Ysize;
|
||||
}
|
||||
|
||||
int copy_image_to_memory(VipsImage *in, VipsImage **out) {
|
||||
VipsImage *memory = vips_image_copy_memory(in);
|
||||
if (!memory) {
|
||||
return 1;
|
||||
}
|
||||
*out = memory;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int write_image_to_disc(VipsImage *in, const char *path, VipsImage **out) {
|
||||
// Render the full image to a vips-native (.v) scratch file in one
|
||||
// sequential pass, then reopen it as a random-access image backed by
|
||||
// the file. Decode/reader errors surface here, during the write pass.
|
||||
if (vips_image_write_to_file(in, path, NULL)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
VipsImage *disc =
|
||||
vips_image_new_from_file(path, "access", VIPS_ACCESS_RANDOM, NULL);
|
||||
if (!disc) {
|
||||
return 1;
|
||||
}
|
||||
*out = disc;
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef int (*SetSaveOptionsFn)(VipsOperation *operation, SaveParams *params);
|
||||
|
||||
static int save_target(const char *operationName, SaveParams *params,
|
||||
VipsTargetCustom *target,
|
||||
SetSaveOptionsFn setSaveOptions) {
|
||||
VipsOperation *operation = vips_operation_new(operationName);
|
||||
if (!operation) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (vips_object_set(VIPS_OBJECT(operation), "in", params->inputImage,
|
||||
"target", target, NULL)) {
|
||||
g_object_unref(operation);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (setSaveOptions(operation, params)) {
|
||||
g_object_unref(operation);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Same as save_buffer: "keep" overrides the deprecated "strip".
|
||||
if (params->keepSet &&
|
||||
vips_object_set(VIPS_OBJECT(operation), "keep", params->keep, NULL)) {
|
||||
g_object_unref(operation);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Build uncached, mirroring load_from_source: the unique target
|
||||
// object makes cache hits impossible.
|
||||
if (vips_object_build(VIPS_OBJECT(operation))) {
|
||||
vips_object_unref_outputs(VIPS_OBJECT(operation));
|
||||
g_object_unref(operation);
|
||||
return 1;
|
||||
}
|
||||
|
||||
vips_object_unref_outputs(VIPS_OBJECT(operation));
|
||||
g_object_unref(operation);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int save_jpeg_to_target(SaveParams *params, VipsTargetCustom *target) {
|
||||
return save_target("jpegsave_target", params, target, set_jpegsave_options);
|
||||
}
|
||||
|
||||
int save_png_to_target(SaveParams *params, VipsTargetCustom *target) {
|
||||
return save_target("pngsave_target", params, target, set_pngsave_options);
|
||||
}
|
||||
|
||||
int save_webp_to_target(SaveParams *params, VipsTargetCustom *target) {
|
||||
return save_target("webpsave_target", params, target, set_webpsave_options);
|
||||
}
|
||||
|
||||
int save_heif_to_target(SaveParams *params, VipsTargetCustom *target) {
|
||||
return save_target("heifsave_target", params, target, set_heifsave_options);
|
||||
}
|
||||
|
||||
// No save_tiff_to_target: libtiff requires seekable output, so the Go
|
||||
// side always encodes TIFF through the buffer path.
|
||||
|
||||
int save_gif_to_target(SaveParams *params, VipsTargetCustom *target) {
|
||||
return save_target("gifsave_target", params, target, set_gifsave_options);
|
||||
}
|
||||
|
||||
void clear_source(VipsSourceCustom **source) {
|
||||
if (source && *source) {
|
||||
g_object_unref(*source);
|
||||
*source = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void clear_target(VipsTargetCustom **target) {
|
||||
if (target && *target) {
|
||||
g_object_unref(*target);
|
||||
*target = NULL;
|
||||
}
|
||||
}
|
||||
@@ -1,842 +0,0 @@
|
||||
package vips
|
||||
|
||||
// Streaming I/O via VipsSourceCustom/VipsTargetCustom.
|
||||
//
|
||||
// libvips worker threads pull/push bytes through C trampolines (stream.c)
|
||||
// that dispatch to the exported Go callbacks below via integer handles in
|
||||
// a global registry. Holding a registry reference also keeps the Go
|
||||
// reader/writer alive while the C side may still call back into it.
|
||||
|
||||
// #include "stream.h"
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// sourceEntry is the registry entry for one streaming load. The mutex
|
||||
// serializes callback invocations so callers never need thread-safe
|
||||
// readers, even though libvips may call from multiple worker threads.
|
||||
type sourceEntry struct {
|
||||
reader io.Reader
|
||||
seeker io.Seeker // non-nil only when reader implements io.Seeker
|
||||
mu sync.Mutex
|
||||
lastErr error
|
||||
|
||||
// size is the lazily resolved length of the seekable source,
|
||||
// cached for clamping past-EOF seek targets. Guarded by mu.
|
||||
size int64
|
||||
sizeKnown bool
|
||||
}
|
||||
|
||||
// sourceSizeLocked resolves and caches the source length without
|
||||
// disturbing the current position. The entry mutex must be held.
|
||||
func (e *sourceEntry) sourceSizeLocked() (int64, error) {
|
||||
if e.sizeKnown {
|
||||
return e.size, nil
|
||||
}
|
||||
cur, err := e.seeker.Seek(0, io.SeekCurrent)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
size, err := e.seeker.Seek(0, io.SeekEnd)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if _, err := e.seeker.Seek(cur, io.SeekStart); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
e.size, e.sizeKnown = size, true
|
||||
return size, nil
|
||||
}
|
||||
|
||||
// targetEntry is the registry entry for one streaming save.
|
||||
type targetEntry struct {
|
||||
writer io.Writer
|
||||
mu sync.Mutex
|
||||
lastErr error
|
||||
}
|
||||
|
||||
func (e *sourceEntry) takeErr() error {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
err := e.lastErr
|
||||
e.lastErr = nil
|
||||
return err
|
||||
}
|
||||
|
||||
func (e *targetEntry) takeErr() error {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
err := e.lastErr
|
||||
e.lastErr = nil
|
||||
return err
|
||||
}
|
||||
|
||||
// streamCallbacks maps integer handles to active source/target entries.
|
||||
// The registry mutex only guards the maps; per-entry mutexes guard the
|
||||
// actual I/O so the global lock is never held during a Read/Write.
|
||||
var streamCallbacks = struct {
|
||||
sync.Mutex
|
||||
sources map[int]*sourceEntry
|
||||
targets map[int]*targetEntry
|
||||
nextHandle int
|
||||
}{
|
||||
sources: make(map[int]*sourceEntry),
|
||||
targets: make(map[int]*targetEntry),
|
||||
}
|
||||
|
||||
// allocStreamHandle returns the next free handle. Handles cross the CGo
|
||||
// boundary as C int (via GLib's GINT_TO_POINTER), so they must stay
|
||||
// within int32 range: wrap instead of overflowing, and skip any handle
|
||||
// that is still registered after a wrap. The caller must hold the
|
||||
// streamCallbacks lock.
|
||||
func allocStreamHandle() int {
|
||||
for {
|
||||
streamCallbacks.nextHandle++
|
||||
if streamCallbacks.nextHandle > math.MaxInt32 {
|
||||
streamCallbacks.nextHandle = 1
|
||||
}
|
||||
h := streamCallbacks.nextHandle
|
||||
if _, live := streamCallbacks.sources[h]; live {
|
||||
continue
|
||||
}
|
||||
if _, live := streamCallbacks.targets[h]; live {
|
||||
continue
|
||||
}
|
||||
return h
|
||||
}
|
||||
}
|
||||
|
||||
func registerSource(r io.Reader) (int, *sourceEntry) {
|
||||
entry := &sourceEntry{reader: r}
|
||||
if s, ok := r.(io.Seeker); ok {
|
||||
entry.seeker = s
|
||||
}
|
||||
|
||||
streamCallbacks.Lock()
|
||||
defer streamCallbacks.Unlock()
|
||||
handle := allocStreamHandle()
|
||||
streamCallbacks.sources[handle] = entry
|
||||
return handle, entry
|
||||
}
|
||||
|
||||
func deregisterSource(handle int) {
|
||||
streamCallbacks.Lock()
|
||||
defer streamCallbacks.Unlock()
|
||||
delete(streamCallbacks.sources, handle)
|
||||
}
|
||||
|
||||
func lookupSource(handle int) *sourceEntry {
|
||||
streamCallbacks.Lock()
|
||||
defer streamCallbacks.Unlock()
|
||||
return streamCallbacks.sources[handle]
|
||||
}
|
||||
|
||||
func registerTarget(w io.Writer) (int, *targetEntry) {
|
||||
entry := &targetEntry{writer: w}
|
||||
|
||||
streamCallbacks.Lock()
|
||||
defer streamCallbacks.Unlock()
|
||||
handle := allocStreamHandle()
|
||||
streamCallbacks.targets[handle] = entry
|
||||
return handle, entry
|
||||
}
|
||||
|
||||
func deregisterTarget(handle int) {
|
||||
streamCallbacks.Lock()
|
||||
defer streamCallbacks.Unlock()
|
||||
delete(streamCallbacks.targets, handle)
|
||||
}
|
||||
|
||||
func lookupTarget(handle int) *targetEntry {
|
||||
streamCallbacks.Lock()
|
||||
defer streamCallbacks.Unlock()
|
||||
return streamCallbacks.targets[handle]
|
||||
}
|
||||
|
||||
//export goSourceReadCb
|
||||
func goSourceReadCb(handle C.int, buffer unsafe.Pointer, length C.gint64) C.gint64 {
|
||||
if length <= 0 {
|
||||
return 0
|
||||
}
|
||||
buf := unsafe.Slice((*byte)(buffer), int(length))
|
||||
return C.gint64(sourceRead(int(handle), buf))
|
||||
}
|
||||
|
||||
//export goSourceSeekCb
|
||||
func goSourceSeekCb(handle C.int, offset C.gint64, whence C.int) C.gint64 {
|
||||
return C.gint64(sourceSeek(int(handle), int64(offset), int(whence)))
|
||||
}
|
||||
|
||||
//export goTargetWriteCb
|
||||
func goTargetWriteCb(handle C.int, data unsafe.Pointer, length C.gint64) C.gint64 {
|
||||
if length <= 0 {
|
||||
return 0
|
||||
}
|
||||
buf := unsafe.Slice((*byte)(data), int(length))
|
||||
return C.gint64(targetWrite(int(handle), buf))
|
||||
}
|
||||
|
||||
//export goTargetEndCb
|
||||
func goTargetEndCb(handle C.int) C.int {
|
||||
return C.int(targetEnd(int(handle)))
|
||||
}
|
||||
|
||||
func sourceRead(handle int, buf []byte) int64 {
|
||||
entry := lookupSource(handle)
|
||||
if entry == nil {
|
||||
return -1
|
||||
}
|
||||
|
||||
entry.mu.Lock()
|
||||
defer entry.mu.Unlock()
|
||||
|
||||
// (0, nil) is allowed by the io.Reader contract; retry rather than
|
||||
// returning 0, which libvips would treat as EOF. Bound the retries so
|
||||
// a pathological reader that returns (0, nil) forever fails the load
|
||||
// instead of pinning a libvips worker thread inside this callback.
|
||||
const maxZeroReads = 100
|
||||
for i := 0; i < maxZeroReads; i++ {
|
||||
n, err := entry.reader.Read(buf)
|
||||
if n > 0 {
|
||||
// A non-EOF error alongside n>0 will surface on the next call.
|
||||
return int64(n)
|
||||
}
|
||||
if errors.Is(err, io.EOF) {
|
||||
return 0
|
||||
}
|
||||
if err != nil {
|
||||
entry.lastErr = err
|
||||
return -1
|
||||
}
|
||||
}
|
||||
entry.lastErr = fmt.Errorf("reader returned (0, nil) %d times in a row", maxZeroReads)
|
||||
return -1
|
||||
}
|
||||
|
||||
func sourceSeek(handle int, offset int64, whence int) int64 {
|
||||
entry := lookupSource(handle)
|
||||
if entry == nil || entry.seeker == nil {
|
||||
return -1
|
||||
}
|
||||
|
||||
entry.mu.Lock()
|
||||
defer entry.mu.Unlock()
|
||||
|
||||
size, err := entry.sourceSizeLocked()
|
||||
if err != nil {
|
||||
entry.lastErr = err
|
||||
return -1
|
||||
}
|
||||
|
||||
// Resolve the absolute target ourselves. libvips whence values are
|
||||
// SEEK_SET/SEEK_CUR/SEEK_END, matching io.SeekStart/Current/End.
|
||||
var base int64
|
||||
switch whence {
|
||||
case io.SeekStart:
|
||||
base = 0
|
||||
case io.SeekCurrent:
|
||||
cur, err := entry.seeker.Seek(0, io.SeekCurrent)
|
||||
if err != nil {
|
||||
entry.lastErr = err
|
||||
return -1
|
||||
}
|
||||
base = cur
|
||||
case io.SeekEnd:
|
||||
base = size
|
||||
default:
|
||||
return -1
|
||||
}
|
||||
|
||||
target := base + offset
|
||||
if target < 0 {
|
||||
// POSIX lseek rejects negative positions (EINVAL) without
|
||||
// affecting the stream; treat it as a probe, not a stream
|
||||
// error, so it is not wrapped into the load error later.
|
||||
return -1
|
||||
}
|
||||
if target > size {
|
||||
// POSIX permits seeking beyond EOF, and codecs probe container
|
||||
// structures that way (libheif seeks past the final HEIC box).
|
||||
// Report the requested position: libvips itself range-checks it
|
||||
// against the source length and turns it into the failure the
|
||||
// heif glue's wait_for_file_size protocol relies on. Park the
|
||||
// underlying seeker at EOF so strict io.Seeker implementations
|
||||
// are never asked for an out-of-range position; any read then
|
||||
// returns 0 bytes. One divergence from POSIX: a subsequent
|
||||
// SEEK_CUR resolves from EOF, not from the reported past-EOF
|
||||
// position. That is fine in practice because libvips' codec glue
|
||||
// only ever follows a past-EOF probe with an absolute seek.
|
||||
if _, err := entry.seeker.Seek(size, io.SeekStart); err != nil {
|
||||
entry.lastErr = err
|
||||
return -1
|
||||
}
|
||||
return target
|
||||
}
|
||||
|
||||
pos, err := entry.seeker.Seek(target, io.SeekStart)
|
||||
if err != nil {
|
||||
entry.lastErr = err
|
||||
return -1
|
||||
}
|
||||
return pos
|
||||
}
|
||||
|
||||
func targetWrite(handle int, buf []byte) int64 {
|
||||
entry := lookupTarget(handle)
|
||||
if entry == nil {
|
||||
return -1
|
||||
}
|
||||
|
||||
entry.mu.Lock()
|
||||
defer entry.mu.Unlock()
|
||||
|
||||
n, err := entry.writer.Write(buf)
|
||||
if err != nil {
|
||||
entry.lastErr = err
|
||||
return -1
|
||||
}
|
||||
return int64(n)
|
||||
}
|
||||
|
||||
func targetEnd(handle int) int {
|
||||
entry := lookupTarget(handle)
|
||||
if entry == nil {
|
||||
return -1
|
||||
}
|
||||
|
||||
entry.mu.Lock()
|
||||
defer entry.mu.Unlock()
|
||||
|
||||
if entry.lastErr != nil {
|
||||
return -1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// streamSourceRef ties a live VipsSourceCustom (and its registry entry)
|
||||
// to a sequentially stream-loaded ImageRef. The image pulls pixels from
|
||||
// the source on demand, so both stay alive until the image is closed or
|
||||
// materialized.
|
||||
type streamSourceRef struct {
|
||||
handle int
|
||||
entry *sourceEntry
|
||||
source *C.VipsSourceCustom
|
||||
}
|
||||
|
||||
func (s *streamSourceRef) release() {
|
||||
C.clear_source(&s.source)
|
||||
deregisterSource(s.handle)
|
||||
}
|
||||
|
||||
// streamMaterialize holds the knobs for the default (random-access)
|
||||
// load path: decoded images at most threshold bytes are materialized in
|
||||
// memory, larger ones in a scratch file on disc.
|
||||
var streamMaterialize = struct {
|
||||
sync.RWMutex
|
||||
scratchDir string // "" → os.TempDir()
|
||||
threshold int64 // <0 → resolve from VIPS_DISC_THRESHOLD / default
|
||||
}{threshold: -1}
|
||||
|
||||
// defaultDiscThreshold mirrors the libvips default for
|
||||
// VIPS_DISC_THRESHOLD: decoded images above 100 MB go to disc.
|
||||
const defaultDiscThreshold = 100 << 20
|
||||
|
||||
// SetStreamScratchDir sets the directory used for scratch files when a
|
||||
// stream-loaded image is materialized to disc (see
|
||||
// SetStreamDiscThreshold). An empty string restores the default
|
||||
// (os.TempDir()). Scratch files are unlinked as soon as the decoded
|
||||
// image has been written and reopened, so on the happy path they never
|
||||
// outlive the ImageRef; a crash mid-materialization can leave one
|
||||
// behind in this directory.
|
||||
func SetStreamScratchDir(dir string) {
|
||||
streamMaterialize.Lock()
|
||||
defer streamMaterialize.Unlock()
|
||||
streamMaterialize.scratchDir = dir
|
||||
}
|
||||
|
||||
// SetStreamDiscThreshold sets the decoded-size threshold (in bytes)
|
||||
// above which LoadImageFromReader materializes images to a scratch file
|
||||
// on disc instead of memory. Zero sends every image to disc. A negative
|
||||
// value restores the default: the VIPS_DISC_THRESHOLD environment
|
||||
// variable (number with optional k/m/g suffix) or 100 MB.
|
||||
func SetStreamDiscThreshold(bytes int64) {
|
||||
streamMaterialize.Lock()
|
||||
defer streamMaterialize.Unlock()
|
||||
streamMaterialize.threshold = bytes
|
||||
}
|
||||
|
||||
func streamDiscThresholdBytes() int64 {
|
||||
streamMaterialize.RLock()
|
||||
t := streamMaterialize.threshold
|
||||
streamMaterialize.RUnlock()
|
||||
if t >= 0 {
|
||||
return t
|
||||
}
|
||||
if env := os.Getenv("VIPS_DISC_THRESHOLD"); env != "" {
|
||||
if v, ok := parseVipsSize(env); ok {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return defaultDiscThreshold
|
||||
}
|
||||
|
||||
func streamScratchDir() string {
|
||||
streamMaterialize.RLock()
|
||||
defer streamMaterialize.RUnlock()
|
||||
if streamMaterialize.scratchDir != "" {
|
||||
return streamMaterialize.scratchDir
|
||||
}
|
||||
return os.TempDir()
|
||||
}
|
||||
|
||||
// parseVipsSize parses libvips-style size strings: a number with an
|
||||
// optional k/m/g suffix (powers of 1024).
|
||||
func parseVipsSize(s string) (int64, bool) {
|
||||
s = strings.TrimSpace(strings.ToLower(s))
|
||||
mult := int64(1)
|
||||
switch {
|
||||
case strings.HasSuffix(s, "k"):
|
||||
mult, s = 1<<10, s[:len(s)-1]
|
||||
case strings.HasSuffix(s, "m"):
|
||||
mult, s = 1<<20, s[:len(s)-1]
|
||||
case strings.HasSuffix(s, "g"):
|
||||
mult, s = 1<<30, s[:len(s)-1]
|
||||
}
|
||||
v, err := strconv.ParseFloat(strings.TrimSpace(s), 64)
|
||||
if err != nil || v < 0 {
|
||||
return 0, false
|
||||
}
|
||||
return int64(v * float64(mult)), true
|
||||
}
|
||||
|
||||
// materializeImage renders the lazy image in to either memory or a
|
||||
// scratch file on disc, depending on its decoded size and the configured
|
||||
// threshold. The returned image no longer reads from the source.
|
||||
func materializeImage(in *C.VipsImage) (*C.VipsImage, error) {
|
||||
decoded := int64(C.image_decoded_size(in))
|
||||
|
||||
if decoded <= streamDiscThresholdBytes() {
|
||||
var out *C.VipsImage
|
||||
if C.copy_image_to_memory(in, &out) != 0 {
|
||||
return nil, handleVipsError()
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
f, err := os.CreateTemp(streamScratchDir(), "govips-scratch-*.v")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("streaming load: create scratch file: %w", err)
|
||||
}
|
||||
path := f.Name()
|
||||
_ = f.Close()
|
||||
|
||||
cPath := C.CString(path)
|
||||
defer C.free(unsafe.Pointer(cPath))
|
||||
|
||||
var out *C.VipsImage
|
||||
code := C.write_image_to_disc(in, cPath, &out)
|
||||
// Unlink immediately: the open file keeps the data alive until the
|
||||
// image is closed. (A crash during write_image_to_disc itself can
|
||||
// still leave the scratch file behind; see SetStreamScratchDir.)
|
||||
_ = os.Remove(path)
|
||||
if code != 0 {
|
||||
return nil, handleVipsError()
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// sequentialAccess reports whether params request sequential streaming.
|
||||
func sequentialAccess(params *ImportParams) bool {
|
||||
if !params.Access.IsSet() {
|
||||
return false
|
||||
}
|
||||
a := params.Access.Get()
|
||||
return a == AccessSequential || a == AccessSequentialUnbuffered
|
||||
}
|
||||
|
||||
// LoadImageFromReader loads an image from the given io.Reader using
|
||||
// libvips streaming (VipsSourceCustom). If r also implements io.Seeker,
|
||||
// seek is exposed to libvips for efficient random-access loading.
|
||||
// Otherwise libvips uses sequential mode with automatic header buffering
|
||||
// (see SetPipeReadLimit).
|
||||
//
|
||||
// By default the image is fully materialized before this function
|
||||
// returns — in memory when its decoded size is at most the disc
|
||||
// threshold, otherwise in an unlinked scratch file (see
|
||||
// SetStreamDiscThreshold and SetStreamScratchDir). The reader is
|
||||
// consumed during this call and is not retained; callers may close it
|
||||
// immediately. Decode errors (including truncated input) surface here.
|
||||
//
|
||||
// If params.Access is AccessSequential, the image is instead loaded
|
||||
// lazily: libvips pulls compressed bytes from r on demand while later
|
||||
// operations consume pixels, so peak memory stays bounded by libvips
|
||||
// line caches regardless of image size. The reader MUST stay open until
|
||||
// the ImageRef is closed (Close releases it). Only operations that read
|
||||
// pixels strictly top-to-bottom are valid on such images — see the
|
||||
// "Streaming" section of the README. Decode errors surface from the
|
||||
// operation that first consumes the pixels (typically SaveToWriter or
|
||||
// Export*), not from this function.
|
||||
//
|
||||
// In every mode the returned ImageRef has no buffer backing: the full
|
||||
// compressed input is never held in Go memory. (NewImageFromReader, by
|
||||
// contrast, slurps the whole reader into a buffer via io.ReadAll before
|
||||
// decoding; prefer this function when input size matters.)
|
||||
//
|
||||
// Error strictness: when params.FailOnError is set, streaming loads use
|
||||
// libvips' fail_on=truncated, which is slightly laxer than the
|
||||
// fail_on=error used by the buffer loaders — truncated input always
|
||||
// errors deterministically, but some recoverable decode warnings that
|
||||
// the buffer path would reject are tolerated.
|
||||
//
|
||||
// params may be nil for default import settings.
|
||||
func LoadImageFromReader(r io.Reader, params *ImportParams) (*ImageRef, error) {
|
||||
if r == nil {
|
||||
return nil, errors.New("reader is nil")
|
||||
}
|
||||
if err := startupIfNeeded(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if params == nil {
|
||||
params = NewImportParams()
|
||||
}
|
||||
|
||||
incOpCounter("load_source")
|
||||
|
||||
handle, entry := registerSource(r)
|
||||
|
||||
source := C.create_source_custom(C.int(handle), C.int(boolToInt(entry.seeker != nil)))
|
||||
if source == nil {
|
||||
deregisterSource(handle)
|
||||
return nil, handleVipsError()
|
||||
}
|
||||
|
||||
// Sniff the signature up front (buffered and rewound, not consumed):
|
||||
// the loader nickname alone cannot express sub-formats — AVIF loads
|
||||
// via heifload, BMP/PSD via magickload — but the buffer path reports
|
||||
// them via DetermineImageType, and the streaming path must match.
|
||||
var header [12]byte
|
||||
headerKnown := C.source_sniff_header(source, (*C.uchar)(unsafe.Pointer(&header[0])), C.int(len(header))) == 0
|
||||
|
||||
loadParams := createImportParams(ImageTypeUnknown, params)
|
||||
|
||||
if code := C.load_from_source(source, &loadParams); code != 0 {
|
||||
err := wrapStreamError("streaming load", handleImageError(loadParams.outputImage), entry.takeErr())
|
||||
C.clear_source(&source)
|
||||
deregisterSource(handle)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
lazy := loadParams.outputImage
|
||||
format := ImageType(loadParams.inputFormat)
|
||||
originalFormat := format
|
||||
if headerKnown && (format == ImageTypeHEIF || format == ImageTypeMagick) {
|
||||
if sniffed := DetermineImageType(header[:]); sniffed != ImageTypeUnknown {
|
||||
originalFormat = sniffed
|
||||
if !isNeedToChangeLoaderToMagick(sniffed) {
|
||||
format = sniffed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if sequentialAccess(params) {
|
||||
ref := newImageRef(lazy, format, originalFormat, nil)
|
||||
ref.streamSource = &streamSourceRef{handle: handle, entry: entry, source: source}
|
||||
govipsLog("govips", LogLevelDebug, fmt.Sprintf("created sequential imageRef %p from reader", ref))
|
||||
return ref, nil
|
||||
}
|
||||
|
||||
// Default path: materialize now so the source (and the caller's
|
||||
// reader) can be released before returning. Upstream resources
|
||||
// (file handles, HTTP connections) are freed early.
|
||||
out, err := materializeImage(lazy)
|
||||
clearImage(lazy)
|
||||
C.clear_source(&source)
|
||||
deregisterSource(handle)
|
||||
if err != nil {
|
||||
return nil, wrapStreamError("streaming load", err, entry.takeErr())
|
||||
}
|
||||
|
||||
ref := newImageRef(out, format, originalFormat, nil)
|
||||
govipsLog("govips", LogLevelDebug, fmt.Sprintf("created imageRef %p from reader", ref))
|
||||
return ref, nil
|
||||
}
|
||||
|
||||
// materialize converts a sequentially stream-loaded image into a
|
||||
// materialized one (memory or scratch disc, by threshold) and releases
|
||||
// its source, making random-access operations valid. It is a no-op for
|
||||
// images that are already materialized.
|
||||
func (r *ImageRef) materialize() error {
|
||||
r.lock.Lock()
|
||||
|
||||
if r.image == nil {
|
||||
r.lock.Unlock()
|
||||
return errors.New("attempt to materialize a closed ImageRef")
|
||||
}
|
||||
src := r.streamSource
|
||||
if src == nil {
|
||||
r.lock.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
out, err := materializeImage(r.image)
|
||||
if err != nil {
|
||||
r.lock.Unlock()
|
||||
return wrapStreamError("streaming load", err, src.entry.takeErr())
|
||||
}
|
||||
|
||||
clearImage(r.image)
|
||||
r.image = out
|
||||
r.streamSource = nil
|
||||
r.lock.Unlock()
|
||||
|
||||
src.release()
|
||||
return nil
|
||||
}
|
||||
|
||||
// SaveToWriter encodes the image in the specified format and writes the
|
||||
// encoded bytes directly to w using libvips streaming (VipsTargetCustom).
|
||||
// The writer receives chunks of encoded data as they are produced and is
|
||||
// not retained after SaveToWriter returns.
|
||||
//
|
||||
// Supported formats: ImageTypeJPEG, ImageTypePNG, ImageTypeWEBP,
|
||||
// ImageTypeHEIF, ImageTypeTIFF, ImageTypeGIF. Output is byte-identical to
|
||||
// the corresponding Export* method with equivalent parameters.
|
||||
//
|
||||
// TIFF is encoded in memory and written in a single chunk, because the
|
||||
// TIFF container requires seekable output; all other formats stream
|
||||
// encoded chunks to w as they are produced.
|
||||
//
|
||||
// params may be nil for the format's default export settings; the format
|
||||
// argument takes precedence over params.Format. For access to the full
|
||||
// set of format-specific options, use the typed variants
|
||||
// (SaveToWriterJpeg, SaveToWriterPng, SaveToWriterWebp, SaveToWriterTiff,
|
||||
// SaveToWriterHeif, SaveToWriterGif).
|
||||
func (r *ImageRef) SaveToWriter(w io.Writer, format ImageType, params *ExportParams) error {
|
||||
return r.saveToWriter(w, format, func(in *C.VipsImage) (C.struct_SaveParams, func(), error) {
|
||||
return streamSaveParams(in, format, params)
|
||||
})
|
||||
}
|
||||
|
||||
// SaveToWriterJpeg streams the image to w as JPEG with the full set of
|
||||
// JPEG export options. Behaves like SaveToWriter; params may be nil for
|
||||
// defaults. Output is byte-identical to ExportJpeg.
|
||||
func (r *ImageRef) SaveToWriterJpeg(w io.Writer, params *JpegExportParams) error {
|
||||
if params == nil {
|
||||
params = NewJpegExportParams()
|
||||
}
|
||||
p := *params
|
||||
return r.saveToWriter(w, ImageTypeJPEG, func(in *C.VipsImage) (C.struct_SaveParams, func(), error) {
|
||||
sp, err := newSaveParamsJPEG(in, p)
|
||||
return sp, func() {}, err
|
||||
})
|
||||
}
|
||||
|
||||
// SaveToWriterPng streams the image to w as PNG with the full set of PNG
|
||||
// export options (Palette, Dither, Bitdepth, Filter, ...). Behaves like
|
||||
// SaveToWriter; params may be nil for defaults. Output is byte-identical
|
||||
// to ExportPng.
|
||||
func (r *ImageRef) SaveToWriterPng(w io.Writer, params *PngExportParams) error {
|
||||
if params == nil {
|
||||
params = NewPngExportParams()
|
||||
}
|
||||
p := *params
|
||||
return r.saveToWriter(w, ImageTypePNG, func(in *C.VipsImage) (C.struct_SaveParams, func(), error) {
|
||||
sp, err := newSaveParamsPNG(in, p)
|
||||
return sp, func() {}, err
|
||||
})
|
||||
}
|
||||
|
||||
// SaveToWriterWebp streams the image to w as WebP with the full set of
|
||||
// WebP export options (NearLossless, IccProfile, TargetSize, ...).
|
||||
// Behaves like SaveToWriter; params may be nil for defaults. Output is
|
||||
// byte-identical to ExportWebp.
|
||||
func (r *ImageRef) SaveToWriterWebp(w io.Writer, params *WebpExportParams) error {
|
||||
if params == nil {
|
||||
params = NewWebpExportParams()
|
||||
}
|
||||
p := *params
|
||||
return r.saveToWriter(w, ImageTypeWEBP, func(in *C.VipsImage) (C.struct_SaveParams, func(), error) {
|
||||
return newSaveParamsWebP(in, p)
|
||||
})
|
||||
}
|
||||
|
||||
// SaveToWriterTiff writes the image to w as TIFF with the full set of
|
||||
// TIFF export options (Compression, Predictor, Tile, ...). Like the TIFF
|
||||
// path of SaveToWriter, the image is encoded in memory and written in a
|
||||
// single chunk (libtiff requires seekable output). params may be nil for
|
||||
// defaults. Output is byte-identical to ExportTiff.
|
||||
func (r *ImageRef) SaveToWriterTiff(w io.Writer, params *TiffExportParams) error {
|
||||
if params == nil {
|
||||
params = NewTiffExportParams()
|
||||
}
|
||||
p := *params
|
||||
return r.saveToWriter(w, ImageTypeTIFF, func(in *C.VipsImage) (C.struct_SaveParams, func(), error) {
|
||||
sp, err := newSaveParamsTIFF(in, p)
|
||||
return sp, func() {}, err
|
||||
})
|
||||
}
|
||||
|
||||
// SaveToWriterHeif streams the image to w as HEIF with the full set of
|
||||
// HEIF export options (Bitdepth, Effort, ...). Behaves like SaveToWriter;
|
||||
// params may be nil for defaults. Output is byte-identical to ExportHeif.
|
||||
func (r *ImageRef) SaveToWriterHeif(w io.Writer, params *HeifExportParams) error {
|
||||
if params == nil {
|
||||
params = NewHeifExportParams()
|
||||
}
|
||||
p := *params
|
||||
return r.saveToWriter(w, ImageTypeHEIF, func(in *C.VipsImage) (C.struct_SaveParams, func(), error) {
|
||||
sp, err := newSaveParamsHEIF(in, p)
|
||||
return sp, func() {}, err
|
||||
})
|
||||
}
|
||||
|
||||
// SaveToWriterGif streams the image to w as GIF with the full set of GIF
|
||||
// export options (Dither, Effort, Bitdepth, ...). Behaves like
|
||||
// SaveToWriter; params may be nil for defaults. Output is byte-identical
|
||||
// to ExportGIF.
|
||||
func (r *ImageRef) SaveToWriterGif(w io.Writer, params *GifExportParams) error {
|
||||
if params == nil {
|
||||
params = NewGifExportParams()
|
||||
}
|
||||
p := *params
|
||||
return r.saveToWriter(w, ImageTypeGIF, func(in *C.VipsImage) (C.struct_SaveParams, func(), error) {
|
||||
sp, err := newSaveParamsGIF(in, p)
|
||||
return sp, func() {}, err
|
||||
})
|
||||
}
|
||||
|
||||
// saveToWriter is the shared core of SaveToWriter and its typed
|
||||
// variants: it locks the image, builds the C save params via
|
||||
// buildParams, and runs the streaming (or, for TIFF, buffered) save.
|
||||
func (r *ImageRef) saveToWriter(w io.Writer, format ImageType, buildParams func(*C.VipsImage) (C.struct_SaveParams, func(), error)) error {
|
||||
if w == nil {
|
||||
return errors.New("writer is nil")
|
||||
}
|
||||
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
defer runtime.KeepAlive(r)
|
||||
|
||||
if r.image == nil {
|
||||
return errors.New("attempt to save a closed ImageRef")
|
||||
}
|
||||
|
||||
saveParams, cleanup, err := buildParams(r.image)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer cleanup()
|
||||
|
||||
incOpCounter("save_" + ImageTypes[format] + "_target")
|
||||
|
||||
if format == ImageTypeTIFF {
|
||||
// libtiff requires a seekable, readable output stream (it
|
||||
// rewrites IFD offsets after encoding), which a plain io.Writer
|
||||
// cannot provide. Encode through the buffer path and emit a
|
||||
// single write; the bytes are identical to ExportTiff.
|
||||
buf, err := vipsSaveToBuffer(saveParams)
|
||||
if err != nil {
|
||||
var ioErr error
|
||||
if r.streamSource != nil {
|
||||
// Sequential decode runs during this encode; surface
|
||||
// the reader's error like the streaming-target path.
|
||||
ioErr = r.streamSource.entry.takeErr()
|
||||
}
|
||||
return wrapStreamError("streaming save", err, ioErr)
|
||||
}
|
||||
if _, err := w.Write(buf); err != nil {
|
||||
return fmt.Errorf("streaming save: writer error: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
handle, entry := registerTarget(w)
|
||||
defer deregisterTarget(handle)
|
||||
|
||||
target := C.create_target_custom(C.int(handle))
|
||||
if target == nil {
|
||||
return handleVipsError()
|
||||
}
|
||||
defer C.clear_target(&target)
|
||||
|
||||
var code C.int
|
||||
switch format {
|
||||
case ImageTypeJPEG:
|
||||
code = C.save_jpeg_to_target(&saveParams, target)
|
||||
case ImageTypePNG:
|
||||
code = C.save_png_to_target(&saveParams, target)
|
||||
case ImageTypeWEBP:
|
||||
code = C.save_webp_to_target(&saveParams, target)
|
||||
case ImageTypeHEIF:
|
||||
code = C.save_heif_to_target(&saveParams, target)
|
||||
// ImageTypeTIFF is handled by the buffer-path early return above.
|
||||
case ImageTypeGIF:
|
||||
code = C.save_gif_to_target(&saveParams, target)
|
||||
}
|
||||
|
||||
if code != 0 {
|
||||
ioErr := entry.takeErr()
|
||||
if ioErr == nil && r.streamSource != nil {
|
||||
// For sequentially stream-loaded images the decode runs
|
||||
// during this save; surface the reader's error too.
|
||||
ioErr = r.streamSource.entry.takeErr()
|
||||
}
|
||||
return wrapStreamError("streaming save", handleVipsError(), ioErr)
|
||||
}
|
||||
if ioErr := entry.takeErr(); ioErr != nil {
|
||||
return fmt.Errorf("streaming save: writer error: %w", ioErr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// streamSaveParams builds the C save parameters for SaveToWriter using
|
||||
// the same ExportParams mapping as (*ImageRef).Export (via the shared
|
||||
// *ParamsFromExport helpers) and the same C-struct population as the
|
||||
// Export* buffer savers, so streaming output stays byte-identical to the
|
||||
// buffer path. The returned cleanup must be called after the save
|
||||
// completes.
|
||||
func streamSaveParams(in *C.VipsImage, format ImageType, params *ExportParams) (C.struct_SaveParams, func(), error) {
|
||||
noop := func() {}
|
||||
|
||||
switch format {
|
||||
case ImageTypeJPEG:
|
||||
p, err := newSaveParamsJPEG(in, *jpegParamsFromExport(params))
|
||||
return p, noop, err
|
||||
case ImageTypePNG:
|
||||
p, err := newSaveParamsPNG(in, *pngParamsFromExport(params))
|
||||
return p, noop, err
|
||||
case ImageTypeWEBP:
|
||||
return newSaveParamsWebP(in, *webpParamsFromExport(params))
|
||||
case ImageTypeHEIF:
|
||||
p, err := newSaveParamsHEIF(in, *heifParamsFromExport(params))
|
||||
return p, noop, err
|
||||
case ImageTypeTIFF:
|
||||
p, err := newSaveParamsTIFF(in, *tiffParamsFromExport(params))
|
||||
return p, noop, err
|
||||
case ImageTypeGIF:
|
||||
p, err := newSaveParamsGIF(in, *gifParamsFromExport(params))
|
||||
return p, noop, err
|
||||
default:
|
||||
return C.struct_SaveParams{}, noop, fmt.Errorf("streaming save does not support format %q", ImageTypes[format])
|
||||
}
|
||||
}
|
||||
|
||||
// wrapStreamError combines the libvips error with the original Go
|
||||
// reader/writer error, when one was stored during a callback.
|
||||
func wrapStreamError(op string, vipsErr, ioErr error) error {
|
||||
if ioErr != nil {
|
||||
return fmt.Errorf("%s: %w (caused by: %w)", op, vipsErr, ioErr)
|
||||
}
|
||||
return vipsErr
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
// Streaming I/O via VipsSourceCustom/VipsTargetCustom.
|
||||
// C declarations for the callback bridge between libvips worker threads
|
||||
// and Go io.Reader/io.Writer instances registered in stream.go.
|
||||
|
||||
#ifndef STREAM_H
|
||||
#define STREAM_H
|
||||
|
||||
#include "foreign.h"
|
||||
|
||||
// Exported Go callbacks, defined in stream.go via //export.
|
||||
// Called by the static C trampolines in stream.c. The handle identifies
|
||||
// an entry in the Go-side callback registry. The buffer/data pointers are
|
||||
// owned by libvips and are only valid for the duration of the call; Go
|
||||
// must not retain them.
|
||||
|
||||
// Reads up to length bytes into buffer. Returns bytes read, 0 on EOF,
|
||||
// -1 on error.
|
||||
extern gint64 goSourceReadCb(int handle, void *buffer, gint64 length);
|
||||
|
||||
// Seeks the underlying reader. Returns the new absolute offset, or -1 on
|
||||
// error or if the reader does not support seeking.
|
||||
extern gint64 goSourceSeekCb(int handle, gint64 offset, int whence);
|
||||
|
||||
// Writes length bytes from data. Returns bytes written, -1 on error.
|
||||
extern gint64 goTargetWriteCb(int handle, void *data, gint64 length);
|
||||
|
||||
// Signals end of output. Returns 0 on success, -1 on error.
|
||||
extern int goTargetEndCb(int handle);
|
||||
|
||||
// Creates a VipsSourceCustom with the "read" trampoline connected, and the
|
||||
// "seek" trampoline too when has_seek is non-zero. The handle is stored as
|
||||
// signal user_data. Returns a new reference owned by the caller; release
|
||||
// with clear_source. Returns NULL on failure.
|
||||
VipsSourceCustom *create_source_custom(int handle, int has_seek);
|
||||
|
||||
// Creates a VipsTargetCustom with the "write" and "end" trampolines
|
||||
// connected. The handle is stored as signal user_data. Returns a new
|
||||
// reference owned by the caller; release with clear_target. Returns NULL
|
||||
// on failure.
|
||||
VipsTargetCustom *create_target_custom(int handle);
|
||||
|
||||
// Detects the image format by sniffing the source, then loads it.
|
||||
// Borrows source (does not take ownership). On success returns 0, sets
|
||||
// params->inputFormat to the detected format, and stores a new VipsImage
|
||||
// reference in params->outputImage owned by the caller. The image is
|
||||
// LAZY: it pulls from the source on demand until materialized, so the
|
||||
// source must stay alive (and registered) until the image is either
|
||||
// materialized or closed. On failure returns non-zero with the error in
|
||||
// the vips error buffer.
|
||||
int load_from_source(VipsSourceCustom *source, LoadParams *params);
|
||||
|
||||
// Returns the decoded (uncompressed) pixel size of in, in bytes.
|
||||
gint64 image_decoded_size(VipsImage *in);
|
||||
|
||||
// Renders in into a memory image. Borrows in; on success stores a new
|
||||
// reference in *out owned by the caller. Returns non-zero on failure.
|
||||
int copy_image_to_memory(VipsImage *in, VipsImage **out);
|
||||
|
||||
// Renders in into the vips-native file at path (one sequential pass),
|
||||
// then reopens it read-only with random access. Borrows in; on success
|
||||
// stores a new reference in *out owned by the caller; the caller may
|
||||
// unlink path immediately (the open file keeps the data alive).
|
||||
// Returns non-zero on failure.
|
||||
int write_image_to_disc(VipsImage *in, const char *path, VipsImage **out);
|
||||
|
||||
// Encode params->inputImage to the target. Both the image and the target
|
||||
// are borrowed (the caller retains ownership). Return 0 on success,
|
||||
// non-zero on failure with the error in the vips error buffer.
|
||||
int save_jpeg_to_target(SaveParams *params, VipsTargetCustom *target);
|
||||
int save_png_to_target(SaveParams *params, VipsTargetCustom *target);
|
||||
int save_webp_to_target(SaveParams *params, VipsTargetCustom *target);
|
||||
int save_heif_to_target(SaveParams *params, VipsTargetCustom *target);
|
||||
int save_gif_to_target(SaveParams *params, VipsTargetCustom *target);
|
||||
|
||||
// Copies the first len bytes of the source into out without consuming
|
||||
// them (vips_source_sniff buffers and rewinds). Returns non-zero if the
|
||||
// stream is shorter than len or unreadable. Call before loading so the
|
||||
// sniff window is still rewindable for every source kind.
|
||||
int source_sniff_header(VipsSourceCustom *source, unsigned char *out, int len);
|
||||
|
||||
// Unrefs *source / *target and sets it to NULL. NULL-safe.
|
||||
void clear_source(VipsSourceCustom **source);
|
||||
void clear_target(VipsTargetCustom **target);
|
||||
|
||||
#endif // STREAM_H
|
||||
@@ -1,6 +0,0 @@
|
||||
package vips
|
||||
|
||||
// relative to "/vips/.."
|
||||
const (
|
||||
resources = "../resources/"
|
||||
)
|
||||
@@ -1,104 +0,0 @@
|
||||
package vips
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
// TranscodeOptions configures TranscodeStream.
|
||||
type TranscodeOptions struct {
|
||||
// Format is the output image type. ImageTypeUnknown keeps the
|
||||
// input format (which must be one of the streaming-save formats:
|
||||
// JPEG, PNG, WebP, HEIF, TIFF, GIF).
|
||||
Format ImageType
|
||||
|
||||
// ImportParams configures the load. The Access field is ignored:
|
||||
// TranscodeStream picks the access mode itself.
|
||||
ImportParams *ImportParams
|
||||
|
||||
// ExportParams configures the encode; nil uses the format's
|
||||
// defaults, exactly like SaveToWriter.
|
||||
ExportParams *ExportParams
|
||||
|
||||
// AutoRotate applies the EXIF orientation to the pixels and clears
|
||||
// the orientation tag. Orientations that need a non-sequential
|
||||
// transform (3-8: 180° and 90°/270° families) force the image to be
|
||||
// materialized first — see the package streaming documentation.
|
||||
AutoRotate bool
|
||||
}
|
||||
|
||||
// TranscodeStream decodes an image from r, optionally applies EXIF
|
||||
// auto-rotation, and encodes it to w in the requested format. Encoded
|
||||
// bytes are handed to w in chunks as they are produced and are
|
||||
// byte-identical to the corresponding Export* method with the same
|
||||
// parameters.
|
||||
//
|
||||
// The pipeline picks the cheapest decode strategy automatically:
|
||||
//
|
||||
// - Sequential fast path: when no random-access transform is needed
|
||||
// (AutoRotate false, or EXIF orientation is 1 or 2), pixels flow
|
||||
// from r to w in one pass. Peak memory is bounded by libvips line
|
||||
// caches, independent of both file size and pixel count.
|
||||
// - Materialized path: when AutoRotate requires a rotation, the
|
||||
// decoded image is first rendered to memory or to an unlinked
|
||||
// scratch file, depending on its decoded size and the configured
|
||||
// threshold (SetStreamDiscThreshold, SetStreamScratchDir).
|
||||
//
|
||||
// Reader/decode errors (including truncated input) surface from this
|
||||
// call: on the sequential path during the encode (the first full pass
|
||||
// over the pixels), on the materialized path during materialization.
|
||||
// In both cases the original reader/writer error is wrapped and
|
||||
// matchable with errors.Is.
|
||||
//
|
||||
// govips knows nothing about the destination: w is the chunk callback.
|
||||
// Compose on the consumer side with io.MultiWriter (hash while
|
||||
// writing), io.Pipe (bridge to reader-shaped sinks), or io.Copy.
|
||||
func TranscodeStream(r io.Reader, w io.Writer, opts *TranscodeOptions) error {
|
||||
if r == nil {
|
||||
return errors.New("transcode: reader is nil")
|
||||
}
|
||||
if w == nil {
|
||||
return errors.New("transcode: writer is nil")
|
||||
}
|
||||
if opts == nil {
|
||||
opts = &TranscodeOptions{}
|
||||
}
|
||||
|
||||
importParams := NewImportParams()
|
||||
if opts.ImportParams != nil {
|
||||
*importParams = *opts.ImportParams
|
||||
}
|
||||
importParams.Access.Set(AccessSequential)
|
||||
|
||||
img, err := LoadImageFromReader(r, importParams)
|
||||
if err != nil {
|
||||
return fmt.Errorf("transcode: %w", err)
|
||||
}
|
||||
defer img.Close()
|
||||
|
||||
if opts.AutoRotate {
|
||||
// Orientation is header metadata, available before any pixel is
|
||||
// decoded. Orientations 1 (none) and 2 (horizontal flip) are
|
||||
// sequential-safe; everything else transposes or reverses line
|
||||
// order and needs random access.
|
||||
if img.Orientation() >= 3 {
|
||||
if err := img.materialize(); err != nil {
|
||||
return fmt.Errorf("transcode: %w", err)
|
||||
}
|
||||
}
|
||||
if err := img.AutoRotate(); err != nil {
|
||||
return fmt.Errorf("transcode: autorotate: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
format := opts.Format
|
||||
if format == ImageTypeUnknown {
|
||||
format = img.Format()
|
||||
}
|
||||
|
||||
if err := img.SaveToWriter(w, format, opts.ExportParams); err != nil {
|
||||
return fmt.Errorf("transcode: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -361,9 +361,6 @@ github.com/cyphar/filepath-securejoin/internal/consts
|
||||
# github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
|
||||
## explicit
|
||||
github.com/davecgh/go-spew/spew
|
||||
# github.com/davidbyttow/govips/v2 v2.20.0
|
||||
## explicit; go 1.25.0
|
||||
github.com/davidbyttow/govips/v2/vips
|
||||
# github.com/deckarep/golang-set v1.8.0
|
||||
## explicit; go 1.17
|
||||
github.com/deckarep/golang-set
|
||||
|
||||