Mercurial > vba-clojure
view clojure/com/aurellem/run/image.clj @ 514:a00981db92da
can now display black and white images (still working on color).
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 22 Jun 2012 21:03:04 -0500 |
parents | 3dbb863eb801 |
children | f68d600b089c |
line wrap: on
line source
1 (ns com.aurellem.run.image2 (:use (com.aurellem.gb saves gb-driver util constants3 items vbm characters money4 rlm-assembly))5 (:use (com.aurellem.run util music title save-corruption6 bootstrap-0 bootstrap-1))7 (:require clojure.string)8 (:import [com.aurellem.gb.gb_driver SaveState])9 (:import java.io.File))11 ;; want to display an image onto the screen.12 ;; probably will be the six ponies, possibly with scrolling.14 ;; probably don't need hi-color mode since the images shuld be15 ;; simple.17 ;; use background tiles? they provide greater color depth than18 ;; sprites, and can still be scrolled, so why not?20 ;; could also use sprites to get 3 more colors per tile for a total of21 ;; 7 colors per tile, although not for all tiles...25 ;; want a function to27 ;; 1. read an image28 ;; 2. split into a grid of 8x8 pixels29 ;; 3. convert all RGB colors to gb-RGB colors30 ;; 4. determine efficient color palletes for the image31 ;; 5. output efficient assembly code to draw the image to the gb32 ;; screen.35 (def image-program-target 0xB000)37 (def display-width 160)38 (def display-height 144)42 ;{:r :g :b }44 (def character-data 0x8000)45 (def character-data-end 0x97FF)50 (def BG-data-1 0x9800)52 (def BG-data-2 0x9C00)54 (def OAM 0xFE00)58 (def video-bank-select-register 0xFF4F)60 (defn gb-rgb->bits [[r g b]]61 (assert (<= 0 r 31))62 (assert (<= 0 g 31))63 (assert (<= 0 b 31))64 [(bit-and65 0xFF66 (+67 r68 (bit-shift-left g 5)))69 (+70 (bit-shift-right g 3)71 (bit-shift-left b 2))])74 (def bg-palette-select 0xFF68)75 (def bg-palette-data 0xFF69)77 (def obj-palette-select 0xFF6A)78 (def obj-palette-data 0xFF6B)80 (def max-palettes 8)82 (defn write-byte [target data]83 (flatten84 [0x3E ;; load literal to A85 data86 0xEA ;; load A into target87 (reverse (disect-bytes-2 target))]))89 (defn begin-sequential-palette-write90 [palette-num palette-select-address]91 (assert (<= 0 palette-num max-palettes))92 (assert93 (or (= palette-select-address bg-palette-select)94 (= palette-select-address obj-palette-select)))95 (let [palette-write-data96 (Integer/parseInt97 (str "1" ;; auto increment98 "0" ;; not used99 (format100 "%03d"101 (Integer/parseInt102 (Integer/toBinaryString palette-num) 10))103 "00" ;; color num104 "0" ;; H/L105 ) 2)]106 (write-byte palette-select-address palette-write-data)))108 (defn set-palettes [palette-select palette-data palettes]109 (assert (<= (count palettes)) max-palettes)110 (flatten111 [(begin-sequential-palette-write 0 palette-select)113 0x21 ;; target address to HL114 (reverse (disect-bytes-2 palette-data))117 (for [palette palettes]118 (map (fn [byte]119 [0x3E ;; literal to A120 byte121 0x77]) ;; A -> (HL)123 (flatten124 (map #(gb-rgb->bits (get palette % [0 0 0]))125 (range 4)))))]))128 (defn display-one-color129 "Displayes a single color onto the gameboy screen. Input rgb in130 gameboy rgb."131 ([state [r g b]]132 ;; construct a kernel that displays a single color133 (let134 [palettes (repeat 8 [[r g b] [r g b] [r g b] [r g b]])135 kernel-address 0xC000136 kernel137 [0xF3 ;; disable interrupts138 (clear-music-registers)139 (frame-metronome)140 ;;(set-palettes141 ;; obj-palette-select obj-palette-data palettes)142 (set-palettes143 bg-palette-select bg-palette-data palettes)144 (infinite-loop)]]145 (-> (set-memory-range state146 kernel-address (flatten kernel))147 (PC! kernel-address))))148 ([[r g b]]149 (display-one-color @current-state [r g b])))151 ;;(require 'cortex.sense)152 (import java.awt.image.BufferedImage)154 ;; (defn show-screenshot []155 ;; (let [im (BufferedImage. 160 144 BufferedImage/TYPE_INT_RGB)156 ;; pix (vec (pixels))157 ;; view (cortex.sense/view-image)]158 ;; (dorun (for [x (range 160) y (range 144)]159 ;; (.setRGB im x y (pix (+ x (* 160 y))))))160 ;; (view im)))162 (defn gb-rgb->vga-rgb [[r g b]]163 (let [vga-rgb164 (first (pixels165 (run-moves166 (display-one-color167 (tick @current-state)168 [r g b])169 [[][]])))]170 [(bit-shift-right (bit-and vga-rgb 0xFF0000) 16)171 (bit-shift-right (bit-and vga-rgb 0xFF00) 8)172 (bit-and vga-rgb 0xFF)]))174 (defn generate-gb-color-map []175 (set-state! (mid-game))176 (let [gb-colors177 (for [r (range 32)178 g (range 32)179 b (range 32)]180 [r g b])]181 (zipmap gb-colors182 (map gb-rgb->vga-rgb183 gb-colors))))185 (import java.io.FileWriter)187 (def gb-color-map-file188 (File. user-home "proj/vba-clojure/gb-color-map"))190 (defn write-gb-color-map! []191 (binding [*out*(FileWriter. gb-color-map-file)]192 (let [out-str193 (.replace194 (str195 (into (sorted-map) (generate-gb-color-map)))196 "," ",\n")]197 (println out-str))))199 (def gb-color-map200 (read-string (slurp gb-color-map-file)))202 (import javax.imageio.stream.FileImageOutputStream)203 (import '(javax.imageio ImageWriteParam IIOImage ImageIO))206 (defn gen-gb-color-image! []207 (let [im (BufferedImage. 68 69 BufferedImage/TYPE_INT_RGB)208 pix (vec210 (reduce211 concat212 (map (partial213 sort-by214 (fn [[r g b]]215 (let [s (max r g b)216 det217 (cond218 (= s r)219 (+ -1000 (- g) b)220 (= s b)221 (+ (- r) g)222 (= s g)223 (+ 1000 (- b) r))]224 det)))225 (partition226 68 68 []227 (sort-by228 (fn euclidean-distance [[r g b]]229 (Math/sqrt (+ (* r r) (* g g) (* b b))))230 (filter231 (fn [[r g b]]232 (= (max r g b) b ))234 (seq (set (vals gb-color-map)))))))))235 ;;view (cortex.sense/view-image)236 target (File. user-home "proj/vba-clojure/gb-color-map-unique.png")]237 (dorun (for [x (range 68) y (range 69)]238 (let [[r g b] (get pix (+ x (* 68 y)) [0 0 0])239 rgb (+ (bit-shift-left r 16)240 (bit-shift-left g 8)241 b)]242 (.setRGB im x y rgb))))243 ;;(view im)244 (doto245 (.next (ImageIO/getImageWritersByFormatName "png"))246 (.setOutput (FileImageOutputStream. target))247 (.write (IIOImage. im nil nil))248 (.dispose))249 im))251 (defn gen-gb-color-image*! []252 (let [im (BufferedImage. 213 213 BufferedImage/TYPE_INT_RGB)253 squares254 (vec255 (for [r (range 32)]256 (vec257 (for [b (range 32) g (range 32)]258 (gb-color-map [r g b])))))259 ;;view (cortex.sense/view-image)260 target (File. user-home "proj/vba-clojure/gb-color-map.png")]262 (dorun263 (for [s-index (range 32)]264 (dorun265 (for [x (range 32) y (range 32)]267 (let [[r g b] ((squares s-index) (+ x (* 32 y)))268 rgb (+ (bit-shift-left r 16)269 (bit-shift-left g 8)270 b)]271 (.setRGB im272 (+ 3 (* 35 (rem s-index 6)) x)273 (+ 3 (* 35 (int (/ s-index 6))) y)274 rgb))))))275 ;;(view im)276 (doto277 (.next (ImageIO/getImageWritersByFormatName "png"))278 (.setOutput (FileImageOutputStream. target))279 (.write (IIOImage. im nil nil))280 (.dispose))281 im))283 (def test-image284 (ImageIO/read285 (File. user-home "/proj/vba-clojure/images/test-gb-image.png")))287 (def test-image-2288 (ImageIO/read289 (File. user-home "/proj/vba-clojure/images/test-gb-image-2.png")))293 (defn rgb->triplet [rgb]294 (let [r (bit-shift-right (bit-and rgb 0xFF0000) 16)295 g (bit-shift-right (bit-and rgb 0xFF00) 8)296 b (bit-and rgb 0xFF)]297 [r g b]))299 (def reverse-gb-color-map300 (zipmap (vals gb-color-map)301 (keys gb-color-map)))303 (defn vga-rgb->gb-rgb [[r g b]]304 (reverse-gb-color-map [r g b]))306 (defn gb-tiles [^BufferedImage image]307 (for [tile (range 360)]308 (for [y (range 8) x (range 8)]309 (vga-rgb->gb-rgb310 (rgb->triplet311 (.getRGB image (+ x (* 8 (rem tile 20)))312 (+ y (* 8 (int (/ tile 20))))))))))314 (defn tile->palette [tile]315 (vec (sort (set tile))))317 (require 'clojure.set)319 (defn absorb-contract [objs]320 (reduce321 (fn [accepted new-element]322 (if (some323 (fn [obj]324 (clojure.set/subset? (set new-element) (set obj)))325 accepted)326 accepted327 (conj accepted new-element)))328 []329 (sort-by (comp - count) objs)))331 (defn palettes [^BufferedImage image]332 (let [palettes (map tile->palette (gb-tiles image))333 unique-palettes (absorb-contract (set palettes))]334 unique-palettes))336 (defn tile-pallete337 "find the first appropirate palette for the tile in the338 provided list of palettes."339 [tile palettes]340 (let [tile-colors (set tile)]341 (swank.util/find-first342 #(clojure.set/subset? tile-colors (set %))343 palettes)))346 (defn image->gb-image347 "Returns the image in a format amenable to the gameboy's348 internal representation. The format is:349 {:width -- width of the image350 :height -- height of the image351 :palettes -- vector of all the palettes the image352 needs, in proper order353 :tiles -- vector of all the tiles the image needs,354 in proper order. A tile is 64 palette355 indices.356 :data -- vector of pairs of the format:357 [tile-index, palette-index]358 in row-oriented order}"359 [^BufferedImage image]360 (let [image-palettes (palettes image)361 palette-index (zipmap362 image-palettes363 (range (count image-palettes)))364 tiles (gb-tiles image)365 unique-tiles (vec (distinct tiles))366 tile-index (zipmap unique-tiles367 (range (count unique-tiles)))]368 {:width (.getWidth image)369 :height (.getHeight image)370 :palettes image-palettes371 :tiles372 (vec373 (for [tile unique-tiles]374 (let [colors375 (vec (tile-pallete tile image-palettes))376 color-index377 (zipmap colors (range (count colors)))]378 (mapv color-index tile))))379 :data380 (vec381 (for [tile tiles]382 (let [tile-colors (set (tile->palette tile))]383 [(tile-index tile)384 (palette-index385 (tile-pallete tile image-palettes))])))}))387 (defn wait-until-v-blank388 "Modified version of frame-metronome. waits untill LY == 144,389 indicating start of v-blank period."390 []391 (let [timing-loop392 [0x01 ; \393 0x44 ; | load 0xFF44 into BC394 0xFF ; /395 0x0A] ;; (BC) -> A, now A = LY (vertical line coord)396 continue-if-144397 [0xFE398 144 ;; compare LY (in A) with 144399 0x20 ;; jump back to beginning if LY != 144 (not-v-blank)400 (->signed-8-bit401 (+ -4 (- (count timing-loop))))]]402 (concat timing-loop continue-if-144)))404 (def bg-character-data 0x9000)406 (defn gb-tile->bytes407 "Tile is a vector of 64 numbers between 0 and 3 that408 represent a single 8x8 color tile in the GB screen.409 It gets bit-packed into to 16 8-bit numbers in the following410 form:412 0-low 1-low ... 7-low413 0-high 1-high ... 7-high414 .415 .416 .417 55-low ........ 63-low418 55-high ........ 63-high"419 [tile]420 (let [row->bits421 (fn [row]422 (mapv423 (fn [row*]424 (Integer/parseInt (apply str row*) 2))425 [(map #(bit-and 0x01 %) row)426 (map #(bit-shift-right (bit-and 0x02 %) 1)427 row)]))]428 (vec429 (flatten430 (map row->bits431 (partition 8 tile))))))433 (defn write-data434 "Efficient assembly to write a sequence of values to435 memory, starting at a target address."436 [base-address target-address data]437 (let [len (count data)438 program-length 21] ;; change this if program length439 ;; below changes!440 (flatten441 [0x21 ;; load data address start into HL442 (reverse (disect-bytes-2 (+ base-address program-length)))444 0x01 ;; load target address into BC445 (reverse (disect-bytes-2 target-address))447 0x11 ;; load len into DE448 (reverse (disect-bytes-2 len))451 ;; data x-fer loop start452 0x2A ;; (HL) -> A; HL++;453 0x02 ;; A -> (BC);454 0x03 ;; INC BC;455 0x1B ;; DEC DE457 0xAF458 0xB2 ;; (OR D E) -> A459 0xB3462 0x20 ;; if DE is not now 0,463 (->signed-8-bit -9) ;; GOTO start465 0xC3466 (reverse467 (disect-bytes-2468 (+ len base-address program-length)))469 data])))471 (defn write-image472 "Assume the image data is specified as 360 blocks."473 [base-address target-address image-data]475 (let [len (count image-data)476 gen-program477 (fn [program-length]478 (flatten479 [0x01 ;; load data address start into BC480 (reverse481 (disect-bytes-2 (+ base-address program-length)))483 0x21 ;; load target address into HL484 (reverse (disect-bytes-2 target-address))486 0x1E ;; total-rows (18) -> E487 18489 0x16 ;; total columns (20) -> D490 20492 ;; data x-fer loop start493 0x0A ;; (BC) -> A;494 0x03 ;; INC BC;495 0x22 ;; A -> (HL); HL++;499 0x15 ;; dec D500 0x20501 (->signed-8-bit -6) ;; continue writing row503 ;; row is complete, advance to next row504 ;; HL += 12506 0xC5 ;; push BC508 0x06 ;; 0 -> B509 0511 0x0E512 12 ;; 12 -> C514 0x09 ;; HL + BC -> HL516 0xC1 ;; pop BC518 0x1D ;; dec E519 0x20520 (->signed-8-bit -18) ;; contunue writing image522 0xC3523 (reverse524 (disect-bytes-2525 (+ len base-address program-length)))]))]526 (flatten (concat527 (gen-program (count (gen-program 0)))528 image-data))))530 (defn test-write-data []531 (let [test-data (concat (range 256)532 (reverse (range 256)))533 base-address 0xC000534 target-address 0xD000536 test-kernel537 (flatten538 [0xF3 ;; disable interrupts539 (write-data (+ 1 base-address)540 target-address test-data)541 (infinite-loop)])]542 (assert543 (= test-data544 (-> (mid-game)545 tick tick tick546 (set-memory-range base-address test-kernel)547 (PC! base-address)548 (run-moves (repeat 100 []))549 (memory)550 vec551 (subvec target-address552 (+ target-address553 (count test-data))))))))555 (def LCD-bank-select-address 0xFF4F)557 (def BG-1-address 0x9800)558 (def BG-2-address 0x9C00)559 (def character-data-address 0x8000)561 (def LCD-control-register 0xFF40)562 (def STAT-register 0xFF41)564 (def SCX-register 0xFF42)565 (def SCY-register 0xFF43)567 (defn select-LCD-bank [n]568 (assert (or (= n 0) (= n 1)))569 (write-byte LCD-bank-select-address n))571 (defn write-image* [_ _ _] [])573 (defn display-image-kernel [base-address ^BufferedImage image]574 (let [gb-image (image->gb-image image)576 A [(clear-music-registers)578 ;; [X] disable LCD protection circuit.579 (write-byte LCD-control-register 0x00)580 ;; now we can write to all video RAM anytime with581 ;; impunity.583 ;; [ ] We're only using background palettes; just set the584 ;; minimum required bg palettes for this image, starting585 ;; with palette #0.587 (set-palettes bg-palette-select bg-palette-data588 (:palettes gb-image))590 ;; [X] switch to bank 0 to set BG character data.591 (select-LCD-bank 0)592 ;; [X] set SCX and SCY to 0593 (write-byte SCX-register 0)594 (write-byte SCY-register 0)595 ]596 A (flatten A)598 B [;; [X] write minimum amount of tiles to BG character599 ;; section600 (write-data601 (+ base-address (count A))602 character-data-address603 (flatten604 (map gb-tile->bytes (:tiles gb-image))))]605 B (flatten B)608 C [;; [ ] write image to the screen in terms of tiles609 (write-image610 (+ base-address (+ (count A) (count B)))611 BG-1-address612 (map first (:data gb-image)))]614 C (flatten C)616 D [;; [ ] specifiy pallets for each character617 (select-LCD-bank 1)618 ;;(write-image619 ;; (+ base-address (+ (count A) (count B) (count C)))620 ;; BG-1-address621 ;;(map second (:data gb-image)))624 ;; [X] reactivate the LCD display625 ;; we're using only BG images, located at626 ;; BG-1 (0x9800), with background character data627 ;; stored starting at 0x8000629 (write-byte630 LCD-control-register631 (Integer/parseInt632 (str633 "1" ;; LCDC on/off634 "0" ;; Window code area635 "0" ;; Windowing on?636 "1" ;; BG tile base (1 = 0x8000)637 "0" ;; BG-1 or BG-2 ?638 "0" ;; OBJ-block composition639 "0" ;; OBJ-on flag640 "1") ;; no-effect641 2))643 (infinite-loop)]644 D (flatten D)]646 (concat A B C D)))648 (defn display-image [#^BufferedImage image]649 (let [kernel-address 0xB000]650 (-> (tick (tick (tick (mid-game))))651 (set-memory-range652 kernel-address653 (display-image-kernel kernel-address image))654 (PC! kernel-address))))