From e9740e5c8d3382aba3e358f64b13d86c47ecc3f4 Mon Sep 17 00:00:00 2001 From: Roman Zipp Date: Thu, 20 Jan 2022 18:06:14 +0100 Subject: [PATCH] Add grayscale image step --- config/example.json | 6 ++++- pipelines/executable_step.go | 11 +++++++++ pipelines/pipeline_test.go | 44 ++++++++++++++++++++++++++++++++++++ pipelines/step.go | 4 ++++ 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/config/example.json b/config/example.json index d688687..e6600bc 100644 --- a/config/example.json +++ b/config/example.json @@ -15,10 +15,14 @@ }, { "name": "rotate image", - "type": 0, + "type": 1, "options": { "angle": 90.0 } + }, + { + "name": "grayscale", + "type": 2 } ], "output": { diff --git a/pipelines/executable_step.go b/pipelines/executable_step.go index 878f8c2..5ad7ef4 100644 --- a/pipelines/executable_step.go +++ b/pipelines/executable_step.go @@ -38,3 +38,14 @@ func (s RotateImageStep) Execute(src image.Image) (image.Image, error) { src = imaging.Rotate(src, s.Options.Angle, image.Black) return src, nil } + +// Grayscale image + +type GrayscaleImageStep struct { + Step +} + +func (s GrayscaleImageStep) Execute(src image.Image) (image.Image, error) { + src = imaging.Grayscale(src) + return src, nil +} diff --git a/pipelines/pipeline_test.go b/pipelines/pipeline_test.go index 4619445..3665f0f 100644 --- a/pipelines/pipeline_test.go +++ b/pipelines/pipeline_test.go @@ -189,6 +189,50 @@ func TestExecuteSteps(t *testing.T) { os.Remove(storageProvider.GetPath(Bucket, "source.jpg")) os.Remove(storageProvider.GetPath(Bucket, dest)) }) + + t.Run("Image grayscale step is successful", func(t *testing.T) { + const Bucket string = "pipeline_test_05" + const Payload string = `{ + "name": "example pipeline", + "type": 1, + "removeMetadata": false, + "steps": [ + { + "name": "grayscale", + "type": 2 + } + ] + }` + + wd, _ := os.Getwd() + pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0] + + storageProvider := storage.GetFileSystemStorageProvider("test", "..") + + // copy test file to storage bucket + _, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/900x900.jpg")) + assert.Nil(t, err, "Test file should be readable") + assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg")) + + // run pipeline steps + dest, err := pipe.Run("source.jpg", Bucket, storageProvider) + assert.Nil(t, err) + assert.FileExists(t, storageProvider.GetPath(Bucket, dest)) + + // read image config + file, err := storageProvider.OpenFile(Bucket, dest) + assert.Nil(t, err) + + imgConf, _, err := image.DecodeConfig(file) + assert.Nil(t, err) + + assert.Equal(t, 900, imgConf.Width) + assert.Equal(t, 900, imgConf.Height) + + // clean up + os.Remove(storageProvider.GetPath(Bucket, "source.jpg")) + os.Remove(storageProvider.GetPath(Bucket, dest)) + }) } // output options diff --git a/pipelines/step.go b/pipelines/step.go index 8aae1af..919c685 100644 --- a/pipelines/step.go +++ b/pipelines/step.go @@ -10,6 +10,7 @@ type StepType int const ( TypeResizeImageStep StepType = iota TypeRotateImageStep + TypeGrayscaleImageStep ) type Step struct { @@ -34,6 +35,9 @@ func (s Step) GetExecutable() (IExecutableStep, error) { return nil, err } return step, nil + + case TypeGrayscaleImageStep: + return GrayscaleImageStep{}, nil } return nil, errors.New("invalid type")