Sign in to follow this  
TobyWilson

[SlimDX] Loading a hunormous .tif

Recommended Posts

It's been a few years since I've done anything with DirectX. I'm in the process of cutting over an Autocad viewer that uses GDI+ to Direct3D with SlimDX.

One of the files references extremely large (7874 * 7874 pixel) .tif images. 10 of them to be exact. Needless to say they're not proving easy to deal with.

I've tried a few unsuccessful methods.
I'd kind of assume that SlimDX.Direct3D9.Texture load methods would use System.Drawing.Image under the hood?

Anyway, this is what I consider to be my most sensible attempt:

[code]
FileStream stream = new FileStream(path, FileMode.Open);
texture = Texture.FromStream(d3d, bmp.g, D3DX.DefaultNonPowerOf2, D3DX.DefaultNonPowerOf2, 0, Usage.None, Format.Unknown, Pool.Managed, Filter.None, Filter.None, 0);
[/code]


I realise ten 62million pixel images might be a lot to ask of this laptop's little onboard graphics chip, but if I can successfully load and use just one of them I can look into optimising. I don't have a lot of other choice as that's what the files contain!
Thankfully I don't need to worry too much about framerate at this stage.

Can anyone advise what may be going wrong? Am I loading incorrectly or are they simply too large and hitting a DirectX limitation?

Share this post


Link to post
Share on other sites
it depends on the max texture size the video card handles. if I remember right, my NVidia 285 has a max of 4096x4096. You might try breaking up each image into several smaller ones

Share this post


Link to post
Share on other sites
It's not so much a DirectX limit as a hardware limit. You can find out what your GPU supports by running the DirectX Caps Viewer. It'll be in Direct3D9 Devices / your hardware's name / D3D Device Types / HAL / Caps, listed as MaxTextureWidth and MaxTextureHeight. The 9800 GT in my work computer reports 8192 for each, but older or mobile GPUs may only support the minimum 4096 required by DirectX.

You could always query the size limit with [url="http://msdn.microsoft.com/en-us/library/bb174320%28v=VS.85%29.aspx"]IDirect3D9::GetDeviceCaps[/url] and shrink the images at load time if they're larger than the limit.

Share this post


Link to post
Share on other sites
Considering you mention an on-board chip on a laptop, I'm going to suggest that your hardware probably cannot load an image that large into video memory. You're looking at ~256 megs of memory just for that one picture, assuming a 4 byte RGBA texture format and no generated mipmaps. It would be entirely impossible to load ten of them, let alone one.

You'll either need to scale the images down to something far more manageable, or chop the image up into smaller fragments and only upload the ones to video memory that you can see on screen.

Share this post


Link to post
Share on other sites
Ok, I'm going down the route of cutting up the images.

At this stage I'm trying to just get something to load. The line where texture is assigned throws the exception 'Index was outside the bounds of the array':

[code]
Bitmap bmp = new Bitmap(path);
Bitmap dest = new Bitmap(4096, 4096);

Graphics ctx = Graphics.FromImage(dest);
ctx.DrawImage(bmp, new Rectangle(0, 0, 4096, 4096), new Rectangle(0, 0, 4096, 4096), GraphicsUnit.Pixel);

MemoryStream stream = new MemoryStream();
dest.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);

texture = Texture.FromStream(d3d, stream, D3DX.DefaultNonPowerOf2, D3DX.DefaultNonPowerOf2, 0, Usage.None, Format.Unknown, Pool.Managed, Filter.None, Filter.None, 0);
[/code]

I'm not necessarily sure that this is even the best way of doing this.
Can anyone advise?

Share this post


Link to post
Share on other sites
You need to reset the read/write pointer in the memory stream. It's pointing to the end of the stream when your call to Save completes:

[code]
stream.Position = 0;
[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this