2009-06-15 24 views
1

我試圖讀取掃描圖像並將其從內存中的DIB壓縮爲TIF文件。我正在使用libtiff庫,並在網上找到了幾個例子,但沒有一個真正做到了,因爲我需要它們。我需要從DIB中獲取圖像並將其變爲B圖像。如何使用libtiff將DIB轉換爲tif

這是我從一個在線示例修改的代碼。它確實將它變成黑白色,但它也只顯示掃描的一部分,而不是整個部分。任何幫助,將不勝感激。

編輯*:我注意到這種情況,如果我掃描它作爲一個灰色的圖像,如果我掃描它作爲黑色和白色,然後返回的圖像是完全黑色,我不知道這是否有幫助在所有。

// set up the image tags 
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, w); 
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, h); 
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 1); 
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4); 
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); 
TIFFSetField(tif, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB); 
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1); 
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 *)lpBits; 
unsigned char * pdst = new unsigned char[(w)]; 

UINT32 src_index; 
UINT32 dst_index; 

// now go line by line to write out the image data 
for (unsigned int row = 0; row < h; row++) 
{ 

    // initialize the scan line to zero 
    memset(pdst,0,(size_t)(w)); 

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

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

回答

0

我可能是錯誤的,但是我認爲對於CCITTFAX4編碼的libtiff預計每字節8個像素填充到字節的結束時,如果該圖像寬度不是整除8.

如果寬度例如,掃描線應該是19字節,而不是150.

有一個使用libtiff here的很好的例子,雖然它不包括在閾值處關閉顏色信息幷包裝像素每個字節8個。

+0

我剛剛試過這個,它更接近,那種。我使用他的代碼,而不是25 * 8使用w * 8,所以如你所想,圖像真的很寬,但現在我可以看到整個圖像,而不僅僅是它的一部分。儘管如此,它被扭曲得如此廣泛。所以如果我能想出如何去包裝每個字節的像素8,我可能可以解決這個問題,我只是不知道如何去做這件事。 – netadptr0719 2009-06-15 19:13:42

相關問題