-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimgcmbsm.c
1616 lines (1218 loc) · 40.1 KB
/
imgcmbsm.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
#define _XOPEN_SOURCE
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <jpeglib.h>
#include <jerror.h>
#include <png.h>
// for shared memory
#include <sys/ipc.h>
#include <sys/shm.h>
#include "mkbmp.h"
#include "dispftns.h"
// Thermal camera selection (default without any defines = AMG8833)
#define _MLX90640
// Added lightness for thermal overlay
#define _thermlite 255
#define _USE_5D_THERMCMB 0
#define _USE_JPEG_MINMAX 0
// RGB factor for 5D thermal-optical alignment
// TODO: make this depend on spacing constants also make this a one-time global for performance reasons
#define rgbf 0.0007
// darkness compensation for hues.
// Increase if night shots are too infrared;
// decrease if monochrome illumination looks washed out.
#define iauc 0.01
#ifdef _MLX90640
#define _THERM_W 32
#define _THERM_H 24
// Thermcam offset to align to NoIR - multiply these by wid, hei
float therm_off_x = 0.215;
float therm_off_y = 0.15;
// Thermcam is mounted crooked because I'm under the weather and drunk
float therm_rot_rad = 2.0 * 3.1415926535897932384626 / 180;
// NoIR size per thermcam 16x16-pixel block - multiply these by wid, hei
#define therm_sz_x 0.0213
#define therm_sz_y 0.0285
// Half-size for new thermal squares to be drawn
#define sq_hsz 4
#else
#define _THERM_W 8
#define _THERM_H 8
// Thermcam offset to align to NoIR - multiply these by wid, hei
#define therm_off_x 0.17
#define therm_off_y 0.05
// NoIR size per thermcam 16x16-pixel block - multiply these by wid, hei
#define therm_sz_x 0.10
#define therm_sz_y 0.12
// Half-size for new thermal squares to be drawn
#define sq_hsz 10
#endif
// Eye offset to align to NoIR - multiply these by wid, hei
#define eye_off_x 0.02
#define eye_off_y -.125
// NoIR size per eye pixel - multiply these by wid, hei
#define eye_sz_x 0.195
#define eye_sz_y 0.25
#define line_spacing 81
#define line_width 8
// Mappings
#define _rgi 0x659
#define _rig 0x695
#define _gir 0x596
#define _gri 0x569
#define _irg 0x965
#define _igr 0x956
int histogram[10000];
int histmx = 0;
int therm_mode = _THM_HUE;
typedef struct
{
unsigned int x;
unsigned int y;
uint8_t r;
uint8_t g;
uint8_t b;
}
pixel_5d;
typedef struct
{ unsigned int idx[4];
float wt[4];
}
wghidx;
float distance_between_pix5(pixel_5d a, pixel_5d b)
{
float r, d;
d = a.x - b.x; r = d*d;
d = a.y - b.y; r += d*d;
d = rgbf*(a.r - b.r); r += d*d;
d = rgbf*(a.g - b.g); r += d*d;
d = rgbf*(a.b - b.b); r += d*d;
return pow(r, 0.5);
}
pixel_5d thermspot[_THERM_W*_THERM_H];
unsigned char thermr[_THERM_W*_THERM_H], thermg[_THERM_W*_THERM_H], thermb[_THERM_W*_THERM_H];
int wid, hei, wid2, hei2;
float thmult[780];
unsigned char jprmx, jpgmx, jpbmx, jprmn, jpgmn, jpbmn;
int nearest_thermspot_5d(pixel_5d target)
{
int x, y, i, j, k, l;
float r1, r2;
// First, narrow it down by Y
r1 = 999999;
j = -1;
for (y=0; y<_THERM_H; y++)
{ i = _THERM_W*y;
r2 = abs(thermspot[i].y - target.y);
if (r2 < r1) { r1 = r2; j = y; }
}
// Then, narrow it down by X
r1 = 999999;
k = -1;
for (x=0; x<_THERM_W; x++)
{ i = _THERM_W*j + x;
r2 = abs(thermspot[i].x - target.x);
if (r2 < r1) { r1 = r2; k = x; }
}
// Now scan
r1 = 999999;
l = k + _THERM_W*j; // default in case we don't find
// return l;
for (y = j-2; y <= j+2; y++)
{ if (y < 0) y = 0;
if (y >= _THERM_H) break;
for (x = k-2; x <= k+2; x++)
{ if (x<0) x = 0;
if (x>=_THERM_W) continue;
i = x + _THERM_W*y;
r2 = distance_between_pix5(target, thermspot[i]);
if (r2 < r1) { r1 = r2; l = i; }
}
}
return l;
}
wghidx blur_thermspot(pixel_5d target)
{ int x, y, i, j, k, l;
wghidx retval;
#if _USE_5D_THERMCMB
int cx0, cx1, cy0, cy1;
int htx, hty;
htx = (wid*therm_sz_x);
hty = (hei*therm_sz_y);
cx0 = target.x - htx;
cx1 = target.x + htx;
cy0 = target.y - hty;
cy1 = target.y + hty;
int q1, q2, q3, q4;
target.x = cx0;
target.y = cy0;
retval.idx[0] = q1 = nearest_thermspot_5d(target);
float r1 = distance_between_pix5(target, thermspot[q1]);
target.x = cx1;
retval.idx[1] = q2 = nearest_thermspot_5d(target);
float r2 = distance_between_pix5(target, thermspot[q2]);
target.y = cy1;
retval.idx[2] = q3 = nearest_thermspot_5d(target);
float r3 = distance_between_pix5(target, thermspot[q3]);
target.y = cy0;
retval.idx[3] = q4 = nearest_thermspot_5d(target);
float r4 = distance_between_pix5(target, thermspot[q4]);
r1 = 1.0/(r1+0.001);
r2 = 1.0/(r2+0.001);
r3 = 1.0/(r3+0.001);
r4 = 1.0/(r4+0.001);
float sumr = r1+r2+r3+r4;
retval.wt[0] = r1 / sumr;
retval.wt[1] = r2 / sumr;
retval.wt[2] = r3 / sumr;
retval.wt[3] = r4 / sumr;
return retval;
#else
float r0, r1, r2, r3;
float tx, ty;
// Apply the thermcam offset
tx = target.x - wid*therm_off_x;
ty = target.y - hei*therm_off_y;
// Apply the rotation
float sr, cr;
sr = sin(-therm_rot_rad);
cr = cos(-therm_rot_rad);
tx -= wid2;
ty -= hei2;
tx = tx * cr - ty * sr;
ty = ty * cr + tx * sr;
tx += wid2;
ty += hei2;
// Divide by the therm pixel spacing
tx /= (wid*therm_sz_x);
ty /= (hei*therm_sz_y);
// Clip the limits
if (tx < 0) tx = 0;
if (tx >= _THERM_W) tx = _THERM_W-1;
if (ty < 0) ty = 0;
if (ty >= _THERM_H) ty = _THERM_H-1;
// Take the floors and set aside the remainders
int txi, tyi;
txi = floor(tx);
tyi = floor(ty);
float dx, dy;
dx = tx - txi;
dy = ty - tyi;
// TODO: Perform a bicubic resample
// For now just linear is fine
retval.idx[0] = txi + _THERM_W*tyi;
retval.idx[1] = (txi < _THERM_W-1)
? retval.idx[0]+1
: retval.idx[0];
;
retval.idx[2] = (tyi < _THERM_H-1)
? retval.idx[0]+_THERM_W
: retval.idx[0];
;
retval.idx[3] = (tyi < _THERM_H-1)
? retval.idx[1]+_THERM_W
: retval.idx[1];
;
// retval.wt[0] = 1; retval.wt[1] = retval.wt[2] = retval.wt[3] = 0; return retval;
r0 = abs(target.r - thermspot[retval.idx[0]].r)
+ abs(target.g - thermspot[retval.idx[0]].g)
+ abs(target.b - thermspot[retval.idx[0]].b)
;
r1 = abs(target.r - thermspot[retval.idx[1]].r)
+ abs(target.g - thermspot[retval.idx[1]].g)
+ abs(target.b - thermspot[retval.idx[1]].b)
;
r2 = abs(target.r - thermspot[retval.idx[2]].r)
+ abs(target.g - thermspot[retval.idx[2]].g)
+ abs(target.b - thermspot[retval.idx[2]].b)
;
r3 = abs(target.r - thermspot[retval.idx[3]].r)
+ abs(target.g - thermspot[retval.idx[3]].g)
+ abs(target.b - thermspot[retval.idx[3]].b)
;
r0 = r0 * rgbf + dx * dy ;
r1 = r1 * rgbf + (1.0 - dx) * dy ;
r2 = r2 * rgbf + dx * (1.0 - dy);
r3 = r3 * rgbf + (1.0 - dx) * (1.0 - dy);
/*
retval.wt[0] = (1.0 - dx) * (1.0 - dy);
retval.wt[1] = dx * (1.0 - dy);
retval.wt[2] = (1.0 - dx) * dy ;
retval.wt[3] = dx * dy ;
*/
r1 = 1.0/(r1+0.001);
r2 = 1.0/(r2+0.001);
r3 = 1.0/(r3+0.001);
r0 = 1.0/(r0+0.001);
float sumr = r1+r2+r3+r0;
retval.wt[0] = r0 / sumr;
retval.wt[1] = r1 / sumr;
retval.wt[2] = r2 / sumr;
retval.wt[3] = r3 / sumr;
return retval;
#endif
}
char* rgb_from_reading(int reading, int tempmin, int tempmax, int sensortmp)
{ char* rgb;
switch (therm_mode)
{ case _THM_FIRE:
rgb = fire_grad(reading, tempmin, tempmax);
break;
case _THM_LAVA:
rgb = lava_grad(reading, tempmin, tempmax);
break;
case _THM_FEVR:
rgb = rgb_from_temp_fever(reading);
break;
case _THM_ROOM:
rgb = centered_grad(reading, 5*4+25, 50*4+25, 25*4+25);
break;
case _THM_AMB:
rgb = centered_grad(reading, tempmin-5, tempmax+5, sensortmp); // thermdat[1023]
break;
case _THM_RAINB:
rgb = rgb_in_minmax(reading, tempmin, tempmax);
break;
case _THM_BLEU:
case _THM_TIV:
rgb = bleu_grad(reading, tempmin, tempmax);
break;
case _THM_HUE:
default:
rgb = rgb_from_temp(reading);
break;
}
return rgb;
}
/* A coloured pixel. */
typedef struct
{
uint8_t red;
uint8_t green;
uint8_t blue;
}
pixel_t;
/* A picture. */
typedef struct
{
pixel_t *pixels;
size_t width;
size_t height;
}
bitmap_t;
/* Given "bitmap", this returns the pixel of bitmap at the point
("x", "y"). */
static pixel_t * pixel_at (bitmap_t * bitmap, int x, int y)
{
return bitmap->pixels + bitmap->width * y + x;
}
/* Write "bitmap" to a PNG file specified by "path"; returns 0 on
success, non-zero on error. */
static int save_png_to_file (bitmap_t *bitmap, const char *path)
{
FILE * fp;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
size_t x, y;
png_byte ** row_pointers = NULL;
/* "status" contains the return value of this function. At first
it is set to a value which means 'failure'. When the routine
has finished its work, it is set to a value which means
'success'. */
int status = -1;
/* The following number is set by trial and error only. I cannot
see where it it is documented in the libpng manual.
*/
int pixel_size = 3;
int depth = 8;
fp = fopen (path, "wb");
if (! fp) {
printf("fopen failed.\n");
goto fopen_failed;
}
// MingW has a version mismatch; hopefully RPi doesn't.
png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
printf("create write struct failed.\n");
goto png_create_write_struct_failed;
}
info_ptr = png_create_info_struct (png_ptr);
if (info_ptr == NULL) {
printf("create info struct failed.\n");
goto png_create_info_struct_failed;
}
/* Set up error handling. */
if (setjmp (png_jmpbuf (png_ptr))) {
printf("jmpbuf failed.\n");
goto png_failure;
}
/* Set image attributes. */
png_set_IHDR (png_ptr,
info_ptr,
bitmap->width,
bitmap->height,
depth,
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
/* Initialize rows of PNG. */
row_pointers = png_malloc (png_ptr, bitmap->height * sizeof (png_byte *));
for (y = 0; y < bitmap->height; y++) {
png_byte *row =
png_malloc (png_ptr, sizeof (uint8_t) * bitmap->width * pixel_size);
row_pointers[y] = row;
for (x = 0; x < bitmap->width; x++) {
pixel_t * pixel = pixel_at (bitmap, x, y);
*row++ = pixel->red;
*row++ = pixel->green;
*row++ = pixel->blue;
}
}
/* Write the image data to "fp". */
png_init_io (png_ptr, fp);
png_set_rows (png_ptr, info_ptr, row_pointers);
png_write_png (png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
/* The routine has successfully written the file, so we set
"status" to a value which indicates success. */
status = 0;
for (y = 0; y < bitmap->height; y++) {
png_free (png_ptr, row_pointers[y]);
}
png_free (png_ptr, row_pointers);
png_failure:
png_create_info_struct_failed:
png_destroy_write_struct (&png_ptr, &info_ptr);
png_create_write_struct_failed:
fclose (fp);
fopen_failed:
return status;
}
/* Given "value" and "max", the maximum value which we expect "value"
to take, this returns an integer between 0 and 255 proportional to
"value" divided by "max". */
static int pix (int value, int max)
{
if (value < 0) {
return 0;
}
return (int) (256.0 *((double) (value)/(double) max));
}
unsigned int type;
unsigned char * rowptr[1]; // pointer to an array
unsigned char * jdata; // data for the image
struct jpeg_decompress_struct info; //for our jpeg info
struct jpeg_error_mgr err; //the error handler
unsigned char* LoadJPEG(char* FileName)
//================================
{
unsigned long x, y;
unsigned int texture_id;
unsigned long data_size; // length of the file
int channels; // 3 =>RGB 4 =>RGBA
jprmx = jpgmx = jpbmx = 0;
jprmn = jpgmn = jpbmn = 255;
FILE* file = fopen(FileName, "rb"); //open the file
info.err = jpeg_std_error(& err);
jpeg_create_decompress(& info); //fills info structure
//if the jpeg file doesn't load
if(!file) {
fprintf(stderr, "Error reading JPEG file %s!", FileName);
return 0;
}
jpeg_stdio_src(&info, file);
jpeg_read_header(&info, TRUE); // read jpeg file header
jpeg_start_decompress(&info); // decompress the file
//set width and height
x = info.output_width;
y = info.output_height;
channels = info.num_components;
//type = GL_RGB;
//if(channels == 4) type = GL_RGBA;
data_size = x * y * 3;
//--------------------------------------------
// read scanlines one at a time & put bytes
// in jdata[] array. Assumes an RGB image
//--------------------------------------------
jdata = (unsigned char *)malloc(data_size);
int maxx = 3* info.output_width;
while (info.output_scanline < info.output_height) // loop
{
// Enable jpeg_read_scanlines() to fill our jdata array
rowptr[0] = (unsigned char *)jdata + // secret to method
maxx * info.output_scanline;
jpeg_read_scanlines(&info, rowptr, 1);
#if _USE_JPEG_MINMAX
for (int x=0; x<maxx; x+=3)
{ if (rowptr[0][x] > jprmx) jprmx = rowptr[0][x];
if (rowptr[0][x] < jprmn) jprmn = rowptr[0][x];
if (rowptr[0][x+1] > jpgmx) jpgmx = rowptr[0][x+1];
if (rowptr[0][x+1] < jpgmn) jpgmn = rowptr[0][x+1];
if (rowptr[0][x+2] > jpbmx) jpbmx = rowptr[0][x+2];
if (rowptr[0][x+2] < jpbmn) jpbmn = rowptr[0][x+2];
}
#endif
}
//---------------------------------------------------
jpeg_finish_decompress(&info); //finish decompressing
return jdata;
}
void load_thalign(void)
{ FILE* pf = fopen("/home/pi/thalign", "r");
if (pf)
{ char buffer[1024];
fgets(buffer, 1024, pf); therm_off_x = atof(buffer);
fgets(buffer, 1024, pf); therm_off_y = atof(buffer);
fgets(buffer, 1024, pf); therm_rot_rad = atof(buffer);
fclose(pf);
}
}
int main(char argc, char** argv)
{
char *in_rgi, *in_therm, *in_eye, *out_comp;
unsigned char *rdat, *gdat, *bdat;
unsigned long rtot, gtot, btot;
unsigned char rmax, gmax, bmax;
int del_inp_f = 0;
int mypid = getpid();
char cmdbuf[256];
// sprintf(cmdbuf, "sudo renice -n +10 -p %i", mypid);
// system(cmdbuf);
// nice doesn't work - neither does cpulimit, and you have to install it
// sprintf(cmdbuf, "cpulimit -p %i -n 25", getpid());
// system(cmdbuf);
in_rgi = in_therm = in_eye = 0;
#ifdef _MLX90640
key_t keyt = ftok("/tmp/shm",73);
int shmidt = shmget(keyt, 2048*sizeof(int), 0666 | IPC_CREAT);
int *thermdatr = (int*)shmat(shmidt, (void*)0, 0);
int *thermdat = malloc(1024*sizeof(int));
for (int i=0; i<768; i++) thermdat[i] = thermdatr[i];
#else
// AMG8833
key_t keyt = ftok("/tmp/shm",71);
int shmidt = shmget(keyt, 80*sizeof(int), 0666 | IPC_CREAT);
int *thermdat = (int*)shmat(shmidt, (void*)0, 0);
#endif
load_thalign();
char* thdat;
int rotcol = 0, rgb = 0, thermfill = 0; //, fire=0;
int cmapping = _rgi;
int bluewhite = 0;
int found = 0;
int imono = 0;
for (int i=1; i<(argc-1); i++)
{ if (!strcmp(argv[i], "-rgi" )) { in_rgi = argv[i+1]; rgb = 0; found++; }
if (!strcmp(argv[i], "-rgb" )) { in_rgi = argv[i+1]; rgb = 1; found++; }
if (!strcmp(argv[i], "-therm")) { in_therm = "yes"; found++; }
if (!strcmp(argv[i], "-o" )) { out_comp = argv[i+1]; }
if (!strcmp(argv[i], "-thdat")) { thdat = argv[i+1]; }
if (!strcmp(argv[i], "-r")) { rotcol = 1; cmapping = _irg; }
if (!strcmp(argv[i], "-im")) imono = 1;
if (!strcmp(argv[i], "-tf")) thermfill = 1;
if (!strcmp(argv[i], "-fire")) therm_mode = _THM_FIRE;
if (!strcmp(argv[i], "-fevr")) therm_mode = _THM_FEVR;
if (!strcmp(argv[i], "-room")) therm_mode = _THM_ROOM;
if (!strcmp(argv[i], "-amb")) therm_mode = _THM_AMB;
if (!strcmp(argv[i], "-rain")) therm_mode = _THM_RAINB;
if (!strcmp(argv[i], "-bleu")) therm_mode = _THM_BLEU;
if (!strcmp(argv[i], "-lava")) therm_mode = _THM_LAVA;
if (!strcmp(argv[i], "-tiv")) { therm_mode = _THM_TIV; rotcol = 1; cmapping = _irg; imono = 0; }
if (!strcmp(argv[i], "-dif")) del_inp_f = 1;
if (!strcmp(argv[i], "-rgi")) cmapping = _rgi;
if (!strcmp(argv[i], "-rig")) cmapping = _rig;
if (!strcmp(argv[i], "-gir")) cmapping = _gir;
if (!strcmp(argv[i], "-gri")) cmapping = _gri;
if (!strcmp(argv[i], "-irg")) cmapping = _irg;
if (!strcmp(argv[i], "-igr")) cmapping = _igr;
if (!strcmp(argv[i], "-bw")) { cmapping = _rgi; bluewhite = 1; }
}
// if (therm_mode == _THM_FIRE) printf("fire\n");
if (!out_comp)
{ printf("No output file.\nUsage:\nimgcomb -rgi NoIR_input.jpg -therm -o output.bmp\n");
return 3;
}
/*if (found < 2)
{ printf("Nothing to process.\n");
return 1;
}*/
#if _USE_JPEG_MINMAX
// If for any reason no minmax data, then no correction.
if (jprmx <= jprmn) { jprmx = 255; jprmn = 0; }
if (jpgmx <= jpgmn) { jpgmx = 255; jpgmn = 0; }
if (jpbmx <= jpbmn) { jpbmx = 255; jpbmn = 0; }
float rcorr = 255.0 / (jprmx - jprmn);
float gcorr = 255.0 / (jpgmx - jpgmn);
float bcorr = 255.0 / (jpbmx - jpbmn);
#endif
for (int i=0; i<10000; i++) histogram[i] = 0;
if (in_rgi)
{ LoadJPEG(in_rgi);
wid = info.output_width;
hei = info.output_height;
wid2 = wid/2;
hei2 = hei/2;
rtot = gtot = btot = wid*hei*16;
rmax = gmax = bmax = 0;
int rmin = 255;
int perline = wid*3, pixels = wid*hei;
rdat = (unsigned char *)malloc(pixels);
gdat = (unsigned char *)malloc(pixels);
bdat = (unsigned char *)malloc(pixels);
#if 1
float r,g,b;
unsigned long satttl = 1, lumttl = 1;
for (unsigned int y=0; y<hei; y+=16)
{ int ly = y * perline;
int by = y * wid;
for (unsigned int x=0; x<wid; x+=16)
{ int lx = x * 3;
int bx = by+x;
r = /* rdat[bx] =*/ jdata[ly+x*3 ];
g = /* gdat[bx] =*/ jdata[ly+x*3+1];
b = /* bdat[bx] =*/ jdata[ly+x*3+2];
#if _USE_JPEG_MINMAX
/*r = (r - jprmn) * rcorr;
g = (g - jpgmn) * gcorr;
b = (b - jpbmn) * bcorr;*/
#endif
satttl += fabs(r-g) + fabs(g-b) + fabs(r-b);
lumttl += fmax(r, fmax(g, b));
}
}
float satcorr = 0.5*(float)lumttl / satttl;
if (satcorr > 0.75) satcorr = 0.75;
for (unsigned int y=0; y<hei; y++)
{ int ly = y * perline;
int by = y * wid;
for (unsigned int x=0; x<wid; x++)
{ int lx = x * 3;
int bx = by+x;
r = /* rdat[bx] =*/ jdata[ly+x*3 ];
g = /* gdat[bx] =*/ jdata[ly+x*3+1];
b = /* bdat[bx] =*/ jdata[ly+x*3+2];
#if _USE_JPEG_MINMAX
r = (r - jprmn) * rcorr;
g = (g - jpgmn) * gcorr;
b = (b - jpbmn) * bcorr;
#endif
int rg = 99.9 * (float)r / (r+g );
int yb = 49.9 * (float)(r+g) / (r+g+b);
histogram[rg + 100*yb]++;
if (histogram[rg + 100*yb] > histmx) histmx = histogram[rg + 100*yb];
if (imono)
{ float a = 0.3 * r + 0.3 * g + 0.4 * b;
if (a < b) b = a;
r = g = b;
}
else if (hei < 800) // no enhancement in 1080p for performance reasons.
{
/*r = 255 * pow(0.00390625*r, 1.08);
g = 255 * pow(0.00390625*g, 1.03);
b = 255 * pow(0.00390625*b, 1.33);*/
// r *= 0.95; g *= 0.95; b *= 0.95;
// Correct for IR paleness
if (!rgb)
{ b *= 1.5;
/*if (b > r)*/ r -= 0.8*satcorr*(b - r);
// else r -= 0.4 * b;
/*if (b > g)*/ g -= 0.8*satcorr*(b - g);
// else g -= 0.4 * b;
b *= .67;
}
// Correct purpling at high brightness
if (b > 192)
{ r += (b - 192);
g += 2*(b - 192);
if (r > 255) r = 255;
if (g > 255) g = 255;
}
// RG saturation
float rg = (r+1)/(g+1);
if (rg < 0.5) rg = 0.5;
if (rg > 2 ) rg = 2 ;
float rgsat = (rg > 1) ? 0.63 : 0.77;
rgsat *= satcorr;
// rgsat *= .005*(g-56);
rg = pow(rg, 0.333);
r = rgsat*(rg*r) + (1.0-rgsat)*r;
g = rgsat*(g/rg) + (1.0-rgsat)*g;
// if (r<0) r=0;
// if (g<0) g=0;
/*
// Decrease Y-B saturation
int yb = (0.5*rdat[bx]+0.5*gdat[bx]) - bdat[bx];
rdat[bx] -= 0.25 * yb;
gdat[bx] -= 0.25 * yb;
bdat[bx] += 0.25 * yb;
*/
// Increase G-B saturation
int yb = (0.2*rdat[bx]+0.9*gdat[bx]) - bdat[bx];
rdat[bx] += 0.25 * yb;
gdat[bx] += 0.25 * yb;
bdat[bx] -= 0.33 * yb;
r = 0.5 * r
+ 0.5 * (g < b ? g : b);
if (r < 0) r = 0; if (g < 0) g = 0; if (b < 0) b = 0;
if (r > 255) r = 255; if (g > 255) g = 255; if (b > 255) b = 255;
/*
// Increase green gamma
float gam = .00392 * g;
gam = pow(gam, 0.91);
g = 255.0 * gam;
// Increase red gamma
gam = .00392 * r;
gam = pow(gam, 0.94);
r = 255.0 * gam;
*/
// Don't decrease blue gamma
/*
if (!rgb)
{ gam = .00392 * b;
gam = pow(gam, 1.25);
b = 255.0 * gam;
}*/
}
int plum; // pixel lum, pronounced "plume"
float r_l, g_l, b_l;
if (cmapping != _rgi)
{ // plum = 0.30 * r + 0.56 * g + 0.14 * b;
plum = 0.40 * r + 0.60 * g;
r_l = r - plum;
g_l = g - plum;
b_l = (b - plum)*.75;
if (therm_mode == _THM_TIV && b_l > 0) b_l /= 2;
}
switch (cmapping)
{
case _irg:
rdat[bx] = plum + b_l;
gdat[bx] = plum + r_l;
bdat[bx] = plum + g_l;
break;
case _igr:
rdat[bx] = plum + b_l;
gdat[bx] = plum + g_l;
bdat[bx] = plum + r_l;
break;
case _rig:
/*
r_l *= .00625;
g_l *= .00625;
b_l *= .00625;
if (r_l > 0) r_l = pow(r_l, 0.81);
else r_l = -pow(-r_l, 0.81);
if (g_l > 0) g_l = pow(g_l, 0.67);
else g_l = -pow(-g_l, 0.67);
if (b_l > 0) b_l = pow(b_l, 0.78);
else b_l = -pow(-b_l, 0.78);
r_l *= 160;
g_l *= 160;
b_l *= 160;
*/
r_l *= 1.25;
g_l *= 1.33;
b_l *= 1.5;
r = plum + r_l;
g = plum + b_l;
b = plum + g_l;
if (r < 0) r = 0; if (g < 0) g = 0; if (b < 0) b = 0;
if (r > 255) r = 255; if (g > 255) g = 255; if (b > 255) b = 255;
rdat[bx] = r; //plum + r_l;
gdat[bx] = g; //plum + b_l;
bdat[bx] = b; //plum + g_l;
if (rdat[bx] > gdat[bx] && bdat[bx] > gdat[bx])
{ gdat[bx] += 0.5*(rdat[bx] - gdat[bx]);
gdat[bx] += 0.5*(bdat[bx] - gdat[bx]);
}
break;
case _gir:
rdat[bx] = plum + g_l;
gdat[bx] = plum + b_l;
bdat[bx] = plum + r_l;
break;
case _gri:
if (b_l < g_l)
r_l = 0.5 * r_l + 0.5 * b_l;
if (b_l < 0) g_l -= b_l;
if (g_l > 0)
{ g_l *= 2.5;
}
r = plum + g_l;
g = plum + r_l;
b = plum + b_l;
if (r < 0) r = 0; if (g < 0) g = 0; if (b < 0) b = 0;
if (r > 255) r = 255; if (g > 255) g = 255; if (b > 255) b = 255;
rdat[bx] = r;
gdat[bx] = g;
bdat[bx] = b;
break;
default:
rdat[bx] = r;
gdat[bx] = g;
bdat[bx] = b;
}
rtot += rdat[bx];
gtot += gdat[bx];