By default there is a difference in the HTML output for images inserted alone or via .
A direct image link will output an <a href="">
to an actual image mime type, like:
<a href="https://wordpress.stackexchange.com/questions/119659/some-image.jpg">
An attachment <a href="">
outputs an attachment URI like:
<a href="https://wordpress.stackexchange.com/questions/119659/some-link?attachment_id=126"
or ../attachment/2013/
As for a filter I don’t think there is a way unless you parse the_content
for the above matches.