Skip to content

Commit

Permalink
windows: resolve image pixel alignment Windows (#39)
Browse files Browse the repository at this point in the history
Co-authored-by: Changkun Ou <hi@changkun.de>
  • Loading branch information
nocd5 and changkun authored Mar 5, 2023
1 parent 52dde9e commit 72ffba4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
21 changes: 16 additions & 5 deletions clipboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"bytes"
"context"
"errors"
"image/color"
"image/png"
"os"
"reflect"
Expand Down Expand Up @@ -95,8 +96,20 @@ func TestClipboard(t *testing.T) {
incorrect := 0
for i := 0; i < w; i++ {
for j := 0; j < h; j++ {
want := img1.At(i, j)
got := img2.At(i, j)
wr, wg, wb, wa := img1.At(i, j).RGBA()
gr, gg, gb, ga := img2.At(i, j).RGBA()
want := color.RGBA{
R: uint8(wr),
G: uint8(wg),
B: uint8(wb),
A: uint8(wa),
}
got := color.RGBA{
R: uint8(gr),
G: uint8(gg),
B: uint8(gb),
A: uint8(ga),
}

if !reflect.DeepEqual(want, got) {
t.Logf("read data from clipbaord is inconsistent with previous written data, pix: (%d,%d), got: %+v, want: %+v", i, j, got, want)
Expand All @@ -105,9 +118,7 @@ func TestClipboard(t *testing.T) {
}
}

// FIXME: it looks like windows can produce incorrect pixels when y == 0.
// Needs more investigation.
if incorrect > w {
if incorrect > 0 {
t.Fatalf("read data from clipboard contains too much inconsistent pixels to the previous written data, number of incorrect pixels: %v", incorrect)
}
})
Expand Down
16 changes: 2 additions & 14 deletions clipboard_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,8 @@ func readImage() ([]byte, error) {
for y := 0; y < int(info.Height); y++ {
for x := 0; x < int(info.Width); x++ {
idx := offset + 4*(y*stride+x)

// FIXME: It seems that reading from clipboard data causes 3 pixels
// offset. I don't have a clear evidence on the root reason yet.
// xhat := (x + int(info.Width-3)) % int(info.Width)

xhat := (x + int(info.Width)) % int(info.Width)
yhat := int(info.Height) - y
yhat := int(info.Height) - 1 - y
r := data[idx+2]
g := data[idx+1]
b := data[idx+0]
Expand Down Expand Up @@ -235,14 +230,7 @@ func writeImage(buf []byte) error {
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
idx := int(offset) + 4*(y*width+x)

// FIXME: It seems that reading from clipboard data causes 3 pixels
// offset. I don't have a clear evidence on the root reason yet.
// xhat := (x + int(width) - 3) % int(width)
// yhat := int(height) - y
// r, g, b, a := img.At(xhat, yhat).RGBA()

r, g, b, a := img.At(x, height-y).RGBA()
r, g, b, a := img.At(x, height-1-y).RGBA()
data[idx+2] = uint8(r)
data[idx+1] = uint8(g)
data[idx+0] = uint8(b)
Expand Down

0 comments on commit 72ffba4

Please sign in to comment.