public Image ChangeColorSpace(ColorSpace cs) { if (_cm.colorspace == cs) { return(this); } if (_cm.colorspace == ColorSpace.RGB && cs == ColorSpace.YCbCr) { for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { YCbCr.fromRGB(ref _raster[0][i, j], ref _raster[1][i, j], ref _raster[2][i, j]); } } _cm.colorspace = ColorSpace.YCbCr; } else if (_cm.colorspace == ColorSpace.YCbCr && cs == ColorSpace.RGB) { for (int k = 0; k < width; k++) { for (int l = 0; l < height; l++) { YCbCr.toRGB(ref _raster[0][k, l], ref _raster[1][k, l], ref _raster[2][k, l]); } } _cm.colorspace = ColorSpace.RGB; } else if (_cm.colorspace == ColorSpace.Gray && cs == ColorSpace.YCbCr) { byte[,] array = new byte[width, height]; byte[,] array2 = new byte[width, height]; for (int m = 0; m < width; m++) { for (int n = 0; n < height; n++) { array[m, n] = 128; array2[m, n] = 128; } } _raster = new byte[3][, ] { _raster[0], array, array2 }; _cm.colorspace = ColorSpace.YCbCr; } else { if (_cm.colorspace != 0 || cs != ColorSpace.RGB) { throw new Exception("Colorspace conversion not supported."); } ChangeColorSpace(ColorSpace.YCbCr); ChangeColorSpace(ColorSpace.RGB); } return(this); }
/// <summary> /// Converts the colorspace of an image (in-place) /// </summary> /// <param name="cs">Colorspace to convert into</param> /// <returns>Self</returns> public Image ChangeColorSpace(ColorSpace cs) { // Colorspace is already correct if (_cm.colorspace == cs) { return(this); } byte[] ycbcr = new byte[3]; byte[] rgb = new byte[3]; if (_cm.colorspace == ColorSpace.RGB && cs == ColorSpace.YCbCr) { /* * Y' = + 0.299 * R'd + 0.587 * G'd + 0.114 * B'd * Cb = 128 - 0.168736 * R'd - 0.331264 * G'd + 0.5 * B'd * Cr = 128 + 0.5 * R'd - 0.418688 * G'd - 0.081312 * B'd * */ for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { YCbCr.fromRGB(ref _raster[0][x, y], ref _raster[1][x, y], ref _raster[2][x, y]); } } _cm.colorspace = ColorSpace.YCbCr; } else if (_cm.colorspace == ColorSpace.YCbCr && cs == ColorSpace.RGB) { for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { // 0 is LUMA // 1 is BLUE // 2 is RED YCbCr.toRGB(ref _raster[0][x, y], ref _raster[1][x, y], ref _raster[2][x, y]); } } _cm.colorspace = ColorSpace.RGB; } else if (_cm.colorspace == ColorSpace.Gray && cs == ColorSpace.YCbCr) { // To convert to YCbCr, we just add two 128-filled chroma channels byte[,] Cb = new byte[width, height]; byte[,] Cr = new byte[width, height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Cb[x, y] = 128; Cr[x, y] = 128; } } _raster = new byte[][, ] { _raster[0], Cb, Cr }; _cm.colorspace = ColorSpace.YCbCr; } else if (_cm.colorspace == ColorSpace.Gray && cs == ColorSpace.RGB) { ChangeColorSpace(ColorSpace.YCbCr); ChangeColorSpace(ColorSpace.RGB); } else { throw new Exception("Colorspace conversion not supported."); } return(this); }
public Image ChangeColorSpace(ColorSpace cs) { if (this._cm.colorspace != cs) { int num; int num2; byte[] buffer = new byte[3]; byte[] buffer2 = new byte[3]; if ((this._cm.colorspace == ColorSpace.RGB) && (cs == ColorSpace.YCbCr)) { for (num = 0; num < this.width; num++) { num2 = 0; while (num2 < this.height) { YCbCr.fromRGB(ref this._raster[0][num, num2], ref this._raster[1][num, num2], ref this._raster[2][num, num2]); num2++; } } this._cm.colorspace = ColorSpace.YCbCr; } else if ((this._cm.colorspace == ColorSpace.YCbCr) && (cs == ColorSpace.RGB)) { for (num = 0; num < this.width; num++) { num2 = 0; while (num2 < this.height) { YCbCr.toRGB(ref this._raster[0][num, num2], ref this._raster[1][num, num2], ref this._raster[2][num, num2]); num2++; } } this._cm.colorspace = ColorSpace.RGB; } else if ((this._cm.colorspace == ColorSpace.Gray) && (cs == ColorSpace.YCbCr)) { byte[,] buffer3 = new byte[this.width, this.height]; byte[,] buffer4 = new byte[this.width, this.height]; for (num = 0; num < this.width; num++) { for (num2 = 0; num2 < this.height; num2++) { buffer3[num, num2] = 0x80; buffer4[num, num2] = 0x80; } } this._raster = new byte[][, ] { this._raster[0], buffer3, buffer4 }; this._cm.colorspace = ColorSpace.YCbCr; } else { if ((this._cm.colorspace != ColorSpace.Gray) || (cs != ColorSpace.RGB)) { throw new Exception("Colorspace conversion not supported."); } this.ChangeColorSpace(ColorSpace.YCbCr); this.ChangeColorSpace(ColorSpace.RGB); } } return(this); }