summaryrefslogtreecommitdiff
path: root/examples/thumb.c
blob: 03f8983539b862d18d557ed2455477d3dde9bb48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/** 
 * \file thumb.c
 * Example program to send and associate album art to an entity 
 * on a device.
 *
 * Copyright (C) 2006 Robert Reardon <rreardon@monkshatch.vispa.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include "config.h"
#include "common.h"
#include "string.h"
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <sys/types.h>
#ifdef HAVE_SYS_UIO_H
#include <sys/uio.h>
#endif
#include <sys/stat.h>

static void usage(void) {
  printf("Usage: thumb -i <fileid/trackid> <imagefile>\n");
  exit(0);
}

int main (int argc, char **argv) {
  int opt;
  extern int optind;
  extern char *optarg;
  LIBMTP_mtpdevice_t *device = NULL;
  int fd;
  uint32_t id = 0;
  uint64_t filesize;
  uint8_t *imagedata = NULL;
  char *path = NULL;
  char *rest;
  struct stat statbuff;
  int ret;

  fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");

  while ( (opt = getopt(argc, argv, "hi:")) != -1 ) {
    switch (opt) {
    case 'h':
      usage();
    case 'i':
      id = strtoul(optarg, &rest, 0);
      break;
    default:
      usage();
    }
  }
  argc -= optind;
  argv += optind;

  if ( argc != 1 ) {
    printf("You need to pass a filename.\n");
    usage();
  }

  path = argv[0];

  if ( stat(path, &statbuff) == -1 ) {
    fprintf(stderr, "%s: ", path);
    perror("stat");
    exit(1);
  }
  filesize = (uint64_t) statbuff.st_size;
  imagedata = malloc(filesize * sizeof(uint16_t));

#ifdef __WIN32__
  if ( (fd = open(path, O_RDONLY|O_BINARY) == -1) ) {
#else
  if ( (fd = open(path, O_RDONLY)) == -1) {
#endif
    printf("Couldn't open image file %s (%s)\n",path,strerror(errno));
    return 1;
  }
  else {
#ifdef TIZEN_EXT
    int rv;
    rv = read(fd, imagedata, filesize);
    close(fd);

    if (rv < 0) {
      printf("Read fail.\n");
      return 0;
    }
#else /* TIZEN_EXT */
    read(fd, imagedata, filesize);
    close(fd);
#endif /* TIZEN_EXT */
  }

  LIBMTP_Init();
  device = LIBMTP_Get_First_Device();
  if (device == NULL) {
    printf("No devices.\n");
    return 0;
  }
  
  LIBMTP_filesampledata_t *thumb = LIBMTP_new_filesampledata_t();

  int i;
  thumb->data = malloc(sizeof(uint16_t) * filesize);
  for (i = 0; i < filesize; i++) {
    thumb->data[i] = imagedata[i];
  }

  thumb->size = filesize;
  thumb->filetype = LIBMTP_FILETYPE_JPEG;
  
  ret = LIBMTP_Send_Representative_Sample(device,id,thumb);
  if (ret != 0) {
    printf("Couldn't send thumbnail\n");
    LIBMTP_Dump_Errorstack(device);
    LIBMTP_Clear_Errorstack(device);
  }

  free(imagedata);
  LIBMTP_destroy_filesampledata_t(thumb);

  LIBMTP_Release_Device(device);
  printf("OK.\n");
  return 0;
}