Merge pull request #823 from DavidKarlas/master
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing.Imaging / TiffCodecTest.cs
1 //
2 // TIFF Codec class testing unit
3 //
4 // Authors:
5 //      Jordi Mas i Hernàndez (jordi@ximian.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2006, 2007 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
30 using System;
31 using System.Drawing;
32 using System.Drawing.Imaging;
33 using System.IO;
34 using System.Security.Permissions;
35 using System.Text;
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Drawing.Imaging {
39
40         [TestFixture]
41         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
42         public class TiffCodecTest {
43
44                 [TestFixtureSetUp]
45                 public void SetUp ()
46                 {
47                         HostIgnoreList.CheckTest ("MonoTests.System.Drawing.Imaging.TiffCodecTest");
48                 }
49
50                 /* Get suffix to add to the filename */
51                 internal string getOutSufix ()
52                 {
53                         string s;
54
55                         int p = (int) Environment.OSVersion.Platform;
56                         if ((p == 4) || (p == 128) || (p == 6))
57                                 s = "-unix";
58                         else
59                                 s = "-windows";
60
61                         if (Type.GetType ("Mono.Runtime", false) == null)
62                                 s += "-msnet";
63                         else
64                                 s += "-mono";
65
66                         return s;
67                 }
68
69                 /* Get the input directory depending on the runtime*/
70                 internal string getInFile (string file)
71                 {
72                         string sRslt = Path.GetFullPath ("../System.Drawing/" + file);
73
74                         if (!File.Exists (sRslt))
75                                 sRslt = "Test/System.Drawing/" + file;
76
77                         return sRslt;
78                 }
79
80                 /* Checks bitmap features on a know 32bbp bitmap */
81                 [Test]
82                 public void Bitmap32bitsFeatures ()
83                 {
84                         string sInFile = getInFile ("bitmaps/almogaver32bits.tif");
85                         using (Bitmap bmp = new Bitmap (sInFile)) {
86                                 GraphicsUnit unit = GraphicsUnit.World;
87                                 RectangleF rect = bmp.GetBounds (ref unit);
88 // MS reports 24 bpp while we report 32 bpp
89 //                              Assert.AreEqual (PixelFormat.Format24bppRgb, bmp.PixelFormat);
90                                 Assert.AreEqual (173, bmp.Width, "bmp.Width");
91                                 Assert.AreEqual (183, bmp.Height, "bmp.Height");
92
93                                 Assert.AreEqual (0, rect.X, "rect.X");
94                                 Assert.AreEqual (0, rect.Y, "rect.Y");
95                                 Assert.AreEqual (173, rect.Width, "rect.Width");
96                                 Assert.AreEqual (183, rect.Height, "rect.Height");
97
98                                 Assert.AreEqual (173, bmp.Size.Width, "bmp.Size.Width");
99                                 Assert.AreEqual (183, bmp.Size.Height, "bmp.Size.Height");
100                         }
101                 }
102
103                 [Test]
104                 public void Bitmap32bitsPixels ()
105                 {
106                         string sInFile = getInFile ("bitmaps/almogaver32bits.tif");
107                         using (Bitmap bmp = new Bitmap (sInFile)) {
108 #if false
109                                 for (int x = 0; x < bmp.Width; x += 32) {
110                                         for (int y = 0; y < bmp.Height; y += 32)
111                                                 Console.WriteLine ("\t\t\t\tAssert.AreEqual ({0}, bmp.GetPixel ({1}, {2}).ToArgb (), \"{1},{2}\");", bmp.GetPixel (x, y).ToArgb (), x, y);
112                                 }
113 #else
114                                 // sampling values from a well known bitmap
115                                 Assert.AreEqual (-1579559, bmp.GetPixel (0, 0).ToArgb (), "0,0");
116                                 Assert.AreEqual (-1645353, bmp.GetPixel (0, 32).ToArgb (), "0,32");
117                                 Assert.AreEqual (-461332, bmp.GetPixel (0, 64).ToArgb (), "0,64");
118                                 Assert.AreEqual (-330005, bmp.GetPixel (0, 96).ToArgb (), "0,96");
119                                 Assert.AreEqual (-2237489, bmp.GetPixel (0, 128).ToArgb (), "0,128");
120                                 Assert.AreEqual (-1251105, bmp.GetPixel (0, 160).ToArgb (), "0,160");
121                                 Assert.AreEqual (-3024947, bmp.GetPixel (32, 0).ToArgb (), "32,0");
122                                 Assert.AreEqual (-2699070, bmp.GetPixel (32, 32).ToArgb (), "32,32");
123                                 Assert.AreEqual (-2366734, bmp.GetPixel (32, 64).ToArgb (), "32,64");
124                                 Assert.AreEqual (-4538413, bmp.GetPixel (32, 96).ToArgb (), "32,96");
125                                 Assert.AreEqual (-6116681, bmp.GetPixel (32, 128).ToArgb (), "32,128");
126                                 Assert.AreEqual (-7369076, bmp.GetPixel (32, 160).ToArgb (), "32,160");
127                                 Assert.AreEqual (-13024729, bmp.GetPixel (64, 0).ToArgb (), "64,0");
128                                 Assert.AreEqual (-7174020, bmp.GetPixel (64, 32).ToArgb (), "64,32");
129                                 Assert.AreEqual (-51, bmp.GetPixel (64, 64).ToArgb (), "64,64");
130                                 Assert.AreEqual (-16053503, bmp.GetPixel (64, 96).ToArgb (), "64,96");
131                                 Assert.AreEqual (-8224431, bmp.GetPixel (64, 128).ToArgb (), "64,128");
132                                 Assert.AreEqual (-16579326, bmp.GetPixel (64, 160).ToArgb (), "64,160");
133                                 Assert.AreEqual (-2502457, bmp.GetPixel (96, 0).ToArgb (), "96,0");
134                                 Assert.AreEqual (-9078395, bmp.GetPixel (96, 32).ToArgb (), "96,32");
135                                 Assert.AreEqual (-12696508, bmp.GetPixel (96, 64).ToArgb (), "96,64");
136                                 Assert.AreEqual (-70772, bmp.GetPixel (96, 96).ToArgb (), "96,96");
137                                 Assert.AreEqual (-4346279, bmp.GetPixel (96, 128).ToArgb (), "96,128");
138                                 Assert.AreEqual (-11583193, bmp.GetPixel (96, 160).ToArgb (), "96,160");
139                                 Assert.AreEqual (-724763, bmp.GetPixel (128, 0).ToArgb (), "128,0");
140                                 Assert.AreEqual (-7238268, bmp.GetPixel (128, 32).ToArgb (), "128,32");
141                                 Assert.AreEqual (-2169612, bmp.GetPixel (128, 64).ToArgb (), "128,64");
142                                 Assert.AreEqual (-3683883, bmp.GetPixel (128, 96).ToArgb (), "128,96");
143                                 Assert.AreEqual (-12892867, bmp.GetPixel (128, 128).ToArgb (), "128,128");
144                                 Assert.AreEqual (-3750464, bmp.GetPixel (128, 160).ToArgb (), "128,160");
145                                 Assert.AreEqual (-3222844, bmp.GetPixel (160, 0).ToArgb (), "160,0");
146                                 Assert.AreEqual (-65806, bmp.GetPixel (160, 32).ToArgb (), "160,32");
147                                 Assert.AreEqual (-2961726, bmp.GetPixel (160, 64).ToArgb (), "160,64");
148                                 Assert.AreEqual (-2435382, bmp.GetPixel (160, 96).ToArgb (), "160,96");
149                                 Assert.AreEqual (-2501944, bmp.GetPixel (160, 128).ToArgb (), "160,128");
150                                 Assert.AreEqual (-9211799, bmp.GetPixel (160, 160).ToArgb (), "160,160");
151 #endif
152                         }
153                 }
154
155                 [Test]
156                 public void Bitmap32bitsData ()
157                 {
158                         string sInFile = getInFile ("bitmaps/almogaver32bits.tif");
159                         using (Bitmap bmp = new Bitmap (sInFile)) {
160                                 BitmapData data = bmp.LockBits (new Rectangle (0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
161                                 try {
162                                         Assert.AreEqual (bmp.Height, data.Height, "Height");
163                                         Assert.AreEqual (bmp.Width, data.Width, "Width");
164                                         Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
165                                         Assert.AreEqual (520, data.Stride, "Stride");
166                                         int size = data.Height * data.Stride;
167                                         unsafe {
168                                                 byte* scan = (byte*) data.Scan0;
169 #if false
170                                                 // 1009 is the first prime after 1000 (so we're not affected by a recurring pattern)
171                                                 for (int p = 0; p < size; p += 1009) {
172                                                         Console.WriteLine ("\t\t\t\t\t\tAssert.AreEqual ({0}, *(scan + {1}), \"{1}\");", *(scan + p), p);
173                                                 }
174 #else
175                                                 // sampling values from a well known bitmap
176                                                 Assert.AreEqual (217, *(scan + 0), "0");
177                                                 Assert.AreEqual (192, *(scan + 1009), "1009");
178                                                 Assert.AreEqual (210, *(scan + 2018), "2018");
179                                                 Assert.AreEqual (196, *(scan + 3027), "3027");
180                                                 Assert.AreEqual (216, *(scan + 4036), "4036");
181                                                 Assert.AreEqual (215, *(scan + 5045), "5045");
182                                                 Assert.AreEqual (218, *(scan + 6054), "6054");
183                                                 Assert.AreEqual (218, *(scan + 7063), "7063");
184                                                 Assert.AreEqual (95, *(scan + 8072), "8072");
185                                                 Assert.AreEqual (9, *(scan + 9081), "9081");
186                                                 Assert.AreEqual (247, *(scan + 10090), "10090");
187                                                 Assert.AreEqual (161, *(scan + 11099), "11099");
188                                                 Assert.AreEqual (130, *(scan + 12108), "12108");
189                                                 Assert.AreEqual (131, *(scan + 13117), "13117");
190                                                 Assert.AreEqual (175, *(scan + 14126), "14126");
191                                                 Assert.AreEqual (217, *(scan + 15135), "15135");
192                                                 Assert.AreEqual (201, *(scan + 16144), "16144");
193                                                 Assert.AreEqual (183, *(scan + 17153), "17153");
194                                                 Assert.AreEqual (236, *(scan + 18162), "18162");
195                                                 Assert.AreEqual (242, *(scan + 19171), "19171");
196                                                 Assert.AreEqual (125, *(scan + 20180), "20180");
197                                                 Assert.AreEqual (193, *(scan + 21189), "21189");
198                                                 Assert.AreEqual (227, *(scan + 22198), "22198");
199                                                 Assert.AreEqual (44, *(scan + 23207), "23207");
200                                                 Assert.AreEqual (230, *(scan + 24216), "24216");
201                                                 Assert.AreEqual (224, *(scan + 25225), "25225");
202                                                 Assert.AreEqual (164, *(scan + 26234), "26234");
203                                                 Assert.AreEqual (43, *(scan + 27243), "27243");
204                                                 Assert.AreEqual (200, *(scan + 28252), "28252");
205                                                 Assert.AreEqual (255, *(scan + 29261), "29261");
206                                                 Assert.AreEqual (226, *(scan + 30270), "30270");
207                                                 Assert.AreEqual (230, *(scan + 31279), "31279");
208                                                 Assert.AreEqual (178, *(scan + 32288), "32288");
209                                                 Assert.AreEqual (224, *(scan + 33297), "33297");
210                                                 Assert.AreEqual (233, *(scan + 34306), "34306");
211                                                 Assert.AreEqual (212, *(scan + 35315), "35315");
212                                                 Assert.AreEqual (153, *(scan + 36324), "36324");
213                                                 Assert.AreEqual (143, *(scan + 37333), "37333");
214                                                 Assert.AreEqual (215, *(scan + 38342), "38342");
215                                                 Assert.AreEqual (116, *(scan + 39351), "39351");
216                                                 Assert.AreEqual (26, *(scan + 40360), "40360");
217                                                 Assert.AreEqual (28, *(scan + 41369), "41369");
218                                                 Assert.AreEqual (75, *(scan + 42378), "42378");
219                                                 Assert.AreEqual (50, *(scan + 43387), "43387");
220                                                 Assert.AreEqual (244, *(scan + 44396), "44396");
221                                                 Assert.AreEqual (191, *(scan + 45405), "45405");
222                                                 Assert.AreEqual (200, *(scan + 46414), "46414");
223                                                 Assert.AreEqual (197, *(scan + 47423), "47423");
224                                                 Assert.AreEqual (232, *(scan + 48432), "48432");
225                                                 Assert.AreEqual (186, *(scan + 49441), "49441");
226                                                 Assert.AreEqual (210, *(scan + 50450), "50450");
227                                                 Assert.AreEqual (215, *(scan + 51459), "51459");
228                                                 Assert.AreEqual (155, *(scan + 52468), "52468");
229                                                 Assert.AreEqual (56, *(scan + 53477), "53477");
230                                                 Assert.AreEqual (149, *(scan + 54486), "54486");
231                                                 Assert.AreEqual (137, *(scan + 55495), "55495");
232                                                 Assert.AreEqual (141, *(scan + 56504), "56504");
233                                                 Assert.AreEqual (36, *(scan + 57513), "57513");
234                                                 Assert.AreEqual (39, *(scan + 58522), "58522");
235                                                 Assert.AreEqual (25, *(scan + 59531), "59531");
236                                                 Assert.AreEqual (44, *(scan + 60540), "60540");
237                                                 Assert.AreEqual (12, *(scan + 61549), "61549");
238                                                 Assert.AreEqual (161, *(scan + 62558), "62558");
239                                                 Assert.AreEqual (179, *(scan + 63567), "63567");
240                                                 Assert.AreEqual (181, *(scan + 64576), "64576");
241                                                 Assert.AreEqual (165, *(scan + 65585), "65585");
242                                                 Assert.AreEqual (182, *(scan + 66594), "66594");
243                                                 Assert.AreEqual (186, *(scan + 67603), "67603");
244                                                 Assert.AreEqual (201, *(scan + 68612), "68612");
245                                                 Assert.AreEqual (49, *(scan + 69621), "69621");
246                                                 Assert.AreEqual (161, *(scan + 70630), "70630");
247                                                 Assert.AreEqual (140, *(scan + 71639), "71639");
248                                                 Assert.AreEqual (2, *(scan + 72648), "72648");
249                                                 Assert.AreEqual (15, *(scan + 73657), "73657");
250                                                 Assert.AreEqual (33, *(scan + 74666), "74666");
251                                                 Assert.AreEqual (17, *(scan + 75675), "75675");
252                                                 Assert.AreEqual (0, *(scan + 76684), "76684");
253                                                 Assert.AreEqual (47, *(scan + 77693), "77693");
254                                                 Assert.AreEqual (4, *(scan + 78702), "78702");
255                                                 Assert.AreEqual (142, *(scan + 79711), "79711");
256                                                 Assert.AreEqual (151, *(scan + 80720), "80720");
257                                                 Assert.AreEqual (124, *(scan + 81729), "81729");
258                                                 Assert.AreEqual (81, *(scan + 82738), "82738");
259                                                 Assert.AreEqual (214, *(scan + 83747), "83747");
260                                                 Assert.AreEqual (217, *(scan + 84756), "84756");
261                                                 Assert.AreEqual (30, *(scan + 85765), "85765");
262                                                 Assert.AreEqual (185, *(scan + 86774), "86774");
263                                                 Assert.AreEqual (200, *(scan + 87783), "87783");
264                                                 Assert.AreEqual (37, *(scan + 88792), "88792");
265                                                 Assert.AreEqual (2, *(scan + 89801), "89801");
266                                                 Assert.AreEqual (41, *(scan + 90810), "90810");
267                                                 Assert.AreEqual (16, *(scan + 91819), "91819");
268                                                 Assert.AreEqual (0, *(scan + 92828), "92828");
269                                                 Assert.AreEqual (146, *(scan + 93837), "93837");
270                                                 Assert.AreEqual (163, *(scan + 94846), "94846");
271 #endif
272                                         }
273                                 }
274                                 finally {
275                                         bmp.UnlockBits (data);
276                                 }
277                         }
278                 }
279
280                 private void Save (PixelFormat original, PixelFormat expected, bool colorCheck)
281                 {
282                         string sOutFile = String.Format ("linerect{0}-{1}.tif", getOutSufix (), expected.ToString ());
283
284                         // Save         
285                         Bitmap bmp = new Bitmap (100, 100, original);
286                         Graphics gr = Graphics.FromImage (bmp);
287
288                         using (Pen p = new Pen (Color.BlueViolet, 2)) {
289                                 gr.DrawLine (p, 10.0F, 10.0F, 90.0F, 90.0F);
290                                 gr.DrawRectangle (p, 10.0F, 10.0F, 80.0F, 80.0F);
291                         }
292
293                         try {
294                                 bmp.Save (sOutFile, ImageFormat.Tiff);
295
296                                 // Load
297                                 using (Bitmap bmpLoad = new Bitmap (sOutFile)) {
298                                         Assert.AreEqual (expected, bmpLoad.PixelFormat, "PixelFormat");
299                                         if (colorCheck) {
300                                                 Color color = bmpLoad.GetPixel (10, 10);
301                                                 Assert.AreEqual (Color.FromArgb (255, 138, 43, 226), color, "BlueViolet");
302                                         }
303                                 }
304                         }
305                         finally {
306                                 gr.Dispose ();
307                                 bmp.Dispose ();
308                                 try {
309                                         File.Delete (sOutFile);
310                                 }
311                                 catch {
312                                 }
313                         }
314                 }
315
316                 [Test]
317                 public void Save_24bppRgb ()
318                 {
319                         Save (PixelFormat.Format24bppRgb, PixelFormat.Format24bppRgb, true);
320                 }
321
322                 [Test]
323                 public void Save_32bppRgb ()
324                 {
325                         Save (PixelFormat.Format32bppRgb, PixelFormat.Format32bppArgb, true);
326                 }
327
328                 [Test]
329                 public void Save_32bppArgb ()
330                 {
331                         Save (PixelFormat.Format32bppArgb, PixelFormat.Format32bppArgb, true);
332                 }
333
334                 [Test]
335                 public void Save_32bppPArgb ()
336                 {
337                         Save (PixelFormat.Format32bppPArgb, PixelFormat.Format32bppArgb, true);
338                 }
339
340                 [Test]
341                 [Category ("NotWorking")]
342                 public void Save_48bppRgb ()
343                 {
344                         Save (PixelFormat.Format48bppRgb, PixelFormat.Format24bppRgb, false);
345                 }
346
347                 [Test]
348                 [Category ("NotWorking")]
349                 public void Save_64bppArgb ()
350                 {
351                         Save (PixelFormat.Format64bppArgb, PixelFormat.Format32bppArgb, false);
352                 }
353
354                 [Test]
355                 [Category ("NotWorking")]
356                 public void Save_64bppPArgb ()
357                 {
358                         Save (PixelFormat.Format64bppPArgb, PixelFormat.Format32bppArgb, false);
359                 }
360         }
361 }