tracker-extract-jpeg.c 16.4 KB
Newer Older
1
/*
2
 * Copyright (C) 2006, Jamie McCracken <jamiemcc@gnome.org>
3
 * Copyright (C) 2008, Nokia <ivan.frade@nokia.com>
4
 *
5 6
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15 16
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
17 18 19 20 21 22 23
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include "config.h"

#include <stdio.h>
24
#include <setjmp.h>
25

26 27
#include <jpeglib.h>

28
#include <libtracker-miners-common/tracker-common.h>
29
#include <libtracker-extract/tracker-extract.h>
30
#include <libtracker-sparql/tracker-sparql.h>
31

32
#include "tracker-main.h"
33

34
#define CMS_PER_INCH            2.54
35

36
#ifdef HAVE_LIBEXIF
Martyn Russell's avatar
Martyn Russell committed
37 38
#define EXIF_NAMESPACE          "Exif"
#define EXIF_NAMESPACE_LENGTH   4
39
#endif /* HAVE_LIBEXIF */
Jürg Billeter's avatar
Jürg Billeter committed
40

41
#ifdef HAVE_EXEMPI
Martyn Russell's avatar
Martyn Russell committed
42 43
#define XMP_NAMESPACE           "http://ns.adobe.com/xap/1.0/\x00"
#define XMP_NAMESPACE_LENGTH    29
44 45
#endif /* HAVE_EXEMPI */

46
#ifdef HAVE_LIBIPTCDATA
Martyn Russell's avatar
Martyn Russell committed
47 48
#define PS3_NAMESPACE           "Photoshop 3.0\0"
#define PS3_NAMESPACE_LENGTH    14
49 50 51
#include <libiptcdata/iptc-jpeg.h>
#endif /* HAVE_LIBIPTCDATA */

52 53 54 55 56 57
enum {
	JPEG_RESOLUTION_UNIT_UNKNOWN = 0,
	JPEG_RESOLUTION_UNIT_PER_INCH = 1,
	JPEG_RESOLUTION_UNIT_PER_CENTIMETER = 2,
};

58
typedef struct {
59 60
	const gchar *make;
	const gchar *model;
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	const gchar *title;
	const gchar *orientation;
	const gchar *copyright;
	const gchar *white_balance;
	const gchar *fnumber;
	const gchar *flash;
	const gchar *focal_length;
	const gchar *artist;
	const gchar *exposure_time;
	const gchar *iso_speed_ratings;
	const gchar *date;
	const gchar *description;
	const gchar *metering_mode;
	const gchar *creator;
	const gchar *comment;
	const gchar *city;
	const gchar *state;
	const gchar *address;
79
	const gchar *country;
80 81 82
	const gchar *gps_altitude;
	const gchar *gps_latitude;
	const gchar *gps_longitude;
83
	const gchar *gps_direction;
84 85 86
} MergeData;

struct tej_error_mgr {
87 88 89 90
	struct jpeg_error_mgr jpeg;
	jmp_buf setjmp_buffer;
};

91
static void
92 93 94 95 96 97 98
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);
}

99 100 101 102 103 104
static gboolean
guess_dlna_profile (gint          width,
                    gint          height,
                    const gchar **dlna_profile,
                    const gchar **dlna_mimetype)
{
105
	const gchar *profile = NULL;
106 107 108 109 110 111 112 113 114

	if (dlna_profile) {
		*dlna_profile = NULL;
	}

	if (dlna_mimetype) {
		*dlna_mimetype = NULL;
	}

115
	if (width <= 640 && height <= 480) {
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
		profile = "JPEG_SM";
	} else if (width <= 1024 && height <= 768) {
		profile = "JPEG_MED";
	} else if (width <= 4096 && height <= 4096) {
		profile = "JPEG_LRG";
	}

	if (profile) {
		if (dlna_profile) {
			*dlna_profile = profile;
		}

		if (dlna_mimetype) {
			*dlna_mimetype = "image/jpeg";
		}

		return TRUE;
	}

	return FALSE;
}

138
G_MODULE_EXPORT gboolean
139
tracker_extract_get_metadata (TrackerExtractInfo *info)
140
{
141 142 143
	struct jpeg_decompress_struct cinfo;
	struct tej_error_mgr tejerr;
	struct jpeg_marker_struct *marker;
144
	TrackerResource *metadata;
145 146 147
	TrackerXmpData *xd = NULL;
	TrackerExifData *ed = NULL;
	TrackerIptcData *id = NULL;
148
	MergeData md = { 0 };
149
	GFile *file;
150 151
	FILE *f;
	goffset size;
152
	gchar *filename, *uri;
153
	gchar *comment = NULL;
154
	const gchar *dlna_profile, *dlna_mimetype;
155
	GPtrArray *keywords;
156
	gboolean success = TRUE;
157
	guint i;
Jürg Billeter's avatar
Jürg Billeter committed
158

159 160
	file = tracker_extract_info_get_file (info);
	filename = g_file_get_path (file);
161

162
	size = tracker_file_get_size (filename);
163 164

	if (size < 18) {
165
		g_free (filename);
166
		return FALSE;
167 168
	}

169
	f = tracker_file_open (filename);
170
	g_free (filename);
171

172
	if (!f) {
173
		return FALSE;
174
	}
175

176 177
	uri = g_file_get_uri (file);

178 179 180
	cinfo.err = jpeg_std_error (&tejerr.jpeg);
	tejerr.jpeg.error_exit = extract_jpeg_error_exit;
	if (setjmp (tejerr.setjmp_buffer)) {
181
		success = FALSE;
182 183
		goto fail;
	}
184

185 186 187 188
	metadata = tracker_resource_new (NULL);
	tracker_resource_add_uri (metadata, "rdf:type", "nfo:Image");
	tracker_resource_add_uri (metadata, "rdf:type", "nmm:Photo");

189
	jpeg_create_decompress (&cinfo);
190

191 192 193
	jpeg_save_markers (&cinfo, JPEG_COM, 0xFFFF);
	jpeg_save_markers (&cinfo, JPEG_APP0 + 1, 0xFFFF);
	jpeg_save_markers (&cinfo, JPEG_APP0 + 13, 0xFFFF);
194

195
	jpeg_stdio_src (&cinfo, f);
196

197
	jpeg_read_header (&cinfo, TRUE);
198

199 200 201 202 203 204 205 206
	/* FIXME? It is possible that there are markers after SOS,
	 * but there shouldn't be. Should we decompress the whole file?
	 *
	 * jpeg_start_decompress(&cinfo);
	 * jpeg_finish_decompress(&cinfo);
	 *
	 * jpeg_calc_output_dimensions(&cinfo);
	 */
207

208
	marker = (struct jpeg_marker_struct *) &cinfo.marker_list;
209

210 211 212
	while (marker) {
		gchar *str;
		gsize len;
213
#ifdef HAVE_LIBIPTCDATA
214 215
		gsize offset;
		guint sublen;
216 217
#endif /* HAVE_LIBIPTCDATA */

218 219 220 221 222
		switch (marker->marker) {
		case JPEG_COM:
			g_free (comment);
			comment = g_strndup ((gchar*) marker->data, marker->data_length);
			break;
223

224 225 226
		case JPEG_APP0 + 1:
			str = (gchar*) marker->data;
			len = marker->data_length;
227

228
#ifdef HAVE_LIBEXIF
229
			if (strncmp (EXIF_NAMESPACE, str, EXIF_NAMESPACE_LENGTH) == 0) {
230
				ed = tracker_exif_new ((guchar *) marker->data, len, uri);
231
			}
232 233
#endif /* HAVE_LIBEXIF */

234
#ifdef HAVE_EXEMPI
235
			if (strncmp (XMP_NAMESPACE, str, XMP_NAMESPACE_LENGTH) == 0) {
236 237 238
				xd = tracker_xmp_new (str + XMP_NAMESPACE_LENGTH,
				                      len - XMP_NAMESPACE_LENGTH,
				                      uri);
239
			}
240
#endif /* HAVE_EXEMPI */
241

242
			break;
243

244 245 246
		case JPEG_APP0 + 13:
			str = (gchar*) marker->data;
			len = marker->data_length;
247
#ifdef HAVE_LIBIPTCDATA
248
			if (len > 0 && strncmp (PS3_NAMESPACE, str, PS3_NAMESPACE_LENGTH) == 0) {
249
				offset = iptc_jpeg_ps3_find_iptc (str, len, &sublen);
250
				if (offset > 0 && sublen > 0) {
251
					id = tracker_iptc_new (str + offset, sublen, uri);
252
				}
253
			}
254
#endif /* HAVE_LIBIPTCDATA */
255

256
			break;
257

258
		default:
259
			marker = marker->next;
260
			continue;
261 262
		}

263 264
		marker = marker->next;
	}
265

266 267 268 269 270 271 272 273 274 275 276 277
	if (!ed) {
		ed = g_new0 (TrackerExifData, 1);
	}

	if (!xd) {
		xd = g_new0 (TrackerXmpData, 1);
	}

	if (!id) {
		id = g_new0 (TrackerIptcData, 1);
	}

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
	md.title = tracker_coalesce_strip (4, xd->title, ed->document_name, xd->title2, xd->pdf_title);
	md.orientation = tracker_coalesce_strip (3, xd->orientation, ed->orientation, id->image_orientation);
	md.copyright = tracker_coalesce_strip (4, xd->copyright, xd->rights, ed->copyright, id->copyright_notice);
	md.white_balance = tracker_coalesce_strip (2, xd->white_balance, ed->white_balance);
	md.fnumber = tracker_coalesce_strip (2, xd->fnumber, ed->fnumber);
	md.flash = tracker_coalesce_strip (2, xd->flash, ed->flash);
	md.focal_length =  tracker_coalesce_strip (2, xd->focal_length, ed->focal_length);
	md.artist = tracker_coalesce_strip (3, xd->artist, ed->artist, xd->contributor);
	md.exposure_time = tracker_coalesce_strip (2, xd->exposure_time, ed->exposure_time);
	md.iso_speed_ratings = tracker_coalesce_strip (2, xd->iso_speed_ratings, ed->iso_speed_ratings);
	md.date = tracker_coalesce_strip (5, xd->date, xd->time_original, ed->time, id->date_created, ed->time_original);
	md.description = tracker_coalesce_strip (2, xd->description, ed->description);
	md.metering_mode = tracker_coalesce_strip (2, xd->metering_mode, ed->metering_mode);
	md.city = tracker_coalesce_strip (2, xd->city, id->city);
	md.state = tracker_coalesce_strip (2, xd->state, id->state);
	md.address = tracker_coalesce_strip (2, xd->address, id->sublocation);
	md.country = tracker_coalesce_strip (2, xd->country, id->country_name);
295 296

	/* FIXME We are not handling the altitude ref here for xmp */
297 298 299
	md.gps_altitude = tracker_coalesce_strip (2, xd->gps_altitude, ed->gps_altitude);
	md.gps_latitude = tracker_coalesce_strip (2, xd->gps_latitude, ed->gps_latitude);
	md.gps_longitude = tracker_coalesce_strip (2, xd->gps_longitude, ed->gps_longitude);
300
	md.gps_direction = tracker_coalesce_strip (2, xd->gps_direction, ed->gps_direction);
301 302
	md.creator = tracker_coalesce_strip (3, xd->creator, id->byline, id->credit);
	md.comment = tracker_coalesce_strip (2, comment, ed->user_comment);
303 304
	md.make = tracker_coalesce_strip (2, xd->make, ed->make);
	md.model = tracker_coalesce_strip (2, xd->model, ed->model);
305 306

	/* Prioritize on native dimention in all cases */
307 308
	tracker_resource_set_int64 (metadata, "nfo:width", cinfo.image_width);
	tracker_resource_set_int64 (metadata, "nfo:height", cinfo.image_height);
309

310
	if (guess_dlna_profile (cinfo.image_width, cinfo.image_height, &dlna_profile, &dlna_mimetype)) {
311 312
		tracker_resource_set_string (metadata, "nmm:dlnaProfile", dlna_profile);
		tracker_resource_set_string (metadata, "nmm:dlnaMime", dlna_mimetype);
313 314
	}

315
	if (id->contact) {
316 317 318
		TrackerResource *contact = tracker_extract_new_contact (id->contact);
		tracker_resource_add_relation (metadata, "nco:contributor", contact);
		g_object_unref (contact);
319
	}
320

321
	keywords = g_ptr_array_new_with_free_func ((GDestroyNotify) g_free);
322

323
	if (xd->keywords) {
324
		tracker_keywords_parse (keywords, xd->keywords);
325
	}
326

327
	if (xd->pdf_keywords) {
328
		tracker_keywords_parse (keywords, xd->pdf_keywords);
329
	}
330

331
	if (xd->subject) {
332
		tracker_keywords_parse (keywords, xd->subject);
333
	}
334

335
	if (xd->publisher) {
336 337 338
		TrackerResource *publisher = tracker_extract_new_contact (xd->publisher);
		tracker_resource_add_relation (metadata, "nco:publisher", publisher);
		g_object_unref (publisher);
339
	}
340

341
	if (xd->type) {
342
		tracker_resource_set_string (metadata, "dc:type", xd->type);
343
	}
344

345
	if (xd->rating) {
346
		tracker_resource_set_string (metadata, "nao:numericRating", xd->rating);
347
	}
348

349
	if (xd->format) {
350
		tracker_resource_set_string (metadata, "dc:format", xd->format);
351
	}
352

353
	if (xd->identifier) {
354
		tracker_resource_set_string (metadata, "dc:identifier", xd->identifier);
355
	}
356

357
	if (xd->source) {
358
		tracker_resource_set_string (metadata, "dc:source", xd->source);
359
	}
360

361
	if (xd->language) {
362
		tracker_resource_set_string (metadata, "dc:language", xd->language);
363
	}
364

365
	if (xd->relation) {
366
		tracker_resource_set_string (metadata, "dc:relation", xd->relation);
367
	}
368

369
	if (xd->coverage) {
370
		tracker_resource_set_string (metadata, "dc:coverage", xd->coverage);
371
	}
372

373
	if (xd->license) {
374
		tracker_resource_set_string (metadata, "nie:license", xd->license);
375
	}
376

377 378 379
	if (xd->regions) {
		tracker_xmp_apply_regions_to_resource (metadata, xd);
	}
380

381
	if (id->keywords) {
382 383 384 385
		tracker_keywords_parse (keywords, id->keywords);
	}

	for (i = 0; i < keywords->len; i++) {
386
		TrackerResource *tag;
387
		const gchar *p;
388 389

		p = g_ptr_array_index (keywords, i);
390 391 392 393 394
		tag = tracker_extract_new_tag (p);

		tracker_resource_add_relation (metadata, "nao:hasTag", tag);

		g_object_unref (tag);
395
	}
396
	g_ptr_array_free (keywords, TRUE);
397

398
	if (md.make || md.model) {
399 400 401
		TrackerResource *equipment = tracker_extract_new_equipment (md.make, md.model);
		tracker_resource_add_relation (metadata, "nfo:equipment", equipment);
		g_object_unref (equipment);
402
	}
403

404 405 406 407 408
	tracker_guarantee_resource_title_from_file (metadata,
	                                            "nie:title",
	                                            md.title,
	                                            uri,
	                                            NULL);
409

410
	if (md.orientation) {
411 412 413 414 415
		TrackerResource *orientation;

		orientation = tracker_resource_new (md.orientation);
		tracker_resource_set_relation (metadata, "nfo:orientation", orientation);
		g_object_unref (orientation);
416
	}
417

418
	if (md.copyright) {
419
		tracker_guarantee_resource_utf8_string (metadata, "nie:copyright", md.copyright);
420
	}
421

422
	if (md.white_balance) {
423 424 425
		TrackerResource *white_balance;

		white_balance = tracker_resource_new (md.white_balance);
426
		tracker_resource_set_relation (metadata, "nmm:whiteBalance", white_balance);
427
		g_object_unref (white_balance);
428
	}
429

430 431
	if (md.fnumber) {
		gdouble value;
432

433
		value = g_strtod (md.fnumber, NULL);
434
		tracker_resource_set_double (metadata, "nmm:fnumber", value);
435
	}
436

437
	if (md.flash) {
438 439 440 441 442
		TrackerResource *flash;

		flash = tracker_resource_new (md.flash);
		tracker_resource_set_relation (metadata, "nmm:flash", flash);
		g_object_unref (flash);
443
	}
444

445 446
	if (md.focal_length) {
		gdouble value;
447

448
		value = g_strtod (md.focal_length, NULL);
449
		tracker_resource_set_double (metadata, "nmm:focalLength", value);
450
	}
451

452
	if (md.artist) {
453 454 455
		TrackerResource *artist = tracker_extract_new_contact (md.artist);
		tracker_resource_add_relation (metadata, "nco:contributor", artist);
		g_object_unref (artist);
456
	}
457

458 459
	if (md.exposure_time) {
		gdouble value;
460

461
		value = g_strtod (md.exposure_time, NULL);
462
		tracker_resource_set_double (metadata, "nmm:exposureTime", value);
463
	}
464

465 466
	if (md.iso_speed_ratings) {
		gdouble value;
467

468
		value = g_strtod (md.iso_speed_ratings, NULL);
469
		tracker_resource_set_double (metadata, "nmm:isoSpeed", value);
470
	}
471

472 473 474 475
	tracker_guarantee_resource_date_from_file_mtime (metadata,
	                                                 "nie:contentCreated",
	                                                 md.date,
	                                                 uri);
476

477
	if (md.description) {
478
		tracker_guarantee_resource_utf8_string (metadata, "nie:description", md.description);
479
	}
480

481
	if (md.metering_mode) {
482 483 484 485 486
		TrackerResource *metering;

		metering = tracker_resource_new (md.metering_mode);
		tracker_resource_set_relation (metadata, "nmm:meteringMode", metering);
		g_object_unref (metering);
487
	}
488

489
	if (md.creator) {
490 491 492
		TrackerResource *creator = tracker_extract_new_contact (md.creator);
		tracker_resource_add_relation (metadata, "nco:creator", creator);
		g_object_unref (creator);
493 494 495 496 497

		/* NOTE: We only have affiliation with
		 * nco:PersonContact and we are using
		 * nco:Contact here.
		 */
498

499
		/* if (id->byline_title) { */
500
		/* 	tracker_sparql_builder_insert_open (preupdate, NULL); */
501

502 503 504
		/* 	tracker_sparql_builder_subject (preupdate, "_:affiliation_by_line"); */
		/* 	tracker_sparql_builder_predicate (preupdate, "a"); */
		/* 	tracker_sparql_builder_object (preupdate, "nco:Affiliation"); */
505

506
		/* 	tracker_sparql_builder_predicate (preupdate, "nco:title"); */
507
		/* 	tracker_sparql_builder_object_unvalidated (preupdate, id->byline_title); */
508

509
		/* 	tracker_sparql_builder_insert_close (preupdate); */
510

511 512 513 514 515 516
		/*      tracker_sparql_builder_predicate (preupdate, "a"); */
		/*      tracker_sparql_builder_object (preupdate, "nco:Contact"); */
		/*      tracker_sparql_builder_predicate (preupdate, "nco:hasAffiliation"); */
		/*      tracker_sparql_builder_object (preupdate, "_:affiliation_by_line"); */
		/* } */
	}
Jürg Billeter's avatar
Jürg Billeter committed
517

518
	if (md.comment) {
519
		tracker_guarantee_resource_utf8_string (metadata, "nie:comment", md.comment);
520
	}
521

522 523
	if (md.address || md.state || md.country || md.city ||
	    md.gps_altitude || md.gps_latitude || md.gps_longitude) {
524

525 526 527
		TrackerResource *location = tracker_extract_new_location (md.address,
		        md.state, md.city, md.country, md.gps_altitude,
		        md.gps_latitude, md.gps_longitude);
528

529
		tracker_resource_add_relation (metadata, "slo:location", location);
530

531
		g_object_unref (location);
532
	}
Jürg Billeter's avatar
Jürg Billeter committed
533

534
	if (md.gps_direction) {
535
		tracker_resource_set_string (metadata, "nfo:heading", md.gps_direction);
536 537
	}

538
	if (cinfo.density_unit != 0 || ed->x_resolution) {
539 540
		gdouble value;

541 542
		if (cinfo.density_unit == JPEG_RESOLUTION_UNIT_UNKNOWN) {
			if (ed->resolution_unit == EXIF_RESOLUTION_UNIT_PER_CENTIMETER)
543
				value = g_strtod (ed->x_resolution, NULL) * CMS_PER_INCH;
544 545
			else
				value = g_strtod (ed->x_resolution, NULL);
546
		} else {
547
			if (cinfo.density_unit == JPEG_RESOLUTION_UNIT_PER_INCH)
548 549
				value = cinfo.X_density;
			else
550
				value = cinfo.X_density * CMS_PER_INCH;
551 552
		}

553
		tracker_resource_set_double (metadata, "nfo:horizontalResolution", value);
554 555
	}

556
	if (cinfo.density_unit != 0 || ed->y_resolution) {
557 558
		gdouble value;

559 560
		if (cinfo.density_unit == JPEG_RESOLUTION_UNIT_UNKNOWN) {
			if (ed->resolution_unit == EXIF_RESOLUTION_UNIT_PER_CENTIMETER)
561
				value = g_strtod (ed->y_resolution, NULL) * CMS_PER_INCH;
562 563
			else
				value = g_strtod (ed->y_resolution, NULL);
564
		} else {
565
			if (cinfo.density_unit == JPEG_RESOLUTION_UNIT_PER_INCH)
566 567
				value = cinfo.Y_density;
			else
568
				value = cinfo.Y_density * CMS_PER_INCH;
569 570
		}

571
		tracker_resource_set_double (metadata, "nfo:verticalResolution", value);
572 573
	}

574 575
	jpeg_destroy_decompress (&cinfo);

576 577 578
	tracker_exif_free (ed);
	tracker_xmp_free (xd);
	tracker_iptc_free (id);
Carlos Garnacho's avatar
Carlos Garnacho committed
579
	g_free (comment);
580

581 582 583
	tracker_extract_info_set_resource (info, metadata);
	g_object_unref (metadata);

584 585
fail:
	tracker_file_close (f, FALSE);
586
	g_free (uri);
587

588
	return success;
589
}