EncoderParameter.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. //
  2. // System.Drawing.Imaging.EncoderParameter.cs
  3. //
  4. // Author:
  5. // Ravindra ([email protected])
  6. // Vladimir Vukicevic ([email protected])
  7. //
  8. // (C) 2004 Novell, Inc. http://www.novell.com
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.Text;
  34. using System.Runtime.InteropServices;
  35. namespace System.Drawing.Imaging {
  36. [StructLayout(LayoutKind.Sequential)]
  37. public sealed class EncoderParameter : IDisposable {
  38. private Encoder encoder;
  39. private int valuesCount;
  40. private EncoderParameterValueType type;
  41. private IntPtr valuePtr;
  42. internal EncoderParameter ()
  43. {
  44. }
  45. public EncoderParameter (Encoder encoder, byte value)
  46. {
  47. this.encoder = encoder;
  48. this.valuesCount = 1;
  49. this.type = EncoderParameterValueType.ValueTypeByte;
  50. this.valuePtr = Marshal.AllocHGlobal (1);
  51. Marshal.WriteByte (this.valuePtr, value);
  52. }
  53. public EncoderParameter (Encoder encoder, byte[] value)
  54. {
  55. this.encoder = encoder;
  56. this.valuesCount = value.Length;
  57. this.type = EncoderParameterValueType.ValueTypeByte;
  58. this.valuePtr = Marshal.AllocHGlobal (1 * valuesCount);
  59. Marshal.Copy (value, 0, this.valuePtr, valuesCount);
  60. }
  61. public EncoderParameter (Encoder encoder, short value)
  62. {
  63. this.encoder = encoder;
  64. this.valuesCount = 1;
  65. this.type = EncoderParameterValueType.ValueTypeShort;
  66. this.valuePtr = Marshal.AllocHGlobal (2);
  67. Marshal.WriteInt16 (this.valuePtr, value);
  68. }
  69. public EncoderParameter (Encoder encoder, short[] value)
  70. {
  71. this.encoder = encoder;
  72. this.valuesCount = value.Length;
  73. this.type = EncoderParameterValueType.ValueTypeShort;
  74. this.valuePtr = Marshal.AllocHGlobal (2 * valuesCount);
  75. Marshal.Copy (value, 0, this.valuePtr, valuesCount);
  76. }
  77. public EncoderParameter (Encoder encoder, long value)
  78. {
  79. this.encoder = encoder;
  80. this.valuesCount = 1;
  81. this.type = EncoderParameterValueType.ValueTypeLong;
  82. this.valuePtr = Marshal.AllocHGlobal (4);
  83. Marshal.WriteInt32 (this.valuePtr, (int) value);
  84. }
  85. public EncoderParameter (Encoder encoder, long[] value)
  86. {
  87. this.encoder = encoder;
  88. this.valuesCount = value.Length;
  89. this.type = EncoderParameterValueType.ValueTypeLong;
  90. this.valuePtr = Marshal.AllocHGlobal (4 * valuesCount);
  91. int [] ivals = new int[value.Length];
  92. for (int i = 0; i < value.Length; i++) ivals[i] = (int) value[i];
  93. Marshal.Copy (ivals, 0, this.valuePtr, valuesCount);
  94. }
  95. public EncoderParameter (Encoder encoder, string value)
  96. {
  97. this.encoder = encoder;
  98. ASCIIEncoding ascii = new ASCIIEncoding ();
  99. int asciiByteCount = ascii.GetByteCount (value);
  100. byte[] bytes = new byte [asciiByteCount];
  101. ascii.GetBytes (value, 0, value.Length, bytes, 0);
  102. this.valuesCount = bytes.Length;
  103. this.type = EncoderParameterValueType.ValueTypeAscii;
  104. this.valuePtr = Marshal.AllocHGlobal (valuesCount);
  105. Marshal.Copy (bytes, 0, this.valuePtr, valuesCount);
  106. }
  107. public EncoderParameter (Encoder encoder, byte value, bool undefined)
  108. {
  109. this.encoder = encoder;
  110. this.valuesCount = 1;
  111. if (undefined)
  112. this.type = EncoderParameterValueType.ValueTypeUndefined;
  113. else
  114. this.type = EncoderParameterValueType.ValueTypeByte;
  115. this.valuePtr = Marshal.AllocHGlobal (1);
  116. Marshal.WriteByte (this.valuePtr, value);
  117. }
  118. public EncoderParameter (Encoder encoder, byte[] value, bool undefined)
  119. {
  120. this.encoder = encoder;
  121. this.valuesCount = value.Length;
  122. if (undefined)
  123. this.type = EncoderParameterValueType.ValueTypeUndefined;
  124. else
  125. this.type = EncoderParameterValueType.ValueTypeByte;
  126. this.valuePtr = Marshal.AllocHGlobal (valuesCount);
  127. Marshal.Copy (value, 0, this.valuePtr, valuesCount);
  128. }
  129. public EncoderParameter (Encoder encoder, int numerator, int denominator)
  130. {
  131. this.encoder = encoder;
  132. this.valuesCount = 1;
  133. this.type = EncoderParameterValueType.ValueTypeRational;
  134. this.valuePtr = Marshal.AllocHGlobal (8);
  135. int [] valuearray = { numerator, denominator };
  136. Marshal.Copy (valuearray, 0, this.valuePtr, valuearray.Length);
  137. }
  138. public EncoderParameter (Encoder encoder, int[] numerator, int[] denominator)
  139. {
  140. if (numerator.Length != denominator.Length)
  141. throw new ArgumentException ("Invalid parameter used.");
  142. this.encoder = encoder;
  143. this.valuesCount = numerator.Length;
  144. this.type = EncoderParameterValueType.ValueTypeRational;
  145. this.valuePtr = Marshal.AllocHGlobal (4 * valuesCount * 2);
  146. for (int i = 0; i < valuesCount; i++) {
  147. Marshal.WriteInt32 (valuePtr, i * 4, (int) numerator[i]);
  148. Marshal.WriteInt32 (valuePtr, (i + 1) * 4, (int) denominator[i]);
  149. }
  150. }
  151. public EncoderParameter (Encoder encoder, long rangebegin, long rangeend)
  152. {
  153. this.encoder = encoder;
  154. this.valuesCount = 1;
  155. this.type = EncoderParameterValueType.ValueTypeLongRange;
  156. this.valuePtr = Marshal.AllocHGlobal (8);
  157. int [] valuearray = { (int) rangebegin, (int) rangeend };
  158. Marshal.Copy (valuearray, 0, this.valuePtr, valuearray.Length);
  159. }
  160. public EncoderParameter (Encoder encoder, long[] rangebegin, long[] rangeend)
  161. {
  162. if (rangebegin.Length != rangeend.Length)
  163. throw new ArgumentException ("Invalid parameter used.");
  164. this.encoder = encoder;
  165. this.valuesCount = rangebegin.Length;
  166. this.type = EncoderParameterValueType.ValueTypeLongRange;
  167. this.valuePtr = Marshal.AllocHGlobal (4 * valuesCount * 2);
  168. IntPtr dest = this.valuePtr;
  169. for (int i = 0; i < valuesCount; i++) {
  170. Marshal.WriteInt32 (dest, i * 4, (int) rangebegin[i]);
  171. Marshal.WriteInt32 (dest, (i + 1) * 4, (int) rangeend[i]);
  172. }
  173. }
  174. public EncoderParameter (Encoder encoder, int numberOfValues, int type, int value)
  175. {
  176. this.encoder = encoder;
  177. this.valuePtr = (IntPtr) value;
  178. this.valuesCount = numberOfValues;
  179. this.type = (EncoderParameterValueType) type;
  180. }
  181. public EncoderParameter (Encoder encoder, int numerator1, int denominator1, int numerator2, int denominator2)
  182. {
  183. this.encoder = encoder;
  184. this.valuesCount = 1;
  185. this.type = EncoderParameterValueType.ValueTypeRationalRange;
  186. this.valuePtr = Marshal.AllocHGlobal (4 * 4);
  187. int [] valuearray = { numerator1, denominator1, numerator2, denominator2 };
  188. Marshal.Copy (valuearray, 0, this.valuePtr, 4);
  189. }
  190. public EncoderParameter (Encoder encoder, int[] numerator1, int[] denominator1, int[] numerator2, int[] denominator2)
  191. {
  192. if (numerator1.Length != denominator1.Length ||
  193. numerator2.Length != denominator2.Length ||
  194. numerator1.Length != numerator2.Length)
  195. throw new ArgumentException ("Invalid parameter used.");
  196. this.encoder = encoder;
  197. this.valuesCount = numerator1.Length;
  198. this.type = EncoderParameterValueType.ValueTypeRationalRange;
  199. this.valuePtr = Marshal.AllocHGlobal (4 * valuesCount * 4);
  200. IntPtr dest = this.valuePtr;
  201. for (int i = 0; i < valuesCount; i++) {
  202. Marshal.WriteInt32 (dest, i * 4, numerator1[i]);
  203. Marshal.WriteInt32 (dest, (i + 1) * 4, denominator1[i]);
  204. Marshal.WriteInt32 (dest, (i + 2) * 4, numerator2[i]);
  205. Marshal.WriteInt32 (dest, (i + 3) * 4, denominator2[i]);
  206. }
  207. }
  208. public Encoder Encoder {
  209. get {
  210. return encoder;
  211. }
  212. set {
  213. encoder = value;
  214. }
  215. }
  216. public int NumberOfValues {
  217. get {
  218. return valuesCount;
  219. }
  220. }
  221. public EncoderParameterValueType Type {
  222. get {
  223. return type;
  224. }
  225. }
  226. public EncoderParameterValueType ValueType {
  227. get {
  228. return type;
  229. }
  230. }
  231. void Dispose (bool disposing) {
  232. if (valuePtr != IntPtr.Zero) {
  233. Marshal.FreeHGlobal (valuePtr);
  234. valuePtr = IntPtr.Zero;
  235. }
  236. }
  237. public void Dispose () {
  238. Dispose (true);
  239. GC.SuppressFinalize(this);
  240. }
  241. ~EncoderParameter () {
  242. Dispose (false);
  243. }
  244. internal static int NativeSize () {
  245. return Marshal.SizeOf (typeof(GdipEncoderParameter));
  246. }
  247. internal void ToNativePtr (IntPtr epPtr) {
  248. GdipEncoderParameter ep = new GdipEncoderParameter ();
  249. ep.guid = this.encoder.Guid;
  250. ep.numberOfValues = (uint) this.valuesCount;
  251. ep.type = this.type;
  252. ep.value = this.valuePtr;
  253. Marshal.StructureToPtr (ep, epPtr, false);
  254. }
  255. internal static EncoderParameter FromNativePtr (IntPtr epPtr) {
  256. GdipEncoderParameter ep;
  257. ep = (GdipEncoderParameter) Marshal.PtrToStructure (epPtr, typeof(GdipEncoderParameter));
  258. Type valType;
  259. uint valCount;
  260. switch (ep.type) {
  261. case EncoderParameterValueType.ValueTypeAscii:
  262. case EncoderParameterValueType.ValueTypeByte:
  263. case EncoderParameterValueType.ValueTypeUndefined:
  264. valType = typeof(byte);
  265. valCount = ep.numberOfValues;
  266. break;
  267. case EncoderParameterValueType.ValueTypeShort:
  268. valType = typeof(short);
  269. valCount = ep.numberOfValues;
  270. break;
  271. case EncoderParameterValueType.ValueTypeLong:
  272. valType = typeof(int);
  273. valCount = ep.numberOfValues;
  274. break;
  275. case EncoderParameterValueType.ValueTypeLongRange:
  276. case EncoderParameterValueType.ValueTypeRational:
  277. valType = typeof(int);
  278. valCount = ep.numberOfValues * 2;
  279. break;
  280. case EncoderParameterValueType.ValueTypeRationalRange:
  281. valType = typeof(int);
  282. valCount = ep.numberOfValues * 4;
  283. break;
  284. default:
  285. return null;
  286. }
  287. EncoderParameter eparam = new EncoderParameter();
  288. eparam.encoder = new Encoder(ep.guid);
  289. eparam.valuesCount = (int) ep.numberOfValues;
  290. eparam.type = ep.type;
  291. eparam.valuePtr = Marshal.AllocHGlobal ((int)(valCount * Marshal.SizeOf(valType)));
  292. /* There's nothing in Marshal to do a memcpy() between two IntPtrs. This sucks. */
  293. unsafe {
  294. byte *s = (byte *) ep.value;
  295. byte *d = (byte *) eparam.valuePtr;
  296. for (int i = 0; i < valCount * Marshal.SizeOf(valType); i++)
  297. *d++ = *s++;
  298. }
  299. return eparam;
  300. }
  301. }
  302. }