mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-07 21:48:13 -08:00
Add support for 45° rotation.
This commit is contained in:
parent
66f3ccc217
commit
00fee003f7
4 changed files with 44 additions and 10 deletions
BIN
fixtures/test_square.jpg
Normal file
BIN
fixtures/test_square.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 134 KiB |
|
|
@ -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.
|
||||
|
|
|
|||
20
vips.h
20
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
|
||||
|
|
|
|||
26
vips_test.go
26
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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue