-(define (get-bbox file-name)
- (let* ((bbox (string-append file-name ".bbox"))
- ;; -sOutputFile does not work with bbox?
- (cmd (format #t "gs\
- -sDEVICE=bbox\
- -q\
- -dNOPAUSE\
- ~S\
- -c showpage\
- -c quit 2>~S"
- file-name bbox))
- (status (system cmd))
- (s (read (open-file bbox "r")))
- (m (string-match BOUNDING_BOX_RE s)))
- (display m)
- (newline)
- (if m
- (list->vector
- (map (lambda (x) (string->number (car x))) (vector->list m)))
- #f)))
+(define-public (gulp-file file-name . max-size)
+ (ly:gulp-file file-name (if (pair? max-size) (car max-size))))
+
+;; 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)
+ (define (with-pbm)
+ (let* ((status 0)
+ (old (string-append file ".old")))
+
+ (rename-file file old)
+ (my-system
+ be-verbose #t
+ (format #f
+ "pngtopnm ~a | pnmscale -reduce ~a 2>/dev/null | pnmtopng -compression 9 2>/dev/null > ~a"
+ old factor file))
+ (delete-file old)))
+
+ (with-pbm))
+
+(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)))