求高手帮忙看看程序:读一系列的tiff文件
我想要把一系列的tiff图片(8位)读到image里面,程序运行后得到的文件是黑的,求高手帮忙!!
#include <stdio.h>
#include "Windows.h"
#include <string.h>
//几个全局变量,存放读入图像的位图数据、宽、高、颜色表及每像素所占位数(比特)
//此处定义全局变量主要为了后面的图像数据访问及图像存储作准备
unsigned char *pBmpBuf;//读入图像数据的指针
unsigned char *image;//存储图像数据的指针
//RGBQUAD *color;//存储图像颜色表项目;
int bmpWidth;//图像的宽
int bmpHeight;//图像的高
RGBQUAD *pColorTable;//颜色表指针
int biBitCount;//图像类型
unsigned long int size_x;
unsigned long int size_y;
unsigned long int size_z;
unsigned long int size_xy;
unsigned long int size_xyz;
//unsigned long int sliceNum;
/*========= function read_image =========*/
/* -------------
considered global variables:
NONE
changed global variables:
sliceNum
size_x
size_y
size_z
size_xy
size_xyz
image
----------------*/
/***********************************************************************
* 函数名称:
* readBmp()
*
*函数参数:
* char *bmpName -文件名字及路径
*
*返回值:
* 0为失败,1为成功
*
*说明:给定一个图像文件名及其路径,读图像的位图数据、宽、高、颜色表及每像素
* 位数等数据进内存,存放在相应的全局变量中
***********************************************************************/
bool readBmp(char *bmpName,int sliceNum)
{
//读入指定BMP文件进内存
char readPath[100];
//char Path[]="D:\\QJ.Xiang\\process program\\d6c_pk_source\\dog_";
char num[]="0000";
char str[4];
unsigned long int i, p, q, pointNum;
/*获取三维图片数据的存储空间,及其相同的参数信息*/
//二进制读方式打开指定的图像文件
memset(readPath,0,sizeof(readPath)); //重新初始化readPath
strcpy(readPath,bmpName);
strcat(readPath,num);
strcat(readPath,".BMP");//获取图像路径
FILE *fp_inp_img=fopen(readPath,"rb");
if(fp_inp_img==0) return 0;
//跳过位图文件头结构BITMAPFILEHEADER
fseek(fp_inp_img, sizeof(BITMAPFILEHEADER),0);
//定义位图信息头结构变量,读取位图信息头进内存,存放在变量head中
BITMAPINFOHEADER head;
fread(&head, sizeof(BITMAPINFOHEADER), 1,fp_inp_img);
fclose(fp_inp_img);
//获取图像宽、高、每像素所占位数等信息
bmpWidth = head.biWidth;
bmpHeight= head.biHeight;
biBitCount=head.biBitCount;
if ( biBitCount !=8 )//处理的图像位数必须是8位
{
printf("ERROR: Cannot handle %d depth in image data \n", biBitCount);
exit(-1);
}
//定义变量,计算图像每行像素所占的字节数(windows规定扫描行所占字节数必须是4的倍数)
int lineByte=(bmpWidth * biBitCount/8+3)/4*4;
/* set dimensions */
size_x = lineByte;//位图宽度,以像素为单位
size_y = head.biHeight;//位图高度,以像素为单位
size_z=sliceNum;//the whole slice number
//if ( size_z <= 1 )
//{
// printf("ERROR: The image data is not a real 3D one. Number of slices is %d \n", size_z);
// exit(-1);
//}
printf("\n Size of the input image: %d, %d, %d\n", size_x, size_y, size_z);
/* add frame */
size_x += 2;
size_y += 2;
size_z += 2;
/* set slice- and image size */
size_xy = size_x * size_y;
size_xyz = size_xy * size_z;
printf("\n Size of the volume in processing: %d\n", size_xyz);
/* alloc image */
pColorTable=new RGBQUAD[256*sliceNum];//颜色表项大小为1024
printf("\n Size of the pColorTable: %d byte\n", sizeof(pColorTable));
image = (unsigned char *)malloc(size_xyz);
if ( image == NULL )
{
printf("\n Alloc. error (image)");
exit(0);
}
/* init image */
for (int i=0; i<size_xyz; i++)
*(image +i) = 0;
/* read image & color*/
p = size_xy + size_x + 1;//由于三维图像表面扩展了一层,因此要使指针指向存放图像的正确位置
q = 0;
for (int i=0; i<sliceNum; i++)
{
memset(readPath,0,sizeof(readPath)); //重新初始化readPath
strcpy(readPath,bmpName);
itoa(i,str,10);
sprintf(num,"%04s", str);
strcat(readPath,num);
strcat(readPath,".BMP");//获取图像路径
FILE *fp_inp_img=fopen(readPath,"rb");
if(fp_inp_img==0) return 0;
//跳过位图文件头结构BITMAPFILEHEADER
fseek(fp_inp_img, sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER),0);//If successful, the function returns a zero value.Otherwise, it returns nonzero value.
//申请颜色表所需要的空间,读颜色表进内存
fread(pColorTable+q,sizeof(RGBQUAD),256,fp_inp_img);
fread(image+p, 1, lineByte*bmpHeight, fp_inp_img) ;//一次性读一整幅图
//printf("lineByte=%d,size_x-2=%d\n",lineByte,size_x-2);
q+=256;
p+=size_x*size_y;
}
fclose(fp_inp_img);
/* counting object points and set 1 */
pointNum = 0;
for (i=0; i<size_xyz; i++)
if ( *(image +i) )// if *(image +i) !=0,set it=1,二值化
{
*(image +i) = 1;
pointNum++;
}
printf("\n Number of object points in the original image: %d\n", num);
return 1;
}
/***********************************************************************
* 函数名称:
* saveBmp()
*
*函数参数:
* char *bmpName -文件名字及路径
* unsigned char *imgBuf -待存盘的位图数据
* int width -像素为单位待存盘位图的宽
* int height -像素为单位待存盘位图高
* int biBitCount -每像素所占位数
* RGBQUAD *pColorTable -颜色表指针
*返回值:
* 0为失败,1为成功
*
*说明:给定一个图像位图数据、宽、高、颜色表指针及每像素所占的位数等信息,
* 将其写到指定文件中
***********************************************************************/
bool saveBmp(char *bmpName, unsigned char *image, int sliceNum, int width, int height,
int biBitCount,RGBQUAD *color)
{
//如果位图数据指针为0,则没有数据传入,函数返回
if(!image)
return 0;
//颜色表大小,以字节为单位,灰度图像颜色表为1024字节,彩色图像颜色表大小为0
int colorTablesize=0;
if(biBitCount==8)
colorTablesize=256*sizeof(RGBQUAD);//1024byte
//待存储图像数据每行字节数为4的倍数
int lineByte=(width * biBitCount/8+3)/4*4;
//读入指定BMP文件进内存
char writePath[100];
memset(writePath,0,sizeof(writePath)); //重新初始化writePath
//char Path[]="D:\\QJ.Xiang\\process program\\d6c_pk_source\\dog_";
char num[]="0000";
char str[4];
unsigned long int p, q, pointNum;
/*获取三维图片数据的存储空间,及其相同的参数信息*/
//二进制读方式打开指定的图像文件
q=0;
p = size_xy + size_x + 1;//由于三维图像表面扩展了一层,因此要使指针指向存放图像的正确位置
for(int i=0;i<sliceNum;i++)
{
memset(writePath,0,80); //重新初始化readPath
strcpy(writePath,bmpName);
itoa(i,str,10);
sprintf(num,"%04s", str);
//strcpy(writePath,bmpName);
strcat(writePath,num);
strcat(writePath,".BMP");//获取图像路径
//以二进制写的方式打开文件
FILE *fp=fopen(writePath,"wb");
if(fp==0) return 0;
//申请位图文件头结构变量,填写文件头信息
BITMAPFILEHEADER fileHead;
fileHead.bfType = 0x4D42;//bmp类型
//bfSize是图像文件4个组成部分之和
fileHead.bfSize= sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)
+ colorTablesize + lineByte*height;
fileHead.bfReserved1 = 0;
fileHead.bfReserved2 = 0;
//bfOffBits是图像文件前三个部分所需空间之和
fileHead.bfOffBits=54+colorTablesize;
//写文件头进文件
fwrite(&fileHead, sizeof(BITMAPFILEHEADER),1, fp);
//申请位图信息头结构变量,填写信息头信息
BITMAPINFOHEADER head;
head.biBitCount=biBitCount;
head.biClrImportant=0;
head.biClrUsed=0;
head.biCompression=0;
head.biHeight=height;
head.biPlanes=1;
head.biSize=40;
head.biSizeImage=lineByte*height;
head.biWidth=width;
head.biXPelsPerMeter=0;
head.biYPelsPerMeter=0;
//写位图信息头进内存
fwrite(&head, sizeof(BITMAPINFOHEADER),1, fp);
//如果灰度图像,有颜色表,写入文件
fwrite(color+q, sizeof(RGBQUAD),256, fp);
q+=256;
//写位图数据进文件
fwrite(image+p, height*lineByte, 1, fp);
p+=size_x*size_y;
//关闭文件
fclose(fp);
}
/* counting object points and set 1 */
pointNum = 0;
for (unsigned long int i=0; i<size_xyz; i++)
{
if ( *(image +i) )// if *(image +i) !=0,set it=1,二值化 因为8位时数据为0或255
{
*(image +i) = 1;
pointNum++;
}
printf("\n Number of object points in the original image: %d\n", num);
return 1;
}
return 1;
}
//调色板与灰度图像的关系
void main()
{
//读入指定BMP文件进内存
char readPath[]="D:\\QJ.Xiang\\process program\\d6c_pk_source\\dog_";
int sliceNum=1;
/*
char num[]="0000";
char str[4];
for(int i=0;i<2;i++)
{
itoa(i,str,10);
sprintf(num,"%04s", str);
strcpy(readPath,Path);
strcat(readPath,num);
strcat(readPath,".BMP");
printf("readPath:%s\n",readPath);
*/
readBmp(readPath,sliceNum);
//}
//输出图像的信息
//printf("width=%d,height=%d,biBitCount=%d,num=%s\n",bmpWidth,bmpHeight,biBitCount,num);
/*
//改变灰度图像的颜色表蓝色分量的值,察看前后变化
if(biBitCount==8){
for(int i=0; i<256;i++){
pColorTable[i].rgbBlue = 255-pColorTable[i].rgbBlue;
}
}
*/
//将图像数据存盘
char writePath[]="D:\\QJ.Xiang\\process program\\d6c_pk_source\\dogcpy_";
saveBmp(writePath, image, sliceNum, bmpWidth, bmpHeight, biBitCount, pColorTable);
//清除缓冲区,image和pColorTable是全局变量,在文件读入时申请的空间
delete []image;
if(biBitCount==8)
delete []pColorTable;
}