comparison clojure/com/aurellem/run/image.clj @ 506:130ba9f49db5

added function to slice up an image into an appropriate representation for the GB.
author Robert McIntyre <rlm@mit.edu>
date Wed, 20 Jun 2012 16:50:36 -0500
parents f992a0a0480d
children 24b459a95b46
comparison
equal deleted inserted replaced
505:f992a0a0480d 506:130ba9f49db5
306 (rgb->triplet 306 (rgb->triplet
307 (.getRGB image (+ x (* 8 (rem tile 20))) 307 (.getRGB image (+ x (* 8 (rem tile 20)))
308 (+ y (* 8 (int (/ tile 20)))))))))) 308 (+ y (* 8 (int (/ tile 20))))))))))
309 309
310 (defn tile->palette [tile] 310 (defn tile->palette [tile]
311 (sort (set tile))) 311 (vec (sort (set tile))))
312 312
313 (require 'clojure.set) 313 (require 'clojure.set)
314 314
315 (defn absorb-contract [objs] 315 (defn absorb-contract [objs]
316 (reduce 316 (reduce
330 330
331 (defn palettes [^BufferedImage image] 331 (defn palettes [^BufferedImage image]
332 (let [palettes (map tile->palette (gb-tiles image)) 332 (let [palettes (map tile->palette (gb-tiles image))
333 unique-palettes (absorb-contract (set palettes))] 333 unique-palettes (absorb-contract (set palettes))]
334 unique-palettes)) 334 unique-palettes))
335
336 (defn tile-pallete
337 "find the first appropirate palette for the tile in the
338 provided list of palettes."
339 [tile palettes]
340 (let [tile-colors (set tile)]
341 (swank.util/find-first
342 #(clojure.set/subset? tile-colors (set %))
343 palettes)))
344
345
346 (defn image->gb-image
347 "Returns the image in a format amenable to the gameboy's
348 internal representation. The format is:
349 {:width -- width of the image
350 :height -- height of the image
351 :palettes -- vector of all the palettes the image
352 needs, in proper order
353 :tiles -- vector of all the tiles the image needs,
354 in proper order. A tile is 64 palette
355 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 (zipmap
362 image-palettes
363 (range (count image-palettes)))
364 tiles (gb-tiles image)
365 unique-tiles (vec (distinct tiles))
366 tile-index (zipmap unique-tiles
367 (range (count unique-tiles)))]
368 {:width (.getWidth image)
369 :height (.getHeight image)
370 :palettes image-palettes
371 :tiles
372 (vec
373 (for [tile unique-tiles]
374 (let [colors
375 (vec (tile-pallete tile image-palettes))
376 color-index
377 (zipmap colors (range (count colors)))]
378 (mapv color-index tile))))
379 :data
380 (vec
381 (for [tile tiles]
382 (let [tile-colors (set (tile->palette tile))]
383 [(tile-index tile)
384 (palette-index
385 (tile-pallete tile image-palettes))])))}))
386
387
388
389
390
391
392
393
394
395
396 )
335 397
336 (defn wait-until-v-blank 398 (defn wait-until-v-blank
337 "Modified version of frame-metronome. waits untill LY == 144, 399 "Modified version of frame-metronome. waits untill LY == 144,
338 indicating start of v-blank period." 400 indicating start of v-blank period."
339 [] 401 []