fpimage.pp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2003 by the Free Pascal development team
  5. fpImage base definitions.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$mode objfpc}{$h+}
  13. unit FPimage;
  14. interface
  15. uses sysutils, classes;
  16. type
  17. TFPCustomImageReader = class;
  18. TFPCustomImageWriter = class;
  19. TFPCustomImage = class;
  20. FPImageException = class (exception);
  21. TFPColor = record
  22. red,green,blue,alpha : word;
  23. end;
  24. PFPColor = ^TFPColor;
  25. TColorFormat = (cfMono,cfGray2,cfGray4,cfGray8,cfGray16,cfGray24,
  26. cfGrayA8,cfGrayA16,cfGrayA32,
  27. cfRGB15,cfRGB16,cfRGB24,cfRGB32,cfRGB48,
  28. cfRGBA8,cfRGBA16,cfRGBA32,cfRGBA64,
  29. cfBGR15,cfBGR16,cfBGR24,cfBGR32,cfBGR48,
  30. cfABGR8,cfABGR16,cfABGR32,cfABGR64);
  31. TColorData = qword;
  32. PColorData = ^TColorData;
  33. TDeviceColor = record
  34. Fmt : TColorFormat;
  35. Data : TColorData;
  36. end;
  37. {$ifdef CPU68K}
  38. { 1.0 m68k cpu compiler does not allow
  39. types larger than 32k....
  40. if we remove range checking all should be fine PM }
  41. TFPColorArray = array [0..0] of TFPColor;
  42. {$R-}
  43. {$else not CPU68K}
  44. TFPColorArray = array [0..(maxint-1) div sizeof(TFPColor)-1] of TFPColor;
  45. {$endif CPU68K}
  46. PFPColorArray = ^TFPColorArray;
  47. TFPImgProgressStage = (psStarting, psRunning, psEnding);
  48. TFPImgProgressEvent = procedure (Sender: TObject; Stage: TFPImgProgressStage;
  49. PercentDone: Byte; RedrawNow: Boolean; const R: TRect;
  50. const Msg: AnsiString; var Continue : Boolean) of object;
  51. // Delphi compatibility
  52. TProgressStage = TFPImgProgressStage;
  53. TProgressEvent = TFPImgProgressEvent;
  54. TFPPalette = class
  55. protected
  56. FData : PFPColorArray;
  57. FCount, FCapacity : integer;
  58. procedure SetCount (Value:integer); virtual;
  59. function GetCount : integer;
  60. procedure SetColor (index:integer; const Value:TFPColor); virtual;
  61. function GetColor (index:integer) : TFPColor;
  62. procedure CheckIndex (index:integer); virtual;
  63. procedure EnlargeData; virtual;
  64. public
  65. constructor Create (ACount : integer);
  66. destructor Destroy; override;
  67. procedure Build (Img : TFPCustomImage); virtual;
  68. procedure Merge (pal : TFPPalette); virtual;
  69. function IndexOf (const AColor: TFPColor) : integer; virtual;
  70. function Add (const Value: TFPColor) : integer; virtual;
  71. procedure Clear; virtual;
  72. property Color [Index : integer] : TFPColor read GetColor write SetColor; default;
  73. property Count : integer read GetCount write SetCount;
  74. end;
  75. TFPCustomImage = class(TPersistent)
  76. private
  77. FOnProgress : TFPImgProgressEvent;
  78. FExtra : TStringlist;
  79. FPalette : TFPPalette;
  80. FHeight, FWidth : integer;
  81. procedure SetHeight (Value : integer);
  82. procedure SetWidth (Value : integer);
  83. procedure SetExtra (const key:String; const AValue:string);
  84. function GetExtra (const key:String) : string;
  85. procedure SetExtraValue (index:integer; const AValue:string);
  86. function GetExtraValue (index:integer) : string;
  87. procedure SetExtraKey (index:integer; const AValue:string);
  88. function GetExtraKey (index:integer) : string;
  89. procedure CheckIndex (x,y:integer);
  90. procedure CheckPaletteIndex (PalIndex:integer);
  91. procedure SetColor (x,y:integer; const Value:TFPColor);
  92. function GetColor (x,y:integer) : TFPColor;
  93. procedure SetPixel (x,y:integer; Value:integer);
  94. function GetPixel (x,y:integer) : integer;
  95. function GetUsePalette : boolean;
  96. protected
  97. // Procedures to store the data. Implemented in descendants
  98. procedure SetInternalColor (x,y:integer; const Value:TFPColor); virtual;
  99. function GetInternalColor (x,y:integer) : TFPColor; virtual;
  100. procedure SetInternalPixel (x,y:integer; Value:integer); virtual; abstract;
  101. function GetInternalPixel (x,y:integer) : integer; virtual; abstract;
  102. procedure SetUsePalette (Value:boolean);virtual;
  103. procedure Progress(Sender: TObject; Stage: TProgressStage;
  104. PercentDone: Byte; RedrawNow: Boolean; const R: TRect;
  105. const Msg: AnsiString; var Continue: Boolean); Virtual;
  106. public
  107. constructor create (AWidth,AHeight:integer); virtual;
  108. destructor destroy; override;
  109. procedure Assign(Source: TPersistent); override;
  110. // Saving and loading
  111. procedure LoadFromStream (Str:TStream; Handler:TFPCustomImageReader);
  112. procedure LoadFromStream (Str:TStream);
  113. procedure LoadFromFile (const filename:String; Handler:TFPCustomImageReader);
  114. procedure LoadFromFile (const filename:String);
  115. procedure SaveToStream (Str:TStream; Handler:TFPCustomImageWriter);
  116. procedure SaveToFile (const filename:String; Handler:TFPCustomImageWriter);
  117. procedure SaveToFile (const filename:String);
  118. // Size and data
  119. procedure SetSize (AWidth, AHeight : integer); virtual;
  120. property Height : integer read FHeight write SetHeight;
  121. property Width : integer read FWidth write SetWidth;
  122. property Colors [x,y:integer] : TFPColor read GetColor write SetColor; default;
  123. // Use of palette for colors
  124. property UsePalette : boolean read GetUsePalette write SetUsePalette;
  125. property Palette : TFPPalette read FPalette;
  126. property Pixels [x,y:integer] : integer read GetPixel write SetPixel;
  127. // Info unrelated with the image representation
  128. property Extra [const key:string] : string read GetExtra write SetExtra;
  129. property ExtraValue [index:integer] : string read GetExtraValue write SetExtraValue;
  130. property ExtraKey [index:integer] : string read GetExtraKey write SetExtraKey;
  131. procedure RemoveExtra (const key:string);
  132. function ExtraCount : integer;
  133. property OnProgress: TFPImgProgressEvent read FOnProgress write FOnProgress;
  134. end;
  135. TFPCustomImageClass = class of TFPCustomImage;
  136. {$ifdef CPU68K}
  137. { 1.0 m68k cpu compiler does not allow
  138. types larger than 32k....
  139. if we remove range checking all should be fine PM }
  140. TFPIntegerArray = array [0..0] of integer;
  141. {$R-}
  142. {$else not CPU68K}
  143. TFPIntegerArray = array [0..(maxint-1) div sizeof(integer)-1] of integer;
  144. {$endif CPU68K}
  145. PFPIntegerArray = ^TFPIntegerArray;
  146. TFPMemoryImage = class (TFPCustomImage)
  147. private
  148. FData : PFPIntegerArray;
  149. function GetInternalColor(x,y:integer):TFPColor;override;
  150. procedure SetInternalColor (x,y:integer; const Value:TFPColor);override;
  151. procedure SetUsePalette (Value:boolean);override;
  152. protected
  153. procedure SetInternalPixel (x,y:integer; Value:integer); override;
  154. function GetInternalPixel (x,y:integer) : integer; override;
  155. public
  156. constructor create (AWidth,AHeight:integer); override;
  157. destructor destroy; override;
  158. procedure SetSize (AWidth, AHeight : integer); override;
  159. end;
  160. TFPCustomImageHandler = class
  161. private
  162. FOnProgress : TFPImgProgressEvent;
  163. FStream : TStream;
  164. FImage : TFPCustomImage;
  165. protected
  166. procedure Progress(Stage: TProgressStage; PercentDone: Byte; RedrawNow: Boolean; const R: TRect;
  167. const Msg: AnsiString; var Continue: Boolean); Virtual;
  168. property TheStream : TStream read FStream;
  169. property TheImage : TFPCustomImage read FImage;
  170. public
  171. constructor Create; virtual;
  172. Property OnProgress : TFPImgProgressEvent Read FOnProgress Write FOnProgress;
  173. end;
  174. TFPCustomImageReader = class (TFPCustomImageHandler)
  175. private
  176. FDefImageClass:TFPCustomImageClass;
  177. protected
  178. procedure InternalRead (Str:TStream; Img:TFPCustomImage); virtual; abstract;
  179. function InternalCheck (Str:TStream) : boolean; virtual; abstract;
  180. public
  181. constructor Create; override;
  182. function ImageRead (Str:TStream; Img:TFPCustomImage) : TFPCustomImage;
  183. // reads image
  184. function CheckContents (Str:TStream) : boolean;
  185. // Gives True if contents is readable
  186. property DefaultImageClass : TFPCustomImageClass read FDefImageClass write FDefImageClass;
  187. // Image Class to create when no img is given for reading
  188. end;
  189. TFPCustomImageReaderClass = class of TFPCustomImageReader;
  190. TFPCustomImageWriter = class (TFPCustomImageHandler)
  191. protected
  192. procedure InternalWrite (Str:TStream; Img:TFPCustomImage); virtual; abstract;
  193. public
  194. procedure ImageWrite (Str:TStream; Img:TFPCustomImage);
  195. // writes given image to stream
  196. end;
  197. TFPCustomImageWriterClass = class of TFPCustomImageWriter;
  198. TIHData = class
  199. private
  200. FExtention, FTypeName, FDefaultExt : string;
  201. FReader : TFPCustomImageReaderClass;
  202. FWriter : TFPCustomImageWriterClass;
  203. end;
  204. TImageHandlersManager = class
  205. private
  206. FData : TList;
  207. function GetReader (const TypeName:string) : TFPCustomImageReaderClass;
  208. function GetWriter (const TypeName:string) : TFPCustomImageWriterClass;
  209. function GetExt (const TypeName:string) : string;
  210. function GetDefExt (const TypeName:string) : string;
  211. function GetTypeName (index:integer) : string;
  212. function GetData (const ATypeName:string) : TIHData;
  213. function GetData (index : integer) : TIHData;
  214. function GetCount : integer;
  215. public
  216. constructor Create;
  217. destructor Destroy; override;
  218. procedure RegisterImageHandlers (const ATypeName,TheExtentions:string;
  219. AReader:TFPCustomImageReaderClass; AWriter:TFPCustomImageWriterClass);
  220. procedure RegisterImageReader (const ATypeName,TheExtentions:string;
  221. AReader:TFPCustomImageReaderClass);
  222. procedure RegisterImageWriter (const ATypeName,TheExtentions:string;
  223. AWriter:TFPCustomImageWriterClass);
  224. property Count : integer read GetCount;
  225. property ImageReader [const TypeName:string] : TFPCustomImageReaderClass read GetReader;
  226. property ImageWriter [const TypeName:string] : TFPCustomImageWriterClass read GetWriter;
  227. property Extentions [const TypeName:string] : string read GetExt;
  228. property DefaultExtention [const TypeName:string] : string read GetDefExt;
  229. property TypeNames [index:integer] : string read GetTypeName;
  230. end;
  231. {function ShiftAndFill (initial:word; CorrectBits:byte):word;
  232. function FillOtherBits (initial:word;CorrectBits:byte):word;
  233. }
  234. function CalculateGray (const From : TFPColor) : word;
  235. (*
  236. function ConvertColor (const From : TDeviceColor) : TFPColor;
  237. function ConvertColor (const From : TColorData; FromFmt:TColorFormat) : TFPColor;
  238. function ConvertColorToData (const From : TFPColor; Fmt : TColorFormat) : TColorData;
  239. function ConvertColorToData (const From : TDeviceColor; Fmt : TColorFormat) : TColorData;
  240. function ConvertColor (const From : TFPColor; Fmt : TColorFormat) : TDeviceColor;
  241. function ConvertColor (const From : TDeviceColor; Fmt : TColorFormat) : TDeviceColor;
  242. *)
  243. function FPColor (r,g,b,a:word) : TFPColor;
  244. function FPColor (r,g,b:word) : TFPColor;
  245. {$ifdef debug}function MakeHex (n:TColordata;nr:byte): string;{$endif}
  246. operator = (const c,d:TFPColor) : boolean;
  247. operator or (const c,d:TFPColor) : TFPColor;
  248. operator and (const c,d:TFPColor) : TFPColor;
  249. operator xor (const c,d:TFPColor) : TFPColor;
  250. function CompareColors(const Color1, Color2: TFPColor): integer;
  251. var ImageHandlers : TImageHandlersManager;
  252. type
  253. TErrorTextIndices = (
  254. StrInvalidIndex,
  255. StrNoImageToWrite,
  256. StrNoFile,
  257. StrNoStream,
  258. StrPalette,
  259. StrImageX,
  260. StrImageY,
  261. StrImageExtra,
  262. StrTypeAlreadyExist,
  263. StrTypeReaderAlreadyExist,
  264. StrTypeWriterAlreadyExist,
  265. StrCantDetermineType,
  266. StrNoCorrectReaderFound,
  267. StrReadWithError,
  268. StrWriteWithError,
  269. StrNoPaletteAvailable
  270. );
  271. const
  272. // MG: ToDo: move to implementation and add a function to map to resourcestrings
  273. ErrorText : array[TErrorTextIndices] of string =
  274. ('Invalid %s index %d',
  275. 'No image to write',
  276. 'File "%s" does not exist',
  277. 'No stream to write to',
  278. 'palette',
  279. 'horizontal pixel',
  280. 'vertical pixel',
  281. 'extra',
  282. 'Image type "%s" already exists',
  283. 'Image type "%s" already has a reader class',
  284. 'Image type "%s" already has a writer class',
  285. 'Error while determining image type of stream: %s',
  286. 'Can''t determine image type of stream',
  287. 'Error while reading stream: %s',
  288. 'Error while writing stream: %s',
  289. 'No palette available'
  290. );
  291. {$i FPColors.inc}
  292. type
  293. TGrayConvMatrix = record
  294. red, green, blue : single;
  295. end;
  296. var
  297. GrayConvMatrix : TGrayConvMatrix;
  298. const
  299. GCM_NTSC : TGrayConvMatrix = (red:0.299; green:0.587; blue:0.114);
  300. GCM_JPEG : TGrayConvMatrix = (red:0.299; green:0.587; blue:0.114);
  301. GCM_Mathematical : TGrayConvMatrix = (red:0.334; green:0.333; blue:0.333);
  302. GCM_Photoshop : TGrayConvMatrix = (red:0.213; green:0.715; blue:0.072);
  303. implementation
  304. procedure FPImgError (Fmt:TErrorTextIndices; data : array of const);
  305. begin
  306. raise FPImageException.CreateFmt (ErrorText[Fmt],data);
  307. end;
  308. procedure FPImgError (Fmt:TErrorTextIndices);
  309. begin
  310. raise FPImageException.Create (ErrorText[Fmt]);
  311. end;
  312. {$i FPImage.inc}
  313. {$i FPHandler.inc}
  314. {$i FPPalette.inc}
  315. {$i FPColCnv.inc}
  316. function FPColor (r,g,b:word) : TFPColor;
  317. begin
  318. with result do
  319. begin
  320. red := r;
  321. green := g;
  322. blue := b;
  323. alpha := alphaOpaque;
  324. end;
  325. end;
  326. function FPColor (r,g,b,a:word) : TFPColor;
  327. begin
  328. with result do
  329. begin
  330. red := r;
  331. green := g;
  332. blue := b;
  333. alpha := a;
  334. end;
  335. end;
  336. operator = (const c,d:TFPColor) : boolean;
  337. begin
  338. result := (c.Red = d.Red) and
  339. (c.Green = d.Green) and
  340. (c.Blue = d.Blue) and
  341. (c.Alpha = d.Alpha);
  342. end;
  343. function GetFullColorData (color:TFPColor) : TColorData;
  344. begin
  345. result := PColorData(@color)^;
  346. end;
  347. function SetFullColorData (color:TColorData) : TFPColor;
  348. begin
  349. result := PFPColor (@color)^;
  350. end;
  351. operator or (const c,d:TFPColor) : TFPColor;
  352. begin
  353. result := SetFullColorData(GetFullColorData(c) OR GetFullColorData(d));
  354. end;
  355. operator and (const c,d:TFPColor) : TFPColor;
  356. begin
  357. result := SetFullColorData(GetFullColorData(c) AND GetFullColorData(d));
  358. end;
  359. operator xor (const c,d:TFPColor) : TFPColor;
  360. begin
  361. result := SetFullColorData(GetFullColorData(c) XOR GetFullColorData(d));
  362. end;
  363. {$ifdef debug}
  364. function MakeHex (n:TColordata;nr:byte): string;
  365. const hexnums : array[0..15] of char =
  366. ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
  367. var r : integer;
  368. begin
  369. result := '';
  370. for r := 0 to nr-1 do
  371. begin
  372. result := hexnums[n and $F] + result;
  373. n := n shr 4;
  374. if ((r+1) mod 4) = 0 then
  375. result := ' ' + result;
  376. end;
  377. end;
  378. {$endif}
  379. initialization
  380. ImageHandlers := TImageHandlersManager.Create;
  381. GrayConvMatrix := GCM_JPEG;
  382. // Following lines are here because the compiler 1.0 can't work with int64 constants
  383. (* ColorBits [cfRGB48,1] := ColorBits [cfRGB48,1] shl 16;
  384. ColorBits [cfRGBA64,1] := ColorBits [cfRGBA64,1] shl 32;
  385. ColorBits [cfRGBA64,2] := ColorBits [cfRGBA64,2] shl 16;
  386. ColorBits [cfABGR64,0] := ColorBits [cfABGR64,0] shl 32;
  387. ColorBits [cfABGR64,3] := ColorBits [cfABGR64,3] shl 16;
  388. ColorBits [cfBGR48,3] := ColorBits [cfBGR48,3] shl 16;
  389. PrepareBitMasks;*)
  390. finalization
  391. ImageHandlers.Free;
  392. end.