davidbyttow / govips

A lightning fast image processing and resizing library for Go
MIT License
1.25k stars 196 forks source link

VipsJpeg: Invalid SOS parameters for sequential JPEG #348

Open pathouse opened 1 year ago

pathouse commented 1 year ago

i'm running into this error when trying to export a jpeg image

from what i can tell govips has no problem loading the jpeg but then it fails to export that same image

i put together a small example repo here that reproduces the problem: https://github.com/pathouse/jpeg-govips

package main

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

func main() {
    img, err := vips.NewImageFromFile("example.jpg")
    if err != nil {
        fmt.Println("error: ", err)
        os.Exit(1)
    }
    imgBytes, _, err := img.ExportJpeg(vips.NewJpegExportParams())
    if err != nil {
        fmt.Println("error: ", err)
        os.Exit(1)
    }
    ioutil.WriteFile("out.jpg", imgBytes, 0644)
}

some searching around the web suggests this is an issue w/ jpegs produced by samsung phones (https://github.com/lovell/sharp/issues/1578) and that the work around may be ignoring this error?

wondering how i can accomplish that in govips

if there's a code change required i'm happy to try to take that on if you're able to point me in the right direction

edit: full stack trace as well

2023/03/09 09:53:27 [VIPS.info] found /opt/homebrew/Cellar/vips/8.14.1_1/lib/vips-modules-8.14
2023/03/09 09:53:27 [VIPS.info] using configure-time prefix
2023/03/09 09:53:27 [VIPS.info] VIPS_PREFIX = /opt/homebrew/Cellar/vips/8.14.1_1
2023/03/09 09:53:27 [VIPS.info] VIPS_LIBDIR = /opt/homebrew/Cellar/vips/8.14.1_1/lib
2023/03/09 09:53:27 [VIPS.info] prefix = /opt/homebrew/Cellar/vips/8.14.1_1
2023/03/09 09:53:27 [VIPS.info] libdir = /opt/homebrew/Cellar/vips/8.14.1_1/lib
2023/03/09 09:53:27 [VIPS.info] searching "/opt/homebrew/Cellar/vips/8.14.1_1/lib/vips-modules-8.14"
2023/03/09 09:53:27 [VIPS.info] loading "/opt/homebrew/Cellar/vips/8.14.1_1/lib/vips-modules-8.14/vips-jxl.so"
2023/03/09 09:53:27 [VIPS.info] loading "/opt/homebrew/Cellar/vips/8.14.1_1/lib/vips-modules-8.14/vips-poppler.so"
2023/03/09 09:53:27 [VIPS.info] loading "/opt/homebrew/Cellar/vips/8.14.1_1/lib/vips-modules-8.14/vips-heif.so"
2023/03/09 09:53:27 [VIPS.info] loading "/opt/homebrew/Cellar/vips/8.14.1_1/lib/vips-modules-8.14/vips-openslide.so"
2023/03/09 09:53:27 [VIPS.info] loading "/opt/homebrew/Cellar/vips/8.14.1_1/lib/vips-modules-8.14/vips-magick.so"
2023/03/09 09:53:27 [VIPS.info] searching "/opt/homebrew/Cellar/vips/8.14.1_1/lib/vips-plugins-8.14"
2023/03/09 09:53:27 [govips.info] vips 8.14.1 started with concurrency=1 cache_max_files=0 cache_max_mem=52428800 cache_max=100
2023/03/09 09:53:27 [govips.info] registered image type loader type=pdf
2023/03/09 09:53:27 [govips.info] registered image type loader type=png
2023/03/09 09:53:27 [govips.info] registered image type loader type=svg
2023/03/09 09:53:27 [govips.info] registered image type loader type=heif
2023/03/09 09:53:27 [govips.info] registered image type loader type=heif
2023/03/09 09:53:27 [govips.info] registered image type loader type=jpeg
2023/03/09 09:53:27 [govips.info] registered image type loader type=magick
2023/03/09 09:53:27 [govips.info] registered image type loader type=webp
2023/03/09 09:53:27 [govips.info] registered image type loader type=jp2k
2023/03/09 09:53:27 [govips.info] registered image type loader type=gif
2023/03/09 09:53:27 [govips.info] registered image type loader type=tiff
2023/03/09 09:53:27 [VIPS.warning] error in tile 0 x 0
2023/03/09 09:53:27 [VIPS.warning] error in tile 0 x 8
error:  VipsJpeg: Invalid SOS parameters for sequential JPEG

Stack:
goroutine 1 [running]:
runtime/debug.Stack()
    /usr/local/go/src/runtime/debug/stack.go:24 +0x68
github.com/davidbyttow/govips/v2/vips.handleVipsError()
    /Users/pjm/go/pkg/mod/github.com/davidbyttow/govips/v2@v2.11.0/vips/error.go:38 +0x3c
github.com/davidbyttow/govips/v2/vips.handleSaveBufferError(0x140001adbe8?)
    /Users/pjm/go/pkg/mod/github.com/davidbyttow/govips/v2@v2.11.0/vips/error.go:31 +0x28
github.com/davidbyttow/govips/v2/vips.vipsSaveToBuffer({0x14a868000, 0x0, 0x1, 0x0, 0x0, 0x50, 0x1, 0x0, 0x0, 0x0, ...})
    /Users/pjm/go/pkg/mod/github.com/davidbyttow/govips/v2@v2.11.0/vips/foreign.go:440 +0x9c
github.com/davidbyttow/govips/v2/vips.vipsSaveJPEGToBuffer(0x14a868000, {0x0, 0x50, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0})
    /Users/pjm/go/pkg/mod/github.com/davidbyttow/govips/v2@v2.11.0/vips/foreign.go:335 +0x118
github.com/davidbyttow/govips/v2/vips.(*ImageRef).ExportJpeg(0x140000a23c0, 0xb?)
    /Users/pjm/go/pkg/mod/github.com/davidbyttow/govips/v2@v2.11.0/vips/image.go:845 +0x94
main.main()
    /Users/pjm/Code/Pathouse/jpeg-govips/main.go:16 +0xbc
n0vad3v commented 5 months ago

You can try FailOnError in vips.ImportParams, example code:

NumPages: intMinusOne, is used to make sure GIF images are loaded correctly

package main

import (
    "os"

    "github.com/davidbyttow/govips/v2/vips"
)

var (
    boolFalse   vips.BoolParameter
    intMinusOne vips.IntParameter
)

func main() {
    vips.LoggingSettings(nil, vips.LogLevelError)
    vips.Startup(nil)
    defer vips.Shutdown()
    boolFalse.Set(false)
    intMinusOne.Set(-1)

    img, err := vips.LoadImageFromFile("invalid.jpg", &vips.ImportParams{
        FailOnError: boolFalse,
        NumPages:    intMinusOne,
    })
    if err != nil {
        panic(err)
    }

    imageBytes, _, _ := img.ExportWebp(&vips.WebpExportParams{
        Quality: 75,
    })

    _ = os.WriteFile("input_webp.webp", imageBytes, 0644)

}
pathouse commented 5 months ago

ah thank you @n0vad3v this did indeed resolve the error when i updated my example repo 👍