File indexing completed on 2024-12-01 09:50:50

0001 /*
0002     Large image displaying library.
0003 
0004     Copyright (C) 2005 Maks Orlovich (maksim@kde.org)
0005 
0006     Permission is hereby granted, free of charge, to any person obtaining a copy
0007     of this software and associated documentation files (the "Software"), to deal
0008     in the Software without restriction, including without limitation the rights
0009     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0010     copies of the Software, and to permit persons to whom the Software is
0011     furnished to do so, subject to the following conditions:
0012 
0013     The above copyright notice and this permission notice shall be included in
0014     all copies or substantial portions of the Software.
0015 
0016     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0017     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0018     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
0019     AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
0020     AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0021     CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0022 
0023 */
0024 #include "imageplane.h"
0025 #include "imagemanager.h"
0026 #include <cstring>
0027 
0028 #include <QPainter>
0029 
0030 namespace khtmlImLoad
0031 {
0032 
0033 bool ImagePlane::checkUpToDate(const unsigned char *versions, PixmapTile *tile)
0034 {
0035     //If the tile is discarded, it's clearly not up-to-date ;-)
0036     if (!tile->pixmap) {
0037         return false;
0038     }
0039 
0040     //Now, just check the version array. It's enough to memcmp,
0041     //since we init all to zero, and assume the pixmap plains don't
0042     //magically figure out the image before the loader.
0043     return std::memcmp(versions, tile->versions, Tile::TileSize) == 0;
0044 }
0045 
0046 void ImagePlane::setupTile(unsigned int tileX, unsigned int tileY, PixmapTile *tile)
0047 {
0048     tile->pixmap = new QPixmap(tileWidth(tileX), tileHeight(tileY));
0049     ImageManager::pixmapCache()->addEntry(tile);
0050 }
0051 
0052 void ImagePlane::updatePixmap(PixmapTile *tile, const QImage &image,
0053                               unsigned int tileX, unsigned int tileY,
0054                               unsigned int offX,  unsigned int offY,
0055                               unsigned char *versions)
0056 {
0057     //Determine the range which needs pushing.
0058     int first = 0xFFFF, last = 0;
0059     if (!tile->pixmap) {
0060         //### this can be wasteful if we do conversion
0061         setupTile(tileX, tileY, tile);
0062         first = 0;
0063         last  = tileHeight(tileY) - 1;
0064     } else {
0065         ImageManager::pixmapCache()->touchEntry(tile);
0066 
0067         // figure out the dirty range
0068         for (unsigned int line = 0; line < tileHeight(tileY); ++line) {
0069             if (versions[line] > tile->versions[line]) {
0070                 last = line;
0071                 if (first == 0xFFFF) {
0072                     first = line;
0073                 }
0074             }
0075         }
0076     }
0077 
0078     // Now tile will be up-to-date, so sync up w/our versions array
0079     std::memcpy(tile->versions, versions, Tile::TileSize);
0080 
0081     assert(tile->pixmap);
0082 
0083     //Special case, hopefully-fast path: if we just wants to push
0084     //the whole image to the whole tile, do convertFromImage directly.
0085     if (offX == 0 && offY == 0 && first == 0  && last == image.height() - 1 &&
0086             tile->pixmap->width()  == image.width() &&
0087             tile->pixmap->height() == image.height()) {
0088         tile->discard();
0089         tile->pixmap = new QPixmap(QPixmap::fromImage(image));
0090     } else {
0091         if (image.hasAlphaChannel()) {
0092             //### When supported, use the src op.
0093             QRect imagePortion(offX, offY,
0094                                tile->pixmap->width(),
0095                                tile->pixmap->height());
0096 
0097             //Note: we're copying the entire tile-size here,
0098             //and not just the dirty portion.
0099             QImage portion = image.copy(imagePortion);
0100             tile->pixmap->fill(Qt::transparent);
0101             QPainter p(tile->pixmap);
0102             p.drawImage(0, 0, portion);
0103         } else {
0104             QRect imagePortion(offX, offY + first,
0105                                tile->pixmap->width(), last - first + 1);
0106 
0107             QImage portion = image.copy(imagePortion);
0108             //Push the image portion update
0109             QPainter p(tile->pixmap);
0110             p.drawImage(QPoint(0, first), portion);
0111         }
0112     }
0113 }
0114 
0115 ImagePlane::~ImagePlane()
0116 {}
0117 
0118 }