00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "contributors.h"
00016
00017 #include "global.h"
00018 #include "report.h"
00019 #include "io_tiff.h"
00020
00021 int64 bufferSize = 0;
00022
00023
00024
00025
00026
00027
00028
00029
00030 void OpenTiffFile( VideoDataFile *input_file, InputParameters *p_Inp, int FrameNumberInFile)
00031 {
00032 char infile [FILE_NAME_SIZE], in_number[16];
00033 int length = 0;
00034
00035 if (input_file->num_digits > 0)
00036 {
00037 in_number[length]='\0';
00038 length = strlen(input_file->fhead);
00039 strncpy(infile, input_file->fhead, length);
00040 infile[length]='\0';
00041 if (input_file->zero_pad)
00042 snprintf(in_number, 16, "%0*d", input_file->num_digits, FrameNumberInFile);
00043 else
00044 snprintf(in_number, 16, "%*d", input_file->num_digits, FrameNumberInFile);
00045
00046 strncat(infile, in_number, sizeof(in_number));
00047 length += sizeof(in_number);
00048 infile[length]='\0';
00049 strncat(infile, input_file->ftail,strlen(input_file->ftail));
00050 length += strlen(input_file->ftail);
00051 infile[length]='\0';
00052 }
00053 else
00054 {
00055 strcpy(infile, input_file->fname);
00056 }
00057
00058 if ((input_file->f_num = open(infile, OPENFLAGS_READ)) == -1)
00059 {
00060 printf ("OpenTiffFile: cannot open file %s\n", infile);
00061 report_stats_on_error();
00062 }
00063 }
00064
00065
00066
00067
00068
00069
00070
00071
00072 int AllocateTIFFBufferMemory (unsigned char *buf, int buffersize)
00073 {
00074 if (NULL == (buf = (unsigned char *) realloc (buf, buffersize)))
00075 return (1);
00076 else
00077 return (0);
00078 }
00079
00080
00081
00082
00083
00084
00085
00086
00087 void DeleteTiffBufferMemory (unsigned char *buf)
00088 {
00089 if (buf)
00090 {
00091 free (buf);
00092 buf = NULL;
00093 }
00094 }
00095
00096
00097
00098
00099
00100
00101
00102
00103 static int64 ReadTIFFSize(int video_file)
00104 {
00105 int64 fsize;
00106
00107 lseek(video_file, 0, SEEK_END);
00108 fsize = tell((int) video_file);
00109 lseek(video_file, 0, SEEK_SET);
00110
00111 return fsize;
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121 static void ReadTIFFHeader (int vfile, TIFFHeader *tiff)
00122 {
00123 if (read(vfile, tiff, sizeof(TIFFHeader)) != sizeof(TIFFHeader))
00124 {
00125 printf ("ReadTIFFHeader: cannot read header info from input file. Exiting...\n");
00126 report_stats_on_error();
00127 }
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137 static void ReadTIFFIFDEntry (int vfile, TIFFHeader *tiffHeader, uint16 *ifd_count, TIFFIFDEntry **tiffIFD)
00138 {
00139 int i;
00140
00141
00142
00143 if (lseek (vfile, tiffHeader->IFDoff, SEEK_SET) == -1)
00144 {
00145 error ("ReadTIFFIFDEntry: cannot lseek to first IDF entry", -1);
00146 }
00147
00148 if (read(vfile, ifd_count, sizeof(uint16)) != sizeof(uint16))
00149 {
00150 printf ("ReadTIFFIFDEntry: cannot read number of IFD entries from input file. Exiting...\n");
00151 report_stats_on_error();
00152 }
00153
00154 if ((*tiffIFD = (TIFFIFDEntry *) malloc (*ifd_count* sizeof(TIFFIFDEntry))) == NULL)
00155 {
00156 printf ("ReadTIFFIFDEntry: cannot allocate memory for IFD entries. Exiting...\n");
00157 report_stats_on_error();
00158 }
00159
00160 for (i = 0; i < *ifd_count; i++)
00161 {
00162 if (read(vfile, &(*tiffIFD)[i], sizeof(TIFFIFDEntry)) != sizeof(TIFFIFDEntry))
00163 {
00164 printf ("ReadTIFFIFDEntry: cannot read IFD entry from input file. Exiting...\n");
00165 report_stats_on_error();
00166 }
00167 }
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177 static void ParseTIFFIFD (uint16 ifd_count, TIFFIFDEntry *tiffIFD, FrameFormat *source)
00178 {
00179 int i;
00180
00181 for (i = 0; i < ifd_count; i++)
00182 {
00183 switch (tiffIFD[i].tIFD_tag)
00184 {
00185 case TIFFTAG_COMPRESSION :
00186 if (tiffIFD[i].tIFD_count != 1)
00187 {
00188 printf ("ParseTIFFIFD: Compressed TIFF data not supported. Exiting...\n");
00189 report_stats_on_error();
00190 }
00191 break;
00192 case TIFFTAG_IMAGEWIDTH :
00193 if (tiffIFD[i].tIFD_offset != (unsigned int) source->width)
00194 {
00195 printf ("ParseTIFFIFD: Tiff width (%d) different from encoder input width (%d) . Exiting...\n", tiffIFD[i].tIFD_offset, source->width);
00196 report_stats_on_error();
00197 }
00198 break;
00199 case TIFFTAG_IMAGELENGTH :
00200 if (tiffIFD[i].tIFD_offset != (unsigned int) source->height)
00201 {
00202 printf ("ParseTIFFIFD: Tiff height different from encoder input height. Exiting...\n");
00203 report_stats_on_error();
00204 }
00205 break;
00206 case TIFFTAG_BITSPERSAMPLE :
00207 if (tiffIFD[i].tIFD_count != 3)
00208 {
00209 printf ("ParseTIFFIFD: Only 3 channel TIFF files supported. Exiting...\n");
00210 report_stats_on_error();
00211 }
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233 break;
00234 default:
00235 break;
00236 }
00237 }
00238 }
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259 void ReadTIFFImage (InputParameters *p_Inp, VideoDataFile *input_file, int FrameNoInFile, FrameFormat *source, unsigned char *buf)
00260 {
00261 int64 fileSize = 0;
00262 int f_num;
00263
00264 TIFFHeader tiffHeader;
00265 TIFFIFDEntry *tiffIFD = NULL;
00266 uint16 ifd_count = 0;
00267
00268 OpenTiffFile( input_file, p_Inp, FrameNoInFile + p_Inp->start_frame);
00269 f_num = input_file->f_num;
00270
00271 fileSize = ReadTIFFSize(f_num);
00272
00273 fileSize = source->size;
00274
00275 ReadTIFFHeader (f_num, &tiffHeader);
00276 ReadTIFFIFDEntry (f_num, &tiffHeader, &ifd_count, &tiffIFD);
00277 ParseTIFFIFD (ifd_count, tiffIFD, source);
00278 #define DEBUG_TIF
00279 #ifdef DEBUG_TIF
00280 {
00281 int i;
00282 for (i = 0; i < ifd_count; i++)
00283 printf("Value (%d) %d %d %d %d\n", i, tiffIFD[i].tIFD_tag, tiffIFD[i].tIFD_type, tiffIFD[i].tIFD_count, tiffIFD[i].tIFD_offset);
00284 }
00285 #endif
00286
00287 if (fileSize != bufferSize)
00288 {
00289
00290 AllocateTIFFBufferMemory(buf, (int) fileSize);
00291 bufferSize = fileSize;
00292 }
00293
00294
00295 if (read(f_num, buf, (int) fileSize) != (int) fileSize)
00296 {
00297 printf ("ReadTIFFImage: cannot read %d bytes from input file, unexpected EOF, exiting...\n", (int) fileSize);
00298 report_stats_on_error();
00299 }
00300
00301 close(input_file->f_num);
00302
00303 if (tiffIFD != NULL)
00304 free(tiffIFD);
00305 }