-
Notifications
You must be signed in to change notification settings - Fork 0
/
scatter-rolling.c
152 lines (120 loc) · 4.08 KB
/
scatter-rolling.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "logging.h"
#include "io.h"
#include "opt.h"
#include "image.h"
extern LogLevel logLevel;
#define MAX_UPSCALE 4
int getTarget(char *srcPath, unsigned int *targetWidth, unsigned int *targetHeight)
{
if (sizeJpeg(srcPath, targetWidth, targetHeight)) {
logFatal("%s: Cannot open file.", srcPath);
}
return 0; // Win.
}
int accumulate(double *dstImage, unsigned char **srcBuffer, double *alphaImage, char* srcPath,
unsigned int targetWidth, unsigned int targetHeight, unsigned int exposure,
double t)
{
unsigned int width, height;
double mean, gain;
if (loadJpeg(srcPath, srcBuffer, &width, &height))
{
logWarning("Loading failed.");
return 1; // Fail.
}
if (targetWidth != width)
{
logError("%s: Width %u does not equal target width %u.", srcPath, width, targetWidth);
return 1; // Fail.
}
if (targetHeight != height)
{
logError("%s: Height %u does not equal target height %u.", srcPath, height, targetHeight);
return 1; // Fail.
}
mean = imageMean(*srcBuffer, targetWidth * targetHeight);
logDebug("%.2f", mean);
gain = 255.0 / fmax(mean, 1.0);
gain = gain * exposure + (1.0 - exposure);
addMultCharToDoubleColorRolling(dstImage, *srcBuffer, targetWidth, targetHeight, gain, t);
addMultCharToDoubleRolling(alphaImage, targetWidth, targetHeight, gain, t);
return 0; // Win.
}
int main (int argc, char **argv)
{
char ** inPathList;
unsigned int nInPath;
char * outPath;
unsigned int fGiven, fStart, fEnd, fStep;
unsigned int force;
double exposure, rollOffset;
unsigned int width, height;
size_t pixels;
double * meanData = NULL;
unsigned char * frameData = NULL;
double * alphaData = NULL;
double t;
getOptionsScatterRolling(&logLevel,
&inPathList, &nInPath, &outPath,
&fGiven, &fStart, &fEnd, &fStep,
&force, &exposure, &rollOffset,
argc, argv);
if (fGiven)
{
int f;
for (f = fStart; f <= fEnd; f += fStep)
{
char * path;
t = (double)(f - fStart) / (double)(fEnd - fStart + 1);
substitutePositiveInteger(&path, inPathList[0], f);
logInfo(path);
if (!meanData)
{
getTarget(path, &width, &height);
pixels = width * height;
meanData = (double*) malloc(pixels * N_COMPONENTS * sizeof(double));
frameData = (unsigned char*) malloc(pixels * N_COMPONENTS * sizeof(unsigned char));
alphaData = (double*) malloc(pixels * sizeof(double));
zeroDouble(meanData, pixels);
zeroDouble(alphaData, pixels / N_COMPONENTS);
}
accumulate(meanData, &frameData, alphaData, path, width, height, exposure, t + rollOffset);
free(path);
}
}
else
{
unsigned int i;
for (i = 0; i < nInPath; i++)
{
char * path = inPathList[i];
t = (double)i / (double)nInPath;
logInfo(path);
if (!meanData)
{
getTarget(path, &width, &height);
pixels = width * height;
meanData = (double*) malloc(pixels * N_COMPONENTS * sizeof(double));
frameData = (unsigned char*) malloc(pixels * N_COMPONENTS * sizeof(unsigned char));
alphaData = (double*) malloc(pixels * sizeof(double));
zeroDouble(meanData, pixels);
zeroDouble(alphaData, pixels / N_COMPONENTS);
}
accumulate(meanData, &frameData, alphaData, path, width, height, exposure, t + rollOffset);
}
}
divideDoubleColorDouble(meanData, alphaData, pixels);
doubleToChar(frameData, meanData, pixels);
logDebug("Saving to: %s", outPath);
if (saveTiff(outPath, frameData, width, height)) {
logFatal("Saving failed.");
}
if (frameData) free(frameData);
if (meanData) free(meanData);
freePathList(&inPathList, nInPath);
return 0;
}