2 // GIF Codec class testing unit
5 // Jordi Mas i Hernàndez (jordi@ximian.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2006, 2007 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 using System.Drawing.Imaging;
34 using System.Security.Permissions;
36 using NUnit.Framework;
38 namespace MonoTests.System.Drawing.Imaging {
41 public class GifCodecTest {
43 /* Get suffix to add to the filename */
44 internal string getOutSufix ()
48 int p = (int) Environment.OSVersion.Platform;
49 if ((p == 4) || (p == 128) || (p == 6))
54 if (Type.GetType ("Mono.Runtime", false) == null)
62 /* Get the input directory depending on the runtime*/
63 internal string getInFile (string file)
65 string sRslt = Path.GetFullPath ("../System.Drawing/" + file);
67 if (!File.Exists (sRslt))
68 sRslt = "Test/System.Drawing/" + file;
73 /* Checks bitmap features on a know 1bbp bitmap */
74 /* Checks bitmap features on a know 1bbp bitmap */
75 private void Bitmap8bitsFeatures (string filename)
77 using (Bitmap bmp = new Bitmap (filename)) {
78 GraphicsUnit unit = GraphicsUnit.World;
79 RectangleF rect = bmp.GetBounds (ref unit);
81 Assert.AreEqual (PixelFormat.Format8bppIndexed, bmp.PixelFormat);
82 Assert.AreEqual (110, bmp.Width, "bmp.Width");
83 Assert.AreEqual (100, bmp.Height, "bmp.Height");
85 Assert.AreEqual (0, rect.X, "rect.X");
86 Assert.AreEqual (0, rect.Y, "rect.Y");
87 Assert.AreEqual (110, rect.Width, "rect.Width");
88 Assert.AreEqual (100, rect.Height, "rect.Height");
90 Assert.AreEqual (110, bmp.Size.Width, "bmp.Size.Width");
91 Assert.AreEqual (100, bmp.Size.Height, "bmp.Size.Height");
96 public void Bitmap8bitsFeatures_Gif89 ()
98 Bitmap8bitsFeatures (getInFile ("bitmaps/nature24bits.gif"));
102 public void Bitmap8bitsFeatures_Gif87 ()
104 Bitmap8bitsFeatures (getInFile ("bitmaps/nature24bits87.gif"));
107 private void Bitmap8bitsPixels (string filename)
109 using (Bitmap bmp = new Bitmap (filename)) {
111 for (int x = 0; x < bmp.Width; x += 32) {
112 for (int y = 0; y < bmp.Height; y += 32)
113 Console.WriteLine ("\t\t\t\tAssert.AreEqual ({0}, bmp.GetPixel ({1}, {2}).ToArgb (), \"{1},{2}\");", bmp.GetPixel (x, y).ToArgb (), x, y);
116 // sampling values from a well known bitmap
117 Assert.AreEqual (-10644802, bmp.GetPixel (0, 0).ToArgb (), "0,0");
118 Assert.AreEqual (-12630705, bmp.GetPixel (0, 32).ToArgb (), "0,32");
119 Assert.AreEqual (-14537409, bmp.GetPixel (0, 64).ToArgb (), "0,64");
120 Assert.AreEqual (-14672099, bmp.GetPixel (0, 96).ToArgb (), "0,96");
121 Assert.AreEqual (-526863, bmp.GetPixel (32, 0).ToArgb (), "32,0");
122 Assert.AreEqual (-10263970, bmp.GetPixel (32, 32).ToArgb (), "32,32");
123 Assert.AreEqual (-10461317, bmp.GetPixel (32, 64).ToArgb (), "32,64");
124 Assert.AreEqual (-9722415, bmp.GetPixel (32, 96).ToArgb (), "32,96");
125 Assert.AreEqual (-131076, bmp.GetPixel (64, 0).ToArgb (), "64,0");
126 Assert.AreEqual (-2702435, bmp.GetPixel (64, 32).ToArgb (), "64,32");
127 Assert.AreEqual (-6325922, bmp.GetPixel (64, 64).ToArgb (), "64,64");
128 Assert.AreEqual (-12411924, bmp.GetPixel (64, 96).ToArgb (), "64,96");
129 Assert.AreEqual (-131076, bmp.GetPixel (96, 0).ToArgb (), "96,0");
130 Assert.AreEqual (-7766649, bmp.GetPixel (96, 32).ToArgb (), "96,32");
131 Assert.AreEqual (-11512986, bmp.GetPixel (96, 64).ToArgb (), "96,64");
132 Assert.AreEqual (-12616230, bmp.GetPixel (96, 96).ToArgb (), "96,96");
138 public void Bitmap8bitsPixels_Gif89 ()
140 Bitmap8bitsPixels (getInFile ("bitmaps/nature24bits.gif"));
144 public void Bitmap8bitsPixels_Gif87 ()
146 Bitmap8bitsPixels (getInFile ("bitmaps/nature24bits87.gif"));
150 public void Bitmap8bitsData ()
152 string sInFile = getInFile ("bitmaps/nature24bits.gif");
153 using (Bitmap bmp = new Bitmap (sInFile)) {
154 BitmapData data = bmp.LockBits (new Rectangle (0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
156 Assert.AreEqual (bmp.Height, data.Height, "Height");
157 Assert.AreEqual (bmp.Width, data.Width, "Width");
158 Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
159 Assert.AreEqual (332, data.Stride, "Stride");
160 int size = data.Height * data.Stride;
162 byte* scan = (byte*) data.Scan0;
164 // 1009 is the first prime after 1000 (so we're not affected by a recurring pattern)
165 for (int p = 0; p < size; p += 1009) {
166 Console.WriteLine ("\t\t\t\t\t\tAssert.AreEqual ({0}, *(scan + {1}), \"{1}\");", *(scan + p), p);
169 // sampling values from a well known bitmap
170 Assert.AreEqual (190, *(scan + 0), "0");
171 Assert.AreEqual (217, *(scan + 1009), "1009");
172 Assert.AreEqual (120, *(scan + 2018), "2018");
173 Assert.AreEqual (253, *(scan + 3027), "3027");
174 Assert.AreEqual (233, *(scan + 4036), "4036");
175 Assert.AreEqual (176, *(scan + 5045), "5045");
176 Assert.AreEqual (151, *(scan + 6054), "6054");
177 Assert.AreEqual (220, *(scan + 7063), "7063");
178 Assert.AreEqual (139, *(scan + 8072), "8072");
179 Assert.AreEqual (121, *(scan + 9081), "9081");
180 Assert.AreEqual (160, *(scan + 10090), "10090");
181 Assert.AreEqual (92, *(scan + 11099), "11099");
182 Assert.AreEqual (96, *(scan + 12108), "12108");
183 Assert.AreEqual (64, *(scan + 13117), "13117");
184 Assert.AreEqual (156, *(scan + 14126), "14126");
185 Assert.AreEqual (68, *(scan + 15135), "15135");
186 Assert.AreEqual (156, *(scan + 16144), "16144");
187 Assert.AreEqual (84, *(scan + 17153), "17153");
188 Assert.AreEqual (55, *(scan + 18162), "18162");
189 Assert.AreEqual (68, *(scan + 19171), "19171");
190 Assert.AreEqual (116, *(scan + 20180), "20180");
191 Assert.AreEqual (61, *(scan + 21189), "21189");
192 Assert.AreEqual (69, *(scan + 22198), "22198");
193 Assert.AreEqual (75, *(scan + 23207), "23207");
194 Assert.AreEqual (61, *(scan + 24216), "24216");
195 Assert.AreEqual (66, *(scan + 25225), "25225");
196 Assert.AreEqual (40, *(scan + 26234), "26234");
197 Assert.AreEqual (55, *(scan + 27243), "27243");
198 Assert.AreEqual (53, *(scan + 28252), "28252");
199 Assert.AreEqual (215, *(scan + 29261), "29261");
200 Assert.AreEqual (99, *(scan + 30270), "30270");
201 Assert.AreEqual (67, *(scan + 31279), "31279");
202 Assert.AreEqual (142, *(scan + 32288), "32288");
207 bmp.UnlockBits (data);
213 public void Interlaced ()
215 string sInFile = getInFile ("bitmaps/81773-interlaced.gif");
216 using (Bitmap bmp = new Bitmap (sInFile)) {
217 for (int i = 0; i < 255; i++) {
218 Color c = bmp.GetPixel (0, i);
219 Assert.AreEqual (255, c.A, "A" + i.ToString ());
220 Assert.AreEqual (i, c.R, "R" + i.ToString ());
221 Assert.AreEqual (i, c.G, "G" + i.ToString ());
222 Assert.AreEqual (i, c.B, "B" + i.ToString ());
227 private void Save (PixelFormat original, PixelFormat expected, bool exactColorCheck)
229 string sOutFile = String.Format ("linerect{0}-{1}.gif", getOutSufix (), expected.ToString ());
232 Bitmap bmp = new Bitmap (100, 100, original);
233 Graphics gr = Graphics.FromImage (bmp);
235 using (Pen p = new Pen (Color.Red, 2)) {
236 gr.DrawLine (p, 10.0F, 10.0F, 90.0F, 90.0F);
237 gr.DrawRectangle (p, 10.0F, 10.0F, 80.0F, 80.0F);
241 bmp.Save (sOutFile, ImageFormat.Gif);
244 using (Bitmap bmpLoad = new Bitmap (sOutFile)) {
245 Assert.AreEqual (expected, bmpLoad.PixelFormat, "PixelFormat");
246 Color color = bmpLoad.GetPixel (10, 10);
247 if (exactColorCheck) {
248 Assert.AreEqual (Color.FromArgb (255, 255, 0, 0), color, "Red");
250 // FIXME: we don't save a pure red (F8 instead of FF) into the file so the color-check assert will fail
251 // this is due to libgif's QuantizeBuffer. An alternative would be to make our own that checks if less than 256 colors
252 // are used in the bitmap (or else use QuantizeBuffer).
253 Assert.AreEqual (255, color.A, "A");
254 Assert.IsTrue (color.R >= 248, "R");
255 Assert.AreEqual (0, color.G, "G");
256 Assert.AreEqual (0, color.B, "B");
264 File.Delete (sOutFile);
272 public void Save_24bppRgb ()
274 Save (PixelFormat.Format24bppRgb, PixelFormat.Format8bppIndexed, false);
278 public void Save_32bppRgb ()
280 Save (PixelFormat.Format32bppRgb, PixelFormat.Format8bppIndexed, false);
284 public void Save_32bppArgb ()
286 Save (PixelFormat.Format32bppArgb, PixelFormat.Format8bppIndexed, false);
290 public void Save_32bppPArgb ()
292 Save (PixelFormat.Format32bppPArgb, PixelFormat.Format8bppIndexed, false);
296 [Category ("NotWorking")] // libgdiplus/cairo can't create a bitmap with this format
297 public void Save_48bppRgb ()
299 Save (PixelFormat.Format48bppRgb, PixelFormat.Format8bppIndexed, false);
303 [Category ("NotWorking")] // libgdiplus/cairo can't create a bitmap with this format
304 public void Save_64bppArgb ()
306 Save (PixelFormat.Format64bppArgb, PixelFormat.Format8bppIndexed, false);
310 [Category ("NotWorking")] // libgdiplus/cairo can't create a bitmap with this format
311 public void Save_64bppPArgb ()
313 Save (PixelFormat.Format64bppPArgb, PixelFormat.Format8bppIndexed, false);