blob: e235a21e757883e1ba44566b3fa6710c9a8b8518 [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;
44 struct stat statbuff;
45 int ret;
46
47 while ( (opt = getopt(argc, argv, "hi:")) != -1 ) {
48 switch (opt) {
49 case 'h':
50 usage();
51 case 'i':
52 id = atoi(strdup(optarg));
53 break;
54 default:
55 usage();
56 }
57 }
58 argc -= optind;
59 argv += optind;
60
61 if ( argc != 1 ) {
62 printf("You need to pass a filename.\n");
63 usage();
64 }
65
66 path = argv[0];
67
68 if ( stat(path, &statbuff) == -1 ) {
69 fprintf(stderr, "%s: ", path);
70 perror("stat");
71 exit(1);
72 }
73 filesize = (uint64_t) statbuff.st_size;
74 imagedata = malloc(filesize * sizeof(uint16_t));
75
76#ifdef __WIN32__
77 if ( (fd = open(path, O_RDONLY|O_BINARY) == -1 ) {
78#else
79 if ( (fd = open(path, O_RDONLY)) == -1) {
80#endif
81 printf("Couldn't open image file %s (%s)\n",path,strerror(errno));
82 return 1;
83 }
84 else {
85 read(fd, imagedata, filesize);
86 close(fd);
87 }
88
89 LIBMTP_Init();
90 device = LIBMTP_Get_First_Device();
91 if (device == NULL) {
92 printf("No devices.\n");
93 return 0;
94 }
95
96 LIBMTP_filesampledata_t *thumb = LIBMTP_new_filesampledata_t();
97
98 int i;
99 thumb->data = malloc(sizeof(uint16_t) * filesize);
100 for (i = 0; i < filesize; i++) {
101 thumb->data[i] = imagedata[i];
102 }
103
104 thumb->size = filesize;
105 thumb->filetype = LIBMTP_FILETYPE_JPEG;
106
107 ret = LIBMTP_Send_Representative_Sample(device,id,thumb);
108 if (ret != 0) {
109 printf("Couldn't send thumbnail\n");
Linus Walleij070e9b42007-01-22 08:49:28 +0000110 LIBMTP_Dump_Errorstack(device);
111 LIBMTP_Clear_Errorstack(device);
rreardon5332f9c2006-12-05 10:18:23 +0000112 }
113
114 free(imagedata);
Linus Walleijf1b02f22006-12-06 09:03:23 +0000115 LIBMTP_destroy_filesampledata_t(thumb);
rreardon5332f9c2006-12-05 10:18:23 +0000116
117 LIBMTP_Release_Device(device);
118 printf("OK.\n");
119 return 0;
120}