はここについては、ちょうどIFDチェーンを歩く方法
uint16_t TIFFSHORT(unsigned char *buffer, bool bMotorola)
{
uint16_t s;
if (bMotorola)
s = (buffer[0] << 8) + buffer[1];
else
s = buffer[0] + (buffer[1] << 8);
return s;
}
uint32_t TIFFLONG(unsigned char *buffer, bool bMotorola)
{
uint32_t l;
if (bMotorola)
l = (buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3];
else
l = buffer[0] + (buffer[1] << 8) + (buffer[2] << 16) + (buffer[3] << 24);
return l;
}
int TIFFPageCount(char *filename)
{
int i, iTags, IFD;
int iOffset, iTotalPages;
unsigned char buf[8];
FILE *handle;
bool bMotorola;
int iFileSize;
handle = fopen((char *)filename, "rb");
if (handle == NULL)
return 0;
// get the file size
fseek(handle, 0, SEEK_END);
iFileSize = (int)ftell(handle);
fseek(handle, 0, SEEK_SET);
i = fread(buf, 1, 8, handle); // Read TIFF header and first IFD offset
if (buf[0] != 'I' && buf[0] != 'M')
{
fclose(handle);
return 0; // Not a TIFF file
}
bMotorola = (buf[0] == 'M'); // get the byte order
IFD = TIFFLONG(&buf[4], bMotorola); // Read the first IFD pointer
if (IFD < 8 || IFD > iFileSize) // corrupt file, don't process it
{
fclose(handle);
return 0;
}
iTotalPages = 0;
while(IFD != 0 && IFD < iFileSize-4) // count the number of pages
{
fseek(handle, IFD, SEEK_SET);
fread(buf, 1, 2, handle); // get the number of tags in this page
iTags = TIFFSHORT(buf, bMotorola); // Number of tags in this dir
i = iTags * 12 + 2; // Offset to next IFD in chain
if (iTags > 256) // Invalid header, abort
break;
fseek(handle, IFD+i, SEEK_SET);
fread(buf, 1, 4, handle); // read the next IFD value; 0 means end of chain
IFD = TIFFLONG(buf, bMotorola);
iTotalPages++;
}
fclose(handle);
return iTotalPages;
} // TIFFPageCount()
TIFFファイル内のページ数をカウントするために、一般的なC言語の関数ですか?それは非常にシンプルで、CreateFile()、ReadFile()以上のものを必要としません。 – BitBank
BitBankに感謝します。 しかし、私はTIFFファイルについて何も知らなかったので、元々は "IFDチェーン"の意味を知らなかった。私は最終的にTIFFファイル形式のいくつかのWebページを読んだ後、あなたが話していたことを見つけました。 –
申し訳ありませんが私は私の応答でとても簡潔でした。次回誰かが尋ねると、私はコードサンプルを提供します。 – BitBank