public NewImage GetRect(Rectangle Rect) { var rectImg = new NewImage(Rect.Width, Rect.Height, this.Depth); for (int x = 0; x < Rect.Width; x++) { for (int y = 0; y < Rect.Height; y++) { for (int k = 0; k < Depth; k++) { rectImg.Data[y * rectImg.Depth * rectImg.Width + x * rectImg.Depth + k] = Data[(y + Rect.Y) * Depth * Width + (x + Rect.X) * Depth + k]; } } } return(rectImg); }
public NewImage ConvertToDepth(int ToDepth) { NewImage convertedImage = null; int stride = Depth * Width; int cstride = ToDepth * Width; var convertedData = new byte[cstride * Height]; const float cr = 0.5f; const float cg = 0.419f; const float cb = 0.081f; switch (this.Depth) { case 1: switch (ToDepth) { case 3: for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { convertedData[y * cstride + ToDepth * x] = convertedData[y * cstride + ToDepth * x + 1] = convertedData[y * cstride + ToDepth * x + 2] = Data[y * stride + Depth * x]; } } break; case 4: for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { convertedData[y * cstride + ToDepth * x] = convertedData[y * cstride + ToDepth * x + 1] = convertedData[y * cstride + ToDepth * x + 2] = Data[y * stride + Depth * x]; convertedData[y * cstride + ToDepth * x + 3] = 255; } } break; default: throw new Exception("Image Depth to Converted Not Supported"); } break; case 3: switch (ToDepth) { case 1: for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { convertedData[y * cstride + ToDepth * x] = (byte)(cb * Data[y * stride + Depth * x] + cg * Data[y * stride + Depth * x + 1] + cr * Data[y * stride + Depth * x + 2]); } } break; case 4: for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { convertedData[y * cstride + ToDepth * x] = Data[y * stride + Depth * x]; convertedData[y * cstride + ToDepth * x + 1] = Data[y * stride + Depth * x + 1]; convertedData[y * cstride + ToDepth * x + 2] = Data[y * stride + Depth * x + 2]; convertedData[y * cstride + ToDepth * x + 3] = 255; } } break; default: throw new Exception("Image Depth to Converted Not Supported"); } break; case 4: switch (ToDepth) { case 1: for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { convertedData[y * cstride + ToDepth * x] = (byte)(cb * Data[y * stride + Depth * x] + cg * Data[y * stride + Depth * x + 1] + cr * Data[y * stride + Depth * x + 2]); } } break; case 3: for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { convertedData[y * cstride + ToDepth * x] = Data[y * stride + Depth * x]; convertedData[y * cstride + ToDepth * x + 1] = Data[y * stride + Depth * x + 1]; convertedData[y * cstride + ToDepth * x + 2] = Data[y * stride + Depth * x + 2]; } } break; default: throw new Exception("Image Depth to Converted Not Supported"); } break; default: throw new Exception("Image Depth Not Supported!"); } return(convertedImage); }