github.com/neohugo/neohugo@v0.123.8/resources/images/dither.go (about)

     1  // Copyright 2024 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package images
    15  
    16  import (
    17  	"image"
    18  	"image/draw"
    19  
    20  	"github.com/disintegration/gift"
    21  	"github.com/makeworld-the-better-one/dither/v2"
    22  )
    23  
    24  var _ gift.Filter = (*ditherFilter)(nil)
    25  
    26  type ditherFilter struct {
    27  	ditherer *dither.Ditherer
    28  }
    29  
    30  var ditherMethodsErrorDiffusion = map[string]dither.ErrorDiffusionMatrix{
    31  	"atkinson":            dither.Atkinson,
    32  	"burkes":              dither.Burkes,
    33  	"falsefloydsteinberg": dither.FalseFloydSteinberg,
    34  	"floydsteinberg":      dither.FloydSteinberg,
    35  	"jarvisjudiceninke":   dither.JarvisJudiceNinke,
    36  	"sierra":              dither.Sierra,
    37  	"sierra2":             dither.Sierra2,
    38  	"sierra2_4a":          dither.Sierra2_4A,
    39  	"sierra3":             dither.Sierra3,
    40  	"sierralite":          dither.SierraLite,
    41  	"simple2d":            dither.Simple2D,
    42  	"stevenpigeon":        dither.StevenPigeon,
    43  	"stucki":              dither.Stucki,
    44  	"tworowsierra":        dither.TwoRowSierra,
    45  }
    46  
    47  var ditherMethodsOrdered = map[string]dither.OrderedDitherMatrix{
    48  	"clustereddot4x4":            dither.ClusteredDot4x4,
    49  	"clustereddot6x6":            dither.ClusteredDot6x6,
    50  	"clustereddot6x6_2":          dither.ClusteredDot6x6_2,
    51  	"clustereddot6x6_3":          dither.ClusteredDot6x6_3,
    52  	"clustereddot8x8":            dither.ClusteredDot8x8,
    53  	"clustereddotdiagonal16x16":  dither.ClusteredDotDiagonal16x16,
    54  	"clustereddotdiagonal6x6":    dither.ClusteredDotDiagonal6x6,
    55  	"clustereddotdiagonal8x8":    dither.ClusteredDotDiagonal8x8,
    56  	"clustereddotdiagonal8x8_2":  dither.ClusteredDotDiagonal8x8_2,
    57  	"clustereddotdiagonal8x8_3":  dither.ClusteredDotDiagonal8x8_3,
    58  	"clustereddothorizontalline": dither.ClusteredDotHorizontalLine,
    59  	"clustereddotspiral5x5":      dither.ClusteredDotSpiral5x5,
    60  	"clustereddotverticalline":   dither.ClusteredDotVerticalLine,
    61  	"horizontal3x5":              dither.Horizontal3x5,
    62  	"vertical5x3":                dither.Vertical5x3,
    63  }
    64  
    65  func (f ditherFilter) Draw(dst draw.Image, src image.Image, options *gift.Options) {
    66  	gift.New().Draw(dst, f.ditherer.Dither(src))
    67  }
    68  
    69  func (f ditherFilter) Bounds(srcBounds image.Rectangle) image.Rectangle {
    70  	return image.Rect(0, 0, srcBounds.Dx(), srcBounds.Dy())
    71  }