Lua: Fix type confusion between signed and unsigned
[lsnes.git] / src / genlanczos.lua
blob67b31299030025430dba34e5367d647d2d40f2c0
1 fnname = arg[1];
2 src = tonumber(arg[2]);
3 dest = tonumber(arg[3]);
4 a = tonumber(arg[4]);
5 bits8 = (arg[5] == "8");
7 coeffscale = 16384;
9 coeffs_for = function(xpos, srcsize, dstsize, alpha)
10 local ret = {};
11 local center = math.floor(xpos * srcsize / dstsize);
12 for i = -alpha + 1, alpha do
13 local point = center + i;
14 local x = point + 0.5 - xpos * srcsize / dstsize - 0.5 * srcsize / dstsize;
15 if x < -1e-10 or x > 1e-10 then
16 local xpi = x * math.pi;
17 if point >= 0 and point < srcsize then
18 ret[point] = a * math.sin(xpi) * math.sin(xpi / a) / (xpi * xpi);
19 end
20 else
21 if point >= 0 and point < srcsize then
22 ret[point] = 1;
23 end
24 end
25 end
26 local sum = 0;
27 local k, v;
28 for k, v in pairs(ret) do
29 sum = sum + v;
30 end
31 for k, v in pairs(ret) do
32 ret[k] = math.floor(coeffscale * ret[k] / sum + 0.5);
33 end
34 return ret;
35 end
37 emit_pixel = function(xpos, srcsize, dstsize, alpha)
38 local t = coeffs_for(xpos, srcsize, dstsize, alpha);
39 io.stdout:write("tmp[" .. xpos .. "]=(");
40 for k,v in pairs(t) do
41 io.stdout:write(v .. "*src[" .. k .. "]+");
42 end
43 io.stdout:write("0)/" .. coeffscale .. ";\n");
44 end
46 emit_fn_prologue = function(name, dstsize, b8f)
47 if b8f then
48 io.stdout:write("void " .. name .. "(unsigned char* dst, unsigned char* src)\n");
49 else
50 io.stdout:write("void " .. name .. "(unsigned short* dst, unsigned short* src)\n");
51 end
52 io.stdout:write("{\n");
53 io.stdout:write("int tmp[" .. dstsize .. "];\n");
54 end
56 emit_fn_epilogue = function(dstsize, b8f)
57 io.stdout:write("for(int i = 0; i < " .. dstsize .. "; i++)\n");
58 io.stdout:write("if(tmp[i] < 0) dst[i] = 0;\n");
59 if b8f then
60 io.stdout:write("else if(tmp[i] > 255) dst[i] = 255;\n");
61 else
62 io.stdout:write("else if(tmp[i] > 65535) dst[i] = 65535;\n");
63 end
64 io.stdout:write("else dst[i] = tmp[i];\n");
65 io.stdout:write("}\n");
66 end
68 emit_fn = function(name, srcsize, dstsize, alpha, b8f)
69 emit_fn_prologue(name, dstsize, b8f);
70 for i = 0, dstsize - 1 do
71 emit_pixel(i, srcsize, dstsize, alpha)
72 end
73 emit_fn_epilogue(dstsize, b8f);
74 end
76 emit_fn(fnname, src, dest, a, bits8);