File indexing completed on 2024-04-28 04:20:22

0001 
0002 /*
0003    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
0004    All rights reserved.
0005 
0006    Redistribution and use in source and binary forms, with or without
0007    modification, are permitted provided that the following conditions
0008    are met:
0009 
0010    1. Redistributions of source code must retain the above copyright
0011       notice, this list of conditions and the following disclaimer.
0012    2. Redistributions in binary form must reproduce the above copyright
0013       notice, this list of conditions and the following disclaimer in the
0014       documentation and/or other materials provided with the distribution.
0015 
0016    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0017    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0018    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0019    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0020    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0021    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0022    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0023    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0025    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026 */
0027 
0028 
0029 #define DEBUG_KP_PIXMAP_FX 0
0030 
0031 
0032 #include "kpPixmapFX.h"
0033 
0034 
0035 #include <QImage>
0036 #include <QPainter>
0037 #include <QPoint>
0038 #include <QRect>
0039 
0040 #include "kpLogCategories.h"
0041 
0042 #include "imagelib/kpColor.h"
0043 
0044 //---------------------------------------------------------------------
0045 
0046 // public static
0047 QImage kpPixmapFX::getPixmapAt (const QImage &image, const QRect &rect)
0048 {
0049   return image.copy(rect);
0050 }
0051 
0052 //---------------------------------------------------------------------
0053 
0054 // public static
0055 void kpPixmapFX::setPixmapAt(QImage *destPtr, const QRect &destRect,
0056                              const QImage &src)
0057 {
0058 #if DEBUG_KP_PIXMAP_FX && 1
0059     qCDebug(kpLogPixmapfx) << "kpPixmapFX::setPixmapAt(destPixmap->rect="
0060                << destPtr->rect ()
0061                << ",destRect="
0062                << destRect
0063                << ",src.rect="
0064                << src.rect ()
0065                << ")";
0066 #endif
0067 
0068     Q_ASSERT (destPtr);
0069 
0070     // You cannot copy more than what you have.
0071     Q_ASSERT (destRect.width () <= src.width () &&
0072               destRect.height () <= src.height ());
0073 
0074     QPainter painter(destPtr);
0075     // destination shall be source only
0076     painter.setCompositionMode(QPainter::CompositionMode_Source);
0077     painter.drawImage(destRect.topLeft(), src, QRect(0, 0, destRect.width(), destRect.height()));
0078 }
0079 
0080 //---------------------------------------------------------------------
0081 
0082 // public static
0083 void kpPixmapFX::setPixmapAt (QImage *destPtr, const QPoint &destAt,
0084                               const QImage &src)
0085 {
0086     kpPixmapFX::setPixmapAt (destPtr,
0087                              QRect (destAt.x (), destAt.y (),
0088                                     src.width (), src.height ()),
0089                              src);
0090 }
0091 
0092 //---------------------------------------------------------------------
0093 
0094 // public static
0095 void kpPixmapFX::setPixmapAt (QImage *destPtr, int destX, int destY,
0096                               const QImage &src)
0097 {
0098     kpPixmapFX::setPixmapAt (destPtr, QPoint (destX, destY), src);
0099 }
0100 
0101 //---------------------------------------------------------------------
0102 
0103 // public static
0104 void kpPixmapFX::paintPixmapAt (QImage *destPtr, const QPoint &destAt,
0105                                 const QImage &src)
0106 {
0107     // draw image with SourceOver composition mode
0108     QPainter painter(destPtr);
0109     painter.drawImage(destAt, src);
0110 }
0111 
0112 //---------------------------------------------------------------------
0113 
0114 // public static
0115 void kpPixmapFX::paintPixmapAt (QImage *destPtr, int destX, int destY,
0116                                 const QImage &src)
0117 {
0118     kpPixmapFX::paintPixmapAt(destPtr, QPoint (destX, destY), src);
0119 }
0120 
0121 //---------------------------------------------------------------------
0122 
0123 // public static
0124 kpColor kpPixmapFX::getColorAtPixel (const QImage &img, const QPoint &at)
0125 {
0126     if (!img.valid (at.x (), at.y ())) {
0127         return kpColor::Invalid;
0128     }
0129 
0130     QRgb rgba = img.pixel(at);
0131     return kpColor (rgba);
0132 }
0133 
0134 //---------------------------------------------------------------------
0135 
0136 // public static
0137 kpColor kpPixmapFX::getColorAtPixel (const QImage &img, int x, int y)
0138 {
0139     return kpPixmapFX::getColorAtPixel (img, QPoint (x, y));
0140 }
0141 
0142 //---------------------------------------------------------------------