unit imageIO;
(* ***** BEGIN LICENSE BLOCK *****
* Copyright (C) 2004 Durand Emmanuel
* Copyright (C) 2004 Burgel Eric
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact :
* filters@edurand.com
* filters@burgel.com
*
* ***** END LICENSE BLOCK ***** *)
{
edurand (filters@edurand.com)
eburgel (filters@burgel.com)
}
interface
uses
image, SysUtils, ExtCtrls;
function createImageFromFile( const aFileName:String ) : PBitmap32;
procedure copyImageToFile( const aImage:PBitmap32; const aFileName:String );
implementation
uses {Jpeg, }Windows, FreeImage;
{see http://freeimage.sourceforge.net}
{see http://www.dcs.ed.ac.uk/home/mxr/gfx/2d-hi.html}
function createImageFromFile(const aFileName:String) : PBitmap32;
var
t : FREE_IMAGE_FORMAT;
Ext : string;
dib, dib32 : PFIBITMAP;
width, height : Integer;
scan_width : Integer;
img : PBitmap32;
begin
t := FreeImage_GetFileType(PChar(aFileName), 16);
if t = FIF_UNKNOWN then begin
// Check for types not supported by GetFileType
Ext := UpperCase(ExtractFileExt(aFileName));
if (Ext = '.TGA') or(Ext = '.TARGA') then
t := FIF_TARGA
else if Ext = '.MNG' then
t := FIF_MNG
else if Ext = '.PCD' then
t := FIF_PCD
else if Ext = '.WBMP' then
t := FIF_WBMP
else if Ext = '.CUT' then
t := FIF_CUT
else if (Ext = '.JPG') or (Ext = '.JPEG') then
t := FIF_JPEG
else if Ext = '.TIF' then
t := FIF_TIFF
else
raise Exception.Create('The file [' + aFileName + '] cannot be loaded : can not recognise the file type.');
end;
dib := FreeImage_Load(t, PChar(aFileName), 0);
FreeImage_FlipVertical(dib);
dib32 := FreeImage_ConvertTo32Bits(dib);
FreeImage_Unload(dib);
width := FreeImage_GetWidth(dib32);
height := FreeImage_GetHeight(dib32);
scan_width := FreeImage_GetPitch(dib32);
img := createImage(width,height);
FreeImage_ConvertToRawBits(PBYTE(img.Bits), dib32, scan_width, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, TRUE);
FreeImage_Unload(dib32);
Result:=img;
end;
procedure copyImageToFile( const aImage:PBitmap32; const aFileName:String);
var
ext : String;
fif : FREE_IMAGE_FORMAT;
flags : Integer;
dib, dib24 : PFIBITMAP;
dibBits : PByte;
begin
if aImage=nil then raise Exception.Create('error on copyImageToFile : image is null');
ext:=extractFileExt(aFileName);
fif:=FIF_BMP; flags:=BMP_DEFAULT;
if (ext='.jpg') or (ext='.jpeg') then begin
fif:=FIF_JPEG; flags:=JPEG_QUALITYSUPERB;
end else if (ext='.tiff') or (ext='.tif') then begin
fif:=FIF_TIFF; flags:=TIFF_LZW;
end;
dib:=FreeImage_Allocate(aImage.Width,aImage.Height,32);
dibBits:=FreeImage_GetBits(dib);
Move(aImage.Bits^, dibBits^, aImage.Width*aImage.Height*SizeOf(TColor32));
dib24 := FreeImage_ConvertTo24Bits(dib);
FreeImage_FlipVertical(dib24);
FreeImage_Save(fif,dib24,PChar(aFileName),flags);
FreeImage_Unload(dib24);
FreeImage_Unload(dib);
end;
initialization
FreeImage_Initialise(false);
finalization
FreeImage_DeInitialise
end.