BUGS FIXED! getting super close...

This commit is contained in:
Zach Fox 2016-11-15 11:05:31 -08:00
parent adcbb0b760
commit cc8bf0ce6e
2 changed files with 57 additions and 46 deletions

View file

@ -21,60 +21,51 @@ GifWriter SnapshotAnimated::snapshotAnimatedGifWriter;
qint64 SnapshotAnimated::snapshotAnimatedTimestamp = 0; qint64 SnapshotAnimated::snapshotAnimatedTimestamp = 0;
qint64 SnapshotAnimated::snapshotAnimatedFirstFrameTimestamp = 0; qint64 SnapshotAnimated::snapshotAnimatedFirstFrameTimestamp = 0;
qint64 SnapshotAnimated::snapshotAnimatedLastWriteFrameDuration = 0; qint64 SnapshotAnimated::snapshotAnimatedLastWriteFrameDuration = 0;
bool SnapshotAnimated::snapshotAnimatedTimerRunning = false;
QString SnapshotAnimated::snapshotAnimatedPath;
QString SnapshotAnimated::snapshotStillPath;
void SnapshotAnimated::saveSnapshotAnimated(bool includeAnimated, QString pathStillSnapshot, float aspectRatio, Application* app, QSharedPointer<WindowScriptingInterface> dm) { void SnapshotAnimated::saveSnapshotAnimated(bool includeAnimated, QString pathStill, float aspectRatio, Application* app, QSharedPointer<WindowScriptingInterface> dm) {
// If we're not in the middle of capturing an animated snapshot... // If we're not in the middle of capturing an animated snapshot...
if ((snapshotAnimatedFirstFrameTimestamp == 0) && (includeAnimated)) if ((SnapshotAnimated::snapshotAnimatedFirstFrameTimestamp == 0) && (includeAnimated))
{ {
// Define the output location of the animated snapshot // Define the output location of the still and animated snapshots.
QString pathAnimatedSnapshot(pathStillSnapshot); SnapshotAnimated::snapshotStillPath = pathStill;
pathAnimatedSnapshot.replace("jpg", "gif"); SnapshotAnimated::snapshotAnimatedPath = pathStill;
SnapshotAnimated::snapshotAnimatedPath.replace("jpg", "gif");
// Reset the current animated snapshot last frame duration // Reset the current animated snapshot last frame duration
snapshotAnimatedLastWriteFrameDuration = SNAPSNOT_ANIMATED_INITIAL_WRITE_DURATION_MSEC; SnapshotAnimated::snapshotAnimatedLastWriteFrameDuration = SNAPSNOT_ANIMATED_INITIAL_WRITE_DURATION_MSEC;
// Ensure the snapshot timer is Precise (attempted millisecond precision) // Ensure the snapshot timer is Precise (attempted millisecond precision)
snapshotAnimatedTimer.setTimerType(Qt::PreciseTimer); SnapshotAnimated::snapshotAnimatedTimer.setTimerType(Qt::PreciseTimer);
// Connect the snapshotAnimatedTimer QTimer to the lambda slot function // Connect the snapshotAnimatedTimer QTimer to the lambda slot function
QObject::connect(&(snapshotAnimatedTimer), &QTimer::timeout, [=] { QObject::connect(&(SnapshotAnimated::snapshotAnimatedTimer), &QTimer::timeout, [=] {
// Get a screenshot from the display, then scale the screenshot down, if (SnapshotAnimated::snapshotAnimatedTimerRunning)
// then convert it to the image format the GIF library needs, {
// then save all that to the QImage named "frame" // Get a screenshot from the display, then scale the screenshot down,
QImage frame(app->getActiveDisplayPlugin()->getScreenshot(aspectRatio)); // then convert it to the image format the GIF library needs,
frame = frame.scaledToWidth(SNAPSNOT_ANIMATED_WIDTH); // then save all that to the QImage named "frame"
frame = frame.convertToFormat(QImage::Format_RGBA8888); QImage frame(app->getActiveDisplayPlugin()->getScreenshot(aspectRatio));
frame = frame.scaledToWidth(SNAPSNOT_ANIMATED_WIDTH);
frame = frame.convertToFormat(QImage::Format_RGBA8888);
// If this is the first frame... // If this is the first frame...
if (SnapshotAnimated::snapshotAnimatedTimestamp == 0) if (SnapshotAnimated::snapshotAnimatedTimestamp == 0)
{
// Write out the header and beginning of the GIF file
GifBegin(&(SnapshotAnimated::snapshotAnimatedGifWriter), qPrintable(pathAnimatedSnapshot), frame.width(), frame.height(), SNAPSNOT_ANIMATED_FRAME_DELAY_MSEC / 10);
// Write the first to the gif
GifWriteFrame(&(SnapshotAnimated::snapshotAnimatedGifWriter),
(uint8_t*)frame.bits(),
frame.width(),
frame.height(),
SNAPSNOT_ANIMATED_FRAME_DELAY_MSEC / 10);
// Record the current frame timestamp
SnapshotAnimated::snapshotAnimatedTimestamp = QDateTime::currentMSecsSinceEpoch();
SnapshotAnimated::snapshotAnimatedFirstFrameTimestamp = SnapshotAnimated::snapshotAnimatedTimestamp;
}
else
{
// If that was the last frame...
if ((SnapshotAnimated::snapshotAnimatedTimestamp - SnapshotAnimated::snapshotAnimatedFirstFrameTimestamp) >= (SNAPSNOT_ANIMATED_DURATION_SECS * 1000))
{ {
// Stop the snapshot QTimer // Write out the header and beginning of the GIF file
SnapshotAnimated::snapshotAnimatedTimer.stop(); GifBegin(&(SnapshotAnimated::snapshotAnimatedGifWriter), qPrintable(SnapshotAnimated::snapshotAnimatedPath), frame.width(), frame.height(), SNAPSNOT_ANIMATED_FRAME_DELAY_MSEC / 10);
// Reset the current frame timestamp // Write the first to the gif
SnapshotAnimated::snapshotAnimatedTimestamp = 0; GifWriteFrame(&(SnapshotAnimated::snapshotAnimatedGifWriter),
SnapshotAnimated::snapshotAnimatedFirstFrameTimestamp = 0; (uint8_t*)frame.bits(),
// Write out the end of the GIF frame.width(),
GifEnd(&(SnapshotAnimated::snapshotAnimatedGifWriter)); frame.height(),
// Let the dependency manager know that the snapshots have been taken. SNAPSNOT_ANIMATED_FRAME_DELAY_MSEC / 10);
emit dm->snapshotTaken(pathStillSnapshot, pathAnimatedSnapshot, false); // Record the current frame timestamp
SnapshotAnimated::snapshotAnimatedTimestamp = QDateTime::currentMSecsSinceEpoch();
SnapshotAnimated::snapshotAnimatedFirstFrameTimestamp = SnapshotAnimated::snapshotAnimatedTimestamp;
} }
// If that was an intermediate frame... // If that was an intermediate or the final frame...
else else
{ {
// Variable used to determine how long the current frame took to pack // Variable used to determine how long the current frame took to pack
@ -89,17 +80,34 @@ void SnapshotAnimated::saveSnapshotAnimated(bool includeAnimated, QString pathSt
SnapshotAnimated::snapshotAnimatedLastWriteFrameDuration = QDateTime::currentMSecsSinceEpoch() - framePackStartTime; SnapshotAnimated::snapshotAnimatedLastWriteFrameDuration = QDateTime::currentMSecsSinceEpoch() - framePackStartTime;
// Record the current frame timestamp // Record the current frame timestamp
SnapshotAnimated::snapshotAnimatedTimestamp = QDateTime::currentMSecsSinceEpoch(); SnapshotAnimated::snapshotAnimatedTimestamp = QDateTime::currentMSecsSinceEpoch();
// If that was the last frame...
if ((SnapshotAnimated::snapshotAnimatedTimestamp - SnapshotAnimated::snapshotAnimatedFirstFrameTimestamp) >= (SNAPSNOT_ANIMATED_DURATION_SECS * 1000))
{
// Stop the snapshot QTimer. This action by itself DOES NOT GUARANTEE
// that the slot will not be called again in the future.
// See: http://lists.qt-project.org/pipermail/qt-interest-old/2009-October/013926.html
SnapshotAnimated::snapshotAnimatedTimer.stop();
SnapshotAnimated::snapshotAnimatedTimerRunning = false;
// Reset the current frame timestamp
SnapshotAnimated::snapshotAnimatedTimestamp = 0;
SnapshotAnimated::snapshotAnimatedFirstFrameTimestamp = 0;
// Write out the end of the GIF
GifEnd(&(SnapshotAnimated::snapshotAnimatedGifWriter));
// Let the dependency manager know that the snapshots have been taken.
emit dm->snapshotTaken(SnapshotAnimated::snapshotStillPath, SnapshotAnimated::snapshotAnimatedPath, false);
}
} }
} }
}); });
// Start the snapshotAnimatedTimer QTimer - argument for this is in milliseconds // Start the snapshotAnimatedTimer QTimer - argument for this is in milliseconds
snapshotAnimatedTimer.start(SNAPSNOT_ANIMATED_FRAME_DELAY_MSEC); SnapshotAnimated::snapshotAnimatedTimer.start(SNAPSNOT_ANIMATED_FRAME_DELAY_MSEC);
SnapshotAnimated::snapshotAnimatedTimerRunning = true;
} }
// If we're already in the middle of capturing an animated snapshot... // If we're already in the middle of capturing an animated snapshot...
else else
{ {
// Just tell the dependency manager that the capture of the still snapshot has taken place. // Just tell the dependency manager that the capture of the still snapshot has taken place.
emit dm->snapshotTaken(pathStillSnapshot, "", false); emit dm->snapshotTaken(pathStill, "", false);
} }
} }

View file

@ -37,8 +37,11 @@ private:
static qint64 snapshotAnimatedTimestamp; static qint64 snapshotAnimatedTimestamp;
static qint64 snapshotAnimatedFirstFrameTimestamp; static qint64 snapshotAnimatedFirstFrameTimestamp;
static qint64 snapshotAnimatedLastWriteFrameDuration; static qint64 snapshotAnimatedLastWriteFrameDuration;
static bool snapshotAnimatedTimerRunning;
static QString snapshotAnimatedPath;
static QString snapshotStillPath;
public: public:
static void saveSnapshotAnimated(bool includeAnimated, QString stillSnapshotPath, float aspectRatio, Application* app, QSharedPointer<WindowScriptingInterface> dm); static void saveSnapshotAnimated(bool includeAnimated, QString pathStill, float aspectRatio, Application* app, QSharedPointer<WindowScriptingInterface> dm);
}; };
#endif // hifi_SnapshotAnimated_h #endif // hifi_SnapshotAnimated_h