diff options
author | Ivan Frade <ivan.frade@gmail.com> | 2011-10-20 16:07:11 +0300 |
---|---|---|
committer | Ivan Frade <ivan.frade@gmail.com> | 2011-10-20 16:07:44 +0300 |
commit | af43a834bcc9b00ffd9c0f06ebcd075356d9d63f (patch) | |
tree | c6a25ceff22d516cb7916659d836332b44637aeb /tests | |
parent | fd2ec123bff2e004f8aad1edc0ae7f37e236d8d9 (diff) | |
download | tracker-af43a834bcc9b00ffd9c0f06ebcd075356d9d63f.tar.gz |
tests/libtracker-extract: unit tests for tracker-iptc
Diffstat (limited to 'tests')
-rw-r--r-- | tests/libtracker-extract/Makefile.am | 12 | ||||
-rw-r--r-- | tests/libtracker-extract/iptc-img.jpg | bin | 0 -> 3782 bytes | |||
-rw-r--r-- | tests/libtracker-extract/tracker-iptc-test.c | 163 |
3 files changed, 174 insertions, 1 deletions
diff --git a/tests/libtracker-extract/Makefile.am b/tests/libtracker-extract/Makefile.am index 375772e74..57cde1558 100644 --- a/tests/libtracker-extract/Makefile.am +++ b/tests/libtracker-extract/Makefile.am @@ -9,6 +9,11 @@ TEST_PROGS += \ tracker-exif-test \ tracker-guarantee-test +if HAVE_IPTC +# This test also requires libjpeg... +TEST_PROGS += tracker-iptc-test +endif + if HAVE_ENCA TEST_PROGS += tracker-encoding else @@ -46,6 +51,10 @@ tracker_exif_test_SOURCES = tracker-exif-test.c tracker_guarantee_test_SOURCES = tracker-guarantee-test.c +tracker_iptc_test_SOURCES = tracker-iptc-test.c +tracker_iptc_test_LDADD = $(LDADD) $(LIBJPEG_LIBS) +tracker_iptc_test_CFLAGS = $(LIBJPEG_CFLAGS) + EXTRA_DIST = encoding-detect.bin \ areas.xmp \ areas-with-contacts.xmp \ @@ -55,4 +64,5 @@ EXTRA_DIST = encoding-detect.bin \ exif-img.jpg \ exif-free-img.jpg \ guarantee-mtime-test.txt \ - getline-test.txt + getline-test.txt \ + iptc-img.jpg diff --git a/tests/libtracker-extract/iptc-img.jpg b/tests/libtracker-extract/iptc-img.jpg Binary files differnew file mode 100644 index 000000000..d40001062 --- /dev/null +++ b/tests/libtracker-extract/iptc-img.jpg diff --git a/tests/libtracker-extract/tracker-iptc-test.c b/tests/libtracker-extract/tracker-iptc-test.c new file mode 100644 index 000000000..b016b3038 --- /dev/null +++ b/tests/libtracker-extract/tracker-iptc-test.c @@ -0,0 +1,163 @@ +/* + * Copyright (C) 2011, Nokia <ivan.frade@nokia.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "config.h" + +#include <stdio.h> +#include <string.h> +#include <setjmp.h> + +#include <jpeglib.h> + +#include <glib.h> +#include <gio/gio.h> +#include <libtracker-extract/tracker-iptc.h> + +#define PS3_NAMESPACE "Photoshop 3.0\0" +#define PS3_NAMESPACE_LENGTH 14 +#include <libiptcdata/iptc-jpeg.h> + +struct tej_error_mgr { + struct jpeg_error_mgr jpeg; + jmp_buf setjmp_buffer; +}; + + +static void +extract_jpeg_error_exit (j_common_ptr cinfo) +{ + struct tej_error_mgr *h = (struct tej_error_mgr *) cinfo->err; + (*cinfo->err->output_message)(cinfo); + longjmp (h->setjmp_buffer, 1); +} + +/* + * libiptcdata doesn't scan the file until find the IPTC blob. + * We need to find the blob ourselves. This code comes from tracker-extract-jpeg + */ +TrackerIptcData * +load_iptc_blob (const gchar *filename) +{ + struct jpeg_decompress_struct cinfo; + struct tej_error_mgr tejerr; + struct jpeg_marker_struct *marker; + TrackerIptcData *id = NULL; + FILE *f; + gchar *uri; + GFile *file; + + file = g_file_new_for_path (filename); + uri = g_file_get_uri (file); + g_object_unref (file); + + f = fopen (filename, "r"); + + cinfo.err = jpeg_std_error (&tejerr.jpeg); + tejerr.jpeg.error_exit = extract_jpeg_error_exit; + if (setjmp (tejerr.setjmp_buffer)) { + fclose (f); + g_free (uri); + return NULL; + } + + jpeg_create_decompress (&cinfo); + + jpeg_save_markers (&cinfo, JPEG_COM, 0xFFFF); + jpeg_save_markers (&cinfo, JPEG_APP0 + 1, 0xFFFF); + jpeg_save_markers (&cinfo, JPEG_APP0 + 13, 0xFFFF); + + jpeg_stdio_src (&cinfo, f); + + jpeg_read_header (&cinfo, TRUE); + + marker = (struct jpeg_marker_struct *) &cinfo.marker_list; + + while (marker) { + gchar *str; + gsize len; + gint offset; + guint sublen; + + switch (marker->marker) { + case JPEG_COM: + break; + + case JPEG_APP0 + 1: + break; + + case JPEG_APP0 + 13: + str = (gchar*) marker->data; + len = marker->data_length; + if (len > 0 && strncmp (PS3_NAMESPACE, str, PS3_NAMESPACE_LENGTH) == 0) { + offset = iptc_jpeg_ps3_find_iptc ((guchar *)str, len, &sublen); + if (offset > 0 && sublen > 0) { + id = tracker_iptc_new ((const guchar *)str + offset, sublen, uri); + } + } + break; + + default: + marker = marker->next; + continue; + } + + marker = marker->next; + } + + g_free (uri); + fclose (f); + + return id; +} + + +void +test_iptc_extraction () +{ + TrackerIptcData *data; + + data = load_iptc_blob ("./iptc-img.jpg"); + g_assert (data); + + g_assert_cmpstr (data->keywords, ==, "Coverage, test"); + g_assert (g_str_has_prefix (data->date_created, "2011-10-22")); + g_assert_cmpstr (data->byline, ==, "BylineValue"); + g_assert_cmpstr (data->byline_title, ==, "BylineTitleValue"); + g_assert_cmpstr (data->credit, ==, "CreditValue"); + g_assert_cmpstr (data->copyright_notice, ==, "IptcToolAuthors"); + g_assert_cmpstr (data->image_orientation, ==, "nfo:orientation-left"); + g_assert_cmpstr (data->city, ==, "Helsinki"); + g_assert_cmpstr (data->state, ==, "N/A"); + g_assert_cmpstr (data->sublocation, ==, "Ruoholahti"); + g_assert_cmpstr (data->country_name, ==, "Finland"); + g_assert_cmpstr (data->contact, ==, "Dilbert"); + + tracker_iptc_free (data); +} + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/libtracker-extract/iptc/extraction", + test_iptc_extraction); + return g_test_run (); +} |