summaryrefslogtreecommitdiff
path: root/tests/libtracker-extract/tracker-iptc-test.c
blob: 2306c08fda0375fa0f2d88b961efb1b02349d170 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * 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
 */
static 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;
}

static void
test_iptc_extraction (void)
{
        TrackerIptcData *data;

        data = load_iptc_blob (TOP_SRCDIR "/tests/libtracker-extract/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_test_init (&argc, &argv, NULL);

        g_test_add_func ("/libtracker-extract/iptc/extraction",
                         test_iptc_extraction);
        return g_test_run ();
}