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

0001 /*
0002     Large image displaying library.
0003 
0004     Copyright (C) 2004,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 
0025 #include "pixmapplane.h"
0026 
0027 #include <QPainter>
0028 
0029 namespace khtmlImLoad
0030 {
0031 
0032 void PixmapPlane::paint(int dx, int dy, QPainter *p,
0033                         int sx, int sy, int sWidth, int sHeight)
0034 {
0035     //Do some basic clipping, discarding invalid requests and adjusting sizes of others.
0036     if (sy >= (int)height) {
0037         return;
0038     }
0039     if (sx >= (int)width) {
0040         return;
0041     }
0042 
0043     if (sWidth == -1) {
0044         sWidth = width;
0045     }
0046 
0047     if (sHeight == -1) {
0048         sHeight = height;
0049     }
0050 
0051     unsigned int ey = sy + sHeight - 1;
0052     if (ey > height - 1) {
0053         ey = height - 1;
0054     }
0055 
0056     unsigned int ex = sx + sWidth - 1;
0057     if (ex > width - 1) {
0058         ex = width - 1;
0059     }
0060 
0061     sHeight = ey - sy + 1;
0062     sWidth  = ex - sx + 1;
0063 
0064     //Calculate the range of tiles to paint, in both directions
0065     unsigned int startTileY = sy / Tile::TileSize;
0066     unsigned int endTileY   = ey / Tile::TileSize;
0067 
0068     unsigned int startTileX = sx / Tile::TileSize;
0069     unsigned int endTileX   = ex / Tile::TileSize;
0070 
0071     //Walk through all the rows
0072     unsigned int paintY = dy;
0073     for (unsigned int tileY = startTileY; tileY <= endTileY; ++tileY) {
0074         //see how much we have to paint -- end points are different
0075         unsigned int startY = 0;
0076         unsigned int endY   = Tile::TileSize - 1;
0077 
0078         if (tileY == startTileY) {
0079             startY = sy % Tile::TileSize;
0080         }
0081 
0082         if (tileY == endTileY) {
0083             endY   = ey % Tile::TileSize;
0084         }
0085 
0086         unsigned int paintHeight = endY - startY + 1;
0087 
0088         //Now through some columns
0089         unsigned int paintX = dx;
0090         for (unsigned int tileX = startTileX; tileX <= endTileX; ++tileX) {
0091             //calculate the horizontal size. Some redundancy here,
0092             //since these are the same for all rows, but I'd rather
0093             //avoid heap allocation or alloca..
0094             unsigned int startX = 0;
0095             unsigned int endX   = Tile::TileSize - 1;
0096 
0097             if (tileX == startTileX) {
0098                 startX = sx % Tile::TileSize;
0099             }
0100 
0101             if (tileX == endTileX) {
0102                 endX   = ex % Tile::TileSize;
0103             }
0104 
0105             int paintWidth = endX - startX + 1;
0106 
0107             //Update from image plane if need be
0108             PixmapTile &tile = tiles.at(tileX, tileY);
0109             if (!parent->isUpToDate(tileX, tileY, &tile)) {
0110                 parent->ensureUpToDate(tileX, tileY, &tile);
0111             }
0112 
0113             //Draw as much as we have
0114             if (tile.pixmap) {
0115                 //Scan the versions to see how much to paint.
0116                 unsigned int h = 0;
0117                 for (int checkY = startY; checkY < Tile::TileSize && tile.versions[checkY]; ++checkY) {
0118                     ++h;
0119                 }
0120 
0121                 //Draw it, if there is anything (note: Qt would interpret 0 as everything)
0122                 if (h)
0123                     p->drawPixmap(paintX, paintY, *tile.pixmap, startX, startY,
0124                                   paintWidth, qMin(h, paintHeight));
0125             }
0126             paintX += paintWidth;
0127         }
0128         paintY += paintHeight;
0129     }
0130 }
0131 
0132 void PixmapPlane::flushCache()
0133 {
0134     parent->flushCache();
0135     for (unsigned tileX = 0; tileX < tilesWidth; ++tileX) {
0136         for (unsigned tileY = 0; tileY < tilesHeight; ++tileY) {
0137             PixmapTile &pixTile = tiles.at(tileX, tileY);
0138             if (pixTile.pixmap) {
0139                 ImageManager::pixmapCache()->removeEntry(&pixTile);
0140             }
0141         }
0142     }
0143 }
0144 
0145 }
0146