1 {
2 $Id: ImagingClasses.pas 173 2009-09-04 17:05:52Z galfar $
3 Vampyre Imaging Library
4 by Marek Mauder
5 http://imaginglib.sourceforge.net
7 The contents of this file are used with permission, subject to the Mozilla
8 Public License Version 1.1 (the "License"); you may not use this file except
9 in compliance with the License. You may obtain a copy of the License at
10 http://www.mozilla.org/MPL/MPL-1.1.html
12 Software distributed under the License is distributed on an "AS IS" basis,
13 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14 the specific language governing rights and limitations under the License.
16 Alternatively, the contents of this file may be used under the terms of the
17 GNU Lesser General Public License (the "LGPL License"), in which case the
18 provisions of the LGPL License are applicable instead of those above.
19 If you wish to allow use of your version of this file only under the terms
20 of the LGPL License and not to allow others to use your version of this file
21 under the MPL, indicate your decision by deleting the provisions above and
22 replace them with the notice and other provisions required by the LGPL
23 License. If you do not delete the provisions above, a recipient may use
24 your version of this file under either the MPL or the LGPL License.
26 For more information about the LGPL: http://www.gnu.org/copyleft/lesser.html
27 }
29 { This unit contains class based wrapper to Imaging library.}
32 {$I ImagingOptions.inc}
34 interface
36 uses
39 type
40 { Base abstract high level class wrapper to low level Imaging structures and
41 functions.}
43 protected
65 published
66 public
70 { Returns info about current image.}
73 { Creates a new image data with the given size and format. Old image
74 data is lost. Works only for the current image of TMultiImage.}
76 { Resizes current image with optional resampling.}
78 { Flips current image. Reverses the image along its horizontal axis the top
79 becomes the bottom and vice versa.}
81 { Mirrors current image. Reverses the image along its vertical axis the left
82 side becomes the right and vice versa.}
84 { Rotates image by Angle degrees counterclockwise.}
86 { Copies rectangular part of SrcImage to DstImage. No blending is performed -
87 alpha is simply copied to destination image. Operates also with
88 negative X and Y coordinates.
89 Note that copying is fastest for images in the same data format
90 (and slowest for images in special formats).}
92 { Stretches the contents of the source rectangle to the destination rectangle
93 with optional resampling. No blending is performed - alpha is
94 simply copied/resampled to destination image. Note that stretching is
95 fastest for images in the same data format (and slowest for
96 images in special formats).}
97 procedure StretchTo(SrcX, SrcY, SrcWidth, SrcHeight: LongInt; DstImage: TBaseImage; DstX, DstY, DstWidth, DstHeight: LongInt; Filter: TResizeFilter);
98 { Replaces pixels with OldPixel in the given rectangle by NewPixel.
99 OldPixel and NewPixel should point to the pixels in the same format
100 as the given image is in.}
102 { Swaps SrcChannel and DstChannel color or alpha channels of image.
103 Use ChannelRed, ChannelBlue, ChannelGreen, ChannelAlpha constants to
104 identify channels.}
107 { Loads current image data from file.}
109 { Loads current image data from stream.}
112 { Saves current image data to file.}
114 { Saves current image data to stream. Ext identifies desired image file
115 format (jpg, png, dds, ...)}
118 { Width of current image in pixels.}
120 { Height of current image in pixels.}
122 { Image data format of current image.}
124 { Size in bytes of current image's data.}
126 { Pointer to memory containing image bits.}
128 { Pointer to palette for indexed format images. It is nil for others.
129 Max palette entry is at index [PaletteEntries - 1].}
131 { Number of entries in image's palette}
133 { Provides indexed access to each line of pixels. Does not work with special
134 format images (like DXT).}
136 { Returns pointer to image pixel at [X, Y] coordinates.}
138 { Extended image format information.}
140 { This gives complete access to underlying TImageData record.
141 It can be used in functions that take TImageData as parameter
142 (for example: ReduceColors(SingleImageInstance.ImageData^, 64)).}
144 { Indicates whether the current image is valid (proper format,
145 allowed dimensions, right size, ...).}
147 {{ Specifies the bounding rectangle of the image.}
149 { This event occurs when the image data size has just changed. That means
150 image width, height, or format has been changed.}
152 { This event occurs when some pixels of the image have just changed.}
156 { Extension of TBaseImage which uses single TImageData record to
157 store image. All methods inherited from TBaseImage work with this record.}
159 protected
162 public
169 { Assigns single image from another single image or multi image.}
173 { Extension of TBaseImage which uses array of TImageData records to
174 store multiple images. Images are independent on each other and they don't
175 share any common characteristic. Each can have different size, format, and
176 palette. All methods inherited from TBaseImage work only with
177 active image (it could represent mipmap level, animation frame, or whatever).
178 Methods whose names contain word 'Multi' work with all images in array
179 (as well as other methods with obvious names).}
181 protected
194 public
201 { Assigns multi image from another multi image or single image.}
204 { Adds new image at the end of the image array. }
206 { Adds existing image at the end of the image array. }
208 { Adds existing image (Active image of a TmultiImage)
209 at the end of the image array. }
211 { Adds existing image array ((all images of a multi image))
212 at the end of the image array. }
214 { Adds existing MultiImage images at the end of the image array. }
217 { Inserts new image image at the given position in the image array. }
218 procedure InsertImage(Index, AWidth, AHeight: LongInt; AFormat: TImageFormat = ifDefault); overload;
219 { Inserts existing image at the given position in the image array. }
221 { Inserts existing image (Active image of a TmultiImage)
222 at the given position in the image array. }
224 { Inserts existing image at the given position in the image array. }
226 { Inserts existing images (all images of a TmultiImage) at
227 the given position in the image array. }
230 { Exchanges two images at the given positions in the image array. }
232 { Deletes image at the given position in the image array.}
234 { Rearranges images so that the first image will become last and vice versa.}
237 { Converts all images to another image data format.}
239 { Resizes all images.}
242 { Overloaded loading method that will add new image to multiimage if
243 image array is empty bero loading. }
245 { Overloaded loading method that will add new image to multiimage if
246 image array is empty bero loading. }
249 { Loads whole multi image from file.}
251 { Loads whole multi image from stream.}
253 { Saves whole multi image to file.}
255 { Saves whole multi image to stream. Ext identifies desired
256 image file format (jpg, png, dds, ...).}
259 { Indicates active image of this multi image. All methods inherited
260 from TBaseImage operate on this image only.}
262 { Number of images of this multi image.}
264 { This value is True if all images of this TMultiImage are valid.}
266 { This gives complete access to underlying TDynImageDataArray.
267 It can be used in functions that take TDynImageDataArray
268 as parameter.}
270 { Array property for accessing individual images of TMultiImage. When you
271 set image at given index the old image is freed and the source is cloned.}
275 implementation
277 const
283 begin
288 { TBaseImage class implementation }
291 begin
292 SetPointer;
296 begin
297 Create;
302 begin
307 begin
310 else
315 begin
318 else
323 begin
326 else
331 var
333 begin
335 begin
339 else
341 end
342 else
347 begin
350 else
355 begin
358 else
363 begin
366 else
371 begin
374 else
379 begin
384 begin
387 else
392 begin
397 begin
402 begin
407 begin
412 begin
414 DoDataSizeChanged;
418 begin
421 DoPixelsChanged;
425 begin
431 begin
433 DoDataSizeChanged;
437 begin
439 DoDataSizeChanged;
443 begin
445 DoPixelsChanged;
449 begin
451 DoPixelsChanged;
455 begin
457 DoPixelsChanged;
462 begin
464 begin
472 begin
474 begin
483 begin
485 begin
487 DoPixelsChanged;
492 begin
494 begin
496 DoPixelsChanged;
501 begin
506 begin
508 DoDataSizeChanged;
512 begin
514 DoDataSizeChanged;
518 begin
524 begin
530 { TSingleImage class implementation }
533 begin
539 begin
545 begin
548 begin
550 DoDataSizeChanged;
551 end
552 else
553 Create;
557 begin
563 begin
569 begin
575 begin
580 begin
582 begin
583 Create;
584 end
586 begin
588 end
590 begin
593 else
595 end
596 else
601 { TMultiImage class implementation }
604 begin
611 var
613 begin
622 var
624 begin
628 begin
629 // Clone only valid images
632 else
639 begin
644 begin
649 begin
655 begin
657 SetPointer;
661 begin
666 var
668 begin
670 begin
671 // Create new empty images if array will be enlarged
676 end
677 else
678 begin
679 // Free images that exceed desired count and shrink array
684 SetPointer;
688 begin
693 begin
699 begin
705 begin
707 begin
710 end
711 else
712 begin
719 var
721 begin
722 // Inserting to empty image will add image at index 0
727 begin
730 begin
731 // Move imges to new position
734 // Null old images, not free them!
739 end
740 else
745 var
747 begin
750 begin
758 begin
764 var
766 begin
768 begin
769 Create;
770 end
772 begin
775 end
777 begin
782 end
783 else
788 begin
793 begin
798 begin
804 begin
809 begin
815 begin
820 begin
825 begin
832 begin
837 begin
842 var
844 begin
847 begin
855 var
857 begin
859 begin
860 // Free image at index to be deleted
863 begin
864 // Move images to new indices if necessary
868 // Set new array length and update pointer to active image
870 SetPointer;
875 var
877 begin
884 var
886 begin
892 var
894 begin
900 begin
907 begin
914 begin
920 begin
926 begin
931 begin
935 {
936 File Notes:
938 -- TODOS ----------------------------------------------------
939 - nothing now
940 - add SetPalette, create some pal wrapper first
941 - put all low level stuff here like ReplaceColor etc, change
942 CopyTo to Copy, and add overload Copy(SrcRect, DstX, DstY) ...
944 -- 0.24.3 Changes/Bug Fixes ---------------------------------
945 - Added TMultiImage.ReverseImages method.
947 -- 0.23 Changes/Bug Fixes -----------------------------------
948 - Added SwapChannels method to TBaseImage.
949 - Added ReplaceColor method to TBaseImage.
950 - Added ToString method to TBaseImage.
952 -- 0.21 Changes/Bug Fixes -----------------------------------
953 - Inserting images to empty MultiImage will act as Add method.
954 - MultiImages with empty arrays will now create one image when
955 LoadFromFile or LoadFromStream is called.
956 - Fixed bug that caused AVs when getting props like Width, Height, asn Size
957 and when inlining was off. There was call to Iff but with inlining disabled
958 params like FPData.Size were evaluated and when FPData was nil => AV.
959 - Added many FPData validity checks to many methods. There were AVs
960 when calling most methods on empty TMultiImage.
961 - Added AllImagesValid property to TMultiImage.
962 - Fixed memory leak in TMultiImage.CreateFromParams.
964 -- 0.19 Changes/Bug Fixes -----------------------------------
965 - added ResizeImages method to TMultiImage
966 - removed Ext parameter from various LoadFromStream methods, no
967 longer needed
968 - fixed various issues concerning ActiveImage of TMultiImage
969 (it pointed to invalid location after some operations)
970 - most of property set/get methods are now inline
971 - added PixelPointers property to TBaseImage
972 - added Images default array property to TMultiImage
973 - renamed methods in TMultiImage to contain 'Image' instead of 'Level'
974 - added canvas support
975 - added OnDataSizeChanged and OnPixelsChanged event to TBaseImage
976 - renamed TSingleImage.NewImage to RecreateImageData, made public, and
977 moved to TBaseImage
979 -- 0.17 Changes/Bug Fixes -----------------------------------
980 - added props PaletteEntries and ScanLine to TBaseImage
981 - aded new constructor to TBaseImage that take TBaseImage source
982 - TMultiImage levels adding and inserting rewritten internally
983 - added some new functions to TMultiImage: AddLevels, InsertLevels
984 - added some new functions to TBaseImage: Flip, Mirror, Rotate,
985 CopyRect, StretchRect
986 - TBasicImage.Resize has now filter parameter
987 - new stuff added to TMultiImage (DataArray prop, ConvertLevels)
989 -- 0.13 Changes/Bug Fixes -----------------------------------
990 - added AddLevel, InsertLevel, ExchangeLevels and DeleteLevel
991 methods to TMultiImage
992 - added TBaseImage, TSingleImage and TMultiImage with initial
993 members
994 }