Function like "crop to content" from GIMP

I wonder if somehow has implemented a function like the “crop to content” which is available at gimp?

please link to examples/specification of this thing in GIMP, and source code if possible.

please provide an example situation (code, data) on which you need this function performed, and how the result in GIMP looks like.