File indexing completed on 2024-05-19 15:12:18

0001 /*
0002     KHTML image displaying library.
0003 
0004     Copyright (C) 2007 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 "canvasimage.h"
0025 #include "imageowner.h"
0026 #include "pixmapplane.h"
0027 #include "rawimageplane.h"
0028 #include "scaledimageplane.h"
0029 
0030 namespace khtmlImLoad
0031 {
0032 
0033 class TrivialImageOwner : public ImageOwner
0034 {
0035 public:
0036     void imageHasGeometry(Image *, int, int) override {}
0037     void imageChange(Image *, QRect) override {}
0038     void imageError(Image *) override {};
0039     void imageDone(Image *) override {};
0040 };
0041 
0042 ImageOwner *CanvasImage::s_trivialOwner = nullptr;
0043 
0044 ImageOwner *CanvasImage::trivialOwner()
0045 {
0046     if (!s_trivialOwner) {
0047         s_trivialOwner = new TrivialImageOwner;
0048     }
0049     return s_trivialOwner;
0050 }
0051 
0052 void CanvasImage::setupOriginalPlane(int width, int height)
0053 {
0054     fullyDecoded = true;
0055     this->width  = width;
0056     this->height = height;
0057 
0058     RawImagePlane *imgPlane = new RawImagePlane(width, height, 1 /*already "loaded"*/);
0059     imgPlane->format.type = ImageFormat::Image_ARGB_32;
0060     imgPlane->image = QImage(width, height, QImage::Format_ARGB32_Premultiplied);
0061     original = new PixmapPlane(width, height, imgPlane);
0062 
0063 }
0064 
0065 CanvasImage::CanvasImage(int width, int height): Image(trivialOwner())
0066 {
0067     setupOriginalPlane(width, height);
0068 }
0069 
0070 void CanvasImage::contentUpdated()
0071 {
0072     flushAllCaches();
0073 }
0074 
0075 void CanvasImage::flushAllCaches()
0076 {
0077     // Flush all the planes, including any scaled ones, etc.
0078     original->flushCache();
0079 
0080     for (QMap<QPair<int, int>, PixmapPlane *>::iterator i = scaled.begin(); i != scaled.end(); ++i) {
0081         i.value()->flushCache();
0082     }
0083 }
0084 
0085 void CanvasImage::resizeImage(int width, int height)
0086 {
0087     // Dump any cached info, it's useless
0088     flushAllCaches();
0089 
0090     // Create a new master pixmap and raw image planes
0091     delete original;
0092     setupOriginalPlane(width, height);
0093     RawImagePlane *imgPlane = static_cast<RawImagePlane *>(original->parent);
0094 
0095     // Now go through the scaling cache, and fix things up.
0096     for (QMap<QPair<int, int>, PixmapPlane *>::iterator i = scaled.begin(); i != scaled.end(); ++i) {
0097         PixmapPlane *scaledPix = i.value();
0098         delete scaledPix->parent;
0099         scaledPix->parent = new ScaledImagePlane(scaledPix->width, scaledPix->height, imgPlane);
0100     }
0101 }
0102 
0103 }
0104