react-native-image-stitcher 0.8.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +269 -0
- package/android/build.gradle +10 -0
- package/android/src/main/java/io/imagestitcher/rn/IncrementalStitcher.kt +115 -10
- package/android/src/main/java/io/imagestitcher/rn/RNImageStitcherPackage.kt +17 -3
- package/android/src/main/java/io/imagestitcher/rn/SaveFrameAsJpegPlugin.kt +162 -0
- package/cpp/stitcher_worklet_registry.cpp +10 -0
- package/cpp/stitcher_worklet_registry.hpp +10 -0
- package/cpp/tests/CMakeLists.txt +98 -0
- package/cpp/tests/README.md +86 -0
- package/cpp/tests/pose_test.cpp +74 -0
- package/cpp/tests/stitcher_frame_data_test.cpp +132 -0
- package/cpp/tests/stitcher_worklet_registry_test.cpp +195 -0
- package/cpp/tests/stubs/jsi/jsi.h +33 -0
- package/cpp/tests/stubs/react-native-worklets-core/WKTJsiWorklet.h +34 -0
- package/dist/camera/useCapture.d.ts +1 -1
- package/dist/camera/useCapture.js +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.js +20 -1
- package/dist/stitching/incremental.d.ts +41 -0
- package/dist/stitching/useFrameStream.d.ts +34 -0
- package/dist/stitching/useFrameStream.js +234 -0
- package/dist/stitching/useThrottledFrameProcessor.d.ts +33 -0
- package/dist/stitching/useThrottledFrameProcessor.js +132 -0
- package/dist/types.d.ts +87 -0
- package/ios/Sources/RNImageStitcher/IncrementalStitcher.swift +138 -9
- package/ios/Sources/RNImageStitcher/IncrementalStitcherBridge.swift +50 -14
- package/ios/Sources/RNImageStitcher/SaveFrameAsJpegPlugin.mm +185 -0
- package/package.json +1 -1
- package/src/camera/useCapture.ts +1 -1
- package/src/index.ts +19 -0
- package/src/stitching/__tests__/subscribeIncrementalState.refine.test.ts +276 -0
- package/src/stitching/__tests__/useThrottledFrameProcessor.test.ts +178 -0
- package/src/stitching/incremental.ts +42 -0
- package/src/stitching/useFrameStream.ts +271 -0
- package/src/stitching/useThrottledFrameProcessor.ts +145 -0
- package/src/types.ts +95 -0
|
@@ -1863,12 +1863,27 @@ public final class IncrementalStitcher: NSObject {
|
|
|
1863
1863
|
config: [String: Any],
|
|
1864
1864
|
completion: @escaping ([String: Any]?, NSError?) -> Void
|
|
1865
1865
|
) {
|
|
1866
|
+
// v0.10.0 #15A — emit `validating` at the very top so JS sees
|
|
1867
|
+
// refine activity even when validation fails fast. Frames may
|
|
1868
|
+
// be empty here; report whatever the caller asked for.
|
|
1869
|
+
emitRefineProgress(
|
|
1870
|
+
stage: "validating",
|
|
1871
|
+
fraction: 0.05,
|
|
1872
|
+
frames: framePaths.count,
|
|
1873
|
+
errorMessage: nil
|
|
1874
|
+
)
|
|
1866
1875
|
guard framePaths.count >= 2 else {
|
|
1876
|
+
let msg = "refinePanorama requires at least 2 framePaths (got \(framePaths.count))."
|
|
1877
|
+
emitRefineProgress(
|
|
1878
|
+
stage: "error",
|
|
1879
|
+
fraction: 1.0,
|
|
1880
|
+
frames: framePaths.count,
|
|
1881
|
+
errorMessage: msg
|
|
1882
|
+
)
|
|
1867
1883
|
completion(nil, NSError(
|
|
1868
1884
|
domain: "RNImageStitcherIncremental",
|
|
1869
1885
|
code: 9101,
|
|
1870
|
-
userInfo: [NSLocalizedDescriptionKey:
|
|
1871
|
-
"refinePanorama requires at least 2 framePaths (got \(framePaths.count))."]
|
|
1886
|
+
userInfo: [NSLocalizedDescriptionKey: msg]
|
|
1872
1887
|
))
|
|
1873
1888
|
return
|
|
1874
1889
|
}
|
|
@@ -1876,11 +1891,17 @@ public final class IncrementalStitcher: NSObject {
|
|
|
1876
1891
|
for p in framePaths {
|
|
1877
1892
|
let cleaned = p.hasPrefix("file://") ? String(p.dropFirst(7)) : p
|
|
1878
1893
|
if !fm.fileExists(atPath: cleaned) {
|
|
1894
|
+
let msg = "refinePanorama: keyframe missing on disk — \(cleaned)"
|
|
1895
|
+
emitRefineProgress(
|
|
1896
|
+
stage: "error",
|
|
1897
|
+
fraction: 1.0,
|
|
1898
|
+
frames: framePaths.count,
|
|
1899
|
+
errorMessage: msg
|
|
1900
|
+
)
|
|
1879
1901
|
completion(nil, NSError(
|
|
1880
1902
|
domain: "RNImageStitcherIncremental",
|
|
1881
1903
|
code: 9102,
|
|
1882
|
-
userInfo: [NSLocalizedDescriptionKey:
|
|
1883
|
-
"refinePanorama: keyframe missing on disk — \(cleaned)"]
|
|
1904
|
+
userInfo: [NSLocalizedDescriptionKey: msg]
|
|
1884
1905
|
))
|
|
1885
1906
|
return
|
|
1886
1907
|
}
|
|
@@ -1912,7 +1933,18 @@ public final class IncrementalStitcher: NSObject {
|
|
|
1912
1933
|
cleanedOutput,
|
|
1913
1934
|
warper, blender, seam)
|
|
1914
1935
|
|
|
1915
|
-
|
|
1936
|
+
// v0.10.0 #15A — capture for the inner closure; `self` is
|
|
1937
|
+
// captured weakly so the progress emitter survives only as
|
|
1938
|
+
// long as the IncrementalStitcher itself does. An emitter
|
|
1939
|
+
// call on a torn instance is a no-op via `?.`.
|
|
1940
|
+
let frameCount = framePaths.count
|
|
1941
|
+
refineQueue.async { [weak self] in
|
|
1942
|
+
self?.emitRefineProgress(
|
|
1943
|
+
stage: "stitching",
|
|
1944
|
+
fraction: 0.1,
|
|
1945
|
+
frames: frameCount,
|
|
1946
|
+
errorMessage: nil
|
|
1947
|
+
)
|
|
1916
1948
|
// C2-style: closure captures only value-typed locals
|
|
1917
1949
|
// (paths, output path, config strings). No `self` access
|
|
1918
1950
|
// is needed for the cv::Stitcher call — OpenCVStitcher is
|
|
@@ -1935,24 +1967,53 @@ public final class IncrementalStitcher: NSObject {
|
|
|
1935
1967
|
// OpenCVStitcher hit one of its six guarded failure
|
|
1936
1968
|
// returns; surface as a clean NSError.
|
|
1937
1969
|
if r.width == 0 && r.height == 0 {
|
|
1970
|
+
let msg = "refinePanorama: stitcher returned sentinel — see preceding [BatchStitcher] log for cause."
|
|
1971
|
+
self?.emitRefineProgress(
|
|
1972
|
+
stage: "error",
|
|
1973
|
+
fraction: 1.0,
|
|
1974
|
+
frames: frameCount,
|
|
1975
|
+
errorMessage: msg
|
|
1976
|
+
)
|
|
1938
1977
|
completion(nil, NSError(
|
|
1939
1978
|
domain: "RNImageStitcherIncremental",
|
|
1940
1979
|
code: 9107,
|
|
1941
|
-
userInfo: [NSLocalizedDescriptionKey:
|
|
1942
|
-
"refinePanorama: stitcher returned sentinel — see preceding [BatchStitcher] log for cause."]
|
|
1980
|
+
userInfo: [NSLocalizedDescriptionKey: msg]
|
|
1943
1981
|
))
|
|
1944
1982
|
return
|
|
1945
1983
|
}
|
|
1984
|
+
// Stitch succeeded — OpenCVStitcher writes the JPEG
|
|
1985
|
+
// internally before returning, so "writing" really
|
|
1986
|
+
// captures the final assembly + file I/O cost. Emit
|
|
1987
|
+
// here so JS can flip its label from "Stitching" to
|
|
1988
|
+
// "Writing" before the done event fires.
|
|
1989
|
+
self?.emitRefineProgress(
|
|
1990
|
+
stage: "writing",
|
|
1991
|
+
fraction: 0.9,
|
|
1992
|
+
frames: frameCount,
|
|
1993
|
+
errorMessage: nil
|
|
1994
|
+
)
|
|
1995
|
+
self?.emitRefineProgress(
|
|
1996
|
+
stage: "done",
|
|
1997
|
+
fraction: 1.0,
|
|
1998
|
+
frames: frameCount,
|
|
1999
|
+
errorMessage: nil
|
|
2000
|
+
)
|
|
1946
2001
|
completion([
|
|
1947
2002
|
"panoramaPath": r.outputPath,
|
|
1948
2003
|
"width": Int(r.width),
|
|
1949
2004
|
"height": Int(r.height),
|
|
1950
|
-
"framesRequested":
|
|
1951
|
-
"framesIncluded":
|
|
2005
|
+
"framesRequested": frameCount,
|
|
2006
|
+
"framesIncluded": frameCount,
|
|
1952
2007
|
"framesDropped": 0,
|
|
1953
2008
|
"finalConfidenceThresh": -1.0,
|
|
1954
2009
|
], nil)
|
|
1955
2010
|
} catch let err as NSError {
|
|
2011
|
+
self?.emitRefineProgress(
|
|
2012
|
+
stage: "error",
|
|
2013
|
+
fraction: 1.0,
|
|
2014
|
+
frames: frameCount,
|
|
2015
|
+
errorMessage: err.localizedDescription
|
|
2016
|
+
)
|
|
1956
2017
|
completion(nil, err)
|
|
1957
2018
|
}
|
|
1958
2019
|
}
|
|
@@ -2092,6 +2153,74 @@ public final class IncrementalStitcher: NSObject {
|
|
|
2092
2153
|
)
|
|
2093
2154
|
}
|
|
2094
2155
|
|
|
2156
|
+
/// v0.10.0 #15A — emit a refine-pipeline phase update on the same
|
|
2157
|
+
/// `IncrementalStateUpdate` channel that carries `isRefining` /
|
|
2158
|
+
/// `refinedPanoramaPath`. Five `stage` values fire across the
|
|
2159
|
+
/// lifetime of one `refinePanorama` call:
|
|
2160
|
+
///
|
|
2161
|
+
/// - `validating` (fraction 0.05) — synchronous input checks
|
|
2162
|
+
/// - `stitching` (fraction 0.10) — start of the OpenCV stitch
|
|
2163
|
+
/// - `writing` (fraction 0.90) — stitch returned, JPEG written
|
|
2164
|
+
/// - `done` (fraction 1.00) — completion handler invoked
|
|
2165
|
+
/// - `error` (fraction 1.00) — failure path (`errorMessage`
|
|
2166
|
+
/// is non-nil)
|
|
2167
|
+
///
|
|
2168
|
+
/// `fraction` is intentionally coarse: OpenCV's `Stitcher` doesn't
|
|
2169
|
+
/// expose stage-by-stage callbacks, so the 0.10 → 0.90 jump is a
|
|
2170
|
+
/// single opaque step. JS uses the `stage` string for the UI
|
|
2171
|
+
/// label and `fraction` purely for spinner progress.
|
|
2172
|
+
///
|
|
2173
|
+
/// Reuses the existing channel (rather than introducing a new
|
|
2174
|
+
/// device-event name) so the JS subscriber doesn't need to wire
|
|
2175
|
+
/// a second listener. The payload carries the same skeleton
|
|
2176
|
+
/// `emitRefinementState` emits (lastState fields preserved) so
|
|
2177
|
+
/// `isRefining` / `refinedPanoramaPath` sticky-merge logic on the
|
|
2178
|
+
/// JS side keeps working untouched.
|
|
2179
|
+
private func emitRefineProgress(
|
|
2180
|
+
stage: String,
|
|
2181
|
+
fraction: Double,
|
|
2182
|
+
frames: Int?,
|
|
2183
|
+
errorMessage: String?
|
|
2184
|
+
) {
|
|
2185
|
+
// Disk-trail breadcrumb — every refine emit lands here so a
|
|
2186
|
+
// future regression can be diagnosed by pulling the bridge's
|
|
2187
|
+
// debug file without needing live Console.app access.
|
|
2188
|
+
IncrementalStitcher.fileLog(
|
|
2189
|
+
"[refine.progress] stage=\(stage) frac=\(fraction) frames=\(frames ?? -1) hasError=\(errorMessage != nil)"
|
|
2190
|
+
)
|
|
2191
|
+
stateLock.lock()
|
|
2192
|
+
let prev = self.lastState
|
|
2193
|
+
stateLock.unlock()
|
|
2194
|
+
let state = IncrementalStateObject(
|
|
2195
|
+
panoramaPath: prev?.panoramaPath,
|
|
2196
|
+
width: prev?.width ?? 0,
|
|
2197
|
+
height: prev?.height ?? 0,
|
|
2198
|
+
acceptedCount: prev?.acceptedCount ?? 0,
|
|
2199
|
+
outcome: prev?.outcome ?? .acceptedHigh,
|
|
2200
|
+
confidence: prev?.confidence ?? 1.0,
|
|
2201
|
+
overlapPercent: prev?.overlapPercent ?? -1.0,
|
|
2202
|
+
processingMs: 0,
|
|
2203
|
+
isLandscape: prev?.isLandscape ?? false,
|
|
2204
|
+
paintedExtent: prev?.paintedExtent ?? 0,
|
|
2205
|
+
panExtent: prev?.panExtent ?? 0,
|
|
2206
|
+
keyframeMax: prev?.keyframeMax ?? 0
|
|
2207
|
+
)
|
|
2208
|
+
var dict = state.asDictionary()
|
|
2209
|
+
dict["refineStage"] = stage
|
|
2210
|
+
dict["refineProgress"] = fraction
|
|
2211
|
+
if let f = frames {
|
|
2212
|
+
dict["refineFrames"] = f
|
|
2213
|
+
}
|
|
2214
|
+
if let e = errorMessage {
|
|
2215
|
+
dict["refineError"] = e
|
|
2216
|
+
}
|
|
2217
|
+
NotificationCenter.default.post(
|
|
2218
|
+
name: .retailensIncrementalStateUpdate,
|
|
2219
|
+
object: nil,
|
|
2220
|
+
userInfo: dict
|
|
2221
|
+
)
|
|
2222
|
+
}
|
|
2223
|
+
|
|
2095
2224
|
/// Cancel an in-progress capture without producing output.
|
|
2096
2225
|
/// Same V12.1 synchronous-stop pattern as finalize.
|
|
2097
2226
|
@objc public func cancel() {
|
|
@@ -33,6 +33,18 @@ public final class IncrementalStitcherBridge: RCTEventEmitter {
|
|
|
33
33
|
|
|
34
34
|
public override init() {
|
|
35
35
|
super.init()
|
|
36
|
+
// Under RN bridgeless interop the bridge's init() can be
|
|
37
|
+
// invoked twice on the same instance (observed via identical
|
|
38
|
+
// instance pointers firing the observer selector twice per
|
|
39
|
+
// notification). Defensively remove any prior registration
|
|
40
|
+
// for this notification name before adding one, so the
|
|
41
|
+
// observer can only fire once per post regardless of how
|
|
42
|
+
// many times init runs.
|
|
43
|
+
NotificationCenter.default.removeObserver(
|
|
44
|
+
self,
|
|
45
|
+
name: .retailensIncrementalStateUpdate,
|
|
46
|
+
object: nil
|
|
47
|
+
)
|
|
36
48
|
// Subscribe once at construction. The handler self-checks
|
|
37
49
|
// `hasListeners` before forwarding, so we don't have to
|
|
38
50
|
// unsubscribe / resubscribe on every JS listener attach/detach.
|
|
@@ -58,9 +70,6 @@ public final class IncrementalStitcherBridge: RCTEventEmitter {
|
|
|
58
70
|
return [Self.stateUpdateEvent]
|
|
59
71
|
}
|
|
60
72
|
|
|
61
|
-
// (startObserving / stopObserving moved next to handleStateUpdate
|
|
62
|
-
// for the PiP investigation; remove this comment after.)
|
|
63
|
-
|
|
64
73
|
// MARK: - Module methods
|
|
65
74
|
|
|
66
75
|
/// `options` (all optional, sensible defaults documented in
|
|
@@ -477,26 +486,53 @@ public final class IncrementalStitcherBridge: RCTEventEmitter {
|
|
|
477
486
|
|
|
478
487
|
@objc private func handleStateUpdate(_ notification: Notification) {
|
|
479
488
|
let hasPath = (notification.userInfo?["panoramaPath"] != nil)
|
|
480
|
-
|
|
489
|
+
let refineStage = notification.userInfo?["refineStage"] as? String
|
|
490
|
+
if hasPath || refineStage != nil {
|
|
481
491
|
IncrementalStitcher.fileLog(
|
|
482
|
-
"bridge handleStateUpdate hasListeners=\(hasListeners) hasPath=\(hasPath) thread=\(Thread.isMainThread ? "main" : "bg")"
|
|
492
|
+
"bridge handleStateUpdate hasListeners=\(hasListeners) hasPath=\(hasPath) refineStage=\(refineStage ?? "nil") thread=\(Thread.isMainThread ? "main" : "bg")"
|
|
483
493
|
)
|
|
484
494
|
}
|
|
485
|
-
guard hasListeners else {
|
|
495
|
+
guard hasListeners else {
|
|
496
|
+
if let stage = refineStage {
|
|
497
|
+
IncrementalStitcher.fileLog(
|
|
498
|
+
"bridge handleStateUpdate DROPPED refineStage=\(stage) — hasListeners=false"
|
|
499
|
+
)
|
|
500
|
+
}
|
|
501
|
+
return
|
|
502
|
+
}
|
|
486
503
|
guard let userInfo = notification.userInfo else { return }
|
|
487
|
-
//
|
|
488
|
-
//
|
|
489
|
-
//
|
|
490
|
-
//
|
|
491
|
-
//
|
|
504
|
+
// We deliver via `bridge.enqueueJSCall("RCTDeviceEventEmitter", "emit", ...)`
|
|
505
|
+
// rather than `RCTEventEmitter.sendEvent(...)` because under RN
|
|
506
|
+
// bridgeless interop `sendEvent` silently no-ops for some
|
|
507
|
+
// event-body shapes even when `_bridge` is non-nil and
|
|
508
|
+
// `_listenerCount > 0` (confirmed via os_log instrumentation
|
|
509
|
+
// during v0.10.0 PR B development — refine events with
|
|
510
|
+
// refineStage/refineProgress/refineFrames were not reaching
|
|
511
|
+
// any JS subscriber while live state events with a smaller
|
|
512
|
+
// body shape on the same channel were). enqueueJSCall is
|
|
513
|
+
// the underlying mechanism sendEvent uses in Paper mode, so
|
|
514
|
+
// it is strictly at least as well-supported.
|
|
492
515
|
DispatchQueue.main.async { [weak self] in
|
|
493
516
|
guard let self = self else { return }
|
|
494
|
-
if hasPath {
|
|
517
|
+
if hasPath || refineStage != nil {
|
|
495
518
|
IncrementalStitcher.fileLog(
|
|
496
|
-
"bridge
|
|
519
|
+
"bridge enqueueJSCall (main queue) body.panoramaPath=\(userInfo["panoramaPath"] ?? "MISSING") refineStage=\(refineStage ?? "nil")"
|
|
497
520
|
)
|
|
498
521
|
}
|
|
499
|
-
self.
|
|
522
|
+
guard let bridge = self.bridge else {
|
|
523
|
+
if hasPath || refineStage != nil {
|
|
524
|
+
IncrementalStitcher.fileLog(
|
|
525
|
+
"bridge enqueueJSCall DROPPED — self.bridge is nil"
|
|
526
|
+
)
|
|
527
|
+
}
|
|
528
|
+
return
|
|
529
|
+
}
|
|
530
|
+
bridge.enqueueJSCall(
|
|
531
|
+
"RCTDeviceEventEmitter",
|
|
532
|
+
method: "emit",
|
|
533
|
+
args: [Self.stateUpdateEvent, userInfo],
|
|
534
|
+
completion: nil
|
|
535
|
+
)
|
|
500
536
|
}
|
|
501
537
|
}
|
|
502
538
|
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
//
|
|
3
|
+
// SaveFrameAsJpegPlugin.mm — v0.9.0 Layer 1: vc Frame Processor plugin
|
|
4
|
+
// that JPEG-encodes the supplied frame's pixel buffer to a host-
|
|
5
|
+
// supplied path. Worklet-callable; thin wrapper around the standard
|
|
6
|
+
// iOS CIImage → CGImage → UIImage → UIImageJPEGRepresentation path.
|
|
7
|
+
//
|
|
8
|
+
// JS-side usage (from a worklet — typically inside `useFrameStream`
|
|
9
|
+
// (Layer 3) or directly from a custom `useFrameProcessor` body):
|
|
10
|
+
//
|
|
11
|
+
// const plugin = VisionCameraProxy.initFrameProcessorPlugin(
|
|
12
|
+
// 'save_frame_as_jpeg', {},
|
|
13
|
+
// );
|
|
14
|
+
//
|
|
15
|
+
// const fp = useFrameProcessor((frame) => {
|
|
16
|
+
// 'worklet';
|
|
17
|
+
// if (plugin == null) return;
|
|
18
|
+
// const result = plugin.call(frame, {
|
|
19
|
+
// path: '/path/to/output.jpg',
|
|
20
|
+
// quality: 75, // 0-100; defaults to 75
|
|
21
|
+
// });
|
|
22
|
+
// // result: { ok: true, path, width, height } OR
|
|
23
|
+
// // { ok: false, error: "..." }
|
|
24
|
+
// }, [plugin]);
|
|
25
|
+
//
|
|
26
|
+
// ## Why a separate plugin (not folded into KeyframeGateFrameProcessor)
|
|
27
|
+
//
|
|
28
|
+
// `cv_flow_gate_process_frame` (the existing plugin) drives the lib's
|
|
29
|
+
// FIRST-PARTY stitching pipeline: it consumes the frame, evaluates
|
|
30
|
+
// the keyframe gate, dispatches into `IncrementalStitcher`. It owns
|
|
31
|
+
// state.
|
|
32
|
+
//
|
|
33
|
+
// `save_frame_as_jpeg` is STATELESS — a pure encode-and-write function.
|
|
34
|
+
// Mixing them would force every JS-side caller of either to pay both
|
|
35
|
+
// codepaths' arg-parsing costs (and would confuse the use-case
|
|
36
|
+
// boundary). Two plugins, one job each.
|
|
37
|
+
//
|
|
38
|
+
// ## CONDITIONAL COMPILATION
|
|
39
|
+
//
|
|
40
|
+
// Same `__has_include` guard as `KeyframeGateFrameProcessor.mm` — if
|
|
41
|
+
// vision-camera isn't on the host's classpath, this file is a no-op
|
|
42
|
+
// translation unit. See that file's header for the rationale.
|
|
43
|
+
|
|
44
|
+
#import <Foundation/Foundation.h>
|
|
45
|
+
|
|
46
|
+
#if __has_include(<VisionCamera/FrameProcessorPlugin.h>)
|
|
47
|
+
|
|
48
|
+
#import <VisionCamera/Frame.h>
|
|
49
|
+
#import <VisionCamera/FrameProcessorPlugin.h>
|
|
50
|
+
#import <VisionCamera/FrameProcessorPluginRegistry.h>
|
|
51
|
+
#import <VisionCamera/VisionCameraProxyHolder.h>
|
|
52
|
+
#import <CoreVideo/CoreVideo.h>
|
|
53
|
+
#import <CoreImage/CoreImage.h>
|
|
54
|
+
#import <UIKit/UIKit.h>
|
|
55
|
+
|
|
56
|
+
@interface SaveFrameAsJpegPlugin : FrameProcessorPlugin
|
|
57
|
+
@end
|
|
58
|
+
|
|
59
|
+
@implementation SaveFrameAsJpegPlugin
|
|
60
|
+
|
|
61
|
+
- (instancetype)initWithProxy:(VisionCameraProxyHolder*)proxy
|
|
62
|
+
withOptions:(NSDictionary* _Nullable)options {
|
|
63
|
+
return [super initWithProxy:proxy withOptions:options];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Helper: read a string arg with a fallback. Returns nil only when
|
|
67
|
+
// the arg is missing AND no fallback was supplied.
|
|
68
|
+
static NSString* sfj_argString(NSDictionary* args, NSString* key,
|
|
69
|
+
NSString* _Nullable fallback) {
|
|
70
|
+
id v = args[key];
|
|
71
|
+
if ([v isKindOfClass:[NSString class]]) return (NSString*)v;
|
|
72
|
+
return fallback;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Helper: read a numeric arg (NSNumber or NSString-parseable) with a
|
|
76
|
+
// fallback. Matches the pattern in KeyframeGateFrameProcessor.mm.
|
|
77
|
+
static double sfj_argDouble(NSDictionary* args, NSString* key,
|
|
78
|
+
double fallback) {
|
|
79
|
+
id v = args[key];
|
|
80
|
+
if ([v isKindOfClass:[NSNumber class]]) return [(NSNumber*)v doubleValue];
|
|
81
|
+
if ([v isKindOfClass:[NSString class]]) return [(NSString*)v doubleValue];
|
|
82
|
+
return fallback;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// The host-callable plugin entry point. vc dispatches each
|
|
86
|
+
// `plugin.call(frame, args)` from a worklet here.
|
|
87
|
+
//
|
|
88
|
+
// ## Arguments
|
|
89
|
+
//
|
|
90
|
+
// - `path` (string, REQUIRED): absolute filesystem path to write
|
|
91
|
+
// the JPEG to. Parent directory must exist (we don't `mkdir -p`).
|
|
92
|
+
// Existing file is overwritten atomically.
|
|
93
|
+
// - `quality` (number, optional): 0-100 JPEG quality. Default 75
|
|
94
|
+
// (matches `KeyframeGate.onAccept`'s encoder). Clamped silently
|
|
95
|
+
// to `[1, 100]`.
|
|
96
|
+
//
|
|
97
|
+
// ## Returns
|
|
98
|
+
//
|
|
99
|
+
// - On success: `{ ok: YES, path: <path>, width: <px>, height: <px> }`
|
|
100
|
+
// - On failure: `{ ok: NO, error: "<reason>" }`
|
|
101
|
+
//
|
|
102
|
+
// Errors are surfaced via the result dict, NOT thrown as `JSError` —
|
|
103
|
+
// host worklets that want to react to encoder failures (e.g., to
|
|
104
|
+
// rotate slot paths, or to back off) can branch on `result.ok`
|
|
105
|
+
// without try/catch boilerplate. Throwing would break the
|
|
106
|
+
// Layer 3 `useFrameStream` flow which only sees the result.
|
|
107
|
+
- (id)callback:(Frame*)frame withArguments:(NSDictionary*)arguments {
|
|
108
|
+
NSString* path = sfj_argString(arguments, @"path", nil);
|
|
109
|
+
if (path == nil) {
|
|
110
|
+
return @{@"ok": @NO, @"error": @"missing required `path` argument"};
|
|
111
|
+
}
|
|
112
|
+
double q = sfj_argDouble(arguments, @"quality", 75.0);
|
|
113
|
+
if (q < 1.0) q = 1.0;
|
|
114
|
+
if (q > 100.0) q = 100.0;
|
|
115
|
+
|
|
116
|
+
CMSampleBufferRef sampleBuffer = frame.buffer;
|
|
117
|
+
if (sampleBuffer == NULL) {
|
|
118
|
+
return @{@"ok": @NO, @"error": @"frame.buffer was NULL"};
|
|
119
|
+
}
|
|
120
|
+
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
|
|
121
|
+
if (pixelBuffer == NULL) {
|
|
122
|
+
return @{@"ok": @NO, @"error": @"CMSampleBufferGetImageBuffer returned NULL"};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// CIImage → CGImage → UIImage → JPEG. Standard iOS path; the
|
|
126
|
+
// CIContext + colorSpace are cheap to construct per-call (CoreImage
|
|
127
|
+
// caches GPU resources internally). If profiling shows this in
|
|
128
|
+
// the hot path, lift the context to a static; for v0.9.0 baseline,
|
|
129
|
+
// per-call construction is fine.
|
|
130
|
+
CIImage* ciImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];
|
|
131
|
+
if (ciImage == nil) {
|
|
132
|
+
return @{@"ok": @NO, @"error": @"CIImage imageWithCVPixelBuffer returned nil"};
|
|
133
|
+
}
|
|
134
|
+
CIContext* ctx = [CIContext context];
|
|
135
|
+
CGImageRef cgImage = [ctx createCGImage:ciImage fromRect:ciImage.extent];
|
|
136
|
+
if (cgImage == NULL) {
|
|
137
|
+
return @{@"ok": @NO, @"error": @"CIContext createCGImage failed"};
|
|
138
|
+
}
|
|
139
|
+
UIImage* uiImage = [UIImage imageWithCGImage:cgImage];
|
|
140
|
+
size_t width = CGImageGetWidth(cgImage);
|
|
141
|
+
size_t height = CGImageGetHeight(cgImage);
|
|
142
|
+
CGImageRelease(cgImage);
|
|
143
|
+
|
|
144
|
+
NSData* jpegData = UIImageJPEGRepresentation(uiImage, (CGFloat)(q / 100.0));
|
|
145
|
+
if (jpegData == nil) {
|
|
146
|
+
return @{@"ok": @NO, @"error": @"UIImageJPEGRepresentation returned nil"};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Atomic write — under the hood NSData writes to a temp file then
|
|
150
|
+
// renames. Avoids torn writes if a reader tries to open the path
|
|
151
|
+
// mid-write (would otherwise see a partial JPEG and choke).
|
|
152
|
+
NSError* err = nil;
|
|
153
|
+
BOOL ok = [jpegData writeToFile:path
|
|
154
|
+
options:NSDataWritingAtomic
|
|
155
|
+
error:&err];
|
|
156
|
+
if (!ok) {
|
|
157
|
+
NSString* msg = err.localizedDescription ?: @"NSData writeToFile returned NO";
|
|
158
|
+
return @{@"ok": @NO, @"error": msg};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return @{
|
|
162
|
+
@"ok": @YES,
|
|
163
|
+
@"path": path,
|
|
164
|
+
@"width": @(width),
|
|
165
|
+
@"height": @(height),
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Auto-register the plugin at class-load time. Name must match what
|
|
170
|
+
// JS passes to `VisionCameraProxy.initFrameProcessorPlugin('save_frame_as_jpeg')`.
|
|
171
|
+
// Same pattern as KeyframeGateFrameProcessor's +load.
|
|
172
|
+
+ (void)load {
|
|
173
|
+
[FrameProcessorPluginRegistry
|
|
174
|
+
addFrameProcessorPlugin:@"save_frame_as_jpeg"
|
|
175
|
+
withInitializer:^FrameProcessorPlugin* _Nonnull(
|
|
176
|
+
VisionCameraProxyHolder* proxy,
|
|
177
|
+
NSDictionary* _Nullable options) {
|
|
178
|
+
return [[SaveFrameAsJpegPlugin alloc]
|
|
179
|
+
initWithProxy:proxy withOptions:options];
|
|
180
|
+
}];
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
@end
|
|
184
|
+
|
|
185
|
+
#endif // __has_include(<VisionCamera/FrameProcessorPlugin.h>)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-image-stitcher",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Pose-aware panorama capture + stitching for React Native. One <Camera> component, both tap-to-photo and hold-to-pan modes, both AR-backed and IMU-fallback capture paths.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
package/src/camera/useCapture.ts
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
* - This hook does NOT persist captures. Host apps hand the
|
|
19
19
|
* returned CaptureResult to their own storage layer (WatermelonDB
|
|
20
20
|
* insert, Redux dispatch, whatever).
|
|
21
|
-
* - Video recording lives in useVideoCapture
|
|
21
|
+
* - Video recording lives in useVideoCapture.
|
|
22
22
|
*
|
|
23
23
|
* The public API is designed to be minimal and replaceable: host apps
|
|
24
24
|
* that prefer the raw vision-camera API can opt out of this hook and
|
package/src/index.ts
CHANGED
|
@@ -198,6 +198,25 @@ export type {
|
|
|
198
198
|
// cross-runtime handoff (the AR runtime iterating the registry).
|
|
199
199
|
// See the hook's docstring + StitcherFrame.ts for the contract.
|
|
200
200
|
export { useFrameProcessor } from './stitching/useFrameProcessor';
|
|
201
|
+
// v0.9.0 Layer 2 — `useThrottledFrameProcessor`. Throttle gate over
|
|
202
|
+
// `useFrameProcessor` for sub-frame-rate worklet-native processing
|
|
203
|
+
// (native OCR via Vision.framework / ML Kit, TFLite ML detection,
|
|
204
|
+
// LiDAR depth). The worklet runtime has direct access to
|
|
205
|
+
// `frame.toArrayBuffer()` / `frame.arDepth`; bridge small payloads
|
|
206
|
+
// (bboxes, depth-derived metrics) to JS via `runOnJS`. For JS-thread
|
|
207
|
+
// JPEG consumers (file-path OCR libs, cloud upload, thumbnail UI),
|
|
208
|
+
// prefer `useFrameStream` (Layer 3, ships in the same release).
|
|
209
|
+
export { useThrottledFrameProcessor } from './stitching/useThrottledFrameProcessor';
|
|
210
|
+
export type { ThrottledFrameProcessorOptions } from './types';
|
|
211
|
+
// v0.9.0 Layer 3 — `useFrameStream`. JS-thread sampled-frame
|
|
212
|
+
// stream over Layer 1 (`save_frame_as_jpeg` vc plugin) + Layer 2
|
|
213
|
+
// (`useThrottledFrameProcessor`). Use for JS-thread consumers:
|
|
214
|
+
// file-path OCR libs (RN modules), cloud upload, thumbnail UI.
|
|
215
|
+
// For worklet-native processing (Vision/ML Kit as vc plugins,
|
|
216
|
+
// TFLite ML, LiDAR depth), prefer `useThrottledFrameProcessor`
|
|
217
|
+
// (Layer 2) — lower latency, no JPEG roundtrip.
|
|
218
|
+
export { useFrameStream } from './stitching/useFrameStream';
|
|
219
|
+
export type { FrameStreamOptions, SampledFrame } from './types';
|
|
201
220
|
// vision-camera Frame Processor driver for non-AR captures. As
|
|
202
221
|
// of v0.6 the only non-AR driver exported (the legacy
|
|
203
222
|
// `useIncrementalJSDriver` was removed; was deprecated in v0.5).
|