PageRenderTime 71ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/coders/dds.c

https://gitlab.com/ImageMagick/ImageMagick
C | 3328 lines | 2691 code | 400 blank | 237 comment | 453 complexity | a5d4924118c634266bc284a256ad0aff MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. % %
  4. % %
  5. % %
  6. % DDDD DDDD SSSSS %
  7. % D D D D SS %
  8. % D D D D SSS %
  9. % D D D D SS %
  10. % DDDD DDDD SSSSS %
  11. % %
  12. % %
  13. % Read/Write Microsoft Direct Draw Surface Image Format %
  14. % %
  15. % Software Design %
  16. % Bianca van Schaik %
  17. % March 2008 %
  18. % Dirk Lemstra %
  19. % September 2013 %
  20. % %
  21. % %
  22. % Copyright 1999-2019 ImageMagick Studio LLC, a non-profit organization %
  23. % dedicated to making software imaging solutions freely available. %
  24. % %
  25. % You may not use this file except in compliance with the License. You may %
  26. % obtain a copy of the License at %
  27. % %
  28. % https://imagemagick.org/script/license.php %
  29. % %
  30. % Unless required by applicable law or agreed to in writing, software %
  31. % distributed under the License is distributed on an "AS IS" BASIS, %
  32. % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
  33. % See the License for the specific language governing permissions and %
  34. % limitations under the License. %
  35. % %
  36. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  37. %
  38. %
  39. */
  40. /*
  41. Include declarations.
  42. */
  43. #include "MagickCore/studio.h"
  44. #include "MagickCore/attribute.h"
  45. #include "MagickCore/blob.h"
  46. #include "MagickCore/blob-private.h"
  47. #include "MagickCore/cache.h"
  48. #include "MagickCore/colorspace.h"
  49. #include "MagickCore/colorspace-private.h"
  50. #include "MagickCore/exception.h"
  51. #include "MagickCore/exception-private.h"
  52. #include "MagickCore/image.h"
  53. #include "MagickCore/image-private.h"
  54. #include "MagickCore/list.h"
  55. #include "MagickCore/log.h"
  56. #include "MagickCore/magick.h"
  57. #include "MagickCore/memory_.h"
  58. #include "MagickCore/monitor.h"
  59. #include "MagickCore/monitor-private.h"
  60. #include "MagickCore/option.h"
  61. #include "MagickCore/pixel-accessor.h"
  62. #include "MagickCore/profile.h"
  63. #include "MagickCore/quantum.h"
  64. #include "MagickCore/quantum-private.h"
  65. #include "MagickCore/resource_.h"
  66. #include "MagickCore/static.h"
  67. #include "MagickCore/string_.h"
  68. #include "MagickCore/string-private.h"
  69. #include "MagickCore/module.h"
  70. #include "MagickCore/transform.h"
  71. /*
  72. Definitions
  73. */
  74. #define DDSD_CAPS 0x00000001
  75. #define DDSD_HEIGHT 0x00000002
  76. #define DDSD_WIDTH 0x00000004
  77. #define DDSD_PITCH 0x00000008
  78. #define DDSD_PIXELFORMAT 0x00001000
  79. #define DDSD_MIPMAPCOUNT 0x00020000
  80. #define DDSD_LINEARSIZE 0x00080000
  81. #define DDSD_DEPTH 0x00800000
  82. #define DDPF_ALPHAPIXELS 0x00000001
  83. #define DDPF_FOURCC 0x00000004
  84. #define DDPF_RGB 0x00000040
  85. #define DDPF_LUMINANCE 0x00020000
  86. #define FOURCC_DXT1 0x31545844
  87. #define FOURCC_DXT3 0x33545844
  88. #define FOURCC_DXT5 0x35545844
  89. #define DDSCAPS_COMPLEX 0x00000008
  90. #define DDSCAPS_TEXTURE 0x00001000
  91. #define DDSCAPS_MIPMAP 0x00400000
  92. #define DDSCAPS2_CUBEMAP 0x00000200
  93. #define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
  94. #define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
  95. #define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
  96. #define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
  97. #define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
  98. #define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
  99. #define DDSCAPS2_VOLUME 0x00200000
  100. #ifndef SIZE_MAX
  101. #define SIZE_MAX ((size_t) -1)
  102. #endif
  103. /*
  104. Structure declarations.
  105. */
  106. typedef struct _DDSPixelFormat
  107. {
  108. size_t
  109. flags,
  110. fourcc,
  111. rgb_bitcount,
  112. r_bitmask,
  113. g_bitmask,
  114. b_bitmask,
  115. alpha_bitmask;
  116. } DDSPixelFormat;
  117. typedef struct _DDSInfo
  118. {
  119. size_t
  120. flags,
  121. height,
  122. width,
  123. pitchOrLinearSize,
  124. depth,
  125. mipmapcount,
  126. ddscaps1,
  127. ddscaps2;
  128. DDSPixelFormat
  129. pixelformat;
  130. } DDSInfo;
  131. typedef struct _DDSColors
  132. {
  133. unsigned char
  134. r[4],
  135. g[4],
  136. b[4],
  137. a[4];
  138. } DDSColors;
  139. typedef struct _DDSVector4
  140. {
  141. float
  142. x,
  143. y,
  144. z,
  145. w;
  146. } DDSVector4;
  147. typedef struct _DDSVector3
  148. {
  149. float
  150. x,
  151. y,
  152. z;
  153. } DDSVector3;
  154. typedef struct _DDSSourceBlock
  155. {
  156. unsigned char
  157. start,
  158. end,
  159. error;
  160. } DDSSourceBlock;
  161. typedef struct _DDSSingleColourLookup
  162. {
  163. DDSSourceBlock sources[2];
  164. } DDSSingleColourLookup;
  165. typedef MagickBooleanType
  166. DDSDecoder(const ImageInfo *,Image *,DDSInfo *,const MagickBooleanType,
  167. ExceptionInfo *);
  168. typedef MagickBooleanType
  169. DDSPixelDecoder(Image *,DDSInfo *,ExceptionInfo *);
  170. static const DDSSingleColourLookup DDSLookup_5_4[] =
  171. {
  172. { { { 0, 0, 0 }, { 0, 0, 0 } } },
  173. { { { 0, 0, 1 }, { 0, 1, 1 } } },
  174. { { { 0, 0, 2 }, { 0, 1, 0 } } },
  175. { { { 0, 0, 3 }, { 0, 1, 1 } } },
  176. { { { 0, 0, 4 }, { 0, 2, 1 } } },
  177. { { { 1, 0, 3 }, { 0, 2, 0 } } },
  178. { { { 1, 0, 2 }, { 0, 2, 1 } } },
  179. { { { 1, 0, 1 }, { 0, 3, 1 } } },
  180. { { { 1, 0, 0 }, { 0, 3, 0 } } },
  181. { { { 1, 0, 1 }, { 1, 2, 1 } } },
  182. { { { 1, 0, 2 }, { 1, 2, 0 } } },
  183. { { { 1, 0, 3 }, { 0, 4, 0 } } },
  184. { { { 1, 0, 4 }, { 0, 5, 1 } } },
  185. { { { 2, 0, 3 }, { 0, 5, 0 } } },
  186. { { { 2, 0, 2 }, { 0, 5, 1 } } },
  187. { { { 2, 0, 1 }, { 0, 6, 1 } } },
  188. { { { 2, 0, 0 }, { 0, 6, 0 } } },
  189. { { { 2, 0, 1 }, { 2, 3, 1 } } },
  190. { { { 2, 0, 2 }, { 2, 3, 0 } } },
  191. { { { 2, 0, 3 }, { 0, 7, 0 } } },
  192. { { { 2, 0, 4 }, { 1, 6, 1 } } },
  193. { { { 3, 0, 3 }, { 1, 6, 0 } } },
  194. { { { 3, 0, 2 }, { 0, 8, 0 } } },
  195. { { { 3, 0, 1 }, { 0, 9, 1 } } },
  196. { { { 3, 0, 0 }, { 0, 9, 0 } } },
  197. { { { 3, 0, 1 }, { 0, 9, 1 } } },
  198. { { { 3, 0, 2 }, { 0, 10, 1 } } },
  199. { { { 3, 0, 3 }, { 0, 10, 0 } } },
  200. { { { 3, 0, 4 }, { 2, 7, 1 } } },
  201. { { { 4, 0, 4 }, { 2, 7, 0 } } },
  202. { { { 4, 0, 3 }, { 0, 11, 0 } } },
  203. { { { 4, 0, 2 }, { 1, 10, 1 } } },
  204. { { { 4, 0, 1 }, { 1, 10, 0 } } },
  205. { { { 4, 0, 0 }, { 0, 12, 0 } } },
  206. { { { 4, 0, 1 }, { 0, 13, 1 } } },
  207. { { { 4, 0, 2 }, { 0, 13, 0 } } },
  208. { { { 4, 0, 3 }, { 0, 13, 1 } } },
  209. { { { 4, 0, 4 }, { 0, 14, 1 } } },
  210. { { { 5, 0, 3 }, { 0, 14, 0 } } },
  211. { { { 5, 0, 2 }, { 2, 11, 1 } } },
  212. { { { 5, 0, 1 }, { 2, 11, 0 } } },
  213. { { { 5, 0, 0 }, { 0, 15, 0 } } },
  214. { { { 5, 0, 1 }, { 1, 14, 1 } } },
  215. { { { 5, 0, 2 }, { 1, 14, 0 } } },
  216. { { { 5, 0, 3 }, { 0, 16, 0 } } },
  217. { { { 5, 0, 4 }, { 0, 17, 1 } } },
  218. { { { 6, 0, 3 }, { 0, 17, 0 } } },
  219. { { { 6, 0, 2 }, { 0, 17, 1 } } },
  220. { { { 6, 0, 1 }, { 0, 18, 1 } } },
  221. { { { 6, 0, 0 }, { 0, 18, 0 } } },
  222. { { { 6, 0, 1 }, { 2, 15, 1 } } },
  223. { { { 6, 0, 2 }, { 2, 15, 0 } } },
  224. { { { 6, 0, 3 }, { 0, 19, 0 } } },
  225. { { { 6, 0, 4 }, { 1, 18, 1 } } },
  226. { { { 7, 0, 3 }, { 1, 18, 0 } } },
  227. { { { 7, 0, 2 }, { 0, 20, 0 } } },
  228. { { { 7, 0, 1 }, { 0, 21, 1 } } },
  229. { { { 7, 0, 0 }, { 0, 21, 0 } } },
  230. { { { 7, 0, 1 }, { 0, 21, 1 } } },
  231. { { { 7, 0, 2 }, { 0, 22, 1 } } },
  232. { { { 7, 0, 3 }, { 0, 22, 0 } } },
  233. { { { 7, 0, 4 }, { 2, 19, 1 } } },
  234. { { { 8, 0, 4 }, { 2, 19, 0 } } },
  235. { { { 8, 0, 3 }, { 0, 23, 0 } } },
  236. { { { 8, 0, 2 }, { 1, 22, 1 } } },
  237. { { { 8, 0, 1 }, { 1, 22, 0 } } },
  238. { { { 8, 0, 0 }, { 0, 24, 0 } } },
  239. { { { 8, 0, 1 }, { 0, 25, 1 } } },
  240. { { { 8, 0, 2 }, { 0, 25, 0 } } },
  241. { { { 8, 0, 3 }, { 0, 25, 1 } } },
  242. { { { 8, 0, 4 }, { 0, 26, 1 } } },
  243. { { { 9, 0, 3 }, { 0, 26, 0 } } },
  244. { { { 9, 0, 2 }, { 2, 23, 1 } } },
  245. { { { 9, 0, 1 }, { 2, 23, 0 } } },
  246. { { { 9, 0, 0 }, { 0, 27, 0 } } },
  247. { { { 9, 0, 1 }, { 1, 26, 1 } } },
  248. { { { 9, 0, 2 }, { 1, 26, 0 } } },
  249. { { { 9, 0, 3 }, { 0, 28, 0 } } },
  250. { { { 9, 0, 4 }, { 0, 29, 1 } } },
  251. { { { 10, 0, 3 }, { 0, 29, 0 } } },
  252. { { { 10, 0, 2 }, { 0, 29, 1 } } },
  253. { { { 10, 0, 1 }, { 0, 30, 1 } } },
  254. { { { 10, 0, 0 }, { 0, 30, 0 } } },
  255. { { { 10, 0, 1 }, { 2, 27, 1 } } },
  256. { { { 10, 0, 2 }, { 2, 27, 0 } } },
  257. { { { 10, 0, 3 }, { 0, 31, 0 } } },
  258. { { { 10, 0, 4 }, { 1, 30, 1 } } },
  259. { { { 11, 0, 3 }, { 1, 30, 0 } } },
  260. { { { 11, 0, 2 }, { 4, 24, 0 } } },
  261. { { { 11, 0, 1 }, { 1, 31, 1 } } },
  262. { { { 11, 0, 0 }, { 1, 31, 0 } } },
  263. { { { 11, 0, 1 }, { 1, 31, 1 } } },
  264. { { { 11, 0, 2 }, { 2, 30, 1 } } },
  265. { { { 11, 0, 3 }, { 2, 30, 0 } } },
  266. { { { 11, 0, 4 }, { 2, 31, 1 } } },
  267. { { { 12, 0, 4 }, { 2, 31, 0 } } },
  268. { { { 12, 0, 3 }, { 4, 27, 0 } } },
  269. { { { 12, 0, 2 }, { 3, 30, 1 } } },
  270. { { { 12, 0, 1 }, { 3, 30, 0 } } },
  271. { { { 12, 0, 0 }, { 4, 28, 0 } } },
  272. { { { 12, 0, 1 }, { 3, 31, 1 } } },
  273. { { { 12, 0, 2 }, { 3, 31, 0 } } },
  274. { { { 12, 0, 3 }, { 3, 31, 1 } } },
  275. { { { 12, 0, 4 }, { 4, 30, 1 } } },
  276. { { { 13, 0, 3 }, { 4, 30, 0 } } },
  277. { { { 13, 0, 2 }, { 6, 27, 1 } } },
  278. { { { 13, 0, 1 }, { 6, 27, 0 } } },
  279. { { { 13, 0, 0 }, { 4, 31, 0 } } },
  280. { { { 13, 0, 1 }, { 5, 30, 1 } } },
  281. { { { 13, 0, 2 }, { 5, 30, 0 } } },
  282. { { { 13, 0, 3 }, { 8, 24, 0 } } },
  283. { { { 13, 0, 4 }, { 5, 31, 1 } } },
  284. { { { 14, 0, 3 }, { 5, 31, 0 } } },
  285. { { { 14, 0, 2 }, { 5, 31, 1 } } },
  286. { { { 14, 0, 1 }, { 6, 30, 1 } } },
  287. { { { 14, 0, 0 }, { 6, 30, 0 } } },
  288. { { { 14, 0, 1 }, { 6, 31, 1 } } },
  289. { { { 14, 0, 2 }, { 6, 31, 0 } } },
  290. { { { 14, 0, 3 }, { 8, 27, 0 } } },
  291. { { { 14, 0, 4 }, { 7, 30, 1 } } },
  292. { { { 15, 0, 3 }, { 7, 30, 0 } } },
  293. { { { 15, 0, 2 }, { 8, 28, 0 } } },
  294. { { { 15, 0, 1 }, { 7, 31, 1 } } },
  295. { { { 15, 0, 0 }, { 7, 31, 0 } } },
  296. { { { 15, 0, 1 }, { 7, 31, 1 } } },
  297. { { { 15, 0, 2 }, { 8, 30, 1 } } },
  298. { { { 15, 0, 3 }, { 8, 30, 0 } } },
  299. { { { 15, 0, 4 }, { 10, 27, 1 } } },
  300. { { { 16, 0, 4 }, { 10, 27, 0 } } },
  301. { { { 16, 0, 3 }, { 8, 31, 0 } } },
  302. { { { 16, 0, 2 }, { 9, 30, 1 } } },
  303. { { { 16, 0, 1 }, { 9, 30, 0 } } },
  304. { { { 16, 0, 0 }, { 12, 24, 0 } } },
  305. { { { 16, 0, 1 }, { 9, 31, 1 } } },
  306. { { { 16, 0, 2 }, { 9, 31, 0 } } },
  307. { { { 16, 0, 3 }, { 9, 31, 1 } } },
  308. { { { 16, 0, 4 }, { 10, 30, 1 } } },
  309. { { { 17, 0, 3 }, { 10, 30, 0 } } },
  310. { { { 17, 0, 2 }, { 10, 31, 1 } } },
  311. { { { 17, 0, 1 }, { 10, 31, 0 } } },
  312. { { { 17, 0, 0 }, { 12, 27, 0 } } },
  313. { { { 17, 0, 1 }, { 11, 30, 1 } } },
  314. { { { 17, 0, 2 }, { 11, 30, 0 } } },
  315. { { { 17, 0, 3 }, { 12, 28, 0 } } },
  316. { { { 17, 0, 4 }, { 11, 31, 1 } } },
  317. { { { 18, 0, 3 }, { 11, 31, 0 } } },
  318. { { { 18, 0, 2 }, { 11, 31, 1 } } },
  319. { { { 18, 0, 1 }, { 12, 30, 1 } } },
  320. { { { 18, 0, 0 }, { 12, 30, 0 } } },
  321. { { { 18, 0, 1 }, { 14, 27, 1 } } },
  322. { { { 18, 0, 2 }, { 14, 27, 0 } } },
  323. { { { 18, 0, 3 }, { 12, 31, 0 } } },
  324. { { { 18, 0, 4 }, { 13, 30, 1 } } },
  325. { { { 19, 0, 3 }, { 13, 30, 0 } } },
  326. { { { 19, 0, 2 }, { 16, 24, 0 } } },
  327. { { { 19, 0, 1 }, { 13, 31, 1 } } },
  328. { { { 19, 0, 0 }, { 13, 31, 0 } } },
  329. { { { 19, 0, 1 }, { 13, 31, 1 } } },
  330. { { { 19, 0, 2 }, { 14, 30, 1 } } },
  331. { { { 19, 0, 3 }, { 14, 30, 0 } } },
  332. { { { 19, 0, 4 }, { 14, 31, 1 } } },
  333. { { { 20, 0, 4 }, { 14, 31, 0 } } },
  334. { { { 20, 0, 3 }, { 16, 27, 0 } } },
  335. { { { 20, 0, 2 }, { 15, 30, 1 } } },
  336. { { { 20, 0, 1 }, { 15, 30, 0 } } },
  337. { { { 20, 0, 0 }, { 16, 28, 0 } } },
  338. { { { 20, 0, 1 }, { 15, 31, 1 } } },
  339. { { { 20, 0, 2 }, { 15, 31, 0 } } },
  340. { { { 20, 0, 3 }, { 15, 31, 1 } } },
  341. { { { 20, 0, 4 }, { 16, 30, 1 } } },
  342. { { { 21, 0, 3 }, { 16, 30, 0 } } },
  343. { { { 21, 0, 2 }, { 18, 27, 1 } } },
  344. { { { 21, 0, 1 }, { 18, 27, 0 } } },
  345. { { { 21, 0, 0 }, { 16, 31, 0 } } },
  346. { { { 21, 0, 1 }, { 17, 30, 1 } } },
  347. { { { 21, 0, 2 }, { 17, 30, 0 } } },
  348. { { { 21, 0, 3 }, { 20, 24, 0 } } },
  349. { { { 21, 0, 4 }, { 17, 31, 1 } } },
  350. { { { 22, 0, 3 }, { 17, 31, 0 } } },
  351. { { { 22, 0, 2 }, { 17, 31, 1 } } },
  352. { { { 22, 0, 1 }, { 18, 30, 1 } } },
  353. { { { 22, 0, 0 }, { 18, 30, 0 } } },
  354. { { { 22, 0, 1 }, { 18, 31, 1 } } },
  355. { { { 22, 0, 2 }, { 18, 31, 0 } } },
  356. { { { 22, 0, 3 }, { 20, 27, 0 } } },
  357. { { { 22, 0, 4 }, { 19, 30, 1 } } },
  358. { { { 23, 0, 3 }, { 19, 30, 0 } } },
  359. { { { 23, 0, 2 }, { 20, 28, 0 } } },
  360. { { { 23, 0, 1 }, { 19, 31, 1 } } },
  361. { { { 23, 0, 0 }, { 19, 31, 0 } } },
  362. { { { 23, 0, 1 }, { 19, 31, 1 } } },
  363. { { { 23, 0, 2 }, { 20, 30, 1 } } },
  364. { { { 23, 0, 3 }, { 20, 30, 0 } } },
  365. { { { 23, 0, 4 }, { 22, 27, 1 } } },
  366. { { { 24, 0, 4 }, { 22, 27, 0 } } },
  367. { { { 24, 0, 3 }, { 20, 31, 0 } } },
  368. { { { 24, 0, 2 }, { 21, 30, 1 } } },
  369. { { { 24, 0, 1 }, { 21, 30, 0 } } },
  370. { { { 24, 0, 0 }, { 24, 24, 0 } } },
  371. { { { 24, 0, 1 }, { 21, 31, 1 } } },
  372. { { { 24, 0, 2 }, { 21, 31, 0 } } },
  373. { { { 24, 0, 3 }, { 21, 31, 1 } } },
  374. { { { 24, 0, 4 }, { 22, 30, 1 } } },
  375. { { { 25, 0, 3 }, { 22, 30, 0 } } },
  376. { { { 25, 0, 2 }, { 22, 31, 1 } } },
  377. { { { 25, 0, 1 }, { 22, 31, 0 } } },
  378. { { { 25, 0, 0 }, { 24, 27, 0 } } },
  379. { { { 25, 0, 1 }, { 23, 30, 1 } } },
  380. { { { 25, 0, 2 }, { 23, 30, 0 } } },
  381. { { { 25, 0, 3 }, { 24, 28, 0 } } },
  382. { { { 25, 0, 4 }, { 23, 31, 1 } } },
  383. { { { 26, 0, 3 }, { 23, 31, 0 } } },
  384. { { { 26, 0, 2 }, { 23, 31, 1 } } },
  385. { { { 26, 0, 1 }, { 24, 30, 1 } } },
  386. { { { 26, 0, 0 }, { 24, 30, 0 } } },
  387. { { { 26, 0, 1 }, { 26, 27, 1 } } },
  388. { { { 26, 0, 2 }, { 26, 27, 0 } } },
  389. { { { 26, 0, 3 }, { 24, 31, 0 } } },
  390. { { { 26, 0, 4 }, { 25, 30, 1 } } },
  391. { { { 27, 0, 3 }, { 25, 30, 0 } } },
  392. { { { 27, 0, 2 }, { 28, 24, 0 } } },
  393. { { { 27, 0, 1 }, { 25, 31, 1 } } },
  394. { { { 27, 0, 0 }, { 25, 31, 0 } } },
  395. { { { 27, 0, 1 }, { 25, 31, 1 } } },
  396. { { { 27, 0, 2 }, { 26, 30, 1 } } },
  397. { { { 27, 0, 3 }, { 26, 30, 0 } } },
  398. { { { 27, 0, 4 }, { 26, 31, 1 } } },
  399. { { { 28, 0, 4 }, { 26, 31, 0 } } },
  400. { { { 28, 0, 3 }, { 28, 27, 0 } } },
  401. { { { 28, 0, 2 }, { 27, 30, 1 } } },
  402. { { { 28, 0, 1 }, { 27, 30, 0 } } },
  403. { { { 28, 0, 0 }, { 28, 28, 0 } } },
  404. { { { 28, 0, 1 }, { 27, 31, 1 } } },
  405. { { { 28, 0, 2 }, { 27, 31, 0 } } },
  406. { { { 28, 0, 3 }, { 27, 31, 1 } } },
  407. { { { 28, 0, 4 }, { 28, 30, 1 } } },
  408. { { { 29, 0, 3 }, { 28, 30, 0 } } },
  409. { { { 29, 0, 2 }, { 30, 27, 1 } } },
  410. { { { 29, 0, 1 }, { 30, 27, 0 } } },
  411. { { { 29, 0, 0 }, { 28, 31, 0 } } },
  412. { { { 29, 0, 1 }, { 29, 30, 1 } } },
  413. { { { 29, 0, 2 }, { 29, 30, 0 } } },
  414. { { { 29, 0, 3 }, { 29, 30, 1 } } },
  415. { { { 29, 0, 4 }, { 29, 31, 1 } } },
  416. { { { 30, 0, 3 }, { 29, 31, 0 } } },
  417. { { { 30, 0, 2 }, { 29, 31, 1 } } },
  418. { { { 30, 0, 1 }, { 30, 30, 1 } } },
  419. { { { 30, 0, 0 }, { 30, 30, 0 } } },
  420. { { { 30, 0, 1 }, { 30, 31, 1 } } },
  421. { { { 30, 0, 2 }, { 30, 31, 0 } } },
  422. { { { 30, 0, 3 }, { 30, 31, 1 } } },
  423. { { { 30, 0, 4 }, { 31, 30, 1 } } },
  424. { { { 31, 0, 3 }, { 31, 30, 0 } } },
  425. { { { 31, 0, 2 }, { 31, 30, 1 } } },
  426. { { { 31, 0, 1 }, { 31, 31, 1 } } },
  427. { { { 31, 0, 0 }, { 31, 31, 0 } } }
  428. };
  429. static const DDSSingleColourLookup DDSLookup_6_4[] =
  430. {
  431. { { { 0, 0, 0 }, { 0, 0, 0 } } },
  432. { { { 0, 0, 1 }, { 0, 1, 0 } } },
  433. { { { 0, 0, 2 }, { 0, 2, 0 } } },
  434. { { { 1, 0, 1 }, { 0, 3, 1 } } },
  435. { { { 1, 0, 0 }, { 0, 3, 0 } } },
  436. { { { 1, 0, 1 }, { 0, 4, 0 } } },
  437. { { { 1, 0, 2 }, { 0, 5, 0 } } },
  438. { { { 2, 0, 1 }, { 0, 6, 1 } } },
  439. { { { 2, 0, 0 }, { 0, 6, 0 } } },
  440. { { { 2, 0, 1 }, { 0, 7, 0 } } },
  441. { { { 2, 0, 2 }, { 0, 8, 0 } } },
  442. { { { 3, 0, 1 }, { 0, 9, 1 } } },
  443. { { { 3, 0, 0 }, { 0, 9, 0 } } },
  444. { { { 3, 0, 1 }, { 0, 10, 0 } } },
  445. { { { 3, 0, 2 }, { 0, 11, 0 } } },
  446. { { { 4, 0, 1 }, { 0, 12, 1 } } },
  447. { { { 4, 0, 0 }, { 0, 12, 0 } } },
  448. { { { 4, 0, 1 }, { 0, 13, 0 } } },
  449. { { { 4, 0, 2 }, { 0, 14, 0 } } },
  450. { { { 5, 0, 1 }, { 0, 15, 1 } } },
  451. { { { 5, 0, 0 }, { 0, 15, 0 } } },
  452. { { { 5, 0, 1 }, { 0, 16, 0 } } },
  453. { { { 5, 0, 2 }, { 1, 15, 0 } } },
  454. { { { 6, 0, 1 }, { 0, 17, 0 } } },
  455. { { { 6, 0, 0 }, { 0, 18, 0 } } },
  456. { { { 6, 0, 1 }, { 0, 19, 0 } } },
  457. { { { 6, 0, 2 }, { 3, 14, 0 } } },
  458. { { { 7, 0, 1 }, { 0, 20, 0 } } },
  459. { { { 7, 0, 0 }, { 0, 21, 0 } } },
  460. { { { 7, 0, 1 }, { 0, 22, 0 } } },
  461. { { { 7, 0, 2 }, { 4, 15, 0 } } },
  462. { { { 8, 0, 1 }, { 0, 23, 0 } } },
  463. { { { 8, 0, 0 }, { 0, 24, 0 } } },
  464. { { { 8, 0, 1 }, { 0, 25, 0 } } },
  465. { { { 8, 0, 2 }, { 6, 14, 0 } } },
  466. { { { 9, 0, 1 }, { 0, 26, 0 } } },
  467. { { { 9, 0, 0 }, { 0, 27, 0 } } },
  468. { { { 9, 0, 1 }, { 0, 28, 0 } } },
  469. { { { 9, 0, 2 }, { 7, 15, 0 } } },
  470. { { { 10, 0, 1 }, { 0, 29, 0 } } },
  471. { { { 10, 0, 0 }, { 0, 30, 0 } } },
  472. { { { 10, 0, 1 }, { 0, 31, 0 } } },
  473. { { { 10, 0, 2 }, { 9, 14, 0 } } },
  474. { { { 11, 0, 1 }, { 0, 32, 0 } } },
  475. { { { 11, 0, 0 }, { 0, 33, 0 } } },
  476. { { { 11, 0, 1 }, { 2, 30, 0 } } },
  477. { { { 11, 0, 2 }, { 0, 34, 0 } } },
  478. { { { 12, 0, 1 }, { 0, 35, 0 } } },
  479. { { { 12, 0, 0 }, { 0, 36, 0 } } },
  480. { { { 12, 0, 1 }, { 3, 31, 0 } } },
  481. { { { 12, 0, 2 }, { 0, 37, 0 } } },
  482. { { { 13, 0, 1 }, { 0, 38, 0 } } },
  483. { { { 13, 0, 0 }, { 0, 39, 0 } } },
  484. { { { 13, 0, 1 }, { 5, 30, 0 } } },
  485. { { { 13, 0, 2 }, { 0, 40, 0 } } },
  486. { { { 14, 0, 1 }, { 0, 41, 0 } } },
  487. { { { 14, 0, 0 }, { 0, 42, 0 } } },
  488. { { { 14, 0, 1 }, { 6, 31, 0 } } },
  489. { { { 14, 0, 2 }, { 0, 43, 0 } } },
  490. { { { 15, 0, 1 }, { 0, 44, 0 } } },
  491. { { { 15, 0, 0 }, { 0, 45, 0 } } },
  492. { { { 15, 0, 1 }, { 8, 30, 0 } } },
  493. { { { 15, 0, 2 }, { 0, 46, 0 } } },
  494. { { { 16, 0, 2 }, { 0, 47, 0 } } },
  495. { { { 16, 0, 1 }, { 1, 46, 0 } } },
  496. { { { 16, 0, 0 }, { 0, 48, 0 } } },
  497. { { { 16, 0, 1 }, { 0, 49, 0 } } },
  498. { { { 16, 0, 2 }, { 0, 50, 0 } } },
  499. { { { 17, 0, 1 }, { 2, 47, 0 } } },
  500. { { { 17, 0, 0 }, { 0, 51, 0 } } },
  501. { { { 17, 0, 1 }, { 0, 52, 0 } } },
  502. { { { 17, 0, 2 }, { 0, 53, 0 } } },
  503. { { { 18, 0, 1 }, { 4, 46, 0 } } },
  504. { { { 18, 0, 0 }, { 0, 54, 0 } } },
  505. { { { 18, 0, 1 }, { 0, 55, 0 } } },
  506. { { { 18, 0, 2 }, { 0, 56, 0 } } },
  507. { { { 19, 0, 1 }, { 5, 47, 0 } } },
  508. { { { 19, 0, 0 }, { 0, 57, 0 } } },
  509. { { { 19, 0, 1 }, { 0, 58, 0 } } },
  510. { { { 19, 0, 2 }, { 0, 59, 0 } } },
  511. { { { 20, 0, 1 }, { 7, 46, 0 } } },
  512. { { { 20, 0, 0 }, { 0, 60, 0 } } },
  513. { { { 20, 0, 1 }, { 0, 61, 0 } } },
  514. { { { 20, 0, 2 }, { 0, 62, 0 } } },
  515. { { { 21, 0, 1 }, { 8, 47, 0 } } },
  516. { { { 21, 0, 0 }, { 0, 63, 0 } } },
  517. { { { 21, 0, 1 }, { 1, 62, 0 } } },
  518. { { { 21, 0, 2 }, { 1, 63, 0 } } },
  519. { { { 22, 0, 1 }, { 10, 46, 0 } } },
  520. { { { 22, 0, 0 }, { 2, 62, 0 } } },
  521. { { { 22, 0, 1 }, { 2, 63, 0 } } },
  522. { { { 22, 0, 2 }, { 3, 62, 0 } } },
  523. { { { 23, 0, 1 }, { 11, 47, 0 } } },
  524. { { { 23, 0, 0 }, { 3, 63, 0 } } },
  525. { { { 23, 0, 1 }, { 4, 62, 0 } } },
  526. { { { 23, 0, 2 }, { 4, 63, 0 } } },
  527. { { { 24, 0, 1 }, { 13, 46, 0 } } },
  528. { { { 24, 0, 0 }, { 5, 62, 0 } } },
  529. { { { 24, 0, 1 }, { 5, 63, 0 } } },
  530. { { { 24, 0, 2 }, { 6, 62, 0 } } },
  531. { { { 25, 0, 1 }, { 14, 47, 0 } } },
  532. { { { 25, 0, 0 }, { 6, 63, 0 } } },
  533. { { { 25, 0, 1 }, { 7, 62, 0 } } },
  534. { { { 25, 0, 2 }, { 7, 63, 0 } } },
  535. { { { 26, 0, 1 }, { 16, 45, 0 } } },
  536. { { { 26, 0, 0 }, { 8, 62, 0 } } },
  537. { { { 26, 0, 1 }, { 8, 63, 0 } } },
  538. { { { 26, 0, 2 }, { 9, 62, 0 } } },
  539. { { { 27, 0, 1 }, { 16, 48, 0 } } },
  540. { { { 27, 0, 0 }, { 9, 63, 0 } } },
  541. { { { 27, 0, 1 }, { 10, 62, 0 } } },
  542. { { { 27, 0, 2 }, { 10, 63, 0 } } },
  543. { { { 28, 0, 1 }, { 16, 51, 0 } } },
  544. { { { 28, 0, 0 }, { 11, 62, 0 } } },
  545. { { { 28, 0, 1 }, { 11, 63, 0 } } },
  546. { { { 28, 0, 2 }, { 12, 62, 0 } } },
  547. { { { 29, 0, 1 }, { 16, 54, 0 } } },
  548. { { { 29, 0, 0 }, { 12, 63, 0 } } },
  549. { { { 29, 0, 1 }, { 13, 62, 0 } } },
  550. { { { 29, 0, 2 }, { 13, 63, 0 } } },
  551. { { { 30, 0, 1 }, { 16, 57, 0 } } },
  552. { { { 30, 0, 0 }, { 14, 62, 0 } } },
  553. { { { 30, 0, 1 }, { 14, 63, 0 } } },
  554. { { { 30, 0, 2 }, { 15, 62, 0 } } },
  555. { { { 31, 0, 1 }, { 16, 60, 0 } } },
  556. { { { 31, 0, 0 }, { 15, 63, 0 } } },
  557. { { { 31, 0, 1 }, { 24, 46, 0 } } },
  558. { { { 31, 0, 2 }, { 16, 62, 0 } } },
  559. { { { 32, 0, 2 }, { 16, 63, 0 } } },
  560. { { { 32, 0, 1 }, { 17, 62, 0 } } },
  561. { { { 32, 0, 0 }, { 25, 47, 0 } } },
  562. { { { 32, 0, 1 }, { 17, 63, 0 } } },
  563. { { { 32, 0, 2 }, { 18, 62, 0 } } },
  564. { { { 33, 0, 1 }, { 18, 63, 0 } } },
  565. { { { 33, 0, 0 }, { 27, 46, 0 } } },
  566. { { { 33, 0, 1 }, { 19, 62, 0 } } },
  567. { { { 33, 0, 2 }, { 19, 63, 0 } } },
  568. { { { 34, 0, 1 }, { 20, 62, 0 } } },
  569. { { { 34, 0, 0 }, { 28, 47, 0 } } },
  570. { { { 34, 0, 1 }, { 20, 63, 0 } } },
  571. { { { 34, 0, 2 }, { 21, 62, 0 } } },
  572. { { { 35, 0, 1 }, { 21, 63, 0 } } },
  573. { { { 35, 0, 0 }, { 30, 46, 0 } } },
  574. { { { 35, 0, 1 }, { 22, 62, 0 } } },
  575. { { { 35, 0, 2 }, { 22, 63, 0 } } },
  576. { { { 36, 0, 1 }, { 23, 62, 0 } } },
  577. { { { 36, 0, 0 }, { 31, 47, 0 } } },
  578. { { { 36, 0, 1 }, { 23, 63, 0 } } },
  579. { { { 36, 0, 2 }, { 24, 62, 0 } } },
  580. { { { 37, 0, 1 }, { 24, 63, 0 } } },
  581. { { { 37, 0, 0 }, { 32, 47, 0 } } },
  582. { { { 37, 0, 1 }, { 25, 62, 0 } } },
  583. { { { 37, 0, 2 }, { 25, 63, 0 } } },
  584. { { { 38, 0, 1 }, { 26, 62, 0 } } },
  585. { { { 38, 0, 0 }, { 32, 50, 0 } } },
  586. { { { 38, 0, 1 }, { 26, 63, 0 } } },
  587. { { { 38, 0, 2 }, { 27, 62, 0 } } },
  588. { { { 39, 0, 1 }, { 27, 63, 0 } } },
  589. { { { 39, 0, 0 }, { 32, 53, 0 } } },
  590. { { { 39, 0, 1 }, { 28, 62, 0 } } },
  591. { { { 39, 0, 2 }, { 28, 63, 0 } } },
  592. { { { 40, 0, 1 }, { 29, 62, 0 } } },
  593. { { { 40, 0, 0 }, { 32, 56, 0 } } },
  594. { { { 40, 0, 1 }, { 29, 63, 0 } } },
  595. { { { 40, 0, 2 }, { 30, 62, 0 } } },
  596. { { { 41, 0, 1 }, { 30, 63, 0 } } },
  597. { { { 41, 0, 0 }, { 32, 59, 0 } } },
  598. { { { 41, 0, 1 }, { 31, 62, 0 } } },
  599. { { { 41, 0, 2 }, { 31, 63, 0 } } },
  600. { { { 42, 0, 1 }, { 32, 61, 0 } } },
  601. { { { 42, 0, 0 }, { 32, 62, 0 } } },
  602. { { { 42, 0, 1 }, { 32, 63, 0 } } },
  603. { { { 42, 0, 2 }, { 41, 46, 0 } } },
  604. { { { 43, 0, 1 }, { 33, 62, 0 } } },
  605. { { { 43, 0, 0 }, { 33, 63, 0 } } },
  606. { { { 43, 0, 1 }, { 34, 62, 0 } } },
  607. { { { 43, 0, 2 }, { 42, 47, 0 } } },
  608. { { { 44, 0, 1 }, { 34, 63, 0 } } },
  609. { { { 44, 0, 0 }, { 35, 62, 0 } } },
  610. { { { 44, 0, 1 }, { 35, 63, 0 } } },
  611. { { { 44, 0, 2 }, { 44, 46, 0 } } },
  612. { { { 45, 0, 1 }, { 36, 62, 0 } } },
  613. { { { 45, 0, 0 }, { 36, 63, 0 } } },
  614. { { { 45, 0, 1 }, { 37, 62, 0 } } },
  615. { { { 45, 0, 2 }, { 45, 47, 0 } } },
  616. { { { 46, 0, 1 }, { 37, 63, 0 } } },
  617. { { { 46, 0, 0 }, { 38, 62, 0 } } },
  618. { { { 46, 0, 1 }, { 38, 63, 0 } } },
  619. { { { 46, 0, 2 }, { 47, 46, 0 } } },
  620. { { { 47, 0, 1 }, { 39, 62, 0 } } },
  621. { { { 47, 0, 0 }, { 39, 63, 0 } } },
  622. { { { 47, 0, 1 }, { 40, 62, 0 } } },
  623. { { { 47, 0, 2 }, { 48, 46, 0 } } },
  624. { { { 48, 0, 2 }, { 40, 63, 0 } } },
  625. { { { 48, 0, 1 }, { 41, 62, 0 } } },
  626. { { { 48, 0, 0 }, { 41, 63, 0 } } },
  627. { { { 48, 0, 1 }, { 48, 49, 0 } } },
  628. { { { 48, 0, 2 }, { 42, 62, 0 } } },
  629. { { { 49, 0, 1 }, { 42, 63, 0 } } },
  630. { { { 49, 0, 0 }, { 43, 62, 0 } } },
  631. { { { 49, 0, 1 }, { 48, 52, 0 } } },
  632. { { { 49, 0, 2 }, { 43, 63, 0 } } },
  633. { { { 50, 0, 1 }, { 44, 62, 0 } } },
  634. { { { 50, 0, 0 }, { 44, 63, 0 } } },
  635. { { { 50, 0, 1 }, { 48, 55, 0 } } },
  636. { { { 50, 0, 2 }, { 45, 62, 0 } } },
  637. { { { 51, 0, 1 }, { 45, 63, 0 } } },
  638. { { { 51, 0, 0 }, { 46, 62, 0 } } },
  639. { { { 51, 0, 1 }, { 48, 58, 0 } } },
  640. { { { 51, 0, 2 }, { 46, 63, 0 } } },
  641. { { { 52, 0, 1 }, { 47, 62, 0 } } },
  642. { { { 52, 0, 0 }, { 47, 63, 0 } } },
  643. { { { 52, 0, 1 }, { 48, 61, 0 } } },
  644. { { { 52, 0, 2 }, { 48, 62, 0 } } },
  645. { { { 53, 0, 1 }, { 56, 47, 0 } } },
  646. { { { 53, 0, 0 }, { 48, 63, 0 } } },
  647. { { { 53, 0, 1 }, { 49, 62, 0 } } },
  648. { { { 53, 0, 2 }, { 49, 63, 0 } } },
  649. { { { 54, 0, 1 }, { 58, 46, 0 } } },
  650. { { { 54, 0, 0 }, { 50, 62, 0 } } },
  651. { { { 54, 0, 1 }, { 50, 63, 0 } } },
  652. { { { 54, 0, 2 }, { 51, 62, 0 } } },
  653. { { { 55, 0, 1 }, { 59, 47, 0 } } },
  654. { { { 55, 0, 0 }, { 51, 63, 0 } } },
  655. { { { 55, 0, 1 }, { 52, 62, 0 } } },
  656. { { { 55, 0, 2 }, { 52, 63, 0 } } },
  657. { { { 56, 0, 1 }, { 61, 46, 0 } } },
  658. { { { 56, 0, 0 }, { 53, 62, 0 } } },
  659. { { { 56, 0, 1 }, { 53, 63, 0 } } },
  660. { { { 56, 0, 2 }, { 54, 62, 0 } } },
  661. { { { 57, 0, 1 }, { 62, 47, 0 } } },
  662. { { { 57, 0, 0 }, { 54, 63, 0 } } },
  663. { { { 57, 0, 1 }, { 55, 62, 0 } } },
  664. { { { 57, 0, 2 }, { 55, 63, 0 } } },
  665. { { { 58, 0, 1 }, { 56, 62, 1 } } },
  666. { { { 58, 0, 0 }, { 56, 62, 0 } } },
  667. { { { 58, 0, 1 }, { 56, 63, 0 } } },
  668. { { { 58, 0, 2 }, { 57, 62, 0 } } },
  669. { { { 59, 0, 1 }, { 57, 63, 1 } } },
  670. { { { 59, 0, 0 }, { 57, 63, 0 } } },
  671. { { { 59, 0, 1 }, { 58, 62, 0 } } },
  672. { { { 59, 0, 2 }, { 58, 63, 0 } } },
  673. { { { 60, 0, 1 }, { 59, 62, 1 } } },
  674. { { { 60, 0, 0 }, { 59, 62, 0 } } },
  675. { { { 60, 0, 1 }, { 59, 63, 0 } } },
  676. { { { 60, 0, 2 }, { 60, 62, 0 } } },
  677. { { { 61, 0, 1 }, { 60, 63, 1 } } },
  678. { { { 61, 0, 0 }, { 60, 63, 0 } } },
  679. { { { 61, 0, 1 }, { 61, 62, 0 } } },
  680. { { { 61, 0, 2 }, { 61, 63, 0 } } },
  681. { { { 62, 0, 1 }, { 62, 62, 1 } } },
  682. { { { 62, 0, 0 }, { 62, 62, 0 } } },
  683. { { { 62, 0, 1 }, { 62, 63, 0 } } },
  684. { { { 62, 0, 2 }, { 63, 62, 0 } } },
  685. { { { 63, 0, 1 }, { 63, 63, 1 } } },
  686. { { { 63, 0, 0 }, { 63, 63, 0 } } }
  687. };
  688. static const DDSSingleColourLookup*
  689. DDS_LOOKUP[] =
  690. {
  691. DDSLookup_5_4,
  692. DDSLookup_6_4,
  693. DDSLookup_5_4
  694. };
  695. /*
  696. Macros
  697. */
  698. #define C565_r(x) (((x) & 0xF800) >> 11)
  699. #define C565_g(x) (((x) & 0x07E0) >> 5)
  700. #define C565_b(x) ((x) & 0x001F)
  701. #define C565_red(x) ( (C565_r(x) << 3 | C565_r(x) >> 2))
  702. #define C565_green(x) ( (C565_g(x) << 2 | C565_g(x) >> 4))
  703. #define C565_blue(x) ( (C565_b(x) << 3 | C565_b(x) >> 2))
  704. #define DIV2(x) ((x) > 1 ? ((x) >> 1) : 1)
  705. #define FixRange(min, max, steps) \
  706. if (min > max) \
  707. min = max; \
  708. if ((ssize_t) max - min < steps) \
  709. max = MagickMin(min + steps, 255); \
  710. if ((ssize_t) max - min < steps) \
  711. min = MagickMax(0, (ssize_t) max - steps)
  712. #define Dot(left, right) (left.x*right.x) + (left.y*right.y) + (left.z*right.z)
  713. #define VectorInit(vector, value) vector.x = vector.y = vector.z = vector.w \
  714. = value
  715. #define VectorInit3(vector, value) vector.x = vector.y = vector.z = value
  716. #define IsBitMask(mask, r, g, b, a) (mask.r_bitmask == r && mask.g_bitmask == \
  717. g && mask.b_bitmask == b && mask.alpha_bitmask == a)
  718. /*
  719. Forward declarations
  720. */
  721. /*
  722. Forward declarations
  723. */
  724. static MagickBooleanType
  725. ConstructOrdering(const size_t,const DDSVector4 *,const DDSVector3,
  726. DDSVector4 *, DDSVector4 *, unsigned char *, size_t),
  727. ReadDDSInfo(Image *,DDSInfo *),
  728. ReadDXT1(const ImageInfo *,Image *,DDSInfo *,const MagickBooleanType,
  729. ExceptionInfo *),
  730. ReadDXT3(const ImageInfo *,Image *,DDSInfo *,const MagickBooleanType,
  731. ExceptionInfo *),
  732. ReadDXT5(const ImageInfo *,Image *,DDSInfo *,const MagickBooleanType,
  733. ExceptionInfo *),
  734. ReadUncompressedRGB(const ImageInfo *,Image *,DDSInfo *,
  735. const MagickBooleanType,ExceptionInfo *),
  736. ReadUncompressedRGBA(const ImageInfo *,Image *,DDSInfo *,
  737. const MagickBooleanType,ExceptionInfo *),
  738. SkipDXTMipmaps(Image *,DDSInfo *,int,ExceptionInfo *),
  739. SkipRGBMipmaps(Image *,DDSInfo *,int,ExceptionInfo *),
  740. WriteDDSImage(const ImageInfo *,Image *,ExceptionInfo *),
  741. WriteMipmaps(Image *,const ImageInfo*,const size_t,const size_t,const size_t,
  742. const MagickBooleanType,const MagickBooleanType,const MagickBooleanType,
  743. ExceptionInfo *);
  744. static void
  745. RemapIndices(const ssize_t *,const unsigned char *,unsigned char *),
  746. WriteDDSInfo(Image *,const size_t,const size_t,const size_t),
  747. WriteFourCC(Image *,const size_t,const MagickBooleanType,
  748. const MagickBooleanType,ExceptionInfo *),
  749. WriteImageData(Image *,const size_t,const size_t,const MagickBooleanType,
  750. const MagickBooleanType,ExceptionInfo *),
  751. WriteIndices(Image *,const DDSVector3,const DDSVector3,unsigned char *),
  752. WriteSingleColorFit(Image *,const DDSVector4 *,const ssize_t *),
  753. WriteUncompressed(Image *,ExceptionInfo *);
  754. static inline void VectorAdd(const DDSVector4 left, const DDSVector4 right,
  755. DDSVector4 *destination)
  756. {
  757. destination->x = left.x + right.x;
  758. destination->y = left.y + right.y;
  759. destination->z = left.z + right.z;
  760. destination->w = left.w + right.w;
  761. }
  762. static inline void VectorClamp(DDSVector4 *value)
  763. {
  764. value->x = MagickMin(1.0f,MagickMax(0.0f,value->x));
  765. value->y = MagickMin(1.0f,MagickMax(0.0f,value->y));
  766. value->z = MagickMin(1.0f,MagickMax(0.0f,value->z));
  767. value->w = MagickMin(1.0f,MagickMax(0.0f,value->w));
  768. }
  769. static inline void VectorClamp3(DDSVector3 *value)
  770. {
  771. value->x = MagickMin(1.0f,MagickMax(0.0f,value->x));
  772. value->y = MagickMin(1.0f,MagickMax(0.0f,value->y));
  773. value->z = MagickMin(1.0f,MagickMax(0.0f,value->z));
  774. }
  775. static inline void VectorCopy43(const DDSVector4 source,
  776. DDSVector3 *destination)
  777. {
  778. destination->x = source.x;
  779. destination->y = source.y;
  780. destination->z = source.z;
  781. }
  782. static inline void VectorCopy44(const DDSVector4 source,
  783. DDSVector4 *destination)
  784. {
  785. destination->x = source.x;
  786. destination->y = source.y;
  787. destination->z = source.z;
  788. destination->w = source.w;
  789. }
  790. static inline void VectorNegativeMultiplySubtract(const DDSVector4 a,
  791. const DDSVector4 b, const DDSVector4 c, DDSVector4 *destination)
  792. {
  793. destination->x = c.x - (a.x * b.x);
  794. destination->y = c.y - (a.y * b.y);
  795. destination->z = c.z - (a.z * b.z);
  796. destination->w = c.w - (a.w * b.w);
  797. }
  798. static inline void VectorMultiply(const DDSVector4 left,
  799. const DDSVector4 right, DDSVector4 *destination)
  800. {
  801. destination->x = left.x * right.x;
  802. destination->y = left.y * right.y;
  803. destination->z = left.z * right.z;
  804. destination->w = left.w * right.w;
  805. }
  806. static inline void VectorMultiply3(const DDSVector3 left,
  807. const DDSVector3 right, DDSVector3 *destination)
  808. {
  809. destination->x = left.x * right.x;
  810. destination->y = left.y * right.y;
  811. destination->z = left.z * right.z;
  812. }
  813. static inline void VectorMultiplyAdd(const DDSVector4 a, const DDSVector4 b,
  814. const DDSVector4 c, DDSVector4 *destination)
  815. {
  816. destination->x = (a.x * b.x) + c.x;
  817. destination->y = (a.y * b.y) + c.y;
  818. destination->z = (a.z * b.z) + c.z;
  819. destination->w = (a.w * b.w) + c.w;
  820. }
  821. static inline void VectorMultiplyAdd3(const DDSVector3 a, const DDSVector3 b,
  822. const DDSVector3 c, DDSVector3 *destination)
  823. {
  824. destination->x = (a.x * b.x) + c.x;
  825. destination->y = (a.y * b.y) + c.y;
  826. destination->z = (a.z * b.z) + c.z;
  827. }
  828. static inline void VectorReciprocal(const DDSVector4 value,
  829. DDSVector4 *destination)
  830. {
  831. destination->x = 1.0f / value.x;
  832. destination->y = 1.0f / value.y;
  833. destination->z = 1.0f / value.z;
  834. destination->w = 1.0f / value.w;
  835. }
  836. static inline void VectorSubtract(const DDSVector4 left,
  837. const DDSVector4 right, DDSVector4 *destination)
  838. {
  839. destination->x = left.x - right.x;
  840. destination->y = left.y - right.y;
  841. destination->z = left.z - right.z;
  842. destination->w = left.w - right.w;
  843. }
  844. static inline void VectorSubtract3(const DDSVector3 left,
  845. const DDSVector3 right, DDSVector3 *destination)
  846. {
  847. destination->x = left.x - right.x;
  848. destination->y = left.y - right.y;
  849. destination->z = left.z - right.z;
  850. }
  851. static inline void VectorTruncate(DDSVector4 *value)
  852. {
  853. value->x = value->x > 0.0f ? floor(value->x) : ceil(value->x);
  854. value->y = value->y > 0.0f ? floor(value->y) : ceil(value->y);
  855. value->z = value->z > 0.0f ? floor(value->z) : ceil(value->z);
  856. value->w = value->w > 0.0f ? floor(value->w) : ceil(value->w);
  857. }
  858. static inline void VectorTruncate3(DDSVector3 *value)
  859. {
  860. value->x = value->x > 0.0f ? floor(value->x) : ceil(value->x);
  861. value->y = value->y > 0.0f ? floor(value->y) : ceil(value->y);
  862. value->z = value->z > 0.0f ? floor(value->z) : ceil(value->z);
  863. }
  864. static void CalculateColors(unsigned short c0, unsigned short c1,
  865. DDSColors *c, MagickBooleanType ignoreAlpha)
  866. {
  867. c->a[0] = c->a[1] = c->a[2] = c->a[3] = 0;
  868. c->r[0] = (unsigned char) C565_red(c0);
  869. c->g[0] = (unsigned char) C565_green(c0);
  870. c->b[0] = (unsigned char) C565_blue(c0);
  871. c->r[1] = (unsigned char) C565_red(c1);
  872. c->g[1] = (unsigned char) C565_green(c1);
  873. c->b[1] = (unsigned char) C565_blue(c1);
  874. if (ignoreAlpha != MagickFalse || c0 > c1)
  875. {
  876. c->r[2] = (unsigned char) ((2 * c->r[0] + c->r[1]) / 3);
  877. c->g[2] = (unsigned char) ((2 * c->g[0] + c->g[1]) / 3);
  878. c->b[2] = (unsigned char) ((2 * c->b[0] + c->b[1]) / 3);
  879. c->r[3] = (unsigned char) ((c->r[0] + 2 * c->r[1]) / 3);
  880. c->g[3] = (unsigned char) ((c->g[0] + 2 * c->g[1]) / 3);
  881. c->b[3] = (unsigned char) ((c->b[0] + 2 * c->b[1]) / 3);
  882. }
  883. else
  884. {
  885. c->r[2] = (unsigned char) ((c->r[0] + c->r[1]) / 2);
  886. c->g[2] = (unsigned char) ((c->g[0] + c->g[1]) / 2);
  887. c->b[2] = (unsigned char) ((c->b[0] + c->b[1]) / 2);
  888. c->r[3] = c->g[3] = c->b[3] = 0;
  889. c->a[3] = 255;
  890. }
  891. }
  892. static size_t CompressAlpha(const size_t min, const size_t max,
  893. const size_t steps, const ssize_t *alphas, unsigned char* indices)
  894. {
  895. unsigned char
  896. codes[8];
  897. register ssize_t
  898. i;
  899. size_t
  900. error,
  901. index,
  902. j,
  903. least,
  904. value;
  905. codes[0] = (unsigned char) min;
  906. codes[1] = (unsigned char) max;
  907. codes[6] = 0;
  908. codes[7] = 255;
  909. for (i=1; i < (ssize_t) steps; i++)
  910. codes[i+1] = (unsigned char) (((steps-i)*min + i*max) / steps);
  911. error = 0;
  912. for (i=0; i<16; i++)
  913. {
  914. if (alphas[i] == -1)
  915. {
  916. indices[i] = 0;
  917. continue;
  918. }
  919. value = alphas[i];
  920. least = SIZE_MAX;
  921. index = 0;
  922. for (j=0; j<8; j++)
  923. {
  924. size_t
  925. dist;
  926. dist = value - (size_t)codes[j];
  927. dist *= dist;
  928. if (dist < least)
  929. {
  930. least = dist;
  931. index = j;
  932. }
  933. }
  934. indices[i] = (unsigned char)index;
  935. error += least;
  936. }
  937. return error;
  938. }
  939. static void CompressClusterFit(const size_t count,
  940. const DDSVector4 *points, const ssize_t *map, const DDSVector3 principle,
  941. const DDSVector4 metric, DDSVector3 *start, DDSVector3* end,
  942. unsigned char *indices)
  943. {
  944. DDSVector3
  945. axis;
  946. DDSVector4
  947. grid,
  948. gridrcp,
  949. half,
  950. onethird_onethird2,
  951. pointsWeights[16],
  952. two,
  953. twonineths,
  954. twothirds_twothirds2,
  955. xSumwSum;
  956. float
  957. bestError = 1e+37f;
  958. size_t
  959. bestIteration = 0,
  960. besti = 0,
  961. bestj = 0,
  962. bestk = 0,
  963. iterationIndex;
  964. ssize_t
  965. i;
  966. unsigned char
  967. *o,
  968. order[128],
  969. unordered[16];
  970. VectorInit(half,0.5f);
  971. VectorInit(two,2.0f);
  972. VectorInit(onethird_onethird2,1.0f/3.0f);
  973. onethird_onethird2.w = 1.0f/9.0f;
  974. VectorInit(twothirds_twothirds2,2.0f/3.0f);
  975. twothirds_twothirds2.w = 4.0f/9.0f;
  976. VectorInit(twonineths,2.0f/9.0f);
  977. grid.x = 31.0f;
  978. grid.y = 63.0f;
  979. grid.z = 31.0f;
  980. grid.w = 0.0f;
  981. gridrcp.x = 1.0f/31.0f;
  982. gridrcp.y = 1.0f/63.0f;
  983. gridrcp.z = 1.0f/31.0f;
  984. gridrcp.w = 0.0f;
  985. xSumwSum.x = 0.0f;
  986. xSumwSum.y = 0.0f;
  987. xSumwSum.z = 0.0f;
  988. xSumwSum.w = 0.0f;
  989. ConstructOrdering(count,points,principle,pointsWeights,&xSumwSum,order,0);
  990. for (iterationIndex = 0;;)
  991. {
  992. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  993. #pragma omp parallel for schedule(dynamic,1) \
  994. num_threads(GetMagickResourceLimit(ThreadResource))
  995. #endif
  996. for (i=0; i < (ssize_t) count; i++)
  997. {
  998. DDSVector4
  999. part0,
  1000. part1,
  1001. part2;
  1002. size_t
  1003. ii,
  1004. j,
  1005. k,
  1006. kmin;
  1007. VectorInit(part0,0.0f);
  1008. for(ii=0; ii < (size_t) i; ii++)
  1009. VectorAdd(pointsWeights[ii],part0,&part0);
  1010. VectorInit(part1,0.0f);
  1011. for (j=(size_t) i;;)
  1012. {
  1013. if (j == 0)
  1014. {
  1015. VectorCopy44(pointsWeights[0],&part2);
  1016. kmin = 1;
  1017. }
  1018. else
  1019. {
  1020. VectorInit(part2,0.0f);
  1021. kmin = j;
  1022. }
  1023. for (k=kmin;;)
  1024. {
  1025. DDSVector4
  1026. a,
  1027. alpha2_sum,
  1028. alphax_sum,
  1029. alphabeta_sum,
  1030. b,
  1031. beta2_sum,
  1032. betax_sum,
  1033. e1,
  1034. e2,
  1035. factor,
  1036. part3;
  1037. float
  1038. error;
  1039. VectorSubtract(xSumwSum,part2,&part3);
  1040. VectorSubtract(part3,part1,&part3);
  1041. VectorSubtract(part3,part0,&part3);
  1042. VectorMultiplyAdd(part1,twothirds_twothirds2,part0,&alphax_sum);
  1043. VectorMultiplyAdd(part2,onethird_onethird2,alphax_sum,&alphax_sum);
  1044. VectorInit(alpha2_sum,alphax_sum.w);
  1045. VectorMultiplyAdd(part2,twothirds_twothirds2,part3,&betax_sum);
  1046. VectorMultiplyAdd(part1,onethird_onethird2,betax_sum,&betax_sum);
  1047. VectorInit(beta2_sum,betax_sum.w);
  1048. VectorAdd(part1,part2,&alphabeta_sum);
  1049. VectorInit(alphabeta_sum,alphabeta_sum.w);
  1050. VectorMultiply(twonineths,alphabeta_sum,&alphabeta_sum);
  1051. VectorMultiply(alpha2_sum,beta2_sum,&factor);
  1052. VectorNegativeMultiplySubtract(alphabeta_sum,alphabeta_sum,factor,
  1053. &factor);
  1054. VectorReciprocal(factor,&factor);
  1055. VectorMultiply(alphax_sum,beta2_sum,&a);
  1056. VectorNegativeMultiplySubtract(betax_sum,alphabeta_sum,a,&a);
  1057. VectorMultiply(a,factor,&a);
  1058. VectorMultiply(betax_sum,alpha2_sum,&b);
  1059. VectorNegativeMultiplySubtract(alphax_sum,alphabeta_sum,b,&b);
  1060. VectorMultiply(b,factor,&b);
  1061. VectorClamp(&a);
  1062. VectorMultiplyAdd(grid,a,half,&a);
  1063. VectorTruncate(&a);
  1064. VectorMultiply(a,gridrcp,&a);
  1065. VectorClamp(&b);
  1066. VectorMultiplyAdd(grid,b,half,&b);
  1067. VectorTruncate(&b);
  1068. VectorMultiply(b,gridrcp,&b);
  1069. VectorMultiply(b,b,&e1);
  1070. VectorMultiply(e1,beta2_sum,&e1);
  1071. VectorMultiply(a,a,&e2);
  1072. VectorMultiplyAdd(e2,alpha2_sum,e1,&e1);
  1073. VectorMultiply(a,b,&e2);
  1074. VectorMultiply(e2,alphabeta_sum,&e2);
  1075. VectorNegativeMultiplySubtract(a,alphax_sum,e2,&e2);
  1076. VectorNegativeMultiplySubtract(b,betax_sum,e2,&e2);
  1077. VectorMultiplyAdd(two,e2,e1,&e2);
  1078. VectorMultiply(e2,metric,&e2);
  1079. error = e2.x + e2.y + e2.z;
  1080. if (error < bestError)
  1081. {
  1082. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  1083. #pragma omp critical (DDS_CompressClusterFit)
  1084. #endif
  1085. {
  1086. if (error < bestError)
  1087. {
  1088. VectorCopy43(a,start);
  1089. VectorCopy43(b,end);
  1090. bestError = error;
  1091. besti = i;
  1092. bestj = j;
  1093. bestk = k;
  1094. bestIteration = iterationIndex;
  1095. }
  1096. }
  1097. }
  1098. if (k == count)
  1099. break;
  1100. VectorAdd(pointsWeights[k],part2,&part2);
  1101. k++;
  1102. }
  1103. if (j == count)
  1104. break;
  1105. VectorAdd(pointsWeights[j],part1,&part1);
  1106. j++;
  1107. }
  1108. }
  1109. if (bestIteration != iterationIndex)
  1110. break;
  1111. iterationIndex++;
  1112. if (iterationIndex == 8)
  1113. break;
  1114. VectorSubtract3(*end,*start,&axis);
  1115. if (ConstructOrdering(count,points,axis,pointsWeights,&xSumwSum,order,
  1116. iterationIndex) == MagickFalse)
  1117. break;
  1118. }
  1119. o = order + (16*bestIteration);
  1120. for (i=0; i < (ssize_t) besti; i++)
  1121. unordered[o[i]] = 0;
  1122. for (i=besti; i < (ssize_t) bestj; i++)
  1123. unordered[o[i]] = 2;
  1124. for (i=bestj; i < (ssize_t) bestk; i++)
  1125. unordered[o[i]] = 3;
  1126. for (i=bestk; i < (ssize_t) count; i++)
  1127. unordered[o[i]] = 1;
  1128. RemapIndices(map,unordered,indices);
  1129. }
  1130. static void CompressRangeFit(const size_t count,
  1131. const DDSVector4* points, const ssize_t *map, const DDSVector3 principle,
  1132. const DDSVector4 metric, DDSVector3 *start, DDSVector3 *end,
  1133. unsigned char *indices)
  1134. {
  1135. float
  1136. d,
  1137. bestDist,
  1138. max,
  1139. min,
  1140. val;
  1141. DDSVector3
  1142. codes[4],
  1143. grid,
  1144. gridrcp,
  1145. half,
  1146. dist;
  1147. register ssize_t
  1148. i;
  1149. size_t
  1150. bestj,
  1151. j;
  1152. unsigned char
  1153. closest[16];
  1154. VectorInit3(half,0.5f);
  1155. grid.x = 31.0f;
  1156. grid.y = 63.0f;
  1157. grid.z = 31.0f;
  1158. gridrcp.x = 1.0f/31.0f;
  1159. gridrcp.y = 1.0f/63.0f;
  1160. gridrcp.z = 1.0f/31.0f;
  1161. if (count > 0)
  1162. {
  1163. VectorCopy43(points[0],start);
  1164. VectorCopy43(points[0],end);
  1165. min = max = Dot(points[0],principle);
  1166. for (i=1; i < (ssize_t) count; i++)
  1167. {
  1168. val = Dot(points[i],principle);
  1169. if (val < min)
  1170. {
  1171. VectorCopy43(points[i],start);
  1172. min = val;
  1173. }
  1174. else if (val > max)
  1175. {
  1176. VectorCopy43(points[i],end);
  1177. max = val;
  1178. }
  1179. }
  1180. }
  1181. VectorClamp3(start);
  1182. VectorMultiplyAdd3(grid,*start,half,start);
  1183. VectorTruncate3(start);
  1184. VectorMultiply3(*start,gridrcp,start);
  1185. VectorClamp3(end);
  1186. VectorMultiplyAdd3(grid,*end,half,end);
  1187. VectorTruncate3(end);
  1188. VectorMultiply3(*end,gridrcp,end);
  1189. codes[0] = *start;
  1190. codes[1] = *end;
  1191. codes[2].x = (start->x * (2.0f/3.0f)) + (end->x * (1.0f/3.0f));
  1192. codes[2].y = (start->y * (2.0f/3.0f)) + (end->y * (1.0f/3.0f));
  1193. codes[2].z = (start->z * (2.0f/3.0f)) + (end->z * (1.0f/3.0f));
  1194. codes[3].x = (start->x * (1.0f/3.0f)) + (end->x * (2.0f/3.0f));
  1195. codes[3].y = (start->y * (1.0f/3.0f)) + (end->y * (2.0f/3.0f));
  1196. codes[3].z = (start->z * (1.0f/3.0f)) + (end->z * (2.0f/3.0f));
  1197. for (i=0; i < (ssize_t) count; i++)
  1198. {
  1199. bestDist = 1e+37f;
  1200. bestj = 0;
  1201. for (j=0; j < 4; j++)
  1202. {
  1203. dist.x = (points[i].x - codes[j].x) * metric.x;
  1204. dist.y = (points[i].y - codes[j].y) * metric.y;
  1205. dist.z = (points[i].z - codes[j].z) * metric.z;
  1206. d = Dot(dist,dist);
  1207. if (d < bestDist)
  1208. {
  1209. bestDist = d;
  1210. bestj = j;
  1211. }
  1212. }
  1213. closest[i] = (unsigned char) bestj;
  1214. }
  1215. RemapIndices(map, closest, indices);
  1216. }
  1217. static void ComputeEndPoints(const DDSSingleColourLookup *lookup[],
  1218. const unsigned char *color, DDSVector3 *start, DDSVector3 *end,
  1219. unsigned char *index)
  1220. {
  1221. register ssize_t
  1222. i;
  1223. size_t
  1224. c,
  1225. maxError = SIZE_MAX;
  1226. for (i=0; i < 2; i++)
  1227. {
  1228. const DDSSourceBlock*
  1229. sources[3];
  1230. size_t
  1231. error = 0;
  1232. for (c=0; c < 3; c++)
  1233. {
  1234. sources[c] = &lookup[c][color[c]].sources[i];
  1235. error += ((size_t) sources[c]->error) * ((size_t) sources[c]->error);
  1236. }
  1237. if (error > maxError)
  1238. continue;
  1239. start->x = (float) sources[0]->start / 31.0f;
  1240. start->y = (float) sources[1]->start / 63.0f;
  1241. start->z = (float) sources[2]->start / 31.0f;
  1242. end->x = (float) sources[0]->end / 31.0f;
  1243. end->y = (float) sources[1]->end / 63.0f;
  1244. end->z = (float) sources[2]->end / 31.0f;
  1245. *index = (unsigned char) (2*i);
  1246. maxError = error;
  1247. }
  1248. }
  1249. static void ComputePrincipleComponent(const float *covariance,
  1250. DDSVector3 *principle)
  1251. {
  1252. DDSVector4
  1253. row0,
  1254. row1,
  1255. row2,
  1256. v;
  1257. register ssize_t
  1258. i;
  1259. row0.x = covariance[0];
  1260. row0.y = covariance[1];
  1261. row0.z = covariance[2];
  1262. row0.w = 0.0f;
  1263. row1.x = covariance[1];
  1264. row1.y = covariance[3];
  1265. row1.z = covariance[4];
  1266. row1.w = 0.0f;
  1267. row2.x = covariance[2];
  1268. row2.y = covariance[4];
  1269. row2.z = covariance[5];
  1270. row2.w = 0.0f;
  1271. VectorInit(v,1.0f);
  1272. for (i=0; i < 8; i++)
  1273. {
  1274. DDSVector4
  1275. w;
  1276. float
  1277. a;
  1278. w.x = row0.x * v.x;
  1279. w.y = row0.y * v.x;
  1280. w.z = row0.z * v.x;
  1281. w.w = row0.w * v.x;
  1282. w.x = (row1.x * v.y) + w.x;
  1283. w.y = (row1.y * v.y) + w.y;
  1284. w.z = (row1.z * v.y) + w.z;
  1285. w.w = (row1.w * v.y) + w.w;
  1286. w.x = (row2.x * v.z) + w.x;
  1287. w.y = (row2.y * v.z) + w.y;
  1288. w.z = (row2.z * v.z) + w.z;
  1289. w.w = (row2.w * v.z) + w.w;
  1290. a = (float) PerceptibleReciprocal(MagickMax(w.x,MagickMax(w.y,w.z)));
  1291. v.x = w.x * a;
  1292. v.y = w.y * a;
  1293. v.z = w.z * a;
  1294. v.w = w.w * a;
  1295. }
  1296. VectorCopy43(v,principle);
  1297. }
  1298. static void ComputeWeightedCovariance(const size_t count,
  1299. const DDSVector4 *points, float *covariance)
  1300. {
  1301. DDSVector3
  1302. centroid;
  1303. float
  1304. total;
  1305. size_t
  1306. i;
  1307. total = 0.0f;
  1308. VectorInit3(centroid,0.0f);
  1309. for (i=0; i < count; i++)
  1310. {
  1311. total += points[i].w;
  1312. centroid.x += (points[i].x * points[i].w);
  1313. centroid.y += (points[i].y * points[i].w);
  1314. centroid.z += (points[i].z * points[i].w);
  1315. }
  1316. if( total > 1.192092896e-07F)
  1317. {
  1318. centroid.x /= total;
  1319. centroid.y /= total;
  1320. centroid.z /= total;
  1321. }
  1322. for (i=0; i < 6; i++)
  1323. covariance[i] = 0.0f;
  1324. for (i = 0; i < count; i++)
  1325. {
  1326. DDSVector3
  1327. a,
  1328. b;
  1329. a.x = points[i].x - centroid.x;
  1330. a.y = points[i].y - centroid.y;
  1331. a.z = points[i].z - centroid.z;
  1332. b.x = points[i].w * a.x;
  1333. b.y = points[i].w * a.y;
  1334. b.z = points[i].w * a.z;
  1335. covariance[0] += a.x*b.x;
  1336. covariance[1] += a.x*b.y;
  1337. covariance[2] += a.x*b.z;
  1338. covariance[3] += a.y*b.y;
  1339. covariance[4] += a.y*b.z;
  1340. covariance[5] += a.z*b.z;
  1341. }
  1342. }
  1343. static MagickBooleanType ConstructOrdering(const size_t count,
  1344. const DDSVector4 *points, const DDSVector3 axis, DDSVector4 *pointsWeights,
  1345. DDSVector4 *xSumwSum, unsigned char *order, size_t iteration)
  1346. {
  1347. float
  1348. dps[16],
  1349. f;
  1350. register ssize_t
  1351. i;
  1352. size_t
  1353. j;
  1354. unsigned char
  1355. c,
  1356. *o,
  1357. *p;
  1358. o = order + (16*iteration);
  1359. for (i=0; i < (ssize_t) count; i++)
  1360. {
  1361. dps[i] = Dot(points[i],axis);
  1362. o[i] = (unsigned char)i;
  1363. }
  1364. for (i=0; i < (ssize_t) count; i++)
  1365. {
  1366. for (j=i; j > 0 && dps[j] < dps[j - 1]; j--)
  1367. {
  1368. f = dps[j];
  1369. dps[j] = dps[j - 1];
  1370. dps[j - 1] = f;
  1371. c = o[j];
  1372. o[j] = o[j - 1];
  1373. o[j - 1] = c;
  1374. }
  1375. }
  1376. for (i=0; i < (ssize_t) iteration; i++)
  1377. {
  1378. MagickBooleanType
  1379. same;
  1380. p = order + (16*i);
  1381. same = MagickTrue;
  1382. for (j=0; j < count; j++)
  1383. {
  1384. if (o[j] != p[j])
  1385. {
  1386. same = MagickFalse;
  1387. break;
  1388. }
  1389. }
  1390. if (same != MagickFalse)
  1391. return MagickFalse;
  1392. }
  1393. xSumwSum->x = 0;
  1394. xSumwSum->y = 0;
  1395. xSumwSum->z = 0;
  1396. xSumwSum->w = 0;
  1397. for (i=0; i < (ssize_t) count; i++)
  1398. {
  1399. DDSVector4
  1400. v;
  1401. j = (size_t) o[i];
  1402. v.x = points[j].w * points[j].x;
  1403. v.y = points[j].w * points[j].y;
  1404. v.z = points[j].w * points[j].z;
  1405. v.w = points[j].w * 1.0f;
  1406. VectorCopy44(v,&pointsWeights[i]);
  1407. VectorAdd(*xSumwSum,v,xSumwSum);
  1408. }
  1409. return MagickTrue;
  1410. }
  1411. /*
  1412. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1413. % %
  1414. % %
  1415. % %
  1416. % I s D D S %
  1417. % %
  1418. % %
  1419. % %
  1420. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1421. %
  1422. % IsDDS() returns MagickTrue if the image format type, identified by the
  1423. % magick string, is DDS.
  1424. %
  1425. % The format of the IsDDS method is:
  1426. %
  1427. % MagickBooleanType IsDDS(const unsigned char *magick,const size_t length)
  1428. %
  1429. % A description of each parameter follows:
  1430. %
  1431. % o magick: compare image format pattern against these bytes.
  1432. %
  1433. % o length: Specifies the length of the magick string.
  1434. %
  1435. */
  1436. static MagickBooleanType IsDDS(const unsigned char *magick, const size_t length)
  1437. {
  1438. if (length < 4)
  1439. return(MagickFalse);
  1440. if (LocaleNCompare((char *) magick,"DDS ", 4) == 0)
  1441. return(MagickTrue);
  1442. return(MagickFalse);
  1443. }
  1444. /*
  1445. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1446. % %
  1447. % %
  1448. % %
  1449. % R e a d D D S I m a g e %
  1450. % %
  1451. % %
  1452. % %
  1453. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1454. %
  1455. % ReadDDSImage() reads a DirectDraw Surface image file and returns it. It
  1456. % allocates the memory necessary for the new Image structure and returns a
  1457. % pointer to the new image.
  1458. %
  1459. % The format of the ReadDDSImage method is:
  1460. %
  1461. % Image *ReadDDSImage(const ImageInfo *image_info,ExceptionInfo *exception)
  1462. %
  1463. % A description of each parameter follows:
  1464. %
  1465. % o image_info: The image info.
  1466. %
  1467. % o exception: return any errors or warnings in this structure.
  1468. %
  1469. */
  1470. static Image *ReadDDSImage(const ImageInfo *image_info,ExceptionInfo *exception)
  1471. {
  1472. const char
  1473. *option;
  1474. CompressionType
  1475. compression;
  1476. DDSInfo
  1477. dds_info;
  1478. DDSDecoder
  1479. *decoder;
  1480. Image
  1481. *image;
  1482. MagickBooleanType
  1483. status,
  1484. cubemap,
  1485. volume,
  1486. read_mipmaps;
  1487. PixelTrait
  1488. alpha_trait;
  1489. size_t
  1490. n,
  1491. num_images;
  1492. /*
  1493. Open image file.
  1494. */
  1495. assert(image_info != (const ImageInfo *) NULL);
  1496. assert(image_info->signature == MagickCoreSignature);
  1497. if (image_info->debug != MagickFalse)
  1498. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
  1499. image_info->filename);
  1500. assert(exception != (ExceptionInfo *) NULL);
  1501. assert(exception->signature == MagickCoreSignature);
  1502. cubemap=MagickFalse,
  1503. volume=MagickFalse,
  1504. read_mipmaps=MagickFalse;
  1505. image=AcquireImage(image_info,exception);
  1506. status=OpenBlob(image_info,image,Rea

Large files files are truncated, but you can click here to view the full file