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