NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

(define ERROR 0.01)




;; ====== Problem 6.1 ======

;; A struct representing a fully opaque color in the HSL color system, see
;; e.g. https://en.wikipedia.org/wiki/HSL_and_HSV
;; H: number - hue component (normalized to [0, 1])
;; S: number - saturation component (normalized to [0, 1])
;; L: number - lightness component (normalized to [0, 1])

(define-struct hsl (H S L))

;; rgb-to-hsl: color −> hsl
;; Komsumiert ein Color und wandelt den darin gegebenen Farbwert in ein r,g oder b um.
;; Example: (ausstehend)!

(define (rgb-to-hsl rgb-col)
(local(
(define (big-r-to-r red)
(*(/ 1 255)(color-red red)) )
(define (big-g-to-g green)
(*(/ 1 255)(color-green green)) )
(define (big-b-to-b blue)
(*(/ 1 255)(color-blue blue)) )

(define (max rgb)
;; Berechnet das Max (M) von den R,G,B Eingaben.
;; max: number number number -> number
(max red green blue) )
(define (min rgb)
;; Berechnet das Min (m) von den R,G,B Eingaben.
;; min: number number number -> number
(min red green blue) )
(define (C max min)
;; Berechnet C, indem Min von Max abgezogen wird.
;; C: number number -> number
(- max min) )

(define (H red green blue max C)
(cond
[(= c 0)0]
[(= max red) (/(*(-(/(- green blue) C) 6) /floor (/(/(- green blue) C) 6)))6]
[(= max green) (/(+(/(- blue green) C) 2) 6)]
[(= max blue) (/(+(/(- red green) C) 4) 6)] ))

(define (L max min)
;; L: number number -> number
(/(+ max min) 2))

(define (amount n)
;; Berechnet den Betrag.
;; amount: number -> number
(cond
[(> n 0)(+ 1 (amount (- n 1)))]
[(< n 0)(+ 1 (amount (+ n 1)))]
[else 0] ))

(define (S C L)
(cond
[(or (= C 0) (= L 0) (= L 1)) 0]
[else (/ C (- 1 (amount (- (* 2 L) 1))))] ))

)

(make-hsl (H S L))
)
)

;; Tests

;; rgb-to-hsl-list: (listof color)−> (listof hsl)
;; Konsumiert eine list of Color und gibt eine list of HSL aus.
;; Example: (ausstehend)!

(define (rgb-to-hsl-list rgb-list)
(map rgb-to-hsl rgb-list) )

;; Tests



;; ====== Problem 6.2 ======

;; hsl-to-ascii: hsl -> integer
;; Konsumiert ein HSL und berechnet daraus den ASCII Wert.
;; Example: (ausstehend)!

(define (hsl-to-ascii hsl)
(cond
[(<= (hsl-L hsl) 0,05) 35]
[(and (< 0,05 hsl-L) (<= hsl-L 0,5)) (round (+ 65 (- (* H 90) (* H 65))))]
[(and (< 0,05 hsl-L) (<= hsl-L) (<= hsl-L 0,5)) (round (+ 97 (- (* H 122) (* H 97))))]
[else 32] ))

;; Tests


;; hsl-to-ascii-list: (listof hsl -> (listof integer)
;; (Beschreibung ausstehend)!
;; Example: (ausstehend)!

(define (hsl-to-ascii-list hsl-list)
(map hsl-to-ascii hsl-list) )


;; Tests



;; ====== Problem 6.3 ======

;; average3: (listof color)−> (listof color)
;; Komsumiert eine Color Liste und wandelt sie in eine ASCII Liste um.
;; Example: (ausstehend)!
(define (average3 image)
(local (
(define (ascii-list hsl-list)
(hsl-to-ascii-list (rgb-to-hsl-list) (hsl-list)))

(define mask-alg hsl-list)
;; Konsumiert eine ASCII Liste und macht daraus eine Liste von Listen mit jeweils
;; 3 Elementen (i-1, i, i+1).
(cond
[(empty? ascii) 0]
[else
(cons [(first ascii-list (first (rest ascii-list) (first (rest (rest ascii-list))))
(mash-alg (rest ascii-list)))] )]) ;;zweites

(define (first-m)
;; Das erste Element laut dem Mask-Alg mit first = 0.
(list (0 (first ascii-list) (first (rest ascii-list)))) )


;; No tests necessary here



;; ====== Problem 6.4 ======

;; downscale: (listof color)integer −> (listof color)
;; (Beschreibung ausstehend)!
;; Example: (ausstehend)!
(define (downscale image width)
...)

;; No tests necessary here

;; ====== Provided code ======

;; downsample: (listof color) integer -> (listof color)
;; Downsamples an image by a factor of 2, i.e. first blurs the image with horizontal kernel
;; 0.25 * [1 2 1], then removes every second pixel horizontally and every second row, pads with (0, 0, 0);
;; Example: (downsample (list (make-color 0 0 0) (make-color 1 1 1) (make-color 2 2 2) (make-color 3 3 3)
;; (make-color 4 5 5) (make-color 5 5 5) (make-color 6 6 6) (make-color 7 7 7)) 4) returns
;; (list (make-color 0 0 0) (make-color 2 2 2)) with width of 2
(define (downsample rgb-list width)
(if
(empty? rgb-list)
empty
(downscale (average3 rgb-list) width)))

;; Test
;; error here -> student did forget to check for an empty list
(check-expect (downsample empty 0) empty)
;; error here -> student assumes that list-length is greater than 1 or pads wrong
(check-expect (downsample (list (make-color 128 128 128)) 1) (list (make-color 64 64 64 255)))
;; error here -> student probably does not take the imge's width correctly into account
(check-expect (downsample (list
(make-color 0 0 0) (make-color 1 1 1) (make-color 2 2 2) (make-color 3 3 3)
(make-color 4 4 4) (make-color 5 5 5) (make-color 6 6 6) (make-color 7 7 7)) 4)
(list (make-color 0 0 0) (make-color 2 2 2)))


;; downsample-pyramid: (listof color) integer integer -> (listof color)
;; Executes pyramidical downsampling (i.e. n times downscaling by factor two), the resulting width is width / 2^(n - 1)
;; Example: (visual)
(define (downsample-pyramid rgb-list width n)
(if
(= n 0)
;; no more steps for downscaling - return image
rgb-list
;; otherwise, sample one more step
(downsample (downsample-pyramid rgb-list width (- n 1)) (/ width (expt 2 (- n 1))))))

;; here: only visual tests (load, downsample/average3 and export image, do a visual comparison)
;; load-averag3-save: string string integer -> boolean (and PNG-image on disk)
(define (load-average3-save in-path out-path)
(local
((define in-img (bitmap/file in-path)))
(save-image
(color-list->bitmap
(average3 (image->color-list in-img))
(image-width in-img)
(image-height in-img))
out-path)))

;; load-downscale-save: string string integer -> boolean (and PNG-image on disk)
(define (load-downscale-save in-path out-path)
(local
((define in-img (bitmap/file in-path)))
(save-image
(color-list->bitmap
(downscale (image->color-list in-img) (image-width in-img))
(/ (image-width in-img) 2)
(/ (image-height in-img) 2))
out-path)))

;; Visual tests (Only check for successful save)
(check-expect (load-average3-save "fop-u04-material/gcc_logo.jpg" "fop-u04-material/gcc_logo_blurred.png") true)
(check-expect (load-average3-save "fop-u04-material/solid_snake.jpg" "fop-u04-material/solid_snake_blurred.png") true)
(check-expect (load-average3-save "fop-u04-material/big_boss.jpg" "fop-u04-material/big_boss_blurred.png") true)

(check-expect (load-downscale-save "fop-u04-material/gcc_logo.jpg" "fop-u04-material/gcc_logo_scaled.png") true)
(check-expect (load-downscale-save "fop-u04-material/solid_snake.jpg" "fop-u04-material/solid_snake_scaled.png") true)
(check-expect (load-downscale-save "fop-u04-material/big_boss.jpg" "fop-u04-material/big_boss_scaled.png") true)

;; ascii-artify: string number -> string
;; Loads an image from the given path, downscales it n times by 2 and converts it to ascii art, the saves it as HTML-page in fop-04-material/ascii_art.html
(define (ascii-artify img-path n)
(local
;; save-ascii-file: (listof integer) integer string -> filename (and ascii-image wrapped into HTML page on disk)
;; Saves a list of integers as ASCII image of a gven width in HTML page (zoomable)
((define (save-ascii-image ascii-list width filename)
(local
;; converts integers in input ist to characters
((define char-list (map integer->char ascii-list))
;; add-newlines: (listof char) integer integer -> (listof char)
;; Adds a newline character (#newline) every width characters
(define (add-newlines lst i width)
(cond
[(empty? lst) lst]
;; passed width characters -> add <br /> and #newline for HTML view
[(and (= (modulo i width) 0) (> i 0)) (append (list #< #b #r #/ #> (first lst)) (add-newlines (rest lst) (+ i 1) width))]
;; else just keep character
[else (cons (first lst) (add-newlines (rest lst) (+ i 1) width))])))
;; write ASCII file as HTML page
(write-file
filename
;; add custom square font to HTML page and keep formatting of ASCII art
(string-append
"<html><head><title>FOP Ascii Art</title>"
"<style type="text/css">@font-face {font-family: Square; src: url("square.ttf") format("truetype")}</style>"
"</head><body><pre style="font-family:Square, monospace;">"
(list->string (add-newlines char-list 0 width))
"</pre></body></html>"))))
(define img (bitmap/file img-path)))
;; save downscaled image
(save-ascii-image
(hsl-to-ascii-list (rgb-to-hsl-list (downsample-pyramid (image->color-list img) (image-width img) n)))
(/ (image-width img) (expt 2 n))
"fop-u04-material/ascii_art.html")))

;; Execution - CALL HERE FOR ASCII ART GENERATION!
(ascii-artify "fop-u04-material/gcc_logo.jpg" 2)

     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.