fpimage.pp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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)] 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. procedure SetUsePalette (Value:boolean);virtual;
  97. protected
  98. // Procedures to store the data. Implemented in descendants
  99. procedure SetInternalColor (x,y:integer; const Value:TFPColor); virtual;
  100. function GetInternalColor (x,y:integer) : TFPColor; virtual;
  101. procedure SetInternalPixel (x,y:integer; Value:integer); virtual; abstract;
  102. function GetInternalPixel (x,y:integer) : integer; virtual; abstract;
  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. // Size and data
  118. procedure SetSize (AWidth, AHeight : integer); virtual;
  119. property Height : integer read FHeight write SetHeight;
  120. property Width : integer read FWidth write SetWidth;
  121. property Colors [x,y:integer] : TFPColor read GetColor write SetColor; default;
  122. // Use of palette for colors
  123. property UsePalette : boolean read GetUsePalette write SetUsePalette;
  124. property Palette : TFPPalette read FPalette;
  125. property Pixels [x,y:integer] : integer read GetPixel write SetPixel;
  126. // Info unrelated with the image representation
  127. property Extra [const key:string] : string read GetExtra write SetExtra;
  128. property ExtraValue [index:integer] : string read GetExtraValue write SetExtraValue;
  129. property ExtraKey [index:integer] : string read GetExtraKey write SetExtraKey;
  130. procedure RemoveExtra (const key:string);
  131. function ExtraCount : integer;
  132. property OnProgress: TFPImgProgressEvent read FOnProgress write FOnProgress;
  133. end;
  134. TFPCustomImageClass = class of TFPCustomImage;
  135. {$ifdef CPU68K}
  136. { 1.0 m68k cpu compiler does not allow
  137. types larger than 32k....
  138. if we remove range checking all should be fine PM }
  139. TFPIntegerArray = array [0..0] of integer;
  140. {$R-}
  141. {$else not CPU68K}
  142. TFPIntegerArray = array [0..(maxint-1) div sizeof(integer)] of integer;
  143. {$endif CPU68K}
  144. PFPIntegerArray = ^TFPIntegerArray;
  145. TFPMemoryImage = class (TFPCustomImage)
  146. private
  147. FData : PFPIntegerArray;
  148. function GetInternalColor(x,y:integer):TFPColor;override;
  149. procedure SetInternalColor (x,y:integer; const Value:TFPColor);override;
  150. procedure SetUsePalette (Value:boolean);override;
  151. protected
  152. procedure SetInternalPixel (x,y:integer; Value:integer); override;
  153. function GetInternalPixel (x,y:integer) : integer; override;
  154. public
  155. constructor create (AWidth,AHeight:integer); override;
  156. destructor destroy; override;
  157. procedure SetSize (AWidth, AHeight : integer); override;
  158. end;
  159. TFPCustomImageHandler = class
  160. private
  161. FOnProgress : TFPImgProgressEvent;
  162. FStream : TStream;
  163. FImage : TFPCustomImage;
  164. protected
  165. procedure Progress(Stage: TProgressStage; PercentDone: Byte; RedrawNow: Boolean; const R: TRect;
  166. const Msg: AnsiString; var Continue: Boolean); Virtual;
  167. property TheStream : TStream read FStream;
  168. property TheImage : TFPCustomImage read FImage;
  169. public
  170. constructor Create; virtual;
  171. Property OnProgress : TFPImgProgressEvent Read FOnProgress Write FOnProgress;
  172. end;
  173. TFPCustomImageReader = class (TFPCustomImageHandler)
  174. private
  175. FDefImageClass:TFPCustomImageClass;
  176. protected
  177. procedure InternalRead (Str:TStream; Img:TFPCustomImage); virtual; abstract;
  178. function InternalCheck (Str:TStream) : boolean; virtual; abstract;
  179. public
  180. constructor Create; override;
  181. function ImageRead (Str:TStream; Img:TFPCustomImage) : TFPCustomImage;
  182. // reads image
  183. function CheckContents (Str:TStream) : boolean;
  184. // Gives True if contents is readable
  185. property DefaultImageClass : TFPCustomImageClass read FDefImageClass write FDefImageClass;
  186. // Image Class to create when no img is given for reading
  187. end;
  188. TFPCustomImageReaderClass = class of TFPCustomImageReader;
  189. TFPCustomImageWriter = class (TFPCustomImageHandler)
  190. protected
  191. procedure InternalWrite (Str:TStream; Img:TFPCustomImage); virtual; abstract;
  192. public
  193. procedure ImageWrite (Str:TStream; Img:TFPCustomImage);
  194. // writes given image to stream
  195. end;
  196. TFPCustomImageWriterClass = class of TFPCustomImageWriter;
  197. TIHData = class
  198. private
  199. FExtention, FTypeName, FDefaultExt : string;
  200. FReader : TFPCustomImageReaderClass;
  201. FWriter : TFPCustomImageWriterClass;
  202. end;
  203. TImageHandlersManager = class
  204. private
  205. FData : TList;
  206. function GetReader (const TypeName:string) : TFPCustomImageReaderClass;
  207. function GetWriter (const TypeName:string) : TFPCustomImageWriterClass;
  208. function GetExt (const TypeName:string) : string;
  209. function GetDefExt (const TypeName:string) : string;
  210. function GetTypeName (index:integer) : string;
  211. function GetData (const ATypeName:string) : TIHData;
  212. function GetData (index : integer) : TIHData;
  213. function GetCount : integer;
  214. public
  215. constructor Create;
  216. destructor Destroy; override;
  217. procedure RegisterImageHandlers (const ATypeName,TheExtentions:string;
  218. AReader:TFPCustomImageReaderClass; AWriter:TFPCustomImageWriterClass);
  219. procedure RegisterImageReader (const ATypeName,TheExtentions:string;
  220. AReader:TFPCustomImageReaderClass);
  221. procedure RegisterImageWriter (const ATypeName,TheExtentions:string;
  222. AWriter:TFPCustomImageWriterClass);
  223. property Count : integer read GetCount;
  224. property ImageReader [const TypeName:string] : TFPCustomImageReaderClass read GetReader;
  225. property ImageWriter [const TypeName:string] : TFPCustomImageWriterClass read GetWriter;
  226. property Extentions [const TypeName:string] : string read GetExt;
  227. property DefaultExtention [const TypeName:string] : string read GetDefExt;
  228. property TypeNames [index:integer] : string read GetTypeName;
  229. end;
  230. {function ShiftAndFill (initial:word; CorrectBits:byte):word;
  231. function FillOtherBits (initial:word;CorrectBits:byte):word;
  232. }
  233. function CalculateGray (const From : TFPColor) : word;
  234. (*
  235. function ConvertColor (const From : TDeviceColor) : TFPColor;
  236. function ConvertColor (const From : TColorData; FromFmt:TColorFormat) : TFPColor;
  237. function ConvertColorToData (const From : TFPColor; Fmt : TColorFormat) : TColorData;
  238. function ConvertColorToData (const From : TDeviceColor; Fmt : TColorFormat) : TColorData;
  239. function ConvertColor (const From : TFPColor; Fmt : TColorFormat) : TDeviceColor;
  240. function ConvertColor (const From : TDeviceColor; Fmt : TColorFormat) : TDeviceColor;
  241. *)
  242. function FPColor (r,g,b,a:word) : TFPColor;
  243. function FPColor (r,g,b:word) : TFPColor;
  244. {$ifdef debug}function MakeHex (n:TColordata;nr:byte): string;{$endif}
  245. operator = (const c,d:TFPColor) : boolean;
  246. operator or (const c,d:TFPColor) : TFPColor;
  247. operator and (const c,d:TFPColor) : TFPColor;
  248. operator xor (const c,d:TFPColor) : TFPColor;
  249. function CompareColors(const Color1, Color2: TFPColor): integer;
  250. var ImageHandlers : TImageHandlersManager;
  251. type
  252. TErrorTextIndices = (
  253. StrInvalidIndex,
  254. StrNoImageToWrite,
  255. StrNoFile,
  256. StrNoStream,
  257. StrPalette,
  258. StrImageX,
  259. StrImageY,
  260. StrImageExtra,
  261. StrTypeAlreadyExist,
  262. StrTypeReaderAlreadyExist,
  263. StrTypeWriterAlreadyExist,
  264. StrCantDetermineType,
  265. StrNoCorrectReaderFound,
  266. StrReadWithError,
  267. StrNoPaletteAvailable
  268. );
  269. const
  270. // MG: ToDo: move to implementation and add a function to map to resourcestrings
  271. ErrorText : array[TErrorTextIndices] of string =
  272. ('Invalid %s index %d',
  273. 'No image to write',
  274. 'File "%s" does not exist',
  275. 'No stream to write to',
  276. 'palette',
  277. 'horizontal pixel',
  278. 'vertical pixel',
  279. 'extra',
  280. 'Image type "%s" already exists',
  281. 'Image type "%s" already has a reader class',
  282. 'Image type "%s" already has a writer class',
  283. 'Error while determining image type of stream: %s',
  284. 'Can''t determine image type of stream',
  285. 'Error while reading stream: %s',
  286. 'No palette available'
  287. );
  288. {$i FPColors.inc}
  289. type
  290. TGrayConvMatrix = record
  291. red, green, blue : single;
  292. end;
  293. var
  294. GrayConvMatrix : TGrayConvMatrix;
  295. const
  296. GCM_NTSC : TGrayConvMatrix = (red:0.299; green:0.587; blue:0.114);
  297. GCM_JPEG : TGrayConvMatrix = (red:0.299; green:0.587; blue:0.114);
  298. GCM_Mathematical : TGrayConvMatrix = (red:0.334; green:0.333; blue:0.333);
  299. GCM_Photoshop : TGrayConvMatrix = (red:0.213; green:0.715; blue:0.072);
  300. implementation
  301. procedure FPImgError (Fmt:TErrorTextIndices; data : array of const);
  302. begin
  303. raise FPImageException.CreateFmt (ErrorText[Fmt],data);
  304. end;
  305. procedure FPImgError (Fmt:TErrorTextIndices);
  306. begin
  307. raise FPImageException.Create (ErrorText[Fmt]);
  308. end;
  309. {$i FPImage.inc}
  310. {$i FPHandler.inc}
  311. {$i FPPalette.inc}
  312. {$i FPColCnv.inc}
  313. function FPColor (r,g,b:word) : TFPColor;
  314. begin
  315. with result do
  316. begin
  317. red := r;
  318. green := g;
  319. blue := b;
  320. alpha := alphaOpaque;
  321. end;
  322. end;
  323. function FPColor (r,g,b,a:word) : TFPColor;
  324. begin
  325. with result do
  326. begin
  327. red := r;
  328. green := g;
  329. blue := b;
  330. alpha := a;
  331. end;
  332. end;
  333. operator = (const c,d:TFPColor) : boolean;
  334. begin
  335. result := (c.Red = d.Red) and
  336. (c.Green = d.Green) and
  337. (c.Blue = d.Blue) and
  338. (c.Alpha = d.Alpha);
  339. end;
  340. function GetFullColorData (color:TFPColor) : TColorData;
  341. begin
  342. result := PColorData(@color)^;
  343. end;
  344. function SetFullColorData (color:TColorData) : TFPColor;
  345. begin
  346. result := PFPColor (@color)^;
  347. end;
  348. operator or (const c,d:TFPColor) : TFPColor;
  349. begin
  350. result := SetFullColorData(GetFullColorData(c) OR GetFullColorData(d));
  351. end;
  352. operator and (const c,d:TFPColor) : TFPColor;
  353. begin
  354. result := SetFullColorData(GetFullColorData(c) AND GetFullColorData(d));
  355. end;
  356. operator xor (const c,d:TFPColor) : TFPColor;
  357. begin
  358. result := SetFullColorData(GetFullColorData(c) XOR GetFullColorData(d));
  359. end;
  360. {$ifdef debug}
  361. function MakeHex (n:TColordata;nr:byte): string;
  362. const hexnums : array[0..15] of char =
  363. ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
  364. var r : integer;
  365. begin
  366. result := '';
  367. for r := 0 to nr-1 do
  368. begin
  369. result := hexnums[n and $F] + result;
  370. n := n shr 4;
  371. if ((r+1) mod 4) = 0 then
  372. result := ' ' + result;
  373. end;
  374. end;
  375. {$endif}
  376. initialization
  377. ImageHandlers := TImageHandlersManager.Create;
  378. GrayConvMatrix := GCM_JPEG;
  379. // Following lines are here because the compiler 1.0 can't work with int64 constants
  380. (* ColorBits [cfRGB48,1] := ColorBits [cfRGB48,1] shl 16;
  381. ColorBits [cfRGBA64,1] := ColorBits [cfRGBA64,1] shl 32;
  382. ColorBits [cfRGBA64,2] := ColorBits [cfRGBA64,2] shl 16;
  383. ColorBits [cfABGR64,0] := ColorBits [cfABGR64,0] shl 32;
  384. ColorBits [cfABGR64,3] := ColorBits [cfABGR64,3] shl 16;
  385. ColorBits [cfBGR48,3] := ColorBits [cfBGR48,3] shl 16;
  386. PrepareBitMasks;*)
  387. finalization
  388. ImageHandlers.Free;
  389. end.