Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mobile][rust] Prune scanner comments

laurenspriem22130ebbfe46
diff --git a/mobile/apps/locker/android/app/build.gradle b/mobile/apps/locker/android/app/build.gradle
index e14486bbde..fd414300cd 100644
--- a/mobile/apps/locker/android/app/build.gradle
+++ b/mobile/apps/locker/android/app/build.gradle
@@ -12,9 +12,6 @@ if (keystorePropertiesFile.exists()) {
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}
-// Ente's custom prebuilt ONNX Runtime AAR is resolved straight from its
-// GitHub release through an Ivy repository; its SHA-256 is pinned in
-// gradle/verification-metadata.xml.
repositories {
exclusiveContent {
forRepository {
@@ -114,7 +111,6 @@ flutter {
dependencies {
// Needed at runtime for Tink since R8 strips these annotations otherwise.
implementation 'com.google.errorprone:error_prone_annotations:2.24.0'
- // Custom WebGPU/XNNPACK build required by the Rust document scanner,
- // which dynamically loads libonnxruntime.so.
+ // Loaded at runtime via dlopen by the Rust document scanner.
implementation 'io.ente.onnxruntime:onnxruntime-webgpu-android:1.28.0-r3@aar'
}
diff --git a/mobile/apps/locker/ios/Podfile b/mobile/apps/locker/ios/Podfile
index f3608d5344..50377e24ba 100644
--- a/mobile/apps/locker/ios/Podfile
+++ b/mobile/apps/locker/ios/Podfile
@@ -31,9 +31,7 @@ target 'Runner' do
use_frameworks!
use_modular_headers!
- # Ente's custom prebuilt ONNX Runtime static libraries (download + SHA-256
- # verification handled by CocoaPods). Consumed by the ente_locker_frb
- # build phase, which points the Rust `ort` crate at the platform slice.
+ # Prebuilt ONNX Runtime static libraries, consumed by the ente_locker_frb build phase.
pod 'EnteOnnxRuntime', :podspec => '../../../../apple/packages/EnteOnnxRuntime/EnteOnnxRuntime.podspec'
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
diff --git a/mobile/apps/locker/lib/services/scanner/auto_capture_controller.dart b/mobile/apps/locker/lib/services/scanner/auto_capture_controller.dart
index 97717eb5ee..0c33987b93 100644
--- a/mobile/apps/locker/lib/services/scanner/auto_capture_controller.dart
+++ b/mobile/apps/locker/lib/services/scanner/auto_capture_controller.dart
@@ -2,25 +2,15 @@ import 'package:locker/services/scanner/scanner_models.dart';
enum AutoCaptureState { searching, arming, cooldown }
-/// Time-based state machine for hands-free capture, fed once per analyzed
-/// frame. A stabilizer-approved quad that keeps passing the gates for
-/// [armHold] fires a capture, after which the frame must stay quad-free for
-/// [clearHold] (with no capture processing pending) before re-arming, so a
-/// document left in view is captured exactly once.
class AutoCaptureController {
- /// How long an eligible quad must be held before firing.
static const armHold = Duration(milliseconds: 1200);
- /// Ineligible time tolerated while arming before progress resets.
static const armGrace = Duration(milliseconds: 250);
- /// How long the frame must stay quad-free after a capture to re-arm.
static const clearHold = Duration(milliseconds: 700);
- /// Cap on per-frame elapsed time, so stalls don't jump the timers.
static const maxFrameGap = Duration(milliseconds: 200);
- /// Minimum quad area as a fraction of the mask area.
static const minAreaFraction = 0.15;
AutoCaptureState _state = AutoCaptureState.searching;
@@ -31,11 +21,8 @@ class AutoCaptureController {
AutoCaptureState get state => _state;
- /// Arming progress in [0, 1]; 0 outside [AutoCaptureState.arming].
double get progress => _progress;
- /// Advances the machine with the stabilizer output for one frame. Returns
- /// true when the hold completed and a capture should fire now.
bool onFrame(ScanQuad? stableQuad, {required bool captureBusy}) {
final now = DateTime.now();
final last = _lastFrameAt;
@@ -84,8 +71,6 @@ class AutoCaptureController {
}
}
- /// Disarms until the document leaves the frame. Called for every capture,
- /// manual or automatic, so a document still in view is not re-captured.
void notifyCaptureStarted() {
_state = AutoCaptureState.cooldown;
_progress = 0;
diff --git a/mobile/apps/locker/lib/services/scanner/document_scanner_service.dart b/mobile/apps/locker/lib/services/scanner/document_scanner_service.dart
index c012f26301..5cb53b59ef 100644
--- a/mobile/apps/locker/lib/services/scanner/document_scanner_service.dart
+++ b/mobile/apps/locker/lib/services/scanner/document_scanner_service.dart
@@ -2,18 +2,11 @@ import 'dart:typed_data';
import 'package:locker/services/scanner/scanner_models.dart';
-/// Seam between the scanner UI and the native processing pipeline.
-///
-/// Implementations own the storage of page artifacts (temp files); callers
-/// own page lifecycle via [disposePage].
abstract class DocumentScannerService {
Future<void> init();
bool get isReady;
- /// Live document detection on an Android YUV420 preview frame.
- /// Returns the detected quad in mask space of the upright
- /// ([rotationDegrees]-applied) frame, or null when nothing is detected.
Future<ScanQuad?> detectLiveYuv({
required Uint8List y,
required Uint8List u,
@@ -26,7 +19,6 @@ abstract class DocumentScannerService {
required int rotationDegrees,
});
- /// Live document detection on an iOS BGRA8888 preview frame.
Future<ScanQuad?> detectLiveBgra(
Uint8List bytes,
int rowStride,
@@ -35,21 +27,14 @@ abstract class DocumentScannerService {
int rotationDegrees,
);
- /// Detects the document in [capturedJpeg], applies perspective crop,
- /// auto color mode and enhancement at high quality.
Future<ScannedPage> processCapture(Uint8List capturedJpeg);
- /// Re-renders [page] from its source with a manual [quad] (mask space)
- /// and/or [rotationDegrees]. Returns a page that supersedes [page]; its
- /// processedJpeg is a new file and superseded render artifacts are cleaned
- /// up by the service.
Future<ScannedPage> updatePage(
ScannedPage page, {
ScanQuad? quad,
int? rotationDegrees,
});
- /// Deletes all files backing [page].
Future<void> disposePage(ScannedPage page);
Future<void> dispose();
diff --git a/mobile/apps/locker/lib/services/scanner/pdf_writer.dart b/mobile/apps/locker/lib/services/scanner/pdf_writer.dart
index eae357717b..f8adda470b 100644
--- a/mobile/apps/locker/lib/services/scanner/pdf_writer.dart
+++ b/mobile/apps/locker/lib/services/scanner/pdf_writer.dart
@@ -1,10 +1,6 @@
import 'dart:convert';
import 'dart:typed_data';
-/// Minimal PDF writer: one page per JPEG, each embedded verbatim as a
-/// DCTDecode image XObject. Only the JPEG's SOF header is parsed (for
-/// dimensions and color space); the compressed bytes are never touched.
-
class JpegInfo {
const JpegInfo({
required this.width,
@@ -33,7 +29,6 @@ class JpegInfo {
continue;
}
final marker = bytes[offset + 1];
- // Standalone markers carry no length.
if (marker == 0xD8 ||
marker == 0x01 ||
(marker >= 0xD0 && marker <= 0xD7)) {
@@ -75,8 +70,6 @@ class PdfPageSpec {
final double heightMm;
}
-/// Shrinks a sheet until it fits inside A4's long edge and Letter's short
-/// edge; never enlarges.
({double widthMm, double heightMm}) constrainToMaxFormat(
double widthMm,
double heightMm,
@@ -105,8 +98,6 @@ class PdfWriter {
void raw(List<int> bytes) => out.add(bytes);
void text(String value) => raw(latin1.encode(value));
- // Objects: 1 catalog, 2 page tree, 3 info, then page/contents/image per
- // page.
int pageObj(int index) => 4 + index * 3;
int contentObj(int index) => pageObj(index) + 1;
int imageObj(int index) => pageObj(index) + 2;
diff --git a/mobile/apps/locker/lib/services/scanner/rust_document_scanner_service.dart b/mobile/apps/locker/lib/services/scanner/rust_document_scanner_service.dart
index db19280ba7..fdf16d8405 100644
--- a/mobile/apps/locker/lib/services/scanner/rust_document_scanner_service.dart
+++ b/mobile/apps/locker/lib/services/scanner/rust_document_scanner_service.dart
@@ -9,8 +9,6 @@ import 'package:logging/logging.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
-/// Rust-backed implementation: ONNX segmentation for detection, perspective
-/// extraction and enhancement in the native pipeline.
class RustDocumentScannerService implements DocumentScannerService {
static const int _maxPixels = 4000000;
@@ -42,8 +40,6 @@ class RustDocumentScannerService implements DocumentScannerService {
p.join(tmp.path, 'document_scan'),
).create(recursive: true);
final support = await getApplicationSupportDirectory();
- // The Rust side downloads the model into this store on first use and
- // reuses the verified copy afterwards.
_session = await ScannerSession.create(
assetsDir: p.join(support.path, 'assets'),
);
@@ -248,7 +244,6 @@ class RustDocumentScannerService implements DocumentScannerService {
Offset(quad.bottomLeft.x, quad.bottomLeft.y),
]);
- /// Source pixel space -> 256 mask space.
static ScanQuad _maskQuadFromSource(RustQuad quad, int width, int height) {
final sx = ScanQuad.maskSide / width;
final sy = ScanQuad.maskSide / height;
@@ -261,7 +256,6 @@ class RustDocumentScannerService implements DocumentScannerService {
]);
}
- /// 256 mask space -> source pixel space.
static RustQuad _sourceQuadFromMask(ScanQuad quad, int width, int height) {
final sx = width / ScanQuad.maskSide;
final sy = height / ScanQuad.maskSide;
diff --git a/mobile/apps/locker/lib/services/scanner/scan_geometry.dart b/mobile/apps/locker/lib/services/scanner/scan_geometry.dart
index 8020ec3439..7a8e952c20 100644
--- a/mobile/apps/locker/lib/services/scanner/scan_geometry.dart
+++ b/mobile/apps/locker/lib/services/scanner/scan_geometry.dart
@@ -16,8 +16,6 @@ double maxCornerDistance(ScanQuad a, ScanQuad b) {
return worst;
}
-/// Orders four corners clockwise by angle around their centroid; the first
-/// corner after sorting is the top-left role.
ScanQuad orderClockwise(List<Offset> corners) {
assert(corners.length == 4);
final cx = corners.map((c) => c.dx).reduce((a, b) => a + b) / 4;
@@ -31,8 +29,6 @@ ScanQuad orderClockwise(List<Offset> corners) {
return ScanQuad(sorted);
}
-/// The rectangle content of aspect ratio [aspect] occupies inside [container]
-/// under BoxFit.contain.
Rect fittedRect(Size container, double aspect) {
final containerAspect = container.width / container.height;
final Size display = aspect > containerAspect
@@ -46,8 +42,6 @@ Rect fittedRect(Size container, double aspect) {
);
}
-/// A quad is surfaced only once [requiredStableFrames] consecutive detections
-/// agree to within [maxCornerDrift] in mask space.
class QuadStabilizer {
static const double maxCornerDrift = 20.0;
static const int requiredStableFrames = 3;
diff --git a/mobile/apps/locker/lib/services/scanner/scan_session_controller.dart b/mobile/apps/locker/lib/services/scanner/scan_session_controller.dart
index 34fd520aab..60e81359f4 100644
--- a/mobile/apps/locker/lib/services/scanner/scan_session_controller.dart
+++ b/mobile/apps/locker/lib/services/scanner/scan_session_controller.dart
@@ -10,8 +10,6 @@ import 'package:logging/logging.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
-/// State of one scan session: the ordered page list, the capture-processing
-/// queue and the temp artifacts. Disposed when the scanner flow is left.
class ScanSessionController extends ChangeNotifier {
ScanSessionController({DocumentScannerService? service})
: _service = service ?? documentScannerService;
@@ -32,15 +30,12 @@ class ScanSessionController extends ChangeNotifier {
int get pageCount => _pages.length;
bool get isProcessing => _pendingCount > 0;
- /// PDF filename including the extension, once initialized.
String? get fileName => _fileName;
void ensureFileName(String fileName) {
_fileName ??= fileName;
}
- /// Renames the PDF to [baseName] (extension re-appended). Empty or
- /// separator-only input keeps the current name.
void renameFile(String baseName) {
var cleaned = baseName.replaceAll(RegExp(r'[/\\]'), '').trim();
if (cleaned.toLowerCase().endsWith(_pdfExtension)) {
@@ -55,7 +50,6 @@ class ScanSessionController extends ChangeNotifier {
ScannedPage? get lastPage => _pages.isEmpty ? null : _pages.last;
- /// Returns and clears the most recent processing error, if any.
Object? takeLastError() {
final error = _lastError;
_lastError = null;
@@ -97,8 +91,6 @@ class ScanSessionController extends ChangeNotifier {
) =>
_service.detectLiveBgra(bytes, rowStride, width, height, rotationDegrees);
- /// Enqueues a capture; processing is sequential so page order follows
- /// capture order.
void addCapture(Uint8List capturedJpeg) {
_pendingCount++;
notifyListeners();
@@ -120,7 +112,6 @@ class ScanSessionController extends ChangeNotifier {
});
}
- /// Completes when every enqueued capture has been processed.
Future<void> waitForPending() async {
while (_pendingCount > 0) {
await _queue;
@@ -156,7 +147,6 @@ class ScanSessionController extends ChangeNotifier {
);
}
- /// Moves a page; indices follow ReorderableListView's onReorder convention.
void reorderPage(int oldIndex, int newIndex) {
if (oldIndex < 0 || oldIndex >= _pages.length) return;
if (newIndex > oldIndex) newIndex -= 1;
@@ -175,8 +165,6 @@ class ScanSessionController extends ChangeNotifier {
await _service.disposePage(page);
}
- /// Assembles all pages, in order, into a PDF named [fileName] in the temp
- /// dir.
Future<File> buildPdf() async {
final fileName = _fileName;
if (fileName == null) {
@@ -201,7 +189,6 @@ class ScanSessionController extends ChangeNotifier {
return file;
}
- /// Long edge scaled to A4 height, then constrained to standard formats.
static ({double widthMm, double heightMm}) _pageSizeMm(Uint8List jpeg) {
final info = JpegInfo.parse(jpeg);
const a4HeightMm = 297.0;
diff --git a/mobile/apps/locker/lib/services/scanner/scanner_models.dart b/mobile/apps/locker/lib/services/scanner/scanner_models.dart
index a981607647..1bc3ae21db 100644
--- a/mobile/apps/locker/lib/services/scanner/scanner_models.dart
+++ b/mobile/apps/locker/lib/services/scanner/scanner_models.dart
@@ -3,9 +3,6 @@ import 'dart:ui';
enum ScanColorMode { auto, color, grayscale }
-/// Four corners in the detector's 256x256 mask space, clockwise from
-/// top-left. Mask space maps proportionally onto the upright frame it
-/// describes (live preview frame or source capture).
class ScanQuad {
const ScanQuad(this.corners);
@@ -35,21 +32,16 @@ class ScannedPage {
final String id;
- /// The upright (EXIF-normalized) capture, kept for re-cropping.
final File sourceJpeg;
- /// Rendered output; crop, enhancement and [rotationDegrees] are baked in.
final File processedJpeg;
- /// Quad used for the render, in mask space of [sourceJpeg].
final ScanQuad quad;
final int rotationDegrees;
- /// Color or grayscale after auto-detection resolved.
final ScanColorMode resolvedColorMode;
- /// Dimensions of [processedJpeg].
final int width;
final int height;
diff --git a/mobile/apps/locker/lib/ui/pages/scanner/scan_quad_overlay.dart b/mobile/apps/locker/lib/ui/pages/scanner/scan_quad_overlay.dart
index a7d95e8939..40c7e401f3 100644
--- a/mobile/apps/locker/lib/ui/pages/scanner/scan_quad_overlay.dart
+++ b/mobile/apps/locker/lib/ui/pages/scanner/scan_quad_overlay.dart
@@ -3,9 +3,6 @@ import 'package:flutter/scheduler.dart' show Ticker;
import 'package:locker/services/scanner/scan_geometry.dart';
import 'package:locker/services/scanner/scanner_models.dart';
-/// Draws the stabilized detection quad over the preview, easing towards each
-/// new target a little every frame. [quad] is in mask space and is stretched
-/// onto this widget's box, which must exactly cover the preview frame.
class ScanQuadOverlay extends StatefulWidget {
const ScanQuadOverlay({super.key, required this.quad, required this.color});
diff --git a/mobile/apps/locker/lib/ui/pages/scanner/scanner_capture_page.dart b/mobile/apps/locker/lib/ui/pages/scanner/scanner_capture_page.dart
index 543e2c47ad..f5bb19f611 100644
--- a/mobile/apps/locker/lib/ui/pages/scanner/scanner_capture_page.dart
+++ b/mobile/apps/locker/lib/ui/pages/scanner/scanner_capture_page.dart
@@ -19,8 +19,6 @@ import 'package:logging/logging.dart';
enum _CameraStatus { starting, ready, permissionDenied, error }
-/// Camera screen of the document scanner. Pops with `true` when the scan was
-/// saved, otherwise the caller treats the flow as cancelled.
class ScannerCapturePage extends StatefulWidget {
const ScannerCapturePage({super.key, required this.onUploadFiles});
@@ -117,8 +115,6 @@ class _ScannerCapturePageState extends State<ScannerCapturePage>
);
final controller = CameraController(
back,
- // Lower presets cap still-capture resolution at preview-like sizes;
- // `max` is required for full-resolution captures.
ResolutionPreset.max,
enableAudio: false,
imageFormatGroup: Platform.isIOS
@@ -162,8 +158,6 @@ class _ScannerCapturePageState extends State<ScannerCapturePage>
await camera.dispose();
}
- // One analysis frame in flight at a time; newer frames are dropped until
- // the current one finishes.
void _onFrame(CameraImage image) {
if (_analysisInFlight || _takingPicture || !_session.isServiceReady) {
return;
@@ -213,7 +207,6 @@ class _ScannerCapturePageState extends State<ScannerCapturePage>
}
}
} catch (_) {
- // Dropped frames are fine; the next one will do.
} finally {
_analysisInFlight = false;
}
@@ -490,7 +483,6 @@ class _ShutterButton extends StatelessWidget {
final bool enabled;
- /// Auto-capture arming progress in [0, 1]; drawn as an arc over the ring.
final double armingProgress;
final Color armingColor;
final Future<void> Function() onTap;
@@ -554,7 +546,6 @@ class _ArmingArcPainter extends CustomPainter {
void paint(Canvas canvas, Size size) {
if (progress <= 0 || size.isEmpty) return;
canvas.drawArc(
- // Centered on the button's 4px ring, which is inset in the same box.
Rect.fromCircle(
center: size.center(Offset.zero),
radius: size.shortestSide / 2 - 2,
@@ -575,8 +566,6 @@ class _ArmingArcPainter extends CustomPainter {
oldDelegate.progress != progress || oldDelegate.color != color;
}
-/// Auto/manual capture-mode toggle: pill with the auto-camera glyph and the
-/// current mode's label, filled with the accent color while auto is on.
class _AutoToggle extends StatelessWidget {
const _AutoToggle({required this.active, required this.onTap});
@@ -625,8 +614,6 @@ class _AutoToggle extends StatelessWidget {
}
}
-/// Prominent "done capturing" button: filled accent circle with a checkmark
-/// and a page-count badge. Animates in when the first page arrives.
class _DoneButton extends StatelessWidget {
const _DoneButton({
required this.session,
diff --git a/mobile/apps/locker/lib/ui/pages/scanner/scanner_crop_page.dart b/mobile/apps/locker/lib/ui/pages/scanner/scanner_crop_page.dart
index 4b96dbbff7..2f32bcc69d 100644
--- a/mobile/apps/locker/lib/ui/pages/scanner/scanner_crop_page.dart
+++ b/mobile/apps/locker/lib/ui/pages/scanner/scanner_crop_page.dart
@@ -9,8 +9,6 @@ import 'package:locker/services/scanner/scan_geometry.dart';
import 'package:locker/services/scanner/scan_session_controller.dart';
import 'package:locker/services/scanner/scanner_models.dart';
-/// Manual corner adjustment on the source capture. Saving re-renders the
-/// page; cancelling discards the edit.
class ScannerCropPage extends StatefulWidget {
const ScannerCropPage({
super.key,
diff --git a/mobile/apps/locker/lib/ui/pages/scanner/scanner_review_page.dart b/mobile/apps/locker/lib/ui/pages/scanner/scanner_review_page.dart
index 95b6f2a8a7..7b223dcba2 100644
--- a/mobile/apps/locker/lib/ui/pages/scanner/scanner_review_page.dart
+++ b/mobile/apps/locker/lib/ui/pages/scanner/scanner_review_page.dart
@@ -13,9 +13,6 @@ import 'package:locker/ui/components/text_input_sheet.dart';
import 'package:locker/ui/pages/scanner/scanner_crop_page.dart';
import 'package:share_plus/share_plus.dart';
-/// Review of the scanned pages. Pops with `true` after a successful save so
-/// the capture page can close the flow; popping otherwise returns to the
-/// camera for more pages.
class ScannerReviewPage extends StatefulWidget {
const ScannerReviewPage({
super.key,
@@ -148,8 +145,6 @@ class _ScannerReviewPageState extends State<ScannerReviewPage> {
);
}
- /// Reorders and keeps the currently viewed page in view at its new
- /// position.
void _onReorder(int oldIndex, int newIndex) {
final pages = widget.session.pages;
if (pages.isEmpty) return;
@@ -170,7 +165,6 @@ class _ScannerReviewPageState extends State<ScannerReviewPage> {
return '${text.substring(0, half)}…${text.substring(text.length - half)}';
}
- /// Filename styled as an editable field so renaming is discoverable.
Widget _buildFileNameField(ColorTokens colors) {
final fileName = widget.session.fileName ?? '';
return Tooltip(
diff --git a/mobile/apps/locker/rust_builder/ios/ente_locker_frb.podspec b/mobile/apps/locker/rust_builder/ios/ente_locker_frb.podspec
index c288255479..a37bfa3a2b 100644
--- a/mobile/apps/locker/rust_builder/ios/ente_locker_frb.podspec
+++ b/mobile/apps/locker/rust_builder/ios/ente_locker_frb.podspec
@@ -22,18 +22,13 @@ A new Flutter FFI plugin project.
s.dependency 'Flutter'
s.platform = :ios, '15.1'
- # Flutter.framework does not contain an i386 slice, and ONNX Runtime 1.28
- # does not publish an x86_64 iOS Simulator binary.
+ # Flutter.framework has no i386 slice; ONNX Runtime 1.28 has no x86_64 simulator binary.
s.user_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386 x86_64' }
s.swift_version = '5.0'
s.script_phase = {
:name => 'Build Rust library',
- # Point the Rust `ort` crate at the custom prebuilt ONNX Runtime static
- # archive for the active platform (downloaded and SHA-256 verified by
- # CocoaPods as the EnteOnnxRuntime pod), then run the Cargokit build.
- # The build_pod.sh arguments are the relative path to the `rust` folder
- # and the name of the Rust library.
+ # ORT_LIB_PATH points the Rust `ort` crate at the EnteOnnxRuntime pod's static archive.
:script => <<-SCRIPT,
set -e
case "$PLATFORM_NAME" in
@@ -51,17 +46,13 @@ A new Flutter FFI plugin project.
SCRIPT
:execution_position => :before_compile,
:input_files => ['${BUILT_PRODUCTS_DIR}/cargokit_phony'],
- # Let Xcode know that the static library linked below is created by this
- # build step.
+ # Let Xcode know the static library linked below is created by this build step.
:output_files => ["${BUILT_PRODUCTS_DIR}/libente_locker_frb.a"],
}
s.pod_target_xcconfig = {
'DEFINES_MODULE' => 'YES',
- # ONNX Runtime 1.28 supports only the ARM64 iOS Simulator slice.
'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386 x86_64',
- # Root the primary Flutter Rust Bridge dispatcher so the linker pulls in
- # the bridge and its transitive FFI exports without force-loading ONNX
- # Runtime's duplicate protobuf objects.
+ # Roots the FRB dispatcher instead of -force_load to avoid ONNX Runtime's duplicate protobuf objects.
'OTHER_LDFLAGS' => [
'$(inherited)',
'${BUILT_PRODUCTS_DIR}/libente_locker_frb.a',
diff --git a/mobile/apps/locker/rust_builder/macos/ente_locker_frb.podspec b/mobile/apps/locker/rust_builder/macos/ente_locker_frb.podspec
index 47feafffc9..6f2a152021 100644
--- a/mobile/apps/locker/rust_builder/macos/ente_locker_frb.podspec
+++ b/mobile/apps/locker/rust_builder/macos/ente_locker_frb.podspec
@@ -22,8 +22,7 @@ A new Flutter FFI plugin project.
s.dependency 'FlutterMacOS'
s.platform = :osx, '10.11'
- # ONNX Runtime 1.28 does not publish Apple x86_64 binaries, so consumers
- # must also avoid trying to link an x86_64 application slice.
+ # ONNX Runtime 1.28 does not publish Apple x86_64 binaries.
s.user_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=macosx*]' => 'x86_64' }
s.swift_version = '5.0'
@@ -39,7 +38,6 @@ A new Flutter FFI plugin project.
}
s.pod_target_xcconfig = {
'DEFINES_MODULE' => 'YES',
- # ONNX Runtime 1.28 does not publish Apple x86_64 binaries.
'EXCLUDED_ARCHS[sdk=macosx*]' => 'x86_64',
'OTHER_LDFLAGS' => '-force_load ${BUILT_PRODUCTS_DIR}/libente_locker_frb.a -lc++',
}
diff --git a/rust/bindings/frb/locker/src/api/document_scanner_api.rs b/rust/bindings/frb/locker/src/api/document_scanner_api.rs
index 27f294d788..d7f1a8b778 100644
--- a/rust/bindings/frb/locker/src/api/document_scanner_api.rs
+++ b/rust/bindings/frb/locker/src/api/document_scanner_api.rs
@@ -44,8 +44,7 @@ pub struct RustScanOptions {
#[derive(Clone, Debug)]
pub struct RustReprocessOptions {
- /// Same coordinate space as `RustScanResult.quad`: the decoded source
- /// image.
+ /// Same space as `RustScanResult.quad`: the decoded source image.
pub quad: RustQuad,
/// Must be a multiple of 90.
pub rotation_degrees: i32,
@@ -56,9 +55,7 @@ pub struct RustReprocessOptions {
#[derive(Clone, Debug)]
pub struct RustScanResult {
- /// In decoded-source coordinates (EXIF applied, before
- /// `rotation_degrees`); `None` when nothing was detected and the whole
- /// frame was returned.
+ /// Decoded-source coordinates (EXIF applied, before `rotation_degrees`); `None` when nothing was detected.
pub quad: Option<RustQuad>,
pub color_mode: RustColorMode,
pub output_width: u32,
@@ -76,7 +73,6 @@ pub enum RustScanErrorKind {
ModelLoad,
Codec,
Pipeline,
- /// A panic caught at the FFI boundary.
Internal,
}
@@ -86,15 +82,12 @@ pub struct RustScanError {
pub message: String,
}
-/// `Send + Sync`: one session serves both the live preview stream and
-/// capture processing.
#[frb(opaque)]
pub struct ScannerSession {
inner: scan::ScannerSession,
}
impl ScannerSession {
- /// Downloads the model into the asset store at `assets_dir` on first use.
pub async fn create(assets_dir: String) -> Result<ScannerSession, RustScanError> {
let store = AssetStore::new(assets_dir);
let model_path = scan::ensure_segmentation_model(&store)
@@ -109,10 +102,7 @@ impl ScannerSession {
})
}
- /// Takes an iOS BGRA8888 frame with `row_stride` bytes per row, so Dart
- /// passes the camera buffer through unchanged. The returned quad is in
- /// mask space (256x256), already rotated by `rotation_degrees`: rotate
- /// the overlay quad, not the frame.
+ /// The returned quad is in mask space (256x256), already rotated by `rotation_degrees`.
pub fn live_detect_bgra(
&self,
bgra: Vec<u8>,
@@ -130,9 +120,7 @@ impl ScannerSession {
}))
}
- /// Accepts both planar I420 (`uv_pixel_stride == 1`) and interleaved
- /// NV21/NV12 (`uv_pixel_stride == 2`) YUV_420_888 planes. Quad semantics
- /// as in [`Self::live_detect_bgra`].
+ /// Quad semantics as in [`Self::live_detect_bgra`].
pub fn live_detect_yuv420(
&self,
y: Vec<u8>,
@@ -166,7 +154,6 @@ impl ScannerSession {
}))
}
- /// No inference runs.
pub fn reprocess(
&self,
source_bytes: Vec<u8>,
diff --git a/rust/crates/ml/src/cv/arith.rs b/rust/crates/ml/src/cv/arith.rs
index 96cde77215..6ca0058e98 100644
--- a/rust/crates/ml/src/cv/arith.rs
+++ b/rust/crates/ml/src/cv/arith.rs
@@ -1,6 +1,3 @@
-//! Scalar multiply and the weighted sum deliberately promote to f64; the
-//! rest stays in f32.
-
use rayon::prelude::*;
use crate::cv::OpResult;
diff --git a/rust/crates/ml/src/cv/canny.rs b/rust/crates/ml/src/cv/canny.rs
index c38c82201b..15bf30efaf 100644
--- a/rust/crates/ml/src/cv/canny.rs
+++ b/rust/crates/ml/src/cv/canny.rs
@@ -79,7 +79,6 @@ pub(crate) fn canny(src: &ImageU8, threshold1: f64, threshold2: f64) -> OpResult
}
}
- // The one-pixel `NO` frame keeps the unguarded hysteresis flood inside.
let step = w + 2;
let mut mag = vec![0i32; step * (h + 2)];
for y in 0..h {
diff --git a/rust/crates/ml/src/cv/draw.rs b/rust/crates/ml/src/cv/draw.rs
index 4db69545fa..5a46988da7 100644
--- a/rust/crates/ml/src/cv/draw.rs
+++ b/rust/crates/ml/src/cv/draw.rs
@@ -18,7 +18,6 @@ pub(crate) fn fill_poly(
let mut canvas = GrayImage::new(width as u32, height as u32);
let color = Luma([saturate_u8_f64(value)]);
- // imageproc wants an open path with distinct first/last points.
let mut pts: Vec<Point<i32>> = Vec::with_capacity(polygon.len());
for &(x, y) in polygon {
if pts.last() != Some(&Point::new(x, y)) {
diff --git a/rust/crates/ml/src/cv/filter.rs b/rust/crates/ml/src/cv/filter.rs
index a305273c40..e4ae7f756e 100644
--- a/rust/crates/ml/src/cv/filter.rs
+++ b/rust/crates/ml/src/cv/filter.rs
@@ -3,7 +3,6 @@ use crate::cv::image::{ImageF32, ImageU8};
use super::reflect101;
-/// Taps sum to 256 (8 fractional bits).
fn gaussian_taps(n: i32) -> OpResult<&'static [u32]> {
Ok(match n {
1 => &[256],
@@ -28,7 +27,6 @@ pub(crate) fn gaussian_blur_u8(src: &ImageU8, ksize: i32) -> OpResult<ImageU8> {
let (w, h) = (src.width as usize, src.height as usize);
let (ax, ay) = ((kw / 2) as i64, (kh / 2) as i64);
- // Horizontal pass into u16 (taps sum to 256), vertical into u32.
let mut hsum = vec![0u16; w * h];
for y in 0..h {
let row = &src.data[y * w..(y + 1) * w];
diff --git a/rust/crates/ml/src/cv/image.rs b/rust/crates/ml/src/cv/image.rs
index a341ae86ce..bd09fdfa22 100644
--- a/rust/crates/ml/src/cv/image.rs
+++ b/rust/crates/ml/src/cv/image.rs
@@ -1,5 +1,3 @@
-//! Row-major, channel-interleaved, fully packed (no row padding).
-
use super::OpResult;
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -140,7 +138,6 @@ impl Image {
}
}
-/// `area` is the shoelace area.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct Contour {
pub points: Vec<(i32, i32)>,
diff --git a/rust/crates/ml/src/cv/lab.rs b/rust/crates/ml/src/cv/lab.rs
index 53c88ac7ac..b278dbb8f6 100644
--- a/rust/crates/ml/src/cv/lab.rs
+++ b/rust/crates/ml/src/cv/lab.rs
@@ -1,5 +1,3 @@
-//! 8-bit BGR <-> Lab, as fixed-point dot products over precomputed tables.
-//!
//! L, a and b are all scaled onto 0..255 (a and b biased by 128); the
//! pipeline's chroma and luminance thresholds are calibrated in these units.
@@ -20,7 +18,6 @@ const MIN_AB_VALUE: i32 = -8145;
const AB_TO_XZ_LEN: usize = (LAB_BASE as usize) * 9 / 4;
const LAB2RGB_SHIFT: i32 = LAB_SHIFT + (BASE_SHIFT - INV_GAMMA_SHIFT);
-// sRGB <-> XYZ (D65), rows normalized by the white point during table build.
const SRGB2XYZ_D65: [f64; 9] = [
0.412453, 0.35758, 0.180423, 0.212671, 0.71516, 0.072169, 0.019334, 0.119193, 0.950227,
];
@@ -76,9 +73,7 @@ struct LabTabs {
srgb_inv_gamma: Box<[u16]>,
lab_to_yf: [u16; 512],
ab_to_xz: Box<[i32]>,
- /// Ordered so a BGR pixel is consumed in storage order.
fwd_coeffs: [i32; 9],
- /// Rows are R, G, B.
inv_coeffs: [i32; 9],
}
@@ -128,7 +123,6 @@ fn build_tabs() -> LabTabs {
lab_to_yf[i as usize * 2 + 1] = ify as u16;
}
- // Integer division truncates toward zero; `i` starts negative.
let mut ab_to_xz = vec![0i32; AB_TO_XZ_LEN];
let bias = LAB_BASE * 16 / 116 * 108 / 841;
for (k, out) in ab_to_xz.iter_mut().enumerate() {
diff --git a/rust/crates/ml/src/cv/mod.rs b/rust/crates/ml/src/cv/mod.rs
index 0c4459a49f..0ff49e9107 100644
--- a/rust/crates/ml/src/cv/mod.rs
+++ b/rust/crates/ml/src/cv/mod.rs
@@ -106,8 +106,6 @@ pub(crate) fn saturate_u8_f64(v: f64) -> u8 {
v.round_ties_even().clamp(0.0, 255.0) as u8
}
-/// Mirrors an out-of-range coordinate back into `[0, len)` without repeating
-/// the edge pixel.
pub(crate) fn reflect101(mut p: i64, len: i32) -> i64 {
let len = len as i64;
if p >= 0 && p < len {
diff --git a/rust/crates/ml/src/cv/morph.rs b/rust/crates/ml/src/cv/morph.rs
index 9f384e4ca6..0560dac93a 100644
--- a/rust/crates/ml/src/cv/morph.rs
+++ b/rust/crates/ml/src/cv/morph.rs
@@ -32,7 +32,6 @@ fn morph(src: &ImageU8, kernel: &ImageU8, erode: bool) -> OpResult<ImageU8> {
}
let mut out = vec![0u8; w * h];
- // Tap-major keeps the min/max folds vectorizable.
let row = |y: usize, dst: &mut [u8]| {
let base = y * pw;
dst.copy_from_slice(&padded[base + offsets[0]..base + offsets[0] + w]);
diff --git a/rust/crates/ml/src/cv/resize.rs b/rust/crates/ml/src/cv/resize.rs
index 7b33fb55ae..7824954862 100644
--- a/rust/crates/ml/src/cv/resize.rs
+++ b/rust/crates/ml/src/cv/resize.rs
@@ -1,5 +1,4 @@
-//! The `Interpolation` algs sample without low-pass filtering; downstream
-//! calibration depends on that.
+//! Sampling without low-pass filtering; downstream calibration depends on that.
use fast_image_resize::{
FilterType, PixelType, ResizeAlg, ResizeOptions, Resizer,
diff --git a/rust/crates/ml/src/cv/transform.rs b/rust/crates/ml/src/cv/transform.rs
index 9293afb36f..30a005ce52 100644
--- a/rust/crates/ml/src/cv/transform.rs
+++ b/rust/crates/ml/src/cv/transform.rs
@@ -16,8 +16,6 @@ pub(crate) fn rotate_u8(src: &ImageU8, degrees: i32) -> OpResult<ImageU8> {
};
let mut data = vec![0u8; src.data.len()];
- // A per-pixel `copy_from_slice` is a `memcpy` call per pixel; the common
- // channel counts are spelled out instead.
let move_px = |dst: &mut [u8], d: usize, s: usize| match cn {
1 => dst[d] = src.data[s],
3 => {
diff --git a/rust/crates/ml/src/scan/codec.rs b/rust/crates/ml/src/scan/codec.rs
index ffa1086ed2..b63e6b4020 100644
--- a/rust/crates/ml/src/scan/codec.rs
+++ b/rust/crates/ml/src/scan/codec.rs
@@ -4,7 +4,6 @@ use ente_image::image_compression::{EncodedImageFormat, encode_rgb};
use super::scanner::ScanError;
use crate::cv::image::ImageU8;
-/// EXIF orientation comes pre-applied by the decoder.
pub(crate) fn decode_bgr(bytes: &[u8]) -> Result<ImageU8, ScanError> {
let decoded = decode_image_from_bytes(bytes)
.map_err(|err| ScanError::Codec(format!("failed to decode image: {err}")))?;
diff --git a/rust/crates/ml/src/scan/color.rs b/rust/crates/ml/src/scan/color.rs
index 182e2e3c04..d7599c04c8 100644
--- a/rust/crates/ml/src/scan/color.rs
+++ b/rust/crates/ml/src/scan/color.rs
@@ -16,7 +16,6 @@ const PROPORTION_THRESHOLD: f64 = 0.0003;
const LUMINANCE_MIN: f64 = 40.0;
const LUMINANCE_MAX: f64 = 180.0;
-/// `quad` is in original-image coordinates.
pub(crate) fn auto_color_mode(img: &ImageU8, mask: &Mask, quad: &Quad) -> OpResult<ColorMode> {
let resized_img = resize_for_max_pixels(img, 1024.0 * 768.0)?;
let work_size = resized_img.size();
diff --git a/rust/crates/ml/src/scan/contour_orientation.rs b/rust/crates/ml/src/scan/contour_orientation.rs
index 3a457d44b7..9305676c51 100644
--- a/rust/crates/ml/src/scan/contour_orientation.rs
+++ b/rust/crates/ml/src/scan/contour_orientation.rs
@@ -27,7 +27,6 @@ pub(crate) fn find_quad_from_contour_orientation(contour: &[Point]) -> Option<Ve
let angles = compute_smoothed_angles(contour, SMOOTH_WINDOW);
- // Open polyline: n-1 terms.
let perimeter: f64 = contour
.windows(2)
.map(|w| (w[1].x - w[0].x).hypot(w[1].y - w[0].y))
@@ -84,7 +83,6 @@ fn angle_mean(a: f64, b: f64) -> f64 {
y.atan2(x)
}
-/// Total least squares direction from the second-moment matrix.
fn fit_line(points: &[Point]) -> Line {
let cx = average(points.iter().map(|p| p.x));
let cy = average(points.iter().map(|p| p.y));
@@ -210,7 +208,6 @@ fn compute_smoothed_angles(contour: &[Point], window: i32) -> Vec<f64> {
let mut sx = 0.0;
let mut sy = 0.0;
for k in -window..=window {
- // n >= 20 and window == 5, so `k + n` is always positive.
let idx = ((k + ni) % ni) as usize;
sx += cos_a[idx];
sy += sin_a[idx];
@@ -257,7 +254,6 @@ fn merge_segments(segments: &[ContourSegment], angle_threshold: f64) -> Vec<Cont
merged
}
-/// Both sorts are stable, which decides ties.
fn select_dominant_segments(
segments: &[ContourSegment],
max_count: usize,
diff --git a/rust/crates/ml/src/scan/detection.rs b/rust/crates/ml/src/scan/detection.rs
index 2b546dd7b7..bd92f5f429 100644
--- a/rust/crates/ml/src/scan/detection.rs
+++ b/rust/crates/ml/src/scan/detection.rs
@@ -16,7 +16,6 @@ pub(crate) fn size_trunc(width: f64, height: f64) -> (i32, i32) {
(width as i32, height as i32)
}
-/// The returned quad is in mask coordinates.
pub(crate) fn detect_document_quad(
mask: &Mask,
original_size: ImageSize,
@@ -75,8 +74,7 @@ fn find_quad_from_orientation_with_adaptive_threshold(
Ok(best_quad)
}
-/// Deliberately no upper bound: corners past the mask edge are legitimate
-/// for edge-touching documents.
+/// Deliberately no upper bound: edge-touching documents put corners past the mask edge.
pub(crate) fn is_valid_quad(quad: &[Point]) -> bool {
quad.iter().all(|p| p.x >= 0.0 && p.y >= 0.0)
}
diff --git a/rust/crates/ml/src/scan/geometry.rs b/rust/crates/ml/src/scan/geometry.rs
index 50f40b4630..2ee45b1910 100644
--- a/rust/crates/ml/src/scan/geometry.rs
+++ b/rust/crates/ml/src/scan/geometry.rs
@@ -71,8 +71,6 @@ impl Quad {
]
}
- /// A negative `iterations` falls into the identity branch (truncating
- /// `%`).
pub(crate) fn rotate90(&self, iterations: i32, image_size: ImageSize) -> Quad {
let rotate = |p: Point| -> Point {
match iterations % 4 {
@@ -138,7 +136,6 @@ mod tests {
#[test]
fn create_quad_orders_corners_by_angle_from_centroid() {
- // With y down, atan2 ascending starts at the top-left.
let vertices = vec![
Point::new(100.0, 100.0),
Point::new(0.0, 100.0),
diff --git a/rust/crates/ml/src/scan/mod.rs b/rust/crates/ml/src/scan/mod.rs
index 87e4497c73..43c6dd9b19 100644
--- a/rust/crates/ml/src/scan/mod.rs
+++ b/rust/crates/ml/src/scan/mod.rs
@@ -22,8 +22,6 @@ use std::path::PathBuf;
use ente_assets::download::CancellationToken;
use ente_assets::{Asset, AssetFile, AssetStore};
-/// Downloads are verified against this before the path reaches
-/// [`ScannerSession::new`].
pub const SEGMENTATION_MODEL_SHA256: &str =
"36b8eeadd42592af496bf2e125a6aad9bebcbca1bda2ac19fa22e108574217a3";
diff --git a/rust/crates/ml/src/scan/perspective.rs b/rust/crates/ml/src/scan/perspective.rs
index 0033a1bee9..94ab229406 100644
--- a/rust/crates/ml/src/scan/perspective.rs
+++ b/rust/crates/ml/src/scan/perspective.rs
@@ -1,5 +1,3 @@
-//! Aspect-ratio estimation from the quad's vanishing points.
-
use super::geometry::{Point, Quad, norm};
#[derive(Clone, Copy, Debug)]
@@ -158,7 +156,6 @@ mod tests {
#[test]
fn estimate_real_dimensions_falls_back_for_weak_perspective() {
- // f exceeds max(w,h)*1.2, so the fallback applies.
let quad = Quad {
top_left: Point::new(100.0, 100.0),
top_right: Point::new(900.0, 100.0),
diff --git a/rust/crates/ml/src/scan/postprocess.rs b/rust/crates/ml/src/scan/postprocess.rs
index a3453cfe23..6cb4997209 100644
--- a/rust/crates/ml/src/scan/postprocess.rs
+++ b/rust/crates/ml/src/scan/postprocess.rs
@@ -16,11 +16,6 @@ const SHADOW_KERNEL_FRAC: f64 = 1.0 / 16.0;
const SHADOW_MAX_GAIN: f32 = 2.8;
const SHADOW_PAPER_PERCENTILE: f64 = 0.90;
-/// Flat-field illumination correction on a luminance channel: estimate the
-/// background map (closing + blur at map scale), then divide toward the
-/// paper level. Below `paper / SHADOW_MAX_GAIN` the gain ramps back to 1:
-/// regions that much darker than paper are content (photos, figures), not
-/// cast shadows, and must not be brightened.
fn remove_shadows(channel: &ImageU8) -> OpResult<ImageU8> {
let (width, height) = channel.size();
let long_edge = width.max(height) as f64;
@@ -78,7 +73,6 @@ fn multi_scale_retinex_on_l(bgr: &ImageU8) -> OpResult<ImageU8> {
let l_float = cv::add_f32_scalar(&l_float_raw, 1.0)?;
let scale_factor = 2.0;
- // `max_dim_small` deliberately uses the untruncated sizes.
let small_w = l_float.width as f64 / scale_factor;
let small_h = l_float.height as f64 / scale_factor;
let (small_cols, small_rows) = size_trunc(small_w, small_h);
@@ -153,8 +147,6 @@ fn multi_scale_retinex_on_l(bgr: &ImageU8) -> OpResult<ImageU8> {
cv::lab_to_bgr(&merged)
}
-/// Values outside [0,256) are not counted, but the denominator stays the
-/// full pixel count.
pub(crate) fn percentile_l(l: &ImageF32, p: f64) -> OpResult<f64> {
let hist = cv::hist_256_f32(l)?;
let total = l.pixels() as f64;
@@ -186,13 +178,11 @@ fn enhance_grayscale_image(img: &ImageU8) -> OpResult<ImageU8> {
let img_float = cv::add_f32_scalar(&img_float_raw, 1.0)?;
let log_img = cv::log_f32(&img_float)?;
- // Unlike the color path, no odd-kernel forcing.
let kernel_sizes = [max_dim / 6.0, max_dim / 50.0];
let weight = 1.0 / kernel_sizes.len() as f64;
let mut retinex = ImageF32::zeros(gray.width, gray.height, 1)?;
for kernel_size in kernel_sizes {
- // Unlike the color path, blurs the linear image and logs afterwards.
let (kw, kh) = size_trunc(kernel_size, kernel_size);
let blur_raw = cv::box_filter_f32(&img_float, kw, kh)?;
let blur = cv::add_f32_scalar(&blur_raw, 1.0)?;
@@ -218,7 +208,6 @@ fn enhance_grayscale_image(img: &ImageU8) -> OpResult<ImageU8> {
let result8u = cv::f32_to_u8(&clamped)?;
- // The mode over [180,255] estimates the background level.
let hist = cv::hist_256_u8(&result8u)?;
let mut mode_val = 220usize;
let mut mode_count = 0.0f64;
@@ -230,7 +219,6 @@ fn enhance_grayscale_image(img: &ImageU8) -> OpResult<ImageU8> {
}
let stretched8u = if mode_val >= 254 {
- // Retinex over-amplified; normalize the original grayscale instead.
let gray_f = cv::u8_to_f32(&gray)?;
let g_low = cv::percentile_f32(&gray_f, 0.01)? as f64;
let g_high = cv::percentile_f32(&gray_f, 0.99)? as f64;
diff --git a/rust/crates/ml/src/scan/quad_score.rs b/rust/crates/ml/src/scan/quad_score.rs
index dd2c62141d..22e085a1e3 100644
--- a/rust/crates/ml/src/scan/quad_score.rs
+++ b/rust/crates/ml/src/scan/quad_score.rs
@@ -8,8 +8,6 @@ pub(crate) fn make_polygon_mask(width: i32, height: i32, polygon: &[Point]) -> O
cv::fill_poly(width, height, &pts, 1.0)
}
-/// `probmap` values are 0..255 (the binarized mask as f32), not normalized
-/// probabilities.
pub(crate) fn score_quad_against_probmap(
quad: &[Point],
probmap: &ImageF32,
diff --git a/rust/crates/ml/src/scan/scanner.rs b/rust/crates/ml/src/scan/scanner.rs
index 28750f1360..10f4308d0d 100644
--- a/rust/crates/ml/src/scan/scanner.rs
+++ b/rust/crates/ml/src/scan/scanner.rs
@@ -29,16 +29,12 @@ pub enum ScanError {
pub struct ScanOptions {
pub color_mode_override: Option<ColorMode>,
pub max_pixels: Option<u32>,
- /// Must be a multiple of 90.
pub rotation_degrees: i32,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ReprocessOptions {
- /// Same coordinate space as [`ScanResult::quad`]: the decoded source
- /// image.
pub quad: Quad,
- /// Must be a multiple of 90.
pub rotation_degrees: i32,
pub color_mode: ColorMode,
pub max_pixels: Option<u32>,
@@ -47,17 +43,12 @@ pub struct ReprocessOptions {
#[derive(Debug, Clone, PartialEq)]
pub struct ScanResult {
- /// In decoded-source coordinates (EXIF applied, before
- /// `rotation_degrees`); `None` when nothing was detected and the whole
- /// frame was returned.
pub quad: Option<Quad>,
pub color_mode: ColorMode,
pub output_width: u32,
pub output_height: u32,
- /// The size [`ScanResult::quad`] is relative to.
pub source_width: u32,
pub source_height: u32,
- /// JPEG-encoded.
pub processed_image: Vec<u8>,
}
@@ -66,8 +57,6 @@ pub struct ScannerSession {
}
impl ScannerSession {
- /// The caller is expected to have verified the model file against
- /// [`SEGMENTATION_MODEL_SHA256`](super::SEGMENTATION_MODEL_SHA256).
pub fn new(model_path: &str) -> Result<Self, ScanError> {
let start = std::time::Instant::now();
let result = Self::load(model_path);
@@ -101,9 +90,6 @@ impl ScannerSession {
Ok(Mask::from_probmap(&probmap, MASK_SIDE, MASK_SIDE))
}
- /// The returned quad is in mask space (256x256), already rotated by
- /// `rotation_degrees`: the caller rotates the overlay quad, not the
- /// frame.
pub fn live_detect_rgba(
&self,
rgba: &[u8],
@@ -116,7 +102,6 @@ impl ScannerSession {
self.live_detect(&bgr, rotation_degrees)
}
- /// Quad semantics as in [`Self::live_detect_rgba`].
pub fn live_detect_yuv420(
&self,
y: &[u8],
@@ -196,7 +181,6 @@ impl ScannerSession {
finish(Some(quad), color_mode, &bgr, &page, DEFAULT_JPEG_QUALITY)
}
- /// No inference runs.
pub fn reprocess(
&self,
source_bytes: &[u8],
diff --git a/rust/crates/ml/src/scan/segmentation.rs b/rust/crates/ml/src/scan/segmentation.rs
index afdffa63e4..444df23d26 100644
--- a/rust/crates/ml/src/scan/segmentation.rs
+++ b/rust/crates/ml/src/scan/segmentation.rs
@@ -13,13 +13,11 @@ use crate::cv::image::{ImageRef, ImageU8};
pub const MASK_SIDE: i32 = 256;
pub(crate) struct Segmenter {
- // `Session::run` needs `&mut self`.
session: Mutex<Session>,
}
impl Segmenter {
- /// CPU-only on purpose: the model is tiny and the thresholded downstream
- /// pipeline needs deterministic output across devices.
+ /// CPU-only on purpose: downstream thresholds need deterministic output across devices.
pub(crate) fn new(model_path: &str) -> Result<Self, ScanError> {
let build = || -> Result<Session, ort::Error> {
Session::builder()?
@@ -76,7 +74,6 @@ impl Segmenter {
};
let data = run()?;
- // The model outputs probabilities directly; no sigmoid.
Ok(data
.iter()
.map(|&v| (v.clamp(0.0, 1.0) * 255.0).round_ties_even() as u8)
diff --git a/rust/crates/ml/src/scan/yuv.rs b/rust/crates/ml/src/scan/yuv.rs
index d8351454d3..b0a68a9a3a 100644
--- a/rust/crates/ml/src/scan/yuv.rs
+++ b/rust/crates/ml/src/scan/yuv.rs
@@ -1,5 +1,3 @@
-//! YUV_420_888 camera planes -> packed BGR.
-//!
//! Reproduces libyuv's limited-range BT.601 fixed-point arithmetic β€” not the
//! textbook full-range matrix β€” because that is what Android camera stacks
//! apply when converting frames to RGBA, so YUV and RGBA frames yield the
@@ -7,7 +5,6 @@
use crate::cv::image::ImageU8;
-// Q6 fixed-point BT.601 limited-range coefficients.
const YG: i32 = 18997; // round(1.164 * 64 * 256 * 256 / 257)
const YGB: i32 = -1160; // 1.164 * 64 * -16 + 64 * 0.5 (rounding bias folded in)
const UB: i32 = -128; // max(-128, round(-2.018 * 64)): clamped to fit a signed byte
@@ -15,7 +12,6 @@ const UG: i32 = 25; // round(0.391 * 64)
const VG: i32 = 52; // round(0.813 * 64)
const VR: i32 = -102; // round(-1.596 * 64)
-// Bias terms; fold in the -128 U/V offset.
const BB: i32 = UB * 128 + YGB;
const BG: i32 = UG * 128 + VG * 128 + YGB;
const BR: i32 = VR * 128 + YGB;
@@ -31,7 +27,6 @@ fn clamp_u8(value: i32) -> u8 {
#[inline]
pub(crate) fn yuv_to_bgr(y: u8, u: u8, v: u8) -> (u8, u8, u8) {
let (y, u, v) = (y as i32, u as i32, v as i32);
- // 0x0101 replicates 8 bits to 16, keeping the luma multiply in 32 bits.
let y1 = ((y as u32 * 0x0101 * YG as u32) >> 16) as i32;
let b = clamp_u8((-(u * UB) + y1 + BB) >> 6);
let g = clamp_u8((-(u * UG + v * VG) + y1 + BG) >> 6);
@@ -156,8 +151,6 @@ pub(crate) fn rgba_to_bgr(rgba: &[u8], width: i32, height: i32) -> Result<ImageU
mod tests {
use super::*;
- /// `ub` is a parameter: libyuv's effective blue coefficient (-2.0, from
- /// the `UB` clamp) differs from the documented -2.018.
fn reference_yuv_to_bgr(y: u8, u: u8, v: u8, ub: f64) -> (u8, u8, u8) {
let yf = (y as f64 - 16.0) * 1.164;
let uf = u as f64 - 128.0;
@@ -212,7 +205,6 @@ mod tests {
assert_eq!(yuv_to_bgr(235, 128, 128), (255, 255, 255));
assert_eq!(yuv_to_bgr(0, 128, 128), (0, 0, 0));
assert_eq!(yuv_to_bgr(255, 128, 128), (255, 255, 255));
- // Y=126 is the mid point of the limited range 16..235.
let (b, g, r) = yuv_to_bgr(126, 128, 128);
assert_eq!((b, g, r), (128, 128, 128));
}
@@ -309,8 +301,6 @@ mod tests {
)
.expect("planar conversion");
- // Android exposes NV21 as two plane views one byte apart into one
- // interleaved buffer, both with pixel stride 2.
let cw = ceil_half(width);
let ch = ceil_half(height);
let mut vu = vec![0u8; (cw * ch * 2) as usize];
diff --git a/rust/crates/ml/tests/scan.rs b/rust/crates/ml/tests/scan.rs
index 2ec0d97109..9852434f84 100644
--- a/rust/crates/ml/tests/scan.rs
+++ b/rust/crates/ml/tests/scan.rs
@@ -1,6 +1,3 @@
-//! Point `ENTE_DOC_SEGMENTATION_MODEL` at the model file; without it every
-//! test skips.
-
use std::io::Cursor;
use ente_ml::scan::{ColorMode, PlaneLayout, Quad, ReprocessOptions, ScanOptions, ScannerSession};
@@ -114,8 +111,6 @@ fn live_detect_rgba_and_yuv_agree_on_a_grayscale_frame() {
let mut rgba = Vec::with_capacity((width * height * 4) as usize);
let mut y_plane = Vec::with_capacity((width * height) as usize);
for px in doc.pixels() {
- // value = 1.164 * (y - 16), chroma neutral, so the limited-range YUV
- // round trip is exact.
let y = 16 + (px.0[0] as i32 * 219 / 255) as u8;
let value = (((y as i32 - 16) * 1164 + 500) / 1000).clamp(0, 255) as u8;
rgba.extend_from_slice(&[value, value, value, 255]);