EncoderParameter.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. public sealed class EncoderParameter : IDisposable {
  37. private Encoder encoder;
  38. private int valuesCount;
  39. private EncoderParameterValueType type;
  40. private IntPtr valuePtr;
  41. internal EncoderParameter ()
  42. {
  43. }
  44. public EncoderParameter (Encoder encoder, byte value)
  45. {
  46. this.encoder = encoder;
  47. this.valuesCount = 1;
  48. this.type = EncoderParameterValueType.ValueTypeByte;
  49. this.valuePtr = Marshal.AllocHGlobal (1);
  50. Marshal.WriteByte (this.valuePtr, value);
  51. }
  52. public EncoderParameter (Encoder encoder, byte[] value)
  53. {
  54. this.encoder = encoder;
  55. this.valuesCount = value.Length;
  56. this.type = EncoderParameterValueType.ValueTypeByte;
  57. this.valuePtr = Marshal.AllocHGlobal (1 * valuesCount);
  58. Marshal.Copy (value, 0, this.valuePtr, valuesCount);
  59. }
  60. public EncoderParameter (Encoder encoder, short value)
  61. {
  62. this.encoder = encoder;
  63. this.valuesCount = 1;
  64. this.type = EncoderParameterValueType.ValueTypeShort;
  65. this.valuePtr = Marshal.AllocHGlobal (2);
  66. Marshal.WriteInt16 (this.valuePtr, value);
  67. }
  68. public EncoderParameter (Encoder encoder, short[] value)
  69. {
  70. this.encoder = encoder;
  71. this.valuesCount = value.Length;
  72. this.type = EncoderParameterValueType.ValueTypeShort;
  73. this.valuePtr = Marshal.AllocHGlobal (2 * valuesCount);
  74. Marshal.Copy (value, 0, this.valuePtr, valuesCount);
  75. }
  76. public EncoderParameter (Encoder encoder, long value)
  77. {
  78. this.encoder = encoder;
  79. this.valuesCount = 1;
  80. this.type = EncoderParameterValueType.ValueTypeLong;
  81. this.valuePtr = Marshal.AllocHGlobal (4);
  82. Marshal.WriteInt32 (this.valuePtr, (int) value);
  83. }
  84. public EncoderParameter (Encoder encoder, long[] value)
  85. {
  86. this.encoder = encoder;
  87. this.valuesCount = value.Length;
  88. this.type = EncoderParameterValueType.ValueTypeLong;
  89. this.valuePtr = Marshal.AllocHGlobal (4 * valuesCount);
  90. int [] ivals = new int[value.Length];
  91. for (int i = 0; i < value.Length; i++) ivals[i] = (int) value[i];
  92. Marshal.Copy (ivals, 0, this.valuePtr, valuesCount);
  93. }
  94. public EncoderParameter (Encoder encoder, string value)
  95. {
  96. this.encoder = encoder;
  97. ASCIIEncoding ascii = new ASCIIEncoding ();
  98. int asciiByteCount = ascii.GetByteCount (value);
  99. byte[] bytes = new byte [asciiByteCount];
  100. ascii.GetBytes (value, 0, value.Length, bytes, 0);
  101. this.valuesCount = bytes.Length;
  102. this.type = EncoderParameterValueType.ValueTypeAscii;
  103. this.valuePtr = Marshal.AllocHGlobal (valuesCount);
  104. Marshal.Copy (bytes, 0, this.valuePtr, valuesCount);
  105. }
  106. public EncoderParameter (Encoder encoder, byte value, bool undefined)
  107. {
  108. this.encoder = encoder;
  109. this.valuesCount = 1;
  110. if (undefined)
  111. this.type = EncoderParameterValueType.ValueTypeUndefined;
  112. else
  113. this.type = EncoderParameterValueType.ValueTypeByte;
  114. this.valuePtr = Marshal.AllocHGlobal (1);
  115. Marshal.WriteByte (this.valuePtr, value);
  116. }
  117. public EncoderParameter (Encoder encoder, byte[] value, bool undefined)
  118. {
  119. this.encoder = encoder;
  120. this.valuesCount = value.Length;
  121. if (undefined)
  122. this.type = EncoderParameterValueType.ValueTypeUndefined;
  123. else
  124. this.type = EncoderParameterValueType.ValueTypeByte;
  125. this.valuePtr = Marshal.AllocHGlobal (valuesCount);
  126. Marshal.Copy (value, 0, this.valuePtr, valuesCount);
  127. }
  128. public EncoderParameter (Encoder encoder, int numerator, int denominator)
  129. {
  130. this.encoder = encoder;
  131. this.valuesCount = 1;
  132. this.type = EncoderParameterValueType.ValueTypeRational;
  133. this.valuePtr = Marshal.AllocHGlobal (8);
  134. int [] valuearray = { numerator, denominator };
  135. Marshal.Copy (valuearray, 0, this.valuePtr, valuearray.Length);
  136. }
  137. public EncoderParameter (Encoder encoder, int[] numerator, int[] denominator)
  138. {
  139. if (numerator.Length != denominator.Length)
  140. throw new ArgumentException ("Invalid parameter used.");
  141. this.encoder = encoder;
  142. this.valuesCount = numerator.Length;
  143. this.type = EncoderParameterValueType.ValueTypeRational;
  144. this.valuePtr = Marshal.AllocHGlobal (4 * valuesCount * 2);
  145. IntPtr dest = this.valuePtr;
  146. for (int i = 0; i < valuesCount; i++) {
  147. Marshal.WriteInt32 (dest, (int) numerator[i]);
  148. dest = (IntPtr) ((int) dest + 4);
  149. Marshal.WriteInt32 (dest, (int) denominator[i]);
  150. dest = (IntPtr) ((int) dest + 4);
  151. }
  152. }
  153. public EncoderParameter (Encoder encoder, long rangebegin, long rangeend)
  154. {
  155. this.encoder = encoder;
  156. this.valuesCount = 1;
  157. this.type = EncoderParameterValueType.ValueTypeLongRange;
  158. this.valuePtr = Marshal.AllocHGlobal (8);
  159. int [] valuearray = { (int) rangebegin, (int) rangeend };
  160. Marshal.Copy (valuearray, 0, this.valuePtr, valuearray.Length);
  161. }
  162. public EncoderParameter (Encoder encoder, long[] rangebegin, long[] rangeend)
  163. {
  164. if (rangebegin.Length != rangeend.Length)
  165. throw new ArgumentException ("Invalid parameter used.");
  166. this.encoder = encoder;
  167. this.valuesCount = rangebegin.Length;
  168. this.type = EncoderParameterValueType.ValueTypeLongRange;
  169. this.valuePtr = Marshal.AllocHGlobal (4 * valuesCount * 2);
  170. IntPtr dest = this.valuePtr;
  171. for (int i = 0; i < valuesCount; i++) {
  172. Marshal.WriteInt32 (dest, (int) rangebegin[i]);
  173. dest = (IntPtr) ((int) dest + 4);
  174. Marshal.WriteInt32 (dest, (int) rangeend[i]);
  175. dest = (IntPtr) ((int) dest + 4);
  176. }
  177. }
  178. public EncoderParameter (Encoder encoder, int numberOfValues, int type, int value)
  179. {
  180. this.encoder = encoder;
  181. this.valuePtr = (IntPtr) value;
  182. this.valuesCount = numberOfValues;
  183. this.type = (EncoderParameterValueType) type;
  184. }
  185. public EncoderParameter (Encoder encoder, int numerator1, int denominator1, int numerator2, int denominator2)
  186. {
  187. this.encoder = encoder;
  188. this.valuesCount = 1;
  189. this.type = EncoderParameterValueType.ValueTypeRationalRange;
  190. this.valuePtr = Marshal.AllocHGlobal (4 * 4);
  191. int [] valuearray = { numerator1, denominator1, numerator2, denominator2 };
  192. Marshal.Copy (valuearray, 0, this.valuePtr, 4);
  193. }
  194. public EncoderParameter (Encoder encoder, int[] numerator1, int[] denominator1, int[] numerator2, int[] denominator2)
  195. {
  196. if (numerator1.Length != denominator1.Length ||
  197. numerator2.Length != denominator2.Length ||
  198. numerator1.Length != numerator2.Length)
  199. throw new ArgumentException ("Invalid parameter used.");
  200. this.encoder = encoder;
  201. this.valuesCount = numerator1.Length;
  202. this.type = EncoderParameterValueType.ValueTypeRationalRange;
  203. this.valuePtr = Marshal.AllocHGlobal (4 * valuesCount * 4);
  204. IntPtr dest = this.valuePtr;
  205. for (int i = 0; i < valuesCount; i++) {
  206. Marshal.WriteInt32 (dest, numerator1[i]);
  207. dest = (IntPtr) ((int) dest + 4);
  208. Marshal.WriteInt32 (dest, denominator1[i]);
  209. dest = (IntPtr) ((int) dest + 4);
  210. Marshal.WriteInt32 (dest, numerator2[i]);
  211. dest = (IntPtr) ((int) dest + 4);
  212. Marshal.WriteInt32 (dest, denominator2[i]);
  213. dest = (IntPtr) ((int) dest + 4);
  214. }
  215. }
  216. public Encoder Encoder {
  217. get {
  218. return encoder;
  219. }
  220. set {
  221. encoder = value;
  222. }
  223. }
  224. public int NumberOfValues {
  225. get {
  226. return valuesCount;
  227. }
  228. }
  229. public EncoderParameterValueType Type {
  230. get {
  231. return type;
  232. }
  233. }
  234. public EncoderParameterValueType ValueType {
  235. get {
  236. return type;
  237. }
  238. }
  239. void Dispose (bool disposing) {
  240. if (valuePtr != IntPtr.Zero) {
  241. Marshal.FreeHGlobal (valuePtr);
  242. valuePtr = IntPtr.Zero;
  243. }
  244. }
  245. public void Dispose () {
  246. Dispose (true);
  247. }
  248. ~EncoderParameter () {
  249. Dispose (false);
  250. }
  251. internal static int NativeSize () {
  252. return Marshal.SizeOf (typeof(GdipEncoderParameter));
  253. }
  254. internal void ToNativePtr (IntPtr epPtr) {
  255. GdipEncoderParameter ep = new GdipEncoderParameter ();
  256. ep.guid = this.encoder.Guid;
  257. ep.numberOfValues = (uint) this.valuesCount;
  258. ep.type = this.type;
  259. ep.value = this.valuePtr;
  260. Marshal.StructureToPtr (ep, epPtr, false);
  261. }
  262. internal static EncoderParameter FromNativePtr (IntPtr epPtr) {
  263. GdipEncoderParameter ep;
  264. ep = (GdipEncoderParameter) Marshal.PtrToStructure (epPtr, typeof(GdipEncoderParameter));
  265. Type valType;
  266. uint valCount;
  267. switch (ep.type) {
  268. case EncoderParameterValueType.ValueTypeAscii:
  269. case EncoderParameterValueType.ValueTypeByte:
  270. case EncoderParameterValueType.ValueTypeUndefined:
  271. valType = typeof(byte);
  272. valCount = ep.numberOfValues;
  273. break;
  274. case EncoderParameterValueType.ValueTypeShort:
  275. valType = typeof(short);
  276. valCount = ep.numberOfValues;
  277. break;
  278. case EncoderParameterValueType.ValueTypeLong:
  279. valType = typeof(int);
  280. valCount = ep.numberOfValues;
  281. break;
  282. case EncoderParameterValueType.ValueTypeLongRange:
  283. case EncoderParameterValueType.ValueTypeRational:
  284. valType = typeof(int);
  285. valCount = ep.numberOfValues * 2;
  286. break;
  287. case EncoderParameterValueType.ValueTypeRationalRange:
  288. valType = typeof(int);
  289. valCount = ep.numberOfValues * 4;
  290. break;
  291. default:
  292. return null;
  293. }
  294. EncoderParameter eparam = new EncoderParameter();
  295. eparam.encoder = new Encoder(ep.guid);
  296. eparam.valuesCount = (int) ep.numberOfValues;
  297. eparam.type = ep.type;
  298. eparam.valuePtr = Marshal.AllocHGlobal ((int)(valCount * Marshal.SizeOf(valType)));
  299. /* There's nothing in Marshal to do a memcpy() between two IntPtrs. This sucks. */
  300. unsafe {
  301. byte *s = (byte *) ep.value;
  302. byte *d = (byte *) eparam.valuePtr;
  303. for (int i = 0; i < valCount * Marshal.SizeOf(valType); i++)
  304. *d++ = *s++;
  305. }
  306. return eparam;
  307. }
  308. }
  309. }