📄 tiff2pdf.c
字号:
http://bugzilla.remotesensing.org/buglist.cgi?product=libtiff See also libtiff.3t, tiffcp. */int main(int argc, char** argv){ extern int optind; extern char* optarg; T2P* t2p = NULL; TIFF* input = NULL; TIFF* output = NULL; tsize_t written=0; int c=0; t2p = t2p_init(); if (t2p == NULL){ TIFFError( TIFF2PDF_MODULE, "Can't initialize context"); goto failexit; } while ((c = getopt(argc, argv, "o:q:u:x:y:w:l:r:p:e:c:a:t:s:k:jzndifh")) != -1){ switch (c) { case 'o': output=TIFFOpen(optarg, "w"); if(output==NULL){ TIFFError( TIFF2PDF_MODULE, "Can't open output file %s for writing", optarg); goto failfreet2p; } if(output->tif_seekproc != NULL){ TIFFSeekFile(output, (toff_t) 0, SEEK_SET); } break;#ifdef JPEG_SUPPORT case 'j': t2p->pdf_defaultcompression=T2P_COMPRESS_JPEG; break;#endif#ifndef JPEG_SUPPORT case 'j': TIFFWarning( TIFF2PDF_MODULE, "JPEG support in libtiff required for JPEG compression, ignoring option"); break;#endif#ifdef ZIP_SUPPORT case 'z': t2p->pdf_defaultcompression=T2P_COMPRESS_ZIP; break;#endif#ifndef ZIP_SUPPORT case 'z': TIFFWarning( TIFF2PDF_MODULE, "Zip support in libtiff required for Zip compression, ignoring option"); break;#endif case 'q': t2p->pdf_defaultcompressionquality=atoi(optarg); break; case 'n': t2p->pdf_nopassthrough=1; break; case 'd': t2p->pdf_defaultcompression=T2P_COMPRESS_NONE; break; case 'u': if(optarg[0]=='m'){ t2p->pdf_centimeters=1; } break; case 'x': t2p->pdf_defaultxres = atof(optarg) / (t2p->pdf_centimeters?2.54:1.0); break; case 'y': t2p->pdf_defaultyres = atof(optarg) / (t2p->pdf_centimeters?2.54:1.0); break; case 'w': t2p->pdf_overridepagesize=1; t2p->pdf_defaultpagewidth = (atof(optarg) * 72) / (t2p->pdf_centimeters?2.54:1.0); break; case 'l': t2p->pdf_overridepagesize=1; t2p->pdf_defaultpagelength = (atof(optarg) * 72) / (t2p->pdf_centimeters?2.54:1.0); break; case 'r': if(optarg[0]=='o'){ t2p->pdf_overrideres=1; } break; case 'p': if(tiff2pdf_match_paper_size( &(t2p->pdf_defaultpagewidth), &(t2p->pdf_defaultpagelength), optarg)){ t2p->pdf_overridepagesize=1; } else { TIFFWarning( TIFF2PDF_MODULE, "Unknown paper size %s, ignoring option", optarg); } break; case 'i': t2p->pdf_colorspace_invert=1; case 'f': t2p->pdf_fitwindow=1; break; case 'e': t2p->pdf_datetime= (char*)_TIFFmalloc(17); if(t2p->pdf_datetime==NULL){ TIFFError( TIFF2PDF_MODULE, "Can't allocate %u bytes of memory for main", 17); goto failcloseoutput; } if(strlen(optarg)==0){ t2p->pdf_datetime[0]=0; } else { if(strlen(optarg)>14){optarg[14]=0;} t2p->pdf_datetime[0]='D'; t2p->pdf_datetime[1]=':'; strcpy(&(t2p->pdf_datetime[2]), optarg); } break; case 'c': t2p->pdf_creator= (char*)_TIFFmalloc(strlen(optarg)+1); if(t2p->pdf_creator==NULL){ TIFFError( TIFF2PDF_MODULE, "Can't allocate %u bytes of memory for main", strlen(optarg)+1); goto failcloseoutput; } strcpy(t2p->pdf_creator, optarg); t2p->pdf_creator[strlen(optarg)]=0; break; case 'a': t2p->pdf_author= (char*)_TIFFmalloc(strlen(optarg)+1); if(t2p->pdf_author==NULL){ TIFFError( TIFF2PDF_MODULE, "Can't allocate %u bytes of memory for main", strlen(optarg)+1); goto failcloseoutput; } strcpy(t2p->pdf_author, optarg); t2p->pdf_author[strlen(optarg)]=0; break; case 't': t2p->pdf_title= (char*)_TIFFmalloc(strlen(optarg)+1); if(t2p->pdf_title==NULL){ TIFFError( TIFF2PDF_MODULE, "Can't allocate %u bytes of memory for main", strlen(optarg)+1); goto failcloseoutput; } strcpy(t2p->pdf_title, optarg); t2p->pdf_title[strlen(optarg)]=0; break; case 's': t2p->pdf_subject= (char*)_TIFFmalloc(strlen(optarg)+1); if(t2p->pdf_subject==NULL){ TIFFError( TIFF2PDF_MODULE, "Can't allocate %u bytes of memory for main", strlen(optarg)+1); goto failcloseoutput; } strcpy(t2p->pdf_subject, optarg); t2p->pdf_subject[strlen(optarg)]=0; break; case 'k': t2p->pdf_keywords= (char*)_TIFFmalloc(strlen(optarg)+1); if(t2p->pdf_keywords==NULL){ TIFFError( TIFF2PDF_MODULE, "Can't allocate %u bytes of memory for main", strlen(optarg)+1); goto failcloseoutput; } strcpy(t2p->pdf_keywords, optarg); t2p->pdf_keywords[strlen(optarg)]=0; break; case 'h': case '?': tiff2pdf_usage(); goto failcloseoutput; break; } } t2p_validate(t2p); if(argc>optind){ input = TIFFOpen(argv[optind++], "r"); if(input==NULL){ TIFFError( TIFF2PDF_MODULE, "Can't open input file %s for reading", argv[optind-1]); goto failcloseoutput; } } else { TIFFError( TIFF2PDF_MODULE, "No input file specified"); tiff2pdf_usage(); goto failcloseoutput; } if(argc>optind){ TIFFError( TIFF2PDF_MODULE, "No support for multiple input files"); tiff2pdf_usage(); goto failcloseinput; } if(output==NULL){#ifndef _WIN32 output = TIFFFdOpen( (int)fileno(tmpfile()), "-", "w");#endif#ifdef _WIN32 { TCHAR temppath[MAX_PATH]; TCHAR tempfile[MAX_PATH]; GetTempPath((DWORD)MAX_PATH, (LPTSTR)temppath); GetTempFileName((LPCTSTR)temppath, (LPTSTR) _T("t2p"), 0, (LPTSTR)tempfile); output = TIFFFdOpen( (int)CreateFile( (LPCTSTR)tempfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL), "-", "w"); }#endif if(output==NULL){ TIFFError( TIFF2PDF_MODULE, "Can't open temporary output file for writing to stdout", argv[optind-1]); goto failcloseinput; } TIFFFlush(output); output->tif_readproc=t2p_empty_readproc; output->tif_seekproc=t2p_empty_seekproc; output->tif_closeproc=t2p_empty_closeproc;#ifndef _WIN32 close(output->tif_fd); output->tif_fd=(int)fileno(stdout);#endif#ifdef _WIN32 CloseHandle((HANDLE) output->tif_fd); output->tif_fd=(int)GetStdHandle(STD_OUTPUT_HANDLE);#endif output->tif_clientdata=(thandle_t)output->tif_fd; } written = t2p_write_pdf(t2p, input, output); if(t2p->t2p_error != 0){ TIFFError( TIFF2PDF_MODULE, "An error occurred in converting TIFF %s to PDF %s", TIFFFileName(input), TIFFFileName(output) ); goto failcloseinput; } if(input != NULL){ TIFFClose(input); } if(output != NULL){ TIFFClose(output); } if(t2p != NULL){ t2p_free(t2p); } return(EXIT_SUCCESS); failcloseinput: if(input != NULL){ TIFFClose(input); } failcloseoutput: if(output != NULL){ if(strcmp(TIFFFileName(output), "-") != 0){ unlink(TIFFFileName(output)); } TIFFClose(output); } failfreet2p: if(t2p != NULL){ t2p_free(t2p); } failexit: return(EXIT_FAILURE);}void tiff2pdf_usage(){ char* lines[]={ "usage: tiff2pdf [options] input.tiff", "options:", " -o: output to file name",#ifdef JPEG_SUPPORT " -j compress with JPEG", #endif#ifdef ZIP_SUPPORT " -z compress with Zip/Deflate",#endif " -q: compression quality", " -n no compressed data passthrough", " -d do not compress (decompress)", " -u: set distance unit, 'i' for inch, 'm' for centimeter", " -x: set x resolution default in dots per unit", " -y: set y resolution default in dots per unit", " -w: width in units", " -l: length in units", " -r: 'd' for resolution default, 'o' for resolution override", " -p: paper size, eg \"letter\", \"legal\", \"A4\"", " -f set PDF \"Fit Window\" user preference", " -e: date, overrides image or current date/time default, YYYYMMDDHHMMSS", " -c: sets document creator, overrides image software default", " -a: sets document author, overrides image artist default", " -t: sets document title, overrides image document name default", " -s: sets document subject, overrides image image description default", " -k: sets document keywords", " -h usage", NULL }; int i=0; fprintf(stderr, "%s\n\n", TIFFGetVersion()); for (i=0;lines[i]!=NULL;i++){ fprintf(stderr, "%s\n", lines[i]); } return;}int tiff2pdf_match_paper_size(float* width, float* length, char* papersize){ int i=0; int len=0; const char* sizes[]={ "LETTER", "A4", "LEGAL", "EXECUTIVE", "LETTER", "LEGAL", "LEDGER", "TABLOID", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "A10", "A9", "A8", "A7", "A6", "A5", "A4", "A3", "A2", "A1", "A0", "2A0", "4A0", "2A", "4A", "B10", "B9", "B8", "B7", "B6", "B5", "B4", "B3", "B2", "B1", "B0", "JISB10", "JISB9", "JISB8", "JISB7", "JISB6", "JISB5", "JISB4", "JISB3", "JISB2", "JISB1", "JISB0", "C10", "C9", "C8", "C7", "C6", "C5", "C4", "C3", "C2", "C1", "C0", "RA2", "RA1", "RA0", "SRA4", "SRA3", "SRA2", "SRA1", "SRA0", "A3EXTRA", "A4EXTRA", "STATEMENT", "FOLIO", "QUARTO", NULL } ; const int widths[]={ 612, 595, 612, 522, 612,612,792,792, 612,792,1224,1584,2448,2016,792,2016,2448,2880, 74,105,147,210,298,420,595,842,1191,1684,2384,3370,4768,3370,4768, 88,125,176,249,354,499,709,1001,1417,2004,2835, 91,128,181,258,363,516,729,1032,1460,2064,2920, 79,113,162,230,323,459,649,918,1298,1298,2599, 1219,1729,2438,638,907,1276,1814,2551, 914,667, 396, 612, 609, 0 }; const int lengths[]={ 792,842,1008, 756,792,1008,1224,1224, 792,1224,1584,2448,3168,2880,6480,10296,12672,10296, 105,147,210,298,420,595,842,1191,1684,2384,3370,4768,6741,4768,6741, 125,176,249,354,499,709,1001,1417,2004,2835,4008, 128,181,258,363,516,729,1032,1460,2064,2920,4127, 113,162,230,323,459,649,918,1298,1837,1837,3677, 1729,2438,3458,907,1276,1814,2551,3628, 1262,914, 612, 936, 780, 0 }; len=strlen(papersize); for(i=0;i<len;i++){ papersize[i]=toupper(papersize[i]); } for(i=0;sizes[i]!=NULL; i++){ if (strcmp( (const char*)papersize, sizes[i])==0){ *width=(float)widths[i]; *length=(float)lengths[i]; return(1); } } return(0);}/* This function allocates and initializes a T2P context struct pointer.*/T2P* t2p_init(){ T2P* t2p = (T2P*) _TIFFmalloc(sizeof(T2P)); _TIFFmemset(t2p, 0x00, sizeof(T2P)); if(t2p==NULL){ TIFFError( TIFF2PDF_MODULE, "Can't allocate %u bytes of memory for t2p_init", sizeof(T2P)); return( (T2P*) NULL ); } t2p->pdf_majorversion=1; t2p->pdf_minorversion=1; t2p->pdf_defaultxres=300.0; t2p->pdf_defaultyres=300.0; t2p->pdf_defaultpagewidth=612.0; t2p->pdf_defaultpagelength=792.0; t2p->pdf_xrefcount=3; /* Catalog, Info, Pages */ return(t2p);}/* This function frees a T2P context struct pointer and any allocated data fields of it.*/void t2p_free(T2P* t2p){ int i=0; if(t2p != NULL){ if(t2p->pdf_xrefoffsets != NULL){ _TIFFfree( (tdata_t) t2p->pdf_xrefoffsets); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -