source: trunk/src/gui/image/qimageiohandler.cpp@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 17.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43 \class QImageIOHandler
44 \brief The QImageIOHandler class defines the common image I/O
45 interface for all image formats in Qt.
46 \reentrant
47
48 Qt uses QImageIOHandler for reading and writing images through
49 QImageReader and QImageWriter. You can also derive from this class
50 to write your own image format handler using Qt's plugin mechanism.
51
52 Call setDevice() to assign a device to the handler, and
53 setFormat() to assign a format to it. One QImageIOHandler may
54 support more than one image format. canRead() returns true if an
55 image can be read from the device, and read() and write() return
56 true if reading or writing an image was completed successfully.
57
58 QImageIOHandler also has support for animations formats, through
59 the functions loopCount(), imageCount(), nextImageDelay() and
60 currentImageNumber().
61
62 In order to determine what options an image handler supports, Qt
63 will call supportsOption() and setOption(). Make sure to
64 reimplement these functions if you can provide support for any of
65 the options in the ImageOption enum.
66
67 To write your own image handler, you must at least reimplement
68 canRead() and read(). Then create a QImageIOPlugin that
69 can create the handler. Finally, install your plugin, and
70 QImageReader and QImageWriter will then automatically load the
71 plugin, and start using it.
72
73 \sa QImageIOPlugin, QImageReader, QImageWriter
74*/
75
76/*! \enum QImageIOHandler::ImageOption
77
78 This enum describes the different options supported by
79 QImageIOHandler. Some options are used to query an image for
80 properties, and others are used to toggle the way in which an
81 image should be written.
82
83 \value Size The original size of an image. A handler that supports
84 this option is expected to read the size of the image from the
85 image metadata, and return this size from option() as a QSize.
86
87 \value ClipRect The clip rect, or ROI (Region Of Interest). A
88 handler that supports this option is expected to only read the
89 provided QRect area from the original image in read(), before any
90 other transformation is applied.
91
92 \value ScaledSize The scaled size of the image. A handler that
93 supports this option is expected to scale the image to the
94 provided size (a QSize), after applying any clip rect
95 transformation (ClipRect). If the handler does not support this
96 option, QImageReader will perform the scaling after the image has
97 been read.
98
99 \value ScaledClipRect The scaled clip rect (or ROI, Region Of
100 Interest) of the image. A handler that supports this option is
101 expected to apply the provided clip rect (a QRect), after applying
102 any scaling (ScaleSize) or regular clipping (ClipRect). If the
103 handler does not support this option, QImageReader will apply the
104 scaled clip rect after the image has been read.
105
106 \value Description The image description. Some image formats,
107 such as GIF and PNG, allow embedding of text
108 or comments into the image data (e.g., for storing copyright
109 information). It's common that the text is stored in key-value
110 pairs, but some formats store all text in one continuous block.
111 QImageIOHandler returns the text as one
112 QString, where keys and values are separated by a ':', and
113 keys-value pairs are separated by two newlines (\\n\\n). For example,
114 "Title: Sunset\\n\\nAuthor: Jim Smith\\nSarah Jones\\n\\n". Formats that
115 store text in a single block can use "Description" as the key.
116
117 \value CompressionRatio The compression ratio of the image data. A
118 handler that supports this option is expected to set its
119 compression rate depending on the value of this option (an int)
120 when writing.
121
122 \value Gamma The gamma level of the image. A handler that supports
123 this option is expected to set the image gamma level depending on
124 the value of this option (a float) when writing.
125
126 \value Quality The quality level of the image. A handler that
127 supports this option is expected to set the image quality level
128 depending on the value of this option (an int) when writing.
129
130 \value Name The name of the image. A handler that supports this
131 option is expected to read the name from the image metadata and
132 return this as a QString, or when writing an image it is expected
133 to store the name in the image metadata.
134
135 \value SubType The subtype of the image. A handler that supports
136 this option can use the subtype value to help when reading and
137 writing images. For example, a PPM handler may have a subtype
138 value of "ppm" or "ppmraw".
139
140 \value IncrementalReading A handler that supports this option is
141 expected to read the image in several passes, as if it was an
142 animation. QImageReader will treat the image as an animation.
143
144 \value Endianness The endianness of the image. Certain image
145 formats can be stored as BigEndian or LittleEndian. A handler that
146 supports Endianness uses the value of this option to determine how
147 the image should be stored.
148
149 \value Animation Image formats that support animation return
150 true for this value in supportsOption(); otherwise, false is returned.
151
152 \value BackgroundColor Certain image formats allow the
153 background color to be specified. A handler that supports
154 BackgroundColor initializes the background color to this option
155 (a QColor) when reading an image.
156
157 \value ImageFormat The image's data format returned by the handler.
158 This can be any of the formats listed in QImage::Format.
159*/
160
161/*!
162 \class QImageIOPlugin
163 \brief The QImageIOPlugin class defines an interface for writing
164 an image format plugin.
165 \reentrant
166
167 \ingroup plugins
168
169 QImageIOPlugin is a factory for creating QImageIOHandler objects,
170 which are used internally by QImageReader and QImageWriter to add
171 support for different image formats to Qt.
172
173 Writing an image I/O plugin is achieved by subclassing this
174 base class, reimplementing the pure virtual functions capabilities(),
175 create(), and keys(), and exporting the class with the
176 Q_EXPORT_PLUGIN2() macro. See \l{How to Create Qt Plugins} for details.
177
178 An image format plugin can support three capabilities: reading (\l
179 CanRead), writing (\l CanWrite) and \e incremental reading (\l
180 CanReadIncremental). Reimplement capabilities() in you subclass to
181 expose the capabilities of your image format.
182
183 create() should create an instance of your QImageIOHandler
184 subclass, with the provided device and format properly set, and
185 return this handler. You must also reimplement keys() so that Qt
186 knows which image formats your plugin supports.
187
188 Different plugins can support different capabilities. For example,
189 you may have one plugin that supports reading the GIF format, and
190 another that supports writing. Qt will select the correct plugin
191 for the job, depending on the return value of capabilities(). If
192 several plugins support the same capability, Qt will select one
193 arbitrarily.
194
195 \sa QImageIOHandler, {How to Create Qt Plugins}
196*/
197
198/*!
199 \enum QImageIOPlugin::Capability
200
201 This enum describes the capabilities of a QImageIOPlugin.
202
203 \value CanRead The plugin can read images.
204 \value CanWrite The plugin can write images.
205 \value CanReadIncremental The plugin can read images incrementally.
206*/
207
208/*!
209 \class QImageIOHandlerFactoryInterface
210 \brief The QImageIOHandlerFactoryInterface class provides the factory
211 interface for QImageIOPlugin.
212 \reentrant
213
214 \internal
215
216 \sa QImageIOPlugin
217*/
218
219#include "qimageiohandler.h"
220
221#include <qbytearray.h>
222#include <qimage.h>
223#include <qvariant.h>
224
225QT_BEGIN_NAMESPACE
226
227class QIODevice;
228
229class QImageIOHandlerPrivate
230{
231 Q_DECLARE_PUBLIC(QImageIOHandler)
232public:
233 QImageIOHandlerPrivate(QImageIOHandler *q);
234 virtual ~QImageIOHandlerPrivate();
235
236 QIODevice *device;
237 mutable QByteArray format;
238
239 QImageIOHandler *q_ptr;
240};
241
242QImageIOHandlerPrivate::QImageIOHandlerPrivate(QImageIOHandler *q)
243{
244 device = 0;
245 q_ptr = q;
246}
247
248QImageIOHandlerPrivate::~QImageIOHandlerPrivate()
249{
250}
251
252/*!
253 Constructs a QImageIOHandler object.
254*/
255QImageIOHandler::QImageIOHandler()
256 : d_ptr(new QImageIOHandlerPrivate(this))
257{
258}
259
260/*! \internal
261
262 Constructs a QImageIOHandler object, using the private member \a
263 dd.
264*/
265QImageIOHandler::QImageIOHandler(QImageIOHandlerPrivate &dd)
266 : d_ptr(&dd)
267{
268}
269
270/*!
271 Destructs the QImageIOHandler object.
272*/
273QImageIOHandler::~QImageIOHandler()
274{
275 delete d_ptr;
276}
277
278/*!
279 Sets the device of the QImageIOHandler to \a device. The image
280 handler will use this device when reading and writing images.
281
282 The device can only be set once and must be set before calling
283 canRead(), read(), write(), etc. If you need to read multiple
284 files, construct multiple instances of the appropriate
285 QImageIOHandler subclass.
286
287 \sa device()
288*/
289void QImageIOHandler::setDevice(QIODevice *device)
290{
291 Q_D(QImageIOHandler);
292 d->device = device;
293}
294
295/*!
296 Returns the device currently assigned to the QImageIOHandler. If
297 not device has been assigned, 0 is returned.
298*/
299QIODevice *QImageIOHandler::device() const
300{
301 Q_D(const QImageIOHandler);
302 return d->device;
303}
304
305/*!
306 Sets the format of the QImageIOHandler to \a format. The format is
307 most useful for handlers that support multiple image formats.
308
309 \sa format()
310*/
311void QImageIOHandler::setFormat(const QByteArray &format)
312{
313 Q_D(QImageIOHandler);
314 d->format = format;
315}
316
317/*!
318 Sets the format of the QImageIOHandler to \a format. The format is
319 most useful for handlers that support multiple image formats.
320
321 This function is declared const so that it can be called from canRead().
322
323 \sa format()
324*/
325void QImageIOHandler::setFormat(const QByteArray &format) const
326{
327 Q_D(const QImageIOHandler);
328 d->format = format;
329}
330
331/*!
332 Returns the format that is currently assigned to
333 QImageIOHandler. If no format has been assigned, an empty string
334 is returned.
335
336 \sa setFormat()
337*/
338QByteArray QImageIOHandler::format() const
339{
340 Q_D(const QImageIOHandler);
341 return d->format;
342}
343
344/*!
345 \fn bool QImageIOHandler::read(QImage *image)
346
347 Read an image from the device, and stores it in \a image.
348 Returns true if the image is successfully read; otherwise returns
349 false.
350
351 For image formats that support incremental loading, and for animation
352 formats, the image handler can assume that \a image points to the
353 previous frame.
354
355 \sa canRead()
356*/
357
358/*!
359 \fn bool QImageIOHandler::canRead() const
360
361 Returns true if an image can be read from the device (i.e., the
362 image format is supported, the device can be read from and the
363 initial header information suggests that the image can be read);
364 otherwise returns false.
365
366 When reimplementing canRead(), make sure that the I/O device
367 (device()) is left in its original state (e.g., by using peek()
368 rather than read()).
369
370 \sa read(), QIODevice::peek()
371*/
372
373/*!
374 \obsolete
375
376 Use format() instead.
377*/
378
379QByteArray QImageIOHandler::name() const
380{
381 return format();
382}
383
384/*!
385 Writes the image \a image to the assigned device. Returns true on
386 success; otherwise returns false.
387
388 The default implementation does nothing, and simply returns false.
389*/
390bool QImageIOHandler::write(const QImage &image)
391{
392 Q_UNUSED(image);
393 return false;
394}
395
396/*!
397 Sets the option \a option with the value \a value.
398
399 \sa option(), ImageOption
400*/
401void QImageIOHandler::setOption(ImageOption option, const QVariant &value)
402{
403 Q_UNUSED(option);
404 Q_UNUSED(value);
405}
406
407/*!
408 Returns the value assigned to \a option as a QVariant. The type of
409 the value depends on the option. For example, option(Size) returns
410 a QSize variant.
411
412 \sa setOption(), supportsOption()
413*/
414QVariant QImageIOHandler::option(ImageOption option) const
415{
416 Q_UNUSED(option);
417 return QVariant();
418}
419
420/*!
421 Returns true if the QImageIOHandler supports the option \a option;
422 otherwise returns false. For example, if the QImageIOHandler
423 supports the \l Size option, supportsOption(Size) must return
424 true.
425
426 \sa setOption(), option()
427*/
428bool QImageIOHandler::supportsOption(ImageOption option) const
429{
430 Q_UNUSED(option);
431 return false;
432}
433
434/*!
435 For image formats that support animation, this function returns
436 the sequence number of the current image in the animation. If
437 this function is called before any image is read(), -1 is
438 returned. The number of the first image in the sequence is 0.
439
440 If the image format does not support animation, 0 is returned.
441
442 \sa read()
443*/
444int QImageIOHandler::currentImageNumber() const
445{
446 return 0;
447}
448
449/*!
450 Returns the rect of the current image. If no rect is defined for the
451 image, and empty QRect() is returned.
452
453 This function is useful for animations, where only parts of the frame
454 may be updated at a time.
455*/
456QRect QImageIOHandler::currentImageRect() const
457{
458 return QRect();
459}
460
461/*!
462 For image formats that support animation, this function returns
463 the number of images in the animation. If the image format does
464 not support animation, or if it is unable to determine the number
465 of images, 0 is returned.
466
467 The default implementation returns 1 if canRead() returns true;
468 otherwise 0 is returned.
469*/
470int QImageIOHandler::imageCount() const
471{
472 return canRead() ? 1 : 0;
473}
474
475/*!
476 For image formats that support animation, this function jumps to the
477 next image.
478
479 The default implementation does nothing, and returns false.
480*/
481bool QImageIOHandler::jumpToNextImage()
482{
483 return false;
484}
485
486/*!
487 For image formats that support animation, this function jumps to the image
488 whose sequence number is \a imageNumber. The next call to read() will
489 attempt to read this image.
490
491 The default implementation does nothing, and returns false.
492*/
493bool QImageIOHandler::jumpToImage(int imageNumber)
494{
495 Q_UNUSED(imageNumber);
496 return false;
497}
498
499/*!
500 For image formats that support animation, this function returns
501 the number of times the animation should loop. If the image format
502 does not support animation, 0 is returned.
503*/
504int QImageIOHandler::loopCount() const
505{
506 return 0;
507}
508
509/*!
510 For image formats that support animation, this function returns
511 the number of milliseconds to wait until reading the next
512 image. If the image format does not support animation, 0 is
513 returned.
514*/
515int QImageIOHandler::nextImageDelay() const
516{
517 return 0;
518}
519
520/*!
521 Constructs an image plugin with the given \a parent. This is
522 invoked automatically by the Q_EXPORT_PLUGIN2() macro.
523*/
524QImageIOPlugin::QImageIOPlugin(QObject *parent)
525 : QObject(parent)
526{
527}
528
529/*!
530 Destroys the picture format plugin.
531
532 You never have to call this explicitly. Qt destroys a plugin
533 automatically when it is no longer used.
534*/
535QImageIOPlugin::~QImageIOPlugin()
536{
537}
538
539/*! \fn QImageIOPlugin::capabilities(QIODevice *device, const QByteArray &format) const
540
541 Returns the capabilities on the plugin, based on the data in \a
542 device and the format \a format. For example, if the
543 QImageIOHandler supports the BMP format, and the data in the
544 device starts with the characters "BM", this function should
545 return \l CanRead. If \a format is "bmp" and the handler supports
546 both reading and writing, this function should return \l CanRead |
547 \l CanWrite.
548*/
549
550/*!
551 \fn QImageIOPlugin::keys() const
552
553 Returns the list of image keys this plugin supports.
554
555 These keys are usually the names of the image formats that are implemented
556 in the plugin (e.g., "jpg" or "gif").
557
558 \sa capabilities()
559*/
560
561/*!
562 \fn QImageIOHandler *QImageIOPlugin::create(QIODevice *device, const QByteArray &format) const
563
564 Creates and returns a QImageIOHandler subclass, with \a device
565 and \a format set. The \a format must come from the list returned by keys().
566 Format names are case sensitive.
567
568 \sa keys()
569*/
570
571QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.