ws-cleanup: codecs/compress/PackBits; UTF-16 and half floats from one place
- codecs/compress/PackBits (new, from image/tiff/Pixels): decode (lenient count + a clean flag for strict callers) and encode; image/tiff and layered/psd (which carried its own decoder) use it. tests/codecs/packbits. - codecs/text/Text.utf16(buf, offset, length, bigEndian?): the UTF-16 -> UTF-8 conversion codecs/font/TrueType, archive/7z and layered/psd each wrote (lone surrogates -> U+FFFD everywhere now). - image/tiff/Pixels and data/cbor use core/Bytes.half instead of their own float16 decoders.
This commit is contained in:
1 parent
51b08575c4
commit
44a599f00c
13 files changed
+191
-178
No files matched your search
@@ -5,6 +5,7 @@
|
||||
-- writer. Adobe Photoshop File Formats Specification (2016), "Channel image data" / "Image data section".
|
||||
-- Never throws: every read is range-checked first; failures return nil, "message".
|
||||
local Deflate = require("@formats/codecs/compress/Deflate")
|
||||
local PackBits = require("@formats/codecs/compress/PackBits")
|
||||
|
||||
local Channels = {}
|
||||
|
||||
@@ -21,36 +22,6 @@ function Channels.rowBytes(w: number, depth: number): number
|
||||
return w * (depth // 8)
|
||||
end
|
||||
|
||||
-- packbits(src, p, limit, dst, d, n) -> the source offset after decoding n bytes into dst at d | nil
|
||||
local function packbits(src: buffer, p: number, limit: number, dst: buffer, d: number, n: number): number?
|
||||
local e = d + n
|
||||
while d < e do
|
||||
if p >= limit then
|
||||
return nil
|
||||
end
|
||||
local h = readu8(src, p)
|
||||
p += 1
|
||||
if h < 128 then
|
||||
local c = h + 1
|
||||
if p + c > limit or d + c > e then
|
||||
return nil
|
||||
end
|
||||
bcopy(dst, d, src, p, c)
|
||||
p += c
|
||||
d += c
|
||||
elseif h > 128 then
|
||||
local c = 257 - h
|
||||
if p >= limit or d + c > e then
|
||||
return nil
|
||||
end
|
||||
bfill(dst, d, readu8(src, p), c)
|
||||
p += 1
|
||||
d += c
|
||||
end
|
||||
end
|
||||
return p
|
||||
end
|
||||
|
||||
-- rleCounts(src, p, limit, rows, psb) -> { counts }, offset after them | nil
|
||||
function Channels.rleCounts(src: buffer, p: number, limit: number, rows: number, psb: boolean): ({ number }?, number)
|
||||
local size = if psb then 4 else 2
|
||||
@@ -76,8 +47,8 @@ function Channels.rle(src: buffer, p: number, limit: number, counts: { number },
|
||||
if p + c > limit then
|
||||
return nil, "RLE row past the end"
|
||||
end
|
||||
local q = packbits(src, p, p + c, dst, d + i * rowBytes, rowBytes)
|
||||
if q == nil then
|
||||
local _, _, clean = PackBits.decode(src, p, p + c, dst, d + i * rowBytes, rowBytes)
|
||||
if not clean then
|
||||
-- a short or overlong row: keep what decoded, zero the rest (Photoshop is lenient too)
|
||||
return nil, "bad RLE row"
|
||||
end
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Text = require("@formats/codecs/text/Text")
|
||||
local Blend = require("@formats/codecs/image/Blend")
|
||||
local Channels = require("@formats/layered/psd/Channels")
|
||||
|
||||
@@ -24,7 +25,7 @@ local Psd = {
|
||||
category = "layered",
|
||||
extensions = { "psd", "psb" },
|
||||
mime = { "image/vnd.adobe.photoshop" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/image/Blend", "codecs/compress/Deflate" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/image/Blend", "codecs/compress/Deflate", "codecs/compress/PackBits", "codecs/text/Text" },
|
||||
caps = { decode = true, encode = true, layers = true, groups = true, depths = { 1, 8, 16, 32 } },
|
||||
}
|
||||
|
||||
@@ -97,23 +98,7 @@ local function len(r: Bytes.Reader, psb: boolean): number
|
||||
end
|
||||
|
||||
local function utf16be(s: string): string
|
||||
local out = {}
|
||||
local i = 1
|
||||
while i + 1 <= #s do
|
||||
local c = string.byte(s, i) * 256 + string.byte(s, i + 1)
|
||||
i += 2
|
||||
if c >= 0xD800 and c < 0xDC00 and i + 1 <= #s then
|
||||
local c2 = string.byte(s, i) * 256 + string.byte(s, i + 1)
|
||||
if c2 >= 0xDC00 and c2 < 0xE000 then
|
||||
c = 0x10000 + (c - 0xD800) * 1024 + (c2 - 0xDC00)
|
||||
i += 2
|
||||
end
|
||||
end
|
||||
if c ~= 0 then
|
||||
table.insert(out, utf8.char(c))
|
||||
end
|
||||
end
|
||||
return table.concat(out)
|
||||
return (string.gsub(Text.utf16(buffer.fromstring(s), 0, #s, true), "%z", ""))
|
||||
end
|
||||
|
||||
-- the layer records + channel data at r (a Reader over the layer info, after its length): flat records | nil, Err
|
||||
|
||||
Reference in new issue
Block a user