Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Allow setting DICOM thumbnail dimensions and resize function #910

Merged
merged 1 commit into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 118 additions & 58 deletions Libs/DICOM/Widgets/ctkDICOMThumbnailGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
#include "ctkLogger.h"

// Qt includes
#include <QDebug>
#include <QDir>
#include <QImage>

// DCMTK includes
#include "dcmtk/dcmimgle/dcmimage.h"

static ctkLogger logger ( "org.commontk.dicom.DICOMThumbnailGenerator" );
struct Node;

//------------------------------------------------------------------------------
class ctkDICOMThumbnailGeneratorPrivate
Expand All @@ -44,20 +45,26 @@ class ctkDICOMThumbnailGeneratorPrivate
protected:
ctkDICOMThumbnailGenerator* const q_ptr;

int Width;
int Height;
bool SmoothResize;

private:
Q_DISABLE_COPY( ctkDICOMThumbnailGeneratorPrivate );
};

//------------------------------------------------------------------------------
ctkDICOMThumbnailGeneratorPrivate::ctkDICOMThumbnailGeneratorPrivate(ctkDICOMThumbnailGenerator& o):q_ptr(&o)
ctkDICOMThumbnailGeneratorPrivate::ctkDICOMThumbnailGeneratorPrivate(ctkDICOMThumbnailGenerator& o)
: q_ptr(&o)
, Width(256)
, Height(256)
, SmoothResize(false)
{

}

//------------------------------------------------------------------------------
ctkDICOMThumbnailGeneratorPrivate::~ctkDICOMThumbnailGeneratorPrivate()
{

}


Expand All @@ -74,64 +81,117 @@ ctkDICOMThumbnailGenerator::~ctkDICOMThumbnailGenerator()
}

//------------------------------------------------------------------------------
bool ctkDICOMThumbnailGenerator::generateThumbnail(DicomImage *dcmImage, const QString &path){
QImage image;
// Check whether we have a valid image
EI_Status result = dcmImage->getStatus();
if (result != EIS_Normal)
{
logger.error(QString("Rendering of DICOM image failed for thumbnail failed: ") + DicomImage::getString(result));
return false;
}
// Select first window defined in image. If none, compute min/max window as best guess.
// Only relevant for monochrome.
if (dcmImage->isMonochrome())
{
if (dcmImage->getWindowCount() > 0)
{
dcmImage->setWindow(0);
}
else
{
dcmImage->setMinMaxWindow(OFTrue /* ignore extreme values */);
}
}
/* get image extension and prepare image header */
const unsigned long width = dcmImage->getWidth();
const unsigned long height = dcmImage->getHeight();
unsigned long offset = 0;
unsigned long length = 0;
QString header;

if (dcmImage->isMonochrome())
int ctkDICOMThumbnailGenerator::width()const
{
Q_D(const ctkDICOMThumbnailGenerator);
return d->Width;
}

//------------------------------------------------------------------------------
void ctkDICOMThumbnailGenerator::setWidth(int width)
{
Q_D(ctkDICOMThumbnailGenerator);
d->Width = width;
}

//------------------------------------------------------------------------------
int ctkDICOMThumbnailGenerator::height()const
{
Q_D(const ctkDICOMThumbnailGenerator);
return d->Height;
}

//------------------------------------------------------------------------------
void ctkDICOMThumbnailGenerator::setHeight(int height)
{
Q_D(ctkDICOMThumbnailGenerator);
d->Height = height;
}

//------------------------------------------------------------------------------
bool ctkDICOMThumbnailGenerator::smoothResize()const
{
Q_D(const ctkDICOMThumbnailGenerator);
return d->SmoothResize;
}

//------------------------------------------------------------------------------
void ctkDICOMThumbnailGenerator::setSmoothResize(bool on)
{
Q_D(ctkDICOMThumbnailGenerator);
d->SmoothResize = on;
}

//------------------------------------------------------------------------------
bool ctkDICOMThumbnailGenerator::generateThumbnail(DicomImage *dcmImage, const QString &path)
{
Q_D(ctkDICOMThumbnailGenerator);

QImage image;
// Check whether we have a valid image
EI_Status result = dcmImage->getStatus();
if (result != EIS_Normal)
{
qCritical() << Q_FUNC_INFO << QString("Rendering of DICOM image failed for thumbnail failed: ") + DicomImage::getString(result);
return false;
}
// Select first window defined in image. If none, compute min/max window as best guess.
// Only relevant for monochrome.
if (dcmImage->isMonochrome())
{
if (dcmImage->getWindowCount() > 0)
{
// write PGM header (binary monochrome image format)
header = QString("P5 %1 %2 255\n").arg(width).arg(height);
offset = header.length();
length = width * height + offset;
dcmImage->setWindow(0);
}
else
{
// write PPM header (binary color image format)
header = QString("P6 %1 %2 255\n").arg(width).arg(height);
offset = header.length();
length = width * height * 3 /* RGB */ + offset;
dcmImage->setMinMaxWindow(OFTrue /* ignore extreme values */);
}
/* create output buffer for DicomImage class */
QByteArray buffer;
/* copy header to output buffer and resize it for pixel data */
buffer.append(header);
buffer.resize(length);

/* render pixel data to buffer */
if (dcmImage->getOutputData(static_cast<void *>(buffer.data() + offset), length - offset, 8, 0))
{
if (!image.loadFromData( buffer ))
{
logger.error("QImage couldn't created");
return false;
}
}
/* get image extension and prepare image header */
const unsigned long width = dcmImage->getWidth();
const unsigned long height = dcmImage->getHeight();
unsigned long offset = 0;
unsigned long length = 0;
QString header;

if (dcmImage->isMonochrome())
{
// write PGM header (binary monochrome image format)
header = QString("P5 %1 %2 255\n").arg(width).arg(height);
offset = header.length();
length = width * height + offset;
}
else
{
// write PPM header (binary color image format)
header = QString("P6 %1 %2 255\n").arg(width).arg(height);
offset = header.length();
length = width * height * 3 /* RGB */ + offset;
}
/* create output buffer for DicomImage class */
QByteArray buffer;
/* copy header to output buffer and resize it for pixel data */
buffer.append(header);
buffer.resize(length);

/* render pixel data to buffer */
if (dcmImage->getOutputData(static_cast<void *>(buffer.data() + offset), length - offset, 8, 0))
{
if (!image.loadFromData( buffer ))
{
qCritical() << Q_FUNC_INFO << "QImage couldn't created";
return false;
}
image.scaled(128,128,Qt::KeepAspectRatio).save(path,"PNG");
return true;
}
image.scaled( d->Width, d->Height, Qt::KeepAspectRatio,
(d->SmoothResize ? Qt::SmoothTransformation : Qt::FastTransformation) ).save(path,"PNG");
return true;
}

//------------------------------------------------------------------------------
bool ctkDICOMThumbnailGenerator::generateThumbnail(const QString dcmImagePath, const QString& thumbnailPath)
{
DicomImage dcmImage(QDir::toNativeSeparators(dcmImagePath).toUtf8());
return this->generateThumbnail(&dcmImage, thumbnailPath);
}
23 changes: 21 additions & 2 deletions Libs/DICOM/Widgets/ctkDICOMThumbnailGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,32 @@ class DicomImage;
class CTK_DICOM_WIDGETS_EXPORT ctkDICOMThumbnailGenerator : public ctkDICOMAbstractThumbnailGenerator
{
Q_OBJECT
Q_PROPERTY(int width READ width WRITE setWidth)
Q_PROPERTY(int height READ height WRITE setHeight)
Q_PROPERTY(bool smoothResize READ smoothResize WRITE setSmoothResize)

public:
/// \brief Construct a ctkDICOMThumbnailGenerator object
///
explicit ctkDICOMThumbnailGenerator(QObject* parent = 0);
virtual ~ctkDICOMThumbnailGenerator();

virtual bool generateThumbnail(DicomImage* dcmImage, const QString& path );
virtual bool generateThumbnail(DicomImage* dcmImage, const QString& path);

Q_INVOKABLE bool generateThumbnail(const QString dcmImagePath, const QString& thumbnailPath);

/// Set thumbnail width
void setWidth(int width);
/// Get thumbnail width
int width() const;
/// Set thumbnail height
void setHeight(int height);
/// Get thumbnail height
int height() const;
/// Set thumbnail resize method
/// \param on Smooth resize if true, fast if false. False by default
void setSmoothResize(bool on);
/// Get thumbnail height
bool smoothResize() const;

protected:
QScopedPointer<ctkDICOMThumbnailGeneratorPrivate> d_ptr;
Expand Down