#!/bin/bash

# thumb size
thumb_size="250x"

# Check if the input file is provided
if [ $# -ne 1 ]; then
    echo "Usage: $0 <input_image>"
    exit 1
fi

# Assign the input filename to a variable
input_file="$1"

# Create the "color" subfolder if it doesn't exist
mkdir -p color
mkdir -p color/thumb

# Step 1: Compress the image and save it to the "color" subfolder
convert -limit memory 100MB "$input_file" -quality 66% -interlace Plane -unsharp 0x0.35 "color/$(basename "$input_file")"
convert -limit memory 100MB "color/$(basename "$input_file")" -thumbnail "$thumb_size" "color/thumb/$(basename "$input_file")"

# Create the "black_and_white" subfolder if it doesn't exist
mkdir -p black_and_white
mkdir -p black_and_white/thumb

# Step 2: Convert the compressed image to black and white and save it to the "black_and_white" subfolder
convert -limit memory 100MB "color/$(basename "$input_file")" -colorspace Gray "black_and_white/$(basename "$input_file")"
convert -limit memory 100MB "black_and_white/$(basename "$input_file")" -thumbnail "$thumb_size" "black_and_white/thumb/$(basename "$input_file")"

# Create the "hue_rotated_180" subfolder if it doesn't exist
mkdir -p hue_rotated_180
mkdir -p hue_rotated_180/thumb

# Step 3: Convert the compressed image to hue 180 rotate and save it to the "hue_rotated_180" subfolder
convert -limit memory 100MB "color/$(basename "$input_file")" -define modulate:colorspace=LCHab -modulate 100,100,0 "hue_rotated_180/$(basename "$input_file")"
convert -limit memory 100MB "hue_rotated_180/$(basename "$input_file")" -thumbnail "$thumb_size" "hue_rotated_180/thumb/$(basename "$input_file")"

# Create the "negative" subfolder if it doesn't exist
mkdir -p negative
mkdir -p negative/thumb

# Step 4: Convert the compressed image to negative and save it to the "negative" subfolder
convert -limit memory 100MB "color/$(basename "$input_file")" -negate "negative/$(basename "$input_file")"
convert -limit memory 100MB "negative/$(basename "$input_file")" -thumbnail "$thumb_size" "negative/thumb/$(basename "$input_file")"


rm -f "$input_file"

