Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for higher bit depth AVIF and HEIF images #322

Merged
merged 1 commit into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
File renamed without changes.
Binary file not shown.
Binary file added resources/tif-16bit.tif
Binary file not shown.
61 changes: 44 additions & 17 deletions vips/foreign.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,18 +294,6 @@ int set_webpsave_options(VipsOperation *operation, SaveParams *params) {
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 (!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-tiffsave-buffer
int set_tiffsave_options(VipsOperation *operation, SaveParams *params) {
int ret = vips_object_set(
Expand Down Expand Up @@ -348,10 +336,49 @@ int set_gifsave_options(VipsOperation *operation, SaveParams *params) {
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), "compression", VIPS_FOREIGN_HEIF_COMPRESSION_AV1,
"lossless", params->heifLossless, "speed", params->avifSpeed, NULL);
int ret = vips_object_set(VIPS_OBJECT(operation), "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);
Expand Down Expand Up @@ -494,7 +521,9 @@ static SaveParams defaultSaveParams = {
.webpReductionEffort = 4,
.webpIccProfile = NULL,

.heifBitdepth = 8,
.heifLossless = FALSE,
.heifEffort = 5,

.tiffCompression = VIPS_FOREIGN_TIFF_COMPRESSION_LZW,
.tiffPredictor = VIPS_FOREIGN_TIFF_PREDICTOR_HORIZONTAL,
Expand All @@ -505,8 +534,6 @@ static SaveParams defaultSaveParams = {
.tiffXRes = 1.0,
.tiffYRes = 1.0,

.avifSpeed = 5,

.jp2kLossless = FALSE,
.jp2kTileHeight = 512,
.jp2kTileWidth = 512};
Expand Down
13 changes: 11 additions & 2 deletions vips/foreign.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func isBMP(buf []byte) bool {
return bytes.HasPrefix(buf, bmpHeader)
}

//X'0000 000C 6A50 2020 0D0A 870A'
// 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
Expand Down Expand Up @@ -406,19 +406,28 @@ func vipsSaveHEIFToBuffer(in *C.VipsImage, params HeifExportParams) ([]byte, err
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)

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.outputFormat = C.AVIF
p.quality = C.int(params.Quality)
p.heifLossless = C.int(boolToInt(params.Lossless))
p.avifSpeed = C.int(params.Speed)
p.heifBitdepth = C.int(params.Bitdepth)
p.heifEffort = C.int(effort)

return vipsSaveToBuffer(p)
}
Expand Down
9 changes: 4 additions & 5 deletions vips/foreign.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ typedef struct SaveParams {
int webpReductionEffort;
char *webpIccProfile;

// HEIF
BOOL heifLossless;
// 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;
Expand All @@ -120,9 +122,6 @@ typedef struct SaveParams {
double tiffXRes;
double tiffYRes;

// AVIF
int avifSpeed;

// JPEG2000
BOOL jp2kLossless;
int jp2kTileWidth;
Expand Down
2 changes: 1 addition & 1 deletion vips/foreign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func Test_DetermineImageType__BMP(t *testing.T) {
func Test_DetermineImageType__AVIF(t *testing.T) {
Startup(&Config{})

buf, err := ioutil.ReadFile(resources + "avif.avif")
buf, err := ioutil.ReadFile(resources + "avif-8bit.avif")
assert.NoError(t, err)
assert.NotNil(t, buf)

Expand Down
41 changes: 25 additions & 16 deletions vips/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,20 +292,6 @@ func NewWebpExportParams() *WebpExportParams {
}
}

// HeifExportParams are options when exporting a HEIF to file or buffer
type HeifExportParams struct {
Quality int
Lossless bool
}

// NewHeifExportParams creates default values for an export of a HEIF image.
func NewHeifExportParams() *HeifExportParams {
return &HeifExportParams{
Quality: 80,
Lossless: false,
}
}

// TiffExportParams are options when exporting a TIFF to file or buffer
type TiffExportParams struct {
StripMetadata bool
Expand Down Expand Up @@ -340,20 +326,43 @@ func NewGifExportParams() *GifExportParams {
}
}

// HeifExportParams are options when exporting a HEIF to file or buffer
type HeifExportParams struct {
Quality int
Bitdepth int
Effort int
Lossless bool
}

// 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
Speed int

// DEPRECATED - Use Effort instead.
Speed int
}

// NewAvifExportParams creates default values for an export of an AVIF image.
func NewAvifExportParams() *AvifExportParams {
return &AvifExportParams{
Quality: 80,
Bitdepth: 8,
Effort: 5,
Lossless: false,
Speed: 5,
}
}

Expand Down
29 changes: 29 additions & 0 deletions vips/image_golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,29 @@ func TestImage_AutoRotate_6__heic_to_jpg(t *testing.T) {
)
}

func TestImage_Export_AVIF_8_Bit(t *testing.T) {
avifExportParams := NewAvifExportParams()
goldenTest(t, resources+"avif-8bit.avif",
func(img *ImageRef) error {
return nil
},
func(result *ImageRef) {
}, exportAvif(avifExportParams),
)
}

func TestImage_TIF_16_Bit_To_AVIF_12_Bit(t *testing.T) {
avifExportParams := NewAvifExportParams()
avifExportParams.Bitdepth = 12
goldenTest(t, resources+"tif-16bit.tif",
func(img *ImageRef) error {
return nil
},
func(result *ImageRef) {
}, exportAvif(avifExportParams),
)
}

func TestImage_Sharpen_24bit_Alpha(t *testing.T) {
goldenTest(t, resources+"png-24bit+alpha.png", func(img *ImageRef) error {
//usm_0.66_1.00_0.01
Expand Down Expand Up @@ -861,6 +884,12 @@ func exportJpeg(exportParams *JpegExportParams) func(img *ImageRef) ([]byte, *Im
}
}

func exportAvif(exportParams *AvifExportParams) func(img *ImageRef) ([]byte, *ImageMetadata, error) {
return func(img *ImageRef) ([]byte, *ImageMetadata, error) {
return img.ExportAvif(exportParams)
}
}

func exportPng(exportParams *PngExportParams) func(img *ImageRef) ([]byte, *ImageMetadata, error) {
return func(img *ImageRef) ([]byte, *ImageMetadata, error) {
return img.ExportPng(exportParams)
Expand Down
2 changes: 1 addition & 1 deletion vips/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ func TestImageRef_Linear_Fails(t *testing.T) {
func TestImageRef_AVIF(t *testing.T) {
Startup(nil)

raw, err := ioutil.ReadFile(resources + "avif.avif")
raw, err := ioutil.ReadFile(resources + "avif-8bit.avif")
require.NoError(t, err)

img, err := NewImageFromBuffer(raw)
Expand Down