1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
/*
/--------------------------------------------------------------------
|
| $Id: pldicomdec.cpp,v 1.3 2004/11/09 15:55:06 artcom Exp $
| DICOM Decoder Class
|
| Copyright (c) 1996-2002 Ulrich von Zadow
|
\--------------------------------------------------------------------
*/
#include "pldicomdec.h"
#include "plexcept.h"
#include <dcfilefo.h>
#include <dcrledrg.h>
#include <djdecode.h>
#include <dcmimage.h>
#include <dcistrma.h>
#include <vector>
/*
#ifdef _WIN32
#pragma comment(lib, "ofstd.lib")
#pragma comment(lib, "dcmdata.lib")
#pragma comment(lib, "Netapi32.lib")
#pragma comment(lib, "dcmimage.lib")
#pragma comment(lib, "dcmjpeg.lib")
#pragma comment(lib, "dcmimgle.lib")
#pragma comment(lib, "ijg16.lib")
#pragma comment(lib, "ijg8.lib")
#pragma comment(lib, "ijg12.lib")
#endif
*/
using namespace std;
extern const OFCondition EC_Normal;
extern const OFCondition EC_InvalidStream;
class DICOMTK {
public:
static DICOMTK & get();
~DICOMTK();
private:
DICOMTK();
};
class PLProducer
: public DcmProducer
{
public:
PLProducer(PLDataSource * pDatasource);
OFBool good() const;
OFCondition status() const;
OFBool eos() const;
Uint32 avail() const;
Uint32 read(void *buf, Uint32 buflen);
Uint32 skip(Uint32 skiplen);
void putback(Uint32 num);
Uint32 tell() const;
PLDataSource * GetDataSource() const;
private:
Uint32 m_currentPos;
PLDataSource * m_pSrc;
bool m_bGood;
};
class PLInputStreamFactory
: public DcmInputStreamFactory
{
public:
PLInputStreamFactory(PLDataSource *pSrc, Uint32 initialPos);
DcmInputStream *create() const;
DcmInputStreamFactory *clone() const;
private:
PLDataSource *m_pSrc;
Uint32 m_initialPos;
};
class PLInputStream
: public DcmInputStream
{
public:
PLInputStream(PLProducer *pProducer);
DcmInputStreamFactory *newFactory() const;
private:
PLProducer * m_pProducer;
};
PLDICOMDecoder::PLDICOMDecoder
()
: PLPicDecoder(),
m_di(0),
m_bInvert(false)
// Creates a pDecoder
{
// make sure the DICOM toolkit is initialized
DICOMTK::get();
}
PLDICOMDecoder::~PLDICOMDecoder
()
{
}
void PLDICOMDecoder::Open (PLDataSource * pDataSrc)
{
PLPixelFormat pf = PLPixelFormat::DONTCARE;
DcmFileFormat *dfile = new DcmFileFormat(); // NOTE: this is owned
// by the Image it is going to construct (sic!)
m_pProducer = new PLProducer(pDataSrc);
m_pInputStream = new PLInputStream(m_pProducer);
OFCondition cond = dfile->read(*m_pInputStream, EXS_Unknown,
EGL_withoutGL, DCM_MaxReadLength);
if (cond.bad())
{
cerr<< cond.text() << endl;
throw PLTextException(PL_ERRFORMAT_UNKNOWN, cond.text());
}
E_TransferSyntax xfer = dfile->getDataset()->getOriginalXfer();
unsigned long opt_compatibilityMode =
CIF_MayDetachPixelData | CIF_TakeOverExternalDataset;
m_di = new DicomImage(dfile, xfer, opt_compatibilityMode, 0 /*first frame*/, 0 /* all frames */);
if (m_di->getStatus() != EIS_Normal) {
cerr << DicomImage::getString(m_di->getStatus()) << endl;
throw PLTextException(PL_ERRFORMAT_UNKNOWN, m_di->getString(m_di->getStatus()));
}
/*
const char *SOPClassUID = NULL;
const char *SOPInstanceUID = NULL;
const char *SOPClassText = NULL;
*/
/*E_TransferSyntax xfer = dfile->getDataset()->getOriginalXfer();
const char *XferText = DcmXfer(xfer).getXferName();
*/
/*
dfile->getDataset()->findAndGetString(DCM_SOPClassUID, SOPClassUID);
dfile->getDataset()->findAndGetString(DCM_SOPInstanceUID, SOPInstanceUID);
if (SOPInstanceUID == NULL)
SOPInstanceUID = "not present";
if (SOPClassUID == NULL)
SOPClassText = "not present";
else
SOPClassText = dcmFindNameOfUID(SOPClassUID);
if (SOPClassText == NULL)
SOPClassText = SOPClassUID;
char aspectRatio[30];
OFStandard::ftoa(aspectRatio, sizeof(aspectRatio), m_di->getHeightWidthRatio(), OFStandard::ftoa_format_f, 0, 2);
cerr<< "transfer syntax : " << XferText << endl
<< "SOP class : " << SOPClassText << endl
<< "SOP instance UID : " << SOPInstanceUID << endl << endl
<< "columns x rows : " << m_di->getWidth() << " x " << m_di->getHeight() << endl
<< "bits per sample : " << m_di->getDepth() << endl
<< "color model : " << colorModel << endl;
cerr << "pixel aspect ratio : " << aspectRatio << endl
<< "number of frames : " << m_di->getFrameCount() << endl << endl;
*/
int bpp = m_di->getDepth();
bpp = 8;
switch(m_di->getPhotometricInterpretation()) {
case EPI_Monochrome1: // 0==white
m_bInvert = true;
case EPI_Monochrome2: // 0==black
pf = PLPixelFormat::FromChannels("L", bpp);
break;
case EPI_PaletteColor:
pf = PLPixelFormat::FromChannels("I", bpp);
break;
case EPI_RGB:
pf = PLPixelFormat::FromChannels("RGB", bpp);
break;
case EPI_ARGB:
pf = PLPixelFormat::FromChannels("ARGB", bpp);
break;
default:
throw PLTextException(PL_ERRFORMAT_NOT_SUPPORTED, m_di->getString(m_di->getPhotometricInterpretation()));
}
SetBmpInfo (PLPoint (m_di->getWidth(), m_di->getHeight()),
PLPoint(0, 0), pf);
}
void PLDICOMDecoder::Close ()
{
delete m_di;
delete m_pInputStream;
delete m_pProducer;
PLPicDecoder::Close();
}
void PLDICOMDecoder::GetImage (PLBmpBase & Bmp)
{
PLBYTE ** pLineArray = Bmp.GetLineArray();
unsigned long buffer_size = m_di->getOutputDataSize(8);
vector<char> buffer(buffer_size);
int data_size = m_di->getOutputData(
&buffer[0],
buffer_size, 8, 0
);
int y;
int Height = GetHeight();
int Width = GetWidth();
for (y=0; y < Height; y++)
{
memcpy(pLineArray[y], &buffer[y * Width], Width);
}
Bmp.SetGrayPalette ();
}
// --
DICOMTK & DICOMTK :: get() {
static DICOMTK theDICOMTK;
return theDICOMTK;
}
DICOMTK :: DICOMTK() {
DicomImageClass::setDebugLevel(
DicomImageClass::getDebugLevel() |
DicomImageClass::DL_Informationals |
DicomImageClass::DL_DebugMessages);
// register RLE decompression codec
DcmRLEDecoderRegistration::registerCodecs(OFFalse /*pCreateSOPInstanceUID*/, OFTrue /*debug mode*/);
// register JPEG decompression codecs
DJDecoderRegistration::registerCodecs(EDC_always /*always convert YUV to RGB*/, EUC_default, EPC_default, OFTrue/*debug mode*/);
}
DICOMTK :: ~DICOMTK() {
// deregister RLE decompression codec
DcmRLEDecoderRegistration::cleanup();
// deregister JPEG decompression codecs
DJDecoderRegistration::cleanup();
}
// -- PLDICOMDecoder
PLProducer :: PLProducer(PLDataSource * pDatasource)
: m_pSrc(pDatasource),
m_bGood(true),
m_currentPos(0)
{}
/** returns the status of the producer. Unless the status is good,
* the producer will not permit any operation.
* @return status, true if good
*/
OFBool PLProducer :: good() const
{
return m_bGood;
}
/** returns the status of the producer as an OFCondition object.
* Unless the status is good, the producer will not permit any operation.
* @return status, EC_Normal if good
*/
OFCondition PLProducer :: status() const
{
return m_bGood ? EC_Normal : EC_InvalidStream;
}
/** returns true if the producer is at the end of stream.
* @return true if end of stream, false otherwise
*/
OFBool PLProducer :: eos() const
{
return m_currentPos >= Uint32(m_pSrc->GetFileSize());
}
/** returns the minimum number of bytes that can be read with the
* next call to read(). The DcmObject read methods rely on avail
* to return a value > 0 if there is no I/O suspension since certain
* data such as tag and length are only read "en bloc", i.e. all
* or nothing.
* @return minimum of data available in producer
*/
Uint32 PLProducer :: avail() const
{
return m_pSrc->GetFileSize() - m_currentPos;
}
/** reads as many bytes as possible into the given block.
* @param buf pointer to memory block, must not be NULL
* @param buflen length of memory block
* @return number of bytes actually read.
*/
Uint32 PLProducer :: read(void *buf, Uint32 buflen)
{
m_pSrc->Seek(m_currentPos);
if (buflen > avail()) {
buflen = avail();
}
if (buflen) {
memcpy(buf, m_pSrc->ReadNBytes(buflen), buflen);
m_currentPos += buflen;
}
return buflen;
}
/** skips over the given number of bytes (or less)
* @param skiplen number of bytes to skip
* @return number of bytes actually skipped.
*/
Uint32 PLProducer :: skip(Uint32 skiplen)
{
if (skiplen > avail()) {
skiplen = avail();
} else {
m_currentPos += skiplen;
m_pSrc->Seek(m_currentPos);
}
return skiplen;
}
/** resets the stream to the position by the given number of bytes.
* @param num number of bytes to putback. If the putback operation
* fails, the producer status becomes bad.
*/
void PLProducer :: putback(Uint32 num)
{
if (m_currentPos < num) {
m_bGood = false;
} else {
m_currentPos -= num;
m_pSrc->Seek(m_currentPos);
}
}
Uint32 PLProducer :: tell() const
{
return m_currentPos;
}
PLDataSource * PLProducer :: GetDataSource() const {
return m_pSrc;
}
// -- PLInputStreamFactory
PLInputStreamFactory :: PLInputStreamFactory(PLDataSource *pSrc, Uint32 initialPos)
: m_pSrc(pSrc),
m_initialPos(initialPos)
{}
DcmInputStream * PLInputStreamFactory :: create() const
{
PLProducer *pProducer = new PLProducer(m_pSrc);
pProducer->skip(m_initialPos);
return new PLInputStream(pProducer);
}
DcmInputStreamFactory * PLInputStreamFactory :: clone() const {
return new PLInputStreamFactory(*this);
}
// --
PLInputStream :: PLInputStream(PLProducer *pProducer)
: DcmInputStream(pProducer),
m_pProducer(pProducer)
{}
DcmInputStreamFactory * PLInputStream :: newFactory() const {
return new PLInputStreamFactory(m_pProducer->GetDataSource(), m_pProducer->tell());
}
|