-
-extraoffset=`echo "127.5 - 127.5 * ($realmedian + $ofs)" | bc -l`
-
-echo "dpoffsetmapping - $extrascale $extraoffset"
+# note: ofs is in pre-scale units
+extraoffset=`echo "($realmedian + $ofs / $realscale)" | bc -l`
+
+# note: this tool maps -1 to 1 neutral, and +1 to 255 neutral
+# darkplaces maps 1 to 0 neutral, and 0 to 255 neutral
+#
+# p_t(-1) = 1
+# p_t(+1) = 255
+# -> p_t(x) = 128 + 127 * x
+# p_d(1) = 0
+# p_d(0) = 255
+# -> p_d(x) = 255 * (1 - x)
+# -> p_d^-1(x) = 1 - x / 255
+#
+# we need p_d^-1(p_t(x)) = 1 - (128 + 127 * x) / 255
+#
+extraoffset="match8 "`echo "128 + 127 * $extraoffset" | bc -l`
+
+echo " dpoffsetmapping - $extrascale $extraoffset"