2
0

demo1.pas 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. unit demo1;
  2. interface
  3. uses
  4. Windows, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  5. ExtCtrls, StdCtrls, ExtDlgs, lcms2dll, ComCtrls;
  6. type
  7. TForm1 = class(TForm)
  8. Image1: TImage;
  9. Image2: TImage;
  10. Panel1: TPanel;
  11. Splitter1: TSplitter;
  12. Button2: TButton;
  13. ComboBoxInput: TComboBox;
  14. ComboBoxOutput: TComboBox;
  15. Label1: TLabel;
  16. Label2: TLabel;
  17. WBCompensation: TCheckBox;
  18. NoTransform: TCheckBox;
  19. RadioGroup1: TRadioGroup;
  20. OpenPictureDialog1: TOpenPictureDialog;
  21. Button1: TButton;
  22. ProgressBar1: TProgressBar;
  23. ComboBoxIntent: TComboBox;
  24. Label3: TLabel;
  25. Button3: TButton;
  26. Button4: TButton;
  27. OpenDialog1: TOpenDialog;
  28. Label4: TLabel;
  29. ScrollBar1: TScrollBar;
  30. procedure Button2Click(Sender: TObject);
  31. procedure Button1Click(Sender: TObject);
  32. procedure Button3Click(Sender: TObject);
  33. procedure Button4Click(Sender: TObject);
  34. procedure ComboBoxIntentChange(Sender: TObject);
  35. procedure ScrollBar1Change(Sender: TObject);
  36. private
  37. { Private declarations }
  38. function ComputeFlags: DWORD;
  39. public
  40. constructor Create(Owner: TComponent); Override;
  41. { Public declarations }
  42. end;
  43. var
  44. Form1: TForm1;
  45. implementation
  46. {$R *.DFM}
  47. CONST
  48. IS_INPUT = $1;
  49. IS_DISPLAY = $2;
  50. IS_COLORSPACE = $4;
  51. IS_OUTPUT = $8;
  52. IS_ABSTRACT = $10;
  53. VAR
  54. IntentCodes: array [0 .. 20] of cmsUInt32Number;
  55. FUNCTION InSignatures(Signature: cmsProfileClassSignature; dwFlags: DWORD): Boolean;
  56. BEGIN
  57. if (((dwFlags AND IS_DISPLAY) <> 0) AND (Signature = cmsSigDisplayClass)) then
  58. InSignatures := TRUE
  59. else if (((dwFlags AND IS_OUTPUT) <> 0) AND (Signature = cmsSigOutputClass))
  60. then
  61. InSignatures := TRUE
  62. else if (((dwFlags AND IS_INPUT) <> 0) AND (Signature = cmsSigInputClass))
  63. then
  64. InSignatures := TRUE
  65. else if (((dwFlags AND IS_COLORSPACE) <> 0) AND
  66. (Signature = cmsSigColorSpaceClass)) then
  67. InSignatures := TRUE
  68. else if (((dwFlags AND IS_ABSTRACT) <> 0) AND
  69. (Signature = cmsSigAbstractClass)) then
  70. InSignatures := TRUE
  71. else
  72. InSignatures := FALSE
  73. END;
  74. PROCEDURE FillCombo(var Combo: TComboBox; Signatures: DWORD);
  75. var
  76. Files, Descriptions: TStringList;
  77. Found: Integer;
  78. SearchRec: TSearchRec;
  79. Path, Profile: String;
  80. Dir: ARRAY [0 .. 1024] OF Char;
  81. hProfile: cmsHPROFILE;
  82. Descrip: array [0 .. 256] of Char;
  83. begin
  84. Files := TStringList.Create;
  85. Descriptions := TStringList.Create;
  86. GetSystemDirectory(Dir, 1023);
  87. Path := String(Dir) + '\SPOOL\DRIVERS\COLOR\';
  88. Found := FindFirst(Path + '*.ic?', faAnyFile, SearchRec);
  89. while Found = 0 do
  90. begin
  91. Profile := Path + SearchRec.Name;
  92. hProfile := cmsOpenProfileFromFile(PAnsiChar(AnsiString(Profile)), 'r');
  93. if (hProfile <> NIL) THEN
  94. begin
  95. if ((cmsGetColorSpace(hProfile) = cmsSigRgbData) AND InSignatures
  96. (cmsGetDeviceClass(hProfile), Signatures)) then
  97. begin
  98. cmsGetProfileInfo(hProfile, cmsInfoDescription, 'EN', 'us', Descrip,
  99. 256);
  100. Descriptions.Add(Descrip);
  101. Files.Add(Profile);
  102. end;
  103. cmsCloseProfile(hProfile);
  104. end;
  105. Found := FindNext(SearchRec);
  106. end;
  107. FindClose(SearchRec);
  108. Combo.Items := Descriptions;
  109. Combo.Tag := Integer(Files);
  110. end;
  111. // A rather simple Logger... note the "cdecl" convention
  112. PROCEDURE ErrorLogger(ContextID: cmsContext; ErrorCode: cmsUInt32Number;
  113. Text: PAnsiChar); Cdecl;
  114. begin
  115. MessageBox(0, PWideChar(WideString(Text)), 'Something is going wrong...',
  116. MB_OK OR MB_ICONWARNING or MB_TASKMODAL);
  117. end;
  118. constructor TForm1.Create(Owner: TComponent);
  119. var
  120. IntentNames: array [0 .. 20] of PAnsiChar;
  121. i, n: Integer;
  122. begin
  123. inherited Create(Owner);
  124. // Set the logger
  125. cmsSetLogErrorHandler(ErrorLogger);
  126. ScrollBar1.Min := 0;
  127. ScrollBar1.Max := 100;
  128. FillCombo(ComboBoxInput, IS_INPUT OR IS_COLORSPACE OR IS_DISPLAY);
  129. FillCombo(ComboBoxOutput, $FFFF );
  130. // Get the supported intents
  131. n := cmsGetSupportedIntents(20, @IntentCodes, @IntentNames);
  132. ComboBoxIntent.Items.BeginUpdate;
  133. ComboBoxIntent.Items.Clear;
  134. for i:= 0 TO n - 1 DO
  135. ComboBoxIntent.Items.Add(String(IntentNames[i]));
  136. ComboBoxIntent.ItemIndex := 0;
  137. ComboBoxIntent.Items.EndUpdate;
  138. end;
  139. procedure TForm1.ScrollBar1Change(Sender: TObject);
  140. var d: Integer;
  141. s: String;
  142. begin
  143. d := ScrollBar1.Position;
  144. Str(d, s);
  145. Label4.Caption := 'Adaptation state '+s + '% (Abs. col only)';
  146. end;
  147. procedure TForm1.Button2Click(Sender: TObject);
  148. begin
  149. if OpenPictureDialog1.Execute then
  150. begin
  151. Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
  152. Image1.Picture.Bitmap.PixelFormat := pf24bit;
  153. Image2.Picture.LoadFromFile(OpenPictureDialog1.FileName);
  154. Image2.Picture.Bitmap.PixelFormat := pf24bit;
  155. end
  156. end;
  157. function SelectedFile(var Combo: TComboBox): string;
  158. var
  159. List: TStringList;
  160. n: Integer;
  161. begin
  162. List := TStringList(Combo.Tag);
  163. n := Combo.ItemIndex;
  164. if (n >= 0) then
  165. SelectedFile := List.Strings[n]
  166. else
  167. SelectedFile := Combo.Text;
  168. end;
  169. procedure TForm1.ComboBoxIntentChange(Sender: TObject);
  170. begin
  171. ScrollBar1.Enabled := (ComboBoxIntent.itemIndex = 3);
  172. end;
  173. function TForm1.ComputeFlags: DWORD;
  174. var
  175. dwFlags: DWORD;
  176. begin
  177. dwFlags := 0;
  178. if (WBCompensation.Checked) then
  179. begin
  180. dwFlags := dwFlags OR cmsFLAGS_BLACKPOINTCOMPENSATION
  181. end;
  182. if (NoTransform.Checked) then
  183. begin
  184. dwFlags := dwFlags OR cmsFLAGS_NULLTRANSFORM
  185. end;
  186. case RadioGroup1.ItemIndex of
  187. 0:
  188. dwFlags := dwFlags OR cmsFLAGS_NOOPTIMIZE;
  189. 1:
  190. dwFlags := dwFlags OR cmsFLAGS_HIGHRESPRECALC;
  191. 3:
  192. dwFlags := dwFlags OR cmsFLAGS_LOWRESPRECALC;
  193. end;
  194. ComputeFlags := dwFlags
  195. end;
  196. procedure TForm1.Button1Click(Sender: TObject);
  197. var
  198. Source, Dest: String;
  199. hSrc, hDest: cmsHPROFILE;
  200. xform: cmsHTRANSFORM;
  201. i, PicW, PicH: Integer;
  202. Intent: Integer;
  203. dwFlags: DWORD;
  204. begin
  205. Source := SelectedFile(ComboBoxInput);
  206. Dest := SelectedFile(ComboBoxOutput);
  207. dwFlags := ComputeFlags;
  208. Intent := IntentCodes[ComboBoxIntent.ItemIndex];
  209. cmsSetAdaptationState( ScrollBar1.Position / 100.0 );
  210. if (Source <> '') AND (Dest <> '') then
  211. begin
  212. hSrc := cmsOpenProfileFromFile(PAnsiChar(AnsiString(Source)), 'r');
  213. hDest := cmsOpenProfileFromFile(PAnsiChar(AnsiString(Dest)), 'r');
  214. if (hSrc <> Nil) and (hDest <> Nil) then
  215. begin
  216. xform := cmsCreateTransform(hSrc, TYPE_BGR_8, hDest, TYPE_BGR_8, Intent,
  217. dwFlags);
  218. end
  219. else
  220. begin
  221. xform := nil;
  222. end;
  223. if hSrc <> nil then
  224. begin
  225. cmsCloseProfile(hSrc);
  226. end;
  227. if hDest <> Nil then
  228. begin
  229. cmsCloseProfile(hDest);
  230. end;
  231. if (xform <> nil) then
  232. begin
  233. PicW := Image2.Picture.width;
  234. PicH := Image2.Picture.height;
  235. ProgressBar1.Min := 0;
  236. ProgressBar1.Max := PicH;
  237. ProgressBar1.Step := 1;
  238. for i := 0 TO (PicH - 1) do
  239. begin
  240. if ((i MOD 100) = 0) then
  241. ProgressBar1.Position := i;
  242. cmsDoTransform(xform, Image1.Picture.Bitmap.Scanline[i],
  243. Image2.Picture.Bitmap.Scanline[i], PicW);
  244. end;
  245. ProgressBar1.Position := PicH;
  246. cmsDeleteTransform(xform);
  247. end;
  248. Image2.Repaint;
  249. ProgressBar1.Position := 0;
  250. end
  251. end;
  252. procedure TForm1.Button3Click(Sender: TObject);
  253. begin
  254. if OpenDialog1.Execute then
  255. ComboBoxInput.Text := OpenDialog1.FileName;
  256. end;
  257. procedure TForm1.Button4Click(Sender: TObject);
  258. begin
  259. if OpenDialog1.Execute then
  260. ComboBoxOutput.Text := OpenDialog1.FileName;
  261. end;
  262. end.