📄 tiff2pdf.c
字号:
t2p->tiff_datasize+=2*stripcount; t2p->tiff_datasize+=2048; return; }#endif#ifdef JPEG_SUPPORT if(t2p->tiff_compression == COMPRESSION_JPEG){ if(TIFFGetField(input, TIFFTAG_JPEGTABLES, &xuint16, &jpt) != 0 ){ if(xuint16>4){ t2p->tiff_datasize+= xuint16; t2p->tiff_datasize -=2; /* don't use EOI of header */ } } else { t2p->tiff_datasize=2; /* SOI for first strip */ } stripcount=TIFFNumberOfStrips(input); if(!TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc)){ TIFFError(TIFF2PDF_MODULE, "Input file %s missing field: TIFFTAG_STRIPBYTECOUNTS", TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return; } for(i=0;i<stripcount;i++){ t2p->tiff_datasize += sbc[i]; t2p->tiff_datasize -=4; /* don't use SOI or EOI of strip */ } t2p->tiff_datasize +=2; /* use EOI of last strip */ }#endif (void) 0; } t2p->tiff_datasize=TIFFScanlineSize(input) * t2p->tiff_length; if(t2p->tiff_planar==PLANARCONFIG_SEPARATE){ t2p->tiff_datasize*= t2p->tiff_samplesperpixel; } return;}/* This function returns the necessary size of a data buffer to contain the raw or uncompressed image data from the input TIFF for a tile of a page.*/void t2p_read_tiff_size_tile(T2P* t2p, TIFF* input, ttile_t tile){ uint32* tbc = NULL; uint16 edge=0;#ifdef JPEG_SUPPORT uint16 xuint16=0; unsigned char* jpt;#endif edge |= t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile); edge |= t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile); if(t2p->pdf_transcode==T2P_TRANSCODE_RAW){ if(edge#if defined(JPEG_SUPPORT) || defined(OJPEG_SUPPORT) && !(t2p->pdf_compression==T2P_COMPRESS_JPEG)#endif ){ t2p->tiff_datasize=TIFFTileSize(input); return; } else { TIFFGetField(input, TIFFTAG_TILEBYTECOUNTS, &tbc); t2p->tiff_datasize=tbc[tile];#ifdef OJPEG_SUPPORT if(t2p->tiff_compression==COMPRESSION_OJPEG){ t2p->tiff_datasize+=2048; return; }#endif#ifdef JPEG_SUPPORT if(t2p->tiff_compression==COMPRESSION_JPEG){ if(TIFFGetField(input, TIFFTAG_JPEGTABLES, &xuint16, &jpt)!=0){ if(xuint16>4){ t2p->tiff_datasize+=xuint16; t2p->tiff_datasize-=4; /* don't use EOI of header or SOI of tile */ } } }#endif return; } } t2p->tiff_datasize=TIFFTileSize(input); if(t2p->tiff_planar==PLANARCONFIG_SEPARATE){ t2p->tiff_datasize*= t2p->tiff_samplesperpixel; } return;}/* This functions returns a non-zero value when the tile is on the right edge and does not have full imaged tile width.*/int t2p_tile_is_right_edge(T2P_TILES tiles, ttile_t tile){ if( ((tile+1) % tiles.tiles_tilecountx == 0) && (tiles.tiles_edgetilewidth != 0) ){ return(1); } else { return(0); } return(0); }/* This functions returns a non-zero value when the tile is on the bottom edge and does not have full imaged tile length.*/int t2p_tile_is_bottom_edge(T2P_TILES tiles, ttile_t tile){ if( ((tile+1) > (tiles.tiles_tilecount-tiles.tiles_tilecountx) ) && (tiles.tiles_edgetilelength != 0) ){ return(1); } else { return(0); } return(0);}/* This function returns a non-zero value when the tile is a right edge tile or a bottom edge tile.*/int t2p_tile_is_edge(T2P_TILES tiles, ttile_t tile){ return(t2p_tile_is_right_edge(tiles, tile) | t2p_tile_is_bottom_edge(tiles, tile) );}/* This function returns a non-zero value when the tile is a right edge tile and a bottom edge tile.*/int t2p_tile_is_corner_edge(T2P_TILES tiles, ttile_t tile){ return(t2p_tile_is_right_edge(tiles, tile) & t2p_tile_is_bottom_edge(tiles, tile) );}/* This function is an empty (dummy) TIFFReadWriteProc that returns the amount requested to be read without reading anything.*/tsize_t t2p_empty_readproc(thandle_t fd, tdata_t buf, tsize_t size){ (void) fd; (void) buf; (void) size; return (size);}/* This function is an empty (dummy) TIFFReadWriteProc that returns the amount requested to be written without writing anything.*/tsize_t t2p_empty_writeproc(thandle_t fd, tdata_t buf, tsize_t size){ (void) fd; (void) buf; (void) size; return (size);}/* This function is an empty (dummy) TIFFSeekProc that returns off.*/toff_t t2p_empty_seekproc(thandle_t fd, toff_t off, int whence){ (void) fd; (void) off; (void) whence; return( off );}/* This function is an empty (dummy) TIFFCloseProc that returns 0.*/int t2p_empty_closeproc(thandle_t fd){ (void) fd; return(0);}/* This function reads the raster image data from the input TIFF for an image and writes the data to the output PDF XObject image dictionary stream. It returns the amount written or zero on error.*/tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){ tsize_t written=0; unsigned char* buffer=NULL; unsigned char* samplebuffer=NULL; tsize_t bufferoffset=0; tsize_t samplebufferoffset=0; tsize_t read=0; tstrip_t i=0; tstrip_t j=0; tstrip_t stripcount=0; tsize_t stripsize=0; tsize_t sepstripcount=0; tsize_t sepstripsize=0;#ifdef OJPEG_SUPPORT toff_t inputoffset=0; uint16 h_samp=1; uint16 v_samp=1; uint16 ri=1; uint32 rows=0;#endif#ifdef JPEG_SUPPORT unsigned char* jpt; uint16 xuint16_1=0; uint16 xuint16_2=0; float* xfloatp; uint32* sbc; unsigned char* stripbuffer; uint32 striplength=0; uint32 max_striplength=0;#endif if(t2p->pdf_transcode == T2P_TRANSCODE_RAW){#ifdef CCITT_SUPPORT if(t2p->pdf_compression == T2P_COMPRESS_G4){ buffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } TIFFReadRawStrip(input, 0, (tdata_t) buffer, t2p->tiff_datasize); if (t2p->tiff_fillorder==FILLORDER_LSB2MSB){ // make sure is lsb-to-msb bit-endianness fill order TIFFReverseBits(buffer, t2p->tiff_datasize); } TIFFWriteFile(output, (tdata_t) buffer, t2p->tiff_datasize); _TIFFfree(buffer); return(t2p->tiff_datasize); }#endif#ifdef ZIP_SUPPORT if(t2p->pdf_compression == T2P_COMPRESS_ZIP){ buffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } TIFFReadRawStrip(input, 0, (tdata_t) buffer, t2p->tiff_datasize); if (t2p->tiff_fillorder==FILLORDER_LSB2MSB){ TIFFReverseBits(buffer, t2p->tiff_datasize); } TIFFWriteFile(output, (tdata_t) buffer, t2p->tiff_datasize); _TIFFfree(buffer); return(t2p->tiff_datasize); }#endif#ifdef OJPEG_SUPPORT if(t2p->tiff_compression == COMPRESSION_OJPEG){ if(t2p->tiff_dataoffset != 0){ buffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } if(t2p->pdf_ojpegiflength==0){ inputoffset=TIFFSeekFile(input, 0, SEEK_CUR); TIFFSeekFile(input, t2p->tiff_dataoffset, SEEK_SET); TIFFReadFile(input, (tdata_t) buffer, t2p->tiff_datasize); TIFFSeekFile(input, inputoffset, SEEK_SET); TIFFWriteFile(output, (tdata_t) buffer, t2p->tiff_datasize); _TIFFfree(buffer); return(t2p->tiff_datasize); } else { inputoffset=TIFFSeekFile(input, 0, SEEK_CUR); TIFFSeekFile(input, t2p->tiff_dataoffset, SEEK_SET); bufferoffset=TIFFReadFile(input, (tdata_t) buffer, t2p->pdf_ojpegiflength); t2p->pdf_ojpegiflength=0; TIFFSeekFile(input, inputoffset, SEEK_SET); TIFFGetField(input, TIFFTAG_YCBCRSUBSAMPLING, &h_samp, &v_samp); buffer[bufferoffset++]= 0xff; buffer[bufferoffset++]= 0xdd; buffer[bufferoffset++]= 0x00; buffer[bufferoffset++]= 0x04; h_samp*=8; v_samp*=8; ri=(t2p->tiff_width+h_samp-1) / h_samp; TIFFGetField(input, TIFFTAG_ROWSPERSTRIP, &rows); ri*=(rows+v_samp-1)/v_samp; buffer[bufferoffset++]= (ri>>8) & 0xff; buffer[bufferoffset++]= ri & 0xff; stripcount=TIFFNumberOfStrips(input); for(i=0;i<stripcount;i++){ if(i != 0 ){ buffer[bufferoffset++]=0xff; buffer[bufferoffset++]=(0xd0 | ((i-1)%8)); } bufferoffset+=TIFFReadRawStrip(input, i, (tdata_t) &(((unsigned char*)buffer)[bufferoffset]), -1); } TIFFWriteFile(output, (tdata_t) buffer, bufferoffset); _TIFFfree(buffer); return(bufferoffset); } } else { if(! t2p->pdf_ojpegdata){ TIFFError(TIFF2PDF_MODULE, "No support for OJPEG image %s with bad tables", TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } buffer=(unsigned char*) _TIFFmalloc(t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } _TIFFmemcpy(buffer, t2p->pdf_ojpegdata, t2p->pdf_ojpegdatalength); bufferoffset=t2p->pdf_ojpegdatalength; stripcount=TIFFNumberOfStrips(input); for(i=0;i<stripcount;i++){ if(i != 0){ buffer[bufferoffset++]=0xff; buffer[bufferoffset++]=(0xd0 | ((i-1)%8)); } bufferoffset+=TIFFReadRawStrip(input, i, (tdata_t) &(((unsigned char*)buffer)[bufferoffset]), -1); } if( ! ( (buffer[bufferoffset-1]==0xd9) && (buffer[bufferoffset-2]==0xff) ) ){ buffer[bufferoffset++]=0xff; buffer[bufferoffset++]=0xd9; } TIFFWriteFile(output, (tdata_t) buffer, bufferoffset); _TIFFfree(buffer); return(bufferoffset); TIFFError(TIFF2PDF_MODULE, "No support for OJPEG image %s with no JPEG File Interchange offset", TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } return(t2p->tiff_datasize); }#endif#ifdef JPEG_SUPPORT if(t2p->tiff_compression == COMPRESSION_JPEG){ buffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } if(TIFFGetField(input, TIFFTAG_JPEGTABLES, &xuint16_1, &jpt) != 0){ if(xuint16_1>4){ _TIFFmemcpy(buffer, jpt, xuint16_1); bufferoffset+=xuint16_1-2; } } stripcount=TIFFNumberOfStrips(input); TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc); for(i=0;i<stripcount;i++){ if(sbc[i]>max_striplength) max_striplength=sbc[i]; } stripbuffer=(unsigned char*) _TIFFmalloc(max_striplength); if(stripbuffer==NULL){ TIFFError(TIFF2PDF_MODULE, "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", max_striplength, TIFFFileName(input)); _TIFFfree(buffer); t2p->t2p_error = T2P_ERR_ERROR; return(0); } for(i=0;i<stripcount;i++){ striplength=TIFFReadRawStrip(input, i, (tdata_t) stripbuffer, -1); if(!t2p_process_jpeg_strip( stripbuffer, &striplength, buffer, &bufferoffset, i, t2p->tiff_length)){ TIFFError(TIFF2PDF_MODULE, "Can't process JPEG data in input file %s", TIFFFileName(input)); _TIFFfree(samplebuffer); _TIFFfree(buffer); t2p->t2p_error = T2P_ERR_ERROR; return(0); } } buffer[bufferoffset++]=0xff; buffer[bufferoffset++]=0xd9; TIFFWriteFile(output, (tdata_t) buffer, bufferoffset); _TIFFfree(stripbuffer); _TIFFfree(buffer); return(bufferoffset); }#endif (void)0; } if(t2p->pdf_sample==T2P_SAMPLE_NOTHING){ buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } stripsize=TIFFStripSize(input); stripcount=TIFFNumberOfStrips(input); for(i=0;i<stripcount;i++){ read = TIFFReadE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -