📄 image.cpp
字号:
// Image.cpp: implementation of the CImage class.
//
//////////////////////////////////////////////////////////////////////
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include "Image.h"
//char m_strFilename[50]; // global variabl, the name of input image.
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CImage::CImage()
{
}
CImage::~CImage()
{
}
void CImage::LoadDimensionOfImage(long &m_lWidth, long &m_lHeight)
{
long m_lDataoffset=0;
int m_nBitcount=8;
int m_byteswap=0;
int i=0;
DecodeTIFF(m_lWidth, m_lHeight, m_lDataoffset, m_nBitcount, m_byteswap);
}
void CImage::ReadTIFFImage(long &m_lWidth, long &m_lHeight, float InputImage[])
{
long m_lDataoffset=0;
int m_nBitcount=8;
int m_byteswap=0;
int i=0;
BYTE *m_p1=NULL;
cout<<"nammmmme ="<<m_strFilename<<endl;
DecodeTIFF(m_lWidth, m_lHeight, m_lDataoffset, m_nBitcount, m_byteswap);
m_p1 =(BYTE*)malloc(m_lWidth * m_lHeight * long(m_nBitcount / 8.0));
if(m_p1 == NULL)
{
cout<<"Problem in memory allocation for m_p1 ...";
getchar();
exit(1);
}
if (! Open(m_lWidth, m_lHeight, m_lDataoffset, m_nBitcount, m_byteswap, m_p1))
{
cout<<"Problem in the lecture image: Function Open."<<endl;
getchar();
exit(1);
}
for (i=0; i<m_lWidth*m_lHeight; i++)
InputImage[i]= (float)m_p1[i];
}
//**********************Open file*********************************
//****************************************************************
// Input: strFilename;
// Output: m_type, m_lWidth, m_lHeight, m_pData,
//
//
//
int CImage::Open(long m_lWidth, long m_lHeight, long m_lDataoffset, int m_nBitcount, int m_byteswap, BYTE m_p[])
//int Open(char *m_strFilename, long &m_lWidth, long &m_lHeight, BYTE *m_pData, BYTE m_p[])
{
//void Load(long m_lWidth, long m_lHeight, BYTE *m_pData, long m_lDataoffset, int m_nBitcount);
BYTE filetype;
FILE *fp1 = NULL;
BYTE *m_pData=NULL;
int i=0;
/* Get first identifying byte */
fp1 = fopen(m_strFilename, "rb");
if(fp1 == NULL)
{
cout<<"unable to open the file"<<endl;
return 0;
}
filetype = fgetc(fp1)&0xff;
fclose(fp1);
/* Check the value */
switch(filetype)
{
case 0x49:
/* TIFF */
{
m_byteswap = 0;
m_pData =(BYTE*)malloc(m_lWidth * m_lHeight * long(m_nBitcount / 8.0));
if(m_pData == NULL)
{
cout<<"encountered a problem allocating a block of memory"<<endl;
return 0;
}
Load(m_lWidth, m_lHeight, m_pData, m_lDataoffset, m_nBitcount);
for (i=0; i<m_lWidth * m_lHeight; i++)
m_p[i]=m_pData[i];
return 1;
}
default:
{
cout<<"Unknown or unsupported image file format"<<endl;
return 0;
}
}
}
//**************Decod Tif image***************************
//********************************************************
void CImage::DecodeTIFF(long &m_lWidth, long &m_lHeight, long &m_lDataoffset, int &m_nBitcount, int &m_byteswap)
{
FILE *pfile = NULL;
short sTag, sTagtype, sTagcount, i, j;
long lTaglength, lOldoffset;
unsigned long *pulStripoffsets = NULL;
short sImagetype, sOrder, sCompression;
long g_lStripcount, lIfdoffset;
if (m_lWidth == 0) // first tme
{
cout<<"Please enter name of tif image: ";
cin>> m_strFilename;
}
/* Read input file */
pfile = fopen(m_strFilename, "rb");
if(pfile == NULL)
{
cout<<"unable to open the file, program exit : Function DecodTIFF"<<endl;
exit(1);
}
/* Get the byte g_sOrder and IFD offset */
sOrder = GetShort(pfile, m_byteswap); //#####GetShort
fseek(pfile, 4, 0);
lIfdoffset = GetLong(pfile, m_byteswap); //####GetLong
/* Decode TIFF file */
sCompression = 1;
fseek(pfile, lIfdoffset, 0);
sTagcount = GetShort(pfile, m_byteswap); //#####GetShort
for(i = 0; i < sTagcount; i++)
{
sTag = GetShort(pfile, m_byteswap); //#####GetShort
switch(sTag)
{
case 0x100: /* Image width */
fseek(pfile, 6, 1);
m_lWidth = GetShort(pfile, m_byteswap);//#####GetShort
fseek(pfile, 2, 1);
cout<<"m_lWidth= "<<m_lWidth<<endl;
break;
case 0x101: /* Image height */
fseek(pfile, 6, 1);
m_lHeight = GetShort(pfile, m_byteswap); //#####GetShort
fseek(pfile, 2, 1);
cout<<"m_lHeight"<<m_lHeight<<endl;
break;
case 0x102: /* Bits per pixel */
fseek(pfile, 6, 1);
m_nBitcount = GetShort(pfile, m_byteswap); //#####GetShort
fseek(pfile, 2, 1);
cout<<"m_nBitcount= "<<m_nBitcount<<endl;
break;
case 0x103: /* Compression */
fseek(pfile, 6, 1);
sCompression = GetShort(pfile, m_byteswap); //#####GetShort
fseek(pfile, 2, 1);
cout<<"sCompression= "<<sCompression<<endl;
break;
case 0x106: /* Image type */
fseek(pfile, 6, 1);
sImagetype = GetShort(pfile, m_byteswap); //#####GetShort
fseek(pfile, 2, 1);
cout<<"sImagetype = "<<sImagetype<<endl;
break;
case 0x111:
/* IFD */
sTagtype = GetShort(pfile, m_byteswap);
lTaglength = GetLong(pfile, m_byteswap);
m_lDataoffset = GetLong(pfile, m_byteswap); //#####GetShort
if(lTaglength != 1)
{
g_lStripcount = lTaglength;
lOldoffset = ftell(pfile);
fseek(pfile, m_lDataoffset, 0);
pulStripoffsets =
(unsigned long *)malloc(lTaglength *sizeof(unsigned long));
if(pulStripoffsets == NULL)
{
cout<<"encountered a problem allocating a block of memory"<<endl;
exit(1);
}
for(j = 0; j < lTaglength; j++)
{
pulStripoffsets[j] = GetLong(pfile, m_byteswap); //#####GetLong
}
fseek(pfile, lOldoffset, 0);
m_lDataoffset = pulStripoffsets[0];
}
break;
default:
fseek(pfile, 10, 1);
break;
}
}
/* Check TIFF type */
if(sCompression != 1)
{
cout<<"Cannot read compressed TIFF file"<<endl;
exit(1);
}
if(sImagetype == 2)
{
cout<<"RGB TIFFs are not supported"<<endl;
exit(1);
}
if(pulStripoffsets != NULL)
{
free(pulStripoffsets);
}
if(m_nBitcount == 0)
{
cout<<"Pixel size is undefined, assuming 8-bit"<<endl;
m_nBitcount = 8;
}
fclose(pfile);
cout<<"open Cimage: before"<<endl;
cout<<"Open Cimage: after\n"<<endl;
cout<< " before : m_lWidth ="<<m_lWidth<<endl;
cout<<" before: m_lHeight ="<<m_lHeight<<endl;
cout<<" before : m_lDataoffset ="<<m_lDataoffset<<endl;
cout<<" before :m_nBitcount = "<<m_nBitcount<<endl;
cout<<" before : m_byteswap = "<<m_byteswap<<endl;
}
//**************GetLong***************************
//************************************************
long CImage::GetLong(FILE *pfile, int m_byteswap)
{
BYTE byte1 = fgetc(pfile)&0xff;
BYTE byte2 = fgetc(pfile)&0xff;
BYTE byte3 = fgetc(pfile)&0xff;
BYTE byte4 = fgetc(pfile)&0xff;
if(m_byteswap)
{
return(byte4 + (byte3 << 8) + (byte2 << 16) + (byte1 << 24));
}
else
{
return(byte1 + (byte2 << 8) + (byte3 << 16) + (byte4 << 24));
}
}
//**************Get Short*********************
//********************************************
short CImage::GetShort(FILE *pfile, int m_byteswap)
{
BYTE byte1 = fgetc(pfile)&0xff;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -