B4J Code Snippet Geometrize in B4J

Full Geometrize source attached — and yes, it actually works! 🎉
Just a heads-up: if you’re using BIG source images, grab a good movie, a large coffee, and maybe a backup coffee… it’s gonna be a while. ☕🎬

As for the GUI — let’s just say it’s technically functional. Not exactly “production-ready,” but hey, it does the job. 😅

have fun

gui.png
flower_sm.jpg
ezgif.com-optimize.gif
 

Attachments

  • geometric.zip
    12.5 KB · Views: 15

William Lancee

Well-Known Member
Licensed User
Longtime User
Last edited:

madru

Active Member
Licensed User
Longtime User
put this in, will give you a little speed bump on Windows

VirtualMachineArgs Windows:
#VirtualMachineArgs: -Dprism.order=d3d,es2,sw -Dprism.forceGPU=true -Dprism.vsync=false -Dprism.verbose=true --add-exports javafx.graphics/com.sun.prism=ALL-UNNAMED --add-exports javafx.graphics/com.sun.javafx.sg.prism=ALL-UNNAMED

VirtualMachineArgs OSX:
#VirtualMachineArgs: -Dprism.order=es2,sw -Dprism.forceGPU=True -Dprism.vsync=False -Dprism.verbose=True
does not work on OSX (no idea why), but does work if using using this:

B4X:
java -Dprism.order=es2,sw \
     -Dprism.forceGPU=True \
     -Dprism.vsync=False \
     -Dprism.verbose=True \
     -jar file.jar

here some code to test if the GPU is enabled

B4X:
Sub CheckGPU
    ' Check if arguments are loaded
    Dim jo As JavaObject
    jo.InitializeStatic("java.lang.System")
    Dim prismOrder As String = jo.RunMethod("getProperty", Array("prism.order", "NOT_SET"))
   
    Log($"Prism Order: ${prismOrder}"$)
   
    If prismOrder = "NOT_SET" Then
        Log("VM arguments NOT loaded!")
        Log("Fix: Put all args on ONE line")
    Else
        Log("VM arguments loaded correctly")
    End If
   
    ' Check actual pipeline at runtime
    Try
        Dim pipeline As JavaObject
        pipeline.InitializeStatic("com.sun.prism.GraphicsPipeline")
        Dim currentPipeline As Object = pipeline.RunMethod("getPipeline", Null)
        Log($"Active Pipeline: ${currentPipeline}"$)
       
        Dim pipelineName As String = currentPipeline
        If pipelineName.Contains("D3D") Then
            Log("Using Direct3D (GPU)")
        Else If pipelineName.Contains("ES2") Then
            Log("Using OpenGL ES2 (GPU)")
        Else
            Log("Using Software Rendering")
        End If
    Catch
        Log($"Pipeline detection error: ${LastException}"$)
    End Try
End Sub
 

madru

Active Member
Licensed User
Longtime User
First and Last Update

Additions:
  • Antialiasing
  • Rotated Rectangle
  • Rotated Ellipse
  • Polyline
  • Quadratic Bézier
  • GPU Detection
Performance:
  • Highly optimized math now


 

Attachments

  • geometric.zip
    14.9 KB · Views: 16
Last edited:

madru

Active Member
Licensed User
Longtime User
This is a lovely way to "artify" an image.

I noticed that colors are retained but small details get absorbed by the shapes.
What happens if you superimpose a word say "happy" on the flower? Will it disappear?

It reminds me of a B4X project I did some years ago where I needed to obscure details on a Google satellite image.
https://www.b4x.com/android/forum/t...ge-creating-an-impressionistic-effect.143149/
try it

geometrized_final_22445shapes.png


this one uses only 3000 shapes (lines)...already quite detailed
 

William Lancee

Well-Known Member
Licensed User
Longtime User
What happened to the smiling snail?

The algorithm creates a mosaic of overlapping random shapes. The color of each defined by some formula based on the color of the corresponding pixels in the original contained by the shape. Since the snail's outline color is thin, most overlapping shapes will only see a small part of and it will be drowned out by the other colors contained in the shape. @madru correct me if I'm wrong.
 

madru

Active Member
Licensed User
Longtime User
The algorithm creates a mosaic of overlapping random shapes. The color of each defined by some formula based on the color of the corresponding pixels in the original contained by the shape. Since the snail's outline color is thin, most overlapping shapes will only see a small part of and it will be drowned out by the other colors contained in the shape. @madru correct me if I'm wrong.

If I haven't messed up the formula, it should be like this. 😅
The algorithm places shapes iteratively, always choosing the one that most reduces visual error.
Early shapes (1-200) capture large features because they provide the biggest improvement.
Later shapes (200-1000+) progressively refine smaller details. Thin lines don't appear immediately because a single shape covering a 2-pixel line (representing only 4% of its area) barely reduces error. However, as large regions become well-approximated, the remaining error concentrates along edges and fine details, causing the algorithm to place more shapes there. With enough iterations (1000-2000+ shapes), thin features gradually emerge through accumulated refinement.
The process naturally goes from coarse → finedetail.

It's not that thin details can't appear—they appear later because they require many shapes to build up gradually.

More shapes = more detail! (fyi: The first version could process a maximum of 500 shapes, while the updated version can process significantly more.)

some examples:
The first one was created with “Quadratic Bézier,” the second with rotated rectangles, each with about 4,000 shapes. The 3rd activated all options and calculated about 32,000 shapes.

....to emexes question: no pest control 😅😅
 

Attachments

  • geometrized_final_9820shapes.png
    geometrized_final_9820shapes.png
    253.1 KB · Views: 7
  • geometrized_final_9935shapes.png
    geometrized_final_9935shapes.png
    309.1 KB · Views: 6
  • geometrized_final_83955shapes.png
    geometrized_final_83955shapes.png
    263.8 KB · Views: 6
Last edited:
Top