/* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. */

// DisplayDicomDlg.cpp : implementation file
//

#include "stdafx.h"
#include "clientUI.h"
#include "DisplayDicomDlg.h"

#include <imaging.h>
#include <initguid.h>
#include <imgguids.h>
#include <winuser.h>

// DisplayDicomDlg dialog

IMPLEMENT_DYNAMIC(DisplayDicomDlg, CDialog)

DisplayDicomDlg::DisplayDicomDlg(char *image, int size, 
    CWnd* pParent /*=NULL*/)
	: CDialog(DisplayDicomDlg::IDD, pParent)
{
	// Initialize image and its size.
	m_image = image;
	m_size = size;
}

DisplayDicomDlg::~DisplayDicomDlg()
{
}

void DisplayDicomDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


void DisplayDicomDlg::OnPaint()
{
	// The structure of client area of the window.
	PAINTSTRUCT ps;
	
	// Begin to paint on the client area.
	BeginPaint(&ps);
	
	// Get the device and draw the image on the device.
	CDC *pDC =  GetDC(); 
	DrawImage(pDC->GetSafeHdc(), m_image, m_size);

	// Release the device.
	pDC->DeleteDC();
        pDC = NULL;   
        
	// Finish painting on the client area.
        EndPaint(&ps);
}


void DisplayDicomDlg::DrawImage(HDC hdc, void *buffer, int length)
{
	IImagingFactory *pImgFactory = NULL;     // Imaging factory
	IImage *pImage = NULL;                   // Image object
	RECT rc = {0, 0, GetSystemMetrics(SM_CXSCREEN), 
            GetSystemMetrics(SM_CYSCREEN) - 50}; // Rectangle to draw the 
						 // image

	// Calls CoInitialize before calling CoCreateInstance.
        CoInitializeEx(NULL, COINIT_MULTITHREADED);

        // Create the imaging factory.
        if (SUCCEEDED(CoCreateInstance (CLSID_ImagingFactory, NULL, 
                                        CLSCTX_INPROC_SERVER,
                                        IID_IImagingFactory,
                                        (void **)&pImgFactory))) {
                // Load the image from the buffer.
                if (SUCCEEDED(pImgFactory->CreateImageFromBuffer(buffer, 
                    length, BufferDisposalFlagNone, &pImage))) {
                        // Draw the image.
                        pImage->Draw(hdc, &rc, NULL);
                        
                        // Release the image object.
                        pImage->Release();
                }
                
                // Release the image factory.
                pImgFactory->Release();        
        }
        
        // Free resources.
        CoUninitialize();
}


BEGIN_MESSAGE_MAP(DisplayDicomDlg, CDialog)
		ON_WM_PAINT()
		ON_BN_CLICKED(IDOK, &DisplayDicomDlg::OnBnClickedOk)
END_MESSAGE_MAP()


// DisplayDicomDlg message handlers

void DisplayDicomDlg::OnBnClickedOk()
{
	OnOK();
}
