+
+;; copy of ly:system. ly:* not available via lilypond-ps2png.scm
+(define (my-system be-verbose exit-on-error cmd)
+ (define status 0)
+ (if be-verbose
+ (begin
+ (format (current-error-port) (_ "Invoking `~a'...") cmd)
+ (newline (current-error-port))))
+
+
+ (set! status (system cmd))
+
+ (if (not (= status 0))
+ (begin
+ (format (current-error-port)
+ (format #f (_ "~a exited with status: ~S") "GS" status))
+ (if exit-on-error
+ (exit 1))))
+
+ status)
+
+(define (scale-down-image be-verbose factor file)
+ (let* ((status 0)
+ (percentage (* 100 (/ 1.0 factor)))
+ (old (string-append file ".old")))
+
+ (rename-file file old)
+ (my-system be-verbose
+ #t
+ (format #f "convert -scale \"~a%\" ~a ~a" percentage old file))
+ (delete-file old)
+ ))
+
+(define-public (ps-page-count ps-name)
+ (let*
+ ((byte-count 10240)
+ (header (gulp-file ps-name byte-count))
+ (first-null (string-index header #\nul))
+ (match (string-match "%%Pages: ([0-9]+)"
+ (if (number? first-null)
+ (substring header 0 first-null)
+ header))))
+
+ (if match
+ (string->number (match:substring match 1))
+ 0)))
+