aboutsummaryrefslogtreecommitdiff
path: root/pnm/ppm.scm
diff options
context:
space:
mode:
authorMasaya Tojo <masaya@tojo.tokyo>2024-08-04 18:56:16 +0900
committerMasaya Tojo <masaya@tojo.tokyo>2024-08-04 18:56:16 +0900
commitf5d3c117582d25e1aa5b7ea37947653b2276585c (patch)
treea7ecdb71186866bacfd90436c01276bd69fdb6f5 /pnm/ppm.scm
parent5aebc7faba25a31c813b911a3ef253d9bf426bfd (diff)
Check the length of the given bytevector
Diffstat (limited to 'pnm/ppm.scm')
-rw-r--r--pnm/ppm.scm10
1 files changed, 8 insertions, 2 deletions
diff --git a/pnm/ppm.scm b/pnm/ppm.scm
index 4ca453e..95ce837 100644
--- a/pnm/ppm.scm
+++ b/pnm/ppm.scm
@@ -40,7 +40,8 @@
(< 65536 maxval))
(error "(pnm ppm) make-ppm-image: Maxval is out of range"))
(if (< maxval 256)
- (let* ((w*3 (* width 3)))
+ (let* ((w*3 (* width 3))
+ (byte-count (* w*3 height)))
(define (xy->idx x y) (+ (* 3 x) (* y w*3)))
(define (pixel-getter x y)
(let ((idx (xy->idx x y)))
@@ -52,8 +53,11 @@
(bytevector-u8-set! data idx r)
(bytevector-u8-set! data (+ idx 1) g)
(bytevector-u8-set! data (+ idx 2) b)))
+ (unless (= byte-count (bytevector-length data))
+ (error (string-append "(pnm pbm) make-pbm-image: Invalid bytevector length" byte-count)))
(make-image 'ppm width height maxval data pixel-getter pixel-setter))
- (let* ((w*6 (* width 6)))
+ (let* ((w*6 (* width 6))
+ (byte-count (* w*6 height)))
(define (xy->idx x y) (+ (* 6 x) (* y w*6)))
(define (pixel-getter x y)
(let ((idx (xy->idx x y)))
@@ -74,6 +78,8 @@
(bytevector-u8-set! data (+ idx 3) g2)
(bytevector-u8-set! data (+ idx 4) b1)
(bytevector-u8-set! data (+ idx 5) b2))))
+ (unless (= byte-count (bytevector-length data))
+ (error (string-append "(pnm pbm) make-pbm-image: Invalid bytevector length" byte-count)))
(make-image 'ppm width height maxval data pixel-getter pixel-setter))))))
(define (split-value v)