Code Sync up from tizen_2.4
[platform/upstream/libmtp.git] / examples / thumb.c
1 /** 
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  */
23 #include "config.h"
24 #include "common.h"
25 #include "string.h"
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <limits.h>
31 #include <sys/types.h>
32 #ifdef HAVE_SYS_UIO_H
33 #include <sys/uio.h>
34 #endif
35 #include <sys/stat.h>
36
37 static void usage(void) {
38   printf("Usage: thumb -i <fileid/trackid> <imagefile>\n");
39   exit(0);
40 }
41
42 int main (int argc, char **argv) {
43   int opt;
44   extern int optind;
45   extern char *optarg;
46   LIBMTP_mtpdevice_t *device = NULL;
47   int fd;
48   uint32_t id = 0;
49   uint64_t filesize;
50   uint8_t *imagedata = NULL;
51   char *path = NULL;
52   char *rest;
53   struct stat statbuff;
54   int ret;
55
56   fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
57
58   while ( (opt = getopt(argc, argv, "hi:")) != -1 ) {
59     switch (opt) {
60     case 'h':
61       usage();
62     case 'i':
63       id = strtoul(optarg, &rest, 0);
64       break;
65     default:
66       usage();
67     }
68   }
69   argc -= optind;
70   argv += optind;
71
72   if ( argc != 1 ) {
73     printf("You need to pass a filename.\n");
74     usage();
75   }
76
77   path = argv[0];
78
79   if ( stat(path, &statbuff) == -1 ) {
80     fprintf(stderr, "%s: ", path);
81     perror("stat");
82     exit(1);
83   }
84   filesize = (uint64_t) statbuff.st_size;
85   imagedata = malloc(filesize * sizeof(uint16_t));
86
87 #ifdef __WIN32__
88   if ( (fd = open(path, O_RDONLY|O_BINARY) == -1) ) {
89 #else
90   if ( (fd = open(path, O_RDONLY)) == -1) {
91 #endif
92     printf("Couldn't open image file %s (%s)\n",path,strerror(errno));
93     return 1;
94   }
95   else {
96     read(fd, imagedata, filesize);
97     close(fd);
98   }
99
100   LIBMTP_Init();
101   device = LIBMTP_Get_First_Device();
102   if (device == NULL) {
103     printf("No devices.\n");
104     return 0;
105   }
106   
107   LIBMTP_filesampledata_t *thumb = LIBMTP_new_filesampledata_t();
108
109   int i;
110   thumb->data = malloc(sizeof(uint16_t) * filesize);
111   for (i = 0; i < filesize; i++) {
112     thumb->data[i] = imagedata[i];
113   }
114
115   thumb->size = filesize;
116   thumb->filetype = LIBMTP_FILETYPE_JPEG;
117   
118   ret = LIBMTP_Send_Representative_Sample(device,id,thumb);
119   if (ret != 0) {
120     printf("Couldn't send thumbnail\n");
121     LIBMTP_Dump_Errorstack(device);
122     LIBMTP_Clear_Errorstack(device);
123   }
124
125   free(imagedata);
126   LIBMTP_destroy_filesampledata_t(thumb);
127
128   LIBMTP_Release_Device(device);
129   printf("OK.\n");
130   return 0;
131 }