| 1 | /****************************************************************************
|
|---|
| 2 | ** $Id: qimage.h 2 2005-11-16 15:49:26Z dmik $
|
|---|
| 3 | **
|
|---|
| 4 | ** Definition of QImage and QImageIO classes
|
|---|
| 5 | **
|
|---|
| 6 | ** Created : 950207
|
|---|
| 7 | **
|
|---|
| 8 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
|---|
| 9 | **
|
|---|
| 10 | ** This file is part of the kernel module of the Qt GUI Toolkit.
|
|---|
| 11 | **
|
|---|
| 12 | ** This file may be distributed under the terms of the Q Public License
|
|---|
| 13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
|---|
| 14 | ** LICENSE.QPL included in the packaging of this file.
|
|---|
| 15 | **
|
|---|
| 16 | ** This file may be distributed and/or modified under the terms of the
|
|---|
| 17 | ** GNU General Public License version 2 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 19 | ** packaging of this file.
|
|---|
| 20 | **
|
|---|
| 21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
|---|
| 22 | ** licenses may use this file in accordance with the Qt Commercial License
|
|---|
| 23 | ** Agreement provided with the Software.
|
|---|
| 24 | **
|
|---|
| 25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
|---|
| 26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|---|
| 27 | **
|
|---|
| 28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
|---|
| 29 | ** information about Qt Commercial License Agreements.
|
|---|
| 30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
|---|
| 31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
|---|
| 32 | **
|
|---|
| 33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
|---|
| 34 | ** not clear to you.
|
|---|
| 35 | **
|
|---|
| 36 | **********************************************************************/
|
|---|
| 37 |
|
|---|
| 38 | #ifndef QIMAGE_H
|
|---|
| 39 | #define QIMAGE_H
|
|---|
| 40 |
|
|---|
| 41 | #ifndef QT_H
|
|---|
| 42 | #include "qpixmap.h"
|
|---|
| 43 | #include "qstrlist.h"
|
|---|
| 44 | #include "qstringlist.h"
|
|---|
| 45 | #endif // QT_H
|
|---|
| 46 |
|
|---|
| 47 | class QImageDataMisc; // internal
|
|---|
| 48 | #ifndef QT_NO_IMAGE_TEXT
|
|---|
| 49 | class Q_EXPORT QImageTextKeyLang {
|
|---|
| 50 | public:
|
|---|
| 51 | QImageTextKeyLang(const char* k, const char* l) : key(k), lang(l) { }
|
|---|
| 52 | QImageTextKeyLang() { }
|
|---|
| 53 |
|
|---|
| 54 | QCString key;
|
|---|
| 55 | QCString lang;
|
|---|
| 56 |
|
|---|
| 57 | bool operator< (const QImageTextKeyLang& other) const
|
|---|
| 58 | { return key < other.key || key==other.key && lang < other.lang; }
|
|---|
| 59 | bool operator== (const QImageTextKeyLang& other) const
|
|---|
| 60 | { return key==other.key && lang==other.lang; }
|
|---|
| 61 | };
|
|---|
| 62 | #endif //QT_NO_IMAGE_TEXT
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 | class Q_EXPORT QImage
|
|---|
| 66 | {
|
|---|
| 67 | public:
|
|---|
| 68 | enum Endian { IgnoreEndian, BigEndian, LittleEndian };
|
|---|
| 69 |
|
|---|
| 70 | QImage();
|
|---|
| 71 | QImage( int width, int height, int depth, int numColors=0,
|
|---|
| 72 | Endian bitOrder=IgnoreEndian );
|
|---|
| 73 | QImage( const QSize&, int depth, int numColors=0,
|
|---|
| 74 | Endian bitOrder=IgnoreEndian );
|
|---|
| 75 | #ifndef QT_NO_IMAGEIO
|
|---|
| 76 | QImage( const QString &fileName, const char* format=0 );
|
|---|
| 77 | QImage( const char * const xpm[] );
|
|---|
| 78 | QImage( const QByteArray &data );
|
|---|
| 79 | #endif
|
|---|
| 80 | QImage( uchar* data, int w, int h, int depth,
|
|---|
| 81 | QRgb* colortable, int numColors,
|
|---|
| 82 | Endian bitOrder );
|
|---|
| 83 | #ifdef Q_WS_QWS
|
|---|
| 84 | QImage( uchar* data, int w, int h, int depth, int pbl,
|
|---|
| 85 | QRgb* colortable, int numColors,
|
|---|
| 86 | Endian bitOrder );
|
|---|
| 87 | #endif
|
|---|
| 88 | QImage( const QImage & );
|
|---|
| 89 | ~QImage();
|
|---|
| 90 |
|
|---|
| 91 | QImage &operator=( const QImage & );
|
|---|
| 92 | QImage &operator=( const QPixmap & );
|
|---|
| 93 | bool operator==( const QImage & ) const;
|
|---|
| 94 | bool operator!=( const QImage & ) const;
|
|---|
| 95 | void detach();
|
|---|
| 96 | QImage copy() const;
|
|---|
| 97 | QImage copy(int x, int y, int w, int h, int conversion_flags=0) const;
|
|---|
| 98 | QImage copy(const QRect&) const;
|
|---|
| 99 | #ifndef QT_NO_MIME
|
|---|
| 100 | static QImage fromMimeSource( const QString& abs_name );
|
|---|
| 101 | #endif
|
|---|
| 102 | bool isNull() const { return data->bits == 0; }
|
|---|
| 103 |
|
|---|
| 104 | int width() const { return data->w; }
|
|---|
| 105 | int height() const { return data->h; }
|
|---|
| 106 | QSize size() const { return QSize(data->w,data->h); }
|
|---|
| 107 | QRect rect() const { return QRect(0,0,data->w,data->h); }
|
|---|
| 108 | int depth() const { return data->d; }
|
|---|
| 109 | int numColors() const { return data->ncols; }
|
|---|
| 110 | Endian bitOrder() const { return (Endian) data->bitordr; }
|
|---|
| 111 |
|
|---|
| 112 | QRgb color( int i ) const;
|
|---|
| 113 | void setColor( int i, QRgb c );
|
|---|
| 114 | void setNumColors( int );
|
|---|
| 115 |
|
|---|
| 116 | bool hasAlphaBuffer() const;
|
|---|
| 117 | void setAlphaBuffer( bool );
|
|---|
| 118 |
|
|---|
| 119 | bool allGray() const;
|
|---|
| 120 | bool isGrayscale() const;
|
|---|
| 121 |
|
|---|
| 122 | uchar *bits() const;
|
|---|
| 123 | uchar *scanLine( int ) const;
|
|---|
| 124 | uchar **jumpTable() const;
|
|---|
| 125 | QRgb *colorTable() const;
|
|---|
| 126 | int numBytes() const;
|
|---|
| 127 | int bytesPerLine() const;
|
|---|
| 128 |
|
|---|
| 129 | #ifdef Q_WS_QWS
|
|---|
| 130 | QGfx * graphicsContext();
|
|---|
| 131 | #endif
|
|---|
| 132 |
|
|---|
| 133 | bool create( int width, int height, int depth, int numColors=0,
|
|---|
| 134 | Endian bitOrder=IgnoreEndian );
|
|---|
| 135 | bool create( const QSize&, int depth, int numColors=0,
|
|---|
| 136 | Endian bitOrder=IgnoreEndian );
|
|---|
| 137 | void reset();
|
|---|
| 138 |
|
|---|
| 139 | void fill( uint pixel );
|
|---|
| 140 | void invertPixels( bool invertAlpha = TRUE );
|
|---|
| 141 |
|
|---|
| 142 | QImage convertDepth( int ) const;
|
|---|
| 143 | #ifndef QT_NO_IMAGE_TRUECOLOR
|
|---|
| 144 | QImage convertDepthWithPalette( int, QRgb* p, int pc, int cf=0 ) const;
|
|---|
| 145 | #endif
|
|---|
| 146 | QImage convertDepth( int, int conversion_flags ) const;
|
|---|
| 147 | QImage convertBitOrder( Endian ) const;
|
|---|
| 148 |
|
|---|
| 149 | enum ScaleMode {
|
|---|
| 150 | ScaleFree,
|
|---|
| 151 | ScaleMin,
|
|---|
| 152 | ScaleMax
|
|---|
| 153 | };
|
|---|
| 154 | #ifndef QT_NO_IMAGE_SMOOTHSCALE
|
|---|
| 155 | QImage smoothScale( int w, int h, ScaleMode mode=ScaleFree ) const;
|
|---|
| 156 | QImage smoothScale( const QSize& s, ScaleMode mode=ScaleFree ) const;
|
|---|
| 157 | #endif
|
|---|
| 158 | #ifndef QT_NO_IMAGE_TRANSFORMATION
|
|---|
| 159 | QImage scale( int w, int h, ScaleMode mode=ScaleFree ) const;
|
|---|
| 160 | QImage scale( const QSize& s, ScaleMode mode=ScaleFree ) const;
|
|---|
| 161 | QImage scaleWidth( int w ) const;
|
|---|
| 162 | QImage scaleHeight( int h ) const;
|
|---|
| 163 | QImage xForm( const QWMatrix &matrix ) const;
|
|---|
| 164 | #endif
|
|---|
| 165 |
|
|---|
| 166 | #ifndef QT_NO_IMAGE_DITHER_TO_1
|
|---|
| 167 | QImage createAlphaMask( int conversion_flags=0 ) const;
|
|---|
| 168 | #endif
|
|---|
| 169 | #ifndef QT_NO_IMAGE_HEURISTIC_MASK
|
|---|
| 170 | QImage createHeuristicMask( bool clipTight=TRUE ) const;
|
|---|
| 171 | #endif
|
|---|
| 172 | #ifndef QT_NO_IMAGE_MIRROR
|
|---|
| 173 | QImage mirror() const;
|
|---|
| 174 | QImage mirror(bool horizontally, bool vertically) const;
|
|---|
| 175 | #endif
|
|---|
| 176 | QImage swapRGB() const;
|
|---|
| 177 |
|
|---|
| 178 | static Endian systemBitOrder();
|
|---|
| 179 | static Endian systemByteOrder();
|
|---|
| 180 |
|
|---|
| 181 | #ifndef QT_NO_IMAGEIO
|
|---|
| 182 | static const char* imageFormat( const QString &fileName );
|
|---|
| 183 | static QStrList inputFormats();
|
|---|
| 184 | static QStrList outputFormats();
|
|---|
| 185 | #ifndef QT_NO_STRINGLIST
|
|---|
| 186 | static QStringList inputFormatList();
|
|---|
| 187 | static QStringList outputFormatList();
|
|---|
| 188 | #endif
|
|---|
| 189 | bool load( const QString &fileName, const char* format=0 );
|
|---|
| 190 | bool loadFromData( const uchar *buf, uint len,
|
|---|
| 191 | const char *format=0 );
|
|---|
| 192 | bool loadFromData( QByteArray data, const char* format=0 );
|
|---|
| 193 | bool save( const QString &fileName, const char* format,
|
|---|
| 194 | int quality=-1 ) const;
|
|---|
| 195 | bool save( QIODevice * device, const char* format,
|
|---|
| 196 | int quality=-1 ) const;
|
|---|
| 197 | #endif //QT_NO_IMAGEIO
|
|---|
| 198 |
|
|---|
| 199 | bool valid( int x, int y ) const;
|
|---|
| 200 | int pixelIndex( int x, int y ) const;
|
|---|
| 201 | QRgb pixel( int x, int y ) const;
|
|---|
| 202 | void setPixel( int x, int y, uint index_or_rgb );
|
|---|
| 203 |
|
|---|
| 204 | // Auxiliary data
|
|---|
| 205 | int dotsPerMeterX() const;
|
|---|
| 206 | int dotsPerMeterY() const;
|
|---|
| 207 | void setDotsPerMeterX(int);
|
|---|
| 208 | void setDotsPerMeterY(int);
|
|---|
| 209 | QPoint offset() const;
|
|---|
| 210 | void setOffset(const QPoint&);
|
|---|
| 211 | #ifndef QT_NO_IMAGE_TEXT
|
|---|
| 212 | QValueList<QImageTextKeyLang> textList() const;
|
|---|
| 213 | QStringList textLanguages() const;
|
|---|
| 214 | QStringList textKeys() const;
|
|---|
| 215 | QString text(const char* key, const char* lang=0) const;
|
|---|
| 216 | QString text(const QImageTextKeyLang&) const;
|
|---|
| 217 | void setText(const char* key, const char* lang, const QString&);
|
|---|
| 218 | #endif
|
|---|
| 219 | private:
|
|---|
| 220 | void init();
|
|---|
| 221 | void reinit();
|
|---|
| 222 | void freeBits();
|
|---|
| 223 | static void warningIndexRange( const char *, int );
|
|---|
| 224 |
|
|---|
| 225 | struct QImageData : public QShared { // internal image data
|
|---|
| 226 | int w; // image width
|
|---|
| 227 | int h; // image height
|
|---|
| 228 | int d; // image depth
|
|---|
| 229 | int ncols; // number of colors
|
|---|
| 230 | int nbytes; // number of bytes data
|
|---|
| 231 | int bitordr; // bit order (1 bit depth)
|
|---|
| 232 | QRgb *ctbl; // color table
|
|---|
| 233 | uchar **bits; // image data
|
|---|
| 234 | bool alpha; // alpha buffer
|
|---|
| 235 | int dpmx; // dots per meter X (or 0)
|
|---|
| 236 | int dpmy; // dots per meter Y (or 0)
|
|---|
| 237 | QPoint offset; // offset in pixels
|
|---|
| 238 | #ifndef QT_NO_IMAGE_TEXT
|
|---|
| 239 | QImageDataMisc* misc; // less common stuff
|
|---|
| 240 | #endif
|
|---|
| 241 | bool ctbl_mine; // this allocated ctbl
|
|---|
| 242 | } *data;
|
|---|
| 243 | #ifndef QT_NO_IMAGE_TEXT
|
|---|
| 244 | QImageDataMisc& misc() const;
|
|---|
| 245 | #endif
|
|---|
| 246 | #ifndef QT_NO_IMAGEIO
|
|---|
| 247 | bool doImageIO( QImageIO* io, int quality ) const;
|
|---|
| 248 | #endif
|
|---|
| 249 | friend Q_EXPORT void bitBlt( QImage* dst, int dx, int dy,
|
|---|
| 250 | const QImage* src, int sx, int sy,
|
|---|
| 251 | int sw, int sh, int conversion_flags );
|
|---|
| 252 | };
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 | // QImage stream functions
|
|---|
| 256 |
|
|---|
| 257 | #if !defined(QT_NO_DATASTREAM) && !defined(QT_NO_IMAGEIO)
|
|---|
| 258 | Q_EXPORT QDataStream &operator<<( QDataStream &, const QImage & );
|
|---|
| 259 | Q_EXPORT QDataStream &operator>>( QDataStream &, QImage & );
|
|---|
| 260 | #endif
|
|---|
| 261 |
|
|---|
| 262 | #ifndef QT_NO_IMAGEIO
|
|---|
| 263 | class QIODevice;
|
|---|
| 264 | typedef void (*image_io_handler)( QImageIO * ); // image IO handler
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 | struct QImageIOData;
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 | class Q_EXPORT QImageIO
|
|---|
| 271 | {
|
|---|
| 272 | public:
|
|---|
| 273 | QImageIO();
|
|---|
| 274 | QImageIO( QIODevice *ioDevice, const char *format );
|
|---|
| 275 | QImageIO( const QString &fileName, const char* format );
|
|---|
| 276 | ~QImageIO();
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 | const QImage &image() const { return im; }
|
|---|
| 280 | int status() const { return iostat; }
|
|---|
| 281 | const char *format() const { return frmt; }
|
|---|
| 282 | QIODevice *ioDevice() const { return iodev; }
|
|---|
| 283 | QString fileName() const { return fname; }
|
|---|
| 284 | int quality() const;
|
|---|
| 285 | QString description() const { return descr; }
|
|---|
| 286 | const char *parameters() const;
|
|---|
| 287 | float gamma() const;
|
|---|
| 288 |
|
|---|
| 289 | void setImage( const QImage & );
|
|---|
| 290 | void setStatus( int );
|
|---|
| 291 | void setFormat( const char * );
|
|---|
| 292 | void setIODevice( QIODevice * );
|
|---|
| 293 | void setFileName( const QString & );
|
|---|
| 294 | void setQuality( int );
|
|---|
| 295 | void setDescription( const QString & );
|
|---|
| 296 | void setParameters( const char * );
|
|---|
| 297 | void setGamma( float );
|
|---|
| 298 |
|
|---|
| 299 | bool read();
|
|---|
| 300 | bool write();
|
|---|
| 301 |
|
|---|
| 302 | static const char* imageFormat( const QString &fileName );
|
|---|
| 303 | static const char *imageFormat( QIODevice * );
|
|---|
| 304 | static QStrList inputFormats();
|
|---|
| 305 | static QStrList outputFormats();
|
|---|
| 306 |
|
|---|
| 307 | static void defineIOHandler( const char *format,
|
|---|
| 308 | const char *header,
|
|---|
| 309 | const char *flags,
|
|---|
| 310 | image_io_handler read_image,
|
|---|
| 311 | image_io_handler write_image );
|
|---|
| 312 |
|
|---|
| 313 | private:
|
|---|
| 314 | void init();
|
|---|
| 315 |
|
|---|
| 316 | QImage im; // image
|
|---|
| 317 | int iostat; // IO status
|
|---|
| 318 | QCString frmt; // image format
|
|---|
| 319 | QIODevice *iodev; // IO device
|
|---|
| 320 | QString fname; // file name
|
|---|
| 321 | char *params; // image parameters //### change to QImageIOData *d in 3.0
|
|---|
| 322 | QString descr; // image description
|
|---|
| 323 | QImageIOData *d;
|
|---|
| 324 |
|
|---|
| 325 | private: // Disabled copy constructor and operator=
|
|---|
| 326 | #if defined(Q_DISABLE_COPY)
|
|---|
| 327 | QImageIO( const QImageIO & );
|
|---|
| 328 | QImageIO &operator=( const QImageIO & );
|
|---|
| 329 | #endif
|
|---|
| 330 | };
|
|---|
| 331 |
|
|---|
| 332 | #endif //QT_NO_IMAGEIO
|
|---|
| 333 |
|
|---|
| 334 | Q_EXPORT void bitBlt( QImage* dst, int dx, int dy, const QImage* src,
|
|---|
| 335 | int sx=0, int sy=0, int sw=-1, int sh=-1,
|
|---|
| 336 | int conversion_flags=0 );
|
|---|
| 337 |
|
|---|
| 338 |
|
|---|
| 339 | /*****************************************************************************
|
|---|
| 340 | QImage member functions
|
|---|
| 341 | *****************************************************************************/
|
|---|
| 342 |
|
|---|
| 343 | inline bool QImage::hasAlphaBuffer() const
|
|---|
| 344 | {
|
|---|
| 345 | return data->alpha;
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | inline uchar *QImage::bits() const
|
|---|
| 349 | {
|
|---|
| 350 | return data->bits ? data->bits[0] : 0;
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | inline uchar **QImage::jumpTable() const
|
|---|
| 354 | {
|
|---|
| 355 | return data->bits;
|
|---|
| 356 | }
|
|---|
| 357 |
|
|---|
| 358 | inline QRgb *QImage::colorTable() const
|
|---|
| 359 | {
|
|---|
| 360 | return data->ctbl;
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| 363 | inline int QImage::numBytes() const
|
|---|
| 364 | {
|
|---|
| 365 | return data->nbytes;
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 | inline int QImage::bytesPerLine() const
|
|---|
| 369 | {
|
|---|
| 370 | return data->h ? data->nbytes/data->h : 0;
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | inline QImage QImage::copy(const QRect& r) const
|
|---|
| 374 | {
|
|---|
| 375 | return copy(r.x(), r.y(), r.width(), r.height());
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | inline QRgb QImage::color( int i ) const
|
|---|
| 379 | {
|
|---|
| 380 | #if defined(QT_CHECK_RANGE)
|
|---|
| 381 | if ( i >= data->ncols )
|
|---|
| 382 | warningIndexRange( "color", i );
|
|---|
| 383 | #endif
|
|---|
| 384 | return data->ctbl ? data->ctbl[i] : (QRgb)-1;
|
|---|
| 385 | }
|
|---|
| 386 |
|
|---|
| 387 | inline void QImage::setColor( int i, QRgb c )
|
|---|
| 388 | {
|
|---|
| 389 | #if defined(QT_CHECK_RANGE)
|
|---|
| 390 | if ( i >= data->ncols )
|
|---|
| 391 | warningIndexRange( "setColor", i );
|
|---|
| 392 | #endif
|
|---|
| 393 | if ( data->ctbl )
|
|---|
| 394 | data->ctbl[i] = c;
|
|---|
| 395 | }
|
|---|
| 396 |
|
|---|
| 397 | inline uchar *QImage::scanLine( int i ) const
|
|---|
| 398 | {
|
|---|
| 399 | #if defined(QT_CHECK_RANGE)
|
|---|
| 400 | if ( i >= data->h )
|
|---|
| 401 | warningIndexRange( "scanLine", i );
|
|---|
| 402 | #endif
|
|---|
| 403 | return data->bits ? data->bits[i] : 0;
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | inline int QImage::dotsPerMeterX() const
|
|---|
| 407 | {
|
|---|
| 408 | return data->dpmx;
|
|---|
| 409 | }
|
|---|
| 410 |
|
|---|
| 411 | inline int QImage::dotsPerMeterY() const
|
|---|
| 412 | {
|
|---|
| 413 | return data->dpmy;
|
|---|
| 414 | }
|
|---|
| 415 |
|
|---|
| 416 | inline QPoint QImage::offset() const
|
|---|
| 417 | {
|
|---|
| 418 | return data->offset;
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 | #endif // QIMAGE_H
|
|---|