2 // Image class testing unit
5 // Jordi Mas i Hernàndez (jmas@softcatala.org>
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2005 Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2005-2007 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
33 using System.Drawing.Imaging;
35 using System.Reflection;
36 using System.Security.Permissions;
37 using System.Xml.Serialization;
38 using NUnit.Framework;
40 namespace MonoTests.System.Drawing{
43 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
44 public class ImageTest {
47 private bool callback;
50 public void FixtureSetup ()
52 fname = Path.GetTempFileName ();
56 public void FixtureTearDown ()
72 [ExpectedException (typeof (FileNotFoundException))]
73 public void FileDoesNotExists ()
75 Image.FromFile ("FileDoesNotExists.jpg");
78 private bool CallbackTrue ()
84 private bool CallbackFalse ()
91 public void GetThumbnailImage_NullCallback_Tiff ()
93 using (Bitmap bmp = new Bitmap (10, 10)) {
94 // according to documentation a callback is mandatory
95 Image tn = bmp.GetThumbnailImage (10, 5, null, IntPtr.Zero);
96 Assert.AreEqual (5, tn.Height, "Height");
97 Assert.AreEqual (10, tn.Width, "Width");
98 Assert.IsFalse (callback, "Callback called");
99 tn.Save (fname, ImageFormat.Tiff);
104 [ExpectedException (typeof (OutOfMemoryException))]
105 public void GetThumbnailImage_Height_Zero ()
107 using (Bitmap bmp = new Bitmap (10, 10)) {
108 Image tn = bmp.GetThumbnailImage (5, 0, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
113 [ExpectedException (typeof (OutOfMemoryException))]
114 public void GetThumbnailImage_Width_Negative ()
116 using (Bitmap bmp = new Bitmap (10, 10)) {
117 Image tn = bmp.GetThumbnailImage (-5, 5, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
122 public void GetThumbnailImage_CallbackData_Invalid ()
124 using (Bitmap bmp = new Bitmap (10, 10)) {
125 // according to documentation IntPtr.Zero must be supplied as data
126 Image tn = bmp.GetThumbnailImage (5, 5, new Image.GetThumbnailImageAbort (CallbackFalse), (IntPtr)Int32.MaxValue);
127 Assert.AreEqual (5, tn.Height, "Height");
128 Assert.AreEqual (5, tn.Width, "Width");
129 Assert.IsFalse (callback, "Callback called");
130 tn.Save (fname, ImageFormat.Tiff);
135 public void GetThumbnailImage_SameSize_Bmp ()
137 using (Bitmap bmp = new Bitmap (10, 10)) {
138 Image tn = bmp.GetThumbnailImage (10, 10, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
139 Assert.AreEqual (10, tn.Height, "Height");
140 Assert.AreEqual (10, tn.Width, "Width");
141 Assert.IsFalse (callback, "Callback called");
142 tn.Save (fname, ImageFormat.Bmp);
147 public void GetThumbnailImage_Smaller_Gif ()
149 using (Bitmap bmp = new Bitmap (10, 10)) {
150 Image tn = bmp.GetThumbnailImage (4, 4, new Image.GetThumbnailImageAbort (CallbackTrue), IntPtr.Zero);
151 Assert.AreEqual (4, tn.Height, "Height");
152 Assert.AreEqual (4, tn.Width, "Width");
153 Assert.IsFalse (callback, "Callback called");
154 tn.Save (fname, ImageFormat.Gif);
159 public void GetThumbnailImage_Bigger_Png ()
161 using (Bitmap bmp = new Bitmap (10, 10)) {
162 Image tn = bmp.GetThumbnailImage (40, 40, new Image.GetThumbnailImageAbort (CallbackTrue), IntPtr.Zero);
163 Assert.AreEqual (40, tn.Height, "Height");
164 Assert.AreEqual (40, tn.Width, "Width");
165 Assert.IsFalse (callback, "Callback called");
166 tn.Save (fname, ImageFormat.Png);
172 [Category ("NotDotNet")] // MS 1.x throws an ArgumentNullException in this case
174 public void Stream_Unlocked ()
178 using (MemoryStream ms = new MemoryStream ()) {
179 using (Bitmap bmp = new Bitmap (10, 10)) {
180 bmp.Save (ms, ImageFormat.Png);
183 img = Image.FromStream (ms);
185 // stream isn't available anymore
186 ((Bitmap) img).MakeTransparent (Color.Transparent);
188 catch (OutOfMemoryException) {
189 int p = (int) Environment.OSVersion.Platform;
190 // libgdiplus (UNIX) doesn't lazy load the image so the
191 // stream may be freed (and this exception will never occur)
192 if ((p == 4) || (p == 128) || (p == 6))
199 [Category ("NotDotNet")] // MS 1.x throws an ArgumentNullException in this case
201 public void Stream_Locked ()
204 using (MemoryStream ms = new MemoryStream ()) {
205 using (Bitmap bmp = new Bitmap (10, 10)) {
206 bmp.Save (ms, ImageFormat.Png);
209 img = Image.FromStream (ms);
210 // stream is available
211 ((Bitmap) img).MakeTransparent (Color.Transparent);
217 [Category ("NotWorking")] // http://bugzilla.ximian.com/show_bug.cgi?id=80558
219 [ExpectedException (typeof (InvalidOperationException))]
221 public void XmlSerialize ()
223 new XmlSerializer (typeof (Image));
226 private void Wmf (Image img)
228 Assert.IsFalse (img is Bitmap, "Bitmap");
229 Assert.IsTrue (img is Metafile, "Metafile");
231 Assert.AreEqual (327683, img.Flags, "Flags");
232 Assert.IsTrue (img.RawFormat.Equals (ImageFormat.Wmf), "Wmf");
234 Assert.IsNull (img.Tag, "Tag");
239 public void FromFile_Metafile_Wmf ()
241 string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf");
242 using (Image img = Image.FromFile (filename)) {
248 public void FromStream_Metafile_Wmf ()
250 string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf");
251 using (FileStream fs = File.OpenRead (filename)) {
252 using (Image img = Image.FromStream (fs)) {
259 [Category ("NotWorking")] // https://bugzilla.novell.com/show_bug.cgi?id=338779
260 [ExpectedException (typeof (ArgumentException))]
261 public void FromStream_Metafile_Wmf_NotOrigin ()
263 string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf");
264 using (FileStream fs = File.OpenRead (filename)) {
265 fs.Position = fs.Length / 2;
266 Image.FromStream (fs);
270 private void Emf (Image img)
272 Assert.IsFalse (img is Bitmap, "Bitmap");
273 Assert.IsTrue (img is Metafile, "Metafile");
275 Assert.AreEqual (327683, img.Flags, "Flags");
276 Assert.IsTrue (img.RawFormat.Equals (ImageFormat.Emf), "Emf");
278 Assert.IsNull (img.Tag, "Tag");
283 public void FromFile_Metafile_Emf ()
285 string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
286 using (Image img = Image.FromFile (filename)) {
292 public void FromStream_Metafile_Emf ()
294 string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
295 using (FileStream fs = File.OpenRead (filename)) {
296 using (Image img = Image.FromStream (fs)) {
303 [Category ("NotWorking")] // https://bugzilla.novell.com/show_bug.cgi?id=338779
304 [ExpectedException (typeof (ArgumentException))]
305 public void FromStream_Metafile_Emf_NotOrigin ()
307 string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
308 using (FileStream fs = File.OpenRead (filename)) {
309 fs.Position = fs.Length / 2;
310 Image.FromStream (fs);
315 [ExpectedException (typeof (OutOfMemoryException))]
316 public void FromFile_Invalid ()
318 string filename = Assembly.GetExecutingAssembly ().Location;
319 Image.FromFile (filename);
323 [ExpectedException (typeof (ArgumentException))]
324 public void FromStream_Invalid ()
326 string filename = Assembly.GetExecutingAssembly ().Location;
327 using (FileStream fs = File.OpenRead (filename)) {
328 Image.FromStream (fs);
332 private Bitmap GetBitmap ()
334 Bitmap bmp = new Bitmap (20, 10, PixelFormat.Format24bppRgb);
335 using (Graphics g = Graphics.FromImage (bmp))
337 Pen pen = new Pen (Color.Black, 3);
338 g.DrawRectangle (pen, 0, 0, 5, 10);
344 public void StreamSaveLoad ()
346 using (MemoryStream ms = new MemoryStream ()) {
347 using (Bitmap bmp = GetBitmap ()) {
348 Assert.AreEqual (0, ms.Position, "Position-1");
349 bmp.Save (ms, ImageFormat.Bmp);
350 Assert.IsTrue (ms.Position > 0, "Position-2");
352 ms.Position = ms.Length;
353 Assert.AreEqual (ms.Length, ms.Position, "Position-3");
355 Bitmap bmp2 = (Bitmap)Image.FromStream (ms);
356 Assert.IsTrue (ms.Position > 20, "Position-4");
358 Assert.IsTrue (bmp2.RawFormat.Equals (ImageFormat.Bmp), "Bmp");
360 Assert.AreEqual (bmp.GetPixel (0, 0), bmp2.GetPixel (0, 0), "0,0");
361 Assert.AreEqual (bmp.GetPixel (10, 0), bmp2.GetPixel (10, 0), "10,0");
367 [ExpectedException (typeof (ArgumentException))]
368 public void StreamJunkSaveLoad ()
370 using (MemoryStream ms = new MemoryStream ()) {
374 Assert.AreEqual (2, ms.Position, "Position-1");
376 using (Bitmap bmp = GetBitmap ()) {
377 bmp.Save (ms, ImageFormat.Bmp);
378 Assert.IsTrue (ms.Position > 2, "Position-2");
380 Image.FromStream (ms);
387 [ExpectedException (typeof (InvalidOperationException))]
389 public void XmlSerialization ()
391 new XmlSerializer (typeof (Image));