mirror of
https://github.com/vale981/tdesktop
synced 2025-03-06 10:11:41 -05:00
rounding gifs in messages history
This commit is contained in:
parent
18361ce144
commit
e953e11b7f
4 changed files with 66 additions and 35 deletions
|
@ -104,26 +104,29 @@ namespace anim {
|
|||
|
||||
bool AnimatedGif::animStep(float64 ms) {
|
||||
int32 f = frame;
|
||||
while (f < frames.size() && ms > delays[f]) {
|
||||
while (f < images.size() && ms > delays[f]) {
|
||||
++f;
|
||||
if (f == frames.size() && frames.size() < framesCount) {
|
||||
if (f == images.size() && images.size() < framesCount) {
|
||||
if (reader->read(&img)) {
|
||||
int64 d = reader->nextImageDelay(), delay = delays[f - 1];
|
||||
if (!d) d = 1;
|
||||
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);
|
||||
for (int32 i = 0; i < frames.size(); ++i) {
|
||||
if (!frames[i].isNull()) {
|
||||
for (int32 i = 0; i < images.size(); ++i) {
|
||||
if (!images[i].isNull() || !frames[i].isNull()) {
|
||||
images[i] = QImage();
|
||||
frames[i] = QPixmap();
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
framesCount = frames.size();
|
||||
framesCount = images.size();
|
||||
}
|
||||
}
|
||||
if (f == frames.size()) {
|
||||
if (f == images.size()) {
|
||||
if (!duration) {
|
||||
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) {
|
||||
delays[i] += duration;
|
||||
}
|
||||
if (frames[f].isNull()) {
|
||||
if (images[f].isNull()) {
|
||||
QString fname = reader->fileName();
|
||||
delete reader;
|
||||
reader = new QImageReader(fname);
|
||||
}
|
||||
}
|
||||
if (frames[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 (images[f].isNull() && reader->read(&img)) {
|
||||
if (img.size() != QSize(w, h)) img = img.scaled(w, h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
||||
images[f] = img;
|
||||
frames[f] = QPixmap();
|
||||
}
|
||||
}
|
||||
if (frame != f) {
|
||||
|
@ -172,12 +177,15 @@ void AnimatedGif::start(HistoryItem *row, const QString &file) {
|
|||
}
|
||||
|
||||
frames.reserve(framesCount);
|
||||
images.reserve(framesCount);
|
||||
delays.reserve(framesCount);
|
||||
|
||||
int32 sizeLeft = MediaViewImageSizeLimit, delay = 0;
|
||||
for (bool read = reader->read(&img); read; read = reader->read(&img)) {
|
||||
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();
|
||||
if (!d) d = 1;
|
||||
delay += d;
|
||||
|
@ -202,6 +210,7 @@ void AnimatedGif::stop(bool onItemRemoved) {
|
|||
HistoryItem *row = msg;
|
||||
msg = 0;
|
||||
frames.clear();
|
||||
images.clear();
|
||||
delays.clear();
|
||||
w = h = frame = framesCount = duration = 0;
|
||||
|
||||
|
@ -211,3 +220,16 @@ void AnimatedGif::stop(bool onItemRemoved) {
|
|||
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];
|
||||
}
|
||||
|
|
|
@ -348,6 +348,8 @@ public:
|
|||
stop(true);
|
||||
}
|
||||
|
||||
const QPixmap ¤t(int32 width = 0, int32 height = 0, bool rounded = false);
|
||||
|
||||
signals:
|
||||
|
||||
void updated();
|
||||
|
@ -357,7 +359,12 @@ public:
|
|||
HistoryItem *msg;
|
||||
QImage img;
|
||||
QImageReader *reader;
|
||||
int32 w, h, frame;
|
||||
|
||||
private:
|
||||
|
||||
QVector<QPixmap> frames;
|
||||
QVector<QImage> images;
|
||||
QVector<int64> delays;
|
||||
int32 w, h, frame, framesCount, duration;
|
||||
int32 framesCount, duration;
|
||||
};
|
||||
|
|
|
@ -2650,21 +2650,25 @@ void HistoryDocument::draw(QPainter &p, const HistoryItem *parent, bool selected
|
|||
|
||||
bool out = parent->out(), hovered, pressed;
|
||||
if (parent == animated.msg) {
|
||||
if (width >= animated.w) {
|
||||
p.drawPixmap(0, 0, animated.frames[animated.frame]);
|
||||
if (selected) {
|
||||
p.fillRect(0, 0, animated.w, animated.h, textstyleCurrent()->selectOverlay->b);
|
||||
}
|
||||
} else {
|
||||
bool s = p.renderHints().testFlag(QPainter::SmoothPixmapTransform);
|
||||
if (!s) p.setRenderHint(QPainter::SmoothPixmapTransform);
|
||||
int32 h = (width == w) ? _height : (width * animated.h / animated.w);
|
||||
if (h < 1) h = 1;
|
||||
p.drawPixmap(QRect(0, 0, width, h), animated.frames[animated.frame]);
|
||||
if (!s) p.setRenderHint(QPainter::SmoothPixmapTransform, false);
|
||||
if (selected) {
|
||||
p.fillRect(0, 0, width, h, textstyleCurrent()->selectOverlay->b);
|
||||
}
|
||||
int32 pw = animated.w, ph = animated.h;
|
||||
if (width < pw) {
|
||||
pw = width;
|
||||
ph = (pw == w) ? _height : (pw * animated.h / animated.w);
|
||||
if (ph < 1) ph = 1;
|
||||
}
|
||||
|
||||
QPixmap **cors = App::corners(selected ? InSelectedShadowCorners : InShadowCorners);
|
||||
int32 cw = cors[0]->width() / cIntRetinaFactor(), ch = cors[0]->height() / cIntRetinaFactor();
|
||||
style::color shadow(selected ? st::msgInSelectShadow : st::msgInShadow);
|
||||
p.fillRect(cw, ph, pw - 2 * cw, st::msgShadow, shadow->b);
|
||||
p.fillRect(0, ph - ch, cw, st::msgShadow, shadow->b);
|
||||
p.fillRect(pw - cw, ph - ch, cw, st::msgShadow, shadow->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;
|
||||
}
|
||||
|
|
|
@ -183,7 +183,6 @@ void MediaView::documentUpdated(DocumentData *doc) {
|
|||
}
|
||||
|
||||
void MediaView::onGifUpdated() {
|
||||
_currentGif.frames[_currentGif.frame].setDevicePixelRatio(cRetinaFactor());
|
||||
update(_x, _y, _w, _h);
|
||||
}
|
||||
|
||||
|
@ -905,9 +904,8 @@ void MediaView::displayDocument(DocumentData *doc, HistoryItem *item) {
|
|||
_w = _current.width() / cIntRetinaFactor();
|
||||
_h = _current.height() / cIntRetinaFactor();
|
||||
} else {
|
||||
_currentGif.frames[_currentGif.frame].setDevicePixelRatio(cRetinaFactor());
|
||||
_w = _currentGif.frames[_currentGif.frame].width() / cIntRetinaFactor();
|
||||
_h = _currentGif.frames[_currentGif.frame].height() / cIntRetinaFactor();
|
||||
_w = _currentGif.w / cIntRetinaFactor();
|
||||
_h = _currentGif.h / cIntRetinaFactor();
|
||||
}
|
||||
if (isHidden()) {
|
||||
moveToScreen();
|
||||
|
@ -1002,7 +1000,7 @@ void MediaView::paintEvent(QPaintEvent *e) {
|
|||
p.setOpacity(1);
|
||||
if (_photo || !_current.isNull() || !_currentGif.isNull()) {
|
||||
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 (toDraw->hasAlpha() && (!_doc || !_doc->sticker || _doc->sticker->img->isNull())) {
|
||||
p.fillRect(imgRect, _transparentBrush);
|
||||
|
@ -1315,7 +1313,7 @@ void MediaView::keyPressEvent(QKeyEvent *e) {
|
|||
newZoom = 0;
|
||||
}
|
||||
_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;
|
||||
if (z >= 0) {
|
||||
_x = qRound(_x * (z + 1));
|
||||
|
@ -1335,8 +1333,8 @@ void MediaView::keyPressEvent(QKeyEvent *e) {
|
|||
}
|
||||
if (_zoom != newZoom) {
|
||||
float64 nx, ny, z = (_zoom == ZoomToScreenLevel) ? _zoomToScreen : _zoom;
|
||||
_w = (_currentGif.isNull() ? _current.width() : _currentGif.frames[_currentGif.frame].width()) / cIntRetinaFactor();
|
||||
_h = (_currentGif.isNull() ? _current.height() : _currentGif.frames[_currentGif.frame].height()) / cIntRetinaFactor();
|
||||
_w = (_currentGif.isNull() ? _current.width() : _currentGif.w) / cIntRetinaFactor();
|
||||
_h = (_currentGif.isNull() ? _current.height() : _currentGif.h) / cIntRetinaFactor();
|
||||
if (z >= 0) {
|
||||
nx = (_x - width() / 2.) / (z + 1);
|
||||
ny = (_y - height() / 2.) / (z + 1);
|
||||
|
|
Loading…
Add table
Reference in a new issue