• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cshum / imagor / 15317608139

29 May 2025 06:15AM UTC coverage: 92.443% (+0.03%) from 92.412%
15317608139

Pull #549

github

cshum
FallbackFuncs
Pull Request #549: feat(vips): buffer and BMP fallback

59 of 71 new or added lines in 3 files covered. (83.1%)

1 existing line in 1 file now uncovered.

4881 of 5280 relevant lines covered (92.44%)

1.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.73
/processor/vipsprocessor/fallback.go
1
package vipsprocessor
2

3
import (
4
        "github.com/cshum/imagor"
5
        "github.com/cshum/vipsgen/vips"
6
        "golang.org/x/image/bmp"
7
        "image"
8
        "image/draw"
9
        "io"
10
)
11

12
// FallbackFunc vips.Image fallback handler when vips.NewImageFromSource failed
13
type FallbackFunc func(blob *imagor.Blob, options *vips.LoadOptions) (*vips.Image, error)
14

15
// BufferFallbackFunc load image from buffer FallbackFunc
16
func BufferFallbackFunc(blob *imagor.Blob, options *vips.LoadOptions) (*vips.Image, error) {
1✔
17
        buf, err := blob.ReadAll()
1✔
18
        if err != nil {
1✔
NEW
19
                return nil, err
×
NEW
20
        }
×
21
        return vips.NewImageFromBuffer(buf, options)
1✔
22
}
23

24
func loadImageFromBMP(r io.Reader) (*vips.Image, error) {
1✔
25
        img, err := bmp.Decode(r)
1✔
26
        if err != nil {
1✔
NEW
27
                return nil, err
×
NEW
28
        }
×
29
        rect := img.Bounds()
1✔
30
        size := rect.Size()
1✔
31
        rgba, ok := img.(*image.RGBA)
1✔
32
        if !ok {
2✔
33
                rgba = image.NewRGBA(rect)
1✔
34
                draw.Draw(rgba, rect, img, rect.Min, draw.Src)
1✔
35
        }
1✔
36
        return vips.NewImageFromMemory(rgba.Pix, size.X, size.Y, 4)
1✔
37
}
38
func BmpFallbackFunc(blob *imagor.Blob, _ *vips.LoadOptions) (*vips.Image, error) {
1✔
39
        if blob.BlobType() == imagor.BlobTypeBMP {
2✔
40
                // fallback with Go BMP decoder if vips error on BMP
1✔
41
                r, _, err := blob.NewReader()
1✔
42
                if err != nil {
1✔
NEW
43
                        return nil, err
×
NEW
44
                }
×
45
                defer func() {
2✔
46
                        _ = r.Close()
1✔
47
                }()
1✔
48
                return loadImageFromBMP(r)
1✔
NEW
49
        } else {
×
NEW
50
                return nil, imagor.ErrUnsupportedFormat
×
NEW
51
        }
×
52
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc