document all currently defined image options and their values
[wxWidgets.git] / interface / wx / image.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: image.h
3 // Purpose: interface of wxImageHandler and wxImage
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
8
9 /**
10 Possible values for the image resolution option.
11
12 @see wxImage::GetOptionInt().
13 */
14 enum wxImageResolution
15 {
16 /// Resolution not specified.
17 wxIMAGE_RESOLUTION_NONE = 0,
18
19 /// Resolution specified in inches.
20 wxIMAGE_RESOLUTION_INCHES = 1,
21
22 /// Resolution specified in centimetres.
23 wxIMAGE_RESOLUTION_CM = 2
24 };
25
26 /**
27 Possible values for PNG image type option.
28
29 @see wxImage::GetOptionInt().
30 */
31 enum wxImagePNGType
32 {
33 wxPNG_TYPE_COLOUR = 0, ///< Colour PNG image.
34 wxPNG_TYPE_GREY = 2, ///< Greyscale PNG image converted from RGB.
35 wxPNG_TYPE_GREY_RED = 3 ///< Greyscale PNG image using red as grey.
36 };
37
38 /**
39 @class wxImageHandler
40
41 This is the base class for implementing image file loading/saving, and
42 image creation from data.
43 It is used within wxImage and is not normally seen by the application.
44
45 If you wish to extend the capabilities of wxImage, derive a class from
46 wxImageHandler and add the handler using wxImage::AddHandler in your
47 application initialisation.
48
49 Note that all wxImageHandlers provided by wxWidgets are part of
50 the @ref page_libs_wxcore library.
51 For details about the default handlers, please see the section
52 @ref image_handlers in the wxImage class documentation.
53
54
55 @section imagehandler_note Note (Legal Issue)
56
57 This software is based in part on the work of the Independent JPEG Group.
58 (Applies when wxWidgets is linked with JPEG support.
59 wxJPEGHandler uses libjpeg created by IJG.)
60
61
62 @stdobjects
63 ::wxNullImage
64
65 @library{wxcore}
66 @category{misc}
67
68 @see wxImage, wxInitAllImageHandlers()
69 */
70 class wxImageHandler : public wxObject
71 {
72 public:
73 /**
74 Default constructor.
75
76 In your own default constructor, initialise the members
77 m_name, m_extension and m_type.
78 */
79 wxImageHandler();
80
81 /**
82 Destroys the wxImageHandler object.
83 */
84 virtual ~wxImageHandler();
85
86 /**
87 Gets the file extension associated with this handler.
88 */
89 const wxString& GetExtension() const;
90
91 /**
92 If the image file contains more than one image and the image handler is capable
93 of retrieving these individually, this function will return the number of
94 available images.
95
96 @param stream
97 Opened input stream for reading image data.
98 Currently, the stream must support seeking.
99
100 @return Number of available images. For most image handlers, this is 1
101 (exceptions are TIFF and ICO formats).
102 */
103 virtual int GetImageCount(wxInputStream& stream);
104
105 /**
106 Gets the MIME type associated with this handler.
107 */
108 const wxString& GetMimeType() const;
109
110 /**
111 Gets the name of this handler.
112 */
113 const wxString& GetName() const;
114
115 /**
116 Gets the image type associated with this handler.
117 */
118 wxBitmapType GetType() const;
119
120 /**
121 Loads a image from a stream, putting the resulting data into @a image.
122
123 If the image file contains more than one image and the image handler is
124 capable of retrieving these individually, @a index indicates which image
125 to read from the stream.
126
127 @param image
128 The image object which is to be affected by this operation.
129 @param stream
130 Opened input stream for reading image data.
131 @param verbose
132 If set to @true, errors reported by the image handler will produce
133 wxLogMessages.
134 @param index
135 The index of the image in the file (starting from zero).
136
137 @return @true if the operation succeeded, @false otherwise.
138
139 @see wxImage::LoadFile, wxImage::SaveFile, SaveFile()
140 */
141 virtual bool LoadFile(wxImage* image, wxInputStream& stream,
142 bool verbose = true, int index = -1);
143
144 /**
145 Saves a image in the output stream.
146
147 @param image
148 The image object which is to be affected by this operation.
149 @param stream
150 Opened output stream for writing the data.
151 @param verbose
152 If set to @true, errors reported by the image handler will produce
153 wxLogMessages.
154
155 @return @true if the operation succeeded, @false otherwise.
156
157 @see wxImage::LoadFile, wxImage::SaveFile, LoadFile()
158 */
159 virtual bool SaveFile(wxImage* image, wxOutputStream& stream,
160 bool verbose = true);
161
162 /**
163 Sets the handler extension.
164
165 @param extension
166 Handler extension.
167 */
168 void SetExtension(const wxString& extension);
169
170 /**
171 Sets the handler MIME type.
172
173 @param mimetype
174 Handler MIME type.
175 */
176 void SetMimeType(const wxString& mimetype);
177
178 /**
179 Sets the handler name.
180
181 @param name
182 Handler name.
183 */
184 void SetName(const wxString& name);
185 };
186
187
188 /**
189 Constant used to indicate the alpha value conventionally defined as
190 the complete transparency.
191 */
192 const unsigned char wxIMAGE_ALPHA_TRANSPARENT = 0;
193
194 /**
195 Constant used to indicate the alpha value conventionally defined as
196 the complete opacity.
197 */
198 const unsigned char wxIMAGE_ALPHA_OPAQUE = 0xff;
199
200 /**
201 @class wxImage
202
203 This class encapsulates a platform-independent image.
204
205 An image can be created from data, or using wxBitmap::ConvertToImage.
206 An image can be loaded from a file in a variety of formats, and is extensible
207 to new formats via image format handlers. Functions are available to set and
208 get image bits, so it can be used for basic image manipulation.
209
210 A wxImage cannot (currently) be drawn directly to a wxDC.
211 Instead, a platform-specific wxBitmap object must be created from it using
212 the wxBitmap::wxBitmap(wxImage,int depth) constructor.
213 This bitmap can then be drawn in a device context, using wxDC::DrawBitmap.
214
215 One colour value of the image may be used as a mask colour which will lead to
216 the automatic creation of a wxMask object associated to the bitmap object.
217
218
219 @section image_alpha Alpha channel support
220
221 Starting from wxWidgets 2.5.0 wxImage supports alpha channel data, that is
222 in addition to a byte for the red, green and blue colour components for each
223 pixel it also stores a byte representing the pixel opacity.
224
225 An alpha value of 0 corresponds to a transparent pixel (null opacity) while
226 a value of 255 means that the pixel is 100% opaque.
227 The constants ::wxIMAGE_ALPHA_TRANSPARENT and ::wxIMAGE_ALPHA_OPAQUE can be
228 used to indicate those values in a more readable form.
229
230 Unlike RGB data, not all images have an alpha channel and before using
231 wxImage::GetAlpha you should check if this image contains an alpha channel
232 with wxImage::HasAlpha. Note that currently only the PNG format has full
233 alpha channel support so only the images loaded from PNG files can have
234 alpha and, if you initialize the image alpha channel yourself using
235 wxImage::SetAlpha, you should save it in PNG format to avoid losing it.
236
237
238 @section image_handlers Available image handlers
239
240 The following image handlers are available.
241 wxBMPHandler is always installed by default.
242 To use other image formats, install the appropriate handler with
243 wxImage::AddHandler or call ::wxInitAllImageHandlers().
244
245 - wxBMPHandler: For loading and saving, always installed.
246 - wxPNGHandler: For loading (including alpha support) and saving.
247 - wxJPEGHandler: For loading and saving.
248 - wxGIFHandler: Only for loading, due to legal issues.
249 - wxPCXHandler: For loading and saving (see below).
250 - wxPNMHandler: For loading and saving (see below).
251 - wxTIFFHandler: For loading and saving.
252 - wxTGAHandler: For loading only.
253 - wxIFFHandler: For loading only.
254 - wxXPMHandler: For loading and saving.
255 - wxICOHandler: For loading and saving.
256 - wxCURHandler: For loading and saving.
257 - wxANIHandler: For loading only.
258
259 When saving in PCX format, wxPCXHandler will count the number of different
260 colours in the image; if there are 256 or less colours, it will save as 8 bit,
261 else it will save as 24 bit.
262
263 Loading PNMs only works for ASCII or raw RGB images.
264 When saving in PNM format, wxPNMHandler will always save as raw RGB.
265
266
267 @library{wxcore}
268 @category{gdi}
269
270 @stdobjects
271 ::wxNullImage
272
273 @see wxBitmap, wxInitAllImageHandlers(), wxPixelData
274 */
275 class wxImage : public wxObject
276 {
277 public:
278 class RGBValue
279 {
280 public:
281 /**
282 Constructor for RGBValue, an object that contains values for red, green
283 and blue which represent the value of a color.
284
285 It is used by wxImage::HSVtoRGB and wxImage::RGBtoHSV, which converts
286 between HSV color space and RGB color space.
287 */
288 RGBValue(unsigned char r=0, unsigned char g=0, unsigned char b=0);
289 };
290
291 class HSVValue
292 {
293 public:
294 /**
295 Constructor for HSVValue, an object that contains values for hue, saturation
296 and value which represent the value of a color.
297
298 It is used by wxImage::HSVtoRGB() and wxImage::RGBtoHSV(), which converts
299 between HSV color space and RGB color space.
300 */
301 HSVValue(double h=0.0, double s=0.0, double v=0.0);
302 };
303
304 /**
305 Creates an empty wxImage object without an alpha channel.
306 */
307 wxImage();
308
309 /**
310 Creates an image with the given size and clears it if requested.
311
312 Does not create an alpha channel.
313
314 @param width
315 Specifies the width of the image.
316 @param height
317 Specifies the height of the image.
318 @param clear
319 If @true, initialize the image to black.
320 */
321 wxImage(int width, int height, bool clear = true);
322
323 /**
324 Creates an image from data in memory. If @a static_data is @false
325 then the wxImage will take ownership of the data and free it
326 afterwards. For this, it has to be allocated with @e malloc.
327
328 @param width
329 Specifies the width of the image.
330 @param height
331 Specifies the height of the image.
332 @param data
333 A pointer to RGB data
334 @param static_data
335 Indicates if the data should be free'd after use
336
337 */
338 wxImage(int width, int height, unsigned char* data, bool static_data = false);
339
340 /**
341 Creates an image from data in memory. If @a static_data is @false
342 then the wxImage will take ownership of the data and free it
343 afterwards. For this, it has to be allocated with @e malloc.
344
345 @param width
346 Specifies the width of the image.
347 @param height
348 Specifies the height of the image.
349 @param data
350 A pointer to RGB data
351 @param alpha
352 A pointer to alpha-channel data
353 @param static_data
354 Indicates if the data should be free'd after use
355
356 */
357 wxImage(int width, int height, unsigned char* data, unsigned char* alpha,
358 bool static_data = false );
359
360 /**
361 Creates an image from XPM data.
362
363 @param xpmData
364 A pointer to XPM image data.
365 */
366 wxImage(const char* const* xpmData);
367
368 /**
369 Creates an image from a file.
370
371 @param name
372 Name of the file from which to load the image.
373 @param type
374 May be one of the following:
375 @li wxBITMAP_TYPE_BMP: Load a Windows bitmap file.
376 @li wxBITMAP_TYPE_GIF: Load a GIF bitmap file.
377 @li wxBITMAP_TYPE_JPEG: Load a JPEG bitmap file.
378 @li wxBITMAP_TYPE_PNG: Load a PNG bitmap file.
379 @li wxBITMAP_TYPE_PCX: Load a PCX bitmap file.
380 @li wxBITMAP_TYPE_PNM: Load a PNM bitmap file.
381 @li wxBITMAP_TYPE_TIF: Load a TIFF bitmap file.
382 @li wxBITMAP_TYPE_TGA: Load a TGA bitmap file.
383 @li wxBITMAP_TYPE_XPM: Load a XPM bitmap file.
384 @li wxBITMAP_TYPE_ICO: Load a Windows icon file (ICO).
385 @li wxBITMAP_TYPE_CUR: Load a Windows cursor file (CUR).
386 @li wxBITMAP_TYPE_ANI: Load a Windows animated cursor file (ANI).
387 @li wxBITMAP_TYPE_ANY: Will try to autodetect the format.
388 @param index
389 Index of the image to load in the case that the image file contains
390 multiple images. This is only used by GIF, ICO and TIFF handlers.
391 The default value (-1) means "choose the default image" and is
392 interpreted as the first image (index=0) by the GIF and TIFF handler
393 and as the largest and most colourful one by the ICO handler.
394
395 @remarks Depending on how wxWidgets has been configured and by which
396 handlers have been loaded, not all formats may be available.
397 Any handler other than BMP must be previously initialized with
398 wxImage::AddHandler or wxInitAllImageHandlers.
399
400 @note
401 You can use GetOptionInt() to get the hotspot when loading cursor files:
402 @code
403 int hotspot_x = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
404 int hotspot_y = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
405 @endcode
406
407 @see LoadFile()
408 */
409 wxImage(const wxString& name, wxBitmapType type = wxBITMAP_TYPE_ANY, int index = -1);
410
411 /**
412 Creates an image from a file using MIME-types to specify the type.
413
414 @param name
415 Name of the file from which to load the image.
416 @param mimetype
417 MIME type string (for example 'image/jpeg')
418 @param index
419 See description in wxImage(const wxString&, wxBitmapType, int) overload.
420 */
421 wxImage(const wxString& name, const wxString& mimetype, int index = -1);
422
423 /**
424 Creates an image from a stream.
425
426 @param stream
427 Opened input stream from which to load the image. Currently,
428 the stream must support seeking.
429 @param type
430 See description in wxImage(const wxString&, wxBitmapType, int) overload.
431 @param index
432 See description in wxImage(const wxString&, wxBitmapType, int) overload.
433 */
434 wxImage(wxInputStream& stream, wxBitmapType type = wxBITMAP_TYPE_ANY, int index = -1);
435
436 /**
437 Creates an image from a stream using MIME-types to specify the type.
438
439 @param stream
440 Opened input stream from which to load the image. Currently,
441 the stream must support seeking.
442 @param mimetype
443 MIME type string (for example 'image/jpeg')
444 @param index
445 See description in wxImage(const wxString&, wxBitmapType, int) overload.
446 */
447 wxImage(wxInputStream& stream, const wxString& mimetype, int index = -1);
448
449
450 /**
451 Destructor.
452
453 See @ref overview_refcount_destruct "reference-counted object destruction"
454 for more info.
455 */
456 virtual ~wxImage();
457
458 /**
459 Register an image handler.
460 */
461 static void AddHandler(wxImageHandler* handler);
462
463 /**
464 Blurs the image in both horizontal and vertical directions by the
465 specified pixel @a blurRadius. This should not be used when using
466 a single mask colour for transparency.
467
468 @see BlurHorizontal(), BlurVertical()
469 */
470 wxImage Blur(int blurRadius) const;
471
472 /**
473 Blurs the image in the horizontal direction only. This should not be used
474 when using a single mask colour for transparency.
475
476 @see Blur(), BlurVertical()
477 */
478 wxImage BlurHorizontal(int blurRadius) const;
479
480 /**
481 Blurs the image in the vertical direction only. This should not be used
482 when using a single mask colour for transparency.
483
484 @see Blur(), BlurHorizontal()
485 */
486 wxImage BlurVertical(int blurRadius) const;
487
488 /**
489 Returns @true if the current image handlers can read this file
490 */
491 static bool CanRead(const wxString& filename);
492
493 /**
494 Deletes all image handlers.
495 This function is called by wxWidgets on exit.
496 */
497 static void CleanUpHandlers();
498
499 /**
500 Computes the histogram of the image. @a histogram is a reference to
501 wxImageHistogram object. wxImageHistogram is a specialization of
502 wxHashMap "template" and is defined as follows:
503
504 @code
505 class WXDLLEXPORT wxImageHistogramEntry
506 {
507 public:
508 wxImageHistogramEntry() : index(0), value(0) {}
509 unsigned long index;
510 unsigned long value;
511 };
512
513 WX_DECLARE_EXPORTED_HASH_MAP(unsigned long, wxImageHistogramEntry,
514 wxIntegerHash, wxIntegerEqual,
515 wxImageHistogram);
516 @endcode
517
518 @return Returns number of colours in the histogram.
519 */
520 unsigned long ComputeHistogram(wxImageHistogram& histogram) const;
521
522 /**
523 If the image has alpha channel, this method converts it to mask.
524
525 All pixels with alpha value less than @a threshold are replaced with mask
526 colour and the alpha channel is removed. Mask colour is chosen automatically
527 using FindFirstUnusedColour().
528
529 If the image image doesn't have alpha channel, ConvertAlphaToMask() does nothing.
530
531 @return @false if FindFirstUnusedColour returns @false, @true otherwise.
532 */
533 bool ConvertAlphaToMask(unsigned char threshold = wxIMAGE_ALPHA_THRESHOLD);
534
535 /**
536 @deprecated
537 Use the equivalent @ref wxBitmap::wxBitmap "wxBitmap constructor"
538 (which takes wxImage and depth as its arguments) instead.
539 */
540 wxBitmap ConvertToBitmap() const;
541
542 /**
543 Returns a greyscale version of the image.
544
545 The returned image uses the luminance component of the original to
546 calculate the greyscale. Defaults to using the standard ITU-T BT.601
547 when converting to YUV, where every pixel equals
548 (R * @a lr) + (G * @a lg) + (B * @a lb).
549 */
550 wxImage ConvertToGreyscale(double lr = 0.299, double lg = 0.587, double lb = 1.114) const;
551
552 /**
553 Returns monochromatic version of the image.
554
555 The returned image has white colour where the original has @e (r,g,b)
556 colour and black colour everywhere else.
557 */
558 wxImage ConvertToMono(unsigned char r, unsigned char g, unsigned char b) const;
559
560 /**
561 Returns an identical copy of the image.
562 */
563 wxImage Copy() const;
564
565 /**
566 Creates a fresh image.
567
568 If @a clear is @true, the new image will be initialized to black.
569 Otherwise, the image data will be uninitialized.
570
571 @param width
572 The width of the image in pixels.
573 @param height
574 The height of the image in pixels.
575 @param clear
576 If @true, initialize the image data with zeros.
577
578 @return @true if the call succeeded, @false otherwise.
579 */
580 bool Create(int width, int height, bool clear = true);
581
582 /**
583 Destroys the image data.
584 */
585 void Destroy();
586
587 /**
588 Finds the first colour that is never used in the image.
589 The search begins at given initial colour and continues by increasing
590 R, G and B components (in this order) by 1 until an unused colour is
591 found or the colour space exhausted.
592
593 The parameters @a r, @a g, @a b are pointers to variables to save the colour.
594
595 The parameters @a startR, @a startG, @a startB define the initial values
596 of the colour.
597 The returned colour will have RGB values equal to or greater than these.
598
599 @return Returns @false if there is no unused colour left, @true on success.
600
601 @note
602 This method involves computing the histogram, which is a
603 computationally intensive operation.
604 */
605 bool FindFirstUnusedColour(unsigned char* r, unsigned char* g,
606 unsigned char* b, unsigned char startR = 1,
607 unsigned char startG = 0,
608 unsigned char startB = 0) const;
609
610 /**
611 Finds the handler with the given name.
612
613 @param name
614 The handler name.
615
616 @return A pointer to the handler if found, @NULL otherwise.
617
618 @see wxImageHandler
619 */
620 static wxImageHandler* FindHandler(const wxString& name);
621
622 /**
623 Finds the handler associated with the given extension and type.
624
625 @param extension
626 The file extension, such as "bmp".
627 @param imageType
628 The image type; one of the ::wxBitmapType values.
629
630 @return A pointer to the handler if found, @NULL otherwise.
631
632 @see wxImageHandler
633 */
634 static wxImageHandler* FindHandler(const wxString& extension,
635 wxBitmapType imageType);
636
637 /**
638 Finds the handler associated with the given image type.
639
640 @param imageType
641 The image type; one of the ::wxBitmapType values.
642
643 @return A pointer to the handler if found, @NULL otherwise.
644
645 @see wxImageHandler
646 */
647 static wxImageHandler* FindHandler(wxBitmapType imageType);
648
649 /**
650 Finds the handler associated with the given MIME type.
651
652 @param mimetype
653 MIME type.
654
655 @return A pointer to the handler if found, @NULL otherwise.
656
657 @see wxImageHandler
658 */
659 static wxImageHandler* FindHandlerMime(const wxString& mimetype);
660
661 /**
662 Return alpha value at given pixel location.
663 */
664 unsigned char GetAlpha(int x, int y) const;
665
666 /**
667 Returns pointer to the array storing the alpha values for this image.
668
669 This pointer is @NULL for the images without the alpha channel. If the image
670 does have it, this pointer may be used to directly manipulate the alpha values
671 which are stored as the RGB ones.
672 */
673 unsigned char* GetAlpha() const;
674
675 /**
676 Returns the blue intensity at the given coordinate.
677 */
678 unsigned char GetBlue(int x, int y) const;
679
680 /**
681 Returns the image data as an array.
682
683 This is most often used when doing direct image manipulation.
684 The return value points to an array of characters in RGBRGBRGB... format
685 in the top-to-bottom, left-to-right order, that is the first RGB triplet
686 corresponds to the pixel first pixel of the first row, the second one ---
687 to the second pixel of the first row and so on until the end of the first
688 row, with second row following after it and so on.
689
690 You should not delete the returned pointer nor pass it to SetData().
691 */
692 unsigned char* GetData() const;
693
694 /**
695 Returns the green intensity at the given coordinate.
696 */
697 unsigned char GetGreen(int x, int y) const;
698
699 /**
700 Returns the static list of image format handlers.
701
702 @see wxImageHandler
703 */
704 static wxList GetHandlers();
705
706 /**
707 Gets the height of the image in pixels.
708 */
709 int GetHeight() const;
710
711 //@{
712 /**
713 If the image file contains more than one image and the image handler is
714 capable of retrieving these individually, this function will return the
715 number of available images.
716
717 For the overload taking the parameter @a filename, that's the name
718 of the file to query.
719 For the overload taking the parameter @a stream, that's the ppened input
720 stream with image data. Currently, the stream must support seeking.
721
722 The parameter @a type may be one of the following values:
723 @li wxBITMAP_TYPE_BMP: Load a Windows bitmap file.
724 @li wxBITMAP_TYPE_GIF: Load a GIF bitmap file.
725 @li wxBITMAP_TYPE_JPEG: Load a JPEG bitmap file.
726 @li wxBITMAP_TYPE_PNG: Load a PNG bitmap file.
727 @li wxBITMAP_TYPE_PCX: Load a PCX bitmap file.
728 @li wxBITMAP_TYPE_PNM: Load a PNM bitmap file.
729 @li wxBITMAP_TYPE_TIF: Load a TIFF bitmap file.
730 @li wxBITMAP_TYPE_TGA: Load a TGA bitmap file.
731 @li wxBITMAP_TYPE_XPM: Load a XPM bitmap file.
732 @li wxBITMAP_TYPE_ICO: Load a Windows icon file (ICO).
733 @li wxBITMAP_TYPE_CUR: Load a Windows cursor file (CUR).
734 @li wxBITMAP_TYPE_ANI: Load a Windows animated cursor file (ANI).
735 @li wxBITMAP_TYPE_ANY: Will try to autodetect the format.
736
737 @return Number of available images. For most image handlers, this is 1
738 (exceptions are TIFF and ICO formats).
739 */
740 static int GetImageCount(const wxString& filename,
741 wxBitmapType type = wxBITMAP_TYPE_ANY);
742 static int GetImageCount(wxInputStream& stream,
743 wxBitmapType type = wxBITMAP_TYPE_ANY);
744 //@}
745
746 /**
747 Iterates all registered wxImageHandler objects, and returns a string containing
748 file extension masks suitable for passing to file open/save dialog boxes.
749
750 @return The format of the returned string is @c "(*.ext1;*.ext2)|*.ext1;*.ext2".
751 It is usually a good idea to prepend a description before passing
752 the result to the dialog.
753 Example:
754 @code
755 wxFileDialog FileDlg( this, "Choose Image", ::wxGetCwd(), "",
756 _("Image Files ") + wxImage::GetImageExtWildcard(),
757 wxFD_OPEN );
758 @endcode
759
760 @see wxImageHandler
761 */
762 static wxString GetImageExtWildcard();
763
764 /**
765 Gets the blue value of the mask colour.
766 */
767 unsigned char GetMaskBlue() const;
768
769 /**
770 Gets the green value of the mask colour.
771 */
772 unsigned char GetMaskGreen() const;
773
774 /**
775 Gets the red value of the mask colour.
776 */
777 unsigned char GetMaskRed() const;
778
779 /**
780 Gets a user-defined string-valued option.
781
782 Currently the only defined string option is
783 @li wxIMAGE_OPTION_FILENAME: The name of the file from which the image
784 was loaded.
785
786 @see SetOption(), GetOptionInt(), HasOption()
787
788 @param name
789 The name of the option, case-insensitive.
790 @return
791 The value of the option or an empty string if not found. Use
792 HasOption() if an empty string can be a valid option value.
793 */
794 wxString GetOption(const wxString& name) const;
795
796 /**
797 Gets a user-defined integer-valued option.
798
799 The function is case-insensitive to @a name.
800 If the given option is not present, the function returns 0.
801 Use HasOption() is 0 is a possibly valid value for the option.
802
803 Generic options:
804 @li wxIMAGE_OPTION_MAX_WIDTH and wxIMAGE_OPTION_MAX_HEIGHT: If either
805 of these options is specified, the loaded image will be scaled down
806 (preserving its aspect ratio) so that its width is less than the
807 max width given if it is not 0 @em and its height is less than the
808 max height given if it is not 0. This is typically used for loading
809 thumbnails and the advantage of using these options compared to
810 calling Rescale() after loading is that some handlers (only JPEG
811 one right now) support rescaling the image during loading which is
812 vastly more efficient than loading the entire huge image and
813 rescaling it later (if these options are not supported by the
814 handler, this is still what happens however). These options must be
815 set before calling LoadFile() to have any effect.
816
817 @li wxIMAGE_OPTION_QUALITY: JPEG quality used when saving. This is an
818 integer in 0..100 range with 0 meaning very poor and 100 excellent
819 (but very badly compressed). This option is currently ignored for
820 the other formats.
821
822 @li wxIMAGE_OPTION_RESOLUTIONUNIT: The value of this option determines
823 whether the resolution of the image is specified in centimetres or
824 inches, see wxImageResolution enum elements.
825
826 @li wxIMAGE_OPTION_RESOLUTION, wxIMAGE_OPTION_RESOLUTIONX and
827 wxIMAGE_OPTION_RESOLUTIONY: These options define the resolution of
828 the image in the units corresponding to wxIMAGE_OPTION_RESOLUTIONUNIT
829 options value. The first option can be set before saving the image
830 to set both horizontal and vertical resolution to the same value.
831 The X and Y options are set by the image handlers if they support
832 the image resolution (currently BMP, JPEG and TIFF handlers do) and
833 the image provides the resolution information and can be queried
834 after loading the image.
835
836 Options specific to wxPNGHandler:
837 @li wxIMAGE_OPTION_PNG_FORMAT: Format for saving a PNG file, see
838 wxImagePNGType for the supported values.
839 @li wxIMAGE_OPTION_PNG_BITDEPTH: Bit depth for every channel (R/G/B/A).
840
841 @see SetOption(), GetOption()
842
843 @param name
844 The name of the option, case-insensitive.
845 @return
846 The value of the option or 0 if not found. Use HasOption() if 0
847 can be a valid option value.
848 */
849 int GetOptionInt(const wxString& name) const;
850
851 /**
852 Get the current mask colour or find a suitable unused colour that could be
853 used as a mask colour. Returns @true if the image currently has a mask.
854 */
855 bool GetOrFindMaskColour(unsigned char* r, unsigned char* g,
856 unsigned char* b) const;
857
858 /**
859 Returns the palette associated with the image.
860 Currently the palette is only used when converting to wxBitmap under Windows.
861
862 Some of the wxImage handlers have been modified to set the palette if
863 one exists in the image file (usually 256 or less colour images in
864 GIF or PNG format).
865 */
866 const wxPalette& GetPalette() const;
867
868 /**
869 Returns the red intensity at the given coordinate.
870 */
871 unsigned char GetRed(int x, int y) const;
872
873 /**
874 Returns a sub image of the current one as long as the rect belongs entirely
875 to the image.
876 */
877 wxImage GetSubImage(const wxRect& rect) const;
878
879 /**
880 Gets the type of image found by LoadFile() or specified with SaveFile().
881
882 @since 2.9.0
883 */
884 wxBitmapType GetType() const;
885
886 /**
887 Gets the width of the image in pixels.
888
889 @see GetHeight()
890 */
891 int GetWidth() const;
892
893 /**
894 Converts a color in HSV color space to RGB color space.
895 */
896 static wxImage::RGBValue HSVtoRGB(const wxImage::HSVValue& hsv);
897
898 /**
899 Returns @true if this image has alpha channel, @false otherwise.
900
901 @see GetAlpha(), SetAlpha()
902 */
903 bool HasAlpha() const;
904
905 /**
906 Returns @true if there is a mask active, @false otherwise.
907 */
908 bool HasMask() const;
909
910 /**
911 Returns @true if the given option is present.
912 The function is case-insensitive to @a name.
913
914 @see SetOption(), GetOption(), GetOptionInt()
915 */
916 bool HasOption(const wxString& name) const;
917
918 /**
919 Initializes the image alpha channel data.
920
921 It is an error to call it if the image already has alpha data.
922 If it doesn't, alpha data will be by default initialized to all pixels
923 being fully opaque. But if the image has a mask colour, all mask pixels
924 will be completely transparent.
925 */
926 void InitAlpha();
927
928 /**
929 Internal use only. Adds standard image format handlers.
930 It only install wxBMPHandler for the time being, which is used by wxBitmap.
931
932 This function is called by wxWidgets on startup, and shouldn't be called by
933 the user.
934
935 @see wxImageHandler, wxInitAllImageHandlers(), wxQuantize
936 */
937 static void InitStandardHandlers();
938
939 /**
940 Adds a handler at the start of the static list of format handlers.
941
942 @param handler
943 A new image format handler object. There is usually only one instance
944 of a given handler class in an application session.
945
946 @see wxImageHandler
947 */
948 static void InsertHandler(wxImageHandler* handler);
949
950 /**
951 Returns @true if image data is present.
952 */
953 bool IsOk() const;
954
955 /**
956 Returns @true if the given pixel is transparent, i.e. either has the mask
957 colour if this image has a mask or if this image has alpha channel and alpha
958 value of this pixel is strictly less than @a threshold.
959 */
960 bool IsTransparent(int x, int y,
961 unsigned char threshold = wxIMAGE_ALPHA_THRESHOLD) const;
962
963 /**
964 Loads an image from an input stream.
965
966 @param stream
967 Opened input stream from which to load the image.
968 Currently, the stream must support seeking.
969 @param type
970 May be one of the following:
971 @li wxBITMAP_TYPE_BMP: Load a Windows bitmap file.
972 @li wxBITMAP_TYPE_GIF: Load a GIF bitmap file.
973 @li wxBITMAP_TYPE_JPEG: Load a JPEG bitmap file.
974 @li wxBITMAP_TYPE_PNG: Load a PNG bitmap file.
975 @li wxBITMAP_TYPE_PCX: Load a PCX bitmap file.
976 @li wxBITMAP_TYPE_PNM: Load a PNM bitmap file.
977 @li wxBITMAP_TYPE_TIF: Load a TIFF bitmap file.
978 @li wxBITMAP_TYPE_TGA: Load a TGA bitmap file.
979 @li wxBITMAP_TYPE_XPM: Load a XPM bitmap file.
980 @li wxBITMAP_TYPE_ICO: Load a Windows icon file (ICO).
981 @li wxBITMAP_TYPE_CUR: Load a Windows cursor file (CUR).
982 @li wxBITMAP_TYPE_ANI: Load a Windows animated cursor file (ANI).
983 @li wxBITMAP_TYPE_ANY: Will try to autodetect the format.
984 @param index
985 Index of the image to load in the case that the image file contains
986 multiple images. This is only used by GIF, ICO and TIFF handlers.
987 The default value (-1) means "choose the default image" and is
988 interpreted as the first image (index=0) by the GIF and TIFF handler
989 and as the largest and most colourful one by the ICO handler.
990
991 @return @true if the operation succeeded, @false otherwise.
992 If the optional index parameter is out of range, @false is
993 returned and a call to wxLogError() takes place.
994
995 @remarks Depending on how wxWidgets has been configured, not all formats
996 may be available.
997
998 @note
999 You can use GetOptionInt() to get the hotspot when loading cursor files:
1000 @code
1001 int hotspot_x = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
1002 int hotspot_y = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
1003 @endcode
1004
1005 @see SaveFile()
1006 */
1007 virtual bool LoadFile(wxInputStream& stream,
1008 wxBitmapType type = wxBITMAP_TYPE_ANY,
1009 int index = -1);
1010
1011 /**
1012 Loads an image from a file.
1013 If no handler type is provided, the library will try to autodetect the format.
1014
1015 @param name
1016 Name of the file from which to load the image.
1017 @param type
1018 See the description in the LoadFile(wxInputStream&, wxBitmapType, int) overload.
1019 @param index
1020 See the description in the LoadFile(wxInputStream&, wxBitmapType, int) overload.
1021 */
1022 virtual bool LoadFile(const wxString& name,
1023 wxBitmapType type = wxBITMAP_TYPE_ANY,
1024 int index = -1);
1025
1026 /**
1027 Loads an image from a file.
1028 If no handler type is provided, the library will try to autodetect the format.
1029
1030 @param name
1031 Name of the file from which to load the image.
1032 @param mimetype
1033 MIME type string (for example 'image/jpeg')
1034 @param index
1035 See the description in the LoadFile(wxInputStream&, wxBitmapType, int) overload.
1036 */
1037 virtual bool LoadFile(const wxString& name, const wxString& mimetype,
1038 int index = -1);
1039
1040
1041 /**
1042 Loads an image from an input stream.
1043
1044 @param stream
1045 Opened input stream from which to load the image.
1046 Currently, the stream must support seeking.
1047 @param mimetype
1048 MIME type string (for example 'image/jpeg')
1049 @param index
1050 See the description in the LoadFile(wxInputStream&, wxBitmapType, int) overload.
1051 */
1052 virtual bool LoadFile(wxInputStream& stream, const wxString& mimetype,
1053 int index = -1);
1054
1055 /**
1056 Returns a mirrored copy of the image.
1057 The parameter @a horizontally indicates the orientation.
1058 */
1059 wxImage Mirror(bool horizontally = true) const;
1060
1061 /**
1062 Copy the data of the given @a image to the specified position in this image.
1063 */
1064 void Paste(const wxImage& image, int x, int y);
1065
1066 /**
1067 Converts a color in RGB color space to HSV color space.
1068 */
1069 static wxImage::HSVValue RGBtoHSV(const wxImage::RGBValue& rgb);
1070
1071 /**
1072 Finds the handler with the given name, and removes it.
1073 The handler is not deleted.
1074
1075 @param name
1076 The handler name.
1077
1078 @return @true if the handler was found and removed, @false otherwise.
1079
1080 @see wxImageHandler
1081 */
1082 static bool RemoveHandler(const wxString& name);
1083
1084 /**
1085 Replaces the colour specified by @e r1,g1,b1 by the colour @e r2,g2,b2.
1086 */
1087 void Replace(unsigned char r1, unsigned char g1,
1088 unsigned char b1, unsigned char r2,
1089 unsigned char g2, unsigned char b2);
1090
1091 /**
1092 Changes the size of the image in-place by scaling it: after a call to this
1093 function,the image will have the given width and height.
1094
1095 For a description of the @a quality parameter, see the Scale() function.
1096 Returns the (modified) image itself.
1097
1098 @see Scale()
1099 */
1100 wxImage& Rescale(int width, int height,
1101 int quality = wxIMAGE_QUALITY_NORMAL);
1102
1103 /**
1104 Changes the size of the image in-place without scaling it by adding either a
1105 border with the given colour or cropping as necessary.
1106
1107 The image is pasted into a new image with the given @a size and background
1108 colour at the position @a pos relative to the upper left of the new image.
1109
1110 If @a red = green = blue = -1 then use either the current mask colour
1111 if set or find, use, and set a suitable mask colour for any newly exposed
1112 areas.
1113
1114 @return The (modified) image itself.
1115
1116 @see Size()
1117 */
1118 wxImage& Resize(const wxSize& size, const wxPoint& pos, int red = -1,
1119 int green = -1, int blue = -1);
1120
1121 /**
1122 Rotates the image about the given point, by @a angle radians.
1123
1124 Passing @true to @a interpolating results in better image quality, but is slower.
1125
1126 If the image has a mask, then the mask colour is used for the uncovered
1127 pixels in the rotated image background. Else, black (rgb 0, 0, 0) will be used.
1128
1129 Returns the rotated image, leaving this image intact.
1130 */
1131 wxImage Rotate(double angle, const wxPoint& rotationCentre,
1132 bool interpolating = true,
1133 wxPoint* offsetAfterRotation = NULL) const;
1134
1135 /**
1136 Returns a copy of the image rotated 90 degrees in the direction
1137 indicated by @a clockwise.
1138 */
1139 wxImage Rotate90(bool clockwise = true) const;
1140
1141 /**
1142 Rotates the hue of each pixel in the image by @e angle, which is a double in
1143 the range of -1.0 to +1.0, where -1.0 corresponds to -360 degrees and +1.0
1144 corresponds to +360 degrees.
1145 */
1146 void RotateHue(double angle);
1147
1148 /**
1149 Saves an image in the given stream.
1150
1151 @param stream
1152 Opened output stream to save the image to.
1153 @param mimetype
1154 MIME type.
1155
1156 @return @true if the operation succeeded, @false otherwise.
1157
1158 @remarks Depending on how wxWidgets has been configured, not all formats
1159 may be available.
1160
1161 @note
1162 You can use SetOption() to set the hotspot when saving an image
1163 into a cursor file (default hotspot is in the centre of the image):
1164 @code
1165 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, hotspotX);
1166 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, hotspotY);
1167 @endcode
1168
1169 @see LoadFile()
1170 */
1171 virtual bool SaveFile(wxOutputStream& stream,
1172 const wxString& mimetype) const;
1173
1174 /**
1175 Saves an image in the named file.
1176
1177 @param name
1178 Name of the file to save the image to.
1179 @param type
1180 Currently these types can be used:
1181 @li wxBITMAP_TYPE_BMP: Save a BMP image file.
1182 @li wxBITMAP_TYPE_JPEG: Save a JPEG image file.
1183 @li wxBITMAP_TYPE_PNG: Save a PNG image file.
1184 @li wxBITMAP_TYPE_PCX: Save a PCX image file
1185 (tries to save as 8-bit if possible, falls back to 24-bit otherwise).
1186 @li wxBITMAP_TYPE_PNM: Save a PNM image file (as raw RGB always).
1187 @li wxBITMAP_TYPE_TIFF: Save a TIFF image file.
1188 @li wxBITMAP_TYPE_XPM: Save a XPM image file.
1189 @li wxBITMAP_TYPE_ICO: Save a Windows icon file (ICO).
1190 The size may be up to 255 wide by 127 high. A single image is saved
1191 in 8 colors at the size supplied.
1192 @li wxBITMAP_TYPE_CUR: Save a Windows cursor file (CUR).
1193 */
1194 virtual bool SaveFile(const wxString& name, wxBitmapType type) const;
1195
1196 /**
1197 Saves an image in the named file.
1198
1199 @param name
1200 Name of the file to save the image to.
1201 @param mimetype
1202 MIME type.
1203 */
1204 virtual bool SaveFile(const wxString& name, const wxString& mimetype) const;
1205
1206 /**
1207 Saves an image in the named file.
1208
1209 File type is determined from the extension of the file name.
1210 Note that this function may fail if the extension is not recognized!
1211 You can use one of the forms above to save images to files with
1212 non-standard extensions.
1213
1214 @param name
1215 Name of the file to save the image to.
1216 */
1217 virtual bool SaveFile(const wxString& name) const;
1218
1219 /**
1220 Saves an image in the given stream.
1221
1222 @param stream
1223 Opened output stream to save the image to.
1224 @param type
1225 MIME type.
1226 */
1227 virtual bool SaveFile(wxOutputStream& stream, wxBitmapType type) const;
1228
1229 /**
1230 Returns a scaled version of the image.
1231
1232 This is also useful for scaling bitmaps in general as the only other way
1233 to scale bitmaps is to blit a wxMemoryDC into another wxMemoryDC.
1234
1235 The parameter @a quality determines what method to use for resampling the image.
1236 Can be one of the following:
1237 - wxIMAGE_QUALITY_NORMAL: Uses the normal default scaling method of pixel
1238 replication
1239 - wxIMAGE_QUALITY_HIGH: Uses bicubic and box averaging resampling methods
1240 for upsampling and downsampling respectively
1241
1242 It should be noted that although using @c wxIMAGE_QUALITY_HIGH produces much nicer
1243 looking results it is a slower method. Downsampling will use the box averaging
1244 method which seems to operate very fast. If you are upsampling larger images using
1245 this method you will most likely notice that it is a bit slower and in extreme
1246 cases it will be quite substantially slower as the bicubic algorithm has to process a
1247 lot of data.
1248
1249 It should also be noted that the high quality scaling may not work as expected
1250 when using a single mask colour for transparency, as the scaling will blur the
1251 image and will therefore remove the mask partially. Using the alpha channel
1252 will work.
1253
1254 Example:
1255 @code
1256 // get the bitmap from somewhere
1257 wxBitmap bmp = ...;
1258
1259 // rescale it to have size of 32*32
1260 if ( bmp.GetWidth() != 32 || bmp.GetHeight() != 32 )
1261 {
1262 wxImage image = bmp.ConvertToImage();
1263 bmp = wxBitmap(image.Scale(32, 32));
1264
1265 // another possibility:
1266 image.Rescale(32, 32);
1267 bmp = image;
1268 }
1269 @endcode
1270
1271 @see Rescale()
1272 */
1273 wxImage Scale(int width, int height,
1274 int quality = wxIMAGE_QUALITY_NORMAL) const;
1275
1276 /**
1277 This function is similar to SetData() and has similar restrictions.
1278
1279 The pointer passed to it may however be @NULL in which case the function
1280 will allocate the alpha array internally -- this is useful to add alpha
1281 channel data to an image which doesn't have any.
1282
1283 If the pointer is not @NULL, it must have one byte for each image pixel
1284 and be allocated with malloc().
1285 wxImage takes ownership of the pointer and will free it unless @a static_data
1286 parameter is set to @true -- in this case the caller should do it.
1287 */
1288 void SetAlpha(unsigned char* alpha = NULL,
1289 bool static_data = false);
1290
1291 /**
1292 Sets the alpha value for the given pixel.
1293 This function should only be called if the image has alpha channel data,
1294 use HasAlpha() to check for this.
1295 */
1296 void SetAlpha(int x, int y, unsigned char alpha);
1297
1298 //@{
1299 /**
1300 Sets the image data without performing checks.
1301
1302 The data given must have the size (width*height*3) or results will be
1303 unexpected. Don't use this method if you aren't sure you know what you
1304 are doing.
1305
1306 The data must have been allocated with @c malloc(), @b NOT with
1307 @c operator new.
1308
1309 After this call the pointer to the data is owned by the wxImage object,
1310 that will be responsible for deleting it.
1311 Do not pass to this function a pointer obtained through GetData().
1312 */
1313 void SetData(unsigned char* data, bool static_data = false);
1314 void SetData(unsigned char* data, int new_width, int new_height,
1315 bool static_data = false);
1316 //@}
1317
1318 /**
1319 Specifies whether there is a mask or not.
1320
1321 The area of the mask is determined by the current mask colour.
1322 */
1323 void SetMask(bool hasMask = true);
1324
1325 /**
1326 Sets the mask colour for this image (and tells the image to use the mask).
1327 */
1328 void SetMaskColour(unsigned char red, unsigned char green,
1329 unsigned char blue);
1330
1331 /**
1332 Sets image's mask so that the pixels that have RGB value of mr,mg,mb in
1333 mask will be masked in the image.
1334
1335 This is done by first finding an unused colour in the image, setting
1336 this colour as the mask colour and then using this colour to draw all
1337 pixels in the image who corresponding pixel in mask has given RGB value.
1338
1339 The parameter @a mask is the mask image to extract mask shape from.
1340 It must have the same dimensions as the image.
1341
1342 The parameters @a mr, @a mg, @a mb are the RGB values of the pixels in
1343 mask that will be used to create the mask.
1344
1345 @return Returns @false if mask does not have same dimensions as the image
1346 or if there is no unused colour left. Returns @true if the mask
1347 was successfully applied.
1348
1349 @note
1350 Note that this method involves computing the histogram, which is a
1351 computationally intensive operation.
1352 */
1353 bool SetMaskFromImage(const wxImage& mask, unsigned char mr,
1354 unsigned char mg,
1355 unsigned char mb);
1356
1357 //@{
1358 /**
1359 Sets a user-defined option. The function is case-insensitive to @a name.
1360
1361 For example, when saving as a JPEG file, the option @b quality is
1362 used, which is a number between 0 and 100 (0 is terrible, 100 is very good).
1363
1364 @see GetOption(), GetOptionInt(), HasOption()
1365 */
1366 void SetOption(const wxString& name, const wxString& value);
1367 void SetOption(const wxString& name, int value);
1368 //@}
1369
1370 /**
1371 Associates a palette with the image.
1372
1373 The palette may be used when converting wxImage to wxBitmap (MSW only at present)
1374 or in file save operations (none as yet).
1375 */
1376 void SetPalette(const wxPalette& palette);
1377
1378 /**
1379 Sets the colour of the pixels within the given rectangle.
1380
1381 This routine performs bounds-checks for the coordinate so it can be considered
1382 a safe way to manipulate the data.
1383 */
1384 void SetRGB(const wxRect& rect,
1385 unsigned char red,
1386 unsigned char green,
1387 unsigned char blue);
1388
1389 /**
1390 Set the type of image returned by GetType().
1391
1392 This method is mostly used internally by the library but can also be
1393 called from the user code if the image was created from data in the
1394 given bitmap format without using LoadFile() (which would set the type
1395 correctly automatically).
1396
1397 Notice that the image must be created before this function is called.
1398
1399 @since 2.9.0
1400
1401 @param type
1402 One of bitmap type constants, @c wxBITMAP_TYPE_INVALID is a valid
1403 value for it and can be used to reset the bitmap type to default
1404 but @c wxBITMAP_TYPE_MAX is not allowed here.
1405 */
1406 void SetType(wxBitmapType type);
1407
1408 /**
1409 Returns a resized version of this image without scaling it by adding either a
1410 border with the given colour or cropping as necessary.
1411
1412 The image is pasted into a new image with the given @a size and background
1413 colour at the position @a pos relative to the upper left of the new image.
1414
1415 If @a red = green = blue = -1 then the areas of the larger image not covered
1416 by this image are made transparent by filling them with the image mask colour
1417 (which will be allocated automatically if it isn't currently set).
1418
1419 Otherwise, the areas will be filled with the colour with the specified RGB components.
1420
1421 @see Resize()
1422 */
1423 wxImage Size(const wxSize& size, const wxPoint& pos, int red = -1,
1424 int green = -1, int blue = -1) const;
1425
1426 /**
1427 Assignment operator, using @ref overview_refcount "reference counting".
1428
1429 @param image
1430 Image to assign.
1431
1432 @return Returns 'this' object.
1433 */
1434 wxImage& operator=(const wxImage& image);
1435 };
1436
1437 // ============================================================================
1438 // Global functions/macros
1439 // ============================================================================
1440
1441 /** @ingroup group_funcmacro_appinitterm */
1442 //@{
1443
1444 /**
1445 Initializes all available image handlers. For a list of available handlers,
1446 see wxImage.
1447
1448 @see wxImage, wxImageHandler
1449
1450 @header{wx/image.h}
1451 */
1452 void wxInitAllImageHandlers();
1453
1454 //@}
1455