-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtransforms.cpp
938 lines (799 loc) · 34.4 KB
/
transforms.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
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
/**
* @file openjpeg.c
* @author Andrew Cannon
* @author Slava Kitaeff
* @date December 2011
*
* @brief Command line parser
*
* Copyright 2012. International Centre for Radio Astronomy. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
*
* 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 THE COPYRIGHT OWNER OR CONTRIBUTORS 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.
*/
/* ------------------------------------------------------------------------------------ */
#include <ctype.h>
#include <string.h>
#include <stdio.h> // so we can use `sscanf' for arg parsing.
#include <errno.h>
// Kakadu core includes
#include "kdu_arch.h"
#include "kdu_elementary.h"
#include "kdu_messaging.h"
#include "kdu_params.h"
#include "kdu_compressed.h"
#include "kdu_roi_processing.h"
#include "kdu_sample_processing.h"
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include "fc2jk.h"
/**
* Percentage standard deviation of Gaussian noise to be generated in image. Will be
* 0.0 unless otheriwse specified by the user on the command line. The noise defined by
* this parameter will be added to the raw FITS values before they are transformed into
* JPEG 2000 pixel intensities. This is a percentage of the difference between the greatest
* and least values in the raw FITS data.
*/
double gaussianNoisePctStdDeviation = 0.0;
/**
* Macro to add Gaussian noise to raw floating point data and ensure that it still
* remains within its known minimum and maximum values.
*/
#define ADD_GAUSSIAN_NOISE_TO_RAW_VALUES() {\
if (gaussianNoisePctStdDeviation >= 0.0000001 || gaussianNoisePctStdDeviation <= -0.0000001) {\
rawData[index] += (datamax-datamin) * getPctGaussianNoise();\
\
if (rawData[index] > datamax) {\
rawData[index] = datamax;\
}\
\
if (rawData[index] < datamin) {\
rawData[index] = datamin;\
}\
}\
}
/**
* Macro to add Gaussian noise to integer pixel intensities, calculate the noise added and
* add the square of this value to a cumulative total.
*
* @param max Maximum pixel intensity in the image.
* @param noise_min Minimum noise value. Usually (max+1)/2-1;
* @param noise_max Maximum noise value. Usually -(max+1)/2;
*/
#define ADD_GAUSSIAN_NOISE_TO_INTEGER_VALUES(max,noise_min,noise_max) {\
int oldValue = imageData[ii];\
imageData[ii] += getIntegerGaussianNoise(NULL,NULL,NULL);\
FIT_TO_RANGE(0,max,imageData[ii]);\
int dif = imageData[ii]-oldValue;\
unsigned long long int absDif = (unsigned long long int) abs(dif);\
squareNoiseSum += absDif*absDif;\
if (writeNoiseField) {\
noiseData[ii] = dif;\
FIT_TO_RANGE(noise_min,noise_max,noiseData[ii]);\
}\
}
/**
* Function to print out Gaussian noise benchmark, showing the actual PSNR in image after
* noise has been added and the raw integer data used to calculate that value.
*
* @param max Maximum pixel intensity in the image. Should be an integer.
*/
Print_Noise_Benchmark(unsigned long long int squareNoiseSum,
size_t len,
int max) {
fprintf(stdout,"[Squared Noise Sum] [Pixels] [Maximum Intensity] [PSNR with noise (dB)]\n");
fprintf(stdout,"%llu %zu %d ",squareNoiseSum,len,max);
if (squareNoiseSum > 0) {
fprintf(stdout,"%f\n",10.0*log10(((double)len)*((double)max*max)/((double)squareNoiseSum)));
}else{
fprintf(stdout,"NO-PSNR\n");
}
}
/**
* Macro to truncate data values so that they lie inside a particular range.
*
* @param min Smallest permissible value.
* @param max Largest permissible value.
* @param var Variable to truncate.
*/
#define FIT_TO_RANGE(min,max,var) {\
if (var < min) {\
var = min;\
}\
else if (var > max) {\
var = max;\
}\
}
/**
* Macro to update index enable vertical flipping of a FITS file after
* it has been read.
*/
#define UPDATE_FLIPPING_INDEX() {\
index++;\
dif++;\
\
if (dif >= width) {\
dif = 0;\
index -= 2*width;\
}\
}
#ifdef noise
#define TRANSFORM_END ,writeNoiseField ? noiseField->comps[0].data : NULL,writeNoiseField,printNoiseBenchmark
#else
#define TRANSFORM_END
#endif
/**
* Macro to encode an image losslessly. Requires an integer, 'result' to be defined in the
* same scope. By reading this integer after this macro is run, it may be checked whether
* compression was successful.
*
* @param image opj_image_t image structure that will be written to a
* lossless JPEG 2000 file.
* @param name String to append to output file name.
* @param nameLength Length of the string name.
* @param outFileStub Start of output file name.
*/
#define ENCODE_LOSSLESSLY(image,name,nameLength,outFileStub) {\
OPJ_CODEC_FORMAT losslessCodec = CODEC_JP2;\
opj_cparameters_t lossless;\
opj_set_default_encoder_parameters(&lossless);\
lossless.tcp_mct = 0;\
if (lossless.tcp_numlayers == 0) {\
lossless.tcp_rates[0] = 0;\
lossless.tcp_numlayers++;\
lossless.cp_disto_alloc = 1;\
}\
char losslessFile[stublen + 6 + nameLength];\
\
sprintf(losslessFile,"%s_" name ".jp2",outFileStub);\
\
result = createJPEG2000Image(losslessFile,losslessCodec,&lossless,&image);\
}
/**
* Function that returns a noise value that may be added to a pixel intensity. These values
* are normally (Gaussian) distributed with a mean of 0 and standard deviation specified by
* the user at the command line. If no noise value is specified at the command line, this
* function will always return 0.
*
* To generate (non-zero) noise, this function must be initialised with the maximum pixel
* intensity that will appear in the image (255/65535 respectively for images scaled to occupy
* the full intensity range of 8/16 bit images respectively) and the PSNR (in DB) of the image
* after noise has been added. An optional seed for the random number generator may also be
* specified. Until noiseDB and maxIntensity have been specified, the function will only return
* 0. Until both these values are set, any specified values of noiseDB, maxIntensity or seed will
* be stored (overwriting any previous stored values) with each function call. Once these two
* values are specified, the parameters to the function are ignored and random variates will be
* return when it is called.
*
* If no seed is specified, the random number generator is seeded with the system clock.
*
* @param noiseDB PSNR (in DB) of image after noise has been added (used to initialise function).
* @param maxIntensity Maximum pixel intensity in the image (used to initialise function).
* @param seed Need for random number generator (used to initialise function).
*
* @return Gaussian random variate with mean 0 and standard deviation gaussianNoiseStdDeviation
*/
int getIntegerGaussianNoise(double *noiseDB, int *maxIntensity, unsigned long int *seed) {
// Have the static variables been properly setup to return Gaussian noise?
static bool initialised = false;
// PSNR (in dB) of image after noise has been added.
static double db = 0.0;
static bool noiseSet = false;
// Standard deviation of noise to be generated (as a pixel intensity).
static double noiseDev = 0.0;
// Maximum pixel intensity in image.
static int maxPixelIntensity = 0;
static bool maxIntensitySet = false;
// Random number generator seed.
static unsigned long generatorSeed = 0;
static bool seedSet = false;
// Random number generator.
static gsl_rng *r = NULL;
if (initialised) {
return gsl_ran_gaussian_ziggurat(r,noiseDev);
}
else {
// Set random number generator seed.
if (seed != NULL) {
generatorSeed = *seed;
seedSet = true;
}
// Set noise (in DB) to be added to image.
if (noiseDB != NULL) {
db = *noiseDB;
noiseSet = true;
}
// Set maximum intensity of pixels in image.
if (maxIntensity != NULL) {
maxPixelIntensity = *maxIntensity;
maxIntensitySet = true;
}
// Function becomes initialised to start producing random variates at
// the point when the amount of noise (in DB) to be added and the maximum
// image pixel intensity are set.
if (noiseSet && maxIntensitySet) {
initialised = true;
// Create random number generator.
// Allocate/initialise random number generator.
// Using the Mersenne Twister - this could be changed if necessary.
r = gsl_rng_alloc(gsl_rng_mt19937);
// Check allocation was successful.
if (r == NULL) {
fprintf(stderr,"Unable to allocate memory for random number generator.\n");
exit(EXIT_FAILURE);
}
// See if a particular seed was specified, otherwise seed with system time.
if (seedSet == true) {
gsl_rng_set(r,generatorSeed);
}
else {
gsl_rng_set(r,time(NULL));
}
// Calculate standard deviation for Gaussian noise distribution.
noiseDev = ((double) maxPixelIntensity) * pow(10.0,-0.05 * db);
}
// Return 0 in the case
return 0;
}
}
/**
* Function that returns a floating point value with mean 0. These values are normally
* (Gaussian) distributed with a standard deviation specified by a user command line
* parameter indicating a percentage (of the difference between the minimum and maximum
* raw FITS values). If no such value is specified at the command line, this function
* will always return 0.0.
*
* @return Gaussian random variate with mean 0 and standard deviation gaussianNoisePctStdDeviation
*/
double getPctGaussianNoise() {
// Always return 0.0 if the specified Gaussian noise distribution is close to 0.
if (gaussianNoisePctStdDeviation < 0.0000001 && gaussianNoisePctStdDeviation > -0.0000001) {
return 0.0;
}
else {
// Random number generator.
static gsl_rng *r = NULL;
if (r == NULL) {
// Allocate/initialise random number generator.
// Using the Mersenne Twister - this could be changed if necessary.
r = gsl_rng_alloc(gsl_rng_mt19937);
// Check allocation was successful.
if (r == NULL) {
fprintf(stderr,"Unable to allocate memory for floating point random number generator.\n");
exit(EXIT_FAILURE);
}
// Seed random number generator with system time.
// Maybe the seed should be fixed in the name of getting reproducible results?
// Offset time by 100 to ensure this does not match the random number generator
// in getIntegerGaussianNoise().
gsl_rng_set(r,time(NULL)+100);
}
// Return noise value.
return gsl_ran_gaussian_ziggurat(r,gaussianNoisePctStdDeviation/100.0);
}
}
/**
* Function for transforming a raw array of data from a FITS file (in the form of
* a long long int array) into grayscale image intensities (between 0 and 2^16-1 inclusive).
*
* @param rawData long long int array read from a FITS file using CFITSIO
* @param imageData int array, assumed to be the same length as rawData, to be populated
* with grayscale image intensities.
* @param transform transform to perform on each datum of rawData to get imageData.
* @param len - length of rawData & imageData arrays.
* @param width width of image.
* @param noiseData int array, assumed to be the same length as rawData, to be populated
* with grayscale noise value intensities. Will only be accessed if writeNoiseField is
* set to true. If the definition of noise is removed from f2j.h, this parameter will
* disappear.
* @param writeNoiseField Should noise data be written? If the definition of noise is removed
* from f2j.h, this parameter will disappear.
* @param printNoiseBenchmark Should information on the actual PSNR achieved by adding noise to the image be displayed
* to the user? This parameter will disappear if the definition of noise is removed from f2j.h.
*
* @return 0 if the transform could be performed successfully, 1 otherwise.
*/
int longLongImgTransform(long long int *rawData, int *imageData, transform transform, size_t len, size_t width
, int *noiseData, bool writeNoiseField, bool printNoiseBenchmark) {
fprintf(stderr,"This data type is not currently supported.\n");
return 1;
}
/**
* Function for transforming a raw array of data from a FITS file (in the form of
* an int array) into grayscale image intensities (between 0 and 2^16-1 inclusive).
*
* @param rawData int array read from a FITS file using CFITSIO
* @param imageData int array, assumed to be the same length as rawData, to be populated
* with grayscale image intensities.
* @param transform transform to perform on each datum of rawData to get imageData.
* @param len length of rawData & imageData arrays.
* @param width width of image.
* @param noiseData int array, assumed to be the same length as rawData, to be populated
* with grayscale noise value intensities. Will only be accessed if writeNoiseField is
* set to true. If the definition of noise is removed from f2j.h, this parameter will
* disappear.
* @param writeNoiseField Should noise data be written? If the definition of noise is removed
* from f2j.h, this parameter will disappear.
* @param printNoiseBenchmark Should information on the actual PSNR achieved by adding noise to the image be displayed
* to the user? This parameter will disappear if the definition of noise is removed from f2j.h.
*
* @return 0 if the transform could be performed successfully, 1 otherwise.
*/
int intImgTransform(int *rawData, int *imageData, transform transform, size_t len, size_t width
, int *noiseData, bool writeNoiseField, bool printNoiseBenchmark) {
fprintf(stderr,"This data type is not currently supported.\n");
return 1;
}
/**
* Function for transforming a raw array of data from a FITS file (in the form of
* an unsigned int array) into grayscale image intensities (between 0 and 2^16-1 inclusive).
*
* @param rawData unsigned int array read from a FITS file using CFITSIO
* @param imageData int array, assumed to be the same length as rawData, to be populated
* with grayscale image intensities.
* @param transform transform to perform on each datum of rawData to get imageData.
* @param len length of rawData & imageData arrays.
* @param width width of image.
* @param noiseData int array, assumed to be the same length as rawData, to be populated
* with grayscale noise value intensities. Will only be accessed if writeNoiseField is
* set to true. If the definition of noise is removed from f2j.h, this parameter will
* disappear.
* @param writeNoiseField Should noise data be written? If the definition of noise is removed
* from f2j.h, this parameter will disappear.
* @param printNoiseBenchmark Should information on the actual PSNR achieved by adding noise to the image be displayed
* to the user? This parameter will disappear if the definition of noise is removed from f2j.h.
*
* @return 0 if the transform could be performed successfully, 1 otherwise.
*/
int uIntImgTransform(unsigned int *rawData, int *imageData, transform transform, size_t len, size_t width
, int *noiseData, bool writeNoiseField, bool printNoiseBenchmark) {
fprintf(stderr,"This data type is not currently supported.\n");
return 1;
}
/**
* Function for transforming a raw array of data from a FITS file (in the form of
* a short array) into grayscale image intensities (between 0 and 2^16-1 inclusive).
*
* Very basic parameter checking is performed, but the responsibility for checking
* parameters are valid and meaningful is largely left to the calling function.
*
* @param rawData short array read from a FITS file using CFITSIO
* @param imageData int array, assumed to be the same length as rawData, to be populated
* with grayscale image intensities.
* @param transform transform to perform on each datum of rawData to get imageData.
* @param len length of rawData & imageData arrays.
* @param width width of image.
* @param noiseData int array, assumed to be the same length as rawData, to be populated
* with grayscale noise value intensities. Will only be accessed if writeNoiseField is
* set to true. If the definition of noise is removed from f2j.h, this parameter will
* disappear.
* @param writeNoiseField Should noise data be written? If the definition of noise is removed
* from f2j.h, this parameter will disappear.
* @param printNoiseBenchmark Should information on the actual PSNR achieved by adding noise to the image be displayed
* to the user? This parameter will disappear if the definition of noise is removed from f2j.h.
*
* @return 0 if the transform could be performed successfully, 1 otherwise.
*/
int shortImgTransform(short *rawData, int *imageData, transform transform, size_t len, size_t width
, int *noiseData, bool writeNoiseField, bool printNoiseBenchmark) {
if (rawData == NULL || imageData == NULL || len < 1) {
fprintf(stderr,"Data arrays to shortImgTransform cannot be null or empty.\n");
return 1;
}
// Loop variables
size_t ii;
// Sum of the squared error introduced to image.
unsigned long long int squareNoiseSum = 0;
if (transform == RAW) {
// Variables that enable us to flip the image vertically as we read it in.
size_t index = len-width;
size_t dif = 0;
// Shift scales (from signed to unsigned) then do a 1-1 mapping.
for (ii=0; ii<len; ii++) {
imageData[ii] = (int) rawData[index] + 32768;
ADD_GAUSSIAN_NOISE_TO_INTEGER_VALUES(65535,-32768,32767);
UPDATE_FLIPPING_INDEX();
}
if (printNoiseBenchmark) Print_Noise_Benchmark(squareNoiseSum, len, 65535);
return 0;
}
else if (transform == NEGATIVE_RAW) {
// Variables that enable us to flip the image vertically as we read it in.
size_t index = len-width;
size_t dif = 0;
// As for linear, but subtract from 65535
for (ii=0; ii<len; ii++) {
imageData[ii] = 32767 - (int) rawData[index];
ADD_GAUSSIAN_NOISE_TO_INTEGER_VALUES(65535,-32768,32767);
UPDATE_FLIPPING_INDEX();
}
if (printNoiseBenchmark) Print_Noise_Benchmark(squareNoiseSum, len, 65535);
return 0;
}
fprintf(stderr,"This scaling is not currently supported for this data type.\n");
return 1;
}
/**
* Function for transforming a raw array of data from a FITS file (in the form of
* an unsigned short array) into grayscale image intensities (between 0 and 2^16-1 inclusive).
*
* Very basic parameter checking is performed, but the responsibility for checking
* parameters are valid and meaningful is largely left to the calling function.
*
* @param rawData unsigned short array read from a FITS file using CFITSIO
* @param imageData int array, assumed to be the same length as rawData, to be populated
* with grayscale image intensities.
* @param transform transform to perform on each datum of rawData to get imageData.
* @param len length of rawData & imageData arrays.
* @param width width of image.
* @param noiseData int array, assumed to be the same length as rawData, to be populated
* with grayscale noise value intensities. Will only be accessed if writeNoiseField is
* set to true. If the definition of noise is removed from f2j.h, this parameter will
* disappear.
* @param writeNoiseField Should noise data be written? If the definition of noise is removed
* from f2j.h, this parameter will disappear.
* @param printNoiseBenchmark Should information on the actual PSNR achieved by adding noise to the image be displayed
* to the user? This parameter will disappear if the definition of noise is removed from f2j.h.
*
* @return 0 if the transform could be performed successfully, 1 otherwise.
*/
int uShortImgTransform(unsigned short *rawData,
int *imageData,
transform transform,
size_t len,
size_t width,
int *noiseData,
bool writeNoiseField,
bool printNoiseBenchmark) {
if (rawData == NULL || imageData == NULL || len < 1) {
fprintf(stderr,"Data arrays to uShortImgTransform cannot be null or empty.\n");
return 1;
}
// Loop variables
size_t ii;
// Sum of the squared error introduced to image.
unsigned long long int squareNoiseSum = 0;
if (transform == RAW) {
// Variables that enable us to flip the image vertically as we read it in.
size_t index = len-width;
size_t dif = 0;
// Simple raw copying.
for (ii=0; ii<len; ii++) {
imageData[ii] = (int) rawData[index];
ADD_GAUSSIAN_NOISE_TO_INTEGER_VALUES(65535,-32768,32767);
UPDATE_FLIPPING_INDEX();
}
if (printNoiseBenchmark) Print_Noise_Benchmark(squareNoiseSum, len, 65535);
return 0;
}
else if (transform == NEGATIVE_RAW) {
// Variables that enable us to flip the image vertically as we read it in.
size_t index = len-width;
size_t dif = 0;
// As for linear, but subtract from 65535
for (ii=0; ii<len; ii++) {
imageData[ii] = 65535 - (int) rawData[index];
ADD_GAUSSIAN_NOISE_TO_INTEGER_VALUES(65535,-32768,32767);
UPDATE_FLIPPING_INDEX();
}
if (printNoiseBenchmark) Print_Noise_Benchmark(squareNoiseSum, len, 65535);
return 0;
}
fprintf(stderr,"This transform is not currently supported for this data type.\n");
return 1;
}
/**
* Function for transforming a raw array of data from a FITS file (in the form of
* an unsigned char array) into grayscale image intensities (between 0 and 255 inclusive).
*
* Very basic parameter checking is performed, but the responsibility for checking
* parameters are valid and meaningful is largely left to the calling function.
*
* @param rawData unsigned char array read from a FITS file using CFITSIO
* @param imageData int array, assumed to be the same length as rawData, to be populated
* with grayscale image intensities.
* @param transform transform to perform on each datum of rawData to get imageData.
* @param len length of rawData & imageData arrays.
* @param width width of image.
* @param noiseData int array, assumed to be the same length as rawData, to be populated
* with grayscale noise value intensities. Will only be accessed if writeNoiseField is
* set to true. If the definition of noise is removed from f2j.h, this parameter will
* disappear.
* @param writeNoiseField Should noise data be written? If the definition of noise is removed
* from f2j.h, this parameter will disappear.
* @param printNoiseBenchmark Should information on the actual PSNR achieved by adding noise to the image be displayed
* to the user? This parameter will disappear if the definition of noise is removed from f2j.h.
*
* @return 0 if the transform could be performed successfully, 1 otherwise.
*/
int byteImgTransform(unsigned char *rawData,
int *imageData,
transform transform,
size_t len,
size_t width,
int *noiseData,
bool writeNoiseField,
bool printNoiseBenchmark) {
if (rawData == NULL || imageData == NULL || len < 1) {
fprintf(stderr,"Data arrays to byteImgTransform cannot be null or empty.\n");
return 1;
}
// Loop variables
size_t ii;
// Sum of the squared error introduced to image.
unsigned long long int squareNoiseSum = 0;
if (transform == RAW) {
// Variables that enable us to flip the image vertically as we read it in.
size_t index = len-width;
size_t dif = 0;
// Simple raw transform
for (ii=0; ii<len; ii++) {
imageData[ii] = (int) rawData[index];
ADD_GAUSSIAN_NOISE_TO_INTEGER_VALUES(255,-128,127);
UPDATE_FLIPPING_INDEX();
}
if (printNoiseBenchmark) Print_Noise_Benchmark(squareNoiseSum, len, 255);
return 0;
}
else if (transform == NEGATIVE_RAW) {
// Variables that enable us to flip the image vertically as we read it in.
size_t index = len-width;
size_t dif = 0;
// Invert raw transform
for (ii=0; ii<len; ii++) {
imageData[ii] = 255 - (int) rawData[index];
ADD_GAUSSIAN_NOISE_TO_INTEGER_VALUES(255,-128,127);
UPDATE_FLIPPING_INDEX();
}
if (printNoiseBenchmark) Print_Noise_Benchmark(squareNoiseSum, len, 255);
return 0;
}
fprintf(stderr,"This transform is not currently supported for this data type.\n");
return 1;
}
/**
* Function for transforming a raw array of data from a FITS file (in the form of
* a char array) into grayscale image intensities (between 0 and 2^16-1 inclusive).
*
* Very basic parameter checking is performed, but the responsibility for checking
* parameters are valid and meaningful is largely left to the calling function.
*
* @param rawData signed char array read from a FITS file using CFITSIO
* @param imageData int array, assumed to be the same length as rawData, to be populated
* with grayscale image intensities.
* @param transform transform to perform on each datum of rawData to get imageData.
* @param len length of rawData & imageData arrays.
* @param width width of image.
* @param noiseData int array, assumed to be the same length as rawData, to be populated
* with grayscale noise value intensities. Will only be accessed if writeNoiseField is
* set to true. If the definition of noise is removed from f2j.h, this parameter will
* disappear.
* @param writeNoiseField Should noise data be written? If the definition of noise is removed
* from f2j.h, this parameter will disappear.
* @param printNoiseBenchmark Should information on the actual PSNR achieved by adding noise to the image be displayed
* to the user? This parameter will disappear if the definition of noise is removed from f2j.h.
*
* @return 0 if the transform could be performed successfully, 1 otherwise.
*/
int sByteImgTransform(signed char *rawData, int *imageData, transform transform, size_t len, size_t width
, int *noiseData, bool writeNoiseField, bool printNoiseBenchmark) {
if (rawData == NULL || imageData == NULL || len < 1) {
fprintf(stderr,"Data arrays to sByteImgTransform cannot be null or empty.\n");
return 1;
}
// Loop variables
size_t ii;
// Sum of the squared error introduced to image.
unsigned long long int squareNoiseSum = 0;
if (transform == RAW) {
// Variables that enable us to flip the image vertically as we read it in.
size_t index = len-width;
size_t dif = 0;
// Take raw data, shift it to be unsigned.
for (ii=0; ii<len; ii++) {
imageData[ii] = 128 + (int) rawData[index];
ADD_GAUSSIAN_NOISE_TO_INTEGER_VALUES(255,-128,127);
UPDATE_FLIPPING_INDEX();
}
if (printNoiseBenchmark) Print_Noise_Benchmark(squareNoiseSum, len, 255);
return 0;
}
else if (transform == NEGATIVE_RAW) {
// Variables that enable us to flip the image vertically as we read it in.
size_t index = len-width;
size_t dif = 0;
// Invert raw transform.
for (ii=0; ii<len; ii++) {
imageData[ii] = 127 + (int) rawData[index];
ADD_GAUSSIAN_NOISE_TO_INTEGER_VALUES(255,-128,127);
UPDATE_FLIPPING_INDEX();
}
if (printNoiseBenchmark) Print_Noise_Benchmark(squareNoiseSum, len, 255);
return 0;
}
fprintf(stderr,"This transform is not currently supported for this data type.\n");
return 1;
}
/**
* Function for transforming a raw array of data from a FITS file (in the form of
* a double array) into grayscale image intensities (between 0 and 2^16-1 inclusive).
*
* Very basic parameter checking is performed, but the responsibility for checking that
* parameters are valid and meaningful is largely left to the calling function.
*
* @param rawData double array read from a FITS file using CFITSIO
* @param imageData int array, assumed to be the same length as rawData, to be populated
* with grayscale image intensities.
* @param transform transform to perform on each datum of rawData to get imageData.
* @param len length of rawData & imageData arrays.
* @param datamin minimum value in rawData.
* @param datamax maximum value in rawData.
* @param width width of image.
* @param noiseData int array, assumed to be the same length as rawData, to be populated
* with grayscale noise value intensities. Will only be accessed if writeNoiseField is
* set to true. If the definition of noise is removed from f2j.h, this parameter will
* disappear.
* @param writeNoiseField Should noise data be written? If the definition of noise is removed
* from f2j.h, this parameter will disappear.
* @param printNoiseBenchmark Should information on the actual PSNR achieved by adding noise to the image be displayed
* to the user? This parameter will disappear if the definition of noise is removed from f2j.h.
*
* @return 0 if the transform could be performed successfully, 1 otherwise.
*/
int floatDoubleTransform(double *rawData, int *imageData, transform transform, size_t len, double datamin, double datamax, size_t width
, int *noiseData, bool writeNoiseField, bool printNoiseBenchmark) {
if (rawData == NULL || imageData == NULL || len < 1) {
fprintf(stderr,"Data arrays in floatDoubleTransform cannot be null or empty.\n");
return 1;
}
// Loop variables
size_t ii;
// Sum of the squared error introduced to image.
unsigned long long int squareNoiseSum = 0;
if (transform == LOG || transform == NEGATIVE_LOG) {
double absMin = datamin;
double zero = 0.0;
if (datamin < 0.0) {
absMin = -absMin;
zero = 2*absMin;
}
else if (datamin <= 0.0) {
absMin = 0.000001;
zero = absMin;
}
double scale = 65535.0/log((datamax+zero)/absMin);
// Variables that enable us to flip the image vertically as we read it in.
size_t index = len-width;
size_t dif = 0;
for (ii=0; ii<len; ii++) {
ADD_GAUSSIAN_NOISE_TO_RAW_VALUES();
// Read the flipped image pixel.
imageData[ii] = (int) (scale * log( (rawData[index] + zero) / absMin) );
// Shouldn't get values outside this range, but just in case.
FIT_TO_RANGE(0,65535,imageData[ii]);
ADD_GAUSSIAN_NOISE_TO_INTEGER_VALUES(65535,-32768,32767);
if (transform == NEGATIVE_LOG) {
imageData[ii] = 65535 - imageData[ii];
}
UPDATE_FLIPPING_INDEX();
}
// Print (or don't print) noise simulation benchmarks.
if (printNoiseBenchmark) Print_Noise_Benchmark(squareNoiseSum, len, 65535);
return 0;
}
else if (transform == LINEAR || transform == NEGATIVE_LINEAR) {
double absMin = datamin;
double zero = 0.0;
if (datamin < 0.0) {
absMin = -absMin;
zero = absMin;
}
double scale = 65535.0/(datamax+zero);
// Variables that enable us to flip the image vertically as we read it in.
size_t index = len-width;
size_t dif = 0;
for (ii=0; ii<len; ii++) {
ADD_GAUSSIAN_NOISE_TO_RAW_VALUES();
imageData[ii] = (int) (rawData[index] * scale);
FIT_TO_RANGE(0,65535,imageData[ii]);
ADD_GAUSSIAN_NOISE_TO_INTEGER_VALUES(65535,-32768,32767);
if (transform == NEGATIVE_LINEAR) {
imageData[ii] = 65535 - imageData[ii];
}
UPDATE_FLIPPING_INDEX();
}
if (printNoiseBenchmark) Print_Noise_Benchmark(squareNoiseSum, len, 65535);
return 0;
}
else if (transform == SQRT || transform == NEGATIVE_SQRT) {
// Scale factor.
double scale = 0.0;
if (datamin != datamax) {
scale = 65535.0/sqrt(datamax-datamin);
}
// Variables that enable us to flip the image vertically as we read it in.
size_t index = len-width;
size_t dif = 0;
for (ii=0; ii<len; ii++) {
ADD_GAUSSIAN_NOISE_TO_RAW_VALUES();
imageData[ii] = (int) (scale * sqrt(rawData[index]-datamin));
FIT_TO_RANGE(0,65535,imageData[ii]);
ADD_GAUSSIAN_NOISE_TO_INTEGER_VALUES(65535,-32768,32767);
if (transform == NEGATIVE_SQRT) {
imageData[ii] = 65535 - imageData[ii];
}
UPDATE_FLIPPING_INDEX();
}
if (printNoiseBenchmark) Print_Noise_Benchmark(squareNoiseSum, len, 65535);
return 0;
}
else if (transform == SQUARED || transform == NEGATIVE_SQUARED) {
// Scale factor.
double scale = 0.0;
if (datamin != datamax) {
scale = 65535.0/( (datamax-datamin)*(datamax-datamin) );
}
// Variables that enable us to flip the image vertically as we read it in.
size_t index = len-width;
size_t dif = 0;
for (ii=0; ii<len; ii++) {
ADD_GAUSSIAN_NOISE_TO_RAW_VALUES();
imageData[ii] = (int) (scale * (rawData[index]-datamin) * (rawData[index]-datamin));
FIT_TO_RANGE(0,65535,imageData[ii]);
ADD_GAUSSIAN_NOISE_TO_INTEGER_VALUES(65535,-32768,32767);
if (transform == NEGATIVE_SQUARED) {
imageData[ii] = 65535 - imageData[ii];
}
UPDATE_FLIPPING_INDEX();
}
if (printNoiseBenchmark) Print_Noise_Benchmark(squareNoiseSum, len, 65535);
return 0;
}
else if (transform == POWER || transform == NEGATIVE_POWER) {
// Scale factor.
double scale = 0.0;
if (datamin != datamax) {
scale = 65535.0/( exp(datamax) - exp(datamin) );
}
// Offset.
double offset = 0.0;
if (datamin != datamax) {
offset = 65535.0 * exp(datamin) / ( exp(datamin) - exp(datamax) );
}
// Variables that enable us to flip the image vertically as we read it in.
size_t index = len-width;
size_t dif = 0;
for (ii=0; ii<len; ii++) {
ADD_GAUSSIAN_NOISE_TO_RAW_VALUES();
imageData[ii] = (int) (scale * exp(rawData[index]) + offset);
FIT_TO_RANGE(0,65535,imageData[ii]);
ADD_GAUSSIAN_NOISE_TO_INTEGER_VALUES(65535,-32768,32767);
if (transform == NEGATIVE_POWER) {
imageData[ii] = 65535 - imageData[ii];
}
UPDATE_FLIPPING_INDEX();
}
if (printNoiseBenchmark) Print_Noise_Benchmark(squareNoiseSum, len, 65535);
return 0;
}
fprintf(stderr,"This transform is not currently supported for this data type.\n");
return 1;
}