Skip to content

ProCraft for Android: An Android native painting app written in Rust

Rust

feat(files): packages carry the time-lapse as video segments

Josh4a4d3019eb90
diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.kt
index bde672b..d600b7a 100644
--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.kt
+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.kt
@@ -1507,6 +1507,14 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {
resolver.openInputStream(uri)?.use { input -> temp.outputStream().use { input.copyTo(it) } } ?: return false
val s = runCatching { CanvasSession.openPackage(temp.path) }.getOrNull() ?: return false
s.save(dir.path)
+ // its time-lapse: the settings, and the video as frames
+ val segments = File(getApplication<Application>().cacheDir, "segments-${System.nanoTime()}")
+ try {
+ runCatching { io.github.artcraftandroid.procraft.core.packageTimeLapse(temp.path, segments.path) }
+ .getOrNull()?.let { TimeLapse(dir).take(it) }
+ } finally {
+ segments.deleteRecursively()
+ }
java.util.zip.ZipFile(temp).use { zip ->
zip.getEntry("QuickLook/Thumbnail.png")?.let { e ->
zip.getInputStream(e).use { input -> File(dir, "thumbnail.png").outputStream().use { input.copyTo(it) } }
@@ -1528,7 +1536,7 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {
runCatching {
val base = s.name().filter { it.isLetterOrDigit() || it in " _-" }.trim().replace(' ', '_').ifEmpty { "Untitled_Artwork" }
val out = File(File(getApplication<Application>().cacheDir, "exports").apply { mkdirs() }, "$base.$extension")
- s.exportPackage(out.path, System.nanoTime().toULong())
+ s.exportPackage(out.path, System.nanoTime().toULong(), timeLapse?.forPackage())
out
}.onFailure { android.util.Log.e("ProCraft", "exporting a package", it) }.getOrNull()
}
diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/TimeLapse.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/TimeLapse.kt
index 64c8911..70a286f 100644
--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/TimeLapse.kt
+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/TimeLapse.kt
@@ -6,8 +6,10 @@ import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
+import io.github.artcraftandroid.procraft.core.PackageTimeLapse
import java.io.File
import java.nio.ByteBuffer
+import kotlin.math.roundToInt
/**
* An artwork's time-lapse: a picture of the whole canvas after each change, kept as numbered JPEG
@@ -27,6 +29,8 @@ class TimeLapse(artwork: File) {
private val dir = File(artwork, "timelapse")
private val off = File(dir, "off")
private val settingsFile = File(dir, "settings.json")
+ private val segmentFile = File(dir, "segment.mp4")
+ private val segmentAbout = File(dir, "segment.json")
/** The video's size, quality and codec, kept with the artwork. */
var settings: TimeLapseSettings = runCatching {
@@ -71,12 +75,22 @@ class TimeLapse(artwork: File) {
drawColor(Color.WHITE)
drawBitmap(bitmap, 0f, 0f, null)
}
- dir.mkdirs()
- runCatching { file(count).outputStream().use { flat.compress(Bitmap.CompressFormat.JPEG, JPEG_QUALITY, it) } }.onSuccess { count++ }
+ keep(flat)
bitmap.recycle()
flat.recycle()
}
+ /** Keeps [picture] as the next frame, at most [FRAME_SIDE] on its long side. */
+ private fun keep(picture: Bitmap) {
+ val s = minOf(1f, FRAME_SIDE / maxOf(picture.width, picture.height).toFloat())
+ val frame = if (s < 1f) {
+ Bitmap.createScaledBitmap(picture, (picture.width * s).roundToInt().coerceAtLeast(1), (picture.height * s).roundToInt().coerceAtLeast(1), true)
+ } else picture
+ dir.mkdirs()
+ runCatching { file(count).outputStream().use { frame.compress(Bitmap.CompressFormat.JPEG, JPEG_QUALITY, it) } }.onSuccess { count++ }
+ if (frame !== picture) frame.recycle()
+ }
+
/** What the recorded frames take on disk, bytes. */
fun bytes(): Long = dir.listFiles { f -> f.name.startsWith("frame-") }?.sumOf { it.length() } ?: 0L
@@ -86,21 +100,24 @@ class TimeLapse(artwork: File) {
* The frames as an H.264 MP4 at [out]: all of them, or (with [seconds]) spread evenly over
* that long. False if there are none or the encoder fails.
*/
- fun export(out: File, seconds: Int?): Boolean {
- if (count == 0) return false
- val picks = if (seconds != null && count > seconds * FPS) {
+ fun export(out: File, seconds: Int?): Boolean = encode(out, seconds, count) != null
+
+ /** The first [frames] frames as a video at [out], as [export] makes it; its size, or null. */
+ private fun encode(out: File, seconds: Int?, frames: Int): Pair<Int, Int>? {
+ if (frames == 0) return null
+ val picks = if (seconds != null && frames > seconds * FPS) {
val n = seconds * FPS
- List(n) { (it.toLong() * (count - 1) / (n - 1).coerceAtLeast(1)).toInt() }
- } else List(count) { it }
- val first = frame(picks.first()) ?: return false
+ List(n) { (it.toLong() * (frames - 1) / (n - 1).coerceAtLeast(1)).toInt() }
+ } else List(frames) { it }
+ val first = frame(picks.first()) ?: return null
val video = settings
val mime = if (video.hevc) android.media.MediaFormat.MIMETYPE_VIDEO_HEVC else android.media.MediaFormat.MIMETYPE_VIDEO_AVC
- val (w, h) = VideoWriter.fit(first.width, first.height, video.side, mime) ?: return false
+ val (w, h) = VideoWriter.fit(first.width, first.height, video.side, mime) ?: return null
first.recycle()
val frameBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
val paint = Paint(Paint.FILTER_BITMAP_FLAG)
try {
- return VideoWriter.write(out, w, h, FPS, picks.size, mime, video.bitsPerPixel) { n, pixels ->
+ val ok = VideoWriter.write(out, w, h, FPS, picks.size, mime, video.bitsPerPixel) { n, pixels ->
val source = frame(picks[n]) ?: return@write false
// fitted on white
Canvas(frameBitmap).apply {
@@ -114,15 +131,86 @@ class TimeLapse(artwork: File) {
frameBitmap.getPixels(pixels, 0, w, 0, 0, w, h)
true
}
+ return if (ok) w to h else null
} finally {
frameBitmap.recycle()
}
}
+ /**
+ * Every frame as one video, for a package, with its size and bit rate: made again only when
+ * frames were added or the settings changed. Null with no frames, or if the encoder fails.
+ */
+ private fun segment(): Triple<File, Pair<Int, Int>, Long>? {
+ val frames = count
+ if (frames == 0) return null
+ val video = settings
+ runCatching {
+ val j = org.json.JSONObject(segmentAbout.readText())
+ if (segmentFile.exists() && j.getInt("frames") == frames && j.getInt("resolution") == video.resolution &&
+ j.getInt("quality") == video.quality && j.getBoolean("hevc") == video.hevc
+ ) return Triple(segmentFile, j.getInt("width") to j.getInt("height"), j.getLong("bitrate"))
+ }
+ segmentAbout.delete()
+ val size = encode(segmentFile, null, frames) ?: return null.also { segmentFile.delete() }
+ val bitrate = VideoWriter.bitRate(size.first, size.second, video.bitsPerPixel).toLong()
+ runCatching {
+ segmentAbout.writeText(
+ org.json.JSONObject().put("frames", frames).put("resolution", video.resolution).put("quality", video.quality)
+ .put("hevc", video.hevc).put("width", size.first).put("height", size.second).put("bitrate", bitrate).toString(),
+ )
+ }
+ return Triple(segmentFile, size, bitrate)
+ }
+
+ /** The time-lapse as a package keeps it: its settings, and its frames made into a video. */
+ fun forPackage(): PackageTimeLapse {
+ val video = settings
+ val segment = segment()
+ return PackageTimeLapse(
+ recording = recording,
+ quality = video.quality.toUByte(),
+ resolution = video.resolution.toUByte(),
+ hevc = video.hevc,
+ segments = listOfNotNull(segment?.first?.path),
+ width = (segment?.second?.first ?: 0).toUInt(),
+ height = (segment?.second?.second ?: 0).toUInt(),
+ fps = FPS.toUInt(),
+ bitrate = (segment?.third ?: 0L).toULong(),
+ )
+ }
+
+ /** Takes a package's time-lapse [t]: its settings, and its video's frames after these. */
+ fun take(t: PackageTimeLapse) {
+ settings = TimeLapseSettings(t.resolution.toInt(), t.quality.toInt(), t.hevc)
+ recording = t.recording
+ for (path in t.segments) {
+ val retriever = android.media.MediaMetadataRetriever()
+ try {
+ retriever.setDataSource(path)
+ val n = retriever.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_VIDEO_FRAME_COUNT)?.toIntOrNull() ?: 0
+ // a few at a time: one seek for each run, not each frame
+ var i = 0
+ while (i < n) {
+ val run = minOf(DECODE_RUN, n - i)
+ val pictures = runCatching { retriever.getFramesAtIndex(i, run) }.getOrNull() ?: break
+ pictures.forEach { keep(it); it.recycle() }
+ i += run
+ }
+ } catch (e: Exception) {
+ android.util.Log.e("ProCraft", "a time-lapse's video", e)
+ } finally {
+ retriever.release()
+ }
+ }
+ }
+
companion object {
const val FPS = 30
/** A frame's longest side, pixels. */
const val FRAME_SIDE = 1280
private const val JPEG_QUALITY = 85
+ /** Frames decoded together from a package's video. */
+ private const val DECODE_RUN = 8
}
}
diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/VideoWriter.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/VideoWriter.kt
index 3cbbee3..d25d976 100644
--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/VideoWriter.kt
+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/VideoWriter.kt
@@ -42,7 +42,7 @@ object VideoWriter {
): Boolean {
val format = MediaFormat.createVideoFormat(mime, width, height).apply {
setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Flexible)
- setInteger(MediaFormat.KEY_BIT_RATE, (width * height * bitsPerPixel).coerceAtLeast(1_000_000))
+ setInteger(MediaFormat.KEY_BIT_RATE, bitRate(width, height, bitsPerPixel))
setInteger(MediaFormat.KEY_FRAME_RATE, fps)
setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1)
}
@@ -106,24 +106,54 @@ object VideoWriter {
}
}
+ /** The bit rate a [width] × [height] video is encoded at, bits a second. */
+ fun bitRate(width: Int, height: Int, bitsPerPixel: Int = BITS_PER_PIXEL): Int = (width * height * bitsPerPixel).coerceAtLeast(1_000_000)
+
/** ARGB pixels into the encoder's YUV 4:2:0 planes (BT.601, video range). */
private fun writeYuv(image: android.media.Image, argb: IntArray, w: Int, h: Int) {
val planes = image.planes
val y = planes[0]
val u = planes[1]
val v = planes[2]
+ val yb = y.buffer
+ val ub = u.buffer
+ val vb = v.buffer
+ // a row at a time where a plane's samples are packed; interleaved chroma one by one
+ val luma = ByteArray(w)
+ val half = (w + 1) / 2
+ val cus = ByteArray(half)
+ val cvs = ByteArray(half)
+ val packedChroma = u.pixelStride == 1 && v.pixelStride == 1
for (row in 0 until h) {
+ val even = row % 2 == 0
for (col in 0 until w) {
val c = argb[row * w + col]
val r = (c shr 16) and 255
val g = (c shr 8) and 255
val b = c and 255
- y.buffer.put(row * y.rowStride + col * y.pixelStride, (((66 * r + 129 * g + 25 * b + 128) shr 8) + 16).toByte())
- if (row % 2 == 0 && col % 2 == 0) {
- val cu = ((-38 * r - 74 * g + 112 * b + 128) shr 8) + 128
- val cv = ((112 * r - 94 * g - 18 * b + 128) shr 8) + 128
- u.buffer.put((row / 2) * u.rowStride + (col / 2) * u.pixelStride, cu.toByte())
- v.buffer.put((row / 2) * v.rowStride + (col / 2) * v.pixelStride, cv.toByte())
+ luma[col] = (((66 * r + 129 * g + 25 * b + 128) shr 8) + 16).toByte()
+ if (even && col % 2 == 0) {
+ cus[col / 2] = (((-38 * r - 74 * g + 112 * b + 128) shr 8) + 128).toByte()
+ cvs[col / 2] = (((112 * r - 94 * g - 18 * b + 128) shr 8) + 128).toByte()
+ }
+ }
+ if (y.pixelStride == 1) {
+ yb.position(row * y.rowStride)
+ yb.put(luma, 0, w)
+ } else {
+ for (col in 0 until w) yb.put(row * y.rowStride + col * y.pixelStride, luma[col])
+ }
+ if (!even) continue
+ val at = row / 2
+ if (packedChroma) {
+ ub.position(at * u.rowStride)
+ ub.put(cus, 0, half)
+ vb.position(at * v.rowStride)
+ vb.put(cvs, 0, half)
+ } else {
+ for (i in 0 until half) {
+ ub.put(at * u.rowStride + i * u.pixelStride, cus[i])
+ vb.put(at * v.rowStride + i * v.pixelStride, cvs[i])
}
}
}
diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/gallery/DragOut.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/gallery/DragOut.kt
index 23c4e53..792da92 100644
--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/gallery/DragOut.kt
+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/gallery/DragOut.kt
@@ -93,7 +93,8 @@ fun exportArtwork(dir: File, format: String, out: File, pdfQuality: Int = 100):
if (!s.prepareOffscreen()) return@runCatching false
when (format) {
// ours and the other app's are the same package
- CanvasViewModel.OWN_LABEL, CanvasViewModel.PACKAGE_LABEL -> s.exportPackage(out.path, System.nanoTime().toULong())
+ CanvasViewModel.OWN_LABEL, CanvasViewModel.PACKAGE_LABEL ->
+ s.exportPackage(out.path, System.nanoTime().toULong(), io.github.artcraftandroid.procraft.canvas.TimeLapse(dir).forPackage())
"PDF" -> s.exportPdf(out.path, false, pdfQuality.toUByte())
"PSD" -> s.exportPsd(out.path)
"TIFF" -> s.exportTiff(out.path)
diff --git a/apps/procraft-android/src/project.rs b/apps/procraft-android/src/project.rs
index f16dd97..336dc2d 100644
--- a/apps/procraft-android/src/project.rs
+++ b/apps/procraft-android/src/project.rs
@@ -67,6 +67,22 @@ impl SavedView {
}
}
+/// An artwork's time-lapse in a package: whether it records, its video's settings (quality 0 to 3,
+/// resolution 0 to 2, HEVC or H.264), and its video so far as MP4 files in order, `width` ×
+/// `height` at `fps` and `bitrate` bits a second.
+#[derive(Clone, Debug, uniffi::Record)]
+pub struct PackageTimeLapse {
+ pub recording: bool,
+ pub quality: u8,
+ pub resolution: u8,
+ pub hevc: bool,
+ pub segments: Vec<String>,
+ pub width: u32,
+ pub height: u32,
+ pub fps: u32,
+ pub bitrate: u64,
+}
+
/// What an artwork keeps about itself besides the picture.
#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize, uniffi::Record)]
pub struct ArtworkInfo {
@@ -410,8 +426,32 @@ impl CanvasSession {
Ok(session)
}
- /// The artwork as a layered package at `path`; `seed` makes its layers' ids.
- pub fn export_package(&self, path: String, seed: u64) -> Result<(), MobileError> {
+ /// The artwork as a layered package at `path`, with its time-lapse; `seed` makes its layers' ids.
+ pub fn export_package(
+ &self,
+ path: String,
+ seed: u64,
+ time_lapse: Option<PackageTimeLapse>,
+ ) -> Result<(), MobileError> {
+ let time_lapse = match time_lapse {
+ Some(t) => {
+ let mut segments = Vec::with_capacity(t.segments.len());
+ for s in &t.segments {
+ segments.push(fs::read(s).map_err(|e| file_error("reading the time-lapse", e))?);
+ }
+ Some(procraft_engine::native::TimeLapse {
+ recording: t.recording,
+ quality: t.quality,
+ resolution: t.resolution,
+ hevc: t.hevc,
+ segments,
+ frame_size: (t.width, t.height),
+ fps: t.fps,
+ bitrate: t.bitrate,
+ })
+ }
+ None => None,
+ };
let (doc, pixels, composite, name, info) = {
let mut guard = self.shared.lock();
let st = &mut *guard;
@@ -449,6 +489,7 @@ impl CanvasSession {
strokes: info.strokes,
tracked_seconds: info.tracked_ms as f64 / 1000.0,
seed,
+ time_lapse: time_lapse.as_ref(),
});
write_atomically(Path::new(&path), |f| fs::write(f, &bytes).map_err(|e| file_error("saving", e)))
}
@@ -875,6 +916,39 @@ pub enum PsdOutcome {
Unreadable,
}
+/// The time-lapse the package at `path` keeps, its segments written into the folder `dir` as
+/// `segment-N.mp4` (N from 1, in order); none if it keeps none.
+#[uniffi::export]
+pub fn package_time_lapse(path: String, dir: String) -> Result<Option<PackageTimeLapse>, MobileError> {
+ let bytes = fs::read(&path).map_err(|e| file_error("opening the file", e))?;
+ let Some(t) = procraft_engine::native::read_time_lapse(&bytes)
+ .map_err(|e| file_error("opening the file", format!("{e:?}")))?
+ else {
+ return Ok(None);
+ };
+ let dir = Path::new(&dir);
+ if !t.segments.is_empty() {
+ fs::create_dir_all(dir).map_err(|e| file_error("saving the time-lapse", e))?;
+ }
+ let mut segments = Vec::with_capacity(t.segments.len());
+ for (i, data) in t.segments.iter().enumerate() {
+ let file = dir.join(format!("segment-{}.mp4", i + 1));
+ fs::write(&file, data).map_err(|e| file_error("saving the time-lapse", e))?;
+ segments.push(file.to_string_lossy().into_owned());
+ }
+ Ok(Some(PackageTimeLapse {
+ recording: t.recording,
+ quality: t.quality,
+ resolution: t.resolution,
+ hevc: t.hevc,
+ segments,
+ width: t.frame_size.0,
+ height: t.frame_size.1,
+ fps: t.fps,
+ bitrate: t.bitrate,
+ }))
+}
+
/// Makes the layered PSD (or PSB) at `path` an artwork named `name` in the folder `dir`: its
/// layers, groups and masks, and its flattened picture as the gallery's thumbnail until opened.
#[uniffi::export]
diff --git a/crates/engine/src/native.rs b/crates/engine/src/native.rs
index 61d5cde..dab8219 100644
--- a/crates/engine/src/native.rs
+++ b/crates/engine/src/native.rs
@@ -5,6 +5,9 @@
//! Tiles are `tileSize` squares (smaller along the right and top edges), rows stored bottom-up,
//! pixels premultiplied RGBA (masks: one grey byte). A missing tile is empty. The canvas is stored
//! unturned; `orientation` (1 to 4) says how many quarter turns it is shown at.
+//!
+//! The time-lapse is kept as numbered video segments (`video/segments/segment-N.mp4`, played in
+//! order), described by the archive's segment info and its document's video settings.
use std::sync::Arc;
@@ -21,6 +24,47 @@ pub const EXTENSION: &str = "procreate";
/// The edge of a tile written.
const TILE: u32 = 256;
+/// The names the video settings are kept by: quality Low, Good, Studio and Lossless; resolution
+/// 1080p, 2K and 4K.
+const QUALITY_NAMES: [&str; 4] = ["pro50.vqLow", "pro50.vqGood", "pro50.vqStudio", "pro50.vqLossless"];
+const RESOLUTION_NAMES: [&str; 3] = ["pro50.vr1080", "pro50.vr2K", "pro50.vr4K"];
+/// Where the segments are, and what they are named.
+const SEGMENT_PREFIX: &str = "video/segments/segment-";
+const SEGMENT_EXTENSIONS: [&str; 3] = ["mp4", "mov", "m4v"];
+
+/// A canvas's time-lapse as a package keeps it: whether it records, its video's settings, and the
+/// video recorded so far as segments (MP4s, in order), `frame_size` at `fps`.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub struct TimeLapse {
+ pub recording: bool,
+ /// Low, Good, Studio or Lossless (0 to 3).
+ pub quality: u8,
+ /// 1080p, 2K or 4K (0 to 2).
+ pub resolution: u8,
+ /// HEVC, or H.264.
+ pub hevc: bool,
+ pub segments: Vec<Vec<u8>>,
+ pub frame_size: (u32, u32),
+ pub fps: u32,
+ /// Bits a second.
+ pub bitrate: u64,
+}
+
+impl Default for TimeLapse {
+ fn default() -> TimeLapse {
+ TimeLapse {
+ recording: true,
+ quality: 1,
+ resolution: 0,
+ hevc: false,
+ segments: Vec::new(),
+ frame_size: (0, 0),
+ fps: 30,
+ bitrate: 0,
+ }
+ }
+}
+
/// An artwork read from a package.
pub struct Imported {
pub document: Document,
@@ -384,6 +428,8 @@ pub struct Export<'a> {
pub tracked_seconds: f64,
/// Seeds the layers' UUIDs.
pub seed: u64,
+ /// The time-lapse, if the canvas has one.
+ pub time_lapse: Option<&'a TimeLapse>,
}
/// Writes a package.
@@ -448,58 +494,174 @@ pub fn write(e: &Export<'_>) -> Vec<u8> {
let strokes = b.add(Value::Int(i64::try_from(e.strokes).unwrap_or(i64::MAX)));
let reference_kind = b.string("{0, 0}");
let reference_size = b.string("{350, 532}");
- b.fill(
- &doc_ref,
- &["SilicaDocument", "ValkyrieDocument", "NSObject"],
- vec![
- ("SilicaDocumentArchiveDPIKey", Value::Real(f64::from(doc.dpi))),
- ("SilicaDocumentArchiveUnitKey", Value::Int(0)),
- ("SilicaDocumentTrackedTimeKey", Value::Real(e.tracked_seconds)),
- ("SilicaDocumentVideoPurgedKey", Value::Bool(false)),
- ("animation", animation),
- ("authorName", author),
- ("backgroundColor", background),
- ("backgroundColorHSBA", hsba),
- ("backgroundHidden", Value::Bool(!doc.background_visible)),
- ("colorProfile", profile),
- ("composite", composite),
- ("faceBackgroundHidden", Value::Bool(false)),
- ("featureSet", Value::Int(1)),
- ("flippedHorizontally", Value::Bool(false)),
- ("flippedVertically", Value::Bool(false)),
- ("isFirstItemAnimationForeground", Value::Bool(false)),
- ("isLastItemAnimationBackground", Value::Bool(false)),
- ("layers", layers),
- ("legacyBehaviour", Value::Int(1)),
- ("mask", mask),
- ("name", name),
- ("orientation", Value::Int(1)),
- ("primaryItem", selected.clone()),
- ("reference3D", Value::Bool(false)),
- ("referenceAnchor", Value::Int(0)),
- ("referenceCorner", Value::Int(0)),
- ("referenceDocked", Value::Bool(false)),
- ("referenceKind", reference_kind),
- ("referenceOrientation", Value::Int(0)),
- ("referenceSize", reference_size),
- ("referenceVisibility", Value::Bool(false)),
- ("selectedLayer", selected),
- ("selectedSamplerLayer", Builder::null()),
- ("size", size),
- ("solo", Builder::null()),
- ("strokeCount", strokes),
- ("tileSize", Value::Int(i64::from(TILE))),
- ("unwrappedLayers", unwrapped),
- ("userSavedSelectionMasks", saved_masks),
- ("version", Value::Int(2)),
- ("videoEnabled", Value::Bool(false)),
- ],
- );
+ let video = write_time_lapse(&mut b, &mut zip, e.time_lapse);
+ let mut fields = vec![
+ ("SilicaDocumentArchiveDPIKey", Value::Real(f64::from(doc.dpi))),
+ ("SilicaDocumentArchiveUnitKey", Value::Int(0)),
+ ("SilicaDocumentTrackedTimeKey", Value::Real(e.tracked_seconds)),
+ ("SilicaDocumentVideoPurgedKey", Value::Bool(false)),
+ ("animation", animation),
+ ("authorName", author),
+ ("backgroundColor", background),
+ ("backgroundColorHSBA", hsba),
+ ("backgroundHidden", Value::Bool(!doc.background_visible)),
+ ("colorProfile", profile),
+ ("composite", composite),
+ ("faceBackgroundHidden", Value::Bool(false)),
+ ("featureSet", Value::Int(1)),
+ ("flippedHorizontally", Value::Bool(false)),
+ ("flippedVertically", Value::Bool(false)),
+ ("isFirstItemAnimationForeground", Value::Bool(false)),
+ ("isLastItemAnimationBackground", Value::Bool(false)),
+ ("layers", layers),
+ ("legacyBehaviour", Value::Int(1)),
+ ("mask", mask),
+ ("name", name),
+ ("orientation", Value::Int(1)),
+ ("primaryItem", selected.clone()),
+ ("reference3D", Value::Bool(false)),
+ ("referenceAnchor", Value::Int(0)),
+ ("referenceCorner", Value::Int(0)),
+ ("referenceDocked", Value::Bool(false)),
+ ("referenceKind", reference_kind),
+ ("referenceOrientation", Value::Int(0)),
+ ("referenceSize", reference_size),
+ ("referenceVisibility", Value::Bool(false)),
+ ("selectedLayer", selected),
+ ("selectedSamplerLayer", Builder::null()),
+ ("size", size),
+ ("solo", Builder::null()),
+ ("strokeCount", strokes),
+ ("tileSize", Value::Int(i64::from(TILE))),
+ ("unwrappedLayers", unwrapped),
+ ("userSavedSelectionMasks", saved_masks),
+ ("version", Value::Int(2)),
+ ];
+ fields.extend(video);
+ fields.sort_by(|a, b| a.0.cmp(b.0));
+ b.fill(&doc_ref, &["SilicaDocument", "ValkyrieDocument", "NSObject"], fields);
zip.add("Document.archive", &b.finish(doc_ref));
zip.add("QuickLook/Thumbnail.png", e.thumbnail_png);
zip.finish()
}
+/// The document's time-lapse fields, its segments added to `zip` (stored: video doesn't deflate).
+fn write_time_lapse(b: &mut Builder, zip: &mut ZipWriter, t: Option<&TimeLapse>) -> Vec<(&'static str, Value)> {
+ let Some(t) = t else { return vec![("videoEnabled", Value::Bool(false))] };
+ let quality = QUALITY_NAMES.get(usize::from(t.quality)).unwrap_or(&"pro50.vqLossless");
+ let resolution = RESOLUTION_NAMES.get(usize::from(t.resolution)).unwrap_or(&"pro50.vr4K");
+ // the codecs by number: H.264, HEVC, HEVC with alpha, ProRes 4444
+ let codec = i64::from(t.hevc);
+ let quality_name = b.string(quality);
+ let resolution_name = b.string(resolution);
+ let mut fields = vec![
+ ("videoCodec2020Key", Value::Int(codec)),
+ ("videoCodecKey", Value::Int(codec)),
+ ("videoEnabled", Value::Bool(t.recording)),
+ ("videoQualityKey", quality_name.clone()),
+ ("videoResolutionKey", resolution_name.clone()),
+ ];
+ if !t.segments.is_empty() {
+ for (i, s) in t.segments.iter().enumerate() {
+ zip.add_aligned(&format!("{SEGMENT_PREFIX}{}.mp4", i + 1), s, 1);
+ }
+ let (w, h) = t.frame_size;
+ let size = b.string(&format!("{{{w}, {h}}}"));
+ let bitrate = b.add(Value::Int(i64::try_from(t.bitrate).unwrap_or(i64::MAX)));
+ let info = b.object(
+ &["VideoSegmentInfo", "NSObject"],
+ vec![
+ ("bitrate", bitrate),
+ ("codec", Value::Int(codec)),
+ ("codec2020", Value::Int(codec)),
+ ("colorSpace", Value::Int(0)),
+ ("frameSize", size),
+ ("framesPerSecond", Value::Int(i64::from(t.fps))),
+ ("qualityPreferenceKey", quality_name),
+ ("resolutionPreferenceKey", resolution_name),
+ ("sourceOrientation", Value::Int(1)),
+ ],
+ );
+ fields.push(("SilicaDocumentVideoSegmentInfoKey", info));
+ }
+ fields
+}
+
+/// The time-lapse a package keeps, or none if it keeps none (no recording setting and no video).
+pub fn read_time_lapse(bytes: &[u8]) -> Result<Option<TimeLapse>, ReadError> {
+ let zip = ZipReader::new(bytes).ok_or(ReadError::NotAPackage)?;
+ let archive = Archive::read(&zip.read("Document.archive").ok_or(ReadError::NotAPackage)?)
+ .ok_or(ReadError::Damaged("document"))?;
+ let root = archive.root().ok_or(ReadError::Damaged("document"))?;
+ let mut numbered: Vec<(u64, String)> =
+ zip.names().filter_map(|n| segment_number(n).map(|k| (k, n.to_owned()))).collect();
+ numbered.sort();
+ let segments: Vec<Vec<u8>> = numbered.iter().filter_map(|(_, n)| zip.read(n)).collect();
+ let recording = root.bool("videoEnabled");
+ if recording.is_none() && segments.is_empty() {
+ return Ok(None);
+ }
+ let info = root.object("SilicaDocumentVideoSegmentInfoKey");
+ let named = |key: &str, of_info: &str| root.string(key).or_else(|| info.as_ref().and_then(|i| i.string(of_info)));
+ let codec = root
+ .int("videoCodec2020Key")
+ .or_else(|| root.int("videoCodecKey"))
+ .or_else(|| info.as_ref().and_then(|i| i.int("codec2020").or_else(|| i.int("codec"))));
+ let d = TimeLapse::default();
+ Ok(Some(TimeLapse {
+ recording: recording.unwrap_or(d.recording),
+ quality: named("videoQualityKey", "qualityPreferenceKey")
+ .as_deref()
+ .and_then(quality_number)
+ .unwrap_or(d.quality),
+ resolution: named("videoResolutionKey", "resolutionPreferenceKey")
+ .as_deref()
+ .and_then(resolution_number)
+ .unwrap_or(d.resolution),
+ // HEVC, with or without alpha
+ hevc: codec.map_or(d.hevc, |c| c == 1 || c == 2),
+ frame_size: info.as_ref().and_then(|i| i.string("frameSize")).and_then(|s| parse_size(&s)).unwrap_or((0, 0)),
+ fps: info
+ .as_ref()
+ .and_then(|i| i.int("framesPerSecond"))
+ .and_then(|f| u32::try_from(f).ok())
+ .filter(|f| (1..=240).contains(f))
+ .unwrap_or(d.fps),
+ bitrate: info.as_ref().and_then(|i| i.int("bitrate")).and_then(|b| u64::try_from(b).ok()).unwrap_or(0),
+ segments,
+ }))
+}
+
+/// A segment's number from its entry's name.
+fn segment_number(name: &str) -> Option<u64> {
+ let (n, ext) = name.strip_prefix(SEGMENT_PREFIX)?.split_once('.')?;
+ if !SEGMENT_EXTENSIONS.iter().any(|e| ext.eq_ignore_ascii_case(e)) {
+ return None;
+ }
+ n.parse().ok()
+}
+
+/// A quality's number from its name, older names too.
+fn quality_number(name: &str) -> Option<u8> {
+ match name {
+ "vqNormal" | "pro50.vqLow" => Some(0),
+ "vqHigh" | "pro50.vqGood" => Some(1),
+ "vqStudio" | "pro50.vqStudio" => Some(2),
+ "vqStudio4K" | "pro50.vqLossless" => Some(3),
+ _ => None,
+ }
+}
+
+/// A resolution's number from its name; the older quality names stood for one too.
+fn resolution_number(name: &str) -> Option<u8> {
+ match name {
+ "pro50.vr1080" | "vqNormal" | "vqHigh" | "vqStudio" => Some(0),
+ "pro50.vr2K" => Some(1),
+ "pro50.vr4K" | "vqStudio4K" => Some(2),
+ _ => None,
+ }
+}
+
/// The ids of what is directly in `group` (or at the top), top first; masks left out.
fn top_first(doc: &Document, group: Option<LayerId>) -> Vec<LayerId> {
doc.layers().iter().rev().filter(|l| l.kind != LayerKind::Mask && l.parent == group).map(|l| l.id).collect()
@@ -830,6 +992,7 @@ mod tests {
strokes: 7,
tracked_seconds: 12.5,
seed: 42,
+ time_lapse: None,
});
let back = read(&bytes).unwrap();
@@ -867,6 +1030,7 @@ mod tests {
strokes: 0,
tracked_seconds: 0.0,
seed: 1,
+ time_lapse: None,
}))
.unwrap()
.document
@@ -929,6 +1093,7 @@ mod tests {
strokes: 0,
tracked_seconds: 0.0,
seed: 3,
+ time_lapse: None,
});
// the picture's 6 tiles, the mask's one, none for the clear layer
let zip = ZipReader::new(&bytes).unwrap();
@@ -971,6 +1136,7 @@ mod tests {
strokes: 0,
tracked_seconds: 0.0,
seed: 1,
+ time_lapse: None,
});
let back = read(&bytes).unwrap();
let d = &back.document;
@@ -1010,6 +1176,7 @@ mod tests {
strokes: 0,
tracked_seconds: 0.0,
seed: 1,
+ time_lapse: None,
});
let zip = ZipReader::new(&bytes).unwrap();
let archive = Archive::read(&zip.read("Document.archive").unwrap()).unwrap();
@@ -1035,6 +1202,67 @@ mod tests {
assert_eq!(parse_size("1488x2266"), None);
}
+ #[test]
+ fn the_time_lapse_round_trips() {
+ let doc = Document::new(16, 16);
+ let id = doc.layers().first().map(|l| l.id).unwrap();
+ let pixels = HashMap::from([(id, vec![0u8; 16 * 16 * 4])]);
+ let export = |t: Option<&TimeLapse>| {
+ write(&Export {
+ document: &doc,
+ pixels: &pixels,
+ composite: &[0; 16 * 16 * 4],
+ thumbnail_png: &[],
+ name: "",
+ author: "",
+ strokes: 0,
+ tracked_seconds: 0.0,
+ seed: 1,
+ time_lapse: t,
+ })
+ };
+ let t = TimeLapse {
+ recording: false,
+ quality: 2,
+ resolution: 1,
+ hevc: true,
+ segments: vec![b"first".to_vec(), b"second".to_vec()],
+ frame_size: (1920, 1080),
+ fps: 30,
+ bitrate: 8_000_000,
+ };
+ let bytes = export(Some(&t));
+ let zip = ZipReader::new(&bytes).unwrap();
+ assert_eq!(zip.read("video/segments/segment-1.mp4").as_deref(), Some(&b"first"[..]));
+ let archive = Archive::read(&zip.read("Document.archive").unwrap()).unwrap();
+ let root = archive.root().unwrap();
+ assert_eq!(root.string("videoQualityKey").as_deref(), Some("pro50.vqStudio"));
+ let info = root.object("SilicaDocumentVideoSegmentInfoKey").unwrap();
+ assert_eq!(info.string("resolutionPreferenceKey").as_deref(), Some("pro50.vr2K"));
+ assert_eq!(info.int("bitrate"), Some(8_000_000));
+ assert_eq!(read_time_lapse(&bytes).unwrap(), Some(t));
+ // the layers read as before
+ assert!(read(&bytes).is_ok());
+
+ // none: not recording, and nothing kept
+ let none = read_time_lapse(&export(None)).unwrap().unwrap();
+ assert!(!none.recording && none.segments.is_empty());
+ let mut z = ZipWriter::new();
+ let mut b = Builder::new();
+ let root = b.object(&["SilicaDocument", "NSObject"], vec![]);
+ z.add("Document.archive", &b.finish(root));
+ assert_eq!(read_time_lapse(&z.finish()), Ok(None));
+ }
+
+ #[test]
+ fn segments_are_ordered_by_number_and_older_names_read() {
+ assert_eq!(segment_number("video/segments/segment-12.mov"), Some(12));
+ assert_eq!(segment_number("video/segments/segment-3.txt"), None);
+ assert_eq!(segment_number("video/segment-3.mp4"), None);
+ assert_eq!(quality_number("vqHigh"), Some(1));
+ assert_eq!(resolution_number("vqStudio4K"), Some(2));
+ }
+
#[test]
fn what_isnt_a_package_is_refused() {
assert_eq!(read(b"hello").err(), Some(ReadError::NotAPackage));
@@ -1096,6 +1324,7 @@ mod tests {
strokes: back.strokes,
tracked_seconds: back.tracked_seconds,
seed: 7,
+ time_lapse: None,
});
std::fs::write(out, package).unwrap();
}