2014-04-08 53 views
0

我的android應用程序使用一個外部庫,使一些圖像處理。治療鏈的最終輸出是一個單色位圖,但保存了一個彩色位圖(32bpp)。32 bpp單色位圖1 bpp TIFF

圖像必須上傳到雲blob,所以爲了帶寬的考慮,我想將它轉換爲1bpp G4壓縮TIFF。我通過JNI成功地將libTIFF集成到了我的應用程序中,現在我正在用C編寫轉換例程。我有點卡在這裏。

我設法產生一個32 BPP TIFF,但不可能減少到1bpp,輸出圖像總是不可讀。有人成功做了類似的任務嗎?

更多speciffically:

  • 應該是什麼的SAMPLE_PER_PIXEL和BITS_PER_SAMPLE 參數值?
  • 如何確定帶材尺寸?
  • 如何填寫每個帶? (即:如何將32bpp像素線轉換爲1 bpp像素帶?)

非常感謝!

UPDATE:與莫希特耆那的珍貴的幫助所產生的代碼

int ConvertMonochrome32BppBitmapTo1BppTiff(char* bitmap, int height, int width, int resx, int resy, char const *tifffilename) 
{ 
    TIFF *tiff; 

    if ((tiff = TIFFOpen(tifffilename, "w")) == NULL) 
    { 
     return TC_ERROR_OPEN_FAILED; 
    } 

    // TIFF Settings 
    TIFFSetField(tiff, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH); 
    TIFFSetField(tiff, TIFFTAG_XRESOLUTION, resx); 
    TIFFSetField(tiff, TIFFTAG_YRESOLUTION, resy); 
    TIFFSetField(tiff, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4); //Group4 compression 
    TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, width); 
    TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, height); 
    TIFFSetField(tiff, TIFFTAG_ROWSPERSTRIP, 1); 
    TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, 1); 
    TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, 1); 
    TIFFSetField(tiff, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); 
    TIFFSetField(tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); 
    TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE); 

    tsize_t tbufsize = (width + 7)/8; //Tiff ScanLine buffer size for 1bpp pixel row 

    //Now writing image to the file one row by one 
    int x, y; 
    for (y = 0; y < height; y++) 
    { 
     char *buffer = malloc(tbufsize); 
     memset(buffer, 0, tbufsize); 

     for (x = 0; x < width; x++) 
     { 
      //offset of the 1st byte of each pixel in the input image (is enough to determine is black or white in 32 bpp monochrome bitmap) 
      uint32 bmpoffset = ((y * width) + x) * 4; 

      if (bitmap[bmpoffset] == 0) //Black pixel ? 
      { 
       uint32 tiffoffset = x/8; 
       *(buffer + tiffoffset) |= (0b10000000 >> (x % 8)); 
      } 
     } 

     if (TIFFWriteScanline(tiff, buffer, y, 0) != 1) 
     { 
      return TC_ERROR_WRITING_FAILED; 
     } 

     if (buffer) 
     { 
      free(buffer); 
      buffer = NULL; 
     } 
    } 

    TIFFClose(tiff); 
    tiff = NULL; 

    return TC_SUCCESSFULL; 
} 

回答

0

要轉換32 BPP 1 BPP,提取RGB並將其轉換成Y(亮度),並使用一些閾值轉換爲1 bpp。

每個像素的採樣數和位數應爲1.

+0

謝謝。由於輸入圖像已經是單色的,因此thresold應該很簡單。帶材尺寸如何? – Poilaupat

+0

由於你有完整的圖像數據,我建議將它保存爲掃描線,即RowsPerStrip = 1。你可以閱讀[基於掃描線的圖像I/O](http://remotesensing.org/libtiff/libtiff.html) –

+0

此外,如果圖像已經是單色的,那麼你甚至不需要轉換成Y,提取任何一個R,G或B分量,因爲它們全都與Y相等和相同。 –