⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tif.cpp

📁 本光盘包含了《精通Visual C++图像处理编程(第3版)》一书中全部的源代码、示例程序的可执行文件以及一些供图像处理测试用的图像文件。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Tif.cpp: implementation of the CTif class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "tif.h"


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CTif::CTif()
{
  m_lPageCount     = - 1;
  m_lPage          = 0; 
  m_tif            = NULL;
  height           = 0;
  width            = 0;
  bitspersample    = 0;
  samplesperpixel  = 0;
  rowsperstrip     = 0;
  photometric      = 0;
  compression      = 0;
  orientation      = 0;
  x_resolution     = 0;
  y_resolution     = 0;

  x        = 0;
  y        = 0;
  x_offset = 0;
  y_offset = 0;
  m_bIsRGB = FALSE;

  memset( &m_info, 0 , sizeof( m_info ) );
  memset( &m_head, 0 , sizeof( m_head ) );
  m_pbSelection = NULL;
  m_pbAlpha     = NULL;
  m_pDib        = NULL;

  m_pDibObject = NULL;
}

CTif::CTif(CDib *pDib)
{
	m_pDibObject = NULL;
	SetDib(pDib);
}

CTif::~CTif()
{
	if (m_pDibObject != NULL)
		delete m_pDibObject;
}

// Load TIFF file
BOOL CTif::Load(LPCTSTR szFileName)
{
	if (! open((char *)szFileName, "rb"))
		return FALSE;

	if (! DecompressImage())
		return FALSE;

    // Allocate enough memory for the new CF_DIB, and copy bits 
	DWORD dwHeaderSize = sizeof(BITMAPINFOHEADER);
	DWORD dwBitsSize = m_head.biSizeImage + (m_head.biClrUsed * sizeof(RGBQUAD));
    HDIB hDIB = GlobalAlloc(GHND, dwHeaderSize + dwBitsSize); 
	if (hDIB == NULL)
		return FALSE;

    LPBYTE lpDIB = (LPBYTE)GlobalLock(hDIB); 
	LPBITMAPINFOHEADER  lpbi;
	lpbi  = (LPBITMAPINFOHEADER)(lpDIB);
	*lpbi = m_head;
	lpbi->biSize = sizeof(BITMAPINFOHEADER);
	LPBYTE lpBits = lpDIB + dwHeaderSize;
	memcpy(lpBits, m_info.pImage, dwBitsSize); 

	if (m_pDibObject != NULL)
		delete m_pDibObject;

	m_pDibObject = new CDib();
	m_pDibObject->Attach(hDIB);

	close();

	return TRUE;
}

// Save TIFF file
BOOL CTif::Save(LPCTSTR lpstrFileName, CDib* pDib)
{
	if (pDib == NULL)
		pDib = m_pDibObject;
	if (pDib == NULL)
		return FALSE;

	HDIB hDib = CopyHandle(pDib->GetHandle());
	if (hDib == NULL)
		return FALSE;

    LPBITMAPINFOHEADER  lpBI;       // Pointer to DIB info structure 
    lpBI = (LPBITMAPINFOHEADER)GlobalLock(hDib); 
    if (!lpBI) 
	{
		GlobalUnlock(hDib);
        return FALSE; 
	}
	UINT32 total_width = lpBI->biWidth;
	GlobalUnlock(hDib);

	CDib* pDibTmp = new CDib;
	pDibTmp->Attach(hDib);
	// convert to 24-bit image
	if (pDibTmp->GetBitCount() != 24)
		pDibTmp->ConvertFormat(24);

	UINT w  = pDibTmp->GetWidth();
	UINT h = pDibTmp->GetHeight();
	UINT32 bitcount = pDibTmp->GetBitCount();
	UINT32 bytecount = bitcount / 8;

	TIFF * tif;
	if ((tif = TIFFOpen(lpstrFileName, "w")) == NULL)
		return FALSE;

    // setup the image tags
	TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, w);
	TIFFSetField(tif, TIFFTAG_IMAGELENGTH, h);
	TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
	TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS);
	TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
	TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
	TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
	TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
	TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE);
	TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);

	unsigned char * psrc = (unsigned char *)pDib->GetBitsPtr();
	unsigned char * pdst = new unsigned char[(w * 3)];

	UINT32 src_index;
	UINT32 dst_index;

	  // now go line by line to write out the image data
	for (UINT row = 0; row < h; row++ )
	{
		  // initialize the scan line to zero
		memset(pdst,0,(size_t)(w * 3));

		  // moving the data from the dib to a row structure that can
		  // be used by the tiff library
		for (UINT col = 0; col < w; col++)
		{
			src_index = (h - row - 1) * total_width * bytecount + col * bytecount;
			dst_index = col * 3;
			pdst[dst_index++] = psrc[src_index+2];
			pdst[dst_index++] = psrc[src_index+1];
			pdst[dst_index] = psrc[src_index];
		}

		  // now actually write the row data
		TIFFWriteScanline(tif, pdst, row, 0);
	}

	TIFFClose(tif);

	return TRUE;
}

/****************************************************************************/
/*                                                                          */
/*   CTif::getdata                                                         */
/*                                                                          */
/****************************************************************************/
void CTif::close()
{   
  try 
  {
    TIFFClose(m_tif); 

    if (m_pbSelection)
      delete[] m_pbSelection;

    if (m_pbAlpha)
      delete[] m_pbAlpha;

    if (m_pDib)
      delete[] m_pDib;
  } 
  catch(...)
  {
  }
};


/****************************************************************************/
/*                                                                          */
/*   CTif::getdata                                                         */
/*                                                                          */
/****************************************************************************/
void CTif::getdata()
{

  rowsperstrip = 0;

  height = 0;
  width = 0;
  bitspersample = 0;
  samplesperpixel = 0;
  photometric = compression  = orientation = 0;
  x = y = 0;
  x_resolution = y_resolution = x_offset = y_offset = 0;
  m_bIsRGB = FALSE;

  TIFFGetField(m_tif, TIFFTAG_COMPRESSION, &compression);
  TIFFGetField(m_tif, TIFFTAG_IMAGEWIDTH, &width);
  TIFFGetField(m_tif, TIFFTAG_IMAGELENGTH, &height);
  TIFFGetField(m_tif, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
  TIFFGetField(m_tif, TIFFTAG_BITSPERSAMPLE, &bitspersample);
  TIFFGetField(m_tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);   
  TIFFGetField(m_tif, TIFFTAG_PHOTOMETRIC, &photometric);
  TIFFGetField(m_tif, TIFFTAG_ORIENTATION, &orientation);
  TIFFGetField(m_tif, TIFFTAG_XRESOLUTION, &x_resolution);
  TIFFGetField(m_tif, TIFFTAG_YRESOLUTION, &y_resolution);
  TIFFGetField(m_tif, TIFFTAG_XPOSITION, &x_offset);
  TIFFGetField(m_tif, TIFFTAG_XPOSITION, &y_offset);

  m_bIsRGB = (bitspersample >= 8) 
                      &&
             (photometric == PHOTOMETRIC_RGB) 
                      ||
             (photometric == PHOTOMETRIC_YCBCR) 
                      ||
             (photometric == PHOTOMETRIC_SEPARATED) 
                      ||
             (photometric == PHOTOMETRIC_LOGLUV);

  //
  //  default to 200 dpi if no res
  //
  if (!x_resolution)
    x_resolution = 200;

  if (!y_resolution)
    y_resolution = 200;


}

/****************************************************************************/
/*                                                                          */
/*   CTif::pagecount                                                       */
/*                                                                          */
/****************************************************************************/
long CTif::pagecount()
{
  return m_lPageCount;
}


/****************************************************************************/
/*                                                                          */
/*  CTif::isFax                                                            */
/*                                                                          */
/****************************************************************************/
int CTif::isFax()
{
  return (compression == COMPRESSION_CCITTFAX3 ||
      compression == COMPRESSION_CCITTFAX4 );
}

/****************************************************************************/
/*                                                                          */
/*  CTif::open                                                             */
/*                                                                          */
/****************************************************************************/
BOOL CTif::open( char* szTiff, char * szMode ) 
{
  char szM[ 10 ];

  if (szMode) {
    memset( szM, 0 , sizeof(char) * 10 );
    memcpy( szM, szMode, lstrlen( szMode ) );
  } else {
    szM[ 0 ] = 'r';
    szM[ 1 ] = 'b';
  }

  m_tif = TIFFOpen(szTiff, szM);

  if (m_lPageCount == -1) {
    m_lPageCount = 0;
    while(TIFFSetDirectory(m_tif,(uint16)m_lPageCount))
      m_lPageCount++;
  }

  if (!TIFFSetDirectory(m_tif, 0))
    return false;

  getdata();

  return true;
}


BOOL CTif::DecompressImage()
{
  try {

    BYTE * bits;   //pointer to source data
    BYTE * bits2;  //pointer to destination data

    //
    //check if it's a tiff file
    //
    if (!m_tif)
      return FALSE;

    //
    //set image info
    //
    m_info.xDPI    = (long)x_resolution;
    m_info.yDPI    = (long)y_resolution;
    m_info.xOffset = (long)x_offset;
    m_info.yOffset = (long)y_offset;

    m_head.biWidth   = width;
    m_head.biHeight  = height;
    m_head.biClrUsed = 0;

    if (m_bIsRGB)
	{
      m_head.biBitCount = 24;
    } 
	else 
	{
      if ( (photometric == PHOTOMETRIC_MINISBLACK)
                        ||
           (photometric==PHOTOMETRIC_MINISWHITE) )
      {
        if (bitspersample == 1) {
          //
          //  Black & White Image
          //
          m_head.biBitCount = 1;    
          m_head.biClrUsed  = 2;
        } else if (bitspersample == 4) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -