public void TestHSIConverter() { var image = Util.ImageFile.LoadBmpImage("..\\..\\..\\TestImages\\lenna.bmp"); image = new HSIConverter(image).Convert().Image; Util.ImageFile.SaveBmpImage(image, "..\\..\\..\\TestImages\\lenna_gray_modified.bmp"); image = Util.ImageFile.LoadBmpImage("..\\..\\..\\TestImages\\lenna_gray_modified.bmp"); // File header Assert.Equal(0x4D42, image.FileHeader.Type); Assert.Equal((uint)(14 + 40 + 256 * 4 + 512 * 512), image.FileHeader.Size); Assert.Equal(0, image.FileHeader.Reserved1); Assert.Equal(0, image.FileHeader.Reserved2); Assert.Equal((uint)(14 + 40 + 256 * 4), image.FileHeader.OffBits); // Info header Assert.Equal(40u, image.InfoHeader.Size); Assert.Equal(512, image.InfoHeader.Width); Assert.Equal(512, image.InfoHeader.Height); Assert.Equal(1u, image.InfoHeader.Planes); Assert.Equal(8u, image.InfoHeader.BitCount); Assert.Equal(0u, image.InfoHeader.Compression); Assert.Equal((uint)(512 * 512), image.InfoHeader.SizeImage); Assert.Equal(0, image.InfoHeader.XPelsPerMeter); Assert.Equal(0, image.InfoHeader.YPelsPerMeter); Assert.Equal(0u, image.InfoHeader.ClrUsed); Assert.Equal(0u, image.InfoHeader.ClrImportanet); // Palette Assert.Equal(256, image.Palette.Length); for (int i = 0; i < image.Palette.Length; i++) { Assert.Equal(i, image.Palette[i].Red); Assert.Equal(i, image.Palette[i].Green); Assert.Equal(i, image.Palette[i].Blue); Assert.Equal(0, image.Palette[i].Flags); } }
private static void ModuleOne(BmpImage image) { /* * 1.检测图像的类型 * (1. gray to binary * (1. danyuzhi * (2. dither * (3. ordered dither * (2. color to gray * (1. RGB -> HSI * (2. RGB -> YCbCr */ bool isGray = true; int photoFormat = CheckPhoto(image); //256 color if (photoFormat == 1) { System.Console.Write("检测到图片为灰度图像:"); System.Console.WriteLine("可进行的操作:\n1.单阈值法\n2.dither\n3.ordered dither\n0.返回"); System.Console.Write("选择的操作是:"); bool inputNum = false; int argument; do { try { int num = Convert.ToInt32(System.Console.ReadLine()); if (num < 4 && num > -1) { inputNum = true; //1.单阈值法 if (num == 1) { System.Console.Write("输入阈值(输入0则为默认值128):"); try { argument = Convert.ToInt32(System.Console.ReadLine()); image = new SingleThresholdConverter(image, argument).Convert().Image; } catch (OverflowException) { System.Console.WriteLine("err:转化的不是一个int型数据"); } catch (FormatException) { System.Console.WriteLine("err:格式错误"); } catch (ArgumentNullException) { System.Console.WriteLine("err:null"); } System.Console.WriteLine("完成单阈值法处理!"); } //2.dither else if (num == 2) { System.Console.Write("输入矩阵参数n(输入0则取默认值N=2):"); try { argument = Convert.ToInt32(System.Console.ReadLine()); image = new DitherConverter(image, argument).Convert().Image; } catch (OverflowException) { System.Console.WriteLine("err:转化的不是一个int型数据"); } catch (FormatException) { System.Console.WriteLine("err:格式错误"); } catch (ArgumentNullException) { System.Console.WriteLine("err:null"); } System.Console.WriteLine("完成dither矩阵处理!"); } //3.ordered dither else if (num == 3) { System.Console.Write("输入矩阵参数n(输入0则取默认值N=2):"); argument = Convert.ToInt32(System.Console.ReadLine()); image = new OrderedDitherConverter(image, argument).Convert().Image; System.Console.WriteLine("完成ordered dither矩阵处理!"); } else { ; } } else { System.Console.Write("输入错误请重新输入:"); } } catch (OverflowException) { System.Console.WriteLine("err:转化的不是一个int型数据"); } catch (FormatException) { System.Console.WriteLine("err:格式错误"); } catch (ArgumentNullException) { System.Console.WriteLine("err:null"); } } while (inputNum == false); } //True color or RGBA else if (photoFormat == 2) { System.Console.Write("检测到图片为真彩图像:"); System.Console.WriteLine("可进行的操作:\n1.RGB->HSI\n2.RGB->YCbCr\n0.返回"); System.Console.Write("选择的操作是:"); bool inputNum = false; do { try { int num = Convert.ToInt32(System.Console.ReadLine()); if (num < 3) { inputNum = true; //1. if (num == 1) { image = new HSIConverter(image).Convert().Image; } //2. if (num == 2) { image = new YCbCrConverter(image).Convert().Image; } else { ; } } else { System.Console.Write("输入错误请重新输入:"); } } catch (OverflowException) { System.Console.WriteLine("err:转化的不是一个int型数据"); } catch (FormatException) { System.Console.WriteLine("err:格式错误"); } catch (ArgumentNullException) { System.Console.WriteLine("err:null"); } } while (inputNum == false); } else { System.Console.Write("\n图片形式不支持模块转换哟~\n"); } }