PageRenderTime 46ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/source/juno/media/imaging.d

http://github.com/JesseKPhillips/Juno-Windows-Class-Library
D | 551 lines | 346 code | 91 blank | 114 comment | 48 complexity | 3fc22273db2637e65aff856082baab3d MD5 | raw file
  1. /**
  2. * Provides access to advanced GDI+ graphics functionality.
  3. *
  4. * For detailed information, refer to MSDN's documentation for the $(LINK2 http://msdn2.microsoft.com/en-us/library/system.drawing.imaging.aspx, System.Drawing.Imaging) namespace.
  5. *
  6. * Copyright: (c) 2009 John Chapman
  7. *
  8. * License: See $(LINK2 ..\..\licence.txt, licence.txt) for use and distribution terms.
  9. */
  10. module juno.media.imaging;
  11. import juno.base.core,
  12. juno.base.string,
  13. juno.base.native,
  14. juno.com.core,
  15. juno.media.constants,
  16. juno.media.core,
  17. juno.media.native;
  18. import core.stdc.string;
  19. import std.conv;
  20. /**
  21. * Specifies the attributes of a bitmap image.
  22. */
  23. final class BitmapData {
  24. int width; /// Gets or sets the pixel _width of the Bitmap object.
  25. int height; /// Gets of sets the pixel _height of the Bitmap object.
  26. int stride; /// Gets or sets the _stride width of the Bitmap object.
  27. PixelFormat pixelFormat; /// Gets or sets the format of the pixel information in the Bitmap object.
  28. void* scan0; /// Gets or sets the address of the first pixel data in the Bitmap object.
  29. int reserved; /// Reserved. Do not use.
  30. }
  31. /**
  32. */
  33. final class PropertyItem {
  34. int id; ///
  35. uint len; ///
  36. ushort type; ///
  37. ubyte[] value; //
  38. }
  39. /**
  40. */
  41. final class ImageFormat {
  42. private static ImageFormat memoryBmp_;
  43. private static ImageFormat bmp_;
  44. private static ImageFormat emf_;
  45. private static ImageFormat wmf_;
  46. private static ImageFormat jpeg_;
  47. private static ImageFormat png_;
  48. private static ImageFormat gif_;
  49. private static ImageFormat tiff_;
  50. private static ImageFormat exif_;
  51. private static ImageFormat icon_;
  52. private Guid guid_;
  53. /**
  54. */
  55. this(Guid guid) {
  56. guid_ = guid;
  57. }
  58. /**
  59. */
  60. @property Guid guid() {
  61. return guid_;
  62. }
  63. /**
  64. */
  65. static @property ImageFormat memoryBmp() {
  66. if (memoryBmp_ is null)
  67. memoryBmp_ = new ImageFormat(Guid(0xb96b3caa, 0x0728, 0x11d3, 0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e));
  68. return memoryBmp_;
  69. }
  70. /**
  71. */
  72. static @property ImageFormat bmp() {
  73. if (bmp_ is null)
  74. bmp_ = new ImageFormat(Guid(0xb96b3cab, 0x0728, 0x11d3, 0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e));
  75. return bmp_;
  76. }
  77. /**
  78. */
  79. static @property ImageFormat emf() {
  80. if (emf_ is null)
  81. emf_ = new ImageFormat(Guid(0xb96b3cac, 0x0728, 0x11d3, 0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e));
  82. return emf_;
  83. }
  84. /**
  85. */
  86. static @property ImageFormat wmf() {
  87. if (wmf_ is null)
  88. wmf_ = new ImageFormat(Guid(0xb96b3cad, 0x0728, 0x11d3, 0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e));
  89. return wmf_;
  90. }
  91. /**
  92. */
  93. static @property ImageFormat jpeg() {
  94. if (jpeg_ is null)
  95. jpeg_ = new ImageFormat(Guid(0xb96b3cae, 0x0728, 0x11d3, 0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e));
  96. return jpeg_;
  97. }
  98. /**
  99. */
  100. static @property ImageFormat png() {
  101. if (png_ is null)
  102. png_ = new ImageFormat(Guid(0xb96b3caf, 0x0728, 0x11d3, 0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e));
  103. return png_;
  104. }
  105. /**
  106. */
  107. static @property ImageFormat gif() {
  108. if (gif_ is null)
  109. gif_ = new ImageFormat(Guid(0xb96b3cb0, 0x0728, 0x11d3, 0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e));
  110. return gif_;
  111. }
  112. /**
  113. */
  114. static @property ImageFormat tiff() {
  115. if (tiff_ is null)
  116. tiff_ = new ImageFormat(Guid(0xb96b3cb1, 0x0728, 0x11d3, 0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e));
  117. return tiff_;
  118. }
  119. /**
  120. */
  121. static @property ImageFormat exif() {
  122. if (exif_ is null)
  123. exif_ = new ImageFormat(Guid(0xb96b3cb2, 0x0728, 0x11d3, 0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e));
  124. return exif_;
  125. }
  126. /**
  127. */
  128. static @property ImageFormat icon() {
  129. if (icon_ !is null)
  130. icon_ = new ImageFormat(Guid(0xb96b3cb5, 0x0728, 0x11d3, 0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e));
  131. return icon_;
  132. }
  133. }
  134. /**
  135. */
  136. final class ImageCodecInfo {
  137. Guid clsid; ///
  138. Guid formatId; ///
  139. string codecName; ///
  140. string dllName; ///
  141. string formatDescription; ///
  142. string filenameExtension; ///
  143. string mimeType; ///
  144. ImageCodecFlags flags; ///
  145. ubyte[][] signaturePatterns; ///
  146. ubyte[][] signatureMasks; ///
  147. /**
  148. */
  149. static ImageCodecInfo[] getImageEncoders() {
  150. int numEncoders, size;
  151. Status status = GdipGetImageEncodersSize(numEncoders, size);
  152. if (status != Status.OK)
  153. throw statusException(status);
  154. auto pCodecs = cast(GpImageCodecInfo*)LocalAlloc(LMEM_FIXED, size);
  155. status = GdipGetImageEncoders(numEncoders, size, pCodecs);
  156. if (status != Status.OK)
  157. throw statusException(status);
  158. auto codecs = new ImageCodecInfo[numEncoders];
  159. for (int i = 0; i < numEncoders; i++) {
  160. with (codecs[i] = new ImageCodecInfo) {
  161. with (pCodecs[i]) {
  162. clsid = Clsid;
  163. formatId = FormatID;
  164. if (CodecName) codecName = to!string(toArray(CodecName));
  165. if (DllName) dllName = to!string(toArray(DllName));
  166. if (FormatDescription) formatDescription = to!string(toArray(FormatDescription));
  167. if (FilenameExtension) filenameExtension = to!string(toArray(FilenameExtension));
  168. if (MimeType) mimeType = to!string(toArray(MimeType));
  169. flags = cast(ImageCodecFlags)Flags;
  170. signaturePatterns.length = SigCount;
  171. signatureMasks.length = SigCount;
  172. for (int j = 0; j < SigCount; j++) {
  173. signaturePatterns[j].length = SigSize;
  174. signatureMasks[j].length = SigSize;
  175. core.stdc.string.memcpy(signaturePatterns[j].ptr, SigPattern + (j * SigSize), SigSize);
  176. core.stdc.string.memcpy(signatureMasks[j].ptr, SigMask + (j * SigSize), SigSize);
  177. }
  178. }
  179. }
  180. }
  181. LocalFree(cast(Handle)pCodecs);
  182. return codecs;
  183. }
  184. /**
  185. */
  186. static ImageCodecInfo[] getImageDecoders() {
  187. int numDecoders, size;
  188. Status status = GdipGetImageDecodersSize(numDecoders, size);
  189. if (status != Status.OK)
  190. throw statusException(status);
  191. auto pCodecs = cast(GpImageCodecInfo*)LocalAlloc(LMEM_FIXED, size);
  192. status = GdipGetImageDecoders(numDecoders, size, pCodecs);
  193. if (status != Status.OK)
  194. throw statusException(status);
  195. auto codecs = new ImageCodecInfo[numDecoders];
  196. for (int i = 0; i < numDecoders; i++) {
  197. with (codecs[i] = new ImageCodecInfo) {
  198. with (pCodecs[i]) {
  199. clsid = Clsid;
  200. formatId = FormatID;
  201. codecName = to!string(toArray(CodecName));
  202. if (DllName) dllName = to!string(toArray(DllName));
  203. if (FormatDescription) formatDescription = to!string(toArray(FormatDescription));
  204. if (FilenameExtension) filenameExtension = to!string(toArray(FilenameExtension));
  205. if (MimeType) mimeType = to!string(toArray(MimeType));
  206. flags = cast(ImageCodecFlags)Flags;
  207. signaturePatterns.length = SigCount;
  208. signatureMasks.length = SigCount;
  209. for (int j = 0; j < SigCount; j++) {
  210. signaturePatterns[j].length = SigSize;
  211. signatureMasks[j].length = SigSize;
  212. core.stdc.string.memcpy(signaturePatterns[j].ptr, SigPattern + (j * SigSize), SigSize);
  213. core.stdc.string.memcpy(signatureMasks[j].ptr, SigMask + (j * SigSize), SigSize);
  214. }
  215. }
  216. }
  217. }
  218. LocalFree(cast(Handle)pCodecs);
  219. return codecs;
  220. }
  221. }
  222. /**
  223. */
  224. final class Encoder {
  225. private static Encoder compression_;
  226. private static Encoder colorDepth_;
  227. private static Encoder scanMethod_;
  228. private static Encoder version_;
  229. private static Encoder renderMethod_;
  230. private static Encoder quality_;
  231. private static Encoder transformation_;
  232. private static Encoder luminanceTable_;
  233. private static Encoder chrominanceTable_;
  234. private static Encoder saveFlag_;
  235. private Guid guid_;
  236. /**
  237. */
  238. this(Guid guid) {
  239. guid_ = guid;
  240. }
  241. /**
  242. */
  243. @property Guid guid() {
  244. return guid_;
  245. }
  246. /**
  247. */
  248. static @property Encoder compression() {
  249. if (compression_ is null)
  250. compression_ = new Encoder(Guid(0xe09d739d, 0xccd4, 0x44ee, 0x8e, 0xba, 0x3f, 0xbf, 0x8b, 0xe4, 0xfc, 0x58));
  251. return compression_;
  252. }
  253. /**
  254. */
  255. static @property Encoder colorDepth() {
  256. if (colorDepth_ is null)
  257. colorDepth_ = new Encoder(Guid(0x66087055, 0xad66, 0x4c7c, 0x9a, 0x18, 0x38, 0xa2, 0x31, 0x0b, 0x83, 0x37));
  258. return colorDepth_;
  259. }
  260. /**
  261. */
  262. static @property Encoder scanMethod() {
  263. if (scanMethod_ is null)
  264. scanMethod_ = new Encoder(Guid(0x3a4e2661, 0x3109, 0x4e56, 0x85, 0x36, 0x42, 0xc1, 0x56, 0xe7, 0xdc, 0xfa));
  265. return scanMethod_;
  266. }
  267. /**
  268. */
  269. static @property Encoder _version() {
  270. if (version_ is null)
  271. version_ = new Encoder(Guid(0x24d18c76, 0x814a, 0x41a4, 0xbf, 0x53, 0x1c, 0x21, 0x9c, 0xcc, 0xf7, 0x97));
  272. return version_;
  273. }
  274. /**
  275. */
  276. static @property Encoder renderMethod() {
  277. if (renderMethod_ is null)
  278. renderMethod_ = new Encoder(Guid(0x6d42c53a, 0x229a, 0x4825, 0x8b, 0xb7, 0x5c, 0x99, 0xe2, 0xb9, 0xa8, 0xb8));
  279. return renderMethod_;
  280. }
  281. /**
  282. */
  283. static @property Encoder quality() {
  284. if (quality_ is null)
  285. quality_ = new Encoder(Guid(0x1d5be4b5, 0xfa4a, 0x452d, 0x9c, 0xdd, 0x5d, 0xb3, 0x51, 0x05, 0xe7, 0xeb));
  286. return quality_;
  287. }
  288. /**
  289. */
  290. static @property Encoder transformation() {
  291. if (transformation_ is null)
  292. transformation_ = new Encoder(Guid(0x8d0eb2d1, 0xa58e, 0x4ea8, 0xaa, 0x14, 0x10, 0x80, 0x74, 0xb7, 0xb6, 0xf9));
  293. return transformation_;
  294. }
  295. /**
  296. */
  297. static @property Encoder luminanceTable() {
  298. if (luminanceTable_ is null)
  299. luminanceTable_ = new Encoder(Guid(0xedb33bce, 0x0266, 0x4a77, 0xb9, 0x04, 0x27, 0x21, 0x60, 0x99, 0xe7, 0x17));
  300. return luminanceTable_;
  301. }
  302. /**
  303. */
  304. static @property Encoder chrominanceTable() {
  305. if (chrominanceTable_ is null)
  306. chrominanceTable_ = new Encoder(Guid(0xf2e455dc, 0x09b3, 0x4316, 0x82, 0x60, 0x67, 0x6a, 0xda, 0x32, 0x48, 0x1c));
  307. return chrominanceTable_;
  308. }
  309. /**
  310. */
  311. static @property Encoder saveFlag() {
  312. if (saveFlag_ is null)
  313. saveFlag_ = new Encoder(Guid(0x292266fc, 0xac40, 0x47bf, 0x8c, 0xfc, 0xa8, 0x5b, 0x89, 0xa6, 0x55, 0xde));
  314. return saveFlag_;
  315. }
  316. }
  317. /**
  318. */
  319. final class EncoderParameter {
  320. private GUID guid_;
  321. private int numberOfValues_;
  322. private int type_;
  323. private void* value_;
  324. /**
  325. */
  326. this(Encoder encoder, int numberOfValues, int type, void* value) {
  327. guid_ = encoder.guid;
  328. numberOfValues_ = numberOfValues;
  329. type_ = type;
  330. value_ = value;
  331. }
  332. ~this() {
  333. dispose();
  334. }
  335. /**
  336. */
  337. void dispose() {
  338. if (value_ != null)
  339. value_ = null;
  340. }
  341. /**
  342. */
  343. @property void encoder(Encoder value) {
  344. guid_ = value.guid;
  345. }
  346. /**
  347. * ditto
  348. */
  349. @property Encoder encoder() {
  350. return new Encoder(guid_);
  351. }
  352. /**
  353. */
  354. @property int numberOfValues() {
  355. return numberOfValues_;
  356. }
  357. /**
  358. */
  359. @property EncoderParameterValueType type() {
  360. return cast(EncoderParameterValueType)type_;
  361. }
  362. }
  363. /**
  364. */
  365. final class EncoderParameters {
  366. /**
  367. */
  368. EncoderParameter[] param;
  369. /**
  370. */
  371. this(int count = 1) {
  372. param.length = count;
  373. }
  374. package this(GpEncoderParameters* p) {
  375. param.length = p.Count;
  376. for (size_t i = 0; i < p.Count; i++) {
  377. param[i] = new EncoderParameter(new Encoder(p.Parameter[i].Guid), p.Parameter[i].NumberOfValues, p.Parameter[i].Type, p.Parameter[i].Value);
  378. }
  379. }
  380. package GpEncoderParameters* forGDIplus() {
  381. GpEncoderParameters* p = cast(GpEncoderParameters*)LocalAlloc(LMEM_FIXED, GpEncoderParameters.sizeof);
  382. p.Count = cast(uint)param.length;
  383. for (size_t i = 0; i < param.length; i++) {
  384. p.Parameter[i].Guid = param[i].guid_;
  385. p.Parameter[i].NumberOfValues = param[i].numberOfValues_;
  386. p.Parameter[i].Type = param[i].type_;
  387. p.Parameter[i].Value = param[i].value_;
  388. }
  389. return p;
  390. }
  391. }
  392. final class FrameDimension {
  393. private static FrameDimension time_;
  394. private static FrameDimension resolution_;
  395. private static FrameDimension page_;
  396. private Guid guid_;
  397. /**
  398. */
  399. this(Guid guid) {
  400. guid_ = guid;
  401. }
  402. /**
  403. */
  404. @property Guid guid() {
  405. return guid_;
  406. }
  407. /**
  408. */
  409. static @property FrameDimension time() {
  410. if (time_ is null)
  411. time_ = new FrameDimension(Guid(0x6aedbd6d, 0x3fb5, 0x418a, 0x83, 0xa6, 0x7f, 0x45, 0x22, 0x9d, 0xc8, 0x72));
  412. return time_;
  413. }
  414. /**
  415. */
  416. static @property FrameDimension resolution() {
  417. if (resolution_ is null)
  418. resolution_ = new FrameDimension(Guid(0x84236f7b, 0x3bd3, 0x428f, 0x8d, 0xab, 0x4e, 0xa1, 0x43, 0x9c, 0xa3, 0x15));
  419. return resolution_;
  420. }
  421. /**
  422. */
  423. static @property FrameDimension page() {
  424. if (page_ is null)
  425. page_ = new FrameDimension(Guid(0x7462dc86, 0x6180, 0x4c7e, 0x8e, 0x3f, 0xee, 0x73, 0x33, 0xa7, 0xa4, 0x83));
  426. return page_;
  427. }
  428. }
  429. /**
  430. */
  431. final class ColorMatrix {
  432. package float[][] matrix_;
  433. /**
  434. */
  435. this() {
  436. matrix_ = [
  437. [ 1f, 0f, 0f, 0f, 0f ],
  438. [ 0f, 1f, 0f, 0f, 0f ],
  439. [ 0f, 0f, 1f, 0f, 0f ],
  440. [ 0f, 0f, 0f, 1f, 0f ],
  441. [ 0f, 0f, 0f, 0f, 0f ]
  442. ];
  443. }
  444. /**
  445. */
  446. this(float[][] newColorMatrix) {
  447. matrix_ = newColorMatrix;
  448. }
  449. /**
  450. */
  451. void opIndexAssign(float value, int row, int column) {
  452. matrix_[row][column] = value;
  453. }
  454. /**
  455. * ditto
  456. */
  457. float opIndex(int row, int column) {
  458. return matrix_[row][column];
  459. }
  460. }