mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-25 17:52:02 +02:00
Allow controlling num of CPUs for images operations
This commit is contained in:
@@ -404,14 +404,14 @@ func (s *scanner_rgb) scan(x1, y1, x2, y2 int, dst []uint8) {
|
||||
}
|
||||
}
|
||||
|
||||
func paste_nrgb_onto_opaque(background *NRGB, img image.Image, pos image.Point, bgcol *NRGBColor) {
|
||||
func (self *Context) paste_nrgb_onto_opaque(background *NRGB, img image.Image, pos image.Point, bgcol *NRGBColor) {
|
||||
bg := NRGBColor{}
|
||||
if bgcol != nil {
|
||||
bg = *bgcol
|
||||
|
||||
}
|
||||
src := newScannerRGB(img, bg)
|
||||
run_paste(src, background, pos, func(dst []byte) {})
|
||||
self.run_paste(src, background, pos, func(dst []byte) {})
|
||||
}
|
||||
|
||||
func NewNRGB(r image.Rectangle) *NRGB {
|
||||
|
||||
@@ -7,41 +7,10 @@ import (
|
||||
"image"
|
||||
"image/color"
|
||||
"math"
|
||||
"runtime"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
// parallel processes the data in separate goroutines.
|
||||
func parallel(start, stop int, fn func(<-chan int)) {
|
||||
count := stop - start
|
||||
if count < 1 {
|
||||
return
|
||||
}
|
||||
|
||||
procs := runtime.GOMAXPROCS(0)
|
||||
if procs > count {
|
||||
procs = count
|
||||
}
|
||||
|
||||
c := make(chan int, count)
|
||||
for i := start; i < stop; i++ {
|
||||
c <- i
|
||||
}
|
||||
close(c)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < procs; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
fn(c)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
type scanner struct {
|
||||
image image.Image
|
||||
w, h int
|
||||
@@ -330,7 +299,7 @@ type Scanner interface {
|
||||
bounds() image.Rectangle
|
||||
}
|
||||
|
||||
func run_paste(src Scanner, background image.Image, pos image.Point, postprocess func([]byte)) {
|
||||
func (self *Context) run_paste(src Scanner, background image.Image, pos image.Point, postprocess func([]byte)) {
|
||||
pos = pos.Sub(background.Bounds().Min)
|
||||
pasteRect := image.Rectangle{Min: pos, Max: pos.Add(src.bounds().Size())}
|
||||
interRect := pasteRect.Intersect(background.Bounds())
|
||||
@@ -352,7 +321,7 @@ func run_paste(src Scanner, background image.Image, pos image.Point, postprocess
|
||||
default:
|
||||
panic(fmt.Sprintf("Unsupported image type: %v", v))
|
||||
}
|
||||
parallel(interRect.Min.Y, interRect.Max.Y, func(ys <-chan int) {
|
||||
self.Parallel(interRect.Min.Y, interRect.Max.Y, func(ys <-chan int) {
|
||||
for y := range ys {
|
||||
x1 := interRect.Min.X - pasteRect.Min.X
|
||||
x2 := interRect.Max.X - pasteRect.Min.X
|
||||
@@ -368,14 +337,14 @@ func run_paste(src Scanner, background image.Image, pos image.Point, postprocess
|
||||
|
||||
}
|
||||
|
||||
func paste_nrgba_onto_opaque(background *image.NRGBA, img image.Image, pos image.Point, bgcol *NRGBColor) {
|
||||
func (self *Context) paste_nrgba_onto_opaque(background *image.NRGBA, img image.Image, pos image.Point, bgcol *NRGBColor) {
|
||||
src := newScanner(img)
|
||||
if bgcol == nil {
|
||||
run_paste(src, background, pos, func([]byte) {})
|
||||
self.run_paste(src, background, pos, func([]byte) {})
|
||||
return
|
||||
}
|
||||
bg := [3]float64{float64(bgcol.R), float64(bgcol.G), float64(bgcol.B)}
|
||||
run_paste(src, background, pos, func(dst []byte) {
|
||||
self.run_paste(src, background, pos, func(dst []byte) {
|
||||
for len(dst) > 0 {
|
||||
a := float64(dst[3]) / 255.0
|
||||
for i := range dst[:3] {
|
||||
@@ -390,19 +359,19 @@ func paste_nrgba_onto_opaque(background *image.NRGBA, img image.Image, pos image
|
||||
}
|
||||
|
||||
// Paste pastes the img image to the background image at the specified position. Optionally composing onto the specified opaque color.
|
||||
func Paste(background image.Image, img image.Image, pos image.Point, opaque_bg *NRGBColor) {
|
||||
func (self *Context) Paste(background image.Image, img image.Image, pos image.Point, opaque_bg *NRGBColor) {
|
||||
switch background.(type) {
|
||||
case *image.NRGBA:
|
||||
paste_nrgba_onto_opaque(background.(*image.NRGBA), img, pos, opaque_bg)
|
||||
self.paste_nrgba_onto_opaque(background.(*image.NRGBA), img, pos, opaque_bg)
|
||||
case *NRGB:
|
||||
paste_nrgb_onto_opaque(background.(*NRGB), img, pos, opaque_bg)
|
||||
self.paste_nrgb_onto_opaque(background.(*NRGB), img, pos, opaque_bg)
|
||||
default:
|
||||
panic("Unsupported background image type")
|
||||
}
|
||||
}
|
||||
|
||||
// PasteCenter pastes the img image to the center of the background image. Optionally composing onto the specified opaque color.
|
||||
func PasteCenter(background image.Image, img image.Image, opaque_bg *NRGBColor) {
|
||||
func (self *Context) PasteCenter(background image.Image, img image.Image, opaque_bg *NRGBColor) {
|
||||
bgBounds := background.Bounds()
|
||||
bgW := bgBounds.Dx()
|
||||
bgH := bgBounds.Dy()
|
||||
@@ -415,7 +384,7 @@ func PasteCenter(background image.Image, img image.Image, opaque_bg *NRGBColor)
|
||||
x0 := centerX - img.Bounds().Dx()/2
|
||||
y0 := centerY - img.Bounds().Dy()/2
|
||||
|
||||
Paste(background, img, image.Pt(x0, y0), opaque_bg)
|
||||
self.Paste(background, img, image.Pt(x0, y0), opaque_bg)
|
||||
}
|
||||
|
||||
func FitImage(width, height, pwidth, pheight int) (final_width int, final_height int) {
|
||||
|
||||
@@ -25,9 +25,9 @@ func reverse_row(bytes_per_pixel int, pix []uint8) {
|
||||
}
|
||||
}
|
||||
|
||||
func FlipPixelsH(bytes_per_pixel, width, height int, pix []uint8) {
|
||||
func (self *Context) FlipPixelsH(bytes_per_pixel, width, height int, pix []uint8) {
|
||||
stride := bytes_per_pixel * width
|
||||
parallel(0, height, func(ys <-chan int) {
|
||||
self.Parallel(0, height, func(ys <-chan int) {
|
||||
for y := range ys {
|
||||
i := y * stride
|
||||
reverse_row(bytes_per_pixel, pix[i:i+stride])
|
||||
@@ -35,10 +35,10 @@ func FlipPixelsH(bytes_per_pixel, width, height int, pix []uint8) {
|
||||
})
|
||||
}
|
||||
|
||||
func FlipPixelsV(bytes_per_pixel, width, height int, pix []uint8) {
|
||||
func (self *Context) FlipPixelsV(bytes_per_pixel, width, height int, pix []uint8) {
|
||||
stride := bytes_per_pixel * width
|
||||
num := height / 2
|
||||
parallel(0, num, func(ys <-chan int) {
|
||||
self.Parallel(0, num, func(ys <-chan int) {
|
||||
for y := range ys {
|
||||
upper := y
|
||||
lower := height - 1 - y
|
||||
|
||||
56
tools/utils/images/utils.go
Normal file
56
tools/utils/images/utils.go
Normal file
@@ -0,0 +1,56 @@
|
||||
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package images
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
type Context struct {
|
||||
num_of_threads atomic.Int32
|
||||
}
|
||||
|
||||
func (self *Context) SetNumberOfThreads(n int) {
|
||||
self.num_of_threads.Store(int32(n))
|
||||
}
|
||||
|
||||
func (self *Context) NumberOfThreads() int {
|
||||
return int(self.num_of_threads.Load())
|
||||
}
|
||||
|
||||
// parallel processes the data in separate goroutines.
|
||||
func (self *Context) Parallel(start, stop int, fn func(<-chan int)) {
|
||||
count := stop - start
|
||||
if count < 1 {
|
||||
return
|
||||
}
|
||||
|
||||
procs := self.NumberOfThreads()
|
||||
if procs <= 0 {
|
||||
procs = runtime.NumCPU()
|
||||
}
|
||||
if procs > count {
|
||||
procs = count
|
||||
}
|
||||
|
||||
c := make(chan int, count)
|
||||
for i := start; i < stop; i++ {
|
||||
c <- i
|
||||
}
|
||||
close(c)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < procs; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
fn(c)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
Reference in New Issue
Block a user