2
0

EncoderParameter.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. IntPtr dest = this.valuePtr;
  147. for (int i = 0; i < valuesCount; i++) {
  148. Marshal.WriteInt32 (dest, (int) numerator[i]);
  149. dest = (IntPtr) ((int) dest + 4);
  150. Marshal.WriteInt32 (dest, (int) denominator[i]);
  151. dest = (IntPtr) ((int) dest + 4);
  152. }
  153. }
  154. public EncoderParameter (Encoder encoder, long rangebegin, long rangeend)
  155. {
  156. this.encoder = encoder;
  157. this.valuesCount = 1;
  158. this.type = EncoderParameterValueType.ValueTypeLongRange;
  159. this.valuePtr = Marshal.AllocHGlobal (8);
  160. int [] valuearray = { (int) rangebegin, (int) rangeend };
  161. Marshal.Copy (valuearray, 0, this.valuePtr, valuearray.Length);
  162. }
  163. public EncoderParameter (Encoder encoder, long[] rangebegin, long[] rangeend)
  164. {
  165. if (rangebegin.Length != rangeend.Length)
  166. throw new ArgumentException ("Invalid parameter used.");
  167. this.encoder = encoder;
  168. this.valuesCount = rangebegin.Length;
  169. this.type = EncoderParameterValueType.ValueTypeLongRange;
  170. this.valuePtr = Marshal.AllocHGlobal (4 * valuesCount * 2);
  171. IntPtr dest = this.valuePtr;
  172. for (int i = 0; i < valuesCount; i++) {
  173. Marshal.WriteInt32 (dest, (int) rangebegin[i]);
  174. dest = (IntPtr) ((int) dest + 4);
  175. Marshal.WriteInt32 (dest, (int) rangeend[i]);
  176. dest = (IntPtr) ((int) dest + 4);
  177. }
  178. }
  179. public EncoderParameter (Encoder encoder, int numberOfValues, int type, int value)
  180. {
  181. this.encoder = encoder;
  182. this.valuePtr = (IntPtr) value;
  183. this.valuesCount = numberOfValues;
  184. this.type = (EncoderParameterValueType) type;
  185. }
  186. public EncoderParameter (Encoder encoder, int numerator1, int denominator1, int numerator2, int denominator2)
  187. {
  188. this.encoder = encoder;
  189. this.valuesCount = 1;
  190. this.type = EncoderParameterValueType.ValueTypeRationalRange;
  191. this.valuePtr = Marshal.AllocHGlobal (4 * 4);
  192. int [] valuearray = { numerator1, denominator1, numerator2, denominator2 };
  193. Marshal.Copy (valuearray, 0, this.valuePtr, 4);
  194. }
  195. public EncoderParameter (Encoder encoder, int[] numerator1, int[] denominator1, int[] numerator2, int[] denominator2)
  196. {
  197. if (numerator1.Length != denominator1.Length ||
  198. numerator2.Length != denominator2.Length ||
  199. numerator1.Length != numerator2.Length)
  200. throw new ArgumentException ("Invalid parameter used.");
  201. this.encoder = encoder;
  202. this.valuesCount = numerator1.Length;
  203. this.type = EncoderParameterValueType.ValueTypeRationalRange;
  204. this.valuePtr = Marshal.AllocHGlobal (4 * valuesCount * 4);
  205. IntPtr dest = this.valuePtr;
  206. for (int i = 0; i < valuesCount; i++) {
  207. Marshal.WriteInt32 (dest, numerator1[i]);
  208. dest = (IntPtr) ((int) dest + 4);
  209. Marshal.WriteInt32 (dest, denominator1[i]);
  210. dest = (IntPtr) ((int) dest + 4);
  211. Marshal.WriteInt32 (dest, numerator2[i]);
  212. dest = (IntPtr) ((int) dest + 4);
  213. Marshal.WriteInt32 (dest, denominator2[i]);
  214. dest = (IntPtr) ((int) dest + 4);
  215. }
  216. }
  217. public Encoder Encoder {
  218. get {
  219. return encoder;
  220. }
  221. set {
  222. encoder = value;
  223. }
  224. }
  225. public int NumberOfValues {
  226. get {
  227. return valuesCount;
  228. }
  229. }
  230. public EncoderParameterValueType Type {
  231. get {
  232. return type;
  233. }
  234. }
  235. public EncoderParameterValueType ValueType {
  236. get {
  237. return type;
  238. }
  239. }
  240. void Dispose (bool disposing) {
  241. if (valuePtr != IntPtr.Zero) {
  242. Marshal.FreeHGlobal (valuePtr);
  243. valuePtr = IntPtr.Zero;
  244. }
  245. }
  246. public void Dispose () {
  247. Dispose (true);
  248. GC.SuppressFinalize(this);
  249. }
  250. ~EncoderParameter () {
  251. Dispose (false);
  252. }
  253. internal static int NativeSize () {
  254. return Marshal.SizeOf (typeof(GdipEncoderParameter));
  255. }
  256. internal void ToNativePtr (IntPtr epPtr) {
  257. GdipEncoderParameter ep = new GdipEncoderParameter ();
  258. ep.guid = this.encoder.Guid;
  259. ep.numberOfValues = (uint) this.valuesCount;
  260. ep.type = this.type;
  261. ep.value = this.valuePtr;
  262. Marshal.StructureToPtr (ep, epPtr, false);
  263. }
  264. internal static EncoderParameter FromNativePtr (IntPtr epPtr) {
  265. GdipEncoderParameter ep;
  266. ep = (GdipEncoderParameter) Marshal.PtrToStructure (epPtr, typeof(GdipEncoderParameter));
  267. Type valType;
  268. uint valCount;
  269. switch (ep.type) {
  270. case EncoderParameterValueType.ValueTypeAscii:
  271. case EncoderParameterValueType.ValueTypeByte:
  272. case EncoderParameterValueType.ValueTypeUndefined:
  273. valType = typeof(byte);
  274. valCount = ep.numberOfValues;
  275. break;
  276. case EncoderParameterValueType.ValueTypeShort:
  277. valType = typeof(short);
  278. valCount = ep.numberOfValues;
  279. break;
  280. case EncoderParameterValueType.ValueTypeLong:
  281. valType = typeof(int);
  282. valCount = ep.numberOfValues;
  283. break;
  284. case EncoderParameterValueType.ValueTypeLongRange:
  285. case EncoderParameterValueType.ValueTypeRational:
  286. valType = typeof(int);
  287. valCount = ep.numberOfValues * 2;
  288. break;
  289. case EncoderParameterValueType.ValueTypeRationalRange:
  290. valType = typeof(int);
  291. valCount = ep.numberOfValues * 4;
  292. break;
  293. default:
  294. return null;
  295. }
  296. EncoderParameter eparam = new EncoderParameter();
  297. eparam.encoder = new Encoder(ep.guid);
  298. eparam.valuesCount = (int) ep.numberOfValues;
  299. eparam.type = ep.type;
  300. eparam.valuePtr = Marshal.AllocHGlobal ((int)(valCount * Marshal.SizeOf(valType)));
  301. /* There's nothing in Marshal to do a memcpy() between two IntPtrs. This sucks. */
  302. unsafe {
  303. byte *s = (byte *) ep.value;
  304. byte *d = (byte *) eparam.valuePtr;
  305. for (int i = 0; i < valCount * Marshal.SizeOf(valType); i++)
  306. *d++ = *s++;
  307. }
  308. return eparam;
  309. }
  310. }
  311. }