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 side effects import for image/jpeg png and gif to avoid image: unknown format #452

Merged
merged 1 commit into from
Nov 1, 2024

Conversation

BennyThink
Copy link
Contributor

Summary

The test code below demonstrates an issue where govips does not correctly handle JPEG images, resulting in the error: image: unknown format.

package main

import (
	"fmt"
	"github.com/davidbyttow/govips/v2/vips"
)

func main() {
	vips.Startup(nil)
	defer vips.Shutdown()
	image1, _ := vips.NewImageFromFile("test.jpg")
	ep := vips.NewDefaultJPEGExportParams()
	_, err := image1.ToImage(ep)
	fmt.Println(err)
}

Issue

The underlying cause is that govips does not automatically import the image/jpeg package as a side effect. As a result, JPEG and GIF formats are not recognized, whereas formats like PNG (which are handled via an import in foreign.go ) work correctly.

Solution

This PR addresses the issue by explicitly adding the necessary side effect imports for JPEG and GIF formats in image.go. With this change, govips will correctly handle JPEG and GIF images without requiring additional user intervention.

@tonimelisma
Copy link
Collaborator

Hey @BennyThink I'm a bit confused by this PR. I've not noticed the issue you are talking about. Could add some more color please?

@n0vad3v
Copy link
Contributor

n0vad3v commented Oct 29, 2024

Hi @tonimelisma , you can have a try on the example code provided by @BennyThink you'll notice the error:

2024/10/29 15:16:00 [VIPS.info] found /usr/lib64/vips-modules-8.15
2024/10/29 15:16:00 [VIPS.info] using configure-time prefix
2024/10/29 15:16:00 [VIPS.info] VIPS_PREFIX = /usr
2024/10/29 15:16:00 [VIPS.info] VIPS_LIBDIR = /usr/lib64
2024/10/29 15:16:00 [VIPS.info] prefix = /usr
2024/10/29 15:16:00 [VIPS.info] libdir = /usr/lib64
2024/10/29 15:16:00 [VIPS.info] searching "/usr/lib64/vips-modules-8.15"
2024/10/29 15:16:00 [VIPS.info] loading "/usr/lib64/vips-modules-8.15/vips-magick.so"
2024/10/29 15:16:00 [VIPS.info] loading "/usr/lib64/vips-modules-8.15/vips-poppler.so"
2024/10/29 15:16:00 [VIPS.info] loading "/usr/lib64/vips-modules-8.15/vips-jxl.so"
2024/10/29 15:16:00 [VIPS.info] loading "/usr/lib64/vips-modules-8.15/vips-heif.so"
2024/10/29 15:16:00 [VIPS.info] loading "/usr/lib64/vips-modules-8.15/vips-openslide.so"
2024/10/29 15:16:00 [VIPS.info] searching "/usr/lib64/vips-plugins-8.15"
2024/10/29 15:16:00 [VIPS.info] im_load_plugins: searching "/usr/lib64/vips-8.15"
2024/10/29 15:16:00 [VIPS.info] im_load_plugins: searching "/usr/lib64"
2024/10/29 15:16:00 [govips.info] vips 8.15.5 started with concurrency=1 cache_max_files=0 cache_max_mem=52428800 cache_max=100
2024/10/29 15:16:00 [govips.info] registered image type loader type=heif
2024/10/29 15:16:00 [govips.info] registered image type loader type=gif
2024/10/29 15:16:00 [govips.info] registered image type loader type=jpeg
2024/10/29 15:16:00 [govips.info] registered image type loader type=pdf
2024/10/29 15:16:00 [govips.info] registered image type loader type=svg
2024/10/29 15:16:00 [govips.info] registered image type loader type=tiff
2024/10/29 15:16:00 [govips.info] registered image type loader type=jp2k
2024/10/29 15:16:00 [govips.info] registered image type loader type=jxl
2024/10/29 15:16:00 [govips.info] registered image type loader type=magick
2024/10/29 15:16:00 [govips.info] registered image type loader type=png
2024/10/29 15:16:00 [govips.info] registered image type loader type=webp
2024/10/29 15:16:00 [govips.info] registered image type loader type=heif
2024/10/29 15:16:00 [VIPS.info] threadpool completed with 1 workers
2024/10/29 15:16:00 [VIPS.info] threadpool completed with 1 workers
image: unknown format

The problem here is that jpeg and gif packages are not imported, but png package is imported at https://github.com/davidbyttow/govips/blob/master/vips/foreign.go#L11

So in this situation, only PNG images will success on ToImage, to make jpeg and gif images work, we need to import those packages "as a side effect of initializing that format's package".

Reference:

Decoding any particular image format requires the prior registration of a decoder function.
https://pkg.go.dev/image

@tonimelisma tonimelisma merged commit 4188b98 into davidbyttow:master Nov 1, 2024
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants