2 * Copyright (C) 2007 by INdT
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * @author Gustavo Sverzut Barbieri <gustavo.barbieri@openbossa.org>
24 * Reads EXIF tags from images.
26 * @todo: get GPS data.
27 * @todo: check if worth using mmap().
34 #define _XOPEN_SOURCE 600
35 #include <lightmediascanner_plugin.h>
36 #include <lightmediascanner_utils.h>
37 #include <lightmediascanner_db.h>
38 #include <sys/types.h>
49 JPEG_MARKER_SOI = 0xd8,
50 JPEG_MARKER_JFIF = 0xe0,
51 JPEG_MARKER_EXIF = 0xe1,
52 JPEG_MARKER_COMM = 0xfe,
53 JPEG_MARKER_SOF0 = 0xc0,
54 JPEG_MARKER_SOF1 = 0xc1,
55 JPEG_MARKER_SOF2 = 0xc2,
56 JPEG_MARKER_SOF9 = 0xc9,
57 JPEG_MARKER_SOF10 = 0xca,
58 JPEG_MARKER_SOS = 0xda
62 * Process SOF JPEG, this contains width and height.
65 _jpeg_sof_process(int fd, unsigned short *width, unsigned short *height)
69 if (read(fd, buf, 6) != 6) {
70 perror("could not read() SOF data");
74 *height = (buf[1] << 8) | buf[2];
75 *width = (buf[3] << 8) | buf[4];
81 * Process COM JPEG, this contains user comment.
84 _jpeg_com_process(int fd, int len, struct lms_string_size *comment)
92 comment->str = malloc(len + 1);
97 if (read(fd, comment->str, len) != len) {
104 if (comment->str[len - 1] == '\0')
107 comment->str[len] = '\0';
110 lms_strstrip(comment->str, &comment->len);
111 if (comment->len == 0) {
120 * Walk JPEG markers in order to get useful information.
123 _jpeg_info_get(int fd, int len, struct lms_image_info *info)
125 unsigned char buf[4];
129 found = info->title.str ? 1 : 0;
130 offset = lseek(fd, len - 2, SEEK_CUR);
133 offset = lseek(fd, offset + len, SEEK_SET);
139 if (read(fd, buf, 4) != 4) {
144 len = ((buf[2] << 8) | buf[3]) - 2;
146 if (buf[0] != 0xff) {
147 fprintf(stderr, "ERROR: expected 0xff marker, got %#x\n", buf[0]);
151 if (buf[1] == JPEG_MARKER_SOF0 ||
152 buf[1] == JPEG_MARKER_SOF1 ||
153 buf[1] == JPEG_MARKER_SOF2 ||
154 buf[1] == JPEG_MARKER_SOF9 ||
155 buf[1] == JPEG_MARKER_SOF10) {
156 if (_jpeg_sof_process(fd, &info->width, &info->height) != 0)
159 } else if (buf[1] == JPEG_MARKER_COMM && !info->title.str) {
160 if (_jpeg_com_process(fd, len, &info->title) != 0)
163 } else if (buf[1] == JPEG_MARKER_SOS)
166 len += 4; /* add read size */
173 * Read JPEG file start (0xffd8 marker) and return the next
174 * marker type and its length.
177 _jpeg_data_get(int fd, int *type, int *len)
179 unsigned char buf[6];
181 if (lseek(fd, 0, SEEK_SET) != 0) {
186 if (read(fd, buf, 6) != 6) {
191 if (buf[0] != 0xff || buf[1] != JPEG_MARKER_SOI || buf[2] != 0xff) {
192 fprintf(stderr, "ERROR: not JPEG file (magic=%#x %#x %#x)\n",
193 buf[0], buf[1], buf[2]);
198 *len = (buf[4] << 8) | buf[5];
203 #define LE_4BYTE(a) ((a)[0] | ((a)[1] << 8) | ((a)[2] << 16) | ((a)[3] << 24))
204 #define BE_4BYTE(a) (((a)[0] << 24) | ((a)[1] << 16) | ((a)[2] << 8) | (a)[3])
206 #define LE_2BYTE(a) ((a)[0] | ((a)[1] << 8))
207 #define BE_2BYTE(a) (((a)[0] << 8) | (a)[1])
209 #define E_2BTYE(little_endian, a) ((little_endian) ? LE_2BYTE(a) : BE_2BYTE(a))
210 #define E_4BTYE(little_endian, a) ((little_endian) ? LE_4BYTE(a) : BE_4BYTE(a))
213 EXIF_TYPE_BYTE = 1, /* 8 bit unsigned */
214 EXIF_TYPE_ASCII = 2, /* 8 bit byte with 7-bit ASCII code, NULL terminated */
215 EXIF_TYPE_SHORT = 3, /* 2-byte unsigned integer */
216 EXIF_TYPE_LONG = 4, /* 4-byte unsigned integer */
217 EXIF_TYPE_RATIONAL = 5, /* 2 4-byte unsigned integer, 1st = numerator */
218 EXIF_TYPE_UNDEFINED = 7, /* 8-bit byte */
219 EXIF_TYPE_SLONG = 9, /* 4-byte signed integer (2'complement) */
220 EXIF_TYPE_SRATIONAL = 10 /* 2 4-byte signed integer, 1st = numerator */
224 EXIF_TAG_ORIENTATION = 0x0112,
225 EXIF_TAG_ARTIST = 0x013b,
226 EXIF_TAG_USER_COMMENT = 0x9286,
227 EXIF_TAG_IMAGE_DESCRIPTION = 0x010e,
228 EXIF_TAG_DATE_TIME = 0x0132,
229 EXIF_TAG_DATE_TIME_ORIGINAL = 0x9003,
230 EXIF_TAG_DATE_TIME_DIGITIZED = 0x9004,
231 EXIF_TAG_EXIF_IFD_POINTER = 0x8769
243 * Read IFD from stream.
246 _exif_ifd_get(int fd, int little_endian, struct exif_ifd *ifd)
248 unsigned char buf[12];
250 if (read(fd, buf, 12) != 12) {
256 ifd->tag = LE_2BYTE(buf);
257 ifd->type = LE_2BYTE(buf + 2);
258 ifd->count = LE_4BYTE(buf + 4);
259 ifd->offset = LE_4BYTE(buf + 8);
261 ifd->tag = BE_2BYTE(buf);
262 ifd->type = BE_2BYTE(buf + 2);
263 ifd->count = BE_4BYTE(buf + 4);
264 ifd->offset = BE_4BYTE(buf + 8);
270 * Get non-exif data based on Exif tag offset.
272 * This will setup the file description position and call _jpeg_info_get().
275 _exif_extra_get(int fd, int abs_offset, int len, struct lms_image_info *info)
277 if (lseek(fd, abs_offset, SEEK_SET) == -1) {
282 if (_jpeg_info_get(fd, len, info) != 0) {
283 fprintf(stderr, "ERROR: could not get image size.\n");
290 _exif_text_encoding_get(int fd, unsigned int count, int offset, struct lms_string_size *s)
295 count -= 8; /* XXX don't just ignore character code, handle it. */
298 if (lseek(fd, offset, SEEK_SET) == -1) {
303 s->str = malloc(count + 1);
305 if (read(fd, s->str, count) != count) {
312 s->str[count] = '\0';
315 lms_strstrip(s->str, &s->len);
325 _exif_text_ascii_get(int fd, unsigned int count, int offset, struct lms_string_size *s)
333 if (lseek(fd, offset, SEEK_SET) == -1) {
338 s->str = malloc(count);
340 if (read(fd, s->str, count) != count) {
347 s->str[count - 1] = '\0';
350 lms_strstrip(s->str, &s->len);
360 _exif_datetime_get(int fd, int offset)
365 if (lseek(fd, offset, SEEK_SET) == -1) {
370 if (read(fd, buf, 20) != 20) {
376 if (strptime(buf, "%Y:%m:%d %H:%M:%S", &tm)) {
382 static int _exif_private_ifd_get(int fd, int base_offset, int offset, int little_endian, struct lms_image_info *info);
385 * Process IFD contents.
388 _exif_ifd_process(int fd, int count, int ifd_offset, int tiff_base, int little_endian, struct lms_image_info *info)
390 int i, torig, tdig, tlast;
392 torig = tdig = tlast = 0;
394 for (i = 0; i < count; i++) {
397 lseek(fd, ifd_offset + i * 12, SEEK_SET);
398 if (_exif_ifd_get(fd, little_endian, &ifd) != 0) {
399 fprintf(stderr, "ERROR: could not read Exif IFD.\n");
404 case EXIF_TAG_ORIENTATION:
405 info->orientation = ifd.offset >> 16;
407 case EXIF_TAG_ARTIST:
408 if (!info->artist.str)
409 _exif_text_ascii_get(fd, ifd.count, tiff_base + ifd.offset,
412 case EXIF_TAG_USER_COMMENT:
413 if (!info->title.str)
414 _exif_text_encoding_get(fd, ifd.count, tiff_base + ifd.offset,
417 case EXIF_TAG_IMAGE_DESCRIPTION:
418 if (!info->title.str)
419 _exif_text_ascii_get(fd, ifd.count, tiff_base + ifd.offset,
422 case EXIF_TAG_DATE_TIME:
423 if (torig == 0 && info->date == 0)
424 tlast = _exif_datetime_get(fd, tiff_base + ifd.offset);
426 case EXIF_TAG_DATE_TIME_ORIGINAL:
427 if (torig == 0 && info->date == 0)
428 torig = _exif_datetime_get(fd, tiff_base + ifd.offset);
430 case EXIF_TAG_DATE_TIME_DIGITIZED:
431 if (torig == 0 && info->date == 0)
432 tdig = _exif_datetime_get(fd, tiff_base + ifd.offset);
434 case EXIF_TAG_EXIF_IFD_POINTER:
435 if (ifd.count == 1 && ifd.type == EXIF_TYPE_LONG)
436 _exif_private_ifd_get(fd, ifd.offset, tiff_base,
437 little_endian, info);
445 if (info->date == 0) {
458 * Process Exif IFD (Exif Private Tag), with more specific info.
461 _exif_private_ifd_get(int fd, int ifd_offset, int tiff_base, int little_endian, struct lms_image_info *info)
466 if (lseek(fd, tiff_base + ifd_offset, SEEK_SET) == -1) {
471 if (read(fd, buf, 2) != 2) {
476 count = E_2BTYE(little_endian, buf);
477 return _exif_ifd_process(fd, count, ifd_offset + 2, tiff_base,
478 little_endian, info);
482 * Process file as it being Exif, will extract Exif as well as other
483 * JPEG markers (comment, size).
486 _exif_data_get(int fd, int len, struct lms_image_info *info)
488 const unsigned char exif_hdr[6] = "Exif\0";
489 unsigned char buf[8];
490 unsigned int little_endian, offset, count;
491 off_t abs_offset, tiff_base;
493 abs_offset = lseek(fd, 0, SEEK_CUR);
494 if (abs_offset == -1) {
499 if (read(fd, buf, 6) != 6) {
504 memset(info, 0, sizeof(*info));
505 info->orientation = 1;
507 if (memcmp(buf, exif_hdr, 6) != 0)
508 return _exif_extra_get(fd, abs_offset, len, info);
510 if (read(fd, buf, 8) != 8) {
515 if (buf[0] == 'I' && buf[1] == 'I') {
517 offset = LE_4BYTE(buf + 4);
518 } else if (buf[0] == 'M' && buf[1] == 'M') {
520 offset = BE_4BYTE(buf + 4);
522 fprintf(stderr, "ERROR: undefined byte sex \"%2.2s\".\n", buf);
527 if (offset > 0 && lseek(fd, offset, SEEK_CUR) == -1) {
532 tiff_base = abs_offset + 6; /* offsets are relative to TIFF base */
534 if (read(fd, buf, 2) != 2) {
538 count = E_2BTYE(little_endian, buf);
540 _exif_ifd_process(fd, count, tiff_base + 8 + 2, tiff_base,
541 little_endian, info);
543 return _exif_extra_get(fd, abs_offset, len, info);
547 * Process file as it being JFIF
550 _jfif_data_get(int fd, int len, struct lms_image_info *info)
552 unsigned char buf[4];
554 memset(info, 0, sizeof(*info));
555 info->orientation = 1;
557 /* JFIF provides no useful information, try to find out Exif */
558 if (lseek(fd, len - 2, SEEK_CUR) == -1) {
563 if (read(fd, buf, 4) != 4) {
568 len = ((buf[2] << 8) | buf[3]);
569 if (buf[0] != 0xff) {
570 fprintf(stderr, "ERROR: expected 0xff marker, got %#x\n", buf[0]);
574 if (buf[1] == JPEG_MARKER_EXIF)
575 return _exif_data_get(fd, len, info);
577 return _jpeg_info_get(fd, len, info);
580 static const char _name[] = "jpeg";
581 static const struct lms_string_size _exts[] = {
582 LMS_STATIC_STRING_SIZE(".jpg"),
583 LMS_STATIC_STRING_SIZE(".jpeg"),
584 LMS_STATIC_STRING_SIZE(".jpe")
588 struct lms_plugin plugin;
589 lms_db_image_t *img_db;
593 _match(struct plugin *p, const char *path, int len, int base)
597 i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
601 return (void*)(i + 1);
605 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
607 struct lms_image_info info = {0};
608 int fd, type, len, r;
610 fd = open(finfo->path, O_RDONLY);
616 if (_jpeg_data_get(fd, &type, &len) != 0) {
621 if (type == JPEG_MARKER_EXIF) {
622 if (_exif_data_get(fd, len, &info) != 0) {
623 fprintf(stderr, "ERROR: could not get EXIF info (%s).\n",
628 } else if (type == JPEG_MARKER_JFIF) {
629 if (_jfif_data_get(fd, len, &info) != 0) {
630 fprintf(stderr, "ERROR: could not get JPEG size (%s).\n",
636 fprintf(stderr, "ERROR: unsupported JPEG marker %#x (%s)\n", type,
643 info.date = finfo->mtime;
645 if (!info.title.str) {
648 ext_idx = ((int)match) - 1;
649 info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
650 info.title.str = malloc((info.title.len + 1) * sizeof(char));
651 memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
652 info.title.str[info.title.len] = '\0';
656 lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
658 lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
661 r = lms_db_image_add(plugin->img_db, &info);
665 free(info.title.str);
667 free(info.artist.str);
669 posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
676 _setup(struct plugin *plugin, struct lms_context *ctxt)
678 plugin->img_db = lms_db_image_new(ctxt->db);
686 _start(struct plugin *plugin, struct lms_context *ctxt)
688 return lms_db_image_start(plugin->img_db);
692 _finish(struct plugin *plugin, struct lms_context *ctxt)
695 return lms_db_image_free(plugin->img_db);
702 _close(struct plugin *plugin)
708 API struct lms_plugin *
709 lms_plugin_open(void)
711 struct plugin *plugin;
713 plugin = malloc(sizeof(*plugin));
714 plugin->plugin.name = _name;
715 plugin->plugin.match = (lms_plugin_match_fn_t)_match;
716 plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
717 plugin->plugin.close = (lms_plugin_close_fn_t)_close;
718 plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
719 plugin->plugin.start = (lms_plugin_start_fn_t)_start;
720 plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
722 return (struct lms_plugin *)plugin;