private FastBitmap _GetGrayImage(FastBitmap image) { FastBitmap res = new FastBitmap(image.GetBitmap()); for (int i = 0; i < res.Width; ++i) { for (int j = 0; j < res.Height; ++j) { Color pixel = image.GetPixel(i, j); byte I = (byte)((pixel.R * 0.299) + (pixel.G * 0.587) + (pixel.B * 0.114)); res.SetPixel(i, j, Color.FromArgb(pixel.A, I, I, I)); } } return(res); }
public void DrawObjects() { FastBitmap bitmap = new FastBitmap(new Bitmap(Camera.Width, Camera.Height)); Matrix cameraMatrix = Camera.GetCameraMatrix(); // z-buffer (z-matrix). For each camera viewport pixel. // first, fill it with double.MaxValue with future z-buffer algoritm in mind. double[,] buffer = new double[Camera.Width, Camera.Height]; for (int i = 0; i < Camera.Width; ++i) { for (int j = 0; j < Camera.Height; ++j) { buffer[i, j] = double.MaxValue; } } if (IsPanning) { // defining light Matrix lightMatrix = ModificationMatrix.NewIdentityMatrix(4); var light = Light; { Point3D lightPosition = new Point3D(light); } } if (SelectedForPanObject != null) { DrawObject(SelectedForPanObject, bitmap, buffer, cameraMatrix); } else { for (int i = 0; i < Objects.Count; ++i) { SceneObject radioObject = Objects[i]; if (IsVisibleForCamera(radioObject.BasePoint, radioObject.MaxLength, cameraMatrix)) { DrawObject(radioObject, bitmap, buffer, cameraMatrix); } } } canvas.Image = bitmap.GetBitmap(); }
private static Bitmap CropBitmapAndUnlok(FastBitmap bmp, Color backgroundColor) { int y = 0; int x; Color c = backgroundColor; int backgroundArgb = backgroundColor.ToArgb(); // Crop top while (y < bmp.Height && IsBackgroundColor(c, backgroundArgb)) { c = bmp.GetPixel(0, y); if (IsBackgroundColor(c, backgroundArgb)) { for (x = 1; x < bmp.Width; x++) { c = bmp.GetPixelNext(); if (c.A != 0 && c.ToArgb() != backgroundArgb) { break; } } } if (IsBackgroundColor(c, backgroundArgb)) { y++; } } int minY = y; if (minY > 3) { minY -= 3; } else { minY = 0; } // Crop left x = 0; c = backgroundColor; while (x < bmp.Width && IsBackgroundColor(c, backgroundArgb)) { for (y = minY; y < bmp.Height; y++) { c = bmp.GetPixel(x, y); if (!IsBackgroundColor(c, backgroundArgb)) { break; } } if (IsBackgroundColor(c, backgroundArgb)) { x++; } } int minX = x; if (minX > 3) { minX -= 3; } else { minX -= 0; } // Crop bottom y = bmp.Height - 1; c = backgroundColor; while (y > minY && IsBackgroundColor(c, backgroundArgb)) { c = bmp.GetPixel(0, y); if (IsBackgroundColor(c, backgroundArgb)) { for (x = 1; x < bmp.Width; x++) { c = bmp.GetPixelNext(); if (!IsBackgroundColor(c, backgroundArgb)) { break; } } } if (IsBackgroundColor(c, backgroundArgb)) { y--; } } int maxY = y + 7; if (maxY >= bmp.Height) { maxY = bmp.Height - 1; } // Crop right x = bmp.Width - 1; c = backgroundColor; while (x > minX && IsBackgroundColor(c, backgroundArgb)) { for (y = minY; y < bmp.Height; y++) { c = bmp.GetPixel(x, y); if (!IsBackgroundColor(c, backgroundArgb)) { break; } } if (IsBackgroundColor(c, backgroundArgb)) { x--; } } int maxX = x + 7; if (maxX >= bmp.Width) { maxX = bmp.Width - 1; } bmp.UnlockImage(); Bitmap bmpImage = bmp.GetBitmap(); if (bmpImage.Width > 1 && bmpImage.Height > 1 && maxX - minX > 0 && maxY - minY > 0) { Bitmap bmpCrop = bmpImage.Clone(new Rectangle(minX, minY, maxX - minX, maxY - minY), bmpImage.PixelFormat); return bmpCrop; } return (Bitmap)bmpImage.Clone(); }
/// <summary> /// Decode caption from the input stream /// </summary> /// <returns>bitmap of the decoded caption</returns> public static Bitmap DecodeImage(PcsObject pcs, IList <OdsData> data, List <PaletteInfo> palettes) { if (pcs == null || data == null || data.Count == 0) { return(new Bitmap(1, 1)); } int w = data[0].Size.Width; int h = data[0].Size.Height; if (w <= 0 || h <= 0 || data[0].Fragment.ImageBuffer.Length == 0) { return(new Bitmap(1, 1)); } var bm = new FastBitmap(new Bitmap(w, h)); bm.LockImage(); var pal = DecodePalette(palettes); int ofs = 0; int xpos = 0; var index = 0; byte[] buf = data[0].Fragment.ImageBuffer; do { int b = buf[index++] & 0xff; if (b == 0 && index < buf.Length) { b = buf[index++] & 0xff; if (b == 0) { // next line ofs = ofs / w * w; if (xpos < w) { ofs += w; } xpos = 0; } else { int size; if ((b & 0xC0) == 0x40) { if (index < buf.Length) { // 00 4x xx -> xxx zeroes size = ((b - 0x40) << 8) + (buf[index++] & 0xff); var c = Color.FromArgb(pal.GetArgb(0)); for (int i = 0; i < size; i++) { PutPixel(bm, ofs++, c); } xpos += size; } } else if ((b & 0xC0) == 0x80) { if (index < buf.Length) { // 00 8x yy -> x times value y size = (b - 0x80); b = buf[index++] & 0xff; var c = Color.FromArgb(pal.GetArgb(b)); for (int i = 0; i < size; i++) { PutPixel(bm, ofs++, c); } xpos += size; } } else if ((b & 0xC0) != 0) { if (index < buf.Length) { // 00 cx yy zz -> xyy times value z size = ((b - 0xC0) << 8) + (buf[index++] & 0xff); b = buf[index++] & 0xff; var c = Color.FromArgb(pal.GetArgb(b)); for (int i = 0; i < size; i++) { PutPixel(bm, ofs++, c); } xpos += size; } } else { // 00 xx -> xx times 0 var c = Color.FromArgb(pal.GetArgb(0)); for (int i = 0; i < b; i++) { PutPixel(bm, ofs++, c); } xpos += b; } } } else { PutPixel(bm, ofs++, b, pal); xpos++; } } while (index < buf.Length); bm.UnlockImage(); return(bm.GetBitmap()); }
/// <summary> /// Decode caption from the input stream /// </summary> /// <param name="pic">SubPicture object containing info about the caption</param> /// <param name="transIdx">index of the transparent color</param> /// <returns>bitmap of the decoded caption</returns> public Bitmap DecodeImage(BluRaySupPalette defaultPalette) { if (ObjectIdImage == null) { return(new Bitmap(1, 1)); } int w = ObjectIdImage.Width; int h = ObjectIdImage.Height; if (w > Width || h > Height) { throw new Exception("Subpicture too large: " + w + "x" + h); } //Bitmap bm = new Bitmap(w, h); FastBitmap bm = new FastBitmap(new Bitmap(w, h)); bm.LockImage(); BluRaySupPalette pal = DecodePalette(defaultPalette); int index = 0; int ofs = 0; int xpos = 0; // just for multi-packet support, copy all of the image data in one common buffer byte[] buf = new byte[ObjectIdImage.BufferSize]; foreach (ImageObjectFragment fragment in ObjectIdImage.Fragments) { Buffer.BlockCopy(fragment.ImageBuffer, 0, buf, index, fragment.ImagePacketSize); index += fragment.ImagePacketSize; } index = 0; do { int b = buf[index++] & 0xff; if (b == 0) { b = buf[index++] & 0xff; if (b == 0) { // next line ofs = (ofs / w) * w; if (xpos < w) { ofs += w; } xpos = 0; } else { int size; if ((b & 0xC0) == 0x40) { // 00 4x xx -> xxx zeroes size = ((b - 0x40) << 8) + (buf[index++] & 0xff); for (int i = 0; i < size; i++) { PutPixel(bm, ofs++, 0, pal); } xpos += size; } else if ((b & 0xC0) == 0x80) { // 00 8x yy -> x times value y size = (b - 0x80); b = buf[index++] & 0xff; for (int i = 0; i < size; i++) { PutPixel(bm, ofs++, b, pal); } xpos += size; } else if ((b & 0xC0) != 0) { // 00 cx yy zz -> xyy times value z size = ((b - 0xC0) << 8) + (buf[index++] & 0xff); b = buf[index++] & 0xff; for (int i = 0; i < size; i++) { PutPixel(bm, ofs++, b, pal); } xpos += size; } else { // 00 xx -> xx times 0 for (int i = 0; i < b; i++) { PutPixel(bm, ofs++, 0, pal); } xpos += b; } } } else { PutPixel(bm, ofs++, b, pal); xpos++; } } while (index < buf.Length); bm.UnlockImage(); return(bm.GetBitmap()); }
public void PaintObjects() { FastBitmap bitmap = new FastBitmap(new Bitmap(Camera.FrameWidth, Camera.FrameHeight)); double[,] buffer = new double[Camera.FrameWidth, Camera.FrameHeight]; for (int i = 0; i < Camera.FrameWidth; ++i) { for (int j = 0; j < Camera.FrameHeight; ++j) { buffer[i, j] = double.MaxValue; } } Matrix cameraMatrix = Camera.GetCameraMatrix(); if (PanObject == null) { if (IsGizmoVisible && IsVisibleForCamera(new Point3D(0, 0, 0), GIZMO_SIZE, cameraMatrix)) { DrawGizmo(bitmap, buffer, ModificationMatrix.GetIdentityMatrix(4), new Point3D(0, 0, 0), cameraMatrix); } Matrix lightMatrix = ModificationMatrix.GetIdentityMatrix(4); foreach (Vector3D light in Lights) { Point3D lightPosition = new Point3D(light); if (IsVisibleForCamera(lightPosition, lightPosition.Length, cameraMatrix)) { Sphere lightSphere = new Sphere(lightPosition, 3, 4, Color.Yellow); foreach (Face face in lightSphere.Faces) { List <Point3D> points = new List <Point3D>(); foreach (Point3D point in face.Points) { points.Add(point.Clone()); } DrawLines(bitmap, buffer, points, lightSphere.Color, lightMatrix, lightPosition, lightMatrix, lightPosition, cameraMatrix); } } } } foreach (SceneObject sceneObject in Objects) { if (IsVisibleForCamera(sceneObject.BasePoint, sceneObject.MaxLength, cameraMatrix) && (PanObject == null || PanObject.Equals(sceneObject))) { Matrix objectMatrix = sceneObject.GetModificationMatrix(); Matrix gizmoModificationMatrix = sceneObject.GetGizmoModificationMatrix(); if (Mode == MODE.WIREFRAME && IsGizmoVisible) { DrawGizmo(bitmap, buffer, gizmoModificationMatrix, sceneObject.BasePoint, cameraMatrix); } foreach (ScenePrimitive scenePrimitive in sceneObject.ScenePrimitives) { Matrix primitiveMatrix = scenePrimitive.GetModificationMatrix(); foreach (Face face in scenePrimitive.Primitive.Faces) { List <Point3D> points = new List <Point3D>(); foreach (Point3D point in face.Points) { points.Add(point.Clone()); } if (Mode == MODE.WIREFRAME) { DrawLines(bitmap, buffer, points, scenePrimitive.Primitive.Color, primitiveMatrix, scenePrimitive.Primitive.BasePoint, objectMatrix, sceneObject.BasePoint, cameraMatrix); } else { ApplyMatrix(points, primitiveMatrix, scenePrimitive.Primitive.BasePoint, objectMatrix, sceneObject.BasePoint); double brightness = CalculateLight(new Face(new Point3D[] { points[0], points[1], points[2] })); Color color = Color.FromArgb( (int)(scenePrimitive.Primitive.Color.R * brightness), (int)(scenePrimitive.Primitive.Color.G * brightness), (int)(scenePrimitive.Primitive.Color.B * brightness) ); ConvertLocalToCamera(points, cameraMatrix); ConvertCameraToScreen(points); Triangle(new Vector3D(points[0]), new Vector3D(points[1]), new Vector3D(points[2]), color, bitmap, buffer); } } } } } FastBitmap bufferImage = new FastBitmap(new Bitmap(canvas.ClientSize.Width, canvas.ClientSize.Height)); double minimum = double.MaxValue; double maximum = double.MinValue; for (int i = 0; i < Camera.FrameWidth; ++i) { for (int j = 0; j < Camera.FrameHeight; ++j) { minimum = Math.Min(minimum, Math.Max(buffer[i, j], double.MinValue)); maximum = Math.Max(maximum, Math.Min(buffer[i, j], double.MaxValue)); } } canvas.Image = bitmap.GetBitmap(); }
public void paintObjects() { LinearGradientBrush linGrBrush = new LinearGradientBrush( new Point(camera.screenWidth / 2, 0), new Point(camera.screenWidth / 2, camera.screenHeight), Color.FromArgb(255, 14, 14, 14), Color.FromArgb(255, 66, 66, 66)); Bitmap gradBack = new Bitmap(camera.screenWidth, camera.screenHeight); Graphics graphics = Graphics.FromImage(gradBack); graphics.FillRectangle(linGrBrush, 0, 0, camera.screenWidth, camera.screenHeight); FastBitmap bitmap = new FastBitmap(gradBack); //FastBitmap bitmap = new FastBitmap(camera.screenWidth, camera.screenHeight, Color.FromArgb(60, 60, 60)); double[,] buffer = new double[camera.screenWidth, camera.screenHeight]; for (int i = 0; i < camera.screenWidth; ++i) { for (int j = 0; j < camera.screenHeight; ++j) { buffer[i, j] = double.MaxValue; } } Array2D cameraMatrix = camera.getCameraMatrix(); for (int i = 0; i < groups.Count; ++i) { Group sceneObject = groups[i]; if (IsVisibleForCamera(sceneObject.BasePoint, sceneObject.MaxLength, cameraMatrix)) { Array2D modificationMatrix = sceneObject.GetModificationMatrix(); foreach (SceneObject scenePrimitive in sceneObject.groupObjects) { Array2D primitiveMatrix = scenePrimitive.getModificationMatrix(); foreach (Face face in scenePrimitive.primitive.getFaces()) { List <Point3D> points = new List <Point3D>(); foreach (Point3D point in face.getPoints()) { points.Add(point.copy()); } if (drawMode == 0 /*|| drawMode == 2 || drawMode == 3*/) { //DrawLines(bitmap, obj.basePoint, points, primitiveMatrix, face.getColor()); DrawLines(bitmap, sceneObject.BasePoint, scenePrimitive.primitive.basePoint, points, modificationMatrix, primitiveMatrix, scenePrimitive.primitive.color); } if (drawMode == 1 || drawMode == 2 || drawMode == 3) { ConvertLocalToCamera(sceneObject.BasePoint, scenePrimitive.primitive.basePoint, points, modificationMatrix, primitiveMatrix); Face temp = new Face(new Point3D[] { points[0], points[1], points[2] }); Vector3D normal = temp.getNormalVector(); Vector3D light1 = new Vector3D(20, 20, 20); double brightness = Math.Abs(Vector3D.AngleCos(normal, light1.normalize())); brightness = Math.Max(brightness, 0.1); //brightness *= Math.Abs(Vector3D.AngleCos(normal, light2.normalize())) if (light1.x == 0 && light1.y == 0 && light1.z == 0) { brightness = 0.1; } //Таргет не можнт иметь все нули - деление на ноль int alpha = 255; if (drawMode == 3) { alpha = 160; } Color color = Color.FromArgb(alpha, (int)(scenePrimitive.primitive.color.R * brightness), (int)(scenePrimitive.primitive.color.G * brightness), (int)(scenePrimitive.primitive.color.B * brightness)); convertCameraToScreenCoords(points); Triangle(new Vector3D(points[0]), new Vector3D(points[1]), new Vector3D(points[2]), color, bitmap, buffer); } } } } } projectionScreen.Image = bitmap.GetBitmap(); }