#! /usr/bin/perl use strict; use threads; use Image::Magick; # Number of sumultaneous conversions my $max_jobs = 5; # Get existing images to convert my @endgames_orig = glob("endgame-??-??-??-??.bmp"); my @screenshots_orig = glob("screenshot-??-??-??-??.bmp"); # Get existing conversions to check where we should leave off my @endgames_done = glob("endgame*.png"); my @screenshots_done = glob("screenshot*.png"); # Counters for current position on where to start my $end_pos = 1; my $scrot_pos = 1; my @threads; # Worker threads my @work; # Job queue foreach (@endgames_done) { $end_pos = $1 >= $end_pos ? ($1 + 1) : $end_pos if m/^endgame(\d+)\.png$/; } foreach (@screenshots_done) { $scrot_pos = $1 >= $scrot_pos ? ($1 + 1) : $scrot_pos if m/^screenshot(\d+)\.png$/; } foreach (@endgames_orig) { push(@work, [$_, "endgame".$end_pos++.".png"]); } foreach (@screenshots_orig) { push(@work, [$_, "screenshot".$scrot_pos++.".png"]); } while (1) { # Make new workers while (scalar @threads < $max_jobs) { if (my $job = pop(@work)) { push @threads, threads->create(sub { threads->self()->detach(); my ($src, $dest) = @_; my $image = new Image::Magick; $image->Read($src); $image->Write($dest); unlink $src; print "Done with $src -> $dest\n"; }, @$job[0], @$job[1]); } else { print "No more images to deal with\n"; goto done; } } # Periodically reap dead shit to free up new slots for (my $i = 0; $i < scalar @threads; $i++) { delete @threads[$i] if $threads[$i] != undef && !$threads[$i]->is_running(); } } done: print "Done";