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 public class ImageTest {
46 private bool callback;
49 public void FixtureSetup ()
51 fname = Path.GetTempFileName ();
55 public void FixtureTearDown ()
71 public void FileDoesNotExists ()
73 Assert.Throws<FileNotFoundException> (() => Image.FromFile ("FileDoesNotExists.jpg"));
76 private bool CallbackTrue ()
82 private bool CallbackFalse ()
89 public void GetThumbnailImage_NullCallback_Tiff ()
91 using (Bitmap bmp = new Bitmap (10, 10)) {
92 // according to documentation a callback is mandatory
93 Image tn = bmp.GetThumbnailImage (10, 5, null, IntPtr.Zero);
94 Assert.AreEqual (5, tn.Height, "Height");
95 Assert.AreEqual (10, tn.Width, "Width");
96 Assert.IsFalse (callback, "Callback called");
97 tn.Save (fname, ImageFormat.Tiff);
102 public void GetThumbnailImage_Height_Zero ()
104 using (Bitmap bmp = new Bitmap (10, 10)) {
105 Assert.Throws<OutOfMemoryException> (() => bmp.GetThumbnailImage (5, 0, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero));
110 public void GetThumbnailImage_Width_Negative ()
112 using (Bitmap bmp = new Bitmap (10, 10)) {
113 Assert.Throws<OutOfMemoryException> (() => bmp.GetThumbnailImage (-5, 5, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero));
118 public void GetThumbnailImage_CallbackData_Invalid ()
120 using (Bitmap bmp = new Bitmap (10, 10)) {
121 // according to documentation IntPtr.Zero must be supplied as data
122 Image tn = bmp.GetThumbnailImage (5, 5, new Image.GetThumbnailImageAbort (CallbackFalse), (IntPtr)Int32.MaxValue);
123 Assert.AreEqual (5, tn.Height, "Height");
124 Assert.AreEqual (5, tn.Width, "Width");
125 Assert.IsFalse (callback, "Callback called");
126 tn.Save (fname, ImageFormat.Tiff);
131 public void GetThumbnailImage_SameSize_Bmp ()
133 using (Bitmap bmp = new Bitmap (10, 10)) {
134 Image tn = bmp.GetThumbnailImage (10, 10, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
135 Assert.AreEqual (10, tn.Height, "Height");
136 Assert.AreEqual (10, tn.Width, "Width");
137 Assert.IsFalse (callback, "Callback called");
138 tn.Save (fname, ImageFormat.Bmp);
143 public void GetThumbnailImage_Smaller_Gif ()
145 using (Bitmap bmp = new Bitmap (10, 10)) {
146 Image tn = bmp.GetThumbnailImage (4, 4, new Image.GetThumbnailImageAbort (CallbackTrue), IntPtr.Zero);
147 Assert.AreEqual (4, tn.Height, "Height");
148 Assert.AreEqual (4, tn.Width, "Width");
149 Assert.IsFalse (callback, "Callback called");
150 tn.Save (fname, ImageFormat.Gif);
155 public void GetThumbnailImage_Bigger_Png ()
157 using (Bitmap bmp = new Bitmap (10, 10)) {
158 Image tn = bmp.GetThumbnailImage (40, 40, new Image.GetThumbnailImageAbort (CallbackTrue), IntPtr.Zero);
159 Assert.AreEqual (40, tn.Height, "Height");
160 Assert.AreEqual (40, tn.Width, "Width");
161 Assert.IsFalse (callback, "Callback called");
162 tn.Save (fname, ImageFormat.Png);
167 public void Stream_Unlocked ()
171 using (MemoryStream ms = new MemoryStream ()) {
172 using (Bitmap bmp = new Bitmap (10, 10)) {
173 bmp.Save (ms, ImageFormat.Png);
176 img = Image.FromStream (ms);
178 // stream isn't available anymore
179 ((Bitmap) img).MakeTransparent (Color.Transparent);
181 catch (OutOfMemoryException) {
182 int p = (int) Environment.OSVersion.Platform;
183 // libgdiplus (UNIX) doesn't lazy load the image so the
184 // stream may be freed (and this exception will never occur)
185 if ((p == 4) || (p == 128) || (p == 6))
191 public void Stream_Locked ()
194 using (MemoryStream ms = new MemoryStream ()) {
195 using (Bitmap bmp = new Bitmap (10, 10)) {
196 bmp.Save (ms, ImageFormat.Png);
199 img = Image.FromStream (ms);
200 // stream is available
201 ((Bitmap) img).MakeTransparent (Color.Transparent);
206 [Category ("NotWorking")] // http://bugzilla.ximian.com/show_bug.cgi?id=80558
207 public void XmlSerialize ()
209 new XmlSerializer (typeof (Image));
212 private void Wmf (Image img)
214 Assert.IsFalse (img is Bitmap, "Bitmap");
215 Assert.IsTrue (img is Metafile, "Metafile");
217 Assert.AreEqual (327683, img.Flags, "Flags");
218 Assert.IsTrue (img.RawFormat.Equals (ImageFormat.Wmf), "Wmf");
219 Assert.IsNull (img.Tag, "Tag");
223 public void FromFile_Metafile_Wmf ()
225 string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf");
226 using (Image img = Image.FromFile (filename)) {
232 public void FromStream_Metafile_Wmf ()
234 string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf");
235 using (FileStream fs = File.OpenRead (filename)) {
236 using (Image img = Image.FromStream (fs)) {
243 [Category ("NotWorking")] // https://bugzilla.novell.com/show_bug.cgi?id=338779
244 public void FromStream_Metafile_Wmf_NotOrigin ()
246 string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf");
247 using (FileStream fs = File.OpenRead (filename)) {
248 fs.Position = fs.Length / 2;
249 Assert.Throws<ArgumentException> (() => Image.FromStream (fs));
253 private void Emf (Image img)
255 Assert.IsFalse (img is Bitmap, "Bitmap");
256 Assert.IsTrue (img is Metafile, "Metafile");
258 Assert.AreEqual (327683, img.Flags, "Flags");
259 Assert.IsTrue (img.RawFormat.Equals (ImageFormat.Emf), "Emf");
260 Assert.IsNull (img.Tag, "Tag");
264 public void FromFile_Metafile_Emf ()
266 string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
267 using (Image img = Image.FromFile (filename)) {
273 public void FromStream_Metafile_Emf ()
275 string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
276 using (FileStream fs = File.OpenRead (filename)) {
277 using (Image img = Image.FromStream (fs)) {
284 [Category ("NotWorking")] // https://bugzilla.novell.com/show_bug.cgi?id=338779
285 public void FromStream_Metafile_Emf_NotOrigin ()
287 string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
288 using (FileStream fs = File.OpenRead (filename)) {
289 fs.Position = fs.Length / 2;
290 Assert.Throws<ArgumentException> (() => Image.FromStream (fs));
295 public void FromFile_Invalid ()
297 string filename = Assembly.GetExecutingAssembly ().Location;
298 Assert.Throws<OutOfMemoryException> (() => Image.FromFile (filename));
302 public void FromStream_Invalid ()
304 string filename = Assembly.GetExecutingAssembly ().Location;
305 using (FileStream fs = File.OpenRead (filename)) {
306 Assert.Throws<ArgumentException> (() => Image.FromStream (fs));
310 private Bitmap GetBitmap ()
312 Bitmap bmp = new Bitmap (20, 10, PixelFormat.Format24bppRgb);
313 using (Graphics g = Graphics.FromImage (bmp))
315 Pen pen = new Pen (Color.Black, 3);
316 g.DrawRectangle (pen, 0, 0, 5, 10);
322 public void StreamSaveLoad ()
324 using (MemoryStream ms = new MemoryStream ()) {
325 using (Bitmap bmp = GetBitmap ()) {
326 Assert.AreEqual (0, ms.Position, "Position-1");
327 bmp.Save (ms, ImageFormat.Bmp);
328 Assert.IsTrue (ms.Position > 0, "Position-2");
330 ms.Position = ms.Length;
331 Assert.AreEqual (ms.Length, ms.Position, "Position-3");
333 Bitmap bmp2 = (Bitmap)Image.FromStream (ms);
334 Assert.IsTrue (ms.Position > 20, "Position-4");
336 Assert.IsTrue (bmp2.RawFormat.Equals (ImageFormat.Bmp), "Bmp");
338 Assert.AreEqual (bmp.GetPixel (0, 0), bmp2.GetPixel (0, 0), "0,0");
339 Assert.AreEqual (bmp.GetPixel (10, 0), bmp2.GetPixel (10, 0), "10,0");
345 public void StreamJunkSaveLoad ()
347 using (MemoryStream ms = new MemoryStream ()) {
351 Assert.AreEqual (2, ms.Position, "Position-1");
353 using (Bitmap bmp = GetBitmap ()) {
354 bmp.Save (ms, ImageFormat.Bmp);
355 Assert.IsTrue (ms.Position > 2, "Position-2");
357 Assert.Throws<ArgumentException> (() => Image.FromStream (ms));
363 public void XmlSerialization ()
365 new XmlSerializer (typeof (Image));