mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 17:05:55 +01:00
Merge main into feat/drag-into-floating-window
This commit is contained in:
commit
72b14d6aaa
275 files changed
+11200
-1744
No files matched your search
Generated
+165
-106
@@ -146,13 +146,28 @@ version = "2.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cc7bb162ec39d46ab1ca8c77bf72e890535becd1751bb45f64c597edb4c8c6b3"
|
||||
|
||||
[[package]]
|
||||
name = "alloc-no-stdlib"
|
||||
version = "3.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c2fb6cfd47bf496ff64095c20eaba0c201404ee38714d4142fcfa1dc334fcc7a"
|
||||
|
||||
[[package]]
|
||||
name = "alloc-stdlib"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0e76a019e91224d279006ff972f1e984179a6e9feb050adba6ce8274aef23195"
|
||||
dependencies = [
|
||||
"alloc-no-stdlib",
|
||||
"alloc-no-stdlib 2.0.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "alloc-stdlib"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b5c1865780388bfa186411ab5f247819487fc4864c6e9c3106611fa347586e1"
|
||||
dependencies = [
|
||||
"alloc-no-stdlib 3.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -596,9 +611,9 @@ version = "8.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5cc91aac060a7a1e25823bdccbfb6af1875b88f17c6daac97894eed8207166b3"
|
||||
dependencies = [
|
||||
"alloc-no-stdlib",
|
||||
"alloc-stdlib",
|
||||
"brotli-decompressor",
|
||||
"alloc-no-stdlib 2.0.4",
|
||||
"alloc-stdlib 0.2.4",
|
||||
"brotli-decompressor 5.0.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -607,8 +622,18 @@ version = "5.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3a32acac15fe1967bc3986b2a6347dffc965602354ea6f450ad07e8bfd253583"
|
||||
dependencies = [
|
||||
"alloc-no-stdlib",
|
||||
"alloc-stdlib",
|
||||
"alloc-no-stdlib 2.0.4",
|
||||
"alloc-stdlib 0.2.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "brotli-decompressor"
|
||||
version = "6.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cc031cd87c6529082b0b65c604e86af0ed7744841b17ccfe60b1e31fa606179e"
|
||||
dependencies = [
|
||||
"alloc-no-stdlib 3.0.0",
|
||||
"alloc-stdlib 0.3.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -976,7 +1001,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-affinity"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=228f6d0c50f1fdee4c5124603602993fc79d4c9d#228f6d0c50f1fdee4c5124603602993fc79d4c9d"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"crc32fast",
|
||||
"flate2",
|
||||
@@ -987,17 +1012,17 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-codepage"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=8bd1412eaa7c0b58f3f9c401495d35fe12d6d85e#8bd1412eaa7c0b58f3f9c401495d35fe12d6d85e"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
|
||||
[[package]]
|
||||
name = "craft-docpath"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb#9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
|
||||
[[package]]
|
||||
name = "craft-dxf"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=8bd1412eaa7c0b58f3f9c401495d35fe12d6d85e#8bd1412eaa7c0b58f3f9c401495d35fe12d6d85e"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"craft-codepage",
|
||||
"encoding_rs",
|
||||
@@ -1009,17 +1034,25 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-eps"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6d620e971e820b517b11a99cad040ebac670e1e2#6d620e971e820b517b11a99cad040ebac670e1e2"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
|
||||
[[package]]
|
||||
name = "craft-fonts-build"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=c4b8405d5fd8f29cf77de528206bd2b38bb0ecb6#c4b8405d5fd8f29cf77de528206bd2b38bb0ecb6"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
|
||||
[[package]]
|
||||
name = "craft-gif"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"gif",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-gzip"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=b0d84cd15db39f2af9869cfb47b8b72cbeca3f20#b0d84cd15db39f2af9869cfb47b8b72cbeca3f20"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
]
|
||||
@@ -1027,7 +1060,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-i18n"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=c4b8405d5fd8f29cf77de528206bd2b38bb0ecb6#c4b8405d5fd8f29cf77de528206bd2b38bb0ecb6"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"fixed_decimal",
|
||||
"icu_decimal",
|
||||
@@ -1050,12 +1083,16 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-image-codecs"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"craft-jpeg",
|
||||
"craft-metadata",
|
||||
"craft-tiff",
|
||||
"crc32fast",
|
||||
"exr",
|
||||
"flate2",
|
||||
"half",
|
||||
"hayro-jpeg2000",
|
||||
"image",
|
||||
"image-webp",
|
||||
"jpeg-encoder",
|
||||
@@ -1064,33 +1101,55 @@ dependencies = [
|
||||
"serde_json",
|
||||
"thiserror 2.0.21",
|
||||
"tiff",
|
||||
"zune-core 0.5.3",
|
||||
"zune-jpeg 0.5.15",
|
||||
"zune-core",
|
||||
"zune-jpeg",
|
||||
"zune-jpegxl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-jpeg"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-jxl"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"jixel",
|
||||
"jxl-oxide",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-keymap"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb#9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"egui",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-metadata"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"brotli-decompressor 6.3.2",
|
||||
"chrono",
|
||||
"craft-tiff",
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-metafile"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6d620e971e820b517b11a99cad040ebac670e1e2#6d620e971e820b517b11a99cad040ebac670e1e2"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"encoding_rs",
|
||||
"image",
|
||||
@@ -1101,7 +1160,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-pdf"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb#9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"hayro-interpret",
|
||||
"hayro-syntax",
|
||||
@@ -1112,7 +1171,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-print"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb#9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"craft-winprint-gdi",
|
||||
"log",
|
||||
@@ -1126,7 +1185,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-proof"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb#9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"log",
|
||||
"miniz_oxide 0.9.1",
|
||||
@@ -1137,7 +1196,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-psd"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"encoding_rs",
|
||||
"flate2",
|
||||
@@ -1145,14 +1204,29 @@ dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-stream-codecs"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"fax",
|
||||
"hayro-ccitt",
|
||||
"hayro-jbig2",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-svg"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6d620e971e820b517b11a99cad040ebac670e1e2#6d620e971e820b517b11a99cad040ebac670e1e2"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=6d620e971e820b517b11a99cad040ebac670e1e2)",
|
||||
"craft-image-codecs",
|
||||
"craft-xml",
|
||||
"data-url",
|
||||
"flate2",
|
||||
"kurbo 0.13.1",
|
||||
"quick-xml 0.41.0",
|
||||
"resvg 0.48.1",
|
||||
"roxmltree 0.21.2",
|
||||
"thiserror 2.0.21",
|
||||
"usvg 0.48.1",
|
||||
@@ -1163,7 +1237,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-tiff"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
@@ -1172,7 +1246,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-ui"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=c4b8405d5fd8f29cf77de528206bd2b38bb0ecb6#c4b8405d5fd8f29cf77de528206bd2b38bb0ecb6"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"craft-fonts-build",
|
||||
"craft-i18n",
|
||||
@@ -1193,7 +1267,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-update"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=e98299546f5c5c3c22388d16507f8b5e61a5f280#e98299546f5c5c3c22388d16507f8b5e61a5f280"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"dunce",
|
||||
@@ -1220,7 +1294,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-vectorcraft-format"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=b0d84cd15db39f2af9869cfb47b8b72cbeca3f20#b0d84cd15db39f2af9869cfb47b8b72cbeca3f20"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"serde",
|
||||
@@ -1242,7 +1316,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-window"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb#9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"egui",
|
||||
"objc2 0.6.5",
|
||||
@@ -1253,7 +1327,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-winprint-gdi"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/winprint.rs?rev=417ac3c4d14d3da5485a99c7d5ce8f05664ec312#417ac3c4d14d3da5485a99c7d5ce8f05664ec312"
|
||||
source = "git+https://github.com/storytold/winprint.rs?rev=64e7afd4d1483512385012c340ecaaaf0f3beb7f#64e7afd4d1483512385012c340ecaaaf0f3beb7f"
|
||||
dependencies = [
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
@@ -1261,18 +1335,10 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-xml"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6d620e971e820b517b11a99cad040ebac670e1e2#6d620e971e820b517b11a99cad040ebac670e1e2"
|
||||
dependencies = [
|
||||
"quick-xml 0.41.0",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-xml"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
@@ -2143,16 +2209,6 @@ dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "gif"
|
||||
version = "0.13.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4ae047235e33e2829703574b54fdec96bfbad892062d97fed2f76022287de61b"
|
||||
dependencies = [
|
||||
"color_quant",
|
||||
"weezl 0.1.12",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "gif"
|
||||
version = "0.14.2"
|
||||
@@ -2386,7 +2442,7 @@ checksum = "9f4d0e94ddd48749f06bbe4e5389fb9799a0c45bcaf00495042076ef05e3241a"
|
||||
[[package]]
|
||||
name = "hayro-cmap"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb#9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"brotli",
|
||||
"hayro-postscript",
|
||||
@@ -2395,7 +2451,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "hayro-interpret"
|
||||
version = "0.7.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb#9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"bitflags 2.13.2",
|
||||
"hayro-cmap",
|
||||
@@ -2413,7 +2469,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "hayro-jbig2"
|
||||
version = "0.3.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb#9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"fearless_simd",
|
||||
"hayro-ccitt",
|
||||
@@ -2431,12 +2487,12 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "hayro-postscript"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb#9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
|
||||
[[package]]
|
||||
name = "hayro-syntax"
|
||||
version = "0.7.2"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb#9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"hayro-ccitt",
|
||||
@@ -2446,7 +2502,7 @@ dependencies = [
|
||||
"rustc-hash 2.1.3",
|
||||
"smallvec",
|
||||
"stringprep",
|
||||
"zune-jpeg 0.5.15",
|
||||
"zune-jpeg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2831,15 +2887,15 @@ dependencies = [
|
||||
"bytemuck",
|
||||
"byteorder-lite",
|
||||
"color_quant",
|
||||
"gif 0.14.2",
|
||||
"gif",
|
||||
"image-webp",
|
||||
"moxcms",
|
||||
"num-traits",
|
||||
"png 0.18.1",
|
||||
"qoi",
|
||||
"tiff",
|
||||
"zune-core 0.5.3",
|
||||
"zune-jpeg 0.5.15",
|
||||
"zune-core",
|
||||
"zune-jpeg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2929,6 +2985,12 @@ version = "1.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
|
||||
|
||||
[[package]]
|
||||
name = "jixel"
|
||||
version = "0.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "278a75e12caacec34be774d62ad05e45fc0b6a52d1f4e9f7fbc0209ecdf794f3"
|
||||
|
||||
[[package]]
|
||||
name = "jni"
|
||||
version = "0.22.4"
|
||||
@@ -3092,7 +3154,7 @@ version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e35d032bcec660647828527ff42c6f5776d2fd44b8357f9f6d9ac6dc07218e46"
|
||||
dependencies = [
|
||||
"brotli-decompressor",
|
||||
"brotli-decompressor 5.0.3",
|
||||
"jxl-bitstream",
|
||||
"jxl-frame",
|
||||
"jxl-grid",
|
||||
@@ -3124,7 +3186,7 @@ version = "0.12.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d36c662923f47586880211f3bc7c0d83fb3a9b410d278c7bde93450748abeef3"
|
||||
dependencies = [
|
||||
"brotli-decompressor",
|
||||
"brotli-decompressor 5.0.3",
|
||||
"jxl-bitstream",
|
||||
"jxl-color",
|
||||
"jxl-frame",
|
||||
@@ -3230,13 +3292,13 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "krilla"
|
||||
version = "0.8.2"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb#9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"bumpalo",
|
||||
"flate2",
|
||||
"float-cmp",
|
||||
"gif 0.14.2",
|
||||
"gif",
|
||||
"image-webp",
|
||||
"imagesize 0.14.0",
|
||||
"indexmap",
|
||||
@@ -3251,7 +3313,7 @@ dependencies = [
|
||||
"tiny-skia-path 0.12.0",
|
||||
"xmp-writer",
|
||||
"yoke",
|
||||
"zune-jpeg 0.5.15",
|
||||
"zune-jpeg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3339,7 +3401,7 @@ checksum = "d4a5ff6bcca6c4867b1c4fd4ef63e4db7436ef363e0ad7531d1558856bae64f4"
|
||||
[[package]]
|
||||
name = "linesweeper"
|
||||
version = "0.5.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=481f66d287b2a06845f3667a19daf35f0fe958ed#481f66d287b2a06845f3667a19daf35f0fe958ed"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"kurbo 0.13.1",
|
||||
@@ -4806,15 +4868,12 @@ version = "0.45.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a8928798c0a55e03c9ca6c4c6846f76377427d2c1e1f7e6de3c06ae57942df43"
|
||||
dependencies = [
|
||||
"gif 0.13.3",
|
||||
"image-webp",
|
||||
"log",
|
||||
"pico-args",
|
||||
"rgb",
|
||||
"svgtypes 0.15.3",
|
||||
"tiny-skia 0.11.4",
|
||||
"usvg 0.45.1",
|
||||
"zune-jpeg 0.4.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4824,7 +4883,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "67e3803f97b999e80cbf7c6ecdd07a8102204d92e1633cf48783720c521196bd"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"gif 0.14.2",
|
||||
"gif",
|
||||
"image-webp",
|
||||
"log",
|
||||
"pico-args",
|
||||
@@ -4832,7 +4891,7 @@ dependencies = [
|
||||
"svgtypes 0.16.1",
|
||||
"tiny-skia 0.12.0",
|
||||
"usvg 0.48.1",
|
||||
"zune-jpeg 0.5.15",
|
||||
"zune-jpeg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -5940,7 +5999,7 @@ dependencies = [
|
||||
"half",
|
||||
"quick-error 2.0.1",
|
||||
"weezl 0.1.12",
|
||||
"zune-jpeg 0.5.15",
|
||||
"zune-jpeg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -6547,8 +6606,8 @@ name = "vectorcraft-affinity"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"craft-affinity",
|
||||
"craft-image-codecs",
|
||||
"crc32fast",
|
||||
"image",
|
||||
"proptest",
|
||||
]
|
||||
|
||||
@@ -6576,6 +6635,7 @@ name = "vectorcraft-cad"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"craft-dxf",
|
||||
"craft-image-codecs",
|
||||
"image",
|
||||
"kurbo 0.13.1",
|
||||
"serde_json",
|
||||
@@ -6628,7 +6688,9 @@ name = "vectorcraft-doc"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"hayro-ccitt",
|
||||
"craft-image-codecs",
|
||||
"craft-stream-codecs",
|
||||
"flate2",
|
||||
"image",
|
||||
"jpeg-encoder",
|
||||
"kurbo 0.13.1",
|
||||
@@ -6639,16 +6701,17 @@ dependencies = [
|
||||
"tiff",
|
||||
"vectorcraft-color",
|
||||
"vectorcraft-geom 0.24.1",
|
||||
"zune-jpeg 0.5.15",
|
||||
"zune-jpeg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-effects"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"craft-image-codecs",
|
||||
"craft-svg",
|
||||
"image",
|
||||
"kurbo 0.13.1",
|
||||
"resvg 0.48.1",
|
||||
"serde_json",
|
||||
"svgtypes 0.16.1",
|
||||
"vectorcraft-color",
|
||||
@@ -6669,11 +6732,14 @@ dependencies = [
|
||||
"cfb",
|
||||
"chrono",
|
||||
"craft-docpath",
|
||||
"craft-image-codecs",
|
||||
"craft-jxl",
|
||||
"craft-proof",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)",
|
||||
"craft-stream-codecs",
|
||||
"craft-xml",
|
||||
"crc32fast",
|
||||
"flate2",
|
||||
"hayro-jpeg2000",
|
||||
"hayro-ccitt",
|
||||
"heic-rs",
|
||||
"image",
|
||||
"jxl-color",
|
||||
@@ -6690,6 +6756,7 @@ dependencies = [
|
||||
"serde_json",
|
||||
"skrifa 0.47.0",
|
||||
"thiserror 2.0.21",
|
||||
"tiff",
|
||||
"vectorcraft-affinity",
|
||||
"vectorcraft-apischema",
|
||||
"vectorcraft-brush",
|
||||
@@ -6712,7 +6779,7 @@ dependencies = [
|
||||
"vectorcraft-tools",
|
||||
"vectorcraft-trace",
|
||||
"wat",
|
||||
"zune-core 0.5.3",
|
||||
"zune-core",
|
||||
"zune-jpegxl",
|
||||
]
|
||||
|
||||
@@ -6721,11 +6788,12 @@ name = "vectorcraft-eps"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"craft-eps",
|
||||
"craft-image-codecs",
|
||||
"craft-pdf",
|
||||
"craft-stream-codecs",
|
||||
"encoding_rs",
|
||||
"fax",
|
||||
"flate2",
|
||||
"hayro-ccitt",
|
||||
"image",
|
||||
"kurbo 0.13.1",
|
||||
"memchr",
|
||||
@@ -6799,6 +6867,7 @@ dependencies = [
|
||||
name = "vectorcraft-mcp"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"craft-image-codecs",
|
||||
"image",
|
||||
"log",
|
||||
"proptest",
|
||||
@@ -6818,6 +6887,7 @@ dependencies = [
|
||||
name = "vectorcraft-metafile"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"craft-image-codecs",
|
||||
"craft-metafile",
|
||||
"image",
|
||||
"kurbo 0.13.1",
|
||||
@@ -6858,8 +6928,10 @@ version = "0.24.1"
|
||||
dependencies = [
|
||||
"aes",
|
||||
"chrono",
|
||||
"craft-image-codecs",
|
||||
"craft-pdf",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)",
|
||||
"craft-stream-codecs",
|
||||
"craft-xml",
|
||||
"flate2",
|
||||
"getrandom 0.4.3",
|
||||
"image",
|
||||
@@ -6884,15 +6956,14 @@ dependencies = [
|
||||
"vectorcraft-geom 0.24.1",
|
||||
"vectorcraft-testkit",
|
||||
"vectorcraft-text",
|
||||
"weezl 0.2.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-plugins"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"craft-image-codecs",
|
||||
"craft-vectorcraft-format",
|
||||
"image",
|
||||
"log",
|
||||
"serde",
|
||||
"serde_json",
|
||||
@@ -6910,11 +6981,12 @@ dependencies = [
|
||||
name = "vectorcraft-render"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"craft-gif",
|
||||
"craft-image-codecs",
|
||||
"craft-psd",
|
||||
"craft-stream-codecs",
|
||||
"craft-tiff",
|
||||
"flate2",
|
||||
"gif 0.14.2",
|
||||
"image",
|
||||
"jpeg-encoder",
|
||||
"log",
|
||||
@@ -6949,13 +7021,13 @@ name = "vectorcraft-svg"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"craft-docpath",
|
||||
"craft-image-codecs",
|
||||
"craft-svg",
|
||||
"data-url",
|
||||
"flate2",
|
||||
"image",
|
||||
"kurbo 0.13.1",
|
||||
"proptest",
|
||||
"resvg 0.45.1",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"simplecss",
|
||||
@@ -6978,8 +7050,8 @@ dependencies = [
|
||||
name = "vectorcraft-testkit"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"craft-image-codecs",
|
||||
"fax",
|
||||
"image",
|
||||
"kurbo 0.13.1",
|
||||
"proptest",
|
||||
"serde_json",
|
||||
@@ -7025,7 +7097,7 @@ dependencies = [
|
||||
name = "vectorcraft-tools"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"image",
|
||||
"craft-image-codecs",
|
||||
"kurbo 0.13.1",
|
||||
"serde",
|
||||
"serde_json",
|
||||
@@ -7057,6 +7129,7 @@ version = "0.24.1"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"chrono",
|
||||
"craft-image-codecs",
|
||||
"craft-keymap",
|
||||
"craft-ui",
|
||||
"craft-update",
|
||||
@@ -8011,7 +8084,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "winprint"
|
||||
version = "0.2.1"
|
||||
source = "git+https://github.com/storytold/winprint.rs?rev=5f80b9baf6b58937c401656634e42d33f00ddf49#5f80b9baf6b58937c401656634e42d33f00ddf49"
|
||||
source = "git+https://github.com/storytold/winprint.rs?rev=8ae097ef941b432b0e7671b1ec3a506800e65e57#8ae097ef941b432b0e7671b1ec3a506800e65e57"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"fmt-derive",
|
||||
@@ -8026,6 +8099,7 @@ dependencies = [
|
||||
"windows",
|
||||
"windows-core",
|
||||
"windows-implement",
|
||||
"windows-numerics",
|
||||
"xml",
|
||||
]
|
||||
|
||||
@@ -8485,12 +8559,6 @@ version = "1.0.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b"
|
||||
|
||||
[[package]]
|
||||
name = "zune-core"
|
||||
version = "0.4.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3f423a2c17029964870cfaabb1f13dfab7d092a62a29a89264f4d36990ca414a"
|
||||
|
||||
[[package]]
|
||||
name = "zune-core"
|
||||
version = "0.5.3"
|
||||
@@ -8506,22 +8574,13 @@ dependencies = [
|
||||
"simd-adler32",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zune-jpeg"
|
||||
version = "0.4.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "29ce2c8a9384ad323cf564b67da86e21d3cfdff87908bc1223ed5c99bc792713"
|
||||
dependencies = [
|
||||
"zune-core 0.4.12",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zune-jpeg"
|
||||
version = "0.5.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "27bc9d5b815bc103f142aa054f561d9187d191692ec7c2d1e2b4737f8dbd7296"
|
||||
dependencies = [
|
||||
"zune-core 0.5.3",
|
||||
"zune-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -8530,7 +8589,7 @@ version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cba11ecd07cc23351500e840ae03841b7e4997923ec8e4832ee3ae199ea0b6b4"
|
||||
dependencies = [
|
||||
"zune-core 0.5.3",
|
||||
"zune-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
+43
-41
@@ -11,35 +11,37 @@ rust-version = "1.95"
|
||||
repository = "https://github.com/storytold/vectorcraft"
|
||||
|
||||
[workspace.dependencies]
|
||||
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb", default-features = false, features = ["interpret", "write"] }
|
||||
craft-print = { git = "https://github.com/storytold/craft-libs", rev = "9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb" }
|
||||
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb" }
|
||||
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "c4b8405d5fd8f29cf77de528206bd2b38bb0ecb6", features = ["serde"] }
|
||||
craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb" }
|
||||
craft-proof = { git = "https://github.com/storytold/craft-libs", rev = "9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb" }
|
||||
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd", default-features = false, features = ["interpret", "write"] }
|
||||
craft-print = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd", features = ["serde"] }
|
||||
craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
craft-proof = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
# Decides who draws the window frame (desktop vs. app) so the suite shares one native titlebar per
|
||||
# desktop. The `egui` feature adds `craft_window::decorate`. (craft-libs#217.)
|
||||
craft-window = { git = "https://github.com/storytold/craft-libs", rev = "9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb", features = ["egui"] }
|
||||
craft-window = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd", features = ["egui"] }
|
||||
# The `.vectorcraft` envelope: its members, version, embedded images and the data after the JSON.
|
||||
craft-vectorcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "b0d84cd15db39f2af9869cfb47b8b72cbeca3f20" }
|
||||
craft-gzip = { git = "https://github.com/storytold/craft-libs", rev = "b0d84cd15db39f2af9869cfb47b8b72cbeca3f20" }
|
||||
craft-vectorcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
craft-gzip = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
# craft-update: app-specific pretend versions apply to startup and manual checks.
|
||||
# Native Affinity documents (craft-libs#914).
|
||||
craft-affinity = { git = "https://github.com/storytold/craft-libs", rev = "228f6d0c50f1fdee4c5124603602993fc79d4c9d" }
|
||||
craft-affinity = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
# SVG parsing, limits, SVGZ and writer (craft-libs#778).
|
||||
craft-svg = { git = "https://github.com/storytold/craft-libs", rev = "6d620e971e820b517b11a99cad040ebac670e1e2", features = ["tree"] }
|
||||
craft-svg = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd", features = ["tree"] }
|
||||
# EPS container, previews, ASCII85 and DSC comments (craft-libs#778).
|
||||
craft-eps = { git = "https://github.com/storytold/craft-libs", rev = "6d620e971e820b517b11a99cad040ebac670e1e2" }
|
||||
craft-eps = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
# DXF tags, reader and writer (craft-libs#778, #926, #927).
|
||||
craft-dxf = { git = "https://github.com/storytold/craft-libs", rev = "8bd1412eaa7c0b58f3f9c401495d35fe12d6d85e" }
|
||||
craft-dxf = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
# WMF, EMF and EMF+ reader (craft-libs#778).
|
||||
craft-metafile = { git = "https://github.com/storytold/craft-libs", rev = "6d620e971e820b517b11a99cad040ebac670e1e2" }
|
||||
craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "e2dc681a3" }
|
||||
craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "e2dc681a3" }
|
||||
craft-gif = { git = "https://github.com/storytold/craft-libs", rev = "e2dc681a3" }
|
||||
craft-tiff = { git = "https://github.com/storytold/craft-libs", rev = "e2dc681a3" }
|
||||
craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "e2dc681a3" }
|
||||
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "e98299546f5c5c3c22388d16507f8b5e61a5f280", features = ["egui"] }
|
||||
craft-metafile = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
craft-gif = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
craft-tiff = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
craft-stream-codecs = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
craft-jxl = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd", features = ["egui"] }
|
||||
# internal
|
||||
vectorcraft-geom = { path = "crates/geom" }
|
||||
vectorcraft-color = { path = "crates/color" }
|
||||
@@ -97,10 +99,12 @@ wat = "1.228"
|
||||
# Without `text` (glyph runs, through glifo and a second skrifa): the renderer draws the outlines
|
||||
# vectorcraft-text gives it. The other default features stay.
|
||||
vello_cpu = { version = "0.2", default-features = false, features = ["std", "png", "u8_pipeline", "f32_pipeline"] }
|
||||
# The image crate still decodes and encodes for the readers not yet moved to craft-image-codecs
|
||||
# (File › Open's raster path, PDF and SVG images, the renderer's image paint, DIBs). Its format
|
||||
# features stay until the last of those moves, so no file that opens today stops opening.
|
||||
image = { version = "0.25", default-features = false, features = ["png", "jpeg", "webp", "gif", "tiff", "bmp", "tga"] }
|
||||
# Without `text` and the system fonts (fontdb, harfrust and skrifa, at other versions than ours):
|
||||
# SVG import reads `<text>` from the XML itself and never gives usvg a font. A test of the SVG
|
||||
# crate turns `text` on.
|
||||
# SVG import reads `<text>` from the XML itself and never gives usvg a font.
|
||||
usvg = { version = "0.48", default-features = false, features = ["svgz"] }
|
||||
# Without `simple-text` (its text shaping, the only user of rustybuzz and ttf-parser, which are
|
||||
# unmaintained, #1730): the PDF writer places glyphs itself. A test of the PDF crate turns it on.
|
||||
@@ -112,18 +116,9 @@ flate2 = { version = "1", default-features = false, features = ["rust_backend"]
|
||||
rustix = { version = "1.1", default-features = false }
|
||||
# Zstandard decoder (pure Rust, builds for wasm32 too): the editing copy an EPS carries of its art.
|
||||
ruzstd = { version = "0.9.1", default-features = false, features = ["std"] }
|
||||
# CCITT Group 3 and Group 4 decoder (ITU-T T.4 and T.6): EPS images read through
|
||||
# `CCITTFaxDecode`. The PDF reader's hayro-syntax uses the same version.
|
||||
hayro-ccitt = "0.3"
|
||||
# Substring search for the EPS reader's PostScript `search`.
|
||||
memchr = "2"
|
||||
# The WHATWG Encoding Standard's legacy encodings: names in legacy .ai files read in the code page
|
||||
# the user picks (Mac OS Roman, Korean, Japanese, Chinese).
|
||||
encoding_rs = "0.8.42"
|
||||
jpeg-encoder = { version = "0.6", default-features = false, features = ["std"] }
|
||||
gif = { version = "0.14", default-features = false, features = ["std", "raii_no_panic"] }
|
||||
# CMYK image samples (vectorcraft-doc's cmyk module): the image crate's own TIFF and JPEG
|
||||
# decoders, read directly (the image crate converts CMYK to RGB).
|
||||
# vectorcraft-doc's CMYK TIFF reader and writer (src/cmyk.rs) still use `tiff`. `zune-jpeg` and
|
||||
# `hayro-ccitt` are only the references tests compare craft-image-codecs' CMYK JPEG inks and
|
||||
# craft-stream-codecs' Group 3 rows with (vectorcraft-doc's and vectorcraft-engine's dev-dependencies).
|
||||
tiff = "0.11"
|
||||
zune-jpeg = "0.5"
|
||||
# JPEG XL decoder (pure Rust, MIT OR Apache-2.0): File › Open and Place read `.jxl` images. It is
|
||||
@@ -136,6 +131,13 @@ jxl-grid = "0.6.2"
|
||||
# Tests: a lossless JPEG XL encoder (pure Rust, MIT OR Apache-2.0 OR Zlib) for the `.jxl` test files.
|
||||
zune-jpegxl = { version = "0.5.2", default-features = false, features = ["std"] }
|
||||
zune-core = "0.5"
|
||||
hayro-ccitt = "0.3"
|
||||
jpeg-encoder = { version = "0.6", default-features = false, features = ["std"] }
|
||||
# Substring search for the EPS reader's PostScript `search`.
|
||||
memchr = "2"
|
||||
# The WHATWG Encoding Standard's legacy encodings: names in legacy .ai files read in the code page
|
||||
# the user picks (Mac OS Roman, Korean, Japanese, Chinese).
|
||||
encoding_rs = "0.8.42"
|
||||
|
||||
# ui
|
||||
egui = "0.37"
|
||||
@@ -179,13 +181,13 @@ lto = "thin"
|
||||
codegen-units = 1
|
||||
|
||||
[patch.crates-io]
|
||||
hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb" }
|
||||
hayro-interpret = { git = "https://github.com/storytold/craft-libs", rev = "9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb" }
|
||||
hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb" }
|
||||
hayro-cmap = { git = "https://github.com/storytold/craft-libs", rev = "9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb" }
|
||||
hayro-postscript = { git = "https://github.com/storytold/craft-libs", rev = "9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb" }
|
||||
krilla = { git = "https://github.com/storytold/craft-libs", rev = "9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb" }
|
||||
linesweeper = { git = "https://github.com/storytold/craft-libs", rev = "481f66d287b2a06845f3667a19daf35f0fe958ed" }
|
||||
hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
hayro-interpret = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
hayro-cmap = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
hayro-postscript = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
krilla = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
linesweeper = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
ecolor = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
|
||||
emath = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
|
||||
epaint = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
|
||||
|
||||
@@ -146,3 +146,125 @@ Indic hyphenation patterns (crates/text/patterns/):
|
||||
Copyright (C) 2016 Santhosh Thottingal, used under the MIT license option.
|
||||
Original copyright and full license notices are preserved in each hyph-*.tex file.
|
||||
Source: hyphenation/tex-hyphen, commit 5684c0f51c0b81133db2efbe60a408b4155a3ff5.
|
||||
|
||||
|
||||
JPEG encoder dependency notices
|
||||
|
||||
jpeg-encoder 0.6.1, https://crates.io/crates/jpeg-encoder/0.6.1
|
||||
Licensed under (MIT OR Apache-2.0) AND IJG. The MIT option and the IJG and SIMD
|
||||
notices are reproduced below. Original license texts and provenance hashes are
|
||||
retained in licenses/jpeg-encoder-0.6.1/.
|
||||
|
||||
--- jpeg-encoder 0.6.1: LICENSE-MIT ---
|
||||
|
||||
Copyright (c) 2021 Volker Ströbel <volkerstroebel@mysurdity.de>
|
||||
|
||||
Permission is hereby granted, free of charge, to any
|
||||
person obtaining a copy of this software and associated
|
||||
documentation files (the "Software"), to deal in the
|
||||
Software without restriction, including without
|
||||
limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software
|
||||
is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice
|
||||
shall be included in all copies or substantial portions
|
||||
of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
||||
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
||||
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
|
||||
--- jpeg-encoder 0.6.1: IJG-NOTICE.txt ---
|
||||
|
||||
/*
|
||||
* Ported from mozjpeg to rust
|
||||
*
|
||||
* This file was part of the Independent JPEG Group's software:
|
||||
* Copyright (C) 1991-1996, Thomas G. Lane.
|
||||
* libjpeg-turbo Modifications:
|
||||
* Copyright (C) 2015, 2020, D. R. Commander.
|
||||
*
|
||||
* Conditions of distribution and use:
|
||||
* In plain English:
|
||||
*
|
||||
* 1. We don't promise that this software works. (But if you find any bugs,
|
||||
* please let us know!)
|
||||
* 2. You can use this software for whatever you want. You don't have to pay us.
|
||||
* 3. You may not pretend that you wrote this software. If you use it in a
|
||||
* program, you must acknowledge somewhere in your documentation that
|
||||
* you've used the IJG code.
|
||||
*
|
||||
* In legalese:
|
||||
*
|
||||
* The authors make NO WARRANTY or representation, either express or implied,
|
||||
* with respect to this software, its quality, accuracy, merchantability, or
|
||||
* fitness for a particular purpose. This software is provided "AS IS", and you,
|
||||
* its user, assume the entire risk as to its quality and accuracy.
|
||||
*
|
||||
* This software is copyright (C) 1991-2020, Thomas G. Lane, Guido Vollbeding.
|
||||
* All Rights Reserved except as specified below.
|
||||
*
|
||||
* Permission is hereby granted to use, copy, modify, and distribute this
|
||||
* software (or portions thereof) for any purpose, without fee, subject to these
|
||||
* conditions:
|
||||
* (1) If any part of the source code for this software is distributed, then this
|
||||
* README file must be included, with this copyright and no-warranty notice
|
||||
* unaltered; and any additions, deletions, or changes to the original files
|
||||
* must be clearly indicated in accompanying documentation.
|
||||
* (2) If only executable code is distributed, then the accompanying
|
||||
* documentation must state that "this software is based in part on the work of
|
||||
* the Independent JPEG Group".
|
||||
* (3) Permission for use of this software is granted only if the user accepts
|
||||
* full responsibility for any undesirable consequences; the authors accept
|
||||
* NO LIABILITY for damages of any kind.
|
||||
*
|
||||
* These conditions apply to any software derived from or based on the IJG code,
|
||||
* not just to the unmodified library. If you use our work, you ought to
|
||||
* acknowledge us.
|
||||
*
|
||||
* Permission is NOT granted for the use of any IJG author's name or company name
|
||||
* in advertising or publicity relating to this software or products derived from
|
||||
* it. This software may be referred to only as "the Independent JPEG Group's
|
||||
* software".
|
||||
*
|
||||
* We specifically permit and encourage the use of this software as the basis of
|
||||
* commercial products, provided that all warranty or liability claims are
|
||||
* assumed by the product vendor.
|
||||
*
|
||||
* This file contains a slower but more accurate integer implementation of the
|
||||
* forward DCT (Discrete Cosine Transform).
|
||||
*
|
||||
* A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
|
||||
* on each column. Direct algorithms are also available, but they are
|
||||
* much more complex and seem not to be any faster when reduced to code.
|
||||
*
|
||||
* This implementation is based on an algorithm described in
|
||||
* C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
|
||||
* Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
|
||||
* Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
|
||||
* The primary algorithm described there uses 11 multiplies and 29 adds.
|
||||
* We use their alternate method with 12 multiplies and 32 adds.
|
||||
* The advantage of this method is that no data path contains more than one
|
||||
* multiplication; this allows a very simple and accurate implementation in
|
||||
* scaled fixed-point arithmetic, with a minimal number of shifts.
|
||||
*/
|
||||
|
||||
--- jpeg-encoder 0.6.1: SIMD-NOTICE.txt ---
|
||||
|
||||
/*
|
||||
* Ported from mozjpeg / jfdctint-avx2.asm to rust
|
||||
* Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
|
||||
* Copyright (C) 2009, 2016, 2018, 2020, D. R. Commander.
|
||||
*
|
||||
* Based on the x86 SIMD extension for IJG JPEG library
|
||||
* Copyright (C) 1999-2006, MIYASAKA Masaru.
|
||||
*/
|
||||
@@ -20,4 +20,4 @@ craft-affinity = { workspace = true }
|
||||
proptest = { workspace = true }
|
||||
# The tests build containers and decode previews.
|
||||
crc32fast = { workspace = true }
|
||||
image = { workspace = true }
|
||||
craft-image-codecs = { workspace = true }
|
||||
@@ -50,8 +50,8 @@ fn source_backed_tiles_retain_the_embedded_jpeg_pixels() {
|
||||
assert_eq!((image.width, image.height), (400, 300));
|
||||
assert_eq!(image.transform, Affine::IDENTITY);
|
||||
let Pixels::Rgba8(pixels) = &image.pixels else { panic!("expected retained level-zero pixels") };
|
||||
let expected = image::load_from_memory(&source(&bytes)).unwrap().into_rgba8();
|
||||
assert_eq!(pixels.as_slice(), expected.as_raw());
|
||||
let expected = craft_image_codecs::decode(&source(&bytes)).unwrap().to_rgba8();
|
||||
assert_eq!(pixels.as_slice(), expected.as_slice());
|
||||
assert!(pixels.as_chunks::<4>().0.iter().all(|p| p[3] == 255));
|
||||
assert!(pixels.as_chunks::<4>().0.iter().any(|p| p[0] != p[1]));
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ vectorcraft-pathops = { workspace = true }
|
||||
vectorcraft-text = { workspace = true }
|
||||
kurbo = { workspace = true }
|
||||
image = { workspace = true }
|
||||
craft-image-codecs = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
serde_json = { workspace = true }
|
||||
|
||||
+20
-19
@@ -5,7 +5,6 @@
|
||||
//! filled outlines where the look needs them.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::io::Cursor;
|
||||
|
||||
use kurbo::PathEl;
|
||||
use vectorcraft_color::Paint;
|
||||
@@ -1403,35 +1402,37 @@ impl<'a> Scene<'a> {
|
||||
|
||||
/// Image `im` as a file in the chosen format, and its size in pixels.
|
||||
fn encode_image(&self, im: &ImageObject) -> Result<(DxfImage, (u32, u32)), String> {
|
||||
use craft_image_codecs::{ChannelLayout, EncodeOptions, Format, Image as CodecImage, SampleType};
|
||||
let blob = self.doc.image_blob(im).filter(|b| !b.bytes.is_empty()).ok_or_else(|| "an image has no pixels and was left out".to_string())?;
|
||||
// The pixels it converts to (RGBA, 4 bytes each) are read from its header before decoding.
|
||||
let (w, h) = image::ImageReader::new(Cursor::new(blob.bytes.as_slice()))
|
||||
.with_guessed_format()
|
||||
.ok()
|
||||
.and_then(|r| r.into_dimensions().ok())
|
||||
.ok_or_else(|| UNDECODED.to_string())?;
|
||||
let (w, h) = vectorcraft_doc::header_size(&blob.bytes).ok_or_else(|| UNDECODED.to_string())?;
|
||||
if u64::from(w) * u64::from(h) > MAX_IMAGE_PIXELS {
|
||||
return Err("an image is too large for DXF and was left out".to_string());
|
||||
}
|
||||
let img = image::load_from_memory(&blob.bytes).map_err(|_| UNDECODED.to_string())?;
|
||||
let size = (img.width(), img.height());
|
||||
let mut bytes = vec![];
|
||||
let written = match self.o.raster {
|
||||
RasterFormat::Png => img.to_rgba8().write_to(&mut Cursor::new(&mut bytes), image::ImageFormat::Png),
|
||||
let decoded = craft_image_codecs::decode(&blob.bytes).map_err(|_| UNDECODED.to_string())?;
|
||||
let size = decoded.dimensions();
|
||||
let mut rgba = decoded.to_rgba8();
|
||||
let bytes = match self.o.raster {
|
||||
RasterFormat::Png => {
|
||||
let img = CodecImage::from_raw(size.0, size.1, ChannelLayout::Rgba, SampleType::U8, rgba)
|
||||
.map_err(|e| format!("an image could not be written: {e}"))?;
|
||||
craft_image_codecs::encode(&img, Format::Png, &EncodeOptions::default())
|
||||
}
|
||||
RasterFormat::Jpeg => {
|
||||
// No alpha in JPEG: transparent pixels over white.
|
||||
let mut rgba = img.to_rgba8();
|
||||
for p in rgba.pixels_mut() {
|
||||
let mut rgb = Vec::with_capacity(rgba.len() / 4 * 3);
|
||||
for p in rgba.as_chunks_mut::<4>().0 {
|
||||
let a = u32::from(p[3]);
|
||||
for c in 0..3 {
|
||||
p[c] = ((u32::from(p[c]) * a + 255 * (255 - a)) / 255) as u8;
|
||||
for c in &p[..3] {
|
||||
rgb.push(((u32::from(*c) * a + 255 * (255 - a)) / 255) as u8);
|
||||
}
|
||||
}
|
||||
let rgb = image::DynamicImage::ImageRgba8(rgba).to_rgb8();
|
||||
rgb.write_with_encoder(image::codecs::jpeg::JpegEncoder::new_with_quality(&mut bytes, 90))
|
||||
let img = CodecImage::from_raw(size.0, size.1, ChannelLayout::Rgb, SampleType::U8, rgb)
|
||||
.map_err(|e| format!("an image could not be written: {e}"))?;
|
||||
craft_image_codecs::encode(&img, Format::Jpeg, &EncodeOptions { jpeg_quality: 90, ..EncodeOptions::default() })
|
||||
}
|
||||
};
|
||||
written.map_err(|e| format!("an image could not be written: {e}"))?;
|
||||
}
|
||||
.map_err(|e| format!("an image could not be written: {e}"))?;
|
||||
Ok((DxfImage { name: format!("{}.{}", blob.content_key(), self.o.raster.extension()), bytes }, size))
|
||||
}
|
||||
}
|
||||
|
||||
+26
-4
@@ -852,8 +852,19 @@ fn the_cnc_preset_writes_version_2000_in_millimeters_with_type_as_paths_and_stro
|
||||
/// A 4 × 2 px PNG placed at (10, 20) under key `raster-1`, and before it in paint order
|
||||
/// `fill`'s rectangle.
|
||||
fn image_and_fill_doc(fill: Paint) -> Document {
|
||||
let mut png = vec![];
|
||||
image::RgbaImage::from_pixel(4, 2, image::Rgba([0, 0, 255, 255])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
let png = craft_image_codecs::encode(
|
||||
&craft_image_codecs::Image::from_raw(
|
||||
4,
|
||||
2,
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U8,
|
||||
[0u8, 0, 255, 255].repeat(8),
|
||||
)
|
||||
.unwrap(),
|
||||
craft_image_codecs::Format::Png,
|
||||
&craft_image_codecs::EncodeOptions::default(),
|
||||
)
|
||||
.unwrap();
|
||||
let mut d = doc_with(|d| {
|
||||
let im = ImageObject {
|
||||
key: "raster-1".into(),
|
||||
@@ -1312,8 +1323,19 @@ fn a_font_height_reference_is_written_as_the_size_drawn() {
|
||||
|
||||
#[test]
|
||||
fn images_link_png_or_jpeg_files() {
|
||||
let mut png = vec![];
|
||||
image::RgbaImage::from_pixel(4, 2, image::Rgba([255, 0, 0, 128])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
let png = craft_image_codecs::encode(
|
||||
&craft_image_codecs::Image::from_raw(
|
||||
4,
|
||||
2,
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U8,
|
||||
[255u8, 0, 0, 128].repeat(8),
|
||||
)
|
||||
.unwrap(),
|
||||
craft_image_codecs::Format::Png,
|
||||
&craft_image_codecs::EncodeOptions::default(),
|
||||
)
|
||||
.unwrap();
|
||||
let mut d = doc_with(|d| {
|
||||
let im = ImageObject {
|
||||
key: "raster-1".into(),
|
||||
|
||||
@@ -17,15 +17,19 @@ vectorcraft-geom = { workspace = true }
|
||||
vectorcraft-color = { workspace = true }
|
||||
kurbo = { workspace = true }
|
||||
image = { workspace = true }
|
||||
# Image headers (PNG and JPEG sizes, `pixels::header_size`) and CMYK JPEG inks (`cmyk`).
|
||||
craft-image-codecs = { workspace = true }
|
||||
tiff = { workspace = true }
|
||||
# 1-bit TIFFs compressed with CCITT Group 3 (Compression 2 and 3).
|
||||
hayro-ccitt = { workspace = true }
|
||||
zune-jpeg = { workspace = true }
|
||||
craft-stream-codecs = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
chrono = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
flate2 = { workspace = true }
|
||||
# The CMYK JPEG reader `cmyk` had before craft-image-codecs, kept as the reference its inks match.
|
||||
zune-jpeg = { workspace = true }
|
||||
jpeg-encoder = { workspace = true }
|
||||
proptest = { workspace = true }
|
||||
+148
-108
@@ -9,10 +9,6 @@
|
||||
use std::io::Cursor;
|
||||
use std::sync::Arc;
|
||||
|
||||
use zune_jpeg::zune_core::bytestream::ZCursor;
|
||||
use zune_jpeg::zune_core::colorspace::ColorSpace;
|
||||
use zune_jpeg::zune_core::options::DecoderOptions;
|
||||
|
||||
use crate::ImageBlob;
|
||||
|
||||
/// Most pixels a CMYK image is decoded with (256 MB of samples): larger ones decode as RGB.
|
||||
@@ -44,12 +40,7 @@ fn pixels(width: u32, height: u32) -> Option<u64> {
|
||||
/// The number of colour components of JPEG `bytes` (its frame header); `None` when they aren't a
|
||||
/// JPEG.
|
||||
pub fn jpeg_components(bytes: &[u8]) -> Option<usize> {
|
||||
if !bytes.starts_with(&[0xFF, 0xD8]) {
|
||||
return None;
|
||||
}
|
||||
let mut d = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(bytes), DecoderOptions::default());
|
||||
d.decode_headers().ok()?;
|
||||
Some(d.info()?.components as usize)
|
||||
craft_image_codecs::jpeg_inks::jpeg_components(bytes)
|
||||
}
|
||||
|
||||
fn is_tiff(bytes: &[u8]) -> bool {
|
||||
@@ -645,9 +636,7 @@ pub fn tiff_icc_profile(bytes: &[u8]) -> Option<Vec<u8>> {
|
||||
/// The ICC profile of a JPEG, put together from all its numbered `ICC_PROFILE` APP2 segments;
|
||||
/// `None` without one.
|
||||
fn jpeg_icc(bytes: &[u8]) -> Option<Vec<u8>> {
|
||||
let mut d = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(bytes), DecoderOptions::default());
|
||||
d.decode_headers().ok()?;
|
||||
d.icc_profile().filter(|icc| !icc.is_empty())
|
||||
craft_image_codecs::jpeg_inks::jpeg_icc_profile(bytes)
|
||||
}
|
||||
|
||||
/// The bytes after an `ICC_PROFILE` APP2 segment's length: its name, then the segment's number and
|
||||
@@ -851,10 +840,7 @@ impl ImageBlob {
|
||||
pub fn header_size(&self) -> Option<(u32, u32)> {
|
||||
let bytes = self.bytes.as_slice();
|
||||
if bytes.starts_with(&[0xFF, 0xD8]) {
|
||||
let mut d = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(bytes), DecoderOptions::default());
|
||||
d.decode_headers().ok()?;
|
||||
let (w, h) = d.dimensions()?;
|
||||
return Some((u32::try_from(w).ok()?, u32::try_from(h).ok()?));
|
||||
return craft_image_codecs::jpeg_inks::jpeg_dimensions(bytes);
|
||||
}
|
||||
tiff(bytes).and_then(|(_, mut d)| d.dimensions().ok())
|
||||
}
|
||||
@@ -932,30 +918,10 @@ fn sample_inks(inks: &Inks, x: f64, y: f64, size: u32) -> Option<[u8; 4]> {
|
||||
/// The ink amounts of a four-component JPEG (its samples inverted back; YCCK converted to CMYK
|
||||
/// first, as JPEG readers do).
|
||||
fn jpeg_inks(bytes: &[u8]) -> Option<Inks> {
|
||||
let mut d = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(bytes), DecoderOptions::default());
|
||||
d.decode_headers().ok()?;
|
||||
let space = d.input_colorspace()?;
|
||||
if d.info()?.components != 4 || !matches!(space, ColorSpace::CMYK | ColorSpace::YCCK) {
|
||||
return None;
|
||||
}
|
||||
let (w, h) = d.dimensions()?;
|
||||
let (w, h) = (u32::try_from(w).ok()?, u32::try_from(h).ok()?);
|
||||
pixels(w, h)?;
|
||||
d.set_options(DecoderOptions::default().jpeg_set_out_colorspace(space));
|
||||
let mut data = d.decode().ok()?;
|
||||
for p in data.as_chunks_mut::<4>().0 {
|
||||
if space == ColorSpace::YCCK {
|
||||
let [y, cb, cr] = [p[0], p[1], p[2]].map(f32::from);
|
||||
// libjpeg's YCCK → CMYK: the complement of the YCbCr colour's RGB.
|
||||
p[0] = (434.456 - y - 1.402 * cr).clamp(0.0, 255.0) as u8;
|
||||
p[1] = (119.541 - y + 0.344 * cb + 0.714 * cr).clamp(0.0, 255.0) as u8;
|
||||
p[2] = (481.816 - y - 1.772 * cb).clamp(0.0, 255.0) as u8;
|
||||
}
|
||||
for v in p.iter_mut() {
|
||||
*v = 255 - *v;
|
||||
}
|
||||
}
|
||||
Inks::new(w, h, data)
|
||||
let limits = craft_image_codecs::Limits { max_pixels: MAX_CMYK_PIXELS, ..craft_image_codecs::Limits::default() };
|
||||
let image = craft_image_codecs::jpeg_inks::decode_jpeg_inks(bytes, &limits).ok()?;
|
||||
let (w, h) = image.dimensions();
|
||||
Inks::new(w, h, image.into_data())
|
||||
}
|
||||
|
||||
/// The ink amounts of a CMYK TIFF (16-bit samples to 8 bits), its inks stored together or one plane
|
||||
@@ -1107,9 +1073,20 @@ pub enum Group3Error {
|
||||
TooLarge { w: u32, h: u32 },
|
||||
/// A strip or tile ends before its last row or can't be decoded (it holds a code the decoder
|
||||
/// doesn't read, such as T.4's uncompressed mode, or a row of the wrong width), or a tile is
|
||||
/// larger than [`crate::MAX_RASTER_ALLOC`] allows as RGBA.
|
||||
#[error("could not decode image: {0}")]
|
||||
Damaged(hayro_ccitt::DecodeError),
|
||||
/// larger than [`crate::MAX_RASTER_ALLOC`] allows as RGBA, or is wider than
|
||||
/// [`craft_stream_codecs::MAX_CCITT_COLUMNS`].
|
||||
#[error("could not decode image: {}", damage(*.0))]
|
||||
Damaged(craft_stream_codecs::CcittErrorKind),
|
||||
}
|
||||
|
||||
/// What went wrong in a Group 3 strip or tile, as [`Group3Error::Damaged`] says it.
|
||||
fn damage(kind: craft_stream_codecs::CcittErrorKind) -> &'static str {
|
||||
use craft_stream_codecs::CcittErrorKind as K;
|
||||
match kind {
|
||||
K::UnexpectedEof => "unexpected end of input",
|
||||
K::Overflow => "arithmetic overflow in position calculation",
|
||||
K::InvalidData => "invalid CCITT code sequence",
|
||||
}
|
||||
}
|
||||
|
||||
/// The pixels of a 1-bit TIFF compressed with CCITT Group 3, which the image decoder doesn't read,
|
||||
@@ -1124,7 +1101,7 @@ pub enum Group3Error {
|
||||
/// an error when the pixels would take more than [`crate::MAX_RASTER_ALLOC`] as RGBA, a tile is
|
||||
/// larger than that allowance, or a strip or tile can't be decoded or ends before its last row.
|
||||
pub fn group3_tiff_rgba(bytes: &[u8]) -> Option<Result<image::RgbaImage, Group3Error>> {
|
||||
use hayro_ccitt::{DecodeError, DecodeSettings, DecoderContext, EncodingMode};
|
||||
use craft_stream_codecs::{CcittErrorKind as DecodeError, CcittParams, decode_ccitt_raw};
|
||||
use tiff::tags::Tag;
|
||||
let (kind, mut d) = tiff(bytes)?;
|
||||
let compression = d.find_tag_unsigned::<u16>(Tag::Compression).ok().flatten()?;
|
||||
@@ -1143,12 +1120,8 @@ pub fn group3_tiff_rgba(bytes: &[u8]) -> Option<Result<image::RgbaImage, Group3E
|
||||
}
|
||||
let t4_options = d.find_tag_unsigned::<u32>(Tag::Unknown(292)).ok().flatten().unwrap_or(0);
|
||||
let lsb_first = d.find_tag_unsigned::<u16>(Tag::FillOrder).ok().flatten() == Some(2);
|
||||
let encoding = if compression == 3 && t4_options & 1 != 0 {
|
||||
// Each row's tag bit says how it is coded.
|
||||
EncodingMode::Group3_2D { k: 2 }
|
||||
} else {
|
||||
EncodingMode::Group3_1D
|
||||
};
|
||||
// Each row's tag bit says how it is coded (K above 0), or every row is one-dimensional (K 0).
|
||||
let k = if compression == 3 && t4_options & 1 != 0 { 2 } else { 0 };
|
||||
// The width and height of each strip or tile, and the tags of their offsets and byte counts.
|
||||
let tiled = d.get_chunk_type() == tiff::decoder::ChunkType::Tile;
|
||||
let (cw, ch, offsets, counts) = if tiled {
|
||||
@@ -1182,76 +1155,54 @@ pub fn group3_tiff_rgba(bytes: &[u8]) -> Option<Result<image::RgbaImage, Group3E
|
||||
budget -= len;
|
||||
let data = bytes.get(start..start + len)?;
|
||||
let data: std::borrow::Cow<[u8]> = if lsb_first { data.iter().map(|b| b.reverse_bits()).collect() } else { data.into() };
|
||||
let settings = DecodeSettings {
|
||||
columns: cw,
|
||||
// A tile is coded whole; the last strip only has the image's remaining rows.
|
||||
rows: if tiled { ch } else { ch.min(h - y0) },
|
||||
end_of_block: compression == 3,
|
||||
// A tile is coded whole; the last strip only has the image's remaining rows.
|
||||
let rows = if tiled { ch } else { ch.min(h - y0) };
|
||||
let params = CcittParams {
|
||||
k,
|
||||
columns: i64::from(cw),
|
||||
rows: i64::from(rows),
|
||||
end_of_line: compression == 3,
|
||||
rows_are_byte_aligned: compression == 2,
|
||||
encoding,
|
||||
invert_black: false,
|
||||
encoded_byte_align: compression == 2,
|
||||
end_of_block: compression == 3,
|
||||
// 1 bits are black runs, 0 bits white ones.
|
||||
black_is_1: true,
|
||||
};
|
||||
let mut rows = Rows { out: &mut gray, width: w, height: h, x0, y0, x: 0, y: 0, zero };
|
||||
if let Err(e) = hayro_ccitt::decode(&data, &mut rows, &mut DecoderContext::new(settings)) {
|
||||
let Ok(out) = decode_ccitt_raw(&data, ¶ms, rows) else {
|
||||
// Wider than the decoder reads.
|
||||
return damaged(DecodeError::Overflow);
|
||||
};
|
||||
if let Some(e) = out.error {
|
||||
return damaged(e);
|
||||
}
|
||||
// The data ended, or six EOL codes came, before the strip's or tile's last row.
|
||||
if rows.y < settings.rows {
|
||||
if out.rows_decoded < rows {
|
||||
return damaged(DecodeError::UnexpectedEof);
|
||||
}
|
||||
put_rows(&mut gray, (w, h), (x0, y0), cw, &out.data, zero);
|
||||
}
|
||||
let gray = image::GrayImage::from_raw(w, h, gray)?;
|
||||
Some(Ok(image::DynamicImage::ImageLuma8(gray).to_rgba8()))
|
||||
}
|
||||
|
||||
/// Where a strip or tile of a Group 3 TIFF ([`group3_tiff_rgba`]) puts its decoded pixels: `out`
|
||||
/// holds the image's levels row by row, `width` × `height`, and the strip or tile starts at (`x0`,
|
||||
/// `y0`). Pixels past the image's right or bottom edge (a tile's padding) are dropped.
|
||||
struct Rows<'a> {
|
||||
out: &'a mut [u8],
|
||||
width: u32,
|
||||
height: u32,
|
||||
x0: u32,
|
||||
y0: u32,
|
||||
/// The next pixel in the strip or tile.
|
||||
x: u64,
|
||||
y: u32,
|
||||
/// The level of a white run.
|
||||
zero: u8,
|
||||
}
|
||||
|
||||
impl Rows<'_> {
|
||||
/// The next `n` pixels of the row, a white run's or a black run's.
|
||||
fn run(&mut self, white: bool, n: u64) {
|
||||
let level = if white { self.zero } else { u8::MAX - self.zero };
|
||||
let row = u64::from(self.y0) + u64::from(self.y);
|
||||
let left = u64::from(self.x0) + self.x;
|
||||
let right = left.saturating_add(n).min(u64::from(self.width));
|
||||
if row < u64::from(self.height) && left < right {
|
||||
let at = row * u64::from(self.width);
|
||||
if let (Ok(a), Ok(b)) = (usize::try_from(at + left), usize::try_from(at + right))
|
||||
&& let Some(px) = self.out.get_mut(a..b)
|
||||
{
|
||||
px.fill(level);
|
||||
}
|
||||
/// The decoded rows `packed` of a strip or tile of a Group 3 TIFF ([`group3_tiff_rgba`]), `columns`
|
||||
/// wide, one bit a pixel (1 a black run's), put into `out`, the image's levels row by row, `size`
|
||||
/// (width, height), from (`x0`, `y0`): a 0 bit as `zero` (the level of a white run), a 1 bit as the
|
||||
/// other level. Pixels past the image's right or bottom edge (a tile's padding) are dropped.
|
||||
fn put_rows(out: &mut [u8], size: (u32, u32), (x0, y0): (u32, u32), columns: u32, packed: &[u8], zero: u8) {
|
||||
let (width, height) = (size.0 as usize, size.1 as usize);
|
||||
let (x0, y0, columns) = (x0 as usize, y0 as usize, columns as usize);
|
||||
let stride = columns.div_ceil(8);
|
||||
if stride == 0 {
|
||||
return;
|
||||
}
|
||||
let across = columns.min(width.saturating_sub(x0));
|
||||
for (y, row) in packed.chunks(stride).enumerate() {
|
||||
let Some(at) = (y0 + y < height).then(|| (y0 + y) * width + x0) else { break };
|
||||
let Some(px) = out.get_mut(at..at + across) else { break };
|
||||
for (x, p) in px.iter_mut().enumerate() {
|
||||
let black = row.get(x / 8).is_some_and(|b| b & (0x80 >> (x % 8)) != 0);
|
||||
*p = if black { u8::MAX - zero } else { zero };
|
||||
}
|
||||
self.x = self.x.saturating_add(n);
|
||||
}
|
||||
}
|
||||
|
||||
impl hayro_ccitt::Decoder for Rows<'_> {
|
||||
fn push_pixel(&mut self, white: bool) {
|
||||
self.run(white, 1);
|
||||
}
|
||||
|
||||
fn push_pixel_chunk(&mut self, white: bool, chunk_count: u32) {
|
||||
self.run(white, u64::from(chunk_count) * 8);
|
||||
}
|
||||
|
||||
fn next_line(&mut self) {
|
||||
self.y = self.y.saturating_add(1);
|
||||
self.x = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2209,4 +2160,93 @@ mod tests {
|
||||
assert_eq!(Inks::new(0, 2, vec![]), None, "empty");
|
||||
assert_eq!(Inks::new(1 << 14, 1 << 13, vec![]), None, "too large");
|
||||
}
|
||||
|
||||
/// The CMYK JPEG reader before craft-image-codecs read the inks (zune-jpeg, every sample
|
||||
/// inverted, libjpeg's truncating YCCK conversion), the reference [`jpeg_inks`] matches.
|
||||
fn zune_inks(bytes: &[u8]) -> Option<Inks> {
|
||||
use zune_jpeg::zune_core::{bytestream::ZCursor, colorspace::ColorSpace, options::DecoderOptions};
|
||||
let mut d = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(bytes), DecoderOptions::default());
|
||||
d.decode_headers().ok()?;
|
||||
let space = d.input_colorspace()?;
|
||||
if d.info()?.components != 4 || !matches!(space, ColorSpace::CMYK | ColorSpace::YCCK) {
|
||||
return None;
|
||||
}
|
||||
let (w, h) = d.dimensions()?;
|
||||
let (w, h) = (u32::try_from(w).ok()?, u32::try_from(h).ok()?);
|
||||
pixels(w, h)?;
|
||||
d.set_options(DecoderOptions::default().jpeg_set_out_colorspace(space));
|
||||
let mut data = d.decode().ok()?;
|
||||
for p in data.as_chunks_mut::<4>().0 {
|
||||
if space == ColorSpace::YCCK {
|
||||
let [y, cb, cr] = [p[0], p[1], p[2]].map(f32::from);
|
||||
p[0] = (434.456 - y - 1.402 * cr).clamp(0.0, 255.0) as u8;
|
||||
p[1] = (119.541 - y + 0.344 * cb + 0.714 * cr).clamp(0.0, 255.0) as u8;
|
||||
p[2] = (481.816 - y - 1.772 * cb).clamp(0.0, 255.0) as u8;
|
||||
}
|
||||
for v in p.iter_mut() {
|
||||
*v = 255 - *v;
|
||||
}
|
||||
}
|
||||
Inks::new(w, h, data)
|
||||
}
|
||||
|
||||
/// CMYK and YCCK JPEGs of many sizes, samplings and qualities, baseline and progressive, with
|
||||
/// and without Adobe's marker and an ICC profile, read the inks, size, component count and
|
||||
/// profile the zune-jpeg reader read; so do gray and RGB JPEGs (no inks) and cut-off files.
|
||||
/// The frames it panicked on (progressive 4:2:0 CMYK, whose C, M and Y `jpeg-encoder` writes at
|
||||
/// 1×1 and K at 2×2) read as no inks.
|
||||
#[test]
|
||||
fn cmyk_jpegs_read_as_the_zune_reader_read_them() {
|
||||
use jpeg_encoder::{ColorType, Encoder, SamplingFactor};
|
||||
let reference_size = |bytes: &[u8]| {
|
||||
use zune_jpeg::zune_core::{bytestream::ZCursor, options::DecoderOptions};
|
||||
let mut d = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(bytes), DecoderOptions::default());
|
||||
d.decode_headers().ok()?;
|
||||
let (w, h) = d.dimensions()?;
|
||||
Some((u32::try_from(w).ok()?, u32::try_from(h).ok()?, usize::from(d.info()?.components), d.icc_profile().filter(|i| !i.is_empty())))
|
||||
};
|
||||
let (mut files, mut read, mut panicked) = (0, 0, 0);
|
||||
for (i, (w, h)) in [(1u16, 1u16), (7, 3), (16, 16), (33, 31), (100, 77)].into_iter().enumerate() {
|
||||
let n = usize::from(w) * usize::from(h);
|
||||
let px: Vec<u8> = (0..n * 4).map(|k| ((k * 2_654_435_761 + i * 97) >> 7) as u8).collect();
|
||||
for ty in [ColorType::Cmyk, ColorType::CmykAsYcck, ColorType::Rgba, ColorType::Luma] {
|
||||
for (q, sampling, progressive, icc) in [
|
||||
(100, SamplingFactor::R_4_4_4, false, false),
|
||||
(70, SamplingFactor::R_4_2_0, true, true),
|
||||
(40, SamplingFactor::R_4_2_2, false, false),
|
||||
] {
|
||||
let mut out = Vec::new();
|
||||
let mut e = Encoder::new(&mut out, q);
|
||||
e.set_sampling_factor(sampling);
|
||||
e.set_progressive(progressive);
|
||||
if icc {
|
||||
let mut icc = vec![4; 70_000];
|
||||
icc[16..20].copy_from_slice(b"CMYK");
|
||||
e.add_icc_profile(&icc).unwrap();
|
||||
}
|
||||
let samples = if ty == ColorType::Luma { &px[..n] } else { &px[..] };
|
||||
e.encode(samples, w, h, ty).unwrap();
|
||||
let cut = out[..out.len() * 2 / 3].to_vec();
|
||||
for file in [out, cut] {
|
||||
let b = ImageBlob::new("image/jpeg", file.clone());
|
||||
// The zune-jpeg reader panicked on some subsampled CMYK frames; those
|
||||
// read as no inks now.
|
||||
let inks = std::panic::catch_unwind(|| zune_inks(&file)).unwrap_or_else(|_| {
|
||||
panicked += 1;
|
||||
None
|
||||
});
|
||||
read += usize::from(inks.is_some());
|
||||
assert_eq!(b.cmyk(), inks, "{w}x{h} {ty:?} q{q}");
|
||||
let r = reference_size(&file);
|
||||
assert_eq!(b.header_size(), r.as_ref().map(|r| (r.0, r.1)));
|
||||
assert_eq!(jpeg_components(&file), r.as_ref().map(|r| r.2));
|
||||
assert_eq!(jpeg_icc(&file), r.and_then(|r| r.3));
|
||||
files += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
assert_eq!(files, 5 * 4 * 3 * 2);
|
||||
assert!(read > 20 && panicked > 0, "{read} read as inks, {panicked} panicked");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
//! Images decoded through craft-image-codecs as the image crate gave them (#778): the renderer,
|
||||
//! File › Open and Place, SVG import, and embedded and linked images read their pixels with
|
||||
//! [`decode_image`] and [`decode_within`], and size them from their header with [`decoded_layout`].
|
||||
|
||||
/// Decoded pixels, as stored and as the image crate gave them, and the ICC profile the file embeds.
|
||||
pub type DecodedImage = (image::DynamicImage, Option<Vec<u8>>);
|
||||
|
||||
/// `bytes` decoded as `format` within `allowance` bytes, the pixels as stored (not turned
|
||||
/// upright) and as the image crate gave them ([`dynamic`]), with the ICC profile the file embeds.
|
||||
/// The renderer, File › Open and Place, SVG import, and embedded and linked images read images
|
||||
/// with it.
|
||||
pub fn decode_image(format: craft_image_codecs::Format, bytes: &[u8], allowance: u64) -> Result<DecodedImage, craft_image_codecs::CodecError> {
|
||||
let opts = craft_image_codecs::DecodeOptions {
|
||||
limits: craft_image_codecs::Limits { max_width: u32::MAX, max_height: u32::MAX, max_pixels: u64::MAX, max_alloc: allowance },
|
||||
keep_orientation: true,
|
||||
};
|
||||
let mut img = craft_image_codecs::decode_as_with(format, bytes, &opts)?;
|
||||
let icc = img.icc.take();
|
||||
let img = dynamic(img).ok_or_else(|| craft_image_codecs::CodecError::InvalidImage("the decoded samples don't fit the image".into()))?;
|
||||
Ok((img, icc))
|
||||
}
|
||||
|
||||
/// `bytes` decoded within `max` bytes, counted as the file's samples and as RGBA (#1717), the
|
||||
/// pixels as stored and as the image crate gave them, with the ICC profile the file embeds.
|
||||
/// Image blobs read their pixels with it. `Ok(None)` when they would take more than `max` as
|
||||
/// samples or as RGBA (the header alone decides; the pixels are not read). `Err` when the bytes
|
||||
/// aren't an image the decoder reads, including a file whose header fits the allowance but whose
|
||||
/// pixels can't be decoded (a cut-short file, or one the decoder refuses as a decompression bomb).
|
||||
pub fn decode_within(bytes: &[u8], max: u64) -> Result<Option<DecodedImage>, craft_image_codecs::CodecError> {
|
||||
let format = craft_image_codecs::detect(bytes).ok_or(craft_image_codecs::CodecError::UnknownFormat)?;
|
||||
let (w, h, per_pixel) =
|
||||
decoded_layout(bytes, format).ok_or_else(|| craft_image_codecs::CodecError::InvalidImage("the image's size can't be read".into()))?;
|
||||
let pixels = u64::from(w) * u64::from(h);
|
||||
if pixels.saturating_mul(per_pixel) > max || pixels.saturating_mul(4) > max {
|
||||
return Ok(None);
|
||||
}
|
||||
decode_image(format, bytes, max).map(Some)
|
||||
}
|
||||
|
||||
/// A decoded image as the image crate's: 8- and 16-bit gray, gray and alpha, RGB and RGBA as they
|
||||
/// are, 32-bit float RGB and RGBA as they are, anything else as 8-bit RGBA.
|
||||
fn dynamic(img: craft_image_codecs::Image) -> Option<image::DynamicImage> {
|
||||
use craft_image_codecs::{ChannelLayout as L, SampleType as S};
|
||||
use image::{DynamicImage as D, ImageBuffer as B};
|
||||
let (w, h) = img.dimensions();
|
||||
Some(match (img.layout(), img.sample_type()) {
|
||||
(L::Gray, S::U8) => D::ImageLuma8(B::from_raw(w, h, img.into_data())?),
|
||||
(L::GrayA, S::U8) => D::ImageLumaA8(B::from_raw(w, h, img.into_data())?),
|
||||
(L::Rgb, S::U8) => D::ImageRgb8(B::from_raw(w, h, img.into_data())?),
|
||||
(L::Rgba, S::U8) => D::ImageRgba8(B::from_raw(w, h, img.into_data())?),
|
||||
(layout @ (L::Gray | L::GrayA | L::Rgb | L::Rgba), S::U16) => {
|
||||
let v = img.to_u16_samples()?;
|
||||
match layout {
|
||||
L::Gray => D::ImageLuma16(B::from_raw(w, h, v)?),
|
||||
L::GrayA => D::ImageLumaA16(B::from_raw(w, h, v)?),
|
||||
L::Rgb => D::ImageRgb16(B::from_raw(w, h, v)?),
|
||||
_ => D::ImageRgba16(B::from_raw(w, h, v)?),
|
||||
}
|
||||
}
|
||||
_ => D::ImageRgba8(B::from_raw(w, h, img.to_rgba8())?),
|
||||
})
|
||||
}
|
||||
|
||||
/// The image's size and the bytes a pixel of it takes decoded ([`decode_as`]), read from its
|
||||
/// header: palettes as RGB (RGBA with a transparent entry; GIF frames always RGBA), bitmaps as 8-bit
|
||||
/// gray, 16-bit samples as two bytes. A PNG whose header alone is there is read from its `IHDR`;
|
||||
/// a format whose header says only its size (TGA, ICO and the like) counts RGBA.
|
||||
pub fn decoded_layout(bytes: &[u8], format: craft_image_codecs::Format) -> Option<(u32, u32, u64)> {
|
||||
use craft_image_codecs::{Format as F, RasterColorType as T};
|
||||
if let Some(i) = craft_image_codecs::inspect_raster(bytes).ok().flatten() {
|
||||
let deep = i.bits_per_sample > 8 && i.color_type != T::Indexed;
|
||||
let channels = match i.color_type {
|
||||
_ if format == F::Gif => 4,
|
||||
T::Indexed => 3 + u64::from(i.transparent_index.is_some()),
|
||||
T::Bitmap => 1,
|
||||
_ => u64::from(i.channels.max(1)),
|
||||
};
|
||||
return Some((i.width, i.height, channels * if deep { 2 } else { 1 }));
|
||||
}
|
||||
if format != F::Png {
|
||||
return crate::header_size(bytes).map(|(w, h)| (w, h, 4));
|
||||
}
|
||||
if bytes.get(12..16)? != b"IHDR" {
|
||||
return None;
|
||||
}
|
||||
let be = |at: usize| Some(u32::from_be_bytes(bytes.get(at..at + 4)?.try_into().ok()?));
|
||||
let (depth, color) = (*bytes.get(24)?, *bytes.get(25)?);
|
||||
let channels: u64 = match color {
|
||||
0 => 1,
|
||||
2 | 3 => 3,
|
||||
4 => 2,
|
||||
6 => 4,
|
||||
_ => return None,
|
||||
};
|
||||
Some((be(16)?, be(20)?, channels * if depth == 16 { 2 } else { 1 }))
|
||||
}
|
||||
@@ -14,6 +14,7 @@ pub mod cmyk;
|
||||
pub mod color_mask;
|
||||
pub mod compound_shape;
|
||||
pub mod corners;
|
||||
pub mod decode;
|
||||
pub mod depth;
|
||||
pub mod dimension;
|
||||
pub mod graph;
|
||||
@@ -54,7 +55,7 @@ pub mod overprint;
|
||||
pub mod pattern;
|
||||
pub mod perspective;
|
||||
mod pixels;
|
||||
pub use pixels::{GraySource, MAX_RASTER_ALLOC, PixelPick};
|
||||
pub use pixels::{GraySource, MAX_RASTER_ALLOC, PixelPick, header_size};
|
||||
pub mod placed_document;
|
||||
pub mod plugin_data;
|
||||
pub mod ppi;
|
||||
|
||||
+47
-29
@@ -145,9 +145,9 @@ impl ImageBlob {
|
||||
/// This blob after its pixels are decoded once, within [`crate::MAX_RASTER_ALLOC`], to check
|
||||
/// that they can be read; with `proxy`, it also gets the preview [`Self::with_proxy`] makes,
|
||||
/// made from that decode. `None` when the pixels can't be read, as with a file cut short that
|
||||
/// has a header but not all its pixels. An image whose pixels would take more than the
|
||||
/// allowance as RGBA is not decoded, and one whose decode needs more than the allowance stops
|
||||
/// decoding; either is returned as it is.
|
||||
/// has a header but not all its pixels, or one the decoder refuses as a decompression bomb.
|
||||
/// An image whose pixels would take more than the allowance as RGBA is not decoded and is
|
||||
/// returned as it is.
|
||||
pub fn checked_with_proxy(self, proxy: bool) -> Option<Self> {
|
||||
self.checked_with_pixels(proxy).map(|(blob, _)| blob)
|
||||
}
|
||||
@@ -155,22 +155,12 @@ impl ImageBlob {
|
||||
/// [`Self::checked_with_proxy`], with the pixels it decoded: `None` beside the blob when it
|
||||
/// decoded none (an image past the allowance).
|
||||
pub fn checked_with_pixels(mut self, proxy: bool) -> Option<(Self, Option<image::DynamicImage>)> {
|
||||
let max = crate::MAX_RASTER_ALLOC;
|
||||
let reader = || image::ImageReader::new(Cursor::new(self.bytes.as_slice())).with_guessed_format().ok();
|
||||
let (w, h) = reader()?.into_dimensions().ok()?;
|
||||
if u64::from(w) * u64::from(h) > max / 4 {
|
||||
return Some((self, None));
|
||||
}
|
||||
let mut r = reader()?;
|
||||
let mut limits = image::Limits::default();
|
||||
limits.max_alloc = Some(max);
|
||||
r.limits(limits);
|
||||
let img = match r.decode() {
|
||||
Ok(img) => img,
|
||||
Err(image::ImageError::Limits(_)) => return Some((self, None)),
|
||||
let img = match crate::decode::decode_within(&self.bytes, crate::MAX_RASTER_ALLOC) {
|
||||
Ok(Some((img, _))) => img,
|
||||
Ok(None) => return Some((self, None)),
|
||||
Err(_) => return None,
|
||||
};
|
||||
if proxy && self.proxy.is_none() && w.max(h) > PROXY_SIZE {
|
||||
if proxy && self.proxy.is_none() && img.width().max(img.height()) > PROXY_SIZE {
|
||||
self.proxy = proxy_of(&img).map(Arc::new);
|
||||
}
|
||||
Some((self, Some(img)))
|
||||
@@ -210,21 +200,16 @@ pub fn missing_placeholder() -> Option<Vec<u8>> {
|
||||
/// `bytes` decoded and shrunk to fit [`PROXY_SIZE`] as a PNG; `None` when small enough already or
|
||||
/// undecodable.
|
||||
fn proxy_png(bytes: &[u8]) -> Option<Vec<u8>> {
|
||||
let (w, h) = image::ImageReader::new(Cursor::new(bytes)).with_guessed_format().ok()?.into_dimensions().ok()?;
|
||||
let format = craft_image_codecs::detect(bytes)?;
|
||||
let (w, h, _) = crate::decode::decoded_layout(bytes, format)?;
|
||||
if w.max(h) <= PROXY_SIZE {
|
||||
return None;
|
||||
}
|
||||
// The proxy decodes within the project's raster cap, counted as RGBA like every other
|
||||
// decode path (#3597): the header alone decides whether the pixels could ever fit, and the
|
||||
// decoder gets the same budget instead of image's own default.
|
||||
if u64::from(w) * u64::from(h) > crate::MAX_RASTER_ALLOC / 4 {
|
||||
return None;
|
||||
}
|
||||
let mut reader = image::ImageReader::new(Cursor::new(bytes)).with_guessed_format().ok()?;
|
||||
let mut limits = image::Limits::default();
|
||||
limits.max_alloc = Some(crate::MAX_RASTER_ALLOC);
|
||||
reader.limits(limits);
|
||||
proxy_of(&reader.decode().ok()?)
|
||||
// The proxy decodes within the project's raster cap, counted as the file's samples and as
|
||||
// RGBA like every other decode path (#3597, #1717): the header alone decides whether the
|
||||
// pixels could ever fit, and the decoder gets the same budget.
|
||||
let (img, _) = crate::decode::decode_within(bytes, crate::MAX_RASTER_ALLOC).ok()??;
|
||||
proxy_of(&img)
|
||||
}
|
||||
|
||||
/// Decoded image `img` shrunk to fit [`PROXY_SIZE`] as a PNG.
|
||||
@@ -748,4 +733,37 @@ mod tests {
|
||||
assert!(p.pixels().all(|q| q.0 == [200, 30, 30, 128]), "{:?}", p.get_pixel(0, 0));
|
||||
}
|
||||
}
|
||||
|
||||
/// A PNG that says it is `w` × `h` 1-bit grey pixels (header and a stub IDAT, no rows).
|
||||
fn png_header_1bit(w: u32, h: u32) -> Vec<u8> {
|
||||
let mut out = vec![0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A];
|
||||
let push = |out: &mut Vec<u8>, chunk: &[u8]| {
|
||||
out.extend((chunk.len() as u32 - 4).to_be_bytes());
|
||||
out.extend(chunk);
|
||||
out.extend(crc32(chunk).to_be_bytes());
|
||||
};
|
||||
let mut ihdr = b"IHDR".to_vec();
|
||||
ihdr.extend(w.to_be_bytes());
|
||||
ihdr.extend(h.to_be_bytes());
|
||||
ihdr.extend([1, 0, 0, 0, 0]);
|
||||
push(&mut out, &ihdr);
|
||||
push(&mut out, b"IDAT\x78\x9c");
|
||||
push(&mut out, b"IEND");
|
||||
out
|
||||
}
|
||||
|
||||
/// A header-only 23,170 × 23,170 1-bit gray PNG is within the 2 GiB RGBA allowance, so Place
|
||||
/// decodes it and must refuse it (no pixel rows). 23,171 × 23,170 is past the allowance and is
|
||||
/// kept unread, not refused. craft-image-codecs reports the cut-short file as LimitExceeded
|
||||
/// (its decompression-bomb guard); that is still "pixels can't be read", not "past the allowance".
|
||||
#[test]
|
||||
fn a_gray_header_within_the_allowance_is_unreadable() {
|
||||
let within = png_header_1bit(23_170, 23_170);
|
||||
let over = png_header_1bit(23_171, 23_170);
|
||||
assert!(crate::decode::decode_within(&within, crate::MAX_RASTER_ALLOC).is_err(), "header-only within the allowance");
|
||||
assert!(matches!(crate::decode::decode_within(&over, crate::MAX_RASTER_ALLOC), Ok(None)), "past the allowance is not decoded");
|
||||
assert!(ImageBlob::new("image/png", within).checked_with_pixels(false).is_none(), "Place refuses the file");
|
||||
let (_, pixels) = ImageBlob::new("image/png", over).checked_with_pixels(false).expect("past the allowance is kept unread");
|
||||
assert!(pixels.is_none());
|
||||
}
|
||||
}
|
||||
+126
-23
@@ -20,15 +20,41 @@ pub type PixelPick<'a> = (Option<[f64; 4]>, usize, &'a dyn Fn(f64, f64) -> bool)
|
||||
/// the header first: the decoder's limit is on the file's samples, and a gray image is a quarter
|
||||
/// of what it becomes here (#1717).
|
||||
fn rgba_within(bytes: &[u8], max: u64) -> Option<image::RgbaImage> {
|
||||
let (w, h) = image::ImageReader::new(Cursor::new(bytes)).with_guessed_format().ok()?.into_dimensions().ok()?;
|
||||
if u64::from(w) * u64::from(h) > max / 4 {
|
||||
Some(crate::decode::decode_within(bytes, max).ok()??.0.into_rgba8())
|
||||
}
|
||||
|
||||
/// An image file's stored size in pixels, read from its header by craft-image-codecs without
|
||||
/// decoding any pixels: PNG's IHDR, a JPEG's frame, a TIFF's first full-resolution page, a
|
||||
/// still or animated WebP's canvas, BMP's header ([`craft_image_codecs::probe_allocation_size`],
|
||||
/// with no size limit), and a GIF's logical screen and the other formats' headers
|
||||
/// ([`craft_image_codecs::probe_size`]). Not turned by an EXIF orientation. `None` when no header
|
||||
/// gives a size.
|
||||
pub fn header_size(bytes: &[u8]) -> Option<(u32, u32)> {
|
||||
if craft_image_codecs::detect(bytes) == Some(craft_image_codecs::Format::Gif) {
|
||||
// A GIF frame may be larger than the logical screen the image crate gave as its size.
|
||||
return craft_image_codecs::probe_size(bytes).ok();
|
||||
}
|
||||
craft_image_codecs::probe_allocation_size(bytes)
|
||||
.ok()
|
||||
.or_else(|| craft_image_codecs::probe_size(bytes).ok())
|
||||
// A BMP whose pixel data does not match its header (a truncated file, or a header that
|
||||
// declares more pixels than the file holds) is refused by the image crate decoder the
|
||||
// probes use. The BITMAPINFOHEADER still names the size, which Place, Paste and Open
|
||||
// need so they can say the image is too large to draw instead of that its size can't
|
||||
// be read.
|
||||
.or_else(|| bmp_header_size(bytes))
|
||||
}
|
||||
|
||||
/// Width and height from a BMP file's BITMAPINFOHEADER, without opening it as a decoder would.
|
||||
/// Height may be negative (top-down storage); the stored magnitude is the size.
|
||||
fn bmp_header_size(bytes: &[u8]) -> Option<(u32, u32)> {
|
||||
if bytes.get(..2) != Some(b"BM") {
|
||||
return None;
|
||||
}
|
||||
let mut reader = image::ImageReader::new(Cursor::new(bytes)).with_guessed_format().ok()?;
|
||||
let mut limits = image::Limits::default();
|
||||
limits.max_alloc = Some(max);
|
||||
reader.limits(limits);
|
||||
Some(reader.decode().ok()?.into_rgba8())
|
||||
let dim = |at: usize| -> Option<i32> { Some(i32::from_le_bytes(bytes.get(at..at + 4)?.try_into().ok()?)) };
|
||||
let width = u32::try_from(dim(18)?).ok().filter(|&n| n > 0)?;
|
||||
let height = dim(22)?.unsigned_abs();
|
||||
(height > 0).then_some((width, height))
|
||||
}
|
||||
|
||||
impl ImageBlob {
|
||||
@@ -385,30 +411,43 @@ mod tests {
|
||||
assert!(ImageBlob::png(b"not a png".to_vec()).sample_pixels(&[(None, 100, &any)]).is_none());
|
||||
}
|
||||
|
||||
/// A GIF whose `w` × `h` screen holds one opaque red pixel at the top left. The file holds no
|
||||
/// other pixels, and the decoder makes them transparent.
|
||||
fn gif_with_one_red_pixel(w: u16, h: u16) -> Vec<u8> {
|
||||
let mut gif = b"GIF89a".to_vec();
|
||||
gif.extend_from_slice(&w.to_le_bytes());
|
||||
gif.extend_from_slice(&h.to_le_bytes());
|
||||
// Flags for a global color table of two colors, the background and aspect bytes, then the
|
||||
// table: red and black.
|
||||
gif.extend_from_slice(&[0x80, 0, 0, 255, 0, 0, 0, 0, 0]);
|
||||
// A 1 × 1 image at (0, 0), its LZW data (clear, color 0, end), and the end of the file.
|
||||
gif.extend_from_slice(&[0x2C, 0, 0, 0, 0, 1, 0, 1, 0, 0, 2, 2, 0x44, 0x01, 0, 0x3B]);
|
||||
gif
|
||||
/// An RGBA PNG of `w` × `h` pixels with one opaque red pixel at the top left, the rest
|
||||
/// transparent. Written row by row so the 11,586² allowance image is not held decoded
|
||||
/// while it is encoded.
|
||||
fn png_with_one_red_pixel(w: u32, h: u32) -> Vec<u8> {
|
||||
use std::io::Write;
|
||||
let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::fast());
|
||||
let mut row = vec![0u8; 1 + w as usize * 4];
|
||||
row[1..5].copy_from_slice(&[255, 0, 0, 255]);
|
||||
z.write_all(&row).unwrap();
|
||||
row[1..5].fill(0);
|
||||
for _ in 1..h {
|
||||
z.write_all(&row).unwrap();
|
||||
}
|
||||
let idat = [&b"IDAT"[..], &z.finish().unwrap()].concat();
|
||||
let ihdr = [&b"IHDR"[..], &w.to_be_bytes(), &h.to_be_bytes(), &[8, 6, 0, 0, 0]].concat();
|
||||
let mut out = b"\x89PNG\r\n\x1a\n".to_vec();
|
||||
for chunk in [&ihdr[..], &idat[..], b"IEND"] {
|
||||
out.extend((chunk.len() as u32 - 4).to_be_bytes());
|
||||
out.extend(chunk);
|
||||
let mut crc = flate2::Crc::new();
|
||||
crc.update(chunk);
|
||||
out.extend(crc.sum().to_be_bytes());
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The pixels are decoded within the allowance, not within the decoder's own 512 MB: an RGBA
|
||||
/// image of 11,586 × 11,586 pixels (536,941,584 bytes) is recolored and sampled.
|
||||
/// image of 11,586 × 11,586 pixels (536,941,584 bytes) is recolored and sampled. A PNG, not a
|
||||
/// sparse GIF: craft-image-codecs refuses a GIF whose unbacked logical screen is past 256 MiB.
|
||||
#[test]
|
||||
fn pixels_past_the_decoders_own_limit_are_decoded_within_the_allowance() {
|
||||
let (w, h) = (11_586, 11_586);
|
||||
let rgba = u64::from(w) * u64::from(h) * 4;
|
||||
assert!(rgba > 512 << 20);
|
||||
let gif = gif_with_one_red_pixel(w, h);
|
||||
assert!(rgba_within(&gif, rgba - 1).is_none(), "past the allowance");
|
||||
let b = ImageBlob::new("image/gif", gif);
|
||||
let png = png_with_one_red_pixel(w, h);
|
||||
assert!(rgba_within(&png, rgba - 1).is_none(), "past the allowance");
|
||||
let b = ImageBlob::png(png);
|
||||
assert!(b.map_rgb(|[r, g, bl]| [bl, g, r]).is_some(), "red becomes blue");
|
||||
assert_eq!(b.sample(0.5, 0.5, 1), Some([255, 0, 0, 255]));
|
||||
assert_eq!(b.sample(f64::from(w) - 0.5, f64::from(h) - 0.5, 1), None, "transparent");
|
||||
@@ -556,4 +595,68 @@ mod tests {
|
||||
assert_eq!(la8.coverage(), Some((4, 1, vec![255, 0, 204, 128])));
|
||||
assert_eq!(ImageBlob::png(vec![1, 2, 3]).coverage(), None);
|
||||
}
|
||||
|
||||
/// Embedded-image pixels through craft-image-codecs (#778) are those the image crate decoded:
|
||||
/// the same layout and samples for PNG, GIF, WebP, TIFF and BMP at each depth the image
|
||||
/// crate writes, and JPEG samples within two steps.
|
||||
#[test]
|
||||
fn decoded_pixels_are_those_the_image_crate_read() {
|
||||
use image::{DynamicImage as D, ImageFormat as F};
|
||||
let rgba =
|
||||
image::RgbaImage::from_fn(9, 5, |x, y| image::Rgba([(x * 29) as u8, (y * 47) as u8, ((x + 2 * y) * 9) as u8, 255 - (x * y * 6) as u8]));
|
||||
let deep = image::ImageBuffer::<image::Rgba<u16>, _>::from_fn(9, 5, |x, y| {
|
||||
image::Rgba([(x * 7_001) as u16, (y * 12_007) as u16, 5, 65_535 - (x * y) as u16])
|
||||
});
|
||||
let images = [
|
||||
D::ImageRgba8(rgba.clone()),
|
||||
D::ImageRgb8(D::ImageRgba8(rgba.clone()).to_rgb8()),
|
||||
D::ImageLuma8(D::ImageRgba8(rgba.clone()).to_luma8()),
|
||||
D::ImageLumaA8(D::ImageRgba8(rgba).to_luma_alpha8()),
|
||||
D::ImageRgba16(deep.clone()),
|
||||
D::ImageLuma16(D::ImageRgba16(deep).to_luma16()),
|
||||
];
|
||||
let mut checked = 0;
|
||||
for img in &images {
|
||||
for f in [F::Png, F::Gif, F::WebP, F::Tiff, F::Bmp, F::Jpeg] {
|
||||
let mut bytes = Vec::new();
|
||||
if img.write_to(&mut Cursor::new(&mut bytes), f).is_err() {
|
||||
continue;
|
||||
}
|
||||
let what = format!("{f:?} {:?}", img.color());
|
||||
let want = image::load_from_memory_with_format(&bytes, f).unwrap();
|
||||
let (got, _) = crate::decode::decode_within(&bytes, MAX_RASTER_ALLOC)
|
||||
.unwrap_or_else(|e| panic!("{what}: {e}"))
|
||||
.unwrap_or_else(|| panic!("{what}: past the allowance"));
|
||||
let tolerance = if f == F::Jpeg { 2 * 257 } else { 0 };
|
||||
if f != F::Jpeg {
|
||||
assert_eq!(got.color(), want.color(), "{what}");
|
||||
}
|
||||
let worst = got.to_rgba16().as_raw().iter().zip(want.to_rgba16().as_raw()).map(|(a, b)| a.abs_diff(*b)).max();
|
||||
assert!(worst.is_some_and(|w| w <= tolerance), "{what}: {worst:?}");
|
||||
// Link previews and the checked-image path read the same pixels.
|
||||
let handed = ImageBlob::new(format!("image/{f:?}").to_ascii_lowercase(), bytes.clone())
|
||||
.checked_with_pixels(false)
|
||||
.unwrap_or_else(|| panic!("{what}: checked"))
|
||||
.1
|
||||
.unwrap_or_else(|| panic!("{what}: checked past the allowance"));
|
||||
assert_eq!(handed.color(), got.color(), "{what} checked");
|
||||
assert_eq!(handed.as_bytes(), got.as_bytes(), "{what} checked");
|
||||
checked += 1;
|
||||
}
|
||||
}
|
||||
assert!(checked >= 20, "{checked}");
|
||||
}
|
||||
|
||||
/// A BMP header that declares 30,000 × 30,000 pixels, with only one pixel of data, still
|
||||
/// reports that size: Paste and Place can then say the image is too large to draw.
|
||||
#[test]
|
||||
fn a_truncated_bmp_header_still_gives_its_declared_size() {
|
||||
let mut bmp = vec![];
|
||||
image::RgbImage::from_pixel(1, 1, image::Rgb([200, 40, 40])).write_to(&mut Cursor::new(&mut bmp), image::ImageFormat::Bmp).unwrap();
|
||||
bmp[18..22].copy_from_slice(&30_000i32.to_le_bytes());
|
||||
bmp[22..26].copy_from_slice(&30_000i32.to_le_bytes());
|
||||
assert_eq!(header_size(&bmp), Some((30_000, 30_000)));
|
||||
let (w, h, _) = crate::decode::decoded_layout(&bmp, craft_image_codecs::Format::Bmp).expect("layout from the header");
|
||||
assert_eq!((w, h), (30_000, 30_000));
|
||||
}
|
||||
}
|
||||
@@ -17,10 +17,10 @@ vectorcraft-text = { workspace = true }
|
||||
vectorcraft-plugins = { workspace = true }
|
||||
vectorcraft-trace = { workspace = true }
|
||||
image = { workspace = true }
|
||||
craft-image-codecs = { workspace = true }
|
||||
kurbo = { workspace = true }
|
||||
# Effect › SVG Filters draws its filters with resvg (MIT OR Apache-2.0), at the usvg version the
|
||||
# workspace reads SVG with; raster-images decodes the object's pixels it hands the filter as a PNG.
|
||||
resvg = { version = "0.48", default-features = false, features = ["raster-images"] }
|
||||
# Effect › SVG Filters draws its filters with craft-svg's resvg (`tree-render`).
|
||||
craft-svg = { workspace = true, features = ["tree-render"] }
|
||||
# The number lists of filter attributes, read as usvg reads them (MIT OR Apache-2.0; already used by usvg).
|
||||
svgtypes = "0.16"
|
||||
serde_json = { workspace = true }
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
//! lies one pixel per point at 100 %, centered on the object's center and repeated in both
|
||||
//! directions.
|
||||
|
||||
use std::io::Cursor;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::{Arc, Mutex, PoisonError};
|
||||
|
||||
@@ -85,9 +84,15 @@ impl LoadedTexture {
|
||||
/// Its [`TEXTURE_IMAGE`] value: the gray levels as a grayscale PNG, base64; `None` when the
|
||||
/// PNG can't be written.
|
||||
pub fn to_stored(&self) -> Option<String> {
|
||||
let img = image::GrayImage::from_raw(self.width, self.height, self.gray.clone())?;
|
||||
let mut png = Vec::new();
|
||||
img.write_to(&mut Cursor::new(&mut png), image::ImageFormat::Png).ok()?;
|
||||
let img = craft_image_codecs::Image::from_raw(
|
||||
self.width,
|
||||
self.height,
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U8,
|
||||
self.gray.clone(),
|
||||
)
|
||||
.ok()?;
|
||||
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).ok()?;
|
||||
Some(base64_encode(&png))
|
||||
}
|
||||
|
||||
@@ -95,21 +100,38 @@ impl LoadedTexture {
|
||||
/// `None` when `png` isn't a PNG or holds no pixels.
|
||||
/// `None` too when its gray levels would take the live textures past [`MAX_LIVE`].
|
||||
fn from_png(png: &[u8]) -> Option<Self> {
|
||||
let reader = || {
|
||||
let mut reader = image::ImageReader::with_format(Cursor::new(png), image::ImageFormat::Png);
|
||||
let mut limits = image::Limits::default();
|
||||
limits.max_alloc = Some(vectorcraft_doc::MAX_RASTER_ALLOC);
|
||||
reader.limits(limits);
|
||||
reader
|
||||
};
|
||||
let (w, h) = reader().into_dimensions().ok()?;
|
||||
let (w, h) = Self::png_ihdr_size(png)?;
|
||||
let gray = (w as usize).checked_mul(h as usize)?;
|
||||
if LIVE.load(Ordering::Relaxed).saturating_add(gray) > MAX_LIVE {
|
||||
return None;
|
||||
}
|
||||
let img = reader().decode().ok()?.to_luma8();
|
||||
let mut opts = craft_image_codecs::DecodeOptions::default();
|
||||
opts.limits.max_alloc = vectorcraft_doc::MAX_RASTER_ALLOC;
|
||||
// The codec default caps each side at 2^18; a stored texture can be longer than that and
|
||||
// still fit the live and allocation budgets.
|
||||
opts.limits.max_width = u32::MAX;
|
||||
opts.limits.max_height = u32::MAX;
|
||||
let img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Png, png, &opts).ok()?;
|
||||
let (width, height) = img.dimensions();
|
||||
(width > 0 && height > 0).then(|| Self::new(width, height, img.into_raw()))
|
||||
if width == 0 || height == 0 {
|
||||
return None;
|
||||
}
|
||||
use craft_image_codecs::{ChannelLayout as L, SampleType as S};
|
||||
let luma = match (img.layout(), img.sample_type()) {
|
||||
(L::Gray, S::U8) => img.into_data(),
|
||||
_ => image::RgbaImage::from_raw(width, height, img.to_rgba8()).map(image::DynamicImage::ImageRgba8)?.into_luma8().into_raw(),
|
||||
};
|
||||
Some(Self::new(width, height, luma))
|
||||
}
|
||||
|
||||
/// Read `(width, height)` from a PNG's IHDR chunk without decoding pixels.
|
||||
fn png_ihdr_size(png: &[u8]) -> Option<(u32, u32)> {
|
||||
if png.len() < 24 || &png[..8] != b"\x89PNG\r\n\x1a\n" || &png[12..16] != b"IHDR" {
|
||||
return None;
|
||||
}
|
||||
let w = u32::from_be_bytes(png[16..20].try_into().ok()?);
|
||||
let h = u32::from_be_bytes(png[20..24].try_into().ok()?);
|
||||
Some((w, h))
|
||||
}
|
||||
|
||||
/// The surface's height (points) at `p` (points at 100 %, from the object's center): the gray
|
||||
|
||||
@@ -1704,6 +1704,14 @@ fn a_loaded_texture_is_its_gray_over_middle_gray() {
|
||||
assert!(texture("not base64!").is_none() && texture("aGVsbG8=").is_none(), "base64 of a PNG only");
|
||||
}
|
||||
|
||||
/// A stored texture longer than the codec's default 2^18-pixel side still reads back.
|
||||
#[test]
|
||||
fn a_stored_texture_longer_than_the_codec_default_side_reads_back() {
|
||||
let t = LoadedTexture::from_rgba(262_145, 1, &vec![128; 262_145 * 4]).and_then(|t| t.to_stored()).unwrap();
|
||||
let loaded = texture(&t).unwrap();
|
||||
assert_eq!(loaded.size(), (262_145, 1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glass_bends_the_image_by_a_loaded_texture() {
|
||||
let (w, h) = (48, 40);
|
||||
|
||||
@@ -8,7 +8,8 @@
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
||||
|
||||
use resvg::{tiny_skia, usvg};
|
||||
use craft_svg::tree::draw::{resvg, tiny_skia};
|
||||
use craft_svg::tree::usvg;
|
||||
use serde_json::Value;
|
||||
use usvg::roxmltree;
|
||||
use vectorcraft_doc::SvgFilter;
|
||||
@@ -624,17 +625,27 @@ fn write_drop_shadow(n: roxmltree::Node, input: &str, result: Option<&str>, name
|
||||
/// first bytes whatever its `data:` URL says, of at most [`MAX_IMAGE_PIXELS`] pixels by its header.
|
||||
/// `None` for anything else, SVG and compressed SVG included.
|
||||
fn raster_image_sized(data: Arc<Vec<u8>>) -> Option<(usvg::ImageKind, u64)> {
|
||||
let format = image::guess_format(&data).ok()?;
|
||||
let (w, h) = image::ImageReader::with_format(std::io::Cursor::new(data.as_slice()), format).into_dimensions().ok()?;
|
||||
let format = craft_image_codecs::detect(&data)?;
|
||||
let (w, h) = match format {
|
||||
craft_image_codecs::Format::Png => {
|
||||
if data.len() < 24 || &data[..8] != b"\x89PNG\r\n\x1a\n" || &data[12..16] != b"IHDR" {
|
||||
return None;
|
||||
}
|
||||
(u32::from_be_bytes(data[16..20].try_into().ok()?), u32::from_be_bytes(data[20..24].try_into().ok()?))
|
||||
}
|
||||
craft_image_codecs::Format::Jpeg => craft_image_codecs::jpeg_info(&data).map(|j| (j.width, j.height))?,
|
||||
craft_image_codecs::Format::Gif | craft_image_codecs::Format::WebP => craft_image_codecs::probe_size(&data).ok()?,
|
||||
_ => return None,
|
||||
};
|
||||
let pixels = u64::from(w) * u64::from(h);
|
||||
if pixels == 0 || pixels > MAX_IMAGE_PIXELS {
|
||||
return None;
|
||||
}
|
||||
let kind = match format {
|
||||
image::ImageFormat::Png => usvg::ImageKind::PNG(data),
|
||||
image::ImageFormat::Jpeg => usvg::ImageKind::JPEG(data),
|
||||
image::ImageFormat::Gif => usvg::ImageKind::GIF(data),
|
||||
image::ImageFormat::WebP => usvg::ImageKind::WEBP(data),
|
||||
craft_image_codecs::Format::Png => usvg::ImageKind::PNG(data),
|
||||
craft_image_codecs::Format::Jpeg => usvg::ImageKind::JPEG(data),
|
||||
craft_image_codecs::Format::Gif => usvg::ImageKind::GIF(data),
|
||||
craft_image_codecs::Format::WebP => usvg::ImageKind::WEBP(data),
|
||||
_ => return None,
|
||||
};
|
||||
Some((kind, pixels))
|
||||
@@ -1636,7 +1647,6 @@ fn on_canvas(data: &[u8], w: usize, h: usize, cw: u32, ch: u32, to_canvas: Affin
|
||||
|
||||
/// The pixels of `data` (premultiplied RGBA8, `w` wide) in columns x0..x1 and rows y0..y1, as a PNG.
|
||||
fn png_of(data: &[u8], w: usize, x0: usize, y0: usize, x1: usize, y1: usize) -> Option<Vec<u8>> {
|
||||
use image::ImageEncoder;
|
||||
let px = data.as_chunks::<4>().0;
|
||||
let mut straight = Vec::with_capacity((x1 - x0) * (y1 - y0) * 4);
|
||||
for y in y0..y1 {
|
||||
@@ -1646,14 +1656,16 @@ fn png_of(data: &[u8], w: usize, x0: usize, y0: usize, x1: usize, y1: usize) ->
|
||||
straight.extend_from_slice(&[un(p[0]), un(p[1]), un(p[2]), p[3]]);
|
||||
}
|
||||
}
|
||||
let mut png = vec![];
|
||||
let encoder = image::codecs::png::PngEncoder::new_with_quality(
|
||||
&mut png,
|
||||
image::codecs::png::CompressionType::Fast,
|
||||
image::codecs::png::FilterType::NoFilter,
|
||||
);
|
||||
encoder.write_image(&straight, (x1 - x0) as u32, (y1 - y0) as u32, image::ExtendedColorType::Rgba8).ok()?;
|
||||
Some(png)
|
||||
let raw = craft_image_codecs::Image::from_raw(
|
||||
(x1 - x0) as u32,
|
||||
(y1 - y0) as u32,
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U8,
|
||||
straight,
|
||||
)
|
||||
.ok()?;
|
||||
let opts = craft_image_codecs::EncodeOptions { png_compression: craft_image_codecs::PngCompression::Fast, ..Default::default() };
|
||||
craft_image_codecs::encode(&raw, craft_image_codecs::Format::Png, &opts).ok()
|
||||
}
|
||||
|
||||
/// `a` as an SVG `matrix(…)`.
|
||||
|
||||
@@ -520,16 +520,15 @@ fn the_filter_is_drawn_and_written_from_its_filter_parts_alone() {
|
||||
#[test]
|
||||
fn filter_images_are_small_raster_images() {
|
||||
fn png(w: u32, h: u32) -> Vec<u8> {
|
||||
let mut out = vec![];
|
||||
image::ImageEncoder::write_image(
|
||||
image::codecs::png::PngEncoder::new(&mut out),
|
||||
&vec![0; (w * h * 4) as usize],
|
||||
let img = craft_image_codecs::Image::from_raw(
|
||||
w,
|
||||
h,
|
||||
image::ExtendedColorType::Rgba8,
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U8,
|
||||
vec![0; (w * h * 4) as usize],
|
||||
)
|
||||
.unwrap();
|
||||
out
|
||||
craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap()
|
||||
}
|
||||
let raster_image = |data| raster_image_sized(data).map(|(kind, _)| kind);
|
||||
assert!(matches!(raster_image(Arc::new(png(2, 2))), Some(usvg::ImageKind::PNG(_))));
|
||||
@@ -547,8 +546,12 @@ fn filter_images_are_small_raster_images() {
|
||||
|
||||
/// A 2 × 2 PNG whose header claims `w` × `h` pixels (its IHDR rewritten, with the CRC fixed).
|
||||
fn png_claiming(w: u32, h: u32) -> Vec<u8> {
|
||||
let mut out = vec![];
|
||||
image::ImageEncoder::write_image(image::codecs::png::PngEncoder::new(&mut out), &[0; 16], 2, 2, image::ExtendedColorType::Rgba8).unwrap();
|
||||
let mut out = craft_image_codecs::encode(
|
||||
&craft_image_codecs::Image::from_raw(2, 2, craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, vec![0; 16]).unwrap(),
|
||||
craft_image_codecs::Format::Png,
|
||||
&craft_image_codecs::EncodeOptions::default(),
|
||||
)
|
||||
.unwrap();
|
||||
out[16..20].copy_from_slice(&w.to_be_bytes());
|
||||
out[20..24].copy_from_slice(&h.to_be_bytes());
|
||||
let crc = crc32(&out[12..29]);
|
||||
|
||||
@@ -15,10 +15,13 @@ fn fx(id: &str) -> Effect {
|
||||
}
|
||||
|
||||
/// `img` encoded as `format`.
|
||||
fn encoded(img: image::DynamicImage, format: image::ImageFormat) -> Vec<u8> {
|
||||
let mut out = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut out), format).unwrap();
|
||||
out
|
||||
fn encoded(img: image::DynamicImage, format: craft_image_codecs::Format) -> Vec<u8> {
|
||||
let (layout, data) = match format {
|
||||
craft_image_codecs::Format::Jpeg => (craft_image_codecs::ChannelLayout::Rgb, img.to_rgb8().into_raw()),
|
||||
_ => (craft_image_codecs::ChannelLayout::Rgba, img.to_rgba8().into_raw()),
|
||||
};
|
||||
let raw = craft_image_codecs::Image::from_raw(img.width(), img.height(), layout, craft_image_codecs::SampleType::U8, data).unwrap();
|
||||
craft_image_codecs::encode(&raw, format, &craft_image_codecs::EncodeOptions::default()).unwrap()
|
||||
}
|
||||
|
||||
/// A 100×100 RGBA image, transparent but for an opaque disc of radius 30 in its middle.
|
||||
@@ -130,7 +133,7 @@ fn outline_object_follows_the_opaque_area_of_an_image_with_alpha() {
|
||||
let gray_alpha = image::DynamicImage::ImageLumaA8(disc().to_luma_alpha8());
|
||||
let deep = image::DynamicImage::ImageRgba16(disc().to_rgba16());
|
||||
for (name, img) in [("rgba", disc()), ("gray and alpha", gray_alpha), ("16 bits a channel", deep)] {
|
||||
let (d, n) = image_doc(encoded(img, image::ImageFormat::Png), vec![stroke(vec![fx("path.outlineObject")])], 1);
|
||||
let (d, n) = image_doc(encoded(img, craft_image_codecs::Format::Png), vec![stroke(vec![fx("path.outlineObject")])], 1);
|
||||
let rows = row_bounds(&d, &n);
|
||||
assert_eq!(rows.len(), 1, "{name}");
|
||||
// The disc runs from 70 to 130; the frame from 50 to 150.
|
||||
@@ -143,9 +146,9 @@ fn outline_object_follows_the_frame_of_an_image_without_transparency() {
|
||||
let opaque = image::DynamicImage::ImageRgb8(image::RgbImage::from_pixel(100, 100, image::Rgb([0, 0, 255])));
|
||||
let solid = image::DynamicImage::ImageRgba8(image::RgbaImage::from_pixel(100, 100, image::Rgba([0, 0, 255, 200])));
|
||||
let formats = [
|
||||
("jpeg", encoded(opaque.clone(), image::ImageFormat::Jpeg)),
|
||||
("rgb png", encoded(opaque, image::ImageFormat::Png)),
|
||||
("rgba png, every pixel more than half opaque", encoded(solid, image::ImageFormat::Png)),
|
||||
("jpeg", encoded(opaque.clone(), craft_image_codecs::Format::Jpeg)),
|
||||
("rgb png", encoded(opaque, craft_image_codecs::Format::Png)),
|
||||
("rgba png, every pixel more than half opaque", encoded(solid, craft_image_codecs::Format::Png)),
|
||||
];
|
||||
for (name, bytes) in formats {
|
||||
let (d, n) = image_doc(bytes, vec![stroke(vec![fx("path.outlineObject")])], 1);
|
||||
@@ -165,7 +168,7 @@ fn convert_to_shape_keeps_the_frame_beside_outline_object() {
|
||||
let mut fill = FillLayer::new(Paint::solid(Color::rgb(0.0, 1.0, 0.0)));
|
||||
fill.effects = vec![rect.clone()];
|
||||
let items = vec![AppearanceItem::Fill(fill), stroke(vec![fx("path.outlineObject")]), stroke(vec![rect, fx("path.outlineObject")])];
|
||||
let (d, n) = image_doc(encoded(disc(), image::ImageFormat::Png), items, 2);
|
||||
let (d, n) = image_doc(encoded(disc(), craft_image_codecs::Format::Png), items, 2);
|
||||
let rows = row_bounds(&d, &n);
|
||||
assert_eq!(rows.len(), 3, "{rows:?}");
|
||||
assert!(near(rows[0], [50.0, 50.0, 150.0, 150.0], 1e-9), "{rows:?}");
|
||||
@@ -179,7 +182,7 @@ fn convert_to_shape_keeps_the_frame_beside_outline_object() {
|
||||
#[test]
|
||||
fn a_transparent_image_gives_outline_object_nothing_to_paint() {
|
||||
let clear = image::DynamicImage::ImageRgba8(image::RgbaImage::from_pixel(100, 100, image::Rgba([0, 0, 0, 100])));
|
||||
let (d, n) = image_doc(encoded(clear, image::ImageFormat::Png), vec![stroke(vec![fx("path.outlineObject")])], 1);
|
||||
let (d, n) = image_doc(encoded(clear, craft_image_codecs::Format::Png), vec![stroke(vec![fx("path.outlineObject")])], 1);
|
||||
assert!(row_bounds(&d, &n).is_empty());
|
||||
let art = reshape::reshape(Some(&d), &n, None).unwrap();
|
||||
assert!(matches!(art.children().unwrap().as_slice(), [only] if matches!(only.kind, NodeKind::Image(_))));
|
||||
@@ -190,7 +193,7 @@ fn the_outline_follows_the_images_own_pixels_and_placement() {
|
||||
// Pixels kept on the image itself (art made outside a document), placed at half size.
|
||||
let (_, mut n) = image_doc(vec![], vec![stroke(vec![fx("path.outlineObject")])], 1);
|
||||
if let NodeKind::Image(im) = &mut n.kind {
|
||||
im.pixels = Some(Arc::new(ImageBlob::new("image/png", encoded(disc(), image::ImageFormat::Png))));
|
||||
im.pixels = Some(Arc::new(ImageBlob::new("image/png", encoded(disc(), craft_image_codecs::Format::Png))));
|
||||
im.xf = Affine::translate((10.0, 20.0)) * Affine::scale(0.5);
|
||||
}
|
||||
let art = reshape::reshape(None, &n, None).unwrap();
|
||||
@@ -206,7 +209,7 @@ fn an_off_center_outline_turns_and_reflects_with_a_smaller_copy_of_the_pixels()
|
||||
let half = image::DynamicImage::ImageRgba8(image::RgbaImage::from_fn(60, 30, |x, y| {
|
||||
image::Rgba([255, 0, 0, if (6..20).contains(&x) && (3..12).contains(&y) { 255 } else { 0 }])
|
||||
}));
|
||||
let (d, mut n) = image_doc(encoded(half, image::ImageFormat::Png), vec![stroke(vec![fx("path.outlineObject")])], 1);
|
||||
let (d, mut n) = image_doc(encoded(half, craft_image_codecs::Format::Png), vec![stroke(vec![fx("path.outlineObject")])], 1);
|
||||
let xf = Affine::translate((100.0, 40.0)) * Affine::rotate(std::f64::consts::FRAC_PI_2) * Affine::scale_non_uniform(-1.0, 1.0);
|
||||
if let NodeKind::Image(im) = &mut n.kind {
|
||||
(im.width, im.height, im.xf) = (120, 60, xf);
|
||||
@@ -221,7 +224,7 @@ fn an_off_center_outline_turns_and_reflects_with_a_smaller_copy_of_the_pixels()
|
||||
|
||||
#[test]
|
||||
fn an_image_in_a_group_exports_with_its_outline() {
|
||||
let (mut d, n) = image_doc(encoded(disc(), image::ImageFormat::Png), vec![stroke(vec![fx("path.outlineObject")])], 1);
|
||||
let (mut d, n) = image_doc(encoded(disc(), craft_image_codecs::Format::Png), vec![stroke(vec![fx("path.outlineObject")])], 1);
|
||||
let l = d.layers[0].id;
|
||||
d.remove(n.id).unwrap();
|
||||
let g = d.alloc_id();
|
||||
@@ -256,7 +259,7 @@ fn baked_strokes(d: &Document) -> Vec<Rect> {
|
||||
fn an_image_under_a_clipping_mask_or_with_an_opacity_mask_exports_with_its_outline() {
|
||||
let disc_outline = |paths: &[Rect]| matches!(paths, [b] if near(*b, [70.0, 70.0, 130.0, 130.0], 0.5));
|
||||
// Under a clipping mask that covers the whole image.
|
||||
let (mut d, n) = image_doc(encoded(disc(), image::ImageFormat::Png), vec![stroke(vec![fx("path.outlineObject")])], 1);
|
||||
let (mut d, n) = image_doc(encoded(disc(), craft_image_codecs::Format::Png), vec![stroke(vec![fx("path.outlineObject")])], 1);
|
||||
let l = d.layers[0].id;
|
||||
d.remove(n.id).unwrap();
|
||||
let clip = Node::path(d.alloc_id(), vectorcraft_geom::shapes::rectangle(Rect::new(0.0, 0.0, 200.0, 200.0)), Default::default());
|
||||
@@ -265,7 +268,7 @@ fn an_image_under_a_clipping_mask_or_with_an_opacity_mask_exports_with_its_outli
|
||||
let paths = baked_strokes(&d);
|
||||
assert!(disc_outline(&paths), "{paths:?}");
|
||||
// With an opacity mask.
|
||||
let (mut d, mut n) = image_doc(encoded(disc(), image::ImageFormat::Png), vec![stroke(vec![fx("path.outlineObject")])], 1);
|
||||
let (mut d, mut n) = image_doc(encoded(disc(), craft_image_codecs::Format::Png), vec![stroke(vec![fx("path.outlineObject")])], 1);
|
||||
let mut white = Node::path(NodeId(999), vectorcraft_geom::shapes::rectangle(Rect::new(0.0, 0.0, 200.0, 200.0)), Default::default());
|
||||
white.appearance.set_fill(Paint::solid(Color::WHITE));
|
||||
n.extras_mut().mask = Some(Box::new(vectorcraft_doc::OpacityMask::new(Node::group(NodeId(998), vec![Arc::new(white)]), true)));
|
||||
@@ -279,7 +282,7 @@ fn an_image_under_a_clipping_mask_or_with_an_opacity_mask_exports_with_its_outli
|
||||
fn outline_object_on_a_fill_and_on_an_indexed_image_follows_the_opaque_area() {
|
||||
let mut fill = FillLayer::new(Paint::solid(Color::rgb(0.0, 1.0, 0.0)));
|
||||
fill.effects = vec![fx("path.outlineObject")];
|
||||
let (d, n) = image_doc(encoded(disc(), image::ImageFormat::Png), vec![AppearanceItem::Fill(fill)], 1);
|
||||
let (d, n) = image_doc(encoded(disc(), craft_image_codecs::Format::Png), vec![AppearanceItem::Fill(fill)], 1);
|
||||
let rows = row_bounds(&d, &n);
|
||||
assert!(matches!(rows.as_slice(), [b] if near(*b, [70.0, 70.0, 130.0, 130.0], 0.5)), "{rows:?}");
|
||||
// A palette image whose first color is transparent (a tRNS chunk).
|
||||
@@ -308,15 +311,18 @@ fn an_image_too_large_or_too_complex_to_outline_keeps_its_frame() {
|
||||
}
|
||||
// A checkerboard of 2 px squares, whose outline would need more than 100,000 anchor points.
|
||||
let checker = image::RgbaImage::from_fn(640, 640, |x, y| image::Rgba([0, 0, 0, if (x / 2 + y / 2) % 2 == 0 { 255 } else { 0 }]));
|
||||
let (d, n) =
|
||||
image_doc(encoded(image::DynamicImage::ImageRgba8(checker), image::ImageFormat::Png), vec![stroke(vec![fx("path.outlineObject")])], 1);
|
||||
let (d, n) = image_doc(
|
||||
encoded(image::DynamicImage::ImageRgba8(checker), craft_image_codecs::Format::Png),
|
||||
vec![stroke(vec![fx("path.outlineObject")])],
|
||||
1,
|
||||
);
|
||||
assert!(matches!(row_bounds(&d, &n).as_slice(), [b] if near(*b, [50.0, 50.0, 150.0, 150.0], 1e-9)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_outline_is_traced_once_for_its_pixels_and_dropped_with_them() {
|
||||
let traces = || reshape::TRACES.with(std::cell::Cell::get);
|
||||
let (d, n) = image_doc(encoded(disc(), image::ImageFormat::Png), vec![stroke(vec![fx("path.outlineObject")])], 1);
|
||||
let (d, n) = image_doc(encoded(disc(), craft_image_codecs::Format::Png), vec![stroke(vec![fx("path.outlineObject")])], 1);
|
||||
let NodeKind::Image(im) = &n.kind else { panic!("not an image") };
|
||||
let before = traces();
|
||||
let first = reshape::opaque_area(&[Some(&d)], im).unwrap();
|
||||
@@ -331,7 +337,7 @@ fn an_outline_is_traced_once_for_its_pixels_and_dropped_with_them() {
|
||||
// Once the document and its pixels are gone, the next trace drops their entry.
|
||||
drop(d);
|
||||
assert!(pixels.upgrade().is_none());
|
||||
let (other, _) = image_doc(encoded(disc(), image::ImageFormat::Png), vec![], 1);
|
||||
let (other, _) = image_doc(encoded(disc(), craft_image_codecs::Format::Png), vec![], 1);
|
||||
reshape::opaque_area(&[Some(&other)], im).unwrap();
|
||||
assert_eq!(traces(), before + 2);
|
||||
assert!(!reshape::keeps_outline(&pixels));
|
||||
|
||||
@@ -44,10 +44,10 @@ vectorcraft-js = { workspace = true }
|
||||
# The commands' parameter schemas (schemas.rs).
|
||||
vectorcraft-apischema = { workspace = true }
|
||||
image = { workspace = true }
|
||||
craft-image-codecs = { workspace = true }
|
||||
jxl-oxide = { workspace = true }
|
||||
jxl-color = { workspace = true }
|
||||
jxl-grid = { workspace = true }
|
||||
hayro-jpeg2000 = { version = "=0.3.5", default-features = false, features = ["std", "simd"] }
|
||||
# Pure Rust, no unsafe; pinned at the version photocraft-heif uses.
|
||||
heic-rs = { version = "=0.1.4", default-features = false, features = ["std"], optional = true }
|
||||
flate2 = { workspace = true }
|
||||
@@ -78,6 +78,16 @@ heic-rs = { version = "=0.1.4", default-features = false, features = ["std", "pa
|
||||
rayon = { version = "1.12", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
# JPEG XL files for the `.jxl` open and place tests.
|
||||
zune-jpegxl = { workspace = true }
|
||||
zune-core = { workspace = true }
|
||||
# JPEG XL files for the import fuzz tests (tests/import_fuzz.rs).
|
||||
craft-jxl = { workspace = true }
|
||||
# vectorcraft-doc's Group 3 TIFF reader before craft-stream-codecs, the reference
|
||||
# tests/group3_reference.rs compares it with.
|
||||
hayro-ccitt = { workspace = true }
|
||||
tiff = { workspace = true }
|
||||
craft-stream-codecs = { workspace = true }
|
||||
vectorcraft-affinity = { workspace = true, features = ["synth"] }
|
||||
crc32fast = { workspace = true }
|
||||
vectorcraft-testkit = { workspace = true }
|
||||
@@ -89,9 +99,6 @@ vectorcraft-pdf = { workspace = true, features = ["read-count"] }
|
||||
wat = { workspace = true }
|
||||
# Zstandard streams for the Illustrator editing data fuzz tests.
|
||||
ruzstd = { workspace = true }
|
||||
# JPEG XL files for the `.jxl` open and place tests.
|
||||
zune-jpegxl = { workspace = true }
|
||||
zune-core = { workspace = true }
|
||||
# The JavaScript parser's lexer, compared with the script checker's on the same scripts.
|
||||
vectorcraft-js = { workspace = true }
|
||||
# A bundled font's own glyph boxes, for the glyph bounds tests.
|
||||
|
||||
@@ -147,9 +147,8 @@ fn is_of_kind(d: &Document, n: &Node, kind: &str) -> bool {
|
||||
|
||||
/// Are the encoded image `bytes` grayscale or 1-bit? Reads only the header.
|
||||
fn is_gray(bytes: &[u8]) -> bool {
|
||||
use image::ImageDecoder as _;
|
||||
let decoder = image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format().ok().and_then(|r| r.into_decoder().ok());
|
||||
decoder.is_some_and(|d| matches!(d.color_type(), image::ColorType::L8 | image::ColorType::La8 | image::ColorType::L16 | image::ColorType::La16))
|
||||
craft_image_codecs::decode(bytes)
|
||||
.is_ok_and(|img| matches!(img.layout(), craft_image_codecs::ChannelLayout::Gray | craft_image_codecs::ChannelLayout::GrayA))
|
||||
}
|
||||
|
||||
fn filter(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
@@ -111,7 +111,41 @@ fn image(s: &mut Session, key: &str, blob: vectorcraft_doc::ImageBlob, x: f64) -
|
||||
|
||||
fn png(img: image::DynamicImage) -> vectorcraft_doc::ImageBlob {
|
||||
let mut bytes = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img;
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| bytes.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
vectorcraft_doc::ImageBlob::png(bytes)
|
||||
}
|
||||
|
||||
|
||||
@@ -228,7 +228,41 @@ fn export_all_open_documents_writes_one_file_each() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 100, "height": 100, "name": "pic"})).unwrap();
|
||||
let mut png = vec![];
|
||||
image::RgbaImage::from_pixel(4, 4, image::Rgba([1, 2, 3, 255])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(4, 4, image::Rgba([1, 2, 3, 255]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.execute("file.place", &json!({"name": "a.png", "dataBase64": vectorcraft_format::base64_encode(&png)})).unwrap();
|
||||
s.execute("document.save", &json!({"path": dir.join("pic.svg"), "format": "svg", "options": {"images": "link"}})).unwrap();
|
||||
let saved: Vec<_> = std::fs::read_dir(&dir).unwrap().filter_map(|e| e.ok()).filter(|e| e.path().is_file()).collect();
|
||||
|
||||
@@ -13,10 +13,9 @@ mod tests;
|
||||
pub(crate) use mask::refresh as refresh_color_masks;
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::io::Cursor;
|
||||
use std::sync::{Arc, Weak};
|
||||
|
||||
use image::{DynamicImage, ImageBuffer, LumaA, Rgba};
|
||||
use image::{ImageBuffer, Rgba};
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_color::Color;
|
||||
use vectorcraft_doc::{ImageBlob, ImageObject, NodeId, NodeKind};
|
||||
@@ -412,23 +411,36 @@ pub(crate) fn clear(blob: &ImageBlob, settings: &Settings, mut kept: Option<&mut
|
||||
bad(cmd, "every pixel matches the background: no image would be left")
|
||||
});
|
||||
}
|
||||
let out = match (px, gray) {
|
||||
(Pixels::Eight(img), false) => DynamicImage::ImageRgba8(img),
|
||||
(Pixels::Sixteen(img), false) => DynamicImage::ImageRgba16(img),
|
||||
(Pixels::Eight(img), true) => {
|
||||
DynamicImage::ImageLumaA8(ImageBuffer::from_fn(w, h, |x, y| LumaA([img.get_pixel(x, y).0[0], img.get_pixel(x, y).0[3]])))
|
||||
let raw = match (px, gray) {
|
||||
(Pixels::Eight(img), false) => {
|
||||
craft_image_codecs::Image::from_raw(w, h, craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, img.into_raw())
|
||||
}
|
||||
(Pixels::Sixteen(img), true) => {
|
||||
DynamicImage::ImageLumaA16(ImageBuffer::from_fn(w, h, |x, y| LumaA([img.get_pixel(x, y).0[0], img.get_pixel(x, y).0[3]])))
|
||||
}
|
||||
};
|
||||
let mut png = Vec::new();
|
||||
let mut encoder = image::codecs::png::PngEncoder::new(&mut png);
|
||||
if let Some(icc) = icc {
|
||||
use image::ImageEncoder as _;
|
||||
encoder.set_icc_profile(icc).map_err(|e| bad(cmd, e.to_string()))?;
|
||||
(Pixels::Sixteen(img), false) => craft_image_codecs::Image::from_raw(
|
||||
w,
|
||||
h,
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
img.into_raw().into_iter().flat_map(u16::to_ne_bytes).collect(),
|
||||
),
|
||||
(Pixels::Eight(img), true) => craft_image_codecs::Image::from_raw(
|
||||
w,
|
||||
h,
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U8,
|
||||
img.pixels().flat_map(|p| [p.0[0], p.0[3]]).collect(),
|
||||
),
|
||||
(Pixels::Sixteen(img), true) => craft_image_codecs::Image::from_raw(
|
||||
w,
|
||||
h,
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
img.pixels().flat_map(|p| [p.0[0], p.0[3]]).flat_map(u16::to_ne_bytes).collect(),
|
||||
),
|
||||
}
|
||||
out.write_with_encoder(encoder).map_err(|e| bad(cmd, e.to_string()))?;
|
||||
.map_err(|e| bad(cmd, e.to_string()))?
|
||||
.with_icc(icc);
|
||||
let mut png = craft_image_codecs::encode(&raw, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map_err(|e| bad(cmd, e.to_string()))?;
|
||||
// The resolution the file declares, which sizes the image at 100% when it is replaced or relinked.
|
||||
if let Some(r) = ppi::resolution(&blob.bytes) {
|
||||
png = ppi::with_png_resolution(&png, r);
|
||||
@@ -436,37 +448,70 @@ pub(crate) fn clear(blob: &ImageBlob, settings: &Settings, mut kept: Option<&mut
|
||||
Ok(Some(Cleared { blob: ImageBlob::png(png), width: w, height: h, cleared }))
|
||||
}
|
||||
|
||||
/// The image's size and the bytes a pixel of it takes to change, read from its header: its
|
||||
/// samples as decoded (bitmaps as 8-bit gray, palettes as RGB, or RGBA with a transparent entry),
|
||||
/// those worked on and kept for the PNG, and the work. A header that gives only the size (one
|
||||
/// whose file can't hold its pixels) counts the fewest: one 8-bit sample. `None` when the header
|
||||
/// doesn't say.
|
||||
fn header_pixels(bytes: &[u8]) -> Option<(u32, u32, u64)> {
|
||||
use craft_image_codecs::RasterColorType as T;
|
||||
let Some(i) = craft_image_codecs::inspect_raster(bytes).ok().flatten() else {
|
||||
return vectorcraft_doc::header_size(bytes).map(|(w, h)| (w, h, 1 + 2 * 4 + WORK_BYTES));
|
||||
};
|
||||
let deep = i.bits_per_sample > 8 && i.color_type != T::Indexed;
|
||||
let channels = match i.color_type {
|
||||
T::Indexed => 3 + u64::from(i.transparent_index.is_some()),
|
||||
T::Bitmap => 1,
|
||||
_ => u64::from(i.channels.max(1)),
|
||||
};
|
||||
let samples = channels * if deep { 2 } else { 1 };
|
||||
Some((i.width, i.height, samples + 2 * if deep { 8 } else { 4 } + WORK_BYTES))
|
||||
}
|
||||
|
||||
/// `blob`'s pixels as straight RGBA, whether they were gray, and its ICC profile. An image that
|
||||
/// can't be decoded or is too large to change is an error. The pixels are kept in `kept`, when it
|
||||
/// is given, if a copy of them fits the decode allowance beside the work. Errors are command `cmd`'s.
|
||||
fn decode(blob: &ImageBlob, kept: Option<&mut DecodedImages>, cmd: &str) -> Result<(Arc<Pixels>, bool, Option<Vec<u8>>)> {
|
||||
let reader = || {
|
||||
image::ImageReader::new(Cursor::new(blob.bytes.as_slice())).with_guessed_format().map_err(|_| bad(cmd, "the image's pixels can't be read"))
|
||||
};
|
||||
let mut decoder = reader()?.into_decoder().map_err(|e| bad(cmd, format!("the image's pixels can't be read: {e}")))?;
|
||||
// The ICC profile the colors are in goes with them into the PNG.
|
||||
let (w, h, kind, icc) = {
|
||||
use image::ImageDecoder as _;
|
||||
let (w, h) = decoder.dimensions();
|
||||
(w, h, decoder.color_type(), decoder.icc_profile().ok().flatten())
|
||||
};
|
||||
drop(decoder);
|
||||
// The file's pixels, those worked on (as many again for the PNG made of them) and the work,
|
||||
// checked from the header before anything is decoded.
|
||||
let deep = kind.bytes_per_pixel() > kind.channel_count();
|
||||
let per_pixel = u64::from(kind.bytes_per_pixel()) + 2 * if deep { 8 } else { 4 } + WORK_BYTES;
|
||||
if u64::from(w).checked_mul(u64::from(h)).and_then(|n| n.checked_mul(per_pixel)).is_none_or(|n| n > MAX_RASTER_ALLOC) {
|
||||
let too_large = |w: u32, h: u32, per_pixel: u64| {
|
||||
u64::from(w).checked_mul(u64::from(h)).and_then(|n| n.checked_mul(per_pixel)).is_none_or(|n| n > MAX_RASTER_ALLOC)
|
||||
};
|
||||
let header = header_pixels(&blob.bytes);
|
||||
if let Some((w, h, per_pixel)) = header
|
||||
&& too_large(w, h, per_pixel)
|
||||
{
|
||||
return Err(bad(cmd, format!("an image of {w} × {h} pixels is too large to change")));
|
||||
}
|
||||
let mut opts = craft_image_codecs::DecodeOptions::default();
|
||||
opts.limits.max_alloc = MAX_RASTER_ALLOC;
|
||||
let img = craft_image_codecs::decode_with(&blob.bytes, &opts).map_err(|e| match e {
|
||||
// Within the allowance from its header: a file that can't hold the pixels it declares.
|
||||
craft_image_codecs::CodecError::LimitExceeded(_) if header.is_none() => bad(cmd, "the image is too large to change"),
|
||||
_ => bad(cmd, format!("the image's pixels can't be read: {e}")),
|
||||
})?;
|
||||
let (w, h) = img.dimensions();
|
||||
let icc = img.icc.clone();
|
||||
let deep = img.sample_type() == craft_image_codecs::SampleType::U16;
|
||||
let gray = matches!(img.layout(), craft_image_codecs::ChannelLayout::Gray | craft_image_codecs::ChannelLayout::GrayA);
|
||||
let per_pixel = u64::from(img.layout().channels() as u32 * img.sample_type().bytes() as u32) + 2 * if deep { 8 } else { 4 } + WORK_BYTES;
|
||||
if too_large(w, h, per_pixel) {
|
||||
return Err(bad(cmd, format!("an image of {w} × {h} pixels is too large to change")));
|
||||
}
|
||||
let mut reader = reader()?;
|
||||
let mut limits = image::Limits::default();
|
||||
limits.max_alloc = Some(MAX_RASTER_ALLOC);
|
||||
reader.limits(limits);
|
||||
let img = reader.decode().map_err(|e| bad(cmd, format!("the image's pixels can't be read: {e}")))?;
|
||||
#[cfg(test)]
|
||||
DECODES.with(|c| c.set(c.get() + 1));
|
||||
let gray = matches!(img.color(), image::ColorType::L8 | image::ColorType::La8 | image::ColorType::L16 | image::ColorType::La16);
|
||||
let px = Arc::new(if deep { Pixels::Sixteen(img.into_rgba16()) } else { Pixels::Eight(img.into_rgba8()) });
|
||||
let px = Arc::new(if deep {
|
||||
let rgba16 = match (img.layout(), img.to_u16_samples()) {
|
||||
(craft_image_codecs::ChannelLayout::Rgba, Some(v)) => v,
|
||||
(craft_image_codecs::ChannelLayout::Rgb, Some(v)) => v.as_chunks::<3>().0.iter().flat_map(|c| [c[0], c[1], c[2], u16::MAX]).collect(),
|
||||
(craft_image_codecs::ChannelLayout::GrayA, Some(v)) => v.as_chunks::<2>().0.iter().flat_map(|c| [c[0], c[0], c[0], c[1]]).collect(),
|
||||
(craft_image_codecs::ChannelLayout::Gray, Some(v)) => v.into_iter().flat_map(|g| [g, g, g, u16::MAX]).collect(),
|
||||
_ => img.to_rgba8().into_iter().map(|b| u16::from(b) * 257).collect(),
|
||||
};
|
||||
Pixels::Sixteen(ImageBuffer::from_raw(w, h, rgba16).ok_or_else(|| bad(cmd, "invalid 16-bit pixel buffer"))?)
|
||||
} else {
|
||||
Pixels::Eight(ImageBuffer::from_raw(w, h, img.to_rgba8()).ok_or_else(|| bad(cmd, "invalid 8-bit pixel buffer"))?)
|
||||
});
|
||||
// A kept copy is worked on as a copy: one more set of the pixels worked on.
|
||||
let with_copy = per_pixel + if deep { 8 } else { 4 };
|
||||
if let Some(kept) = kept
|
||||
|
||||
@@ -287,9 +287,7 @@ fn apply_color_mask(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
/// The size in pixels of masked pixels `blob` (from its header), or the object's when it can't be
|
||||
/// read.
|
||||
fn mask_size(blob: Option<&ImageBlob>, width: u32, height: u32) -> (u32, u32) {
|
||||
blob.and_then(|b| image::ImageReader::new(std::io::Cursor::new(b.bytes.as_slice())).with_guessed_format().ok()?.into_dimensions().ok())
|
||||
.filter(|(w, h)| *w > 0 && *h > 0)
|
||||
.unwrap_or((width, height))
|
||||
blob.and_then(|b| b.pixel_size()).filter(|(w, h)| *w > 0 && *h > 0).unwrap_or((width, height))
|
||||
}
|
||||
|
||||
/// The mask of image node `n` made again for the image's own pixels, with the masked pixels when
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
use std::io::Cursor;
|
||||
|
||||
use image::DynamicImage;
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_color::{Color, Paint};
|
||||
@@ -15,7 +13,41 @@ const RED: [u8; 4] = [200, 0, 0, 255];
|
||||
|
||||
fn png(img: DynamicImage) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
img.write_to(&mut Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img;
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -138,7 +170,24 @@ fn applying_the_mask_makes_it_part_of_the_pixels() {
|
||||
assert_eq!(s.execute(APPLY_COLOR_MASK, &json!({})).unwrap(), json!({"changed": 1, "ids": [id.0]}));
|
||||
let d = doc(&s);
|
||||
assert_eq!((image_of(d, id).key, mask_of(d, id)), (m.key.clone(), None));
|
||||
let px = image::load_from_memory(&d.images[&m.key].bytes).unwrap().to_rgba8();
|
||||
let px = craft_image_codecs::decode(&d.images[&m.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!((px.get_pixel(0, 0).0[3], px.get_pixel(4, 4).0), (0, RED));
|
||||
assert_eq!(drawn(d, id, 0, 0)[3], 0);
|
||||
assert_eq!(undo_len(&s), steps + 1);
|
||||
@@ -149,7 +198,23 @@ fn applying_the_mask_makes_it_part_of_the_pixels() {
|
||||
fn svg_image(svg: &str) -> DynamicImage {
|
||||
let at = svg.find("data:image/png;base64,").expect("an embedded PNG") + "data:image/png;base64,".len();
|
||||
let end = at + svg[at..].find('"').unwrap();
|
||||
image::load_from_memory(&vectorcraft_format::base64_decode(&svg[at..end]).unwrap()).unwrap()
|
||||
craft_image_codecs::decode(&vectorcraft_format::base64_decode(&svg[at..end]).unwrap())
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -159,7 +224,28 @@ fn outputs_write_the_masked_pixels() {
|
||||
|s: &Session, format: &str| crate::cmd::fileio::encode_all(doc(s), format, &json!({"useArtboards": true})).unwrap().files.remove(0).1;
|
||||
let (png0, svg0, pdf0, eps0) = (export(&s, "png"), export(&s, "svg"), export(&s, "pdf"), export(&s, "eps"));
|
||||
s.execute(COLOR_MASK, &hide_white()).unwrap();
|
||||
let alpha = |bytes: &[u8]| image::load_from_memory(bytes).unwrap().to_rgba8().get_pixel(0, 0).0[3];
|
||||
let alpha = |bytes: &[u8]| {
|
||||
craft_image_codecs::decode(bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.get_pixel(0, 0)
|
||||
.0[3]
|
||||
};
|
||||
assert_eq!((alpha(&png0), alpha(&export(&s, "png"))), (255, 0), "PNG");
|
||||
let svg_alpha = |bytes: &[u8]| svg_image(std::str::from_utf8(bytes).unwrap()).to_rgba8().get_pixel(0, 0).0[3];
|
||||
assert_eq!((svg_alpha(&svg0), svg_alpha(&export(&s, "svg"))), (255, 0), "SVG");
|
||||
@@ -344,7 +430,24 @@ fn cropping_off_the_pixel_grid_keeps_the_hidden_colors_in_the_pixels() {
|
||||
let d = doc(&s);
|
||||
let (im, m) = (image_of(d, id), mask_of(d, id).unwrap());
|
||||
assert_eq!(m.from, im.key, "the mask is made for the cut pixels");
|
||||
let own = image::load_from_memory(&d.images[&im.key].bytes).unwrap().to_rgba8();
|
||||
let own = craft_image_codecs::decode(&d.images[&im.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert!(own.pixels().all(|p| p.0[3] == 255), "the cut pixels are the image's own, white included");
|
||||
assert_eq!(drawn(d, id, 0, 0)[3], 0, "white is hidden");
|
||||
s.execute(REMOVE_COLOR_MASK, &json!({"ids": [id.0]})).unwrap();
|
||||
@@ -377,7 +480,24 @@ fn applying_a_mask_made_for_older_pixels_makes_it_again_and_keeps_the_size() {
|
||||
assert_eq!((im.width, im.height, mask_of(d, id)), (24, 20, None));
|
||||
let after = d.node(id).unwrap().geometric_bounds().unwrap();
|
||||
assert!((after.width() - before.width()).abs() < 1e-9 && (after.height() - before.height()).abs() < 1e-9, "{before:?} {after:?}");
|
||||
let px = image::load_from_memory(&d.images[&im.key].bytes).unwrap().to_rgba8();
|
||||
let px = craft_image_codecs::decode(&d.images[&im.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!((px.get_pixel(0, 0).0[3], px.get_pixel(10, 10).0), (0, RED));
|
||||
}
|
||||
|
||||
@@ -435,7 +555,24 @@ fn another_image_colorized_with_transparency_does_not_change_the_refusal() {
|
||||
s.execute("paint.setFill", &json!({"color": "#000000"})).unwrap();
|
||||
assert_eq!(s.execute(COLOR_MASK, &hide_white()).unwrap()["changed"], 1, "an image colorized with black takes the mask beside the other");
|
||||
let m = mask_of(doc(&s), id).unwrap();
|
||||
let masked = image::load_from_memory(&doc(&s).images[&m.key].bytes).unwrap().to_luma_alpha8();
|
||||
let masked = craft_image_codecs::decode(&doc(&s).images[&m.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_luma_alpha8();
|
||||
assert_eq!((masked.get_pixel(0, 0).0[1], masked.get_pixel(4, 4).0[1]), (0, 255), "white is hidden and black shows");
|
||||
}
|
||||
|
||||
@@ -580,7 +717,24 @@ fn masks_outside_the_layers_are_made_again() {
|
||||
let art = &d.node(holder_id).unwrap().extras().mask.as_ref().unwrap().art;
|
||||
assert_eq!(from(art), key, "in the opacity mask's art");
|
||||
let m = Document::color_mask_of(art).unwrap();
|
||||
let px = image::load_from_memory(&d.images[&m.key].bytes).unwrap().to_rgba8();
|
||||
let px = craft_image_codecs::decode(&d.images[&m.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!((px.get_pixel(0, 0).0[3], px.get_pixel(1, 0).0), (0, RED));
|
||||
}
|
||||
|
||||
@@ -696,7 +850,24 @@ fn a_linked_file_read_again_on_open_takes_the_mask_again() {
|
||||
let d = doc(&s);
|
||||
let (im, m) = (image_of(d, id), mask_of(d, id).unwrap());
|
||||
assert_eq!(m.from, im.key, "made for the file's new pixels");
|
||||
let px = image::load_from_memory(&d.images[&m.key].bytes).unwrap().to_rgba8();
|
||||
let px = craft_image_codecs::decode(&d.images[&m.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!((px.get_pixel(0, 0).0[3], px.get_pixel(4, 4).0), (0, [0, 0, 200, 255]));
|
||||
}
|
||||
|
||||
@@ -812,7 +983,24 @@ fn a_live_color_adjustment_recolors_the_image_when_its_masked_pixels_are_missing
|
||||
let NodeKind::Image(im) = &out.kind else { panic!("an image") };
|
||||
assert_ne!(im.key, image_of(&d, id).key, "the expanded image is recolored");
|
||||
assert_eq!(Document::color_mask_of(&out), None, "the expanded image has no mask to make again from the recolored pixels");
|
||||
let px = image::load_from_memory(&d.images[&im.key].bytes).unwrap().to_rgba8();
|
||||
let px = craft_image_codecs::decode(&d.images[&im.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(px.get_pixel(1, 1).0, WHITE, "the expanded image shows the white");
|
||||
}
|
||||
|
||||
@@ -1002,6 +1190,23 @@ fn placing_a_document_brings_the_masked_pixels() {
|
||||
}
|
||||
let [(im, placed)] = found.as_slice() else { panic!("one masked image: {found:?}") };
|
||||
assert!(placed.made_for(im) && placed.from != m.from, "the image was renamed and the mask follows: {placed:?}");
|
||||
let px = image::load_from_memory(&d.images[&placed.key].bytes).unwrap().to_rgba8();
|
||||
let px = craft_image_codecs::decode(&d.images[&placed.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!((px.get_pixel(0, 0).0[3], px.get_pixel(4, 4).0), (0, RED));
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
use std::io::Cursor;
|
||||
use std::sync::Arc;
|
||||
|
||||
use image::{DynamicImage, ImageBuffer, Luma, LumaA, Rgb};
|
||||
@@ -21,7 +20,41 @@ fn settings(background: Background) -> Settings {
|
||||
|
||||
fn png(img: DynamicImage) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
img.write_to(&mut Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img;
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -53,7 +86,32 @@ fn bytes_of(s: &Session, id: NodeId) -> Arc<Vec<u8>> {
|
||||
}
|
||||
|
||||
fn pixels(s: &Session, id: NodeId) -> DynamicImage {
|
||||
image::load_from_memory(&bytes_of(s, id)).unwrap()
|
||||
craft_image_codecs::decode(&bytes_of(s, id))
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(layout, craft_image_codecs::SampleType::U16) => {
|
||||
let v = __cimg.to_u16_samples().unwrap();
|
||||
match layout {
|
||||
craft_image_codecs::ChannelLayout::Gray => image::DynamicImage::ImageLuma16(ImageBuffer::from_raw(__w, __h, v).unwrap()),
|
||||
craft_image_codecs::ChannelLayout::GrayA => image::DynamicImage::ImageLumaA16(ImageBuffer::from_raw(__w, __h, v).unwrap()),
|
||||
craft_image_codecs::ChannelLayout::Rgb => image::DynamicImage::ImageRgb16(ImageBuffer::from_raw(__w, __h, v).unwrap()),
|
||||
_ => image::DynamicImage::ImageRgba16(ImageBuffer::from_raw(__w, __h, v).unwrap()),
|
||||
}
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn alpha(img: &DynamicImage, x: u32, y: u32) -> u8 {
|
||||
@@ -164,13 +222,13 @@ fn preview_steps_on_the_same_pixels_decode_them_once() {
|
||||
/// image with an ICC profile comes out as a run on freshly decoded pixels gives it.
|
||||
#[test]
|
||||
fn reused_pixels_keep_their_depth_gray_and_profile() {
|
||||
use image::{ImageDecoder as _, ImageEncoder as _};
|
||||
let icc = b"a profile's bytes".to_vec();
|
||||
let gray16 = ImageBuffer::from_fn(8, 8, |x, y| Luma([if (2..6).contains(&x) && (2..6).contains(&y) { 7_000u16 } else { 61_000 }]));
|
||||
let mut bytes = Vec::new();
|
||||
let mut encoder = image::codecs::png::PngEncoder::new(&mut bytes);
|
||||
encoder.set_icc_profile(icc.clone()).unwrap();
|
||||
DynamicImage::ImageLuma16(gray16).write_with_encoder(encoder).unwrap();
|
||||
let raw: Vec<u8> = gray16.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect();
|
||||
let mut cimg =
|
||||
craft_image_codecs::Image::from_raw(8, 8, craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U16, raw).unwrap();
|
||||
cimg.icc = Some(icc.clone());
|
||||
let bytes = craft_image_codecs::encode(&cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
|
||||
let decodes = || super::DECODES.with(std::cell::Cell::get);
|
||||
let (mut s, id) = open(&bytes);
|
||||
let start = decodes();
|
||||
@@ -184,8 +242,8 @@ fn reused_pixels_keep_their_depth_gray_and_profile() {
|
||||
let DynamicImage::ImageLumaA16(out) = pixels(&s, id) else { panic!("16-bit gray with alpha") };
|
||||
assert_eq!((out.get_pixel(0, 0).0, out.get_pixel(3, 3).0), ([61_000, 0], [7_000, 65_535]));
|
||||
let result = bytes_of(&s, id);
|
||||
let mut decoder = image::codecs::png::PngDecoder::new(Cursor::new(result.as_slice())).unwrap();
|
||||
assert_eq!(decoder.icc_profile().unwrap(), Some(icc));
|
||||
let decoded = craft_image_codecs::decode_as(craft_image_codecs::Format::Png, result.as_slice()).unwrap();
|
||||
assert_eq!(decoded.icc, Some(icc));
|
||||
}
|
||||
|
||||
/// Once the background is cleared, most edge pixels are clear and a second run clears nothing: the
|
||||
@@ -385,16 +443,22 @@ fn a_gray_image_colorized_with_a_process_color_is_refused() {
|
||||
/// The ICC profile the colors are in stays with them.
|
||||
#[test]
|
||||
fn the_color_profile_is_kept() {
|
||||
use image::{ImageDecoder as _, ImageEncoder as _};
|
||||
let icc = b"a profile's bytes".to_vec();
|
||||
let mut bytes = Vec::new();
|
||||
let mut encoder = image::codecs::png::PngEncoder::new(&mut bytes);
|
||||
encoder.set_icc_profile(icc.clone()).unwrap();
|
||||
DynamicImage::ImageRgba8(framed()).write_with_encoder(encoder).unwrap();
|
||||
let f = framed();
|
||||
let mut cimg = craft_image_codecs::Image::from_raw(
|
||||
f.width(),
|
||||
f.height(),
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U8,
|
||||
f.into_raw(),
|
||||
)
|
||||
.unwrap();
|
||||
cimg.icc = Some(icc.clone());
|
||||
let bytes = craft_image_codecs::encode(&cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
|
||||
let blob = ImageBlob::png(bytes);
|
||||
let out = super::clear(&blob, &settings(Background::Edges), None, super::C).unwrap().unwrap();
|
||||
let mut decoder = image::codecs::png::PngDecoder::new(Cursor::new(out.blob.bytes.as_slice())).unwrap();
|
||||
assert_eq!(decoder.icc_profile().unwrap(), Some(icc));
|
||||
let decoded = craft_image_codecs::decode_as(craft_image_codecs::Format::Png, out.blob.bytes.as_slice()).unwrap();
|
||||
assert_eq!(decoded.icc, Some(icc));
|
||||
}
|
||||
|
||||
/// The resolution the file declares stays with the pixels, so a later Replace or relink sizes the
|
||||
|
||||
@@ -1498,9 +1498,41 @@ mod tests {
|
||||
s.execute("file.new", &json!({"width": 200, "height": 200})).unwrap();
|
||||
s.execute("swatch.new", &json!({"name": "Ink", "color": {"model": "cmyk", "c": 0, "m": 1, "y": 1, "k": 0}, "spot": true})).unwrap();
|
||||
let mut bytes = Vec::new();
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(2, 1, vec![0, 255]).unwrap())
|
||||
.write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(2, 1, vec![0, 255]).unwrap());
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| bytes.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let image = s
|
||||
.edit("Place", |d, sel| {
|
||||
d.images.insert("gray".into(), vectorcraft_doc::ImageBlob::png(bytes));
|
||||
@@ -1655,9 +1687,41 @@ mod tests {
|
||||
s.execute("paint.setStroke", &json!({"ids": [word], "none": true})).unwrap();
|
||||
// A grayscale image colorized with the spot color.
|
||||
let mut bytes = Vec::new();
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(2, 1, vec![0, 255]).unwrap())
|
||||
.write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(2, 1, vec![0, 255]).unwrap());
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| bytes.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let image = s
|
||||
.edit("Place", |d, sel| {
|
||||
d.images.insert("gray".into(), vectorcraft_doc::ImageBlob::png(bytes));
|
||||
|
||||
@@ -526,26 +526,34 @@ fn doc_compare(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
/// RGBA: a PNG, JPEG, WebP, TIFF or BMP within the allocation an opened image may take.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn compare_image(path: &str) -> Result<image::RgbaImage> {
|
||||
use image::ImageDecoder as _;
|
||||
const C: &str = "document.compare";
|
||||
let bytes = super::fileio::read_file(path)?;
|
||||
if let Some((w, h)) = vectorcraft_doc::cmyk::oversized_tiff(&bytes) {
|
||||
return Err(bad(C, format!("an image of {w} × {h} pixels is too large to draw")));
|
||||
}
|
||||
let mut reader = image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format().map_err(|e| bad(C, format!("{path}: {e}")))?;
|
||||
let readable = [image::ImageFormat::Png, image::ImageFormat::Jpeg, image::ImageFormat::WebP, image::ImageFormat::Tiff, image::ImageFormat::Bmp];
|
||||
if !reader.format().is_some_and(|f| readable.contains(&f)) {
|
||||
let format = craft_image_codecs::detect(&bytes);
|
||||
let readable = [
|
||||
craft_image_codecs::Format::Png,
|
||||
craft_image_codecs::Format::Jpeg,
|
||||
craft_image_codecs::Format::WebP,
|
||||
craft_image_codecs::Format::Tiff,
|
||||
craft_image_codecs::Format::Bmp,
|
||||
];
|
||||
let Some(format) = format.filter(|f| readable.contains(f)) else {
|
||||
return Err(bad(C, format!("{path}: not a PNG, JPEG, WebP, TIFF or BMP image")));
|
||||
}
|
||||
let mut limits = image::Limits::default();
|
||||
limits.max_alloc = Some(vectorcraft_doc::MAX_RASTER_ALLOC);
|
||||
reader.limits(limits);
|
||||
let decoder = reader.into_decoder().map_err(|e| bad(C, format!("{path}: {e}")))?;
|
||||
let (w, h) = decoder.dimensions();
|
||||
if decoder.total_bytes() > vectorcraft_doc::MAX_RASTER_ALLOC || u64::from(w) * u64::from(h) * 4 > vectorcraft_doc::MAX_RASTER_ALLOC {
|
||||
return Err(bad(C, format!("an image of {w} × {h} pixels is too large to draw")));
|
||||
}
|
||||
Ok(image::DynamicImage::from_decoder(decoder).map_err(|e| bad(C, format!("{path}: {e}")))?.to_rgba8())
|
||||
};
|
||||
let mut opts = craft_image_codecs::DecodeOptions::default();
|
||||
opts.limits.max_alloc = vectorcraft_doc::MAX_RASTER_ALLOC;
|
||||
opts.limits.max_pixels = vectorcraft_doc::MAX_RASTER_ALLOC / 4;
|
||||
let img = match craft_image_codecs::decode_as_with(format, &bytes, &opts) {
|
||||
Ok(img) => img,
|
||||
Err(craft_image_codecs::CodecError::LimitExceeded(_)) => {
|
||||
return Err(bad(C, "image is too large to draw"));
|
||||
}
|
||||
Err(e) => return Err(bad(C, format!("{path}: {e}"))),
|
||||
};
|
||||
let (w, h) = img.dimensions();
|
||||
image::RgbaImage::from_raw(w, h, img.to_rgba8()).ok_or_else(|| bad(C, format!("{path}: invalid pixel buffer")))
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
//! native document can't be read opens as its embedded preview instead, with a warning that says
|
||||
//! why, and can't be placed.
|
||||
|
||||
use std::io::Cursor;
|
||||
use std::sync::Arc;
|
||||
|
||||
use kurbo::{Affine, BezPath, Point, Rect, Vec2};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::{Builder, Cursor, Document, Result, err, raster_doc};
|
||||
use super::{Builder, Document, Result, err, raster_doc};
|
||||
|
||||
/// What an Affinity file opened as.
|
||||
pub(in super::super) struct Imported {
|
||||
@@ -30,13 +30,11 @@ fn preview(name: &str, bytes: &[u8], native: &vectorcraft_affinity::Error) -> Re
|
||||
let p = vectorcraft_affinity::preview(bytes).map_err(|e| err(format!("{native}; its embedded preview can't be read either ({e})")))?;
|
||||
// `raster_doc` normally probes PNG dimensions and keeps its encoded bytes. Verify this
|
||||
// untrusted preview's complete pixels first, so corrupt data cannot create a blank image.
|
||||
let mut reader = image::ImageReader::with_format(Cursor::new(p.png), image::ImageFormat::Png);
|
||||
let mut limits = image::Limits::default();
|
||||
limits.max_image_width = Some(4096);
|
||||
limits.max_image_height = Some(4096);
|
||||
limits.max_alloc = Some(128 << 20);
|
||||
reader.limits(limits);
|
||||
let _pixels = reader.decode().map_err(err)?;
|
||||
let mut opts = craft_image_codecs::DecodeOptions::default();
|
||||
opts.limits.max_width = 4096;
|
||||
opts.limits.max_height = 4096;
|
||||
opts.limits.max_alloc = 128 << 20;
|
||||
let _pixels = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Png, p.png, &opts).map_err(err)?;
|
||||
let d = raster_doc(name, p.png)?;
|
||||
Ok((
|
||||
d,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::nodes::MAX_DEPTH;
|
||||
use super::{AfAlign, CharStyle, Cursor, Justify, Kind, Rect, TextKind, TextObject, VerticalAlign, af, shapes};
|
||||
use super::{AfAlign, CharStyle, Justify, Kind, Rect, TextKind, TextObject, VerticalAlign, af, shapes};
|
||||
|
||||
/// The master page instances among `nodes` (also inside layers and groups): (master, first page,
|
||||
/// first master page, page count).
|
||||
@@ -131,10 +131,9 @@ pub(super) fn fit_lines(obj: &mut TextObject, w: f64, h: f64) -> bool {
|
||||
|
||||
/// RGBA pixels as a PNG file.
|
||||
pub(super) fn png(width: u32, height: u32, rgba: Vec<u8>) -> Option<Vec<u8>> {
|
||||
let buf = image::RgbaImage::from_raw(width, height, rgba)?;
|
||||
let mut png = Vec::new();
|
||||
buf.write_to(&mut Cursor::new(&mut png), image::ImageFormat::Png).ok()?;
|
||||
Some(png)
|
||||
let raw =
|
||||
craft_image_codecs::Image::from_raw(width, height, craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, rgba).ok()?;
|
||||
craft_image_codecs::encode(&raw, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).ok()
|
||||
}
|
||||
|
||||
/// Where a line of `w` starts in a span from 0 to `room` (as a share of `room - w`).
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! JPEG 2000 JP2 and codestream images for File › Open and File › Place. The decoded image is
|
||||
//! bounded before allocation and converted to RGBA for the common PNG storage path.
|
||||
|
||||
use hayro_jpeg2000::{ColorSpace, DecodeSettings, DecoderContext, Image};
|
||||
use craft_image_codecs::{ChannelLayout, CodecError, Format, Limits};
|
||||
use vectorcraft_color::cms::{IccProfile, Intent};
|
||||
|
||||
/// Recognize a JP2 signature box or the start of a raw codestream. The decoder checks the rest.
|
||||
@@ -31,249 +31,66 @@ pub(super) fn boxes(b: &[u8]) -> impl Iterator<Item = (&[u8], &[u8])> {
|
||||
})
|
||||
}
|
||||
|
||||
/// The decoder ignores JPX layers and composition and silently uses the last top-level jp2c.
|
||||
fn unsupported_jpx_content(bytes: &[u8]) -> bool {
|
||||
fn association_has_content(data: &[u8], depth: usize) -> bool {
|
||||
if depth >= 16 {
|
||||
return true;
|
||||
}
|
||||
boxes(data).any(|(ty, child)| {
|
||||
matches!(ty, b"jp2c" | b"jpch" | b"jplh" | b"comp" | b"ftbl") || (ty == b"asoc" && association_has_content(child, depth + 1))
|
||||
})
|
||||
}
|
||||
let mut codestreams = 0u8;
|
||||
for (ty, data) in boxes(bytes) {
|
||||
match ty {
|
||||
b"jp2c" => {
|
||||
codestreams = codestreams.saturating_add(1);
|
||||
if codestreams > 1 {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
b"jpch" | b"jplh" | b"comp" | b"ftbl" => return true,
|
||||
b"asoc" if association_has_content(data, 0) => return true,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// The largest embedded ICC profile read.
|
||||
const MAX_PROFILE: usize = 4 * 1024 * 1024;
|
||||
/// A codestream numbers its tiles 0 to 65,534 (Isot in SOT, ITU-T T.800 A.4.2).
|
||||
const MAX_TILES: u64 = 65_535;
|
||||
/// Bytes the decoder keeps for each component of each tile before decoding any of them.
|
||||
const TILE_COMPONENT_BYTES: u64 = 256;
|
||||
|
||||
/// SIZ follows SOC in the codestream; Lsiz includes its two length bytes. Returns the component
|
||||
/// count, the image size and the tile count.
|
||||
fn codestream_size(bytes: &[u8]) -> Option<(u64, u32, u32, u64)> {
|
||||
let stream = if bytes.starts_with(b"\xff\x4f") { bytes } else { boxes(bytes).find_map(|(ty, data)| (ty == b"jp2c").then_some(data))? };
|
||||
if stream.get(..4)? != b"\xff\x4f\xff\x51" {
|
||||
return None;
|
||||
}
|
||||
let length = u16::from_be_bytes(stream.get(4..6)?.try_into().ok()?) as usize;
|
||||
let count = u16::from_be_bytes(stream.get(40..42)?.try_into().ok()?) as usize;
|
||||
if count == 0 || length != 38usize.checked_add(count.checked_mul(3)?)? || stream.get(4..4usize.checked_add(length)?)?.len() != length {
|
||||
return None;
|
||||
}
|
||||
let be32 = |range: std::ops::Range<usize>| stream.get(range).and_then(|v| v.try_into().ok()).map(u32::from_be_bytes);
|
||||
let width = be32(8..12)?.checked_sub(be32(16..20)?)?;
|
||||
let height = be32(12..16)?.checked_sub(be32(20..24)?)?;
|
||||
// Tiles across and down (T.800 B-5): the grid from the tile offset, in whole tiles.
|
||||
let tiles = |grid: u32, offset: u32, tile: u32| match (grid.checked_sub(offset), tile) {
|
||||
(Some(span), 1..) => Some(u64::from(span).div_ceil(u64::from(tile))),
|
||||
_ => None,
|
||||
};
|
||||
let across = tiles(be32(8..12)?, be32(32..36)?, be32(24..28)?)?;
|
||||
let down = tiles(be32(12..16)?, be32(36..40)?, be32(28..32)?)?;
|
||||
Some((count as u64, width, height, across.checked_mul(down)?))
|
||||
}
|
||||
|
||||
/// One `jp2h` header with at most one palette, component mapping and channel definition box, so
|
||||
/// the checks below read the boxes the decoder uses.
|
||||
fn single_header_boxes(bytes: &[u8]) -> bool {
|
||||
let mut headers = boxes(bytes).filter(|(ty, _)| *ty == b"jp2h");
|
||||
let (Some((_, header)), None) = (headers.next(), headers.next()) else {
|
||||
return false;
|
||||
};
|
||||
[b"pclr", b"cmap", b"cdef"].iter().all(|kind| boxes(header).filter(|(ty, _)| ty == kind).count() <= 1)
|
||||
}
|
||||
|
||||
/// The number of channel definitions in a JP2 `cdef` box, `None` without one. Only color channels
|
||||
/// 1 to n, in any order, followed by at most one opacity channel for the whole image are read;
|
||||
/// any other layout (premultiplied opacity, opacity before a color, an unassociated channel) gives
|
||||
/// `Some(0)`, which no image matches.
|
||||
fn channel_definitions(bytes: &[u8]) -> Option<u64> {
|
||||
if bytes.starts_with(b"\xff\x4f") {
|
||||
return None;
|
||||
}
|
||||
let header = boxes(bytes).find_map(|(ty, data)| (ty == b"jp2h").then_some(data))?;
|
||||
let cdef = boxes(header).find_map(|(ty, data)| (ty == b"cdef").then_some(data))?;
|
||||
let count = cdef.get(..2).and_then(|n| n.try_into().ok()).map_or(0, |n: [u8; 2]| usize::from(u16::from_be_bytes(n)));
|
||||
let Some(entries) = cdef.get(2..).filter(|rest| count > 0 && rest.len() == count * 6) else {
|
||||
return Some(0);
|
||||
};
|
||||
// (channel, type, association) by channel.
|
||||
let mut defs: Vec<[u16; 3]> =
|
||||
entries.as_chunks::<6>().0.iter().map(|&[a, b, c, d, e, f]| [[a, b], [c, d], [e, f]].map(u16::from_be_bytes)).collect();
|
||||
defs.sort_unstable();
|
||||
let opacity = defs.last().is_some_and(|&[_, kind, association]| kind == 1 && association == 0);
|
||||
let colors = defs.len() - usize::from(opacity);
|
||||
let mut seen = vec![false; colors];
|
||||
let layout_ok = defs.iter().enumerate().all(|(i, &[channel, kind, association])| {
|
||||
usize::from(channel) == i
|
||||
&& (i >= colors
|
||||
|| (kind == 0
|
||||
&& usize::from(association).checked_sub(1).and_then(|slot| seen.get_mut(slot)).is_some_and(|s| !std::mem::replace(s, true))))
|
||||
});
|
||||
Some(if layout_ok { defs.len() as u64 } else { 0 })
|
||||
}
|
||||
|
||||
/// The column count of a JP2 palette (`pclr`), `Ok(None)` without one. A component mapping (`cmap`)
|
||||
/// beside it needs one entry per column, each naming a codestream component and, when it maps
|
||||
/// through the palette, a palette column; anything else is `Err`.
|
||||
fn palette_columns(bytes: &[u8], components: u64) -> Result<Option<u64>, ()> {
|
||||
if bytes.starts_with(b"\xff\x4f") {
|
||||
return Ok(None);
|
||||
}
|
||||
let Some(header) = boxes(bytes).find_map(|(ty, data)| (ty == b"jp2h").then_some(data)) else {
|
||||
return Ok(None);
|
||||
};
|
||||
let Some(palette) = boxes(header).find_map(|(ty, data)| (ty == b"pclr").then_some(data)) else {
|
||||
return Ok(None);
|
||||
};
|
||||
let columns = palette.get(2).copied().ok_or(())?;
|
||||
// Each column's descriptor: unsigned, 1 to 31 bits, as codestream samples are.
|
||||
let depths = palette.get(3..3 + usize::from(columns)).ok_or(())?;
|
||||
if columns == 0 || depths.iter().any(|&d| d & 0x80 != 0 || (d & 0x7f) + 1 > 31) {
|
||||
return Err(());
|
||||
}
|
||||
// Without a mapping the decoder maps every column from the first component.
|
||||
let Some(mapping) = boxes(header).find_map(|(ty, data)| (ty == b"cmap").then_some(data)) else {
|
||||
return Ok(Some(u64::from(columns)));
|
||||
};
|
||||
if mapping.len() != usize::from(columns) * 4 {
|
||||
return Err(());
|
||||
}
|
||||
for &[c0, c1, kind, column] in mapping.as_chunks::<4>().0 {
|
||||
let valid = u64::from(u16::from_be_bytes([c0, c1])) < components
|
||||
&& match kind {
|
||||
0 => true,
|
||||
1 => column < columns,
|
||||
_ => false,
|
||||
};
|
||||
if !valid {
|
||||
return Err(());
|
||||
}
|
||||
}
|
||||
Ok(Some(u64::from(columns)))
|
||||
}
|
||||
|
||||
/// Decode within the raster allocation allowance, counting source samples, RGBA output, and
|
||||
/// decoder intermediates. The decoder may need multiple full-sized component planes.
|
||||
/// Decode `bytes` to RGBA, checking memory limits and converting embedded ICC/CMYK color spaces
|
||||
/// to sRGB via `vectorcraft_color`.
|
||||
pub(super) fn decode(bytes: &[u8], max_alloc: u64) -> Result<image::RgbaImage, String> {
|
||||
if bytes.starts_with(b"\0\0\0\x0cjP \r\n\x87\n") && unsupported_jpx_content(bytes) {
|
||||
return Err("the JPEG 2000 file has an unsupported color space or JPX content".into());
|
||||
}
|
||||
if bytes.starts_with(b"\0\0\0\x0cjP \r\n\x87\n") && !single_header_boxes(bytes) {
|
||||
if !is_jpeg2000(bytes) {
|
||||
return Err("the JPEG 2000 file can't be read".into());
|
||||
}
|
||||
let (codestream_channels, declared_w, declared_h, tile_count) = codestream_size(bytes).ok_or("the JPEG 2000 file can't be read")?;
|
||||
if tile_count == 0 || tile_count > MAX_TILES {
|
||||
return Err("the JPEG 2000 file can't be read".into());
|
||||
}
|
||||
let declared_pixels = u64::from(declared_w).checked_mul(u64::from(declared_h));
|
||||
if declared_w == 0
|
||||
|| declared_h == 0
|
||||
|| declared_pixels.and_then(|n| n.checked_mul(codestream_channels)).and_then(|n| n.checked_mul(4)).is_none_or(|n| n > max_alloc)
|
||||
|| tile_count.checked_mul(codestream_channels).and_then(|n| n.checked_mul(TILE_COMPONENT_BYTES)).is_none_or(|n| n > max_alloc)
|
||||
{
|
||||
return Err(format!("an image of {declared_w} × {declared_h} pixels is too large to draw"));
|
||||
}
|
||||
// The decoder copies an embedded profile (after a 3-byte `colr` header) when it reads the file.
|
||||
let header = boxes(bytes).find_map(|(ty, data)| (ty == b"jp2h").then_some(data)).unwrap_or_default();
|
||||
if boxes(header).any(|(ty, data)| ty == b"colr" && data.len() > MAX_PROFILE + 3) {
|
||||
return Err("the JPEG 2000 file has an unsupported color space or JPX content".into());
|
||||
}
|
||||
let palette = palette_columns(bytes, codestream_channels).map_err(|()| "the JPEG 2000 file can't be read")?;
|
||||
// The decoder keeps each palette value in 8 bytes and each row in its own list.
|
||||
let palette_rows = boxes(header).find_map(|(ty, data)| (ty == b"pclr").then(|| data.get(..2)).flatten());
|
||||
if let (Some(columns), Some(&[r0, r1])) = (palette, palette_rows)
|
||||
&& u64::from(u16::from_be_bytes([r0, r1])) * (columns * 8 + 24) > max_alloc
|
||||
{
|
||||
return Err(format!("an image of {declared_w} × {declared_h} pixels is too large to draw"));
|
||||
}
|
||||
let image = Image::new(bytes, &DecodeSettings::default()).map_err(|_| "the JPEG 2000 file can't be read")?;
|
||||
let limits = Limits { max_alloc, max_pixels: max_alloc / 4, ..Limits::default() };
|
||||
let opts = craft_image_codecs::DecodeOptions { limits, ..craft_image_codecs::DecodeOptions::default() };
|
||||
let image = craft_image_codecs::decode_as_with(Format::Jp2, bytes, &opts).map_err(|e| match e {
|
||||
CodecError::LimitExceeded(_) => "an image is too large to draw".to_string(),
|
||||
CodecError::Unsupported { .. } => "the JPEG 2000 file has an unsupported color space or JPX content".to_string(),
|
||||
_ => "the JPEG 2000 file can't be read".to_string(),
|
||||
})?;
|
||||
let (w, h) = (image.width(), image.height());
|
||||
let color = image.color_space();
|
||||
let channels = match color {
|
||||
ColorSpace::Gray => 1,
|
||||
ColorSpace::RGB => 3,
|
||||
ColorSpace::CMYK => 4,
|
||||
ColorSpace::Icc { num_channels: 1 | 3 | 4, profile } if profile.len() <= MAX_PROFILE => color.num_channels(),
|
||||
_ => return Err("the JPEG 2000 file has an unsupported color space or JPX content".into()),
|
||||
};
|
||||
let stride = u64::from(channels) + u64::from(image.has_alpha());
|
||||
// The decoder sizes its output by the palette's columns and writes one channel per mapping
|
||||
// entry, so the columns, the entries and the output channels must all match.
|
||||
if palette.is_some_and(|columns| columns != stride) {
|
||||
return Err("the JPEG 2000 file can't be read".into());
|
||||
}
|
||||
// The decoder reorders the channels by their definitions (`cdef`) and drops any channel without
|
||||
// one, so a definition is needed for every channel it writes.
|
||||
if channel_definitions(bytes).is_some_and(|count| count != stride || count != palette.unwrap_or(codestream_channels)) {
|
||||
return Err("the JPEG 2000 file can't be read".into());
|
||||
}
|
||||
let plane_channels = codestream_channels.max(stride).max(palette.unwrap_or(0));
|
||||
let too_large = || format!("an image of {w} × {h} pixels is too large to draw");
|
||||
let pixels = u64::from(w).checked_mul(u64::from(h)).ok_or_else(too_large)?;
|
||||
let size = pixels.checked_mul(stride).ok_or_else(too_large)?;
|
||||
let rgba_size = pixels.checked_mul(4).ok_or_else(too_large)?;
|
||||
// hayro 0.3.5 keeps a f32 plane per component, including alpha, and a second
|
||||
// f32 coefficient plane per component. Palette mapping or channel definitions
|
||||
// can temporarily clone the component planes. Reserve another 8 bytes per
|
||||
// pixel for IDWT and code-block scratch while samples and RGBA coexist.
|
||||
let plane_bytes = pixels.checked_mul(plane_channels).and_then(|n| n.checked_mul(4)).ok_or_else(too_large)?;
|
||||
let scratch_bytes = plane_bytes.checked_mul(2).and_then(|n| n.checked_add(pixels.checked_mul(8)?)).ok_or_else(too_large)?;
|
||||
let working =
|
||||
size.checked_add(rgba_size).and_then(|n| n.checked_add(plane_bytes)).and_then(|n| n.checked_add(scratch_bytes)).ok_or_else(too_large)?;
|
||||
if w == 0 || h == 0 || working > max_alloc {
|
||||
if rgba_size > max_alloc {
|
||||
return Err(too_large());
|
||||
}
|
||||
let len = usize::try_from(size).map_err(|_| too_large())?;
|
||||
let mut samples = vec![0; len];
|
||||
image.decode_into(&mut samples, &mut DecoderContext::default()).map_err(|_| "the JPEG 2000 file can't be read")?;
|
||||
let profile = match color {
|
||||
ColorSpace::Icc { profile, .. } => {
|
||||
Some(IccProfile::from_bytes(None, profile).map_err(|_| "the JPEG 2000 file has an unsupported color space or JPX content")?)
|
||||
}
|
||||
_ => None,
|
||||
let layout = image.layout();
|
||||
let (channels, has_alpha) = match layout {
|
||||
ChannelLayout::Gray => (1usize, false),
|
||||
ChannelLayout::GrayA => (1, true),
|
||||
ChannelLayout::Rgb => (3, false),
|
||||
ChannelLayout::Rgba => (3, true),
|
||||
ChannelLayout::Cmyk => (4, false),
|
||||
ChannelLayout::CmykA => (4, true),
|
||||
_ => return Err("the JPEG 2000 file has an unsupported color space or JPX content".into()),
|
||||
};
|
||||
let stride = channels + usize::from(has_alpha);
|
||||
let profile = match image.icc.as_deref() {
|
||||
Some(icc) => Some(IccProfile::from_bytes(None, icc).map_err(|_| "the JPEG 2000 file has an unsupported color space or JPX content")?),
|
||||
None => None,
|
||||
};
|
||||
let cms = vectorcraft_color::cms::active();
|
||||
let mut rgba = Vec::with_capacity(usize::try_from(rgba_size).map_err(|_| too_large())?);
|
||||
for sample in samples.chunks_exact(stride as usize) {
|
||||
for sample in image.data().chunks_exact(stride) {
|
||||
let at = |i: usize| sample.get(i).copied().unwrap_or(0);
|
||||
let rgb = match (color, profile.as_ref()) {
|
||||
(ColorSpace::Gray, _) => [at(0); 3],
|
||||
(ColorSpace::RGB, _) => [at(0), at(1), at(2)],
|
||||
(ColorSpace::CMYK, _) => {
|
||||
cms.cmyk_to_srgb([at(0), at(1), at(2), at(3)].map(|v| f32::from(v) / 255.0), false).map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8)
|
||||
let rgb = if let Some(profile) = profile.as_ref() {
|
||||
let mut values = [0.0f32; 4];
|
||||
for (dst, src) in values.iter_mut().zip(sample.iter().take(channels)) {
|
||||
*dst = f32::from(*src) / 255.0;
|
||||
}
|
||||
(ColorSpace::Icc { .. }, Some(profile)) => {
|
||||
let mut values = [0.0f32; 4];
|
||||
for (dst, src) in values.iter_mut().zip(sample.iter().take(usize::from(channels))) {
|
||||
*dst = f32::from(*src) / 255.0;
|
||||
}
|
||||
profile
|
||||
.to_srgb_into(&values[..usize::from(channels)], Intent::RelativeColorimetric)
|
||||
.ok_or("the JPEG 2000 file has an unsupported color space or JPX content")?
|
||||
.map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8)
|
||||
profile
|
||||
.to_srgb_into(&values[..channels], Intent::RelativeColorimetric)
|
||||
.ok_or("the JPEG 2000 file has an unsupported color space or JPX content")?
|
||||
.map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8)
|
||||
} else {
|
||||
match layout {
|
||||
ChannelLayout::Gray | ChannelLayout::GrayA => [at(0); 3],
|
||||
ChannelLayout::Rgb | ChannelLayout::Rgba => [at(0), at(1), at(2)],
|
||||
ChannelLayout::Cmyk | ChannelLayout::CmykA => cms
|
||||
.cmyk_to_srgb([at(0), at(1), at(2), at(3)].map(|v| f32::from(v) / 255.0), false)
|
||||
.map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8),
|
||||
_ => return Err("the JPEG 2000 file has an unsupported color space or JPX content".into()),
|
||||
}
|
||||
_ => return Err("the JPEG 2000 file has an unsupported color space or JPX content".into()),
|
||||
};
|
||||
let [r, g, b] = rgb;
|
||||
rgba.extend_from_slice(&[r, g, b, if image.has_alpha() { at(usize::from(channels)) } else { 255 }]);
|
||||
rgba.extend_from_slice(&[r, g, b, if has_alpha { at(channels) } else { 255 }]);
|
||||
}
|
||||
image::RgbaImage::from_raw(w, h, rgba).ok_or_else(|| "the JPEG 2000 file can't be read".into())
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
//! `document.open`: every readable format into a new document.
|
||||
|
||||
use std::io::Cursor;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_doc::{Document, ImageBlob, ImageObject, Node, NodeKind};
|
||||
use vectorcraft_geom::Affine;
|
||||
@@ -133,7 +131,7 @@ pub fn detect(name: &str, bytes: &[u8]) -> Option<&'static Format> {
|
||||
if super::heifread::is_heif(bytes) {
|
||||
return format("heif");
|
||||
}
|
||||
if let Some(f) = image::guess_format(bytes).ok().and_then(image_format) {
|
||||
if let Some(f) = craft_image_codecs::detect(bytes).and_then(image_format) {
|
||||
return Some(f);
|
||||
}
|
||||
// A version 2 PICT is known by its version opcodes after the picture's frame, at byte 522 of a
|
||||
@@ -159,9 +157,17 @@ pub fn detect(name: &str, bytes: &[u8]) -> Option<&'static Format> {
|
||||
by_name
|
||||
}
|
||||
|
||||
/// The readable raster format behind an `image` crate format.
|
||||
fn image_format(f: image::ImageFormat) -> Option<&'static Format> {
|
||||
f.extensions_str().iter().find_map(|e| format(e)).filter(|f| f.read && f.raster)
|
||||
/// The readable raster format behind a craft-image-codecs format.
|
||||
fn image_format(f: craft_image_codecs::Format) -> Option<&'static Format> {
|
||||
f.extensions().iter().find_map(|e| format(e)).filter(|f| f.read && f.raster)
|
||||
}
|
||||
|
||||
/// The image's size and the bytes a pixel of it takes decoded, from its header
|
||||
/// ([`vectorcraft_doc::decode::decoded_layout`]), for bytes in a format craft-image-codecs tells.
|
||||
fn header_layout(bytes: &[u8]) -> Option<(craft_image_codecs::Format, u32, u32, u64)> {
|
||||
let format = craft_image_codecs::detect(bytes)?;
|
||||
let (w, h, per_pixel) = vectorcraft_doc::decode::decoded_layout(bytes, format)?;
|
||||
Some((format, w, h, per_pixel))
|
||||
}
|
||||
|
||||
/// Why a file carrying editing data (an SVG or PDF saved with Preserve Editing) opened as plain
|
||||
@@ -892,22 +898,18 @@ pub(crate) use vectorcraft_doc::MAX_RASTER_ALLOC;
|
||||
/// says so; the pixels aren't read): Open, Place and the Links panel say it ([`size_note`], #1479).
|
||||
/// `None` for images within the allowance, and for bytes that aren't an image.
|
||||
pub(crate) fn too_large_note(bytes: &[u8]) -> Option<String> {
|
||||
use image::ImageDecoder as _;
|
||||
if let Some((w, h)) = vectorcraft_doc::cmyk::oversized_tiff(bytes) {
|
||||
return Some(format!("an image of {w} × {h} pixels is too large to draw"));
|
||||
}
|
||||
let reader = image::ImageReader::new(Cursor::new(bytes)).with_guessed_format().ok()?;
|
||||
let format = reader.format()?;
|
||||
let d = reader.into_decoder().ok()?;
|
||||
let (w, h) = d.dimensions();
|
||||
let (format, w, h, per_pixel) = header_layout(bytes)?;
|
||||
// The allowance also counts the pixels as RGBA: an image of fewer than 4 bytes a pixel (gray,
|
||||
// gray with alpha, RGB) takes more than the decoder counts (#1717).
|
||||
let rgba_over = u64::from(w) * u64::from(h) > MAX_RASTER_ALLOC / 4;
|
||||
let samples_over = d.total_bytes() > MAX_RASTER_ALLOC;
|
||||
let samples_over = (u64::from(w) * u64::from(h)).saturating_mul(per_pixel) > MAX_RASTER_ALLOC;
|
||||
// The renderer can stream a non-interlaced PNG a row at a time when those buffers fit. Its
|
||||
// full decoded byte count can exceed the allowance even though drawing it at native size is
|
||||
// safe; use the renderer's decision for that case instead of warning from total_bytes alone.
|
||||
let png_streams_at_size = samples_over && format == image::ImageFormat::Png && vectorcraft_render::reduced_size(bytes) == Some((w, h));
|
||||
let png_streams_at_size = samples_over && format == craft_image_codecs::Format::Png && vectorcraft_render::reduced_size(bytes) == Some((w, h));
|
||||
(rgba_over || (samples_over && !png_streams_at_size)).then(|| format!("an image of {w} × {h} pixels is too large to draw"))
|
||||
}
|
||||
|
||||
@@ -918,7 +920,7 @@ pub(crate) fn too_large_note(bytes: &[u8]) -> Option<String> {
|
||||
pub(crate) fn size_note(bytes: &[u8]) -> Option<String> {
|
||||
let note = too_large_note(bytes)?;
|
||||
let Some(drawn) = vectorcraft_render::reduced_size(bytes) else { return Some(note) };
|
||||
let (w, h) = image::ImageReader::new(Cursor::new(bytes)).with_guessed_format().ok()?.into_dimensions().ok()?;
|
||||
let (w, h) = vectorcraft_doc::header_size(bytes)?;
|
||||
(drawn != (w, h)).then(|| format!("an image of {w} × {h} pixels is shown at reduced size"))
|
||||
}
|
||||
|
||||
@@ -978,17 +980,14 @@ pub fn raster_image(bytes: &[u8]) -> Result<RasterImage> {
|
||||
if super::pxrread::is_pxr(bytes) {
|
||||
return as_png(super::pxrread::decode(bytes, MAX_RASTER_ALLOC).map_err(err)?, None, ppi);
|
||||
}
|
||||
let mut reader = image::ImageReader::new(Cursor::new(bytes)).with_guessed_format().map_err(err)?;
|
||||
if reader.format().is_none() && super::tgaread::is_targa(bytes) {
|
||||
reader.set_format(image::ImageFormat::Tga);
|
||||
let mut detected = craft_image_codecs::detect(bytes);
|
||||
if detected.is_none() && super::tgaread::is_targa(bytes) {
|
||||
detected = Some(craft_image_codecs::Format::Tga);
|
||||
}
|
||||
if reader.format().is_none() && super::pcxread::is_pcx(bytes) {
|
||||
if (detected.is_none() || detected == Some(craft_image_codecs::Format::Pcx)) && super::pcxread::is_pcx(bytes) {
|
||||
return as_png(super::pcxread::decode(bytes, MAX_RASTER_ALLOC).map_err(err)?, None, ppi);
|
||||
}
|
||||
let mut limits = image::Limits::default();
|
||||
limits.max_alloc = Some(MAX_RASTER_ALLOC);
|
||||
reader.limits(limits);
|
||||
let kind = reader.format().ok_or_else(|| err("not an image VectorCraft reads (see document.formats)"))?;
|
||||
let kind = detected.ok_or_else(|| err("not an image VectorCraft reads (see document.formats)"))?;
|
||||
let f = image_format(kind).ok_or_else(|| err(format!("{kind:?} images can't be opened (see document.formats)")))?;
|
||||
// A photo whose EXIF orientation turns or flips it (a camera held upright) is stored as it is
|
||||
// seen, so it places the way it was taken (#4284); the others keep their own bytes.
|
||||
@@ -1009,8 +1008,8 @@ pub fn raster_image(bytes: &[u8]) -> Result<RasterImage> {
|
||||
_ => None,
|
||||
};
|
||||
if let Some(kept) = kept {
|
||||
let (width, height) = reader.into_dimensions().map_err(err)?;
|
||||
return stored(kept, f.mime, (width, height), ppi);
|
||||
let size = vectorcraft_doc::header_size(bytes).ok_or_else(|| err("the image's size can't be read"))?;
|
||||
return stored(kept, f.mime, size, ppi);
|
||||
}
|
||||
let extra_samples = || {
|
||||
let cms = vectorcraft_color::cms::active();
|
||||
@@ -1023,21 +1022,23 @@ pub fn raster_image(bytes: &[u8]) -> Result<RasterImage> {
|
||||
None => match (f.id == "tiff").then(|| vectorcraft_doc::cmyk::group3_tiff_rgba(bytes)).flatten() {
|
||||
Some(read) => image::DynamicImage::ImageRgba8(read.map_err(err)?),
|
||||
None => {
|
||||
use image::ImageDecoder as _;
|
||||
let mut decoder = reader.into_decoder().map_err(err)?;
|
||||
// The allowance counts the decoded samples and the RGBA pixels they become (#1717).
|
||||
let (w, h) = decoder.dimensions();
|
||||
if decoder.total_bytes() > MAX_RASTER_ALLOC || u64::from(w) * u64::from(h) * 4 > MAX_RASTER_ALLOC {
|
||||
// A Targa file's size is read from its header, which no signature points a probe at.
|
||||
let tga = || (kind == craft_image_codecs::Format::Tga).then(|| super::tgaread::size(bytes)).flatten().map(|(w, h)| (w, h, 4));
|
||||
let (w, h, per_pixel) =
|
||||
vectorcraft_doc::decode::decoded_layout(bytes, kind).or_else(tga).ok_or_else(|| err("the image's size can't be read"))?;
|
||||
let pixels = u64::from(w) * u64::from(h);
|
||||
if pixels.saturating_mul(per_pixel) > MAX_RASTER_ALLOC || pixels * 4 > MAX_RASTER_ALLOC {
|
||||
return Err(err(format!("an image of {w} × {h} pixels is too large to draw")));
|
||||
}
|
||||
// A profile the decoder can't read leaves the image without one. The image decoder
|
||||
// reads a TIFF's tags within the size of its pixels, so a small TIFF's profile is
|
||||
// read by the TIFF decoder itself.
|
||||
let (img, profile) = vectorcraft_doc::decode::decode_image(kind, bytes, MAX_RASTER_ALLOC).map_err(err)?;
|
||||
// A profile the decoder can't read leaves the image without one. A TIFF's profile is
|
||||
// read by the TIFF reader, as before.
|
||||
icc = match f.id {
|
||||
"tiff" => vectorcraft_doc::cmyk::tiff_icc_profile(bytes),
|
||||
_ => decoder.icc_profile().ok().flatten(),
|
||||
_ => profile,
|
||||
};
|
||||
image::DynamicImage::from_decoder(decoder).map_err(err)?
|
||||
img
|
||||
}
|
||||
},
|
||||
};
|
||||
@@ -1109,11 +1110,8 @@ pub fn texture_image(bytes: &[u8]) -> Result<String> {
|
||||
LoadedTexture::from_rgba(inks.width, inks.height, &rgba)
|
||||
}
|
||||
None => {
|
||||
let mut reader = image::ImageReader::new(Cursor::new(blob.bytes.as_slice())).with_guessed_format().map_err(err)?;
|
||||
let mut limits = image::Limits::default();
|
||||
limits.max_alloc = Some(MAX_RASTER_ALLOC);
|
||||
reader.limits(limits);
|
||||
let img = reader.decode().map_err(err)?.to_rgba8();
|
||||
let kind = craft_image_codecs::detect(&blob.bytes).ok_or_else(|| err("the image can't be read as a texture"))?;
|
||||
let img = vectorcraft_doc::decode::decode_image(kind, &blob.bytes, MAX_RASTER_ALLOC).map_err(err)?.0.to_rgba8();
|
||||
LoadedTexture::from_rgba(img.width(), img.height(), img.as_raw())
|
||||
}
|
||||
};
|
||||
|
||||
@@ -39,7 +39,23 @@ mod tests {
|
||||
let out = with_text(png.clone(), &[("Title", "Café".into()), ("Author", "Łukasz".into()), ("bad\u{1}", "x".into())]);
|
||||
assert_eq!(text(&out), [("Title".to_string(), "Café".to_string()), ("Author".into(), "Łukasz".into())]);
|
||||
// Still a PNG every decoder reads (the CRCs check out).
|
||||
let img = image::load_from_memory(&out).unwrap();
|
||||
let img = craft_image_codecs::decode(&out)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!((img.width(), img.height()), (2, 1));
|
||||
assert_eq!(with_text(b"not a png".to_vec(), &[("Title", "x".into())]), b"not a png");
|
||||
assert_eq!(with_text(png.clone(), &[]), png);
|
||||
|
||||
@@ -114,7 +114,41 @@ mod tests {
|
||||
|
||||
fn png_of(w: u32, h: u32) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
image::RgbaImage::new(w, h).write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::new(w, h)).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -131,14 +165,64 @@ mod tests {
|
||||
let again = with_png_resolution(&tagged, (72.0, 72.0));
|
||||
assert!(close(resolution(&again).unwrap(), (72.0, 72.0)), "replaced, not duplicated");
|
||||
assert_eq!(again.len(), tagged.len());
|
||||
let img = image::load_from_memory(&again).unwrap();
|
||||
let img = craft_image_codecs::decode(&again)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!((img.width(), img.height()), (4, 3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn jpeg_jfif_density_in_dpi_and_dpcm() {
|
||||
let mut jpg = Vec::new();
|
||||
image::RgbImage::new(2, 2).write_to(&mut std::io::Cursor::new(&mut jpg), image::ImageFormat::Jpeg).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbImage::new(2, 2)).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Jpeg, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| jpg.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
// The encoder writes JFIF with units 0 (aspect only).
|
||||
assert_eq!(resolution(&jpg), None);
|
||||
let jfif = jpg.windows(5).position(|w| w == b"JFIF\0").unwrap();
|
||||
@@ -202,7 +286,41 @@ mod tests {
|
||||
#[test]
|
||||
fn bmp_pixels_per_metre() {
|
||||
let mut bmp = Vec::new();
|
||||
image::RgbImage::new(2, 2).write_to(&mut std::io::Cursor::new(&mut bmp), image::ImageFormat::Bmp).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbImage::new(2, 2)).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Bmp, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| bmp.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
bmp[38..42].copy_from_slice(&11811u32.to_le_bytes());
|
||||
bmp[42..46].copy_from_slice(&11811u32.to_le_bytes());
|
||||
assert!(close(resolution(&bmp).unwrap(), (300.0, 300.0)));
|
||||
|
||||
@@ -4,12 +4,45 @@
|
||||
use super::*;
|
||||
use proptest::prelude::*;
|
||||
use serde_json::json;
|
||||
use std::io::Cursor;
|
||||
|
||||
fn png() -> Vec<u8> {
|
||||
let im = image::RgbaImage::from_fn(2, 1, |x, _| if x == 0 { image::Rgba([220, 40, 60, 255]) } else { image::Rgba([0, 0, 0, 0]) });
|
||||
let mut bytes = Vec::new();
|
||||
im.write_to(&mut Cursor::new(&mut bytes), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = im.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| bytes.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
bytes
|
||||
}
|
||||
|
||||
|
||||
@@ -571,7 +571,24 @@ fn a_one_bit_rgb_image_opens_from_the_editing_data_in_its_colors() {
|
||||
assert_eq!(names(&doc.layers), ["Art"], "{r}");
|
||||
let mut found = vec![];
|
||||
doc.visit_images(|_, im| {
|
||||
let img = image::load_from_memory(&doc.images[&im.key].bytes).unwrap().to_rgb8();
|
||||
let img = craft_image_codecs::decode(&doc.images[&im.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgb8();
|
||||
found.push(img.pixels().map(|p| p.0).collect::<Vec<_>>());
|
||||
});
|
||||
assert_eq!(found, [[[255, 0, 0], [0, 255, 0], [0, 0, 255]]], "{r}");
|
||||
@@ -589,7 +606,27 @@ fn a_zero_width_stroke_in_the_editing_data_draws_a_hairline() {
|
||||
assert_eq!(names(&s.doc().unwrap().doc.layers), ["Art"], "{r}");
|
||||
let v = s.execute("document.export", &json!({"format": "png", "background": "transparent"})).unwrap();
|
||||
let png = vectorcraft_format::base64_decode(v["dataBase64"].as_str().unwrap()).unwrap();
|
||||
image::load_from_memory(&png).unwrap().to_rgba8().pixels().filter(|p| p.0[3] > 0).count()
|
||||
craft_image_codecs::decode(&png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.pixels()
|
||||
.filter(|p| p.0[3] > 0)
|
||||
.count()
|
||||
};
|
||||
assert!(drawn("0 1 1 0 1 0 0 XA\n0 w\n20 20 m\n180 80 L\nS\n") > 0, "the hairline draws");
|
||||
assert_eq!(drawn("0 1 1 0 1 0 0 XA\n0 w\n20 20 m\n180 80 L\nN\n"), 0, "the unstroked path draws nothing");
|
||||
|
||||
@@ -32,7 +32,24 @@ fn with(mut p: Value, more: &Value) -> Value {
|
||||
/// The pixels of the PNG `document.export` writes of the active document with `p`, on transparency.
|
||||
fn png(s: &mut Session, p: Value) -> image::RgbaImage {
|
||||
let v = s.execute("document.export", &with(json!({"format": "png", "background": "transparent"}), &p)).unwrap();
|
||||
image::load_from_memory(&b64(&v)).unwrap().to_rgba8()
|
||||
craft_image_codecs::decode(&b64(&v))
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
}
|
||||
|
||||
/// The file `document.export` writes with `p`.
|
||||
@@ -61,7 +78,26 @@ fn raster_exports_paint_the_color_behind_the_art() {
|
||||
assert_eq!(png(&mut s, json!({"artboard": 0, "includeArtboardFill": false})).get_pixel(2, 2)[3], 0, "left out");
|
||||
assert_eq!(png(&mut s, json!({"artboard": 1})).get_pixel(2, 2)[3], 0, "an artboard without a color");
|
||||
let jpg = file(&mut s, json!({"format": "jpg", "artboard": 0, "quality": 100}));
|
||||
let px = image::load_from_memory(&jpg).unwrap().to_rgb8().get_pixel(2, 2).0;
|
||||
let px = craft_image_codecs::decode(&jpg)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgb8()
|
||||
.get_pixel(2, 2)
|
||||
.0;
|
||||
assert!(px[0] > 240 && px[1] < 16 && px[2] < 16, "JPEG too: {px:?}");
|
||||
for bad in [json!("no"), json!(1)] {
|
||||
assert!(s.execute("document.export", &json!({"format": "png", "includeArtboardFill": bad})).is_err(), "{bad}");
|
||||
@@ -163,7 +199,28 @@ fn export_for_screens_includes_the_fill_unless_unchecked() {
|
||||
let p = with(json!({"artboards": [0], "formats": [{"format": "png"}], "settings": {"png": {"background": "transparent"}}}), &more);
|
||||
s.execute("document.exportForScreens", &p).unwrap()
|
||||
};
|
||||
let corner = |r: &Value| image::load_from_memory(&b64(&r["files"][0])).unwrap().to_rgba8().get_pixel(2, 2).0;
|
||||
let corner = |r: &Value| {
|
||||
craft_image_codecs::decode(&b64(&r["files"][0]))
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.get_pixel(2, 2)
|
||||
.0
|
||||
};
|
||||
assert_eq!(corner(&screens(&mut s, json!({}))), RED, "on by default");
|
||||
assert_eq!(corner(&screens(&mut s, json!({"includeArtboardFill": false})))[3], 0, "unchecked");
|
||||
assert_eq!(s.execute("document.exportSettings", &json!({})).unwrap()["settings"]["includeArtboardFill"], false, "remembered");
|
||||
|
||||
@@ -50,7 +50,23 @@ fn eps_covers_the_art_y_up_from_the_first_artboard_with_a_preview_and_the_docume
|
||||
let bytes = b64(&r);
|
||||
// The default TIFF preview sits behind the binary header.
|
||||
let (_, tiff) = vectorcraft_eps::sections(&bytes).unwrap();
|
||||
let preview = image::load_from_memory_with_format(tiff.unwrap(), image::ImageFormat::Tiff).unwrap();
|
||||
let preview = craft_image_codecs::decode_as(craft_image_codecs::Format::Tiff, tiff.unwrap())
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!((preview.width(), preview.height()), (100, 40));
|
||||
let ps = postscript(&bytes);
|
||||
assert_eq!(dsc(&ps, "BoundingBox").as_deref(), Some("50 100 150 140"), "60..100 down is 100..140 up");
|
||||
@@ -221,13 +237,47 @@ fn placed_images_are_written_and_previews_are_black_and_white_on_request() {
|
||||
/// The PNG thumbnail of an EPS file, decoded.
|
||||
fn thumbnail_png(bytes: &[u8]) -> image::RgbaImage {
|
||||
let png = vectorcraft_eps::thumbnail(bytes).expect("a thumbnail");
|
||||
image::load_from_memory_with_format(&png, image::ImageFormat::Png).unwrap().to_rgba8()
|
||||
craft_image_codecs::decode_as(craft_image_codecs::Format::Png, &png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
}
|
||||
|
||||
/// The TIFF preview of an EPS file, decoded.
|
||||
fn preview_tiff(bytes: &[u8]) -> image::RgbaImage {
|
||||
let tiff = vectorcraft_eps::sections(bytes).unwrap().1.expect("a preview");
|
||||
image::load_from_memory_with_format(tiff, image::ImageFormat::Tiff).unwrap().to_rgba8()
|
||||
craft_image_codecs::decode_as(craft_image_codecs::Format::Tiff, tiff)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
}
|
||||
|
||||
/// The size of a thumbnail render of the rect an EPS file's preview shows (its bounding box).
|
||||
|
||||
@@ -115,7 +115,25 @@ fn tiff_predicted_4_bit_images_open_from_eps_and_postscript_ai_files() {
|
||||
let mut grays = vec![];
|
||||
d.walk(|n| {
|
||||
if let vectorcraft_doc::NodeKind::Image(im) = &n.kind {
|
||||
grays = image::load_from_memory(&d.images[&im.key].bytes).unwrap().to_luma8().into_raw();
|
||||
grays = craft_image_codecs::decode(&d.images[&im.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_luma8()
|
||||
.into_raw();
|
||||
}
|
||||
});
|
||||
assert_eq!(grays, [0, 5, 15, 3, 9, 9, 2, 0].map(|v| v * 17), "{name}");
|
||||
|
||||
@@ -16,7 +16,24 @@ fn b64(v: &Value) -> Vec<u8> {
|
||||
}
|
||||
|
||||
fn rgba(bytes: &[u8]) -> image::RgbaImage {
|
||||
image::load_from_memory(bytes).unwrap().to_rgba8()
|
||||
craft_image_codecs::decode(bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
}
|
||||
|
||||
fn tmp_dir(tag: &str) -> std::path::PathBuf {
|
||||
|
||||
@@ -12,7 +12,27 @@ const COLORS: [[u8; 4]; 4] = [[255, 0, 0, 255], [0, 255, 0, 255], [0, 0, 255, 25
|
||||
|
||||
fn pixels(bytes: &[u8]) -> Vec<[u8; 4]> {
|
||||
let raster = load::raster_image(bytes).unwrap();
|
||||
image::load_from_memory(&raster.blob.bytes).unwrap().to_rgba8().pixels().map(|p| p.0).collect()
|
||||
craft_image_codecs::decode(&raster.blob.bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.pixels()
|
||||
.map(|p| p.0)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn boxed(ty: &[u8; 4], data: &[u8]) -> Vec<u8> {
|
||||
@@ -54,12 +74,58 @@ fn checker_opens_and_places_with_exact_pixels() {
|
||||
assert_eq!(opened["format"], "jpeg2000");
|
||||
assert_eq!(s.doc().unwrap().doc.artboards[0].rect.size(), vectorcraft_geom::Size::new(2.0, 2.0));
|
||||
let blob = s.doc().unwrap().doc.images.values().next().unwrap();
|
||||
assert_eq!(image::load_from_memory(&blob.bytes).unwrap().to_rgba8().pixels().map(|p| p.0).collect::<Vec<_>>(), COLORS);
|
||||
assert_eq!(
|
||||
craft_image_codecs::decode(&blob.bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.pixels()
|
||||
.map(|p| p.0)
|
||||
.collect::<Vec<_>>(),
|
||||
COLORS
|
||||
);
|
||||
s.execute("file.new", &json!({"width": 100, "height": 100})).unwrap();
|
||||
let placed = s.execute("file.place", &json!({"name": name, "dataBase64": encoded})).unwrap();
|
||||
assert_eq!(placed["format"], "jpeg2000");
|
||||
let blob = s.doc().unwrap().doc.images.values().next().unwrap();
|
||||
assert_eq!(image::load_from_memory(&blob.bytes).unwrap().to_rgba8().pixels().map(|p| p.0).collect::<Vec<_>>(), COLORS);
|
||||
assert_eq!(
|
||||
craft_image_codecs::decode(&blob.bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.pixels()
|
||||
.map(|p| p.0)
|
||||
.collect::<Vec<_>>(),
|
||||
COLORS
|
||||
);
|
||||
assert!(load::texture_image(bytes).is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,14 +33,48 @@ fn png8_and_gif_export_palettes() {
|
||||
let png8 = b64(&s.execute("document.export", &json!({"format": "png8", "colors": 16})).unwrap());
|
||||
assert_eq!(png8[25], 3, "colour type 3");
|
||||
assert!(plte_len(&png8) <= 16);
|
||||
let img = image::load_from_memory(&png8).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&png8)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(img.dimensions(), (40, 30));
|
||||
assert_eq!(img.get_pixel(5, 15).0, [255, 0, 0, 255]);
|
||||
assert_eq!(img.get_pixel(39, 0)[3], 0, "transparent where there is no art");
|
||||
|
||||
let gif = b64(&s.execute("document.export", &json!({"format": "gif", "background": "white", "dither": "none"})).unwrap());
|
||||
assert_eq!(&gif[..6], b"GIF89a");
|
||||
let img = image::load_from_memory_with_format(&gif, image::ImageFormat::Gif).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode_as(craft_image_codecs::Format::Gif, &gif)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(img.get_pixel(5, 15).0, [255, 0, 0, 255]);
|
||||
assert_eq!(img.get_pixel(39, 0).0, [255, 255, 255, 255], "the white background");
|
||||
|
||||
@@ -68,5 +102,25 @@ fn palette_formats_reach_export_as_and_screens() {
|
||||
let names: Vec<&str> = r["files"].as_array().unwrap().iter().map(|f| f["name"].as_str().unwrap()).collect();
|
||||
assert_eq!(names, ["Artboard-1@2x.gif", "Artboard-1.png"]);
|
||||
let gif = vectorcraft_format::base64_decode(r["files"][0]["dataBase64"].as_str().unwrap()).unwrap();
|
||||
assert_eq!(image::load_from_memory(&gif).unwrap().width(), 80);
|
||||
assert_eq!(
|
||||
craft_image_codecs::decode(&gif)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.width(),
|
||||
80
|
||||
);
|
||||
}
|
||||
@@ -49,7 +49,27 @@ fn rle(raw: &[u8]) -> Vec<u8> {
|
||||
/// The pixels `raster_image` stores for `f`, row by row.
|
||||
fn pixels(f: &[u8]) -> Vec<[u8; 4]> {
|
||||
let img = raster_image(f).unwrap();
|
||||
image::load_from_memory(&img.blob.bytes).unwrap().to_rgba8().pixels().map(|p| p.0).collect()
|
||||
craft_image_codecs::decode(&img.blob.bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.pixels()
|
||||
.map(|p| p.0)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The 16 colors of a header palette: entry `i` is (16 i, 255 − 16 i, i).
|
||||
|
||||
@@ -375,7 +375,41 @@ fn a_16_bit_png_is_written_at_8_bits_before_pdf_1_5() {
|
||||
let mut s = session(1);
|
||||
let deep = image::ImageBuffer::from_fn(8, 8, |x, y| image::Rgb([x as u16 * 8000, y as u16 * 8000, 40000]));
|
||||
let mut png = vec![];
|
||||
image::DynamicImage::ImageRgb16(deep).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = image::DynamicImage::ImageRgb16(deep);
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.execute("file.place", &json!({"name": "deep.png", "dataBase64": vectorcraft_format::base64_encode(&png), "at": [50, 40]})).unwrap();
|
||||
let readable = json!({"compressText": false});
|
||||
for (cmd, p, depth) in [
|
||||
|
||||
@@ -26,9 +26,41 @@ fn session() -> Session {
|
||||
run(&mut s, "paint.setFill", json!({"color": "#e0c020"}));
|
||||
run(&mut s, "effect.apply", json!({"effect": "stylize.dropShadow", "ids": [shadowed]}));
|
||||
let mut png = vec![];
|
||||
image::RgbaImage::from_fn(8, 8, |x, _| image::Rgba([0, 120, 255, if x < 4 { 0 } else { 255 }]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_fn(8, 8, |x, _| image::Rgba([0, 120, 255, if x < 4 { 0 } else { 255 }]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
run(&mut s, "file.place", json!({"name": "dots.png", "dataBase64": vectorcraft_format::base64_encode(&png), "at": [150, 120]}));
|
||||
s
|
||||
}
|
||||
|
||||
@@ -807,7 +807,28 @@ fn image_outline(doc: &vectorcraft_doc::Document) -> Vec<String> {
|
||||
((p.x * 100.0).round() / 100.0, (p.y * 100.0).round() / 100.0)
|
||||
});
|
||||
let link = im.link.as_ref().map(|l| (l.path.clone(), l.relative.clone()));
|
||||
let pixels = doc.image_blob(im).and_then(|b| image::load_from_memory(&b.bytes).ok()).map(|i| i.to_rgba8().into_raw());
|
||||
let pixels = doc
|
||||
.image_blob(im)
|
||||
.and_then(|b| {
|
||||
craft_image_codecs::decode(&b.bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.ok()
|
||||
})
|
||||
.map(|i| i.to_rgba8().into_raw());
|
||||
out.push(format!("{}x{} {corners:?} {link:?} {pixels:?}", im.width, im.height));
|
||||
}
|
||||
});
|
||||
@@ -825,7 +846,41 @@ fn images_come_back_from_the_editing_copy() {
|
||||
std::fs::create_dir_all(format!("{dir}/art")).unwrap();
|
||||
let png = |w: u32, h: u32, f: &dyn Fn(u32, u32) -> [u8; 4]| {
|
||||
let mut out = vec![];
|
||||
image::RgbaImage::from_fn(w, h, |x, y| image::Rgba(f(x, y))).write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_fn(w, h, |x, y| image::Rgba(f(x, y)))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
};
|
||||
let linked = format!("{dir}/art/linked.png");
|
||||
|
||||
@@ -97,7 +97,23 @@ fn pdf_info_lists_pages_boxes_and_a_thumbnail() {
|
||||
assert_eq!(p2["boxes"]["trim"], json!([15.0, 15.0, 185.0, 385.0]));
|
||||
assert_eq!(p2["boxes"]["media"], json!([0.0, 0.0, 200.0, 400.0]));
|
||||
let png = vectorcraft_format::base64_decode(v["thumbnail"].as_str().unwrap()).unwrap();
|
||||
let img = image::load_from_memory(&png).unwrap();
|
||||
let img = craft_image_codecs::decode(&png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!((img.width(), img.height()), (50, 100), "the art box at 100 px on its longest side");
|
||||
assert!(s.execute("document.pdfInfo", &json!({"dataBase64": b64(&bytes), "thumbnail": 4})).is_err());
|
||||
// Locked: no pages until the password is given.
|
||||
@@ -214,7 +230,27 @@ fn pdf_info_refuses_a_thumbnail_size_that_is_not_a_number() {
|
||||
assert!(e.contains("`colorMode` must be a string"), "{e}");
|
||||
let v = s.execute("document.pdfInfo", &json!({"dataBase64": b64(&bytes), "thumbnail": 1, "thumbnailSize": null})).unwrap();
|
||||
let png = vectorcraft_format::base64_decode(v["thumbnail"].as_str().unwrap()).unwrap();
|
||||
assert_eq!(image::load_from_memory(&png).unwrap().width(), 160);
|
||||
assert_eq!(
|
||||
craft_image_codecs::decode(&png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.width(),
|
||||
160
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -284,7 +320,23 @@ fn pdf_info_reads_postscript_as_one_page() {
|
||||
] {
|
||||
let v = s.execute("document.pdfInfo", &json!({"dataBase64": b64(bytes), "thumbnail": 1, "thumbnailSize": 100})).unwrap();
|
||||
let png = vectorcraft_format::base64_decode(v["thumbnail"].as_str().unwrap()).unwrap();
|
||||
let img = image::load_from_memory(&png).unwrap();
|
||||
let img = craft_image_codecs::decode(&png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(img.width().max(img.height()), 100, "{name}");
|
||||
let mut v = v;
|
||||
v["thumbnail"] = json!("png");
|
||||
@@ -314,7 +366,23 @@ fn pdf_info_refuses_a_thumbnail_that_is_not_a_page_number() {
|
||||
}
|
||||
let v = s.execute("document.pdfInfo", &json!({"dataBase64": b64(bytes), "thumbnail": 1, "thumbnailSize": 64})).unwrap();
|
||||
let png = vectorcraft_format::base64_decode(v["thumbnail"].as_str().unwrap()).unwrap();
|
||||
let img = image::load_from_memory(&png).unwrap();
|
||||
let img = craft_image_codecs::decode(&png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(img.width().max(img.height()), 64, "{pages}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,9 +17,41 @@ fn session() -> Session {
|
||||
s.execute("paint.setFill", &json!({"color": {"model": "rgb", "r": 1.0, "g": 0.2, "b": 0.0}})).unwrap();
|
||||
s.execute("transparency.set", &json!({"opacity": 50})).unwrap();
|
||||
let mut png = vec![];
|
||||
image::RgbaImage::from_fn(8, 8, |x, _| image::Rgba([0, 120, 255, if x < 4 { 0 } else { 255 }]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_fn(8, 8, |x, _| image::Rgba([0, 120, 255, if x < 4 { 0 } else { 255 }]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.execute("file.place", &json!({"name": "dots.png", "dataBase64": vectorcraft_format::base64_encode(&png), "at": [150, 50]})).unwrap();
|
||||
s
|
||||
}
|
||||
@@ -175,9 +207,41 @@ fn pdfx_1a_flattening_keeps_the_inks_behind_an_image_in_a_cmyk_document() {
|
||||
s.execute("paint.setFill", &json!({"color": {"c": teal[0], "m": teal[1], "y": teal[2], "k": teal[3]}})).unwrap();
|
||||
s.execute("paint.setStroke", &json!({"none": true})).unwrap();
|
||||
let mut png = vec![];
|
||||
image::RgbaImage::from_fn(40, 40, |x, _| image::Rgba([0, 120, 255, if x < 20 { 0 } else { 255 }]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_fn(40, 40, |x, _| image::Rgba([0, 120, 255, if x < 20 { 0 } else { 255 }]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.execute("file.place", &json!({"name": "logos.png", "dataBase64": vectorcraft_format::base64_encode(&png), "at": [100, 50]})).unwrap();
|
||||
let v = s.execute("document.exportPdf", &json!({"standard": "pdfX1a"})).unwrap();
|
||||
let bytes = vectorcraft_format::base64_decode(v["dataBase64"].as_str().unwrap()).unwrap();
|
||||
@@ -228,10 +292,45 @@ fn cropped_by(mode: &str, width: f64, art: Art, layer: bool) -> (Session, u64) {
|
||||
let art = match art {
|
||||
Art::Image { see_through } => {
|
||||
let mut png = vec![];
|
||||
image::RgbaImage::from_fn(240, 160, |x, y| {
|
||||
image::Rgba([200, (x % 256) as u8, (y % 256) as u8, if see_through && x < 60 { 128 } else { 255 }])
|
||||
})
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_fn(240, 160, |x, y| {
|
||||
image::Rgba([200, (x % 256) as u8, (y % 256) as u8, if see_through && x < 60 { 128 } else { 255 }])
|
||||
}))
|
||||
.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => {
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw())
|
||||
}
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let placed =
|
||||
run(&mut s, "file.place", json!({"name": "photo.png", "dataBase64": vectorcraft_format::base64_encode(&png), "rect": FRAME}));
|
||||
@@ -486,9 +585,41 @@ fn flat_pdf_files_of_art_that_no_clip_group_hides_are_unchanged() {
|
||||
run(&mut fitted, "shape.rectangle", json!({"x": 180, "y": 90, "width": 160, "height": 160}));
|
||||
run(&mut fitted, "paint.setFill", json!({"color": "#203040"}));
|
||||
let mut png = vec![];
|
||||
image::RgbaImage::from_fn(240, 160, |x, y| image::Rgba([200, x as u8, y as u8, 255]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_fn(240, 160, |x, y| image::Rgba([200, x as u8, y as u8, 255]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let photo =
|
||||
run(&mut fitted, "file.place", json!({"name": "photo.png", "dataBase64": vectorcraft_format::base64_encode(&png), "rect": FRAME}))["ids"][0]
|
||||
.clone();
|
||||
|
||||
@@ -274,7 +274,24 @@ fn image_of(s: &Session, l: &Node) -> ((f64, f64), image::RgbaImage) {
|
||||
fn pixels_of(s: &Session, c: &Node) -> ((f64, f64), image::RgbaImage) {
|
||||
let NodeKind::Image(img) = &c.kind else { panic!("{} isn't an image", name(c)) };
|
||||
let blob = &s.doc().unwrap().doc.images[&img.key];
|
||||
let px = image::load_from_memory(&blob.bytes).unwrap().to_rgba8();
|
||||
let px = craft_image_codecs::decode(&blob.bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!((px.width(), px.height()), (img.width, img.height));
|
||||
let o = img.xf * kurbo::Point::ZERO;
|
||||
((o.x, o.y), px)
|
||||
|
||||
@@ -215,7 +215,24 @@ fn exported(s: &mut Session, p: Value) -> Vec<u8> {
|
||||
#[test]
|
||||
fn psd_exports_open_and_place_again() {
|
||||
let mut s = art();
|
||||
let png = image::load_from_memory(&exported(&mut s, json!({"format": "png"}))).unwrap().to_rgba8();
|
||||
let png = craft_image_codecs::decode(&exported(&mut s, json!({"format": "png"})))
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
for (options, what) in [(json!({}), "layered RGB"), (json!({"layers": false}), "flat RGB"), (json!({"colorModel": "gray"}), "grey")] {
|
||||
let f = exported(&mut s, merge(json!({"format": "psd"}), options));
|
||||
let back = decode(&f, 1 << 30, naive).unwrap();
|
||||
|
||||
@@ -72,7 +72,27 @@ fn wide(samples: &[u16]) -> Vec<u8> {
|
||||
/// The pixels `raster_image` stores for `f`, row by row.
|
||||
fn pixels(f: &[u8]) -> Vec<[u8; 4]> {
|
||||
let img = raster_image(f).unwrap();
|
||||
image::load_from_memory(&img.blob.bytes).unwrap().to_rgba8().pixels().map(|p| p.0).collect()
|
||||
craft_image_codecs::decode(&img.blob.bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.pixels()
|
||||
.map(|p| p.0)
|
||||
.collect()
|
||||
}
|
||||
|
||||
const RGBA: u16 = 15;
|
||||
@@ -241,13 +261,64 @@ fn pxr_files_open_place_and_paste() {
|
||||
let node = doc.node(vectorcraft_doc::NodeId(r["ids"][0].as_u64().unwrap())).unwrap();
|
||||
let vectorcraft_doc::NodeKind::Image(im) = &node.kind else { panic!("link {link}: an image, got {:?}", node.kind) };
|
||||
assert_eq!((im.link.is_some(), im.width, im.height), (link, 2, 1), "link {link}");
|
||||
let shown = image::load_from_memory(&doc.images[&im.key].bytes).unwrap().to_rgba8();
|
||||
let shown = craft_image_codecs::decode(&doc.images[&im.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(shown.pixels().map(|p| p.0).collect::<Vec<_>>(), pixels(&f), "link {link}");
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
// Open sizes it as a PNG of 2 × 1 pixels without a resolution.
|
||||
let mut png = vec![];
|
||||
image::RgbaImage::new(2, 1).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::new(2, 1)).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let mut t = Session::new();
|
||||
t.execute("document.open", &json!({"name": "same.png", "dataBase64": b64(&png)})).unwrap();
|
||||
assert_eq!(t.doc().unwrap().doc.artboards[0].rect.size(), size);
|
||||
|
||||
@@ -16,7 +16,24 @@ fn b64(v: &Value) -> Vec<u8> {
|
||||
}
|
||||
|
||||
fn rgba(bytes: &[u8]) -> image::RgbaImage {
|
||||
image::load_from_memory(bytes).unwrap().to_rgba8()
|
||||
craft_image_codecs::decode(bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
}
|
||||
|
||||
/// A chunk's data by type (first match) in a PNG file.
|
||||
@@ -427,3 +444,55 @@ fn export_for_screens_rows_keep_their_own_size() {
|
||||
.unwrap();
|
||||
assert_eq!(rgba(&b64(&r["files"][0])).dimensions(), (512, 256), "{r}");
|
||||
}
|
||||
|
||||
/// File › Open's raster path reads images through craft-image-codecs (#778) as it read them with
|
||||
/// the image crate: each format the image crate decoded for it (BMP, TGA, TIFF at 8 and 16 bits,
|
||||
/// gray and color, with and without alpha; a PNG, JPEG, GIF and WebP stored as they are) gives
|
||||
/// the same size, sample depth and pixels.
|
||||
#[test]
|
||||
fn opened_rasters_are_the_pixels_the_image_crate_read() {
|
||||
use image::{DynamicImage as D, ImageFormat as F};
|
||||
let rgba = image::RgbaImage::from_fn(11, 6, |x, y| image::Rgba([(x * 23) as u8, (y * 41) as u8, ((x ^ y) * 13) as u8, 255 - (x * y * 3) as u8]));
|
||||
let deep = image::ImageBuffer::<image::Rgba<u16>, _>::from_fn(11, 6, |x, y| {
|
||||
image::Rgba([(x * 5_003) as u16, (y * 9_011) as u16, 77, 65_535 - (x * y) as u16])
|
||||
});
|
||||
let images = [
|
||||
D::ImageRgba8(rgba.clone()),
|
||||
D::ImageRgb8(D::ImageRgba8(rgba.clone()).to_rgb8()),
|
||||
D::ImageLuma8(D::ImageRgba8(rgba.clone()).to_luma8()),
|
||||
D::ImageLumaA8(D::ImageRgba8(rgba).to_luma_alpha8()),
|
||||
D::ImageRgba16(deep.clone()),
|
||||
D::ImageRgb16(D::ImageRgba16(deep.clone()).to_rgb16()),
|
||||
D::ImageLuma16(D::ImageRgba16(deep).to_luma16()),
|
||||
];
|
||||
let mut checked = 0;
|
||||
for img in &images {
|
||||
for f in [F::Bmp, F::Tga, F::Tiff, F::Png, F::Jpeg, F::Gif, F::WebP] {
|
||||
let mut bytes = Vec::new();
|
||||
if img.write_to(&mut std::io::Cursor::new(&mut bytes), f).is_err() {
|
||||
continue;
|
||||
}
|
||||
let what = format!("{f:?} {:?}", img.color());
|
||||
let mut want = image::load_from_memory_with_format(&bytes, f).unwrap();
|
||||
let opened = super::load::raster_image(&bytes).unwrap_or_else(|e| panic!("{what}: {e}"));
|
||||
assert_eq!((opened.width, opened.height), (want.width(), want.height()), "{what}");
|
||||
if opened.blob.mime != "image/png" {
|
||||
// Stored as they are: the file's own bytes.
|
||||
assert_eq!(opened.blob.bytes.as_slice(), bytes.as_slice(), "{what}");
|
||||
checked += 1;
|
||||
continue;
|
||||
}
|
||||
if f == F::Tga {
|
||||
let mut px = want.to_rgba8();
|
||||
super::tgaread::fix_alpha(&bytes, &mut px);
|
||||
want = D::ImageRgba8(px);
|
||||
}
|
||||
let got = image::load_from_memory_with_format(&opened.blob.bytes, F::Png).unwrap();
|
||||
let deep_samples = |c: image::ColorType| c.bytes_per_pixel() > c.channel_count();
|
||||
assert_eq!(deep_samples(got.color()), deep_samples(want.color()), "{what}: {:?} {:?}", got.color(), want.color());
|
||||
assert_eq!(got.to_rgba16().as_raw(), want.to_rgba16().as_raw(), "{what}");
|
||||
checked += 1;
|
||||
}
|
||||
}
|
||||
assert!(checked >= 25, "{checked}");
|
||||
}
|
||||
@@ -34,7 +34,25 @@ fn names(r: &Value) -> Vec<String> {
|
||||
}
|
||||
|
||||
fn size(png: &[u8]) -> (u32, u32) {
|
||||
image::load_from_memory(png).unwrap().to_rgba8().dimensions()
|
||||
craft_image_codecs::decode(png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.dimensions()
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -413,7 +431,28 @@ fn rich_black_reaches_the_files() {
|
||||
s.execute("paint.setFill", &json!({"color": {"c": 0, "m": 0, "y": 0, "k": 1}})).unwrap();
|
||||
s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 40, "height": 30})).unwrap();
|
||||
let p = json!({"formats": [{"format": "png"}]});
|
||||
let px = |r: &Value| image::load_from_memory(&files(r)[0].1).unwrap().to_rgba8().get_pixel(10, 10).0;
|
||||
let px = |r: &Value| {
|
||||
craft_image_codecs::decode(&files(r)[0].1)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.get_pixel(10, 10)
|
||||
.0
|
||||
};
|
||||
let accurate = px(&export(&mut s, p.clone()));
|
||||
assert!(accurate[0] > 10, "accurate: dark grey, {accurate:?}");
|
||||
s.execute("prefs.set", &json!({"key": "blackOutput", "value": "rich"})).unwrap();
|
||||
@@ -589,7 +628,28 @@ fn png_8_rows_write_indexed_colour() {
|
||||
#[test]
|
||||
fn a_second_file_with_a_taken_name_is_numbered() {
|
||||
let mut s = session(1);
|
||||
let px = |png: &[u8]| image::load_from_memory(png).unwrap().to_rgba8().get_pixel(1, 1).0;
|
||||
let px = |png: &[u8]| {
|
||||
craft_image_codecs::decode(png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.get_pixel(1, 1)
|
||||
.0
|
||||
};
|
||||
let rows = json!([{"format": "png", "background": "black"}, {"format": "png", "background": "white"}, {"format": "png", "background": "black"}]);
|
||||
let r = export(&mut s, json!({ "formats": rows }));
|
||||
assert_eq!(names(&r), ["Artboard-1.png", "Artboard-1-2.png"]);
|
||||
@@ -873,7 +933,28 @@ fn kept_settings_leave_out_a_folder_outside_the_read_root() {
|
||||
#[test]
|
||||
fn format_settings_apply_to_their_rows_and_rows_win() {
|
||||
let mut s = session(1);
|
||||
let px = |png: &[u8]| image::load_from_memory(png).unwrap().to_rgba8().get_pixel(1, 1).0;
|
||||
let px = |png: &[u8]| {
|
||||
craft_image_codecs::decode(png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.get_pixel(1, 1)
|
||||
.0
|
||||
};
|
||||
let r = export(
|
||||
&mut s,
|
||||
json!({"settings": {"png": {"background": "black", "scale": 9, "artboard": 7}, "jpg": {"quality": 5}}, "formats": [{"format": "png"}, {"format": "png", "suffix": "-w", "background": "white"}, {"format": "jpg"}, {"format": "jpg", "quality": 100, "suffix": "-100"}]}),
|
||||
|
||||
@@ -43,13 +43,47 @@ fn rect_of(v: &Value) -> Rect {
|
||||
/// What the canvas draws of the whole document over `r`, at 1 px per point.
|
||||
fn canvas(s: &Session, r: Rect) -> image::RgbaImage {
|
||||
let img = vectorcraft_render::Renderer::new().render_region(&s.doc().unwrap().doc, r, 1.0, false);
|
||||
image::load_from_memory(&img.to_png().unwrap()).unwrap().to_rgba8()
|
||||
craft_image_codecs::decode(&img.to_png().unwrap())
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
}
|
||||
|
||||
/// The pixels of `png` that differ from `want` by more than 64 in a channel (anti-aliased edges
|
||||
/// differ by less), and every pixel when the sizes differ.
|
||||
fn differing(png: &[u8], want: &image::RgbaImage) -> usize {
|
||||
let got = image::load_from_memory(png).unwrap().to_rgba8();
|
||||
let got = craft_image_codecs::decode(png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
if got.dimensions() != want.dimensions() {
|
||||
return (want.width() * want.height()) as usize;
|
||||
}
|
||||
@@ -86,7 +120,41 @@ fn export_selection_of_a_clipped_object_keeps_its_clip() {
|
||||
let board = t.doc().unwrap().doc.artboards[0].rect;
|
||||
let reopened = canvas(&t, board);
|
||||
let mut png = vec![];
|
||||
reopened.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = reopened.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let off = differing(&png, &canvas(&s, Rect::new(150.0, 150.0, 350.0, 350.0)));
|
||||
assert!(off < 200, "the reopened SVG: {off} pixels differ from the canvas");
|
||||
}
|
||||
@@ -162,7 +230,24 @@ fn a_selected_clipping_path_and_a_blend_object_export_on_their_own() {
|
||||
// The clipping path alone: the clip group's other art stays out.
|
||||
run(&mut s, "select.set", json!({"ids": [circle]}));
|
||||
let r = run(&mut s, "document.exportSelection", json!({"format": "png", "scale": 1}));
|
||||
let img = image::load_from_memory(&b64(&r)).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&b64(&r))
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(img.dimensions(), (200, 200), "cropped to the clipping path");
|
||||
assert!(!img.pixels().any(|p| p[0] > 200 && p[1] < 80 && p[3] > 0), "the clipped square is left out");
|
||||
// An object of a blend goes in on its own, without the blend's steps.
|
||||
@@ -176,7 +261,24 @@ fn a_selected_clipping_path_and_a_blend_object_export_on_their_own() {
|
||||
let r = run(&mut s, "document.exportSelection", json!({"format": "png", "scale": 1}));
|
||||
let got = rect_of(&r["bounds"]);
|
||||
assert!(got.x1 < 90.0 && got.y1 < 90.0, "the blend object's own bounds: {got:?}");
|
||||
let img = image::load_from_memory(&b64(&r)).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&b64(&r))
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
let centre = img.get_pixel(img.width() / 2, img.height() / 2);
|
||||
assert!(centre[0] > 200 && centre[3] > 200, "the object is drawn: {centre:?}");
|
||||
}
|
||||
@@ -191,7 +293,25 @@ fn a_css_picture_of_a_clipped_object_is_clipped() {
|
||||
let r = run(&mut s, "css.export", json!({"ids": [star], "rasterize": true, "unnamed": true}));
|
||||
let image = r["images"].as_array().and_then(|i| i.first()).unwrap_or_else(|| panic!("no picture: {r}"));
|
||||
let png = b64(image);
|
||||
let size = image::load_from_memory(&png).unwrap().to_rgba8().dimensions();
|
||||
let size = craft_image_codecs::decode(&png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.dimensions();
|
||||
assert_eq!(size, (100, 100), "the picture is cropped to the clip");
|
||||
assert!(differing(&png, &canvas(&s, Rect::new(200.0, 200.0, 300.0, 300.0))) < 100, "the picture matches the canvas");
|
||||
}
|
||||
@@ -204,7 +324,24 @@ fn an_asset_on_a_hidden_layer_is_drawn_clipped() {
|
||||
let asset = run(&mut s, "assets.add", json!({"ids": [square]}))["assets"][0].as_u64().unwrap();
|
||||
run(&mut s, "layer.setProps", json!({"id": layer, "visible": false}));
|
||||
let r = run(&mut s, "assets.export", json!({"assets": [asset], "formats": [{"format": "png", "scale": 1}]}));
|
||||
let img = image::load_from_memory(&b64(&r["files"][0])).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&b64(&r["files"][0]))
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(img.dimensions(), (200, 200), "cropped to the clip");
|
||||
let (centre, corner) = (img.get_pixel(100, 100), img.get_pixel(3, 3));
|
||||
assert!(centre[0] > 200 && centre[3] > 200, "the square is drawn: {centre:?}");
|
||||
@@ -233,7 +370,27 @@ fn export_for_screens_and_assets_by_selection_keep_the_clip() {
|
||||
|
||||
/// The red pixels of `png`.
|
||||
fn red(png: &[u8]) -> usize {
|
||||
image::load_from_memory(png).unwrap().to_rgba8().pixels().filter(|p| p[0] > 200 && p[1] < 80 && p[3] > 200).count()
|
||||
craft_image_codecs::decode(png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.pixels()
|
||||
.filter(|p| p[0] > 200 && p[1] < 80 && p[3] > 200)
|
||||
.count()
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -248,7 +405,41 @@ fn a_selection_on_a_non_printing_layer_exports_to_pdf() {
|
||||
run(&mut t, "document.open", json!({"name": "sel.pdf", "dataBase64": vectorcraft_format::base64_encode(&pdf)}));
|
||||
let page = t.doc().unwrap().doc.artboards[0].rect;
|
||||
let mut png = vec![];
|
||||
canvas(&t, page).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (canvas(&t, page)).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
assert!(red(&png) > 20_000, "the PDF draws the square: {} red pixels", red(&png));
|
||||
assert!(differing(&png, &canvas(&s, Rect::new(150.0, 150.0, 350.0, 350.0))) < 300, "the PDF draws what the canvas draws");
|
||||
}
|
||||
|
||||
@@ -27,9 +27,41 @@ fn tmp_dir(tag: &str) -> std::path::PathBuf {
|
||||
/// A session whose document holds one embedded 2×2 PNG.
|
||||
pub(super) fn image_session() -> Session {
|
||||
let mut png = Vec::new();
|
||||
image::RgbaImage::from_pixel(2, 2, image::Rgba([10, 200, 30, 255]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(2, 2, image::Rgba([10, 200, 30, 255]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let mut s = Session::new();
|
||||
s.execute("document.open", &json!({"name": "dot.png", "dataBase64": vectorcraft_format::base64_encode(&png)})).unwrap();
|
||||
s
|
||||
@@ -120,7 +152,26 @@ fn linked_images_are_written_next_to_the_svg() {
|
||||
let path = dir.join("pic.svg").to_string_lossy().to_string();
|
||||
let r = s.execute("document.export", &json!({"path": path, "images": "link"})).unwrap();
|
||||
let linked = r["linked"][0].as_str().unwrap();
|
||||
assert!(image::load_from_memory(&std::fs::read(linked).unwrap()).is_ok(), "{linked}");
|
||||
assert!(
|
||||
craft_image_codecs::decode(&std::fs::read(linked).unwrap())
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.is_ok(),
|
||||
"{linked}"
|
||||
);
|
||||
let text = std::fs::read_to_string(&path).unwrap();
|
||||
let name = std::path::Path::new(linked).file_name().unwrap().to_string_lossy().to_string();
|
||||
assert!(text.contains(&format!("href=\"{name}\"")), "{text}");
|
||||
|
||||
@@ -32,9 +32,41 @@ impl Drop for Folder {
|
||||
|
||||
fn png(w: u32, h: u32) -> Vec<u8> {
|
||||
let mut out = vec![];
|
||||
image::RgbaImage::from_pixel(w, h, image::Rgba([230, 20, 20, 255]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(w, h, image::Rgba([230, 20, 20, 255]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
|
||||
@@ -65,7 +65,29 @@ fn webp_is_lossless_and_says_so_when_lossy_is_asked_for() {
|
||||
let r = s.execute("document.export", &json!({"format": "webp", "lossless": false, "quality": 60, "background": "black"})).unwrap();
|
||||
assert_eq!(&b64(&r)[12..16], b"VP8L");
|
||||
assert!(r["warnings"][0].as_str().unwrap().contains("lossless"), "{r}");
|
||||
assert_eq!(image::load_from_memory(&b64(&r)).unwrap().to_rgba8().get_pixel(29, 19).0, [0, 0, 0, 255]);
|
||||
assert_eq!(
|
||||
craft_image_codecs::decode(&b64(&r))
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.get_pixel(29, 19)
|
||||
.0,
|
||||
[0, 0, 0, 255]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -74,12 +96,51 @@ fn save_for_office_writes_a_png_at_the_resolution() {
|
||||
s.execute("file.new", &json!({"width": 612, "height": 792, "artboards": 2})).unwrap();
|
||||
let r = s.execute("document.exportForOffice", &json!({"ppi": 150})).unwrap();
|
||||
assert_eq!((r["width"].as_u64(), r["height"].as_u64()), (Some(1275), Some(1650)), "Letter at 150 ppi");
|
||||
let img = image::load_from_memory(&b64(&r)).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&b64(&r))
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(img.dimensions(), (1275, 1650));
|
||||
assert_eq!(img.get_pixel(0, 0).0, [255, 255, 255, 255], "on white");
|
||||
let r = s.execute("document.exportForOffice", &json!({"ppi": 72, "transparent": true, "artboard": 1})).unwrap();
|
||||
assert_eq!((r["width"].as_u64(), r["height"].as_u64()), (Some(612), Some(792)));
|
||||
assert_eq!(image::load_from_memory(&b64(&r)).unwrap().to_rgba8().get_pixel(0, 0)[3], 0, "transparent");
|
||||
assert_eq!(
|
||||
craft_image_codecs::decode(&b64(&r))
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.get_pixel(0, 0)[3],
|
||||
0,
|
||||
"transparent"
|
||||
);
|
||||
assert!(s.execute("document.exportForOffice", &json!({"artboard": 2})).is_err());
|
||||
assert!(s.execute("document.exportForOffice", &json!({"ppi": -3})).is_err());
|
||||
let spec = crate::cmd::find_command("document.exportForOffice").unwrap();
|
||||
|
||||
@@ -23,6 +23,15 @@ pub(super) fn is_targa(bytes: &[u8]) -> bool {
|
||||
&& descriptor & 0xc0 == 0
|
||||
}
|
||||
|
||||
/// A Targa file's size, from its header (there is no signature for a size probe to go by).
|
||||
pub(super) fn size(bytes: &[u8]) -> Option<(u32, u32)> {
|
||||
let le = |at: usize| Some(u32::from(u16::from_le_bytes(bytes.get(at..at + 2)?.try_into().ok()?)));
|
||||
if !is_targa(bytes) {
|
||||
return None;
|
||||
}
|
||||
Some((le(12)?, le(14)?))
|
||||
}
|
||||
|
||||
/// Give `img`, decoded from Targa file `bytes`, the transparency the file stores. Bits 0 to 3 of
|
||||
/// the image descriptor give the number of attribute bits per pixel, and the extension area's
|
||||
/// Attributes Type, when the file has one, says what they hold:
|
||||
|
||||
@@ -70,9 +70,7 @@ pub(crate) fn carry(from: &Node, mut to: Node) -> Node {
|
||||
/// The keys of `doc`'s images that have see-through pixels (an alpha channel that isn't opaque
|
||||
/// everywhere).
|
||||
pub(crate) fn see_through_images(doc: &Document) -> BTreeSet<String> {
|
||||
let see_through = |b: &ImageBlob| {
|
||||
b.mime != "image/jpeg" && image::load_from_memory(&b.bytes).is_ok_and(|i| i.color().has_alpha() && i.to_rgba8().pixels().any(|p| p[3] < 255))
|
||||
};
|
||||
let see_through = |b: &ImageBlob| b.mime != "image/jpeg" && b.has_transparency();
|
||||
doc.images.iter().filter(|(_, b)| see_through(b)).map(|(k, _)| k.clone()).collect()
|
||||
}
|
||||
|
||||
|
||||
@@ -593,8 +593,8 @@ pub(crate) fn store_image(d: &mut Document, key: &str, full: ImageBlob, linked:
|
||||
/// Whether `bytes` are a PNG, JPEG, GIF or WebP file: the formats [`fileio::raster_image`] keeps
|
||||
/// as they are after reading only their header.
|
||||
pub(crate) fn kept_unread(bytes: &[u8]) -> bool {
|
||||
use image::ImageFormat as F;
|
||||
matches!(image::guess_format(bytes), Ok(F::Png | F::Jpeg | F::Gif | F::WebP))
|
||||
use craft_image_codecs::Format as F;
|
||||
matches!(craft_image_codecs::detect(bytes), Some(F::Png | F::Jpeg | F::Gif | F::WebP))
|
||||
}
|
||||
|
||||
/// `blob`, an image [`fileio::raster_image`] read from a file, ready to keep. When `unread` (the
|
||||
@@ -2639,12 +2639,34 @@ const UNEMBED_EXTS: &[&str] = &["png", "jpg", "jpeg", "gif", "webp", "tif", "tif
|
||||
|
||||
/// Encoded image `bytes` re-encoded in the format of extension `ext` (JPEG without alpha).
|
||||
fn convert_image(bytes: &[u8], ext: &str) -> std::result::Result<Vec<u8>, String> {
|
||||
let format = image::ImageFormat::from_extension(ext).ok_or_else(|| format!("can't write .{ext} images"))?;
|
||||
let img = image::load_from_memory(bytes).map_err(|e| e.to_string())?;
|
||||
let img = if format == image::ImageFormat::Jpeg { image::DynamicImage::ImageRgb8(img.to_rgb8()) } else { img };
|
||||
let mut out = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut out), format).map_err(|e| e.to_string())?;
|
||||
Ok(out)
|
||||
let format = match ext.to_ascii_lowercase().as_str() {
|
||||
"png" => craft_image_codecs::Format::Png,
|
||||
"jpg" | "jpeg" => craft_image_codecs::Format::Jpeg,
|
||||
"gif" => craft_image_codecs::Format::Gif,
|
||||
"webp" => craft_image_codecs::Format::WebP,
|
||||
"tif" | "tiff" => craft_image_codecs::Format::Tiff,
|
||||
"bmp" => craft_image_codecs::Format::Bmp,
|
||||
_ => return Err(format!("can't write .{ext} images")),
|
||||
};
|
||||
let mut opts = craft_image_codecs::DecodeOptions::default();
|
||||
opts.limits.max_alloc = vectorcraft_doc::MAX_RASTER_ALLOC;
|
||||
opts.limits.max_width = u32::MAX;
|
||||
opts.limits.max_height = u32::MAX;
|
||||
let img = craft_image_codecs::decode_with(bytes, &opts).map_err(|e| e.to_string())?;
|
||||
let raw = if format == craft_image_codecs::Format::Jpeg {
|
||||
let rgb: Vec<u8> = img.to_rgba8().as_chunks::<4>().0.iter().flat_map(|p| [p[0], p[1], p[2]]).collect();
|
||||
craft_image_codecs::Image::from_raw(
|
||||
img.width(),
|
||||
img.height(),
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U8,
|
||||
rgb,
|
||||
)
|
||||
.map_err(|e| e.to_string())?
|
||||
} else {
|
||||
img
|
||||
};
|
||||
craft_image_codecs::encode(&raw, format, &craft_image_codecs::EncodeOptions::default()).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
fn unembed(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
@@ -719,10 +719,16 @@ fn cmyk_raster(
|
||||
if alpha.iter().all(|&a| a == 255) {
|
||||
return Ok((blob, None));
|
||||
}
|
||||
let mask = image::GrayImage::from_raw(img.width, img.height, alpha)
|
||||
.ok_or_else(|| EngineError::Other("the image's transparency doesn't fit it".into()))?;
|
||||
let mut png = Vec::new();
|
||||
mask.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).map_err(|e| EngineError::Other(e.to_string()))?;
|
||||
let mask = craft_image_codecs::Image::from_raw(
|
||||
img.width,
|
||||
img.height,
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U8,
|
||||
alpha,
|
||||
)
|
||||
.map_err(|_| EngineError::Other("the image's transparency doesn't fit it".into()))?;
|
||||
let png = craft_image_codecs::encode(&mask, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map_err(|e| EngineError::Other(e.to_string()))?;
|
||||
Ok((blob, Some(png)))
|
||||
}
|
||||
|
||||
@@ -1072,9 +1078,16 @@ pub(crate) fn check_resample_size(sw: u32, sh: u32, w: u32, h: u32) -> Result<()
|
||||
|
||||
/// `img` as PNG bytes.
|
||||
fn encode_png(img: &image::RgbaImage) -> Result<Vec<u8>> {
|
||||
let mut png = Vec::new();
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).map_err(|e| bad("object.cropImage", e.to_string()))?;
|
||||
Ok(png)
|
||||
let fail = |e: craft_image_codecs::CodecError| bad("object.cropImage", e.to_string());
|
||||
let raw = craft_image_codecs::Image::from_raw(
|
||||
img.width(),
|
||||
img.height(),
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U8,
|
||||
img.as_raw().clone(),
|
||||
)
|
||||
.map_err(fail)?;
|
||||
craft_image_codecs::encode(&raw, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).map_err(fail)
|
||||
}
|
||||
|
||||
fn crop_image(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
@@ -1221,22 +1234,24 @@ fn crop_image(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
/// The image's pixels as RGBA, held to the allowance opening a raster has.
|
||||
fn decode_rgba(c: &str, blob: &vectorcraft_doc::ImageBlob) -> Result<image::RgbaImage> {
|
||||
// Print rasters run to gigabytes decoded: the same allowance opening them has.
|
||||
let reader = || {
|
||||
image::ImageReader::new(std::io::Cursor::new(blob.bytes.as_slice()))
|
||||
.with_guessed_format()
|
||||
.map_err(|_| bad(c, "the image's pixels can't be read"))
|
||||
};
|
||||
// The image's size as RGBA, read from its header, is held to that allowance before anything
|
||||
// is decoded (#1717).
|
||||
let (w, h) = reader()?.into_dimensions().map_err(|e| bad(c, format!("the image's pixels can't be read: {e}")))?;
|
||||
if u64::from(w) * u64::from(h) > crate::cmd::fileio::MAX_RASTER_ALLOC / 4 {
|
||||
let header = vectorcraft_doc::header_size(&blob.bytes);
|
||||
if let Some((w, h)) = header
|
||||
&& u64::from(w) * u64::from(h) > crate::cmd::fileio::MAX_RASTER_ALLOC / 4
|
||||
{
|
||||
return Err(bad(c, format!("an image of {w} × {h} pixels is too large to draw")));
|
||||
}
|
||||
let mut reader = reader()?;
|
||||
let mut limits = image::Limits::default();
|
||||
limits.max_alloc = Some(crate::cmd::fileio::MAX_RASTER_ALLOC);
|
||||
reader.limits(limits);
|
||||
Ok(reader.decode().map_err(|e| bad(c, format!("the image's pixels can't be read: {e}")))?.to_rgba8())
|
||||
let mut opts = craft_image_codecs::DecodeOptions::default();
|
||||
opts.limits.max_pixels = crate::cmd::fileio::MAX_RASTER_ALLOC / 4;
|
||||
opts.limits.max_alloc = crate::cmd::fileio::MAX_RASTER_ALLOC;
|
||||
let img = craft_image_codecs::decode_with(&blob.bytes, &opts).map_err(|e| match e {
|
||||
// Within the allowance from its header: a file that can't hold the pixels it declares.
|
||||
craft_image_codecs::CodecError::LimitExceeded(_) if header.is_none() => bad(c, "the image is too large to draw"),
|
||||
_ => bad(c, format!("the image's pixels can't be read: {e}")),
|
||||
})?;
|
||||
let (w, h) = img.dimensions();
|
||||
image::RgbaImage::from_raw(w, h, img.to_rgba8()).ok_or_else(|| bad(c, "the image's pixels can't be read"))
|
||||
}
|
||||
|
||||
/// Crop Image's trim: the image cut to the box of its pixels that aren't fully transparent, the
|
||||
@@ -1265,9 +1280,12 @@ fn trim_image(s: &mut Session, id: NodeId, im: ImageObject) -> Result<Value> {
|
||||
return Ok(json!({ "id": id.0, "width": w, "height": h, "trimmed": false }));
|
||||
}
|
||||
let cut = image::imageops::crop_imm(&px, x0, y0, x1 - x0, y1 - y0).to_image();
|
||||
let mut png = Vec::new();
|
||||
cut.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).map_err(|e| bad(C, e.to_string()))?;
|
||||
let (cw, ch) = (x1 - x0, y1 - y0);
|
||||
let raw =
|
||||
craft_image_codecs::Image::from_raw(cw, ch, craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, cut.into_raw())
|
||||
.map_err(|e| bad(C, e.to_string()))?;
|
||||
let png = craft_image_codecs::encode(&raw, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map_err(|e| bad(C, e.to_string()))?;
|
||||
put_pixels(s, id, png, cw, ch, im.xf * Affine::translate((f64::from(x0), f64::from(y0))))?;
|
||||
Ok(json!({ "id": id.0, "width": cw, "height": ch, "trimmed": true }))
|
||||
}
|
||||
|
||||
@@ -1070,15 +1070,9 @@ fn queue(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
/// The colour mode an image file stores its pixels in.
|
||||
fn color_mode(bytes: &[u8]) -> &'static str {
|
||||
use image::{ExtendedColorType as E, ImageDecoder as _};
|
||||
let kind = image::ImageReader::new(std::io::Cursor::new(bytes))
|
||||
.with_guessed_format()
|
||||
.ok()
|
||||
.and_then(|r| r.into_decoder().ok())
|
||||
.map(|d| d.original_color_type());
|
||||
match kind {
|
||||
Some(E::L1 | E::La1 | E::L2 | E::La2 | E::L4 | E::La4 | E::L8 | E::La8 | E::L16 | E::La16) => "Grayscale",
|
||||
Some(E::Cmyk8 | E::Cmyk16) => "CMYK",
|
||||
match craft_image_codecs::decode(bytes).ok().map(|img| img.layout()) {
|
||||
Some(craft_image_codecs::ChannelLayout::Gray | craft_image_codecs::ChannelLayout::GrayA) => "Grayscale",
|
||||
Some(craft_image_codecs::ChannelLayout::Cmyk | craft_image_codecs::ChannelLayout::CmykA) => "CMYK",
|
||||
_ => "RGB",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -366,17 +366,30 @@ fn preview(doc: &Document, art: &Node, frame: Rect) -> Option<Vec<u8>> {
|
||||
})
|
||||
.collect();
|
||||
let rgba = image::RgbaImage::from_raw(w, h, px)?;
|
||||
let mut out = vec![];
|
||||
// Opaque but for the edge's anti-aliasing (an artboard drawn to whole pixels) and compositing's
|
||||
// rounding: JPEG.
|
||||
let inner = |x: u32, y: u32| x > 0 && y > 0 && x + 1 < w && y + 1 < h;
|
||||
if rgba.enumerate_pixels().all(|(x, y, p)| p[3] >= 250 || !inner(x, y)) {
|
||||
let rgb = image::DynamicImage::ImageRgba8(rgba).to_rgb8();
|
||||
image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, 80).encode_image(&rgb).ok()?;
|
||||
let raw = craft_image_codecs::Image::from_raw(
|
||||
rgb.width(),
|
||||
rgb.height(),
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U8,
|
||||
rgb.into_raw(),
|
||||
)
|
||||
.ok()?;
|
||||
let opts = craft_image_codecs::EncodeOptions { jpeg_quality: 80, ..Default::default() };
|
||||
craft_image_codecs::encode(&raw, craft_image_codecs::Format::Jpeg, &opts).ok()
|
||||
} else {
|
||||
rgba.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).ok()?;
|
||||
let raw = craft_image_codecs::Image::from_raw(
|
||||
rgba.width(),
|
||||
rgba.height(),
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U8,
|
||||
rgba.into_raw(),
|
||||
)
|
||||
.ok()?;
|
||||
craft_image_codecs::encode(&raw, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).ok()
|
||||
}
|
||||
Some(out)
|
||||
}
|
||||
|
||||
/// `s` stored in `d` (objects showing the same thing share one blob).
|
||||
|
||||
@@ -1984,7 +1984,41 @@ mod tests {
|
||||
/// A `size` px image of one color `rgba`.
|
||||
fn blob(rgba: [u8; 4], size: u32) -> ImageBlob {
|
||||
let mut png = Vec::new();
|
||||
image::RgbaImage::from_pixel(size, size, image::Rgba(rgba)).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(size, size, image::Rgba(rgba))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
ImageBlob::png(png)
|
||||
}
|
||||
|
||||
@@ -2025,9 +2059,42 @@ mod tests {
|
||||
let d = Document::new(100.0, 100.0);
|
||||
let red = blob([255, 0, 0, 255], 64);
|
||||
let mut png = Vec::new();
|
||||
image::RgbaImage::from_fn(64, 64, |x, y| image::Rgba(if x >= 56 && y >= 56 { [0, 0, 255, 255] } else { [255, 0, 0, 255] }))
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage =
|
||||
(image::RgbaImage::from_fn(64, 64, |x, y| image::Rgba(if x >= 56 && y >= 56 { [0, 0, 255, 255] } else { [255, 0, 0, 255] }))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let corner = ImageBlob::png(png);
|
||||
let mut t = Theme::new(&d);
|
||||
let whole = ImageRead { clip: None, ..clipped(&corner, 64.0, 4) };
|
||||
|
||||
@@ -782,9 +782,9 @@ fn palette_image(rgba: &[u8], w: u32, h: u32, s: &WebSettings, matte: Option<[u8
|
||||
|
||||
/// An optimised image as straight RGBA → `(pixels, width, height)` (the dialog's preview).
|
||||
pub fn decode(o: &Optimized) -> std::result::Result<(Vec<u8>, u32, u32), String> {
|
||||
let img = image::load_from_memory(o.display()).map_err(|e| format!("the optimised image can't be read back: {e}"))?.to_rgba8();
|
||||
let img = craft_image_codecs::decode(o.display()).map_err(|e| format!("the optimised image can't be read back: {e}"))?;
|
||||
let (w, h) = img.dimensions();
|
||||
Ok((img.into_raw(), w, h))
|
||||
Ok((img.to_rgba8(), w, h))
|
||||
}
|
||||
|
||||
/// Seconds to download `bytes` at `kbps` kilobits per second.
|
||||
|
||||
@@ -31,7 +31,41 @@ pub(crate) fn fill_hex(n: &Node) -> String {
|
||||
fn red_image(s: &mut Session) -> NodeId {
|
||||
let img = image::RgbaImage::from_pixel(4, 4, image::Rgba([255, 0, 0, 255]));
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.edit("Place", |d, sel| {
|
||||
d.images.insert("pic".into(), vectorcraft_doc::ImageBlob::png(png));
|
||||
let id = d.alloc_id();
|
||||
@@ -108,13 +142,47 @@ fn an_embedded_image_is_adjusted_for_export_without_changing_its_pixels() {
|
||||
let baked = vectorcraft_render::effects::bake_document(&doc).unwrap();
|
||||
let NodeKind::Image(im) = &baked.node(id).unwrap().kind else { panic!("not an image") };
|
||||
assert_ne!(im.key, "pic", "a recoloured copy");
|
||||
let px = image::load_from_memory(&baked.images[&im.key].bytes).unwrap().to_rgba8();
|
||||
let px = craft_image_codecs::decode(&baked.images[&im.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(px.get_pixel(1, 1).0, [0, 255, 0, 255]);
|
||||
assert_eq!(doc.images.len(), 1, "the document keeps its one image");
|
||||
// Through Expand Appearance too.
|
||||
s.execute("effect.expandAppearance", &json!({})).unwrap();
|
||||
let NodeKind::Image(im) = &node(&s, id).kind else { panic!("not an image") };
|
||||
let px = image::load_from_memory(&s.doc().unwrap().doc.images[&im.key].bytes).unwrap().to_rgba8();
|
||||
let px = craft_image_codecs::decode(&s.doc().unwrap().doc.images[&im.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(px.get_pixel(0, 0).0, [0, 255, 0, 255]);
|
||||
}
|
||||
|
||||
|
||||
@@ -140,7 +140,24 @@ fn an_asset_exports_its_art_alone() {
|
||||
run(&mut s, "assets.add", json!({"ids": [red]}));
|
||||
let out = files(&run(&mut s, "assets.export", json!({"formats": [{"format": "png", "scale": "2x"}]})));
|
||||
assert_eq!(out[0].0, "Asset-1@2x.png");
|
||||
let img = image::load_from_memory(&out[0].1).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&out[0].1)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(img.dimensions(), (40, 40));
|
||||
assert!(img.pixels().all(|p| p.0 == [255, 0, 0, 255]), "only the red square");
|
||||
// Vector formats crop to it too.
|
||||
@@ -163,7 +180,24 @@ fn an_assets_crop_takes_in_its_drop_shadow() {
|
||||
// The shadow's reach (10 pt) on every side.
|
||||
assert_eq!(assets(&mut s)[0]["bounds"], json!([40.0, 40.0, 40.0, 40.0]));
|
||||
let out = files(&run(&mut s, "assets.export", json!({"formats": [{"format": "png", "scale": "1x"}]})));
|
||||
let img = image::load_from_memory(&out[0].1).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&out[0].1)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(img.dimensions(), (40, 40));
|
||||
// The shadow's right half, x 70 to 80.
|
||||
assert!(img.get_pixel(35, 20)[3] > 0, "the shadow is in the image");
|
||||
@@ -417,7 +451,29 @@ fn export_for_screens_writes_chosen_assets_and_keeps_its_rules() {
|
||||
let out = files(&r);
|
||||
assert_eq!(out.len(), 1);
|
||||
assert_eq!(out[0].0, "Asset-2@60w.png");
|
||||
assert_eq!(image::load_from_memory(&out[0].1).unwrap().to_rgba8().dimensions(), (60, 40), "the width of the asset, not the artboard");
|
||||
assert_eq!(
|
||||
craft_image_codecs::decode(&out[0].1)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.dimensions(),
|
||||
(60, 40),
|
||||
"the width of the asset, not the artboard"
|
||||
);
|
||||
// The choice of art isn't remembered; the formats are.
|
||||
let settings = run(&mut s, "document.exportSettings", json!({}))["settings"].clone();
|
||||
assert!(settings.get("assets").is_none() && settings["formats"][0]["scale"] == "60w");
|
||||
@@ -466,7 +522,24 @@ fn assets_export_leaves_no_seam_between_abutting_shapes() {
|
||||
}),
|
||||
));
|
||||
assert_eq!(out[0].0, "Asset-1.png");
|
||||
let img = image::load_from_memory(&out[0].1).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&out[0].1)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(img.dimensions(), (40, 20));
|
||||
// Column 20 is the shared edge in crop space; green must stay red (no white bleed).
|
||||
let palest_g = (2..18).map(|y| img.get_pixel(20, y).0[1].max(img.get_pixel(19, y).0[1])).max().unwrap();
|
||||
|
||||
@@ -5391,7 +5391,41 @@ fn live_paint_bucket_alt_image_samples_fill_and_clears_stroke() {
|
||||
let mut s = session();
|
||||
let img = image::RgbaImage::from_raw(1, 1, vec![0, 255, 0, 255]).unwrap();
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.edit("Place", |d, _| {
|
||||
d.images.insert("px".into(), ImageBlob::png(png));
|
||||
let id = d.alloc_id();
|
||||
|
||||
@@ -59,7 +59,24 @@ fn exported_png_of_a_red_square_is_red() {
|
||||
copy_red_square(&mut s);
|
||||
let r = run(&mut s, "clipboard.exportPng", json!({"scale": 2}));
|
||||
assert_eq!((r["width"].as_u64(), r["height"].as_u64()), (Some(80), Some(80)));
|
||||
let img = image::load_from_memory(&decode(&r)).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&decode(&r))
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(img.dimensions(), (80, 80));
|
||||
assert_eq!(img.get_pixel(40, 40).0, [255, 0, 0, 255]);
|
||||
assert_eq!(img.get_pixel(2, 77).0, [255, 0, 0, 255], "cropped to the square");
|
||||
@@ -155,7 +172,41 @@ fn import_image_then_paste_gives_an_image() {
|
||||
#[test]
|
||||
fn an_image_read_apart_loads_as_import_image_loads_it() {
|
||||
let mut bmp = vec![];
|
||||
image::RgbImage::from_pixel(30, 20, image::Rgb([200, 40, 40])).write_to(&mut std::io::Cursor::new(&mut bmp), image::ImageFormat::Bmp).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbImage::from_pixel(30, 20, image::Rgb([200, 40, 40]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Bmp, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| bmp.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let (mut at_once, mut apart) = (session(), session());
|
||||
let p = json!({"mime": "image/bmp", "center": [100, 80]});
|
||||
let mut with_data = p.clone();
|
||||
@@ -195,7 +246,41 @@ fn a_pasted_bitmap_is_drawn_without_decoding_it_again() {
|
||||
// The image has pixels no other test uses, since every test shares the image store.
|
||||
let img = image::RgbImage::from_fn(300, 200, |x, y| image::Rgb([(x % 253) as u8, (y % 233) as u8, 17]));
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let mut s = session();
|
||||
run(&mut s, "clipboard.importImage", json!({"dataBase64": vectorcraft_format::base64_encode(&png), "mime": "image/png"}));
|
||||
run(&mut s, "edit.paste", json!({}));
|
||||
@@ -278,7 +363,41 @@ fn copied_files_paste_by_their_content() {
|
||||
let png = decode(&run(&mut s, "clipboard.exportPng", json!({})));
|
||||
let pdf = decode(&run(&mut s, "clipboard.exportPdf", json!({})));
|
||||
let mut jpeg = vec![];
|
||||
image::RgbImage::new(2, 2).write_to(&mut std::io::Cursor::new(&mut jpeg), image::ImageFormat::Jpeg).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbImage::new(2, 2)).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Jpeg, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| jpeg.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let svg = b"<svg xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"4\" height=\"4\"/></svg>";
|
||||
let of = |name: &str, bytes: &[u8]| file_flavour(name, &bytes[..bytes.len().min(FILE_HEAD)], &PASTE_ORDER);
|
||||
assert_eq!(of("red.png", &png), Some(PNG));
|
||||
@@ -402,7 +521,23 @@ fn copy_leaves_out_the_png_of_art_too_large_to_scale_down() {
|
||||
run(&mut s, "select.set", json!({"ids": [id]}));
|
||||
run(&mut s, "edit.copy", json!({}));
|
||||
let png = s.clipboard.flavours(false, false).into_iter().find(|f| f.mime == PNG).expect("a PNG");
|
||||
let img = image::load_from_memory(&png.data).unwrap();
|
||||
let img = craft_image_codecs::decode(&png.data)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
assert!(img.width() <= 4096 && img.height() <= 4096, "{}x{}", img.width(), img.height());
|
||||
}
|
||||
|
||||
@@ -445,7 +580,41 @@ fn noise_png(side: u32) -> Vec<u8> {
|
||||
})
|
||||
.collect();
|
||||
let mut out = vec![];
|
||||
image::RgbImage::from_raw(side, side, px).unwrap().write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbImage::from_raw(side, side, px).unwrap()).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,41 @@ fn session() -> Session {
|
||||
|
||||
fn png(img: image::DynamicImage) -> ImageBlob {
|
||||
let mut out = Vec::new();
|
||||
img.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img;
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
ImageBlob::png(out)
|
||||
}
|
||||
|
||||
|
||||
@@ -186,9 +186,41 @@ fn type_with_an_inline_graphic_stays_type_when_its_own_stroke_has_an_effect() {
|
||||
/// A selected 30×30 red image placed with its frame from (50, 50) to (80, 80).
|
||||
fn image(s: &mut Session) -> NodeId {
|
||||
let mut png = vec![];
|
||||
image::RgbaImage::from_pixel(30, 30, image::Rgba([255, 0, 0, 255]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(30, 30, image::Rgba([255, 0, 0, 255]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let r = run(s, "file.place", json!({"name": "red.png", "dataBase64": vectorcraft_format::base64_encode(&png), "at": [65, 65]}));
|
||||
NodeId(r["ids"][0].as_u64().unwrap())
|
||||
}
|
||||
@@ -259,11 +291,44 @@ fn an_images_stroke_with_outline_object_draws_exports_and_expands_along_its_fram
|
||||
/// A 100×100 PNG, transparent but for an opaque red disc of radius 30 in its middle.
|
||||
fn disc_png() -> Vec<u8> {
|
||||
let mut png = vec![];
|
||||
image::RgbaImage::from_fn(100, 100, |x, y| {
|
||||
let r = (f64::from(x) + 0.5 - 50.0).hypot(f64::from(y) + 0.5 - 50.0);
|
||||
image::Rgba([255, 0, 0, if r <= 30.0 { 255 } else { 0 }])
|
||||
})
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_fn(100, 100, |x, y| {
|
||||
let r = (f64::from(x) + 0.5 - 50.0).hypot(f64::from(y) + 0.5 - 50.0);
|
||||
image::Rgba([255, 0, 0, if r <= 30.0 { 255 } else { 0 }])
|
||||
}))
|
||||
.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
png
|
||||
}
|
||||
@@ -316,9 +381,41 @@ fn an_images_stroke_with_outline_object_follows_its_opaque_area() {
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("opaque.png");
|
||||
let mut png = vec![];
|
||||
image::RgbaImage::from_pixel(100, 100, image::Rgba([255, 0, 0, 255]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(100, 100, image::Rgba([255, 0, 0, 255]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
std::fs::write(&path, png).unwrap();
|
||||
run(&mut s, "links.relink", json!({"ids": [im.0], "path": path}));
|
||||
assert_eq!([shade(&s, 41, 100), shade(&s, 45, 100), shade(&s, 64, 100)], ["white", "black", "red"]);
|
||||
|
||||
@@ -798,7 +798,41 @@ fn a_blend_around_a_picked_objects_instance_is_left_alone() {
|
||||
fn image_symbol(s: &mut Session) -> (NodeId, Value) {
|
||||
let mut png = Vec::new();
|
||||
let pixels = image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(2, 1, vec![0, 255]).unwrap());
|
||||
pixels.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = pixels;
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let im = s
|
||||
.edit("Place", |d, _| {
|
||||
d.images.insert("gray".into(), vectorcraft_doc::ImageBlob::png(png));
|
||||
|
||||
@@ -403,14 +403,68 @@ fn blend_moves_a_mesh_to_the_blended_colour() {
|
||||
fn png(px: [[u8; 4]; 4]) -> Vec<u8> {
|
||||
let img = image::RgbaImage::from_raw(2, 2, px.concat()).unwrap();
|
||||
let mut out = Vec::new();
|
||||
img.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
fn pixels(s: &Session, id: NodeId) -> Vec<[u8; 4]> {
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let NodeKind::Image(im) = &d.node(id).unwrap().kind else { panic!("not an image") };
|
||||
image::load_from_memory(&d.images[&im.key].bytes).unwrap().to_rgba8().pixels().map(|p| p.0).collect()
|
||||
craft_image_codecs::decode(&d.images[&im.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.pixels()
|
||||
.map(|p| p.0)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A selected embedded 2×2 image.
|
||||
@@ -652,7 +706,41 @@ fn convert_to_indexed_refuses_bad_params() {
|
||||
/// An embedded `w` × `h` image of the pixels `f(x, y)`, placed and selected.
|
||||
fn placed(s: &mut Session, w: u32, h: u32, f: impl Fn(u32, u32) -> [u8; 4]) -> NodeId {
|
||||
let mut png = Vec::new();
|
||||
image::RgbaImage::from_fn(w, h, |x, y| image::Rgba(f(x, y))).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_fn(w, h, |x, y| image::Rgba(f(x, y)))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let r = s.execute("file.place", &json!({"name": "art.png", "dataBase64": vectorcraft_format::base64_encode(&png)})).unwrap();
|
||||
NodeId(r["ids"][0].as_u64().unwrap())
|
||||
}
|
||||
@@ -681,7 +769,41 @@ fn convert_to_indexed_takes_ids_and_leaves_linked_and_colorized_images() {
|
||||
assert_eq!(image_key(&s, a), ka);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo, "no undo step");
|
||||
let mut png = Vec::new();
|
||||
image::GrayImage::from_pixel(4, 4, image::Luma([90])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::GrayImage::from_pixel(4, 4, image::Luma([90]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let r = s.execute("file.place", &json!({"name": "gray.png", "dataBase64": vectorcraft_format::base64_encode(&png)})).unwrap();
|
||||
let gray = NodeId(r["ids"][0].as_u64().unwrap());
|
||||
s.execute("paint.setFill", &json!({"ids": [gray.0], "color": "#ff0000"})).unwrap();
|
||||
|
||||
@@ -117,7 +117,41 @@ fn the_frame_round_trips_natively() {
|
||||
fn red_image(s: &mut Session) -> NodeId {
|
||||
let img = image::RgbaImage::from_pixel(4, 4, image::Rgba([255, 0, 0, 255]));
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.edit("Place", |d, sel| {
|
||||
d.images.insert("pic".into(), vectorcraft_doc::ImageBlob::png(png));
|
||||
let id = d.alloc_id();
|
||||
@@ -159,7 +193,41 @@ fn a_colorized_image_keeps_its_color_in_an_envelope() {
|
||||
let mut s = session();
|
||||
let img = image::GrayImage::from_pixel(4, 4, image::Luma([0]));
|
||||
let mut png = vec![];
|
||||
image::DynamicImage::ImageLuma8(img).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = image::DynamicImage::ImageLuma8(img);
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let im = s
|
||||
.edit("Place", |d, sel| {
|
||||
d.images.insert("gray".into(), vectorcraft_doc::ImageBlob::png(png));
|
||||
@@ -187,7 +255,41 @@ fn a_colorized_image_keeps_its_color_in_an_envelope() {
|
||||
fn two_color_image(s: &mut Session) -> NodeId {
|
||||
let img = image::RgbaImage::from_fn(2, 2, |x, _| if x == 0 { image::Rgba([255, 0, 0, 255]) } else { image::Rgba([0, 0, 255, 255]) });
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.edit("Place", |d, sel| {
|
||||
d.images.insert("two".into(), vectorcraft_doc::ImageBlob::png(png));
|
||||
let id = d.alloc_id();
|
||||
|
||||
@@ -562,7 +562,41 @@ fn clicking_an_image_samples_its_pixel() {
|
||||
// Two pixels, orange and teal, 100 pt each, at (300, 0).
|
||||
let img = image::RgbaImage::from_raw(2, 1, [[255, 128, 0, 255], [0, 128, 128, 255]].concat()).unwrap();
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.edit("Place", |d, _| {
|
||||
d.images.insert("px".into(), ImageBlob::png(png));
|
||||
let l = d.layers[0].id;
|
||||
@@ -588,7 +622,41 @@ fn cmyk_doc_with_image(px: &[[u8; 4]]) -> Session {
|
||||
let w = px.len() as u32;
|
||||
let img = image::RgbaImage::from_raw(w, 1, px.concat()).unwrap();
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.edit("Place", |d, _| {
|
||||
d.images.insert("px".into(), ImageBlob::png(png));
|
||||
let l = d.layers[0].id;
|
||||
|
||||
@@ -459,7 +459,24 @@ fn export_selection_keeps_the_effects_in_the_crop() {
|
||||
assert_eq!(plain["bounds"], json!([99.5, 99.5, 21.0, 21.0]), "a plain path keeps its crop: {plain}");
|
||||
s.execute("effect.apply", &json!({"effect": effect, "params": params})).unwrap();
|
||||
let png = |v: &serde_json::Value| {
|
||||
image::load_from_memory(&vectorcraft_format::base64_decode(v["dataBase64"].as_str().unwrap()).unwrap()).unwrap().to_rgba8()
|
||||
craft_image_codecs::decode(&vectorcraft_format::base64_decode(v["dataBase64"].as_str().unwrap()).unwrap())
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
};
|
||||
let v = s.execute("document.exportSelection", &json!({"format": "png", "background": "transparent"})).unwrap();
|
||||
let b: Vec<f64> = v["bounds"].as_array().unwrap().iter().map(|n| n.as_f64().unwrap()).collect();
|
||||
@@ -485,7 +502,24 @@ fn a_screen_outer_glow_is_in_raster_exports() {
|
||||
s.execute("effect.apply", &json!({"effect": "stylize.outerGlow"})).unwrap();
|
||||
let mut pixel = |p: serde_json::Value| {
|
||||
let v = s.execute("document.export", &p).unwrap();
|
||||
let img = image::load_from_memory(&vectorcraft_format::base64_decode(v["dataBase64"].as_str().unwrap()).unwrap()).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&vectorcraft_format::base64_decode(v["dataBase64"].as_str().unwrap()).unwrap())
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
// 2 pt past the square's stroke, on the right.
|
||||
*img.get_pixel(122, 110)
|
||||
};
|
||||
@@ -512,7 +546,24 @@ fn export_selection_keeps_a_groups_shadow_in_the_crop() {
|
||||
let b: Vec<f64> = v["bounds"].as_array().unwrap().iter().map(|n| n.as_f64().unwrap()).collect();
|
||||
// The shadow of the right square reaches x = 158.5.
|
||||
assert!(b[0] < 99.5 && b[0] + b[2] >= 158.5, "{v}");
|
||||
let img = image::load_from_memory(&vectorcraft_format::base64_decode(v["dataBase64"].as_str().unwrap()).unwrap()).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&vectorcraft_format::base64_decode(v["dataBase64"].as_str().unwrap()).unwrap())
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert!(img.get_pixel((155.0 - b[0]) as u32, img.height() / 2)[3] > 0, "the shadow is in the image");
|
||||
}
|
||||
|
||||
@@ -534,7 +585,24 @@ fn the_arts_bounds_take_in_the_shadow_of_a_group_and_of_type() {
|
||||
s.execute("effect.apply", &json!({"effect": "stylize.dropShadow", "ids": [id], "params": {"x": 8, "y": 0, "blur": 0}})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [id]})).unwrap();
|
||||
let png = |v: &serde_json::Value| {
|
||||
image::load_from_memory(&vectorcraft_format::base64_decode(v["dataBase64"].as_str().unwrap()).unwrap()).unwrap().to_rgba8()
|
||||
craft_image_codecs::decode(&vectorcraft_format::base64_decode(v["dataBase64"].as_str().unwrap()).unwrap())
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
};
|
||||
let art = s.execute("document.export", &json!({"format": "png", "useArtboards": false, "background": "transparent"})).unwrap();
|
||||
let sel = s.execute("document.exportSelection", &json!({"format": "png", "background": "transparent"})).unwrap();
|
||||
|
||||
@@ -499,7 +499,26 @@ fn png_exports_carry_text_chunks() {
|
||||
assert_eq!(get("Keywords"), Some("fair, Poster, print"));
|
||||
assert_eq!(get("Copyright"), Some("(c) 2026 Ada"));
|
||||
assert!(get("Creation Time").is_some_and(|t| t.ends_with('Z')));
|
||||
assert!(image::load_from_memory(&png).is_ok(), "still a valid PNG");
|
||||
assert!(
|
||||
craft_image_codecs::decode(&png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.is_ok(),
|
||||
"still a valid PNG"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -300,7 +300,41 @@ fn a_radial_gradient_lists_its_stops_from_the_first() {
|
||||
/// A `w`×`h` PNG of one colour (`rgba`), 72 ppi.
|
||||
fn png(w: u32, h: u32, rgba: [u8; 4]) -> Vec<u8> {
|
||||
let mut out = vec![];
|
||||
image::RgbaImage::from_pixel(w, h, image::Rgba(rgba)).write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(w, h, image::Rgba(rgba))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -402,7 +436,41 @@ fn disk(size: u32, r: f64) -> Vec<u8> {
|
||||
image::Rgba([0, 0, 0, ((r + 0.5 - d).clamp(0.0, 1.0) * 255.0).round() as u8])
|
||||
});
|
||||
let mut out = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -530,7 +598,41 @@ fn colorized_bitmap_over_square() -> (Session, Vec<u8>) {
|
||||
s.execute("paint.setFill", &json!({"color": "#ff0000"})).unwrap();
|
||||
let gray = vec![0, 255, 0, 255, 255, 0, 255, 0];
|
||||
let mut bytes = vec![];
|
||||
image::GrayImage::from_raw(4, 2, gray.clone()).unwrap().write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::GrayImage::from_raw(4, 2, gray.clone()).unwrap()).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| bytes.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let p = json!({"name": "b.png", "dataBase64": vectorcraft_format::base64_encode(&bytes), "rect": [30, 10, 40, 20]});
|
||||
s.execute("file.place", &p).unwrap();
|
||||
s.execute("paint.setFill", &json!({"swatch": "Ink"})).unwrap();
|
||||
@@ -593,10 +695,41 @@ fn a_colorized_image_of_black_and_clear_pixels_is_written_as_an_image_mask() {
|
||||
s.execute("file.new", &json!({"width": 200, "height": 100})).unwrap();
|
||||
s.execute("swatch.new", &json!({"name": "Ink", "color": {"c": 100, "m": 0, "y": 0, "k": 0}, "spot": true})).unwrap();
|
||||
let mut bytes = vec![];
|
||||
image::GrayAlphaImage::from_raw(2, 1, vec![0, 255, 0, 0])
|
||||
.unwrap()
|
||||
.write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::GrayAlphaImage::from_raw(2, 1, vec![0, 255, 0, 0]).unwrap()).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| bytes.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let p = json!({"name": "c.png", "dataBase64": vectorcraft_format::base64_encode(&bytes), "rect": [30, 10, 20, 10]});
|
||||
s.execute("file.place", &p).unwrap();
|
||||
s.execute("paint.setFill", &json!({"swatch": "Ink"})).unwrap();
|
||||
@@ -732,7 +865,41 @@ fn an_artboard_the_document_does_not_have_is_refused_as_an_export_refuses_it() {
|
||||
fn checker(a: [u8; 4], b: [u8; 4]) -> Vec<u8> {
|
||||
let img = image::RgbaImage::from_fn(32, 32, |x, y| image::Rgba(if (x < 16) == (y < 16) { a } else { b }));
|
||||
let mut out = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -760,7 +927,41 @@ fn colorized_pattern_over_blue() -> Session {
|
||||
|
||||
let tile = image::GrayImage::from_fn(32, 32, |x, y| image::Luma([if (x < 16) == (y < 16) { 0 } else { 255 }]));
|
||||
let mut bytes = vec![];
|
||||
tile.write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = tile.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| bytes.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let mut s = pattern_over_blue(&bytes, 1.0);
|
||||
let d = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc);
|
||||
assert!(d.patterns[0].art.iter_mut().any(|art| colorize_image(std::sync::Arc::make_mut(art))));
|
||||
|
||||
@@ -35,7 +35,41 @@ impl Drop for Folder {
|
||||
pub(crate) fn png(w: u32, h: u32, rgb: [u8; 3]) -> Vec<u8> {
|
||||
let mut out = vec![];
|
||||
let [r, g, b] = rgb;
|
||||
image::RgbaImage::from_pixel(w, h, image::Rgba([r, g, b, 255])).write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(w, h, image::Rgba([r, g, b, 255]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -200,7 +234,41 @@ fn linked_dib_and_pns_files_place_update_and_relink() {
|
||||
let bmp = |w: u32, h: u32, rgb: [u8; 3]| {
|
||||
let mut out = vec![];
|
||||
let [r, g, b] = rgb;
|
||||
image::RgbImage::from_pixel(w, h, image::Rgb([r, g, b])).write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Bmp).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbImage::from_pixel(w, h, image::Rgb([r, g, b]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Bmp, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
};
|
||||
let dir = Folder::new("dib");
|
||||
@@ -1300,7 +1368,41 @@ pub(crate) fn cut_png(w: u32, h: u32) -> Vec<u8> {
|
||||
image::Rgb([v as u8, (v >> 8) as u8, (v >> 16) as u8])
|
||||
});
|
||||
let mut out = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out.truncate(out.len() / 2);
|
||||
out
|
||||
}
|
||||
@@ -1414,12 +1516,27 @@ fn low_res_proxy_eps_export_uses_full_resolution_from_the_linked_file() {
|
||||
let (full, warnings) = cmd::links::full_linked_images(&s.doc().unwrap().doc, Default::default());
|
||||
assert!(warnings.is_empty(), "{warnings:?}");
|
||||
assert!(!full.images[&key].is_proxy());
|
||||
let dim =
|
||||
image::ImageReader::new(std::io::Cursor::new(full.images[&key].bytes.as_ref())).with_guessed_format().unwrap().into_dimensions().unwrap();
|
||||
let dim = craft_image_codecs::decode(full.images[&key].bytes.as_ref()).unwrap().dimensions();
|
||||
assert_eq!(dim, (600, 300));
|
||||
let png_out = s.execute("document.export", &json!({"format": "png"})).unwrap();
|
||||
let bytes = vectorcraft_format::base64_decode(png_out["dataBase64"].as_str().unwrap()).unwrap();
|
||||
let rendered = image::load_from_memory(&bytes).unwrap();
|
||||
let rendered = craft_image_codecs::decode(&bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
assert!(rendered.width() >= 400 && rendered.height() >= 300);
|
||||
assert!(near(centre_colour(&s.doc().unwrap().doc), RED), "canvas still shows the preview");
|
||||
}
|
||||
@@ -1489,7 +1606,41 @@ fn a_file_read_for_opening_has_its_links_read_and_its_type_laid_out() {
|
||||
let dir = Folder::new("read-for-open");
|
||||
let tif = dir.file("art/photo.tif");
|
||||
let mut tiff = vec![];
|
||||
image::RgbImage::from_pixel(600, 300, image::Rgb(RED)).write_to(&mut std::io::Cursor::new(&mut tiff), image::ImageFormat::Tiff).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbImage::from_pixel(600, 300, image::Rgb(RED))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Tiff, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| tiff.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
write(&tif, &tiff);
|
||||
let (gone, changed) = (dir.file("gone.png"), dir.file("changed.png"));
|
||||
write(&gone, &png(300, 100, BLUE));
|
||||
|
||||
@@ -1525,7 +1525,41 @@ fn two_colour_image(s: &mut Session, clear_right: bool, linked: bool) -> NodeId
|
||||
let right = image::Rgba([0, 0, 255, if clear_right { 0 } else { 255 }]);
|
||||
let img = image::RgbaImage::from_fn(8, 4, |x, _| if x < 4 { image::Rgba([255, 0, 0, 255]) } else { right });
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.edit("Place", |d, sel| {
|
||||
d.images.insert("pic".into(), vectorcraft_doc::ImageBlob::png(png));
|
||||
let id = d.alloc_id();
|
||||
|
||||
@@ -126,7 +126,23 @@ fn preview_is_at_most_256_pixels() {
|
||||
let mut s = session();
|
||||
let bytes = saved(&mut s, json!({"preview": true, "compress": true}));
|
||||
let png = vectorcraft_format::preview(&bytes).expect("a preview");
|
||||
let img = image::load_from_memory(&png).unwrap();
|
||||
let img = craft_image_codecs::decode(&png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!((img.width(), img.height()), (256, 128), "the 400×200 artboard fitted into 256 px");
|
||||
assert!(vectorcraft_format::preview(&saved(&mut s, json!({}))).is_none(), "off by default");
|
||||
}
|
||||
@@ -154,7 +170,41 @@ fn saving_writes_through_a_temporary_file() {
|
||||
fn unused_images_are_left_out_of_saves() {
|
||||
let mut s = session();
|
||||
let mut png = vec![];
|
||||
image::RgbaImage::from_pixel(4, 4, image::Rgba([1, 2, 3, 255])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(4, 4, image::Rgba([1, 2, 3, 255]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let r = s.execute("file.place", &json!({"name": "a.png", "dataBase64": vectorcraft_format::base64_encode(&png)})).unwrap();
|
||||
let id = r["ids"][0].as_u64().unwrap();
|
||||
assert_eq!(json_of(&saved(&mut s, json!({})))["images"].as_object().unwrap().len(), 1);
|
||||
|
||||
@@ -90,7 +90,26 @@ fn a_white_background_exports_opaque() {
|
||||
let mut s = new_doc(json!({"width": 20, "height": 20, "backgroundContents": bg}));
|
||||
let r = s.execute("document.export", &json!({"format": "png"})).unwrap();
|
||||
let png = vectorcraft_format::base64_decode(r["dataBase64"].as_str().unwrap()).unwrap();
|
||||
image::load_from_memory(&png).unwrap().to_rgba8().get_pixel(10, 10).0
|
||||
craft_image_codecs::decode(&png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.get_pixel(10, 10)
|
||||
.0
|
||||
};
|
||||
assert_eq!(alpha("white"), [255, 255, 255, 255]);
|
||||
assert_eq!(alpha("transparent")[3], 0);
|
||||
@@ -540,9 +559,41 @@ fn edit_print_profile(s: &mut Session, dir: &std::path::Path) {
|
||||
// One embedded image becomes a symbol's art, another stays on the profile's layer.
|
||||
let png = |rgb: [u8; 3]| {
|
||||
let mut out = vec![];
|
||||
image::RgbaImage::from_pixel(4, 4, image::Rgba([rgb[0], rgb[1], rgb[2], 255]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(4, 4, image::Rgba([rgb[0], rgb[1], rgb[2], 255]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
vectorcraft_format::base64_encode(&out)
|
||||
};
|
||||
s.execute("file.place", &json!({"name": "red.png", "dataBase64": png([255, 0, 0]), "link": false})).unwrap();
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
//! live objects and single fills and strokes are written with images of their shadows, glows and
|
||||
//! blurs (no "left out" warning), and the PDF reads back looking like the canvas.
|
||||
|
||||
use std::io::Cursor;
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde_json::json;
|
||||
@@ -38,7 +37,41 @@ fn with(mut n: Node, fx: Effect) -> Node {
|
||||
fn png(w: u32, h: u32) -> Vec<u8> {
|
||||
let img = image::RgbaImage::from_fn(w, h, |x, _| image::Rgba([(x * 255 / w) as u8, 60, 160, 255]));
|
||||
let mut out = Vec::new();
|
||||
image::DynamicImage::from(img).write_to(&mut Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = image::DynamicImage::from(img);
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,41 @@ fn session() -> Session {
|
||||
/// A `w`×`h` PNG of one colour (`rgba`), declaring `ppi` when given.
|
||||
fn png(w: u32, h: u32, rgba: [u8; 4], ppi: Option<f64>) -> Vec<u8> {
|
||||
let mut out = vec![];
|
||||
image::RgbaImage::from_pixel(w, h, image::Rgba(rgba)).write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(w, h, image::Rgba(rgba))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
match ppi {
|
||||
Some(r) => cmd::fileio::ppi::with_png_resolution(&out, (r, r)),
|
||||
None => out,
|
||||
@@ -482,13 +516,46 @@ fn keeping_an_image_decodes_nothing() {
|
||||
}
|
||||
|
||||
/// `w`×`h` pixels of noise, which compress little, encoded as `format`.
|
||||
fn noise(w: u32, h: u32, format: image::ImageFormat) -> Vec<u8> {
|
||||
fn noise(w: u32, h: u32, format: craft_image_codecs::Format) -> Vec<u8> {
|
||||
let img = image::RgbImage::from_fn(w, h, |x, y| {
|
||||
let v = x.wrapping_mul(2_654_435_761) ^ y.wrapping_mul(40_503).rotate_left(13);
|
||||
image::Rgb([v as u8, (v >> 8) as u8, (v >> 16) as u8])
|
||||
});
|
||||
let mut out = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut out), format).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, format, &craft_image_codecs::EncodeOptions::default()).map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -497,7 +564,7 @@ fn noise(w: u32, h: u32, format: image::ImageFormat) -> Vec<u8> {
|
||||
/// cut short still decodes, with its missing rows empty, and is placed.
|
||||
#[test]
|
||||
fn an_image_whose_pixels_cant_be_read_is_not_placed() {
|
||||
let mut cut = noise(600, 300, image::ImageFormat::Png);
|
||||
let mut cut = noise(600, 300, craft_image_codecs::Format::Png);
|
||||
cut.truncate(cut.len() / 2);
|
||||
let mut s = session();
|
||||
let info = s.execute("file.place.info", &file("cut.png", &cut)).unwrap();
|
||||
@@ -513,7 +580,7 @@ fn an_image_whose_pixels_cant_be_read_is_not_placed() {
|
||||
assert!(top_children(&s).is_empty(), "{p}: nothing placed");
|
||||
assert_eq!((s.doc().unwrap().history.undo.len(), s.journal.len()), (undo, journal), "{p}");
|
||||
}
|
||||
let mut jpeg = noise(600, 300, image::ImageFormat::Jpeg);
|
||||
let mut jpeg = noise(600, 300, craft_image_codecs::Format::Jpeg);
|
||||
jpeg.truncate(jpeg.len() / 2);
|
||||
let r = place(&mut s, "cut.jpg", &jpeg, json!({}));
|
||||
assert_eq!(r["format"], "jpg");
|
||||
@@ -894,7 +961,41 @@ fn placed_art_brings_its_brushes_renaming_on_conflict() {
|
||||
fn a_placed_brush_whose_art_uses_a_renamed_image_is_copied() {
|
||||
let file = |pixel: u8| {
|
||||
let mut png = vec![];
|
||||
image::GrayImage::from_pixel(2, 1, image::Luma([pixel])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::GrayImage::from_pixel(2, 1, image::Luma([pixel]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let mut s = session();
|
||||
s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap();
|
||||
s.edit("Brush", |d, _| {
|
||||
@@ -942,7 +1043,41 @@ fn a_placed_brush_whose_art_uses_a_renamed_image_is_copied() {
|
||||
fn info_reports_size_resolution_colour_mode_and_link() {
|
||||
let mut s = session();
|
||||
let mut gray = vec![];
|
||||
image::GrayImage::from_pixel(300, 150, image::Luma([128])).write_to(&mut std::io::Cursor::new(&mut gray), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::GrayImage::from_pixel(300, 150, image::Luma([128]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| gray.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let gray = cmd::fileio::ppi::with_png_resolution(&gray, (300.0, 300.0));
|
||||
let mut p = file("scan.png", &gray);
|
||||
p["thumbnail"] = json!(16);
|
||||
@@ -950,7 +1085,23 @@ fn info_reports_size_resolution_colour_mode_and_link() {
|
||||
assert_eq!((info["colorMode"].as_str(), info["pixelWidth"].as_u64()), (Some("Grayscale"), Some(300)));
|
||||
assert_eq!(info["ppi"], json!([300.0, 300.0]));
|
||||
assert!(close(info["width"].as_f64().unwrap(), 72.0));
|
||||
let thumb = image::load_from_memory(&vectorcraft_format::base64_decode(info["thumbnailBase64"].as_str().unwrap()).unwrap()).unwrap();
|
||||
let thumb = craft_image_codecs::decode(&vectorcraft_format::base64_decode(info["thumbnailBase64"].as_str().unwrap()).unwrap())
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!((thumb.width(), thumb.height()), (16, 8));
|
||||
assert!(top_children(&s).is_empty(), "info places nothing");
|
||||
// A path is linked by default.
|
||||
@@ -1090,7 +1241,7 @@ fn files_read_for_the_place_cursor_apart_from_the_session_load_it_as_file_place_
|
||||
/// queue, read with the session or apart from it.
|
||||
#[test]
|
||||
fn the_place_queue_skips_an_image_whose_pixels_cant_be_read() {
|
||||
let mut cut = noise(600, 300, image::ImageFormat::Png);
|
||||
let mut cut = noise(600, 300, craft_image_codecs::Format::Png);
|
||||
cut.truncate(cut.len() / 2);
|
||||
let p = json!({"files": [file("a.png", &png(40, 20, [9, 9, 9, 255], None)), file("cut.png", &cut)], "link": false});
|
||||
let (mut a, mut b) = (session(), session());
|
||||
@@ -1137,7 +1288,41 @@ fn the_place_queue_draws_a_thumbnail_without_decoding_the_image_again() {
|
||||
// Pixels no other test uses, since every test shares the image store.
|
||||
let img = image::RgbImage::from_fn(320, 160, |x, y| image::Rgb([(x % 251) as u8, 23, (y % 241) as u8]));
|
||||
let mut bytes = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| bytes.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let p = json!({"files": [file("q.png", &bytes)], "link": false, "thumbnail": 64});
|
||||
let decodes = vectorcraft_render::image_decodes();
|
||||
let r = session().execute("file.place.queue", &p).unwrap();
|
||||
|
||||
@@ -152,7 +152,26 @@ fn a_placed_document_is_vectors_in_every_output() {
|
||||
assert_eq!(images, 0, "no image in the PDF");
|
||||
assert!(paths > 0);
|
||||
let png = decode(&s.execute("document.serialize", &json!({"format": "png"})).unwrap());
|
||||
let [r, g, b, _] = image::load_from_memory(&png).unwrap().to_rgba8().get_pixel(200, 150).0;
|
||||
let [r, g, b, _] = craft_image_codecs::decode(&png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.get_pixel(200, 150)
|
||||
.0;
|
||||
assert!(near([r, g, b], RED), "{:?}", [r, g, b]);
|
||||
for format in ["eps", "emf", "dxf"] {
|
||||
let out = s.execute("document.serialize", &json!({"format": format})).unwrap();
|
||||
@@ -192,7 +211,26 @@ fn the_preview_is_saved_so_the_document_shows_it_without_its_file() {
|
||||
// Output with the file gone: the preview, with a warning.
|
||||
let png = s.execute("document.serialize", &json!({"format": "png"})).unwrap();
|
||||
assert!(png["warnings"].to_string().contains("1 placed document output as its preview"), "{png}");
|
||||
let [r, g, b, _] = image::load_from_memory(&decode(&png)).unwrap().to_rgba8().get_pixel(200, 150).0;
|
||||
let [r, g, b, _] = craft_image_codecs::decode(&decode(&png))
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.get_pixel(200, 150)
|
||||
.0;
|
||||
assert!(near([r, g, b], RED), "{:?}", [r, g, b]);
|
||||
// Saved for older apps: the art it shows (here its preview), as plain objects.
|
||||
let old: Value =
|
||||
|
||||
@@ -1157,9 +1157,41 @@ fn pathfinder_takes_swatch_linked_pieces_of_type_in_a_reshaped_group() {
|
||||
fn an_image_s_painted_fill_is_hit_and_measured_as_drawn() {
|
||||
let mut s = session();
|
||||
let mut png = vec![];
|
||||
image::RgbaImage::from_pixel(30, 30, image::Rgba([255, 0, 0, 255]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(30, 30, image::Rgba([255, 0, 0, 255]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let r = run(&mut s, "file.place", json!({"name": "red.png", "dataBase64": vectorcraft_format::base64_encode(&png), "at": [65, 65]}));
|
||||
let im = NodeId(r["ids"][0].as_u64().unwrap());
|
||||
run(&mut s, "swatch.new", json!({"name": "Leaf", "color": "#00ff00"}));
|
||||
|
||||
@@ -171,7 +171,24 @@ fn print_as_bitmap_honours_rich_black() {
|
||||
}
|
||||
});
|
||||
let bytes = &prepared.images[key.as_ref().expect("bitmap image")].bytes;
|
||||
let img = image::load_from_memory(bytes).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
img.get_pixel(img.width() / 2, img.height() / 2).0
|
||||
};
|
||||
let accurate = centre(&crate::cmd::printadvanced::prepare("file.print", doc(&s), &set, &saved, false).unwrap().unwrap());
|
||||
|
||||
@@ -287,7 +287,41 @@ fn rings_png() -> Vec<u8> {
|
||||
image::Rgba([v as u8, v as u8, 255 - v as u8, 255])
|
||||
});
|
||||
let mut png = Vec::new();
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
png
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,24 @@ fn effect_image(doc: &Document) -> image::RgbaImage {
|
||||
let kids = flat.layers[0].children().unwrap();
|
||||
let NodeKind::Group { children, .. } = &kids[0].kind else { panic!("a shadow becomes a group") };
|
||||
let NodeKind::Image(im) = &children[0].kind else { panic!("the image goes under the object") };
|
||||
image::load_from_memory(&flat.images[&im.key].bytes).unwrap().to_rgba8()
|
||||
craft_image_codecs::decode(&flat.images[&im.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -167,7 +184,24 @@ fn the_color_model_and_anti_alias_reach_the_pixels() {
|
||||
let out = s.execute("object.rasterize", &json!({"colorModel": "rgb"})).unwrap();
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let NodeKind::Image(im) = &d.node(id(&out)).unwrap().kind else { panic!("an image") };
|
||||
let img = image::load_from_memory(&d.images[&im.key].bytes).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&d.images[&im.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert!(img.pixels().all(|p| p[3] == 0 || p[3] == 255), "hard edges");
|
||||
assert!(img.pixels().any(|p| p[3] == 0) && img.pixels().any(|p| p[3] == 255));
|
||||
}
|
||||
@@ -184,7 +218,24 @@ fn rasterize_takes_the_document_settings_as_defaults() {
|
||||
assert_eq!((out["width"].as_u64(), out["height"].as_u64()), (Some(60), Some(60)), "10 pt around");
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let NodeKind::Image(im) = &d.node(id(&out)).unwrap().kind else { panic!("an image") };
|
||||
let img = image::load_from_memory(&d.images[&im.key].bytes).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&d.images[&im.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(img.get_pixel(0, 0).0, [255, 255, 255, 255], "white around the art");
|
||||
let p = img.get_pixel(30, 30).0;
|
||||
assert!(p[0] == p[1] && p[1] == p[2] && p[0] < 128, "red in grayscale: {p:?}");
|
||||
@@ -395,7 +446,24 @@ fn object_rasterize_takes_in_everything_an_effect_stack_paints() {
|
||||
let made = id(&s.execute("object.rasterize", &json!({"ppi": 72, "background": "transparent"})).unwrap());
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let (width, height, xf, png, _) = rasterized(d, d.node(made).unwrap());
|
||||
let img = image::load_from_memory(&png).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
// 41 pt of rectangle and stroke, and 6 pt of blur and 10 pt of white on each side: white from
|
||||
// the first column to the last.
|
||||
assert_eq!((width, xf.translation().x), (73, 34.0));
|
||||
@@ -503,7 +571,24 @@ fn rasterize_to_cmyk_makes_a_cmyk_image() {
|
||||
let mask = masked.extras().mask.as_deref().expect("an opacity mask").clone();
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let NodeKind::Image(m) = &mask.art.kind else { panic!("the mask is an image") };
|
||||
let alpha = image::load_from_memory(&d.images[&m.key].bytes).unwrap().to_luma8();
|
||||
let alpha = craft_image_codecs::decode(&d.images[&m.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_luma8();
|
||||
assert_eq!((alpha.get_pixel(1, 1).0, alpha.get_pixel(25, 25).0), ([0], [255]));
|
||||
assert!(mask.clip);
|
||||
}
|
||||
@@ -530,7 +615,24 @@ fn rasterize_to_grayscale_or_bitmap_makes_a_grayscale_image() {
|
||||
assert!(n.extras().mask.is_none(), "{mode} {model} {background}: no opacity mask");
|
||||
let NodeKind::Image(im) = &n.kind else { panic!("an image") };
|
||||
let blob = d.images[&im.key].clone();
|
||||
let pixels = image::load_from_memory(&blob.bytes).unwrap().to_luma_alpha8();
|
||||
let pixels = craft_image_codecs::decode(&blob.bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_luma_alpha8();
|
||||
assert_eq!(pixels.dimensions(), (50, 50));
|
||||
let at = |x, y| pixels.get_pixel(x, y).0;
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
|
||||
@@ -1250,7 +1250,41 @@ fn global_adjust_refuses_params_of_the_wrong_form() {
|
||||
fn halves_image(s: &mut Session, w: u32, h: u32, left: [u8; 4], right: [u8; 4]) -> (NodeId, vectorcraft_geom::Rect) {
|
||||
let img = image::RgbaImage::from_fn(w, h, |x, _| image::Rgba(if x < w / 2 { left } else { right }));
|
||||
let mut png = Vec::new();
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let r = run(s, "file.place", json!({"name": "halves.png", "dataBase64": vectorcraft_format::base64_encode(&png)}));
|
||||
let id = NodeId(r["ids"][0].as_u64().unwrap());
|
||||
let b = s.doc().unwrap().doc.node(id).unwrap().visual_bounds().unwrap();
|
||||
@@ -1375,7 +1409,41 @@ fn theme_picker_reads_symbol_art_and_nested_patterns() {
|
||||
fn theme_picker_reads_the_color_of_a_colorized_image() {
|
||||
let mut s = session();
|
||||
let mut png = Vec::new();
|
||||
image::GrayImage::from_pixel(8, 8, image::Luma([90])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::GrayImage::from_pixel(8, 8, image::Luma([90]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let r = run(&mut s, "file.place", json!({"name": "gray.png", "dataBase64": vectorcraft_format::base64_encode(&png)}));
|
||||
let id = NodeId(r["ids"][0].as_u64().unwrap());
|
||||
let b = s.doc().unwrap().doc.node(id).unwrap().visual_bounds().unwrap();
|
||||
@@ -1430,7 +1498,41 @@ fn theme_picker_reads_an_image_inside_its_clipping_path() {
|
||||
image::Rgba(if corner { [0, 0, 255, 255] } else { [255, 0, 0, 255] })
|
||||
});
|
||||
let mut png = Vec::new();
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let r = run(&mut s, "file.place", json!({"name": "corners.png", "dataBase64": vectorcraft_format::base64_encode(&png)}));
|
||||
let id = NodeId(r["ids"][0].as_u64().unwrap());
|
||||
let b = s.doc().unwrap().doc.node(id).unwrap().visual_bounds().unwrap();
|
||||
|
||||
@@ -760,7 +760,23 @@ fn eps_save_bounds_large_page_preview_without_cropping_vector_or_editing_data()
|
||||
let (ps, tiff) = vectorcraft_eps::sections(&bytes).unwrap();
|
||||
let tiff = tiff.unwrap();
|
||||
assert!(tiff.len() < 4 * 1024 * 1024 + 64 * 1024, "compatibility preview grew to {} bytes", tiff.len());
|
||||
let preview = image::load_from_memory_with_format(tiff, image::ImageFormat::Tiff).unwrap();
|
||||
let preview = craft_image_codecs::decode_as(craft_image_codecs::Format::Tiff, tiff)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!((preview.width(), preview.height()), (1024, 1024));
|
||||
assert!(std::str::from_utf8(ps).unwrap().contains("%%BoundingBox: 0 0 4000 4000"));
|
||||
let native = vectorcraft_format::load(&vectorcraft_eps::native(&bytes).unwrap()).unwrap();
|
||||
|
||||
@@ -168,9 +168,41 @@ fn a_click_on_the_drop_shadow_of_a_symbols_art_does_not_select_the_instance() {
|
||||
fn a_click_on_the_drop_shadow_of_an_image_outside_its_frame_does_not_select_it() {
|
||||
let mut s = session();
|
||||
let mut png = vec![];
|
||||
image::RgbaImage::from_pixel(30, 30, image::Rgba([255, 0, 0, 255]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(30, 30, image::Rgba([255, 0, 0, 255]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let r = run(&mut s, "file.place", json!({"name": "red.png", "dataBase64": vectorcraft_format::base64_encode(&png), "at": [65, 65]}));
|
||||
let im = NodeId(r["ids"][0].as_u64().unwrap());
|
||||
// The frame lies from x 50 to 80 and the shadow from 90 to 120.
|
||||
|
||||
@@ -506,7 +506,24 @@ fn a_copied_instances_png_is_cropped_to_its_art() {
|
||||
let r = run(&mut s, "clipboard.exportPng", json!({}));
|
||||
assert_eq!((r["width"].as_u64(), r["height"].as_u64()), (Some(40), Some(20)));
|
||||
let png = vectorcraft_format::base64_decode(r["dataBase64"].as_str().unwrap()).unwrap();
|
||||
let img = image::load_from_memory(&png).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(img.get_pixel(20, 10).0, [0, 0, 0, 255], "the art fills the picture");
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,41 @@ fn quarters(s: &mut Session) -> NodeId {
|
||||
image::Rgba([r, g, b, 255])
|
||||
});
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.edit("Place", |d, sel| {
|
||||
d.images.insert("quarters".into(), vectorcraft_doc::ImageBlob::png(png));
|
||||
let id = d.alloc_id();
|
||||
@@ -168,7 +202,24 @@ fn a_warped_image_keeps_its_bend_in_png() {
|
||||
let mut s = session();
|
||||
arc_image(&mut s);
|
||||
let out = s.execute("document.export", &json!({"format": "png"})).unwrap();
|
||||
let png = image::load_from_memory(&decoded(&out)).unwrap().to_rgba8();
|
||||
let png = craft_image_codecs::decode(&decoded(&out))
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
// The artboard starts at (0, 0) and the export draws one pixel a point.
|
||||
assert_eq!((png.width(), png.height()), (800, 600));
|
||||
for &(u, v) in &SAMPLES {
|
||||
@@ -241,7 +292,41 @@ fn expand_appearance_of_a_warped_image_draws_as_the_canvas_did() {
|
||||
fn red_on_clear(s: &mut Session) -> NodeId {
|
||||
let img = image::GrayImage::from_fn(40, 40, |_, y| image::Luma([if y < 20 { 0 } else { 255 }]));
|
||||
let mut png = vec![];
|
||||
image::DynamicImage::ImageLuma8(img).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = image::DynamicImage::ImageLuma8(img);
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let id = s
|
||||
.edit("Place", |d, sel| {
|
||||
d.images.insert("gray".into(), vectorcraft_doc::ImageBlob::png(png));
|
||||
@@ -302,7 +387,41 @@ fn half_clear_arc(s: &mut Session) {
|
||||
fn clear_arc(s: &mut Session, alpha: impl Fn(u32) -> u8) {
|
||||
let img = image::RgbaImage::from_fn(40, 40, |x, _| image::Rgba([255, 0, 0, alpha(x)]));
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.edit("Place", |d, sel| {
|
||||
d.images.insert("half".into(), vectorcraft_doc::ImageBlob::png(png));
|
||||
let id = d.alloc_id();
|
||||
|
||||
@@ -64,7 +64,24 @@ fn one(s: &mut Session, p: Value) -> Vec<u8> {
|
||||
}
|
||||
|
||||
fn pixels(file: &[u8]) -> image::RgbaImage {
|
||||
image::load_from_memory(file).unwrap().to_rgba8()
|
||||
craft_image_codecs::decode(file)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
}
|
||||
|
||||
fn distinct(img: &image::RgbaImage) -> usize {
|
||||
|
||||
@@ -81,7 +81,24 @@ fn public_affinity_documents_render_like_their_affinity_thumbnails() {
|
||||
let loaded = vectorcraft_engine::cmd::fileio::load(name, &bytes).unwrap();
|
||||
assert!(!loaded.preview_only, "{name} opened as its preview: {:?}", loaded.warnings);
|
||||
let d = &loaded.doc;
|
||||
let thumb = image::load_from_memory(vectorcraft_affinity::preview(&bytes).unwrap().png).unwrap().to_rgba8();
|
||||
let thumb = craft_image_codecs::decode(vectorcraft_affinity::preview(&bytes).unwrap().png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
// The embedded thumbnail shows the union of the artboards. Check every board,
|
||||
// including small ones that the previous largest-board-only check omitted.
|
||||
let union = d.artboards.iter().map(|a| a.rect).reduce(|a, b| a.union(b)).unwrap();
|
||||
@@ -166,7 +183,24 @@ fn affinity3_all_artboards_survive_native_save_and_export() {
|
||||
assert_eq!(vectorcraft_pdf::import(&pdf).unwrap().artboards.len(), 1, "{name} board {index}: PDF reimport");
|
||||
let psd = vectorcraft_engine::cmd::fileio::encode(doc, "psd", ¶ms).unwrap();
|
||||
let png = vectorcraft_engine::cmd::fileio::encode(doc, "png", ¶ms).unwrap();
|
||||
let png = image::load_from_memory(&png).unwrap().to_rgba8();
|
||||
let png = craft_image_codecs::decode(&png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
let (merged, has_alpha) = psd_merged(&psd);
|
||||
assert_eq!(merged.dimensions(), png.dimensions(), "{name} board {index}: PSD image dimensions");
|
||||
// PSD merged RGB is stored on a white matte for readers that ignore alpha;
|
||||
|
||||
@@ -558,7 +558,41 @@ fn the_eyedropper_takes_an_image_pixel_into_the_working_rgb_space() {
|
||||
let mut s = session();
|
||||
let img = image::RgbaImage::from_raw(1, 1, vec![83, 170, 223, 255]).unwrap();
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.edit("Place", |d, _| {
|
||||
d.images.insert("px".into(), ImageBlob::png(png));
|
||||
let l = d.layers[0].id;
|
||||
@@ -653,7 +687,24 @@ fn freeform_rgb_points_mix_in_the_working_rgb_space() {
|
||||
let canvas = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &opts).pixel(50, 50);
|
||||
let svg = vectorcraft_svg::export_full(&d, &vectorcraft_svg::ExportOptions::default(), None).svg;
|
||||
let data = svg.split("data:image/png;base64,").nth(1).unwrap().split('"').next().unwrap();
|
||||
let png = image::load_from_memory(&vectorcraft_testkit::format::base64_decode(data).unwrap()).unwrap().to_rgba8();
|
||||
let png = craft_image_codecs::decode(&vectorcraft_testkit::format::base64_decode(data).unwrap())
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
let written = png.get_pixel(png.width() / 2, png.height() / 2).0;
|
||||
let want = Color::rgb(0.5, 0.15, 0.5).to_rgba8(1.0);
|
||||
let display = f.field(spread_scale(box_)).sample(Point::new(50.5, 50.5)).0.map(|v| (v * 255.0).round() as u8);
|
||||
|
||||
Loaded 100 of 275 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user