Add dither server

This commit is contained in:
David Eisinger
2024-02-01 23:42:50 -05:00
parent 0822998d6a
commit 0a7b98406a
3 changed files with 83 additions and 0 deletions

7
bin/dither/Gemfile Normal file
View File

@@ -0,0 +1,7 @@
# frozen_string_literal: true
source "https://rubygems.org"
gem "sinatra"
gem "rackup"
gem "mini_magick"

36
bin/dither/Gemfile.lock Normal file
View File

@@ -0,0 +1,36 @@
GEM
remote: https://rubygems.org/
specs:
base64 (0.2.0)
mini_magick (4.12.0)
mustermann (3.0.0)
ruby2_keywords (~> 0.0.1)
rack (3.0.9)
rack-protection (4.0.0)
base64 (>= 0.1.0)
rack (>= 3.0.0, < 4)
rack-session (2.0.0)
rack (>= 3.0.0)
rackup (2.1.0)
rack (>= 3)
webrick (~> 1.8)
ruby2_keywords (0.0.5)
sinatra (4.0.0)
mustermann (~> 3.0)
rack (>= 3.0.0, < 4)
rack-protection (= 4.0.0)
rack-session (>= 2.0.0, < 3)
tilt (~> 2.0)
tilt (2.3.0)
webrick (1.8.1)
PLATFORMS
arm64-darwin-22
DEPENDENCIES
mini_magick
rackup
sinatra
BUNDLED WITH
2.4.8

40
bin/dither/dither.rb Normal file
View File

@@ -0,0 +1,40 @@
require "sinatra"
require "mini_magick"
MiniMagick.logger.level = Logger::DEBUG
ROOT = ENV["ROOT"]
KEY = ENV["KEY"]
FileUtils.mkdir_p "tmp"
get "/*" do |path|
filename = File.basename(path)
geometry = params["geo"]
@decrypted = Tempfile.new(filename, "tmp")
@dithered = Tempfile.new([filename, ".png"], "tmp")
%x(
openssl \
aes-256-cbc \
-d \
-in #{ROOT}/#{path}.enc \
-out #{@decrypted.path} \
-pass file:#{KEY} \
-iter 1000000
)
MiniMagick::Tool::Magick.new do |magick|
magick << @decrypted.path
magick.resize "#{geometry}^"
magick.gravity "center"
magick.extent geometry
magick.ordered_dither "o8x8"
magick.monochrome
magick << @dithered.path
end
content_type "image/png"
File.open(@dithered.path)
end