QImage:读取16位灰度TIFF(Qt,C ++)

时间:2015-08-17 07:04:19

标签: c++ qt tiff qimage libtiff

我想用C ++,Qt和libtiff读取16位灰度图像。我创建了 readTiff 函数(下面),它从tiff读取数据到QImage。但是,存在QImage 5.5 doesn't support 16-bit grayscale的问题。如果我使用RGB16,我只会得到噪音。

如何破解QImage以支持Format_Grayscale16或将数据转换为Format_Grayscale8?

-(IBAction)buttonTapped:(id)sender {
FBSDKAppInviteContent *content = [[FBSDKAppInviteContent alloc] init];
content.appLinkURL = [NSURL URLWithString:@"https://fb.me/115385318808986"];
[FBSDKAppInviteDialog showWithContent:content
                             delegate:self];

}

#pragma mark - FBSDKAppInviteDialogDelegate

- (void)appInviteDialog:(FBSDKAppInviteDialog *)appInviteDialog didCompleteWithResults:(NSDictionary *)results
{
  // Intentionally no-op.
}

- (void)appInviteDialog:(FBSDKAppInviteDialog *)appInviteDialog didFailWithError:(NSError *)error
{
NSLog(@"app invite error:%@", error);
NSString *message = error.userInfo[FBSDKErrorLocalizedDescriptionKey] ?:
@"There was a problem sending the invite, please try again later.";
NSString *title = error.userInfo[FBSDKErrorLocalizedTitleKey] ?: @"Oops!";

[[[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}

1 个答案:

答案 0 :(得分:2)

试试这个它应该有效(我没有测试它,只是从头开始写)

QImage convertGray16TifToQImage(TIFF *tif) {
    // Temporary variables
    uint32 width, height;

    // Read dimensions of image
    if (TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width) != 1) {
        QString msg = "Failed to read width of TIFF: '" + path + "'";
        throw new Exception(NULL, msg, this, __FUNCTION__, __LINE__);
    }
    if (TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height) != 1) {
        QString msg = "Failed to read height of TIFF: '" + path + "'";
        throw new Exception(NULL, msg, this, __FUNCTION__, __LINE__);
    }
    QImage result(width, height, QImage::Format_Grayscale8);

    QVarLengthArray<quint16, 1024> src(width);
    for (uint32 y=0; y<height; ++y) {
         TIFFReadScanline(tiff, src.data(), y, 0);
         quint8 *dst = (quint8 *)result.scanLine(y);
         for (uint32 x=0; x<width; ++x) {
              dst[x] = src[x]>>8; // here you have a room for tweaking color range usage
         }
    }
    return result;
}