fTTBMainD.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. unit fTTBMainD;
  2. interface
  3. uses
  4. Winapi.Windows,
  5. System.SysUtils,
  6. System.UITypes,
  7. System.Classes,
  8. System.Actions,
  9. System.ImageList,
  10. Vcl.Graphics,
  11. Vcl.Controls,
  12. Vcl.Forms,
  13. Vcl.Dialogs,
  14. Vcl.ExtCtrls,
  15. Vcl.ExtDlgs,
  16. Vcl.StdCtrls,
  17. Vcl.ComCtrls,
  18. Vcl.ActnList,
  19. Vcl.ToolWin,
  20. Vcl.Menus,
  21. Vcl.ImgList,
  22. Vcl.Imaging.Jpeg,
  23. GLS.Objects,
  24. GLS.HUDObjects,
  25. GLS.Scene,
  26. GLS.SceneViewer,
  27. GLS.FileTGA,
  28. GLS.Coordinates,
  29. GLS.BaseClasses,
  30. GLS.Texture,
  31. GLS.Graphics,
  32. GLS.VectorGeometry,
  33. GLS.State,
  34. GLS.Utils;
  35. type
  36. TTTBMain = class(TForm)
  37. MainMenu: TMainMenu;
  38. ImageList: TImageList;
  39. ActionList: TActionList;
  40. File1: TMenuItem;
  41. ACExit: TAction;
  42. Exit1: TMenuItem;
  43. PAImages: TPanel;
  44. PAPreview: TPanel;
  45. Splitter1: TSplitter;
  46. PageControl: TPageControl;
  47. TSRGB: TTabSheet;
  48. TSAlpha: TTabSheet;
  49. GLSceneViewer: TGLSceneViewer;
  50. GLScene: TGLScene;
  51. GLCamera: TGLCamera;
  52. GLDummyCube: TGLDummyCube;
  53. GLCube: TGLCube;
  54. GLLightSource: TGLLightSource;
  55. HSBkgnd: TGLHUDSprite;
  56. ToolBar: TToolBar;
  57. ACImport: TAction;
  58. ACOpenTexture: TAction;
  59. TBImport: TToolButton;
  60. ScrollBox1: TScrollBox;
  61. IMRGB: TImage;
  62. ScrollBox2: TScrollBox;
  63. IMAlpha: TImage;
  64. OpenPictureDialog: TOpenPictureDialog;
  65. Panel1: TPanel;
  66. Label1: TLabel;
  67. CBWidth: TComboBox;
  68. Label2: TLabel;
  69. CBHeight: TComboBox;
  70. N1: TMenuItem;
  71. Exit2: TMenuItem;
  72. ACSaveTexture: TAction;
  73. SaveDialog: TSaveDialog;
  74. SaveTexture1: TMenuItem;
  75. Panel2: TPanel;
  76. CBTextureFiltering: TCheckBox;
  77. CBBackground: TComboBox;
  78. ools1: TMenuItem;
  79. ACColorDilatation: TAction;
  80. Colormapdilatation1: TMenuItem;
  81. ACAlphaErosion: TAction;
  82. Alphamaperosion1: TMenuItem;
  83. ACExport: TAction;
  84. ToolButton1: TToolButton;
  85. N2: TMenuItem;
  86. ACAlphaDilatation: TAction;
  87. AlphamapDilatation1: TMenuItem;
  88. GenerateAlpha1: TMenuItem;
  89. ACAlphaSuperBlack: TAction;
  90. Alpha1: TMenuItem;
  91. SuperBlackTransparent1: TMenuItem;
  92. ACOpaque: TAction;
  93. ACFromRGBIntensity: TAction;
  94. ACFromRGBSqrtIntensity: TAction;
  95. Opaque1: TMenuItem;
  96. FromRGBIntensity1: TMenuItem;
  97. FromRGBSqrtIntensity1: TMenuItem;
  98. ACAlphaOffset: TAction;
  99. Offset1: TMenuItem;
  100. ACAlphaSaturate: TAction;
  101. Saturate1: TMenuItem;
  102. ACAlphaNegate: TAction;
  103. Negate1: TMenuItem;
  104. N3: TMenuItem;
  105. procedure PAPreviewResize(Sender: TObject);
  106. procedure FormShow(Sender: TObject);
  107. procedure ACImportExecute(Sender: TObject);
  108. procedure CBWidthChange(Sender: TObject);
  109. procedure CBTextureFilteringClick(Sender: TObject);
  110. procedure FormCreate(Sender: TObject);
  111. procedure ACOpenTextureExecute(Sender: TObject);
  112. procedure ACSaveTextureExecute(Sender: TObject);
  113. procedure GLSceneViewerMouseDown(Sender: TObject; Button: TMouseButton;
  114. Shift: TShiftState; X, Y: Integer);
  115. procedure GLSceneViewerMouseMove(Sender: TObject; Shift: TShiftState;
  116. X, Y: Integer);
  117. procedure CBBackgroundChange(Sender: TObject);
  118. procedure ACColorDilatationExecute(Sender: TObject);
  119. procedure ACAlphaErosionExecute(Sender: TObject);
  120. procedure ACExportExecute(Sender: TObject);
  121. procedure ACAlphaDilatationExecute(Sender: TObject);
  122. procedure ACAlphaSuperBlackExecute(Sender: TObject);
  123. procedure ACOpaqueExecute(Sender: TObject);
  124. procedure ACFromRGBIntensityExecute(Sender: TObject);
  125. procedure ACFromRGBSqrtIntensityExecute(Sender: TObject);
  126. procedure ACAlphaOffsetExecute(Sender: TObject);
  127. procedure ACAlphaSaturateExecute(Sender: TObject);
  128. procedure ACAlphaNegateExecute(Sender: TObject);
  129. private
  130. mx, my : Integer;
  131. procedure ResetAlpha;
  132. procedure GenerateAlpha(transparentColor : TColor;
  133. fromIntensity : Boolean;
  134. doSqrt : Boolean);
  135. function SpawnBitmap : TBitmap;
  136. procedure ResizeImage(im : TImage);
  137. procedure NormalizeAlpha;
  138. procedure TextureChanged;
  139. procedure BreakupTexture(bmp : TBitmap);
  140. public
  141. end;
  142. var
  143. TTBMain: TTTBMain;
  144. implementation
  145. {$R *.dfm}
  146. procedure TTTBMain.FormCreate(Sender: TObject);
  147. begin
  148. CBWidth.ItemIndex:=8;
  149. CBHeight.ItemIndex:=8;
  150. ResetAlpha;
  151. end;
  152. procedure TTTBMain.FormShow(Sender: TObject);
  153. begin
  154. PAPreviewResize(Self);
  155. end;
  156. procedure TTTBMain.PAPreviewResize(Sender: TObject);
  157. const
  158. cTileSize = 32;
  159. var
  160. w, h : Integer;
  161. begin
  162. // adjust background, we could just have made huge one,
  163. // but that would have been too simple for a demo ;)
  164. w:=(GLSceneViewer.Width div cTileSize);
  165. h:=(GLSceneViewer.Height div cTileSize);
  166. HSBkgnd.XTiles:=w;
  167. HSBkgnd.YTiles:=h;
  168. w:=w*cTileSize+cTileSize;
  169. h:=h*cTileSize+cTileSize;
  170. HSBkgnd.Width:=w;
  171. HSBkgnd.Height:=h;
  172. HSBkgnd.Position.SetPoint(w div 2, h div 2, 0);
  173. // zoom scene with viewer's width
  174. GLCamera.SceneScale:=GLSceneViewer.Width/120;
  175. end;
  176. procedure TTTBMain.ACImportExecute(Sender: TObject);
  177. begin
  178. if OpenPictureDialog.Execute then begin
  179. if PageControl.ActivePage=TSRGB then begin
  180. IMRGB.Picture.LoadFromFile(OpenPictureDialog.FileName);
  181. ResizeImage(IMRGB);
  182. end else begin
  183. IMAlpha.Picture.LoadFromFile(OpenPictureDialog.FileName);
  184. ResizeImage(IMAlpha);
  185. NormalizeAlpha;
  186. end;
  187. TextureChanged;
  188. end;
  189. end;
  190. procedure TTTBMain.ACExportExecute(Sender: TObject);
  191. begin
  192. if SaveDialog.Execute then begin
  193. if PageControl.ActivePage=TSRGB then
  194. IMRGB.Picture.SaveToFile(SaveDialog.FileName)
  195. else IMAlpha.Picture.SaveToFile(SaveDialog.FileName);
  196. end;
  197. end;
  198. function TTTBMain.SpawnBitmap : TBitmap;
  199. begin
  200. Result:=TBitmap.Create;
  201. Result.PixelFormat:=pf32bit;
  202. Result.Width:=StrToInt(CBWidth.Text);
  203. Result.Height:=StrToInt(CBHeight.Text);
  204. end;
  205. procedure TTTBMain.ResetAlpha;
  206. var
  207. bmp : TBitmap;
  208. begin
  209. // Opaque alpha channel
  210. bmp:=SpawnBitmap;
  211. try
  212. with bmp.Canvas do begin
  213. Brush.Color:=clWhite;
  214. FillRect(Rect(0, 0, bmp.Width, bmp.Height));
  215. end;
  216. IMAlpha.Picture.Bitmap:=bmp;
  217. finally
  218. bmp.Free;
  219. end;
  220. end;
  221. procedure TTTBMain.GenerateAlpha(transparentColor : TColor;
  222. fromIntensity : Boolean;
  223. doSqrt : Boolean);
  224. var
  225. bmp : TBitmap;
  226. bmp32 : TGLBitmap32;
  227. x, y : Integer;
  228. pSrc : PGLPixel32Array;
  229. pDest : PIntegerArray;
  230. c : Integer;
  231. begin
  232. // Opaque alpha channel
  233. bmp:=SpawnBitmap;
  234. bmp32:=TGLBitmap32.Create;
  235. GLSceneViewer.Buffer.RenderingContext.Activate;
  236. try
  237. bmp.Canvas.StretchDraw(Rect(0, 0, bmp.Width, bmp.Height), IMRGB.Picture.Graphic);
  238. bmp32.Assign(bmp);
  239. if transparentColor<>-1 then
  240. bmp32.SetAlphaTransparentForColor(transparentColor);
  241. if fromIntensity then
  242. bmp32.SetAlphaFromIntensity;
  243. if doSqrt then
  244. bmp32.SqrtAlpha;
  245. for y:=0 to bmp.Height-1 do begin
  246. pSrc:=bmp32.ScanLine[y];
  247. pDest:=bmp.ScanLine[bmp.Height-1-y];
  248. for x:=0 to bmp.Width-1 do begin
  249. c:=pSrc[x].a;
  250. c:=c+(c shl 8)+(c shl 16);
  251. pDest[x]:=c;
  252. end;
  253. end;
  254. IMAlpha.Picture.Graphic:=bmp;
  255. finally
  256. bmp32.Free;
  257. bmp.Free;
  258. GLSceneViewer.Buffer.RenderingContext.Deactivate;
  259. end;
  260. TextureChanged;
  261. end;
  262. procedure TTTBMain.NormalizeAlpha;
  263. var
  264. col : Byte;
  265. x, y, c : Integer;
  266. bmp : TBitmap;
  267. pSrc, pDest : PIntegerArray;
  268. begin
  269. GLSceneViewer.Buffer.RenderingContext.Activate;
  270. bmp:=SpawnBitmap;
  271. try
  272. for y:=0 to bmp.Height-1 do begin
  273. pSrc:=IMAlpha.Picture.Bitmap.ScanLine[y];
  274. pDest:=bmp.ScanLine[y];
  275. for x:=0 to bmp.Width-1 do begin
  276. c:=pSrc[x];
  277. col:=Round(0.3*(c and $FF)+0.59*((c shr 8) and $FF)+0.11*((c shr 16) and $FF));
  278. pDest[x]:=col+(col shl 8)+(col shl 16);
  279. end;
  280. end;
  281. IMAlpha.Picture.Bitmap:=bmp;
  282. finally
  283. bmp.Free;
  284. GLSceneViewer.Buffer.RenderingContext.Deactivate;
  285. end;
  286. end;
  287. procedure TTTBMain.ResizeImage(im : TImage);
  288. var
  289. bmp : TBitmap;
  290. begin
  291. if im.Height=0 then Exit;
  292. bmp:=SpawnBitmap;
  293. try
  294. bmp.Canvas.StretchDraw(Rect(0, 0, bmp.Width, bmp.Height), im.Picture.Graphic);
  295. im.Picture.Bitmap:=bmp;
  296. finally
  297. bmp.Free;
  298. end;
  299. end;
  300. procedure TTTBMain.BreakupTexture(bmp : TBitmap);
  301. var
  302. bmpAlpha, bmpRGB : TBitmap;
  303. y, x, c : Integer;
  304. pRGB, pAlpha, pSrc : PIntegerArray;
  305. begin
  306. bmpAlpha:=SpawnBitmap;
  307. bmpRGB:=SpawnBitmap;
  308. try
  309. bmpAlpha.Width:=bmp.Width;
  310. bmpAlpha.Height:=bmp.Height;
  311. bmpRGB.Width:=bmp.Width;
  312. bmpRGB.Height:=bmp.Height;
  313. for y:=0 to bmp.Height-1 do begin
  314. pRGB:=bmpRGB.ScanLine[y];
  315. pAlpha:=bmpAlpha.ScanLine[y];
  316. pSrc:=bmp.ScanLine[y];
  317. for x:=0 to bmp.Width-1 do begin
  318. c:=pSrc[x];
  319. pRGB[x]:=(c and $FFFFFF);
  320. c:=(c shr 24) and $FF;
  321. pAlpha[x]:=c+(c shl 8)+(c shl 16);
  322. end;
  323. end;
  324. IMRGB.Picture.Bitmap:=bmpRGB;
  325. IMAlpha.Picture.Bitmap:=bmpAlpha;
  326. finally
  327. bmpAlpha.Free;
  328. bmpRGB.Free;
  329. end;
  330. end;
  331. procedure TTTBMain.TextureChanged;
  332. var
  333. bmp : TBitmap;
  334. y, x : Integer;
  335. pRGB, pAlpha, pDest : PIntegerArray;
  336. begin
  337. if IMRGB.Picture.Graphic.Empty then Exit;
  338. if IMAlpha.Picture.Height=0 then begin
  339. GLCube.Material.Texture.Assign(IMRGB.Picture);
  340. end else begin
  341. bmp:=SpawnBitmap;
  342. try
  343. for y:=0 to bmp.Height-1 do begin
  344. pRGB:=IMRGB.Picture.Bitmap.ScanLine[y];
  345. pAlpha:=IMAlpha.Picture.Bitmap.ScanLine[y];
  346. pDest:=bmp.ScanLine[y];
  347. for x:=0 to bmp.Width-1 do
  348. pDest[x]:=pRGB[x] or ((pAlpha[x] and $FF) shl 24);
  349. end;
  350. GLCube.Material.Texture.Assign(bmp);
  351. finally
  352. bmp.Free;
  353. end;
  354. end;
  355. end;
  356. procedure TTTBMain.CBWidthChange(Sender: TObject);
  357. begin
  358. ResizeImage(IMRGB);
  359. ResizeImage(IMAlpha);
  360. end;
  361. procedure TTTBMain.CBTextureFilteringClick(Sender: TObject);
  362. begin
  363. with GLCube.Material.Texture do begin
  364. if CBTextureFiltering.Checked then begin
  365. MagFilter:=maLinear;
  366. MinFilter:=miLinearMipmapLinear;
  367. end else begin
  368. MagFilter:=maNearest;
  369. MinFilter:=miNearest;
  370. end;
  371. end;
  372. end;
  373. procedure TTTBMain.ACOpenTextureExecute(Sender: TObject);
  374. var
  375. pic : TPicture;
  376. begin
  377. if OpenPictureDialog.Execute then begin
  378. pic:=TPicture.Create;
  379. try
  380. pic.LoadFromFile(OpenPictureDialog.FileName);
  381. if (pic.Graphic is TBitmap) and (pic.Bitmap.PixelFormat=pf32bit) then begin
  382. BreakupTexture(pic.Bitmap);
  383. ResizeImage(IMAlpha);
  384. end else begin
  385. IMRGB.Picture:=pic;
  386. ResetAlpha;
  387. end;
  388. ResizeImage(IMRGB);
  389. TextureChanged;
  390. finally
  391. pic.Free;
  392. end;
  393. end;
  394. end;
  395. procedure TTTBMain.ACSaveTextureExecute(Sender: TObject);
  396. var
  397. pic : TPicture;
  398. fName : String;
  399. tga : TTGAImage;
  400. begin
  401. pic:=(GLCube.Material.Texture.Image as TGLPictureImage).Picture;
  402. if (pic.Height>0) and SaveDialog.Execute then begin
  403. fName:=SaveDialog.FileName;
  404. if ExtractFileExt(fName)='' then
  405. if SaveDialog.FilterIndex=1 then
  406. fName:=fName+'.bmp'
  407. else fName:=fName+'.tga';
  408. if LowerCase(ExtractFileExt(fName))='.tga' then begin
  409. tga:=TTGAImage.Create;
  410. try
  411. tga.Assign(pic.Bitmap);
  412. tga.SaveToFile(fName)
  413. finally
  414. tga.Free;
  415. end;
  416. end else pic.SaveToFile(fName);
  417. end;
  418. end;
  419. procedure TTTBMain.GLSceneViewerMouseDown(Sender: TObject;
  420. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  421. begin
  422. mx:=x; my:=y;
  423. end;
  424. procedure TTTBMain.GLSceneViewerMouseMove(Sender: TObject;
  425. Shift: TShiftState; X, Y: Integer);
  426. begin
  427. if Shift=[ssLeft] then begin
  428. GLCamera.MoveAroundTarget(my-y, mx-x);
  429. mx:=x; my:=y;
  430. end;
  431. end;
  432. procedure TTTBMain.CBBackgroundChange(Sender: TObject);
  433. begin
  434. HSBkgnd.Visible:=(CBBackground.ItemIndex=0);
  435. case CBBackground.ItemIndex of
  436. 1 : GLSceneViewer.Buffer.BackgroundColor:=clBlack;
  437. 2 : GLSceneViewer.Buffer.BackgroundColor:=clSilver;
  438. 3 : GLSceneViewer.Buffer.BackgroundColor:=clWhite;
  439. end;
  440. end;
  441. procedure TTTBMain.ACColorDilatationExecute(Sender: TObject);
  442. var
  443. x, y, sx, sy : Integer;
  444. bmRGB, bmAlpha : TBitmap;
  445. r, g, b : Single;
  446. weightSum, iw : Single;
  447. procedure WeightIn(x, y : Integer; wBase : Single);
  448. var
  449. w : Single;
  450. rgb, alpha : Integer;
  451. begin
  452. if (Cardinal(x)<Cardinal(sx)) and (Cardinal(y)<Cardinal(sy)) then begin
  453. alpha:=bmAlpha.Canvas.Pixels[x, y];
  454. if alpha>0 then begin
  455. w:=((alpha shr 8) and $FF)*(1/255)*wBase;
  456. weightSum:=weightSum+w;
  457. rgb:=bmRGB.Canvas.Pixels[x, y];
  458. r:=r+GetRValue(rgb)*w;
  459. g:=g+GetGValue(rgb)*w;
  460. b:=b+GetBValue(rgb)*w;
  461. end;
  462. end;
  463. end;
  464. begin
  465. Screen.Cursor:=crHourGlass;
  466. // for all pixels in the color map that are fully transparent,
  467. // change their color to the average of the weighted average of their
  468. // opaque neighbours
  469. bmRGB:=IMRGB.Picture.Bitmap;
  470. bmAlpha:=IMAlpha.Picture.Bitmap;
  471. sx:=StrToInt(CBWidth.Text);
  472. sy:=StrToInt(CBHeight.Text);
  473. for y:=0 to sy-1 do begin
  474. for x:=0 to sx-1 do begin
  475. // if pixel fully opaque
  476. if bmAlpha.Canvas.Pixels[x, y]=0 then begin
  477. // weight-in all neighbours
  478. r:=0; g:=0; b:=0; weightSum:=0;
  479. WeightIn(x-1, y-1, 0.7); WeightIn(x , y-1, 1.0); WeightIn(x+1, y-1, 0.7);
  480. WeightIn(x-1, y , 1.0); WeightIn(x , y , 4.0); WeightIn(x+1, y , 1.0);
  481. WeightIn(x-1, y+1, 0.7); WeightIn(x , y+1, 1.0); WeightIn(x+1, y+1, 0.7);
  482. if weightSum>0 then begin
  483. iw:=1/weightSum;
  484. bmRGB.Canvas.Pixels[x, y]:=RGB(Round(r*iw), Round(g*iw), Round(b*iw));
  485. end;
  486. end;
  487. end;
  488. end;
  489. TextureChanged;
  490. Screen.Cursor:=crDefault;
  491. end;
  492. procedure TTTBMain.ACAlphaErosionExecute(Sender: TObject);
  493. var
  494. x, y, sx, sy : Integer;
  495. bmp, bmAlpha : TBitmap;
  496. minNeighbour : Integer;
  497. begin
  498. // make fully transparent all pixels in the color for all pixels in the alpha map
  499. // that are adjacent to a fully transparent one
  500. bmAlpha:=IMAlpha.Picture.Bitmap;
  501. sx:=StrToInt(CBWidth.Text);
  502. sy:=StrToInt(CBHeight.Text);
  503. bmp:=SpawnBitmap;
  504. for y:=0 to sy-1 do begin
  505. for x:=0 to sx-1 do with bmAlpha.Canvas do begin
  506. if Pixels[x, y]>0 then begin
  507. minNeighbour:=MinInteger(MinInteger(Pixels[x, y-1], Pixels[x, y+1]),
  508. MinInteger(Pixels[x-1, y], Pixels[x+1, y]));
  509. bmp.Canvas.Pixels[x, y]:=MinInteger(minNeighbour, Pixels[x, y]);
  510. end else bmp.Canvas.Pixels[x, y]:=0;
  511. end;
  512. end;
  513. IMAlpha.Picture.Bitmap:=bmp;
  514. bmp.Free;
  515. TextureChanged;
  516. end;
  517. procedure TTTBMain.ACAlphaDilatationExecute(Sender: TObject);
  518. var
  519. x, y, sx, sy : Integer;
  520. bmp, bmAlpha : TBitmap;
  521. maxNeighbour : Integer;
  522. begin
  523. // make fully transparent all pixels in the color for all pixels in the alpha map
  524. // that are adjacent to a fully transparent one
  525. bmAlpha:=IMAlpha.Picture.Bitmap;
  526. sx:=StrToInt(CBWidth.Text);
  527. sy:=StrToInt(CBHeight.Text);
  528. bmp:=SpawnBitmap;
  529. for y:=0 to sy-1 do begin
  530. for x:=0 to sx-1 do with bmAlpha.Canvas do begin
  531. if Pixels[x, y]<clWhite then begin
  532. maxNeighbour:=MaxInteger(MaxInteger(Pixels[x, y-1], Pixels[x, y+1]),
  533. MaxInteger(Pixels[x-1, y], Pixels[x+1, y]));
  534. bmp.Canvas.Pixels[x, y]:=MaxInteger(maxNeighbour, Pixels[x, y]);
  535. end else bmp.Canvas.Pixels[x, y]:=clWhite;
  536. end;
  537. end;
  538. IMAlpha.Picture.Bitmap:=bmp;
  539. bmp.Free;
  540. TextureChanged;
  541. end;
  542. procedure TTTBMain.ACOpaqueExecute(Sender: TObject);
  543. begin
  544. ResetAlpha;
  545. end;
  546. procedure TTTBMain.ACAlphaSuperBlackExecute(Sender: TObject);
  547. begin
  548. GenerateAlpha(clBlack, False, False);
  549. end;
  550. procedure TTTBMain.ACFromRGBIntensityExecute(Sender: TObject);
  551. begin
  552. GenerateAlpha(-1, True, False);
  553. end;
  554. procedure TTTBMain.ACFromRGBSqrtIntensityExecute(Sender: TObject);
  555. begin
  556. GenerateAlpha(-1, True, True);
  557. end;
  558. procedure TTTBMain.ACAlphaOffsetExecute(Sender: TObject);
  559. var
  560. x, y, c, offset : Integer;
  561. bmp : TBitmap;
  562. pSrc, pDest : PIntegerArray;
  563. offsetStr : String;
  564. begin
  565. offsetStr:='0';
  566. if not InputQuery('Alpha Offset', 'Enter Offset Value (-255..255)', offsetStr) then Exit;
  567. offset:=StrToIntDef(offsetStr, MaxInt);
  568. if (offset<-255) or (offset>255) then begin
  569. ShowMessage('Invalid Alpha Offset');
  570. Exit;
  571. end;
  572. bmp:=SpawnBitmap;
  573. try
  574. for y:=0 to bmp.Height-1 do begin
  575. pSrc:=IMAlpha.Picture.Bitmap.ScanLine[y];
  576. pDest:=bmp.ScanLine[y];
  577. for x:=0 to bmp.Width-1 do begin
  578. c:=pSrc[x] and $FF;
  579. c:=c+offset;
  580. if c<=0 then
  581. pDest[x]:=0
  582. else if c>=255 then
  583. pDest[x]:=$FFFFFF
  584. else pDest[x]:=c+(c shl 8)+(c shl 16);
  585. end;
  586. end;
  587. IMAlpha.Picture.Bitmap:=bmp;
  588. finally
  589. bmp.Free;
  590. end;
  591. TextureChanged;
  592. end;
  593. procedure TTTBMain.ACAlphaSaturateExecute(Sender: TObject);
  594. var
  595. x, y : Integer;
  596. bmp : TBitmap;
  597. pSrc, pDest : PIntegerArray;
  598. begin
  599. bmp:=SpawnBitmap;
  600. try
  601. for y:=0 to bmp.Height-1 do begin
  602. pSrc:=IMAlpha.Picture.Bitmap.ScanLine[y];
  603. pDest:=bmp.ScanLine[y];
  604. for x:=0 to bmp.Width-1 do begin
  605. if (pSrc[x] and $FF)>0 then
  606. pDest[x]:=$FFFFFF
  607. else pDest[x]:=0;
  608. end;
  609. end;
  610. IMAlpha.Picture.Bitmap:=bmp;
  611. finally
  612. bmp.Free;
  613. end;
  614. TextureChanged;
  615. end;
  616. procedure TTTBMain.ACAlphaNegateExecute(Sender: TObject);
  617. var
  618. x, y, c : Integer;
  619. bmp : TBitmap;
  620. pSrc, pDest : PIntegerArray;
  621. begin
  622. bmp:=SpawnBitmap;
  623. try
  624. for y:=0 to bmp.Height-1 do begin
  625. pSrc:=IMAlpha.Picture.Bitmap.ScanLine[y];
  626. pDest:=bmp.ScanLine[y];
  627. for x:=0 to bmp.Width-1 do begin
  628. c:=$FF-(pSrc[x] and $FF);
  629. pDest[x]:=c+(c shl 8)+(c shl 16);
  630. end;
  631. end;
  632. IMAlpha.Picture.Bitmap:=bmp;
  633. finally
  634. bmp.Free;
  635. end;
  636. TextureChanged;
  637. end;
  638. end.