-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimproc.h
1352 lines (1187 loc) · 51.8 KB
/
improc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file improc.h
* @author Arnold Meijster (a.meijster@rug.nl), Niels Bugel (n.a.bugel@rug.nl)
* @brief This file contains all the functions you need to load, save, view and manipulate images. There are different
* iamges available. For the full documentation, see the GitHub page at
* https://github.com/BugelNiels/improcc/wiki/Documentation. Most image types have a certain dynamic range. This refers
* to a minimum and maximum value a pixel in the image can have at most. Attempting to store a value that is not in the
* dynamic range of an image is not possible. For now, the framework only support saving/loading of images in the netpbm
* format.
*
* Whereas traditional images have an x domain of [0..width) and a y domain
* of [0..height), the images in this framework have an x domain of [minX..maxX] and a y domain of [minY..maxY]. This
* means that these images can work with negative indices if the image domain allows for it.
* @version 3.2
* @date 2023-08-01
*
* @copyright Copyright (c) 2023 Niels Bugel, Arnold Meijster
*
*/
#ifndef IMPROC_H
#define IMPROC_H
// Distance transform metrics
#define SQEUCLID 0
#define EUCLID 1
#define MANHATTAN 2
#define CHESSBOARD 3
#include <complex.h>
#include <stdio.h>
typedef struct ImageDomain {
int minX, maxX;
int minY, maxY;
} ImageDomain;
typedef struct IntImage {
ImageDomain domain;
int **pixels;
int minRange, maxRange;
} IntImage;
typedef struct RgbImage {
ImageDomain domain;
int **red;
int **green;
int **blue;
int minRange, maxRange;
} RgbImage;
typedef struct ComplexImage {
ImageDomain domain;
double complex **pixels;
} ComplexImage;
typedef struct DoubleImage {
ImageDomain domain;
double **pixels;
double minRange, maxRange;
} DoubleImage;
typedef struct Histogram {
int *frequencies;
int minRange, maxRange;
} Histogram;
/* ----------------------------- Image Initialization ----------------------------- */
/**
* @brief Allocates an empty image in the domain [0...width) x [0..height) with the specified parameters.
*
* @param width The width of the image in pixels.
* @param height The height of the image in pixels.
* @param minValue The minimum possible value this image should be able to contain.
* @param maxValue The maximum possible value this image should be able to contain.
* @return IntImage A newly allocated IntImage. Note that you should free the resulting image when you are done with it.
*/
IntImage allocateIntImage(int width, int height, int minValue, int maxValue);
/**
* @brief Allocates an empty image in the domain [0...width) x [0..height) with an infinite dynamic range.
*
* @param width The width of the image in pixels.
* @param height The height of the image in pixels.
* @return IntImage A newly allocated IntImage. Note that you should free the resulting image when you are done with it.
*/
IntImage allocateDefaultIntImage(int width, int height);
/**
* @brief Allocates an empty image with the domain and dynamic range of the provided image. Does not copy pixel values.
*
* @param image The image whose properties to copy.
* @return IntImage A newly allocated IntImage.
*/
IntImage allocateFromIntImage(IntImage image);
/**
* @brief Creates a copy of the provided image.
*
* @param image The image to copy.
* @return IntImage A copy of the provided image.
*/
IntImage copyIntImage(IntImage image);
/**
* @brief Allocates an empty image in the domain [minX...maxX] x [minY..maxY] with the specified parameters.
*
* @param minX The start of the image domain in the x direction.
* @param maxX The end of the image domain in the x direction.
* @param minY The start of the image domain in the y direction.
* @param maxY The end of the image domain in the y direction.
* @param minValue The minimum possible value this image should be able to contain.
* @param maxValue The maximum possible value this image should be able to contain.
* @return IntImage A newly allocated IntImage. Note that you should free the resulting image when you are done with it.
*/
IntImage allocateIntImageGrid(int minX, int maxX, int minY, int maxY, int minValue, int maxValue);
/**
* @brief Allocates an empty image in the domain [minX...maxX] x [minY..maxY] with the specified parameters.
*
* @param domain Domain the image should have.
* @param minValue The minimum possible value this image should be able to contain.
* @param maxValue The maximum possible value this image should be able to contain.
* @return IntImage A newly allocated IntImage. Note that you should free the resulting image when you are done with it.
*/
IntImage allocateIntImageGridDomain(ImageDomain domain, int minValue, int maxValue);
/**
* @brief Frees the memory used by the provided image.
*
* @param image The image for which to free the memory.
*/
void freeIntImage(IntImage image);
/* ----------------------------- Image Getters ----------------------------- */
/**
* @brief Retrieve the domain information of the provided image.
*
* @param image The image from which to retrieve the domain.
* @return ImageDomain The domain of the image.
*/
ImageDomain getIntImageDomain(IntImage image);
// trivial domain getters/setters
int getMinX(ImageDomain domain);
int getMaxX(ImageDomain domain);
int getMinY(ImageDomain domain);
int getMaxY(ImageDomain domain);
int getWidth(ImageDomain domain);
int getHeight(ImageDomain domain);
void getWidthHeight(ImageDomain domain, int *width, int *height);
/**
* @brief Checks whether the provided (x,y) coordinates are in the provided domain.
*
* @param domain The domain of the image.
* @param x The x coordinate of the pixel to check.
* @param y The y coordinate of the pixel to check.
* @return int 1 if the (x,y) coordinates fall within the domain. 0 otherwise.
*/
int isInDomain(ImageDomain domain, int x, int y);
/**
* @brief Checks whether the provided (x,y) coordinates are in the provided domain without taking into consideration the
* image domain. This means that the x and y should fall within the range [0..width) and [0..height) respectively.
*
* @param domain The domain of the image.
* @param x The x coordinate of the pixel to check.
* @param y The y coordinate of the pixel to check.
* @return int 1 if the (x,y) coordinates fall within the domain. 0 otherwise.
*/
int isInDomainI(ImageDomain domain, int x, int y);
/**
* @brief Quality of life function. Puts the properties of the image domain into the provided arguments.
*
* @param domain The domain of the image.
* @param minX The start of the image domain in the x direction.
* @param maxX The end of the image domain in the x direction.
* @param minY The start of the image domain in the y direction.
* @param maxY The end of the image domain in the y direction.
*/
void getImageDomainValues(ImageDomain domain, int *minX, int *maxX, int *minY, int *maxY);
/**
* @brief Puts the minimum value and maximum value found in the provided image into the minimalValue and maximalValue
* respectively.
*
* @param image The image in which to find the minimum and maximum values.
* @param minimalValue The resulting minimal value will be put here.
* @param maximalValue The resulting maximal value will be put here.
*/
void getMinMax(IntImage image, int *minimalValue, int *maximalValue);
/**
* @brief Retrieve the dynamic range of the provided image.
*
* @param image The image from which to retrieve the dynamic range.
* @param minRange The minimum possible value this image is able to contain will be put here.
* @param maxRange The maximum possible value this image is able to contain will be put here.
*/
void getDynamicRange(IntImage image, int *minRange, int *maxRange);
/**
* @brief Retrieves the pixel value of the image at the provided coordinates. Note that x and y can be negative if the
* image domain allows for this.
*
* @param image The image from which to retrieve the pixel value.
* @param x The x coordinate of the pixel to retrieve.
* @param y The y coordinate of the pixel to retrieve.
* @return int The grey value at (x,y).
*/
int getIntPixel(IntImage image, int x, int y);
/**
* @brief Retrieves the pixel value of the image at the provided coordinates without taking into consideration the image
* domain. This means that the x and y should fall within the range [0..width) and [0..height) respectively.
*
* @param image The image from which to retrieve the pixel value.
* @param x The x coordinate of the pixel to retrieve.
* @param y The y coordinate of the pixel to retrieve.
* @return int The grey value at (x,y).
*/
int getIntPixelI(IntImage image, int x, int y);
/* ----------------------------- Image Setters ----------------------------- */
/**
* @brief Set the grey value of the image at the provided coordinateswithout taking into consideration the image
* domain. This means that the x and y should fall within the range [0..width) and [0..height) respectively.
* Additionally, the grey value should fit in the dynamic range of the image.
*
* @param image The image in which to set the pixel value.
* @param x The x coordinate of the pixel to set.
* @param y The y coordinate of the pixel to set.
* @param greyValue The grey value to put at (x,y).
*/
void setIntPixel(IntImage *image, int x, int y, int greyValue);
/**
* @brief Set the grey value of the image at the provided coordinates. Note that the x and y should fall within the
* range [0..width) and [0..height) respectively.
*
* @param image The image in which to set the pixel value.
* @param x The x coordinate of the pixel to set.
* @param y The y coordinate of the pixel to set.
* @param greyValue The grey value to put at (x,y).
*/
void setIntPixelI(IntImage *image, int x, int y, int greyValue);
/**
* @brief Sets all the pixels in the provided image to the provided grey value. Note that the grey value should fit in
* the dynamic range of the image.
*
* @param image The image in which to set all the pixel values.
* @param greyValue The grey value to put in the image.
*/
void setAllIntPixels(IntImage *image, int greyValue);
/**
* @brief Updates the dynamic range of the image
*
* @param image The image to update the dynamic range of.
* @param newMinRange The new minimum possible value this image should be able to contain.
* @param newMaxRange The new maximum possible value this image should be able to contain.
*/
void setDynamicRange(IntImage *image, int newMinRange, int newMaxRange);
/* ----------------------------- Image Printing + Viewing ----------------------------- */
/**
* @brief Prints all the pixel values in the provided image to stdout. Every row is put on a new line.
*
* @param image The image to print.
*/
void printIntBuffer(IntImage image);
/**
* @brief Prints a LaTeX compatible table representation of the provided image to stdout.
*
* @param image The image to print the LaTeX table of.
*/
void printIntImageLatexTable(IntImage image);
/**
* @brief Prints a LaTeX compatible table representation of the provided image to the provided file stream.
*
* @param out File stream to print the image to.
* @param image The image to print the LaTeX table of.
*/
void printIntLatexTableToFile(FILE *out, IntImage image);
/**
* @brief Opens a window that allows the user to view the image. Note that this uses OpenGL which in turn allocates
* memory that cannot be freed. If you want to check for memory leaks, make sure that you do not run any image displays.
* Only prints a warning if the NOVIEW flag is enabled.
*
* @param image The image to view
* @param windowTitle The title of the window.
*/
void displayIntImage(IntImage image, const char *windowTitle);
/* ----------------------------- Image Loading + Saving ----------------------------- */
/**
* @brief Loads an image from the provided file. Supported extensions: .pbm, .pgm, .ppm.
*
* @param path The path of the image to load.
* @return IntImage An integer image represtation of the provided file.
*/
IntImage loadIntImage(const char *path);
/**
* @brief Saves an image at the provided location. Supported extensions: .pbm, .pgm, .ppm. This will save the netpbm
* files as their binary formats.
*
* @param image The images to save.
* @param path The location to save the image at.
*/
void saveIntImage(IntImage image, const char *path);
/**
* @brief Saves an image at the provided location. The location must be a .pgm file. Pixels values are stored as raw
* bytes in the file.
*
* @param image The images to save.
* @param path The location to save the image at.
*/
void saveIntImagePGMRaw(IntImage image, const char *path);
/**
* @brief Saves an image at the provided location. The location must be a .pgm file. Pixels values are stored as ascii
* (human readable) values in the file.
*
* @param image The images to save.
* @param path The location to save the image at.
*/
void saveIntImagePGMAscii(IntImage image, const char *path);
/**
* @brief Saves an image as a binary image at the provided location. The location must be a .pbm file. Pixels values are
* stored as raw bits in the file.
*
* @param image The images to save.
* @param path The location to save the image at.
*/
void saveIntImagePBMRaw(IntImage image, const char *path);
/**
* @brief Saves an image as a binary image at the provided location. The location must be a .pbm file. Pixels values are
* stored as ascii (human readable) values in the file.
*
* @param image The images to save.
* @param path The location to save the image at.
*/
void saveIntImagePBMAscii(IntImage image, const char *path);
/* ----------------------------- Image Useful stuff ----------------------------- */
/**
* @brief Creates a new image h from two input images f and g where each pixel is defined as h(x,y) = max(f(x,y),
* g(x,y)).
*
* @param imageA First input image
* @param imageB Second input image
* @return IntImage Output image where each pixel is the maximum of the corresponding pixels in the two input images
*/
IntImage maxIntImage(IntImage imageA, IntImage imageB);
/**
* @brief Creates a new image h from two input images f and g where each pixel is defined as h(x,y) = min(f(x,y),
* g(x,y)).
*
* @param imageA First input image.
* @param imageB Second input image.
* @return IntImage Output image where each pixel is the minimum of the corresponding pixels in the two input images
*/
IntImage minIntImage(IntImage imageA, IntImage imageB);
/**
* @brief Creates a new image h from two input images f and g where each pixel is defined as h(x,y) = f(x,y) + g(x,y).
*
* @param imageA First input image.
* @param imageB Second input image.
* @return IntImage Output image where each pixel is the sum of the corresponding pixels in the two input images
*/
IntImage addIntImage(IntImage imageA, IntImage imageB);
/**
* @brief Creates a new image h from two input images f and g where each pixel is defined as h(x,y) = f(x,y) - g(x,y).
*
* @param imageA First input image.
* @param imageB Second input image.
* @return IntImage Output image where each pixel is the subtraction of the corresponding pixels in the two input images
*/
IntImage subtractIntImage(IntImage imageA, IntImage imageB);
/**
* @brief Creates a new image h from two input images f and g where each pixel is defined as h(x,y) = f(x,y) * g(x,y).
*
* @param imageA First input image.
* @param imageB Second input image.
* @return IntImage Output image where each pixel is the multiplication of the corresponding pixels in the two input
* images
*/
IntImage multiplyIntImage(IntImage imageA, IntImage imageB);
/**
* @brief Produces an output image that is the result of applying a lookup table (LUT) to the input image. The LUT
* should have the same size as the dynamic range of the input image.
*
* @param image Input image.
* @param LUT The lookup table. Maps a gray value in [minRange..maxRange] of the image to a new gray value.
* @param LUTSize The size of the lookup table.
* @return IntImage Image produced from applying the LUT to the input iamge.
*/
IntImage applyLutIntImage(IntImage image, int *LUT, int LUTsize);
/* ----------------------------- Image Transformations ----------------------------- */
/**
* @brief Performs a distance transform on the provided image.
* It calculates for each foreground pixel the distance to the nearest background pixel based on some metric.
*
* @param image The image to perform the distance transform on.
* @param metric The metric to use for the distance transform. Should be one of the constants: MANHATTAN, CHESSBOARD,
* EUCLID or SQEUCLID.
* @param foreground Pixels with this pixel value are assumed as foreground pixels in the distance transforms. Pixels
* that have different values are assumed to be background pixels.
* @return IntImage An image containing the distances of each pixel to the corresponding provided pixel value.
*/
IntImage distanceTransform(IntImage image, int metric, int foreground);
/**
* @brief Produces a new, padded image from the provided image. Note that this allocates a new image, which should
* subsequently be freed.
*
* @param image The image to pad.
* @param top Number of pixels to pad at the top.
* @param right Number of pixels to pad to the right.
* @param bottom Number of pixels to pad at the bottom.
* @param left Number of pixels to pad to the left.
* @param padValue Grey value the padded pixels should have.
* @return IntImage The newly padded image.
*/
IntImage padIntImage(IntImage image, int top, int right, int bottom, int left, int padValue);
/**
* @brief Translates the image domain by x, y. Does not affect the underlying pixel values, only the domain of the
* image.
*
* @param image The image to translate.
* @param x Number of pixels to translate in the x direction.
* @param y Number of pixels to translate in the y direction.
*/
void translateIntImage(IntImage *image, int x, int y);
/**
* @brief Horizontally flips an image around the origin.
*
* @param image The image to flip.
*/
void flipIntImageHorizontal(IntImage *image);
/**
* @brief Vertically flips an image around the origin.
*
* @param image The image to flip.
*/
void flipIntImageVertical(IntImage *image);
/**
* @brief Perform a grayscale dilation on the input image. The structuring element (kernel) that will be used will be a
* rectangle of width `kw` and height `kh`.
*
* @param image The image that the dilation or erosion will be applied on.
* @param kw The width of the rectangular structuring element (kernel)
* @param kh The heigth of the rectangular structuring element (kernel)
* @return IntImage The dilated input image
*/
IntImage dilateIntImageRect(IntImage image, int kw, int kh);
/**
* @brief Perform a grayscale erosion on the input image. The structuring element (kernel) that will be used will be a
* rectangle of width `kw` and height `kh`.
*
* @param image The image that the dilation or erosion will be applied on.
* @param kw The width of the rectangular structuring element (kernel)
* @param kh The heigth of the rectangular structuring element (kernel)
* @return IntImage The eroded input image
*/
IntImage erodeIntImageRect(IntImage image, int kw, int kh);
/* ----------------------------- Image Histogram Functions ----------------------------- */
/**
* @brief Creates a histogram from the provided image. Note that this will allocate a lot of memory when the dynamic
* range is large, since each bin of the histogram is a single pixel value.
*
* @param image The image to create the histogram of.
* @return Histogram The histogram of the image.
*/
Histogram createHistogram(IntImage image);
/**
* @brief Creates a histogram for the channels red, green and blue from the provided image. Note that this will allocate
* a lot of memory when the dynamic range is large, since each bin of the histogram is a single pixel value.
*
* @param image The image to create the histogram of.
* @param redHist Histogram of the red channel.
* @param greenHist Histogram of the green channel.
* @param blueHist Histogram of the blue channel.
*/
void createRgbHistograms(RgbImage image, Histogram *redHist, Histogram *greenHist, Histogram *blueHist);
/**
* @brief Creates an empty histogram.
*
* @param minRange Minimum possible pixel value in the histogram.
* @param maxRange Maximum possible pixel value in the histogram.
* @return Histogram Empy histogram with the value 0 for each pixel.
*/
Histogram createEmptyHistogram(int minRange, int maxRange);
/**
* @brief Frees the memory used by the provided histogram.
*
* @param histogram The histogram for which to free the memory.
*/
void freeHistogram(Histogram histogram);
/**
* @brief Retrieves the frequency of the provided pixel value in the histogram.
*
* @param histogram The histogram.
* @param pixelVal The pixelvalue for which to retrieve the frequency.
* @return int Frequency of the pixel value.
*/
int getHistogramFrequency(Histogram histogram, int pixelVal);
/**
* @brief Sets the frequency of the provided pixel value to a certain value.
*
* @param histogram The histogram.
* @param pixelVal The pixel value for which to set the frequency.
* @param freq The frequency the provided pixel value should have.
*/
void setHistogramFrequency(Histogram *histogram, int pixelVal, int freq);
/**
* @brief Increments the freqyency of the provided pixel value by 1.
*
* @param histogram The histogram.
* @param pixelVal The pixel value to increment.
*/
void incrementHistogramFrequency(Histogram *histogram, int pixelVal);
/**
* @brief Prints the histogram pixel value-frequency pairs.
*
* @param histogram The histogram to print.
*/
void printHistogram(Histogram histogram);
/**
* @brief Retrieves the range of values the provided histogram contains.
*
* @param histogram The histogram to retrieve the dynamic range of.
* @param minRange The value of the minimum bin in the histogram will be put here.
* @param maxRange The value of the maximum bin in the histogram will be put here.
*/
void getHistogramRange(Histogram histogram, int *minRange, int *maxRange);
/* ----------------------------- Image Initialization ----------------------------- */
/**
* @brief Allocates an empty image in the domain [0...width) x [0..height) with the specified parameters.
*
* @param width The width of the image in pixels.
* @param height The height of the image in pixels.
* @param minValue The minimum possible value a channel in this image should be able to contain.
* @param maxValue The maximum possible value a channel in this image should be able to contain.
* @return RgbImage A newly allocated RgbImage. Note that you should free the resulting image when you are done with it.
*/
RgbImage allocateRgbImage(int width, int height, int minValue, int maxValue);
/**
* @brief Allocates an empty image in the domain [0...width) x [0..height) with an infinite dynamic range.
*
* @param width The width of the image in pixels.
* @param height The height of the image in pixels.
* @return RgbImage A newly allocated IntImage. Note that you should free the resulting image when you are done with it.
*/
RgbImage allocateDefaultRgbImage(int width, int height);
/**
* @brief Allocates an empty image with the domain and dynamic range of the provided image. Does not copy pixel values.
*
* @param image The image whose properties to copy.
* @return RgbImage A newly allocated RgbImage.
*/
RgbImage allocateFromRgbImage(RgbImage image);
/**
* @brief Creates a copy of the provided image.
*
* @param image The image to copy.
* @return RgbImage A copy of the provided image.
*/
RgbImage copyRgbImage(RgbImage image);
/**
* @brief Allocates an empty image in the domain [minX...maxX] x [minY..maxY] with the specified parameters.
*
* @param minX The start of the image domain in the x direction.
* @param maxX The end of the image domain in the x direction.
* @param minY The start of the image domain in the y direction.
* @param maxY The end of the image domain in the y direction.
* @param minValue The minimum possible value this image should be able to contain.
* @param maxValue The maximum possible value this image should be able to contain.
* @return RgbImage A newly allocated RgbImage. Note that you should free the resulting image when you are done with it.
*/
RgbImage allocateRgbImageGrid(int minX, int maxX, int minY, int maxY, int minValue, int maxValue);
/**
* @brief Allocates an empty image in the domain [minX...maxX] x [minY..maxY] with the specified parameters.
*
* @param domain The domain the image should have.
* @param minValue The minimum possible value this image should be able to contain.
* @param maxValue The maximum possible value this image should be able to contain.
* @return RgbImage A newly allocated RgbImage. Note that you should free the resulting image when you are done with it.
*/
RgbImage allocateRgbImageGridDomain(ImageDomain domain, int minValue, int maxValue);
/**
* @brief Frees the memory used by the provided image.
*
* @param image The image for which to free the memory.
*/
void freeRgbImage(RgbImage image);
/* ----------------------------- Image Getters ----------------------------- */
/**
* @brief Retrieve the domain information of the provided image.
*
* @param image The image from which to retrieve the domain.
* @return ImageDomain The domain of the image.
*/
ImageDomain getRgbImageDomain(RgbImage image);
/**
* @brief Retrieve the dynamic range of the provided image.
*
* @param image The image from which to retrieve the dynamic range.
* @param minRange The minimum possible value a channel in this image is able to contain will be put here.
* @param maxRange The maximum possible value a channel in this image is able to contain will be put here.
*/
void getRgbDynamicRange(RgbImage image, int *minRange, int *maxRange);
/**
* @brief Retrieves the rgb values of the image at the provided coordinates. Note that x and y can be negative if the
* image domain allows for this.
*
* @param image The image from which to retrieve the rgb value.
* @param x The x coordinate of the pixel to retrieve.
* @param y The y coordinate of the pixel to retrieve.
* @param r The red channel value at (x,y) will be put here.
* @param g The green channel value at (x,y) will be put here.
* @param b The blue channel value at (x,y) will be put here.
*/
void getRgbPixel(RgbImage image, int x, int y, int *r, int *g, int *b);
/**
* @brief Retrieves the rgb values of the image at the provided coordinates without taking into consideration the image
* domain. This means that the x and y should fall within the range [0..width) and [0..height) respectively.
*
* @param image The image from which to retrieve the rgb value.
* @param x The x coordinate of the pixel to retrieve.
* @param y The y coordinate of the pixel to retrieve.
* @param r The red channel value at (x,y) will be put here.
* @param g The green channel value at (x,y) will be put here.
* @param b The blue channel value at (x,y) will be put here.
*/
void getRgbPixelI(RgbImage image, int x, int y, int *r, int *g, int *b);
/* ----------------------------- Image Setters ----------------------------- */
/**
* @brief Set the rgb value of the image at the provided coordinates. Note that x and y can be negative if the
* image domain allows for this. Additionally, the rgb value should fit in the dynamic range of the image.
*
* @param image The image from which to retrieve the rgb value.
* @param x The x coordinate of the pixel to retrieve.
* @param y The y coordinate of the pixel to retrieve.
* @param r The red channel value at (x,y).
* @param g The green channel value at (x,y).
* @param b The blue channel value at (x,y).
*/
void setRgbPixel(RgbImage *image, int x, int y, int r, int g, int b);
/**
* @brief Set the rgb value of the image at the provided coordinates without taking into consideration the image
* domain. This means that the x and y should fall within the range [0..width) and [0..height) respectively.
* Additionally, the rgb value should fit in the dynamic range of the image.
*
* @param image The image from which to retrieve the rgb value.
* @param x The x coordinate of the pixel to retrieve.
* @param y The y coordinate of the pixel to retrieve.
* @param r The red channel value at (x,y).
* @param g The green channel value at (x,y).
* @param b The blue channel value at (x,y).
*/
void setRgbPixelI(RgbImage *image, int x, int y, int r, int g, int b);
/**
* @brief Sets all the pixels in the provided image to the provided rgb value. Note that the rgb values should fit in
* the dynamic range of the image.
*
* @param image The image in which to set all the rgb values.
* @param r The red channel value to put in the image.
* @param g The green channel value to put in the image.
* @param b The blue channel value to put in the image.
*/
void setAllRgbPixels(RgbImage *image, int r, int g, int b);
/* ----------------------------- Image Printing + Viewing ----------------------------- */
/**
* @brief Prints all the pixel values in the provided image to stdout. Every row is put on a new line.
* Each rgb value is printed as (r,g,b).
*
* @param image The image to print.
*/
void printRgbBuffer(RgbImage image);
/**
* @brief Prints a LaTeX compatible table representation of the provided image to stdout.
*
* @param image The image to print the LaTeX table of.
*/
void printRgbImageLatexTable(RgbImage image);
/**
* @brief Prints a LaTeX compatible table representation of the provided image to the provided file stream.
*
* @param out File stream to print the image to.
* @param image The image to print the LaTeX table of.
*/
void printRgbLatexTableToFile(FILE *out, RgbImage image);
/**
* @brief Opens a window that allows the user to view the image. Note that this uses OpenGL which in turn allocates
* memory that cannot be freed. If you want to check for memory leaks, make sure that you do not run any image displays.
* Only prints a warning if the NOVIEW flag is enabled.
*
* @param image The image to view.
* @param windowTitle The title of the window.
*/
void displayRgbImage(RgbImage image, const char *windowTitle);
/* ----------------------------- Image Loading + Saving ----------------------------- */
/**
* @brief Loads an image from the provided file. Supported extensions: .ppm.
*
* @param path The path of the image to load.
* @return RgbImage An integer image represtation of the provided file.
*/
RgbImage loadRgbImage(const char *path);
/**
* @brief Saves an image at the provided location. Supported extensions: .ppm. This will save the netpbm
* file in its binary format.
*
* @param image The images to save.
* @param path The location to save the image at.
*/
void saveRgbImage(RgbImage image, const char *path);
/**
* @brief Saves an image at the provided location. The location must be a .ppm file. Pixels values are stored as raw
* bytes in the file.
*
* @param image The images to save.
* @param path The location to save the image at.
*/
void saveRgbImagePPMRaw(RgbImage image, const char *path);
/**
* @brief Saves an image at the provided location. The location must be a .ppm file. Pixels values are stored as ascii
* (human readable) values in the file.
*
* @param image The images to save.
* @param path The location to save the image at.
*/
void saveRgbImagePPMAscii(RgbImage image, const char *path);
/* ----------------------------- General Operations ----------------------------- */
/**
* @brief Creates a new image h from two input images f and g where each pixel is defined as h(x,y) = max(f(x,y),
* g(x,y)). Performs the operation seperately for each channel.
*
* @param imageA First input image.
* @param imageB Second input image.
* @return RgbImage Output image where each pixel is the maximum of the corresponding pixels in the two input images.
*/
RgbImage maxRgbImage(RgbImage imageA, RgbImage imageB);
/**
* @brief Creates a new image h from two input images f and g where each pixel is defined as h(x,y) = min(f(x,y),
* g(x,y)). Performs the operation seperately for each channel.
*
* @param imageA First input image.
* @param imageB Second input image.
* @return IntImage Output image where each pixel is the minimum of the corresponding pixels in the two input images.
*/
RgbImage minRgbImage(RgbImage imageA, RgbImage imageB);
/**
* @brief Creates a new image h from two input images f and g where each pixel is defined as h(x,y) = f(x,y) + g(x,y).
* Performs the operation seperately for each channel.
*
* @param imageA First input image.
* @param imageB Second input image.
* @return IntImage Output image where each pixel is the sum of the corresponding pixels in the two input images.
*/
RgbImage addRgbImage(RgbImage imageA, RgbImage imageB);
/**
* @brief Creates a new image h from two input images f and g where each pixel is defined as h(x,y) = f(x,y) - g(x,y).
* Performs the operation seperately for each channel.
*
* @param imageA First input image.
* @param imageB Second input image.
* @return IntImage Output image where each pixel is the subtraction of the corresponding pixels in the two input
* images.
*/
RgbImage subtractRgbImage(RgbImage imageA, RgbImage imageB);
/**
* @brief Creates a new image h from two input images f and g where each pixel is defined as h(x,y) = f(x,y) * g(x,y).
* Performs the operation seperately for each channel.
*
* @param imageA First input image.
* @param imageB Second input image.
* @return IntImage Output image where each pixel is the multiplication of the corresponding pixels in the two input.
* images
*/
RgbImage multiplyRgbImage(RgbImage imageA, RgbImage imageB);
/**
* @brief Produces an output image that is the result of applying a lookup table (LUT) to the input image. The LUT
* should have the same size as the dynamic range of the input image. Should have a value for each channel.
*
* @param image Input image.
* @param LUT The lookup table. Maps a gray value in [minRange..maxRange] of the image to a new gray value.
* @param LUTSize The size of the lookup table.
* @return IntImage Image produced from applying the LUT to the input iamge
*/
RgbImage applyLutRgbImage(RgbImage image, int **LUT, int LUTsize);
/* ----------------------------- Image Transformations ----------------------------- */
/**
* @brief Produces a new, padded image from the provided image. Note that this allocates a new image, which should
* subsequently be freed.
*
* @param image The image to pad.
* @param top Number of pixels to pad at the top.
* @param right Number of pixels to pad to the right.
* @param bottom Number of pixels to pad at the bottom.
* @param left Number of pixels to pad to the left.
* @param r Value the red channel of the padded pixels should have.
* @param g Value the green channel of the padded pixels should have.
* @param b Value the blue channel of the padded pixels should have.
* @return RgbImage The newly padded image.
*/
RgbImage padRgbImage(RgbImage image, int top, int right, int bottom, int left, int r, int g, int b);
/**
* @brief Translates the image domain by x, y. Does not affect the underlying pixel values, only the domain of the
* image.
*
* @param image The image to translate.
* @param x Number of pixels to translate in the x direction.
* @param y Number of pixels to translate in the y direction.
*/
void translateRgbImage(RgbImage *image, int x, int y);
/**
* @brief Horizontally flips an image around the origin.
*
* @param image The image to flip.
*/
void flipRgbImageHorizontal(RgbImage *image);
/**
* @brief Vertically flips an image around the origin.
*
* @param image The image to flip.
*/
void flipRgbImageVertical(RgbImage *image);
/* ----------------------------- Complex Image ----------------------------- */
/**
* @brief Allocates an empty complex image in the domain [0...width) x [0..height) with the specified parameters.
*
* @param width The width of the image in pixels.
* @param height The height of the image in pixels.
* @return ComplexImage A newly allocated ComplexImage. Note that you should free the resulting image when you are done
* with it.
*/
ComplexImage allocateComplexImage(int width, int height);
/**
* @brief Allocates an empty image with the domain and dynamic range of the provided image. Does not copy pixel values.
*
* @param image The image whose properties to copy.
* @return ComplexImage A newly allocated DoubleImage.
*/
ComplexImage allocateFromComplexImage(ComplexImage image);
/**
* @brief Creates a copy of the provided image.
*
* @param image The image to copy.
* @return ComplexImage A copy of the provided image
*/
ComplexImage copyComplexImage(ComplexImage image);
/**
* @brief Allocates an empty complex image in the domain [minX...maxX] x [minY..maxY] with the specified parameters.
*
* @param minX The start of the image domain in the x direction.
* @param maxX The end of the image domain in the x direction.
* @param minY The start of the image domain in the y direction.
* @param maxY The end of the image domain in the y direction.
* @return ComplexImage A newly allocated ComplexImage. Note that you should free the resulting image when you are done
* with it.
*/
ComplexImage allocateComplexImageGrid(int minX, int maxX, int minY, int maxY);
/**
* @brief Allocates an empty complex image in the domain [minX...maxX] x [minY..maxY] with the specified parameters.
*
* @param domain Domain the image should have.
* @return ComplexImage A newly allocated ComplexImage. Note that you should free the resulting image when you are done
* with it.
*/
ComplexImage allocateComplexImageGridDomain(ImageDomain domain);
/**
* @brief Frees the memory used by the provided image.
*
* @param image The image for which to free the memory.
*/
void freeComplexImage(ComplexImage image);
/* ----------------------------- Image Getters ----------------------------- */
/**
* @brief Retrieve the domain information of the provided image.
*
* @param image The image from which to retrieve the domain.
* @return ImageDomain The domain of the image.
*/
ImageDomain getComplexImageDomain(ComplexImage image);
/**
* @brief Puts the minimum real value and maximum real value found in the provided image into the minimalValue and
* maximalValue respectively.
*
* @param image The image in which to find the minimum and maximum values.
* @param minimalValue The resulting minimal value will be put here.
* @param maximalValue The resulting maximal value will be put here.
*/
void getComplexMinMax(ComplexImage image, double *minimalValue, double *maximalValue);
/**
* @brief Retrieves the complex value of the image at the provided coordinates. Note that x and y can be negative if the
* image domain allows for this.
*
* @param image The image from which to retrieve the pixel value.
* @param x The x coordinate of the pixel to retrieve.
* @param y The y coordinate of the pixel to retrieve.
* @return double complex The complex value at (x,y).
*/
double complex getComplexPixel(ComplexImage image, int x, int y);
/**
* @brief Retrieves the complex value of the image at the provided coordinates without taking into consideration the
* image domain. This means that the x and y should fall within the range [0..width) and [0..height) respectively.
*
* @param image The image from which to retrieve the pixel value.
* @param x The x coordinate of the pixel to retrieve.
* @param y The y coordinate of the pixel to retrieve.