aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-06-20 21:28:23 +0200
committerbptato <nincsnevem662@gmail.com>2024-06-20 22:38:33 +0200
commit7f66b5ebc88936db974e3320d77c7ec9d4ab85e6 (patch)
tree669b2c307e2ea84476d6bbfd46ef127c0fc1c6f9 /src
parent2ab1e53b4bc15af3319994fdb25bb739b4b8e6db (diff)
downloadchawan-7f66b5ebc88936db974e3320d77c7ec9d4ab85e6.tar.gz
img: use stb_image, drop zlib as dependency
Now we have decoders for gif, jpeg, bmp. Also, the in-house PNG decoder has been replaced in favor of the stbi implementation; this means we no longer depend on zlib, since stbi comes with a built in inflate implementation.
Diffstat (limited to 'src')
-rw-r--r--src/html/dom.nim26
-rw-r--r--src/img/bitmap.nim22
-rw-r--r--src/img/painter.nim2
-rw-r--r--src/loader/response.nim2
-rw-r--r--src/types/color.nim28
-rw-r--r--src/utils/sandbox.nim5
6 files changed, 58 insertions, 27 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim
index be297d70..e6ea61d4 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -624,12 +624,12 @@ proc loadUnifont(window: Window) =
)
let response = window.loader.doRequest(request)
assert response.res == 0
- let dims = response.headers.table["X-Image-Dimensions"][0]
+ let dims = response.headers.table["Cha-Image-Dimensions"][0]
let width = parseUInt64(dims.until('x'), allowSign = false).get
let height = parseUInt64(dims.after('x'), allowSign = false).get
let len = int(width) * int(height)
let bitmap = ImageBitmap(
- px: cast[seq[ARGBColor]](newSeqUninitialized[uint32](len)),
+ px: cast[seq[RGBAColorBE]](newSeqUninitialized[uint32](len)),
width: width,
height: height
)
@@ -2909,6 +2909,10 @@ proc loadResource(window: Window; image: HTMLImageElement) =
let url = parseURL(src, window.document.url.some)
if url.isSome:
let url = url.get
+ if window.document.url.scheme == "https" and url.scheme == "http":
+ # mixed content :/
+ #TODO maybe do this in loader?
+ url.scheme = "https"
let p = window.loader.fetch(newRequest(url))
.then(proc(res: JSResult[Response]): Promise[JSResult[Response]] =
if res.isNone:
@@ -2935,14 +2939,14 @@ proc loadResource(window: Window; image: HTMLImageElement) =
# the `resume' command in pager.
response.unregisterFun()
response.body.sclose()
- if "X-Image-Dimensions" notin response.headers.table:
- window.console.error("X-Image-Dimensions missing in", $response.url)
+ if "Cha-Image-Dimensions" notin response.headers.table:
+ window.console.error("Cha-Image-Dimensions missing in", $response.url)
return
- let dims = response.headers.table["X-Image-Dimensions"][0]
+ let dims = response.headers.table["Cha-Image-Dimensions"][0]
let width = parseUInt64(dims.until('x'), allowSign = false)
let height = parseUInt64(dims.after('x'), allowSign = false)
if width.isNone or height.isNone:
- window.console.error("wrong X-Image-Dimensions")
+ window.console.error("wrong Cha-Image-Dimensions in", $response.url)
return
image.bitmap = NetworkBitmap(
width: width.get,
@@ -4251,7 +4255,7 @@ proc toBlob(ctx: JSContext; this: HTMLCanvasElement; callback: JSValue;
newURL("img-codec+" & contentType.after('/') & ":encode").get,
httpMethod = hmPost,
headers = newHeaders({
- "X-Image-Dimensions": $this.bitmap.width & 'x' & $this.bitmap.height
+ "Cha-Image-Dimensions": $this.bitmap.width & 'x' & $this.bitmap.height
}),
body = RequestBody(t: rbtString, s: s)
)
@@ -4271,15 +4275,15 @@ proc toBlob(ctx: JSContext; this: HTMLCanvasElement; callback: JSValue;
JS_FreeValue(ctx, callback)
return
let response = res.get
- if "X-Image-Dimensions" notin response.headers.table:
- window.console.error("X-Image-Dimensions missing")
+ if "Cha-Image-Dimensions" notin response.headers.table:
+ window.console.error("Cha-Image-Dimensions missing")
JS_FreeValue(ctx, callback)
return
- let dims = response.headers.table["X-Image-Dimensions"][0]
+ let dims = response.headers.table["Cha-Image-Dimensions"][0]
let width = parseUInt64(dims.until('x'), allowSign = false)
let height = parseUInt64(dims.after('x'), allowSign = false)
if width.isNone or height.isNone:
- window.console.error("wrong X-Image-Dimensions")
+ window.console.error("wrong Cha-Image-Dimensions")
JS_FreeValue(ctx, callback)
return
response.blob().then(proc(blob: JSResult[Blob]) =
diff --git a/src/img/bitmap.nim b/src/img/bitmap.nim
index a186b47f..70244643 100644
--- a/src/img/bitmap.nim
+++ b/src/img/bitmap.nim
@@ -2,7 +2,7 @@ import types/color
type
Bitmap* = ref object of RootObj
- px*: seq[ARGBColor]
+ px*: seq[RGBAColorBE]
width*: uint64
height*: uint64
@@ -14,19 +14,25 @@ type
proc newBitmap*(width, height: uint64): ImageBitmap =
return ImageBitmap(
- px: newSeq[ARGBColor](width * height),
+ px: newSeq[RGBAColorBE](width * height),
width: width,
height: height
)
-proc setpx*(bmp: Bitmap; x, y: uint64; color: ARGBColor) {.inline.} =
+proc setpx*(bmp: Bitmap; x, y: uint64; color: RGBAColorBE) {.inline.} =
bmp.px[bmp.width * y + x] = color
-proc getpx*(bmp: Bitmap; x, y: uint64): ARGBColor {.inline.} =
+proc setpx*(bmp: Bitmap; x, y: uint64; color: ARGBColor) {.inline.} =
+ bmp.px[bmp.width * y + x] = rgba_be(color.r, color.g, color.b, color.a)
+
+proc getpx*(bmp: Bitmap; x, y: uint64): RGBAColorBE {.inline.} =
return bmp.px[bmp.width * y + x]
-proc setpxb*(bmp: Bitmap; x, y: uint64; color: ARGBColor) {.inline.} =
- if color.a == 255:
- bmp.setpx(x, y, color)
+proc setpxb*(bmp: Bitmap; x, y: uint64; c: RGBAColorBE) {.inline.} =
+ if c.a == 255:
+ bmp.setpx(x, y, c)
else:
- bmp.setpx(x, y, bmp.getpx(x, y).blend(color))
+ bmp.setpx(x, y, bmp.getpx(x, y).blend(c))
+
+proc setpxb*(bmp: Bitmap; x, y: uint64; c: ARGBColor) {.inline.} =
+ bmp.setpxb(x, y, rgba_be(c.r, c.g, c.b, c.a))
diff --git a/src/img/painter.nim b/src/img/painter.nim
index d02f3602..4e8d1f72 100644
--- a/src/img/painter.nim
+++ b/src/img/painter.nim
@@ -151,7 +151,7 @@ proc getCharBmp(u: uint32): Bitmap =
let gx = uint64(32 + 16 * (u mod 0x100))
let gy = uint64(64 + 16 * (u div 0x100))
var fullwidth = false
- const white = rgba(255, 255, 255, 255)
+ const white = rgba_be(255, 255, 255, 255)
block loop:
# hack to recognize full width characters
for y in 0 ..< 16u64:
diff --git a/src/loader/response.nim b/src/loader/response.nim
index 3834d5a9..2f624a1a 100644
--- a/src/loader/response.nim
+++ b/src/loader/response.nim
@@ -215,7 +215,7 @@ proc saveToBitmap*(response: Response; bmp: Bitmap): EmptyPromise =
assert not response.bodyUsed
let opaque = BitmapOpaque(bmp: bmp, idx: 0)
let size = bmp.width * bmp.height
- bmp.px = cast[seq[ARGBColor]](newSeqUninitialized[uint32](size))
+ bmp.px = cast[seq[RGBAColorBE]](newSeqUninitialized[uint32](size))
response.opaque = opaque
response.onRead = onReadBitmap
response.bodyUsed = true
diff --git a/src/types/color.nim b/src/types/color.nim
index 8f362003..1f9c3347 100644
--- a/src/types/color.nim
+++ b/src/types/color.nim
@@ -6,9 +6,17 @@ import utils/twtstr
type
RGBColor* = distinct uint32
- # RGBA color, stored in ARGB format
+ # ARGB color. machine-dependent format, so that bit shifts and arithmetic
+ # works. (Alpha is MSB, then come R, G, B.)
ARGBColor* = distinct uint32
+ # RGBA format; machine-independent, always big-endian.
+ RGBAColorBE* {.packed.} = object
+ r*: uint8
+ g*: uint8
+ b*: uint8
+ a*: uint8
+
ANSIColor* = distinct uint8
EightBitColor* = distinct uint8
@@ -23,12 +31,17 @@ type
t*: ColorTag
n: uint32
+func rgba*(r, g, b, a: uint8): ARGBColor
+
func toRGBColor*(i: ARGBColor): RGBColor =
return RGBColor(uint32(i) and 0xFFFFFFu32)
converter toARGBColor*(i: RGBColor): ARGBColor =
return ARGBColor(uint32(i) or 0xFF000000u32)
+converter toARGBColor*(c: RGBAColorBE): ARGBColor =
+ return rgba(c.r, c.g, c.b, c.a)
+
func `==`*(a, b: ARGBColor): bool {.borrow.}
func `==`*(a, b: ANSIColor): bool {.borrow.}
@@ -301,8 +314,6 @@ func fastmul1(c, ca: uint32): uint32 =
func fastmul1(c: ARGBColor; ca: uint32): ARGBColor =
return ARGBColor(fastmul1(uint32(c), ca))
-func rgba*(r, g, b, a: uint8): ARGBColor
-
func premul(c: ARGBColor): ARGBColor =
return ARGBColor(fastmul(uint32(c), uint32(c.a)))
@@ -375,11 +386,20 @@ func rgba*(r, g, b, a: uint8): ARGBColor =
return ARGBColor((uint32(a) shl 24) or (uint32(r) shl 16) or
(uint32(g) shl 8) or uint32(b))
+func rgba_be*(r, g, b, a: uint8): RGBAColorBE =
+ return RGBAColorBE(r: r, g: g, b: b, a: a)
+
+func rgb_be*(r, g, b: uint8): RGBAColorBE =
+ return RGBAColorBE(r: r, g: g, b: b, a: 0xFF)
+
func rgba*(r, g, b, a: int): ARGBColor =
return rgba(uint8(r), uint8(g), uint8(b), uint8(a))
func gray*(n: uint8): RGBColor =
- return rgb(n, n, n) #TODO use yuv instead?
+ return rgb(n, n, n)
+
+func gray_be*(n: uint8): RGBAColorBE =
+ return rgb_be(n, n, n)
# NOTE: this assumes n notin 0..15 (which would be ANSI 4-bit)
func toRGB*(param0: EightBitColor): RGBColor =
diff --git a/src/utils/sandbox.nim b/src/utils/sandbox.nim
index 0e79ec06..ce9b194e 100644
--- a/src/utils/sandbox.nim
+++ b/src/utils/sandbox.nim
@@ -91,6 +91,7 @@ elif defined(linux) and not disableSandbox:
"gettimeofday", # used by QuickJS in Date.now()
"mmap", # memory allocation
"mmap2", # memory allocation
+ "mremap", # memory allocation
"munmap", # memory allocation
"pipe", # for pipes to child process
"pipe2", # for when pipe is implemented as pipe2
@@ -125,13 +126,13 @@ elif defined(linux) and not disableSandbox:
onSignal SIGSYS:
discard sig
raise newException(Defect, "Sandbox violation in network process")
- let ctx = seccomp_init(SCMP_ACT_TRAP)
+ let ctx = seccomp_init(SCMP_ACT_KILL_PROCESS)
doAssert pointer(ctx) != nil
const allowList = [
cstring"close", "exit_group", # duh
"read", "write", "recv", "send", "recvfrom", "sendto", # socket i/o
"fcntl", "fcntl64", # so we can set nonblock etc.
- "mmap", "mmap2", "munmap", "brk", # memory allocation
+ "mmap", "mmap2", "mremap", "munmap", "brk", # memory allocation
"poll", # curl needs poll
"getpid", # used indirectly by OpenSSL EVP_RAND_CTX_new (through drbg)
"fstat", # glibc fread seems to call it