#lang racket/base (require 2htdp/universe 2htdp/image racket/local racket/list (only-in lang/htdp-beginner check-expect)) ;; 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) (if (image? 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))) (error 'image->2d-color-list "expected argument of type , found ~a" 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) (if (or (empty? rows) (and (cons? rows) (or (empty? (first rows)) (and (cons? (first rows)) (color? (first (first rows))))))) (color-list->bitmap (apply append rows) (if (empty? rows) 0 (length (first rows))) (length rows)) (error '2d-color-list->image "expected argument of type >, found ~a" rows))) ;; image->image: image -> image ;; consumes an image and outputs the same image ;; but in the format returned by color-list->image and the pinhole at (0, 0) (define (image->image image) (color-list->bitmap (image->color-list image) (image-width image) (image-height image))) ;; check-image: image image -> void? ;; takes two images and prints whether or not they are the same as a test message (define-syntax-rule (check-image image1 image2) (check-expect (image->image image1) (image->image image2))) (provide image->2d-color-list 2d-color-list->image check-image)