Mercurial > vba-clojure
view clojure/com/aurellem/run/image.clj @ 504:81e43f0350db
resolved conflict.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Mon, 18 Jun 2012 18:56:49 -0500 |
parents | 4703b74f1fb1 |
children | f992a0a0480d |
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.46 (def image-program-target 0xB000)48 (def display-width 160)49 (def display-height 144)53 ;{:r :g :b }55 (def character-data 0x8000)56 (def character-data-end 0x97FF)61 (def BG-data-1 0x9800)63 (def BG-data-2 0x9C00)65 (def OAM 0xFE00)69 (def video-bank-select-register 0xFF4F)71 (defn gb-rgb->bits [[r g b]]72 (assert (<= 0 r 31))73 (assert (<= 0 g 31))74 (assert (<= 0 b 31))75 [(bit-and76 0xFF77 (+78 r79 (bit-shift-left g 5)))80 (+81 (bit-shift-right g 3)82 (bit-shift-left b 2))])85 (def bg-palette-select 0xFF68)86 (def bg-palette-data 0xFF69)88 (def obj-palette-select 0xFF6A)89 (def obj-palette-data 0xFF6B)91 (def max-palettes 8)93 (defn write-data [target data]94 (flatten95 [0x3E ;; load literal to A96 data97 0xEA ;; load A into target98 (reverse (disect-bytes-2 target))]))100 (defn begin-sequential-palette-write101 [palette-num palette-select-address]102 (assert (<= 0 palette-num max-palettes))103 (assert104 (or (= palette-select-address bg-palette-select)105 (= palette-select-address obj-palette-select)))106 (let [palette-write-data107 (Integer/parseInt108 (str "1" ;; auto increment109 "0" ;; not used110 (format111 "%03d"112 (Integer/parseInt113 (Integer/toBinaryString palette-num) 10))114 "00" ;; color num115 "0" ;; H/L116 ) 2)]117 (write-data palette-select-address palette-write-data)))119 (defn set-palettes [palette-select palette-data palettes]120 (assert (<= (count palettes)) max-palettes)121 (flatten122 [(begin-sequential-palette-write 0 palette-select)123 (map (partial write-data palette-data)124 (flatten (map gb-rgb->bits palettes)))]))126 (defn display-one-color127 "Displayes a single color onto the gameboy screen. input rgb in128 gameboy rgb."129 <<<<<<< local130 [[r g b]]131 ;; construct a kernel that displays a single color132 (let133 [palettes (repeat 8 [r g b])134 kernel-address 0xC000135 kernel136 [0xF3 ;; disable interrupts137 (clear-music-registers)138 (frame-metronome)139 (set-palettes obj-palette-select obj-palette-data palettes)140 (set-palettes bg-palette-select bg-palette-data palettes)141 (infinite-loop)]]142 (-> (set-memory-range (second (music-base))143 kernel-address (flatten kernel))144 (PC! kernel-address))))145 =======146 ([state [r g b]]147 ;; construct a kernel that displays a single color148 (let149 [palettes (repeat 8 [r g b])150 kernel-address 0xC000151 kernel152 [0xF3 ;; disable interrupts153 (clear-music-registers)154 (frame-metronome)155 (set-palettes obj-palette-select obj-palette-data palettes)156 (set-palettes bg-palette-select bg-palette-data palettes)157 (infinite-loop)]]158 (-> (set-memory-range state159 kernel-address (flatten kernel))160 (PC! kernel-address))))161 ([[r g b]]162 (display-one-color @current-state [r g b])))164 (require 'cortex.sense)165 (import java.awt.image.BufferedImage)167 (defn show-screenshot []168 (let [im (BufferedImage. 160 144 BufferedImage/TYPE_INT_RGB)169 pix (vec (pixels))170 view (cortex.sense/view-image)]171 (dorun (for [x (range 160) y (range 144)]172 (.setRGB im x y (pix (+ x (* 160 y))))))173 (view im)))175 (defn gb-rgb->vga-rgb [[r g b]]176 (let [vga-rgb177 (first (pixels178 (run-moves179 (display-one-color180 (tick @current-state)181 [r g b])182 [[][]])))]183 [(bit-shift-right (bit-and vga-rgb 0xFF0000) 16)184 (bit-shift-right (bit-and vga-rgb 0xFF00) 8)185 (bit-and vga-rgb 0xFF)]))187 (defn generate-gb-color-map []188 (set-state! (mid-game))189 (let [gb-colors190 (for [r (range 32)191 g (range 32)192 b (range 32)]193 [r g b])]194 (zipmap gb-colors195 (map gb-rgb->vga-rgb196 gb-colors))))198 (import java.io.FileWriter)200 (def gb-color-map-file201 (File. user-home "proj/vba-clojure/gb-color-map"))203 (defn write-gb-color-map! []204 (binding [*out*(FileWriter. gb-color-map-file)]205 (let [out-str206 (.replace207 (str208 (into (sorted-map) (generate-gb-color-map)))209 "," ",\n")]210 (println out-str))))212 (def gb-color-map213 (read-string (slurp gb-color-map-file)))215 (import javax.imageio.stream.FileImageOutputStream)216 (import '(javax.imageio ImageWriteParam IIOImage ImageIO))219 (defn gen-gb-color-image! []220 (let [im (BufferedImage. 68 69 BufferedImage/TYPE_INT_RGB)221 pix (vec223 (reduce224 concat225 (map (partial226 sort-by227 (fn [[r g b]]228 (let [s (max r g b)229 det230 (cond231 (= s r)232 (+ -1000 (- g) b)233 (= s b)234 (+ (- r) g)235 (= s g)236 (+ 1000 (- b) r))]237 det)))238 (partition239 68 68 []240 (sort-by241 (fn euclidean-distance [[r g b]]242 (Math/sqrt (+ (* r r) (* g g) (* b b))))243 (filter244 (fn [[r g b]]245 (= (max r g b) b ))247 (seq (set (vals gb-color-map)))))))))248 view (cortex.sense/view-image)249 target (File. user-home "proj/vba-clojure/gb-color-map-unique.png")]250 (dorun (for [x (range 68) y (range 69)]251 (let [[r g b] (get pix (+ x (* 68 y)) [0 0 0])252 rgb (+ (bit-shift-left r 16)253 (bit-shift-left g 8)254 b)]255 (.setRGB im x y rgb))))256 (view im)257 (doto258 (.next (ImageIO/getImageWritersByFormatName "png"))259 (.setOutput (FileImageOutputStream. target))260 (.write (IIOImage. im nil nil))261 (.dispose))262 im))264 (defn gen-gb-color-image*! []265 (let [im (BufferedImage. 213 213 BufferedImage/TYPE_INT_RGB)266 squares267 (vec268 (for [r (range 32)]269 (vec270 (for [b (range 32) g (range 32)]271 (gb-color-map [r g b])))))272 view (cortex.sense/view-image)273 target (File. user-home "proj/vba-clojure/gb-color-map.png")]275 (dorun276 (for [s-index (range 32)]277 (dorun278 (for [x (range 32) y (range 32)]280 (let [[r g b] ((squares s-index) (+ x (* 32 y)))281 rgb (+ (bit-shift-left r 16)282 (bit-shift-left g 8)283 b)]284 (.setRGB im285 (+ 3 (* 35 (rem s-index 6)) x)286 (+ 3 (* 35 (int (/ s-index 6))) y)287 rgb))))))288 (view im)289 (doto290 (.next (ImageIO/getImageWritersByFormatName "png"))291 (.setOutput (FileImageOutputStream. target))292 (.write (IIOImage. im nil nil))293 (.dispose))294 im))297 (count298 (filter299 (fn [[r g b]]300 (= (max r g b) r ))(set (vals gb-color-map))))302 (def test-image303 (ImageIO/read304 (File. user-home "/proj/vba-clojure/images/test-gb-image.png")))306 (defn rgb->triplet [rgb]307 (let [r (bit-shift-right (bit-and rgb 0xFF0000) 16)308 g (bit-shift-right (bit-and rgb 0xFF00) 8)309 b (bit-and rgb 0xFF)]310 [r g b]))312 (def reverse-gb-color-map313 (zipmap (vals gb-color-map)314 (keys gb-color-map)))316 (defn vga-rgb->gb-rgb [[r g b]]317 (reverse-gb-color-map [r g b]))319 (defn gb-tiles [^BufferedImage image]320 (for [tile (range 360)]321 (for [x (range 8) y (range 8)]322 (vga-rgb->gb-rgb323 (rgb->triplet324 (.getRGB image (+ x (* 8 (rem tile 20)))325 (+ y (* 8 (int (/ tile 20))))))))))327 (defn tile->palette [tile]328 (sort (set tile)))330 (require 'clojure.set)332 (defn absorb-contract [objs]333 (reduce334 (fn [accepted new-element]335 (if (some336 (fn [obj]337 (clojure.set/subset? (set new-element) (set obj)))338 accepted)339 accepted340 (conj accepted new-element)))341 []342 (sort-by (comp - count) objs)))344 (defn absorb-combine-4 [objs]348 )350 (defn palettes [^BufferedImage image]351 (let [palettes (map tile->palette (gb-tiles image))352 unique-palettes (absorb-contract (set palettes))]353 unique-palettes))362 (defn display-image-kernel [^BufferedImage image]366 )