2
0

EncoderParameter.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // System.Drawing.Imaging.EncoderParameter.cs
  3. //
  4. // Author:
  5. // Ravindra ([email protected])
  6. //
  7. // (C) 2004 Novell, Inc. http://www.novell.com
  8. //
  9. using System;
  10. using System.Text;
  11. namespace System.Drawing.Imaging {
  12. public sealed class EncoderParameter : IDisposable {
  13. Encoder encoder;
  14. Object value;
  15. int valuesCount;
  16. EncoderParameterValueType type;
  17. public EncoderParameter (Encoder encoder, byte value)
  18. {
  19. this.encoder = encoder;
  20. this.value = value;
  21. this.valuesCount = 1;
  22. this.type = EncoderParameterValueType.ValueTypeByte;
  23. }
  24. public EncoderParameter (Encoder encoder, byte[] value)
  25. {
  26. this.encoder = encoder;
  27. this.value = value;
  28. this.valuesCount = value.Length;
  29. this.type = EncoderParameterValueType.ValueTypeByte;
  30. }
  31. public EncoderParameter (Encoder encoder, short value)
  32. {
  33. this.encoder = encoder;
  34. this.value = value;
  35. this.valuesCount = 1;
  36. this.type = EncoderParameterValueType.ValueTypeShort;
  37. }
  38. public EncoderParameter (Encoder encoder, short[] value)
  39. {
  40. this.encoder = encoder;
  41. this.value = value;
  42. this.valuesCount = value.Length;
  43. this.type = EncoderParameterValueType.ValueTypeShort;
  44. }
  45. public EncoderParameter (Encoder encoder, long value)
  46. {
  47. this.encoder = encoder;
  48. this.value = (int) value;
  49. this.valuesCount = 1;
  50. this.type = EncoderParameterValueType.ValueTypeLong;
  51. }
  52. public EncoderParameter (Encoder encoder, long[] value)
  53. {
  54. this.encoder = encoder;
  55. this.value = convertToIntArr (value);
  56. this.valuesCount = value.Length;
  57. this.type = EncoderParameterValueType.ValueTypeLong;
  58. }
  59. public EncoderParameter (Encoder encoder, string value)
  60. {
  61. this.encoder = encoder;
  62. ASCIIEncoding ascii = new ASCIIEncoding ();
  63. int asciiByteCount = ascii.GetByteCount (value);
  64. byte[] bytes = new byte [asciiByteCount];
  65. ascii.GetBytes (value, 0, value.Length, bytes, 0);
  66. this.value = ascii.GetString (bytes);
  67. this.valuesCount = bytes.Length;
  68. this.type = EncoderParameterValueType.ValueTypeAscii;
  69. }
  70. public EncoderParameter (Encoder encoder, byte value, bool undefined)
  71. {
  72. this.encoder = encoder;
  73. this.value = value;
  74. this.valuesCount = 1;
  75. if (undefined)
  76. this.type = EncoderParameterValueType.ValueTypeUndefined;
  77. else
  78. this.type = EncoderParameterValueType.ValueTypeByte;
  79. }
  80. public EncoderParameter (Encoder encoder, byte[] value, bool undefined)
  81. {
  82. this.encoder = encoder;
  83. this.value = value;
  84. this.valuesCount = value.Length;
  85. if (undefined)
  86. this.type = EncoderParameterValueType.ValueTypeUndefined;
  87. else
  88. this.type = EncoderParameterValueType.ValueTypeByte;
  89. }
  90. public EncoderParameter (Encoder encoder, int numerator, int denominator)
  91. {
  92. this.encoder = encoder;
  93. this.value = new int[] {numerator, denominator};
  94. this.valuesCount = 1;
  95. this.type = EncoderParameterValueType.ValueTypeRational;
  96. }
  97. public EncoderParameter (Encoder encoder, int[] numerator, int[] denominator)
  98. {
  99. if (numerator.Length != denominator.Length)
  100. throw new ArgumentException ("Invalid parameter used.");
  101. this.encoder = encoder;
  102. this.value = new int[][] {numerator, denominator};
  103. this.valuesCount = numerator.Length;
  104. this.type = EncoderParameterValueType.ValueTypeRational;
  105. }
  106. public EncoderParameter (Encoder encoder, long rangebegin, long rangeend)
  107. {
  108. this.encoder = encoder;
  109. this.value = new int[] { (int) rangebegin, (int) rangeend};
  110. this.valuesCount = 1;
  111. this.type = EncoderParameterValueType.ValueTypeLongRange;
  112. }
  113. public EncoderParameter (Encoder encoder, long[] rangebegin, long[] rangeend)
  114. {
  115. if (rangebegin.Length != rangeend.Length)
  116. throw new ArgumentException ("Invalid parameter used.");
  117. this.encoder = encoder;
  118. int[] startRange = convertToIntArr (rangebegin);
  119. int[] endRange = convertToIntArr (rangeend);
  120. this.value = new int[][] {startRange, endRange};
  121. this.valuesCount = rangebegin.Length;
  122. this.type = EncoderParameterValueType.ValueTypeLongRange;
  123. }
  124. public EncoderParameter (Encoder encoder, int numberOfValues, int type, int value)
  125. {
  126. this.encoder = encoder;
  127. this.value = value;
  128. this.valuesCount = numberOfValues;
  129. this.type = (EncoderParameterValueType) type;
  130. }
  131. public EncoderParameter (Encoder encoder, int numerator1, int denominator1, int numerator2, int denominator2)
  132. {
  133. this.encoder = encoder;
  134. this.value = new int[] {numerator1, denominator1, numerator2, denominator2};
  135. this.valuesCount = 1;
  136. this.type = EncoderParameterValueType.ValueTypeRationalRange;
  137. }
  138. public EncoderParameter (Encoder encoder, int[] numerator1, int[] denominator1, int[] numerator2, int[] denominator2)
  139. {
  140. if (numerator1.Length != denominator1.Length ||
  141. numerator2.Length != denominator2.Length ||
  142. numerator1.Length != numerator2.Length)
  143. throw new ArgumentException ("Invalid parameter used.");
  144. this.encoder = encoder;
  145. this.value = new int[][] {numerator1, denominator1, numerator2, denominator2};
  146. this.valuesCount = numerator1.Length;
  147. this.type = EncoderParameterValueType.ValueTypeRationalRange;
  148. }
  149. public Encoder Encoder {
  150. get {
  151. return encoder;
  152. }
  153. set {
  154. encoder = value;
  155. }
  156. }
  157. public int NumberOfValues {
  158. get {
  159. return valuesCount;
  160. }
  161. }
  162. public EncoderParameterValueType Type {
  163. get {
  164. return type;
  165. }
  166. }
  167. public EncoderParameterValueType ValueType {
  168. get {
  169. return type;
  170. }
  171. }
  172. void Dispose (bool disposing) {
  173. // release the resources
  174. }
  175. public void Dispose () {
  176. Dispose (true);
  177. }
  178. ~EncoderParameter () {
  179. Dispose (false);
  180. }
  181. internal Object Value {
  182. get {
  183. return value;
  184. }
  185. }
  186. internal int[] convertToIntArr (long[] arr)
  187. {
  188. int[] intArr = new int [arr.Length];
  189. for (int i = 0; i < arr.Length; i++)
  190. intArr[i] = (int) arr[i];
  191. return intArr;
  192. }
  193. }
  194. }