[vtkusers] Problem using DirectX with vtkImageReslice

Sebastian Schuberth s.schuberth at tu-bs.de
Fri Jun 10 04:26:44 EDT 2005


I've written a small stand-alone program which demonstrates the issue 
I'm having. It would be nice if someone could confirm the behavior, even 
he / she has no solution for it. In the following code, if I define 
CREATE_DEVICE, the contents of input.raw and output.raw are not the 
same. In output.raw, a little more than the top half is just filled with 
zeros, whereas the bottom half seems to be okay. Everything works fine 
if CREATE_DEVICE it not defined, input.raw and output.raw are equal then.
I've tested the code with VTK 4.2, using both VC 6.0 and VC 7.1, and DX 
9.0c April 2005 update.

I would highly appreciate if anyone has an idea of what's going on.

----(begin)----
#include <vtkImageData.h>
#include <vtkImageReslice.h>

#include <windows.h>
#include <d3dx9.h>

#include <stdio.h>

#define WRITE_INPUT
#define CREATE_DEVICE

int main() {
     const int width=512;
     const int height=721;

     float *data=new float[width*height];

     // Create a simple checker-like pattern.
     float *ptr=data;
     for (int y=0;y<height;++y) {
         for (int x=0;x<width;++x) {
             *ptr++=(x&31)^(y&31);
         }
     }

     FILE *f;
     size_t items;

#ifdef WRITE_INPUT
     if (!(f=fopen("C:/input.raw","wb")))
         return -1;

     items=fwrite(data,sizeof(float),width*height,f);
     if (items!=width*height)
         return -1;

     fclose(f);
#endif

     // Create a D3D object and device.
     IDirect3D9 *d3d9obj=Direct3DCreate9(D3D_SDK_VERSION);
     if (!d3d9obj)
         return -1;

     D3DPRESENT_PARAMETERS d3dpp;
     ZeroMemory(&d3dpp,sizeof(d3dpp));
     d3dpp.Windowed               = TRUE;
     d3dpp.SwapEffect             = D3DSWAPEFFECT_DISCARD,
     d3dpp.BackBufferFormat       = D3DFMT_UNKNOWN;
     d3dpp.Flags                  = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
     d3dpp.PresentationInterval   = D3DPRESENT_INTERVAL_IMMEDIATE;
     d3dpp.EnableAutoDepthStencil = FALSE;

#ifdef CREATE_DEVICE
     IDirect3DDevice9 *d3d9dev;
     if 
(FAILED(d3d9obj->CreateDevice(0,D3DDEVTYPE_HAL,GetDesktopWindow(),D3DCREATE_HARDWARE_VERTEXPROCESSING,&d3dpp,&d3d9dev)))
         return -1;
#endif

     // Copy the image to and back from VTK.
     vtkImageData *pvtkImageData=vtkImageData::New();
     pvtkImageData->Initialize();
     pvtkImageData->SetDimensions(width,height,1);
     pvtkImageData->SetScalarType(VTK_FLOAT);
     pvtkImageData->SetNumberOfScalarComponents(1);
 
memcpy(pvtkImageData->GetScalarPointer(),data,width*height*sizeof(float));

     vtkImageReslice *pImageReslice=vtkImageReslice::New();
     pImageReslice->SetInput(pvtkImageData);
     pImageReslice->Update();

     float *output=(float*)pImageReslice->GetOutput()->GetScalarPointer();
     memcpy(data,output,width*height*sizeof(float));

     pImageReslice->Delete();
     pImageReslice=NULL;
     pvtkImageData->Delete();
     pvtkImageData=NULL;

     // Write the output file.
     if (!(f=fopen("C:/output.raw","wb")))
         return -1;

     items=fwrite(data,sizeof(float),width*height,f);
     if (items!=width*height)
         return -1;

     fclose(f);

     delete [] data;

     return 0;
}
----(end)----

-- 
Sebastian Schuberth

Randall Hand wrote:

> I just noticed that you don't call "Initialize" for your vtkImageData 
> anywhere. Could that have something to do with it?
> 
> On 6/9/05, Sebastian Schuberth <s.schuberth at tu-bs.de> wrote:
> 
>>Hi all,
>>
>>I'm having some strange issues with the following code:
>>
>>----(begin)----
>>
>>float *pImageData; // Pointer to a floating-point grayscale image.
>>int nWidth=512,nHeight=721; // Width and height of the image.
>>
>>vtkImageReslice *pImageReslice = vtkImageReslice::New();
>>
>>vtkImageData *pvtkImageData = vtkImageData::New();
>>pvtkImageData->SetDimensions(nWidth,nHeight,1);
>>pvtkImageData->SetScalarType(VTK_FLOAT);
>>pvtkImageData->SetNumberOfScalarComponents(1);
>>
>>memcpy(pvtkImageData->GetScalarPointer(),pImageData,nWidth*nHeight*sizeof(float));
>>
>>pImageReslice->SetInput(pvtkImageData);
>>// Some image transformations were omitted here for simplicity.
>>pImageReslice->Update();
>>float *pfData = (float*)pImageReslice->GetOutput()->GetScalarPointer();
>>memcpy(pImageData,pfData,sizeof(float)*nWidth*nHeight);
>>
>>pvtkImageData->Delete();
>>pvtkImageData= NULL;
>>pImageReslice->Delete();
>>pImageReslice = NULL;
>>
>>----(end)----
>>
>>Because I've omitted some transformation operations, the code above just
>>copies the image from and back to pImageData. It works just fine until I
>>create a DirectX device *before* the code above:
>>
>>----(begin)----
>>
>>HWND m_window_handle=::GetDesktopWindow();
>>
>>IDirect3D9 *m_d3d9_object=Direct3DCreate9(D3D_SDK_VERSION);
>>if (!m_d3d9_object)
>>return;
>>
>>D3DPRESENT_PARAMETERS d3dpp;
>>ZeroMemory(&d3dpp,sizeof(d3dpp));
>>d3dpp.Windowed = TRUE;
>>d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD,
>>d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
>>d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
>>d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
>>d3dpp.EnableAutoDepthStencil = FALSE;
>>
>>IDirect3DDevice9 *m_d3d9_device;
>>if
>>
>>(FAILED(m_d3d9_object->CreateDevice(0,D3DDEVTYPE_HAL,m_window_handle,D3DCREATE_HARDWARE_VERTEXPROCESSING,&d3dpp,
>>&m_d3d9_device))) return;
>>
>>----(end)----
>>
>>If I comment out the line which contains the call to CreateDevice all
>>works fine again. Any ideas what happening? Does vtkImageReslice
>>internally even use DirectX in any way?
>>
>>Thanks for any hints.
>>
>>--
>>Sebastian Schuberth
>>_______________________________________________
>>This is the private VTK discussion list.
>>Please keep messages on-topic. Check the FAQ at: 
>>http://www.vtk.org/Wiki/VTK_FAQ
>>Follow this link to subscribe/unsubscribe:
>>http://www.vtk.org/mailman/listinfo/vtkusers



More information about the vtkusers mailing list