用libtiff库写标准.tif传真文件,急急急,快疯了

yan13579 2005-05-12 11:07:15
我用libtiff库来写传真文件的时候遇到了难题,我已经按照规范来设置了传真tag了,但是还是不行
打印出来的是空纸!!

//NewSubfileType 254
TIFFSetField(tif,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
//图象宽度 256
TIFFSetField(tif,TIFFTAG_IMAGEWIDTH,w);
//图象高度 257
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, h);
//每个样本的位数 258
TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,1);
//压缩算法 259
TIFFSetField(tif,TIFFTAG_COMPRESSION,3);
//颜色 262
TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,0);
//WhiteIsZero 266
TIFFSetField(tif,TIFFTAG_FILLORDER,2);
//273
// TIFFSetField(tif,TIFFTAG_STRIPOFFSETS,1269616);
// 274
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
//SamplesPerPixel 277
TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,1);//3);//1);
//RowsPerStrip 278
TIFFSetField(tif,TIFFTAG_ROWSPERSTRIP,h);
//279
// TIFFSetField(tif,TIFFTAG_STRIPBYTECOUNTS,1269632);
//XResolution 282
TIFFSetField(tif,TIFFTAG_XRESOLUTION,204);
//YResolution 283
TIFFSetField(tif,TIFFTAG_YRESOLUTION,196);
// 284
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
//Group3Options 292
TIFFSetField(tif,TIFFTAG_GROUP3OPTIONS,0);
//分辨率 296
TIFFSetField(tif,TIFFTAG_RESOLUTIONUNIT,RESUNIT_INCH);
//PageNumber 297
TIFFSetField(tif,TIFFTAG_PAGENUMBER,0);

另外,TIFFTAG_STRIPOFFSETS和TIFFTAG_STRIPBYTECOUNTS这两个tag是否要非设不可,我尝试不设的时候,打印出来的是白纸,如果去设这两个tag的话,我查过libtiff源代码中,libtiff中的TIFFSetField是不提供TIFFTAG_STRIPOFFSETS和TIFFTAG_STRIPBYTECOUNTS的设置的,运行是会弹出警告对话框,提示“Invalid tag (not supported by codec)”。
我也试过用CxImage库去写tif文件,但是CxImage里面的Save函数简单是很简单,但是保存出来的tif根本就不能通过dialogic卡来发传真。我看过CxImage的源代码,里面的tag字段设置没有完全适合tif传真规范。

有没有高手是高手是搞过这方面的,救救我吧,怎样才能设置正确的tag?或者有人做过tif,bmp修改然后将其保存为可传真的tif图像的,请求发几段代码给我看看。或者可以email联系我:yan13579@163.com
谢谢了。各位好心人。
...全文
505 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hxy2003 2005-05-22
  • 打赏
  • 举报
回复
..
wuyapu 2005-05-13
  • 打赏
  • 举报
回复
设置TIFFTAG_FILLORDER标记
下面那个值我忘了,你试试
FILLORDER_MSB2LSB
FILLORDER_LSB2MSB
yan13579 2005-05-13
  • 打赏
  • 举报
回复
// compression
if (GetCodecOption(CXIMAGE_FORMAT_TIF)) {
compression = (WORD)GetCodecOption(CXIMAGE_FORMAT_TIF);
} else {
switch (bitcount) {
case 1 :
compression = COMPRESSION_CCITTFAX3;//COMPRESSION_CCITTFAX4;
break;
case 4 :
case 8 :
compression = COMPRESSION_LZW;
break;
case 24 :
case 32 :
compression = COMPRESSION_JPEG;
break;
default :
compression = COMPRESSION_NONE;
break;
}
}
// 259
TIFFSetField(m_tif, TIFFTAG_COMPRESSION, compression);
//adding Group3Options 292
TIFFSetField(m_tif,TIFFTAG_GROUP3OPTIONS,0);
switch (compression) {
case COMPRESSION_JPEG:
TIFFSetField(m_tif, TIFFTAG_JPEGQUALITY, info.nQuality);
TIFFSetField(m_tif, TIFFTAG_ROWSPERSTRIP, ((7+rowsperstrip)>>3)<<3);
break;
case COMPRESSION_LZW:
if (bitcount>=8) TIFFSetField(m_tif, TIFFTAG_PREDICTOR, 2);
break;
}

// read the DIB lines from bottom to top and save them in the TIF

BYTE *bits;
switch(bitcount) {
case 1 :
case 4 :
case 8 :
{
if (samplesperpixel==1){
for (y = 0; y < height; y++) {
bits= info.pImage + (height - y - 1)*info.dwEffWidth;
if (TIFFWriteScanline(m_tif,bits, y, 0)==-1) return false;
}
}
#if CXIMAGE_SUPPORT_ALPHA
else { //8bpp + alpha layer
bits = (BYTE*)malloc(2*width);
if (!bits) return false;
for (y = 0; y < height; y++) {
for (x=0;x<width;x++){
bits[2*x]=GetPixelIndex(x,height - y - 1);
bits[2*x+1]=AlphaGet(x,height - y - 1);
}
if (TIFFWriteScanline(m_tif,bits, y, 0)==-1) {
free(bits);
return false;
}
}
free(bits);
}
#endif //CXIMAGE_SUPPORT_ALPHA
break;
}
case 24:
{
BYTE *buffer = (BYTE *)malloc(info.dwEffWidth);
if (!buffer) return false;
for (y = 0; y < height; y++) {
// get a pointer to the scanline
memcpy(buffer, info.pImage + (height - y - 1)*info.dwEffWidth, info.dwEffWidth);
// TIFFs store color data RGB instead of BGR
BYTE *pBuf = buffer;
for (x = 0; x < width; x++) {
BYTE tmp = pBuf[0];
pBuf[0] = pBuf[2];
pBuf[2] = tmp;
pBuf += 3;
}
// write the scanline to disc
if (TIFFWriteScanline(m_tif, buffer, y, 0)==-1){
free(buffer);
return false;
}
}
free(buffer);
break;
}
case 32 :
{
#if CXIMAGE_SUPPORT_ALPHA
BYTE *buffer = (BYTE *)malloc((info.dwEffWidth*4)/3);
if (!buffer) return false;
for (y = 0; y < height; y++) {
// get a pointer to the scanline
memcpy(buffer, info.pImage + (height - y - 1)*info.dwEffWidth, info.dwEffWidth);
// TIFFs store color data RGB instead of BGR
BYTE *pSrc = buffer + 3 * width;
BYTE *pDst = buffer + 4 * width;
for (x = 0; x < width; x++) {
pDst-=4;
pSrc-=3;
pDst[3] = AlphaGet(width-x-1,height-y-1);
pDst[2] = pSrc[0];
pDst[1] = pSrc[1];
pDst[0] = pSrc[2];
}
// write the scanline to disc
if (TIFFWriteScanline(m_tif, buffer, y, 0)==-1){
free(buffer);
return false;
}
}
free(buffer);
#endif //CXIMAGE_SUPPORT_ALPHA
break;
}
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
#endif // CXIMAGE_SUPPORT_ENCODE
yan13579 2005-05-13
  • 打赏
  • 举报
回复
各位,好消息。我已经能够成功发送传真文件了,我修改了一下CxImage 的源代码,使其符合Dialogic传真的规范:
////////////////////////////////////////////////////////////////////////////////
bool CxImageTIF::EncodeBody(TIFF *m_tif, bool multipage, int page, int pagecount)
{
uint32 height=head.biHeight;
uint32 width=head.biWidth;
uint16 bitcount=head.biBitCount;
uint16 bitspersample;
uint16 samplesperpixel;
uint16 photometric=0;
uint16 compression;
// uint16 pitch;
// int line;
uint32 x, y;

samplesperpixel = ((bitcount == 24) || (bitcount == 32)) ? (BYTE)3 : (BYTE)1;
#if CXIMAGE_SUPPORT_ALPHA
if (bitcount==24 && AlphaIsValid()) { bitcount=32; samplesperpixel=4; }
#endif //CXIMAGE_SUPPORT_ALPHA

bitspersample = bitcount / samplesperpixel;

//set the PHOTOMETRIC tag
RGBQUAD *rgb = GetPalette();
switch (bitcount) {
case 1:
if (CompareColors(&rgb[0],&rgb[1])<0) {
/* <abe> some viewers do not handle PHOTOMETRIC_MINISBLACK:
* let's transform the image in PHOTOMETRIC_MINISWHITE
*/
//invert the colors
RGBQUAD tempRGB=GetPaletteColor(0);
SetPaletteColor(0,GetPaletteColor(1));
SetPaletteColor(1,tempRGB);
//invert the pixels
BYTE *iSrc=info.pImage;
for (unsigned long i=0;i<head.biSizeImage;i++){
*iSrc=(BYTE)~(*(iSrc));
iSrc++;
}
photometric = PHOTOMETRIC_MINISWHITE;
//photometric = PHOTOMETRIC_MINISBLACK;
} else {
photometric = PHOTOMETRIC_MINISWHITE;
}
break;
case 4: // Check if the DIB has a color or a greyscale palette
case 8:
photometric = PHOTOMETRIC_MINISBLACK; //default to gray scale
for (x = 0; x < head.biClrUsed; x++) {
if ((rgb->rgbRed != x)||(rgb->rgbRed != rgb->rgbGreen)||(rgb->rgbRed != rgb->rgbBlue)){
photometric = PHOTOMETRIC_PALETTE;
break;
}
rgb++;
}
break;
case 24:
case 32:
photometric = PHOTOMETRIC_RGB;
break;
}

#if CXIMAGE_SUPPORT_ALPHA
if (AlphaIsValid() && bitcount==8) samplesperpixel=2; //8bpp + alpha layer
#endif //CXIMAGE_SUPPORT_ALPHA

// line = CalculateLine(width, bitspersample * samplesperpixel);
// pitch = (uint16)CalculatePitch(line);

//prepare the palette struct
RGBQUAD pal[256];
if (GetPalette()){
BYTE b;
memcpy(pal,GetPalette(),GetPaletteSize());
for(WORD a=0;a<head.biClrUsed;a++){ //swap blue and red components
b=pal[a].rgbBlue; pal[a].rgbBlue=pal[a].rgbRed; pal[a].rgbRed=b;
}
}

// handle standard width/height/bpp stuff
// 256
TIFFSetField(m_tif, TIFFTAG_IMAGEWIDTH, width);
// 257
TIFFSetField(m_tif, TIFFTAG_IMAGELENGTH, height);
// 277
TIFFSetField(m_tif, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
// 258
TIFFSetField(m_tif, TIFFTAG_BITSPERSAMPLE, bitspersample);
// 262
TIFFSetField(m_tif, TIFFTAG_PHOTOMETRIC, photometric);
// adding WhiteIsZero 266
TIFFSetField(m_tif,TIFFTAG_FILLORDER,2);
// 284
TIFFSetField(m_tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); // single image plane
// 274
TIFFSetField(m_tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
//adding 273
// TIFFSetField(m_tif,TIFFTAG_STRIPOFFSETS,info.yDPI+8);

// uint32 rowsperstrip = TIFFDefaultStripSize(m_tif, (uint32) -1); //<REC> gives better compression
uint32 rowsperstrip = height;
// 278
TIFFSetField(m_tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
//adding 279
// TIFFSetField(m_tif,TIFFTAG_STRIPBYTECOUNTS);
// handle metrics
// 296
TIFFSetField(m_tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
// 282
TIFFSetField(m_tif, TIFFTAG_XRESOLUTION, (float)info.xDPI);
// 283
TIFFSetField(m_tif, TIFFTAG_YRESOLUTION, (float)info.yDPI);
// TIFFSetField(m_tif, TIFFTAG_XPOSITION, (float)info.xOffset);
// TIFFSetField(m_tif, TIFFTAG_YPOSITION, (float)info.yOffset);

// multi-paging - Thanks to Abe <God(dot)bless(at)marihuana(dot)com>
if (multipage)
{
char page_number[20];
sprintf(page_number, "Page %d", page);
// 254
TIFFSetField(m_tif, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
// 297
TIFFSetField(m_tif, TIFFTAG_PAGENUMBER, page,pagecount);
// 285
TIFFSetField(m_tif, TIFFTAG_PAGENAME, page_number);
} else {
TIFFSetField(m_tif, TIFFTAG_SUBFILETYPE, 0);
}

// palettes (image colormaps are automatically scaled to 16-bits)
if (photometric == PHOTOMETRIC_PALETTE) {
uint16 *r, *g, *b;
r = (uint16 *) _TIFFmalloc(sizeof(uint16) * 3 * 256);
g = r + 256;
b = g + 256;

for (int i = 255; i >= 0; i--) {
b[i] = (uint16)SCALE((uint16)pal[i].rgbRed);
g[i] = (uint16)SCALE((uint16)pal[i].rgbGreen);
r[i] = (uint16)SCALE((uint16)pal[i].rgbBlue);
}
// 320
TIFFSetField(m_tif, TIFFTAG_COLORMAP, r, g, b);
_TIFFfree(r);
}

支持GPL协议: one line to give the program\'s name and an idea of what it does. Copyright (C) Ports Project 2007/01/30 horse_b This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.      horse_b 研究了6年代码跨平台移植项目,可以做大部分项目的移植,在这里的项目大部分是GPL工程 所以大家得到我的PORTS工程以后,也请尊重GPL协议   文件列表: libpcap-cygwin-port.tar.gz libnet-cygwin-port.tar.gz libtiff-cygwin-port.tar.gz libtiff-mingw-port.tar.gz libtiff-vc-vs.net-port.rar 说明: 所有这些PORTS项目都是horse_b的个人制作,2001年我为NIDS系统软件SNORT项目做了一个WIN32-Port的项目, 应该比SNORT作者出win32 port要早半年,但是我没有公开过这个WIN32 PORT,这个项目的WIN32 的二进制程序 在黑客网站黑白中可以找到 http://www.yiii.net/app/club/view.jsp?Information_Id=I00014317 抓包驱动snort182drv.zip http://www.yiii.net/app/club/view.jsp?Information_Id=I00014560 ids.zip 请大家尊重我的劳动,不允许任意改动我做的PORTS项目,用要做PORTS项目的,请发邮件和我联系 如果有人和我讨论移植和开发编译器,可以来信和我讨论: 我的专门接受此类问题的EMAIL: winddk64@gmail.com 或者 winddk64@163.com       技术下载区 Libpcap cygwin port下载:    libpcap-cygwin-port.tar.gz libnet cygwin port下载: libnet-cygwin-port.tar.gz   libtiff cygwin port下载: libtiff-cygwin-port.tar.gz libtiff mingw port下载: libnet-mingw-port.tar.gz (这个工程我觉得有VC和VS。NET的PORT了,我就没做这个PORT,有人要这个PORT,可以和我联系,半天就能搞定了) libtiff vc++&

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧