File: imageutil.go

package info (click to toggle)
golang-golang-x-exp 0.0~git20230522.2e198f4-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 6,404 kB
  • sloc: ansic: 1,900; objc: 276; sh: 272; asm: 48; makefile: 26
file content (101 lines) | stat: -rw-r--r-- 1,912 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package imageutil implements some image utility functions.
package imageutil

import (
	"image"
)

// TODO: move Border into the standard library's package image?

// Border returns four rectangles that together contain those points between r
// and r.Inset(inset). Visually:
//
//	00000000
//	00000000
//	11....22
//	11....22
//	11....22
//	33333333
//	33333333
//
// The inset may be negative, in which case the points will be outside r.
//
// Some of the returned rectangles may be empty. None of the returned
// rectangles will overlap.
func Border(r image.Rectangle, inset int) [4]image.Rectangle {
	if inset == 0 {
		return [4]image.Rectangle{}
	}
	if r.Dx() <= 2*inset || r.Dy() <= 2*inset {
		return [4]image.Rectangle{r}
	}

	x := [4]int{
		r.Min.X,
		r.Min.X + inset,
		r.Max.X - inset,
		r.Max.X,
	}
	y := [4]int{
		r.Min.Y,
		r.Min.Y + inset,
		r.Max.Y - inset,
		r.Max.Y,
	}
	if inset < 0 {
		x[0], x[1] = x[1], x[0]
		x[2], x[3] = x[3], x[2]
		y[0], y[1] = y[1], y[0]
		y[2], y[3] = y[3], y[2]
	}

	// The top and bottom sections are responsible for filling the corners.
	// The top and bottom sections go from x[0] to x[3], across the y's.
	// The left and right sections go from y[1] to y[2], across the x's.

	return [4]image.Rectangle{{
		// Top section.
		Min: image.Point{
			X: x[0],
			Y: y[0],
		},
		Max: image.Point{
			X: x[3],
			Y: y[1],
		},
	}, {
		// Left section.
		Min: image.Point{
			X: x[0],
			Y: y[1],
		},
		Max: image.Point{
			X: x[1],
			Y: y[2],
		},
	}, {
		// Right section.
		Min: image.Point{
			X: x[2],
			Y: y[1],
		},
		Max: image.Point{
			X: x[3],
			Y: y[2],
		},
	}, {
		// Bottom section.
		Min: image.Point{
			X: x[0],
			Y: y[2],
		},
		Max: image.Point{
			X: x[3],
			Y: y[3],
		},
	}}
}