This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce the overhead caused by using a huge amount of goroutines to compute histograms. The concurrent algorithms will now divide the `Rectangle` that makes up the image in up to `runtime.NumProc()` parts to be analyzed concurrently. This gives slight speed improvements and decreases memory consumption.
- Loading branch information
Showing
5 changed files
with
212 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2019 Alessandro Pomponio. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package histogram | ||
|
||
import ( | ||
"image" | ||
) | ||
|
||
// splitInto splits a rectangle in up to amount parts. | ||
func splitInto(amount int, rectangle image.Rectangle) []image.Rectangle { | ||
|
||
// Avoid infinite recursion in case amount ends up being odd | ||
if amount < 4 { | ||
return split(rectangle) | ||
} | ||
|
||
// Check if it's worth to stop the recursion | ||
xBound := rectangle.Dx() | ||
yBound := rectangle.Dy() | ||
if xBound < 400 && yBound < 400 { | ||
return split(rectangle) | ||
} | ||
|
||
rects := split(rectangle) | ||
return append(splitInto(amount/2, rects[0]), splitInto(amount/2, rects[1])...) | ||
|
||
} | ||
|
||
// split splits a Rectangle in two, horizontally. | ||
func split(r image.Rectangle) []image.Rectangle { | ||
|
||
return []image.Rectangle{ | ||
{ | ||
|
||
Min: image.Point{ | ||
X: r.Min.X, | ||
Y: r.Min.Y, | ||
}, | ||
Max: image.Point{ | ||
X: ((r.Max.X + r.Min.X) / 2) - 1, | ||
Y: r.Max.Y, | ||
}, | ||
}, | ||
{ | ||
Min: image.Point{ | ||
X: (r.Max.X + r.Min.X) / 2, | ||
Y: r.Min.Y, | ||
}, | ||
Max: image.Point{ | ||
X: r.Max.X, | ||
Y: r.Max.Y, | ||
}, | ||
}, | ||
} | ||
|
||
} |
Oops, something went wrong.