forked from Blendman974/faceclaw
Remove remnants of BMP format from FaceclawBleCommunicator etc
This commit is contained in:
1 parent
b738ce4bb2
commit
e96011ef47
3 files changed
+163
-227
No files matched your search
@@ -120,20 +120,25 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
private PowerManager.WakeLock g2ScreenWakeLock;
|
private PowerManager.WakeLock g2ScreenWakeLock;
|
||||||
|
|
||||||
private String displayedFingerprint = "";
|
private String displayedFingerprint = "";
|
||||||
private byte[] displayedBmp = new byte[0];
|
|
||||||
// The frame the firmware shadow will hold once the current image pipeline
|
// The frame the firmware shadow will hold once the current image pipeline
|
||||||
// drains: the most recently ENQUEUED image, which is the correct base for the
|
// drains: the most recently ENQUEUED image (headerless packed 4bpp, see
|
||||||
// next delta when frames are pipelined (displayedBmp only reflects the last
|
// BmpUtil.pack4bppFromGray8), which is the correct base for the next delta
|
||||||
// ACKED frame and lags behind). Set at enqueue; cleared whenever the image
|
// when frames are pipelined. Set at enqueue; cleared whenever the image
|
||||||
// pipeline is cleared (clearAllMessagesLocked / clearMessagesOfKindLocked
|
// pipeline is cleared (clearAllMessagesLocked / clearMessagesOfKindLocked
|
||||||
// "image"), so it is only ever read while it holds a valid current-session base.
|
// "image"), so it is only ever read while it holds a valid current-session base.
|
||||||
private byte[] lastEnqueuedImageBmp = new byte[0];
|
private byte[] lastEnqueuedPacked = new byte[0];
|
||||||
|
private int lastEnqueuedWidth;
|
||||||
|
private int lastEnqueuedHeight;
|
||||||
private String lastEnqueuedFingerprint = "";
|
private String lastEnqueuedFingerprint = "";
|
||||||
private final Map<Integer, BleImageOptimizer.ImageUpdateStats> imageUpdateStats = new HashMap<>();
|
private final Map<Integer, BleImageOptimizer.ImageUpdateStats> imageUpdateStats = new HashMap<>();
|
||||||
|
|
||||||
private final Object desiredTilesLock = new Object();
|
private final Object desiredTilesLock = new Object();
|
||||||
private String desiredFingerprint = "";
|
private String desiredFingerprint = "";
|
||||||
private byte[] desiredBmp = new byte[0];
|
// Headerless packed 4bpp frame (see BmpUtil.pack4bppFromGray8) plus its
|
||||||
|
// pixel dimensions.
|
||||||
|
private byte[] desiredPacked = new byte[0];
|
||||||
|
private int desiredWidth;
|
||||||
|
private int desiredHeight;
|
||||||
private int desiredPaintMs;
|
private int desiredPaintMs;
|
||||||
private int desiredFrameId;
|
private int desiredFrameId;
|
||||||
// Highest compositor sequence stored as the desired frame; composites that
|
// Highest compositor sequence stored as the desired frame; composites that
|
||||||
@@ -492,8 +497,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
public void setSurfaceVisible(String id, boolean visible) {
|
public void setSurfaceVisible(String id, boolean visible) {
|
||||||
compositor.setSurfaceVisible(id, visible);
|
compositor.setSurfaceVisible(id, visible);
|
||||||
SurfaceCompositor.Composite composite = compositor.composite();
|
SurfaceCompositor.Composite composite = compositor.composite();
|
||||||
byte[] bmp = BmpUtil.build4bppBmp(composite.gray, composite.width, composite.height);
|
byte[] packed = BmpUtil.pack4bppFromGray8(composite.gray, composite.width, composite.height);
|
||||||
storeDesiredComposite(composite, bmp, 0, 0);
|
storeDesiredComposite(composite, packed, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -504,8 +509,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
public void setScreenBlanked(boolean blanked) {
|
public void setScreenBlanked(boolean blanked) {
|
||||||
compositor.setBlanked(blanked);
|
compositor.setBlanked(blanked);
|
||||||
SurfaceCompositor.Composite composite = compositor.composite();
|
SurfaceCompositor.Composite composite = compositor.composite();
|
||||||
byte[] bmp = BmpUtil.build4bppBmp(composite.gray, composite.width, composite.height);
|
byte[] packed = BmpUtil.pack4bppFromGray8(composite.gray, composite.width, composite.height);
|
||||||
storeDesiredComposite(composite, bmp, 0, 0);
|
storeDesiredComposite(composite, packed, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -547,16 +552,17 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
SurfaceCompositor.Composite composite = compositor.applyAndComposite(
|
SurfaceCompositor.Composite composite = compositor.applyAndComposite(
|
||||||
surfaceId, pixels8bpp, rectX, rectY, rectWidth, rectHeight, contentFingerprint);
|
surfaceId, pixels8bpp, rectX, rectY, rectWidth, rectHeight, contentFingerprint);
|
||||||
FrameTimings.getInstance().spanEnd(frameId, "composite");
|
FrameTimings.getInstance().spanEnd(frameId, "composite");
|
||||||
// Convert the composited 8bpp buffer into the BMP wire format here so
|
// Pack the composited 8bpp buffer down to the headerless 4bpp frame
|
||||||
// that all framing concerns live on the Java side.
|
// format the wire planners consume; BMP framing is added later only on
|
||||||
FrameTimings.getInstance().spanStart(frameId, "bmp-convert");
|
// the rare paths that still need it (warmup, uncompressed fallback).
|
||||||
byte[] bmp = BmpUtil.build4bppBmp(composite.gray, composite.width, composite.height);
|
FrameTimings.getInstance().spanStart(frameId, "pack-4bpp");
|
||||||
FrameTimings.getInstance().spanEnd(frameId, "bmp-convert");
|
byte[] packed = BmpUtil.pack4bppFromGray8(composite.gray, composite.width, composite.height);
|
||||||
storeDesiredComposite(composite, bmp, paintMs, frameId);
|
FrameTimings.getInstance().spanEnd(frameId, "pack-4bpp");
|
||||||
|
storeDesiredComposite(composite, packed, paintMs, frameId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Store a composite as the desired frame unless a newer one won the race. */
|
/** Store a composite as the desired frame unless a newer one won the race. */
|
||||||
private void storeDesiredComposite(SurfaceCompositor.Composite composite, byte[] bmp, int paintMs, int frameId) {
|
private void storeDesiredComposite(SurfaceCompositor.Composite composite, byte[] packed, int paintMs, int frameId) {
|
||||||
int supersededFrameId = 0;
|
int supersededFrameId = 0;
|
||||||
boolean stale = false;
|
boolean stale = false;
|
||||||
synchronized (desiredTilesLock) {
|
synchronized (desiredTilesLock) {
|
||||||
@@ -567,7 +573,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
} else {
|
} else {
|
||||||
lastStoredCompositeSeq = composite.seq;
|
lastStoredCompositeSeq = composite.seq;
|
||||||
supersededFrameId = desiredFrameId;
|
supersededFrameId = desiredFrameId;
|
||||||
desiredBmp = bmp;
|
desiredPacked = packed;
|
||||||
|
desiredWidth = composite.width;
|
||||||
|
desiredHeight = composite.height;
|
||||||
desiredFingerprint = composite.fingerprint;
|
desiredFingerprint = composite.fingerprint;
|
||||||
desiredPaintMs = paintMs;
|
desiredPaintMs = paintMs;
|
||||||
desiredFrameId = frameId;
|
desiredFrameId = frameId;
|
||||||
@@ -644,7 +652,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
/**
|
/**
|
||||||
* Start a fresh EvenHub plugin task on the existing BLE transport, then let
|
* Start a fresh EvenHub plugin task on the existing BLE transport, then let
|
||||||
* the session driver create the layout, warm up the image path, and send
|
* the session driver create the layout, warm up the image path, and send
|
||||||
* desiredBmp.
|
* the desired frame.
|
||||||
*/
|
*/
|
||||||
public boolean resumeEvenHubSession() {
|
public boolean resumeEvenHubSession() {
|
||||||
int claimGeneration = 0;
|
int claimGeneration = 0;
|
||||||
@@ -695,7 +703,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
audioCaptureActive = false;
|
audioCaptureActive = false;
|
||||||
clearAllMessagesPreservingWakeLeaseLocked("EvenHub resume");
|
clearAllMessagesPreservingWakeLeaseLocked("EvenHub resume");
|
||||||
displayedFingerprint = "";
|
displayedFingerprint = "";
|
||||||
displayedBmp = new byte[0];
|
|
||||||
imageRetryAfterMs = 0;
|
imageRetryAfterMs = 0;
|
||||||
lastHeartbeatSentAtMs = 0;
|
lastHeartbeatSentAtMs = 0;
|
||||||
lastHeartbeatAckedAtMs = 0;
|
lastHeartbeatAckedAtMs = 0;
|
||||||
@@ -728,7 +735,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
fixedLayoutCreated = false;
|
fixedLayoutCreated = false;
|
||||||
warmedUp = false;
|
warmedUp = false;
|
||||||
displayedFingerprint = "";
|
displayedFingerprint = "";
|
||||||
displayedBmp = new byte[0];
|
|
||||||
};
|
};
|
||||||
message.onTimeout = () -> {
|
message.onTimeout = () -> {
|
||||||
if (reconnectOnTimeout) {
|
if (reconnectOnTimeout) {
|
||||||
@@ -954,7 +960,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
fixedLayoutCreated = false;
|
fixedLayoutCreated = false;
|
||||||
warmedUp = false;
|
warmedUp = false;
|
||||||
displayedFingerprint = "";
|
displayedFingerprint = "";
|
||||||
displayedBmp = new byte[0];
|
|
||||||
clearAllMessagesLocked("firmware exit event");
|
clearAllMessagesLocked("firmware exit event");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1049,7 +1054,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
audioPacketListener = null;
|
audioPacketListener = null;
|
||||||
clearAllMessagesLocked("connection lost");
|
clearAllMessagesLocked("connection lost");
|
||||||
displayedFingerprint = "";
|
displayedFingerprint = "";
|
||||||
displayedBmp = new byte[0];
|
|
||||||
reconnectAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.RECONNECT_DELAY_MS;
|
reconnectAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.RECONNECT_DELAY_MS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1081,7 +1085,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
warmedUp = false;
|
warmedUp = false;
|
||||||
clearAllMessagesLocked("session ready");
|
clearAllMessagesLocked("session ready");
|
||||||
displayedFingerprint = "";
|
displayedFingerprint = "";
|
||||||
displayedBmp = new byte[0];
|
|
||||||
lastAckAtMs = SystemClock.elapsedRealtime();
|
lastAckAtMs = SystemClock.elapsedRealtime();
|
||||||
lastIncomingAtMs = lastAckAtMs;
|
lastIncomingAtMs = lastAckAtMs;
|
||||||
lastConnectionOrInputAtMs = lastAckAtMs;
|
lastConnectionOrInputAtMs = lastAckAtMs;
|
||||||
@@ -1384,7 +1387,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
&& windowHasRoom && !hasPendingImageLocked()
|
&& windowHasRoom && !hasPendingImageLocked()
|
||||||
&& now >= imageRetryAfterMs
|
&& now >= imageRetryAfterMs
|
||||||
&& !getDesiredFingerprint().equals(lastEnqueuedFingerprint)) {
|
&& !getDesiredFingerprint().equals(lastEnqueuedFingerprint)) {
|
||||||
// Enqueue the next frame's delta against lastEnqueuedImageBmp
|
// Enqueue the next frame's delta against lastEnqueuedPacked
|
||||||
// (what the shadow will be), so it can pipeline behind an
|
// (what the shadow will be), so it can pipeline behind an
|
||||||
// image still awaiting its ack.
|
// image still awaiting its ack.
|
||||||
Log.i(TAG, "Enqueued image update");
|
Log.i(TAG, "Enqueued image update");
|
||||||
@@ -1744,7 +1747,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
clearMessagesOfKindLocked("startup-text-probe");
|
clearMessagesOfKindLocked("startup-text-probe");
|
||||||
fixedLayoutCreated = true;
|
fixedLayoutCreated = true;
|
||||||
displayedFingerprint = "";
|
displayedFingerprint = "";
|
||||||
displayedBmp = new byte[0];
|
|
||||||
};
|
};
|
||||||
message.onTimeout = () -> {
|
message.onTimeout = () -> {
|
||||||
if (startupProbePending) {
|
if (startupProbePending) {
|
||||||
@@ -1770,7 +1772,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
fixedLayoutCreated = true;
|
fixedLayoutCreated = true;
|
||||||
warmedUp = false;
|
warmedUp = false;
|
||||||
displayedFingerprint = "";
|
displayedFingerprint = "";
|
||||||
displayedBmp = new byte[0];
|
|
||||||
logLine("existing dashboard layout accepted text probe; image warmup still required");
|
logLine("existing dashboard layout accepted text probe; image warmup still required");
|
||||||
};
|
};
|
||||||
message.onTimeout = () -> {
|
message.onTimeout = () -> {
|
||||||
@@ -1786,14 +1787,17 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void enqueueWarmupLocked() {
|
private void enqueueWarmupLocked() {
|
||||||
byte[] bmp;
|
int width;
|
||||||
|
int height;
|
||||||
synchronized (desiredTilesLock) {
|
synchronized (desiredTilesLock) {
|
||||||
bmp = desiredBmp;
|
width = desiredWidth;
|
||||||
|
height = desiredHeight;
|
||||||
}
|
}
|
||||||
if (bmp == null || bmp.length == 0) {
|
// A blank full-size raw BMP: warmup primes the firmware image path with
|
||||||
bmp = new byte[] {0};
|
// a frame shaped like the real ones, without flashing stale content.
|
||||||
}
|
byte[] bmp = (width > 0 && height > 0)
|
||||||
bmp = BmpUtil.buildBlankWarmupBmp(bmp);
|
? BmpUtil.build4bppBmpFromPacked(new byte[((width + 1) >> 1) * height], width, height)
|
||||||
|
: new byte[] {0};
|
||||||
BleProtocol.ImageTileOptions tile = DASHBOARD_TILE;
|
BleProtocol.ImageTileOptions tile = DASHBOARD_TILE;
|
||||||
int sessionId = nextMapSessionId();
|
int sessionId = nextMapSessionId();
|
||||||
List<BleProtocol.ImageFragment> fragments = BleImageOptimizer.planImageFragments(bmp, ConnectionOptions.IMAGE_FRAGMENT_SIZE);
|
List<BleProtocol.ImageFragment> fragments = BleImageOptimizer.planImageFragments(bmp, ConnectionOptions.IMAGE_FRAGMENT_SIZE);
|
||||||
@@ -1813,7 +1817,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
warmedUp = false;
|
warmedUp = false;
|
||||||
clearMessagesOfKindLocked("warmup");
|
clearMessagesOfKindLocked("warmup");
|
||||||
displayedFingerprint = "";
|
displayedFingerprint = "";
|
||||||
displayedBmp = new byte[0];
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
logLine("queue blank warmup");
|
logLine("queue blank warmup");
|
||||||
@@ -1840,19 +1843,24 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
|
|
||||||
private void enqueueDesiredImageLocked() {
|
private void enqueueDesiredImageLocked() {
|
||||||
String fingerprint = getDesiredFingerprint();
|
String fingerprint = getDesiredFingerprint();
|
||||||
byte[] bmp;
|
byte[] packed;
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
int paintMs;
|
int paintMs;
|
||||||
int frameId;
|
int frameId;
|
||||||
synchronized (desiredTilesLock) {
|
synchronized (desiredTilesLock) {
|
||||||
bmp = desiredBmp;
|
packed = desiredPacked;
|
||||||
|
width = desiredWidth;
|
||||||
|
height = desiredHeight;
|
||||||
paintMs = desiredPaintMs;
|
paintMs = desiredPaintMs;
|
||||||
frameId = desiredFrameId;
|
frameId = desiredFrameId;
|
||||||
desiredFrameId = 0;
|
desiredFrameId = 0;
|
||||||
}
|
}
|
||||||
if (bmp == null) {
|
if (packed == null) {
|
||||||
bmp = new byte[0];
|
packed = new byte[0];
|
||||||
}
|
}
|
||||||
if (Arrays.equals(bmp, lastEnqueuedImageBmp)) {
|
if (lastEnqueuedWidth == width && lastEnqueuedHeight == height
|
||||||
|
&& Arrays.equals(packed, lastEnqueuedPacked)) {
|
||||||
lastEnqueuedFingerprint = fingerprint;
|
lastEnqueuedFingerprint = fingerprint;
|
||||||
finishFrame(frameId, "discarded: image content identical to displayed");
|
finishFrame(frameId, "discarded: image content identical to displayed");
|
||||||
return;
|
return;
|
||||||
@@ -1861,14 +1869,15 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
FrameTimings.getInstance().spanStart(frameId, "compress-and-plan");
|
FrameTimings.getInstance().spanStart(frameId, "compress-and-plan");
|
||||||
// Incremental (mode 3 bounding box) update against the last ENQUEUED frame
|
// Incremental (mode 3 bounding box) update against the last ENQUEUED frame
|
||||||
// (the base the firmware shadow will hold when this update is applied).
|
// (the base the firmware shadow will hold when this update is applied).
|
||||||
// lastEnqueuedImageBmp is cleared whenever the image pipeline is cleared, so
|
// lastEnqueuedPacked is cleared whenever the image pipeline is cleared, so
|
||||||
// a non-empty value means the display base is trusted.
|
// a non-empty value means the display base is trusted.
|
||||||
byte[] incrementalPayload = null;
|
byte[] incrementalPayload = null;
|
||||||
String incrementalLog = null;
|
String incrementalLog = null;
|
||||||
if (connectionOptions.INCREMENTAL_FRAMES && lastEnqueuedImageBmp.length > 0) {
|
if (connectionOptions.INCREMENTAL_FRAMES && lastEnqueuedPacked.length > 0
|
||||||
|
&& lastEnqueuedWidth == width && lastEnqueuedHeight == height) {
|
||||||
int baseFid = nextImageFrameId;
|
int baseFid = nextImageFrameId;
|
||||||
BleImageOptimizer.IncrementalPlan single =
|
BleImageOptimizer.IncrementalPlan single =
|
||||||
BleImageOptimizer.buildIncrementalImagePayload(lastEnqueuedImageBmp, bmp, baseFid);
|
BleImageOptimizer.buildIncrementalImagePayload(lastEnqueuedPacked, packed, width, height, baseFid);
|
||||||
if (single != null) {
|
if (single != null) {
|
||||||
incrementalPayload = single.payload;
|
incrementalPayload = single.payload;
|
||||||
// advance only when a delta is actually emitted, so consecutive
|
// advance only when a delta is actually emitted, so consecutive
|
||||||
@@ -1886,7 +1895,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
if (connectionOptions.MULTI_RECT_FRAMES
|
if (connectionOptions.MULTI_RECT_FRAMES
|
||||||
&& (single.clusterCount > 1 || single.payload.length > ConnectionOptions.MULTI_RECT_MIN_PAYLOAD)) {
|
&& (single.clusterCount > 1 || single.payload.length > ConnectionOptions.MULTI_RECT_MIN_PAYLOAD)) {
|
||||||
BleImageOptimizer.MultiRectPlan multi = BleImageOptimizer.buildMultiRectImagePayload(
|
BleImageOptimizer.MultiRectPlan multi = BleImageOptimizer.buildMultiRectImagePayload(
|
||||||
lastEnqueuedImageBmp, bmp, baseFid, ConnectionOptions.MULTI_RECT_MAX_RECTS);
|
lastEnqueuedPacked, packed, width, height, baseFid, ConnectionOptions.MULTI_RECT_MAX_RECTS);
|
||||||
if (multi != null && multi.payload.length < single.payload.length) {
|
if (multi != null && multi.payload.length < single.payload.length) {
|
||||||
incrementalPayload = multi.payload;
|
incrementalPayload = multi.payload;
|
||||||
nextImageFrameId = multi.nextFid; // rectCount fids consumed
|
nextImageFrameId = multi.nextFid; // rectCount fids consumed
|
||||||
@@ -1898,8 +1907,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
BleImageOptimizer.TileImagePlan plan = incrementalPayload != null
|
BleImageOptimizer.TileImagePlan plan = incrementalPayload != null
|
||||||
? new BleImageOptimizer.TileImagePlan(0, DASHBOARD_TILE, bmp, nextMapSessionId(), incrementalPayload)
|
? new BleImageOptimizer.TileImagePlan(0, DASHBOARD_TILE, packed, width, height, nextMapSessionId(), incrementalPayload)
|
||||||
: new BleImageOptimizer.TileImagePlan(0, DASHBOARD_TILE, bmp, nextMapSessionId());
|
: new BleImageOptimizer.TileImagePlan(0, DASHBOARD_TILE, packed, width, height, nextMapSessionId());
|
||||||
plan.fragments = BleImageOptimizer.planImageFragments(plan.payload, ConnectionOptions.IMAGE_FRAGMENT_SIZE);
|
plan.fragments = BleImageOptimizer.planImageFragments(plan.payload, ConnectionOptions.IMAGE_FRAGMENT_SIZE);
|
||||||
FrameTimings.getInstance().spanEnd(frameId, "compress-and-plan");
|
FrameTimings.getInstance().spanEnd(frameId, "compress-and-plan");
|
||||||
if (incrementalLog != null) {
|
if (incrementalLog != null) {
|
||||||
@@ -1915,8 +1924,11 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
}
|
}
|
||||||
// This frame is now the base for the next delta (it will be the firmware
|
// This frame is now the base for the next delta (it will be the firmware
|
||||||
// shadow once applied), even though it hasn't been acked yet — that is what
|
// shadow once applied), even though it hasn't been acked yet — that is what
|
||||||
// lets the next frame pipeline behind it. plan.bmp is the full frame.
|
// lets the next frame pipeline behind it. plan.packed is the full frame;
|
||||||
lastEnqueuedImageBmp = BmpUtil.copyTileBmp(plan.bmp);
|
// frames are immutable by convention, so referencing it is safe.
|
||||||
|
lastEnqueuedPacked = plan.packed;
|
||||||
|
lastEnqueuedWidth = plan.width;
|
||||||
|
lastEnqueuedHeight = plan.height;
|
||||||
lastEnqueuedFingerprint = fingerprint;
|
lastEnqueuedFingerprint = fingerprint;
|
||||||
|
|
||||||
FrameTimings.getInstance().log(frameId, "queued image update#" + updateId
|
FrameTimings.getInstance().log(frameId, "queued image update#" + updateId
|
||||||
@@ -1944,9 +1956,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
// with active rendering.
|
// with active rendering.
|
||||||
lastHeartbeatAckedAtMs = SystemClock.elapsedRealtime();
|
lastHeartbeatAckedAtMs = SystemClock.elapsedRealtime();
|
||||||
logImageUpdateAckLandmarkLocked(message);
|
logImageUpdateAckLandmarkLocked(message);
|
||||||
if (message.tileIndex >= 0 && !hasPendingOrInflightTileLocked(message.tileIndex)) {
|
|
||||||
displayedBmp = BmpUtil.copyTileBmp(plan.bmp);
|
|
||||||
}
|
|
||||||
boolean imageStillInFlight = false;
|
boolean imageStillInFlight = false;
|
||||||
for (OutboundMessage inFlight : inFlightMessages) {
|
for (OutboundMessage inFlight : inFlightMessages) {
|
||||||
if ("image".equals(inFlight.kind)) {
|
if ("image".equals(inFlight.kind)) {
|
||||||
@@ -1971,7 +1980,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
discardImageUpdateStatsLocked(message.imageUpdateId, "image ack timeout (will retry)");
|
discardImageUpdateStatsLocked(message.imageUpdateId, "image ack timeout (will retry)");
|
||||||
clearMessagesOfKindLocked("image");
|
clearMessagesOfKindLocked("image");
|
||||||
displayedFingerprint = "";
|
displayedFingerprint = "";
|
||||||
displayedBmp = new byte[0];
|
|
||||||
imageRetryAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.IMAGE_RETRY_DELAY_MS;
|
imageRetryAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.IMAGE_RETRY_DELAY_MS;
|
||||||
};
|
};
|
||||||
pendingMessages.addLast(message);
|
pendingMessages.addLast(message);
|
||||||
@@ -2063,7 +2071,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
warmedUp = false;
|
warmedUp = false;
|
||||||
startupProbePending = false;
|
startupProbePending = false;
|
||||||
displayedFingerprint = "";
|
displayedFingerprint = "";
|
||||||
displayedBmp = new byte[0];
|
|
||||||
finishDesiredFrameLocked("discarded: glasses charging");
|
finishDesiredFrameLocked("discarded: glasses charging");
|
||||||
logLine("glasses are charging; pausing display communication");
|
logLine("glasses are charging; pausing display communication");
|
||||||
setStateDisplay("charging", chargingStatusText(battery));
|
setStateDisplay("charging", chargingStatusText(battery));
|
||||||
@@ -2220,7 +2227,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
if ("image".equals(kind)) {
|
if ("image".equals(kind)) {
|
||||||
// The image pipeline was flushed (e.g. ack timeout -> keyframe resync):
|
// The image pipeline was flushed (e.g. ack timeout -> keyframe resync):
|
||||||
// drop the pipelined delta base so the next image is a full keyframe.
|
// drop the pipelined delta base so the next image is a full keyframe.
|
||||||
lastEnqueuedImageBmp = new byte[0];
|
lastEnqueuedPacked = new byte[0];
|
||||||
lastEnqueuedFingerprint = "";
|
lastEnqueuedFingerprint = "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2230,7 +2237,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
clearInFlightMessagesLocked(reason);
|
clearInFlightMessagesLocked(reason);
|
||||||
// The image pipeline is gone: drop the pipelined delta base so the next
|
// The image pipeline is gone: drop the pipelined delta base so the next
|
||||||
// image is a full keyframe rather than a delta onto a stale base.
|
// image is a full keyframe rather than a delta onto a stale base.
|
||||||
lastEnqueuedImageBmp = new byte[0];
|
lastEnqueuedPacked = new byte[0];
|
||||||
lastEnqueuedFingerprint = "";
|
lastEnqueuedFingerprint = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2260,7 +2267,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
clearInFlightMessagesLocked(reason);
|
clearInFlightMessagesLocked(reason);
|
||||||
lastEnqueuedImageBmp = new byte[0];
|
lastEnqueuedPacked = new byte[0];
|
||||||
lastEnqueuedFingerprint = "";
|
lastEnqueuedFingerprint = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2338,7 +2345,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
chargingMode = false;
|
chargingMode = false;
|
||||||
imageRetryAfterMs = 0;
|
imageRetryAfterMs = 0;
|
||||||
displayedFingerprint = "";
|
displayedFingerprint = "";
|
||||||
displayedBmp = new byte[0];
|
|
||||||
faceclawWakePendingNonce = -1;
|
faceclawWakePendingNonce = -1;
|
||||||
lastFaceclawWakeLeaseQueuedAtMs = 0;
|
lastFaceclawWakeLeaseQueuedAtMs = 0;
|
||||||
faceclawWakeControlSentCount = 0;
|
faceclawWakeControlSentCount = 0;
|
||||||
@@ -2377,26 +2383,11 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
lastFaceclawWakeLeaseQueuedAtMs = 0;
|
lastFaceclawWakeLeaseQueuedAtMs = 0;
|
||||||
faceclawWakeControlSentCount = 0;
|
faceclawWakeControlSentCount = 0;
|
||||||
displayedFingerprint = "";
|
displayedFingerprint = "";
|
||||||
displayedBmp = new byte[0];
|
|
||||||
// Deliberately not clearing silentMode: it is a property of the glasses,
|
// Deliberately not clearing silentMode: it is a property of the glasses,
|
||||||
// not of our session, and silent mode blocks app launches, so it can be
|
// not of our session, and silent mode blocks app launches, so it can be
|
||||||
// the very cause of the session teardown that got us here.
|
// the very cause of the session teardown that got us here.
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean hasPendingOrInflightTileLocked(int tileIndex) {
|
|
||||||
for (OutboundMessage queued : pendingMessages) {
|
|
||||||
if (queued.tileIndex == tileIndex && ("image".equals(queued.kind) || "warmup".equals(queued.kind))) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (OutboundMessage inFlight : inFlightMessages) {
|
|
||||||
if (inFlight.tileIndex == tileIndex && ("image".equals(inFlight.kind) || "warmup".equals(inFlight.kind))) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void emitRingEvent(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode, int frameId) {
|
private void emitRingEvent(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode, int frameId) {
|
||||||
final FaceclawBleCommunicatorListener current = listener;
|
final FaceclawBleCommunicatorListener current = listener;
|
||||||
if (current == null) {
|
if (current == null) {
|
||||||
|
|||||||
+49
-50
@@ -73,28 +73,37 @@ public final class BleImageOptimizer {
|
|||||||
public static final class TileImagePlan {
|
public static final class TileImagePlan {
|
||||||
final int tileIndex;
|
final int tileIndex;
|
||||||
final BleProtocol.ImageTileOptions tile;
|
final BleProtocol.ImageTileOptions tile;
|
||||||
final byte[] bmp; // raw 4bpp BMP, kept for the displayed-tile dedup cache
|
// Headerless packed 4bpp frame (top-down, stride ceil(width/2)), kept for
|
||||||
final byte[] payload; // bytes actually streamed: mode-6 zlib(rle(4bpp)) when it shrinks, else == bmp
|
// the displayed-tile dedup cache. Frames are immutable by convention:
|
||||||
|
// producers always allocate a fresh buffer, so no defensive copy here.
|
||||||
|
public final byte[] packed;
|
||||||
|
public final int width;
|
||||||
|
public final int height;
|
||||||
|
final byte[] payload; // bytes actually streamed: mode-6 zlib(rle(4bpp)) when it shrinks, else a raw BMP
|
||||||
final int sessionId;
|
final int sessionId;
|
||||||
List<BleProtocol.ImageFragment> fragments = Collections.emptyList();
|
List<BleProtocol.ImageFragment> fragments = Collections.emptyList();
|
||||||
|
|
||||||
TileImagePlan(int tileIndex, BleProtocol.ImageTileOptions tile, byte[] bmp, int sessionId) {
|
TileImagePlan(int tileIndex, BleProtocol.ImageTileOptions tile, byte[] packed, int width, int height, int sessionId) {
|
||||||
this.tileIndex = tileIndex;
|
this.tileIndex = tileIndex;
|
||||||
this.tile = tile;
|
this.tile = tile;
|
||||||
this.bmp = BmpUtil.copyTileBmp(bmp);
|
this.packed = packed == null ? new byte[0] : packed;
|
||||||
this.payload = maybeCompress(this.bmp);
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
this.payload = maybeCompress(this.packed, width, height);
|
||||||
this.sessionId = sessionId;
|
this.sessionId = sessionId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plan with an explicit wire payload (e.g. a mode-3 incremental update).
|
* Plan with an explicit wire payload (e.g. a mode-3 incremental update).
|
||||||
* bmp must still be the FULL new frame: it feeds the displayed-frame
|
* packed must still be the FULL new frame: it feeds the displayed-frame
|
||||||
* dedup cache, which after an applied incremental equals the full frame.
|
* dedup cache, which after an applied incremental equals the full frame.
|
||||||
*/
|
*/
|
||||||
TileImagePlan(int tileIndex, BleProtocol.ImageTileOptions tile, byte[] bmp, int sessionId, byte[] payloadOverride) {
|
TileImagePlan(int tileIndex, BleProtocol.ImageTileOptions tile, byte[] packed, int width, int height, int sessionId, byte[] payloadOverride) {
|
||||||
this.tileIndex = tileIndex;
|
this.tileIndex = tileIndex;
|
||||||
this.tile = tile;
|
this.tile = tile;
|
||||||
this.bmp = BmpUtil.copyTileBmp(bmp);
|
this.packed = packed == null ? new byte[0] : packed;
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
this.payload = payloadOverride;
|
this.payload = payloadOverride;
|
||||||
this.sessionId = sessionId;
|
this.sessionId = sessionId;
|
||||||
}
|
}
|
||||||
@@ -137,18 +146,10 @@ public final class BleImageOptimizer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IncrementalPlan buildIncrementalImagePayload(byte[] previousBmp, byte[] newBmp, int frameId) {
|
public static IncrementalPlan buildIncrementalImagePayload(byte[] previous, byte[] next, int width, int height, int frameId) {
|
||||||
int width = BmpUtil.readBmpWidth(newBmp);
|
|
||||||
int height = BmpUtil.readBmpHeight(newBmp);
|
|
||||||
if (width <= 0 || height <= 0
|
|
||||||
|| width != BmpUtil.readBmpWidth(previousBmp)
|
|
||||||
|| height != BmpUtil.readBmpHeight(previousBmp)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
byte[] previous = BmpUtil.pack4bppFromBmp(previousBmp);
|
|
||||||
byte[] next = BmpUtil.pack4bppFromBmp(newBmp);
|
|
||||||
int stride = (width + 1) >> 1;
|
int stride = (width + 1) >> 1;
|
||||||
if (previous.length != stride * height || next.length != stride * height) {
|
if (width <= 0 || height <= 0 || previous == null || next == null
|
||||||
|
|| previous.length != stride * height || next.length != stride * height) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -271,18 +272,10 @@ public final class BleImageOptimizer {
|
|||||||
* single region (use the single-rect path), when it fragments past maxRects, or
|
* single region (use the single-rect path), when it fragments past maxRects, or
|
||||||
* when the frames aren't comparable.
|
* when the frames aren't comparable.
|
||||||
*/
|
*/
|
||||||
public static MultiRectPlan buildMultiRectImagePayload(byte[] previousBmp, byte[] newBmp, int fidStart, int maxRects) {
|
public static MultiRectPlan buildMultiRectImagePayload(byte[] previous, byte[] next, int width, int height, int fidStart, int maxRects) {
|
||||||
int width = BmpUtil.readBmpWidth(newBmp);
|
|
||||||
int height = BmpUtil.readBmpHeight(newBmp);
|
|
||||||
if (width <= 0 || height <= 0
|
|
||||||
|| width != BmpUtil.readBmpWidth(previousBmp)
|
|
||||||
|| height != BmpUtil.readBmpHeight(previousBmp)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
byte[] previous = BmpUtil.pack4bppFromBmp(previousBmp);
|
|
||||||
byte[] next = BmpUtil.pack4bppFromBmp(newBmp);
|
|
||||||
int stride = (width + 1) >> 1;
|
int stride = (width + 1) >> 1;
|
||||||
if (previous.length != stride * height || next.length != stride * height) {
|
if (width <= 0 || height <= 0 || previous == null || next == null
|
||||||
|
|| previous.length != stride * height || next.length != stride * height) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -417,19 +410,16 @@ public final class BleImageOptimizer {
|
|||||||
/**
|
/**
|
||||||
* RLE- then zlib-compress headerless 4bpp pixels for CFW load_image_z mode 6 when
|
* RLE- then zlib-compress headerless 4bpp pixels for CFW load_image_z mode 6 when
|
||||||
* it shrinks the payload. Wire format: [6][zlib(rle(4bpp pixels))]. A raw BMP
|
* it shrinks the payload. Wire format: [6][zlib(rle(4bpp pixels))]. A raw BMP
|
||||||
* (starts 'B') is sent verbatim when compression does not help.
|
* (starts 'B') is sent when compression does not help; that is the only spot
|
||||||
|
* where BMP framing is still built.
|
||||||
*/
|
*/
|
||||||
static byte[] maybeCompress(byte[] bmp) {
|
static byte[] maybeCompress(byte[] packed, int width, int height) {
|
||||||
if (bmp == null || bmp.length == 0) {
|
if (packed == null || packed.length == 0) {
|
||||||
return bmp;
|
return packed;
|
||||||
}
|
|
||||||
byte[] packed = BmpUtil.pack4bppFromBmp(bmp);
|
|
||||||
if (packed.length == 0) {
|
|
||||||
return bmp;
|
|
||||||
}
|
}
|
||||||
byte[] z = deflate(rleEncode(packed));
|
byte[] z = deflate(rleEncode(packed));
|
||||||
if (z == null || z.length + 1 >= packed.length) {
|
if (z == null || z.length + 1 >= packed.length) {
|
||||||
return bmp;
|
return BmpUtil.build4bppBmpFromPacked(packed, width, height);
|
||||||
}
|
}
|
||||||
byte[] out = new byte[z.length + 1];
|
byte[] out = new byte[z.length + 1];
|
||||||
out[0] = 6;
|
out[0] = 6;
|
||||||
@@ -487,24 +477,33 @@ public final class BleImageOptimizer {
|
|||||||
return (i & 1) != 0 ? (b & 0x0f) : (b >> 4);
|
return (i & 1) != 0 ? (b & 0x0f) : (b >> 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Level 6: BEST_COMPRESSION cost 18-109ms per frame on the BLE worker,
|
||||||
|
// but BEST_SPEED inflated typical payloads from ~2.7KB past the 3800-byte
|
||||||
|
// fragment boundary, adding a whole extra ack round trip (~350ms). The
|
||||||
|
// default level keeps payloads under one fragment at about half the CPU.
|
||||||
|
// One long-lived Deflater per thread: constructing one allocates and
|
||||||
|
// initializes a native zlib stream, which is measurable at per-frame rates.
|
||||||
|
private static final ThreadLocal<Deflater> DEFLATER =
|
||||||
|
new ThreadLocal<Deflater>() {
|
||||||
|
@Override protected Deflater initialValue() {
|
||||||
|
return new Deflater(Deflater.DEFAULT_COMPRESSION);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
private static byte[] deflate(byte[] data) {
|
private static byte[] deflate(byte[] data) {
|
||||||
// Level 6: BEST_COMPRESSION cost 18-109ms per frame on the BLE worker,
|
Deflater deflater = DEFLATER.get();
|
||||||
// but BEST_SPEED inflated typical payloads from ~2.7KB past the 3800-byte
|
|
||||||
// fragment boundary, adding a whole extra ack round trip (~350ms). The
|
|
||||||
// default level keeps payloads under one fragment at about half the CPU.
|
|
||||||
Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION);
|
|
||||||
deflater.setInput(data);
|
|
||||||
deflater.finish();
|
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream(Math.max(64, data.length / 3));
|
|
||||||
byte[] buf = new byte[4096];
|
|
||||||
try {
|
try {
|
||||||
|
deflater.setInput(data);
|
||||||
|
deflater.finish();
|
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream(Math.max(64, data.length / 3));
|
||||||
|
byte[] buf = new byte[4096];
|
||||||
while (!deflater.finished()) {
|
while (!deflater.finished()) {
|
||||||
out.write(buf, 0, deflater.deflate(buf));
|
out.write(buf, 0, deflater.deflate(buf));
|
||||||
}
|
}
|
||||||
|
return out.toByteArray();
|
||||||
} finally {
|
} finally {
|
||||||
deflater.end();
|
deflater.reset();
|
||||||
}
|
}
|
||||||
return out.toByteArray();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class ImageUpdateStats {
|
public static final class ImageUpdateStats {
|
||||||
|
|||||||
@@ -9,33 +9,64 @@ public class BmpUtil {
|
|||||||
return Arrays.copyOf(bmp, bmp.length);
|
return Arrays.copyOf(bmp, bmp.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] buildBlankWarmupBmp(byte[] bmp) {
|
// Quantize an 8-bit gray value to a 4-bit nibble. Truncate rather than round
|
||||||
byte[] warmup = BmpUtil.copyTileBmp(bmp);
|
// up at the bottom: the shell's color-key convention paints intentional
|
||||||
int pixelOffset = readBmpPixelOffset(warmup);
|
// black as 1 (0 being transparent), which must land on nibble 0, not a
|
||||||
if (pixelOffset <= 0 || pixelOffset >= warmup.length) {
|
// faintly-visible level 1.
|
||||||
Arrays.fill(warmup, (byte) 0);
|
private static final byte[] GRAY_TO_NIBBLE = new byte[256];
|
||||||
return warmup;
|
static {
|
||||||
|
for (int v = 1; v < 256; v++) {
|
||||||
|
GRAY_TO_NIBBLE[v] = (byte) Math.min(15, (v + 8) >> 4);
|
||||||
}
|
}
|
||||||
Arrays.fill(warmup, pixelOffset, warmup.length, (byte) 0);
|
|
||||||
return warmup;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build a 4bpp grayscale BMP (BITMAPINFOHEADER + 16-entry gray palette,
|
* Pack a full-resolution 8bpp grayscale buffer (row-major, top-to-bottom,
|
||||||
* bottom-up rows padded to a 4-byte stride) from a full-resolution 8bpp
|
* one byte per pixel) into the headerless 4bpp wire format used by CFW
|
||||||
* grayscale buffer. The buffer is row-major, top-to-bottom, one byte per
|
* load_image_z modes 3/6/8: top-down rows, stride ceil(width/2), high
|
||||||
* pixel with values 0..255; each pixel is quantized to a 4-bit nibble.
|
* nibble = left pixel. This is the canonical in-memory frame format; BMP
|
||||||
*
|
* framing (needed only when an uncompressed full frame goes on the wire)
|
||||||
* Building the wire format here, rather than on the TypeScript side, keeps
|
* is added on demand by build4bppBmpFromPacked.
|
||||||
* all framing concerns in one place so future firmware changes that drop or
|
|
||||||
* replace the BMP header (e.g. 8-bit color depth or iframes) only touch Java.
|
|
||||||
*/
|
*/
|
||||||
public static byte[] build4bppBmp(byte[] gray8, int width, int height) {
|
public static byte[] pack4bppFromGray8(byte[] gray8, int width, int height) {
|
||||||
if (width <= 0 || height <= 0) {
|
if (width <= 0 || height <= 0) {
|
||||||
return new byte[0];
|
return new byte[0];
|
||||||
}
|
}
|
||||||
int bytesPerPixelRow = (width + 1) >> 1;
|
int stride = (width + 1) >> 1;
|
||||||
int rowStride = (bytesPerPixelRow + 3) & ~3;
|
byte[] out = new byte[stride * height];
|
||||||
|
if (gray8 == null || gray8.length < width * height) {
|
||||||
|
return out; // short buffer: treat missing pixels as black
|
||||||
|
}
|
||||||
|
int pairs = width >> 1;
|
||||||
|
int src = 0;
|
||||||
|
int dst = 0;
|
||||||
|
for (int y = 0; y < height; y++) {
|
||||||
|
for (int i = 0; i < pairs; i++) {
|
||||||
|
out[dst++] = (byte) ((GRAY_TO_NIBBLE[gray8[src] & 0xff] << 4)
|
||||||
|
| GRAY_TO_NIBBLE[gray8[src + 1] & 0xff]);
|
||||||
|
src += 2;
|
||||||
|
}
|
||||||
|
if ((width & 1) != 0) {
|
||||||
|
out[dst++] = (byte) (GRAY_TO_NIBBLE[gray8[src++] & 0xff] << 4);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrap a packed 4bpp frame in BMP framing (BITMAPINFOHEADER + 16-entry gray
|
||||||
|
* palette, bottom-up rows padded to a 4-byte stride). Only the full-frame
|
||||||
|
* image path still speaks BMP on the wire (warmup, and the fallback when
|
||||||
|
* mode-6 compression does not shrink the frame); everything else uses the
|
||||||
|
* headerless packed format directly.
|
||||||
|
*/
|
||||||
|
public static byte[] build4bppBmpFromPacked(byte[] packed, int width, int height) {
|
||||||
|
int packedStride = (width + 1) >> 1;
|
||||||
|
if (width <= 0 || height <= 0
|
||||||
|
|| packed == null || packed.length < packedStride * height) {
|
||||||
|
return new byte[0];
|
||||||
|
}
|
||||||
|
int rowStride = (packedStride + 3) & ~3;
|
||||||
int pixelDataSize = rowStride * height;
|
int pixelDataSize = rowStride * height;
|
||||||
|
|
||||||
int fileHeaderSize = 14;
|
int fileHeaderSize = 14;
|
||||||
@@ -69,31 +100,12 @@ public class BmpUtil {
|
|||||||
|
|
||||||
for (int bmpRow = 0; bmpRow < height; bmpRow++) {
|
for (int bmpRow = 0; bmpRow < height; bmpRow++) {
|
||||||
int srcY = height - 1 - bmpRow; // BMP rows run bottom-up
|
int srcY = height - 1 - bmpRow; // BMP rows run bottom-up
|
||||||
int srcRowOffset = srcY * width;
|
System.arraycopy(packed, srcY * packedStride,
|
||||||
int rowOffset = pixelOffset + bmpRow * rowStride;
|
buf, pixelOffset + bmpRow * rowStride, packedStride);
|
||||||
for (int x = 0; x < width; x += 2) {
|
|
||||||
int hi = grayToNibble(sampleGray(gray8, srcRowOffset + x));
|
|
||||||
int lo = (x + 1 < width) ? grayToNibble(sampleGray(gray8, srcRowOffset + x + 1)) : 0;
|
|
||||||
buf[rowOffset + (x >> 1)] = (byte) ((hi << 4) | lo);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int sampleGray(byte[] gray8, int index) {
|
|
||||||
return (index >= 0 && index < gray8.length) ? (gray8[index] & 0xff) : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int grayToNibble(int value) {
|
|
||||||
// Truncate rather than round up: the shell's color-key convention
|
|
||||||
// paints intentional black as 1 (0 being transparent), which must
|
|
||||||
// land on nibble 0, not a faintly-visible level 1.
|
|
||||||
if (value <= 0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return Math.min(15, (value+8) >> 4);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void putUint16Le(byte[] buf, int offset, int value) {
|
private static void putUint16Le(byte[] buf, int offset, int value) {
|
||||||
buf[offset] = (byte) (value & 0xff);
|
buf[offset] = (byte) (value & 0xff);
|
||||||
buf[offset + 1] = (byte) ((value >>> 8) & 0xff);
|
buf[offset + 1] = (byte) ((value >>> 8) & 0xff);
|
||||||
@@ -109,70 +121,4 @@ public class BmpUtil {
|
|||||||
private static void putInt32Le(byte[] buf, int offset, int value) {
|
private static void putInt32Le(byte[] buf, int offset, int value) {
|
||||||
putUint32Le(buf, offset, value);
|
putUint32Le(buf, offset, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int readBmpWidth(byte[] bmp) {
|
|
||||||
if (bmp == null || bmp.length < 0x36 || bmp[0] != 0x42 || bmp[1] != 0x4d) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return readUint32Le(bmp, 18);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int readBmpHeight(byte[] bmp) {
|
|
||||||
if (bmp == null || bmp.length < 0x36 || bmp[0] != 0x42 || bmp[1] != 0x4d) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
int heightSigned = readInt32Le(bmp, 22);
|
|
||||||
return heightSigned < 0 ? -heightSigned : heightSigned;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int readBmpPixelOffset(byte[] bmp) {
|
|
||||||
if (bmp == null || bmp.length < 14 || bmp[0] != 0x42 || bmp[1] != 0x4d) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return (bmp[10] & 0xff)
|
|
||||||
| ((bmp[11] & 0xff) << 8)
|
|
||||||
| ((bmp[12] & 0xff) << 16)
|
|
||||||
| ((bmp[13] & 0xff) << 24);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Strip the BMP header and row padding, producing the headerless 4bpp wire
|
|
||||||
* format expected by CFW load_image_z mode 6: top-down rows, stride =
|
|
||||||
* ceil(width/2), high nibble = left pixel.
|
|
||||||
*/
|
|
||||||
public static byte[] pack4bppFromBmp(byte[] bmp) {
|
|
||||||
if (bmp == null || bmp.length < 0x36 || bmp[0] != 0x42 || bmp[1] != 0x4d) {
|
|
||||||
return new byte[0];
|
|
||||||
}
|
|
||||||
int width = readUint32Le(bmp, 18);
|
|
||||||
int heightSigned = readInt32Le(bmp, 22);
|
|
||||||
int height = heightSigned < 0 ? -heightSigned : heightSigned;
|
|
||||||
int pixelOffset = readBmpPixelOffset(bmp);
|
|
||||||
if (width <= 0 || height <= 0 || pixelOffset < 0 || pixelOffset >= bmp.length) {
|
|
||||||
return new byte[0];
|
|
||||||
}
|
|
||||||
int packedStride = (width + 1) >> 1;
|
|
||||||
int bmpStride = (packedStride + 3) & ~3;
|
|
||||||
if (pixelOffset + (long) bmpStride * height > bmp.length) {
|
|
||||||
return new byte[0];
|
|
||||||
}
|
|
||||||
byte[] out = new byte[packedStride * height];
|
|
||||||
for (int y = 0; y < height; y++) {
|
|
||||||
int srcY = height - 1 - y;
|
|
||||||
int srcRow = pixelOffset + srcY * bmpStride;
|
|
||||||
System.arraycopy(bmp, srcRow, out, y * packedStride, packedStride);
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int readUint32Le(byte[] buf, int offset) {
|
|
||||||
return (buf[offset] & 0xff)
|
|
||||||
| ((buf[offset + 1] & 0xff) << 8)
|
|
||||||
| ((buf[offset + 2] & 0xff) << 16)
|
|
||||||
| ((buf[offset + 3] & 0xff) << 24);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int readInt32Le(byte[] buf, int offset) {
|
|
||||||
return readUint32Le(buf, offset);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user