rounding gifs in messages history

This commit is contained in:
John Preston 2015-05-21 15:08:05 +03:00
parent 18361ce144
commit e953e11b7f
4 changed files with 66 additions and 35 deletions

View file

@ -104,26 +104,29 @@ namespace anim {
bool AnimatedGif::animStep(float64 ms) { bool AnimatedGif::animStep(float64 ms) {
int32 f = frame; int32 f = frame;
while (f < frames.size() && ms > delays[f]) { while (f < images.size() && ms > delays[f]) {
++f; ++f;
if (f == frames.size() && frames.size() < framesCount) { if (f == images.size() && images.size() < framesCount) {
if (reader->read(&img)) { if (reader->read(&img)) {
int64 d = reader->nextImageDelay(), delay = delays[f - 1]; int64 d = reader->nextImageDelay(), delay = delays[f - 1];
if (!d) d = 1; if (!d) d = 1;
delay += d; delay += d;
frames.push_back(QPixmap::fromImage(img.size() == QSize(w, h) ? img : img.scaled(w, h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), Qt::ColorOnly)); if (img.size() != QSize(w, h)) img = img.scaled(w, h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
images.push_back(img);
frames.push_back(QPixmap());
delays.push_back(delay); delays.push_back(delay);
for (int32 i = 0; i < frames.size(); ++i) { for (int32 i = 0; i < images.size(); ++i) {
if (!frames[i].isNull()) { if (!images[i].isNull() || !frames[i].isNull()) {
images[i] = QImage();
frames[i] = QPixmap(); frames[i] = QPixmap();
break; break;
} }
} }
} else { } else {
framesCount = frames.size(); framesCount = images.size();
} }
} }
if (f == frames.size()) { if (f == images.size()) {
if (!duration) { if (!duration) {
duration = delays.isEmpty() ? 1 : delays.back(); duration = delays.isEmpty() ? 1 : delays.back();
} }
@ -132,14 +135,16 @@ bool AnimatedGif::animStep(float64 ms) {
for (int32 i = 0, s = delays.size() - 1; i <= s; ++i) { for (int32 i = 0, s = delays.size() - 1; i <= s; ++i) {
delays[i] += duration; delays[i] += duration;
} }
if (frames[f].isNull()) { if (images[f].isNull()) {
QString fname = reader->fileName(); QString fname = reader->fileName();
delete reader; delete reader;
reader = new QImageReader(fname); reader = new QImageReader(fname);
} }
} }
if (frames[f].isNull() && reader->read(&img)) { if (images[f].isNull() && reader->read(&img)) {
frames[f] = QPixmap::fromImage(img.size() == QSize(w, h) ? img : img.scaled(w, h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), Qt::ColorOnly); if (img.size() != QSize(w, h)) img = img.scaled(w, h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
images[f] = img;
frames[f] = QPixmap();
} }
} }
if (frame != f) { if (frame != f) {
@ -172,12 +177,15 @@ void AnimatedGif::start(HistoryItem *row, const QString &file) {
} }
frames.reserve(framesCount); frames.reserve(framesCount);
images.reserve(framesCount);
delays.reserve(framesCount); delays.reserve(framesCount);
int32 sizeLeft = MediaViewImageSizeLimit, delay = 0; int32 sizeLeft = MediaViewImageSizeLimit, delay = 0;
for (bool read = reader->read(&img); read; read = reader->read(&img)) { for (bool read = reader->read(&img); read; read = reader->read(&img)) {
sizeLeft -= w * h * 4; sizeLeft -= w * h * 4;
frames.push_back(QPixmap::fromImage(img.size() == s ? img : img.scaled(w, h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), Qt::ColorOnly)); if (img.size() != s) img = img.scaled(w, h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
images.push_back(img);
frames.push_back(QPixmap());
int32 d = reader->nextImageDelay(); int32 d = reader->nextImageDelay();
if (!d) d = 1; if (!d) d = 1;
delay += d; delay += d;
@ -202,6 +210,7 @@ void AnimatedGif::stop(bool onItemRemoved) {
HistoryItem *row = msg; HistoryItem *row = msg;
msg = 0; msg = 0;
frames.clear(); frames.clear();
images.clear();
delays.clear(); delays.clear();
w = h = frame = framesCount = duration = 0; w = h = frame = framesCount = duration = 0;
@ -211,3 +220,16 @@ void AnimatedGif::stop(bool onItemRemoved) {
if (App::main()) App::main()->itemResized(row, true); if (App::main()) App::main()->itemResized(row, true);
} }
} }
const QPixmap &AnimatedGif::current(int32 width, int32 height, bool rounded) {
if (!width) width = w;
if (!height) height = h;
if ((frames[frame].isNull() || frames[frame].width() != width || frames[frame].height() != height) && !images[frame].isNull()) {
QImage img = images[frame];
if (img.width() != width || img.height() != height) img = img.scaled(width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
if (rounded) imageRound(img);
frames[frame] = QPixmap::fromImage(img, Qt::ColorOnly);
frames[frame].setDevicePixelRatio(cRetinaFactor());
}
return frames[frame];
}

View file

@ -348,6 +348,8 @@ public:
stop(true); stop(true);
} }
const QPixmap &current(int32 width = 0, int32 height = 0, bool rounded = false);
signals: signals:
void updated(); void updated();
@ -357,7 +359,12 @@ public:
HistoryItem *msg; HistoryItem *msg;
QImage img; QImage img;
QImageReader *reader; QImageReader *reader;
int32 w, h, frame;
private:
QVector<QPixmap> frames; QVector<QPixmap> frames;
QVector<QImage> images;
QVector<int64> delays; QVector<int64> delays;
int32 w, h, frame, framesCount, duration; int32 framesCount, duration;
}; };

View file

@ -2650,21 +2650,25 @@ void HistoryDocument::draw(QPainter &p, const HistoryItem *parent, bool selected
bool out = parent->out(), hovered, pressed; bool out = parent->out(), hovered, pressed;
if (parent == animated.msg) { if (parent == animated.msg) {
if (width >= animated.w) { int32 pw = animated.w, ph = animated.h;
p.drawPixmap(0, 0, animated.frames[animated.frame]); if (width < pw) {
if (selected) { pw = width;
p.fillRect(0, 0, animated.w, animated.h, textstyleCurrent()->selectOverlay->b); ph = (pw == w) ? _height : (pw * animated.h / animated.w);
} if (ph < 1) ph = 1;
} else { }
bool s = p.renderHints().testFlag(QPainter::SmoothPixmapTransform);
if (!s) p.setRenderHint(QPainter::SmoothPixmapTransform); QPixmap **cors = App::corners(selected ? InSelectedShadowCorners : InShadowCorners);
int32 h = (width == w) ? _height : (width * animated.h / animated.w); int32 cw = cors[0]->width() / cIntRetinaFactor(), ch = cors[0]->height() / cIntRetinaFactor();
if (h < 1) h = 1; style::color shadow(selected ? st::msgInSelectShadow : st::msgInShadow);
p.drawPixmap(QRect(0, 0, width, h), animated.frames[animated.frame]); p.fillRect(cw, ph, pw - 2 * cw, st::msgShadow, shadow->b);
if (!s) p.setRenderHint(QPainter::SmoothPixmapTransform, false); p.fillRect(0, ph - ch, cw, st::msgShadow, shadow->b);
if (selected) { p.fillRect(pw - cw, ph - ch, cw, st::msgShadow, shadow->b);
p.fillRect(0, 0, width, h, textstyleCurrent()->selectOverlay->b); p.drawPixmap(0, ph - ch + st::msgShadow, *cors[2]);
} p.drawPixmap(pw - cw, ph - ch + st::msgShadow, *cors[3]);
p.drawPixmap(0, 0, animated.current(pw, ph, true));
if (selected) {
App::roundRect(p, 0, 0, pw, ph, textstyleCurrent()->selectOverlay, SelectedOverlayCorners);
} }
return; return;
} }

View file

@ -183,7 +183,6 @@ void MediaView::documentUpdated(DocumentData *doc) {
} }
void MediaView::onGifUpdated() { void MediaView::onGifUpdated() {
_currentGif.frames[_currentGif.frame].setDevicePixelRatio(cRetinaFactor());
update(_x, _y, _w, _h); update(_x, _y, _w, _h);
} }
@ -905,9 +904,8 @@ void MediaView::displayDocument(DocumentData *doc, HistoryItem *item) {
_w = _current.width() / cIntRetinaFactor(); _w = _current.width() / cIntRetinaFactor();
_h = _current.height() / cIntRetinaFactor(); _h = _current.height() / cIntRetinaFactor();
} else { } else {
_currentGif.frames[_currentGif.frame].setDevicePixelRatio(cRetinaFactor()); _w = _currentGif.w / cIntRetinaFactor();
_w = _currentGif.frames[_currentGif.frame].width() / cIntRetinaFactor(); _h = _currentGif.h / cIntRetinaFactor();
_h = _currentGif.frames[_currentGif.frame].height() / cIntRetinaFactor();
} }
if (isHidden()) { if (isHidden()) {
moveToScreen(); moveToScreen();
@ -1002,7 +1000,7 @@ void MediaView::paintEvent(QPaintEvent *e) {
p.setOpacity(1); p.setOpacity(1);
if (_photo || !_current.isNull() || !_currentGif.isNull()) { if (_photo || !_current.isNull() || !_currentGif.isNull()) {
QRect imgRect(_x, _y, _w, _h); QRect imgRect(_x, _y, _w, _h);
const QPixmap *toDraw = _currentGif.isNull() ? &_current : &_currentGif.frames[_currentGif.frame]; const QPixmap *toDraw = _currentGif.isNull() ? &_current : &_currentGif.current(_currentGif.w, _currentGif.h, false);
if (imgRect.intersects(r)) { if (imgRect.intersects(r)) {
if (toDraw->hasAlpha() && (!_doc || !_doc->sticker || _doc->sticker->img->isNull())) { if (toDraw->hasAlpha() && (!_doc || !_doc->sticker || _doc->sticker->img->isNull())) {
p.fillRect(imgRect, _transparentBrush); p.fillRect(imgRect, _transparentBrush);
@ -1315,7 +1313,7 @@ void MediaView::keyPressEvent(QKeyEvent *e) {
newZoom = 0; newZoom = 0;
} }
_x = -_width / 2; _x = -_width / 2;
_y = -(((_currentGif.isNull() ? _current.height() : _currentGif.frames[_currentGif.frame].height()) / cIntRetinaFactor()) / 2); _y = -(((_currentGif.isNull() ? _current.height() : _currentGif.h) / cIntRetinaFactor()) / 2);
float64 z = (_zoom == ZoomToScreenLevel) ? _zoomToScreen : _zoom; float64 z = (_zoom == ZoomToScreenLevel) ? _zoomToScreen : _zoom;
if (z >= 0) { if (z >= 0) {
_x = qRound(_x * (z + 1)); _x = qRound(_x * (z + 1));
@ -1335,8 +1333,8 @@ void MediaView::keyPressEvent(QKeyEvent *e) {
} }
if (_zoom != newZoom) { if (_zoom != newZoom) {
float64 nx, ny, z = (_zoom == ZoomToScreenLevel) ? _zoomToScreen : _zoom; float64 nx, ny, z = (_zoom == ZoomToScreenLevel) ? _zoomToScreen : _zoom;
_w = (_currentGif.isNull() ? _current.width() : _currentGif.frames[_currentGif.frame].width()) / cIntRetinaFactor(); _w = (_currentGif.isNull() ? _current.width() : _currentGif.w) / cIntRetinaFactor();
_h = (_currentGif.isNull() ? _current.height() : _currentGif.frames[_currentGif.frame].height()) / cIntRetinaFactor(); _h = (_currentGif.isNull() ? _current.height() : _currentGif.h) / cIntRetinaFactor();
if (z >= 0) { if (z >= 0) {
nx = (_x - width() / 2.) / (z + 1); nx = (_x - width() / 2.) / (z + 1);
ny = (_y - height() / 2.) / (z + 1); ny = (_y - height() / 2.) / (z + 1);