package attachment import ( "bytes" "encoding/binary" "image" "image/color" "testing" ) func TestFitGeometry(t *testing.T) { for _, c := range []struct{ w, h, limit, x, y int }{{320, 200, 256, 256, 160}, {513, 257, 256, 256, 128}, {257, 513, 256, 128, 256}, {12, 8, 256, 12, 8}, {1, 40000, 256, 1, 256}} { w, h := fit(c.w, c.h, c.limit) if w != c.x || h != c.y { t.Fatalf("fit(%d,%d)=%d,%d", c.w, c.h, w, h) } } } func TestOrientationsAndAlpha(t *testing.T) { img := image.NewNRGBA(image.Rect(0, 0, 3, 2)) for y := 0; y < 2; y++ { for x := 0; x < 3; x++ { img.SetNRGBA(x, y, color.NRGBA{R: uint8(1 + x + y*3), A: 255}) } } expected := [][]uint8{{1, 2, 3, 4, 5, 6}, {3, 2, 1, 6, 5, 4}, {6, 5, 4, 3, 2, 1}, {4, 5, 6, 1, 2, 3}, {1, 4, 2, 5, 3, 6}, {4, 1, 5, 2, 6, 3}, {6, 3, 5, 2, 4, 1}, {3, 6, 2, 5, 1, 4}} for i, want := range expected { src := oriented{img, i + 1} got := []uint8{} for y := 0; y < src.Bounds().Dy(); y++ { for x := 0; x < src.Bounds().Dx(); x++ { got = append(got, src.At(x, y).(color.RGBA).R) } } if !bytes.Equal(got, want) { t.Fatalf("orientation %d: %v want %v", i+1, got, want) } } img.SetNRGBA(0, 0, color.NRGBA{R: 255, A: 128}) got := oriented{img, 1}.At(0, 0).(color.RGBA) if got != (color.RGBA{255, 127, 127, 255}) { t.Fatalf("white alpha: %v", got) } } func TestEXIFOrientation(t *testing.T) { for _, order := range []binary.ByteOrder{binary.LittleEndian, binary.BigEndian} { for v := 1; v <= 8; v++ { tiff := make([]byte, 26) copy(tiff, "II") if order == binary.BigEndian { copy(tiff, "MM") } order.PutUint16(tiff[2:], 42) order.PutUint32(tiff[4:], 8) order.PutUint16(tiff[8:], 1) order.PutUint16(tiff[10:], 0x112) order.PutUint16(tiff[12:], 3) order.PutUint32(tiff[14:], 1) order.PutUint16(tiff[18:], uint16(v)) data := append([]byte{255, 216, 255, 225, 0, 34}, append([]byte("Exif\x00\x00"), tiff...)...) if Orientation(data) != v { t.Fatalf("EXIF value %d", v) } pngData := []byte{137, 80, 78, 71, 13, 10, 26, 10} size := make([]byte, 4) binary.BigEndian.PutUint32(size, uint32(len(tiff))) pngData = append(pngData, size...) pngData = append(pngData, []byte("eXIf")...) pngData = append(pngData, tiff...) pngData = append(pngData, 0, 0, 0, 0) if Orientation(pngData) != v { t.Fatalf("PNG EXIF value %d", v) } } } for _, b := range [][]byte{nil, {}, {255, 216, 255, 225, 255, 255}, {255, 216, 255, 255, 225, 0, 2}} { if Orientation(b) != 1 { t.Fatal("malformed EXIF should be ignored") } } }