Fixes for TARGET_JVM tests.
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing.Imaging / TestJpegCodec.cs
1 //
2 // JpegCodec class testing unit
3 //
4 // Authors:
5 //      Jordi Mas i Hernàndez (jordi@ximian.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2004 Ximian, Inc.  http://www.ximian.com
9 // Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Drawing;
33 using System.Drawing.Imaging;
34 using NUnit.Framework;
35 using System.IO;
36 using System.Security.Permissions;
37
38 namespace MonoTests.System.Drawing.Imaging {
39
40         [TestFixture]
41         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
42         public class JpegCodecTest {
43
44                 /* Get suffix to add to the filename */
45                 internal string getOutSufix ()
46                 {
47                         string s;
48
49                         int p = (int) Environment.OSVersion.Platform;
50                         if ((p == 4) || (p == 128))
51                                 s = "-unix";
52                         else
53                                 s = "-windows";
54
55                         if (Type.GetType ("Mono.Runtime", false) == null)
56                                 s += "-msnet";
57                         else
58                                 s += "-mono";
59
60                         return s;
61                 }
62
63                 /* Get the input directory depending on the runtime*/
64                 internal string getInFile (string file)
65                 {                               
66                         string sRslt = Path.GetFullPath ("../System.Drawing/" + file);
67                                 
68                         if (!File.Exists (sRslt)) 
69                                 sRslt = "Test/System.Drawing/" + file;                                                  
70                         
71                         return sRslt;
72                 }
73                 
74                 /* Checks bitmap features on a know 24-bits bitmap */
75                 [Test]
76                 public void Bitmap24bitFeatures ()
77                 {
78                         string sInFile = getInFile ("bitmaps/nature24bits.jpg");
79                         using (Bitmap bmp = new Bitmap (sInFile)) {
80                                 GraphicsUnit unit = GraphicsUnit.World;
81                                 RectangleF rect = bmp.GetBounds (ref unit);
82
83                                 Assert.AreEqual (PixelFormat.Format24bppRgb, bmp.PixelFormat, "PixelFormat");
84                                 Assert.AreEqual (110, bmp.Width, "bmp.Width");
85                                 Assert.AreEqual (100, bmp.Height, "bmp.Height");
86
87                                 Assert.AreEqual (0, rect.X, "rect.X");
88                                 Assert.AreEqual (0, rect.Y, "rect.Y");
89                                 Assert.AreEqual (110, rect.Width, "rect.Width");
90                                 Assert.AreEqual (100, rect.Height, "rect.Height");
91
92                                 Assert.AreEqual (110, bmp.Size.Width, "bmp.Size.Width");
93                                 Assert.AreEqual (100, bmp.Size.Height, "bmp.Size.Height");
94                         }
95                 }
96
97                 [Test]
98                 public void Bitmap24bitPixels ()
99                 {
100                         string sInFile = getInFile ("bitmaps/nature24bits.jpg");
101                         using (Bitmap bmp = new Bitmap (sInFile)) {
102 #if false
103                                 for (int x = 0; x < bmp.Width; x += 32) {
104                                         for (int y = 0; y < bmp.Height; y += 32)
105                                                 Console.WriteLine ("\t\t\t\tAssert.AreEqual ({0}, bmp.GetPixel ({1}, {2}).ToArgb (), \"{1},{2}\");", bmp.GetPixel (x, y).ToArgb (), x, y);
106                                 }
107 #else
108                                 // sampling values from a well known bitmap
109                                 Assert.AreEqual (-10447423, bmp.GetPixel (0, 0).ToArgb (), "0,0");
110                                 Assert.AreEqual (-12171958, bmp.GetPixel (0, 32).ToArgb (), "0,32");
111                                 Assert.AreEqual (-15192259, bmp.GetPixel (0, 64).ToArgb (), "0,64");
112                                 Assert.AreEqual (-15131110, bmp.GetPixel (0, 96).ToArgb (), "0,96");
113                                 Assert.AreEqual (-395272, bmp.GetPixel (32, 0).ToArgb (), "32,0");
114                                 Assert.AreEqual (-10131359, bmp.GetPixel (32, 32).ToArgb (), "32,32");
115                                 Assert.AreEqual (-10984322, bmp.GetPixel (32, 64).ToArgb (), "32,64");
116                                 Assert.AreEqual (-11034683, bmp.GetPixel (32, 96).ToArgb (), "32,96");
117                                 Assert.AreEqual (-1, bmp.GetPixel (64, 0).ToArgb (), "64,0");
118                                 Assert.AreEqual (-3163242, bmp.GetPixel (64, 32).ToArgb (), "64,32");
119                                 Assert.AreEqual (-7311538, bmp.GetPixel (64, 64).ToArgb (), "64,64");
120                                 Assert.AreEqual (-12149780, bmp.GetPixel (64, 96).ToArgb (), "64,96");
121                                 Assert.AreEqual (-1, bmp.GetPixel (96, 0).ToArgb (), "96,0");
122                                 Assert.AreEqual (-8224378, bmp.GetPixel (96, 32).ToArgb (), "96,32");
123                                 Assert.AreEqual (-11053718, bmp.GetPixel (96, 64).ToArgb (), "96,64");
124                                 Assert.AreEqual (-12944166, bmp.GetPixel (96, 96).ToArgb (), "96,96");
125 #endif
126                         }
127                 }
128
129 #if !TARGET_JVM
130                 [Test]
131                 [Category ("NotWorking")]
132                 public void Bitmap24bitData ()
133                 {
134                         string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
135                         using (Bitmap bmp = new Bitmap (sInFile)) {
136                                 BitmapData data = bmp.LockBits (new Rectangle (0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
137                                 try {
138                                         Assert.AreEqual (bmp.Height, data.Height, "Height");
139                                         Assert.AreEqual (bmp.Width, data.Width, "Width");
140                                         Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
141                                         int size = data.Height * data.Stride;
142                                         unsafe {
143                                                 byte* scan = (byte*) data.Scan0;
144 #if false
145                                                 // 1009 is the first prime after 1000 (so we're not affected by a recurring pattern)
146                                                 for (int p = 0; p < size; p += 1009) {
147                                                         Console.WriteLine ("\t\t\t\t\t\tAssert.AreEqual ({0}, *(scan + {1}), \"{1}\");", *(scan + p), p);
148                                                 }
149 #else
150                                                 // sampling values from a well known bitmap
151                                                 Assert.AreEqual (217, *(scan + 0), "0");
152                                                 Assert.AreEqual (192, *(scan + 1009), "1009");
153                                                 Assert.AreEqual (210, *(scan + 2018), "2018");
154                                                 Assert.AreEqual (196, *(scan + 3027), "3027");
155                                                 Assert.AreEqual (216, *(scan + 4036), "4036");
156                                                 Assert.AreEqual (215, *(scan + 5045), "5045");
157                                                 Assert.AreEqual (218, *(scan + 6054), "6054");
158                                                 Assert.AreEqual (218, *(scan + 7063), "7063");
159                                                 Assert.AreEqual (95, *(scan + 8072), "8072");
160                                                 Assert.AreEqual (9, *(scan + 9081), "9081");
161                                                 Assert.AreEqual (247, *(scan + 10090), "10090");
162                                                 Assert.AreEqual (161, *(scan + 11099), "11099");
163                                                 Assert.AreEqual (130, *(scan + 12108), "12108");
164                                                 Assert.AreEqual (131, *(scan + 13117), "13117");
165                                                 Assert.AreEqual (175, *(scan + 14126), "14126");
166                                                 Assert.AreEqual (217, *(scan + 15135), "15135");
167                                                 Assert.AreEqual (201, *(scan + 16144), "16144");
168                                                 Assert.AreEqual (183, *(scan + 17153), "17153");
169                                                 Assert.AreEqual (236, *(scan + 18162), "18162");
170                                                 Assert.AreEqual (242, *(scan + 19171), "19171");
171                                                 Assert.AreEqual (125, *(scan + 20180), "20180");
172                                                 Assert.AreEqual (193, *(scan + 21189), "21189");
173                                                 Assert.AreEqual (227, *(scan + 22198), "22198");
174                                                 Assert.AreEqual (44, *(scan + 23207), "23207");
175                                                 Assert.AreEqual (230, *(scan + 24216), "24216");
176                                                 Assert.AreEqual (224, *(scan + 25225), "25225");
177                                                 Assert.AreEqual (164, *(scan + 26234), "26234");
178                                                 Assert.AreEqual (43, *(scan + 27243), "27243");
179                                                 Assert.AreEqual (200, *(scan + 28252), "28252");
180                                                 Assert.AreEqual (255, *(scan + 29261), "29261");
181                                                 Assert.AreEqual (226, *(scan + 30270), "30270");
182                                                 Assert.AreEqual (230, *(scan + 31279), "31279");
183                                                 Assert.AreEqual (178, *(scan + 32288), "32288");
184                                                 Assert.AreEqual (224, *(scan + 33297), "33297");
185                                                 Assert.AreEqual (233, *(scan + 34306), "34306");
186                                                 Assert.AreEqual (212, *(scan + 35315), "35315");
187                                                 Assert.AreEqual (153, *(scan + 36324), "36324");
188                                                 Assert.AreEqual (143, *(scan + 37333), "37333");
189                                                 Assert.AreEqual (215, *(scan + 38342), "38342");
190                                                 Assert.AreEqual (116, *(scan + 39351), "39351");
191                                                 Assert.AreEqual (26, *(scan + 40360), "40360");
192                                                 Assert.AreEqual (28, *(scan + 41369), "41369");
193                                                 Assert.AreEqual (75, *(scan + 42378), "42378");
194                                                 Assert.AreEqual (50, *(scan + 43387), "43387");
195                                                 Assert.AreEqual (244, *(scan + 44396), "44396");
196                                                 Assert.AreEqual (191, *(scan + 45405), "45405");
197                                                 Assert.AreEqual (200, *(scan + 46414), "46414");
198                                                 Assert.AreEqual (197, *(scan + 47423), "47423");
199                                                 Assert.AreEqual (232, *(scan + 48432), "48432");
200                                                 Assert.AreEqual (186, *(scan + 49441), "49441");
201                                                 Assert.AreEqual (210, *(scan + 50450), "50450");
202                                                 Assert.AreEqual (215, *(scan + 51459), "51459");
203                                                 Assert.AreEqual (155, *(scan + 52468), "52468");
204                                                 Assert.AreEqual (56, *(scan + 53477), "53477");
205                                                 Assert.AreEqual (149, *(scan + 54486), "54486");
206                                                 Assert.AreEqual (137, *(scan + 55495), "55495");
207                                                 Assert.AreEqual (141, *(scan + 56504), "56504");
208                                                 Assert.AreEqual (36, *(scan + 57513), "57513");
209                                                 Assert.AreEqual (39, *(scan + 58522), "58522");
210                                                 Assert.AreEqual (25, *(scan + 59531), "59531");
211                                                 Assert.AreEqual (44, *(scan + 60540), "60540");
212                                                 Assert.AreEqual (12, *(scan + 61549), "61549");
213                                                 Assert.AreEqual (161, *(scan + 62558), "62558");
214                                                 Assert.AreEqual (179, *(scan + 63567), "63567");
215                                                 Assert.AreEqual (181, *(scan + 64576), "64576");
216                                                 Assert.AreEqual (165, *(scan + 65585), "65585");
217                                                 Assert.AreEqual (182, *(scan + 66594), "66594");
218                                                 Assert.AreEqual (186, *(scan + 67603), "67603");
219                                                 Assert.AreEqual (201, *(scan + 68612), "68612");
220                                                 Assert.AreEqual (49, *(scan + 69621), "69621");
221                                                 Assert.AreEqual (161, *(scan + 70630), "70630");
222                                                 Assert.AreEqual (140, *(scan + 71639), "71639");
223                                                 Assert.AreEqual (2, *(scan + 72648), "72648");
224                                                 Assert.AreEqual (15, *(scan + 73657), "73657");
225                                                 Assert.AreEqual (33, *(scan + 74666), "74666");
226                                                 Assert.AreEqual (17, *(scan + 75675), "75675");
227                                                 Assert.AreEqual (0, *(scan + 76684), "76684");
228                                                 Assert.AreEqual (47, *(scan + 77693), "77693");
229                                                 Assert.AreEqual (4, *(scan + 78702), "78702");
230                                                 Assert.AreEqual (142, *(scan + 79711), "79711");
231                                                 Assert.AreEqual (151, *(scan + 80720), "80720");
232                                                 Assert.AreEqual (124, *(scan + 81729), "81729");
233                                                 Assert.AreEqual (81, *(scan + 82738), "82738");
234                                                 Assert.AreEqual (214, *(scan + 83747), "83747");
235                                                 Assert.AreEqual (217, *(scan + 84756), "84756");
236                                                 Assert.AreEqual (30, *(scan + 85765), "85765");
237                                                 Assert.AreEqual (185, *(scan + 86774), "86774");
238                                                 Assert.AreEqual (200, *(scan + 87783), "87783");
239                                                 Assert.AreEqual (37, *(scan + 88792), "88792");
240                                                 Assert.AreEqual (2, *(scan + 89801), "89801");
241                                                 Assert.AreEqual (41, *(scan + 90810), "90810");
242                                                 Assert.AreEqual (16, *(scan + 91819), "91819");
243                                                 Assert.AreEqual (0, *(scan + 92828), "92828");
244                                                 Assert.AreEqual (146, *(scan + 93837), "93837");
245                                                 Assert.AreEqual (163, *(scan + 94846), "94846");
246 #endif
247                                         }
248                                 }
249                                 finally {
250                                         bmp.UnlockBits (data);
251                                 }
252                         }
253                 }
254 #endif
255
256                 [Test]
257                 public void Save () 
258                 {                               
259                         string sOutFile =  "linerect" + getOutSufix() + ".jpeg";
260                                                 
261                         // Save         
262                         Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppRgb);                                         
263                         Graphics gr = Graphics.FromImage (bmp);
264
265                         using (Pen p = new Pen (Color.Red, 2)) {
266                                 gr.DrawLine (p, 10.0F, 10.0F, 90.0F, 90.0F);
267                                 gr.DrawRectangle (p, 10.0F, 10.0F, 80.0F, 80.0F);
268                         }
269
270                         try {
271                                 bmp.Save (sOutFile, ImageFormat.Bmp);
272
273                                 // Load                 
274                                 using (Bitmap bmpLoad = new Bitmap (sOutFile)) {
275                                         Color color = bmpLoad.GetPixel (10, 10);
276                                         Assert.AreEqual (Color.FromArgb (255, 255, 0, 0), color);
277                                 }
278                         }
279                         finally {
280                                 gr.Dispose ();
281                                 bmp.Dispose ();
282                                 try {
283                                         File.Delete (sOutFile);
284                                 }
285                                 catch {
286                                 }
287                         }
288                 }
289         }
290 }