-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageEditor.H
4176 lines (3676 loc) · 129 KB
/
ImageEditor.H
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2006-2011, Guillaume Gimenez <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of G.Gimenez nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL G.Gimenez SA BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Authors:
* * Guillaume Gimenez <[email protected]>
*
*/
/*
CREATE TABLE gallery_process (
filename VARCHAR, -- chemin réel du fichier url_encodé
order INTEGER, -- numero d'operation dans le process
operator VARCHAR, -- nom de l'operateur
params VARCHAR -- les parametres
)
*/
#include <Magick++.h>
#include "CIELab.H"
#include "Profile.H"
#include <apr_md5.h>
using namespace Magick;
namespace op {
static inline void Labize(Image &image, Image &labImage);
static inline void unLabize(Image &image, Image &labImage);
#define LAB_G 3.0L
//#define LAB_N 0.159997L
#define LAB_N 0.008856L
//faux 2.64388L
#define SRGB_G 2.4L
#define SRGB_N 0.00304L
//8.36171L
#define BT709_G 2.222L
#define BT709_N 0.018L
//5.79586L
#define LIN_G 1.0L
#define LIN_N 0.0L
/*
pour un gamma à deux param : pente p + gamma g
on a :
d/dx (1+a)*x^(1/g)-a = d/dx p*x + k
(a+1)*x^(1/g-1) / g = p
donc la partie linéaire et la partie nonlinéaire se croisent pour :
x0=(p*g/(a+1))^(1/(1/g-1))
en revanche, on peut déterminer a et p par rapport à x0 pour que la pente
soit linéaire (f(0)=0) pour les zones de basses lumières inférieures à x0
à l'aide de y = f'(x0)*(x-x0)+f(x0)
Y = A*x+B
A = (a+1)*x0^(1/g)/(g*x0)
B = (a+1)(g-1)*x0^(1/g)/g - a = 0
-> a = - (g-1)*x0^(1/g)/((g-1)*x0^(1/g)-g)
-> p = f'(x0) = (a+1)*x0^(1/g)/(g*x0)
->
concernant la réciproque :
d/dx ((x+a)/(a+1))^g = d/dx x/p
g*((x+a)/(a+1))^g/(x+a) = 1/p
il faut obtenir x0=(1+a)*(x0 demandé)^(1/g)-a
A= on s'en moque
B= ((x0+a)/(a+1))^g - g*x0((x0+a)/(a+1))^g/(x0+a) = 0
-> a = (g-1)*x0 (et aussi a = -x0 )
-> p = f'(x0) = g*((x0+a)/(a+1))^g/(x0+a)
*/
static inline void iGamma(Image& image, double gamma, double x0, bool invert=false, int channels=3) {
//semble optimale
//Profile prof("iGamma");
if ( gamma == 1.0L ) return;
double a = - ( gamma - 1.L)*pow(x0,1.L/gamma)/((gamma-1.L)*pow(x0,1.L/gamma)-gamma);
double p=0;
if ( invert ) {
//recalcul de x0 et a
x0=(1.L+a)*pow(x0,1.L/gamma)-a;
a=(gamma-1.L)*x0;
p=gamma*pow((x0+a)/(a+1.L),gamma)/(x0+a);
}
else
p=(a+1.L)*pow(x0,1.L/gamma)/(gamma*x0);
quantum_t lut[QuantumRange+1];
#pragma omp parallel for
for ( unsigned int i = 0 ; i <= sizeof(lut)/sizeof(*lut) ; ++i ) {
double xx= double(i)/double(QuantumRange);
if ( xx > x0 ) {
if ( invert ) {
lut[i]=pow(((xx+a)/(a+1.L)),gamma)*QuantumRange;
}
else {
lut[i]=((1.L+a)*pow(xx,(1.L/gamma))-a)*QuantumRange;
}
}
else {
lut[i]=p*xx*double(QuantumRange);
}
}
#pragma omp barrier
int h = image.rows(),
w = image.columns();
image.modifyImage();
Pixels pixel_cache(image);
#pragma omp parallel for
for ( int y = 0 ; y < h ; ++y ) {
PixelPacket *pixels = pixel_cache.get(0,y,w,1);
if ( !pixels ) continue;
for ( int x = 0 ; x < w ; ++x ) {
pixels[x].red=lut[pixels[x].red];
pixels[x].green=lut[pixels[x].green];
pixels[x].blue=lut[pixels[x].blue];
}
}
#pragma omp barrier
pixel_cache.sync();
}
template<typename t> t clamp(t v,t min, t max) {
if ( v < min )
return min;
else if ( v > max )
return max;
else
return v;
}
static void Temperature_to_RGB(double T, double RGB[3]) {
//thanks to ufraw_routines.c from ufraw-0.11
// and http://www.brucelindbloom.com/index.html?Eqn_T_to_xy.html
const double XYZ_to_RGB[3][3] = {
{ 3.24071, -0.969258, 0.0556352 },
{-1.53726, 1.87599, -0.203996 },
{-0.498571, 0.0415557, 1.05707 } };
int c;
double xD, yD, X, Y, Z, max;
// Fit for CIE Daylight illuminant
if (T<= 4000) {
xD = 0.27475e9/(T*T*T) - 0.98598e6/(T*T) + 1.17444e3/T + 0.145986;
} else if (T<= 7000) {
xD = -4.6070e9/(T*T*T) + 2.9678e6/(T*T) + 0.09911e3/T + 0.244063;
} else {
xD = -2.0064e9/(T*T*T) + 1.9018e6/(T*T) + 0.24748e3/T + 0.237040;
}
yD = -3*xD*xD + 2.87*xD - 0.275;
// Fit for Blackbody using CIE standard observer function at 2 degrees
//xD = -1.8596e9/(T*T*T) + 1.37686e6/(T*T) + 0.360496e3/T + 0.232632;
//yD = -2.6046*xD*xD + 2.6106*xD - 0.239156;
// Fit for Blackbody using CIE standard observer function at 10 degrees
//xD = -1.98883e9/(T*T*T) + 1.45155e6/(T*T) + 0.364774e3/T + 0.231136;
//yD = -2.35563*xD*xD + 2.39688*xD - 0.196035;
X = xD/yD;
Y = 1;
Z = (1-xD-yD)/yD;
max = 0;
for (c=0; c<3; c++) {
RGB[c] = X*XYZ_to_RGB[0][c] + Y*XYZ_to_RGB[1][c] + Z*XYZ_to_RGB[2][c];
if (RGB[c]>max) max = RGB[c];
}
for (c=0; c<3; c++) RGB[c] = RGB[c]/max;
}
static inline Image removeHotPixels(Image& input, double delta=0.5L, bool aggressive=false, bool naive=false) {
Profile prof("removeHotPixels");
Image image=input;
image.modifyImage();
Pixels output_cache(image),
input_cache(input);
int w = image.columns(),
h = image.rows();
#pragma omp parallel for
for ( int y = 1 ; y < h-1 ; ++y ) {
PixelPacket *output_pixels = output_cache.get(0,y,w,1);
PixelPacket *input_pixels[3];
input_pixels[0] = input_cache.get(0,y-1,w,1);
input_pixels[1] = input_cache.get(0,y,w,1);
input_pixels[2] = input_cache.get(0,y+1,w,1);
for ( int x = 1 ; x < w-1 ; ++x ) {
extended_quantum_t max_rgb[3]={0,0,0};
extended_quantum_t min_rgb[3]={QuantumRange,QuantumRange,QuantumRange};
extended_quantum_t sum_rgb[3]={0,0,0};
extended_quantum_t rgb[3];
extended_quantum_t nrgb[3];
extended_quantum_t other_channels=0;
rgb[0]=input_pixels[1][x].red;
rgb[1]=input_pixels[1][x].green;
rgb[2]=input_pixels[1][x].blue;
#define color_op_naive(q) \
sum_rgb[q]+=nrgb[q];
#define color_op(q) \
if ( nrgb[q] > max_rgb[q] ) max_rgb[q] = nrgb[q]; \
if ( nrgb[q] < min_rgb[q] ) min_rgb[q] = nrgb[q]; \
sum_rgb[q]+=nrgb[q];
#define loop_code_naive(j,i) \
nrgb[0]=input_pixels[j+1][x+i].red; \
nrgb[1]=input_pixels[j+1][x+i].green; \
nrgb[2]=input_pixels[j+1][x+i].blue; \
color_op_naive(0); color_op_naive(1); color_op_naive(2)
#define loop_code(j,i) \
nrgb[0]=input_pixels[j+1][x+i].red; \
nrgb[1]=input_pixels[j+1][x+i].green; \
nrgb[2]=input_pixels[j+1][x+i].blue; \
color_op(0); color_op(1); color_op(2)
#define color_op2(q) \
other_channels = (rgb[(q+2)%3]+rgb[(q+1)%3])/2; \
sum_rgb[q]-=(max_rgb[q]+min_rgb[q]); \
sum_rgb[q]/=6; \
if ( sum_rgb[q]*delta < rgb[q] && other_channels*delta < rgb[q] ) rgb[q]=sum_rgb[q]; \
if ( sum_rgb[q]/delta > rgb[q] && other_channels/delta > rgb[q] ) rgb[q]=sum_rgb[q];
#define color_op2_naive(q) \
other_channels = (rgb[(q+2)%3]+rgb[(q+1)%3])/2; \
sum_rgb[q]/=8; \
if ( sum_rgb[q]*delta < rgb[q] && other_channels*delta < rgb[q] ) rgb[q]=sum_rgb[q]; \
if ( sum_rgb[q]/delta > rgb[q] && other_channels/delta > rgb[q] ) rgb[q]=sum_rgb[q];
#define color_op2_aggressive(q) \
sum_rgb[q]-=(max_rgb[q]+min_rgb[q]); \
sum_rgb[q]/=6; \
if ( sum_rgb[q]*delta < rgb[q] ) rgb[q]=sum_rgb[q]; \
if ( sum_rgb[q]/delta > rgb[q] ) rgb[q]=sum_rgb[q];
#define color_op2_naive_aggressive(q) \
sum_rgb[q]/=8; \
if ( sum_rgb[q]*delta < rgb[q] ) rgb[q]=sum_rgb[q]; \
if ( sum_rgb[q]/delta > rgb[q] ) rgb[q]=sum_rgb[q];
if ( naive ) {
loop_code_naive(-1,-1); loop_code_naive(-1, 0); loop_code_naive(-1, 1);
loop_code_naive( 0,-1); loop_code_naive( 0, 1);
loop_code_naive( 1,-1); loop_code_naive( 1, 0); loop_code_naive( 1, 1);
if ( aggressive ) {
color_op2_naive_aggressive(0); color_op2_naive_aggressive(1); color_op2_naive_aggressive(2);
}
else {
color_op2_naive(0); color_op2_naive(1); color_op2_naive(2);
}
}
else {
loop_code(-1,-1); loop_code(-1, 0); loop_code(-1, 1);
loop_code( 0,-1); loop_code( 0, 1);
loop_code( 1,-1); loop_code( 1, 0); loop_code( 1, 1);
if ( aggressive ) {
color_op2_aggressive(0); color_op2_aggressive(1); color_op2_aggressive(2);
}
else {
color_op2(0); color_op2(1); color_op2(2);
}
}
#undef loop_code
#undef loop_code_naive
#undef color_op
#undef color_op_naive
#undef color_op2
#undef color_op2_naive
#undef color_op2_aggressive
#undef color_op2_naive_aggressive
//end of loops unroll
output_pixels[x].red=rgb[0]>QuantumRange?QuantumRange:rgb[0];
output_pixels[x].green=rgb[1]>QuantumRange?QuantumRange:rgb[1];
output_pixels[x].blue=rgb[2]>QuantumRange?QuantumRange:rgb[2];
}
}
#pragma omp barrier
input_cache.sync();
output_cache.sync();
return image;
}
DECLARE_EXCEPTION(OperatorException,raii::Exception);
DECLARE_EXCEPTION(ProcessException,raii::Exception);
class ImageInfo {
public:
double gamma;
double non_linear_range;
int iso_speed;
double shutter_speed,aperture,focal;
double daylight_multipliers[3],camera_multipliers[3];
String camera;
String timestamp;
String filterPattern;
ImageInfo(double g,double n) : gamma(g), non_linear_range(n), iso_speed(-1),
shutter_speed(-1), aperture(-1), focal(-1),
daylight_multipliers(), camera_multipliers(),
camera("Unknown"), timestamp("Unknown"), filterPattern("") {
for (int i=0 ; i < 3 ; ++i ) {
daylight_multipliers[i]=1.;
camera_multipliers[i]=1.;
}
}
void probeFile(const String& path) {
/*
Info à lire
Camera: NIKON D700
Timestamp: Wed Jun 18 22:54:47 2008
ISO speed: 12800
Shutter: 1/200.0 sec
Aperture: f/10.0
Focal length: 200.0 mm
Daylight multipliers: 2.064871 0.932310 1.112389
Camera multipliers: 1.503906 1.000000 1.648438 0.000000
*/
FILE *f=popen(("dcraw -i -v \""+path+"\"").c_str(),"r");
if ( f ) {
char buf[1024];
while ( fgets(buf,1024,f) ) {
String str=buf;
Vector<String> elem = str.explode(" ");
if ( str.matches("^ISO speed: ") ) {
iso_speed = elem[2].toi();
}
else if ( str.matches("Shutter: ") ) {
Vector<String> sh = elem[1].explode("/");
shutter_speed = sh[0].tof();
if ( sh.size() == 2 ) shutter_speed/=sh[1].tof();
}
else if ( str.matches("Aperture: ") ) {
Vector<String> sh = elem[1].explode("/");
aperture = sh[1].tof();
}
else if ( str.matches("Focal length: ") ) {
focal = elem[2].tof();
}
else if ( str.matches("Daylight multipliers: ") ) {
daylight_multipliers[0] = elem[2].tof();
daylight_multipliers[1] = elem[3].tof();
daylight_multipliers[2] = elem[4].tof();
if ( elem.size() == 6 ) {
double div = elem[5].tof();
if ( div != 0.L ) {
daylight_multipliers[0]/=div;
daylight_multipliers[1]/=div;
daylight_multipliers[2]/=div;
}
}
}
else if ( str.matches("Camera multipliers: ") ) {
camera_multipliers[0] = elem[2].tof();
camera_multipliers[1] = elem[3].tof();
camera_multipliers[2] = elem[4].tof();
if ( elem.size() == 6 ) {
double div = elem[5].tof();
if ( div != 0.L ) {
camera_multipliers[0]/=div;
camera_multipliers[1]/=div;
camera_multipliers[2]/=div;
}
}
}
else if ( str.matches("Camera: ") ) {
camera=buf+8;
}
else if ( str.matches("TimeStamp: ") ) {
timestamp=buf+11;
}
else if ( str.matches("Filter pattern: ") ) {
filterPattern=buf+16;
}
}
pclose(f);
}
else
throw OperatorException("'popen dcraw -i -v \""+path+"\"' failed: "+strerror(errno));
}
};
class Operator : public Object {
protected:
bool upToDate;
public:
String name;
Map<String,String> param;
bool enabled;
int id;
int serial;
Image image;
ptr<Operator> lastOp;
ImageInfo imageInfo;
Image curve;
public:
Operator(const String& str,int i,ptr<Operator> op)
: upToDate(false), name(str), param(), enabled(true),
id(i), serial(0), image(), lastOp(op), imageInfo(LIN_G,LIN_N),curve(1,1,"RGBA",IntegerPixel,"\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0" /* "\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0" */) {
mod("maxsize","0");
}
virtual ~Operator() {}
void enable() { upToDate=false; enabled=true; }
void disable() { upToDate=false; enabled=false; }
bool isEnabled() { return enabled; }
int getId() { return id; }
int getSerial() { return serial; }
virtual Image* propagate() {
return ℑ
}
virtual Image * getImage() {
return ℑ
}
virtual Image * getCurve() {
return &curve;
}
virtual void doJob()=0;
bool apply(bool force) {
if ( ! upToDate || force ) {
//Logger log("Process");
//log(String("applying operator ")+name+" "+itostring(id));
if ( lastOp ) {
image =* (lastOp->propagate());
curve =* (lastOp->getCurve());
imageInfo = (lastOp->imageInfo);
}
if ( isEnabled() )
doJob();
upToDate = true;
return true;
}
return false;
}
String partialName() { return String("/operator/_")+name+".csp"; }
String getName() { return name; }
virtual bool isParamNameValid(const String& name) { return true; }
void mod(const String& name, const String& value) {
upToDate=false;
++serial;
if ( isParamNameValid(name) )
param[name]=value;
}
void outDate() { upToDate=false; }
String mod(const String& name) {
if ( isParamNameValid(name) )
return param[name];
else return "";
}
int getMaxSize() {
if ( mod("maxsize").empty() || mod("maxsize").toi() == 0 ) {
if ( lastOp )
return lastOp->getMaxSize();
else
return 0;
}
return mod("maxsize").toi();
}
void resize(Magick::Image &img) {
int maxsize=getMaxSize();
if ( maxsize != 0 ) {
Magick::Geometry geo = img.size();
int w=geo.width(),
h=geo.height(),
W,H;
double coef=1.;
if ( w < h )
coef=((double)maxsize)/((double)h);
else
coef=((double)maxsize)/((double)w);
W=(int)(w*coef);
H=(int)(h*coef);
img.scale(Geometry(W,H));
}
}
};
class GenericLoad : public Operator {
Image cropped;
Image resized;
public:
GenericLoad(bool raw,const String& root, const String& tmp, const String& filename)
: Operator(raw?"loadraw":"load",0,NULL), cropped(),resized() {
mod("root",root);
mod("tmp",tmp);
mod("filename",filename);
mod("autocrop","");
mod("crop_x1","0");
mod("crop_y1","0");
mod("crop_x2","0");
mod("crop_y2","0");
mod("rotate","0");
mod("zoom","100");
serial=0;
}
void setMaxSize(int maxsize) {
bool saved_upToDate = upToDate;
mod("maxsize", itostring(maxsize));
upToDate=saved_upToDate;
}
Image* propagate() {
cropped=image;
double rotate=mod("rotate").tof();
cropped.rotate(rotate);
Magick::Geometry geo = cropped.size();
int w=geo.width(),
h=geo.height();
//% de l'image et non pas % des dimensions
double zoom=sqrt(mod("zoom").tof()/100);
if ( zoom > 0 && zoom < 100 ) {
cropped.crop(Geometry(zoom*w,zoom*h,(w-zoom*w)/2,(h-zoom*h)/2));
geo = cropped.size();
w=geo.width();
h=geo.height();
}
String autocrop = mod("autocrop");
if ( autocrop == "reset" ) {
mod("crop_x1","0");mod("crop_y1","0");
mod("crop_x2","0");mod("crop_y2","0");
mod("autocrop","");
}
else if ( autocrop == "center" ) {
if ( w>h ) {
mod("crop_x1",itostring((w-h)/2));mod("crop_y1","0");
mod("crop_x2",itostring(w-(w-h)/2));mod("crop_y2",itostring(h));
cropped.crop(Geometry(h,h,(w-h)/2,0));
}
else {
mod("crop_y1",itostring((h-w)/2));mod("crop_x1","0");
mod("crop_y2",itostring(w-(w-h)/2));mod("crop_x2",itostring(w));
cropped.crop(Geometry(w,w,0,(h-w)/2));
}
}
else if ( autocrop == "left/top" ) {
int l = w>h?h:w;
mod("crop_x1","0");mod("crop_y1","0");
mod("crop_x2",itostring(l));mod("crop_y2",itostring(l));
cropped.crop(Geometry(l,l,0,0));
}
else if ( autocrop == "right/bottom" ) {
int l = w>h?h:w;
mod("crop_x1",itostring(w-l));mod("crop_y1",itostring(h-l));
mod("crop_x2",itostring(w));mod("crop_y2",itostring(h));
cropped.crop(Geometry(l,l,w-l,h-l));
}
else if ( autocrop == "4/3-center" ) {
int l = w>h?h:w;
int L = l*4/3;
if ( w>h ) {
mod("crop_x1",itostring((w-L)/2));mod("crop_y1","0");
mod("crop_x2",itostring(w-(w-L)/2));mod("crop_y2",itostring(h));
cropped.crop(Geometry(L,l,(w-L)/2,0));
}
else {
mod("crop_y1",itostring((h-L)/2));mod("crop_x1","0");
mod("crop_y2",itostring(w-(w-L)/2));mod("crop_x2",itostring(w));
cropped.crop(Geometry(l,L,0,(h-L)/2));
}
}
else if ( autocrop == "4/3-left/top" ) {
int l = w>h?h:w;
int L = l*4/3;
if ( w>h ) {
mod("crop_x1","0");mod("crop_y1","0");
mod("crop_x2",itostring(L));mod("crop_y2",itostring(l));
cropped.crop(Geometry(L,l,0,0));
}
else {
mod("crop_x1","0");mod("crop_y1","0");
mod("crop_x2",itostring(l));mod("crop_y2",itostring(L));
cropped.crop(Geometry(l,L,0,0));
}
}
else if ( autocrop == "4/3-right/bottom" ) {
int l = w>h?h:w;
int L = l*4/3;
if ( w>h ) {
mod("crop_x1",itostring(w-L));mod("crop_y1",itostring(h-l));
mod("crop_x2",itostring(w));mod("crop_y2",itostring(h));
cropped.crop(Geometry(L,l,w-L,h-l));
}
else {
mod("crop_x1",itostring(w-l));mod("crop_y1",itostring(h-L));
mod("crop_x2",itostring(w));mod("crop_y2",itostring(h));
cropped.crop(Geometry(l,L,w-l,h-L));
}
}
else /* if ( autocrop.empty() ) */ {
int x1=mod("crop_x1").toi(),
y1=mod("crop_y1").toi(),
x2=mod("crop_x2").toi(),
y2=mod("crop_y2").toi();
if ( ! (x1 == 0 && x2 == 0 && y1 == 0 && y2 == 0 ) ) {
if ( x1 > x2 ) { int t=x1; x1=x2; x2=t; }
if ( y1 > y2 ) { int t=y1; y1=y2; y2=t; }
cropped.crop(Geometry(x2-x1,y2-y1,x1,y1));
}
}
upToDate = true;
resize(cropped);
return &cropped;
}
Image* getImage() {
resized=image;
resize(resized);
return &resized;
}
};
class Load : public GenericLoad {
public:
Load(const String& root, const String& tmp, const String& fn) : GenericLoad(false,root, tmp,fn) {
mod("discrete","0");
mod("linear","0");
serial=0;
}
void doJob() {
if ( upToDate )
return;
try{
image.read(mod("root") + mod("filename"));
}
catch(...){
throw raii::Exception("ici");
}
try {
enum { UP_IS_UNDEF = 0, UP_IS_UP = 1 , UP_IS_LEFT = 2, UP_IS_BOTTOM = 4, UP_IS_RIGHT = 8};
ExifTags etags(mod("root") + mod("filename"));
switch (etags.Photo_Orientation)
{
case UP_IS_LEFT:
image.rotate(90.);
case UP_IS_RIGHT:
image.rotate(-90.);
break;
case UP_IS_BOTTOM:
image.rotate(180.);
break;
case UP_IS_UP:
default:
(void)0;
}
}
catch(...) {}
curve.size("65536x1");
Pixels curve_cache(curve);
PixelPacket *pixels = curve_cache.get(0,0,65536,1);
if ( mod("discrete").toi() ) {
double discrete=mod("discrete").tof();
for(int i=0; i < 65536 ; ++i) {
quantum_t c = pow(2., 16. - floor( -discrete*log(double(i)/QuantumRange)/log(2) )/discrete );
if (c > QuantumRange) c=QuantumRange;
pixels[i].red=pixels[i].green=pixels[i].blue=c;
}
}
else
for(int i=0; i < 65536 ; ++i)
pixels[i].red=pixels[i].green=pixels[i].blue=i;
curve_cache.sync();
if ( mod("linear").toi() ) {
imageInfo.gamma=LIN_G;
imageInfo.non_linear_range=LIN_N;
}
else {
//sRGB
imageInfo.gamma=SRGB_G;
imageInfo.non_linear_range=SRGB_N;
}
iGamma(curve,imageInfo.gamma,imageInfo.non_linear_range,false);
imageInfo.probeFile(mod("root")+mod("filename"));
}
};
class LoadRaw : public GenericLoad {
int last_halfSize;
int last_drizzle;
int last_divisor;
public:
LoadRaw(const String& root, const String& tmp, const String& fn) : GenericLoad(true,root,tmp,fn), last_halfSize(-1), last_drizzle(-1), last_divisor(-1) {
//mod("autWB","0");//(false),
//mod("cameraWB","1");//(true),
mod("whitebalance","2");
mod("brightness","1.0");//(1.0),
mod("blackpoint","");//(0),
mod("whitepoint","");
mod("highlights","0");//(0),
mod("quality","1");//(0),
mod("halfSize","0");//(0),
mod("fourColors","0");//(false),
mod("sigmaDomain","2");//(2),
mod("sigmaRange","4");//(4)
mod("16bit","0");
mod("temperature","6500");
mod("tint","1.0");
mod("red_mag","1.000");
mod("blue_mag","1.000");
mod("noise","");
mod("hp_enabled","0"); //seuil ISO
mod("hp_delta","1");
mod("hp_aggressive","0"); //seuil ISO
mod("hp_naive","0");
mod("discrete","0");
if ( fn.matches("\\.[sS][tT][kK]$") )
{
mod("stack", "1");
mod("drizzle", "1");
mod("divisor", "1");
}
else
{
mod("stack", "0");
}
serial=0;
}
String getTmpFile(const String& filename) {
prepareTmpDir(mod("root"),mod("tmp"),filename);
return mod("tmp") + "/processed" + mod("root") + filename + ".ppm";
}
String getDcrawCommandLine(const String& filename, const String& tfile) {
StringStream ss;
ss << "dcraw -c ";
int wb=mod("whitebalance").toi();
int stack=mod("stack").toi();
if (stack)
ss << "-t 0 ";
imageInfo.probeFile(mod("root")+filename);
switch (wb) {
case -1: {
double RGB[3];
double temperature = clamp<double>(mod("temperature").tof(),2000,12000);
mod("temperature",ftostring(temperature));
Temperature_to_RGB(temperature,RGB);
RGB[1]= RGB[1] / mod("tint").tof();
//Daylight multipliers
RGB[0]= imageInfo.daylight_multipliers[0] /RGB[0];
RGB[1]= imageInfo.daylight_multipliers[1] /RGB[1];
RGB[2]= imageInfo.daylight_multipliers[2] /RGB[2];
double minmul=RGB[0];
if ( minmul > RGB[1] ) minmul=RGB[1];
if ( minmul > RGB[2] ) minmul=RGB[2];
RGB[0]/=minmul;
RGB[1]/=minmul;
RGB[2]/=minmul;
ss << "-r " << RGB[0] <<" "<< RGB[1] << " "<< RGB[2] << " 1 ";
break;
}
default:
{
Connection conn;
ResultSet rs= conn.query("SELECT op,rx,gx,bx"
" FROM gallery_white_balance"
" WHERE id='"+itostring(wb)+"'");
if ( rs.next() ) {
if ( rs["op"] == "-r" ) {
double RGB[3];
RGB[0]= imageInfo.daylight_multipliers[0] * rs["rx"].tof();
RGB[1]= imageInfo.daylight_multipliers[1] * rs["gx"].tof();
RGB[2]= imageInfo.daylight_multipliers[2] * rs["bx"].tof();
double minmul=RGB[0];
if ( minmul > RGB[1] ) minmul=RGB[1];
if ( minmul > RGB[2] ) minmul=RGB[2];
RGB[0]/=minmul;
RGB[1]/=minmul;
RGB[2]/=minmul;
ss << rs["op"] << " " << RGB[0] <<" "<< RGB[1] << " "<< RGB[2] << " 1 ";
}
else {
ss << rs["op"] << " ";
}
}
}
}
if ( ! mod("noise").empty() )
ss << "-n " <<mod("noise").toi()<<" ";
ss << "-b "<<mod("brightness").tof()<<" ";
if ( ! mod("blackpoint").empty() )
ss << "-k "<<mod("blackpoint").toi()<<" ";
if ( ! mod("whitepoint").empty() )
ss << "-S "<<mod("whitepoint").toi()<<" ";
ss << "-q "<<mod("quality").toi()<<" ";
switch ( mod("halfSize").toi() ) {
case 0: //full size dcraw interpolation
//nop
break;
default:
case 1: //halfsize
ss << "-h ";
break;
case 2: //rawdata
case 3: //internal interpolation
ss << "-d ";
break;
}
if (mod("fourColors").toi()) ss<<"-f ";
ss << "-H "<<mod("highlights").toi()<<" ";
switch ( mod("16bit").toi() ) {
case 1: //16bit linear
ss << "-6 -W -g 1 1 ";
imageInfo.gamma=LIN_G;
imageInfo.non_linear_range=LIN_N;
break;
case 2: //16bit IUT BT.709
ss << "-6 -W -g 2.222 4.5 ";
imageInfo.gamma=BT709_G;
imageInfo.non_linear_range=BT709_N;
break;
case 3: //16bit sRGB
ss << "-6 -W -g 2.4 12.8 ";
imageInfo.gamma=SRGB_G;
imageInfo.non_linear_range=SRGB_N;
break;
case 4: //16bit IUT BT.709 (auto WP)
ss << "-6 -g 2.222 4.5 ";
imageInfo.gamma=BT709_G;
imageInfo.non_linear_range=BT709_N;
break;
case 5: //16bit sRGB (auto WP)
ss << "-6 -g 2.4 12.8 ";
imageInfo.gamma=SRGB_G;
imageInfo.non_linear_range=SRGB_N;
break;
case 6: //8bit sRGB (auto WP)
ss << "-g 2.4 12.8 ";
imageInfo.gamma=SRGB_G;
imageInfo.non_linear_range=SRGB_N;
break;
case 7: //8bit linear
ss << "-W -g 1 1 ";
imageInfo.gamma=LIN_G;
imageInfo.non_linear_range=LIN_N;
break;
case 8: //8bit IUT BT.709
ss << "-W -g 2.222 4.5 ";
imageInfo.gamma=BT709_G;
imageInfo.non_linear_range=BT709_N;
break;
case 9: //8bit sRGB
ss << "-W -g 2.4 12.8 ";
imageInfo.gamma=SRGB_G;
imageInfo.non_linear_range=SRGB_N;
break;
case 0: //8bit IUT BT.709 (auto WP)
default:
imageInfo.gamma=BT709_G;
imageInfo.non_linear_range=BT709_N;
break;
}
if ( mod("red_mag") != "1.000" || mod("blue_mag") != "1.000" )
ss << "-C " << mod("red_mag").tof() << " " << mod("blue_mag").tof() << " ";
ss << "\"" << mod("root") << filename << "\"" << " > \"" << tfile << "\"";
return ss.str();
}
void doCurve() {
Profile prof("curve creation");
curve.size("65536x1");
Pixels curve_cache(curve);
PixelPacket *pixels = curve_cache.get(0,0,65536,1);
if ( mod("discrete").toi() ) {
double discrete=mod("discrete").tof();
for(int i=0; i < 65536 ; ++i) {
quantum_t c = pow(2., 16. - floor( -discrete*log(double(i)/QuantumRange)/log(2) )/discrete );
if (c > QuantumRange) c=QuantumRange;
pixels[i].red=pixels[i].green=pixels[i].blue=c;
}
}
else
for(int i=0; i < 65536 ; ++i)
pixels[i].red=pixels[i].green=pixels[i].blue=i;
curve_cache.sync();
iGamma(curve,imageInfo.gamma,imageInfo.non_linear_range,false);
}
void doHotPixels() {
int hp_enabled = mod("hp_enabled").toi();
int hp_aggressive = mod("hp_aggressive").toi();
if ( hp_enabled ) {
if ( imageInfo.iso_speed >= hp_enabled )
image = removeHotPixels(image,
pow(2.,mod("hp_delta").tof()),
imageInfo.iso_speed >= hp_aggressive ? 1 : 0,
mod("hp_naive").toi());
}
}
void doJob() {
if ( upToDate )
return;
int drizzle=mod("drizzle").toi();
int halfSize=mod("halfSize").toi();
int divisor=mod("divisor").toi();
bool recrop=false;
double coef=0.;
if (-1 == last_drizzle)
last_drizzle = drizzle;
if (-1 == last_halfSize)
last_halfSize=halfSize;