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>
32 #define _XOPEN_SOURCE 600
33 #include <lightmediascanner_plugin.h>
34 #include <lightmediascanner_utils.h>
35 #include <lightmediascanner_db.h>
36 #include <sys/types.h>
46 static inline unsigned int
47 _chunk_to_uint(unsigned char *buf)
49 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
53 _png_data_get(int fd, struct lms_image_info *info)
55 unsigned char buf[16], *p;
56 const unsigned char sig[8] = {0x89, 0x50, 0x4e, 0x47, 0xd, 0xa, 0x1a, 0xa};
57 const unsigned char ihdr[4] = {'I', 'H', 'D', 'R'};
60 if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
65 if (memcmp(buf, sig, sizeof(sig)) != 0) {
66 fprintf(stderr, "ERROR: invalid PNG signature.\n");
70 p = buf + sizeof(sig) + 4;
71 if (memcmp(p, ihdr, sizeof(ihdr)) != 0) {
72 fprintf(stderr, "ERROR: invalid first chunk: %4.4s.\n", p);
76 p = buf + sizeof(sig);
77 length = _chunk_to_uint(p);
79 fprintf(stderr, "ERROR: IHDR chunk size is too small: %d.\n", length);
83 if (read(fd, buf, 8) != 8) {
87 info->width = _chunk_to_uint(buf);
88 info->height = _chunk_to_uint(buf + 4);
93 static const char _name[] = "png";
94 static const struct lms_string_size _exts[] = {
95 LMS_STATIC_STRING_SIZE(".png")
99 struct lms_plugin plugin;
100 lms_db_image_t *img_db;
104 _match(struct plugin *p, const char *path, int len, int base)
108 i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
112 return (void*)(i + 1);
116 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
118 struct lms_image_info info = {0};
121 fd = open(finfo->path, O_RDONLY);
127 if (_png_data_get(fd, &info) != 0) {
133 info.date = finfo->mtime;
135 if (!info.title.str) {
138 ext_idx = ((int)match) - 1;
139 info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
140 info.title.str = malloc((info.title.len + 1) * sizeof(char));
141 memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
142 info.title.str[info.title.len] = '\0';
146 lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
148 lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
151 r = lms_db_image_add(plugin->img_db, &info);
155 free(info.title.str);
157 free(info.artist.str);
159 posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
166 _setup(struct plugin *plugin, struct lms_context *ctxt)
168 plugin->img_db = lms_db_image_new(ctxt->db);
176 _start(struct plugin *plugin, struct lms_context *ctxt)
178 return lms_db_image_start(plugin->img_db);
182 _finish(struct plugin *plugin, struct lms_context *ctxt)
185 return lms_db_image_free(plugin->img_db);
192 _close(struct plugin *plugin)
198 API struct lms_plugin *
199 lms_plugin_open(void)
201 struct plugin *plugin;
203 plugin = malloc(sizeof(*plugin));
204 plugin->plugin.name = _name;
205 plugin->plugin.match = (lms_plugin_match_fn_t)_match;
206 plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
207 plugin->plugin.close = (lms_plugin_close_fn_t)_close;
208 plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
209 plugin->plugin.start = (lms_plugin_start_fn_t)_start;
210 plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
212 return (struct lms_plugin *)plugin;