2014-04-08 54 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相等和相同。 –