File: transforms.go

package info (click to toggle)
kitty 0.42.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 28,564 kB
  • sloc: ansic: 82,787; python: 55,191; objc: 5,122; sh: 1,295; xml: 364; makefile: 143; javascript: 78
file content (55 lines) | stat: -rw-r--r-- 1,227 bytes parent folder | download | duplicates (2)
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
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>

package images

import (
	"fmt"
)

var _ = fmt.Print

func reverse_row(bytes_per_pixel int, pix []uint8) {
	if len(pix) <= bytes_per_pixel {
		return
	}
	i := 0
	j := len(pix) - bytes_per_pixel
	for i < j {
		pi := pix[i : i+bytes_per_pixel : i+bytes_per_pixel]
		pj := pix[j : j+bytes_per_pixel : j+bytes_per_pixel]
		for x := 0; x < bytes_per_pixel; x++ {
			pi[x], pj[x] = pj[x], pi[x]
		}
		i += bytes_per_pixel
		j -= bytes_per_pixel
	}
}

func (self *Context) FlipPixelsH(bytes_per_pixel, width, height int, pix []uint8) {
	stride := bytes_per_pixel * width
	self.Parallel(0, height, func(ys <-chan int) {
		for y := range ys {
			i := y * stride
			reverse_row(bytes_per_pixel, pix[i:i+stride])
		}
	})
}

func (self *Context) FlipPixelsV(bytes_per_pixel, width, height int, pix []uint8) {
	stride := bytes_per_pixel * width
	num := height / 2
	self.Parallel(0, num, func(ys <-chan int) {
		for y := range ys {
			upper := y
			lower := height - 1 - y
			a := upper * stride
			b := lower * stride
			as := pix[a : a+stride : a+stride]
			bs := pix[b : b+stride : b+stride]
			for i := range as {
				as[i], bs[i] = bs[i], as[i]
			}
		}
	})

}