Skip to content

Commit 152a6b1

Browse files
authored
Merge pull request #356 from LarsFronius/avif-support
Adds AVIF support
2 parents 4f683f9 + 7138810 commit 152a6b1

File tree

13 files changed

+111
-12
lines changed

13 files changed

+111
-12
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ env:
1111
# - LIBVIPS=8.7.4
1212
# - LIBVIPS=8.8.4
1313
# - LIBVIPS=8.9.2
14-
- LIBVIPS=8.10.0
14+
- LIBVIPS=8.10.1
15+
- LIBVIPS=8.10.2
1516

1617
matrix:
1718
allow_failures:
@@ -26,7 +27,7 @@ install:
2627
- docker build -t h2non/bimg:ci --build-arg LIBVIPS_VERSION=$LIBVIPS .
2728

2829
script:
29-
- docker run h2non/bimg:ci sh -c 'export LD_LIBRARY_PATH=/vips/lib:$LD_LIBRARY_PATH; export PKG_CONFIG_PATH=/vips/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig:/usr/X11/lib/pkgconfig; go vet . && golint . && go test -v -race -covermode=atomic -coverprofile=coverage.out'
30+
- docker run h2non/bimg:ci sh -c 'export LD_LIBRARY_PATH=/vips/lib:/usr/local/lib:$LD_LIBRARY_PATH; export PKG_CONFIG_PATH=/vips/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig:/usr/X11/lib/pkgconfig; go vet . && golint . && go test -v -race -covermode=atomic -coverprofile=coverage.out'
3031

3132
# after_success:
3233
# - goveralls -coverprofile=coverage.out -service=travis-ci

Dockerfile

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ FROM golang:1.14
22
LABEL maintainer "[email protected]"
33

44
ARG LIBVIPS_VERSION=8.9.2
5+
ARG LIBHEIF_VERSION=1.9.1
56
ARG GOLANGCILINT_VERSION=1.29.0
67

78
# Installs libvips + required libraries
@@ -13,7 +14,18 @@ RUN DEBIAN_FRONTEND=noninteractive \
1314
gobject-introspection gtk-doc-tools libglib2.0-dev libjpeg62-turbo-dev libpng-dev \
1415
libwebp-dev libtiff5-dev libgif-dev libexif-dev libxml2-dev libpoppler-glib-dev \
1516
swig libmagickwand-dev libpango1.0-dev libmatio-dev libopenslide-dev libcfitsio-dev \
16-
libgsf-1-dev fftw3-dev liborc-0.4-dev librsvg2-dev libimagequant-dev libheif-dev && \
17+
libgsf-1-dev fftw3-dev liborc-0.4-dev librsvg2-dev libimagequant-dev libaom-dev && \
18+
cd /tmp && \
19+
curl -fsSLO https://github.com/strukturag/libheif/releases/download/v${LIBHEIF_VERSION}/libheif-${LIBHEIF_VERSION}.tar.gz && \
20+
tar zvxf libheif-${LIBHEIF_VERSION}.tar.gz && \
21+
cd /tmp/libheif-${LIBHEIF_VERSION} && \
22+
./configure --prefix=/vips && \
23+
make && \
24+
make install && \
25+
echo '/vips/lib' > /etc/ld.so.conf.d/vips.conf && \
26+
ldconfig -v && \
27+
export LD_LIBRARY_PATH="/vips/lib:$LD_LIBRARY_PATH" && \
28+
export PKG_CONFIG_PATH="/vips/lib/pkgconfig:$PKG_CONFIG_PATH" && \
1729
cd /tmp && \
1830
curl -fsSLO https://github.com/libvips/libvips/releases/download/v${LIBVIPS_VERSION}/vips-${LIBVIPS_VERSION}.tar.gz && \
1931
tar zvxf vips-${LIBVIPS_VERSION}.tar.gz && \
@@ -32,6 +44,8 @@ RUN DEBIAN_FRONTEND=noninteractive \
3244
make install && \
3345
ldconfig
3446

47+
ENV LD_LIBRARY_PATH="/usr/local/lib:$LD_LIBRARY_PATH"
48+
3549
# Install runtime dependencies
3650
# RUN DEBIAN_FRONTEND=noninteractive \
3751
# apt-get update && \
@@ -62,4 +76,4 @@ COPY . .
6276
# apt-get clean && \
6377
# rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
6478

65-
CMD [ "/bin/bash" ]
79+
CMD [ "/bin/bash" ]

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Small [Go](http://golang.org) package for fast high-level image processing using [libvips](https://github.com/jcupitt/libvips) via C bindings, providing a simple [programmatic API](#examples).
44

5-
bimg was designed to be a small and efficient library supporting common [image operations](#supported-image-operations) such as crop, resize, rotate, zoom or watermark. It can read JPEG, PNG, WEBP natively, and optionally TIFF, PDF, GIF and SVG formats if `[email protected]+` is compiled with proper library bindings.
5+
bimg was designed to be a small and efficient library supporting common [image operations](#supported-image-operations) such as crop, resize, rotate, zoom or watermark. It can read JPEG, PNG, WEBP natively, and optionally TIFF, PDF, GIF and SVG formats if `[email protected]+` is compiled with proper library bindings. Lastly AVIF is supported as of `[email protected]+`. For AVIF support `libheif` needs to be [compiled with an applicable AVIF en-/decoder](https://github.com/strukturag/libheif#compiling).
66

77
bimg is able to output images as JPEG, PNG and WEBP formats, including transparent conversion across them.
88

@@ -53,7 +53,9 @@ If you're using `gopkg.in`, you can still rely in the `v0` without worrying abou
5353
- C compatible compiler such as gcc 4.6+ or clang 3.0+
5454
- Go 1.3+
5555

56-
**Note**: `libvips` v8.3+ is required for GIF, PDF and SVG support.
56+
**Note**:
57+
* `libvips` v8.3+ is required for GIF, PDF and SVG support.
58+
* `libvips` v8.9+ is required for AVIF support. `libheif` compiled with a AVIF en-/decoder also needs to be present.
5759

5860
## Installation
5961

image_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,8 @@ func TestImageAutoRotate(t *testing.T) {
352352
}
353353

354354
tests := []struct {
355-
file string
356-
orientation int
355+
file string
356+
orientation int
357357
}{
358358
{"exif/Landscape_1.jpg", 1},
359359
{"exif/Landscape_2.jpg", 1},

metadata_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func TestMetadata(t *testing.T) {
4242
{"test_icc_prophoto.jpg", "jpeg", 0, false, true, "srgb"},
4343
{"test.png", "png", 0, true, false, "srgb"},
4444
{"test.webp", "webp", 0, false, false, "srgb"},
45+
{"test.avif", "avif", 0, false, false, "srgb"},
4546
}
4647

4748
for _, file := range files {

options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ type Options struct {
226226
OutputICC string
227227
InputICC string
228228
Palette bool
229+
// Speed defines the AVIF encoders CPU effort. Valid values are 0-8.
230+
Speed int
229231

230232
// private fields
231233
autoRotateOnly bool

resizer.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func resizer(buf []byte, o Options) ([]byte, error) {
3131
o = applyDefaults(o, imageType)
3232

3333
// Ensure supported type
34-
if !IsTypeSupported(o.Type) {
34+
if !IsTypeSupportedSave(o.Type) {
3535
return nil, errors.New("Unsupported image output type")
3636
}
3737

@@ -51,7 +51,7 @@ func resizer(buf []byte, o Options) ([]byte, error) {
5151
}
5252

5353
// If JPEG or HEIF image, retrieve the buffer
54-
if rotated && (imageType == JPEG || imageType == HEIF) && !o.NoAutoRotate {
54+
if rotated && (imageType == JPEG || imageType == HEIF || imageType == AVIF) && !o.NoAutoRotate {
5555
buf, err = getImageBuffer(image)
5656
if err != nil {
5757
return nil, err
@@ -187,6 +187,7 @@ func saveImage(image *C.VipsImage, o Options) ([]byte, error) {
187187
StripMetadata: o.StripMetadata,
188188
Lossless: o.Lossless,
189189
Palette: o.Palette,
190+
Speed: o.Speed,
190191
}
191192
// Finally get the resultant buffer
192193
return vipsSave(image, saveOptions)

testdata/test.avif

253 KB
Binary file not shown.

type.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ const (
3030
MAGICK
3131
// HEIF represents the HEIC/HEIF/HVEC image type
3232
HEIF
33+
// AVIF represents the AVIF image type.
34+
AVIF
3335
)
3436

3537
var (
@@ -48,6 +50,7 @@ var ImageTypes = map[ImageType]string{
4850
SVG: "svg",
4951
MAGICK: "magick",
5052
HEIF: "heif",
53+
AVIF: "avif",
5154
}
5255

5356
// imageMutex is used to provide thread-safe synchronization

type_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func TestDeterminateImageType(t *testing.T) {
2222
{"test.heic", HEIF},
2323
{"test2.heic", HEIF},
2424
{"test3.heic", HEIF},
25+
{"test.avif", AVIF},
2526
}
2627

2728
for _, file := range files {
@@ -51,12 +52,16 @@ func TestDeterminateImageTypeName(t *testing.T) {
5152
{"test.svg", "svg"},
5253
// {"test.jp2", "magick"},
5354
{"test.heic", "heif"},
55+
{"test.avif", "avif"},
5456
}
5557

5658
for _, file := range files {
5759
if file.expected == "heif" && VipsMajorVersion <= 8 && VipsMinorVersion < 8 {
5860
continue
5961
}
62+
if file.expected == "avif" && VipsMajorVersion <= 8 && VipsMinorVersion < 9 {
63+
continue
64+
}
6065

6166
img, _ := os.Open(path.Join("testdata", file.name))
6267
buf, _ := ioutil.ReadAll(img)
@@ -73,13 +78,16 @@ func TestIsTypeSupported(t *testing.T) {
7378
types := []struct {
7479
name ImageType
7580
}{
76-
{JPEG}, {PNG}, {WEBP}, {GIF}, {PDF}, {HEIF},
81+
{JPEG}, {PNG}, {WEBP}, {GIF}, {PDF}, {HEIF}, {AVIF},
7782
}
7883

7984
for _, n := range types {
8085
if n.name == HEIF && VipsMajorVersion <= 8 && VipsMinorVersion < 8 {
8186
continue
8287
}
88+
if n.name == AVIF && VipsMajorVersion <= 8 && VipsMinorVersion < 9 {
89+
continue
90+
}
8391
if IsTypeSupported(n.name) == false {
8492
t.Fatalf("Image type %s is not valid", ImageTypes[n.name])
8593
}
@@ -97,12 +105,16 @@ func TestIsTypeNameSupported(t *testing.T) {
97105
{"gif", true},
98106
{"pdf", true},
99107
{"heif", true},
108+
{"avif", true},
100109
}
101110

102111
for _, n := range types {
103112
if n.name == "heif" && VipsMajorVersion <= 8 && VipsMinorVersion < 8 {
104113
continue
105114
}
115+
if n.name == "avif" && VipsMajorVersion <= 8 && VipsMinorVersion < 9 {
116+
continue
117+
}
106118
if IsTypeNameSupported(n.name) != n.expected {
107119
t.Fatalf("Image type %s is not valid", n.name)
108120
}
@@ -121,6 +133,9 @@ func TestIsTypeSupportedSave(t *testing.T) {
121133
if VipsVersion >= "8.8.0" {
122134
types = append(types, struct{ name ImageType }{HEIF})
123135
}
136+
if VipsVersion >= "8.9.0" {
137+
types = append(types, struct{ name ImageType }{AVIF})
138+
}
124139

125140
for _, n := range types {
126141
if IsTypeSupportedSave(n.name) == false {
@@ -141,6 +156,7 @@ func TestIsTypeNameSupportedSave(t *testing.T) {
141156
{"pdf", false},
142157
{"tiff", VipsVersion >= "8.5.0"},
143158
{"heif", VipsVersion >= "8.8.0"},
159+
{"avif", VipsVersion >= "8.9.0"},
144160
}
145161

146162
for _, n := range types {

0 commit comments

Comments
 (0)