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

sunshineplan / imgconv / 10827186307

12 Sep 2024 08:32AM UTC coverage: 60.885% (+1.3%) from 59.564%
10827186307

push

github

web-flow
Add Split function (#128)

* Split function

* Add comment and test

45 of 51 new or added lines in 1 file covered. (88.24%)

674 of 1107 relevant lines covered (60.89%)

51313.27 hits per line

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

88.24
/split.go
1
package imgconv
2

3
import (
4
        "errors"
5
        "image"
6
)
7

8
// SplitMode defines the mode in which the image will be split
9
type SplitMode int
10

11
const (
12
        // SplitHorizontalMode splits the image horizontally
13
        SplitHorizontalMode SplitMode = iota
14
        // SplitVerticalMode splits the image vertically
15
        SplitVerticalMode
16
)
17

18
func split(base image.Rectangle, n int, mode SplitMode) (rects []image.Rectangle) {
7✔
19
        var width, height int
7✔
20
        if mode == SplitHorizontalMode {
12✔
21
                width = base.Dx() / n
5✔
22
                height = base.Dy()
5✔
23
        } else {
7✔
24
                width = base.Dx()
2✔
25
                height = base.Dy() / n
2✔
26
        }
2✔
27
        if width == 0 || height == 0 {
9✔
28
                return
2✔
29
        }
2✔
30
        for i := range n {
31✔
31
                var r image.Rectangle
26✔
32
                if mode == SplitHorizontalMode {
44✔
33
                        r = image.Rect(
18✔
34
                                base.Min.X+width*i, base.Min.Y,
18✔
35
                                base.Min.X+width*(i+1), base.Min.Y+height,
18✔
36
                        )
18✔
37
                } else {
26✔
38
                        r = image.Rect(
8✔
39
                                base.Min.X, base.Min.Y+height*i,
8✔
40
                                base.Min.X+width, base.Min.Y+height*(i+1),
8✔
41
                        )
8✔
42
                }
8✔
43
                rects = append(rects, r)
26✔
44
        }
45
        return
5✔
46
}
47

48
// Split splits an image into n smaller images based on the specified split mode.
49
// If n is less than 1, or the image cannot be split, it returns an error.
50
func Split(base image.Image, n int, mode SplitMode) (imgs []image.Image, err error) {
5✔
51
        if n < 1 {
6✔
52
                return nil, errors.New("invalid number of parts: must be at least 1")
1✔
53
        }
1✔
54
        if img, ok := base.(interface {
4✔
55
                SubImage(image.Rectangle) image.Image
4✔
56
        }); ok {
7✔
57
                rects := split(base.Bounds(), n, mode)
3✔
58
                if len(rects) == 0 {
5✔
59
                        return nil, errors.New("failed to split the image: invalid dimensions or n is too large")
2✔
60
                }
2✔
61
                for _, rect := range rects {
11✔
62
                        imgs = append(imgs, img.SubImage(rect))
10✔
63
                }
10✔
64
        } else {
1✔
65
                return nil, errors.New("image type does not support SubImage extraction")
1✔
66
        }
1✔
67
        return
1✔
68
}
69

70
// SplitHorizontal splits an image into n parts horizontally.
NEW
71
func SplitHorizontal(base image.Image, n int) ([]image.Image, error) {
×
NEW
72
        return Split(base, n, SplitHorizontalMode)
×
NEW
73
}
×
74

75
// SplitVertical splits an image into n parts vertically.
NEW
76
func SplitVertical(base image.Image, n int) ([]image.Image, error) {
×
NEW
77
        return Split(base, n, SplitVerticalMode)
×
NEW
78
}
×
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

© 2026 Coveralls, Inc