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

📄 dtpdfconvert.cpp

📁 实现TIFF文件转换未PDF文件的源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//************************
//*      Chris Rubio     *
//*                      *
//*  Copyright (C) 2002  *
//*                      *
//*  dtpdfconvert.cpp    *
//************************
/*
  "I swear by my life and my love of it that I will never live for the
   sake of another man, nor ask another man to live for mine"

                    John Galt in "Atlas Shrugged", by Ayn Rand

    You are free to use,modify,make,sell,share or do whatever you want with this code.
    thanks to tifflib, and CxImage class, ImageMagick for inspiration. Please send any modifications to 
    +s i r r u b e 2 @ y a h o o . c o m+ so I can implement them as well if I choose. 

    Thanks to all the coders on code project in the past who have shared thier source as well.

    To build you need CXImage and libtiff.  /cvsroot/osrs/libtiff/libtiff.

*   THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND
*   EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
*   WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.


*/
//****************************************************************************
//*                              REVISION HISTORY                            *
//****************************************************************************
//*   Date   *                                                     *   Who   *
//****************************************************************************
//* 11/8/2002* Created source file.                                *   CLR   *
//****************************************************************************
#include "stdafx.h"
#include "dtpdfconvert.h"

#include "time.h"
#include "tiffio.h"
#include "tiffiop.h"


/****************************************************************************/
/*                                                                          */
/*   QTiff::getdata                                                         */
/*                                                                          */
/****************************************************************************/
void QTiff::getdata()
{
  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);
}

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


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

EXTERN_C TIFF* TIFFOpenEx(CxFile* stream, const char* mode);

/****************************************************************************/
/*                                                                          */
/*  QTiff::open                                                             */
/*                                                                          */
/****************************************************************************/
int QTiff::open( char* szTiff ) 
{
  if ((hFile.Open( szTiff, "rb"))==NULL)  
    return false;

  m_tif = TIFFOpenEx(&hFile, "rb");

  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;
}


/****************************************************************************/
/*                                                                          */
/*  CDTPDFConvert::tiff_writePDFXObjectLength                               */
/*                                                                          */
/****************************************************************************/
int CDTPDFConvert::tiff_writePDFXObjectLength( FILE * pf, long nObj, QTiff & qt, fpos_t & start, fpos_t & end )
{

  try {
    CString czbuf;
    fpos_t offset = end - start;

    czbuf.Format( "%lu\r",(unsigned long) offset);
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf = "endobj\r";
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
  } catch(...)
  {
    throw -10;
  }

  return 1;
}

/****************************************************************************/
/*                                                                          */
/*  CDTPDFConvert::tiff_writePDFXObject                                     */
/*                                                                          */
/****************************************************************************/
int CDTPDFConvert::tiff_writePDFXObject( FILE * pf, long nObj, QTiff & qt, fpos_t & start, fpos_t & end )
{

  try {

    CString czbuf;

    czbuf = "<<\r";
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf = "/Type /XObject\r";
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf = "/Subtype /Image\r";
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf.Format( "/Name /Im%lu\r",qt.page() );
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf = "/Filter /CCITTFaxDecode\r";
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf.Format( "/DecodeParms << /K -1 /Columns %ld /Rows %ld >>\r", qt.width,qt.height);
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf.Format( "/Width %lu\r",qt.width );
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf.Format( "/Height %lu\r",qt.height );
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf = "/ColorSpace /DeviceGray\r";
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf.Format( "/BitsPerComponent %d\r", 1 );
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf.Format( "/Length %lu 0 R\r",nObj +1 );
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf = ">>\r";
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf = "stream\r\n";
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    fgetpos( pf, &start );

    //
    //  write out buffer.
    //
    unsigned long * byte_count, strip_size;
    unsigned char * buffer;
    uint16 fillorder;
    long count = 0;

    //
    // Allocate raw strip buffer.
    //
    (void) TIFFGetField( qt.m_tif ,TIFFTAG_STRIPBYTECOUNTS,&byte_count );
    strip_size = byte_count[ 0 ];
    for (int i = 1; i < (long) TIFFNumberOfStrips( qt.m_tif ); i++)
      if (byte_count[ i ] > strip_size )
        strip_size = byte_count[ i ];

    buffer = (unsigned char *) new unsigned char[ strip_size ];
    if (buffer == (unsigned char *) NULL) {
      throw -11;
    }

    //
    // Compress runlength encoded to 2D Huffman pixels.
    //
    (void)TIFFGetFieldDefaulted( qt.m_tif,TIFFTAG_FILLORDER,&fillorder );
    for (i=0; i < (long) TIFFNumberOfStrips( qt.m_tif ); i++) {
      count = TIFFReadRawStrip( qt.m_tif,(uint32) i,buffer,(long) byte_count[ i ] );
      if (fillorder == FILLORDER_LSB2MSB)
        TIFFReverseBits( buffer,count );

      //
      //  write out the image
      //
      fwrite( buffer, count, 1 , pf );

    }
  
    fgetpos( pf, &end );
    czbuf = "\rendstream\r";
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf = "endobj\r";
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
    delete[] buffer;
  } catch(...)
  {
    throw -11;
  }


  return 1;
}

/****************************************************************************/
/*                                                                          */
/*  CDTPDFConvert::tiff_writePDFProcsetObject                               */
/*                                                                          */
/****************************************************************************/
int CDTPDFConvert::tiff_writePDFProcsetObject( FILE * pf, long nObj, QTiff & qt, fpos_t & start, fpos_t & end )
{
  try {
    CString czbuf;

    czbuf = "[/PDF /ImageB]\r";
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf = "endobj\r";
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
  } catch(...) 
  {
    throw -12;
  }
  return 1;
}


/****************************************************************************/
/*                                                                          */
/*  CDTPDFConvert::tiff_writePDFContentsObject                              */
/*                                                                          */
/****************************************************************************/
int CDTPDFConvert::tiff_writePDFContentsObject( FILE * pf, long nObj, QTiff & qt, fpos_t & start, fpos_t & end )
{

  try {

    CString czbuf;
    double x_scale = 0;
    double y_scale = 0;
    CString czContentData;

    //
    //  Prepare the content data first.
    //
    x_scale = ( qt.width * 72 ) / qt.x_resolution;
    long tiff_width = x_scale + 0.5;

    y_scale = ( qt.height * 72 ) / qt.y_resolution;
    long tiff_height = (unsigned long)( y_scale + 0.5);

    czContentData += "q\r";
    czbuf.Format( "%ld 0 0 %ld %ld %ld cm\r",tiff_width,tiff_height, 0, 0 );
    czContentData += czbuf;
    czbuf.Format( "/Im%lu Do\r",qt.page() );
    czContentData += czbuf;
    czContentData += "Q\r";

    //
    //  Write it all out.
    //
    czbuf = "<<\r";
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf.Format( "/Length %i\r", czContentData.GetLength() - 1 );
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf = ">>\r";
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf = "stream\r\n";
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    //
    //  content data
    //
    fwrite( (LPCSTR)czContentData, czContentData.GetLength(), 1, pf );

    czbuf = "endstream\r";
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf = "endobj\r";
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
  } catch (...)
  {
    throw -13;
  }

  return 1;
}

/****************************************************************************/
/*                                                                          */
/*  CDTPDFConvert::tiff_writePDFPageObject                                  */
/*                                                                          */
/****************************************************************************/
int CDTPDFConvert::tiff_writePDFPageObject( FILE * pf, long nObj, long pages_id , QTiff & qt )
{
  try {

    CString czbuf;

    double x_scale = 0;
    double y_scale = 0;

    //
    //  Prepare the content data first.
    //
    x_scale = ( qt.width * 72 ) / qt.x_resolution;
    long tiff_width = x_scale + 0.5;

    y_scale = ( qt.height * 72 ) / qt.y_resolution;
    long tiff_height = (unsigned long)( y_scale + 0.5);


    czbuf = "<<\r";
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf = "/Type /Page\r";
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf.Format( "/Parent %lu 0 R\r",pages_id );
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf.Format( "/MediaBox [0 0 %ld %ld]\r",tiff_width,tiff_height);
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf = "/Resources\r<<\r";
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    //
    // next obj must add length for  length == 2 objects
    //
    czbuf.Format( "/XObject << /Im%lu %lu 0 R >>\r",qt.page(), nObj + 3 ); 
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf.Format( "/ProcSet %lu 0 R \r", nObj + 2 );
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf = ">>\r";
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf.Format( "/Contents [%lu 0 R]\r>>\r", nObj + 1 ); //  next obj must add length for the contents == 1 objects
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );

    czbuf = "endobj\r";
    fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
  } catch(...)
  {
    throw -14;
  }

  return 1;
}

⌨️ 快捷键说明

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