ImagingGraphics32.pas 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. {
  2. Vampyre Imaging Library
  3. by Marek Mauder
  4. http://imaginglib.sourceforge.net
  5. The contents of this file are used with permission, subject to the Mozilla
  6. Public License Version 1.1 (the "License"); you may not use this file except
  7. in compliance with the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/MPL-1.1.html
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
  11. the specific language governing rights and limitations under the License.
  12. Alternatively, the contents of this file may be used under the terms of the
  13. GNU Lesser General Public License (the "LGPL License"), in which case the
  14. provisions of the LGPL License are applicable instead of those above.
  15. If you wish to allow use of your version of this file only under the terms
  16. of the LGPL License and not to allow others to use your version of this file
  17. under the MPL, indicate your decision by deleting the provisions above and
  18. replace them with the notice and other provisions required by the LGPL
  19. License. If you do not delete the provisions above, a recipient may use
  20. your version of this file under either the MPL or the LGPL License.
  21. For more information about the LGPL: http://www.gnu.org/copyleft/lesser.html
  22. }
  23. { Unit functions for converting and copying images between Imaging and Graphics32 library.}
  24. unit ImagingGraphics32;
  25. {$I ImagingOptions.inc}
  26. interface
  27. uses
  28. Types, GR32, ImagingTypes, Imaging, ImagingFormats, ImagingUtility, ImagingClasses;
  29. { Converts image from TImageData record to GR32's bitmap. Bitmap32 must be already
  30. instantiated.}
  31. procedure ConvertImageDataToBitmap32(const Image: TImageData; Bitmap32: TCustomBitmap32);
  32. { Converts image from TBaseImage instance to GR32's bitmap. Bitmap32 must be already
  33. instantiated.}
  34. procedure ConvertImageToBitmap32(Image: TBaseImage; Bitmap32: TCustomBitmap32);
  35. { Converts image data from GR32's bitmap to TImageData record.}
  36. procedure ConvertBitmap32ToImageData(Bitmap32: TCustomBitmap32; var Image: TImageData);
  37. { Converts image data from GR32's bitmap to existing TBaseImage instance.}
  38. procedure ConvertBitmap32ToImage(Bitmap32: TCustomBitmap32; Image: TBaseImage);
  39. { Copies pixels from TImageData record (with all the necessary conversion if
  40. the format is not 32bit) to existing GR32's bitmap. Both Image and Bitmap32 must
  41. have the same width and height. }
  42. procedure CopyImageDataToBitmap32(const Image: TImageData; Bitmap32: TCustomBitmap32);
  43. { Copies pixels from TBaseImage instance (with all the necessary conversion if
  44. the format is not 32bit) to existing GR32's bitmap. Both Image and Bitmap32 must
  45. have the same width and height. }
  46. procedure CopyImageToBitmap32(Image: TBaseImage; Bitmap32: TCustomBitmap32);
  47. { Copies rectangular area of pixels from TImageData record to existing GR32's bitmap.}
  48. procedure CopyRectToBitmap32(const Image: TImageData; Bitmap32: TCustomBitmap32;
  49. SrcX, SrcY, Width, Height, DstX, DstY: Integer); overload;
  50. { Copies rectangular area of pixels from TBaseImage instance to existing GR32's bitmap.}
  51. procedure CopyRectToBitmap32(Image: TBaseImage; Bitmap32: TCustomBitmap32;
  52. SrcX, SrcY, Width, Height, DstX, DstY: Integer); overload;
  53. { Maps GR32 bitmap on TImageData record so that they'll both share
  54. the same pixels in memory (Bitmap32.Bits and Image.Bits point to the same
  55. memory address). Usefull if you wan to e.g. save Bitmap32 using Imaging
  56. and don't want to needlesly duplicate the entire image in memory.
  57. Note that you must not call FreeImage on Image after the mapping or
  58. the memory of Bitmap32 would be freed too.}
  59. procedure MapBitmap32ToImageData(Bitmap32: TCustomBitmap32; var Image: TImageData);
  60. implementation
  61. procedure ConvertImageDataToBitmap32(const Image: TImageData; Bitmap32: TCustomBitmap32);
  62. begin
  63. Assert(TestImage(Image));
  64. Bitmap32.SetSize(Image.Width, Image.Height);
  65. CopyImageDataToBitmap32(Image, Bitmap32);
  66. end;
  67. procedure ConvertImageToBitmap32(Image: TBaseImage; Bitmap32: TCustomBitmap32);
  68. begin
  69. ConvertImageDataToBitmap32(Image.ImageDataPointer^, Bitmap32);
  70. end;
  71. procedure ConvertBitmap32ToImageData(Bitmap32: TCustomBitmap32; var Image: TImageData);
  72. begin
  73. Assert(not Bitmap32.Empty);
  74. NewImage(Bitmap32.Width, Bitmap32.Height, ifA8R8G8B8, Image);
  75. Move(Bitmap32.Bits^, Image.Bits^, Image.Size);
  76. end;
  77. procedure ConvertBitmap32ToImage(Bitmap32: TCustomBitmap32; Image: TBaseImage);
  78. begin
  79. ConvertBitmap32ToImageData(Bitmap32, Image.ImageDataPointer^);
  80. end;
  81. procedure CopyImageDataToBitmap32(const Image: TImageData; Bitmap32: TCustomBitmap32);
  82. begin
  83. Assert(TestImage(Image) and (Image.Width = Bitmap32.Width) and (Image.Height = Bitmap32.Height));
  84. CopyRectToBitmap32(Image, Bitmap32, 0, 0, Image.Width, Image.Height, 0, 0);
  85. end;
  86. procedure CopyImageToBitmap32(Image: TBaseImage; Bitmap32: TCustomBitmap32);
  87. begin
  88. CopyImageDataToBitmap32(Image.ImageDataPointer^, Bitmap32);
  89. end;
  90. procedure CopyRectToBitmap32(const Image: TImageData; Bitmap32: TCustomBitmap32;
  91. SrcX, SrcY, Width, Height, DstX, DstY: Integer);
  92. var
  93. TempImage: TImageData;
  94. X, Y, Bpp, SrcWidthBytes, DstWidth, MoveBytes: Integer;
  95. DstPtr: PColor32Rec;
  96. SrcPtr: PByte;
  97. Info: TImageFormatInfo;
  98. begin
  99. Assert(TestImage(Image) and not Bitmap32.Empty);
  100. ClipCopyBounds(SrcX, SrcY, Width, Height, DstX, DstY, Image.Width, Image.Height,
  101. Rect(0, 0, Bitmap32.Width, Bitmap32.Height));
  102. if Image.Format in [ifIndex8, ifGray8, ifA8Gray8, ifGray16, ifR8G8B8, ifA8R8G8B8,
  103. ifR16G16B16, ifA16R16G16B16] then
  104. begin
  105. GetImageFormatInfo(Image.Format, Info);
  106. Bpp := Info.BytesPerPixel;
  107. SrcWidthBytes := Image.Width * Bpp;
  108. DstWidth := Bitmap32.Width;
  109. MoveBytes := Width * Bpp;
  110. SrcPtr := @PByteArray(Image.Bits)[SrcY * SrcWidthBytes + SrcX * Bpp];
  111. DstPtr := @PColor32RecArray(Bitmap32.Bits)[DstY * DstWidth + DstX];
  112. for Y := 0 to Height - 1 do
  113. begin
  114. case Image.Format of
  115. ifIndex8:
  116. for X := 0 to Width - 1 do
  117. begin
  118. DstPtr^ := Image.Palette[SrcPtr^];
  119. Inc(DstPtr);
  120. Inc(SrcPtr, Bpp);
  121. end;
  122. ifGray8:
  123. for X := 0 to Width - 1 do
  124. begin
  125. DstPtr.R := SrcPtr^;
  126. DstPtr.G := SrcPtr^;
  127. DstPtr.B := SrcPtr^;
  128. DstPtr.A := 255;
  129. Inc(DstPtr);
  130. Inc(SrcPtr, Bpp);
  131. end;
  132. ifA8Gray8:
  133. for X := 0 to Width - 1 do
  134. begin
  135. DstPtr.R := SrcPtr^;
  136. DstPtr.G := SrcPtr^;
  137. DstPtr.B := SrcPtr^;
  138. DstPtr.A := PWordRec(SrcPtr).High;
  139. Inc(DstPtr);
  140. Inc(SrcPtr, Bpp);
  141. end;
  142. ifGray16:
  143. for X := 0 to Width - 1 do
  144. begin
  145. DstPtr.R := PWord(SrcPtr)^ shr 8;
  146. DstPtr.G := DstPtr.R;
  147. DstPtr.B := DstPtr.R;
  148. DstPtr.A := 255;
  149. Inc(DstPtr);
  150. Inc(SrcPtr, Bpp);
  151. end;
  152. ifR8G8B8:
  153. for X := 0 to Width - 1 do
  154. begin
  155. DstPtr.Color24Rec := PColor24Rec(SrcPtr)^;
  156. DstPtr.A := 255;
  157. Inc(DstPtr);
  158. Inc(SrcPtr, Bpp);
  159. end;
  160. ifA8R8G8B8:
  161. begin
  162. Move(SrcPtr^, DstPtr^, MoveBytes);
  163. Inc(DstPtr, Width);
  164. Inc(SrcPtr, MoveBytes);
  165. end;
  166. ifR16G16B16:
  167. for X := 0 to Width - 1 do
  168. begin
  169. DstPtr.R := PColor48Rec(SrcPtr).R shr 8;
  170. DstPtr.G := PColor48Rec(SrcPtr).G shr 8;
  171. DstPtr.B := PColor48Rec(SrcPtr).B shr 8;
  172. DstPtr.A := 255;
  173. Inc(DstPtr);
  174. Inc(SrcPtr, Bpp);
  175. end;
  176. ifA16R16G16B16:
  177. for X := 0 to Width - 1 do
  178. begin
  179. DstPtr.R := PColor64Rec(SrcPtr).R shr 8;
  180. DstPtr.G := PColor64Rec(SrcPtr).G shr 8;
  181. DstPtr.B := PColor64Rec(SrcPtr).B shr 8;
  182. DstPtr.A := PColor64Rec(SrcPtr).A shr 8;
  183. Inc(DstPtr);
  184. Inc(SrcPtr, Bpp);
  185. end;
  186. end;
  187. Inc(SrcPtr, SrcWidthBytes - MoveBytes);
  188. Inc(DstPtr, DstWidth - Width);
  189. end;
  190. end
  191. else
  192. begin
  193. InitImage(TempImage);
  194. CloneImage(Image, TempImage);
  195. ConvertImage(TempImage, ifA8R8G8B8);
  196. try
  197. CopyRectToBitmap32(TempImage, Bitmap32, SrcX, SrcY, Width, Height, DstX, DstY);
  198. finally
  199. FreeImage(TempImage);
  200. end;
  201. end;
  202. end;
  203. procedure CopyRectToBitmap32(Image: TBaseImage; Bitmap32: TCustomBitmap32;
  204. SrcX, SrcY, Width, Height, DstX, DstY: Integer);
  205. begin
  206. CopyRectToBitmap32(Image.ImageDataPointer^, Bitmap32,
  207. SrcX, SrcY, Width, Height, DstX, DstY);
  208. end;
  209. procedure MapBitmap32ToImageData(Bitmap32: TCustomBitmap32; var Image: TImageData);
  210. begin
  211. Assert(not Bitmap32.Empty);
  212. FreeImage(Image);
  213. Image.Width := Bitmap32.Width;
  214. Image.Height := Bitmap32.Height;
  215. Image.Format := ifA8R8G8B8;
  216. Image.Size := Image.Width * Image.Height * 4;
  217. Image.Bits := Bitmap32.Bits;
  218. end;
  219. {
  220. File Notes:
  221. -- 0.26.5 Changes/Bug Fixes ---------------------------------
  222. - Created with initial stuff.
  223. }
  224. end.