Serving a live preview image

Every imager module (Capturing an image) ends up re-deriving the same small chain: a frame arrives, gets loaded into a working buffer, and a preview JPEG gets written for the frontend to show. IndiModule provides the pieces; this page is about using them safely.

_image: the module’s one resident frame — never read it directly

_image is a QSharedPointer<fileio>, reassigned every time a new BLOB or FITS file is loaded. The trap: a BLOB arrives (and _image gets reassigned) on the module’s own INDI client thread, completely independently of whatever a previous frame’s analysis is still doing on another thread. Inspector hit this live, twice, in two different functions:

// src/modules/inspector/inspector.cpp, refreshPreview()
// A local snapshot, not _image itself: newBLOB()'s own
// loadImageFromBlob() can reassign _image to the next frame at any
// time from the module's INDI client thread - confirmed live,
// crashed on an empty histogram read from a frame this function had
// already moved past _image's own replacement.
QSharedPointer<fileio> img = currentImage();
// src/modules/inspector/inspector.cpp, analyzeFrame()
// A local snapshot, not _image itself: newBLOB()'s own
// loadImageFromBlob() runs on the module's INDI client thread and can
// reassign _image to the next frame at any time (confirmed live:
// crashed reading a histogram never computed for a frame this
// function had already moved past).
QSharedPointer<fileio> img = currentImage();

The rule this gives: never read _image from code that outlives a single synchronous call — anything that resumes later in a callback (a StellarSolver completion signal, an async save). Call currentImage() once, keep its returned snapshot for as long as the frame is needed, and read that instead for the rest of the function.

loadImageFromBlob()/loadImageFromFits(): the only sanctioned way to assign _image

Both apply imgparms.stretch automatically and accept an afterLoad callback, run before the internal lock is released — the natural place to call refreshPreview() or compute stats:

// src/modules/dofassistant/dofassistant.cpp, newBLOB()
loadImageFromBlob(b, 64, [this]()
{
    getProperty("image")->setState(OST::Ok, true);
    refreshPreview();
});

loadImageFromFits() is the same idea from a file already on disk — Inspector uses it to let the user reopen a previously saved frame:

// src/modules/inspector/inspector.cpp
bool ok = withinWebroot && loadImageFromFits(canonicalRequested, [this]()
{
    ...
});

refreshPreview(): override to redraw your own preview

No-op by default — override it to redraw and republish the preview (plain, or with an overlay) from whatever currentImage() returns right now. Navigator’s version is close to the minimal shape every imager repeats:

// src/modules/navigator/navigator.cpp
void Navigator::refreshPreview()
{
    QSharedPointer<fileio> img = currentImage();
    if (!img)
        return;

    QImage rawImage = img->getRawQImage();
    QImage im = rawImage.convertToFormat(QImage::Format_RGB32);
    im.setColorTable(rawImage.colorTable());
    int quality = getInt("imgparms", "jpegquality");
    atomicSaveJpeg(im, getWebroot() + "/" + getModuleName() + ".jpeg", quality);
    OST::ImgData dta = img->ImgStats();
    dta.mUrlJpeg = getModuleName() + ".jpeg";
    ...
}

Inspector’s own version draws an ROI overlay on top of the same base pattern before saving — the overlay is purely cosmetic, drawn on a copy, never on the buffer any analysis actually reads.

onStretchModeChanged(): react to the user changing imgparms.stretch

Called centrally whenever that element changes; the default implementation already re-stretches _image and calls refreshPreview() for free. Stacker overrides it instead, because it keeps no resident _image at all — it re-reads its own already-saved stack FITS from disk on every stretch change:

// src/modules/stacker/stacker.cpp
void Stacker::onStretchModeChanged()
{
    // Re-reads the FITS already on disk from the last stack update -
    // no-op if nothing has been saved yet this session
    // (mStackDestinationPath doesn't exist on disk).
    if (!mStackDestinationPath.isEmpty() && QFileInfo::exists(mStackDestinationPath))
    {
        onStackSaved(mStackDestinationPath);
    }
}

atomicSaveJpeg(): why every preview write goes through it

A plain QImage::save() can leave a reader (nginx serving /ostmedia/, via sendfile()) looking at a half-written file mid-write. atomicSaveJpeg() writes to a temporary file first, then renames it into place — a rename is atomic, so a concurrent reader always sees either the previous complete file or the new one, never a partial one. Declared static on Basemodule, used identically by every module with a live preview (Navigator, Allsky, Guider, Inspector, Dofassistant…):

// src/modules/allsky/allsky.cpp, two different previews of the same module
atomicSaveJpeg(imageStacked, getWebroot() + "/" + getModuleName() + "/" + mFolder + "/stacked.jpeg");
atomicSaveJpeg(mKeog,        getWebroot() + "/" + getModuleName() + "/" + mFolder + "/keogram.jpeg");

It used to be a byte-for-byte copy of the same static function duplicated in each module’s own .cpp file — moved to Basemodule precisely so a fix to the write-then-rename logic itself only has to happen once.