summaryrefslogtreecommitdiff
path: root/src/bin/imlib2_conv.c
blob: 1b05b1f4a7f0e27738c1aa55e2ea36d0c7259e54 (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
#include "config.h"
/* Convert images between formats, using Imlib2's API. Smart enough to know
 * about edb files; defaults to jpg's.
 */

#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define X_DISPLAY_MISSING
#include <Imlib2.h>

#define PROG_NAME "imlib2_conv"

static void         usage(int exit_status);

int
main(int argc, char **argv)
{
   char               *dot, *colon, *n, *oldn;
   Imlib_Image         im;

   /* I'm just plain being lazy here.. get our basename. */
   for (oldn = n = argv[0]; n; oldn = n)
      n = strchr(++oldn, '/');
   if (argc < 3 || !strcmp(argv[1], "-h"))
      usage(-1);
   if (!(im = imlib_load_image(argv[1])))
     {
        fprintf(stderr, PROG_NAME ": Error loading image: %s\n", argv[1]);
        exit(-1);
     }

   /* we only care what format the export format is. */
   imlib_context_set_image(im);
   /* hopefully the last one will be the one we want.. */
   dot = strrchr(argv[2], '.');
   /* if there's a format, snarf it and set the format. */
   if (dot && *(dot + 1))
     {
        colon = strrchr(++dot, ':');
        /* if a db file with a key, export it to a db. */
        if (colon && *(colon + 1))
          {
             *colon = 0;
             /* beats having to look for strcasecmp() */
             if (!strncmp(dot, "db", 2) || !strncmp(dot, "dB", 2) ||
                 !strncmp(dot, "DB", 2) || !strncmp(dot, "Db", 2))
               {
                  imlib_image_set_format("db");
               }
             *colon = ':';
          }
        else
          {
             char               *p, *q;

             /* max length of 8 for format name. seems reasonable. */
             q = p = malloc(8);
             memset(p, 0, 8);
             strncpy(p, dot, (strlen(dot) < 9) ? strlen(dot) : 8);
             /* Imlib2 only recognizes lowercase formats. convert it. */
             for (q[8] = 0; *q; q++)
                *q = tolower(*q);
             imlib_image_set_format(p);
             free(p);
          }
        dot--;
     }
   else
      imlib_image_set_format("jpg");

   imlib_save_image(argv[2]);

   return 0;
}

static void
usage(int exit_status)
{
   fprintf(exit_status ? stderr : stdout,
           PROG_NAME ": Convert images between formats (part of the "
           "Imlib2 package)\n\n"
           "Usage: " PROG_NAME " [ -h | <image1> <image2[.fmt]> ]\n"
           "  <fmt> defaults to jpg if not provided; images in "
           "edb files are supported via\n"
           "        the file.db:/key/name convention.\n"
           "  -h shows this help.\n\n");

   exit(exit_status);
}