librius 0.5.1

A personal library manager CLI written in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<#
====================================================================================
 ImageMagick Utility Toolkit (magick_tools.ps1)
------------------------------------------------------------------------------------
 Author : Alessandro Maestri
 Version: 2.2
 Date   : 2025-10-31
------------------------------------------------------------------------------------
 Changelog v2.2:
   - extract: added --prefix and --format (png|jpg|webp) options.
   - extract: default output directory now 'layers/' instead of <name>_layers.
   - Usage section updated with examples.
   - Fixed encoding artifacts replacing '����' with '->'.
====================================================================================
#>

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

# ------------------------- CONFIG DI DEFAULT -------------------------
$DefaultMagick = "X:\ImageMagick-7.1.2-8-portable-Q16-HDRI-x64\magick.exe"
$ValidActions = 'identify', 'convert', 'resize', 'optimize', 'make-ico', 'extract'

# ------------------------- USAGE -------------------------
function Show-Usage
{
    Write-Host "Usage:" -ForegroundColor Cyan
    Write-Host "  magick_tools.ps1 --action identify --input assets/rbackup.ico" -ForegroundColor Gray
    Write-Host "  magick_tools.ps1 --action convert --input logo.png --output logo.webp" -ForegroundColor Gray
    Write-Host "  magick_tools.ps1 --action resize --input img.png --output img_256.png --width 256" -ForegroundColor Gray
    Write-Host "  magick_tools.ps1 --action make-ico --input logo.png --output favicon.ico --sizes 16,32,48" -ForegroundColor Gray
    Write-Host "  magick_tools.ps1 --action extract --input favicon.ico --output layers --prefix icon --format webp" -ForegroundColor Gray
    Write-Host "Options:" -ForegroundColor Cyan
    Write-Host "  --action|-Action|-a   (required) one of: $( $ValidActions -join ', ' )" -ForegroundColor Gray
    Write-Host "  --input|-Input|-i     input file" -ForegroundColor Gray
    Write-Host "  --output|-Output|-o   output file (file for actions convert/resize/optimize/make-ico; dir for extract)" -ForegroundColor Gray
    Write-Host "  --width|-Width        width (resize)" -ForegroundColor Gray
    Write-Host "  --height|-Height      height (optional for resize)" -ForegroundColor Gray
    Write-Host "  --sizes|-Sizes        size list for make-ico (e.g. 16,32,48)" -ForegroundColor Gray
    Write-Host "  --magick|-Magick      path to magick.exe (default: $DefaultMagick)" -ForegroundColor Gray
    Write-Host "  --verbose|-Verbose    verbose output (no value required)" -ForegroundColor Gray
    Write-Host "  --prefix|-Prefix      filename prefix for extract output (default: layer)" -ForegroundColor Gray
    Write-Host "  --format|-Format      output format for extract (png|jpg|webp) default: png" -ForegroundColor Gray
    Write-Host "  --colors|-Colors      color palette size for make-ico (omit to keep truecolor + alpha)" -ForegroundColor Gray
    Write-Host "  --no-dither           disable dithering when --colors is used" -ForegroundColor Gray
    Write-Host "Available actions and examples:" -ForegroundColor Cyan
    Write-Host "  identify    show image metadata (properties, format, dimensions)" -ForegroundColor Gray
    Write-Host "              Example: magick_tools.ps1 --action identify --input assets/rbackup.ico" -ForegroundColor DarkGray
    Write-Host "  convert     convert input image to output (format inferred by output extension)" -ForegroundColor Gray
    Write-Host "              Example: magick_tools.ps1 --action convert --input logo.png --output logo.webp" -ForegroundColor DarkGray
    Write-Host "  resize      resize image to width x height (height optional)" -ForegroundColor Gray
    Write-Host "              Example: magick_tools.ps1 --action resize --input img.png --output img_256x128.png --width 256 --height 128" -ForegroundColor DarkGray
    Write-Host "  optimize    optimize (strip metadata, interlace Plane, quality 85%)" -ForegroundColor Gray
    Write-Host "              Example: magick_tools.ps1 --action optimize --input photo.jpg --output photo_opt.jpg" -ForegroundColor DarkGray
    Write-Host "  make-ico    build multi-size .ico from input raster (requires --sizes)" -ForegroundColor Gray
    Write-Host "              Example: magick_tools.ps1 --action make-ico --input logo.png --output favicon.ico --sizes 16,32,48,64 --colors 256" -ForegroundColor DarkGray
    Write-Host "  extract     extract all layers from an .ico into numbered PNG files" -ForegroundColor Gray
    Write-Host "              Example: magick_tools.ps1 --action extract --input favicon.ico --output layers --prefix icon --format webp" -ForegroundColor DarkGray
}

# Se nessun argomento, mostra usage e termina
if ($args.Length -eq 0)
{
    Show-Usage; exit 1
}

# ------------------------- PARSER ARGOMENTI -------------------------
# Converte qualsiasi forma --key, -Key o --key=value in chiave canonicalizzata (Action, InputPath, ...) in un hashtable.
$Parsed = [ordered]@{ }
$ExpectValueFor = $null
for ($idx = 0; $idx -lt $args.Length; $idx++) {
    $token = $args[$idx]
    if ($ExpectValueFor) {
        $Parsed[$ExpectValueFor] = $token
        $ExpectValueFor = $null
        continue
    }
    if ($token -match '^--?') {
        # Gestisce forma --key=value
        $pair = $token -split '=', 2
        $flagToken = $pair[0]
        $valueInline = if ($pair.Length -gt 1) { $pair[1] } else { $null }
        $name = $flagToken -replace '^-{1,2}', ''
        $canon = $null  # inizializza per StrictMode
        switch -Regex ( $name.ToLower()) {
            '^(a|action)$'  { $canon = 'Action' }
            '^(i|input)$'   { $canon = 'InputPath' }
            '^(o|output)$'  { $canon = 'OutputPath' }
            '^(width)$'     { $canon = 'Width' }
            '^(height)$'    { $canon = 'Height' }
            '^(sizes)$'     { $canon = 'Sizes' }
            '^(magick)$'    { $canon = 'Magick' }
            '^(verbose)$'   { $canon = 'Verbose' }
            '^(prefix)$'    { $canon = 'Prefix' }
            '^(format)$'    { $canon = 'Format' }
            '^(colors)$'    { $canon = 'Colors' }
            '^(no-dither)$' { $canon = 'NoDither' }
            default {
                Write-Warning "Unknown option '$token' ignored"
            }
        }
        if (-not $canon) {
            # Opzione sconosciuta: se non inline assignment e prossimo token non è un flag, salta il valore accidentale
            if ($null -eq $valueInline -and ($idx + 1) -lt $args.Length -and $args[$idx+1] -notmatch '^--?') { $idx++ }
            continue
        }
        if ($null -ne $valueInline) {
            if ($canon -eq 'Verbose') {
                $Parsed[$canon] = [System.Convert]::ToBoolean($valueInline)
            } else {
                $Parsed[$canon] = $valueInline
            }
        } else {
            if ($canon -eq 'Verbose') {
                $Parsed[$canon] = $true
            } else {
                $ExpectValueFor = $canon
            }
        }
    } else {
        Write-Host "❌ Unexpected bare value '$token' (precede un'opzione?)" -ForegroundColor Red
        Show-Usage; exit 1
    }
}
if ($ExpectValueFor)
{
    Write-Host "❌ Missing value for option '$ExpectValueFor'" -ForegroundColor Red
    Show-Usage; exit 1
}

# Applica default per Magick se non passato
if (-not $Parsed.Contains('Magick'))
{
    $Parsed['Magick'] = $DefaultMagick
}
if (-not $Parsed.Contains('Verbose')) { $Parsed['Verbose'] = $false }
if (-not $Parsed.Contains('Prefix'))  { $Parsed['Prefix']  = 'layer' }
if (-not $Parsed.Contains('Format'))  { $Parsed['Format']  = 'png' }
if (-not $Parsed.Contains('NoDither')) { $Parsed['NoDither'] = $false }

# ------------------------- VALIDAZIONE LOGICA -------------------------
function Fail
{
    param([string]$Msg); Write-Host "ERROR: $Msg" -ForegroundColor Red; Show-Usage; exit 1
}

if (-not $Parsed.Contains('Action'))
{
    Fail 'Missing --action'
}
$Action = $Parsed['Action']
if ($ValidActions -notcontains $Action.ToLower())
{
    Fail "Invalid action '$Action'"
}

# Parametri facoltativi (rinominati per evitare conflitto con variabile automatica $input)
$InputPath = $Parsed['InputPath']
$OutputPath = $Parsed['OutputPath']
$Width = $Parsed['Width']
$Height = $Parsed['Height']
$Sizes = $Parsed['Sizes']
$Magick = $Parsed['Magick']
$Verbose = $Parsed['Verbose']
$Prefix = $Parsed['Prefix']
$Format = $Parsed['Format']
$Colors = $Parsed['Colors']
$NoDither = $Parsed['NoDither']

# Repo root (script sotto dev_tools/) per risoluzione path
$RepoRoot = Split-Path -Parent $PSScriptRoot
function Resolve-RepoPath
{
    param([string]$Path); if (-not $Path)
    {
        return $null
    }; if ( [System.IO.Path]::IsPathRooted($Path))
    {
        return $Path
    }; Join-Path $RepoRoot $Path
}
function Invoke-InputPath
{
    param([string]$Path)
    if (-not $Path)
    {
        Fail 'Missing --input'
    }
    $resolved = Resolve-RepoPath $Path
    if (-not (Test-Path -LiteralPath $resolved))
    {
        Fail "Input file not found: $Path (resolved: $resolved)"
    }
    return $resolved
}
function Invoke-Magick
{
    param([string]$Exe)
    # Se percorso esplicito esiste, usa quello
    if ($Exe -and (Test-Path -LiteralPath $Exe))
    {
        return $Exe
    }
    # Tentativo ricerca in PATH (magick.exe oppure magick)
    $cmd = Get-Command magick.exe -ErrorAction SilentlyContinue
    if (-not $cmd)
    {
        $cmd = Get-Command magick -ErrorAction SilentlyContinue
    }
    if ($cmd)
    {
        Write-Host "🔍 Found magick.exe in PATH: $( $cmd.Source )" -ForegroundColor Yellow
        return $cmd.Source
    }
    Fail "ImageMagick not found. Tried explicit path '$Exe' and PATH search (magick.exe/magick)."
}

# Risolve path eseguibile (assegna eventualmente quello trovato nel PATH)
$Magick = Invoke-Magick $Magick
$MagickVersionLine = (& $Magick -version 2>$null | Select-Object -First 1)
$IsMagick7 = $false
if ($MagickVersionLine -and ($MagickVersionLine -match 'ImageMagick 7')) { $IsMagick7 = $true }
$actionLower = $Action.ToLowerInvariant()
$__startTime = Get-Date

# ------------------------- HEADER GLOBALE -------------------------
$ResolvedInputPath  = if ($InputPath)  { Resolve-RepoPath $InputPath }  else { 'N/A' }
$ResolvedOutputPath = if ($OutputPath) { Resolve-RepoPath $OutputPath } else { 'N/A' }
Write-Host "=== ImageMagick Toolkit ===" -ForegroundColor Cyan
Write-Host "Action : $Action" -ForegroundColor Gray
Write-Host "Input  : $InputPath" -ForegroundColor Gray
Write-Host "         resolved -> $ResolvedInputPath" -ForegroundColor DarkGray
Write-Host "Output : $OutputPath" -ForegroundColor Gray
Write-Host "         resolved -> $ResolvedOutputPath" -ForegroundColor DarkGray
Write-Host "Magick : $Magick" -ForegroundColor Gray
Write-Host "Verbose: $Verbose" -ForegroundColor Gray
Write-Host "Prefix : $Prefix" -ForegroundColor Gray
Write-Host "Format : $Format" -ForegroundColor Gray
Write-Host "Colors : $Colors" -ForegroundColor Gray
Write-Host "NoDith : $NoDither" -ForegroundColor Gray
Write-Host "Started: $($__startTime.ToString('yyyy-MM-dd HH:mm:ss'))" -ForegroundColor Gray
Write-Host "---------------------------" -ForegroundColor DarkGray

# ------------------------- DISPATCH -------------------------
switch ($actionLower)
{
    'identify' {
        $inPath = Invoke-InputPath $InputPath
        if ($Verbose) { Write-Host "[VERBOSE] Running: $Magick identify $inPath" -ForegroundColor DarkGray }
        & $Magick identify $inPath
    }
    'convert' {
        if (-not $InputPath -or -not $OutputPath)
        {
            Fail 'Missing required options: --input, --output'
        }
        $inPath = Invoke-InputPath $InputPath
        $outPath = Resolve-RepoPath $OutputPath
        if ($Verbose) { Write-Host "[VERBOSE] Running: $Magick $inPath $outPath" -ForegroundColor DarkGray }
        & $Magick $inPath $outPath
        Write-Host "✅ Converted: $inPath → $outPath" -ForegroundColor Green
    }
    'resize' {
        if (-not $InputPath -or -not $OutputPath -or -not $Width)
        {
            Fail 'Missing required options: --input, --output, --width'
        }
        $inPath = Invoke-InputPath $InputPath
        $outPath = Resolve-RepoPath $OutputPath
        $size = if ($Height)
        {
            "${Width}x${Height}"
        }
        else
        {
            "$Width"
        }
        if ($Verbose) { Write-Host "[VERBOSE] Running: magick (v7=$IsMagick7) resize $inPath -> $outPath ($size)" -ForegroundColor DarkGray }
        if ($IsMagick7) {
            & $Magick $inPath -resize $size $outPath
        } else {
            & $Magick convert $inPath -resize $size $outPath
        }
        if ($LASTEXITCODE -ne 0) { Fail "Resize failed (exit code $LASTEXITCODE)" }
        Write-Host "✅ Resized $inPath → $outPath ($size)" -ForegroundColor Green
    }
    'optimize' {
        $inPath = Invoke-InputPath $InputPath
        $outPath = if ($OutputPath)
        {
            Resolve-RepoPath $OutputPath
        }
        else
        {
            Resolve-RepoPath $InputPath
        }
        if ($Verbose) { Write-Host "[VERBOSE] Running: magick (v7=$IsMagick7) optimize $inPath -> $outPath" -ForegroundColor DarkGray }
        if ($IsMagick7) {
            & $Magick $inPath -strip -interlace Plane -quality 85% $outPath
        } else {
            & $Magick convert $inPath -strip -interlace Plane -quality 85% $outPath
        }
        if ($LASTEXITCODE -ne 0) { Fail "Optimize failed (exit code $LASTEXITCODE)" }
        Write-Host "✅ Optimized $inPath → $outPath" -ForegroundColor Green
    }
    'make-ico' {
        if (-not $InputPath -or -not $OutputPath -or -not $Sizes)
        {
            Fail 'Missing required options: --input, --output, --sizes'
        }
        $inPath = Invoke-InputPath $InputPath
        $outPath = Resolve-RepoPath $OutputPath
        $sizesList = $Sizes -split ',' | ForEach-Object { $_.Trim() } | Where-Object { $_ -match '^[0-9]+$' }
        if (-not $sizesList)
        {
            Fail 'Invalid --sizes list (expected comma-separated numbers)'
        }
        # Build ICO preserving alpha (IM7 supports auto-resize)
        if ($Verbose) { Write-Host "[VERBOSE] Source alpha detection..." -ForegroundColor DarkGray }
        $channelInfo = (& $Magick identify -format "%[channels]" $inPath 2>$null)
        $hasAlpha = $false; if ($channelInfo -and ($channelInfo -match 'a' -or $channelInfo -match 'alpha')) { $hasAlpha = $true }
        if ($Verbose) { Write-Host "[VERBOSE] Channels: $channelInfo Alpha=$hasAlpha" -ForegroundColor DarkGray }
        $joinedSizes = ($sizesList -join ',')
        $quantArgs = @()
        if ($Colors) {
            if ($Colors -notmatch '^[0-9]+$') { Fail "--colors must be numeric" }
            $quantArgs += '-colors'; $quantArgs += $Colors
            if ($NoDither) { $quantArgs += '-dither'; $quantArgs += 'None' }
        }
        if ($IsMagick7) {
            # One-shot pipeline
            $cmdArgs = @($inPath, '-background','none')
            if ($hasAlpha) { $cmdArgs += '-alpha'; $cmdArgs += 'set' } else { $cmdArgs += '-alpha'; $cmdArgs += 'off' }
            $cmdArgs += '-define'; $cmdArgs += "icon:auto-resize=$joinedSizes"
            if ($Colors) { $cmdArgs += $quantArgs }
            $cmdArgs += '-depth'; $cmdArgs += '32'
            $cmdArgs += $outPath
            if ($Verbose) { Write-Host "[VERBOSE] Running IM7 ICO build: $Magick $($cmdArgs -join ' ')" -ForegroundColor DarkGray }
            & $Magick @cmdArgs
            if ($LASTEXITCODE -ne 0) { Fail "ICO build failed (exit $LASTEXITCODE)" }
        } else {
            # Legacy path: create temp PNG32 images then assemble
            $tempFiles = @()
            foreach ($s in $sizesList) {
                $tmp = [System.IO.Path]::GetTempFileName() + ".png"
                $resizeArgs = @($inPath,'-background','none')
                if ($hasAlpha) { $resizeArgs += '-alpha'; $resizeArgs += 'set' }
                $resizeArgs += '-resize'; $resizeArgs += "${s}x${s}"
                $resizeArgs += "PNG32:$tmp"
                if ($Verbose) { Write-Host "[VERBOSE] Gen size ${s}x${s}: $Magick $($resizeArgs -join ' ')" -ForegroundColor DarkGray }
                & $Magick @resizeArgs
                if ($LASTEXITCODE -ne 0) { Fail "Failed generating size ${s}x${s} (exit $LASTEXITCODE)" }
                $tempFiles += $tmp
            }
            $assemble = @()
            $assemble += $tempFiles
            if ($Colors) { $assemble += $quantArgs }
            $assemble += $outPath
            if ($Verbose) { Write-Host "[VERBOSE] Assemble legacy ICO: $Magick convert $($assemble -join ' ')" -ForegroundColor DarkGray }
            & $Magick convert @assemble
            if ($LASTEXITCODE -ne 0) { Fail "ICO assembly failed (exit $LASTEXITCODE)" }
            foreach ($tmp in $tempFiles) { Remove-Item $tmp -Force -ErrorAction SilentlyContinue }
        }
        Write-Host "✅ ICO created: $outPath (sizes: $joinedSizes alpha=$hasAlpha colors=$Colors)" -ForegroundColor Green
    }
    'extract' {
        if (-not $InputPath) { Fail 'Missing --input for extract action' }
        $inPath = Invoke-InputPath $InputPath
        # Output directory default now fixed to 'layers/' under input folder if not provided
        $outDir = if ($OutputPath) { Resolve-RepoPath $OutputPath } else { Join-Path (Split-Path -Parent $inPath) 'layers' }
        if (-not (Test-Path -LiteralPath $outDir)) { New-Item -ItemType Directory -Path $outDir | Out-Null }
        if ($Verbose) { Write-Host "[VERBOSE] Extract output directory: $outDir" -ForegroundColor DarkGray }
        $fmt = $Format.ToLower()
        if ($fmt -eq 'jpeg') { $fmt = 'jpg' }
        if ($fmt -notin @('png','jpg','webp')) { Fail "Unsupported --format '$Format' (allowed: png, jpg, webp)" }
        # Frame count
        $frameCount = (& $Magick identify -format "%n" $inPath)
        if ($Verbose) { Write-Host "[VERBOSE] Frame count detected: $frameCount" -ForegroundColor DarkGray }
        if (-not $frameCount -or [int]$frameCount -le 0) { Fail "No frames detected in icon: $inPath" }
        $exported = @()
        for ($i = 0; $i -lt [int]$frameCount; $i++) {
            $outFile = Join-Path $outDir ("${Prefix}_{0}.${fmt}" -f $i)
            if ($Verbose) { Write-Host "[VERBOSE] Extract layer $i (v7=$IsMagick7) -> $outFile" -ForegroundColor DarkGray }
            if ($IsMagick7) {
                & $Magick $inPath[$i] $outFile
            } else {
                & $Magick convert $inPath[$i] $outFile
            }
            if ($LASTEXITCODE -ne 0) { Fail "Failed to extract layer index $i (exit $LASTEXITCODE)" }
            $exported += $outFile
        }
        Write-Host "✅ Extracted ${frameCount} layer(s) to: $outDir (format: $fmt, prefix: $Prefix)" -ForegroundColor Green
        if ($Verbose) {
            Write-Host "[VERBOSE] Files:" -ForegroundColor DarkGray
            $exported | ForEach-Object { Write-Host "  $_" -ForegroundColor DarkGray }
        }
    }
    default {
        Fail "Unknown action $Action"
    }
}
$__elapsed = (Get-Date) - $__startTime
if ($Verbose) { Write-Host "[VERBOSE] Elapsed: $([int]$__elapsed.TotalMilliseconds) ms" -ForegroundColor DarkGray }

Write-Host ""; Write-Host "🎉 Operation completed successfully." -ForegroundColor Green
exit 0