Skip to content

Commit 72ffba4

Browse files
nocd5changkun
andauthored
windows: resolve image pixel alignment Windows (#39)
Co-authored-by: Changkun Ou <[email protected]>
1 parent 52dde9e commit 72ffba4

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

clipboard_test.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"bytes"
1111
"context"
1212
"errors"
13+
"image/color"
1314
"image/png"
1415
"os"
1516
"reflect"
@@ -95,8 +96,20 @@ func TestClipboard(t *testing.T) {
9596
incorrect := 0
9697
for i := 0; i < w; i++ {
9798
for j := 0; j < h; j++ {
98-
want := img1.At(i, j)
99-
got := img2.At(i, j)
99+
wr, wg, wb, wa := img1.At(i, j).RGBA()
100+
gr, gg, gb, ga := img2.At(i, j).RGBA()
101+
want := color.RGBA{
102+
R: uint8(wr),
103+
G: uint8(wg),
104+
B: uint8(wb),
105+
A: uint8(wa),
106+
}
107+
got := color.RGBA{
108+
R: uint8(gr),
109+
G: uint8(gg),
110+
B: uint8(gb),
111+
A: uint8(ga),
112+
}
100113

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

108-
// FIXME: it looks like windows can produce incorrect pixels when y == 0.
109-
// Needs more investigation.
110-
if incorrect > w {
121+
if incorrect > 0 {
111122
t.Fatalf("read data from clipboard contains too much inconsistent pixels to the previous written data, number of incorrect pixels: %v", incorrect)
112123
}
113124
})

clipboard_windows.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,8 @@ func readImage() ([]byte, error) {
139139
for y := 0; y < int(info.Height); y++ {
140140
for x := 0; x < int(info.Width); x++ {
141141
idx := offset + 4*(y*stride+x)
142-
143-
// FIXME: It seems that reading from clipboard data causes 3 pixels
144-
// offset. I don't have a clear evidence on the root reason yet.
145-
// xhat := (x + int(info.Width-3)) % int(info.Width)
146-
147142
xhat := (x + int(info.Width)) % int(info.Width)
148-
yhat := int(info.Height) - y
143+
yhat := int(info.Height) - 1 - y
149144
r := data[idx+2]
150145
g := data[idx+1]
151146
b := data[idx+0]
@@ -235,14 +230,7 @@ func writeImage(buf []byte) error {
235230
for y := 0; y < height; y++ {
236231
for x := 0; x < width; x++ {
237232
idx := int(offset) + 4*(y*width+x)
238-
239-
// FIXME: It seems that reading from clipboard data causes 3 pixels
240-
// offset. I don't have a clear evidence on the root reason yet.
241-
// xhat := (x + int(width) - 3) % int(width)
242-
// yhat := int(height) - y
243-
// r, g, b, a := img.At(xhat, yhat).RGBA()
244-
245-
r, g, b, a := img.At(x, height-y).RGBA()
233+
r, g, b, a := img.At(x, height-1-y).RGBA()
246234
data[idx+2] = uint8(r)
247235
data[idx+1] = uint8(g)
248236
data[idx+0] = uint8(b)

0 commit comments

Comments
 (0)