private static Image ReadImage(string path) { const PixelFormat pixelFormat = PixelFormat.Format32bppArgb; // Load file from disk var original = (Bitmap)System.Drawing.Image.FromFile(path); var rectangle = new Rectangle(0, 0, original.Width, original.Height); // Optionally convert pixel format Bitmap clone; if (original.PixelFormat == pixelFormat) { clone = original; } else { clone = new Bitmap(original.Width, original.Height, pixelFormat); using (var graphics = Graphics.FromImage(clone)) graphics.DrawImage(original, rectangle); } // Convert image to byte[] var pixels = new byte[clone.Width * clone.Height * BytesPerPixel]; var bitmapData = clone.LockBits(rectangle, ImageLockMode.ReadOnly, clone.PixelFormat); var ptr = bitmapData.Scan0; Marshal.Copy(ptr, pixels, 0, pixels.Length); clone.UnlockBits(bitmapData); var outputImage = new Image { Pixels = new Pixel[bitmapData.Width * bitmapData.Height] }; for (var i = 0; i < outputImage.Pixels.Length; i++) { var colorValue = BitConverter.ToInt32(pixels, i * BytesPerPixel); outputImage.Pixels[i] = Pixel.FromArgb(colorValue); } outputImage.Path = path; outputImage.Height = bitmapData.Height; outputImage.Width = bitmapData.Width; return(outputImage); }
private static bool ProcessArgs(ref Movement movement, IList <string> strs) { if (strs.Count == 0) { return(false); } movement.EndX = (int)Position.Center; movement.EndY = (int)Position.Center; movement.Filename = null; movement.Height = 0; movement.SmoothEnd = 0f; movement.SmoothStart = 0f; movement.StartX = (int)Position.Center; movement.StartY = (int)Position.Center; movement.Width = 0; movement.ZoomEnd = 1f; movement.ZoomEndCode = (int)ZoomCode.None; movement.ZoomStart = 1f; movement.ZoomStartCode = (int)ZoomCode.None; var actiondata = false; var continued = false; for (var i = 0; i < strs.Count; i++) { var str = strs[i]; string[] location; int h; int w; int x; int y; float f; switch (str.ToLowerInvariant()) { case @"geometry": case @"resize": case @"resolution": case @"size": if (i == strs.Count) { continue; } i++; str = strs[i]; var dimensions = Regex.Split(str, @"[:x]"); if (dimensions.Length < 2) { continue; } if (!int.TryParse(dimensions[0], out w) || !int.TryParse(dimensions[1], out h)) { continue; } if ((w < 1) || (h < 1)) { continue; } _outputWidth = w; _outputHeight = h; break; case @"owidth": case @"outputwidth": case @"width": if (i == strs.Count) { continue; } i++; str = strs[i]; if (!int.TryParse(str, out w) || w <= 0) { continue; } _outputWidth = w; break; case @"height": case @"oheight": case @"outputheight": if (i == strs.Count) { continue; } i++; str = strs[i]; if (!int.TryParse(str, out h) || h <= 0) { continue; } _outputHeight = h; break; case @"sharpness": if (i == strs.Count) { continue; } i++; str = strs[i]; int s; if (!int.TryParse(str, out s) || s <= 0) { continue; } _sharpness = s; break; case @"pansmoothness": if (i == strs.Count) { continue; } i++; str = strs[i]; if (!float.TryParse(str, out f)) { continue; } if (f < 0.0) { f = 0f; } if (f > 0.5) { f = 0.5f; } _panSmoothRatio = f; break; case @"zoomsmoothness": if (i == strs.Count) { continue; } i++; str = strs[i]; if (!float.TryParse(str, out f)) { continue; } if (f < 0.0) { f = 0f; } if (f > 0.5) { f = 0.5f; } _zoomSmoothRatio = f; break; case @"jpegquality": case @"jpgquality": case @"quality": if (i == strs.Count) { continue; } i++; str = strs[i]; if (!int.TryParse(str, out h) || h <= 0) { continue; } _jpegQuality = h; break; case @"fps": case @"framerate": if (i == strs.Count) { continue; } i++; str = strs[i]; if (!float.TryParse(str, out f) || f <= 0) { continue; } _framerate = f; break; case @"output": case @"outputtype": case @"type": if (i == strs.Count) { continue; } i++; str = strs[i]; if (str.Equals(@"jpeg", StringComparison.OrdinalIgnoreCase) || str.Equals(@"jpg", StringComparison.OrdinalIgnoreCase)) { _outputFormat = OutputFormat.JPG; } else if (str.Equals(@"ppm", StringComparison.OrdinalIgnoreCase)) { _outputFormat = OutputFormat.PPM; } break; case @"backgroundcolor": case @"bgcolor": case @"defaultcolor": if (i == strs.Count) { continue; } i++; str = strs[i].Replace(@"#", string.Empty); int c; if (!int.TryParse(str, NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat, out c)) { continue; } _defaultColor = Pixel.FromArgb(c); break; case @"img": if (i == strs.Count) { continue; } i++; str = strs[i]; movement.Filename = str; _image = OpenImage(str, true); movement.Height = _image.Height; movement.Width = _image.Width; actiondata = true; break; case @"crossfade": if (i == strs.Count) { continue; } i++; str = strs[i]; if (!float.TryParse(str, out f) || f <= 0) { continue; } movement.Duration = f; movement.Crossfade = (int)(f * _framerate) - 2; if (_frameCount < movement.Crossfade) { movement.Crossfade = _frameCount; } _frameCount -= movement.Crossfade; movement.CrossFrames = movement.Crossfade; break; case @"startpoint": if (i == strs.Count) { continue; } i++; str = strs[i]; location = str.Split(','); if (!int.TryParse(location[0], out x) || x < 0) { movement.EndX = movement.EndY = movement.StartX = movement.StartY = Translate(location[0]); continue; } if (!int.TryParse(location[1], out y) || y < 0) { continue; } movement.StartX = x; movement.EndX = x; movement.StartY = y; movement.EndY = y; actiondata = true; break; case @"endpoint": if (i == strs.Count) { continue; } i++; str = strs[i]; location = str.Split(','); if (!int.TryParse(location[0], out x) || x < 0) { movement.EndX = movement.EndY = Translate(location[0]); continue; } if (!int.TryParse(location[1], out y) || y < 0) { continue; } movement.EndX = x; movement.EndY = y; actiondata = true; break; case @"zoom": if (i == strs.Count) { continue; } i++; str = strs[i]; var zoom = str.Split(','); if (zoom.Length == 2) { if (float.TryParse(zoom[0], out f)) { movement.ZoomStart = f; } else { movement.ZoomStart = 0; movement.ZoomStartCode = (ZoomCode)Translate(zoom[0]); } if (float.TryParse(zoom[1], out f)) { movement.ZoomEnd = f; } else { movement.ZoomEnd = 0; movement.ZoomEndCode = (ZoomCode)Translate(zoom[1]); } } else { if (continued) { if (float.TryParse(zoom[0], out f)) { movement.ZoomEnd = f; } else { movement.ZoomEnd = 0; movement.ZoomEndCode = (ZoomCode)Translate(zoom[0]); } } else { if (float.TryParse(zoom[0], out f)) { movement.ZoomStart = f; movement.ZoomEnd = f; } else { movement.ZoomStart = 0; movement.ZoomEnd = 0; var code = Translate(zoom[0]); movement.ZoomEndCode = (ZoomCode)code; movement.ZoomStartCode = (ZoomCode)code; } } } actiondata = true; break; case @"duration": if (i == strs.Count) { continue; } i++; str = strs[i]; if (!float.TryParse(str, out f) || f <= 0) { continue; } movement.Duration = f; actiondata = true; break; case @"startsmooth": if (i == strs.Count) { continue; } i++; str = strs[i]; if (!float.TryParse(str, out f) || f <= 0) { continue; } movement.SmoothStart = f; actiondata = true; break; case @"endsmooth": if (i == strs.Count) { continue; } i++; str = strs[i]; if (!float.TryParse(str, out f) || f <= 0) { continue; } movement.SmoothEnd = f; actiondata = true; break; case @"continue": continued = true; movement.EndX = movement.StartX = _lastMovement.EndX; movement.EndY = movement.StartY = _lastMovement.EndY; movement.Filename = _lastMovement.Filename; movement.Height = _lastMovement.Height; movement.Width = _lastMovement.Width; movement.ZoomEnd = movement.ZoomStart = _lastMovement.ZoomEnd; actiondata = true; break; default: Console.WriteLine("Unknown command: {0}", str); break; } } if (actiondata) { Normalize(ref movement); _lastMovement = movement; return(true); } return(false); }