SaveImageDDS thread

Blitz3D Forums/Blitz3D Programming/SaveImageDDS thread

markcw(Posted 2006) [#1]
Hi chaps! I think i've dunnit.

I've been trying to write this DDS stuff to a file for the past couple of days. I've been using various sources to understand the format: the DevIL dds source, FreeImage dds source (read only) and the microsoft dx7 help file and Dxtex source. I found the dx7 help was most useful but the source sometimes was useful too.

Anyway, I just thought it was so cool i should share it in it's current form. It also writes uncompressed dds BUT you can't load this in blitz3d so it's almost useless. Edit: dxt3 and dxt5 are now done.

The function works like SaveImage except it saves dds files. Set the dxt mode with the format parameter, the alpha parameter is now for an alpha image. If you omit it (or set it to 0) you get no alpha if it's dxt1. You can use the image itself or another alpha image. Let me know what you think.

Edit: updated to Tom C's code.

Also, this code is in the code archives here




t3K|Mac(Posted 2006) [#2]
if it works - it will be pretty cool and useful stuff. another great expansion of blitz3d.


markcw(Posted 2006) [#3]
hi t3k|mak, yes it would be cool and maybe even useful to someone! which would be great!

i have just updated the code. it saves dxt1 and dxt1a properly (or at least nearly properly) now so try it out! load in your own images to test it better.

the created image is a little stress test for dxt1a which was giving some problems.

it doesn't work exactly like say, the dxtex tool does (it actually uses bitblt to convert to dds) because i had to write my own algorithm for it. but i think it works nicely now. :)


markcw(Posted 2006) [#4]
just noticed something (which also helped me to fix a small bug in my code).

you can't load a dds texture in blitz3d that's not a power of 2 size without errors!

so if you do load an image and it looks garbled, it's not my bug! :)


markcw(Posted 2006) [#5]
ok, i've updated it again. if i get dx3 and dx5 done i'll put in the code archives, but since it's not finished yet i'll keep it in this thread.

i was thinking it would be better to just make a decls for the DevIL.dll, especially if it supports cubemaps and mipmaps which i don't think i'll be able to figure out. Is there a decls already for it?

Anyway, i've fix quite a few bugs now and it works much better, not quite as good as the Dxtex tool, but still it's very close to it now.


markcw(Posted 2006) [#6]
just fixed another bug and updated this thread again.

hopefully that's it for dxt1. i'll move on to dxt3 now.


jfk EO-11110(Posted 2006) [#7]
Good work! Thanks a lot for this contribution to the community!


markcw(Posted 2006) [#8]
hi jfk, thanks for the feedback.

i've just updated the code again. i don't believe it but i seem to have dxt3 done now. simply because half of it is the same as dxt1. the other half is for transparent/alpha texels and is fairly simple in comparison.

the alpha parameter isn't used for dxt3 because it uses the amount of black to determine the transparency level which is a value of 0..15 per pixel. dxt5 is interpolated and looks a lot more complicated, but i'll see how i get on.


markcw(Posted 2006) [#9]
ok, i've updated the code again.

i have dxt5 done now. it needs tidyed up a bit but it's basically finished. I've changed the alpha flag to an alpha image. if you omit it and use dxt1 format, you get a fully opaque image. if you omit it with dxt3 or dxt5 you get a transparent image. if you pass an alpha image it uses that to set the alpha levels. i decided it needed this alpha image to get the most out of it.


jfk EO-11110(Posted 2006) [#10]
Again a great piece of work. Thanks for sharing!


markcw(Posted 2006) [#11]
small update. i just noticed dxt1a wasn't working quite right. don't know why this way works and the other doesn't, but anyway, as long as it works.


jfk EO-11110(Posted 2006) [#12]

don't know why this way works and the other doesn't, but anyway, as long as it works.


I know what you mean. Sometimes we've got to live with it.


Chroma(Posted 2006) [#13]
Can we load .dds textures now? I just tried and it's a no-go.


Dreamora(Posted 2006) [#14]
DDS is a texture only format, not supported as image format.


markcw(Posted 2006) [#15]
hi Chroma,

you can load dds as image if you use the FreeImage.dll or DevIL.dll.
see here for the FreeImage decls.
http://www.blitzbasic.com/codearcs/codearcs.php?code=1732

I wrote the function as SaveImageDDS because it is most useful when you're editing an image.


Chroma(Posted 2006) [#16]
So, I guess in a sense it IS supported as an image format. /snicker


Dreamora(Posted 2006) [#17]
Not really.

DDS is supported as DXT 1, 3 and 5 (and normally used as such as well)
But 3 and 5 are useless with image as they save alpha data which images don't support at all. For that reason, the saveimageDDS needs 2 images instead of just using true alpha data as well.

Sure for 2D ops, DXT1 wouldn't be bad, but you have to keep in mind that DDS means -> no buffer editing possible. Those color data are static within Blitz3D!


ToM C.(Posted 2007) [#18]
Hi, markcw and all others.
I've improved the code a little bit. Now it's possible to generate and save mipmaps as well.


_33(Posted 2007) [#19]
Hello,

If I understand, this can save a compressed texture as a file in it's native form? Is there any way in blitz3d to handle compressed textures? If I look in LoadTexture, I don't see any way to determine if the texture is compressed in video memory. DXT3 I suppose is quite common in most video cards, but we would need a way to determine if we can use that for our app. I have to go, but when I come back from work, I'll definately look at this closer!


ToM C.(Posted 2007) [#20]
@ _33: no, now you can convert any image file (+ alpha) to compressed DDS textures files and use it afterwards at blitz3d (V1.97)

About any handle to get back the kind of compression (DXT1a etc.) I don't know.


ToM C.(Posted 2007) [#21]
@ markcw
In some cases if I load an image the saved DDS alpha bit of DXT1c will be black (0), althougt format=1 and alpha=0 is used.

*BUG FIXED*


_33(Posted 2007) [#22]
TOM C., I suppose it could be possible to load an uncompressed texture and then compress it to VRAM?


ToM C.(Posted 2007) [#23]
I think only DDS dxt1/3/5 can be compressed at Vram via blitz3d, but I'm not sure.

Please check this topic:

http://www.blitzbasic.com/Community/posts.php?topic=64985#726148


Tom(Posted 2007) [#24]
Hi, I've not read the whole thread, but regarding local/non-local video memory mipmaps...

It's Blitz3Ds behaviour to keep mipmapped textures in non-local video memory (to save space?) A mipmapped texture typicaly only uses about 30% more space than a non-mipmapped texture, I did ask Skid about changing this a while ago but I don't think anything came of it.

Check this (replace texture link with your own, preferably something with lines on the texture so you can see the difference). With the 256 flag, no mipmapping is used even though flag 8 is specified.

Graphics3D 800,600,32,2


cam=CreateCamera()

cube=CreateCube()
cube2=CreateCube()

PositionEntity cube,-2,0,0
PositionEntity cube2,2,0,0
PositionEntity cam,0,0,-10

tex=LoadTexture("C:\Program Files\blitz3d\DX7DLL\brick.png",1+8+256)
tex2=LoadTexture("C:\Program Files\blitz3d\DX7DLL\brick.png",1+8)

EntityTexture cube,tex
EntityTexture cube2,tex2
RenderWorld

WaitKey
End



ToM C.(Posted 2007) [#25]
that seems to be a big lack

otherwise without generating mipmaps inside the DDS file you can't never use mipmaping.
Here an excerpt of the versions.txt:

***** V1.97 *****
...
The mipmap flag is ignored when loading DDS textures. Instead, if the DDS file contains mipmaps, then they are
loaded & used.
...


Tom(Posted 2007) [#26]
woops, I should have known that, I wrote the code for it :)

<self-slap>OW</self-slap>


markcw(Posted 2007) [#27]
Hi Tom C, great job on adding mipmap support! Also, could you clarify where you fixed the bug in DXT1a with a code snippet? Thanks.

_33, if you load dds textures in blitz3d you can't edit them like with read/writepixelfast because they are compressed. If you wanted to generate dds textures you'd have to use saveimagedds and then load the dds file in as a texture.


D4NM4N(Posted 2007) [#28]
Good Job! Im still getting my head around dds as i dont fully understand the capabilities of it yet. What are the other capabilities that it provides other than storing compressed?

BTW, You should add it to the exporter list:
http://blitzmax.com/Community/posts.php?topic=66815
(I know its not saving a mesh but it doesnt matter its still an exporter )


ToM C.(Posted 2007) [#29]
@markcw: Would you add the update to the exporter list, and code archiv.

to the fixed bug:

I found out in some cases the Color0 wasn't the highest and I simply added this:

;make sure color0 is the highest
If color1>color0 Then
     i=color0 : color0=color1 : color1=i ;switch order
EndIf



markcw(Posted 2007) [#30]
> What are the other capabilities that it provides other than storing compressed?

I'm not sure but jfk has a thread somewhere about I think how using dds alpha is faster than uncompressed alpha.

> BTW, You should add it to the exporter list

It seems to be there already. [snip]

Tom C, I have updated the code archive entry now and the first code snippet in this thread. Thanks for clarifying the bug.