annotate clojure/com/aurellem/run/image.clj @ 488:09b3bc0b71b5

added rgb->gbRGB translation code.
author Robert McIntyre <rlm@mit.edu>
date Mon, 07 May 2012 12:31:16 -0500
parents 3f0156038802
children 641e1c511224 2304906d443b
rev   line source
rlm@488 1 (ns com.aurellem.run.image
rlm@486 2 (:use (com.aurellem.gb saves gb-driver util constants
rlm@486 3 items vbm characters money
rlm@486 4 rlm-assembly))
rlm@486 5 (:use (com.aurellem.run util title save-corruption
rlm@486 6 bootstrap-0 bootstrap-1))
rlm@486 7 (:require clojure.string)
rlm@486 8 (:import [com.aurellem.gb.gb_driver SaveState])
rlm@486 9 (:import java.io.File))
rlm@486 10
rlm@486 11 ;; want to display an image onto the screen.
rlm@486 12 ;; probably will be the six ponies, possibly with scrolling.
rlm@486 13
rlm@486 14 ;; probably don't need hi-color mode since the images shuld be
rlm@486 15 ;; simple.
rlm@486 16
rlm@486 17 ;; use background tiles? they provide greater color depth than
rlm@486 18 ;; sprites, and can still be scrolled, so why not?
rlm@486 19
rlm@486 20
rlm@486 21 ;; First of all, RGB colors in an image are not the same as those in a
rlm@486 22 ;; GameBoy, so I need to convert them. Fortunately, this code is
rlm@486 23 ;; already written for me in this C-code from the public domain
rlm@486 24 ;; hi-color converter by Glen Cook, Jeff Frohwein, and Rob Jones.
rlm@486 25
rlm@486 26 ;; the code snipped itself is by Brett Bibby and is translated here
rlm@486 27 ;; from C into clojure.
rlm@486 28
rlm@486 29
rlm@488 30 ;; This section of code is used to convert an RGB (pc) triplet into
rlm@488 31 ;; a RGB (gameboy) triplet. This section of code was kindly donated
rlm@488 32 ;; by Brett Bibby (GameBrains).
rlm@486 33
rlm@488 34 ;; BYTE intensity[32] = {
rlm@488 35 ;; 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x5e, 0x6c, 0x7a, 0x88, 0x94,
rlm@488 36 ;; 0xa0, 0xae, 0xb7, 0xbf, 0xc6, 0xce, 0xd3, 0xd9, 0xdf, 0xe3, 0xe7,
rlm@488 37 ;; 0xeb, 0xef, 0xf3, 0xf6, 0xf9, 0xfb, 0xfd, 0xfe, 0xff, 0xff };
rlm@488 38
rlm@488 39 ;; unsigned char influence[3][3] =
rlm@488 40 ;; {
rlm@488 41 ;; {16,4,4},
rlm@488 42 ;; {8,16,8},
rlm@488 43 ;; {0,8,16}
rlm@488 44 ;; };
rlm@488 45
rlm@488 46 ;; RGBQUAD translate(BYTE rgb[3])
rlm@488 47 ;; {
rlm@488 48 ;; RGBQUAD color;
rlm@488 49 ;; BYTE tmp[3];
rlm@488 50 ;; BYTE m[3][3];
rlm@488 51 ;; BYTE i,j;
rlm@488 52
rlm@488 53 ;; for (i=0;i<3;i++)
rlm@488 54 ;; for (j=0;j<3;j++)
rlm@488 55 ;; m[i][j] = (intensity[rgb[i]>>3]*influence[i][j]) >> 5;
rlm@488 56
rlm@488 57 ;; for (i=0;i<3;i++)
rlm@488 58 ;; {
rlm@488 59 ;; if (m[0][i]>m[1][i])
rlm@488 60 ;; {
rlm@488 61 ;; j=m[0][i];
rlm@488 62 ;; m[0][i]=m[1][i];
rlm@488 63 ;; m[1][i]=j;
rlm@488 64 ;; }
rlm@488 65
rlm@488 66 ;; if (m[1][i]>m[2][i])
rlm@488 67 ;; {
rlm@488 68 ;; j=m[1][i];
rlm@488 69 ;; m[1][i]=m[2][i];
rlm@488 70 ;; m[2][i]=j;
rlm@488 71 ;; }
rlm@488 72
rlm@488 73 ;; if (m[0][i]>m[1][i])
rlm@488 74 ;; {
rlm@488 75 ;; j=m[0][i];
rlm@488 76 ;; m[0][i]=m[1][i];
rlm@488 77 ;; m[1][i]=j;
rlm@488 78 ;; }
rlm@488 79
rlm@488 80 ;; tmp[i]=(((m[0][i]+m[1][i]*2+m[2][i]*4)*5) >> 4)+32;
rlm@488 81 ;; }
rlm@488 82
rlm@488 83 ;; color.rgbRed = tmp[0];
rlm@488 84 ;; color.rgbGreen = tmp[1];
rlm@488 85 ;; color.rgbBlue = tmp[2];
rlm@488 86
rlm@488 87 ;; return color;
rlm@488 88 ;; }
rlm@488 89
rlm@488 90
rlm@488 91 (def intensity
rlm@488 92 [0x00 0x10 0x20 0x30 0x40 0x50 0x5e 0x6c 0x7a 0x88 0x94
rlm@488 93 0xa0 0xae 0xb7 0xbf 0xc6 0xce 0xd3 0xd9 0xdf 0xe3 0xe7
rlm@488 94 0xeb 0xef 0xf3 0xf6 0xf9 0xfb 0xfd 0xfe 0xff 0xff])
rlm@488 95
rlm@488 96 (def influence
rlm@488 97 [[16 4 4]
rlm@488 98 [8 16 8]
rlm@488 99 [0 8 16]])
rlm@486 100
rlm@486 101 (defn rgb->gb-rb [[r g b]]
rlm@488 102 (let [color-matrix
rlm@488 103 (map
rlm@488 104 (fn [color-row]
rlm@488 105 (map
rlm@488 106 (fn [color]
rlm@488 107 (bit-shift-right
rlm@488 108 (* (intensity (bit-shift-right r 3))
rlm@488 109 color) 5)))))]))
rlm@488 110
rlm@488 111