PageRenderTime 38ms CodeModel.GetById 21ms 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
  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,ReadBinaryBlobMode,exception);
  1507. if (status == MagickFalse)
  1508. {
  1509. image=DestroyImageList(image);
  1510. return((Image *) NULL);
  1511. }
  1512. /*
  1513. Initialize image structure.
  1514. */
  1515. if (ReadDDSInfo(image, &dds_info) != MagickTrue)
  1516. ThrowReaderException(CorruptImageError,"ImproperImageHeader");
  1517. if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP)
  1518. cubemap = MagickTrue;
  1519. if (dds_info.ddscaps2 & DDSCAPS2_VOLUME && dds_info.depth > 0)
  1520. volume = MagickTrue;
  1521. (void) SeekBlob(image, 128, SEEK_SET);
  1522. /*
  1523. Determine pixel format
  1524. */
  1525. if (dds_info.pixelformat.flags & DDPF_RGB)
  1526. {
  1527. compression = NoCompression;
  1528. if (dds_info.pixelformat.flags & DDPF_ALPHAPIXELS)
  1529. {
  1530. alpha_trait = BlendPixelTrait;
  1531. decoder = ReadUncompressedRGBA;
  1532. }
  1533. else
  1534. {
  1535. alpha_trait = UndefinedPixelTrait;
  1536. decoder = ReadUncompressedRGB;
  1537. }
  1538. }
  1539. else if (dds_info.pixelformat.flags & DDPF_LUMINANCE)
  1540. {
  1541. compression = NoCompression;
  1542. if (dds_info.pixelformat.flags & DDPF_ALPHAPIXELS)
  1543. {
  1544. /* Not sure how to handle this */
  1545. ThrowReaderException(CorruptImageError, "ImageTypeNotSupported");
  1546. }
  1547. else
  1548. {
  1549. alpha_trait = UndefinedPixelTrait;
  1550. decoder = ReadUncompressedRGB;
  1551. }
  1552. }
  1553. else if (dds_info.pixelformat.flags & DDPF_FOURCC)
  1554. {
  1555. switch (dds_info.pixelformat.fourcc)
  1556. {
  1557. case FOURCC_DXT1:
  1558. {
  1559. alpha_trait = UndefinedPixelTrait;
  1560. compression = DXT1Compression;
  1561. decoder = ReadDXT1;
  1562. break;
  1563. }
  1564. case FOURCC_DXT3:
  1565. {
  1566. alpha_trait = BlendPixelTrait;
  1567. compression = DXT3Compression;
  1568. decoder = ReadDXT3;
  1569. break;
  1570. }
  1571. case FOURCC_DXT5:
  1572. {
  1573. alpha_trait = BlendPixelTrait;
  1574. compression = DXT5Compression;
  1575. decoder = ReadDXT5;
  1576. break;
  1577. }
  1578. default:
  1579. {
  1580. /* Unknown FOURCC */
  1581. ThrowReaderException(CorruptImageError, "ImageTypeNotSupported");
  1582. }
  1583. }
  1584. }
  1585. else
  1586. {
  1587. /* Neither compressed nor uncompressed... thus unsupported */
  1588. ThrowReaderException(CorruptImageError, "ImageTypeNotSupported");
  1589. }
  1590. num_images = 1;
  1591. if (cubemap)
  1592. {
  1593. /*
  1594. Determine number of faces defined in the cubemap
  1595. */
  1596. num_images = 0;
  1597. if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_POSITIVEX) num_images++;
  1598. if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_NEGATIVEX) num_images++;
  1599. if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_POSITIVEY) num_images++;
  1600. if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_NEGATIVEY) num_images++;
  1601. if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_POSITIVEZ) num_images++;
  1602. if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_NEGATIVEZ) num_images++;
  1603. }
  1604. if (volume)
  1605. num_images = dds_info.depth;
  1606. if ((num_images == 0) || (num_images > GetBlobSize(image)))
  1607. ThrowReaderException(CorruptImageError,"ImproperImageHeader");
  1608. if (AcquireMagickResource(ListLengthResource,num_images) == MagickFalse)
  1609. ThrowReaderException(ResourceLimitError,"ListLengthExceedsLimit");
  1610. option=GetImageOption(image_info,"dds:skip-mipmaps");
  1611. if (IsStringFalse(option) != MagickFalse)
  1612. read_mipmaps=MagickTrue;
  1613. for (n = 0; n < num_images; n++)
  1614. {
  1615. if (n != 0)
  1616. {
  1617. /* Start a new image */
  1618. if (EOFBlob(image) != MagickFalse)
  1619. ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
  1620. AcquireNextImage(image_info,image,exception);
  1621. if (GetNextImageInList(image) == (Image *) NULL)
  1622. return(DestroyImageList(image));
  1623. image=SyncNextImageInList(image);
  1624. }
  1625. image->alpha_trait=alpha_trait;
  1626. image->compression=compression;
  1627. image->columns=dds_info.width;
  1628. image->rows=dds_info.height;
  1629. image->storage_class=DirectClass;
  1630. image->endian=LSBEndian;
  1631. image->depth=8;
  1632. if (image_info->ping != MagickFalse)
  1633. {
  1634. (void) CloseBlob(image);
  1635. return(GetFirstImageInList(image));
  1636. }
  1637. status=SetImageExtent(image,image->columns,image->rows,exception);
  1638. if (status == MagickFalse)
  1639. return(DestroyImageList(image));
  1640. (void) SetImageBackgroundColor(image,exception);
  1641. status=(decoder)(image_info,image,&dds_info,read_mipmaps,exception);
  1642. if (status == MagickFalse)
  1643. {
  1644. (void) CloseBlob(image);
  1645. return(GetFirstImageInList(image));
  1646. }
  1647. }
  1648. (void) CloseBlob(image);
  1649. return(GetFirstImageInList(image));
  1650. }
  1651. static MagickBooleanType ReadDDSInfo(Image *image, DDSInfo *dds_info)
  1652. {
  1653. size_t
  1654. hdr_size,
  1655. required;
  1656. /* Seek to start of header */
  1657. (void) SeekBlob(image, 4, SEEK_SET);
  1658. /* Check header field */
  1659. hdr_size = ReadBlobLSBLong(image);
  1660. if (hdr_size != 124)
  1661. return MagickFalse;
  1662. /* Fill in DDS info struct */
  1663. dds_info->flags = ReadBlobLSBLong(image);
  1664. /* Check required flags */
  1665. required=(size_t) (DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT);
  1666. if ((dds_info->flags & required) != required)
  1667. return MagickFalse;
  1668. dds_info->height = ReadBlobLSBLong(image);
  1669. dds_info->width = ReadBlobLSBLong(image);
  1670. dds_info->pitchOrLinearSize = ReadBlobLSBLong(image);
  1671. dds_info->depth = ReadBlobLSBLong(image);
  1672. dds_info->mipmapcount = ReadBlobLSBLong(image);
  1673. (void) SeekBlob(image, 44, SEEK_CUR); /* reserved region of 11 DWORDs */
  1674. /* Read pixel format structure */
  1675. hdr_size = ReadBlobLSBLong(image);
  1676. if (hdr_size != 32)
  1677. return MagickFalse;
  1678. dds_info->pixelformat.flags = ReadBlobLSBLong(image);
  1679. dds_info->pixelformat.fourcc = ReadBlobLSBLong(image);
  1680. dds_info->pixelformat.rgb_bitcount = ReadBlobLSBLong(image);
  1681. dds_info->pixelformat.r_bitmask = ReadBlobLSBLong(image);
  1682. dds_info->pixelformat.g_bitmask = ReadBlobLSBLong(image);
  1683. dds_info->pixelformat.b_bitmask = ReadBlobLSBLong(image);
  1684. dds_info->pixelformat.alpha_bitmask = ReadBlobLSBLong(image);
  1685. dds_info->ddscaps1 = ReadBlobLSBLong(image);
  1686. dds_info->ddscaps2 = ReadBlobLSBLong(image);
  1687. (void) SeekBlob(image, 12, SEEK_CUR); /* 3 reserved DWORDs */
  1688. return MagickTrue;
  1689. }
  1690. static MagickBooleanType SetDXT1Pixels(Image *image,ssize_t x,ssize_t y,
  1691. DDSColors colors,size_t bits,Quantum *q)
  1692. {
  1693. register ssize_t
  1694. i;
  1695. ssize_t
  1696. j;
  1697. unsigned char
  1698. code;
  1699. for (j = 0; j < 4; j++)
  1700. {
  1701. for (i = 0; i < 4; i++)
  1702. {
  1703. if ((x + i) < (ssize_t) image->columns &&
  1704. (y + j) < (ssize_t) image->rows)
  1705. {
  1706. code=(unsigned char) ((bits >> ((j*4+i)*2)) & 0x3);
  1707. SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q);
  1708. SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q);
  1709. SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q);
  1710. SetPixelOpacity(image,ScaleCharToQuantum(colors.a[code]),q);
  1711. if ((colors.a[code] != 0) &&
  1712. (image->alpha_trait == UndefinedPixelTrait))
  1713. return(MagickFalse);
  1714. q+=GetPixelChannels(image);
  1715. }
  1716. }
  1717. }
  1718. return(MagickTrue);
  1719. }
  1720. static MagickBooleanType ReadMipmaps(const ImageInfo *image_info,Image *image,
  1721. DDSInfo *dds_info,DDSPixelDecoder decoder,ExceptionInfo *exception)
  1722. {
  1723. MagickBooleanType
  1724. status;
  1725. /*
  1726. Only skip mipmaps for textures and cube maps
  1727. */
  1728. if (EOFBlob(image) != MagickFalse)
  1729. {
  1730. ThrowFileException(exception,CorruptImageWarning,"UnexpectedEndOfFile",
  1731. image->filename);
  1732. return(MagickFalse);
  1733. }
  1734. status=MagickTrue;
  1735. if (dds_info->ddscaps1 & DDSCAPS_MIPMAP
  1736. && (dds_info->ddscaps1 & DDSCAPS_TEXTURE
  1737. || dds_info->ddscaps2 & DDSCAPS2_CUBEMAP))
  1738. {
  1739. register ssize_t
  1740. i;
  1741. size_t
  1742. h,
  1743. w;
  1744. w=DIV2(dds_info->width);
  1745. h=DIV2(dds_info->height);
  1746. /*
  1747. Mipmapcount includes the main image, so start from one
  1748. */
  1749. for (i = 1; (i < (ssize_t) dds_info->mipmapcount) && w && h; i++)
  1750. {
  1751. AcquireNextImage(image_info,image,exception);
  1752. if (image->next == (Image *) NULL)
  1753. return(MagickFalse);
  1754. image->next->alpha_trait=image->alpha_trait;
  1755. image=SyncNextImageInList(image);
  1756. status=SetImageExtent(image,w,h,exception);
  1757. if (status == MagickFalse)
  1758. break;
  1759. status=decoder(image,dds_info,exception);
  1760. if (status == MagickFalse)
  1761. break;
  1762. if ((w == 1) && (h == 1))
  1763. break;
  1764. w=DIV2(w);
  1765. h=DIV2(h);
  1766. }
  1767. }
  1768. return(status);
  1769. }
  1770. static MagickBooleanType ReadDXT1Pixels(Image *image,
  1771. DDSInfo *magick_unused(dds_info),ExceptionInfo *exception)
  1772. {
  1773. DDSColors
  1774. colors;
  1775. register Quantum
  1776. *q;
  1777. register ssize_t
  1778. x;
  1779. size_t
  1780. bits;
  1781. ssize_t
  1782. y;
  1783. unsigned short
  1784. c0,
  1785. c1;
  1786. magick_unreferenced(dds_info);
  1787. for (y = 0; y < (ssize_t) image->rows; y += 4)
  1788. {
  1789. for (x = 0; x < (ssize_t) image->columns; x += 4)
  1790. {
  1791. /* Get 4x4 patch of pixels to write on */
  1792. q=QueueAuthenticPixels(image,x,y,MagickMin(4,image->columns-x),
  1793. MagickMin(4,image->rows-y),exception);
  1794. if (q == (Quantum *) NULL)
  1795. return(MagickFalse);
  1796. /* Read 8 bytes of data from the image */
  1797. c0=ReadBlobLSBShort(image);
  1798. c1=ReadBlobLSBShort(image);
  1799. bits=ReadBlobLSBLong(image);
  1800. CalculateColors(c0,c1,&colors,MagickFalse);
  1801. if (EOFBlob(image) != MagickFalse)
  1802. return(MagickFalse);
  1803. /* Write the pixels */
  1804. if (SetDXT1Pixels(image,x,y,colors,bits,q) == MagickFalse)
  1805. {
  1806. /* Correct alpha */
  1807. SetImageAlpha(image,QuantumRange,exception);
  1808. q=QueueAuthenticPixels(image,x,y,MagickMin(4,image->columns-x),
  1809. MagickMin(4,image->rows-y),exception);
  1810. if (q != (Quantum *) NULL)
  1811. SetDXT1Pixels(image,x,y,colors,bits,q);
  1812. }
  1813. if (SyncAuthenticPixels(image,exception) == MagickFalse)
  1814. return(MagickFalse);
  1815. }
  1816. if (EOFBlob(image) != MagickFalse)
  1817. return(MagickFalse);
  1818. }
  1819. return(MagickTrue);
  1820. }
  1821. static MagickBooleanType ReadDXT1(const ImageInfo *image_info,Image *image,
  1822. DDSInfo *dds_info,const MagickBooleanType read_mipmaps,
  1823. ExceptionInfo *exception)
  1824. {
  1825. if (ReadDXT1Pixels(image,dds_info,exception) == MagickFalse)
  1826. return(MagickFalse);
  1827. if (read_mipmaps != MagickFalse)
  1828. return(ReadMipmaps(image_info,image,dds_info,ReadDXT1Pixels,exception));
  1829. else
  1830. return(SkipDXTMipmaps(image,dds_info,8,exception));
  1831. }
  1832. static MagickBooleanType ReadDXT3Pixels(Image *image,
  1833. DDSInfo *magick_unused(dds_info),ExceptionInfo *exception)
  1834. {
  1835. DDSColors
  1836. colors;
  1837. register Quantum
  1838. *q;
  1839. register ssize_t
  1840. i,
  1841. x;
  1842. unsigned char
  1843. alpha;
  1844. size_t
  1845. a0,
  1846. a1,
  1847. bits,
  1848. code;
  1849. ssize_t
  1850. j,
  1851. y;
  1852. unsigned short
  1853. c0,
  1854. c1;
  1855. magick_unreferenced(dds_info);
  1856. for (y = 0; y < (ssize_t) image->rows; y += 4)
  1857. {
  1858. for (x = 0; x < (ssize_t) image->columns; x += 4)
  1859. {
  1860. /* Get 4x4 patch of pixels to write on */
  1861. q = QueueAuthenticPixels(image, x, y, MagickMin(4, image->columns - x),
  1862. MagickMin(4, image->rows - y),exception);
  1863. if (q == (Quantum *) NULL)
  1864. return(MagickFalse);
  1865. /* Read alpha values (8 bytes) */
  1866. a0 = ReadBlobLSBLong(image);
  1867. a1 = ReadBlobLSBLong(image);
  1868. /* Read 8 bytes of data from the image */
  1869. c0 = ReadBlobLSBShort(image);
  1870. c1 = ReadBlobLSBShort(image);
  1871. bits = ReadBlobLSBLong(image);
  1872. CalculateColors(c0, c1, &colors, MagickTrue);
  1873. if (EOFBlob(image) != MagickFalse)
  1874. return(MagickFalse);
  1875. /* Write the pixels */
  1876. for (j = 0; j < 4; j++)
  1877. {
  1878. for (i = 0; i < 4; i++)
  1879. {
  1880. if ((x + i) < (ssize_t) image->columns && (y + j) < (ssize_t) image->rows)
  1881. {
  1882. code = (bits >> ((4*j+i)*2)) & 0x3;
  1883. SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q);
  1884. SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q);
  1885. SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q);
  1886. /*
  1887. Extract alpha value: multiply 0..15 by 17 to get range 0..255
  1888. */
  1889. if (j < 2)
  1890. alpha = 17U * (unsigned char) ((a0 >> (4*(4*j+i))) & 0xf);
  1891. else
  1892. alpha = 17U * (unsigned char) ((a1 >> (4*(4*(j-2)+i))) & 0xf);
  1893. SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) alpha),q);
  1894. q+=GetPixelChannels(image);
  1895. }
  1896. }
  1897. }
  1898. if (SyncAuthenticPixels(image,exception) == MagickFalse)
  1899. return(MagickFalse);
  1900. }
  1901. if (EOFBlob(image) != MagickFalse)
  1902. return(MagickFalse);
  1903. }
  1904. return(MagickTrue);
  1905. }
  1906. static MagickBooleanType ReadDXT3(const ImageInfo *image_info,Image *image,
  1907. DDSInfo *dds_info,const MagickBooleanType read_mipmaps,
  1908. ExceptionInfo *exception)
  1909. {
  1910. if (ReadDXT3Pixels(image,dds_info,exception) == MagickFalse)
  1911. return(MagickFalse);
  1912. if (read_mipmaps != MagickFalse)
  1913. return(ReadMipmaps(image_info,image,dds_info,ReadDXT3Pixels,exception));
  1914. else
  1915. return(SkipDXTMipmaps(image,dds_info,16,exception));
  1916. }
  1917. static MagickBooleanType ReadDXT5Pixels(Image *image,
  1918. DDSInfo *magick_unused(dds_info),ExceptionInfo *exception)
  1919. {
  1920. DDSColors
  1921. colors;
  1922. MagickSizeType
  1923. alpha_bits;
  1924. register Quantum
  1925. *q;
  1926. register ssize_t
  1927. i,
  1928. x;
  1929. unsigned char
  1930. a0,
  1931. a1;
  1932. size_t
  1933. alpha,
  1934. bits,
  1935. code,
  1936. alpha_code;
  1937. ssize_t
  1938. j,
  1939. y;
  1940. unsigned short
  1941. c0,
  1942. c1;
  1943. magick_unreferenced(dds_info);
  1944. for (y = 0; y < (ssize_t) image->rows; y += 4)
  1945. {
  1946. for (x = 0; x < (ssize_t) image->columns; x += 4)
  1947. {
  1948. /* Get 4x4 patch of pixels to write on */
  1949. q = QueueAuthenticPixels(image, x, y, MagickMin(4, image->columns - x),
  1950. MagickMin(4, image->rows - y),exception);
  1951. if (q == (Quantum *) NULL)
  1952. return(MagickFalse);
  1953. /* Read alpha values (8 bytes) */
  1954. a0 = (unsigned char) ReadBlobByte(image);
  1955. a1 = (unsigned char) ReadBlobByte(image);
  1956. alpha_bits = (MagickSizeType)ReadBlobLSBLong(image);
  1957. alpha_bits = alpha_bits | ((MagickSizeType)ReadBlobLSBShort(image) << 32);
  1958. /* Read 8 bytes of data from the image */
  1959. c0 = ReadBlobLSBShort(image);
  1960. c1 = ReadBlobLSBShort(image);
  1961. bits = ReadBlobLSBLong(image);
  1962. CalculateColors(c0, c1, &colors, MagickTrue);
  1963. if (EOFBlob(image) != MagickFalse)
  1964. return(MagickFalse);
  1965. /* Write the pixels */
  1966. for (j = 0; j < 4; j++)
  1967. {
  1968. for (i = 0; i < 4; i++)
  1969. {
  1970. if ((x + i) < (ssize_t) image->columns &&
  1971. (y + j) < (ssize_t) image->rows)
  1972. {
  1973. code = (bits >> ((4*j+i)*2)) & 0x3;
  1974. SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q);
  1975. SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q);
  1976. SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q);
  1977. /* Extract alpha value */
  1978. alpha_code = (size_t) (alpha_bits >> (3*(4*j+i))) & 0x7;
  1979. if (alpha_code == 0)
  1980. alpha = a0;
  1981. else if (alpha_code == 1)
  1982. alpha = a1;
  1983. else if (a0 > a1)
  1984. alpha = ((8-alpha_code) * a0 + (alpha_code-1) * a1) / 7;
  1985. else if (alpha_code == 6)
  1986. alpha = 0;
  1987. else if (alpha_code == 7)
  1988. alpha = 255;
  1989. else
  1990. alpha = (((6-alpha_code) * a0 + (alpha_code-1) * a1) / 5);
  1991. SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) alpha),q);
  1992. q+=GetPixelChannels(image);
  1993. }
  1994. }
  1995. }
  1996. if (SyncAuthenticPixels(image,exception) == MagickFalse)
  1997. return(MagickFalse);
  1998. }
  1999. if (EOFBlob(image) != MagickFalse)
  2000. return(MagickFalse);
  2001. }
  2002. return(MagickTrue);
  2003. }
  2004. static MagickBooleanType ReadDXT5(const ImageInfo *image_info,Image *image,
  2005. DDSInfo *dds_info,const MagickBooleanType read_mipmaps,
  2006. ExceptionInfo *exception)
  2007. {
  2008. if (ReadDXT5Pixels(image,dds_info,exception) == MagickFalse)
  2009. return(MagickFalse);
  2010. if (read_mipmaps != MagickFalse)
  2011. return(ReadMipmaps(image_info,image,dds_info,ReadDXT5Pixels,exception));
  2012. else
  2013. return(SkipDXTMipmaps(image,dds_info,16,exception));
  2014. }
  2015. static MagickBooleanType ReadUncompressedRGBPixels(Image *image,
  2016. DDSInfo *dds_info,ExceptionInfo *exception)
  2017. {
  2018. register Quantum
  2019. *q;
  2020. ssize_t
  2021. x, y;
  2022. unsigned short
  2023. color;
  2024. for (y = 0; y < (ssize_t) image->rows; y++)
  2025. {
  2026. q = QueueAuthenticPixels(image, 0, y, image->columns, 1,exception);
  2027. if (q == (Quantum *) NULL)
  2028. return(MagickFalse);
  2029. for (x = 0; x < (ssize_t) image->columns; x++)
  2030. {
  2031. if (dds_info->pixelformat.rgb_bitcount == 8)
  2032. SetPixelGray(image,ScaleCharToQuantum(ReadBlobByte(image)),q);
  2033. else if (dds_info->pixelformat.rgb_bitcount == 16)
  2034. {
  2035. color=ReadBlobShort(image);
  2036. SetPixelRed(image,ScaleCharToQuantum((unsigned char)
  2037. (((color >> 11)/31.0)*255)),q);
  2038. SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
  2039. ((((unsigned short)(color << 5) >> 10)/63.0)*255)),q);
  2040. SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
  2041. ((((unsigned short)(color << 11) >> 11)/31.0)*255)),q);
  2042. }
  2043. else
  2044. {
  2045. SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
  2046. ReadBlobByte(image)),q);
  2047. SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
  2048. ReadBlobByte(image)),q);
  2049. SetPixelRed(image,ScaleCharToQuantum((unsigned char)
  2050. ReadBlobByte(image)),q);
  2051. if (dds_info->pixelformat.rgb_bitcount == 32)
  2052. (void) ReadBlobByte(image);
  2053. }
  2054. q+=GetPixelChannels(image);
  2055. }
  2056. if (SyncAuthenticPixels(image,exception) == MagickFalse)
  2057. return(MagickFalse);
  2058. if (EOFBlob(image) != MagickFalse)
  2059. return(MagickFalse);
  2060. }
  2061. return(MagickTrue);
  2062. }
  2063. static MagickBooleanType ReadUncompressedRGB(const ImageInfo *image_info,
  2064. Image *image,DDSInfo *dds_info,const MagickBooleanType read_mipmaps,
  2065. ExceptionInfo *exception)
  2066. {
  2067. if (dds_info->pixelformat.rgb_bitcount == 8)
  2068. (void) SetImageType(image,GrayscaleType,exception);
  2069. else if (dds_info->pixelformat.rgb_bitcount == 16 && !IsBitMask(
  2070. dds_info->pixelformat,0xf800,0x07e0,0x001f,0x0000))
  2071. ThrowBinaryException(CorruptImageError,"ImageTypeNotSupported",
  2072. image->filename);
  2073. if (ReadUncompressedRGBPixels(image,dds_info,exception) == MagickFalse)
  2074. return(MagickFalse);
  2075. if (read_mipmaps != MagickFalse)
  2076. return(ReadMipmaps(image_info,image,dds_info,ReadUncompressedRGBPixels,
  2077. exception));
  2078. else
  2079. return(SkipRGBMipmaps(image,dds_info,3,exception));
  2080. }
  2081. static MagickBooleanType ReadUncompressedRGBAPixels(Image *image,
  2082. DDSInfo *dds_info,ExceptionInfo *exception)
  2083. {
  2084. register Quantum
  2085. *q;
  2086. ssize_t
  2087. alphaBits,
  2088. x,
  2089. y;
  2090. unsigned short
  2091. color;
  2092. alphaBits=0;
  2093. if (dds_info->pixelformat.rgb_bitcount == 16)
  2094. {
  2095. if (IsBitMask(dds_info->pixelformat,0x7c00,0x03e0,0x001f,0x8000))
  2096. alphaBits=1;
  2097. else if (IsBitMask(dds_info->pixelformat,0x00ff,0x00ff,0x00ff,0xff00))
  2098. {
  2099. alphaBits=2;
  2100. (void) SetImageType(image,GrayscaleAlphaType,exception);
  2101. }
  2102. else if (IsBitMask(dds_info->pixelformat,0x0f00,0x00f0,0x000f,0xf000))
  2103. alphaBits=4;
  2104. else
  2105. ThrowBinaryException(CorruptImageError,"ImageTypeNotSupported",
  2106. image->filename);
  2107. }
  2108. for (y = 0; y < (ssize_t) image->rows; y++)
  2109. {
  2110. q = QueueAuthenticPixels(image, 0, y, image->columns, 1,exception);
  2111. if (q == (Quantum *) NULL)
  2112. return(MagickFalse);
  2113. for (x = 0; x < (ssize_t) image->columns; x++)
  2114. {
  2115. if (dds_info->pixelformat.rgb_bitcount == 16)
  2116. {
  2117. color=ReadBlobShort(image);
  2118. if (alphaBits == 1)
  2119. {
  2120. SetPixelAlpha(image,(color & (1 << 15)) ? QuantumRange : 0,q);
  2121. SetPixelRed(image,ScaleCharToQuantum((unsigned char)
  2122. ((((unsigned short)(color << 1) >> 11)/31.0)*255)),q);
  2123. SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
  2124. ((((unsigned short)(color << 6) >> 11)/31.0)*255)),q);
  2125. SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
  2126. ((((unsigned short)(color << 11) >> 11)/31.0)*255)),q);
  2127. }
  2128. else if (alphaBits == 2)
  2129. {
  2130. SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
  2131. (color >> 8)),q);
  2132. SetPixelGray(image,ScaleCharToQuantum((unsigned char)color),q);
  2133. }
  2134. else
  2135. {
  2136. SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
  2137. (((color >> 12)/15.0)*255)),q);
  2138. SetPixelRed(image,ScaleCharToQuantum((unsigned char)
  2139. ((((unsigned short)(color << 4) >> 12)/15.0)*255)),q);
  2140. SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
  2141. ((((unsigned short)(color << 8) >> 12)/15.0)*255)),q);
  2142. SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
  2143. ((((unsigned short)(color << 12) >> 12)/15.0)*255)),q);
  2144. }
  2145. }
  2146. else
  2147. {
  2148. SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
  2149. ReadBlobByte(image)),q);
  2150. SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
  2151. ReadBlobByte(image)),q);
  2152. SetPixelRed(image,ScaleCharToQuantum((unsigned char)
  2153. ReadBlobByte(image)),q);
  2154. SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
  2155. ReadBlobByte(image)),q);
  2156. }
  2157. q+=GetPixelChannels(image);
  2158. }
  2159. if (SyncAuthenticPixels(image,exception) == MagickFalse)
  2160. return(MagickFalse);
  2161. if (EOFBlob(image) != MagickFalse)
  2162. return(MagickFalse);
  2163. }
  2164. return(MagickTrue);
  2165. }
  2166. static MagickBooleanType ReadUncompressedRGBA(const ImageInfo *image_info,
  2167. Image *image,DDSInfo *dds_info,const MagickBooleanType read_mipmaps,
  2168. ExceptionInfo *exception)
  2169. {
  2170. if (ReadUncompressedRGBAPixels(image,dds_info,exception) == MagickFalse)
  2171. return(MagickFalse);
  2172. if (read_mipmaps != MagickFalse)
  2173. return(ReadMipmaps(image_info,image,dds_info,ReadUncompressedRGBAPixels,
  2174. exception));
  2175. else
  2176. return(SkipRGBMipmaps(image,dds_info,4,exception));
  2177. }
  2178. /*
  2179. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2180. % %
  2181. % %
  2182. % %
  2183. % R e g i s t e r D D S I m a g e %
  2184. % %
  2185. % %
  2186. % %
  2187. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2188. %
  2189. % RegisterDDSImage() adds attributes for the DDS image format to
  2190. % the list of supported formats. The attributes include the image format
  2191. % tag, a method to read and/or write the format, whether the format
  2192. % supports the saving of more than one frame to the same file or blob,
  2193. % whether the format supports native in-memory I/O, and a brief
  2194. % description of the format.
  2195. %
  2196. % The format of the RegisterDDSImage method is:
  2197. %
  2198. % RegisterDDSImage(void)
  2199. %
  2200. */
  2201. ModuleExport size_t RegisterDDSImage(void)
  2202. {
  2203. MagickInfo
  2204. *entry;
  2205. entry = AcquireMagickInfo("DDS","DDS","Microsoft DirectDraw Surface");
  2206. entry->decoder = (DecodeImageHandler *) ReadDDSImage;
  2207. entry->encoder = (EncodeImageHandler *) WriteDDSImage;
  2208. entry->magick = (IsImageFormatHandler *) IsDDS;
  2209. entry->flags|=CoderDecoderSeekableStreamFlag;
  2210. (void) RegisterMagickInfo(entry);
  2211. entry = AcquireMagickInfo("DDS","DXT1","Microsoft DirectDraw Surface");
  2212. entry->decoder = (DecodeImageHandler *) ReadDDSImage;
  2213. entry->encoder = (EncodeImageHandler *) WriteDDSImage;
  2214. entry->magick = (IsImageFormatHandler *) IsDDS;
  2215. entry->flags|=CoderDecoderSeekableStreamFlag;
  2216. (void) RegisterMagickInfo(entry);
  2217. entry = AcquireMagickInfo("DDS","DXT5","Microsoft DirectDraw Surface");
  2218. entry->decoder = (DecodeImageHandler *) ReadDDSImage;
  2219. entry->encoder = (EncodeImageHandler *) WriteDDSImage;
  2220. entry->magick = (IsImageFormatHandler *) IsDDS;
  2221. entry->flags|=CoderDecoderSeekableStreamFlag;
  2222. (void) RegisterMagickInfo(entry);
  2223. return(MagickImageCoderSignature);
  2224. }
  2225. static void RemapIndices(const ssize_t *map, const unsigned char *source,
  2226. unsigned char *target)
  2227. {
  2228. register ssize_t
  2229. i;
  2230. for (i = 0; i < 16; i++)
  2231. {
  2232. if (map[i] == -1)
  2233. target[i] = 3;
  2234. else
  2235. target[i] = source[map[i]];
  2236. }
  2237. }
  2238. /*
  2239. Skip the mipmap images for compressed (DXTn) dds files
  2240. */
  2241. static MagickBooleanType SkipDXTMipmaps(Image *image,DDSInfo *dds_info,
  2242. int texel_size,ExceptionInfo *exception)
  2243. {
  2244. /*
  2245. Only skip mipmaps for textures and cube maps
  2246. */
  2247. if (EOFBlob(image) != MagickFalse)
  2248. {
  2249. ThrowFileException(exception,CorruptImageWarning,"UnexpectedEndOfFile",
  2250. image->filename);
  2251. return(MagickFalse);
  2252. }
  2253. if (dds_info->ddscaps1 & DDSCAPS_MIPMAP
  2254. && (dds_info->ddscaps1 & DDSCAPS_TEXTURE
  2255. || dds_info->ddscaps2 & DDSCAPS2_CUBEMAP))
  2256. {
  2257. MagickOffsetType
  2258. offset;
  2259. register ssize_t
  2260. i;
  2261. size_t
  2262. h,
  2263. w;
  2264. w=DIV2(dds_info->width);
  2265. h=DIV2(dds_info->height);
  2266. /*
  2267. Mipmapcount includes the main image, so start from one
  2268. */
  2269. for (i = 1; (i < (ssize_t) dds_info->mipmapcount) && w && h; i++)
  2270. {
  2271. offset=(MagickOffsetType)((w+3)/4)*((h+3)/4)*texel_size;
  2272. if (SeekBlob(image,offset,SEEK_CUR) < 0)
  2273. break;
  2274. w=DIV2(w);
  2275. h=DIV2(h);
  2276. if ((w == 1) && (h == 1))
  2277. break;
  2278. }
  2279. }
  2280. return(MagickTrue);
  2281. }
  2282. /*
  2283. Skip the mipmap images for uncompressed (RGB or RGBA) dds files
  2284. */
  2285. static MagickBooleanType SkipRGBMipmaps(Image *image,DDSInfo *dds_info,
  2286. int pixel_size,ExceptionInfo *exception)
  2287. {
  2288. /*
  2289. Only skip mipmaps for textures and cube maps
  2290. */
  2291. if (EOFBlob(image) != MagickFalse)
  2292. {
  2293. ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
  2294. image->filename);
  2295. return(MagickFalse);
  2296. }
  2297. if (dds_info->ddscaps1 & DDSCAPS_MIPMAP
  2298. && (dds_info->ddscaps1 & DDSCAPS_TEXTURE
  2299. || dds_info->ddscaps2 & DDSCAPS2_CUBEMAP))
  2300. {
  2301. MagickOffsetType
  2302. offset;
  2303. register ssize_t
  2304. i;
  2305. size_t
  2306. h,
  2307. w;
  2308. w=DIV2(dds_info->width);
  2309. h=DIV2(dds_info->height);
  2310. /*
  2311. Mipmapcount includes the main image, so start from one
  2312. */
  2313. for (i=1; (i < (ssize_t) dds_info->mipmapcount) && w && h; i++)
  2314. {
  2315. offset=(MagickOffsetType)w*h*pixel_size;
  2316. if (SeekBlob(image,offset,SEEK_CUR) < 0)
  2317. break;
  2318. w=DIV2(w);
  2319. h=DIV2(h);
  2320. if ((w == 1) && (h == 1))
  2321. break;
  2322. }
  2323. }
  2324. return(MagickTrue);
  2325. }
  2326. /*
  2327. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2328. % %
  2329. % %
  2330. % %
  2331. % U n r e g i s t e r D D S I m a g e %
  2332. % %
  2333. % %
  2334. % %
  2335. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2336. %
  2337. % UnregisterDDSImage() removes format registrations made by the
  2338. % DDS module from the list of supported formats.
  2339. %
  2340. % The format of the UnregisterDDSImage method is:
  2341. %
  2342. % UnregisterDDSImage(void)
  2343. %
  2344. */
  2345. ModuleExport void UnregisterDDSImage(void)
  2346. {
  2347. (void) UnregisterMagickInfo("DDS");
  2348. (void) UnregisterMagickInfo("DXT1");
  2349. (void) UnregisterMagickInfo("DXT5");
  2350. }
  2351. static void WriteAlphas(Image *image, const ssize_t *alphas, size_t min5,
  2352. size_t max5, size_t min7, size_t max7)
  2353. {
  2354. register ssize_t
  2355. i;
  2356. size_t
  2357. err5,
  2358. err7,
  2359. j;
  2360. unsigned char
  2361. indices5[16],
  2362. indices7[16];
  2363. FixRange(min5,max5,5);
  2364. err5 = CompressAlpha(min5,max5,5,alphas,indices5);
  2365. FixRange(min7,max7,7);
  2366. err7 = CompressAlpha(min7,max7,7,alphas,indices7);
  2367. if (err7 < err5)
  2368. {
  2369. for (i=0; i < 16; i++)
  2370. {
  2371. unsigned char
  2372. index;
  2373. index = indices7[i];
  2374. if( index == 0 )
  2375. indices5[i] = 1;
  2376. else if (index == 1)
  2377. indices5[i] = 0;
  2378. else
  2379. indices5[i] = 9 - index;
  2380. }
  2381. min5 = max7;
  2382. max5 = min7;
  2383. }
  2384. (void) WriteBlobByte(image,(unsigned char) min5);
  2385. (void) WriteBlobByte(image,(unsigned char) max5);
  2386. for(i=0; i < 2; i++)
  2387. {
  2388. size_t
  2389. value = 0;
  2390. for (j=0; j < 8; j++)
  2391. {
  2392. size_t index = (size_t) indices5[j + i*8];
  2393. value |= ( index << 3*j );
  2394. }
  2395. for (j=0; j < 3; j++)
  2396. {
  2397. size_t byte = (value >> 8*j) & 0xff;
  2398. (void) WriteBlobByte(image,(unsigned char) byte);
  2399. }
  2400. }
  2401. }
  2402. static void WriteCompressed(Image *image, const size_t count,
  2403. DDSVector4 *points, const ssize_t *map, const MagickBooleanType clusterFit)
  2404. {
  2405. float
  2406. covariance[16];
  2407. DDSVector3
  2408. end,
  2409. principle,
  2410. start;
  2411. DDSVector4
  2412. metric;
  2413. unsigned char
  2414. indices[16];
  2415. VectorInit(metric,1.0f);
  2416. VectorInit3(start,0.0f);
  2417. VectorInit3(end,0.0f);
  2418. ComputeWeightedCovariance(count,points,covariance);
  2419. ComputePrincipleComponent(covariance,&principle);
  2420. if ((clusterFit == MagickFalse) || (count == 0))
  2421. CompressRangeFit(count,points,map,principle,metric,&start,&end,indices);
  2422. else
  2423. CompressClusterFit(count,points,map,principle,metric,&start,&end,indices);
  2424. WriteIndices(image,start,end,indices);
  2425. }
  2426. /*
  2427. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2428. % %
  2429. % %
  2430. % %
  2431. % W r i t e D D S I m a g e %
  2432. % %
  2433. % %
  2434. % %
  2435. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2436. %
  2437. % WriteDDSImage() writes a DirectDraw Surface image file in the DXT5 format.
  2438. %
  2439. % The format of the WriteBMPImage method is:
  2440. %
  2441. % MagickBooleanType WriteDDSImage(const ImageInfo *image_info,Image *image)
  2442. %
  2443. % A description of each parameter follows.
  2444. %
  2445. % o image_info: the image info.
  2446. %
  2447. % o image: The image.
  2448. %
  2449. */
  2450. static MagickBooleanType WriteDDSImage(const ImageInfo *image_info,
  2451. Image *image, ExceptionInfo *exception)
  2452. {
  2453. const char
  2454. *option;
  2455. size_t
  2456. compression,
  2457. columns,
  2458. maxMipmaps,
  2459. mipmaps,
  2460. pixelFormat,
  2461. rows;
  2462. MagickBooleanType
  2463. clusterFit,
  2464. fromlist,
  2465. status,
  2466. weightByAlpha;
  2467. assert(image_info != (const ImageInfo *) NULL);
  2468. assert(image_info->signature == MagickCoreSignature);
  2469. assert(image != (Image *) NULL);
  2470. assert(image->signature == MagickCoreSignature);
  2471. if (image->debug != MagickFalse)
  2472. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  2473. status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
  2474. if (status == MagickFalse)
  2475. return(status);
  2476. (void) TransformImageColorspace(image,sRGBColorspace,exception);
  2477. pixelFormat=DDPF_FOURCC;
  2478. compression=FOURCC_DXT5;
  2479. if (image->alpha_trait == UndefinedPixelTrait)
  2480. compression=FOURCC_DXT1;
  2481. if (LocaleCompare(image_info->magick,"dxt1") == 0)
  2482. compression=FOURCC_DXT1;
  2483. option=GetImageOption(image_info,"dds:compression");
  2484. if (option != (char *) NULL)
  2485. {
  2486. if (LocaleCompare(option,"dxt1") == 0)
  2487. compression=FOURCC_DXT1;
  2488. if (LocaleCompare(option,"none") == 0)
  2489. pixelFormat=DDPF_RGB;
  2490. }
  2491. clusterFit=MagickFalse;
  2492. weightByAlpha=MagickFalse;
  2493. if (pixelFormat == DDPF_FOURCC)
  2494. {
  2495. option=GetImageOption(image_info,"dds:cluster-fit");
  2496. if (IsStringTrue(option) != MagickFalse)
  2497. {
  2498. clusterFit=MagickTrue;
  2499. if (compression != FOURCC_DXT1)
  2500. {
  2501. option=GetImageOption(image_info,"dds:weight-by-alpha");
  2502. if (IsStringTrue(option) != MagickFalse)
  2503. weightByAlpha=MagickTrue;
  2504. }
  2505. }
  2506. }
  2507. mipmaps=0;
  2508. fromlist=MagickFalse;
  2509. option=GetImageOption(image_info,"dds:mipmaps");
  2510. if (option != (char *) NULL)
  2511. {
  2512. if (LocaleNCompare(option,"fromlist",8) == 0)
  2513. {
  2514. Image
  2515. *next;
  2516. fromlist=MagickTrue;
  2517. next=image->next;
  2518. while(next != (Image *) NULL)
  2519. {
  2520. mipmaps++;
  2521. next=next->next;
  2522. }
  2523. }
  2524. }
  2525. if ((mipmaps == 0) &&
  2526. ((image->columns & (image->columns - 1)) == 0) &&
  2527. ((image->rows & (image->rows - 1)) == 0))
  2528. {
  2529. maxMipmaps=SIZE_MAX;
  2530. if (option != (char *) NULL)
  2531. maxMipmaps=StringToUnsignedLong(option);
  2532. if (maxMipmaps != 0)
  2533. {
  2534. columns=image->columns;
  2535. rows=image->rows;
  2536. while ((columns != 1 || rows != 1) && mipmaps != maxMipmaps)
  2537. {
  2538. columns=DIV2(columns);
  2539. rows=DIV2(rows);
  2540. mipmaps++;
  2541. }
  2542. }
  2543. }
  2544. option=GetImageOption(image_info,"dds:raw");
  2545. if (IsStringTrue(option) == MagickFalse)
  2546. WriteDDSInfo(image,pixelFormat,compression,mipmaps);
  2547. else
  2548. mipmaps=0;
  2549. WriteImageData(image,pixelFormat,compression,clusterFit,weightByAlpha,
  2550. exception);
  2551. if ((mipmaps > 0) && (WriteMipmaps(image,image_info,pixelFormat,compression,
  2552. mipmaps,fromlist,clusterFit,weightByAlpha,exception) == MagickFalse))
  2553. return(MagickFalse);
  2554. (void) CloseBlob(image);
  2555. return(MagickTrue);
  2556. }
  2557. static void WriteDDSInfo(Image *image, const size_t pixelFormat,
  2558. const size_t compression, const size_t mipmaps)
  2559. {
  2560. char
  2561. software[MagickPathExtent];
  2562. register ssize_t
  2563. i;
  2564. unsigned int
  2565. format,
  2566. caps,
  2567. flags;
  2568. flags=(unsigned int) (DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT |
  2569. DDSD_PIXELFORMAT);
  2570. caps=(unsigned int) DDSCAPS_TEXTURE;
  2571. format=(unsigned int) pixelFormat;
  2572. if (format == DDPF_FOURCC)
  2573. flags=flags | DDSD_LINEARSIZE;
  2574. else
  2575. flags=flags | DDSD_PITCH;
  2576. if (mipmaps > 0)
  2577. {
  2578. flags=flags | (unsigned int) DDSD_MIPMAPCOUNT;
  2579. caps=caps | (unsigned int) (DDSCAPS_MIPMAP | DDSCAPS_COMPLEX);
  2580. }
  2581. if (format != DDPF_FOURCC && image->alpha_trait != UndefinedPixelTrait)
  2582. format=format | DDPF_ALPHAPIXELS;
  2583. (void) WriteBlob(image,4,(unsigned char *) "DDS ");
  2584. (void) WriteBlobLSBLong(image,124);
  2585. (void) WriteBlobLSBLong(image,flags);
  2586. (void) WriteBlobLSBLong(image,(unsigned int) image->rows);
  2587. (void) WriteBlobLSBLong(image,(unsigned int) image->columns);
  2588. if (pixelFormat == DDPF_FOURCC)
  2589. {
  2590. /* Compressed DDS requires linear compressed size of first image */
  2591. if (compression == FOURCC_DXT1)
  2592. (void) WriteBlobLSBLong(image,(unsigned int) (MagickMax(1,
  2593. (image->columns+3)/4)*MagickMax(1,(image->rows+3)/4)*8));
  2594. else /* DXT5 */
  2595. (void) WriteBlobLSBLong(image,(unsigned int) (MagickMax(1,
  2596. (image->columns+3)/4)*MagickMax(1,(image->rows+3)/4)*16));
  2597. }
  2598. else
  2599. {
  2600. /* Uncompressed DDS requires byte pitch of first image */
  2601. if (image->alpha_trait != UndefinedPixelTrait)
  2602. (void) WriteBlobLSBLong(image,(unsigned int) (image->columns * 4));
  2603. else
  2604. (void) WriteBlobLSBLong(image,(unsigned int) (image->columns * 3));
  2605. }
  2606. (void) WriteBlobLSBLong(image,0x00);
  2607. (void) WriteBlobLSBLong(image,(unsigned int) mipmaps+1);
  2608. (void) memset(software,0,sizeof(software));
  2609. (void) CopyMagickString(software,"IMAGEMAGICK",MagickPathExtent);
  2610. (void) WriteBlob(image,44,(unsigned char *) software);
  2611. (void) WriteBlobLSBLong(image,32);
  2612. (void) WriteBlobLSBLong(image,format);
  2613. if (pixelFormat == DDPF_FOURCC)
  2614. {
  2615. (void) WriteBlobLSBLong(image,(unsigned int) compression);
  2616. for(i=0;i < 5;i++) /* bitcount / masks */
  2617. (void) WriteBlobLSBLong(image,0x00);
  2618. }
  2619. else
  2620. {
  2621. (void) WriteBlobLSBLong(image,0x00);
  2622. if (image->alpha_trait != UndefinedPixelTrait)
  2623. {
  2624. (void) WriteBlobLSBLong(image,32);
  2625. (void) WriteBlobLSBLong(image,0xff0000);
  2626. (void) WriteBlobLSBLong(image,0xff00);
  2627. (void) WriteBlobLSBLong(image,0xff);
  2628. (void) WriteBlobLSBLong(image,0xff000000);
  2629. }
  2630. else
  2631. {
  2632. (void) WriteBlobLSBLong(image,24);
  2633. (void) WriteBlobLSBLong(image,0xff0000);
  2634. (void) WriteBlobLSBLong(image,0xff00);
  2635. (void) WriteBlobLSBLong(image,0xff);
  2636. (void) WriteBlobLSBLong(image,0x00);
  2637. }
  2638. }
  2639. (void) WriteBlobLSBLong(image,caps);
  2640. for(i=0;i < 4;i++) /* ddscaps2 + reserved region */
  2641. (void) WriteBlobLSBLong(image,0x00);
  2642. }
  2643. static void WriteFourCC(Image *image, const size_t compression,
  2644. const MagickBooleanType clusterFit, const MagickBooleanType weightByAlpha,
  2645. ExceptionInfo *exception)
  2646. {
  2647. register ssize_t
  2648. x;
  2649. ssize_t
  2650. i,
  2651. y,
  2652. bx,
  2653. by;
  2654. register const Quantum
  2655. *p;
  2656. for (y=0; y < (ssize_t) image->rows; y+=4)
  2657. {
  2658. for (x=0; x < (ssize_t) image->columns; x+=4)
  2659. {
  2660. MagickBooleanType
  2661. match;
  2662. DDSVector4
  2663. point,
  2664. points[16];
  2665. size_t
  2666. count = 0,
  2667. max5 = 0,
  2668. max7 = 0,
  2669. min5 = 255,
  2670. min7 = 255,
  2671. columns = 4,
  2672. rows = 4;
  2673. ssize_t
  2674. alphas[16],
  2675. map[16];
  2676. unsigned char
  2677. alpha;
  2678. if (x + columns >= image->columns)
  2679. columns = image->columns - x;
  2680. if (y + rows >= image->rows)
  2681. rows = image->rows - y;
  2682. p=GetVirtualPixels(image,x,y,columns,rows,exception);
  2683. if (p == (const Quantum *) NULL)
  2684. break;
  2685. for (i=0; i<16; i++)
  2686. {
  2687. map[i] = -1;
  2688. alphas[i] = -1;
  2689. }
  2690. for (by=0; by < (ssize_t) rows; by++)
  2691. {
  2692. for (bx=0; bx < (ssize_t) columns; bx++)
  2693. {
  2694. if (compression == FOURCC_DXT5)
  2695. alpha = ScaleQuantumToChar(GetPixelAlpha(image,p));
  2696. else
  2697. alpha = 255;
  2698. if (compression == FOURCC_DXT5)
  2699. {
  2700. if (alpha < min7)
  2701. min7 = alpha;
  2702. if (alpha > max7)
  2703. max7 = alpha;
  2704. if (alpha != 0 && alpha < min5)
  2705. min5 = alpha;
  2706. if (alpha != 255 && alpha > max5)
  2707. max5 = alpha;
  2708. }
  2709. alphas[4*by + bx] = (size_t)alpha;
  2710. point.x = (float)ScaleQuantumToChar(GetPixelRed(image,p)) / 255.0f;
  2711. point.y = (float)ScaleQuantumToChar(GetPixelGreen(image,p)) / 255.0f;
  2712. point.z = (float)ScaleQuantumToChar(GetPixelBlue(image,p)) / 255.0f;
  2713. point.w = weightByAlpha ? (float)(alpha + 1) / 256.0f : 1.0f;
  2714. p+=GetPixelChannels(image);
  2715. match = MagickFalse;
  2716. for (i=0; i < (ssize_t) count; i++)
  2717. {
  2718. if ((points[i].x == point.x) &&
  2719. (points[i].y == point.y) &&
  2720. (points[i].z == point.z) &&
  2721. (alpha >= 128 || compression == FOURCC_DXT5))
  2722. {
  2723. points[i].w += point.w;
  2724. map[4*by + bx] = i;
  2725. match = MagickTrue;
  2726. break;
  2727. }
  2728. }
  2729. if (match != MagickFalse)
  2730. continue;
  2731. points[count].x = point.x;
  2732. points[count].y = point.y;
  2733. points[count].z = point.z;
  2734. points[count].w = point.w;
  2735. map[4*by + bx] = count;
  2736. count++;
  2737. }
  2738. }
  2739. for (i=0; i < (ssize_t) count; i++)
  2740. points[i].w = sqrt(points[i].w);
  2741. if (compression == FOURCC_DXT5)
  2742. WriteAlphas(image,alphas,min5,max5,min7,max7);
  2743. if (count == 1)
  2744. WriteSingleColorFit(image,points,map);
  2745. else
  2746. WriteCompressed(image,count,points,map,clusterFit);
  2747. }
  2748. }
  2749. }
  2750. static void WriteImageData(Image *image, const size_t pixelFormat,
  2751. const size_t compression,const MagickBooleanType clusterFit,
  2752. const MagickBooleanType weightByAlpha, ExceptionInfo *exception)
  2753. {
  2754. if (pixelFormat == DDPF_FOURCC)
  2755. WriteFourCC(image,compression,clusterFit,weightByAlpha,exception);
  2756. else
  2757. WriteUncompressed(image,exception);
  2758. }
  2759. static inline size_t ClampToLimit(const float value, const size_t limit)
  2760. {
  2761. size_t
  2762. result = (int) (value + 0.5f);
  2763. if (result < 0.0f)
  2764. return(0);
  2765. if (result > limit)
  2766. return(limit);
  2767. return result;
  2768. }
  2769. static inline size_t ColorTo565(const DDSVector3 point)
  2770. {
  2771. size_t r = ClampToLimit(31.0f*point.x,31);
  2772. size_t g = ClampToLimit(63.0f*point.y,63);
  2773. size_t b = ClampToLimit(31.0f*point.z,31);
  2774. return (r << 11) | (g << 5) | b;
  2775. }
  2776. static void WriteIndices(Image *image, const DDSVector3 start,
  2777. const DDSVector3 end, unsigned char *indices)
  2778. {
  2779. register ssize_t
  2780. i;
  2781. size_t
  2782. a,
  2783. b;
  2784. unsigned char
  2785. remapped[16];
  2786. const unsigned char
  2787. *ind;
  2788. a = ColorTo565(start);
  2789. b = ColorTo565(end);
  2790. for (i=0; i<16; i++)
  2791. {
  2792. if( a < b )
  2793. remapped[i] = (indices[i] ^ 0x1) & 0x3;
  2794. else if( a == b )
  2795. remapped[i] = 0;
  2796. else
  2797. remapped[i] = indices[i];
  2798. }
  2799. if( a < b )
  2800. Swap(a,b);
  2801. (void) WriteBlobByte(image,(unsigned char) (a & 0xff));
  2802. (void) WriteBlobByte(image,(unsigned char) (a >> 8));
  2803. (void) WriteBlobByte(image,(unsigned char) (b & 0xff));
  2804. (void) WriteBlobByte(image,(unsigned char) (b >> 8));
  2805. for (i=0; i<4; i++)
  2806. {
  2807. ind = remapped + 4*i;
  2808. (void) WriteBlobByte(image,ind[0] | (ind[1] << 2) | (ind[2] << 4) |
  2809. (ind[3] << 6));
  2810. }
  2811. }
  2812. static MagickBooleanType WriteMipmaps(Image *image,const ImageInfo *image_info,
  2813. const size_t pixelFormat,const size_t compression,const size_t mipmaps,
  2814. const MagickBooleanType fromlist,const MagickBooleanType clusterFit,
  2815. const MagickBooleanType weightByAlpha,ExceptionInfo *exception)
  2816. {
  2817. const char
  2818. *option;
  2819. Image
  2820. *mipmap_image,
  2821. *resize_image;
  2822. MagickBooleanType
  2823. fast_mipmaps,
  2824. status;
  2825. register ssize_t
  2826. i;
  2827. size_t
  2828. columns,
  2829. rows;
  2830. columns=DIV2(image->columns);
  2831. rows=DIV2(image->rows);
  2832. option=GetImageOption(image_info,"dds:fast-mipmaps");
  2833. fast_mipmaps=IsStringTrue(option);
  2834. mipmap_image=image;
  2835. resize_image=image;
  2836. status=MagickTrue;
  2837. for (i=0; i < (ssize_t) mipmaps; i++)
  2838. {
  2839. if (fromlist == MagickFalse)
  2840. {
  2841. mipmap_image=ResizeImage(resize_image,columns,rows,TriangleFilter,
  2842. exception);
  2843. if (mipmap_image == (Image *) NULL)
  2844. {
  2845. status=MagickFalse;
  2846. break;
  2847. }
  2848. }
  2849. else
  2850. {
  2851. mipmap_image=mipmap_image->next;
  2852. if ((mipmap_image->columns != columns) || (mipmap_image->rows != rows))
  2853. ThrowBinaryException(CoderError,"ImageColumnOrRowSizeIsNotSupported",
  2854. image->filename);
  2855. }
  2856. DestroyBlob(mipmap_image);
  2857. mipmap_image->blob=ReferenceBlob(image->blob);
  2858. WriteImageData(mipmap_image,pixelFormat,compression,weightByAlpha,
  2859. clusterFit,exception);
  2860. if (fromlist == MagickFalse)
  2861. {
  2862. if (fast_mipmaps == MagickFalse)
  2863. mipmap_image=DestroyImage(mipmap_image);
  2864. else
  2865. {
  2866. if (resize_image != image)
  2867. resize_image=DestroyImage(resize_image);
  2868. resize_image=mipmap_image;
  2869. }
  2870. }
  2871. columns=DIV2(columns);
  2872. rows=DIV2(rows);
  2873. }
  2874. if (resize_image != image)
  2875. resize_image=DestroyImage(resize_image);
  2876. return(status);
  2877. }
  2878. static void WriteSingleColorFit(Image *image, const DDSVector4 *points,
  2879. const ssize_t *map)
  2880. {
  2881. DDSVector3
  2882. start,
  2883. end;
  2884. register ssize_t
  2885. i;
  2886. unsigned char
  2887. color[3],
  2888. index,
  2889. indexes[16],
  2890. indices[16];
  2891. color[0] = (unsigned char) ClampToLimit(255.0f*points->x,255);
  2892. color[1] = (unsigned char) ClampToLimit(255.0f*points->y,255);
  2893. color[2] = (unsigned char) ClampToLimit(255.0f*points->z,255);
  2894. index=0;
  2895. ComputeEndPoints(DDS_LOOKUP,color,&start,&end,&index);
  2896. for (i=0; i< 16; i++)
  2897. indexes[i]=index;
  2898. RemapIndices(map,indexes,indices);
  2899. WriteIndices(image,start,end,indices);
  2900. }
  2901. static void WriteUncompressed(Image *image, ExceptionInfo *exception)
  2902. {
  2903. register const Quantum
  2904. *p;
  2905. register ssize_t
  2906. x;
  2907. ssize_t
  2908. y;
  2909. for (y=0; y < (ssize_t) image->rows; y++)
  2910. {
  2911. p=GetVirtualPixels(image,0,y,image->columns,1,exception);
  2912. if (p == (const Quantum *) NULL)
  2913. break;
  2914. for (x=0; x < (ssize_t) image->columns; x++)
  2915. {
  2916. (void) WriteBlobByte(image,ScaleQuantumToChar(GetPixelBlue(image,p)));
  2917. (void) WriteBlobByte(image,ScaleQuantumToChar(GetPixelGreen(image,p)));
  2918. (void) WriteBlobByte(image,ScaleQuantumToChar(GetPixelRed(image,p)));
  2919. if (image->alpha_trait != UndefinedPixelTrait)
  2920. (void) WriteBlobByte(image,ScaleQuantumToChar(GetPixelAlpha(image,p)));
  2921. p+=GetPixelChannels(image);
  2922. }
  2923. }
  2924. }