#lang plai-typed (require [opaque-type-in 2htdp/image [Color color?] [Image image?]]) (require [typed-in 2htdp/image (image? : ('a -> boolean)) (color : (number number number number -> Color)) (image-width : (Image -> number)) (image->color-list : (Image -> (listof Color))) (color? : ('a -> boolean)) (color-list->bitmap : ((listof Color) number number -> Image)) (image-height : (Image -> number))]) (require [typed-in racket/list (take : ((listof 'a) number -> (listof 'a))) (drop : ((listof 'a) number -> (listof 'a)))]) (require [typed-in racket/base (apply : (((listof 'a) (listof 'a) -> (listof 'a)) (listof 'a) -> 'a))]) ;; image->2d-color-list: image -> (list-of (list-of color)) ;; consumes an image and returns a two-dimensional list of colors in row-major order ;; where each element of the output is the color of the corresponding pixel in the image (define (image->2d-color-list image) (local ((define width (image-width image)) (define (chunk lst) (if (empty? lst) empty (cons (take lst width) (chunk (drop lst width)))))) (chunk (image->color-list image)))) ;; 2d-color-list->image: (list-of (list-of color)) -> image ;; consumes a two-dimensional list of colors in row-major order ;; returns an image where each pixel is the color of the corresponding element in the list (define (2d-color-list->image rows) (color-list->bitmap (apply append rows) (if (empty? rows) 0 (length (first rows))) (length rows)))