diff --git a/resources/jpg-24bit-icc-iec.Pixelate-macos-12.golden.jpeg b/resources/jpg-24bit-icc-iec.Pixelate-macos-12.golden.jpeg new file mode 100644 index 00000000..ba5f9c08 Binary files /dev/null and b/resources/jpg-24bit-icc-iec.Pixelate-macos-12.golden.jpeg differ diff --git a/resources/png-8bit+alpha.Embed_ExtendBackground_Alpha-macos-12.golden.png b/resources/png-8bit+alpha.Embed_ExtendBackground_Alpha-macos-12.golden.png new file mode 100644 index 00000000..192b2224 Binary files /dev/null and b/resources/png-8bit+alpha.Embed_ExtendBackground_Alpha-macos-12.golden.png differ diff --git a/vips/image.go b/vips/image.go index 96e8e2a2..384a6a6e 100644 --- a/vips/image.go +++ b/vips/image.go @@ -1594,3 +1594,25 @@ const ( CodingLABQ Coding = C.VIPS_CODING_LABQ CodingRAD Coding = C.VIPS_CODING_RAD ) + +// Pixelate applies a simple pixelate filter to the image +func Pixelate(imageRef *ImageRef, factor float64) (err error) { + if factor < 1 { + return errors.New("factor must be greater then 1") + } + + width := imageRef.Width() + height := imageRef.Height() + + if err = imageRef.Resize(1/factor, KernelAuto); err != nil { + return + } + + hScale := float64(width) / float64(imageRef.Width()) + vScale := float64(height) / float64(imageRef.Height()) + if err = imageRef.ResizeWithVScale(hScale, vScale, KernelNearest); err != nil { + return + } + + return +} diff --git a/vips/image_golden_test.go b/vips/image_golden_test.go index 0f136528..c4ce9060 100644 --- a/vips/image_golden_test.go +++ b/vips/image_golden_test.go @@ -878,6 +878,14 @@ func TestImage_QuantTable(t *testing.T) { ) } +func TestImage_Pixelate(t *testing.T) { + goldenTest(t, resources+"jpg-24bit-icc-iec.jpg", + func(img *ImageRef) error { + return Pixelate(img, 24) + }, + nil, nil) +} + func testWebpOptimizeIccProfile(t *testing.T, exportParams *WebpExportParams) []byte { return goldenTest(t, resources+"has-icc-profile.png", func(img *ImageRef) error {