Recent

Author Topic: 8Bit Image Filter  (Read 525 times)

Gigatron

  • Sr. Member
  • ****
  • Posts: 282
  • Amiga Rulez !!
8Bit Image Filter
« on: May 23, 2025, 05:12:14 pm »
Hi,

Lazarus Pascal is perfect for developing utilities. A friend who develops games like 8bit asked me if you can (smooth  anti-aliasing ) lines without going through filters from drawing tools. I just created a program for him. Maybe this information will inspire other developers on the forum and demonstrate that in pascal you can program everything. I could easily code this in c#, java or javascript but with lazarus pascal I go very fast!!

The images source from Cpc, C64, Msx , atari .. 8bits console or computer.

Original image and filtered image result are attached;

Regards

Here the code for scale2x routine, the rest is source closed ;)

Code: Pascal  [Select][+][-]
  1. function TForm1.GetPixelSafe(bmp: TBGRABitmap; x, y: integer): TBGRAPixel;
  2. begin
  3.   if (x >= 0) and (x < bmp.Width) and (y >= 0) and (y < bmp.Height) then
  4.     Result := bmp.GetPixel(x, y)
  5.   else
  6.     Result := BGRA(0, 0, 0, 0); // Transparent pixels hors limites !!!
  7. end;
  8.  
  9. function TForm1.ColorsEqual(c1, c2: TBGRAPixel): boolean;
  10. begin
  11.   Result := (c1.red = c2.red) and (c1.green = c2.green) and (c1.blue = c2.blue);
  12. end;
  13.  
  14. function TForm1.Scale2xFilter(source: TBGRABitmap): TBGRABitmap;
  15. var
  16.   x, y: integer;
  17.   A, B, C, D, E, F, G, H, I: TBGRAPixel;
  18.   E0, E1, E2, E3: TBGRAPixel;
  19. begin
  20.   Result := TBGRABitmap.Create(source.Width * 2, source.Height * 2);
  21.  
  22.   for y := 0 to source.Height - 1 do
  23.     for x := 0 to source.Width - 1 do
  24.     begin
  25.       // recupe  9 pixels voisins  en x,y -1 +1 etc
  26.       A := GetPixelSafe(source, x-1, y-1);
  27.       B := GetPixelSafe(source, x, y-1);
  28.       C := GetPixelSafe(source, x+1, y-1);
  29.       D := GetPixelSafe(source, x-1, y);
  30.       E := GetPixelSafe(source, x, y);       // Pixel au milieu
  31.       F := GetPixelSafe(source, x+1, y);
  32.       G := GetPixelSafe(source, x-1, y+1);
  33.       H := GetPixelSafe(source, x, y+1);
  34.       I := GetPixelSafe(source, x+1, y+1);
  35.  
  36.       // Algorithme Scale2x de m*rde !
  37.       if (ColorsEqual(D, B) and not ColorsEqual(D, H) and not ColorsEqual(B, F)) then
  38.         E0 := D
  39.       else
  40.         E0 := E;
  41.  
  42.       if (ColorsEqual(B, F) and not ColorsEqual(B, D) and not ColorsEqual(F, H)) then
  43.         E1 := F
  44.       else
  45.         E1 := E;
  46.  
  47.       if (ColorsEqual(D, H) and not ColorsEqual(D, B) and not ColorsEqual(H, F)) then
  48.         E2 := D
  49.       else
  50.         E2 := E;
  51.  
  52.       if (ColorsEqual(H, F) and not ColorsEqual(H, D) and not ColorsEqual(F, B)) then
  53.         E3 := F
  54.       else
  55.         E3 := E;
  56.  
  57.       // replace les 4 pixel
  58.       Result.SetPixel(x*2,   y*2, E0);
  59.       Result.SetPixel(x*2+1, y*2, E1);
  60.       Result.SetPixel(x*2,   y*2+1, E2);
  61.       Result.SetPixel(x*2+1, y*2+1, E3);
  62.     end;
  63. end;
« Last Edit: May 24, 2025, 02:19:33 am by Gigatron »
Sub Quantum Technology ! Pascal - C - C# - Java - Javascript - Glsl - Lua - Html5 - CSS - Amiga Rules !

 

TinyPortal © 2005-2018