blob: 59401d38705ba1d59c0653d1d3d20388ad36fd69 [file] [log] [blame]
Linus Walleij3d78c4c2007-02-15 12:18:12 +00001/**
2 * \file thumb.c
3 * Example program to send and associate album art to an entity
4 * on a device.
5 *
6 * Copyright (C) 2006 Robert Reardon <rreardon@monkshatch.vispa.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 */
rreardon5332f9c2006-12-05 10:18:23 +000023#include "common.h"
24#include "string.h"
25#include <sys/stat.h>
26#include <fcntl.h>
27#include <errno.h>
28
29static void usage(void) {
30 printf("Usage: thumb -i <fileid/trackid> <imagefile>\n");
31 exit(0);
32}
33
34int main (int argc, char **argv) {
35 int opt;
36 extern int optind;
37 extern char *optarg;
38 LIBMTP_mtpdevice_t *device = NULL;
39 int fd;
40 uint32_t id = 0;
41 uint64_t filesize;
42 uint8_t *imagedata = NULL;
43 char *path = NULL;
Linus Walleij20c3b672007-11-09 19:02:19 +000044 char *rest;
rreardon5332f9c2006-12-05 10:18:23 +000045 struct stat statbuff;
46 int ret;
47
tedbullockcd9f4992007-03-29 06:00:40 +000048 fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
49
rreardon5332f9c2006-12-05 10:18:23 +000050 while ( (opt = getopt(argc, argv, "hi:")) != -1 ) {
51 switch (opt) {
52 case 'h':
53 usage();
54 case 'i':
Linus Walleij20c3b672007-11-09 19:02:19 +000055 id = strtoul(optarg, &rest, 0);
rreardon5332f9c2006-12-05 10:18:23 +000056 break;
57 default:
58 usage();
59 }
60 }
61 argc -= optind;
62 argv += optind;
63
64 if ( argc != 1 ) {
65 printf("You need to pass a filename.\n");
66 usage();
67 }
68
69 path = argv[0];
70
71 if ( stat(path, &statbuff) == -1 ) {
72 fprintf(stderr, "%s: ", path);
73 perror("stat");
74 exit(1);
75 }
76 filesize = (uint64_t) statbuff.st_size;
77 imagedata = malloc(filesize * sizeof(uint16_t));
78
79#ifdef __WIN32__
80 if ( (fd = open(path, O_RDONLY|O_BINARY) == -1 ) {
81#else
82 if ( (fd = open(path, O_RDONLY)) == -1) {
83#endif
84 printf("Couldn't open image file %s (%s)\n",path,strerror(errno));
85 return 1;
86 }
87 else {
88 read(fd, imagedata, filesize);
89 close(fd);
90 }
91
92 LIBMTP_Init();
93 device = LIBMTP_Get_First_Device();
94 if (device == NULL) {
95 printf("No devices.\n");
96 return 0;
97 }
98
99 LIBMTP_filesampledata_t *thumb = LIBMTP_new_filesampledata_t();
100
101 int i;
102 thumb->data = malloc(sizeof(uint16_t) * filesize);
103 for (i = 0; i < filesize; i++) {
104 thumb->data[i] = imagedata[i];
105 }
106
107 thumb->size = filesize;
108 thumb->filetype = LIBMTP_FILETYPE_JPEG;
109
110 ret = LIBMTP_Send_Representative_Sample(device,id,thumb);
111 if (ret != 0) {
112 printf("Couldn't send thumbnail\n");
Linus Walleij070e9b42007-01-22 08:49:28 +0000113 LIBMTP_Dump_Errorstack(device);
114 LIBMTP_Clear_Errorstack(device);
rreardon5332f9c2006-12-05 10:18:23 +0000115 }
116
117 free(imagedata);
Linus Walleijf1b02f22006-12-06 09:03:23 +0000118 LIBMTP_destroy_filesampledata_t(thumb);
rreardon5332f9c2006-12-05 10:18:23 +0000119
120 LIBMTP_Release_Device(device);
121 printf("OK.\n");
122 return 0;
123}