fpimage.pp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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. TFPCustomImageReaderClass = class of TFPCustomImageReader;
  18. TFPCustomImageWriter = class;
  19. TFPCustomImageWriterClass = class of TFPCustomImageWriter;
  20. TIHData = class;
  21. TFPCustomImage = class;
  22. FPImageException = class (exception);
  23. TFPColor = record
  24. red,green,blue,alpha : word;
  25. end;
  26. PFPColor = ^TFPColor;
  27. TColorFormat = (cfMono,cfGray2,cfGray4,cfGray8,cfGray16,cfGray24,
  28. cfGrayA8,cfGrayA16,cfGrayA32,
  29. cfRGB15,cfRGB16,cfRGB24,cfRGB32,cfRGB48,
  30. cfRGBA8,cfRGBA16,cfRGBA32,cfRGBA64,
  31. cfBGR15,cfBGR16,cfBGR24,cfBGR32,cfBGR48,
  32. cfABGR8,cfABGR16,cfABGR32,cfABGR64);
  33. TColorData = qword;
  34. PColorData = ^TColorData;
  35. TDeviceColor = record
  36. Fmt : TColorFormat;
  37. Data : TColorData;
  38. end;
  39. {$ifdef CPU68K}
  40. { 1.0 m68k cpu compiler does not allow
  41. types larger than 32k....
  42. if we remove range checking all should be fine PM }
  43. TFPColorArray = array [0..0] of TFPColor;
  44. {$R-}
  45. {$else not CPU68K}
  46. TFPColorArray = array [0..(maxint-1) div sizeof(TFPColor)-1] of TFPColor;
  47. {$endif CPU68K}
  48. PFPColorArray = ^TFPColorArray;
  49. TFPImgProgressStage = (psStarting, psRunning, psEnding);
  50. TFPImgProgressEvent = procedure (Sender: TObject; Stage: TFPImgProgressStage;
  51. PercentDone: Byte; RedrawNow: Boolean; const R: TRect;
  52. const Msg: AnsiString; var Continue : Boolean) of object;
  53. // Delphi compatibility
  54. TProgressStage = TFPImgProgressStage;
  55. TProgressEvent = TFPImgProgressEvent;
  56. TFPPalette = class
  57. protected
  58. FData : PFPColorArray;
  59. FCount, FCapacity : integer;
  60. procedure SetCount (Value:integer); virtual;
  61. function GetCount : integer;
  62. procedure SetColor (index:integer; const Value:TFPColor); virtual;
  63. function GetColor (index:integer) : TFPColor;
  64. procedure SetCapacity (ind : Integer);
  65. procedure CheckIndex (index:integer); virtual;
  66. procedure EnlargeData; virtual;
  67. public
  68. constructor Create (ACount : integer);
  69. destructor Destroy; override;
  70. procedure Build (Img : TFPCustomImage); virtual;
  71. procedure Copy (APalette: TFPPalette); virtual;
  72. procedure Merge (pal : TFPPalette); virtual;
  73. function IndexOf (const AColor: TFPColor) : integer; virtual;
  74. function Add (const Value: TFPColor) : integer; virtual;
  75. procedure Clear; virtual;
  76. property Color [Index : integer] : TFPColor read GetColor write SetColor; default;
  77. property Count : integer read GetCount write SetCount;
  78. property Capacity : integer read FCapacity write SetCapacity;
  79. end;
  80. TFPCustomImage = class(TPersistent)
  81. private
  82. FOnProgress : TFPImgProgressEvent;
  83. FExtra : TStringlist;
  84. FPalette : TFPPalette;
  85. FHeight, FWidth : integer;
  86. procedure SetHeight (Value : integer);
  87. procedure SetWidth (Value : integer);
  88. procedure SetExtra (const key:String; const AValue:string);
  89. function GetExtra (const key:String) : string;
  90. procedure SetExtraValue (index:integer; const AValue:string);
  91. function GetExtraValue (index:integer) : string;
  92. procedure SetExtraKey (index:integer; const AValue:string);
  93. function GetExtraKey (index:integer) : string;
  94. procedure CheckIndex (x,y:integer);
  95. procedure CheckPaletteIndex (PalIndex:integer);
  96. procedure SetColor (x,y:integer; const Value:TFPColor);
  97. function GetColor (x,y:integer) : TFPColor;
  98. procedure SetPixel (x,y:integer; Value:integer);
  99. function GetPixel (x,y:integer) : integer;
  100. function GetUsePalette : boolean;
  101. protected
  102. // Procedures to store the data. Implemented in descendants
  103. procedure SetInternalColor (x,y:integer; const Value:TFPColor); virtual;
  104. function GetInternalColor (x,y:integer) : TFPColor; virtual;
  105. procedure SetInternalPixel (x,y:integer; Value:integer); virtual; abstract;
  106. function GetInternalPixel (x,y:integer) : integer; virtual; abstract;
  107. procedure SetUsePalette (Value:boolean);virtual;
  108. procedure Progress(Sender: TObject; Stage: TProgressStage;
  109. PercentDone: Byte; RedrawNow: Boolean; const R: TRect;
  110. const Msg: AnsiString; var Continue: Boolean); Virtual;
  111. public
  112. constructor create (AWidth,AHeight:integer); virtual;
  113. destructor destroy; override;
  114. procedure Assign(Source: TPersistent); override;
  115. // Image handlers
  116. class function FindHandlerFromExtension(extension:String): TIHData;
  117. class function FindReaderFromFileName(const filename:String): TFPCustomImageReaderClass;
  118. class function FindReaderFromExtension(const extension:String): TFPCustomImageReaderClass;
  119. class function FindWriterFromFileName(const filename:String): TFPCustomImageWriterClass;
  120. class function FindWriterFromExtension(const extension:String): TFPCustomImageWriterClass;
  121. // Saving and loading
  122. procedure LoadFromStream (Str:TStream; Handler:TFPCustomImageReader);
  123. procedure LoadFromStream (Str:TStream);
  124. procedure LoadFromFile (const filename:String; Handler:TFPCustomImageReader);
  125. function LoadFromFile (const filename:String): Boolean;
  126. procedure SaveToStream (Str:TStream; Handler:TFPCustomImageWriter);
  127. procedure SaveToFile (const filename:String; Handler:TFPCustomImageWriter);
  128. function SaveToFile (const filename:String): Boolean;
  129. // Size and data
  130. procedure SetSize (AWidth, AHeight : integer); virtual;
  131. property Height : integer read FHeight write SetHeight;
  132. property Width : integer read FWidth write SetWidth;
  133. property Colors [x,y:integer] : TFPColor read GetColor write SetColor; default;
  134. // Use of palette for colors
  135. property UsePalette : boolean read GetUsePalette write SetUsePalette;
  136. property Palette : TFPPalette read FPalette;
  137. property Pixels [x,y:integer] : integer read GetPixel write SetPixel;
  138. // Info unrelated with the image representation
  139. property Extra [const key:string] : string read GetExtra write SetExtra;
  140. property ExtraValue [index:integer] : string read GetExtraValue write SetExtraValue;
  141. property ExtraKey [index:integer] : string read GetExtraKey write SetExtraKey;
  142. procedure RemoveExtra (const key:string);
  143. function ExtraCount : integer;
  144. property OnProgress: TFPImgProgressEvent read FOnProgress write FOnProgress;
  145. end;
  146. TFPCustomImageClass = class of TFPCustomImage;
  147. {$ifdef CPU68K}
  148. { 1.0 m68k cpu compiler does not allow
  149. types larger than 32k....
  150. if we remove range checking all should be fine PM }
  151. TFPIntegerArray = array [0..0] of integer;
  152. {$R-}
  153. {$else not CPU68K}
  154. TFPIntegerArray = array [0..(maxint-1) div sizeof(integer)-1] of integer;
  155. {$endif CPU68K}
  156. PFPIntegerArray = ^TFPIntegerArray;
  157. TFPMemoryImage = class (TFPCustomImage)
  158. protected
  159. function GetInternalColor(x,y:integer):TFPColor;override;
  160. procedure SetInternalColor (x,y:integer; const Value:TFPColor);override;
  161. procedure SetUsePalette (Value:boolean);override;
  162. protected
  163. FData : PFPIntegerArray;
  164. procedure SetInternalPixel (x,y:integer; Value:integer); override;
  165. function GetInternalPixel (x,y:integer) : integer; override;
  166. public
  167. constructor create (AWidth,AHeight:integer); override;
  168. destructor destroy; override;
  169. procedure SetSize (AWidth, AHeight : integer); override;
  170. end;
  171. TFPCustomImageHandler = class
  172. private
  173. FOnProgress : TFPImgProgressEvent;
  174. FStream : TStream;
  175. FImage : TFPCustomImage;
  176. protected
  177. procedure Progress(Stage: TProgressStage; PercentDone: Byte; RedrawNow: Boolean; const R: TRect;
  178. const Msg: AnsiString; var Continue: Boolean); Virtual;
  179. property TheStream : TStream read FStream;
  180. property TheImage : TFPCustomImage read FImage;
  181. public
  182. constructor Create; virtual;
  183. Property OnProgress : TFPImgProgressEvent Read FOnProgress Write FOnProgress;
  184. end;
  185. TFPCustomImageReader = class (TFPCustomImageHandler)
  186. private
  187. FDefImageClass:TFPCustomImageClass;
  188. protected
  189. procedure InternalRead (Str:TStream; Img:TFPCustomImage); virtual; abstract;
  190. function InternalCheck (Str:TStream) : boolean; virtual; abstract;
  191. class function InternalSize (Str:TStream): TPoint; virtual;
  192. public
  193. constructor Create; override;
  194. function ImageRead (Str:TStream; Img:TFPCustomImage) : TFPCustomImage;
  195. // reads image
  196. function CheckContents (Str:TStream) : boolean;
  197. // Returns true if the content is readable
  198. class function ImageSize(Str:TStream): TPoint;
  199. // returns the size of image in stream without loading it completely. -1,-1 means this is not implemented.
  200. property DefaultImageClass : TFPCustomImageClass read FDefImageClass write FDefImageClass;
  201. // Image Class to create when no img is given for reading
  202. end;
  203. TFPCustomImageWriter = class (TFPCustomImageHandler)
  204. protected
  205. procedure InternalWrite (Str:TStream; Img:TFPCustomImage); virtual; abstract;
  206. public
  207. procedure ImageWrite (Str:TStream; Img:TFPCustomImage);
  208. // writes given image to stream
  209. end;
  210. TIHData = class
  211. private
  212. FExtension, FTypeName, FDefaultExt : string;
  213. FReader : TFPCustomImageReaderClass;
  214. FWriter : TFPCustomImageWriterClass;
  215. public
  216. property Extension: string read FExtension;
  217. property TypeName: string read FTypeName;
  218. property DefaultExt: string read FDefaultExt;
  219. property Reader : TFPCustomImageReaderClass read FReader;
  220. property Writer : TFPCustomImageWriterClass read FWriter;
  221. end;
  222. TImageHandlersManager = class
  223. private
  224. FData : TList;
  225. function GetReader (const TypeName:string) : TFPCustomImageReaderClass;
  226. function GetWriter (const TypeName:string) : TFPCustomImageWriterClass;
  227. function GetExt (const TypeName:string) : string;
  228. function GetDefExt (const TypeName:string) : string;
  229. function GetTypeName (index:integer) : string;
  230. function GetData (const ATypeName:string) : TIHData;
  231. function GetData (index : integer) : TIHData;
  232. function GetCount : integer;
  233. public
  234. constructor Create;
  235. destructor Destroy; override;
  236. procedure RegisterImageHandlers (const ATypeName,TheExtensions:string;
  237. AReader:TFPCustomImageReaderClass; AWriter:TFPCustomImageWriterClass);
  238. procedure RegisterImageReader (const ATypeName,TheExtensions:string;
  239. AReader:TFPCustomImageReaderClass);
  240. procedure RegisterImageWriter (const ATypeName,TheExtensions:string;
  241. AWriter:TFPCustomImageWriterClass);
  242. procedure UnregisterImageHandlers(const ATypeName: string; ARemoveReader: boolean = True; ARemoveWriter: boolean = True);
  243. property Count : integer read GetCount;
  244. property ImageReader [const TypeName:string] : TFPCustomImageReaderClass read GetReader;
  245. property ImageWriter [const TypeName:string] : TFPCustomImageWriterClass read GetWriter;
  246. property Extensions [const TypeName:string] : string read GetExt;
  247. property DefaultExtension [const TypeName:string] : string read GetDefExt;
  248. property TypeNames [index:integer] : string read GetTypeName;
  249. end;
  250. {function ShiftAndFill (initial:word; CorrectBits:byte):word;
  251. function FillOtherBits (initial:word;CorrectBits:byte):word;
  252. }
  253. function CalculateGray (const From : TFPColor) : word;
  254. (*
  255. function ConvertColor (const From : TDeviceColor) : TFPColor;
  256. function ConvertColor (const From : TColorData; FromFmt:TColorFormat) : TFPColor;
  257. function ConvertColorToData (const From : TFPColor; Fmt : TColorFormat) : TColorData;
  258. function ConvertColorToData (const From : TDeviceColor; Fmt : TColorFormat) : TColorData;
  259. function ConvertColor (const From : TFPColor; Fmt : TColorFormat) : TDeviceColor;
  260. function ConvertColor (const From : TDeviceColor; Fmt : TColorFormat) : TDeviceColor;
  261. *)
  262. function AlphaBlend(color1, color2: TFPColor): TFPColor;
  263. function FPColor (r,g,b,a:word) : TFPColor;
  264. function FPColor (r,g,b:word) : TFPColor;
  265. {$ifdef debug}function MakeHex (n:TColordata;nr:byte): string;{$endif}
  266. operator = (const c,d:TFPColor) : boolean;
  267. operator or (const c,d:TFPColor) : TFPColor;
  268. operator and (const c,d:TFPColor) : TFPColor;
  269. operator xor (const c,d:TFPColor) : TFPColor;
  270. function CompareColors(const Color1, Color2: TFPColor): integer;
  271. var ImageHandlers : TImageHandlersManager;
  272. type
  273. TErrorTextIndices = (
  274. StrInvalidIndex,
  275. StrNoImageToWrite,
  276. StrNoFile,
  277. StrNoStream,
  278. StrPalette,
  279. StrImageX,
  280. StrImageY,
  281. StrImageExtra,
  282. StrTypeAlreadyExist,
  283. StrTypeReaderAlreadyExist,
  284. StrTypeWriterAlreadyExist,
  285. StrCantDetermineType,
  286. StrNoCorrectReaderFound,
  287. StrReadWithError,
  288. StrWriteWithError,
  289. StrNoPaletteAvailable,
  290. StrInvalidHTMLColor
  291. );
  292. const
  293. // MG: ToDo: move to implementation and add a function to map to resourcestrings
  294. ErrorText : array[TErrorTextIndices] of string =
  295. ('Invalid %s index %d',
  296. 'No image to write',
  297. 'File "%s" does not exist',
  298. 'No stream to write to',
  299. 'palette',
  300. 'horizontal pixel',
  301. 'vertical pixel',
  302. 'extra',
  303. 'Image type "%s" already exists',
  304. 'Image type "%s" already has a reader class',
  305. 'Image type "%s" already has a writer class',
  306. 'Error while determining image type of stream: %s',
  307. 'Can''t determine image type of stream',
  308. 'Error while reading stream: %s',
  309. 'Error while writing stream: %s',
  310. 'No palette available',
  311. 'Invalid HTML color : %s'
  312. );
  313. {$i fpcolors.inc}
  314. type
  315. TGrayConvMatrix = record
  316. red, green, blue : single;
  317. end;
  318. var
  319. GrayConvMatrix : TGrayConvMatrix;
  320. const
  321. GCM_NTSC : TGrayConvMatrix = (red:0.299; green:0.587; blue:0.114);
  322. GCM_JPEG : TGrayConvMatrix = (red:0.299; green:0.587; blue:0.114);
  323. GCM_Mathematical : TGrayConvMatrix = (red:0.334; green:0.333; blue:0.333);
  324. GCM_Photoshop : TGrayConvMatrix = (red:0.213; green:0.715; blue:0.072);
  325. function CreateBlackAndWhitePalette : TFPPalette;
  326. function CreateWebSafePalette : TFPPalette;
  327. function CreateGrayScalePalette : TFPPalette;
  328. function CreateVGAPalette : TFPPalette;
  329. Type
  330. TFPCompactImgDesc = record
  331. Gray: boolean; // true = red=green=blue, false: a RGB image
  332. Depth: word; // 8 or 16 bit
  333. HasAlpha: boolean; // has alpha channel
  334. end;
  335. { TFPCompactImgBase }
  336. TFPCompactImgBase = class(TFPCustomImage)
  337. private
  338. FDesc: TFPCompactImgDesc;
  339. public
  340. property Desc: TFPCompactImgDesc read FDesc;
  341. end;
  342. TFPCompactImgBaseClass = class of TFPCompactImgBase;
  343. { TFPCompactImgGray16Bit }
  344. TFPCompactImgGray16Bit = class(TFPCompactImgBase)
  345. protected
  346. FData: PWord;
  347. function GetInternalColor(x, y: integer): TFPColor; override;
  348. function GetInternalPixel({%H-}x, {%H-}y: integer): integer; override;
  349. procedure SetInternalColor (x, y: integer; const Value: TFPColor); override;
  350. procedure SetInternalPixel({%H-}x, {%H-}y: integer; {%H-}Value: integer); override;
  351. public
  352. constructor Create(AWidth, AHeight: integer); override;
  353. destructor Destroy; override;
  354. procedure SetSize(AWidth, AHeight: integer); override;
  355. end;
  356. TFPCompactImgGrayAlpha16BitValue = packed record
  357. g,a: word;
  358. end;
  359. PFPCompactImgGrayAlpha16BitValue = ^TFPCompactImgGrayAlpha16BitValue;
  360. { TFPCompactImgGrayAlpha16Bit }
  361. TFPCompactImgGrayAlpha16Bit = class(TFPCompactImgBase)
  362. protected
  363. FData: PFPCompactImgGrayAlpha16BitValue;
  364. function GetInternalColor(x, y: integer): TFPColor; override;
  365. function GetInternalPixel({%H-}x, {%H-}y: integer): integer; override;
  366. procedure SetInternalColor (x, y: integer; const Value: TFPColor); override;
  367. procedure SetInternalPixel({%H-}x, {%H-}y: integer; {%H-}Value: integer); override;
  368. public
  369. constructor Create(AWidth, AHeight: integer); override;
  370. destructor Destroy; override;
  371. procedure SetSize(AWidth, AHeight: integer); override;
  372. end;
  373. { TFPCompactImgGray8Bit }
  374. TFPCompactImgGray8Bit = class(TFPCompactImgBase)
  375. protected
  376. FData: PByte;
  377. function GetInternalColor(x, y: integer): TFPColor; override;
  378. function GetInternalPixel({%H-}x, {%H-}y: integer): integer; override;
  379. procedure SetInternalColor (x, y: integer; const Value: TFPColor); override;
  380. procedure SetInternalPixel({%H-}x, {%H-}y: integer; {%H-}Value: integer); override;
  381. public
  382. constructor Create(AWidth, AHeight: integer); override;
  383. destructor Destroy; override;
  384. procedure SetSize(AWidth, AHeight: integer); override;
  385. end;
  386. TFPCompactImgGrayAlpha8BitValue = packed record
  387. g,a: byte;
  388. end;
  389. PFPCompactImgGrayAlpha8BitValue = ^TFPCompactImgGrayAlpha8BitValue;
  390. { TFPCompactImgGrayAlpha8Bit }
  391. TFPCompactImgGrayAlpha8Bit = class(TFPCompactImgBase)
  392. protected
  393. FData: PFPCompactImgGrayAlpha8BitValue;
  394. function GetInternalColor(x, y: integer): TFPColor; override;
  395. function GetInternalPixel({%H-}x, {%H-}y: integer): integer; override;
  396. procedure SetInternalColor (x, y: integer; const Value: TFPColor); override;
  397. procedure SetInternalPixel({%H-}x, {%H-}y: integer; {%H-}Value: integer); override;
  398. public
  399. constructor Create(AWidth, AHeight: integer); override;
  400. destructor Destroy; override;
  401. procedure SetSize(AWidth, AHeight: integer); override;
  402. end;
  403. TFPCompactImgRGBA8BitValue = packed record
  404. r,g,b,a: byte;
  405. end;
  406. PFPCompactImgRGBA8BitValue = ^TFPCompactImgRGBA8BitValue;
  407. { TFPCompactImgRGBA8Bit }
  408. TFPCompactImgRGBA8Bit = class(TFPCompactImgBase)
  409. protected
  410. FData: PFPCompactImgRGBA8BitValue;
  411. function GetInternalColor(x, y: integer): TFPColor; override;
  412. function GetInternalPixel({%H-}x, {%H-}y: integer): integer; override;
  413. procedure SetInternalColor (x, y: integer; const Value: TFPColor); override;
  414. procedure SetInternalPixel({%H-}x, {%H-}y: integer; {%H-}Value: integer); override;
  415. public
  416. constructor Create(AWidth, AHeight: integer); override;
  417. destructor Destroy; override;
  418. procedure SetSize(AWidth, AHeight: integer); override;
  419. end;
  420. TFPCompactImgRGB8BitValue = packed record
  421. r,g,b: byte;
  422. end;
  423. PFPCompactImgRGB8BitValue = ^TFPCompactImgRGB8BitValue;
  424. { TFPCompactImgRGB8Bit }
  425. TFPCompactImgRGB8Bit = class(TFPCompactImgBase)
  426. protected
  427. FData: PFPCompactImgRGB8BitValue;
  428. function GetInternalColor(x, y: integer): TFPColor; override;
  429. function GetInternalPixel({%H-}x, {%H-}y: integer): integer; override;
  430. procedure SetInternalColor (x, y: integer; const Value: TFPColor); override;
  431. procedure SetInternalPixel({%H-}x, {%H-}y: integer; {%H-}Value: integer); override;
  432. public
  433. constructor Create(AWidth, AHeight: integer); override;
  434. destructor Destroy; override;
  435. procedure SetSize(AWidth, AHeight: integer); override;
  436. end;
  437. TFPCompactImgRGB16BitValue = packed record
  438. r,g,b: word;
  439. end;
  440. PFPCompactImgRGB16BitValue = ^TFPCompactImgRGB16BitValue;
  441. { TFPCompactImgRGB16Bit }
  442. TFPCompactImgRGB16Bit = class(TFPCompactImgBase)
  443. protected
  444. FData: PFPCompactImgRGB16BitValue;
  445. function GetInternalColor(x, y: integer): TFPColor; override;
  446. function GetInternalPixel({%H-}x, {%H-}y: integer): integer; override;
  447. procedure SetInternalColor (x, y: integer; const Value: TFPColor); override;
  448. procedure SetInternalPixel({%H-}x, {%H-}y: integer; {%H-}Value: integer); override;
  449. public
  450. constructor Create(AWidth, AHeight: integer); override;
  451. destructor Destroy; override;
  452. procedure SetSize(AWidth, AHeight: integer); override;
  453. end;
  454. { TFPCompactImgRGBA16Bit }
  455. TFPCompactImgRGBA16Bit = class(TFPCompactImgBase)
  456. protected
  457. FData: PFPColor;
  458. function GetInternalColor(x, y: integer): TFPColor; override;
  459. function GetInternalPixel({%H-}x, {%H-}y: integer): integer; override;
  460. procedure SetInternalColor (x, y: integer; const Value: TFPColor); override;
  461. procedure SetInternalPixel({%H-}x, {%H-}y: integer; {%H-}Value: integer); override;
  462. public
  463. constructor Create(AWidth, AHeight: integer); override;
  464. destructor Destroy; override;
  465. procedure SetSize(AWidth, AHeight: integer); override;
  466. end;
  467. { Create a descriptor to select a CompactImg class }
  468. function GetFPCompactImgDesc(Gray: boolean; Depth: word; HasAlpha: boolean): TFPCompactImgDesc;
  469. { Returns a CompactImg class that fits the descriptor }
  470. function GetFPCompactImgClass(const Desc: TFPCompactImgDesc): TFPCompactImgBaseClass;
  471. { Create a CompactImg with the descriptor }
  472. function CreateFPCompactImg(const Desc: TFPCompactImgDesc; Width, Height: integer): TFPCustomImage;
  473. { Create a CompactImg with the same features as Img.
  474. If Img is a TFPCompactImgBaseClass it will create that.
  475. Otherwise it returns a CompactImg that fits the Img using GetMinimumPTDesc. }
  476. function CreateCompatibleFPCompactImg(Img: TFPCustomImage; Width, Height: integer
  477. ): TFPCustomImage;
  478. { As CreateCompatibleFPCompactImg, but the image has always an alpha channel. }
  479. function CreateCompatibleFPCompactImgWithAlpha(Img: TFPCustomImage;
  480. Width, Height: integer): TFPCustomImage;
  481. { Returns the smallest descriptor that allows to store the Img.
  482. It returns HasAlpha=false if all pixel are opaque.
  483. It returns Gray=true if all red=green=blue.
  484. It returns Depth=8 if all lo byte equals the hi byte or all lo bytes are 0.
  485. To ignore rounding errors you can pass a FuzzyDepth. For example a FuzzyDepth
  486. of 3 ignores the lower 3 bits when comparing. }
  487. function GetMinimumPTDesc(Img: TFPCustomImage; FuzzyDepth: word = 4): TFPCompactImgDesc;
  488. { Create a smaller CompactImg with the same information as Img.
  489. Pass FreeImg=true to call Img.Free }
  490. function GetMinimumFPCompactImg(Img: TFPCustomImage; FreeImg: boolean;
  491. FuzzyDepth: word = 4): TFPCustomImage;
  492. { HTML Color support. RRGGBB or color name. Only W3 color names s are supported}
  493. function TryHtmlToFPColor(const S: String; out FPColor: TFPColor): Boolean;
  494. function HtmlToFPColorDef(const S: String; out FPColor: TFpColor; Def: TFPColor): TFPColor;
  495. function HtmlToFpColor(const S: String): TFPColor;
  496. implementation
  497. procedure FPImgError (Fmt:TErrorTextIndices; data : array of const);
  498. begin
  499. raise FPImageException.CreateFmt (ErrorText[Fmt],data);
  500. end;
  501. procedure FPImgError (Fmt:TErrorTextIndices);
  502. begin
  503. raise FPImageException.Create (ErrorText[Fmt]);
  504. end;
  505. {$i FPImage.inc}
  506. {$i FPHandler.inc}
  507. {$i FPPalette.inc}
  508. {$i FPColCnv.inc}
  509. {$i fpcompactimg.inc}
  510. function FPColor (r,g,b:word) : TFPColor;
  511. begin
  512. with result do
  513. begin
  514. red := r;
  515. green := g;
  516. blue := b;
  517. alpha := alphaOpaque;
  518. end;
  519. end;
  520. function FPColor (r,g,b,a:word) : TFPColor;
  521. begin
  522. with result do
  523. begin
  524. red := r;
  525. green := g;
  526. blue := b;
  527. alpha := a;
  528. end;
  529. end;
  530. operator = (const c,d:TFPColor) : boolean;
  531. begin
  532. result := (c.Red = d.Red) and
  533. (c.Green = d.Green) and
  534. (c.Blue = d.Blue) and
  535. (c.Alpha = d.Alpha);
  536. end;
  537. function GetFullColorData (color:TFPColor) : TColorData;
  538. begin
  539. result := PColorData(@color)^;
  540. end;
  541. function SetFullColorData (color:TColorData) : TFPColor;
  542. begin
  543. result := PFPColor (@color)^;
  544. end;
  545. operator or (const c,d:TFPColor) : TFPColor;
  546. begin
  547. result := SetFullColorData(GetFullColorData(c) OR GetFullColorData(d));
  548. end;
  549. operator and (const c,d:TFPColor) : TFPColor;
  550. begin
  551. result := SetFullColorData(GetFullColorData(c) AND GetFullColorData(d));
  552. end;
  553. operator xor (const c,d:TFPColor) : TFPColor;
  554. begin
  555. result := SetFullColorData(GetFullColorData(c) XOR GetFullColorData(d));
  556. end;
  557. {$ifdef debug}
  558. function MakeHex (n:TColordata;nr:byte): string;
  559. const hexnums : array[0..15] of char =
  560. ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
  561. var r : integer;
  562. begin
  563. result := '';
  564. for r := 0 to nr-1 do
  565. begin
  566. result := hexnums[n and $F] + result;
  567. n := n shr 4;
  568. if ((r+1) mod 4) = 0 then
  569. result := ' ' + result;
  570. end;
  571. end;
  572. {$endif}
  573. type
  574. THtmlColorName = (
  575. hcnWhite, hcnSilver, hcnGray, hcnBlack,
  576. hcnRed, hcnMaroon, hcnYellow, hcnOlive,
  577. hcnLime, hcnGreen, hcnAqua, hcnTeal, hcnBlue,
  578. hcnNavy, hcnFuchsia, hcnPurple);
  579. const
  580. HtmlColorNameToFPColorMap: array[THtmlColorName] of TFPColor = (
  581. (red: $ff; green: $ff; blue: $ff; alpha: alphaOpaque), //hcnWhite
  582. (red: $c0; green: $c0; blue: $c0; alpha: alphaOpaque), //hcnSilver
  583. (red: $80; green: $80; blue: $80; alpha: alphaOpaque), //hcnGray
  584. (red: $00; green: $00; blue: $00; alpha: alphaOpaque), //hcnBlack
  585. (red: $ff; green: $00; blue: $00; alpha: alphaOpaque), //hcnRed
  586. (red: $80; green: $00; blue: $00; alpha: alphaOpaque), //hcnMaroon
  587. (red: $ff; green: $ff; blue: $00; alpha: alphaOpaque), //hcnYellow
  588. (red: $80; green: $80; blue: $00; alpha: alphaOpaque), //hcnOlive
  589. (red: $00; green: $ff; blue: $00; alpha: alphaOpaque), //hcnLime
  590. (red: $00; green: $80; blue: $00; alpha: alphaOpaque), //hcnGreen
  591. (red: $00; green: $ff; blue: $ff; alpha: alphaOpaque), //hcnAqua
  592. (red: $00; green: $80; blue: $80; alpha: alphaOpaque), //hcnTeal
  593. (red: $00; green: $00; blue: $ff; alpha: alphaOpaque), //hcnBlue
  594. (red: $00; green: $00; blue: $80; alpha: alphaOpaque), //hcnNavy
  595. (red: $ff; green: $00; blue: $ff; alpha: alphaOpaque), //hcnFuchsia
  596. (red: $80; green: $00; blue: $80; alpha: alphaOpaque) //hcnPurple
  597. );
  598. function TryStrToHtmlColorName(const S: String; out AName: THtmlColorName): Boolean;
  599. begin
  600. Result := True;
  601. case LowerCase(S) of
  602. 'white' : AName := hcnWhite;
  603. 'silver' : AName := hcnSilver;
  604. 'gray' : AName := hcnGray;
  605. 'black' : AName := hcnBlack;
  606. 'red' : AName := hcnRed;
  607. 'maroon' : AName := hcnMaroon;
  608. 'yellow' : AName := hcnYellow;
  609. 'olive' : AName := hcnOlive;
  610. 'lime' : AName := hcnLime;
  611. 'green' : AName := hcnGreen;
  612. 'aqua' : AName := hcnAqua;
  613. 'teal' : AName := hcnTeal;
  614. 'blue' : AName := hcnBlue;
  615. 'navy' : AName := hcnNavy;
  616. 'fuchsia': AName := hcnFuchsia;
  617. 'purple' : AName := hcnPurple;
  618. else
  619. Result := False;
  620. end;
  621. end;
  622. { Try to translate HTML color code into TFPColor
  623. Supports following formats
  624. '#rgb'
  625. '#rrggbb'
  626. W3C Html color name
  627. }
  628. function TryHtmlToFPColor(const S: String; out FPColor: TFPColor): Boolean;
  629. function TryHexStrToWord(const Hex: String; out W: Word): Boolean;
  630. var
  631. Code: Integer;
  632. begin
  633. Val('$'+Hex, W, Code);
  634. Result := (Code = 0);
  635. if not Result then W := 0;
  636. end;
  637. var
  638. AName: THtmlColorName;
  639. begin
  640. Result := False;
  641. FPColor.red := 0;
  642. FPColor.green := 0;
  643. FPColor.blue := 0;
  644. FPColor.alpha := alphaOpaque;
  645. if (Length(S) = 0) then
  646. Exit;
  647. if (S[1] = '#') then
  648. begin
  649. if Length(S) = 4 then
  650. begin // #rgb
  651. Result := (TryHexstrToWord(S[2]+S[2], FPColor.red) and
  652. TryHexstrToWord(S[3]+S[3], FPColor.green) and
  653. TryHexstrToWord(S[4]+S[4], FPColor.blue));
  654. end
  655. else if Length(S) = 7 then
  656. begin // #rrggbb
  657. Result := (TryHexstrToWord(S[2]+S[3], FPColor.red) and
  658. TryHexstrToWord(S[4]+S[5], FPColor.green) and
  659. TryHexstrToWord(S[6]+S[7], FPColor.blue));
  660. end;
  661. end
  662. else
  663. begin
  664. Result := TryStrToHtmlColorName(S, AName);
  665. if Result then
  666. FPColor := HtmlColorNameToFPColorMap[AName];
  667. end;
  668. end;
  669. function HtmlToFPColorDef(const S: String; out FPColor: TFpColor; Def: TFPColor): TFPColor;
  670. begin
  671. if not TryHtmlToFPColor(S, Result) then
  672. Result := Def;
  673. end;
  674. function HtmlToFpColor(const S: String): TFPColor;
  675. begin
  676. if not TryHtmlToFpColor(S, Result) then
  677. raise EConvertError.CreateFmt(ErrorText[StrInvalidHTMLColor], [S]);
  678. end;
  679. initialization
  680. ImageHandlers := TImageHandlersManager.Create;
  681. GrayConvMatrix := GCM_JPEG;
  682. // Following lines are here because the compiler 1.0 can't work with int64 constants
  683. (* ColorBits [cfRGB48,1] := ColorBits [cfRGB48,1] shl 16;
  684. ColorBits [cfRGBA64,1] := ColorBits [cfRGBA64,1] shl 32;
  685. ColorBits [cfRGBA64,2] := ColorBits [cfRGBA64,2] shl 16;
  686. ColorBits [cfABGR64,0] := ColorBits [cfABGR64,0] shl 32;
  687. ColorBits [cfABGR64,3] := ColorBits [cfABGR64,3] shl 16;
  688. ColorBits [cfBGR48,3] := ColorBits [cfBGR48,3] shl 16;
  689. PrepareBitMasks;*)
  690. finalization
  691. ImageHandlers.Free;
  692. end.