public void Image_SetGrayScaleToAlpha() { Map m = new Map(256, 256); Image i = new Image(256, 256); m.Background = new Color("white"); m.Render(i); i.SetGrayScaleToAlpha(); Color c = i.GetPixel(0, 0); Assert.AreEqual(c.R, 255); Assert.AreEqual(c.G, 255); Assert.AreEqual(c.B, 255); Assert.AreEqual(c.A, 255); m.Background = new Color("black"); m.Render(i); i.SetGrayScaleToAlpha(); Color c1 = i.GetPixel(0, 0); Assert.AreEqual(c1.R, 255); Assert.AreEqual(c1.G, 255); Assert.AreEqual(c1.B, 255); Assert.AreEqual(c1.A, 0); i.SetGrayScaleToAlpha(new Color("green")); Color c2 = i.GetPixel(0, 0); Assert.AreEqual(c2.R, 0); Assert.AreEqual(c2.G, 128); Assert.AreEqual(c2.B, 0); Assert.AreEqual(c2.A, 255); }
public void Image_GetSetPixel() { Image i = new Image(1, 1); Color c = i.GetPixel(0, 0); Assert.AreEqual(c.ToString(), "rgba(0,0,0,0.0)"); i.SetPixel(0,0, new Color(255, 255, 255)); Color c1 = i.GetPixel(0, 0); Assert.AreEqual(c1.ToString(), "rgb(255,255,255)"); }
public override void Save(Image i, System.IO.Stream dest) { MemoryStream m = new MemoryStream(); m.WriteByte(0); #warning Change this next byte to 255 if you update Image with UInt64 for width and height. m.WriteByte(0); m.WriteByte(0); m.WriteByte(0); // Write the 8 empty bytes at the start of the file. m.WriteByte(0); m.WriteByte(0); m.WriteByte(0); m.WriteByte(0); byte[] dat = BitConverter.GetBytes(i.Height); // Write the height. m.Write(dat, 0, dat.Length); dat = BitConverter.GetBytes(i.Width); // Write the width. m.Write(dat, 0, dat.Length); // Now to write the actual data. Pixel p; for (uint x = 0; x < i.Width; x++) { for (uint y = 0; y < i.Height; y++) { p = i.GetPixel(x, y); m.WriteByte(p.R); m.WriteByte(p.G); m.WriteByte(p.B); m.WriteByte(p.A); } } dat = Orvid.Compression.LZMA.Compress(m.GetBuffer()); dest.WriteByte(255); dest.Write(dat, 0, dat.Length); }
public override Image Load(Stream s) { Tiff tif = Tiff.Open(s); FieldValue[] value = tif.GetField(TiffTag.ImageWidth); int width = value[0].ToInt(); value = tif.GetField(TiffTag.ImageLength); int height = value[0].ToInt(); int[] raster = new int[height * width]; tif.ReadRGBAImage(width, height, raster); Image i = new Image(width, height); int rgba, rasterOffset; byte r, g, b, a; for (uint y = 0; y < i.Height; y++) { rasterOffset = (int)(y * i.Width); for (uint x = 0; x < i.Width; x++) { rgba = raster[rasterOffset++]; r = (byte)(rgba & 0xff); g = (byte)((rgba >> 8) & 0xff); b = (byte)((rgba >> 16) & 0xff); a = (byte)((rgba >> 24) & 0xff); i.SetPixel(x, y, new Pixel(r, g, b, a)); } } // Need to flip the image. Image i2 = new Image(width, height); uint y2 = 0; for (uint y = (uint)(i.Height - 1); y >= 0 && y < i.Height; y--) { for (uint x = 0; x < i.Width; x++) { i2.SetPixel(x, y2, i.GetPixel(x, y)); } y2++; } i.Dispose(); return i2; }
public override void Render(Image im, BoundingBox clip, AffineTransform tx, string text, Vec2 loc, Pixel color) { if (text == null || text.Length == 0) return; int offset = 0, x = 0, y = 0, bdfFontDepth = bdfFont.getDepth(), x_min = int.MaxValue, y_min = int.MaxValue, x_max = int.MinValue, y_max = int.MinValue, charsCount = text.Length, fHeight, scan, fg_r, fg_g, fg_b, bx, by, offsetLine, fPixel, px, py, bg_r, bg_g, bg_b, r, g, b ; int[] fData; Vec2 src, dst; BDFParser.Rectangle glyph_box = new BDFParser.Rectangle(); BDFGlyph glyph; Image img; if ((bdfFont != null) && (charsCount > 0)) { char[] chrs = text.ToCharArray(); int mH = 0; int mY = 0; int mxY = 0; int tW = 0; int tH = 0; for (uint m = 0; m < text.Length; m++) { glyph = bdfFont.getGlyph(chrs[m]); glyph_box = glyph.getBbx(glyph_box); if (glyph_box.height > mH) { mH = glyph_box.height + 2; } if (glyph_box.y < mY) { mY = glyph_box.y; } if (glyph_box.y > mxY) { mxY = glyph_box.y; } tW += glyph_box.width + glyph_box.x + 2; } tH = mH + (mxY - mY); y = mH + -mY; img = new Image(tW, tH); float f_max = (1 << bdfFontDepth) - 1; if (f_max == 0) f_max = 1; glyph_box = new BDFParser.Rectangle(); src = new Vec2(); dst = new Vec2(); for (int i = 0; i < charsCount; i++) { glyph = bdfFont.getGlyph(chrs[i]); if (glyph == null) { continue; } glyph_box = glyph.getBbx(glyph_box); fHeight = glyph_box.height; fData = glyph.getData(); scan = fData.Length / fHeight; fg_r = color.R; fg_g = color.G; fg_b = color.B; //box location bx = x + offset + glyph_box.x; by = y - fHeight - glyph_box.y; for (int k = 0; k < fHeight; k++) { offsetLine = k * scan; for (int j = 0; j < scan; j++) { fPixel = fData[offsetLine + j]; if (fPixel != 0) { //pixel location px = bx + j; py = by + k; if (tx != null) { //src.setLocation(px, py); //tx.transform(src, dst); #warning TODO: Add support for this. //px = dst.X; //py = dst.Y; } //clip if (clip == null || clip.Contains(px, py)) { //compute color Pixel bg_color = img.GetPixel((uint)px, (uint)py); bg_r = bg_color.R; bg_g = bg_color.G; bg_b = bg_color.B; //todo improve this pixel composition float alpha = fPixel / f_max; r = bg_r + ((int)((fg_r - bg_r) * alpha)) & 0xFF; g = bg_g + ((int)((fg_g - bg_g) * alpha)) & 0xFF; b = bg_b + ((int)((fg_b - bg_b) * alpha)) & 0xFF; Pixel nw = new Pixel((byte)r, (byte)g, (byte)b, 255); img.SetPixel((uint)px, (uint)py, nw); if (x_min > px) x_min = px; if (y_min > py) y_min = py; if (x_max < px) x_max = px; if (y_max < py) y_max = py; } } } } offset += glyph.getDWidth().width; } //img = ImageManipulator.Resize(img, new Vec2(img.Width * 8, img.Height * 8), ImageManipulator.ScalingAlgorithm.Bicubic); //img = ImageManipulator.Resize(img, Vec2.Zero, ImageManipulator.ScalingAlgorithm.Hq2x); //img = ImageManipulator.Resize(img, Vec2.Zero, ImageManipulator.ScalingAlgorithm.Hq4x); im.DrawImage(loc, img); } }
static void WriteBitmapData(Stream output, Image input) { int num = 10; int num2 = 5; int num3 = 0; for (int i = 0; i < input.Height; i++) { for (int j = 0; j < input.Width; j++) { Pixel pixel = input.GetPixel((uint)j, (uint)i); ushort num6 = (ushort)(_colorTable8Bit[pixel.B] << (num3 & 0x1f)); ushort num7 = (ushort)(_colorTable8Bit[pixel.G] << (num2 & 0x1f)); ushort num8 = (ushort)(_colorTable8Bit[pixel.R] << (num & 0x1f)); ushort num9 = (ushort)(((0x8000 | num8) | num7) | num6); output.Write(BitConverter.GetBytes(num9), 0, 2); } } }
public override void Update(Image i) { for (ushort x = 0; x < i.Width; x++) { for (ushort y = 0; y < i.Height; y++) { SetPixel(x, y, i.GetPixel(x, y).ToUInt()); } } }
public static void Save(Image img, Stream dest) { WriteASCII("/* XPM */\nstatic char * pixmap[] = {\n", dest); int height = img.Height; int width = img.Width; SortedList<int, Pixel> list = new SortedList<int, Pixel>(); int num3 = 0; for (int i = 0; i < width; i++) { for (int m = 0; m < width; m++) { Pixel pixel = img.GetPixel((uint)i, (uint)m); int key = ColorHash(pixel); if (!list.ContainsKey(key)) { list.Add(key, pixel); num3++; } } } int num7 = 0; int num8 = 0; while (num8 < list.Count) { num8 += 93; num7++; } WriteASCII("/* width height num_colors chars_per_pixel */\n", dest); WriteASCII("\"" + width.ToString() + " " + height.ToString() + " " + num3.ToString() + " " + num7.ToString() + "\",\n", dest); Dictionary<int, string> dictionary = new Dictionary<int, string>(); byte[] ascii = new byte[num7]; for (int j = 0; j < num7; j++) { ascii[j] = 0x20; } ascii[0] = 0x1f; WriteASCII("/* colors */\n", dest); foreach (KeyValuePair<int, Pixel> pair in list) { IncrementChars(ascii); string str2 = Encoding.ASCII.GetString(ascii); Pixel color2 = pair.Value; if (color2.A > 0) { WriteASCII("\"" + str2 + " c #" + color2.R.ToString("x").PadLeft(2, '0') + color2.G.ToString("x").PadLeft(2, '0') + color2.B.ToString("x").PadLeft(2, '0') + "\",\n", dest); } else { WriteASCII("\"" + str2 + " c None\",\n", dest); } dictionary.Add(pair.Key, str2); } WriteASCII("/* pixels */\n", dest); for (int k = 0; k < height; k++) { string str5 = "\""; for (int n = 0; n < width; n++) { string str6; int num13 = ColorHash(img.GetPixel((uint)n, (uint)k)); if (!dictionary.TryGetValue(num13, out str6)) { throw new Exception("Colour missing from the stringList, weird"); } str5 = str5 + str6; } if (k == (height - 1)) { str5 = str5 + "\"\n"; } else { str5 = str5 + "\",\n"; } WriteASCII(str5, dest); } WriteASCII("};\n", dest); }