如何阅读C中的TIFF文件标题? [英] How to read TIFF File headers in c?

查看:172
本文介绍了如何阅读C中的TIFF文件标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



实际上我想学习TIFF Tag ImageWidth和TIFF Tag ImageLength。



如何访问此属性?

http://www.awaresystems.be/imaging/tiff/tifftags/imagewidth.html
http://www.awaresystems.be/imaging/tiff/tifftags/imagelength.html



这段代码的c翻译可以帮助我:

https://stackoverflow.com/a/9071933/2079158



我不知道c好,

试过这样的东西:

  #includestdio.h
#includestdlib.h
$ b $ main()
{
FILE * f = fopen(tifo.tif,rb);
unsigned char info [500];
fread(info,sizeof(unsigned char),500,f);

long int width = *(long int *)& info [256];
short int height = *(short int *)& info [257];

printf(width:%d \\\
,width);
printf(height:%d \\\
,height);

fclose(f);
}

我可以为tiff文件做什么?

解决方案

我用下面的代码解决了问题:

  #include< stdio.h> 
#includetiffio.h
#include< string.h>
#include< dirent.h>
int main(void)
{
DIR * dp;
struct dirent * ep;
uint32 w,h;
float xdpi,ydpi;

dp = opendir(./);
char file_name [30];
char last [30]; ((stp)(ep-> d_name,
if(dp!= NULL)
{
while(ep = readdir(dp))
{
if .tif)!= NULL)||(strstr(ep-> d_name,.TIF)!= NULL))
{
TIFF * tif = TIFFOpen(ep-> d_name,的 r);
TIFFGetField(tif,TIFFTAG_IMAGEWIDTH,& w);
TIFFGetField(tif,TIFFTAG_IMAGELENGTH,& h);
TIFFGetField(tif,TIFFTAG_XRESOLUTION,& xdpi);
TIFFGetField(tif,TIFFTAG_YRESOLUTION,& ydpi);
$ b printf(%s - >%d x%d |%.f - %.f \\\
,ep-> d_name,w,h,xdpi,ydpi);

strncpy(file_name,ep-> d_name,ep-> d_namlen-4);
file_name [ep-> d_namlen-4] ='\0';

sprintf(last,%s(%.fx%.f).tif,file_name,(float)((w / xdpi)* 2.54),(float)((h / ydpi )* 2.54));
printf(| __%s \\\
\\\
,last);
TIFFClose(tif);

重命名(ep-> d_name,last);
}
}
(void)closedir(dp);
}
else
perror(Directory can not open!);

printf(Succesfully finished!);
getchar();

返回0;
}


How can i read a tiff file header in c ?

actually i want to learn TIFF Tag ImageWidth, and TIFF Tag ImageLength.

how can i acces this attributes?

http://www.awaresystems.be/imaging/tiff/tifftags/imagewidth.html http://www.awaresystems.be/imaging/tiff/tifftags/imagelength.html

the c translation of this code can help me :

https://stackoverflow.com/a/9071933/2079158

i don't know c well,
tried something like this :

#include "stdio.h"
#include "stdlib.h"

main()
{
FILE* f = fopen("tifo.tif", "rb");
unsigned char info[500];
fread(info, sizeof(unsigned char), 500, f); 

long int width = *(long int*)&info[256];
short int height = *(short int*)&info[257];

printf("width : %d \n", width);
printf("height : %d \n", height);

fclose(f);
}

what can i do for tiff files??

解决方案

I solve th probelm with this code :

#include <stdio.h>
#include "tiffio.h"
#include <string.h>
#include <dirent.h>     
int main(void)
{
DIR *dp;
struct dirent *ep;
uint32 w, h;
float xdpi,ydpi;

dp = opendir ("./");
char file_name[30];
char last[30];
if (dp != NULL)
{
    while (ep = readdir (dp))
    {
        if( ( strstr(ep->d_name, ".tif") != NULL ) || ( strstr(ep->d_name, ".TIF") != NULL ) )
        {
            TIFF* tif = TIFFOpen(ep->d_name, "r");
            TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
            TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
            TIFFGetField(tif, TIFFTAG_XRESOLUTION, &xdpi);
            TIFFGetField(tif, TIFFTAG_YRESOLUTION, &ydpi);

            printf("%s --> %d x %d | %.f - %.f \n",ep->d_name, w, h, xdpi,ydpi);

            strncpy ( file_name, ep->d_name, ep->d_namlen-4 );
            file_name[ep->d_namlen-4]='\0';

            sprintf(last,"%s (%.f x %.f).tif", file_name, (float) ((w/xdpi)*2.54) , (float) ((h/ydpi)*2.54) );
            printf("      |__ %s\n\n",last);
            TIFFClose(tif);

            rename(ep->d_name, last);
        }
    }
    (void) closedir (dp);
}
else
    perror ("Directory can not open!");

printf("Succesfully finished!");
getchar();

return 0;
}

这篇关于如何阅读C中的TIFF文件标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆