home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kpixmapio.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-09-10  |  5.8 KB  |  186 lines

  1. /* vi: ts=8 sts=4 sw=4
  2.  *
  3.  *
  4.  * This file is part of the KDE project, module kdeui.
  5.  * Copyright (C) 2000 Geert Jansen <jansen@kde.org>
  6.  *
  7.  * You can Freely distribute this program under the GNU Library General
  8.  * Public License. See the file "COPYING.LIB" for the exact licensing terms.
  9.  */
  10.  
  11. #ifndef __KPixmapIO_h_Included__
  12. #define __KPixmapIO_h_Included__
  13.  
  14. #include <kdelibs_export.h>
  15.  
  16. class QPixmap;
  17. class QImage;
  18. class QPoint;
  19. class QRect;
  20. struct KPixmapIOPrivate;
  21. /**
  22.  * @short Fast QImage to/from QPixmap conversion.
  23.  * @author Geert Jansen <jansen@kde.org>
  24.  * @version $Id: kpixmapio.h 345236 2004-09-09 15:23:55Z dfaure $
  25.  *
  26.  * KPixmapIO implements a fast path for QPixmap to/from QImage conversions.
  27.  * It uses the MIT-SHM shared memory extension for this. If this extension is
  28.  * not available, it will fall back to standard Qt methods.
  29.  *
  30.  * <b>Typical usage:</b>\n
  31.  *
  32.  * You can use KPixmapIO for load/saving pixmaps.
  33.  *
  34.  * \code
  35.  * KPixmapIO io;
  36.  * pixmap = io.convertToPixmap(image);
  37.  * image = io.convertToImage(pixmap);
  38.  * \endcode
  39.  *
  40.  * It also has functionality for partially updating/saving pixmaps, see
  41.  * putImage and getImage.
  42.  *
  43.  * <b>KPixmapIO vs. Qt speed comparison</b>\n
  44.  *
  45.  * Speed measurements were taken. These show that usage of KPixmapIO for
  46.  * images up to a certain threshold size, offers no speed advantage over
  47.  * the Qt routines. Below you can see a plot of these measurements.
  48.  *
  49.  * @image html kpixmapio-perf.png "Performance of KPixmapIO"
  50.  *
  51.  * The threshold size, amongst other causes, is determined by the shared
  52.  * memory allocation policy. If the policy is @p ShmDontKeep, the
  53.  * shared memory segment is discarded right after usage, and thus needs to
  54.  * be allocated before each transfer. This introduces a a setup penalty not
  55.  * present when the policy is @p ShmKeepAndGrow. In this case the
  56.  * shared memory segment is kept and resized when necessary, until the
  57.  * KPixmapIO object is destroyed.
  58.  *
  59.  * The default policy is @p ShmDontKeep. This policy makes sense when
  60.  * loading pixmaps once. The corresponding threshold is taken at 5.000
  61.  * pixels as suggested by experiments. Below this threshold, KPixmapIO
  62.  * will not use shared memory and fall back on the Qt routines.
  63.  *
  64.  * When the policy is @p ShmKeepAndGrow, the threshold is taken at
  65.  * 2.000 pixels. Using this policy, you might want to use preAllocShm
  66.  * to pre-allocate a certain amount of shared memory, in order to avoid
  67.  * resizes. This allocation policy makes sense in a multimedia type
  68.  * application where you are constantly updating the screen.
  69.  *
  70.  * Above a couple times the threshold size, KPixmapIO's and Qt's speed become
  71.  * linear in the number of pixels, KPixmapIO being at least 2, and mostly around
  72.  * 4 times faster than Qt, depending on the screen and image depth.
  73.  *
  74.  * Speed difference seems to be the most at 16 bpp, followed by 32 and 24
  75.  * bpp respectively. This can be explained by the relatively poor
  76.  * implementation of 16 bit RGB packing in Qt, while at 32 bpp we need to
  77.  * transfer more data, and thus gain more, than at 24 bpp.
  78.  *
  79.  * <b>Conclusion:</b>\n
  80.  *
  81.  * For large pixmaps, there's a definite speed improvement when using
  82.  * KPixmapIO. On the other hand, there's no speed improvement for small
  83.  * pixmaps. When you know you're only transferring small pixmaps, there's no
  84.  * point in using it.
  85.  */
  86.  
  87. class KDEUI_EXPORT KPixmapIO
  88. {
  89. public:
  90.     KPixmapIO();
  91.     ~KPixmapIO();
  92.  
  93.     /**
  94.      * Convert an image to a pixmap.
  95.      * @param image The image to convert.
  96.      * @return The pixmap containing the image.
  97.      */
  98.     QPixmap convertToPixmap(const QImage &image);
  99.  
  100.     /**
  101.      * Convert a pixmap to an image.
  102.      * @param pixmap The pixmap to convert.
  103.      * @return The image.
  104.      */
  105.     QImage convertToImage(const QPixmap &pixmap);
  106.  
  107.     /**
  108.      * Bitblt an image onto a pixmap.
  109.      * @param dst The destination pixmap.
  110.      * @param dx Destination x offset.
  111.      * @param dy Destination y offset.
  112.      * @param src The image to load.
  113.      */
  114.     void putImage(QPixmap *dst, int dx, int dy, const QImage *src);
  115.  
  116.     /**
  117.      * This function is identical to the one above. It only differs in the
  118.      * arguments it accepts.
  119.      */
  120.     void putImage(QPixmap *dst, const QPoint &offset, const QImage *src);
  121.  
  122.     /**
  123.      * Transfer (a part of) a pixmap to an image.
  124.      * @param src The source pixmap.
  125.      * @param sx Source x offset.
  126.      * @param sy Source y offset.
  127.      * @param sw Source width.
  128.      * @param sh Source height.
  129.      * @return The image.
  130.      */
  131.     QImage getImage(const QPixmap *src, int sx, int sy, int sw, int sh);
  132.  
  133.     /**
  134.      * This function is identical to the one above. It only differs in the
  135.      * arguments it accepts.
  136.      */
  137.     QImage getImage(const QPixmap *src, const QRect &rect);
  138.  
  139.     /**
  140.      * Shared memory allocation policies.
  141.      */
  142.     enum ShmPolicies {
  143.     ShmDontKeep,
  144.     ShmKeepAndGrow
  145.     };
  146.  
  147.     /**
  148.      * Set the shared memory allocation policy. See the introduction for
  149.      * KPixmapIO for a discussion.
  150.      * @param policy The alloction policy.
  151.      */
  152.     void setShmPolicy(int policy);
  153.  
  154.     /**
  155.      * Pre-allocate shared memory. KPixmapIO will be able to transfer images
  156.      * up to this size without resizing.
  157.      * @param size The size of the image in @p pixels.
  158.      */
  159.     void preAllocShm(int size);
  160.  
  161. private:
  162.     /*
  163.      * Supported XImage byte orders. The notation ARGB means bytes
  164.      * containing A:R:G:B succeed in memory.
  165.      */
  166.     enum ByteOrders {
  167.     bo32_ARGB, bo32_BGRA, bo24_RGB, bo24_BGR,
  168.     bo16_RGB_565, bo16_BGR_565, bo16_RGB_555,
  169.     bo16_BGR_555, bo8
  170.     };
  171.  
  172.     bool m_bShm;
  173.     bool initXImage(int w, int h);
  174.     void doneXImage();
  175.     bool createXImage(int w, int h);
  176.     void destroyXImage();
  177.     bool createShmSegment(int size);
  178.     void destroyShmSegment();
  179.     void convertToXImage(const QImage &);
  180.     QImage convertFromXImage();
  181. private:
  182.     KPixmapIOPrivate* d;
  183. };
  184.  
  185. #endif // __KPixmapIO_h_Included__
  186.