PngCodecTest.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. //
  2. // PNG Codec class testing unit
  3. //
  4. // Authors:
  5. // Jordi Mas i Hernàndez ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Drawing;
  31. using System.Drawing.Imaging;
  32. using System.IO;
  33. using System.Security.Permissions;
  34. using System.Text;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Drawing.Imaging {
  37. [TestFixture]
  38. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  39. public class PngCodecTest {
  40. /* Get suffix to add to the filename */
  41. internal string getOutSufix ()
  42. {
  43. string s;
  44. int p = (int) Environment.OSVersion.Platform;
  45. if ((p == 4) || (p == 128))
  46. s = "-unix";
  47. else
  48. s = "-windows";
  49. if (Type.GetType ("Mono.Runtime", false) == null)
  50. s += "-msnet";
  51. else
  52. s += "-mono";
  53. return s;
  54. }
  55. /* Get the input directory depending on the runtime*/
  56. internal string getInFile (string file)
  57. {
  58. string sRslt = Path.GetFullPath ("../System.Drawing/" + file);
  59. if (!File.Exists (sRslt))
  60. sRslt = "Test/System.Drawing/" + file;
  61. return sRslt;
  62. }
  63. /* Checks bitmap features on a know 1bbp bitmap */
  64. [Test]
  65. public void Bitmap1bitFeatures ()
  66. {
  67. string sInFile = getInFile ("bitmaps/1bit.png");
  68. using (Bitmap bmp = new Bitmap (sInFile)) {
  69. GraphicsUnit unit = GraphicsUnit.World;
  70. RectangleF rect = bmp.GetBounds (ref unit);
  71. // ??? why is it a 4bbp ?
  72. Assert.AreEqual (PixelFormat.Format1bppIndexed, bmp.PixelFormat);
  73. Assert.AreEqual (288, bmp.Width, "bmp.Width");
  74. Assert.AreEqual (384, bmp.Height, "bmp.Height");
  75. Assert.AreEqual (0, rect.X, "rect.X");
  76. Assert.AreEqual (0, rect.Y, "rect.Y");
  77. Assert.AreEqual (288, rect.Width, "rect.Width");
  78. Assert.AreEqual (384, rect.Height, "rect.Height");
  79. Assert.AreEqual (288, bmp.Size.Width, "bmp.Size.Width");
  80. Assert.AreEqual (384, bmp.Size.Height, "bmp.Size.Height");
  81. }
  82. }
  83. [Test]
  84. public void Bitmap1bitPixels ()
  85. {
  86. string sInFile = getInFile ("bitmaps/1bit.png");
  87. using (Bitmap bmp = new Bitmap (sInFile)) {
  88. #if false
  89. for (int x = 0; x < bmp.Width; x += 32) {
  90. for (int y = 0; y < bmp.Height; y += 32)
  91. Console.WriteLine ("\t\t\t\tAssert.AreEqual ({0}, bmp.GetPixel ({1}, {2}).ToArgb (), \"{1},{2}\");", bmp.GetPixel (x, y).ToArgb (), x, y);
  92. }
  93. #else
  94. // sampling values from a well known bitmap
  95. Assert.AreEqual (-1, bmp.GetPixel (0, 0).ToArgb (), "0,0");
  96. Assert.AreEqual (-1, bmp.GetPixel (0, 32).ToArgb (), "0,32");
  97. Assert.AreEqual (-1, bmp.GetPixel (0, 64).ToArgb (), "0,64");
  98. Assert.AreEqual (-1, bmp.GetPixel (0, 96).ToArgb (), "0,96");
  99. Assert.AreEqual (-1, bmp.GetPixel (0, 128).ToArgb (), "0,128");
  100. Assert.AreEqual (-16777216, bmp.GetPixel (0, 160).ToArgb (), "0,160");
  101. Assert.AreEqual (-16777216, bmp.GetPixel (0, 192).ToArgb (), "0,192");
  102. Assert.AreEqual (-16777216, bmp.GetPixel (0, 224).ToArgb (), "0,224");
  103. Assert.AreEqual (-1, bmp.GetPixel (0, 256).ToArgb (), "0,256");
  104. Assert.AreEqual (-16777216, bmp.GetPixel (0, 288).ToArgb (), "0,288");
  105. Assert.AreEqual (-1, bmp.GetPixel (0, 320).ToArgb (), "0,320");
  106. Assert.AreEqual (-16777216, bmp.GetPixel (0, 352).ToArgb (), "0,352");
  107. Assert.AreEqual (-16777216, bmp.GetPixel (32, 0).ToArgb (), "32,0");
  108. Assert.AreEqual (-1, bmp.GetPixel (32, 32).ToArgb (), "32,32");
  109. Assert.AreEqual (-1, bmp.GetPixel (32, 64).ToArgb (), "32,64");
  110. Assert.AreEqual (-16777216, bmp.GetPixel (32, 96).ToArgb (), "32,96");
  111. Assert.AreEqual (-1, bmp.GetPixel (32, 128).ToArgb (), "32,128");
  112. Assert.AreEqual (-1, bmp.GetPixel (32, 160).ToArgb (), "32,160");
  113. Assert.AreEqual (-16777216, bmp.GetPixel (32, 192).ToArgb (), "32,192");
  114. Assert.AreEqual (-1, bmp.GetPixel (32, 224).ToArgb (), "32,224");
  115. Assert.AreEqual (-16777216, bmp.GetPixel (32, 256).ToArgb (), "32,256");
  116. Assert.AreEqual (-16777216, bmp.GetPixel (32, 288).ToArgb (), "32,288");
  117. Assert.AreEqual (-1, bmp.GetPixel (32, 320).ToArgb (), "32,320");
  118. Assert.AreEqual (-1, bmp.GetPixel (32, 352).ToArgb (), "32,352");
  119. Assert.AreEqual (-16777216, bmp.GetPixel (64, 0).ToArgb (), "64,0");
  120. Assert.AreEqual (-16777216, bmp.GetPixel (64, 32).ToArgb (), "64,32");
  121. Assert.AreEqual (-16777216, bmp.GetPixel (64, 64).ToArgb (), "64,64");
  122. Assert.AreEqual (-16777216, bmp.GetPixel (64, 96).ToArgb (), "64,96");
  123. Assert.AreEqual (-16777216, bmp.GetPixel (64, 128).ToArgb (), "64,128");
  124. Assert.AreEqual (-16777216, bmp.GetPixel (64, 160).ToArgb (), "64,160");
  125. Assert.AreEqual (-16777216, bmp.GetPixel (64, 192).ToArgb (), "64,192");
  126. Assert.AreEqual (-16777216, bmp.GetPixel (64, 224).ToArgb (), "64,224");
  127. Assert.AreEqual (-16777216, bmp.GetPixel (64, 256).ToArgb (), "64,256");
  128. Assert.AreEqual (-16777216, bmp.GetPixel (64, 288).ToArgb (), "64,288");
  129. Assert.AreEqual (-16777216, bmp.GetPixel (64, 320).ToArgb (), "64,320");
  130. Assert.AreEqual (-16777216, bmp.GetPixel (64, 352).ToArgb (), "64,352");
  131. Assert.AreEqual (-1, bmp.GetPixel (96, 0).ToArgb (), "96,0");
  132. Assert.AreEqual (-16777216, bmp.GetPixel (96, 32).ToArgb (), "96,32");
  133. Assert.AreEqual (-16777216, bmp.GetPixel (96, 64).ToArgb (), "96,64");
  134. Assert.AreEqual (-16777216, bmp.GetPixel (96, 96).ToArgb (), "96,96");
  135. Assert.AreEqual (-16777216, bmp.GetPixel (96, 128).ToArgb (), "96,128");
  136. Assert.AreEqual (-16777216, bmp.GetPixel (96, 160).ToArgb (), "96,160");
  137. Assert.AreEqual (-1, bmp.GetPixel (96, 192).ToArgb (), "96,192");
  138. Assert.AreEqual (-16777216, bmp.GetPixel (96, 224).ToArgb (), "96,224");
  139. Assert.AreEqual (-16777216, bmp.GetPixel (96, 256).ToArgb (), "96,256");
  140. Assert.AreEqual (-16777216, bmp.GetPixel (96, 288).ToArgb (), "96,288");
  141. Assert.AreEqual (-1, bmp.GetPixel (96, 320).ToArgb (), "96,320");
  142. Assert.AreEqual (-1, bmp.GetPixel (96, 352).ToArgb (), "96,352");
  143. Assert.AreEqual (-16777216, bmp.GetPixel (128, 0).ToArgb (), "128,0");
  144. Assert.AreEqual (-16777216, bmp.GetPixel (128, 32).ToArgb (), "128,32");
  145. Assert.AreEqual (-16777216, bmp.GetPixel (128, 64).ToArgb (), "128,64");
  146. Assert.AreEqual (-16777216, bmp.GetPixel (128, 96).ToArgb (), "128,96");
  147. Assert.AreEqual (-16777216, bmp.GetPixel (128, 128).ToArgb (), "128,128");
  148. Assert.AreEqual (-16777216, bmp.GetPixel (128, 160).ToArgb (), "128,160");
  149. Assert.AreEqual (-16777216, bmp.GetPixel (128, 192).ToArgb (), "128,192");
  150. Assert.AreEqual (-16777216, bmp.GetPixel (128, 224).ToArgb (), "128,224");
  151. Assert.AreEqual (-1, bmp.GetPixel (128, 256).ToArgb (), "128,256");
  152. Assert.AreEqual (-16777216, bmp.GetPixel (128, 288).ToArgb (), "128,288");
  153. Assert.AreEqual (-16777216, bmp.GetPixel (128, 320).ToArgb (), "128,320");
  154. Assert.AreEqual (-16777216, bmp.GetPixel (128, 352).ToArgb (), "128,352");
  155. Assert.AreEqual (-1, bmp.GetPixel (160, 0).ToArgb (), "160,0");
  156. Assert.AreEqual (-1, bmp.GetPixel (160, 32).ToArgb (), "160,32");
  157. Assert.AreEqual (-16777216, bmp.GetPixel (160, 64).ToArgb (), "160,64");
  158. Assert.AreEqual (-1, bmp.GetPixel (160, 96).ToArgb (), "160,96");
  159. Assert.AreEqual (-16777216, bmp.GetPixel (160, 128).ToArgb (), "160,128");
  160. Assert.AreEqual (-1, bmp.GetPixel (160, 160).ToArgb (), "160,160");
  161. #endif
  162. }
  163. }
  164. [Test]
  165. [Category ("NotWorking")]
  166. public void Bitmat1bitData ()
  167. {
  168. string sInFile = getInFile ("bitmaps/1bit.png");
  169. using (Bitmap bmp = new Bitmap (sInFile)) {
  170. BitmapData data = bmp.LockBits (new Rectangle (0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
  171. try {
  172. Assert.AreEqual (bmp.Height, data.Height, "Height");
  173. Assert.AreEqual (bmp.Width, data.Width, "Width");
  174. Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
  175. int size = data.Height * data.Stride;
  176. unsafe {
  177. byte* scan = (byte*) data.Scan0;
  178. #if false
  179. // 1009 is the first prime after 1000 (so we're not affected by a recurring pattern)
  180. for (int p = 0; p < size; p += 1009) {
  181. Console.WriteLine ("\t\t\t\t\t\tAssert.AreEqual ({0}, *(scan + {1}), \"{1}\");", *(scan + p), p);
  182. }
  183. #else
  184. // sampling values from a well known bitmap
  185. Assert.AreEqual (255, *(scan + 0), "0");
  186. Assert.AreEqual (255, *(scan + 1009), "1009");
  187. Assert.AreEqual (255, *(scan + 2018), "2018");
  188. Assert.AreEqual (255, *(scan + 3027), "3027");
  189. Assert.AreEqual (255, *(scan + 4036), "4036");
  190. Assert.AreEqual (255, *(scan + 5045), "5045");
  191. Assert.AreEqual (255, *(scan + 6054), "6054");
  192. Assert.AreEqual (255, *(scan + 7063), "7063");
  193. Assert.AreEqual (255, *(scan + 8072), "8072");
  194. Assert.AreEqual (255, *(scan + 9081), "9081");
  195. Assert.AreEqual (255, *(scan + 10090), "10090");
  196. Assert.AreEqual (0, *(scan + 11099), "11099");
  197. Assert.AreEqual (255, *(scan + 12108), "12108");
  198. Assert.AreEqual (255, *(scan + 13117), "13117");
  199. Assert.AreEqual (0, *(scan + 14126), "14126");
  200. Assert.AreEqual (255, *(scan + 15135), "15135");
  201. Assert.AreEqual (255, *(scan + 16144), "16144");
  202. Assert.AreEqual (0, *(scan + 17153), "17153");
  203. Assert.AreEqual (0, *(scan + 18162), "18162");
  204. Assert.AreEqual (255, *(scan + 19171), "19171");
  205. Assert.AreEqual (0, *(scan + 20180), "20180");
  206. Assert.AreEqual (255, *(scan + 21189), "21189");
  207. Assert.AreEqual (255, *(scan + 22198), "22198");
  208. Assert.AreEqual (0, *(scan + 23207), "23207");
  209. Assert.AreEqual (0, *(scan + 24216), "24216");
  210. Assert.AreEqual (0, *(scan + 25225), "25225");
  211. Assert.AreEqual (0, *(scan + 26234), "26234");
  212. Assert.AreEqual (255, *(scan + 27243), "27243");
  213. Assert.AreEqual (255, *(scan + 28252), "28252");
  214. Assert.AreEqual (0, *(scan + 29261), "29261");
  215. Assert.AreEqual (255, *(scan + 30270), "30270");
  216. Assert.AreEqual (0, *(scan + 31279), "31279");
  217. Assert.AreEqual (0, *(scan + 32288), "32288");
  218. Assert.AreEqual (255, *(scan + 33297), "33297");
  219. Assert.AreEqual (255, *(scan + 34306), "34306");
  220. Assert.AreEqual (255, *(scan + 35315), "35315");
  221. Assert.AreEqual (255, *(scan + 36324), "36324");
  222. Assert.AreEqual (0, *(scan + 37333), "37333");
  223. Assert.AreEqual (255, *(scan + 38342), "38342");
  224. Assert.AreEqual (255, *(scan + 39351), "39351");
  225. Assert.AreEqual (255, *(scan + 40360), "40360");
  226. Assert.AreEqual (255, *(scan + 41369), "41369");
  227. Assert.AreEqual (255, *(scan + 42378), "42378");
  228. Assert.AreEqual (0, *(scan + 43387), "43387");
  229. Assert.AreEqual (0, *(scan + 44396), "44396");
  230. Assert.AreEqual (255, *(scan + 45405), "45405");
  231. Assert.AreEqual (255, *(scan + 46414), "46414");
  232. Assert.AreEqual (255, *(scan + 47423), "47423");
  233. Assert.AreEqual (255, *(scan + 48432), "48432");
  234. Assert.AreEqual (255, *(scan + 49441), "49441");
  235. Assert.AreEqual (0, *(scan + 50450), "50450");
  236. Assert.AreEqual (0, *(scan + 51459), "51459");
  237. Assert.AreEqual (255, *(scan + 52468), "52468");
  238. Assert.AreEqual (255, *(scan + 53477), "53477");
  239. Assert.AreEqual (255, *(scan + 54486), "54486");
  240. Assert.AreEqual (0, *(scan + 55495), "55495");
  241. Assert.AreEqual (0, *(scan + 56504), "56504");
  242. Assert.AreEqual (0, *(scan + 57513), "57513");
  243. Assert.AreEqual (255, *(scan + 58522), "58522");
  244. Assert.AreEqual (255, *(scan + 59531), "59531");
  245. Assert.AreEqual (0, *(scan + 60540), "60540");
  246. Assert.AreEqual (0, *(scan + 61549), "61549");
  247. Assert.AreEqual (0, *(scan + 62558), "62558");
  248. Assert.AreEqual (0, *(scan + 63567), "63567");
  249. Assert.AreEqual (255, *(scan + 64576), "64576");
  250. Assert.AreEqual (0, *(scan + 65585), "65585");
  251. Assert.AreEqual (255, *(scan + 66594), "66594");
  252. Assert.AreEqual (255, *(scan + 67603), "67603");
  253. Assert.AreEqual (0, *(scan + 68612), "68612");
  254. Assert.AreEqual (0, *(scan + 69621), "69621");
  255. Assert.AreEqual (0, *(scan + 70630), "70630");
  256. Assert.AreEqual (0, *(scan + 71639), "71639");
  257. Assert.AreEqual (0, *(scan + 72648), "72648");
  258. Assert.AreEqual (255, *(scan + 73657), "73657");
  259. #endif
  260. }
  261. }
  262. finally {
  263. bmp.UnlockBits (data);
  264. }
  265. }
  266. }
  267. /* Checks bitmap features on a know 8bbp bitmap */
  268. [Test]
  269. public void Bitmap4bitFeatures ()
  270. {
  271. string sInFile = getInFile ("bitmaps/4bit.png");
  272. using (Bitmap bmp = new Bitmap (sInFile)) {
  273. GraphicsUnit unit = GraphicsUnit.World;
  274. RectangleF rect = bmp.GetBounds (ref unit);
  275. Assert.AreEqual (PixelFormat.Format4bppIndexed, bmp.PixelFormat);
  276. Assert.AreEqual (288, bmp.Width, "bmp.Width");
  277. Assert.AreEqual (384, bmp.Height, "bmp.Height");
  278. Assert.AreEqual (0, rect.X, "rect.X");
  279. Assert.AreEqual (0, rect.Y, "rect.Y");
  280. Assert.AreEqual (288, rect.Width, "rect.Width");
  281. Assert.AreEqual (384, rect.Height, "rect.Height");
  282. Assert.AreEqual (288, bmp.Size.Width, "bmp.Size.Width");
  283. Assert.AreEqual (384, bmp.Size.Height, "bmp.Size.Height");
  284. }
  285. }
  286. [Test]
  287. public void Bitmap4bitPixels ()
  288. {
  289. string sInFile = getInFile ("bitmaps/4bit.png");
  290. using (Bitmap bmp = new Bitmap (sInFile)) {
  291. #if false
  292. for (int x = 0; x < bmp.Width; x += 32) {
  293. for (int y = 0; y < bmp.Height; y += 32)
  294. Console.WriteLine ("\t\t\t\tAssert.AreEqual ({0}, bmp.GetPixel ({1}, {2}).ToArgb (), \"{1},{2}\");", bmp.GetPixel (x, y).ToArgb (), x, y);
  295. }
  296. #else
  297. // sampling values from a well known bitmap
  298. Assert.AreEqual (-10381134, bmp.GetPixel (0, 0).ToArgb (), "0,0");
  299. Assert.AreEqual (-1314578, bmp.GetPixel (0, 32).ToArgb (), "0,32");
  300. Assert.AreEqual (-1314578, bmp.GetPixel (0, 64).ToArgb (), "0,64");
  301. Assert.AreEqual (-1314578, bmp.GetPixel (0, 96).ToArgb (), "0,96");
  302. Assert.AreEqual (-3824008, bmp.GetPixel (0, 128).ToArgb (), "0,128");
  303. Assert.AreEqual (-12106173, bmp.GetPixel (0, 160).ToArgb (), "0,160");
  304. Assert.AreEqual (-12106173, bmp.GetPixel (0, 192).ToArgb (), "0,192");
  305. Assert.AreEqual (-12106173, bmp.GetPixel (0, 224).ToArgb (), "0,224");
  306. Assert.AreEqual (-12106173, bmp.GetPixel (0, 256).ToArgb (), "0,256");
  307. Assert.AreEqual (-7441574, bmp.GetPixel (0, 288).ToArgb (), "0,288");
  308. Assert.AreEqual (-3492461, bmp.GetPixel (0, 320).ToArgb (), "0,320");
  309. Assert.AreEqual (-5861009, bmp.GetPixel (0, 352).ToArgb (), "0,352");
  310. Assert.AreEqual (-10381134, bmp.GetPixel (32, 0).ToArgb (), "32,0");
  311. Assert.AreEqual (-1314578, bmp.GetPixel (32, 32).ToArgb (), "32,32");
  312. Assert.AreEqual (-7441574, bmp.GetPixel (32, 64).ToArgb (), "32,64");
  313. Assert.AreEqual (-12106173, bmp.GetPixel (32, 96).ToArgb (), "32,96");
  314. Assert.AreEqual (-1314578, bmp.GetPixel (32, 128).ToArgb (), "32,128");
  315. Assert.AreEqual (-1314578, bmp.GetPixel (32, 160).ToArgb (), "32,160");
  316. Assert.AreEqual (-12106173, bmp.GetPixel (32, 192).ToArgb (), "32,192");
  317. Assert.AreEqual (-12106173, bmp.GetPixel (32, 224).ToArgb (), "32,224");
  318. Assert.AreEqual (-12106173, bmp.GetPixel (32, 256).ToArgb (), "32,256");
  319. Assert.AreEqual (-12106173, bmp.GetPixel (32, 288).ToArgb (), "32,288");
  320. Assert.AreEqual (-3492461, bmp.GetPixel (32, 320).ToArgb (), "32,320");
  321. Assert.AreEqual (-2175574, bmp.GetPixel (32, 352).ToArgb (), "32,352");
  322. Assert.AreEqual (-6178617, bmp.GetPixel (64, 0).ToArgb (), "64,0");
  323. Assert.AreEqual (-12106173, bmp.GetPixel (64, 32).ToArgb (), "64,32");
  324. Assert.AreEqual (-12106173, bmp.GetPixel (64, 64).ToArgb (), "64,64");
  325. Assert.AreEqual (-12106173, bmp.GetPixel (64, 96).ToArgb (), "64,96");
  326. Assert.AreEqual (-12106173, bmp.GetPixel (64, 128).ToArgb (), "64,128");
  327. Assert.AreEqual (-12106173, bmp.GetPixel (64, 160).ToArgb (), "64,160");
  328. Assert.AreEqual (-12106173, bmp.GetPixel (64, 192).ToArgb (), "64,192");
  329. Assert.AreEqual (-12106173, bmp.GetPixel (64, 224).ToArgb (), "64,224");
  330. Assert.AreEqual (-5790569, bmp.GetPixel (64, 256).ToArgb (), "64,256");
  331. Assert.AreEqual (-12106173, bmp.GetPixel (64, 288).ToArgb (), "64,288");
  332. Assert.AreEqual (-12106173, bmp.GetPixel (64, 320).ToArgb (), "64,320");
  333. Assert.AreEqual (-5790569, bmp.GetPixel (64, 352).ToArgb (), "64,352");
  334. Assert.AreEqual (-1314578, bmp.GetPixel (96, 0).ToArgb (), "96,0");
  335. Assert.AreEqual (-10381134, bmp.GetPixel (96, 32).ToArgb (), "96,32");
  336. Assert.AreEqual (-12106173, bmp.GetPixel (96, 64).ToArgb (), "96,64");
  337. Assert.AreEqual (-12106173, bmp.GetPixel (96, 96).ToArgb (), "96,96");
  338. Assert.AreEqual (-7441574, bmp.GetPixel (96, 128).ToArgb (), "96,128");
  339. Assert.AreEqual (-12106173, bmp.GetPixel (96, 160).ToArgb (), "96,160");
  340. Assert.AreEqual (-5790569, bmp.GetPixel (96, 192).ToArgb (), "96,192");
  341. Assert.AreEqual (-12106173, bmp.GetPixel (96, 224).ToArgb (), "96,224");
  342. Assert.AreEqual (-4668490, bmp.GetPixel (96, 256).ToArgb (), "96,256");
  343. Assert.AreEqual (-12106173, bmp.GetPixel (96, 288).ToArgb (), "96,288");
  344. Assert.AreEqual (-1314578, bmp.GetPixel (96, 320).ToArgb (), "96,320");
  345. Assert.AreEqual (-3492461, bmp.GetPixel (96, 352).ToArgb (), "96,352");
  346. Assert.AreEqual (-5861009, bmp.GetPixel (128, 0).ToArgb (), "128,0");
  347. Assert.AreEqual (-7441574, bmp.GetPixel (128, 32).ToArgb (), "128,32");
  348. Assert.AreEqual (-7441574, bmp.GetPixel (128, 64).ToArgb (), "128,64");
  349. Assert.AreEqual (-12106173, bmp.GetPixel (128, 96).ToArgb (), "128,96");
  350. Assert.AreEqual (-12106173, bmp.GetPixel (128, 128).ToArgb (), "128,128");
  351. Assert.AreEqual (-12106173, bmp.GetPixel (128, 160).ToArgb (), "128,160");
  352. Assert.AreEqual (-12106173, bmp.GetPixel (128, 192).ToArgb (), "128,192");
  353. Assert.AreEqual (-12106173, bmp.GetPixel (128, 224).ToArgb (), "128,224");
  354. Assert.AreEqual (-12106173, bmp.GetPixel (128, 256).ToArgb (), "128,256");
  355. Assert.AreEqual (-12106173, bmp.GetPixel (128, 288).ToArgb (), "128,288");
  356. Assert.AreEqual (-12106173, bmp.GetPixel (128, 320).ToArgb (), "128,320");
  357. Assert.AreEqual (-12106173, bmp.GetPixel (128, 352).ToArgb (), "128,352");
  358. Assert.AreEqual (-1314578, bmp.GetPixel (160, 0).ToArgb (), "160,0");
  359. Assert.AreEqual (-1314578, bmp.GetPixel (160, 32).ToArgb (), "160,32");
  360. Assert.AreEqual (-12106173, bmp.GetPixel (160, 64).ToArgb (), "160,64");
  361. Assert.AreEqual (-1314578, bmp.GetPixel (160, 96).ToArgb (), "160,96");
  362. Assert.AreEqual (-12106173, bmp.GetPixel (160, 128).ToArgb (), "160,128");
  363. Assert.AreEqual (-5790569, bmp.GetPixel (160, 160).ToArgb (), "160,160");
  364. Assert.AreEqual (-12106173, bmp.GetPixel (160, 192).ToArgb (), "160,192");
  365. #endif
  366. }
  367. }
  368. [Test]
  369. [Category ("NotWorking")]
  370. public void Bitmat4bitData ()
  371. {
  372. string sInFile = getInFile ("bitmaps/4bit.png");
  373. using (Bitmap bmp = new Bitmap (sInFile)) {
  374. BitmapData data = bmp.LockBits (new Rectangle (0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
  375. try {
  376. Assert.AreEqual (bmp.Height, data.Height, "Height");
  377. Assert.AreEqual (bmp.Width, data.Width, "Width");
  378. Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
  379. int size = data.Height * data.Stride;
  380. unsafe {
  381. byte* scan = (byte*) data.Scan0;
  382. #if false
  383. // 1009 is the first prime after 1000 (so we're not affected by a recurring pattern)
  384. for (int p = 0; p < size; p += 1009) {
  385. Console.WriteLine ("\t\t\t\t\t\tAssert.AreEqual ({0}, *(scan + {1}), \"{1}\");", *(scan + p), p);
  386. }
  387. #else
  388. // sampling values from a well known bitmap
  389. Assert.AreEqual (178, *(scan + 0), "0");
  390. Assert.AreEqual (184, *(scan + 1009), "1009");
  391. Assert.AreEqual (235, *(scan + 2018), "2018");
  392. Assert.AreEqual (209, *(scan + 3027), "3027");
  393. Assert.AreEqual (240, *(scan + 4036), "4036");
  394. Assert.AreEqual (142, *(scan + 5045), "5045");
  395. Assert.AreEqual (139, *(scan + 6054), "6054");
  396. Assert.AreEqual (152, *(scan + 7063), "7063");
  397. Assert.AreEqual (235, *(scan + 8072), "8072");
  398. Assert.AreEqual (209, *(scan + 9081), "9081");
  399. Assert.AreEqual (240, *(scan + 10090), "10090");
  400. Assert.AreEqual (142, *(scan + 11099), "11099");
  401. Assert.AreEqual (199, *(scan + 12108), "12108");
  402. Assert.AreEqual (201, *(scan + 13117), "13117");
  403. Assert.AreEqual (97, *(scan + 14126), "14126");
  404. Assert.AreEqual (238, *(scan + 15135), "15135");
  405. Assert.AreEqual (240, *(scan + 16144), "16144");
  406. Assert.AreEqual (158, *(scan + 17153), "17153");
  407. Assert.AreEqual (119, *(scan + 18162), "18162");
  408. Assert.AreEqual (201, *(scan + 19171), "19171");
  409. Assert.AreEqual (88, *(scan + 20180), "20180");
  410. Assert.AreEqual (238, *(scan + 21189), "21189");
  411. Assert.AreEqual (240, *(scan + 22198), "22198");
  412. Assert.AreEqual (120, *(scan + 23207), "23207");
  413. Assert.AreEqual (182, *(scan + 24216), "24216");
  414. Assert.AreEqual (70, *(scan + 25225), "25225");
  415. Assert.AreEqual (71, *(scan + 26234), "26234");
  416. Assert.AreEqual (238, *(scan + 27243), "27243");
  417. Assert.AreEqual (240, *(scan + 28252), "28252");
  418. Assert.AreEqual (120, *(scan + 29261), "29261");
  419. Assert.AreEqual (238, *(scan + 30270), "30270");
  420. Assert.AreEqual (70, *(scan + 31279), "31279");
  421. Assert.AreEqual (71, *(scan + 32288), "32288");
  422. Assert.AreEqual (238, *(scan + 33297), "33297");
  423. Assert.AreEqual (240, *(scan + 34306), "34306");
  424. Assert.AreEqual (210, *(scan + 35315), "35315");
  425. Assert.AreEqual (238, *(scan + 36324), "36324");
  426. Assert.AreEqual (70, *(scan + 37333), "37333");
  427. Assert.AreEqual (97, *(scan + 38342), "38342");
  428. Assert.AreEqual (238, *(scan + 39351), "39351");
  429. Assert.AreEqual (240, *(scan + 40360), "40360");
  430. Assert.AreEqual (235, *(scan + 41369), "41369");
  431. Assert.AreEqual (238, *(scan + 42378), "42378");
  432. Assert.AreEqual (117, *(scan + 43387), "43387");
  433. Assert.AreEqual (158, *(scan + 44396), "44396");
  434. Assert.AreEqual (170, *(scan + 45405), "45405");
  435. Assert.AreEqual (240, *(scan + 46414), "46414");
  436. Assert.AreEqual (235, *(scan + 47423), "47423");
  437. Assert.AreEqual (209, *(scan + 48432), "48432");
  438. Assert.AreEqual (120, *(scan + 49441), "49441");
  439. Assert.AreEqual (71, *(scan + 50450), "50450");
  440. Assert.AreEqual (119, *(scan + 51459), "51459");
  441. Assert.AreEqual (240, *(scan + 52468), "52468");
  442. Assert.AreEqual (235, *(scan + 53477), "53477");
  443. Assert.AreEqual (209, *(scan + 54486), "54486");
  444. Assert.AreEqual (70, *(scan + 55495), "55495");
  445. Assert.AreEqual (71, *(scan + 56504), "56504");
  446. Assert.AreEqual (67, *(scan + 57513), "57513");
  447. Assert.AreEqual (240, *(scan + 58522), "58522");
  448. Assert.AreEqual (167, *(scan + 59531), "59531");
  449. Assert.AreEqual (67, *(scan + 60540), "60540");
  450. Assert.AreEqual (70, *(scan + 61549), "61549");
  451. Assert.AreEqual (71, *(scan + 62558), "62558");
  452. Assert.AreEqual (67, *(scan + 63567), "63567");
  453. Assert.AreEqual (240, *(scan + 64576), "64576");
  454. Assert.AreEqual (120, *(scan + 65585), "65585");
  455. Assert.AreEqual (182, *(scan + 66594), "66594");
  456. Assert.AreEqual (70, *(scan + 67603), "67603");
  457. Assert.AreEqual (120, *(scan + 68612), "68612");
  458. Assert.AreEqual (67, *(scan + 69621), "69621");
  459. Assert.AreEqual (70, *(scan + 70630), "70630");
  460. Assert.AreEqual (71, *(scan + 71639), "71639");
  461. Assert.AreEqual (90, *(scan + 72648), "72648");
  462. Assert.AreEqual (240, *(scan + 73657), "73657");
  463. #endif
  464. }
  465. }
  466. finally {
  467. bmp.UnlockBits (data);
  468. }
  469. }
  470. }
  471. private void Save (PixelFormat original, PixelFormat expected, bool colorCheck)
  472. {
  473. string sOutFile = String.Format ("linerect{0}-{1}.png", getOutSufix (), expected.ToString ());
  474. // Save
  475. Bitmap bmp = new Bitmap (100, 100, original);
  476. Graphics gr = Graphics.FromImage (bmp);
  477. using (Pen p = new Pen (Color.BlueViolet, 2)) {
  478. gr.DrawLine (p, 10.0F, 10.0F, 90.0F, 90.0F);
  479. gr.DrawRectangle (p, 10.0F, 10.0F, 80.0F, 80.0F);
  480. }
  481. try {
  482. bmp.Save (sOutFile, ImageFormat.Png);
  483. // Load
  484. using (Bitmap bmpLoad = new Bitmap (sOutFile)) {
  485. Assert.AreEqual (expected, bmpLoad.PixelFormat, "PixelFormat");
  486. if (colorCheck) {
  487. Color color = bmpLoad.GetPixel (10, 10);
  488. Assert.AreEqual (Color.FromArgb (255, 138, 43, 226), color, "BlueViolet");
  489. }
  490. }
  491. }
  492. finally {
  493. gr.Dispose ();
  494. bmp.Dispose ();
  495. try {
  496. File.Delete (sOutFile);
  497. }
  498. catch {
  499. }
  500. }
  501. }
  502. [Test]
  503. public void Save_24bppRgb ()
  504. {
  505. Save (PixelFormat.Format24bppRgb, PixelFormat.Format24bppRgb, true);
  506. }
  507. [Test]
  508. public void Save_32bppRgb ()
  509. {
  510. Save (PixelFormat.Format32bppRgb, PixelFormat.Format32bppArgb, true);
  511. }
  512. [Test]
  513. public void Save_32bppArgb ()
  514. {
  515. Save (PixelFormat.Format32bppArgb, PixelFormat.Format32bppArgb, true);
  516. }
  517. [Test]
  518. public void Save_32bppPArgb ()
  519. {
  520. Save (PixelFormat.Format32bppPArgb, PixelFormat.Format32bppArgb, true);
  521. }
  522. [Test]
  523. [Category ("NotWorking")]
  524. public void Save_48bppRgb ()
  525. {
  526. Save (PixelFormat.Format48bppRgb, PixelFormat.Format48bppRgb, false);
  527. }
  528. [Test]
  529. [Category ("NotWorking")]
  530. public void Save_64bppArgb ()
  531. {
  532. Save (PixelFormat.Format64bppArgb, PixelFormat.Format64bppArgb, false);
  533. }
  534. [Test]
  535. [Category ("NotWorking")]
  536. public void Save_64bppPArgb ()
  537. {
  538. Save (PixelFormat.Format64bppPArgb, PixelFormat.Format64bppArgb, false);
  539. }
  540. }
  541. }