12 ビット グレースケール (DICOM:MONOCHROME2) 画像を読み込もうとしています。DICOM RGB ファイルを正常に読み取ることができます。グレースケール イメージを NSBitmapImageRep にロードしようとすると、次のエラー メッセージが表示されます。
Inconsistent set of values to create NSBitmapImageRep
次のコードフラグメントがあります。
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes : nil
pixelsWide : width
pixelsHigh : height
bitsPerSample : bitsStored
samplesPerPixel : 1
hasAlpha : NO
isPlanar : NO
colorSpaceName : NSCalibratedWhiteColorSpace
bytesPerRow : width * bitsAllocated / 8
bitsPerPixel : bitsAllocated];
これらの値で:
width = 256
height = 256
bitsStored = 12
bitsAllocated = 16
私には何も矛盾しているようには見えません。画像が幅×高さ×長さ 2 であることを確認しました。したがって、2 バイトのグレースケール形式であると確信しています。パラメータのさまざまなバリエーションを試しましたが、何も機能しません。「bitsPerSample」を 16 に変更すると、エラー メッセージは表示されなくなりますが、真っ黒な画像が表示されます。私が達成できた最も近い成功は、「bitsPerPixel」をゼロに設定することです。これを行うと、画像は正常に生成されますが、明らかに正しくレンダリングされていません (元の画像はほとんどわかりません)。いくつかの提案をしてください!! 私はこれを機能させるために長い間試み、スタックオーバーフローとWebを(何度も)チェックしました。助けてくれてどうもありがとう!
解決:
LEADTOOLS サポートからの非常に有益な提案の後、問題を解決することができました。動作するコードの一部を次に示します (MONOCHROME2 DICOM 画像を想定):
// If, and only if, MONOCHROME2:
NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes : &pixelData
pixelsWide : width
pixelsHigh : height
bitsPerSample : bitsAllocated /*bitsStored-this will not work*/
samplesPerPixel : samplesPerPixel
hasAlpha : NO
isPlanar : NO
colorSpaceName : NSCalibratedWhiteColorSpace
bytesPerRow : width * bitsAllocated / 8
bitsPerPixel : bitsAllocated];
int scale = USHRT_MAX / largestImagePixelValue;
uint16_t *ptr = (uint16_t *)imageRep.bitmapData;
for (int i = 0; i < width * height; i++) *ptr++ *= scale;