KHtml

scaledimageplane.cpp
1 /*
2  Large image displaying library.
3 
4  Copyright (C) 2004,2005 Maks Orlovich ([email protected])
5  Copyright (C) 2012 Martin Sandsmark ([email protected])
6 
7  Permission is hereby granted, free of charge, to any person obtaining a copy
8  of this software and associated documentation files (the "Software"), to deal
9  in the Software without restriction, including without limitation the rights
10  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  copies of the Software, and to permit persons to whom the Software is
12  furnished to do so, subject to the following conditions:
13 
14  The above copyright notice and this permission notice shall be included in
15  all copies or substantial portions of the Software.
16 
17  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 
24 */
25 #include "scaledimageplane.h"
26 #include "imagemanager.h"
27 
28 namespace khtmlImLoad
29 {
30 ScaledImagePlane::ScaledImagePlane(unsigned int _width, unsigned int _height, RawImagePlane *_parent):
31  ImagePlane(_width, _height), parent(_parent), tiles(tilesWidth, tilesHeight), m_width(_width), m_height(_height)
32 {
33 }
34 
35 bool ScaledImagePlane::isUpToDate(unsigned int tileX, unsigned int tileY,
36  PixmapTile *tile)
37 {
38  Q_UNUSED(tileX);
39  if (!tile->pixmap) {
40  return false;
41  }
42 
43  int yStep = (parent->image.height() << 8) / height;
44  for (unsigned int line = 0, srcLine = 0; line < tileHeight(tileY); ++line, srcLine += yStep) {
45  if (tile->versions[line] < parent->versions[(srcLine + yStep) >> 8]) {
46  return false;
47  }
48  }
49 
50  return true;
51 }
52 
53 // Trick from here: https://www.compuphase.com/graphic/scale3.htm
54 #define AVG(a,b) ( ((((a)^(b)) & 0xfefefefeUL) >> 1) + ((a)&(b)) )
55 static void scaleLoop32bit(QImage *dst, const QImage &src, int tileX, int tileY,
56  int height, int width, unsigned char *versions, unsigned char *parentVersions)
57 {
58  int yStep = (src.height() << 8) / height; // We bitshift for when we get large ratios
59  int xStep = (src.width() << 8) / width;
60  for (int yTarget = 0, ySource = tileY * Tile::TileSize * yStep; yTarget < dst->height(); yTarget++, ySource += yStep) {
61  const quint32 *srcBelow = reinterpret_cast<const quint32 *>(src.scanLine(ySource >> 8));
62  const quint32 *srcAbove = reinterpret_cast<const quint32 *>(src.scanLine((ySource + yStep / 2) >> 8));
63  quint32 *destination = reinterpret_cast<quint32 *>(dst->scanLine(yTarget));
64 
65  versions[yTarget] = parentVersions[ySource >> 8];
66  for (int xTarget = 0, xSource = tileX * Tile::TileSize * xStep; xTarget < dst->width(); xTarget++, xSource += xStep) {
67  quint32 average1 = AVG(srcBelow[xSource >> 8], srcAbove[(xSource + xStep / 2) >> 8]);
68  quint32 average2 = AVG(srcBelow[(xSource + xStep / 2) >> 8], srcAbove[xSource >> 8]);
69  destination[xTarget] = AVG(average1, average2);
70  }
71  }
72 }
73 
74 static void scaleLoop8bit(QImage *dst, const QImage &src, int tileX, int tileY,
75  int height, int width, unsigned char *versions, unsigned char *parentVersions)
76 {
77  int yStep = (src.height() << 8) / height; // We bitshift for when we get large ratios
78  int xStep = (src.width() << 8) / width;
79  for (int yTarget = 0, ySource = tileY * Tile::TileSize * yStep; yTarget < dst->height(); yTarget++, ySource += yStep) {
80  const quint8 *srcPix = reinterpret_cast<const quint8 *>(src.scanLine(ySource >> 8));
81  quint8 *destination = reinterpret_cast<quint8 *>(dst->scanLine(yTarget));
82  versions[yTarget] = parentVersions[ySource >> 8];
83  for (int xTarget = 0, xSource = tileX * Tile::TileSize * xStep; xTarget < dst->width(); xTarget++, xSource += xStep) {
84  destination[xTarget] = srcPix[xSource >> 8];
85  }
86  }
87 }
88 
89 void ScaledImagePlane::flushCache()
90 {
91  for (unsigned tileX = 0; tileX < tilesWidth; ++tileX) {
92  for (unsigned tileY = 0; tileY < tilesHeight; ++tileY) {
93  ImageTile &imageTile = tiles.at(tileX, tileY);
94  if (!imageTile.image.isNull()) {
95  ImageManager::imageCache()->removeEntry(&imageTile);
96  }
97  }
98  }
99 }
100 
101 void ScaledImagePlane::ensureUpToDate(unsigned int tileX, unsigned int tileY,
102  PixmapTile *tile)
103 {
104  ImageTile &imageTile = tiles.at(tileX, tileY);
105 
106  //Create the image if need be.
107  if (imageTile.image.isNull()) {
108  imageTile.image = parent->format.makeImage(tileWidth(tileX),
109  tileHeight(tileY));
110  ImageManager::imageCache()->addEntry(&imageTile);
111  std::memset(imageTile.versions, 0, Tile::TileSize);
112  } else {
113  ImageManager::imageCache()->touchEntry(&imageTile);
114  }
115 
116  // Do the actual scaling
117  if (parent->format.depth() == 1) {
118  scaleLoop8bit(&imageTile.image, parent->image, tileX, tileY, m_height, m_width, imageTile.versions, parent->versions);
119  } else {
120  scaleLoop32bit(&imageTile.image, parent->image, tileX, tileY, m_height, m_width, imageTile.versions, parent->versions);
121  }
122 
123  //Now, push stuff into the pixmap.
124  updatePixmap(tile, imageTile.image, tileX, tileY, 0, 0, imageTile.versions);
125 }
126 
127 ScaledImagePlane::~ScaledImagePlane()
128 {
129 }
130 
131 }
132 
uchar * scanLine(int i)
int width() const const
int height() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 25 2021 22:48:22 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.