-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadobedng-bridge.c
125 lines (112 loc) · 3.74 KB
/
adobedng-bridge.c
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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "adobedng-bridge.h"
static const char* search_path[] = {
"./", /* working dir */
".\\", /* working dir (Windows) */
"/Applications/Adobe DNG Converter.app/Contents/MacOS/", /* Mac */
"C:\\Program Files\\Adobe\\", /* Windows */
"C:\\Program Files (x86)\\Adobe\\", /* Some 64-bit Windows */
};
#define COUNT(x) ((int)(sizeof(x)/sizeof((x)[0])))
static int can_exec(const char *file)
{
//~ printf("%s: %d\n", file, !access(file, X_OK));
return !access(file, X_OK);
}
static const char* find_adobe_dng_converter()
{
static char adobe_dng_path[1000] = "";
/* valid path from previous attempt? just return it */
if (adobe_dng_path[0])
return adobe_dng_path;
/* let's try some usual paths */
int i;
for (i = 0; i < COUNT(search_path); i++)
{
int is_win_path = (search_path[i][strlen(search_path[i])-1] == '\\');
if (is_win_path)
{
#if defined(WIN32) || defined(_WIN32)
snprintf(adobe_dng_path, sizeof(adobe_dng_path), "%s%s", search_path[i], "Adobe DNG Converter.exe");
if (can_exec(adobe_dng_path))
return adobe_dng_path;
#else
/* let's try Wine */
snprintf(adobe_dng_path, sizeof(adobe_dng_path), "winepath \"%s%s\"", search_path[i], "Adobe DNG Converter.exe");
FILE* f = popen(adobe_dng_path, "r");
if (f)
{
int ok = fgets(adobe_dng_path, sizeof(adobe_dng_path), f) != 0;
pclose(f);
if (ok)
{
strtok(adobe_dng_path, "\n");
if (can_exec(adobe_dng_path))
return adobe_dng_path;
}
}
#endif
}
else
{
#if !defined(WIN32) && !defined(_WIN32)
snprintf(adobe_dng_path, sizeof(adobe_dng_path), "%s%s", search_path[i], "Adobe DNG Converter");
if (can_exec(adobe_dng_path))
return adobe_dng_path;
#endif
}
}
/* nothing found */
adobe_dng_path[0] = 0;
return 0;
}
void dng_compress(const char* source, int lossy)
{
const char * adobe_dng = find_adobe_dng_converter();
if (adobe_dng)
{
printf("Found %s\n", adobe_dng);
}
else
{
printf("Adobe DNG Converter not found.\n");
return;
}
printf("Compressing %s...\n", source);
/* Adobe DNG does not overwrite files, we need to trick it somehow */
char tmp[1000];
snprintf(tmp, sizeof(tmp), "%s.DNG", source);
rename(source, tmp);
/* Find filename part only, '-o' parameter to DNG converter does not accept path */
const char *src_filename = strrchr(source, '/');
if (!src_filename)
src_filename = strrchr(source, '\\');
if (!src_filename)
src_filename = source;
else
src_filename++;
char compress_cmd[1000];
char* start =
#if defined(WIN32) || defined(_WIN32)
"start \"\" /wait "; /* force Windows to wait for the command to complete */
#else
"";
#endif
snprintf(compress_cmd, sizeof(compress_cmd), "%s\"%s\" -dng1.4 %s -o \"%s\" \"%s\"", start, adobe_dng, lossy ? "-lossy" : "", src_filename, tmp);
printf("%s\n", compress_cmd);
int r = system(compress_cmd);
if(r!=0)
{
/* phuck; restore the old file */
printf("Adobe DNG Converter returned error(%d).\n", r);
rename(tmp, source);
}
else
{
/* looks OK, delete the temporary file */
unlink(tmp);
}
}