-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix-ca.c
1482 lines (1323 loc) · 50.9 KB
/
fix-ca.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
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
/*
* fix-ca.c - gimp3-plugin-fix-ca - Fix Chromatic Aberration
* Copyright (c) 2006, 2007 Kriang Lerdsuwanakij - (original author)
* Copyright (c) 2023, 2024 Jose Da Silva (gimp3, updates, improvements)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
#include <string.h>
#include <math.h>
#ifdef GDK_WINDOWING_QUARTZ
#import <Cocoa/Cocoa.h>
#elif defined (G_OS_WIN32)
#include <windows.h>
#endif
#if __has_include("fix-ca-config.h")
#include "fix-ca-config.h"
#else
#define FIX_CA_VERSION "fix-CA local"
#endif
#ifdef TEST_FIX_CA
#define PLUG_IN_PROC "plug-in-test-fix-ca"
#define PLUG_IN_ROLE "test-fix-ca"
#define PLUG_IN_BINARY "test-fix-ca"
/* No i18n for now */
#define _(x) x
#define d_(x) x
#define N_(x) x
#ifndef DEBUG_TIME
/* be verbose during 'make check' */
#define DEBUG_TIME 1
#endif
#else
#define PLUG_IN_PROC "plug-in-fix-ca"
#define PLUG_IN_ROLE "gimp-fix-ca"
#define PLUG_IN_BINARY "fix-ca"
#ifdef HAVE_GETTEXT
#include <libintl.h>
#include <locale.h>
#define _(String) gettext (String)
#define d_(String) (String)
#ifdef gettext_noop
# define N_(String) gettext_noop (String)
#else
# define N_(String) (String)
#endif
#else
/* No i18n for now */
#define _(x) x
#define d_(x) x
#define N_(x) x
#endif
#endif
#ifdef DEBUG_TIME
# include <sys/time.h>
# include <stdio.h>
#endif
/* For fixca() row buffer management */
#define SOURCE_ROWS 120
#define INPUT_MAX SOURCE_ROWS/4
#define INPUT_MXF SOURCE_ROWS/4.0
#define ROW_INVALID -100
#define ITER_INITIAL -100
typedef struct _FixCa FixCa;
typedef struct _FixCaClass FixCaClass;
struct _FixCa {
GimpPlugIn parent_instance;
};
struct _FixCaClass {
GimpPlugInClass parent_class;
};
/* Storage type */
typedef struct {
gdouble blueL;
gdouble redL;
gdouble lensX;
gdouble lensY;
GimpInterpolationType interpolation;
gdouble blueX;
gdouble redX;
gdouble blueY;
gdouble redY;
/* local image use */
GimpDrawable *drawable;
guchar *srcImg;
guchar *destImg;
gint Xsize;
gint Ysize;
gint bpp;
gint bpc;
gdouble saturation;
gboolean reset_values;
} FixCaParams;
/* Declare local functions. */
#define FIXCA_TYPE (fixca_get_type())
#define FIXCA (obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), FIXCA_TYPE, FixCa))
GType fixca_get_type (void) G_GNUC_CONST;
static GList * fixca_query_procedures (GimpPlugIn *plug_in);
static GimpProcedure * fixca_create_procedure (GimpPlugIn *plug_in,
const gchar *name);
static GimpValueArray * fixca_run (GimpProcedure *procedure,
GimpRunMode run_mode,
GimpImage *image,
GimpDrawable **drawables,
GimpProcedureConfig *proc_config,
gpointer run_data);
static gboolean fixca_dialog (GimpProcedure *procedure,
GObject *config,
FixCaParams *params);
static void fixca_region (FixCaParams *params,
gint x1,
gint x2,
gint y1,
gint y2,
gint show_progress);
static void fixca_help (const gchar *help_id,
gpointer help_data);
static void preview_update (GtkWidget *widget,
GObject *config);
static void set_default_settings (FixCaParams *params);
G_DEFINE_TYPE (FixCa, fixca, GIMP_TYPE_PLUG_IN)
GIMP_MAIN (FIXCA_TYPE)
//DEFINE_STD_SET_I18N; ***don't use - 3rd-party locale external to gimp3
static void
fixca_class_init (FixCaClass *klass)
{
GimpPlugInClass *plug_in_class = GIMP_PLUG_IN_CLASS (klass);
plug_in_class->query_procedures = fixca_query_procedures;
plug_in_class->create_procedure = fixca_create_procedure;
//plug_in_class->set_i18n = STD_SET_I18N;
}
static void
fixca_init (FixCa *fixca)
{
}
static GList *
fixca_query_procedures (GimpPlugIn *plug_in)
{
return g_list_append (NULL, g_strdup (PLUG_IN_PROC));
}
/* Only translate 'Colors', <Image>/Filters/ stays as-English */
static const char *PLUG_IN_MENU_LOCATION = d_("<Image>/Filters/Colors");
static const char *PLUG_IN_MENU_LABEL = d_("Chromatic Aberration...");
static const char *PLUG_IN_SHORT_DESC = d_(
"Fix Chromatic Aberration by shifting red and blue pixels.");
static const char *PLUG_IN_LONG_DESC = d_(
"Lateral Chromatic Aberration is due to camera lens(es) "
"with no aberration at the lens center, and increasing "
"gradually toward the edges of the image. Digital cameras "
"may correct for this in software, analog cameras, or "
"adapters may show this in resulting photos or images.\n\n"
"Directional X and Y axis aberrations are a flat amount "
"of aberration due to image seen through something like "
"glass, water, or another medium of different density. "
"You can shift pixels up/left {-30..+30} down/right.\n\n"
"Lateral aberration correction is applied first, since "
"the lens(es) are closest to the film or image sensor, "
"and directional corrections applied last since this is "
"the furthest away from the camera.\n\n"
"The image to modify is in RGB format. Color precision "
"can be double, 8, 16, 32 or 64. The green pixels are "
"kept stationary, and you can shift red and blue colors "
"within a range of {-30..+30} pixels.");
static GimpProcedure *
fixca_create_procedure (GimpPlugIn *plug_in,
const gchar *name)
{
GimpProcedure *procedure = NULL;
if (!strcmp (name, PLUG_IN_PROC)) {
procedure = gimp_image_procedure_new (plug_in, name,
GIMP_PDB_PROC_TYPE_PLUGIN,
fixca_run, NULL, NULL);
gimp_procedure_set_image_types (procedure, "RGB*");
gimp_procedure_set_sensitivity_mask (procedure,
GIMP_PROCEDURE_SENSITIVE_DRAWABLE);
#ifndef TEST_FIX_CA
#ifdef HAVE_GETTEXT
/* Initialize i18n support */
setlocale (LC_ALL, "");
bindtextdomain (GETTEXT_PACKAGE, gimp_locale_directory ());
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
#endif
textdomain (GETTEXT_PACKAGE);
#endif
#endif
gimp_procedure_set_menu_label (procedure, _(PLUG_IN_MENU_LABEL));
gimp_procedure_add_menu_path (procedure, _(PLUG_IN_MENU_LOCATION));
gimp_procedure_set_documentation (procedure,
/* menu entry tooltip blurb */ _(PLUG_IN_SHORT_DESC),
/* help for script developers */ _(PLUG_IN_LONG_DESC),
/* help ID */ PLUG_IN_PROC);
gimp_procedure_set_attribution (procedure,
/* author(s), original first */ "Kriang Lerdsuwanakij (2006..), Jose Da Silva (2022..)",
/* copyright license */ "GPL3+",
/* date for the latest build */ "2024");
gimp_procedure_add_double_argument (procedure, "bluel",
_("_Blue-L"), _("Blue amount (lateral)"),
-INPUT_MXF, INPUT_MXF, 0.0,
G_PARAM_READWRITE);
gimp_procedure_add_double_argument (procedure, "redl",
_("_Red-L"), _("Red amount (lateral)"),
-INPUT_MXF, INPUT_MXF, 0.0,
G_PARAM_READWRITE);
gimp_procedure_add_double_argument (procedure, "lensx",
_("Lens_X"), _("Lens center X (lateral)"),
-1.0, GIMP_MAX_IMAGE_SIZE-1, -1.0,
G_PARAM_READWRITE);
gimp_procedure_add_double_argument (procedure, "lensy",
_("Lens_Y"), _("Lens center Y (lateral)"),
-1.0, GIMP_MAX_IMAGE_SIZE-1, -1.0,
G_PARAM_READWRITE);
gimp_procedure_add_int_argument (procedure, "interpolation",
_("_Interpolation"),
_("Interpolation 0=None/1=Linear/2=Cubic"),
0, 2, 1, G_PARAM_READWRITE);
gimp_procedure_add_double_argument (procedure, "bluex",
_("B_lue-X"),
_("Blue amount, X axis (directional)"),
-INPUT_MAX, INPUT_MAX, 0.0,
G_PARAM_READWRITE);
gimp_procedure_add_double_argument (procedure, "redx",
_("R_ed-X"),
_("Red amount, X axis (directional)"),
-INPUT_MAX, INPUT_MAX, 0.0,
G_PARAM_READWRITE);
gimp_procedure_add_double_argument (procedure, "bluey",
_("Bl_ue-Y"),
_("Blue amount, Y axis (directional)"),
-INPUT_MAX, INPUT_MAX, 0.0,
G_PARAM_READWRITE);
gimp_procedure_add_double_argument (procedure, "redy",
_("Re_d-Y"),
_("Red amount, Y axis (directional)"),
-INPUT_MAX, INPUT_MAX, 0.0,
G_PARAM_READWRITE);
}
return procedure;
}
static GimpValueArray *
fixca_run (GimpProcedure *procedure,
GimpRunMode run_mode,
GimpImage *image,
GimpDrawable **drawables,
GimpProcedureConfig *proc_config,
gpointer run_data)
{
const Babl *format;
GeglBuffer *srcBuf;
GeglBuffer *shadow;
GimpDrawable *drawable;
FixCaParams fix_ca_params;
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
GError *error = NULL;
gint x, y, width, height, sizeImg;
#ifndef TEST_FIX_CA
#ifdef HAVE_GETTEXT
/* Initialize i18n support */
setlocale(LC_ALL, "");
bindtextdomain(GETTEXT_PACKAGE, gimp_locale_directory());
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
#endif
textdomain(GETTEXT_PACKAGE);
#endif
#endif
if (gimp_core_object_array_get_length ((GObject **)(drawables)) != 1) {
g_set_error (&error, GIMP_PLUG_IN_ERROR, 0,
_("Procedure '%s' only works with one drawable."),
gimp_procedure_get_name (procedure));
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_CALLING_ERROR,
error);
}
fix_ca_params.drawable = drawable = drawables[0];
if (!gimp_drawable_is_rgb (drawable)) {
g_set_error (&error, GIMP_PLUG_IN_ERROR, 0,
_("Procedure '%s' only works with RGB or RGBA."),
gimp_procedure_get_name (procedure));
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_CALLING_ERROR,
error);
}
/* init, and fetch variables */
fix_ca_params.Xsize = gimp_drawable_get_width (drawable);
fix_ca_params.Ysize = gimp_drawable_get_height (drawable);
fix_ca_params.bpp = gimp_drawable_get_bpp (drawable);
fix_ca_params.srcImg = fix_ca_params.destImg = NULL;
set_default_settings (&fix_ca_params);
g_object_get (proc_config,
"interpolation", &fix_ca_params.interpolation,
"bluel", &fix_ca_params.blueL,
"redl", &fix_ca_params.redL,
"lensx", &fix_ca_params.lensX,
"lensy", &fix_ca_params.lensY,
"bluex", &fix_ca_params.blueX,
"redx", &fix_ca_params.redX,
"bluey", &fix_ca_params.blueY,
"redy", &fix_ca_params.redY,
NULL);
if (fix_ca_params.blueL < -INPUT_MAX)
fix_ca_params.blueL = -INPUT_MAX;
if (fix_ca_params.blueL > INPUT_MAX)
fix_ca_params.blueL = INPUT_MAX;
if (fix_ca_params.redL < -INPUT_MAX)
fix_ca_params.redL = -INPUT_MAX;
if (fix_ca_params.redL > INPUT_MAX)
fix_ca_params.redL = INPUT_MAX;
#ifdef DEBUG_TIME
printf("lensX=%g lensY=%g, Xsize=%d Ysize=%d\n",
fix_ca_params.lensX, fix_ca_params.lensY,
fix_ca_params.Xsize, fix_ca_params.Ysize);
#endif
if (fix_ca_params.lensX < 0.0 || fix_ca_params.lensX > fix_ca_params.Xsize-1)
fix_ca_params.lensX = -1.0;
if (fix_ca_params.lensY < 0.0 || fix_ca_params.lensY > fix_ca_params.Ysize-1)
fix_ca_params.lensY = -1.0;
if (fix_ca_params.interpolation < 0 || fix_ca_params.interpolation > 2)
fix_ca_params.interpolation = 0;
if (fix_ca_params.blueX < -INPUT_MAX)
fix_ca_params.blueX = -INPUT_MAX;
if (fix_ca_params.blueX > INPUT_MAX)
fix_ca_params.blueX = INPUT_MAX;
if (fix_ca_params.redX < -INPUT_MAX)
fix_ca_params.redX = -INPUT_MAX;
if (fix_ca_params.redX > INPUT_MAX)
fix_ca_params.redX = INPUT_MAX;
if (fix_ca_params.blueY < -INPUT_MAX)
fix_ca_params.blueY = -INPUT_MAX;
if (fix_ca_params.blueY > INPUT_MAX)
fix_ca_params.blueY = INPUT_MAX;
if (fix_ca_params.redY < -INPUT_MAX)
fix_ca_params.redY = -INPUT_MAX;
if (fix_ca_params.redY > INPUT_MAX)
fix_ca_params.redY = INPUT_MAX;
/* figure-out bytes per color precision */
switch (gimp_image_get_precision (image)) {
case GIMP_PRECISION_U8_LINEAR:
case GIMP_PRECISION_U8_NON_LINEAR:
case GIMP_PRECISION_U8_PERCEPTUAL:
fix_ca_params.bpc = 1;
break;
case GIMP_PRECISION_U16_LINEAR:
case GIMP_PRECISION_U16_NON_LINEAR:
case GIMP_PRECISION_U16_PERCEPTUAL:
fix_ca_params.bpc = 2;
break;
case GIMP_PRECISION_U32_LINEAR:
case GIMP_PRECISION_U32_NON_LINEAR:
case GIMP_PRECISION_U32_PERCEPTUAL:
fix_ca_params.bpc = 4;
break;
case GIMP_PRECISION_DOUBLE_LINEAR:
case GIMP_PRECISION_DOUBLE_NON_LINEAR:
case GIMP_PRECISION_DOUBLE_PERCEPTUAL:
fix_ca_params.bpc = -8; /* IEEE 754 double precision */
break;
case GIMP_PRECISION_FLOAT_LINEAR:
case GIMP_PRECISION_FLOAT_NON_LINEAR:
case GIMP_PRECISION_FLOAT_PERCEPTUAL:
fix_ca_params.bpc = -4; /* IEEE 754 single precision */
break;
case GIMP_PRECISION_HALF_LINEAR:
case GIMP_PRECISION_HALF_NON_LINEAR:
case GIMP_PRECISION_HALF_PERCEPTUAL:
fix_ca_params.bpc = -2; /* IEEE 754 half precision */
break;
default:
if (fix_ca_params.bpp == 24 || fix_ca_params.bpp == 32) {
fix_ca_params.bpc = 8; /* 64bit RBC/RBCA */
} else {
g_set_error (&error, GIMP_PLUG_IN_ERROR, 0,
_("Procedure '%s' cannot use this color precision."),
gimp_procedure_get_name (procedure));
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_CALLING_ERROR,
error);
}
}
#ifdef DEBUG_TIME
printf("lensX=%g lensY=%g, Xsize=%d Ysize=%d, ",
fix_ca_params.lensX, fix_ca_params.lensY,
fix_ca_params.Xsize, fix_ca_params.Ysize);
printf("bytes per pixel=%d, bytes per color=%d\n",
fix_ca_params.bpp, fix_ca_params.bpc);
#endif
/* prepare to draw, or preview, fetch values and image */
sizeImg = fix_ca_params.Xsize * fix_ca_params.Ysize * fix_ca_params.bpp;
fix_ca_params.srcImg = g_new (guchar, sizeImg);
fix_ca_params.destImg = g_new (guchar, sizeImg);
if (fix_ca_params.srcImg == NULL || fix_ca_params.destImg == NULL) {
/* TODO: REDO gimp3-fix-ca to work with super-large-images */
if (fix_ca_params.destImg != NULL) g_free(fix_ca_params.destImg);
if (fix_ca_params.srcImg != NULL) g_free(fix_ca_params.srcImg);
g_set_error (&error, GIMP_PLUG_IN_ERROR, 0,
_("Procedure '%s', not enough RAM."),
gimp_procedure_get_name (procedure));
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_CALLING_ERROR,
error);
}
format = gimp_drawable_get_format (drawable);
gegl_init (NULL, NULL);
srcBuf = gimp_drawable_get_buffer (drawable);
gegl_buffer_get (srcBuf,
GEGL_RECTANGLE (0, 0, fix_ca_params.Xsize, fix_ca_params.Ysize),
1.0, format, fix_ca_params.srcImg, GEGL_AUTO_ROWSTRIDE,
GEGL_ABYSS_NONE);
/* In interactive mode, display dialog to get 'config' variables. */
if (run_mode == GIMP_RUN_INTERACTIVE) {
/* Set lens center before running dialog */
fix_ca_params.lensX = round ((fix_ca_params.Xsize+0.5)/2);
fix_ca_params.lensY = round ((fix_ca_params.Ysize+0.5)/2);
#ifdef DEBUG_TIME
printf("lensX=%g lensY=%g\n", fix_ca_params.lensX, fix_ca_params.lensY);
#endif
if (! fixca_dialog (procedure, G_OBJECT (proc_config), &fix_ca_params))
status = GIMP_PDB_CANCEL;
}
/* Modify image according to input params */
if (status == GIMP_PDB_SUCCESS &&
gimp_drawable_mask_intersect (drawable, &x, &y, &width, &height)) {
#ifdef DEBUG_TIME
double sec;
struct timeval tv1, tv2;
gettimeofday (&tv1, NULL);
printf ("Start fixca() image_size, {x=%d width=%d} {y=%d height=%d}\n",
x, width, y, height);
#endif
shadow = gimp_drawable_get_shadow_buffer(drawable);
/* adjust pixel regions from srcImg to destImg, according to params */
fixca_region (&fix_ca_params, x, (x+width), y, (y+height),
((run_mode == GIMP_RUN_NONINTERACTIVE)? -1:1));
#ifdef DEBUG_TIME
printf ("finished doing fixca_region,\n");
printf ("..Lateral correction: {blue=%g red=%g, lens center(x=%g, y=%g)}\n",
fix_ca_params.blueL, fix_ca_params.redL,
fix_ca_params.lensX, fix_ca_params.lensY);
printf ("..Image size: {Xsize=%d=%d, Ysize=%d=%d}\n",
fix_ca_params.Xsize, width, fix_ca_params.Ysize, height);
printf ("..Differential correction: {x_blue=%g x_red=%g x_blue=%g x_red=%g}\n",
fix_ca_params.blueX, fix_ca_params.redX,
fix_ca_params.blueY, fix_ca_params.redY);
printf ("..interpolation_mode=%d preview_saturation=%g reset_value=%d\n",
fix_ca_params.interpolation, fix_ca_params.saturation,
fix_ca_params.reset_values);
#endif
gegl_buffer_set (shadow, GEGL_RECTANGLE(x, y, width, height),
0, format, fix_ca_params.destImg, GEGL_AUTO_ROWSTRIDE);
gegl_buffer_flush (shadow);
gimp_drawable_merge_shadow (drawable, TRUE);
gimp_drawable_update (drawable, x, y, width, height);
g_object_unref (shadow);
#ifdef DEBUG_TIME
gettimeofday (&tv2, NULL);
sec = tv2.tv_sec - tv1.tv_sec + (tv2.tv_usec - tv1.tv_usec)/1000000.0;
printf ("End fixca(), Elapsed time: %.2f\n", sec);
#endif
}
if (run_mode != GIMP_RUN_NONINTERACTIVE)
gimp_displays_flush ();
g_object_unref(srcBuf);
//gegl_exit ();
g_free (fix_ca_params.destImg);
g_free (fix_ca_params.srcImg);
#ifdef DEBUG_TIME
printf ("Program end.\n");
#endif
return gimp_procedure_new_return_values (procedure, status, NULL);
}
static void
set_default_settings (FixCaParams *params)
{
params->blueL = params->redL = 0.0;
params->lensX = round ((params->Xsize+0.5)/2);
params->lensY = round ((params->Ysize+0.5)/2);
params->interpolation = GIMP_INTERPOLATION_NONE;
params->saturation = 0.0;
params->blueX = params->redX = 0.0;
params->blueY = params->redY = 0.0;
params->reset_values = FALSE;
}
static gdouble
get_pixel (guchar *ptr, gint bpc)
{
/* Returned value is in the range of [0.0..1.0]. */
gdouble ret = 0.0;
if (bpc == 1) {
ret += *ptr;
ret /= 255;
} else if (bpc == 2) {
uint16_t *p = (uint16_t *)(ptr);
ret += *p;
ret /= 65535;
} else if (bpc == 4) {
uint32_t *p = (uint32_t *)(ptr);
ret += *p;
ret /= 4294967295;
} else if (bpc == 8) {
uint64_t *p = (uint64_t *)(ptr);
long double lret = 0.0;
lret += *p;
lret /= 18446744073709551615L;
ret = lret;
} else if (bpc == -8) {
double *p = (double *)(ptr);
ret += *p;
} else if (bpc == -4) {
float *p = (float *)(ptr);
ret += *p;
//} else if (bpc == -2) {
// half *p = ptr;
// ret += *p;
}
return ret;
}
static void
set_pixel (guchar *dest, gdouble d, gint bpc)
{
/* input value is in the range of [0.0..1.0]. */
if (bpc == 1) {
*dest = round (d * 255);
} else if (bpc == 2) {
uint16_t *p = (uint16_t *)(dest);
*p = round (d * 65535);
} else if (bpc == 4) {
uint32_t *p = (uint32_t *)(dest);
*p = round (d * 4294967295);
} else if (bpc == 8) {
uint64_t *p = (uint64_t *)(dest);
*p = roundl (d * 18446744073709551615L);
} else if (bpc == -8) {
double *p = (double *)(dest);
*p = d;
} else if (bpc == -4) {
float *p = (float *)(dest);
*p = (float)(d);
//} else if (bpc == -2) {
// half *p = (half *)(dest);
// *p = d;
}
return;
}
static int
round_nearest (gdouble d)
{
if (d >= 0) {
if (d > INT_MAX)
return INT_MAX;
else
return (int)(d + 0.5);
} else {
if (d < INT_MIN)
return INT_MIN;
else
return -((int)(0.5 - d));
}
}
static int
absolute (gint i)
{
if (i >= 0)
return i;
else
return -i;
}
static void
preview_update (GtkWidget *widget, GObject *config)
{
GimpPreview *preview;
FixCaParams *params;
guchar *buffer;
gint b, i, j, x, y, width, height;
gdouble d;
preview = GIMP_PREVIEW (widget);
params = g_object_get_data (config, "fixcaparams");
gimp_preview_get_position (preview, &x, &y);
gimp_preview_get_size (preview, &width, &height);
#ifdef DEBUG_TIME
printf ("preview_update(), x=%d y=%d w=%d h=%d bpp=%d bcp=%d\n",
x, y, width, height, params->bpp, params->bpc);
printf ("..blue=%g red=%g lensX=%g lensY=%g\n",
params->blueL, params->redL, params->lensX, params->lensY);
printf ("..interpolation=%d, blueX=%g redX=%g, blueY=%g redY=%g\n",
params->interpolation, params->blueX, params->redX,
params->blueY, params->redY);
#endif
fixca_region (params, 0, params->Xsize, y, (y + height), 0);
buffer = g_new (guchar, width * height * params->bpp);
b = absolute (params->bpc);
for (i = 0; i < height; i++) {
if (b == 1) {
memcpy (&buffer[width * i * params->bpp],
¶ms->destImg[(params->Xsize * (y + i) + x) * params->bpp],
width * params->bpp);
} else {
for (j = 0; j < width*params->bpp/b; j++) {
d = get_pixel (¶ms->destImg[(params->Xsize*(y+i)+x)*params->bpp+j*b], params->bpc);
set_pixel (&buffer[width*i*params->bpp/b+j], d, 1);
}
}
}
gimp_preview_draw_buffer (preview, buffer, width * params->bpp/b);
g_free (buffer);
#ifdef DEBUG_TIME
printf ("..done preview_update\n");
#endif
}
static void
gfix_scale_entry_update (GimpLabelSpin *entry,
gdouble *value)
{
*value = gimp_label_spin_get_value (entry);
}
static void
gfix_combo_entry_update (GtkWidget *widget,
gpointer *data)
{
gimp_int_combo_box_get_active (GIMP_INT_COMBO_BOX (widget), (gint *) data);
}
static gboolean
fixca_dialog (GimpProcedure *procedure,
GObject *config,
FixCaParams *params)
{
GtkWidget *dialog;
GtkWidget *main_vbox;
GtkWidget *combo;
GtkWidget *preview;
GtkWidget *frame;
GtkWidget *adj;
gchar *title;
gboolean run;
gimp_ui_init (PLUG_IN_BINARY);
title = g_strdup_printf (_("Chromatic Aberration"));
dialog = gimp_dialog_new (title, "fix_ca",
NULL, (GtkDialogFlags) 0,
fixca_help, PLUG_IN_PROC,
_("_Cancel"), GTK_RESPONSE_CANCEL,
_("_OK"), GTK_RESPONSE_OK,
NULL);
g_free (title);
g_object_set_data (config, "fixcaparams", params);
main_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
main_vbox, TRUE, TRUE, 0);
gtk_widget_show (main_vbox);
preview = gimp_drawable_preview_new_from_drawable (params->drawable);
gtk_box_pack_start (GTK_BOX (main_vbox), preview, TRUE, TRUE, 0);
gtk_widget_show (preview);
g_signal_connect (preview, "invalidated", G_CALLBACK (preview_update), config);
g_signal_connect_swapped (config, "notify",
G_CALLBACK (gimp_preview_invalidate),
preview);
adj = gimp_scale_entry_new (_("_Saturation:"),
params->saturation, -100.0, 100.0, 0);
gimp_label_spin_set_increments (GIMP_LABEL_SPIN(adj), 1, 10);
gimp_help_set_help_data (adj,
_("Saturate colors in preview window to help you see overlaps"),
NULL);
g_signal_connect (adj, "value_changed",
G_CALLBACK (gfix_scale_entry_update),
&(params->saturation));
g_signal_connect_swapped (adj, "value_changed",
G_CALLBACK(gimp_preview_invalidate),
preview);
gtk_box_pack_start (GTK_BOX (main_vbox), adj, FALSE, FALSE, 0);
gtk_widget_show (adj);
combo = gimp_int_combo_box_new (_("_None (Fastest)"), GIMP_INTERPOLATION_NONE,
_("L_inear"), GIMP_INTERPOLATION_LINEAR,
_("_Cubic (Best)"), GIMP_INTERPOLATION_CUBIC,
NULL);
gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (combo), params->interpolation);
gimp_help_set_help_data (combo,
_("Method of how to move Blue and Red Pixels"),
NULL);
g_signal_connect (combo, "changed",
G_CALLBACK (gimp_int_combo_box_get_active),
&(params->interpolation));
gtk_box_pack_start (GTK_BOX (main_vbox), combo, FALSE, FALSE, 0);
gtk_widget_show (combo);
frame = gimp_frame_new (_("Lateral"));
gimp_help_set_help_data (frame,
_("Do corrections for lens affected chromatic aberration"),
NULL);
gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
gtk_widget_show (frame);
adj = gimp_scale_entry_new (_("_Blue:"), params->blueL, -INPUT_MAX, INPUT_MAX, 1);
gimp_label_spin_set_increments (GIMP_LABEL_SPIN (adj), 0.1, 0.5);
gtk_box_pack_start (GTK_BOX (main_vbox), adj, FALSE, FALSE, 0);
gtk_widget_show (adj);
g_signal_connect (adj, "value_changed",
G_CALLBACK (gfix_scale_entry_update),
&(params->blueL));
g_signal_connect_swapped (adj, "value_changed",
G_CALLBACK(gimp_preview_invalidate),
preview);
adj = gimp_scale_entry_new (_("_Red:"), params->redL, -INPUT_MAX, INPUT_MAX, 1);
gimp_label_spin_set_increments (GIMP_LABEL_SPIN (adj), 0.1, 0.5);
gtk_box_pack_start (GTK_BOX (main_vbox), adj, FALSE, FALSE, 0);
gtk_widget_show (adj);
g_signal_connect (adj, "value_changed",
G_CALLBACK (gfix_scale_entry_update),
&(params->redL));
g_signal_connect_swapped (adj, "value_changed",
G_CALLBACK(gimp_preview_invalidate),
preview);
adj = gimp_scale_entry_new (_("Lens_X:"), params->lensX, -1, params->Xsize-1, 0);
gimp_label_spin_set_increments (GIMP_LABEL_SPIN (adj), 10, 100);
gimp_label_spin_set_value (GIMP_LABEL_SPIN (adj), params->lensX);
gtk_box_pack_start (GTK_BOX (main_vbox), adj, FALSE, FALSE, 0);
gtk_widget_show (adj);
g_signal_connect (adj, "value_changed",
G_CALLBACK (gfix_scale_entry_update),
&(params->lensX));
g_signal_connect_swapped (adj, "value_changed",
G_CALLBACK(gimp_preview_invalidate),
preview);
adj = gimp_scale_entry_new (_("Lens_Y:"), params->lensY, -1, params->Ysize-1, 0);
gimp_label_spin_set_increments (GIMP_LABEL_SPIN (adj), 10, 100);
gimp_label_spin_set_value (GIMP_LABEL_SPIN (adj), params->lensY);
gtk_box_pack_start (GTK_BOX (main_vbox), adj, FALSE, FALSE, 0);
gtk_widget_show (adj);
g_signal_connect (adj, "value_changed",
G_CALLBACK (gfix_scale_entry_update),
&(params->lensY));
g_signal_connect_swapped (adj, "value_changed",
G_CALLBACK(gimp_preview_invalidate),
preview);
frame = gimp_frame_new (_("Directional, X axis"));
gimp_help_set_help_data (frame,
_("Do flat directional corrections along the X axis"),
NULL);
gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
gtk_widget_show (frame);
adj = gimp_scale_entry_new (_("B_lue:"), params->blueX, -INPUT_MAX, INPUT_MAX, 1);
gimp_label_spin_set_increments (GIMP_LABEL_SPIN (adj), 0.1, 0.5);
gtk_box_pack_start (GTK_BOX (main_vbox), adj, FALSE, FALSE, 0);
gtk_widget_show (adj);
g_signal_connect (adj, "value_changed",
G_CALLBACK (gfix_scale_entry_update),
&(params->blueX));
g_signal_connect_swapped (adj, "value_changed",
G_CALLBACK(gimp_preview_invalidate),
preview);
adj = gimp_scale_entry_new (_("R_ed:"), params->redX, -INPUT_MAX, INPUT_MAX, 1);
gimp_label_spin_set_increments (GIMP_LABEL_SPIN (adj), 0.1, 0.5);
gtk_box_pack_start (GTK_BOX (main_vbox), adj, FALSE, FALSE, 0);
gtk_widget_show (adj);
g_signal_connect (adj, "value_changed",
G_CALLBACK (gfix_scale_entry_update),
&(params->redX));
g_signal_connect_swapped(adj, "value_changed",
G_CALLBACK(gimp_preview_invalidate),
preview);
frame = gimp_frame_new (_("Directional, Y axis"));
gimp_help_set_help_data (frame,
_("Do flat directional corrections along the Y axis"),
NULL);
gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
gtk_widget_show (frame);
adj = gimp_scale_entry_new (_("Bl_ue:"), params->blueY, -INPUT_MAX, INPUT_MAX, 1);
gimp_label_spin_set_increments (GIMP_LABEL_SPIN (adj), 0.1, 0.5);
gtk_box_pack_start (GTK_BOX (main_vbox), adj, FALSE, FALSE, 0);
gtk_widget_show (adj);
g_signal_connect (adj, "value_changed",
G_CALLBACK (gfix_scale_entry_update),
&(params->blueY));
g_signal_connect_swapped (adj, "value_changed",
G_CALLBACK(gimp_preview_invalidate),
preview);
adj = gimp_scale_entry_new (_("Re_d:"), params->redY, -INPUT_MAX, INPUT_MAX, 1);
gimp_label_spin_set_increments (GIMP_LABEL_SPIN (adj), 0.1, 0.5);
gtk_box_pack_start (GTK_BOX (main_vbox), adj, FALSE, FALSE, 0);
gtk_widget_show (adj);
g_signal_connect (adj, "value_changed",
G_CALLBACK (gfix_scale_entry_update),
&(params->redY));
g_signal_connect_swapped (adj, "value_changed",
G_CALLBACK(gimp_preview_invalidate),
preview);
gtk_widget_show (dialog);
run = (gimp_dialog_run (GIMP_DIALOG(dialog)) == GTK_RESPONSE_OK);
if (run) {
/* set config for future in case we use these settings again */
g_object_set (config,
"interpolation", ¶ms->interpolation,
"bluel", ¶ms->blueL,
"redl", ¶ms->redL,
"lensx", ¶ms->lensX,
"lensy", ¶ms->lensY,
"bluex", ¶ms->blueX,
"redx", ¶ms->redX,
"bluey", ¶ms->blueY,
"redy", ¶ms->redY,
NULL);
}
gtk_widget_destroy (dialog);
return run;
}
static int
scale (gint i, gint center, gint size, gdouble scale_val, gdouble shift_val)
{
gdouble d = (i - center) * scale_val + center - shift_val;
gint j = round_nearest (d);
if (j <= 0)
return 0;
else if (j < size)
return j;
else
return size-1;
}
static double
scale_d (gint i, gint center, gint size, gdouble scale_val, gdouble shift_val)
{
gdouble d = (i - center) * scale_val + center - shift_val;
if (d <= 0.0)
return 0.0;
else if (d >= size-1)
return size-1;
else
return d;
}
static guchar *
load_data (gint fullWidth, gint bpp, guchar *srcPTR,
guchar *src[SOURCE_ROWS], gint src_row[SOURCE_ROWS],
gint src_iter[SOURCE_ROWS], gint band_adj,
gint band_left, gint band_right, gint y, gint iter)
{
gint i, l, x, diff, diff_max = -1, row_best = -1;
int iter_oldest;
for (i = 0; i < SOURCE_ROWS; ++i) {
if (src_row[i] == y) {
src_iter[i] = iter; /* Make sure to keep this row during this iteration */
return src[i];
}
}
/* Find a row to replace */
iter_oldest = INT_MAX; /* Largest possible */
for (i = 0; i < SOURCE_ROWS; ++i) {
if (src_iter[i] < iter_oldest) {
iter_oldest = src_iter[i];
diff_max = absolute (y - src_row[i]);
row_best = i;
} else if (src_iter[i] == iter_oldest) {
diff = absolute (y - src_row[i]);
if (diff > diff_max) {
diff_max = diff;
row_best = i;
}
}
}
x = ((fullWidth * y) + band_left) * bpp;
i = band_adj * bpp;
l = i + (band_right-band_left+1) * bpp;
memcpy (&src[row_best][i], &srcPTR[x + i], l - i);
src_row[row_best] = y;
src_iter[row_best] = iter;
return src[row_best];
}
static void
set_data (guchar *dest, FixCaParams *params, gint xstart, gint yrow, gint width)
{
gint l, x;
x = (params->Xsize * yrow + xstart) * params->bpp;
l = width * params->bpp;
memcpy (¶ms->destImg[x], dest, l);
}
static gdouble
clip_d (gdouble d)
{
if (d <= 0.0)
return 0.0;
if (d >= 1.0)
return 1.0;
return d;
}