• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#objective-cqtwindows誰得cocoapythonphprubygameguibathyscaphec翻訳omegat計画中(planning stage)frameworktwittertestdomvb.netdirectxbtronarduinopreviewerゲームエンジン

A categorical programming language


Commit MetaInfo

修订版55542626012dcf8ceb2e8ee71f28e39d84a51f8c (tree)
时间2022-06-20 09:18:37
作者Corbin <cds@corb...>
CommiterCorbin

Log Message

Factor out some color-handling functions.

更改概述

差异

--- a/cammy-rpy/animate.py
+++ b/cammy-rpy/animate.py
@@ -1,6 +1,5 @@
11 from __future__ import division
22
3-import math
43 import time
54
65 from rpython.rlib.rstring import StringBuilder
@@ -8,19 +7,11 @@ from rpython.rtyper.lltypesystem import rffi
87
98 from cammylib import caca
109 from cammylib.cam import compileArrow
10+from cammylib.colors import hdr2byte
1111 from cammylib.elements import F, P, TypeFail
1212 from cammylib.parser import parse
1313
1414
15-def linear2sRGB(u):
16- try:
17- return u * 25 / 323 if u <= 0.04045 else math.pow((u * 200 + 11) / 211, 12 / 5)
18- except OverflowError:
19- return 1.0
20-
21-def channel(c):
22- return int(255 * max(0.0, min(1.0, linear2sRGB(c))))
23-
2415 def resize(displayWidth, displayHeight):
2516 bufferWidth = displayWidth * 2
2617 # Include a factor of 3/5 since monospaced fonts are extra-tall.
@@ -34,12 +25,13 @@ def rescale(width, height):
3425 hoffset = height >> 1
3526 # Keep pixels square-ish, and let window flexibly scale.
3627 scale = min(width, height) >> 1
37- ulx = -woffset / scale
38- uly = -hoffset / scale
39- lrx = (width - woffset) / scale
40- lry = (height - hoffset) / scale
41- print "Viewport is at (%f,%f) (upper left) to (%f,%f) (lower right)" % (
42- ulx, uly, lrx, lry)
28+ lx = -woffset / scale
29+ ly = -hoffset / scale
30+ rx = (width - woffset) / scale
31+ uy = (height - hoffset) / scale
32+ # View
33+ print "Viewport is at (%f,%f) (lower left) to (%f,%f) (upper right)" % (
34+ lx, ly, rx, uy)
4335 return woffset, hoffset, scale
4436
4537 def drawDisplay(cam, canvas, display):
@@ -64,16 +56,17 @@ def drawDisplay(cam, canvas, display):
6456 0xff, 0xff00, 0xff0000, 0x0)
6557
6658 buf = StringBuilder(w * h)
67- for y in range(-hoff, h - hoff):
59+ # Flip y-axis to get traditional quadrants.
60+ for y in range(h - hoff, -hoff, -1):
6861 for x in range(-woff, w - woff):
6962 coords = P(P(F(x / scale), F(y / scale)), F(t))
7063 rgb = cam.execute(coords)
7164 r = rgb.first().f()
7265 g = rgb.second().first().f()
7366 b = rgb.second().second().f()
74- buf.append(chr(channel(r)))
75- buf.append(chr(channel(g)))
76- buf.append(chr(channel(b)))
67+ buf.append(hdr2byte(r))
68+ buf.append(hdr2byte(g))
69+ buf.append(hdr2byte(b))
7770
7871 nextStamp = time.time()
7972 t += nextStamp - stamp
--- /dev/null
+++ b/cammy-rpy/cammylib/colors.py
@@ -0,0 +1,13 @@
1+from __future__ import division
2+
3+import math
4+
5+
6+def linear2sRGB(u):
7+ try:
8+ return u * 25 / 323 if u <= 0.04045 else math.pow((u * 200 + 11) / 211, 12 / 5)
9+ except OverflowError:
10+ return 1.0
11+
12+def hdr2byte(c):
13+ return chr(int(255 * max(0.0, min(1.0, linear2sRGB(c)))))
--- a/cammy-rpy/draw.py
+++ b/cammy-rpy/draw.py
@@ -11,6 +11,7 @@ from rpython.rlib.rstring import StringBuilder, split
1111 # from rpython.rlib import rvmprof
1212
1313 from cammylib.cam import compileArrow
14+from cammylib.colors import hdr2byte
1415 from cammylib.elements import F, P, TypeFail
1516 from cammylib.parser import parse
1617 from cammylib import stb
@@ -22,15 +23,6 @@ def scale(bot, top, x):
2223 d = top - bot
2324 return bot + d * x
2425
25-def linear2sRGB(u):
26- try:
27- return u * 25 / 323 if u <= 0.04045 else math.pow((u * 200 + 11) / 211, 12 / 5)
28- except OverflowError:
29- return 1.0
30-
31-def finishChannel(c):
32- return int(255 * max(0.0, min(1.0, linear2sRGB(c))))
33-
3426 # Pixel area: 4 / (w * h)
3527 # Pixel radius: √(area / pi) = 2 / √(w * h * pi) = (2 / √pi) / √(w * h)
3628 # This constant is the first half of that.
@@ -110,7 +102,7 @@ def multisample(program, radius, x, y):
110102 g += rgb.second().first().f()
111103 b += rgb.second().second().f()
112104 l = len(offsets)
113- return finishChannel(r / l), finishChannel(g / l), finishChannel(b / l), cost // len(offsets)
105+ return hdr2byte(r / l), hdr2byte(g / l), hdr2byte(b / l), cost // len(offsets)
114106
115107 def drawPixels(size, program, window):
116108 _, stdout, _ = create_stdio()
@@ -120,7 +112,7 @@ def drawPixels(size, program, window):
120112 while i < size:
121113 c1, c2 = window.coordsForPixel(i)
122114 r, g, b, c = multisample(program, window.pixelRadius, c1, c2)
123- sb.append(chr(r) + chr(g) + chr(b))
115+ sb.append(r + g + b)
124116 cost.observe(c)
125117 i += 1
126118 if not (i % 1000):
--- a/jelly/src/main.rs
+++ b/jelly/src/main.rs
@@ -40,8 +40,8 @@ impl CostFunction<SymbolLang> for CAMCostModel {
4040 "curry" => 4 + costs(enode.children[0]),
4141 "uncurry" => 4 + costs(enode.children[0]),
4242 // Loops
43- "pr" => 4 + costs(enode.children[0]) + 3 * costs(enode.children[1]),
44- "fold" => 4 + costs(enode.children[0]) + 5 * costs(enode.children[1]),
43+ "pr" => 4 + 2 * costs(enode.children[0]) + 3 * costs(enode.children[1]),
44+ "fold" => 4 + 2 * costs(enode.children[0]) + 5 * costs(enode.children[1]),
4545 // Term ops which the JIT still has to residualize
4646 _ => 4
4747 }