[tracker] tracker-extract: Add width and height information to BMP extractor



commit 391d34c5f1d75d4626baa4481565a10ea9b8ded8
Author: Mingxiang Lin <paralmx 163 com>
Date:   Thu Feb 5 22:01:40 2015 +0000

    tracker-extract: Add width and height information to BMP extractor
    
    https://bugzilla.gnome.org/show_bug.cgi?id=741109

 src/tracker-extract/tracker-extract-bmp.c |   98 ++++++++++++++++++++++++++++-
 1 files changed, 95 insertions(+), 3 deletions(-)
---
diff --git a/src/tracker-extract/tracker-extract-bmp.c b/src/tracker-extract/tracker-extract-bmp.c
index 423aef4..1f3e41e 100644
--- a/src/tracker-extract/tracker-extract-bmp.c
+++ b/src/tracker-extract/tracker-extract-bmp.c
@@ -1,6 +1,7 @@
 /*
  * Copyright (C) 2013-2014 Jolla Ltd. <andrew den exter jollamobile com>
  * Author: Philip Van Hoof <philip codeminded be>
+ * Author: Mingxiang Lin <paralmx gmail com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -23,6 +24,84 @@
 #include <libtracker-common/tracker-common.h>
 #include <libtracker-extract/tracker-extract.h>
 
+static gboolean
+get_img_resolution (const GFile *file,
+                    gint64      *width,
+                    gint64      *height)
+{
+       GFileInputStream *stream;
+       GInputStream *inputstream;
+       GError *error = NULL;
+       char bfType[2] = { 0 };
+       uint w, h;
+
+       if (width) {
+               *width = 0;
+       }
+
+       if (height) {
+               *height = 0;
+       }
+
+       w = h = 0;
+
+       stream = g_file_read ((GFile *)file, NULL, &error);
+       if (error) {
+               g_message ("Could not read BMP file, %s", error->message);
+               g_clear_error (&error);
+               return FALSE;
+       }
+
+       inputstream = G_INPUT_STREAM (stream);
+
+       if (!g_input_stream_read (inputstream, bfType, 2, NULL, &error)) {
+               g_message ("Could not read BMP header from stream, %s", error ? error->message : "No error 
given");
+               g_clear_error (&error);
+               g_object_unref (stream);
+               return FALSE;
+       }
+
+       if (bfType[0] != 'B' || bfType[1] != 'M') {
+               g_message ("Expected BMP header to read 'B' or 'M', can not continue");
+               g_object_unref (stream);
+               return FALSE;
+       }
+
+       if (!g_input_stream_skip (inputstream, 16, NULL, &error)) {
+               g_message ("Could not read 16 bytes from BMP header, %s", error ? error->message : "No error 
given");
+               g_clear_error (&error);
+               g_object_unref (stream);
+               return FALSE;
+       }
+
+       if (!g_input_stream_read (inputstream, &w, sizeof (uint), NULL, &error)) {
+               g_message ("Could not read width from BMP header, %s", error ? error->message : "No error 
given");
+               g_clear_error (&error);
+               g_object_unref (stream);
+               return FALSE;
+       }
+
+       if (!g_input_stream_read (inputstream, &h, sizeof (uint), NULL, &error)) {
+               g_message ("Could not read height from BMP header, %s", error ? error->message : "No error 
given");
+               g_clear_error (&error);
+               g_object_unref (stream);
+               return FALSE;
+       }
+
+       if (width) {
+               *width = w;
+       }
+
+       if (height) {
+               *height = h;
+       }
+
+       g_input_stream_close (inputstream, NULL, NULL);
+       g_object_unref (stream);
+
+       return TRUE;
+}
+
 G_MODULE_EXPORT gboolean
 tracker_extract_get_metadata (TrackerExtractInfo *info)
 {
@@ -30,16 +109,21 @@ tracker_extract_get_metadata (TrackerExtractInfo *info)
        goffset size;
        gchar *filename;
        GFile *file;
+       gint64 width = 0, height = 0;
 
        metadata = tracker_extract_info_get_metadata_builder (info);
 
        file = tracker_extract_info_get_file (info);
+       if (!file) {
+               return FALSE;
+       }
+
        filename = g_file_get_path (file);
        size = tracker_file_get_size (filename);
+       g_free (filename);
 
        if (size < 14) {
                /* Smaller than BMP header, can't be a real BMP file */
-               g_free (filename);
                return FALSE;
        }
 
@@ -47,9 +131,17 @@ tracker_extract_get_metadata (TrackerExtractInfo *info)
        tracker_sparql_builder_object (metadata, "nfo:Image");
        tracker_sparql_builder_object (metadata, "nmm:Photo");
 
-       /* TODO: Add actual metadata extraction for BMP files */
+       if (get_img_resolution (file, &width, &height)) {
+               if (width > 0) {
+                       tracker_sparql_builder_predicate (metadata, "nfo:width");
+                       tracker_sparql_builder_object_int64 (metadata, width);
+               }
 
-       g_free (filename);
+               if (height > 0) {
+                       tracker_sparql_builder_predicate (metadata, "nfo:height");
+                       tracker_sparql_builder_object_int64 (metadata, height);
+               }
+       }
 
        return TRUE;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]