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 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
42 public class GifCodecTest {
44 /* Get suffix to add to the filename */
45 internal string getOutSufix ()
49 int p = (int) Environment.OSVersion.Platform;
50 if ((p == 4) || (p == 128) || (p == 6))
55 if (Type.GetType ("Mono.Runtime", false) == null)
63 /* Get the input directory depending on the runtime*/
64 internal string getInFile (string file)
66 string sRslt = Path.GetFullPath ("../System.Drawing/" + file);
68 if (!File.Exists (sRslt))
69 sRslt = "Test/System.Drawing/" + file;
74 /* Checks bitmap features on a know 1bbp bitmap */
75 /* Checks bitmap features on a know 1bbp bitmap */
76 private void Bitmap8bitsFeatures (string filename)
78 using (Bitmap bmp = new Bitmap (filename)) {
79 GraphicsUnit unit = GraphicsUnit.World;
80 RectangleF rect = bmp.GetBounds (ref unit);
82 Assert.AreEqual (PixelFormat.Format8bppIndexed, bmp.PixelFormat);
83 Assert.AreEqual (110, bmp.Width, "bmp.Width");
84 Assert.AreEqual (100, bmp.Height, "bmp.Height");
86 Assert.AreEqual (0, rect.X, "rect.X");
87 Assert.AreEqual (0, rect.Y, "rect.Y");
88 Assert.AreEqual (110, rect.Width, "rect.Width");
89 Assert.AreEqual (100, rect.Height, "rect.Height");
91 Assert.AreEqual (110, bmp.Size.Width, "bmp.Size.Width");
92 Assert.AreEqual (100, bmp.Size.Height, "bmp.Size.Height");
97 public void Bitmap8bitsFeatures_Gif89 ()
99 Bitmap8bitsFeatures (getInFile ("bitmaps/nature24bits.gif"));
103 public void Bitmap8bitsFeatures_Gif87 ()
105 Bitmap8bitsFeatures (getInFile ("bitmaps/nature24bits87.gif"));
108 private void Bitmap8bitsPixels (string filename)
110 using (Bitmap bmp = new Bitmap (filename)) {
112 for (int x = 0; x < bmp.Width; x += 32) {
113 for (int y = 0; y < bmp.Height; y += 32)
114 Console.WriteLine ("\t\t\t\tAssert.AreEqual ({0}, bmp.GetPixel ({1}, {2}).ToArgb (), \"{1},{2}\");", bmp.GetPixel (x, y).ToArgb (), x, y);
117 // sampling values from a well known bitmap
118 Assert.AreEqual (-10644802, bmp.GetPixel (0, 0).ToArgb (), "0,0");
119 Assert.AreEqual (-12630705, bmp.GetPixel (0, 32).ToArgb (), "0,32");
120 Assert.AreEqual (-14537409, bmp.GetPixel (0, 64).ToArgb (), "0,64");
121 Assert.AreEqual (-14672099, bmp.GetPixel (0, 96).ToArgb (), "0,96");
122 Assert.AreEqual (-526863, bmp.GetPixel (32, 0).ToArgb (), "32,0");
123 Assert.AreEqual (-10263970, bmp.GetPixel (32, 32).ToArgb (), "32,32");
124 Assert.AreEqual (-10461317, bmp.GetPixel (32, 64).ToArgb (), "32,64");
125 Assert.AreEqual (-9722415, bmp.GetPixel (32, 96).ToArgb (), "32,96");
126 Assert.AreEqual (-131076, bmp.GetPixel (64, 0).ToArgb (), "64,0");
127 Assert.AreEqual (-2702435, bmp.GetPixel (64, 32).ToArgb (), "64,32");
128 Assert.AreEqual (-6325922, bmp.GetPixel (64, 64).ToArgb (), "64,64");
129 Assert.AreEqual (-12411924, bmp.GetPixel (64, 96).ToArgb (), "64,96");
130 Assert.AreEqual (-131076, bmp.GetPixel (96, 0).ToArgb (), "96,0");
131 Assert.AreEqual (-7766649, bmp.GetPixel (96, 32).ToArgb (), "96,32");
132 Assert.AreEqual (-11512986, bmp.GetPixel (96, 64).ToArgb (), "96,64");
133 Assert.AreEqual (-12616230, bmp.GetPixel (96, 96).ToArgb (), "96,96");
139 public void Bitmap8bitsPixels_Gif89 ()
141 Bitmap8bitsPixels (getInFile ("bitmaps/nature24bits.gif"));
145 public void Bitmap8bitsPixels_Gif87 ()
147 Bitmap8bitsPixels (getInFile ("bitmaps/nature24bits87.gif"));
151 public void Bitmap8bitsData ()
153 string sInFile = getInFile ("bitmaps/nature24bits.gif");
154 using (Bitmap bmp = new Bitmap (sInFile)) {
155 BitmapData data = bmp.LockBits (new Rectangle (0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
157 Assert.AreEqual (bmp.Height, data.Height, "Height");
158 Assert.AreEqual (bmp.Width, data.Width, "Width");
159 Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
160 Assert.AreEqual (332, data.Stride, "Stride");
161 int size = data.Height * data.Stride;
163 byte* scan = (byte*) data.Scan0;
165 // 1009 is the first prime after 1000 (so we're not affected by a recurring pattern)
166 for (int p = 0; p < size; p += 1009) {
167 Console.WriteLine ("\t\t\t\t\t\tAssert.AreEqual ({0}, *(scan + {1}), \"{1}\");", *(scan + p), p);
170 // sampling values from a well known bitmap
171 Assert.AreEqual (190, *(scan + 0), "0");
172 Assert.AreEqual (217, *(scan + 1009), "1009");
173 Assert.AreEqual (120, *(scan + 2018), "2018");
174 Assert.AreEqual (253, *(scan + 3027), "3027");
175 Assert.AreEqual (233, *(scan + 4036), "4036");
176 Assert.AreEqual (176, *(scan + 5045), "5045");
177 Assert.AreEqual (151, *(scan + 6054), "6054");
178 Assert.AreEqual (220, *(scan + 7063), "7063");
179 Assert.AreEqual (139, *(scan + 8072), "8072");
180 Assert.AreEqual (121, *(scan + 9081), "9081");
181 Assert.AreEqual (160, *(scan + 10090), "10090");
182 Assert.AreEqual (92, *(scan + 11099), "11099");
183 Assert.AreEqual (96, *(scan + 12108), "12108");
184 Assert.AreEqual (64, *(scan + 13117), "13117");
185 Assert.AreEqual (156, *(scan + 14126), "14126");
186 Assert.AreEqual (68, *(scan + 15135), "15135");
187 Assert.AreEqual (156, *(scan + 16144), "16144");
188 Assert.AreEqual (84, *(scan + 17153), "17153");
189 Assert.AreEqual (55, *(scan + 18162), "18162");
190 Assert.AreEqual (68, *(scan + 19171), "19171");
191 Assert.AreEqual (116, *(scan + 20180), "20180");
192 Assert.AreEqual (61, *(scan + 21189), "21189");
193 Assert.AreEqual (69, *(scan + 22198), "22198");
194 Assert.AreEqual (75, *(scan + 23207), "23207");
195 Assert.AreEqual (61, *(scan + 24216), "24216");
196 Assert.AreEqual (66, *(scan + 25225), "25225");
197 Assert.AreEqual (40, *(scan + 26234), "26234");
198 Assert.AreEqual (55, *(scan + 27243), "27243");
199 Assert.AreEqual (53, *(scan + 28252), "28252");
200 Assert.AreEqual (215, *(scan + 29261), "29261");
201 Assert.AreEqual (99, *(scan + 30270), "30270");
202 Assert.AreEqual (67, *(scan + 31279), "31279");
203 Assert.AreEqual (142, *(scan + 32288), "32288");
208 bmp.UnlockBits (data);
214 public void Interlaced ()
216 string sInFile = getInFile ("bitmaps/81773-interlaced.gif");
217 using (Bitmap bmp = new Bitmap (sInFile)) {
218 for (int i = 0; i < 255; i++) {
219 Color c = bmp.GetPixel (0, i);
220 Assert.AreEqual (255, c.A, "A" + i.ToString ());
221 Assert.AreEqual (i, c.R, "R" + i.ToString ());
222 Assert.AreEqual (i, c.G, "G" + i.ToString ());
223 Assert.AreEqual (i, c.B, "B" + i.ToString ());
228 private void Save (PixelFormat original, PixelFormat expected, bool exactColorCheck)
230 string sOutFile = String.Format ("linerect{0}-{1}.gif", getOutSufix (), expected.ToString ());
233 Bitmap bmp = new Bitmap (100, 100, original);
234 Graphics gr = Graphics.FromImage (bmp);
236 using (Pen p = new Pen (Color.Red, 2)) {
237 gr.DrawLine (p, 10.0F, 10.0F, 90.0F, 90.0F);
238 gr.DrawRectangle (p, 10.0F, 10.0F, 80.0F, 80.0F);
242 bmp.Save (sOutFile, ImageFormat.Gif);
245 using (Bitmap bmpLoad = new Bitmap (sOutFile)) {
246 Assert.AreEqual (expected, bmpLoad.PixelFormat, "PixelFormat");
247 Color color = bmpLoad.GetPixel (10, 10);
248 if (exactColorCheck) {
249 Assert.AreEqual (Color.FromArgb (255, 255, 0, 0), color, "Red");
251 // FIXME: we don't save a pure red (F8 instead of FF) into the file so the color-check assert will fail
252 // this is due to libgif's QuantizeBuffer. An alternative would be to make our own that checks if less than 256 colors
253 // are used in the bitmap (or else use QuantizeBuffer).
254 Assert.AreEqual (255, color.A, "A");
255 Assert.IsTrue (color.R >= 248, "R");
256 Assert.AreEqual (0, color.G, "G");
257 Assert.AreEqual (0, color.B, "B");
265 File.Delete (sOutFile);
273 [Category ("NotWorking")]
274 public void Save_24bppRgb ()
276 Save (PixelFormat.Format24bppRgb, PixelFormat.Format8bppIndexed, false);
280 [Category ("NotWorking")]
281 public void Save_32bppRgb ()
283 Save (PixelFormat.Format32bppRgb, PixelFormat.Format8bppIndexed, false);
287 [Category ("NotWorking")]
288 public void Save_32bppArgb ()
290 Save (PixelFormat.Format32bppArgb, PixelFormat.Format8bppIndexed, false);
294 [Category ("NotWorking")]
295 public void Save_32bppPArgb ()
297 Save (PixelFormat.Format32bppPArgb, PixelFormat.Format8bppIndexed, false);
301 [Category ("NotWorking")] // libgdiplus/cairo can't create a bitmap with this format
302 public void Save_48bppRgb ()
304 Save (PixelFormat.Format48bppRgb, PixelFormat.Format8bppIndexed, false);
308 [Category ("NotWorking")] // libgdiplus/cairo can't create a bitmap with this format
309 public void Save_64bppArgb ()
311 Save (PixelFormat.Format64bppArgb, PixelFormat.Format8bppIndexed, false);
315 [Category ("NotWorking")] // libgdiplus/cairo can't create a bitmap with this format
316 public void Save_64bppPArgb ()
318 Save (PixelFormat.Format64bppPArgb, PixelFormat.Format8bppIndexed, false);