Skip to content

Commit

Permalink
Merge 037c3e9 into a0d13d9
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Feb 7, 2016
2 parents a0d13d9 + 037c3e9 commit c23844b
Show file tree
Hide file tree
Showing 19 changed files with 75,307 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Cask
Expand Up @@ -7,7 +7,8 @@
("maps" "maps/*.txt")
("signs" "signs/*.txt")
("sprites" "sprites/*.json")
("sprites" "sprites/*.xpm"))
("sprites" "sprites/*.xpm")
("sprites" "sprites/*.txt"))

(development
(depends-on "el-mock")
Expand Down
51 changes: 51 additions & 0 deletions pacmacs-image.el
Expand Up @@ -32,6 +32,9 @@

;;; Code:

(require 'f)
(require 'color)

(require 'pacmacs-utils)

(defun pacmacs--make-image-data (width height)
Expand Down Expand Up @@ -128,6 +131,54 @@
(aset (aref canvas-data canvas-y) canvas-x
image-color))))))))


(defun pacmacs--parse-im-enum-header (header)
(let ((header-regexp (concat "# ImageMagick pixel enumeration: "
"\\([[:digit:]]+\\),\\([[:digit:]]+\\),"
"\\([[:digit:]]+\\)")))
(if (string-match header-regexp header)
(list (string-to-number (match-string 1 header))
(string-to-number (match-string 2 header))
(string-to-number (match-string 3 header)))
(error "Incorrect ImageMagick pixel enumeration header: %s" header))))

(defun pacmacs--match-int (num s)
(string-to-number (match-string num s)))

(defun pacmacs--parse-im-enum-pixel (pixel colorspace)
(let ((pixel-regexp (concat "\\([[:digit:]]+\\),\\([[:digit:]]+\\):[[:space:]]*"
"([[:space:]]*\\([[:digit:]]+\\),"
"[[:space:]]*\\([[:digit:]]+\\),"
"[[:space:]]*\\([[:digit:]]+\\),"
"[[:space:]]*\\([[:digit:]]+\\))")))
(if (string-match pixel-regexp pixel)
(list (string-to-number (match-string 1 pixel))
(string-to-number (match-string 2 pixel))
(let ((r (pacmacs--match-int 3 pixel))
(g (pacmacs--match-int 4 pixel))
(b (pacmacs--match-int 5 pixel))
(a (pacmacs--match-int 6 pixel))
(k (/ 1.0 colorspace)))
(if (zerop a)
nil
(color-rgb-to-hex (* r k)
(* g k)
(* b k)))))
(error "Incorrect ImageMagick pixel enumeration pixel: %s" pixel))))

(defun pacmacs--read-im-enum-image (filename)
(-let* (((header-raw . data-raw) (-> filename
(f-read-text)
(split-string "\n" t)))
(header (pacmacs--parse-im-enum-header header-raw)))
(-let (((width height colorspace) header))
(let ((image (pacmacs--make-image width height)))
(-each data-raw
(-lambda (pixel-raw)
(-let (((x y color) (pacmacs--parse-im-enum-pixel pixel-raw colorspace)))
(pacmacs--set-image-pixel image x y color))))
image))))

(defun pacmacs--image-to-xpm (image)
(plist-bind ((width :width)
(height :height)
Expand Down
3,201 changes: 3,201 additions & 0 deletions sprites/Big-Pill.txt

Large diffs are not rendered by default.

6,401 changes: 6,401 additions & 0 deletions sprites/Blinking-Terrified-Ghost.txt

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions sprites/Makefile
@@ -1,10 +1,14 @@
PILL=Pill.json Pill.xpm
IM_ENUMS=Big-Pill.txt Blinking-Terrified-Ghost.txt Pacman-Chomping-Down.txt Pacman-Chomping-Left.txt Pacman-Chomping-Right.txt Pacman-Chomping-Up.txt Pacman-Death.txt Pill.txt Red-Ghost-Down.txt Red-Ghost-Left.txt Red-Ghost-Right.txt Red-Ghost-Up.txt Red-Ghost-Win.txt Terrified-Ghost.txt
PILL=Pill.json Pill.xpm Pill.txt
BIG_PILL=Big-Pill.json Big-Pill.xpm
PACMAN_DEATH=Pacman-Death.json Pacman-Death.xpm
TERRIFIED_GHOST=Terrified-Ghost.json Terrified-Ghost.xpm
BLINKING_TERRIFIED_GHOST=Blinking-Terrified-Ghost.json Blinking-Terrified-Ghost.xpm

all: $(PILL) $(BIG_PILL) $(PACMAN_DEATH) $(TERRIFIED_GHOST) $(BLINKING_TERRIFIED_GHOST) Makefile
all: $(IM_ENUMS) $(PILL) $(BIG_PILL) $(PACMAN_DEATH) $(TERRIFIED_GHOST) $(BLINKING_TERRIFIED_GHOST) Makefile

%.txt: %.xpm
convert $< $@

Pill.json: Pill.raw.json
jq ".meta.image |= \"Pill.xpm\"" Pill.raw.json > Pill.json
Expand Down
6,401 changes: 6,401 additions & 0 deletions sprites/Pacman-Chomping-Down.txt

Large diffs are not rendered by default.

6,401 changes: 6,401 additions & 0 deletions sprites/Pacman-Chomping-Left.txt

Large diffs are not rendered by default.

6,401 changes: 6,401 additions & 0 deletions sprites/Pacman-Chomping-Right.txt

Large diffs are not rendered by default.

6,401 changes: 6,401 additions & 0 deletions sprites/Pacman-Chomping-Up.txt

Large diffs are not rendered by default.

17,601 changes: 17,601 additions & 0 deletions sprites/Pacman-Death.txt

Large diffs are not rendered by default.

3,201 changes: 3,201 additions & 0 deletions sprites/Pill.txt

Large diffs are not rendered by default.

3,201 changes: 3,201 additions & 0 deletions sprites/Red-Ghost-Down.txt

Large diffs are not rendered by default.

3,201 changes: 3,201 additions & 0 deletions sprites/Red-Ghost-Left.txt

Large diffs are not rendered by default.

3,201 changes: 3,201 additions & 0 deletions sprites/Red-Ghost-Right.txt

Large diffs are not rendered by default.

3,201 changes: 3,201 additions & 0 deletions sprites/Red-Ghost-Up.txt

Large diffs are not rendered by default.

3,201 changes: 3,201 additions & 0 deletions sprites/Red-Ghost-Win.txt

Large diffs are not rendered by default.

3,201 changes: 3,201 additions & 0 deletions sprites/Terrified-Ghost.txt

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions test-data/2x2.txt
@@ -0,0 +1,5 @@
# ImageMagick pixel enumeration: 2,2,65535,srgba
0,0:(65535,0,0,65535)
0,1:(0,65535,0,65535)
1,0:(0,0,65535,65535)
1,1:(0,0,0,0)
29 changes: 29 additions & 0 deletions test/pacmacs-image-test.el
Expand Up @@ -95,3 +95,32 @@
(let ((color (aref (aref expected-canvas-data y) x)))
(should (equal color
(pacmacs--get-image-pixel canvas-image x y))))))))

(ert-deftest pacmacs--read-im-enum-image-test ()
(let* ((image-filename "test-data/2x2.txt")
(image (pacmacs--read-im-enum-image image-filename))
(expected-pixels '((0 0 "#ff0000")
(0 1 "#00ff00")
(1 0 "#0000ff")
(1 1 nil))))

(should (= 2 (pacmacs--image-width image)))
(should (= 2 (pacmacs--image-height image)))

(-all-p (-lambda ((x y color))
(should (equal color (pacmacs--get-image-pixel image x y))))
expected-pixels)))

(ert-deftest pacmacs--parse-im-enum-header-test ()
(let* ((input-header "# ImageMagick pixel enumeration: 80,40,255,srgba")
(expected-result (list 80 40 255)))
(should (equal expected-result
(pacmacs--parse-im-enum-header input-header)))
(should-error (pacmacs--parse-im-enum-header "khooy") :type 'error)))

(ert-deftest pacmacs--parse-im-enum-pixel-test ()
(let ((input-pixel "60,16: ( 255, 255, 255, 255) #khooy")
(expected-result (list 60 16 "#ffffff")))
(should (equal expected-result
(pacmacs--parse-im-enum-pixel input-pixel 255)))
(should-error (pacmacs--parse-im-enum-pixel "khooy") :type 'error)))

0 comments on commit c23844b

Please sign in to comment.