Skip to content

Commit

Permalink
Merge pull request #30 from pushd/dither_options_20240415
Browse files Browse the repository at this point in the history
Dither options 20240415
  • Loading branch information
jtomson authored Apr 15, 2024
2 parents e9dc467 + 45dc5c1 commit 120d6c6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 16 deletions.
8 changes: 7 additions & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ RUN apt-get update \
python3-dev \
gfortran \
libopencv-dev \
curl
curl \
zstd

RUN pip3 install scikit-image opencv-python numpy Pillow --break-system-packages

Expand All @@ -49,6 +50,11 @@ COPY plugins/pushd-dither /opt/pushd-dither/
RUN cd /opt/pushd-dither \
&& python3 -m pip install -e . --break-system-packages --target=/usr/local/lib/python3.11/

# decompress LUTs
RUN cd /opt/pushd-dither/lut_dither \
&& zstd -fd --rm lab_13_hack.interpol_clarabel.npy.zst \
&& zstd -fd --rm rgb_13.interpol_clarabel.npy.zst

# ====== END PUSHD DITHER ========


Expand Down
62 changes: 49 additions & 13 deletions options/processing_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,19 @@ const (
)

type DitherOptions struct {
Type DitherType
Contrast bool
Native bool
Desaturate bool
Meter13 bool
SoftProof bool
Clamp bool
Type DitherType
Contrast bool
Native bool
Desaturate bool
Meter13 bool
SoftProof bool
Clamp bool
HullProject bool
LUTFile string
LUTBlue bool
SaturationScale float64
NormalizeContrast bool
CLAHESize int
}

type WatermarkOptions struct {
Expand Down Expand Up @@ -162,7 +168,7 @@ func NewProcessingOptions() *ProcessingOptions {
Blur: 0,
Sharpen: 0,
Dpr: 1,
Dither: DitherOptions{Type: DitherNone, Contrast: false, Native: false, Desaturate: false, Meter13: false, SoftProof: false, Clamp: false},
Dither: DitherOptions{},
Watermark: WatermarkOptions{Opacity: 1, Replicate: false, Gravity: GravityOptions{Type: GravityCenter}},
StripMetadata: config.StripMetadata,
KeepCopyright: config.KeepCopyright,
Expand Down Expand Up @@ -721,10 +727,6 @@ func applyPixelateOption(po *ProcessingOptions, args []string) error {
}

func applyDitherOption(po *ProcessingOptions, args []string) error {
if len(args) > 7 {
return fmt.Errorf("Invalid dither arguments: %v", args)
}

switch args[0] {
case "fs":
po.Dither.Type = DitherBNFS
Expand All @@ -749,14 +751,48 @@ func applyDitherOption(po *ProcessingOptions, args []string) error {
po.Dither.SoftProof = true
case "cl":
po.Dither.Clamp = true
case "hp":
po.Dither.HullProject = true
case "lb":
po.Dither.LUTBlue = true
case "nc":
po.Dither.NormalizeContrast = true
case "llab13":
po.Dither.LUTFile = "lab_13_hack.interpol_clarabel.npy"
case "lrgb13":
po.Dither.LUTFile = "rgb_13.interpol_clarabel.npy"
default:
return fmt.Errorf("Invalid dither argument: %s", arg)
if err := maybeParseNumericDitherOptions(po, arg); err != nil {
return err
}
}
}
}
return nil
}

func maybeParseNumericDitherOptions(po *ProcessingOptions, arg string) error {
var err error
var i int
var f float64
if i, err = strconv.Atoi(arg); err == nil && i > 0 {
// any integer is considered the CLAHE size argument
if po.Dither.CLAHESize > 0 {
return fmt.Errorf("Dither CLAHE size is already set: %d, arg: %s", po.Dither.CLAHESize, arg)
}
po.Dither.CLAHESize = i
} else if f, err = strconv.ParseFloat(arg, 64); err == nil && f >= 0 {
// any float is considered the saturation scale argument
if po.Dither.SaturationScale > 0 {
return fmt.Errorf("Dither saturation scale is already set: %f, arg: %s", po.Dither.SaturationScale, arg)
}
po.Dither.SaturationScale = f
} else {
return fmt.Errorf("Invalid dither argument: %s", arg)
}
return nil
}

func applyPresetOption(po *ProcessingOptions, args []string) error {
for _, preset := range args {
if p, ok := presets[preset]; ok {
Expand Down
2 changes: 1 addition & 1 deletion plugins/pushd-dither
20 changes: 19 additions & 1 deletion processing/dither.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,32 @@ func shellOutDither(inFile string, po *options.ProcessingOptions) error {
cmdArgs = append(cmdArgs, "--desaturate")
}
if po.Dither.Meter13 {
cmdArgs = append(cmdArgs, "--pal-meter-13")
cmdArgs = append(cmdArgs, "--pal-meter-13-hack")
}
if po.Dither.SoftProof {
cmdArgs = append(cmdArgs, "--image-raw", proofFile)
}
if po.Dither.Clamp {
cmdArgs = append(cmdArgs, "--clamp")
}
if po.Dither.CLAHESize > 0 {
cmdArgs = append(cmdArgs, "--clahe-size", fmt.Sprintf("%d", po.Dither.CLAHESize))
}
if po.Dither.SaturationScale > 0 {
cmdArgs = append(cmdArgs, "--saturation-scale", fmt.Sprintf("%f", po.Dither.SaturationScale))
}
if po.Dither.HullProject {
cmdArgs = append(cmdArgs, "--hull-project")
}
if len(po.Dither.LUTFile) > 0 {
cmdArgs = append(cmdArgs, "--lut", fmt.Sprintf("lut_dither/%s", po.Dither.LUTFile))
}
if po.Dither.LUTBlue {
cmdArgs = append(cmdArgs, "--lut-blue")
}
if po.Dither.NormalizeContrast {
cmdArgs = append(cmdArgs, "--normalize-contrast")
}

cmd := exec.Command("python3", cmdArgs...)
cmd.Dir = "/opt/pushd-dither"
Expand Down

0 comments on commit 120d6c6

Please sign in to comment.