fpimage.pp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2003 by the Free Pascal development team
  4. fpImage base definitions.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$mode objfpc}{$h+}
  12. unit FPimage;
  13. interface
  14. uses sysutils, classes;
  15. type
  16. TFPCustomImageReader = class;
  17. TFPCustomImageWriter = class;
  18. TFPCustomImage = class;
  19. FPImageException = class (exception);
  20. TFPColor = record
  21. red,green,blue,alpha : word;
  22. end;
  23. PFPColor = ^TFPColor;
  24. TColorFormat = (cfMono,cfGray2,cfGray4,cfGray8,cfGray16,cfGray24,
  25. cfGrayA8,cfGrayA16,cfGrayA32,
  26. cfRGB15,cfRGB16,cfRGB24,cfRGB32,cfRGB48,
  27. cfRGBA8,cfRGBA16,cfRGBA32,cfRGBA64,
  28. cfBGR15,cfBGR16,cfBGR24,cfBGR32,cfBGR48,
  29. cfABGR8,cfABGR16,cfABGR32,cfABGR64);
  30. TColorData = qword;
  31. PColorData = ^TColorData;
  32. TDeviceColor = record
  33. Fmt : TColorFormat;
  34. Data : TColorData;
  35. end;
  36. {$ifdef CPU68K}
  37. { 1.0 m68k cpu compiler does not allow
  38. types larger than 32k....
  39. if we remove range checking all should be fine PM }
  40. TFPColorArray = array [0..0] of TFPColor;
  41. {$R-}
  42. {$else not CPU68K}
  43. TFPColorArray = array [0..(maxint-1) div sizeof(TFPColor)-1] of TFPColor;
  44. {$endif CPU68K}
  45. PFPColorArray = ^TFPColorArray;
  46. TFPImgProgressStage = (psStarting, psRunning, psEnding);
  47. TFPImgProgressEvent = procedure (Sender: TObject; Stage: TFPImgProgressStage;
  48. PercentDone: Byte; RedrawNow: Boolean; const R: TRect;
  49. const Msg: AnsiString; var Continue : Boolean) of object;
  50. // Delphi compatibility
  51. TProgressStage = TFPImgProgressStage;
  52. TProgressEvent = TFPImgProgressEvent;
  53. TFPPalette = class
  54. protected
  55. FData : PFPColorArray;
  56. FCount, FCapacity : integer;
  57. procedure SetCount (Value:integer); virtual;
  58. function GetCount : integer;
  59. procedure SetColor (index:integer; const Value:TFPColor); virtual;
  60. function GetColor (index:integer) : TFPColor;
  61. procedure CheckIndex (index:integer); virtual;
  62. procedure EnlargeData; virtual;
  63. public
  64. constructor Create (ACount : integer);
  65. destructor Destroy; override;
  66. procedure Build (Img : TFPCustomImage); virtual;
  67. procedure Merge (pal : TFPPalette); virtual;
  68. function IndexOf (const AColor: TFPColor) : integer; virtual;
  69. function Add (const Value: TFPColor) : integer; virtual;
  70. procedure Clear; virtual;
  71. property Color [Index : integer] : TFPColor read GetColor write SetColor; default;
  72. property Count : integer read GetCount write SetCount;
  73. end;
  74. TFPCustomImage = class(TPersistent)
  75. private
  76. FOnProgress : TFPImgProgressEvent;
  77. FExtra : TStringlist;
  78. FPalette : TFPPalette;
  79. FHeight, FWidth : integer;
  80. procedure SetHeight (Value : integer);
  81. procedure SetWidth (Value : integer);
  82. procedure SetExtra (const key:String; const AValue:string);
  83. function GetExtra (const key:String) : string;
  84. procedure SetExtraValue (index:integer; const AValue:string);
  85. function GetExtraValue (index:integer) : string;
  86. procedure SetExtraKey (index:integer; const AValue:string);
  87. function GetExtraKey (index:integer) : string;
  88. procedure CheckIndex (x,y:integer);
  89. procedure CheckPaletteIndex (PalIndex:integer);
  90. procedure SetColor (x,y:integer; const Value:TFPColor);
  91. function GetColor (x,y:integer) : TFPColor;
  92. procedure SetPixel (x,y:integer; Value:integer);
  93. function GetPixel (x,y:integer) : integer;
  94. function GetUsePalette : boolean;
  95. protected
  96. // Procedures to store the data. Implemented in descendants
  97. procedure SetInternalColor (x,y:integer; const Value:TFPColor); virtual;
  98. function GetInternalColor (x,y:integer) : TFPColor; virtual;
  99. procedure SetInternalPixel (x,y:integer; Value:integer); virtual; abstract;
  100. function GetInternalPixel (x,y:integer) : integer; virtual; abstract;
  101. procedure SetUsePalette (Value:boolean);virtual;
  102. procedure Progress(Sender: TObject; Stage: TProgressStage;
  103. PercentDone: Byte; RedrawNow: Boolean; const R: TRect;
  104. const Msg: AnsiString; var Continue: Boolean); Virtual;
  105. public
  106. constructor create (AWidth,AHeight:integer); virtual;
  107. destructor destroy; override;
  108. procedure Assign(Source: TPersistent); override;
  109. // Saving and loading
  110. procedure LoadFromStream (Str:TStream; Handler:TFPCustomImageReader);
  111. procedure LoadFromStream (Str:TStream);
  112. procedure LoadFromFile (const filename:String; Handler:TFPCustomImageReader);
  113. procedure LoadFromFile (const filename:String);
  114. procedure SaveToStream (Str:TStream; Handler:TFPCustomImageWriter);
  115. procedure SaveToFile (const filename:String; Handler:TFPCustomImageWriter);
  116. procedure SaveToFile (const filename:String);
  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)-1] 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. StrWriteWithError,
  268. StrNoPaletteAvailable
  269. );
  270. const
  271. // MG: ToDo: move to implementation and add a function to map to resourcestrings
  272. ErrorText : array[TErrorTextIndices] of string =
  273. ('Invalid %s index %d',
  274. 'No image to write',
  275. 'File "%s" does not exist',
  276. 'No stream to write to',
  277. 'palette',
  278. 'horizontal pixel',
  279. 'vertical pixel',
  280. 'extra',
  281. 'Image type "%s" already exists',
  282. 'Image type "%s" already has a reader class',
  283. 'Image type "%s" already has a writer class',
  284. 'Error while determining image type of stream: %s',
  285. 'Can''t determine image type of stream',
  286. 'Error while reading stream: %s',
  287. 'Error while writing stream: %s',
  288. 'No palette available'
  289. );
  290. {$i FPColors.inc}
  291. type
  292. TGrayConvMatrix = record
  293. red, green, blue : single;
  294. end;
  295. var
  296. GrayConvMatrix : TGrayConvMatrix;
  297. const
  298. GCM_NTSC : TGrayConvMatrix = (red:0.299; green:0.587; blue:0.114);
  299. GCM_JPEG : TGrayConvMatrix = (red:0.299; green:0.587; blue:0.114);
  300. GCM_Mathematical : TGrayConvMatrix = (red:0.334; green:0.333; blue:0.333);
  301. GCM_Photoshop : TGrayConvMatrix = (red:0.213; green:0.715; blue:0.072);
  302. function CreateBlackAndWhitePalette : TFPPalette;
  303. function CreateWebSafePalette : TFPPalette;
  304. function CreateGrayScalePalette : TFPPalette;
  305. function CreateVGAPalette : TFPPalette;
  306. implementation
  307. procedure FPImgError (Fmt:TErrorTextIndices; data : array of const);
  308. begin
  309. raise FPImageException.CreateFmt (ErrorText[Fmt],data);
  310. end;
  311. procedure FPImgError (Fmt:TErrorTextIndices);
  312. begin
  313. raise FPImageException.Create (ErrorText[Fmt]);
  314. end;
  315. {$i FPImage.inc}
  316. {$i FPHandler.inc}
  317. {$i FPPalette.inc}
  318. {$i FPColCnv.inc}
  319. function FPColor (r,g,b:word) : TFPColor;
  320. begin
  321. with result do
  322. begin
  323. red := r;
  324. green := g;
  325. blue := b;
  326. alpha := alphaOpaque;
  327. end;
  328. end;
  329. function FPColor (r,g,b,a:word) : TFPColor;
  330. begin
  331. with result do
  332. begin
  333. red := r;
  334. green := g;
  335. blue := b;
  336. alpha := a;
  337. end;
  338. end;
  339. operator = (const c,d:TFPColor) : boolean;
  340. begin
  341. result := (c.Red = d.Red) and
  342. (c.Green = d.Green) and
  343. (c.Blue = d.Blue) and
  344. (c.Alpha = d.Alpha);
  345. end;
  346. function GetFullColorData (color:TFPColor) : TColorData;
  347. begin
  348. result := PColorData(@color)^;
  349. end;
  350. function SetFullColorData (color:TColorData) : TFPColor;
  351. begin
  352. result := PFPColor (@color)^;
  353. end;
  354. operator or (const c,d:TFPColor) : TFPColor;
  355. begin
  356. result := SetFullColorData(GetFullColorData(c) OR GetFullColorData(d));
  357. end;
  358. operator and (const c,d:TFPColor) : TFPColor;
  359. begin
  360. result := SetFullColorData(GetFullColorData(c) AND GetFullColorData(d));
  361. end;
  362. operator xor (const c,d:TFPColor) : TFPColor;
  363. begin
  364. result := SetFullColorData(GetFullColorData(c) XOR GetFullColorData(d));
  365. end;
  366. {$ifdef debug}
  367. function MakeHex (n:TColordata;nr:byte): string;
  368. const hexnums : array[0..15] of char =
  369. ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
  370. var r : integer;
  371. begin
  372. result := '';
  373. for r := 0 to nr-1 do
  374. begin
  375. result := hexnums[n and $F] + result;
  376. n := n shr 4;
  377. if ((r+1) mod 4) = 0 then
  378. result := ' ' + result;
  379. end;
  380. end;
  381. {$endif}
  382. initialization
  383. ImageHandlers := TImageHandlersManager.Create;
  384. GrayConvMatrix := GCM_JPEG;
  385. // Following lines are here because the compiler 1.0 can't work with int64 constants
  386. (* ColorBits [cfRGB48,1] := ColorBits [cfRGB48,1] shl 16;
  387. ColorBits [cfRGBA64,1] := ColorBits [cfRGBA64,1] shl 32;
  388. ColorBits [cfRGBA64,2] := ColorBits [cfRGBA64,2] shl 16;
  389. ColorBits [cfABGR64,0] := ColorBits [cfABGR64,0] shl 32;
  390. ColorBits [cfABGR64,3] := ColorBits [cfABGR64,3] shl 16;
  391. ColorBits [cfBGR48,3] := ColorBits [cfBGR48,3] shl 16;
  392. PrepareBitMasks;*)
  393. finalization
  394. ImageHandlers.Free;
  395. end.