Preview image versions before sending, and repair unheard chunks

Send sheet: opens on what recipients will see (the encoded photo decoded
on this device) instead of the original. "Try another version" encodes
the whole photo again with a near-invisible zoom/shift; the generative
decoder reconstructs each version differently, and on Kodak the best of
three raised CLIP agreement from 0.828 to 0.853. The user picks the
version to send; packet count and airtime follow the selection. Version 1
is byte-identical to the previous encode, so the wire format is
unchanged. The sender's bubble shows the chosen version and carries the
AI-reconstructed label.

Repeater echoes: the raw RX log handler now decrypts GRP_DATA as well as
GRP_TXT and counts repeaters re-flooding each chunk of our own images.
The bubble shows the repeat count like channel text, and Packet info
lists each packet. About 15 s after sending, chunks no repeater echoed
are resent once, but only when another chunk of the same image was
echoed, so senders with no repeater in earshot do not double their
airtime. Outgoing entries now record their payload size.
This commit is contained in:
zjs81 committed 2026-11-14 20:58:43 -07:00
1 parent aafd541b0f
commit 53e4e457ca
36 files changed
+2366 -150

No files matched your search

+19 -18
View File
@@ -970,15 +970,17 @@ class ImageCodecService extends ChangeNotifier implements ImageSendCodec {
/// [ImageSendCodec] entry point used by `ImageSendPreviewSheet`.
///
/// Takes the raw picked file (JPEG/PNG/HEIC/…), centre-crops and scales it to
/// [kImageCodecSquareSize] square, then encodes. Throws on failure so the
/// Takes the raw picked file (JPEG/PNG/HEIC/…), stretches the part of it
/// that [variant] selects (see [rerollSourceRect]; 0 is the whole photo) to
/// a [kImageCodecSquareSize] square, then encodes. Throws on failure so the
/// sheet can surface it — unlike the nullable service-level methods below,
/// whose contract is "null means not available".
@override
Future<Uint8List> encode(
Uint8List imageBytes,
ImageCodecRatePoint rate,
) async {
ImageCodecRatePoint rate, {
int variant = 0,
}) async {
if (!kImageCodecBitstreamPathAvailable) {
// Fail here rather than after a 512x512 raster and a session load, and
// fail with the specific exception so the sheet can say why.
@@ -990,7 +992,7 @@ class ImageCodecService extends ChangeNotifier implements ImageSendCodec {
throw const ImageCodecBundleIncomplete();
}
final result = await encodeImage(
rgbBytes: await _toSquareRgb(imageBytes),
rgbBytes: await _toSquareRgb(imageBytes, variant: variant),
ratePoint: aeicRatePointForUi(rate),
);
final bitstream = result?.bitstream;
@@ -1300,14 +1302,18 @@ class ImageCodecService extends ChangeNotifier implements ImageSendCodec {
static const int _bytesPerPixel = 3;
/// Centre-crops [imageBytes] to a square and scales it to
/// [kImageCodecSquareSize], returning packed 8-bit RGB.
/// Stretches the [variant] region of [imageBytes] (see [rerollSourceRect];
/// 0 is the whole frame) to a [kImageCodecSquareSize] square, returning
/// packed 8-bit RGB.
///
/// Uses `dart:ui` rather than a decode package so this adds no dependency.
/// It runs on the root isolate, which is acceptable: it is a single 512x512
/// raster, three orders of magnitude cheaper than the 1385 GFLOP inference
/// that follows in the worker.
Future<Uint8List> _toSquareRgb(Uint8List imageBytes) async {
Future<Uint8List> _toSquareRgb(
Uint8List imageBytes, {
int variant = 0,
}) async {
final codec = await ui.instantiateImageCodec(imageBytes);
final frame = await codec.getNextFrame();
final source = frame.image;
@@ -1317,16 +1323,11 @@ class ImageCodecService extends ChangeNotifier implements ImageSendCodec {
// Aspect ratio is deliberately not preserved: the codec's graph is
// statically 512x512, so something has to give, and losing 25% of a 4:3
// photo (44% of 16:9, or the subject's head in a portrait) is worse than
// distorting it. Nothing outside the frame is discarded this way.
// NOTE: the aspect ratio is NOT transmitted, so the receiver cannot undo
// the stretch — see the note in image_chunk_transport.dart on the
// metadata byte.
final src = ui.Rect.fromLTWH(
0,
0,
source.width.toDouble(),
source.height.toDouble(),
);
// distorting it. The source shape travels in the metadata byte's aspect
// nibble, so the receiver un-stretches it. Re-roll variants trim at most
// ~2% at an edge, so they keep the same shape and aspect code.
final r = rerollSourceRect(source.width, source.height, variant);
final src = ui.Rect.fromLTWH(r.left, r.top, r.width, r.height);
final dstSize = kImageCodecSquareSize.toDouble();
final recorder = ui.PictureRecorder();
ui.Canvas(recorder).drawImageRect(
+66 -6
View File
@@ -197,13 +197,30 @@ class ReceivedImageEntry {
/// One chunk was rebuilt from the XOR parity chunk.
final bool recoveredWithParity;
/// This is the local user's own send. Its PNG is their real 512x512 crop, so
/// [synthesized] is false and the R6 label must NOT be shown.
/// This is the local user's own send.
final bool isOutgoing;
/// Whether the pixels are generative-model output. Derived, never supplied:
/// `synthesized == !isOutgoing`.
bool get synthesized => !isOutgoing;
/// Whether the pixels are generative-model output, so the R6 label must be
/// shown. Always true for received images. For the user's own send it is
/// true when the PNG is the version they picked, decoded as recipients see
/// it, and false only when it is the stretched original (a device that
/// could not decode).
final bool synthesized;
/// Our own image only: repeater echoes heard for each chunk, data chunks
/// first and the parity chunk last. Null when not tracked.
final List<int>? chunkEchoes;
/// The fewest echoes any data chunk got: every data chunk was heard
/// repeated at least this many times. Null when not tracked.
int? get minDataChunkEchoes {
final echoes = chunkEchoes;
if (echoes == null || echoes.isEmpty) return null;
final data = echoes.length > totalChunks
? echoes.sublist(0, totalChunks)
: echoes;
return data.reduce((a, b) => a < b ? a : b);
}
final int? decodeMs;
final String? error;
@@ -237,6 +254,8 @@ class ReceivedImageEntry {
this.metadataAssumed = true,
this.recoveredWithParity = false,
this.isOutgoing = false,
bool? synthesized,
this.chunkEchoes,
this.decodeMs,
this.error,
this.needsManualDecode = false,
@@ -245,7 +264,7 @@ class ReceivedImageEntry {
this.bitstreamByteCount = 0,
this.pngByteCount = 0,
this.pngBytes,
});
}) : synthesized = synthesized ?? !isOutgoing;
ImageStreamKey get key => ImageStreamKey(
senderPrefix: senderPrefix,
@@ -282,6 +301,7 @@ class ReceivedImageEntry {
int? bitstreamByteCount,
int? pngByteCount,
Object? pngBytes = _unset,
List<int>? chunkEchoes,
}) {
return ReceivedImageEntry(
streamId: streamId,
@@ -299,6 +319,8 @@ class ReceivedImageEntry {
metadataAssumed: metadataAssumed ?? this.metadataAssumed,
recoveredWithParity: recoveredWithParity ?? this.recoveredWithParity,
isOutgoing: isOutgoing,
synthesized: synthesized,
chunkEchoes: chunkEchoes ?? this.chunkEchoes,
decodeMs: identical(decodeMs, _unset) ? this.decodeMs : decodeMs as int?,
error: identical(error, _unset) ? this.error : error as String?,
needsManualDecode: needsManualDecode ?? this.needsManualDecode,
@@ -329,6 +351,7 @@ class ReceivedImageEntry {
'recoveredWithParity': recoveredWithParity,
'isOutgoing': isOutgoing,
'synthesized': synthesized,
if (chunkEchoes != null) 'chunkEchoes': chunkEchoes,
'needsManualDecode': needsManualDecode,
'bitstreamStored': bitstreamStored,
'pngStored': pngStored,
@@ -357,6 +380,10 @@ class ReceivedImageEntry {
metadataAssumed: json['metadataAssumed'] as bool? ?? true,
recoveredWithParity: json['recoveredWithParity'] as bool? ?? false,
isOutgoing: json['isOutgoing'] as bool? ?? false,
synthesized: json['synthesized'] as bool?,
chunkEchoes: (json['chunkEchoes'] as List<dynamic>?)
?.whereType<int>()
.toList(),
decodeMs: json['decodeMs'] as int?,
error: json['error'] as String?,
needsManualDecode: json['needsManualDecode'] as bool? ?? false,
@@ -1041,6 +1068,9 @@ class ReceivedImageStore extends ChangeNotifier {
required AeicRatePoint rate,
required int chunkCount,
int aspectCode = 0,
bool synthesized = false,
List<int>? chunkEchoes,
int payloadBytes = 0,
DateTime? at,
}) async {
final now = at ?? _clock();
@@ -1069,6 +1099,11 @@ class ReceivedImageStore extends ChangeNotifier {
aspectCode: aspectCode,
metadataAssumed: false,
isOutgoing: true,
synthesized: synthesized,
chunkEchoes: chunkEchoes,
// The size of what was sent, for the caption and packet info. The
// bitstream itself is not kept, so bitstreamStored stays false.
bitstreamByteCount: payloadBytes,
pngStored: pngWritten,
pngByteCount: pngWritten ? previewPng.length : 0,
pngBytes: previewPng,
@@ -1078,6 +1113,31 @@ class ReceivedImageStore extends ChangeNotifier {
return stored;
}
/// Records repeater echoes of the user's own image (see
/// `MeshCoreConnector.ownImageEchoes`). Echoes that land before the image is
/// registered are passed to [registerOutgoing] instead.
Future<void> setOutgoingEchoes({
required int channelIndex,
required int senderPrefix,
required int imgId,
required List<int> echoes,
}) async {
ReceivedImageEntry? match;
for (final entry in _entries.values) {
if (!entry.isOutgoing ||
entry.imgId != imgId ||
entry.channelIndex != channelIndex ||
entry.senderPrefix != senderPrefix) {
continue;
}
if (match == null || entry.firstSeen.isAfter(match.firstSeen)) {
match = entry;
}
}
if (match == null) return;
await _store(match.copyWith(chunkEchoes: List<int>.unmodifiable(echoes)));
}
// ---- decode scheduling ---------------------------------------------------
/// App lifecycle gate. Backgrounding cancels an in-flight decode and returns