-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoo_img.cpp
409 lines (315 loc) · 9.92 KB
/
foo_img.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include "stdafx.h"
using namespace Magick;
DECLARE_COMPONENT_VERSION("Image decoder", "1.1.0", "Open BMP, PNG and JPG images in foobar!\n\nBe sure to add the image file extensions to your media library exclusions or you will have a bad time.\n;*.jpg;*.png;*.bmp;*.jpeg;");
static const GUID spec_height_guid = { 0xcaa64014, 0x6c9c, 0x46b2, { 0x7b, 0xa7, 0xa, 0x5c, 0xbb, 0x10, 0x2f, 0xb6 } };
advconfig_integer_factory spec_height(
"Image decoding spectrogram height",
spec_height_guid,
advconfig_branch::guid_branch_decoding,
0.0,
900,
100,
1200
);
static const GUID amp_scale_guid = { 0xcacc0014, 0x6c9c, 0x46b3, { 0x7b, 0xa7, 0xf, 0x5c, 0xab, 0x10, 0x6f, 0xb6 } };
advconfig_integer_factory amp_scale(
"Image decoding amplitude scale (1-1000)",
amp_scale_guid,
advconfig_branch::guid_branch_decoding,
0.0,
430,
1,
1000
);
static const GUID lowpass_enabled_guid = { 0xfab55014, 0xc445, 0x8ff1, { 0xc3, 0xa7, 0xaa, 0x5c, 0x71, 0x7c, 0xb7, 0xe4 } };
advconfig_checkbox_factory lowpass_enabled(
"Enable image decoding lowpass filter",
lowpass_enabled_guid,
advconfig_branch::guid_branch_decoding,
0.0,
true
);
class img_type : public input_singletrack_impl {
typedef pfc::array_t<uint8_t> BArray;
std::string n;
file_ptr myFile;
t_filestats fs;
int c;
BArray raw;
double **img = nullptr;
audio_sample * buffer = nullptr;
int w, h;
double *phase = nullptr, *hz = nullptr;
double bdw, vol;
int spech;
int spp;
int xx;
double argv;
double *oamp = nullptr;
//ILuint ilimg = 0;
//Blob imBlob;
Magick::Image imImg;
double * sinlook = nullptr;
static const double PI;
static const int LOOKUP_SIZE = 6000;
int get_int32(BArray arr, int at) {
return (arr[at+3] << 24) | (arr[at+2] << 16) | (arr[at+1] << 8) | (arr[at]);
}
int get_int16(BArray arr, int at) {
return (arr[at + 1] << 8) | (arr[at]);
}
double my_sin(double val) {
double mult = val / (2*PI);
mult -= (int)(mult);
double rem = (LOOKUP_SIZE - 1) * mult;
int idx = (int)(rem);
double ret = sinlook[idx];
return ret;
}
public:
void open(service_ptr_t<file> p_filehint,const char * p_path,t_input_open_reason p_reason,abort_callback & p_abort) {
input_open_file_helper(p_filehint, p_path, p_reason, p_abort);
if(p_filehint == nullptr) {
return;
}
myFile = p_filehint;
n = std::string(p_path);
myFile->read_till_eof(raw, p_abort);
try {
Blob imBlob = Blob((void*)raw.get_ptr(), raw.get_count());
imImg = Magick::Image(imBlob);
}
catch (Exception imE) {
throw exception_io_unsupported_format(imE.what());
}
w = imImg.columns();
h = imImg.rows();
spech = spec_height.get();
spp = 870; // just works
vol = amp_scale.get();
vol = 18189 * pow(2.71828, -9.45817 * (vol / 1000.0));
if (vol <= 0)
vol = 1;
if (h > spech) {
try {
double rat = (double)w / h;
Geometry geom((int)(spech*rat), spech);
imImg.resize(geom);
w = imImg.columns();
h = imImg.rows();
}
catch (Exception imE) {}
}
bool dum = false;
foobar2000_io::filesystem::g_get_stats(p_path, fs, dum, p_abort);
hz = phase = nullptr;
sinlook = new double[LOOKUP_SIZE];
for (int i = 0; i < LOOKUP_SIZE; i++) {
double ang = (i / (double)(LOOKUP_SIZE - 1)) * 2 * PI;
sinlook[i] = std::sin(ang);
}
hz = new double[h];
phase = new double[h];
buffer = new audio_sample[spp];
if (lowpass_enabled.get())
oamp = new double[h];
srand(time(NULL));
bdw = 22000.0 / spech;
/*bdw = 22000.0 / h;
if (bdw > (22000.0 / spp))
bdw = (22000.0 / spp);*/
double logmul = 22000.0 / std::pow(h, 2.71828);
for (int a = 0; a < h; a++) {
//phase[a] = ((rand() % 20000) / 19999.0) * 2 * PI;
phase[a] = ((double)rand() / RAND_MAX) * 2 * PI;
//phase[a] = ((1140671485 * a + 12820163) % 10000) / 10000.0 * 2 * PI;
//phase[a] = ((double)(a % 12) / h) *PI/6;
hz[a] = bdw * (h - a - 1);
if (oamp != nullptr)
oamp[a] = 0;
}
xx = 0;
console::printf("foo_img::open: %s", p_path);
}
void get_info(file_info & p_info,abort_callback & p_abort) {
p_info.info_set_int("samplerate", 44100);
p_info.info_set_int("channels", 1);
p_info.info_set_int("bitspersample", 32);
p_info.info_set("codec", "PCM");
p_info.info_set("encoding", "lossless");
p_info.set_length((w * spp) / 44100.0);
p_info.info_set_bitrate(44100 * 32);
int p = n.rfind('\\');
p_info.meta_add("TITLE", n.substr(p+1).c_str());
}
t_filestats get_file_stats(abort_callback & p_abort) {
return fs;
}
void decode_initialize(unsigned p_flags,abort_callback & p_abort) {
console::printf("foo_img::initialize: %s", n.c_str());
int w = this->w;
int h = this->h;
img = new double*[w];
double _min = 1.0, _max = 0.0;
Pixels cache(imImg);
PixelPacket *pix;
pix = cache.get(0, 0, w, h);
for (int x = 0; x < w; x++) {
img[x] = new double[h];
for (int y = h - 1; y >= 0; y--) {
PixelPacket pp = pix[y*w + x];
//double v = ((pow(pp.red / 65535.0, 2.2) * 0.2989 + pow(pp.green / 65535.0, 2.2) * 0.5870 + pow(pp.blue / 65535.0, 2.2) * 0.1140));
double v = (pp.red * 0.2989 + pp.green * 0.5870 + pp.blue * 0.1140) / 65535;
img[x][y] = v;
if (v < _min)
_min = v;
else if (v > _max)
_max = v;
}
}
double scale = (_max-_min);
for (int x = 0; x < w; x++) {
double *c = img[x];
for (int y = 0; y < h; y++) {
c[y] = ((c[y] - _min) * scale);
}
}
}
bool decode_run(audio_chunk & p_chunk,abort_callback & p_abort) {
if(xx >= w)
return false;
double *col = img[xx];
int w = this->w;
int h = this->h;
int xx = this->xx;
int spp = this->spp;
double max = 0;
int pos = xx * spp;
for(int s = 0; s < spp; s++) {
double sum = 0;
int smp = pos + s;
double arg = smp * PI * 2 / 44100.0;
//arg = std::fmod(arg, 2 * PI);
for(int y = 0; y < h; y++) {
double p = col[y];
double amp = (p * 0.2 + p*p * 0.8);
if (oamp != nullptr) {
//amp = oamp[y] * 0.92 + amp * 0.08;
amp = oamp[y] * 0.995 + amp * 0.005;
oamp[y] = amp;
}
double pt = my_sin(hz[y] * arg + phase[y]);
//double prog = (double)s / spp;
//prog = -4.0844*prog*prog + 4.0844 * prog + 1.687;
sum += pt*amp;// *prog;
}
sum = sum / vol;
if(sum < -1.0)
sum = -1.0;
else if(sum > 1.0)
sum = 1.0;
buffer[s] = sum;
}
this->xx++;
p_chunk.set_data(buffer, spp, 1, 44100);
return true;
}
void decode_seek(double p_seconds,abort_callback & p_abort) {
double samps = p_seconds * 44100;
samps /= spp;
xx = (int)samps;
}
bool decode_can_seek() {
return true;
}
bool decode_get_dynamic_info(file_info & p_out, double & p_timestamp_delta){
return false;
}
bool decode_get_dynamic_info_track(file_info & p_out, double & p_timestamp_delta){
return false;
}
void decode_on_idle(abort_callback & p_abort){
}
void retag(const file_info & p_info,abort_callback & p_abort){
// don't care
}
static bool endswith(const char * s1, const char * s2) {
return strlen(s1) >= strlen(s2) && stricmp(s1 + strlen(s1) - strlen(s2), s2) == 0;
}
static bool startswith(const char * s1, const char * s2) {
return strlen(s1) >= strlen(s2) && strnicmp(s1, s2, strlen(s2)) == 0;
}
static bool g_is_our_content_type(const char * p_content_type){
return stricmp(p_content_type, "image/bmp") == 0 ||
stricmp(p_content_type, "image/png") == 0 ||
stricmp(p_content_type, "image/jpeg") == 0;
}
static bool g_is_our_path(const char * p_path,const char * p_extension) {
if (strncmp(p_path, "unpack:", strlen("unpack:")) == 0) {
return false; // don't open packs
}
const char * fname = strrchr(p_path, '\\') + 1;
if (startswith(fname, "albumart") ||
startswith(fname, "cover") ||
startswith(fname, "thumb") ||
startswith(fname, "folder") ||
startswith(fname, "front") ||
startswith(fname, "disc") ||
startswith(fname, "back"))
return false;
return stricmp(p_extension, "bmp") == 0 ||
stricmp(p_extension, "jpg") == 0 ||
stricmp(p_extension, "jpeg") == 0 ||
stricmp(p_extension, "png") == 0;
}
~img_type(){
delete[] hz;
delete[] phase;
delete[] buffer;
delete[] sinlook;
delete[] oamp;
if (img != nullptr) {
for (int i = 0; i < w; i++)
delete[] img[i];
delete[] img;
}
}
};
const double img_type::PI = 3.14159265359;
input_singletrack_factory_t<img_type> fac;
std::string wstrtostr(const std::wstring &wstr)
{
if (wstr.empty()) return std::string();
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
std::string strTo(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
return strTo;
}
// Convert an UTF8 string to a wide Unicode String
std::wstring strtowstr(const std::string &str)
{
if (str.empty()) return std::wstring();
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
std::wstring wstrTo(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
return wstrTo;
}
BOOLEAN WINAPI DllMain(IN HINSTANCE hDllHandle,
IN DWORD nReason,
IN LPVOID Reserved)
{
BOOLEAN bSuccess = TRUE;
// Perform global initialization.
switch (nReason)
{
case DLL_PROCESS_ATTACH:
// For optimization.
DisableThreadLibraryCalls(hDllHandle);
break;
case DLL_PROCESS_DETACH:
break;
}
LPSTR s = GetCommandLineA();
InitializeMagick(s);
return bSuccess;
}