DdsFiles.xml 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <?xml-stylesheet type="text/xsl" href="../../Xsl/doc2html.xsl"?>
  3. <doc>
  4. <title>Loading and Saving DDS Files</title>
  5. <chapter>
  6. <title>Loading and Saving DDS Files</title>
  7. <par>
  8. DirectDraw Surface is Microsoft's file format for storing textures.
  9. You can find information on this file format and on its Imaging support
  10. in <link url="../FileFormats/Dds.xml">Supported File Formats/DDS</link>
  11. section. This section shows how to perform
  12. some load/save operations specific for DDS files (cube map and volume texture support).
  13. Each example consists of description of a action you want to do and
  14. code listing in which the action is carried out by Imaging.
  15. </par>
  16. <scap>Example 1: Simple Loading and Saving</scap>
  17. <par>
  18. You have single texture without mipmaps and you want to load it and
  19. save it.
  20. </par>
  21. <code>
  22. uses
  23. ImagingTypes, Imaging;
  24. var
  25. Img: <ref>TImageData</ref>;
  26. begin
  27. // call this before using any TImageData record
  28. <ref>InitImage</ref>(Img);
  29. // load texture from file
  30. <ref>LoadImageFromFile</ref>('X:\images\tex01.dds', Img);
  31. ...
  32. // do something with the image
  33. ...
  34. // save the image to file
  35. <ref>SaveImageToFile</ref>('X:\images\tex02.dds', Img);
  36. // memory occupied by the image is freed
  37. <ref>FreeImage</ref>(Img);
  38. end.
  39. </code>
  40. <scap>Example 2: Loading and Saving Mipmapped Texture</scap>
  41. <par>
  42. You have single texture with mipmaps and you want to load it and
  43. save it.
  44. </par>
  45. <warn>
  46. Imaging saves mipmaps in the same order as you send them to
  47. <keyword>SaveMultiImageTo*</keyword> functions in <icode>Images</icode> parameter.
  48. Mipmap dimensions (next level has width and height of the previous level divided by two)
  49. and format (must be same as the main image's) are automatically adjusted
  50. during saving.
  51. </warn>
  52. <code>
  53. uses
  54. ImagingTypes, Imaging;
  55. var
  56. ImgArray: <ref>TDynImageDataArray</ref>;
  57. MipMapLevels: LongInt;
  58. begin
  59. // load texture from file
  60. <ref>LoadMultiImageFromFile</ref>('X:\images\tex01mip.dds', ImgArray);
  61. // get the number of mipmap levels in the loaded DDS file
  62. // in this case it is equal to Length(ImgArray)
  63. MipMapLevels := <ref>GetOption</ref>(<ref>ImagingDDSLoadedMipMapCount</ref>);
  64. ...
  65. // do something with the image
  66. ...
  67. // save the texture with mipmaps to file, number of mipmaps saved to
  68. // file will be Length(ImgArray)
  69. <ref>SaveMultiImageToFile</ref>('X:\images\tex02mip.dds', ImgArray);
  70. // memory occupied by the images is freed
  71. <ref>FreeImagesInArray</ref>(ImgArray);
  72. end.
  73. </code>
  74. <scap>Example 3: Loading and Saving Mipmapped Cube Map</scap>
  75. <par>
  76. You have cubic environment map with mipmaps and you want to load it and
  77. save it.
  78. The cube faces in DDS files are written in this order: positive x, negative x,
  79. positive y, negative y, positive z, negative z.
  80. Each face is written with its main image
  81. followed by any mipmap levels. All faces must be the same size
  82. and have the same number of mipmap levels.
  83. </par>
  84. <warn>
  85. Imaging saves cube faces and mipmaps in the same order as you send them to
  86. <keyword>SaveMultiImageTo*</keyword> functions in <icode>Images</icode> parameter.
  87. Make sure that number of images you send to saving functions is equal to
  88. NumberOfFaces * MipMapLevels where these two values are defined by options
  89. interface (see code below). If the number of images is not right then DDS is
  90. saved as simple 2D texture.
  91. Mipmap dimensions (next level has width and height of the previous level divided by two)
  92. and format (must be same as the main image's) are automatically adjusted
  93. during saving.
  94. </warn>
  95. <code>
  96. uses
  97. ImagingTypes, Imaging;
  98. var
  99. ImgArray: <ref>TDynImageDataArray</ref>;
  100. MipMapLevels, Faces: LongInt;
  101. IsCubeMap: Boolean;
  102. begin
  103. // load texture from file
  104. <ref>LoadMultiImageFromFile</ref>('X:\images\tex01cubemip.dds', ImgArray);
  105. // get the number of mipmap levels in the loaded DDS file
  106. // in this case it is equal to Length(ImgArray) div Faces
  107. MipMapLevels := <ref>GetOption</ref>(<ref>ImagingDDSLoadedMipMapCount</ref>);
  108. // check whether we have loaded cube map
  109. IsCubeMap := Boolean(<ref>GetOption</ref>(<ref>ImagingDDSLoadedCubeMap</ref>));
  110. // get the number of cube faces in the loaded DDS file
  111. // Length(ImgArray) = Faces * MipMapLevels
  112. Faces := <ref>GetOption</ref>(<ref>ImagingDDSLoadedDepth</ref>);
  113. ...
  114. // do something with the image
  115. ...
  116. // tell Imaging how many mipmap levels next DDS file should have (for each face)
  117. <ref>SetOption</ref>(<ref>ImagingDDSSaveMipMapCount</ref>, MipMapLevels);
  118. // tell Imaging that the next DDS file should be cubic environment map
  119. <ref>SetOption</ref>(<ref>ImagingDDSSaveCubeMap</ref>, IsCubeMap);
  120. // tell Imaging how many faces next DDS file should have
  121. <ref>SetOption</ref>(<ref>ImagingDDSSaveDepth</ref>, Faces);
  122. // save the cube map with mipmaps to file
  123. <ref>SaveMultiImageToFile</ref>('X:\images\tex02cubemip.dds', ImgArray);
  124. // memory occupied by the images is freed
  125. <ref>FreeImagesInArray</ref>(ImgArray);
  126. end.
  127. </code>
  128. <scap>Example 4: Loading and Saving Mipmapped Volume Texture</scap>
  129. <par>
  130. You have volume texture with mipmaps and you want to load it and
  131. save it.
  132. For volumes without mipmaps,
  133. each depth slice is written to the file in order.
  134. If mipmaps are included, all depth slices for a given mipmap
  135. level are written together, with each level
  136. containing half as many slices as the previous level
  137. with a minimum of 1. Volume textures do not support
  138. DXTn compression as of DirectX 9.0.
  139. </par>
  140. <warn>
  141. Imaging saves volume slices and mipmaps in the same order as you send them to
  142. <keyword>SaveMultiImageTo*</keyword> functions in <icode>Images</icode> parameter.
  143. Make sure that number of images you send to saving functions is right
  144. (it is not NumberOfFaces * MipMapLevels!).
  145. If the number of images is not right then DDS is
  146. saved as simple 2D texture.
  147. Mipmap dimensions (next level has width and height of the previous level divided by two)
  148. and format (must be same as the main image's) are automatically adjusted
  149. during saving.
  150. </warn>
  151. <code>
  152. uses
  153. ImagingTypes, Imaging;
  154. var
  155. ImgArray: <ref>TDynImageDataArray</ref>;
  156. MipMapLevels, Slices: LongInt;
  157. IsVolume: Boolean;
  158. begin
  159. // load texture from file
  160. <ref>LoadMultiImageFromFile</ref>('X:\images\tex01volmip.dds', ImgArray);
  161. // get the number of mipmap levels in the loaded DDS file
  162. // in this case it is NOT equal to Length(ImgArray) div Slices
  163. MipMapLevels := <ref>GetOption</ref>(<ref>ImagingDDSLoadedMipMapCount</ref>);
  164. // check whether we have loaded volume texture
  165. IsVolume := Boolean(<ref>GetOption</ref>(<ref>ImagingDDSLoadedVolume</ref>));
  166. // get the number of volume slices in the loaded DDS file
  167. // Length(ImgArray) &lt;&gt; Slices * MipMapLevels
  168. Slices := <ref>GetOption</ref>(<ref>ImagingDDSLoadedDepth</ref>);
  169. ...
  170. // do something with the image
  171. ...
  172. // tell Imaging how many mipmap levels next DDS file should have
  173. <ref>SetOption</ref>(<ref>ImagingDDSSaveMipMapCount</ref>, MipMapLevels);
  174. // tell Imaging that the next DDS file should be volume texture
  175. <ref>SetOption</ref>(<ref>ImagingDDSSaveVolume</ref>, IsVolume);
  176. // tell Imaging how many slices next DDS file should have
  177. <ref>SetOption</ref>(<ref>ImagingDDSSaveDepth</ref>, Slices);
  178. // save the volume texture with mipmaps to file
  179. <ref>SaveMultiImageToFile</ref>('X:\images\tex02volmip.dds', ImgArray);
  180. // memory occupied by the images is freed
  181. <ref>FreeImagesInArray</ref>(ImgArray);
  182. end.
  183. </code>
  184. </chapter>
  185. </doc>