diff --git a/fixtures/test_square.jpg b/fixtures/test_square.jpg new file mode 100644 index 0000000..c69aab4 Binary files /dev/null and b/fixtures/test_square.jpg differ diff --git a/options.go b/options.go index e3cd738..33119fa 100644 --- a/options.go +++ b/options.go @@ -57,12 +57,20 @@ type Angle int const ( // D0 represents the rotation angle 0 degrees. D0 Angle = 0 + // D45 represents the rotation angle 90 degrees. + D45 Angle = 45 // D90 represents the rotation angle 90 degrees. D90 Angle = 90 + // D135 represents the rotation angle 90 degrees. + D135 Angle = 135 // D180 represents the rotation angle 180 degrees. D180 Angle = 180 + // D180 represents the rotation angle 180 degrees. + D235 Angle = 235 // D270 represents the rotation angle 270 degrees. D270 Angle = 270 + // D315 represents the rotation angle 180 degrees. + D315 Angle = 315 ) // Direction represents the image direction value. diff --git a/vips.h b/vips.h index 1eecdc3..e3dc1c8 100644 --- a/vips.h +++ b/vips.h @@ -121,15 +121,31 @@ int vips_rotate(VipsImage *in, VipsImage **out, int angle) { int rotate = VIPS_ANGLE_D0; - if (angle == 90) { + angle %= 360; + + if (angle == 45) { + rotate = VIPS_ANGLE45_D45; + } else if (angle == 90) { rotate = VIPS_ANGLE_D90; + } else if (angle == 135) { + rotate = VIPS_ANGLE45_D135; } else if (angle == 180) { rotate = VIPS_ANGLE_D180; + } else if (angle == 225) { + rotate = VIPS_ANGLE45_D225; } else if (angle == 270) { rotate = VIPS_ANGLE_D270; + } else if (angle == 315) { + rotate = VIPS_ANGLE45_D315; + } else { + angle = 0; } - return vips_rot(in, out, rotate, NULL); + if (angle > 0 && angle % 90 != 0) { + return vips_rot45(in, out, "angle", rotate, NULL); + } else { + return vips_rot(in, out, rotate, NULL); + } } int diff --git a/vips_test.go b/vips_test.go index da5cea5..6456a54 100644 --- a/vips_test.go +++ b/vips_test.go @@ -42,16 +42,26 @@ func TestVipsSave(t *testing.T) { } func TestVipsRotate(t *testing.T) { - image, _, _ := vipsRead(readImage("test.jpg")) - - newImg, err := vipsRotate(image, D90) - if err != nil { - t.Fatal("Cannot save the image") + files := []struct { + name string + rotate Angle + }{ + {"test.jpg", D90}, + {"test_square.jpg", D45}, } - buf, _ := vipsSave(newImg, vipsSaveOptions{Quality: 95}) - if len(buf) == 0 { - t.Fatal("Empty image") + for _, file := range files { + image, _, _ := vipsRead(readImage(file.name)) + + newImg, err := vipsRotate(image, file.rotate) + if err != nil { + t.Fatal("Cannot rotate the image") + } + + buf, _ := vipsSave(newImg, vipsSaveOptions{Quality: 95}) + if len(buf) == 0 { + t.Fatal("Empty image") + } } }