2 // JpegCodec class testing unit
5 // Jordi Mas i Hernàndez (jordi@ximian.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2004 Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2004-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;
34 using NUnit.Framework;
36 using System.Security.Permissions;
38 namespace MonoTests.System.Drawing.Imaging {
41 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
42 public class JpegCodecTest {
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;
78 public void Bitmap8bbpIndexedGreyscaleFeatures ()
80 string sInFile = getInFile ("bitmaps/nature-greyscale.jpg");
81 using (Bitmap bmp = new Bitmap (sInFile)) {
82 GraphicsUnit unit = GraphicsUnit.World;
83 RectangleF rect = bmp.GetBounds (ref unit);
85 Assert.AreEqual (PixelFormat.Format8bppIndexed, bmp.PixelFormat, "PixelFormat");
86 Assert.AreEqual (110, bmp.Width, "bmp.Width");
87 Assert.AreEqual (100, bmp.Height, "bmp.Height");
89 Assert.AreEqual (0, rect.X, "rect.X");
90 Assert.AreEqual (0, rect.Y, "rect.Y");
91 Assert.AreEqual (110, rect.Width, "rect.Width");
92 Assert.AreEqual (100, rect.Height, "rect.Height");
94 Assert.AreEqual (110, bmp.Size.Width, "bmp.Size.Width");
95 Assert.AreEqual (100, bmp.Size.Height, "bmp.Size.Height");
97 Assert.AreEqual (110, bmp.PhysicalDimension.Width, "bmp.PhysicalDimension.Width");
98 Assert.AreEqual (100, bmp.PhysicalDimension.Height, "bmp.PhysicalDimension.Height");
100 Assert.AreEqual (72, bmp.HorizontalResolution, "HorizontalResolution");
101 Assert.AreEqual (72, bmp.VerticalResolution, "VerticalResolution");
103 Assert.AreEqual (77896, bmp.Flags, "Flags");
105 ColorPalette cp = bmp.Palette;
106 Assert.AreEqual (256, cp.Entries.Length, "Palette.Entries");
107 Assert.AreEqual (0, cp.Flags, "Palette.Flags");
108 for (int i = 0; i < 256; i++) {
109 Color c = cp.Entries [i];
110 Assert.AreEqual (0xFF, c.A, "A" + i.ToString ());
111 Assert.AreEqual (i, c.R, "R" + i.ToString ());
112 Assert.AreEqual (i, c.G, "G" + i.ToString ());
113 Assert.AreEqual (i, c.B, "B" + i.ToString ());
120 [Category ("NotWorking")]
122 public void Bitmap8bbpIndexedGreyscalePixels ()
124 string sInFile = getInFile ("bitmaps/nature-greyscale.jpg");
125 using (Bitmap bmp = new Bitmap (sInFile)) {
127 for (int x = 0; x < bmp.Width; x += 32) {
128 for (int y = 0; y < bmp.Height; y += 32)
129 Console.WriteLine ("\t\t\t\tAssert.AreEqual ({0}, bmp.GetPixel ({1}, {2}).ToArgb (), \"{1},{2}\");", bmp.GetPixel (x, y).ToArgb (), x, y);
132 // sampling values from a well known bitmap
133 Assert.AreEqual (-7697782, bmp.GetPixel (0, 0).ToArgb (), "0,0");
134 Assert.AreEqual (-12171706, bmp.GetPixel (0, 32).ToArgb (), "0,32");
135 Assert.AreEqual (-14013910, bmp.GetPixel (0, 64).ToArgb (), "0,64");
136 Assert.AreEqual (-15132391, bmp.GetPixel (0, 96).ToArgb (), "0,96");
137 Assert.AreEqual (-328966, bmp.GetPixel (32, 0).ToArgb (), "32,0");
138 Assert.AreEqual (-9934744, bmp.GetPixel (32, 32).ToArgb (), "32,32");
139 Assert.AreEqual (-10263709, bmp.GetPixel (32, 64).ToArgb (), "32,64");
140 Assert.AreEqual (-7368817, bmp.GetPixel (32, 96).ToArgb (), "32,96");
141 Assert.AreEqual (-1, bmp.GetPixel (64, 0).ToArgb (), "64,0");
142 Assert.AreEqual (-4276546, bmp.GetPixel (64, 32).ToArgb (), "64,32");
143 Assert.AreEqual (-9079435, bmp.GetPixel (64, 64).ToArgb (), "64,64");
144 Assert.AreEqual (-7697782, bmp.GetPixel (64, 96).ToArgb (), "64,96");
145 Assert.AreEqual (-1, bmp.GetPixel (96, 0).ToArgb (), "96,0");
146 Assert.AreEqual (-8224126, bmp.GetPixel (96, 32).ToArgb (), "96,32");
147 Assert.AreEqual (-11053225, bmp.GetPixel (96, 64).ToArgb (), "96,64");
148 Assert.AreEqual (-9211021, bmp.GetPixel (96, 96).ToArgb (), "96,96");
155 public void Bitmap8bbpIndexedGreyscaleData ()
157 string sInFile = getInFile ("bitmaps/nature-greyscale.jpg");
158 using (Bitmap bmp = new Bitmap (sInFile)) {
159 BitmapData data = bmp.LockBits (new Rectangle (0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
161 Assert.AreEqual (bmp.Height, data.Height, "Height");
162 Assert.AreEqual (bmp.Width, data.Width, "Width");
163 Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
164 int size = data.Height * data.Stride;
166 byte* scan = (byte*) data.Scan0;
168 // 1009 is the first prime after 1000 (so we're not affected by a recurring pattern)
169 for (int p = 0; p < size; p += 1009) {
170 Console.WriteLine ("\t\t\t\t\t\tAssert.AreEqual ({0}, *(scan + {1}), \"{1}\");", *(scan + p), p);
173 // sampling values from a well known bitmap
174 Assert.AreEqual (138, *(scan + 0), "0");
175 Assert.AreEqual (203, *(scan + 1009), "1009");
176 Assert.AreEqual (156, *(scan + 2018), "2018");
177 Assert.AreEqual (248, *(scan + 3027), "3027");
178 Assert.AreEqual (221, *(scan + 4036), "4036");
179 Assert.AreEqual (185, *(scan + 5045), "5045");
180 Assert.AreEqual (128, *(scan + 6054), "6054");
181 Assert.AreEqual (205, *(scan + 7063), "7063");
182 Assert.AreEqual (153, *(scan + 8072), "8072");
183 Assert.AreEqual (110, *(scan + 9081), "9081");
184 Assert.AreEqual (163, *(scan + 10090), "10090");
185 Assert.AreEqual (87, *(scan + 11099), "11099");
186 Assert.AreEqual (90, *(scan + 12108), "12108");
187 Assert.AreEqual (81, *(scan + 13117), "13117");
188 Assert.AreEqual (123, *(scan + 14126), "14126");
189 Assert.AreEqual (99, *(scan + 15135), "15135");
190 Assert.AreEqual (153, *(scan + 16144), "16144");
191 Assert.AreEqual (57, *(scan + 17153), "17153");
192 Assert.AreEqual (89, *(scan + 18162), "18162");
193 Assert.AreEqual (71, *(scan + 19171), "19171");
194 Assert.AreEqual (106, *(scan + 20180), "20180");
195 Assert.AreEqual (55, *(scan + 21189), "21189");
196 Assert.AreEqual (75, *(scan + 22198), "22198");
197 Assert.AreEqual (77, *(scan + 23207), "23207");
198 Assert.AreEqual (58, *(scan + 24216), "24216");
199 Assert.AreEqual (69, *(scan + 25225), "25225");
200 Assert.AreEqual (43, *(scan + 26234), "26234");
201 Assert.AreEqual (55, *(scan + 27243), "27243");
202 Assert.AreEqual (74, *(scan + 28252), "28252");
203 Assert.AreEqual (145, *(scan + 29261), "29261");
204 Assert.AreEqual (87, *(scan + 30270), "30270");
205 Assert.AreEqual (85, *(scan + 31279), "31279");
206 Assert.AreEqual (106, *(scan + 32288), "32288");
211 bmp.UnlockBits (data);
217 /* Checks bitmap features on a known 24-bits bitmap */
220 [Category("NotWorking")]
222 public void Bitmap24bitFeatures ()
224 string sInFile = getInFile ("bitmaps/nature24bits.jpg");
225 using (Bitmap bmp = new Bitmap (sInFile)) {
226 GraphicsUnit unit = GraphicsUnit.World;
227 RectangleF rect = bmp.GetBounds (ref unit);
229 Assert.AreEqual (PixelFormat.Format24bppRgb, bmp.PixelFormat, "PixelFormat");
230 Assert.AreEqual (110, bmp.Width, "bmp.Width");
231 Assert.AreEqual (100, bmp.Height, "bmp.Height");
233 Assert.AreEqual (0, rect.X, "rect.X");
234 Assert.AreEqual (0, rect.Y, "rect.Y");
235 Assert.AreEqual (110, rect.Width, "rect.Width");
236 Assert.AreEqual (100, rect.Height, "rect.Height");
238 Assert.AreEqual (110, bmp.Size.Width, "bmp.Size.Width");
239 Assert.AreEqual (100, bmp.Size.Height, "bmp.Size.Height");
241 Assert.AreEqual (110, bmp.PhysicalDimension.Width, "bmp.PhysicalDimension.Width");
242 Assert.AreEqual (100, bmp.PhysicalDimension.Height, "bmp.PhysicalDimension.Height");
244 Assert.AreEqual (72, bmp.HorizontalResolution, "HorizontalResolution");
245 Assert.AreEqual (72, bmp.VerticalResolution, "VerticalResolution");
247 Assert.AreEqual (77960, bmp.Flags, "Flags");
249 Assert.AreEqual (0, bmp.Palette.Entries.Length, "Palette.Entries");
250 /* note: under MS flags aren't constant between executions in this case (no palette) */
255 public void Bitmap24bitPixels ()
257 string sInFile = getInFile ("bitmaps/nature24bits.jpg");
258 using (Bitmap bmp = new Bitmap (sInFile)) {
260 for (int x = 0; x < bmp.Width; x += 32) {
261 for (int y = 0; y < bmp.Height; y += 32)
262 Console.WriteLine ("\t\t\t\tAssert.AreEqual ({0}, bmp.GetPixel ({1}, {2}).ToArgb (), \"{1},{2}\");", bmp.GetPixel (x, y).ToArgb (), x, y);
265 // sampling values from a well known bitmap
266 Assert.AreEqual (-10447423, bmp.GetPixel (0, 0).ToArgb (), "0,0");
267 Assert.AreEqual (-12171958, bmp.GetPixel (0, 32).ToArgb (), "0,32");
268 Assert.AreEqual (-15192259, bmp.GetPixel (0, 64).ToArgb (), "0,64");
269 Assert.AreEqual (-15131110, bmp.GetPixel (0, 96).ToArgb (), "0,96");
270 Assert.AreEqual (-395272, bmp.GetPixel (32, 0).ToArgb (), "32,0");
271 Assert.AreEqual (-10131359, bmp.GetPixel (32, 32).ToArgb (), "32,32");
272 Assert.AreEqual (-10984322, bmp.GetPixel (32, 64).ToArgb (), "32,64");
273 Assert.AreEqual (-11034683, bmp.GetPixel (32, 96).ToArgb (), "32,96");
274 Assert.AreEqual (-1, bmp.GetPixel (64, 0).ToArgb (), "64,0");
275 Assert.AreEqual (-3163242, bmp.GetPixel (64, 32).ToArgb (), "64,32");
276 Assert.AreEqual (-7311538, bmp.GetPixel (64, 64).ToArgb (), "64,64");
277 Assert.AreEqual (-12149780, bmp.GetPixel (64, 96).ToArgb (), "64,96");
278 Assert.AreEqual (-1, bmp.GetPixel (96, 0).ToArgb (), "96,0");
279 Assert.AreEqual (-8224378, bmp.GetPixel (96, 32).ToArgb (), "96,32");
280 Assert.AreEqual (-11053718, bmp.GetPixel (96, 64).ToArgb (), "96,64");
281 Assert.AreEqual (-12944166, bmp.GetPixel (96, 96).ToArgb (), "96,96");
288 public void Bitmap24bitData ()
290 string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
291 using (Bitmap bmp = new Bitmap (sInFile)) {
292 BitmapData data = bmp.LockBits (new Rectangle (0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
294 Assert.AreEqual (bmp.Height, data.Height, "Height");
295 Assert.AreEqual (bmp.Width, data.Width, "Width");
296 Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
297 Assert.AreEqual (520, data.Stride, "Stride");
298 int size = data.Height * data.Stride;
300 byte* scan = (byte*) data.Scan0;
302 // 1009 is the first prime after 1000 (so we're not affected by a recurring pattern)
303 for (int p = 0; p < size; p += 1009) {
304 Console.WriteLine ("\t\t\t\t\t\tAssert.AreEqual ({0}, *(scan + {1}), \"{1}\");", *(scan + p), p);
307 // sampling values from a well known bitmap
308 Assert.AreEqual (217, *(scan + 0), "0");
309 Assert.AreEqual (192, *(scan + 1009), "1009");
310 Assert.AreEqual (210, *(scan + 2018), "2018");
311 Assert.AreEqual (196, *(scan + 3027), "3027");
312 Assert.AreEqual (216, *(scan + 4036), "4036");
313 Assert.AreEqual (215, *(scan + 5045), "5045");
314 Assert.AreEqual (218, *(scan + 6054), "6054");
315 Assert.AreEqual (218, *(scan + 7063), "7063");
316 Assert.AreEqual (95, *(scan + 8072), "8072");
317 Assert.AreEqual (9, *(scan + 9081), "9081");
318 Assert.AreEqual (247, *(scan + 10090), "10090");
319 Assert.AreEqual (161, *(scan + 11099), "11099");
320 Assert.AreEqual (130, *(scan + 12108), "12108");
321 Assert.AreEqual (131, *(scan + 13117), "13117");
322 Assert.AreEqual (175, *(scan + 14126), "14126");
323 Assert.AreEqual (217, *(scan + 15135), "15135");
324 Assert.AreEqual (201, *(scan + 16144), "16144");
325 Assert.AreEqual (183, *(scan + 17153), "17153");
326 Assert.AreEqual (236, *(scan + 18162), "18162");
327 Assert.AreEqual (242, *(scan + 19171), "19171");
328 Assert.AreEqual (125, *(scan + 20180), "20180");
329 Assert.AreEqual (193, *(scan + 21189), "21189");
330 Assert.AreEqual (227, *(scan + 22198), "22198");
331 Assert.AreEqual (44, *(scan + 23207), "23207");
332 Assert.AreEqual (230, *(scan + 24216), "24216");
333 Assert.AreEqual (224, *(scan + 25225), "25225");
334 Assert.AreEqual (164, *(scan + 26234), "26234");
335 Assert.AreEqual (43, *(scan + 27243), "27243");
336 Assert.AreEqual (200, *(scan + 28252), "28252");
337 Assert.AreEqual (255, *(scan + 29261), "29261");
338 Assert.AreEqual (226, *(scan + 30270), "30270");
339 Assert.AreEqual (230, *(scan + 31279), "31279");
340 Assert.AreEqual (178, *(scan + 32288), "32288");
341 Assert.AreEqual (224, *(scan + 33297), "33297");
342 Assert.AreEqual (233, *(scan + 34306), "34306");
343 Assert.AreEqual (212, *(scan + 35315), "35315");
344 Assert.AreEqual (153, *(scan + 36324), "36324");
345 Assert.AreEqual (143, *(scan + 37333), "37333");
346 Assert.AreEqual (215, *(scan + 38342), "38342");
347 Assert.AreEqual (116, *(scan + 39351), "39351");
348 Assert.AreEqual (26, *(scan + 40360), "40360");
349 Assert.AreEqual (28, *(scan + 41369), "41369");
350 Assert.AreEqual (75, *(scan + 42378), "42378");
351 Assert.AreEqual (50, *(scan + 43387), "43387");
352 Assert.AreEqual (244, *(scan + 44396), "44396");
353 Assert.AreEqual (191, *(scan + 45405), "45405");
354 Assert.AreEqual (200, *(scan + 46414), "46414");
355 Assert.AreEqual (197, *(scan + 47423), "47423");
356 Assert.AreEqual (232, *(scan + 48432), "48432");
357 Assert.AreEqual (186, *(scan + 49441), "49441");
358 Assert.AreEqual (210, *(scan + 50450), "50450");
359 Assert.AreEqual (215, *(scan + 51459), "51459");
360 Assert.AreEqual (155, *(scan + 52468), "52468");
361 Assert.AreEqual (56, *(scan + 53477), "53477");
362 Assert.AreEqual (149, *(scan + 54486), "54486");
363 Assert.AreEqual (137, *(scan + 55495), "55495");
364 Assert.AreEqual (141, *(scan + 56504), "56504");
365 Assert.AreEqual (36, *(scan + 57513), "57513");
366 Assert.AreEqual (39, *(scan + 58522), "58522");
367 Assert.AreEqual (25, *(scan + 59531), "59531");
368 Assert.AreEqual (44, *(scan + 60540), "60540");
369 Assert.AreEqual (12, *(scan + 61549), "61549");
370 Assert.AreEqual (161, *(scan + 62558), "62558");
371 Assert.AreEqual (179, *(scan + 63567), "63567");
372 Assert.AreEqual (181, *(scan + 64576), "64576");
373 Assert.AreEqual (165, *(scan + 65585), "65585");
374 Assert.AreEqual (182, *(scan + 66594), "66594");
375 Assert.AreEqual (186, *(scan + 67603), "67603");
376 Assert.AreEqual (201, *(scan + 68612), "68612");
377 Assert.AreEqual (49, *(scan + 69621), "69621");
378 Assert.AreEqual (161, *(scan + 70630), "70630");
379 Assert.AreEqual (140, *(scan + 71639), "71639");
380 Assert.AreEqual (2, *(scan + 72648), "72648");
381 Assert.AreEqual (15, *(scan + 73657), "73657");
382 Assert.AreEqual (33, *(scan + 74666), "74666");
383 Assert.AreEqual (17, *(scan + 75675), "75675");
384 Assert.AreEqual (0, *(scan + 76684), "76684");
385 Assert.AreEqual (47, *(scan + 77693), "77693");
386 Assert.AreEqual (4, *(scan + 78702), "78702");
387 Assert.AreEqual (142, *(scan + 79711), "79711");
388 Assert.AreEqual (151, *(scan + 80720), "80720");
389 Assert.AreEqual (124, *(scan + 81729), "81729");
390 Assert.AreEqual (81, *(scan + 82738), "82738");
391 Assert.AreEqual (214, *(scan + 83747), "83747");
392 Assert.AreEqual (217, *(scan + 84756), "84756");
393 Assert.AreEqual (30, *(scan + 85765), "85765");
394 Assert.AreEqual (185, *(scan + 86774), "86774");
395 Assert.AreEqual (200, *(scan + 87783), "87783");
396 Assert.AreEqual (37, *(scan + 88792), "88792");
397 Assert.AreEqual (2, *(scan + 89801), "89801");
398 Assert.AreEqual (41, *(scan + 90810), "90810");
399 Assert.AreEqual (16, *(scan + 91819), "91819");
400 Assert.AreEqual (0, *(scan + 92828), "92828");
401 Assert.AreEqual (146, *(scan + 93837), "93837");
402 Assert.AreEqual (163, *(scan + 94846), "94846");
407 bmp.UnlockBits (data);
413 private void Save (PixelFormat original, PixelFormat expected)
415 string sOutFile = String.Format ("linerect{0}-{1}.jpeg", getOutSufix (), expected.ToString ());
418 Bitmap bmp = new Bitmap (100, 100, original);
419 Graphics gr = Graphics.FromImage (bmp);
421 using (Pen p = new Pen (Color.Red, 2)) {
422 gr.DrawLine (p, 10.0F, 10.0F, 90.0F, 90.0F);
423 gr.DrawRectangle (p, 10.0F, 10.0F, 80.0F, 80.0F);
427 bmp.Save (sOutFile, ImageFormat.Jpeg);
430 using (Bitmap bmpLoad = new Bitmap (sOutFile)) {
431 Assert.AreEqual (expected, bmpLoad.PixelFormat, "PixelFormat");
432 Color color = bmpLoad.GetPixel (10, 10);
433 // by default JPEG isn't lossless - so value is "near" read
434 Assert.IsTrue (color.R >= 200, "Red");
435 Assert.IsTrue (color.G < 60, "Green");
436 Assert.IsTrue (color.B < 60, "Blue");
437 Assert.AreEqual (0xFF, color.A, "Alpha");
444 File.Delete (sOutFile);
453 [Category("NotWorking")]
455 public void Save_24bppRgb ()
457 Save (PixelFormat.Format24bppRgb, PixelFormat.Format24bppRgb);
462 [Category("NotWorking")]
464 public void Save_32bppRgb ()
466 Save (PixelFormat.Format32bppRgb, PixelFormat.Format24bppRgb);
471 [Category("NotWorking")]
473 public void Save_32bppArgb ()
475 Save (PixelFormat.Format32bppArgb, PixelFormat.Format24bppRgb);
480 [Category("NotWorking")]
482 public void Save_32bppPArgb ()
484 Save (PixelFormat.Format32bppPArgb, PixelFormat.Format24bppRgb);
488 [Category ("NotWorking")]
489 public void Save_48bppRgb ()
491 Save (PixelFormat.Format48bppRgb, PixelFormat.Format24bppRgb);
495 [Category ("NotWorking")]
496 public void Save_64bppArgb ()
498 Save (PixelFormat.Format64bppArgb, PixelFormat.Format24bppRgb);
502 [Category ("NotWorking")]
503 public void Save_64bppPArgb ()
505 Save (PixelFormat.Format64bppPArgb, PixelFormat.Format24bppRgb);