diff options
author | Morten Sorvig <msorvig@trolltech.com> | 2009-08-27 16:35:52 +0200 |
---|---|---|
committer | Morten Sorvig <msorvig@trolltech.com> | 2009-08-27 16:35:52 +0200 |
commit | 3d93f4c7677610a23b347b5e9e8a6881e021dcd3 (patch) | |
tree | f5a9a1b35e45d58835f062a97ca11690fd78c575 | |
parent | 5a7f65faca6ee6a2a5ab03233c1baa9e7a239798 (diff) |
Store images compressed as pngs. Don't scan images for equality.
-rw-r--r-- | src/eventqueue.cpp | 57 | ||||
-rw-r--r-- | src/eventqueue.h | 2 |
2 files changed, 24 insertions, 35 deletions
diff --git a/src/eventqueue.cpp b/src/eventqueue.cpp index 76c68f7..a1af3e4 100644 --- a/src/eventqueue.cpp +++ b/src/eventqueue.cpp @@ -2,19 +2,6 @@ #include "webclient.h" #include <QImage> -bool imagesEqual(const QImage &a, const QImage &b, QRect rect) -{ - if (a.size() != b.size()) - return false; - - for (int y = rect.top(); y < rect.bottom(); ++y) - for (int x = rect.left(); x < rect.right(); ++x) - if (a.pixel(x,y) != b.pixel(x,y)) - return false; - - return true; -} - void encodeText(QString &text) { // encode "breakout" characters @@ -121,34 +108,34 @@ void EventQueue::handleImageRequest(json_object* jsonRequest, HttpResponse *resp const quintptr id = json_object_get_int(idObject); DEBUG << "id" << this << id << images.value(id).size(); - QByteArray block; - QBuffer buffer(&block); - buffer.open(QIODevice::WriteOnly); - QImageWriter writer(&buffer, "png"); - writer.write(images.value(id)); + // Save server memory and CPU time by storing images in the png + // format after the first serve. The browser could request + // the image again, for example if the user hits refresh. + QByteArray pngImage; + QImage uncompressedImage = images.value(id); + if (uncompressedImage.isNull()== false) { + images.remove(id); + + QBuffer buffer(&pngImage); + buffer.open(QIODevice::WriteOnly); + QImageWriter writer(&buffer, "png"); + writer.write(uncompressedImage); + + compressedImages.insert(id, pngImage); + } + + if (pngImage.isEmpty()) { + pngImage = compressedImages.value(id); + } - response->setBody(block); + response->setBody(pngImage); response->setContentType("image/png"); } void EventQueue::addUpdateEvent(quintptr id, const QImage &image, QRect updateRect) { - DEBUG << "addUpdateEvent" << this << id << image.size(); - - if (images.contains(id) == false) { - DEBUG << "new image for" << id; - images.insert(id, image); - } else { - if (imagesEqual(image, images.value(id), updateRect)) { - DEBUG << "discard update" << id; - return; - } else { - // image.save("images/" + widget->objectName() + ".png"); - images.insert(id, image); - DEBUG << "accept update" << id; - } - } - + DEBUG << "addUpdateEvent" << this << id << image.size(); + images.insert(id, image); addEvent(id, EventEntry::Update); } diff --git a/src/eventqueue.h b/src/eventqueue.h index 3f418b8..7085a32 100644 --- a/src/eventqueue.h +++ b/src/eventqueue.h @@ -53,6 +53,8 @@ public: Session *m_session; QQueue<EventEntry> events; QHash<quintptr, QImage> images; + QHash<quintptr, QByteArray> compressedImages; + QHash<quintptr, QRect> geometries; }; |