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.image
2 (:use (com.aurellem.gb saves gb-driver util constants
3 items vbm characters money
4 rlm-assembly))
5 (:use (com.aurellem.run util music title save-corruption
6 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 be
15 ;; simple.
17 ;; use background tiles? they provide greater color depth than
18 ;; sprites, and can still be scrolled, so why not?
20 ;; could also use sprites to get 3 more colors per tile for a total of
21 ;; 7 colors per tile, although not for all tiles...
25 ;; want a function to
27 ;; 1. read an image
28 ;; 2. split into a grid of 8x8 pixels
29 ;; 3. convert all RGB colors to gb-RGB colors
30 ;; 4. determine efficient color palletes for the image
31 ;; 5. output efficient assembly code to draw the image to the gb
32 ;; 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-and
76 0xFF
77 (+
78 r
79 (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 (flatten
95 [0x3E ;; load literal to A
96 data
97 0xEA ;; load A into target
98 (reverse (disect-bytes-2 target))]))
100 (defn begin-sequential-palette-write
101 [palette-num palette-select-address]
102 (assert (<= 0 palette-num max-palettes))
103 (assert
104 (or (= palette-select-address bg-palette-select)
105 (= palette-select-address obj-palette-select)))
106 (let [palette-write-data
107 (Integer/parseInt
108 (str "1" ;; auto increment
109 "0" ;; not used
110 (format
111 "%03d"
112 (Integer/parseInt
113 (Integer/toBinaryString palette-num) 10))
114 "00" ;; color num
115 "0" ;; H/L
116 ) 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 (flatten
122 [(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-color
127 "Displayes a single color onto the gameboy screen. input rgb in
128 gameboy rgb."
129 <<<<<<< local
130 [[r g b]]
131 ;; construct a kernel that displays a single color
132 (let
133 [palettes (repeat 8 [r g b])
134 kernel-address 0xC000
135 kernel
136 [0xF3 ;; disable interrupts
137 (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 color
148 (let
149 [palettes (repeat 8 [r g b])
150 kernel-address 0xC000
151 kernel
152 [0xF3 ;; disable interrupts
153 (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 state
159 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-rgb
177 (first (pixels
178 (run-moves
179 (display-one-color
180 (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-colors
190 (for [r (range 32)
191 g (range 32)
192 b (range 32)]
193 [r g b])]
194 (zipmap gb-colors
195 (map gb-rgb->vga-rgb
196 gb-colors))))
198 (import java.io.FileWriter)
200 (def gb-color-map-file
201 (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-str
206 (.replace
207 (str
208 (into (sorted-map) (generate-gb-color-map)))
209 "," ",\n")]
210 (println out-str))))
212 (def gb-color-map
213 (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 (vec
223 (reduce
224 concat
225 (map (partial
226 sort-by
227 (fn [[r g b]]
228 (let [s (max r g b)
229 det
230 (cond
231 (= s r)
232 (+ -1000 (- g) b)
233 (= s b)
234 (+ (- r) g)
235 (= s g)
236 (+ 1000 (- b) r))]
237 det)))
238 (partition
239 68 68 []
240 (sort-by
241 (fn euclidean-distance [[r g b]]
242 (Math/sqrt (+ (* r r) (* g g) (* b b))))
243 (filter
244 (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 (doto
258 (.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 squares
267 (vec
268 (for [r (range 32)]
269 (vec
270 (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 (dorun
276 (for [s-index (range 32)]
277 (dorun
278 (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 im
285 (+ 3 (* 35 (rem s-index 6)) x)
286 (+ 3 (* 35 (int (/ s-index 6))) y)
287 rgb))))))
288 (view im)
289 (doto
290 (.next (ImageIO/getImageWritersByFormatName "png"))
291 (.setOutput (FileImageOutputStream. target))
292 (.write (IIOImage. im nil nil))
293 (.dispose))
294 im))
297 (count
298 (filter
299 (fn [[r g b]]
300 (= (max r g b) r ))(set (vals gb-color-map))))
302 (def test-image
303 (ImageIO/read
304 (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-map
313 (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-rgb
323 (rgb->triplet
324 (.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 (reduce
334 (fn [accepted new-element]
335 (if (some
336 (fn [obj]
337 (clojure.set/subset? (set new-element) (set obj)))
338 accepted)
339 accepted
340 (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]