Using dll/so Access

You can use Imaging library directly from your Object Pascal project by means of uses clause. This kind of usage is recommended but you can also use Imaging library in the form of external library - dynamic link library (Windows dll file) or shared objects (Linux SO file). Using external library can be useful if you have have more executables using Imaging which will be distributed together (so you can have smaller executables). This is also only way how to use Imaging from programming languages other than Object Pascal.

First you need Imaging compiled to external library. Projects for building external library can be found in Source/Projects directory. You can also use build scripts named BuildLibrary*.bat and BuildLibrary*.sh. They are located in Scripts directory and compiled library will be placed in Bin directory. In order to successfully use these scripts you must have paths to compilers properly set. When you have compiled library you must assure that operating system is be able to find it at your application's startup. Windows library is named VampyreImaging.dll and Linux version has name libVampyreImaging.so.

Next thing you need is wrapper for programming language you want to use. Now there are wrappers for Object Pascal, C/C++ and Delphi.NET. Wrappers are located in Source/Wrappers directory.

Note: Some types of function parameters differ between dll/so usage and direct usage. string types are changed to PChar types, dynamic arrays are not used at all - wrapper type TImageDataList is used instead. Also all function names have Im prefix.

Here you can find some code fragments which will show you how to use dll/so access to Imaging library. If you want samples in language other than Object Pascal look at Demos.

uses
  ImagingTypes, ImagingImport;
var
  Img: TImageData
begin
  // this call loads external Imaging library, you should check if it was successful
  if not ImLoadLibrary then
    WriteLn('Imaging library dll was not loaded successfully, program will crash soon!');
  // call this before any access to TImageData is made
  ImInitImage(Img);
  // load some image
  ImLoadImageFromFile('/home/galfar/images/jaguar.jpg', Img);
  // compress it to DXT1 format
  ImConvertImage(Img, ifDXT1);
  // and save it in DDS file format
  ImSaveImageToFile('/home/galfar/images/jaguar.dds', Img);
  // free memory occupied by image
  ImFreeImage(Img);
  // unload Imaging library
  ImFreeLibrary;
end.
    

Since TDynImageDataArray is Object Pascal dynamic array, which can not be used from other languages, all parameters of this type are replaced with TImageDataList typed parameters. Some new functions are introduced for accessing list's items and properties. You can see them all in action in the next code listing.

uses
  ImagingTypes, ImagingImport;
var
  ImgList: TImageDataList;
  Img: TImageData;
  I, Size: LongInt;
begin
  // this call loads external Imaging library, you should check if it was successful
  if not ImLoadLibrary then
    WriteLn('Imaging library dll was not loaded successfully, program will crash soon!');
  // make sure list pointer doesn't point to invalid memory address
  ImgList := nil;
  // load some images, list's size is changed during loading
  ImLoadMultiImageFromFile('/home/galfar/images/jaguar_with_mipmaps.jpg', Img);
  // get the actual list's size
  Size := ImGetImageListSize(ImgList);
  for I := 0 to Size - 1 do
  begin
    // get list's element
    ImGetImageListElement(ImgList, Img, I);
    // resize element
    ImResizeImage(Img, Img.Width / 2, Img.Height / 2, rfBicubic);
    // set list's element
    ImSetImageListElement(ImgList, Img, I);
  end;
  // create space for new image in the list
  ImSetImageListSize(ImgList, Size + 1);
  // create new image
  ImNewImage(256, 256, ifDXT1, Img);
  // put this new image into the list
  ImSetImageListElement(ImgList, Img, Size);
  // save halved images to file
  ImSaveMultiImageToFile('/home/galfar/images/jaguar_smaller.dds', Img);
  // free all images in list and list itself
  ImFreeImageList(ImgList);
  // unload Imaging library
  ImFreeLibrary;
end.