-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomp.cpp
1268 lines (1081 loc) · 44.8 KB
/
comp.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
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
/*
imgalt - Image Alignment Tool
Author: GreatAttractor
version 0.5
2014/05/22
This code can be freely distributed and used for any purpose.
File description:
Utility functions implementation.
*/
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdexcept>
#include <fstream>
#include <cctype>
#include <algorithm>
#include <boost/format.hpp>
#include "comp.h"
using namespace boost;
int GetBytesPerPixel(PixelFormat_t pixFmt)
{
switch (pixFmt)
{
case PIX_PAL8:
case PIX_MONO8:
return 1;
case PIX_MONO16: return 2;
case PIX_RGB24: return 3;
case PIX_RGB48: return 3*2;
case PIX_MONO32F: return sizeof(float);
default: return -1;
}
}
/// Returns the smallest power of 2 which is > n
unsigned GetClosestGPowerOf2(unsigned n)
{
int msb = 0;
while (n != 0)
{
n >>= 1;
msb++;
}
return ((unsigned)1 << msb);
}
/// Returns the greatest power of 2 which is <= n
unsigned GetClosestLEPowerOf2(unsigned n)
{
int msb = 0;
while (n != 0)
{
n >>= 1;
msb++;
}
return ((unsigned)1 << (msb-1));
}
/// Returns 0 for x=0, 1 for x=1
inline float CosineWindow(float x)
{
return 0.5f*(1.0f - cosf(3.1415926535f*x));
}
/// Returns 0 for x=0, 1 for x=1
inline float BlackmanWindow(float x)
{
const float A0 = 7938.0f/18608,
A1 = 9240.0f/18608,
A2 = 1430.0f/18608;
return A0 - A1*cosf(3.1415926535f*x) + A2*cosf(2*3.1415926535f*x);
}
#define SQR(x) ((x)*(x))
/// Calculates window function and writes its values into 'buf' (a square array)
void CalcWindowFunction(
int wndSize, ///< Window size
float buf[] ///< Destination buffer, wndSize*wndSize elements
)
{
// The window function is rotationally symmetrical, so calculate it only in a quarter of 'buf'
#pragma omp parallel for
for (int y = 0; y < wndSize/2; y++)
for (int x = 0; x < wndSize/2; x++)
{
float value = 0.0f;
float dist = sqrtf(SQR(wndSize/2 - x) + SQR(wndSize/2 - y));
if (dist < wndSize/2)
value = BlackmanWindow(1.0f-dist/(wndSize/2));
// upper left
buf[x + y*wndSize] = value;
// upper right
buf[wndSize-1-x + y*wndSize] = value;
// lower right
buf[wndSize-1-x + (wndSize-1-y)*wndSize] = value;
// lower left
buf[x + (wndSize-1-y)*wndSize] = value;
}
}
/// Multiplies input image by window function; input and output images may be the same
void ApplyWindowFunction(float img[], ///< Input image, size*size elements; may be the same pointer as 'dest'
float windowFunc[], ///< Window function values, size*size elements
float dest[], ///< Output buffer, size*size elements; may be the same pointer as 'img'
int size ///< Number of rows and columns in each array
)
{
#pragma omp parallel for
for (int y = 0; y < size; y++)
for (int x = 0; x < size; x++)
dest[x + y*size] = img[x + y*size] * windowFunc[x + y*size];
}
/// Resizes and translates image (or its fragment) by cropping and/or padding (with zeros) to the destination size and offset (there is no scaling)
void ResizeAndTranslate(
void *input, ///< Input buffer
PixelFormat_t pixFmt, ///< Format of data in 'input' and 'output'
int srcWidth, ///< Width of input image
int srcHeight, ///< Height of input image
int srcXmin, ///< X min of input data in input image
int srcYmin, ///< Y min of input data in input image
int srcXmax, ///< X max of input data in input image
int srcYmax, ///< Y max of input data in input image
void *output, ///< Output buffer
int destWidth, ///< Width of output image
int destHeight, ///< Height of output image
int xOfs, ///< X offset of input data in output buffer
int yOfs ///< Y offset of input data in output buffer
)
{
int bytesPP = GetBytesPerPixel(pixFmt);
memset(output, 0, destWidth*destHeight*bytesPP); // Works also if 'dest' points to an array of floats; 32 zero bits represent a floating-point 0.0f
// start and end (inclusive) coordinates to fill in the output buffer
unsigned Xstart = (xOfs < 0) ? 0 : xOfs;
unsigned Ystart = (yOfs < 0) ? 0 : yOfs;
unsigned Xend = std::max(0, std::min((int)xOfs + srcXmax, destWidth-1));
unsigned Yend = std::max(0, std::min((int)yOfs + srcYmax, destHeight-1));
for (int y = Ystart; y <= Yend; y++)
{
memcpy((uint8_t *)output + (Xstart + y*destWidth)*bytesPP,
(uint8_t *)input + (Xstart-xOfs+srcXmin + (y-yOfs + srcYmin)*srcWidth)*bytesPP,
(Xend - Xstart + 1)*bytesPP);
}
}
/// Blurs 'src' image using a 5x5 Gaussian kernel and writes the result to 'dest'; 2 pixel-wide borders are not blurred
void BlurImage(
uint8_t *src, ///< Source image (8-bit luminance)
int width, ///< Image width
int height, ///< Image height
uint8_t *dest ///< Destination image (same dimensions as 'src')
)
{
const int KERNEL_SIZE = 5;
const int kernel[KERNEL_SIZE][KERNEL_SIZE] =
{
{ 2, 4, 5, 4, 2 },
{ 4, 9, 12, 9, 4 },
{ 5, 12, 15, 12, 5 },
{ 4, 9, 12, 9, 4 },
{ 2, 4, 5, 4, 2 }
};
for (int y = KERNEL_SIZE/2; y <= height - KERNEL_SIZE/2 - 1; y++)
for (int x = KERNEL_SIZE/2; x <= width - KERNEL_SIZE/2 - 1; x++)
{
int sum = 0;
for (int yofs = -KERNEL_SIZE/2; yofs <= KERNEL_SIZE/2; yofs++)
for (int xofs = -KERNEL_SIZE/2; xofs <= KERNEL_SIZE/2; xofs++)
sum += (int)src[(x + xofs) + (y + yofs) * width] * kernel[xofs + KERNEL_SIZE/2][yofs + KERNEL_SIZE/2];
dest[x + y*width] = sum/159;
}
}
/// Calculates squared gradient lengths in the source image; 3 pixel-wide borders are skipped
void CalcGradients(
uint8_t *src, ///< Source image
int width, ///< Image width
int height, ///< Image height
uint32_t *dest ///< Destination buffer (same dimensions as 'src')
)
{
const int KERNEL_SIZE = 3;
const int kernelX[KERNEL_SIZE][KERNEL_SIZE] =
{
{ -1, 0, 1 },
{ -2, 0, 2 },
{ -1, 0, 1 }
};
const int kernelY[KERNEL_SIZE][KERNEL_SIZE] =
{
{ 1, 2, 1 },
{ 0, 0, 0 },
{ -1, -2, -1 }
};
/// By skipping 3-pixel borders we skip the pixels that are also skipped by BlurImage()
for (int y = 3; y <= height - 4; y++)
for (int x = 3; x <= width - 4; x++)
{
int gradX = 0, gradY = 0;
for (int yofs = -KERNEL_SIZE/2; yofs <= KERNEL_SIZE/2; yofs++)
for (int xofs = -KERNEL_SIZE/2; xofs <= KERNEL_SIZE/2; xofs++)
{
uint8_t srcVal = src[(x + xofs) + (y + yofs) * width];
gradX += srcVal * kernelX[xofs + KERNEL_SIZE/2][yofs + KERNEL_SIZE/2];
gradY += srcVal * kernelY[xofs + KERNEL_SIZE/2][yofs + KERNEL_SIZE/2];
}
dest[x + y*width] = gradX*gradX + gradY*gradY; // this is at most 2*(4*255)^2, so fits easily in uint32_t
}
}
namespace BMP
{
#pragma pack(push)
#pragma pack(1)
typedef struct
{
uint16_t bfType;
uint32_t bfSize;
uint16_t bfReserved1;
uint16_t bfReserved2;
uint32_t bfOffBits;
} BITMAPFILEHEADER_t; ///< BMP file header
#pragma pack(1)
typedef struct
{
uint32_t biSize;
int32_t biWidth;
int32_t biHeight;
uint16_t biPlanes;
uint16_t biBitCount;
uint32_t biCompression;
uint32_t biSizeImage;
int32_t biXPelsPerMeter;
int32_t biYPelsPerMeter;
uint32_t biClrUsed;
uint32_t biClrImportant;
} BITMAPINFOHEADER_t; ///< BMP info header
#pragma pack(pop)
const uint32_t BMP_NO_COMPRESSION = 0;
const int BMP_PALETTE_SIZE = 256*4;
// returns the least multiple of 4 which is >= x
#define UP4MULT(x) (((x)+3)/4*4)
/// Reads a BMP image and returns pointer to the newly allocated buffer with pixel contents or 0 on error
void *ReadBmp(const char *fileName,
int &imgWidth, ///< Receives image width
int &imgHeight, ///< Receives image height
PixelFormat_t &pixFmt, ///< Receives the pixel format
uint8_t palette[] ///< If not NULL and reading from an 8-bit file, receives the palette (1024 bytes)
)
{
std::ifstream file(fileName, std::ios_base::in | std::ios_base::binary);
if (file.fail())
return 0;
BITMAPFILEHEADER_t bmpFileHdr;
BITMAPINFOHEADER_t bmpInfoHdr;
file.read((char *)&bmpFileHdr, sizeof(bmpFileHdr));
file.read((char *)&bmpInfoHdr, sizeof(bmpInfoHdr));
if (file.eof())
return 0;
imgWidth = bmpInfoHdr.biWidth;
imgHeight = bmpInfoHdr.biHeight;
if (imgWidth == 0 || imgHeight == 0 ||
bmpFileHdr.bfType != 'B'+((int)'M'<<8) ||
bmpInfoHdr.biPlanes != 1 ||
bmpInfoHdr.biBitCount != 8 && bmpInfoHdr.biBitCount != 24 ||
bmpInfoHdr.biCompression != BMP_NO_COMPRESSION)
{
return 0;
}
if (bmpInfoHdr.biBitCount == 8)
pixFmt = PIX_PAL8;
else if (bmpInfoHdr.biBitCount == 24)
pixFmt = PIX_RGB24;
int bytesPP = GetBytesPerPixel(pixFmt);
void *pixels = malloc(imgWidth * imgHeight * bytesPP);
if (bmpInfoHdr.biBitCount == 8)
{
unsigned bmpStride = UP4MULT(imgWidth); // line length in bytes in the BMP file's pixel data
unsigned skip = bmpStride - imgWidth; // number of padding bytes at the end of a line
int actualPalSize = bmpInfoHdr.biClrUsed == 0 ? BMP_PALETTE_SIZE : bmpInfoHdr.biClrUsed*4;
// seek to the beginning of palette
file.seekg(sizeof(bmpFileHdr) + bmpInfoHdr.biSize, std::ios_base::beg);
if (palette != 0)
file.read((char *)palette, actualPalSize);
// Seek to the beginning of pixel values
file.seekg(bmpFileHdr.bfOffBits, std::ios_base::beg);
for (int y = imgHeight - 1; y >= 0; y--) // lines in BMP are stored bottom to top
{
file.read((char *)((uint8_t *)pixels + y*imgWidth), imgWidth);
if (skip > 0)
file.seekg(skip, std::ios_base::cur);
}
}
else if (bmpInfoHdr.biBitCount == 24)
{
unsigned bmpStride = UP4MULT(imgWidth*3); // line length in bytes in the BMP file's pixel data
unsigned skip = bmpStride - imgWidth*3; // number of padding bytes at the end of a row
// Seek to the beginning of pixel values
file.seekg(bmpFileHdr.bfOffBits, std::ios_base::beg);
// read the lines directly into the buffer
for (int y = imgHeight - 1; y >= 0; y--) // lines in BMP are stored bottom to top
{
file.read((char *)((uint8_t *)pixels + y*imgWidth*3), imgWidth*3);
if (skip > 0)
file.seekg(skip, std::ios_base::cur);
}
}
return pixels;
}
/// Saves image in BMP format; returns 'false' on error
bool SaveBmp(const char *fileName, ///< Output file name
int imgWidth, ///< Image width
int imgHeight, ///< Image height
PixelFormat_t pixFmt, ///< Pixel format; has to be PIX_PAL8 or PIX_RGB24
void *pixels, ///< Pixel contents in 'pixFmt' format
uint8_t palette[] ///< Points to the palette (1024 bytes) to be saved if pixFmt is PIX_PAL8
)
{
BITMAPFILEHEADER_t bmfh;
BITMAPINFOHEADER_t bmih;
int i;
int bytesPP = GetBytesPerPixel(pixFmt);
unsigned bmpLineWidth = UP4MULT(imgWidth * bytesPP);
bmfh.bfType = 'B'+((int)'M'<<8);
bmfh.bfSize = sizeof(bmfh) + sizeof(bmih) + imgHeight*bmpLineWidth;
if (pixFmt == PIX_PAL8)
bmfh.bfSize += BMP_PALETTE_SIZE;
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfOffBits = sizeof(bmih) + sizeof(bmfh);
if (pixFmt == PIX_PAL8)
bmfh.bfOffBits += BMP_PALETTE_SIZE;
bmih.biSize = sizeof(bmih);
bmih.biWidth = imgWidth;
bmih.biHeight = imgHeight;
bmih.biPlanes = 1;
bmih.biBitCount = bytesPP * 8;
bmih.biCompression = BMP_NO_COMPRESSION;
bmih.biSizeImage = 0;
bmih.biXPelsPerMeter = 1000;
bmih.biYPelsPerMeter = 1000;
bmih.biClrUsed = 0;
bmih.biClrImportant = 0;
std::ofstream file(fileName, std::ios_base::out | std::ios_base::trunc | std::ios_base::binary);
if (file.fail())
return false;
file.write((const char *)&bmfh, sizeof(bmfh));
file.write((const char *)&bmih, sizeof(bmih));
if (pixFmt == PIX_PAL8)
file.write((const char *)palette, BMP_PALETTE_SIZE);
int skip = bmpLineWidth - imgWidth*bytesPP;
for (i = imgHeight - 1; i >= 0; i--) // lines in BMP are stored bottom to top
{
file.write((const char*)pixels + i * imgWidth * bytesPP, imgWidth*bytesPP);
if (skip > 0)
file.write((const char *)pixels, skip); //this is just padding, so write anything
}
file.close();
return true;
}
bool GetBmpDimensions(const char *fileName, unsigned &imgWidth, unsigned &imgHeight)
{
std::ifstream file(fileName, std::ios_base::in | std::ios_base::binary);
if (file.fail())
return false;
BITMAPFILEHEADER_t bmpFileHdr;
BITMAPINFOHEADER_t bmpInfoHdr;
file.read((char *)&bmpFileHdr, sizeof(bmpFileHdr));
file.read((char *)&bmpInfoHdr, sizeof(bmpInfoHdr));
if (file.eof())
return false;
imgWidth = bmpInfoHdr.biWidth;
imgHeight = bmpInfoHdr.biHeight;
return true;
}
} // namespace BMP
namespace TIFF
{
#pragma pack(push, 1)
typedef struct
{
uint16_t tag;
uint16_t type;
uint32_t count;
uint32_t value;
} TiffField_t;
typedef struct
{
uint16_t id;
uint16_t version;
uint32_t dirOffset; // = offset of 'numDirEntries'
} TiffHeader_t;
#pragma pack(pop)
typedef enum { ttByte = 1, ttAscii, ttWord, ttDWord, ttRational } TagType_t;
const int TIFF_VERSION = 42;
const int TAG_IMAGE_WIDTH = 0x100;
const int TAG_IMAGE_HEIGHT = 0x101;
const int TAG_BITS_PER_SAMPLE = 0x102;
const int TAG_COMPRESSION = 0x103;
const int TAG_PHOTOMETRIC_INTERPRETATION = 0x106;
const int TAG_STRIP_OFFSETS = 0x111;
const int TAG_SAMPLES_PER_PIXEL = 0x115;
const int TAG_ROWS_PER_STRIP = 0x116;
const int TAG_STRIP_BYTE_COUNTS = 0x117;
const int TAG_PLANAR_CONFIGURATION = 0x11C;
const uint16_t NO_COMPRESSION = 1;
const uint16_t PLANAR_CONFIGURATION_CHUNKY = 1;
const uint16_t INTEL_BYTE_ORDER = ((uint16_t)'I' << 8) + 'I'; // little-endian
const uint16_t MOTOROLA_BYTE_ORDER = ((uint16_t)'M' << 8) + 'M'; // big-endian
const int PHMET_WHITE_IS_ZERO = 0;
const int PHMET_BLACK_IS_ZERO = 1;
const int PHMET_RGB = 2;
inline unsigned GetFieldTypeLength(TagType_t ttt)
{
switch (ttt)
{
case ttByte: return 1; break;
case ttAscii: return 1; break;
case ttWord: return 2; break;
case ttDWord: return 4; break;
case ttRational: return 8; break;
}
}
/// Conditionally swaps a 32-bit value
uint32_t inline SWAP32cnd(uint32_t x, bool swap)
{
if (swap) return (x << 24) | ((x & 0x00FF0000) >> 8) | ((x & 0x0000FF00) << 8) | (x >> 24);
else return x;
}
/// Conditionally swaps two lower bytes of a 32-bit value
uint32_t inline SWAP16in32cnd(uint32_t x, bool swap)
{
if (swap) return ((x & 0xFF) << 8) | (x >> 8);
else return x;
}
uint16_t inline SWAP16cnd(uint16_t x, bool swap)
{
if (swap) return (x << 8) | (x >> 8);
else return x;
}
/// Changes endianess of 16-bit words in the specified buffer
void SwapBufferWords(uint16_t *buf, int numWords)
{
for (unsigned i = 0; i < numWords; i++)
buf[i] = (buf[i] << 8) | (buf[i] >> 8);
}
/// Reverses values of an 8-bit grayscale buffer
void NegateGrayscale8(uint8_t *buf, int length)
{
for (int i = 0; i < length; i++)
buf[i] = 0xFF - buf[i];
}
/// Reverses values of a 16-bit grayscale buffer
void NegateGrayscale16(uint16_t *buf, int length)
{
for (int i = 0; i < length; i++)
buf[i] = 0xFFFF - buf[i];
}
bool SaveTiff(const char *fileName, ///< Output file name
void *pixels, ///< Pointer to the buffer with pixel data (left to right, top to bottom, no padding)
PixelFormat_t pixFmt, ///< Pixel format of 'pixels'
int imgWidth,
int imgHeight
)
{
if (pixFmt != PIX_MONO8 && pixFmt != PIX_MONO16 && pixFmt != PIX_RGB24 && pixFmt != PIX_RGB48)
throw std::runtime_error("SaveTiff(): only grayscale and RGB, 8- and 16-bit formats are supported.");
std::ofstream file(fileName, std::ios_base::trunc | std::ios_base::binary);
if (file.fail())
return false;
TiffHeader_t tiffHeader;
tiffHeader.id = INTEL_BYTE_ORDER;
tiffHeader.version = TIFF_VERSION;
tiffHeader.dirOffset = sizeof(tiffHeader);
file.write((const char *)&tiffHeader, sizeof(tiffHeader));
uint16_t numDirEntries = 10;
file.write((const char *)&numDirEntries, sizeof(numDirEntries));
uint32_t nextDirOffset = 0;
TiffField_t field;
field.tag = TAG_IMAGE_WIDTH;
field.type = ttWord;
field.count = 1;
field.value = imgWidth;
file.write((const char *)&field, sizeof(field));
field.tag = TAG_IMAGE_HEIGHT;
field.type = ttWord;
field.count = 1;
field.value = imgHeight;
file.write((const char *)&field, sizeof(field));
field.tag = TAG_BITS_PER_SAMPLE;
field.type = ttWord;
field.count = 1;
switch (pixFmt)
{
case PIX_MONO8:
case PIX_RGB24:
field.value = 8; break;
case PIX_MONO16:
case PIX_RGB48:
field.value = 16; break;
}
file.write((const char *)&field, sizeof(field));
field.tag = TAG_COMPRESSION;
field.type = ttWord;
field.count = 1;
field.value = NO_COMPRESSION;
file.write((const char *)&field, sizeof(field));
field.tag = TAG_PHOTOMETRIC_INTERPRETATION;
field.type = ttWord;
field.count = 1;
switch (pixFmt)
{
case PIX_MONO8:
case PIX_MONO16:
field.value = PHMET_BLACK_IS_ZERO; break;
case PIX_RGB24:
case PIX_RGB48:
field.value = PHMET_RGB; break;
}
file.write((const char *)&field, sizeof(field));
field.tag = TAG_STRIP_OFFSETS;
field.type = ttDWord;
field.count = 1;
// we write the header, num. of directory entries, 10 fields and a next directory offset (==0); pixel data starts next
field.value = sizeof(tiffHeader) + sizeof(numDirEntries) + 10*sizeof(field) + sizeof(nextDirOffset);
file.write((const char *)&field, sizeof(field));
field.tag = TAG_SAMPLES_PER_PIXEL;
field.type = ttWord;
field.count = 1;
switch (pixFmt)
{
case PIX_MONO8:
case PIX_MONO16:
field.value = 1; break;
case PIX_RGB24:
case PIX_RGB48:
field.value = 3; break;
}
file.write((const char *)&field, sizeof(field));
field.tag = TAG_ROWS_PER_STRIP;
field.type = ttWord;
field.count = 1;
field.value = imgHeight; // there is only one strip for the whole image
file.write((const char *)&field, sizeof(field));
field.tag = TAG_STRIP_BYTE_COUNTS;
field.type = ttDWord;
field.count = 1;
field.value = imgWidth * imgHeight * GetBytesPerPixel(pixFmt); // there is only one strip for the whole image
file.write((const char *)&field, sizeof(field));
field.tag = TAG_PLANAR_CONFIGURATION;
field.type = ttWord;
field.count = 1;
field.value = PLANAR_CONFIGURATION_CHUNKY; // there is only one strip for the whole image
file.write((const char *)&field, sizeof(field));
// write the next directory offset (0 = no other directories)
file.write((const char *)&nextDirOffset, sizeof(nextDirOffset));
file.write((const char *)pixels, imgWidth * imgHeight * GetBytesPerPixel(pixFmt));
file.close();
return true;
}
/// Returns newly allocated buffer with contents of the specified TIFF file (returns 0 on error)
void *ReadTiff(const char *fileName, ///< Input file name
PixelFormat_t &pixFmt, ///< Receives the pixel format
int &imgWidth, ///< Receives image width
int &imgHeight, ///< Receives image height
std::string *errorMsg ///< If not null, receives error message (if any)
)
{
std::ifstream file(fileName, std::ios_base::binary);
if (file.fail())
return 0;
TiffHeader_t tiffHeader;
file.read((char *)&tiffHeader, sizeof(tiffHeader));
if (file.gcount() != sizeof(tiffHeader))
{
if (errorMsg) *errorMsg = "File header is incomplete.";
return 0;
}
bool isBE = tiffHeader.id == MOTOROLA_BYTE_ORDER; // true if the file has big endian data
if (SWAP16cnd(tiffHeader.version, isBE) != TIFF_VERSION)
{
if (errorMsg) *errorMsg = "Unknown TIFF version.";
return 0;
}
// Seek to the first TIFF directory
file.seekg(SWAP32cnd(tiffHeader.dirOffset, isBE), std::ios_base::beg);
uint16_t numDirEntries;
file.read((char *)&numDirEntries, sizeof(numDirEntries));
numDirEntries = SWAP16cnd(numDirEntries, isBE);
if (file.gcount() != sizeof(numDirEntries))
{
if (errorMsg) *errorMsg = "The number of TIFF directory entries tag is incomplete.";
return 0;
}
unsigned numStrips = 0;
unsigned bitsPerSample = 0;
unsigned *stripOffsets = 0;
unsigned *stripByteCounts = 0;
unsigned rowsPerStrip = 0;
int photometricInterpretation = -1;
int samplesPerPixel = 0;
std::fstream::pos_type nextFieldPos = file.tellg();
for (unsigned i = 0; i < numDirEntries; i++)
{
TiffField_t tiffField;
file.seekg(nextFieldPos, std::ios_base::beg);
file.read((char *)&tiffField, sizeof(tiffField));
if (file.gcount() != sizeof(tiffField))
{
if (errorMsg) *errorMsg = "TIFF field is incomplete.";
return 0;
}
nextFieldPos = file.tellg();
tiffField.tag = SWAP16cnd(tiffField.tag, isBE);
tiffField.type = SWAP16cnd(tiffField.type, isBE);
tiffField.count = SWAP32cnd(tiffField.count, isBE);
if (tiffField.count > 1 || tiffField.type == ttDWord)
tiffField.value = SWAP32cnd(tiffField.value, isBE);
else if (tiffField.count == 1 && tiffField.type == ttWord)
tiffField.value = SWAP16in32cnd(tiffField.value, isBE);
switch (tiffField.tag)
{
case TAG_IMAGE_WIDTH: imgWidth = tiffField.value; break;
case TAG_IMAGE_HEIGHT: imgHeight = tiffField.value; break;
case TAG_BITS_PER_SAMPLE:
if (tiffField.count == 1)
bitsPerSample = tiffField.value;
else
{
// Some files may have as many "bits per sample" values specified
// as there are channels. Make sure they are all the same.
file.seekg(tiffField.value, std::ios_base::beg);
uint16_t *fieldBuf = new uint16_t[tiffField.count];
file.read((char *)fieldBuf, tiffField.count * sizeof(uint16_t));
bool allEqual = true;
uint16_t first = fieldBuf[0];
for (unsigned j = 1; j < tiffField.count; j++)
if (fieldBuf[j] != first)
{
allEqual = false;
break;
}
if (!allEqual)
{
if (errorMsg) *errorMsg = "Files with differing bit depts per channel are not supported.";
return 0;
}
bitsPerSample = SWAP16cnd(first, isBE);
}
if (bitsPerSample != 8 && bitsPerSample != 16)
{
if (errorMsg) *errorMsg = "Only 8 and 16 bits per channel files are supported.";
return 0;
}
break;
case TAG_COMPRESSION:
if (tiffField.value != NO_COMPRESSION)
{
if (errorMsg) *errorMsg = "Compression is not supported.";
return 0;
}
break;
case TAG_PHOTOMETRIC_INTERPRETATION: photometricInterpretation = tiffField.value; break;
case TAG_STRIP_OFFSETS:
numStrips = tiffField.count;
stripOffsets = new unsigned[numStrips];
if (numStrips == 1)
stripOffsets[0] = tiffField.value;
else
{
file.seekg(tiffField.value, std::ios_base::beg);
for (unsigned i = 0; i < numStrips; i++)
{
file.read((char *)&stripOffsets[i], sizeof(stripOffsets[i]));
stripOffsets[i] = SWAP32cnd(stripOffsets[i], isBE);
}
}
break;
case TAG_SAMPLES_PER_PIXEL: samplesPerPixel = tiffField.value; break;
case TAG_ROWS_PER_STRIP: rowsPerStrip = tiffField.value; break;
case TAG_STRIP_BYTE_COUNTS:
stripByteCounts = new unsigned[tiffField.count];
if (tiffField.count == 1)
stripByteCounts[0] = tiffField.value;
else
{
file.seekg(tiffField.value, std::ios_base::beg);
for (unsigned i = 0; i < tiffField.count; i++)
{
file.read((char *)&stripByteCounts[i], sizeof(stripByteCounts[i]));
stripByteCounts[i] = SWAP32cnd(stripByteCounts[i], isBE);
}
}
break;
case TAG_PLANAR_CONFIGURATION:
if (tiffField.value != PLANAR_CONFIGURATION_CHUNKY)
{
if (errorMsg) *errorMsg = "Files with planar configuration other than packed (chunky) are not supported.";
return 0;
}
break;
}
}
if (rowsPerStrip == 0 && numStrips == 1)
// If there is only 1 strip, it contains all the rows
rowsPerStrip = imgHeight;
// Validate the values
if (samplesPerPixel == 1 && photometricInterpretation != PHMET_BLACK_IS_ZERO && photometricInterpretation != PHMET_WHITE_IS_ZERO ||
samplesPerPixel == 3 && photometricInterpretation != PHMET_RGB ||
samplesPerPixel != 1 && samplesPerPixel != 3)
{
if (errorMsg) *errorMsg = "Only RGB and grayscale images are supported.";
return 0;
}
if (samplesPerPixel == 1)
{
if (bitsPerSample == 8)
pixFmt = PIX_MONO8;
else if (bitsPerSample == 16)
pixFmt = PIX_MONO16;
}
else if (samplesPerPixel == 3)
{
if (bitsPerSample == 8)
pixFmt = PIX_RGB24;
else if (bitsPerSample == 16)
pixFmt = PIX_RGB48;
}
// Buffer with all image pixel values, left to right, top to bottom, without any padding
void *pixels = malloc(imgWidth * imgHeight * GetBytesPerPixel(pixFmt));
int bufOfs = 0;
for (unsigned i = 0; i < numStrips; i++)
{
file.seekg(stripOffsets[i], std::ios_base::beg);
file.read((char *)pixels + bufOfs, stripByteCounts[i]);
bufOfs += stripByteCounts[i];
if (file.gcount() != stripByteCounts[i])
{
if (errorMsg) *errorMsg = boost::str(boost::format("The file is incomplete: pixel data in strip %d is too short. Expected %d bytes, but read only %d.") % i % stripByteCounts[i] % file.gcount());
free(pixels);
return 0;
}
}
if ((pixFmt == PIX_MONO16 || pixFmt == PIX_RGB48) && isBE)
SwapBufferWords((uint16_t *)pixels, imgWidth*imgHeight*GetBytesPerPixel(pixFmt)/2);
if (photometricInterpretation == PHMET_WHITE_IS_ZERO)
{
// Reverse the values so that "black" is zero, "white" is 255 or 65535.
if (pixFmt == PIX_MONO8)
NegateGrayscale8((uint8_t *)pixels, imgWidth*imgHeight);
else if (pixFmt == PIX_MONO16)
NegateGrayscale16((uint16_t *)pixels, imgWidth*imgHeight);
}
file.close();
return pixels;
}
bool GetTiffDimensions(const char *fileName, unsigned &imgWidth, unsigned &imgHeight)
{
std::ifstream file(fileName, std::ios_base::binary);
if (file.fail())
return false;
TiffHeader_t tiffHeader;
file.read((char *)&tiffHeader, sizeof(tiffHeader));
if (file.gcount() != sizeof(tiffHeader))
return false;
bool isBE = tiffHeader.id == MOTOROLA_BYTE_ORDER; // true if the file has big endian data
if (SWAP16cnd(tiffHeader.version, isBE) != TIFF_VERSION)
return false;
// Seek to the first TIFF directory
file.seekg(SWAP32cnd(tiffHeader.dirOffset, isBE), std::ios_base::beg);
uint16_t numDirEntries;
file.read((char *)&numDirEntries, sizeof(numDirEntries));
numDirEntries = SWAP16cnd(numDirEntries, isBE);
if (file.gcount() != sizeof(numDirEntries))
return false;
imgWidth = imgHeight = -1;
std::fstream::pos_type nextFieldPos = file.tellg();
for (unsigned i = 0; i < numDirEntries; i++)
{
TiffField_t tiffField;
file.seekg(nextFieldPos, std::ios_base::beg);
file.read((char *)&tiffField, sizeof(tiffField));
if (file.gcount() != sizeof(tiffField))
return false;
nextFieldPos = file.tellg();
tiffField.tag = SWAP16cnd(tiffField.tag, isBE);
tiffField.type = SWAP16cnd(tiffField.type, isBE);
tiffField.count = SWAP32cnd(tiffField.count, isBE);
if (tiffField.count > 1 || tiffField.type == ttDWord)
tiffField.value = SWAP32cnd(tiffField.value, isBE);
else if (tiffField.count == 1 && tiffField.type == ttWord)
tiffField.value = SWAP16in32cnd(tiffField.value, isBE);
switch (tiffField.tag)
{
case TAG_IMAGE_WIDTH: imgWidth = tiffField.value; break;
case TAG_IMAGE_HEIGHT: imgHeight = tiffField.value; break;
}
if (imgWidth != -1 && imgHeight != -1)
break;
}
return true;
}
} // namespace TIFF
/// Converts data in input buffer to the specified pixel format and writes it to the destination buffer; if formats are the same, does nothing
void ConvertPixelFormat(
void *srcBuf, ///< Source (input) buffer (pixels stored left to right, top to bottom, no padding)
void *destBuf, ///< Destination (output) buffer
int width, ///< Image width (number of columns in the buffers)
int height, ///< Image height (number of rows in the buffers)
PixelFormat_t srcPixFmt, ///< Pixel format in 'srcBuf'
PixelFormat_t destPixFmt, ///< Desired pixel format in 'destBuf'; PIX_PAL8 is not supported
uint8_t palette[] ///< Pointer to 256-element RGB(+1) palette (256*4 bytes); required if 'srcPixFmt' or 'destPixFmt' is PIX_PAL8
)
{
if (srcPixFmt == PIX_UNCHANGED || destPixFmt == PIX_UNCHANGED)
throw std::runtime_error("ConvertPixelFormat(): specifying PIX_UNCHANGED is not allowed.");
if (destPixFmt == PIX_PAL8 && srcPixFmt != PIX_PAL8)
throw std::runtime_error("ConvertPixelFormat(): cannot convert to PIX_PAL8");
if (srcPixFmt == PIX_PAL8 && !palette)
throw std::runtime_error("ConvertPixelFormat(): palette required when converting from PIX_PAL8");
if (srcPixFmt == destPixFmt)
return;
uint8_t *inpPtr = (uint8_t*)srcBuf,
*outPtr = (uint8_t*)destBuf;
int inpPtrStep = GetBytesPerPixel(srcPixFmt),
outPtrStep = GetBytesPerPixel(destPixFmt);
for (int i = 0; i < width*height; i++)
{
if (srcPixFmt == PIX_MONO8)
{
uint8_t src = *inpPtr;
switch (destPixFmt)
{
case PIX_MONO16: *(uint16_t *)outPtr = (uint16_t)src << 8; break;
case PIX_MONO32F: *(float *)outPtr = src * 1.0f/0xFF; break;
case PIX_RGB24: outPtr[0] = outPtr[1] = outPtr[2] = src; break;
case PIX_RGB48:
((uint16_t *)outPtr)[0] =
((uint16_t *)outPtr)[1] =
((uint16_t *)outPtr)[2] = (uint16_t)src << 8;
break;
}
}
else if (srcPixFmt == PIX_MONO16)
{
uint16_t src = *(uint16_t *)inpPtr;
switch (destPixFmt)
{