/* gcc gexiv2-fnumber-aperture-value.c -lm $(pkg-config --cflags --libs
   gexiv2 glib-2.0) */

#include <math.h>

#include <gexiv2/gexiv2.h>
#include <glib.h>

static const gchar *FILE_PATH = "fnumber-and-aperture-value.jpg";

gint
main (void)
{
  GExiv2Metadata *metadata = NULL;
  gdouble fnumber;
  gint aperture_den;
  gint aperture_nom;

  metadata = gexiv2_metadata_new ();

  {
    g_autoptr (GError) error = NULL;

    if (!gexiv2_metadata_open_path (metadata, FILE_PATH, &error))
      {
        g_warning ("gexiv2_metadata_open_path: %s", error->message);
        goto out;
      }
  }

  fnumber = gexiv2_metadata_get_fnumber (metadata);
  g_message ("fnumber: %f", fnumber);

  if (gexiv2_metadata_get_exif_tag_rational (metadata,
                                             "Exif.Photo.ApertureValue",
                                             &aperture_nom,
                                             &aperture_den))
    {
      gdouble aperture_value = (gdouble) aperture_nom
        / (gdouble) aperture_den;

      g_message ("aperture value: %f", aperture_value);

      fnumber = pow (2.0, aperture_value / 2.0);
      g_message ("fnumber (from aperture value): %f", fnumber);
    }

 out:
  g_clear_object (&metadata);
  return 0;
}
