Skip to content

Commit

Permalink
add grayscale option
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDen committed Aug 10, 2023
1 parent 8b71b14 commit 1e61b1c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
3 changes: 3 additions & 0 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type CollageRequest struct {
PlayCount bool `in:"query=playcount;default=false"`
DisplayArtist bool `in:"query=artist;default=false"`
BoldFont bool `in:"query=boldfont;default=false"`
Grayscale bool `in:"query=grayscale;default=false"`
Webp bool `in:"query=webp;default=false"`
}

Expand Down Expand Up @@ -61,6 +62,7 @@ func generateCollage(ctx context.Context, request *CollageRequest) (*image.Image
ImageDimension: imageDimension,
FontSize: float64(request.FontSize),
BoldFont: request.BoldFont,
Grayscale: request.Grayscale,
Webp: request.Webp,
Rows: request.Rows,
Columns: request.Columns,
Expand Down Expand Up @@ -112,6 +114,7 @@ func Collage(w http.ResponseWriter, r *http.Request) {
Str("method", request.Method).
Int("fontsize", request.FontSize).
Bool("boldfont", request.BoldFont).
Bool("grayscale", request.Grayscale).
Bool("webp", request.Webp).
Msg("Generating collage")

Expand Down
23 changes: 23 additions & 0 deletions internal/generator/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"image"
"image/color"
"runtime"
"time"

Expand All @@ -27,6 +28,7 @@ type DisplayOptions struct {
PlayCount bool
Resize bool
BoldFont bool
Grayscale bool
Compress bool
ArtistName bool
TrackName bool
Expand Down Expand Up @@ -133,6 +135,23 @@ func webpEncode(buf *bytes.Buffer, collage *image.Image, quality float32) error
return err
}

func convertToGrayscale(imgPtr *image.Image) image.Image {
img := *imgPtr
bounds := img.Bounds()
grayImg := image.NewGray(bounds)

for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
colorRGB := img.At(x, y)
r, g, b, _ := colorRGB.RGBA()

grayValue := uint8((r + g + b) / 3 >> 8)
grayImg.SetGray(x, y, color.Gray{Y: grayValue})
}
}
return grayImg
}

func CreateCollage[T Drawable](ctx context.Context, collageElements []T, displayOptions DisplayOptions) (*image.Image, *bytes.Buffer, error) {
start := time.Now()
logger := zerolog.Ctx(ctx)
Expand Down Expand Up @@ -164,6 +183,10 @@ func CreateCollage[T Drawable](ctx context.Context, collageElements []T, display
collage = *resizeImage(ctx, &collage, displayOptions.Width, displayOptions.Height)
}

if displayOptions.Grayscale {
collage = convertToGrayscale(&collage)
}

gcStart := time.Now()
runtime.GC()
logger.Info().Dur("duration", time.Since(gcStart)).Msg("Garbage collection")
Expand Down
16 changes: 14 additions & 2 deletions ui/src/lib/Form.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
showTextSize: z.boolean().optional(),
showTextLocation: z.boolean().optional(),
WebPLossyCompression: z.boolean().optional(),
showBoldtext: z.boolean().optional(),
showBoldtext: z.boolean().optional(),
grayscaleImage: z.boolean().optional(),
textSize: z.string().optional(),
textLocation: z.string().optional(),
});
Expand Down Expand Up @@ -76,6 +78,9 @@
}
if (values.showBoldtext) {
params.append('boldfont', values.showBoldtext.toString());
}
if (values.grayscaleImage) {
params.append('grayscale', values.grayscaleImage.toString());
}
}
Expand Down Expand Up @@ -104,7 +109,8 @@
showTextSize: false,
showTextLocation: false,
textSize: '12',
showBoldtext: false,
showBoldtext: false,
grayscaleImage: false,
WebPLossyCompression: false,
},
});
Expand Down Expand Up @@ -217,6 +223,12 @@
bind:checked={$data.advancedOptions}
/>
<div class="advanced-options">
<Checkbox
text="Grayscale Image"
name="grayscaleImage"
visible={$data.advancedOptions}
bind:checked={$data.grayscaleImage}
/>
<Checkbox
text="Use Bold Text"
name="showBoldtext"
Expand Down

0 comments on commit 1e61b1c

Please sign in to comment.