transforms.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package images
  3. import (
  4. "fmt"
  5. )
  6. var _ = fmt.Print
  7. func reverse_row(bytes_per_pixel int, pix []uint8) {
  8. if len(pix) <= bytes_per_pixel {
  9. return
  10. }
  11. i := 0
  12. j := len(pix) - bytes_per_pixel
  13. for i < j {
  14. pi := pix[i : i+bytes_per_pixel : i+bytes_per_pixel]
  15. pj := pix[j : j+bytes_per_pixel : j+bytes_per_pixel]
  16. for x := 0; x < bytes_per_pixel; x++ {
  17. pi[x], pj[x] = pj[x], pi[x]
  18. }
  19. i += bytes_per_pixel
  20. j -= bytes_per_pixel
  21. }
  22. }
  23. func (self *Context) FlipPixelsH(bytes_per_pixel, width, height int, pix []uint8) {
  24. stride := bytes_per_pixel * width
  25. self.Parallel(0, height, func(ys <-chan int) {
  26. for y := range ys {
  27. i := y * stride
  28. reverse_row(bytes_per_pixel, pix[i:i+stride])
  29. }
  30. })
  31. }
  32. func (self *Context) FlipPixelsV(bytes_per_pixel, width, height int, pix []uint8) {
  33. stride := bytes_per_pixel * width
  34. num := height / 2
  35. self.Parallel(0, num, func(ys <-chan int) {
  36. for y := range ys {
  37. upper := y
  38. lower := height - 1 - y
  39. a := upper * stride
  40. b := lower * stride
  41. as := pix[a : a+stride : a+stride]
  42. bs := pix[b : b+stride : b+stride]
  43. for i := range as {
  44. as[i], bs[i] = bs[i], as[i]
  45. }
  46. }
  47. })
  48. }