-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfloatTIFF.cpp
More file actions
1886 lines (1539 loc) · 60.6 KB
/
floatTIFF.cpp
File metadata and controls
1886 lines (1539 loc) · 60.6 KB
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
/* *** floatTIFF.cpp ***
------------------------------------------------------------------------
Copyright 2012-2014 Earl J. Kirkland
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
---------------------- NO WARRANTY ------------------
THIS PROGRAM IS PROVIDED AS-IS WITH ABSOLUTELY NO WARRANTY
OR GUARANTEE OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL THE AUTHOR BE LIABLE
FOR DAMAGES RESULTING FROM THE USE OR INABILITY TO USE THIS
PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA
BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR
THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH
ANY OTHER PROGRAM).
------------------------------------------------------------------------
C++ class to read/write floating point images as TIFF format files (in ANSI-C++)
and manage the memory storage. There are three (3) images in each file.
The first image is a simple 8 bit image with square pixels for display only.
The second image is a 32 bit floating point image with possibly rectangular
pixels for calculations. The third image is a 2048 by 1 "image" of
32 bit floating point parameters related t the calculation.
This is mainly an easy to use high level front end derived from the old
tiffsubs.c non-class library.
The computer is assumed to have either big-endian or little-endian
byte ordering and to be a 32/64 bit machine (i.e. a long must be 32 bits).
These extended TIFF routines assume that the computer hardware uses
IEEE floating point (these may still work if this is not true but the
image files will not be transporable).
These routines are thought to conform to TIFF version 6.0
(dated June 3, 1992) from:
Adobe Developers Association
Adobe Systems Incorporated
1585 Charleston Road
P.O. Box 7900
Mountain View, CA 94039-7900
http://www.adobe.com/Support/TechNotes.html
ftp://ftp.adobe.com/pub/adobe/DeveloperSupport/TechNotes/PDFfiles
NOTE-1: This software is thought to be correct but absolutely no guarantee
whether written or implied is given. This software is supplied on a
user-beware "as is" basis.
NOTE-2: These routines read in either byte order (Intel=little endian or
Motorola=big endian) but write only the byte order of the computer
they are running on. It is assumed that the computer supports
byte addressing and ASCII.
NOTE-3: Various symbol definitions (at top of file) can be changed
for different computers and modes (the symbol CompByteOrder
will be automatically determined at run time):
The source code is formatted for a tab size of 4.
----------------------------------------------------------
The public member functions are:
getParam() : return value of parameter
getnpix() : return value of npix
getDateTime() : get date and time of image
max() : return maximum in image
min() : return minimum value in image
nx() : return horz. size of current image (in pixels)
ny() : return vert. size of current image (in pixels)
maxParam() : return maximum nu,ber of parameters
operator()(ix,iy) : return reference to pixel at (ix,iy)
: a complex image is stored side-by-side (real-image)
: as one image with npix=2
read( file ) : read file with name 'file' into memory buffer
(allocate memory buffer if necessary)
resize() : resize current in-memory image (current data may be lost)
setParam() : change value of parameter
setnpix() : change value of npix (must be 1 for real or 2 for complex)
tFloatTest : check if a file has a valid TIFF header
call before topenFloat() to avoid problems
write(file) : write current image in memory to file with name 'file'
zeroParam() : zero all parameter
----------------------------------------------------------
The private routines in this file are:
tclose: close currently open tiff file and
deallocate internal storage
tcreateFloatPixFile: create a floating point image file with an
8 bit image at the beginning for viewing (can be read by
treadFloatPix)
tifferr: common error handler - print messages
(internal use only)
topenFloat: open an extended TIFF floating point image
for reading
tread: read bytes from file with byte reversal if necessary
(internal use only)
treadIFD: read the IFD or directory of currently open file
(internal use only)
treadPix: read a 'standard' TIFF image (8 or 16 bit integer)
file into memory
tsetByteOrder: determine the byte order of the computer that
this is running on and verify data type sizes
----------------------------------------------------------
The floating point image routines use an extended TIFF format
(i.e. they conform to the official TIFF standard but are not a
common permutation). A floating point image (real or
complex=real+imaginary) and a parameter array are stored in
a single file. The first image in the file is a standard
8 bit greyscale image which most TIFF readers should be able
to handle. This 8 bit image may be expanded in one direction
using bilinear interpolation to get square pixels. The second
image in the file is stored as 32 bit IEEE floating point (for
simulation data) and the third image is one line with a 64
element floating point parameter array.
The order of the data in the file is:
<TIFF header>
unsigned 8 bit image data with square pixels
<IFD-1>
32 bit floating point image data (pixels may be rectangular)
<IFD-2>
32 bit parameter data
<IFD-3>
----------------------------------------------------------
----------------------------------------------------------
started class version from tiffsubs.c 5-nov-2003 E. Kirkland
switch from long to long32 with typdef to int/long so it
will work on 64 bit machines 20-may-2008 ejk
work on write(), min()/max() subroutines 11-mar-2012 ejk
change to PMAX parameter length,
add getDateTime() and zeroParam() 18-mar-2012 ejk
add maxParam() member 21-mar-2012
modified for wxWidgets (remove printf() for error messages )
-- only change tifferr() and ifdef symbols 5-feb-2013 ejk
convert sprintf()+char[] to strings 1-feb-2014 ejk
convert to streams IO 1-feb-2014 ejk
convert remaining buffers to use new/delete 2-feb-2014 ejk
update wxGUI error message in tifferr() 10-feb-2014 ejk
last modified 10-feb-2014 ejk
*/
#include <cstdlib>
#include <cstdio>
#include <cerrno>
#include <cstring>
#include <ctime>
#include "floatTIFF.hpp" // class definition + inline functions here
//#define wxGUI // set for wxWidgets graphical user interface
#ifdef wxGUI
#include "wx/wx.h" // regular headers
//#include "wx/wxprec.h" // for precompiled headers
#endif
//-------------- constants ----------------------------------------
// define the byte order types
const int tBigEndian=0x4d4d; // = 'MM' = Motorola
const int tLittleEndian=0x4949; // = 'II' = Intel
const int sizes[] = { 0, 1, 1, 2, 4, 8 }; // size in bytes of each TIFF data type
//---------------------------------------------------------------
//------------- begin functions here ----------------------
//---------------------------------------------------------------
//------------------ constructor --------------------------------
floatTIFF::floatTIFF()
{
nIFD=0;
nstrips=0;
nstripcounts=0;
stempo=NULL;
stempc=NULL;
ifd1 = NULL;
StripByteCounts = NULL;
StripOffsets = NULL;
DateTime = "No-Date-Given";
nxl = nyl = 0; // initialize to "no-data" condition
npix = 1; // default to real
PMAX = 512; // maximum number of parameters
param = new float[PMAX];
if( NULL == param ) { // may exit() here
tifferr( "floatTIFF cannot open allocate param array." );
}
tsetByteOrder();
}
//------------------ destructor ---------------------------------
floatTIFF::~floatTIFF()
{
if( ifd1 != NULL ) {
delete [] ifd1; nIFD = 0; ifd1 = NULL;
}
if( StripByteCounts != NULL) { nstrips = 0;
delete [] StripByteCounts; StripByteCounts = NULL; }
if( StripOffsets != NULL) { nstripcounts = 0;
delete [] StripOffsets; StripOffsets = NULL; }
if( stempo != NULL ) { delete [] stempo; stempo = NULL; }
if( stempc != NULL ) { delete [] stempc; stempc = NULL; }
if( (nxl>0) && (nyl>0) ) delete [] data;
delete [] param;
}
//--------------------- getParam() ----------------------------------
float floatTIFF::getParam( const int i )
{
if( (i>=0) && (i<=PMAX) ) return( param[i] );
else return( 0 );
} // end floatTIFF::getParam()
//-------------------- max() ----------------------------------
// ipix = pix number ( 0 for real part and 1 for imag part )
// ignored if there is only one pix
float floatTIFF::max( int ipix )
{ uslong ix, iy, nx, xo=0;
float val, t;
if( npix > 1 ){
nx = nxl /npix ;
if( ipix > 0 ) xo = nxl/2;
}else {
nx = nxl;
}
val = operator()(xo,0);
for( iy=0; iy<nyl; iy++)
for( ix=0; ix<nx; ix++){
t = operator()(ix+xo,iy);
if( t > val ) val = t;
}
return( val );
}
//-------------------- min() ----------------------------------
// ipix = pix number ( 0 for real part and 1 for imag part )
// ignored if there is only one pix
float floatTIFF::min( int ipix )
{ uslong ix, iy, nx, xo=0;
float val, t;
if( npix > 1 ){
nx = nxl /npix ;
if( ipix > 0 ) xo = nxl/2;
}else {
nx = nxl;
}
val = operator()(xo,0);
for( iy=0; iy<nyl; iy++)
for( ix=0; ix<nx; ix++){
t = operator()(ix+xo,iy);
if( t < val ) val = t;
}
return( val );
}
//--------------------- read() ----------------------------------
int floatTIFF::read( const char *filname )
{
int is=-102;
if( (is= topenFloat( filname )) != 1 ) {
tifferr( "floatTIFF::read() cannot open input file." );
return( is );
}
resize( ImageWidth, ImageLength );
//------ read the image data --------------------------
if( (is= treadFloatPix()) != 1 ) {
tifferr( "floatTIFF::read() cannot read input file." );
tclose();
return( is );
}
//----- read() is succesful ---------------------------
tclose();
return ( 1 );
} // end floatTIFF::read()
//--------------------- resize() ----------------------------------
// note: existing data (if any) may be destroyed
int floatTIFF::resize( const int nx, const int ny )
{
//--- resize buffer ------------------------
if( (nx != int(nxl)) || (ny != int(nyl)) ){
if( (nxl != 0) && (nyl != 0) ) delete [] data;
ImageWidth = nx;
ImageLength = ny;
data = new float [ ImageWidth*ImageLength ];
if( NULL == data ) {
tifferr( "Cannot allocate image storage in floatTIFF::resize()\n");
tclose();
return( -3 );
}
nxl = ImageWidth;
nyl = ImageLength;
}
return( +1 );
} // end floatTIFF::resize()
//--------------------- setParam() ----------------------------------
void floatTIFF::setParam( const int i, const float val )
{
if( (i>=0) && (i<=PMAX) ) param[i] = val;
} // end floatTIFF::setParam()
//--------------------- setnpix() ----------------------------------
void floatTIFF::setnpix( const int i )
{
if( (i>=1) && (i<=2) ) npix = i;
} // end floatTIFF::setParam()
/*--------------------- tclose -------------------*/
/*
close a TIFF file opened by topen
and free allocated data storage
return 1 for success and </=0 for failure
*/
int floatTIFF::tclose( )
{
tfp.close();
if( tfp.fail() ) {
tifferr( "tclose() cannot close the file.");
return( -1 ); }
if( ifd1 != NULL ) {
delete [] ifd1; nIFD = 0; ifd1 = NULL;
}
if( StripByteCounts != NULL) { nstrips = 0;
delete [] StripByteCounts; StripByteCounts = NULL; }
if( StripOffsets != NULL) { nstripcounts = 0;
delete [] StripOffsets; StripOffsets = NULL; }
if( stempo != NULL ) { delete [] stempo; stempo = NULL; }
if( stempc != NULL ) { delete [] stempc; stempc = NULL; }
return(1);
} /* end tclose */
/*--------------------- tFloatTest() -------------------*/
/*
Test that a file is an extended TIFF file and
has a floating point image (not an exhaustive test)
filename = pointer to string with filename
return 1 for success and </=0 for failure
*/
int floatTIFF::tFloatTest( const char *filename )
{
int iread;
tsetByteOrder();
if( tifftest( filename ) != 1 ) return( -1 );
tfp.open( filename, ios_base::binary );
if( tfp.fail() ) {
tifferr( "tFloatTest() bad filename." );
return( -2 ); }
tfp.read( (char*)(&FileByteOrder), sizeof(short) );
if( (tfp.fail()) ||
!( (FileByteOrder==tLittleEndian) ||
(FileByteOrder==tBigEndian) ) ) {
tfp.close( );
return( -3 ); }
tread( &Version, 2, 1, 2L );
if( Version != 42 ) { // the TIFF magic number
tfp.close( );
return( -3 ); }
// get location of IFD and read it
tread( &CurrentIFD, 4, 1, 4L );
iread = treadIFD( CurrentIFD );
if( iread != 1 ) {
tclose();
return( -4 ); }
// find the first floating point image
while( ( SampleFormat != 3 ) &&
( BitsPerSample[0] != 8*sizeof(float) ) &&
( NextIFD > 0 ) ) {
if( treadIFD( NextIFD ) < 1 ) {
tclose( );;
return( -5 );
}
}
tclose();
if( ( SampleFormat != 3 ) ||
( BitsPerSample[0] != 8*sizeof(float) ) ) return( -6 );
return( 1 );
} // end tFloatTest()
/*--------------------- tifferr() ------------------*/
/*
common error handler - print messages
only for internal use
DO NOT CALL FROM MAIN PROGRAM
*/
void floatTIFF::tifferr( const string &error_text )
{
#ifdef wxGUI
string stemp;
stemp = "floatTIFF Error: " + error_text;
wxMessageBox( stemp, wxT("floatTIFF"),
wxOK | wxICON_INFORMATION ); //??? , wxGetApp().GetTopFrame() );
#else
//if( errno != 0 )
// fprintf( stderr, "System Error: %s\n", strerror( errno ) ); // ????
cout << "floatTIFF Error: " << error_text.c_str() << endl;
#endif
return;
} /* end tifferr() */
/*--------------------- tifftest() -------------------*/
/*
check if a file has a valid TIFF header
return a 1 if it is a valid TIFF header, </=0 otherwise
filename = pointer to string with filename
*/
int floatTIFF::tifftest( const char *filename )
{
ifstream tfp3;
short byte01, byte23;
tfp3.open( filename, ios_base::binary );
if( tfp3.fail() ) {
tifferr( "Bad filename in tifftest");
return( -1 ); }
tfp3.read( (char*)(&byte01), sizeof(short) );
tfp3.read( (char*)(&byte23), sizeof(short) );
if( tfp3.fail() ) {
tifferr( "tifftest cannot read header.");
return(-3); }
tfp3.close();
if( tfp3.fail() ) {
tifferr( "tifftest cannot close file.");
return( -2 ); }
/* tLittleEndian = "II" = least significant byte first
tBigEndian = "MM" = most significant byte first
42 = 0x2A = TIFF magic number */
if( (byte01!=tLittleEndian) &&
(byte01!=tBigEndian) ) return(-4);
if( (byte01==CompByteOrder) && (byte23!=0x002A) ) return(-5);
if( (byte01!=CompByteOrder) && (byte23!=0x2A00) ) return(-5);
return( +1 );
} /* end tifftest */
/*--------------------- tinter() ----------------------------*/
/*
Bilinear interpolation from data array
nx,ny = integer dimension of the image
x,y = real coord of image point to interpolate at
0->(nx-1) and 0->(ny-1)
*/
float floatTIFF::tinter( long32 nx, long32 ny, double x, double y )
{
long32 ix, iy, ix2, iy2;
float a, b, c, d, x1, y1, ans;
ix = (int) x;
iy = (int) y;
if( ix > (nx-2) ) ix = (nx-2);
if( ix < 0 ) ix = 0;
if( iy > (ny-2) ) iy = (ny-2);
if( iy < 0 ) iy = 0;
ix2 = ix + 1;
iy2 = iy + 1;
x1 = (float) ix;
y1 = (float) iy;
d = operator()(ix,iy) - operator()(ix,iy2) - operator()(ix2,iy)
+ operator()(ix2,iy2);
c = operator()(ix,iy2) - operator()(ix,iy) - d*x1;
b = operator()(ix2,iy) - operator()(ix,iy) - d*y1;
a = operator()(ix,iy) - b*x1 - c*y1 - d*x1*y1;
ans = (float) ( a + b*x + c*y + d*x*y );
return( ans );
} /* end tinter() */
/*--------------------- topenFloat() -------------------*/
/*
open a extended TIFF file for reading as a floating
point image and check that the header is a valid TIFF header
filename = pointer to string with filename
return 1 for success and </=0 for failure
*/
int floatTIFF::topenFloat( const char *filename )
{
int iread;
tsetByteOrder();
tfp.open( filename, ios_base::binary );
if( tfp.fail() ) {
tifferr( "topenFloat() bad filename." );
return( -1 ); }
tfp.read( (char*)(&FileByteOrder), sizeof(short) );
if( (tfp.fail()) ||
!( (FileByteOrder==tLittleEndian) ||
(FileByteOrder==tBigEndian) ) ) {
tifferr( "Not a TIFF file in topenFloat().");
return( -2 ); }
tread( &Version, 2, 1, 2L );
if( Version != 42 ) { // the TIFF magic number
tifferr( "Not a TIFF file in topenFloat().");
return( -3 ); }
/* get location of IFD and read it */
tread( &CurrentIFD, 4, 1, 4L );
iread = treadIFD( CurrentIFD );
if( iread != 1 ) {
tifferr( "topenFloat() can't read first IFD.");
return( iread ); }
/* find the first floating point image */
while( ( SampleFormat != 3 ) &&
( BitsPerSample[0] != 8*sizeof(float) ) &&
( NextIFD > 0 ) ) {
if( treadIFD( NextIFD ) < 1 ) {
tifferr("Cannot read IFD in treadFloatPix.");
return( -4 );
}
}
if( ( SampleFormat != 3 ) ||
( BitsPerSample[0] != 8*sizeof(float) ) ) return( -5 );
return( 1 );
} /* end topenFloat() */
/*--------------------- tread() -------------------*/
/*
this routine mimics the ANSI fread function
however it fixes the byte order if required
read n elements of length size into the buffer
pointed to by bufptr from the current TIFF file
at offset position (in bytes)
if offset < 0 then do sequential reads from
current position
return 1 for success and </=0 for failure
for internal use only
DO NOT CALL FROM MAIN PROGRAM
*/
int floatTIFF::tread( void *bufptr, int size, int n, long32 offset )
{
int i, j;
unsigned char *temp, *ctmp;
if( offset > 0 ) {
tfp.seekg( offset, ios_base::beg );
if( tfp.fail() ) {
tifferr("Bad file seek in tread().");
return( -1 ); } }
tfp.read( (char*)bufptr, size*n );
if( tfp.fail() ) {
tifferr("Bad file read in tread().");
return( -2 ); }
if ( ( FileByteOrder != CompByteOrder ) && ( size > 1 ) )
{ ctmp = (unsigned char*) new char[ size ];
temp = (unsigned char*) bufptr;
for( i=0; i<n; i++) {
for( j=0; j<size; j++) ctmp[j] = temp[i*size + j];
for( j=0; j<size; j++) temp[i*size + j] =
ctmp[size-1-j];
}
delete [] ctmp;
}
return( 1 );
} /* end tread */
/*--------------------- treadFloatPix() -------------------*/
/*
read in the current image file opend by topen()
(i.e. topen() must be called before this routine)
--- assumed to be a floating point image made by
tcreateFloatPixFile()
this routine returns +1 for success
and </= 0 for failure
NOTE: this routine makes some non-standard assumptions
about the image file (i.e. floating point pixel data
in a specific order - as created by tcreatFloatPix)
return +1 for success, 0 or negative for fatal errors
and >+1 for non-fatal errors
started 25-mar-1996 ejk
converted to class 17-nov-2003 ejk
change param length to PMAX (=2048 in .hpp) 18-mar-2012 ejk
fix maximum # param test 5-apr-2012 ejk
*/
int floatTIFF::treadFloatPix( )
{
float *FStripBuf;
uslong ix, iy, iyi, iyf, nbytes;
int istrip, BytesPerPixel, status, np;
/* check that this is a floating point image */
if( (BitsPerSample[0] != 8*sizeof(float)) || (SampleFormat!=3) ) {
tifferr("floatTIFF::treadFloatPix cannot find floating point image.");
return( -2 );
}
if( nstrips != nstripcounts ) {
tifferr( "nstrips and nstripcounts do not agree in floatTIFF::treadFloatPix.");
/* return( -3 ); leave return for rigorous TIFF */
}
if( SamplesPerPixel != 1 ) {
tifferr("floatTIFF::treadFloatPix cannot handle more than 1 sample/pixel.");
return( -4 ); }
if( (nxl < (signed) ImageWidth ) || ( nyl < (signed) ImageLength ) ) {
tifferr("Pix array size too small in treadFloatPix.");
return( -5 ); }
BytesPerPixel = sizeof(float);
/* if the image is properly broken into strips then
use them, otherwise read it in one row at a time
to minimize the size of the intermediate storage
buffer so that this will work on DOS type machines too */
if( nstrips > 1 )
nbytes = RowsPerStrip * ImageWidth;
else
nbytes = ImageWidth;
FStripBuf = new float[ nbytes ];
if( NULL == FStripBuf ) {
tifferr( "floatTIFF::treadFloatPix unable to allocate pix temporary memory.");
return( -6 ); }
nbytes = nbytes * BytesPerPixel;
if( nstrips > 1 ) { /* use stipes if present */
iyi = 0;
for( istrip=0; istrip<nstrips; istrip++ ) {
if( tread( FStripBuf, BytesPerPixel,
(int) (StripByteCounts[istrip]/BytesPerPixel),
(long32) StripOffsets[istrip] ) != 1) {
delete [] FStripBuf;
tifferr("Bad file read in treadFloatPix.");
return( -7 ); }
iyf = iyi + RowsPerStrip;
if( iyf > ImageLength ) iyf = ImageLength;
for( iy=iyi; iy<iyf; iy++) {
for( ix=0; ix<ImageWidth; ix++)
operator()(ix,iy) = FStripBuf[ix + (iy-iyi)*ImageWidth ];
}
iyi = iyf;
} /* end for istrip loop */
} else { /* otherwise break it up into rows */
tfp.seekg( StripOffsets[0], ios_base::beg );
if( tfp.fail() ) {
delete [] FStripBuf;
tifferr("Bad file seek in floatTIFF::treadFloatPix.");
return( -8 ); }
for( iy=0; iy<ImageLength; iy++ ) {
if( tread( FStripBuf, BytesPerPixel, /* -1 = don't seek */
(int)(nbytes/BytesPerPixel), -1L ) != 1 ) {
delete [] FStripBuf;
tifferr("Bad file read in floatTIFF::treadFloatPix");
return( -9 ); }
for( ix=0; ix<ImageWidth; ix++) operator()(ix,iy) = FStripBuf[ix];
}
} /* end if nstrip */
delete [] FStripBuf;
/* read the parameter array from the 3rd IFD */
if( NextIFD > 0 ) {
if( status=treadIFD( NextIFD ) < 1 ) {
tifferr("Cannot read parameter IFD in floatTIFF::treadFloatPix.");
return( +2 ); /* wrong but not fatal */
}
} else {
tifferr("No parameters available in floatTIFF::treadFloatPix.");
return( +3 ); /* wrong but non-fatal */
}
if( (BitsPerSample[0] != 8*sizeof(float)) || (SampleFormat!=3) ) {
tifferr("floatTIFF::treadFloatPix cannot read parameters.");
return( +4 ); /* wrong but not fatal */
}
np = (int) StripByteCounts[0]/BytesPerPixel; // number of parameters in file
if( PMAX < np ) np = PMAX; // read up to PMAX parameters into param[] array
if( tread( param, BytesPerPixel, np,
(long32) StripOffsets[0] ) != 1) {
tifferr("Bad file read in floatTIFF::treadFloatPix.");
return( -10 ); }
npix = (int) ( param[0] + 0.5 );
/* all done */
return( 1 ); /* success */
} /* end treadFloatPix */
/*--------------------- treadIFD() -------------------*/
/*
read the tiff directory structure (IFD)
this is called by topen()
return 1 for success and </=0 for failure
for internal use only
DO NOT CALL FROM MAIN PROGRAM
*/
int floatTIFF::treadIFD( uslong IFDoffset )
{
usshort n1, *stemp;
int i, j, n, ierr;
uslong ltemp;
string s, s2; // temp storage for error text
stringstream ss;
tread( &n1, 2, 1, IFDoffset);
n = (int) n1;
if( nIFD <= 0 ) ifd1 = (IFD*) new IFD[ n ];
else if( n > nIFD ) {
delete [] ifd1;
ifd1 = (IFD*) new IFD[ n ];
}
if( ifd1 == NULL ){
tifferr( "Unable to allocate memory in treadIFD(1).");
return( -1 ); }
nIFD = short(n);
for( i=0; i<nIFD; i++ ){
tread( &ifd1[i].tag , 2, 1, -1L );
tread( &ifd1[i].type , 2, 1, -1L );
tread( &ifd1[i].length, 4, 1, -1L );
/* to get word order right */
if( (ifd1[i].type == 3) && (ifd1[i].length <= 2 ) )
tread( &ifd1[i].value, 2, 2, -1L );
else
tread( &ifd1[i].value, 4, 1, -1L );
}
stemp = (usshort*) <emp; /* for data type 3 separations */
tread( &NextIFD, 4, 1, -1L); /* point to next IFD */
/* set defaults */
BitsPerSample[0] = 1;
BitsPerSample[1] = 0;
BitsPerSample[2] = 0;
Compression = 1; /* no compression */
DateTime= "No-Date-Given";
FillOrder = 1;
MaxSampleValue = 1; /* tag no longer recommended */
MinSampleValue = 0; /* tag no longer recommended */
NewSubFileType = 0;
nstrips = 0;
nstripcounts = 0;
Orientation = 1; /* tags not recommended */
PhotometricInterpretation = 1; /* 0=black, 1+=white */
PlanarConfiguration = 1;
Predictor = 1;
ResolutionUnit = 2;
RowsPerStrip = 0; /* default is really to infinite- see below */
SamplesPerPixel = 1;
SampleFormat = 1; /* default to unsigned integer */
SubfileType = 0; /* tag no longer recommended */
XPosition[0] = 0;
XPosition[1] = 0;
YPosition[0] = 0;
YPosition[1] = 0;
XResolution[0] = 0;
XResolution[1] = 0;
YResolution[0] = 0;
YResolution[1] = 0;
for( i=0; i<nIFD; i++ ) { ierr = 0;
switch ( ifd1[i].tag ) {
case 254: /* NewSubfileType tag */
if( (ifd1[i].type != 4) ||
(ifd1[i].length != 1 ) ) {
ierr = -254; break; }
NewSubfileType = ifd1[i].value;
break;
case 255: /* SubfileType tag */
if( (ifd1[i].type != 3) || (ifd1[i].length != 1 ) )
{ ierr = -255; break; }
ltemp = ifd1[i].value;
SubfileType = stemp[0];
break;
case 256: /* ImageWidth tag */
if((ifd1[i].length != 1 ) ) {
ierr = -256; break; }
if( ifd1[i].type == 3 ) { /* type = short */
ltemp = ifd1[i].value;
ImageWidth = stemp[0]; }
else if( ifd1[i].type == 4 ) /* type = long */
ImageWidth = ifd1[i].value;
else { ierr = -256; break; }
break;
case 257: /* ImageLength tag */
if((ifd1[i].length != 1 ) ) {
ierr = -257; break; }
if( ifd1[i].type == 3 ) { /* type = short */
ltemp = ifd1[i].value;
ImageLength = stemp[0]; }
else if( ifd1[i].type == 4 ) /* type = long */
ImageLength = ifd1[i].value;
else { ierr = -257; break; }
break;
case 258: /* BitsPerSample tag */
if( (ifd1[i].type != 3) ||
(ifd1[i].length < 1 ) || (ifd1[i].length > 3) ) {
ierr = -258;
ss << "BitsPerSample type= " << ifd1[i].type << ", length= "
<< ifd1[i].length << endl;
s = ss.str();
break; }
if( ifd1[i].length == 1 ) {
ltemp = ifd1[i].value;
BitsPerSample[0] = stemp[0];
if( (BitsPerSample[0] < 1)
|| (BitsPerSample[0] > 32 ) ) {
ss << "Bad BitsPerSample = " << BitsPerSample[0]
<< ", will try 8." << endl;
s2 = ss.str();
tifferr( s2 );
BitsPerSample[0] = 8; }
}
else if( ifd1[i].length == 2 ) {
ltemp = ifd1[i].value;
BitsPerSample[0] = stemp[0];
BitsPerSample[1] = stemp[1]; }
else if( ifd1[i].length == 3 )
tread( BitsPerSample, 2, 3, ifd1[i].value );
break;
case 259: /* Compression tag */
if( (ifd1[i].type != 3) ||
(ifd1[i].length != 1 ) ) {
ierr = -259;
ss << "Compression type= " << ifd1[i].type <<
", length= " << ifd1[i].length << endl;
s = ss.str();
break; }
ltemp = ifd1[i].value;
Compression = stemp[0];
break;
case 262: /* PhotometricInterpretation tag */
if( (ifd1[i].type != 3) ||
(ifd1[i].length != 1 ) ) {
ierr = -262; break; }
ltemp = ifd1[i].value;
PhotometricInterpretation = stemp[0];
break;
case 266: /* FillOrder tag */
if( (ifd1[i].type != 3) ||
(ifd1[i].length != 1 ) ) {
ierr = -266; break; }
ltemp = ifd1[i].value;