Mouse wheel, trackpad and two-finger pinch zoom in the album viewer, up to one image pixel per device pixel; drag pans while zoomed and only the visible tiles of the needed level are loaded. Default WebP quality raised to top 90, mid 88, low 88, with smart_subsample for WebP tiles and the seed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
135 lines
3.1 KiB
Go
135 lines
3.1 KiB
Go
package process
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/binary"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type Vips struct {
|
|
Version string
|
|
Major int
|
|
Minor int
|
|
Strip string
|
|
env []string
|
|
nice string
|
|
}
|
|
|
|
const niceIncrement = "5"
|
|
|
|
var versionPattern = regexp.MustCompile(`vips-(\d+)\.(\d+)(\.\d+)?`)
|
|
|
|
func DetectVips(ctx context.Context, env []string) (*Vips, error) {
|
|
path, err := exec.LookPath("vips")
|
|
if err != nil {
|
|
return nil, errors.New("vips not found in PATH; install libvips-tools")
|
|
}
|
|
cmd := exec.CommandContext(ctx, path, "--version")
|
|
cmd.Env = env
|
|
out, err := cmd.Output()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("vips --version: %w", err)
|
|
}
|
|
m := versionPattern.FindStringSubmatch(string(out))
|
|
if m == nil {
|
|
return nil, fmt.Errorf("vips --version: unrecognised output %q", strings.TrimSpace(string(out)))
|
|
}
|
|
major, _ := strconv.Atoi(m[1])
|
|
minor, _ := strconv.Atoi(m[2])
|
|
v := &Vips{Version: strings.TrimPrefix(m[0], "vips-"), Major: major, Minor: minor, env: env}
|
|
if nice, err := exec.LookPath("nice"); err == nil {
|
|
v.nice = nice
|
|
}
|
|
if major > 8 || (major == 8 && minor >= 15) {
|
|
v.Strip = "keep=none"
|
|
} else {
|
|
v.Strip = "strip"
|
|
}
|
|
if major < 8 || (major == 8 && minor < 12) {
|
|
return nil, fmt.Errorf("libvips %s is too old; 8.12 or newer is required", v.Version)
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
type limitedBuffer struct {
|
|
buf bytes.Buffer
|
|
limit int
|
|
}
|
|
|
|
func (l *limitedBuffer) Write(p []byte) (int, error) {
|
|
if room := l.limit - l.buf.Len(); room > 0 {
|
|
if len(p) > room {
|
|
l.buf.Write(p[:room])
|
|
} else {
|
|
l.buf.Write(p)
|
|
}
|
|
}
|
|
return len(p), nil
|
|
}
|
|
|
|
func (v *Vips) Run(ctx context.Context, args ...string) error {
|
|
var cmd *exec.Cmd
|
|
if v.nice != "" {
|
|
cmd = exec.CommandContext(ctx, v.nice, append([]string{"-n", niceIncrement, "vips"}, args...)...)
|
|
} else {
|
|
cmd = exec.CommandContext(ctx, "vips", args...)
|
|
}
|
|
cmd.Env = v.env
|
|
stderr := &limitedBuffer{limit: 4096}
|
|
cmd.Stderr = stderr
|
|
cmd.Stdout = io.Discard
|
|
if err := cmd.Run(); err != nil {
|
|
if ctx.Err() != nil {
|
|
return ctx.Err()
|
|
}
|
|
msg := strings.TrimSpace(stderr.buf.String())
|
|
if msg == "" {
|
|
msg = err.Error()
|
|
}
|
|
return fmt.Errorf("vips %s: %s", args[0], msg)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (v *Vips) SaveOptions(format string, quality int) string {
|
|
if format == "webp" {
|
|
return "[Q=" + strconv.Itoa(quality) + ",smart_subsample=true," + v.Strip + "]"
|
|
}
|
|
return "[Q=" + strconv.Itoa(quality) + "," + v.Strip + "]"
|
|
}
|
|
|
|
var (
|
|
vipsMagicIntel = []byte{0xb6, 0xa6, 0xf2, 0x08}
|
|
vipsMagicSparc = []byte{0x08, 0xf2, 0xa6, 0xb6}
|
|
)
|
|
|
|
func VSize(path string) (int, int, error) {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return 0, 0, err
|
|
}
|
|
defer f.Close()
|
|
var header [12]byte
|
|
if _, err := io.ReadFull(f, header[:]); err != nil {
|
|
return 0, 0, fmt.Errorf("%s: %w", path, err)
|
|
}
|
|
var order binary.ByteOrder
|
|
switch {
|
|
case bytes.Equal(header[:4], vipsMagicIntel):
|
|
order = binary.LittleEndian
|
|
case bytes.Equal(header[:4], vipsMagicSparc):
|
|
order = binary.BigEndian
|
|
default:
|
|
return 0, 0, fmt.Errorf("%s: not a vips image", path)
|
|
}
|
|
return int(int32(order.Uint32(header[4:8]))), int(int32(order.Uint32(header[8:12]))), nil
|
|
}
|