1 {
2 Vampyre Imaging Library
3 by Marek Mauder
4 http://imaginglib.sourceforge.net
6 The contents of this file are used with permission, subject to the Mozilla
7 Public License Version 1.1 (the "License"); you may not use this file except
8 in compliance with the License. You may obtain a copy of the License at
9 http://www.mozilla.org/MPL/MPL-1.1.html
11 Software distributed under the License is distributed on an "AS IS" basis,
12 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
13 the specific language governing rights and limitations under the License.
15 Alternatively, the contents of this file may be used under the terms of the
16 GNU Lesser General Public License (the "LGPL License"), in which case the
17 provisions of the LGPL License are applicable instead of those above.
18 If you wish to allow use of your version of this file only under the terms
19 of the LGPL License and not to allow others to use your version of this file
20 under the MPL, indicate your decision by deleting the provisions above and
21 replace them with the notice and other provisions required by the LGPL
22 License. If you do not delete the provisions above, a recipient may use
23 your version of this file under either the MPL or the LGPL License.
25 For more information about the LGPL: http://www.gnu.org/copyleft/lesser.html
26 }
28 { This unit contains class based wrapper to Imaging library.}
31 {$I ImagingOptions.inc}
33 interface
35 uses
38 type
39 { Base abstract high level class wrapper to low level Imaging structures and
40 functions.}
42 private
44 protected
67 public
71 { Returns info about current image.}
74 { Creates a new image data with the given size and format. Old image
75 data is lost. Works only for the current image of TMultiImage.}
77 { Maps underlying image data to given TImageData record. Both TBaseImage and
78 TImageData now share some image memory (bits). So don't call FreeImage
79 on TImageData afterwards since this TBaseImage would get really broken.}
81 { Deletes current image.}
84 { Resizes current image with optional resampling.}
87 procedure ResizeToFit(FitWidth, FitHeight: Integer; Filter: TResizeFilter; DstImage: TBaseImage);
88 { Flips current image. Reverses the image along its horizontal axis the top
89 becomes the bottom and vice versa.}
91 { Mirrors current image. Reverses the image along its vertical axis the left
92 side becomes the right and vice versa.}
94 { Rotates image by Angle degrees counterclockwise.}
96 { Copies rectangular part of SrcImage to DstImage. No blending is performed -
97 alpha is simply copied to destination image. Operates also with
98 negative X and Y coordinates.
99 Note that copying is fastest for images in the same data format
100 (and slowest for images in special formats).}
101 procedure CopyTo(SrcX, SrcY, Width, Height: Integer; DstImage: TBaseImage; DstX, DstY: Integer);
102 { Stretches the contents of the source rectangle to the destination rectangle
103 with optional resampling. No blending is performed - alpha is
104 simply copied/resampled to destination image. Note that stretching is
105 fastest for images in the same data format (and slowest for
106 images in special formats).}
107 procedure StretchTo(SrcX, SrcY, SrcWidth, SrcHeight: Integer; DstImage: TBaseImage; DstX, DstY, DstWidth, DstHeight: Integer; Filter: TResizeFilter);
108 { Replaces pixels with OldPixel in the given rectangle by NewPixel.
109 OldPixel and NewPixel should point to the pixels in the same format
110 as the given image is in.}
112 { Swaps SrcChannel and DstChannel color or alpha channels of image.
113 Use ChannelRed, ChannelBlue, ChannelGreen, ChannelAlpha constants to
114 identify channels.}
117 { Loads current image data from file.}
119 { Loads current image data from stream.}
122 { Saves current image data to file.}
124 { Saves current image data to stream. Ext identifies desired image file
125 format (jpg, png, dds, ...)}
128 { Width of current image in pixels.}
130 { Height of current image in pixels.}
132 { Image data format of current image.}
134 { Size in bytes of current image's data.}
136 { Pointer to memory containing image bits.}
138 { Pointer to palette for indexed format images. It is nil for others.
139 Max palette entry is at index [PaletteEntries - 1].}
141 { Number of entries in image's palette}
143 { Provides indexed access to each line of pixels. Does not work with special
144 format images (like DXT).}
146 { Returns pointer to image pixel at [X, Y] coordinates.}
148 { Size/length of one image scanline in bytes.}
150 { Extended image format information.}
152 { This gives complete access to underlying TImageData record.
153 It can be used in functions that take TImageData as parameter
154 (for example: ReduceColors(SingleImageInstance.ImageData^, 64)).}
156 { Indicates whether the current image is valid (proper format,
157 allowed dimensions, right size, ...).}
159 { Indicates whether image containst any data (size in bytes > 0).}
161 { Specifies the bounding rectangle of the image.}
163 { This event occurs when the image data size has just changed. That means
164 image width, height, or format has been changed.}
166 { This event occurs when some pixels of the image have just changed.}
170 { Extension of TBaseImage which uses single TImageData record to
171 store image. All methods inherited from TBaseImage work with this record.}
173 protected
176 public
183 { Assigns single image from another single image or multi image.}
185 { Assigns single image from image data record.}
189 { Extension of TBaseImage which uses array of TImageData records to
190 store multiple images. Images are independent on each other and they don't
191 share any common characteristic. Each can have different size, format, and
192 palette. All methods inherited from TBaseImage work only with
193 active image (it could represent mipmap level, animation frame, or whatever).
194 Methods whose names contain word 'Multi' work with all images in array
195 (as well as other methods with obvious names).}
197 protected
210 public
212 constructor CreateFromParams(AWidth, AHeight: Integer; AFormat: TImageFormat; ImageCount: Integer);
217 { Assigns multi image from another multi image or single image.}
219 { Assigns multi image from array of image data records.}
222 { Adds new image at the end of the image array. }
223 function AddImage(AWidth, AHeight: Integer; AFormat: TImageFormat = ifDefault): Integer; overload;
224 { Adds existing image at the end of the image array. }
226 { Adds existing image (Active image of a TmultiImage)
227 at the end of the image array. }
229 { Adds existing image array ((all images of a multi image))
230 at the end of the image array. }
232 { Adds existing MultiImage images at the end of the image array. }
235 { Inserts new image image at the given position in the image array. }
236 procedure InsertImage(Index, AWidth, AHeight: Integer; AFormat: TImageFormat = ifDefault); overload;
237 { Inserts existing image at the given position in the image array. }
239 { Inserts existing image (Active image of a TmultiImage)
240 at the given position in the image array. }
242 { Inserts existing image at the given position in the image array. }
244 { Inserts existing images (all images of a TmultiImage) at
245 the given position in the image array. }
248 { Exchanges two images at the given positions in the image array. }
250 { Deletes image at the given position in the image array.}
252 { Rearranges images so that the first image will become last and vice versa.}
254 { Deletes all images.}
257 { Converts all images to another image data format.}
259 { Resizes all images.}
262 { Overloaded loading method that will add new image to multiimage if
263 image array is empty bero loading. }
265 { Overloaded loading method that will add new image to multiimage if
266 image array is empty bero loading. }
269 { Loads whole multi image from file.}
271 { Loads whole multi image from stream.}
273 { Saves whole multi image to file.}
275 { Saves whole multi image to stream. Ext identifies desired
276 image file format (jpg, png, dds, ...).}
279 { Indicates active image of this multi image. All methods inherited
280 from TBaseImage operate on this image only.}
282 { Number of images of this multi image.}
284 { This value is True if all images of this TMultiImage are valid.}
286 { This gives complete access to underlying TDynImageDataArray.
287 It can be used in functions that take TDynImageDataArray
288 as parameter.}
290 { Array property for accessing individual images of TMultiImage. When you
291 set image at given index the old image is freed and the source is cloned.}
295 implementation
297 const
302 begin
307 { TBaseImage class implementation }
310 begin
311 SetPointer;
315 begin
316 Create;
321 begin
326 begin
329 else
334 begin
337 else
342 begin
345 else
350 var
352 begin
354 begin
358 else
360 end
361 else
366 begin
369 else
374 begin
377 else
382 begin
385 else
390 begin
393 else
398 begin
401 else
406 begin
411 begin
414 else
419 begin
424 begin
429 begin
434 begin
439 begin
444 begin
446 DoDataSizeChanged;
450 begin
453 DoPixelsChanged;
457 begin
463 begin
465 DoDataSizeChanged;
469 begin
470 Clear;
480 begin
485 begin
487 DoDataSizeChanged;
492 begin
494 begin
502 begin
504 DoPixelsChanged;
508 begin
510 DoPixelsChanged;
514 begin
516 begin
518 DoPixelsChanged;
524 begin
526 begin
534 begin
536 begin
545 begin
547 begin
549 DoPixelsChanged;
554 begin
556 begin
558 DoPixelsChanged;
563 begin
568 begin
570 DoDataSizeChanged;
574 begin
576 DoDataSizeChanged;
580 begin
586 begin
592 { TSingleImage class implementation }
595 begin
597 Clear;
601 begin
607 begin
613 begin
619 begin
625 begin
631 begin
636 begin
638 begin
639 Clear;
640 end
642 begin
644 end
646 begin
649 else
650 Clear;
651 end
652 else
657 begin
659 begin
661 DoDataSizeChanged;
662 end
663 else
664 Clear;
667 { TMultiImage class implementation }
670 begin
676 var
678 begin
688 begin
693 begin
698 begin
703 begin
709 begin
711 SetPointer;
715 begin
720 var
722 begin
724 begin
725 // Create new empty images if array will be enlarged
730 end
731 else
732 begin
733 // Free images that exceed desired count and shrink array
738 SetPointer;
742 begin
747 begin
753 begin
759 begin
761 begin
764 end
765 else
766 begin
773 var
775 begin
776 // Inserting to empty image will add image at index 0
781 begin
784 begin
785 // Move imges to new position
788 // Null old images, not free them!
793 end
794 else
799 var
801 begin
804 begin
812 begin
818 var
820 begin
822 begin
823 ClearAll;
824 end
826 begin
829 end
831 begin
835 end
836 else
841 var
843 begin
847 begin
848 // Clone only valid images
851 else
859 begin
865 begin
871 begin
873 begin
876 end
877 else
882 begin
887 begin
893 begin
898 begin
903 begin
910 begin
915 begin
920 var
922 begin
925 begin
933 var
935 begin
937 begin
938 // Free image at index to be deleted
941 begin
942 // Move images to new indices if necessary
946 // Set new array length and update pointer to active image
948 SetPointer;
953 begin
958 var
960 begin
967 var
969 begin
975 var
977 begin
983 begin
990 begin
997 begin
1003 begin
1009 begin
1014 begin
1018 {
1019 File Notes:
1021 -- TODOS ----------------------------------------------------
1022 - nothing now
1024 -- 0.77.1 ---------------------------------------------------
1025 - Added TSingleImage.AssignFromData and TMultiImage.AssigntFromArray
1026 as a replacement for constructors used as methods (that is
1027 compiler error in Delphi XE3).
1028 - Added TBaseImage.ResizeToFit method.
1029 - Changed TMultiImage to have default state with no images.
1030 - TMultiImage.AddImage now returns index of newly added image.
1031 - Fixed img index bug in TMultiImage.ResizeImages
1033 -- 0.26.5 Changes/Bug Fixes ---------------------------------
1034 - Added MapImageData method to TBaseImage
1035 - Added Empty property to TBaseImage.
1036 - Added Clear method to TBaseImage.
1037 - Added ScanlineSize property to TBaseImage.
1039 -- 0.24.3 Changes/Bug Fixes ---------------------------------
1040 - Added TMultiImage.ReverseImages method.
1042 -- 0.23 Changes/Bug Fixes -----------------------------------
1043 - Added SwapChannels method to TBaseImage.
1044 - Added ReplaceColor method to TBaseImage.
1045 - Added ToString method to TBaseImage.
1047 -- 0.21 Changes/Bug Fixes -----------------------------------
1048 - Inserting images to empty MultiImage will act as Add method.
1049 - MultiImages with empty arrays will now create one image when
1050 LoadFromFile or LoadFromStream is called.
1051 - Fixed bug that caused AVs when getting props like Width, Height, asn Size
1052 and when inlining was off. There was call to Iff but with inlining disabled
1053 params like FPData.Size were evaluated and when FPData was nil => AV.
1054 - Added many FPData validity checks to many methods. There were AVs
1055 when calling most methods on empty TMultiImage.
1056 - Added AllImagesValid property to TMultiImage.
1057 - Fixed memory leak in TMultiImage.CreateFromParams.
1059 -- 0.19 Changes/Bug Fixes -----------------------------------
1060 - added ResizeImages method to TMultiImage
1061 - removed Ext parameter from various LoadFromStream methods, no
1062 longer needed
1063 - fixed various issues concerning ActiveImage of TMultiImage
1064 (it pointed to invalid location after some operations)
1065 - most of property set/get methods are now inline
1066 - added PixelPointers property to TBaseImage
1067 - added Images default array property to TMultiImage
1068 - renamed methods in TMultiImage to contain 'Image' instead of 'Level'
1069 - added canvas support
1070 - added OnDataSizeChanged and OnPixelsChanged event to TBaseImage
1071 - renamed TSingleImage.NewImage to RecreateImageData, made public, and
1072 moved to TBaseImage
1074 -- 0.17 Changes/Bug Fixes -----------------------------------
1075 - added props PaletteEntries and ScanLine to TBaseImage
1076 - aded new constructor to TBaseImage that take TBaseImage source
1077 - TMultiImage levels adding and inserting rewritten internally
1078 - added some new functions to TMultiImage: AddLevels, InsertLevels
1079 - added some new functions to TBaseImage: Flip, Mirror, Rotate,
1080 CopyRect, StretchRect
1081 - TBasicImage.Resize has now filter parameter
1082 - new stuff added to TMultiImage (DataArray prop, ConvertLevels)
1084 -- 0.13 Changes/Bug Fixes -----------------------------------
1085 - added AddLevel, InsertLevel, ExchangeLevels and DeleteLevel
1086 methods to TMultiImage
1087 - added TBaseImage, TSingleImage and TMultiImage with initial
1088 members
1089 }