⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 udisplay.cpp

📁 dicom file 查看工具 DICOM文件是医疗设备使用的文件格式。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//---------------------------------------------------------------------------
// This file is part of Dicom Explorer, see http://www.sourceforge.net/projects/dcmsee
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.                                 
// 
// This software is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Liu Jie (liucoldstar@yahoo.com)
//
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "uDisplay.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "GR32_Image"
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
__fastcall TfrmDisplay::TfrmDisplay(TComponent* Owner)
        : TForm(Owner)
{
  m_bIsMouseDrag=false;
  m_pBuffer=NULL;
  m_pBitmap32=NULL;

  m_TempFileName=ExtractFilePath(Application->ExeName)+"\\Save.dat";
}
//---------------------------------------------------------------------------
__fastcall TfrmDisplay::~TfrmDisplay()
{
  if(m_pBuffer) delete []m_pBuffer;
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::Clear(void)
{
  if(m_pBuffer) delete []m_pBuffer;

  m_pBuffer=NULL;
  m_pBitmap32=NULL;
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::SetImageInformation(TImageInformation *pImageInformation)
{
  m_pImageInformation=pImageInformation;
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::AllocateMemory(void)
{
  int nSize;
  
  ImgView32->Bitmap->SetSize(m_pImageInformation->nImageWidth,m_pImageInformation->nImageHeight);
  m_pBitmap32=ImgView32->Bitmap;

  nSize=m_pImageInformation->nImageWidth*m_pImageInformation->nImageHeight;

  switch(m_pImageInformation->nImageCompressionType)
  {
    case IMAGE_RAW:
         if (m_pImageInformation->nImageBitsAllocated==16)
         {
           m_pBuffer=new char[nSize*2];
         }
         break;

    case IMAGE_JPEG_LOSSLESS:
         if (m_pImageInformation->nImageBitsAllocated==16)
         {
           m_pBuffer=new char[nSize*2];
         }
         break;
         
    case IMAGE_RLE:
         if (m_pImageInformation->nImageBitsAllocated==16)
         {
           m_pBuffer=new char[nSize*2];
         }
         break;
  }
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::SaveToFile(AnsiString FileName)
{
  ImgView32->Bitmap->SaveToFile(FileName);
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::ZoomIn(void)
{
  if (ImgView32->Scale<32)
  {
    ImgView32->Scale*=1.2;
    ImgView32->Update();
  }
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::ZoomOut(void)
{
  if (ImgView32->Scale>0.3)
  {
    ImgView32->Scale/=1.2;
    ImgView32->Update();
  }
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::FitToHeight(void)
{
  float fHeightRatio;
  fHeightRatio=(float)(ImgView32->Height-18)/ImgView32->Bitmap->Height;
  ImgView32->Scale=fHeightRatio;

  ImgView32->Update();
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::FitToWidth(void)
{
  float fWidthRatio;
  fWidthRatio=(float)(ImgView32->Width-18)/ImgView32->Bitmap->Width;
  ImgView32->Scale=fWidthRatio;

  ImgView32->Update();
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::BestFit(void)
{
  float fHeightRatio,fWidthRatio;

  fHeightRatio=(float)(ImgView32->Height-18)/ImgView32->Bitmap->Height;
  fWidthRatio=(float)(ImgView32->Width-18)/ImgView32->Bitmap->Width;

  if (fHeightRatio>fWidthRatio) ImgView32->Scale=fWidthRatio;
  else ImgView32->Scale=fHeightRatio;

  ImgView32->Update();
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::ActualSize(void)
{
  ImgView32->Scale=1;
  ImgView32->Update();
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::Rotate90(void)
{
  ImgView32->Bitmap->Rotate90();
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::Rotate180(void)
{
  ImgView32->Bitmap->Rotate180();
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::Rotate270(void)
{
  ImgView32->Bitmap->Rotate270();
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::FlipHorz(void)
{
  ImgView32->Bitmap->FlipHorz();
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::FlipVert(void)
{
  ImgView32->Bitmap->FlipVert();
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::DrawImage(void)
{
  int nValue;
  short *pBit;
  int nMax,nMin;
  int nSize;
  int i;
  int nWidth,nCenter;
  
  AllocateMemory();

  switch(m_pImageInformation->nImageCompressionType)
  {
    case IMAGE_RAW:
         if(m_pImageInformation->nImageBitsAllocated==8)
         {
           if ((m_pImageInformation->nImageColorType==COLOR_MONOCHROME1)||
               (m_pImageInformation->nImageColorType==COLOR_MONOCHROME2))
           {
             ReadRawGray8();
           }
           else ReadRawColor();
         }
         else if(m_pImageInformation->nImageBitsAllocated==16)
         {
           ReadRawGray16();
         }
         break;

    case IMAGE_JPEG_LOSSLESS:
         if(m_pImageInformation->nImageBitsAllocated==8)
         {
           if ((m_pImageInformation->nImageColorType==COLOR_MONOCHROME1)||
               (m_pImageInformation->nImageColorType==COLOR_MONOCHROME2))
           {
             ReadJPEGLSGray8();
           }
           else ReadJPEGLSColor();
         }
         else if(m_pImageInformation->nImageBitsAllocated==16)
         {
           ReadJPEGLSGray16();
         }
         break;

    case IMAGE_JPEG_BASELINE:
         ReadJPEG();
         break;
         
    case IMAGE_RLE:
         if(m_pImageInformation->nImageBitsAllocated==8)
         {
           if ((m_pImageInformation->nImageColorType==COLOR_MONOCHROME1)||
               (m_pImageInformation->nImageColorType==COLOR_MONOCHROME2))
           {
             ReadRLEGray8();
           }
           else ReadRLEColor();
         }
         else if(m_pImageInformation->nImageBitsAllocated==16)
         {
           ReadRLEGray16();
         }
         break;
         
    default:
         return;
  }

  AutoContrast();
  ImgView32->Changed();
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::AutoContrast(void)
{
  int nValue;
  short *pBit;
  int nMax,nMin;
  int nSize;
  int i;
  int nWidth,nCenter;

  if (m_pImageInformation->nImageBitsAllocated>8)
  {
    if (m_pImageInformation->nWindowWidth>0)
    {
      m_nWindowCenter=m_pImageInformation->nWindowCenter;
      m_nWindowWidth=m_pImageInformation->nWindowWidth;

      pBit=(short int *)m_pBuffer;
      nValue=pBit[0];
      nMax=nValue;
      nMin=nValue;
      nSize=m_pImageInformation->nImageWidth*m_pImageInformation->nImageHeight;

      for(i=0;i<nSize;i++)
      {
        nValue=pBit[i];
        if (nValue<nMin) nMin=nValue;
        if (nValue>nMax) nMax=nValue;
      }

      m_nMaxValue=nMax;
      m_nMinValue=nMin;

      ScaleToByte(m_pImageInformation->nWindowCenter,m_pImageInformation->nWindowWidth);
    }
    else
    {
      pBit=(short int *)m_pBuffer;
      nValue=pBit[0];
      nMax=nValue;
      nMin=nValue;
      nSize=m_pImageInformation->nImageWidth*m_pImageInformation->nImageHeight;

      for(i=0;i<nSize;i++)
      {
        nValue=pBit[i];
        if (nValue<nMin) nMin=nValue;
        if (nValue>nMax) nMax=nValue;
      }
      nValue=nMax-nMin;
      nCenter=(nMax+nMin)/2;
      nWidth=nMax-nCenter;

      m_nWindowCenter=nCenter;
      m_nWindowWidth=nWidth;
      m_nMaxValue=nMax;
      m_nMinValue=nMin;

      ScaleToByte(nCenter,nWidth);
    }
  }
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::GetCenterWidth(int &nCenter,int &nWidth)
{
  nCenter=m_nWindowCenter;
  nWidth=m_nWindowWidth;
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::GetValueRange(int &nMax,int &nMin)
{
  nMax=m_nMaxValue;
  nMin=m_nMinValue;
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::ScaleToByte(int nWindowCenter,int nWindowWidth)
{
  int nCenter,nWidth;
  int nMin,nMax,nValue;
  int nScale;
  int i;
  unsigned int x,y;
  BYTE Bits;
  short *pWord;

  if (!m_pBuffer) return;

  m_nWindowCenter=nWindowCenter;
  m_nWindowWidth=nWindowWidth;

  nCenter=RescaleToBuffer(nWindowCenter);
  nWidth=abs((nWindowWidth/m_pImageInformation->fRescaleSlope)/2);

  nMin=nCenter-nWidth;
  nMax=nCenter+nWidth;
  nValue=nMax-nMin;

  if (!nValue)
  {
    pWord=(short *)m_pBuffer;

    for(y=0;y<m_pImageInformation->nImageHeight;y++)
    {
      for(x=0;x<m_pImageInformation->nImageWidth;x++)
      {
        if (pWord[x]<nCenter) Bits=0;
        else Bits=255;
        
        m_pBitmap32->Pixel[x][y]=Color32(Bits,Bits,Bits);
      }
      pWord+=m_pImageInformation->nImageWidth;
    }
  }
  else
  {
    nScale=(int)(65536.0/nValue*255.0);
    pWord=(short *)m_pBuffer;

    for(y=0;y<m_pImageInformation->nImageHeight;y++)
    {
      for(x=0;x<m_pImageInformation->nImageWidth;x++)
      {
        if (pWord[x]<nMin) Bits=0;
        else if (pWord[x]>nMax) Bits=255;
        else Bits=((pWord[x]-nMin)*nScale>>16);

        m_pBitmap32->Pixel[x][y]=Color32(Bits,Bits,Bits);
      }
      pWord+=m_pImageInformation->nImageWidth;
    }
  }

  ImgView32->Bitmap->Changed();
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::ReadJPEG(void)
{
   int x,y;
   int nTop;
   BYTE *pLine;
   char ColorR,ColorG,ColorB;

   TJPEGImage *pJpeg;
   Graphics::TBitmap *pBitmap;
   pJpeg=NULL;
   pBitmap=NULL;

   pJpeg=new TJPEGImage();

   pJpeg->LoadFromFile(m_TempFileName);

   pBitmap=new Graphics::TBitmap();

   pBitmap->Height=pJpeg->Height;
   pBitmap->Width=pJpeg->Width;
   pBitmap->PixelFormat=pf24bit;
   pBitmap->Canvas->Draw(0,0,pJpeg);

   for(y=0;y<pBitmap->Height;y++)
   {
     pLine=(BYTE *)pBitmap->ScanLine[y];
     for(x=0;x<pBitmap->Width;x++)
     {
       ColorR=pLine[3*x+2];               //Red
       ColorG=pLine[3*x];                 //Green
       ColorB=pLine[3*x+1];               //Blue
       m_pBitmap32->Pixel[x][y]=Color32(ColorR,ColorG,ColorB);
     }
   }

   if(pBitmap) delete pBitmap;
   if(pJpeg) delete pJpeg;
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::ReadRawGray16(void)
{
  //char Bits;
  //unsigned int x,y;
  //char *pCh;
  int i;
  //char *pBackBuffer;
  TFileStream *pStream;
  unsigned short *pBit;
  //int nMax,nMin,nValue;
  int nSize;

  nSize=m_pImageInformation->nImageWidth*m_pImageInformation->nImageHeight;

  //pBackBuffer=new char[nSize];
  pStream=new TFileStream(m_TempFileName,fmOpenRead);
  pStream->ReadBuffer(m_pBuffer,nSize*2);
  delete pStream;

  pBit=(unsigned short *)m_pBuffer;
  if (m_pImageInformation->nImageLargestPixel>32767)
  {
    for(i=0;i<nSize;i++)
    {
      if (pBit[i]>=0) pBit[i]-=32768;
      else pBit[i]+=32768;
    }
  }
  /*
  pBit=(short int *)m_pBuffer;
  nValue=pBit[0];
  nMax=nValue;
  nMin=nValue;

  for(i=0;i<nSize;i++)
  {
    nValue=pBit[i];
    if (nValue<nMin) nMin=nValue;
    if (nValue>nMax) nMax=nValue;
  }
  nValue=nMax-nMin;

  if (nValue==0) nValue=1;
  for (i=0;i<nSize;i++) pBackBuffer[i]=255*(pBit[i]-nMin)/nValue;

  pCh=pBackBuffer;
  for(y=0;y<m_pImageInformation->nImageHeight;y++)
  {
    for(x=0;x<m_pImageInformation->nImageWidth;x++)
    {
      Bits=pCh[x];
      m_pBitmap32->Pixel[x][y]=Color32(Bits,Bits,Bits);
    }
    pCh+=m_pImageInformation->nImageWidth;
  }
  */
  //delete []pBackBuffer;
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::ReadRawGray8(void)
{
  char *pBuffer;
  char Bits;
  char *pCh;
  unsigned int x,y;
  TFileStream *pStream;

  pBuffer=new char[m_pImageInformation->nImageFileSize];

  pStream=new TFileStream(m_TempFileName,fmOpenRead);
  pStream->ReadBuffer(pBuffer,m_pImageInformation->nImageFileSize);
  delete pStream;

  pCh=pBuffer;
  for(y=0;y<m_pImageInformation->nImageHeight;y++)
  {
    for(x=0;x<m_pImageInformation->nImageWidth;x++)
    {
      Bits=pCh[x];
      m_pBitmap32->Pixel[x][y]=Color32(Bits,Bits,Bits);
    }
    pCh+=m_pImageInformation->nImageWidth;
  }
  
  delete []pBuffer;
}
//---------------------------------------------------------------------------
void __fastcall TfrmDisplay::ReadRawColor(void)
{
  char *pBuffer;
  char ColorR,ColorG,ColorB;
  unsigned int x,y;
  TFileStream *pStream;
  char *pCh;

  pBuffer=new char[m_pImageInformation->nImageFileSize];

  pStream=new TFileStream(m_TempFileName,fmOpenRead);
  pStream->ReadBuffer(pBuffer,m_pImageInformation->nImageFileSize);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -