public override void UpdateNormalBitmap(System.Windows.Media.Imaging.WriteableBitmap bitmap, System.Windows.Media.Color color) { unsafe { bitmap.Lock(); int currentPixel = -1; byte* pStart = (byte*)(void*)bitmap.BackBuffer; double iRowUnit = (double)100 / 256; double iRowCurrent = 100; double a = sModel.AComponent(color); double b = sModel.BComponent(color); for (int iRow = 0; iRow < bitmap.PixelHeight; iRow++) { Color lightness = sModel.Color(iRowCurrent, a, b); for (int iCol = 0; iCol < bitmap.PixelWidth; iCol++) { currentPixel++; *(pStart + currentPixel * 3 + 0) = lightness.B; //Blue *(pStart + currentPixel * 3 + 1) = lightness.G; //Green *(pStart + currentPixel * 3 + 2) = lightness.R; //red } iRowCurrent -= iRowUnit; } bitmap.AddDirtyRect(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight)); bitmap.Unlock(); } }
/// <summary> /// Apply color to mask onto the canvas /// </summary> /// <param name="clr">is the color to be used</param> /// <param name="mask">is the mask image to be read</param> /// <param name="canvas">is the destination image to be draw upon</param> /// <param name="maskColor">is mask color used in mask image</param> /// <returns>true if successful</returns> public static bool ApplyColorToMask( System.Windows.Media.Color clr, System.Windows.Media.Imaging.WriteableBitmap mask, System.Windows.Media.Imaging.WriteableBitmap canvas, System.Windows.Media.Color maskColor) { if (mask == null || canvas == null) return false; mask.Lock(); canvas.Lock(); unsafe { uint* pixelsMask = (uint*)mask.BackBuffer; uint* pixelsCanvas = (uint*)canvas.BackBuffer; if (pixelsMask == null || pixelsCanvas == null) return false; uint col = 0; int stride = canvas.BackBufferStride >> 2; for (uint row = 0; row < canvas.Height; ++row) { for (col = 0; col < canvas.Width; ++col) { if (row >= mask.Height || col >= mask.Width) continue; uint index = (uint)(row * stride + col); uint indexMask = (uint)(row * (mask.BackBufferStride >> 2) + col); byte maskByte = 0; if (MaskColor.IsEqual(maskColor, MaskColor.Red)) maskByte = (Byte)((pixelsMask[indexMask] & 0xff0000) >> 16); else if (MaskColor.IsEqual(maskColor, MaskColor.Green)) maskByte = (Byte)((pixelsMask[indexMask] & 0xff00) >> 8); else if (MaskColor.IsEqual(maskColor, MaskColor.Blue)) maskByte = (Byte)(pixelsMask[indexMask] & 0xff); uint color = (uint)(0xff << 24 | clr.R << 16 | clr.G << 8 | clr.B); if(maskByte>0) pixelsCanvas[index] = Alphablend(pixelsCanvas[index], color, (Byte)(pixelsMask[indexMask] >> 24), (Byte)(pixelsMask[indexMask] >> 24)); } } } canvas.Unlock(); mask.Unlock(); return true; }
public override void UpdateColorPlaneBitmap(System.Windows.Media.Imaging.WriteableBitmap bitmap, int normalComponentValue) { unsafe { bitmap.Lock(); byte* pStart = (byte*)(void*)bitmap.BackBuffer; int currentPixel = -1; double iRowUnit = (double)100 / bitmap.PixelHeight; double iColUnit = (double)1; double iRowCurrent = 100; double b = (double)normalComponentValue; for (int iRow = 0; iRow < bitmap.PixelHeight; iRow++) { double l = iRowCurrent; double iColCurrent = -128; for (int iCol = 0; iCol < bitmap.PixelWidth; iCol++) { double theta = 6.0 / 29.0; double a = iColCurrent; double fy = (l + 16) / 116.0; double fx = fy + (a / 500.0); double fz = fy - (b / 200.0); var x = (fx > theta) ? D65X * (fx * fx * fx) : (fx - 16.0 / 116.0) * 3 * (theta * theta) * D65X; var y = (fy > theta) ? D65Y * (fy * fy * fy) : (fy - 16.0 / 116.0) * 3 * (theta * theta) * D65Y; var z = (fz > theta) ? D65Z * (fz * fz * fz) : (fz - 16.0 / 116.0) * 3 * (theta * theta) * D65Z; x = (x > 0.9505) ? 0.9505 : ((x < 0) ? 0 : x); y = (y > 1.0) ? 1.0 : ((y < 0) ? 0 : y); z = (z > 1.089) ? 1.089 : ((z < 0) ? 0 : z); double[] Clinear = new double[3]; Clinear[0] = x * 3.2410 - y * 1.5374 - z * 0.4986; // red Clinear[1] = -x * 0.9692 + y * 1.8760 - z * 0.0416; // green Clinear[2] = x * 0.0556 - y * 0.2040 + z * 1.0570; // blue for (int i = 0; i < 3; i++) { Clinear[i] = (Clinear[i] <= 0.0031308) ? 12.92 * Clinear[i] : (1 + 0.055) * Math.Pow(Clinear[i], (1.0 / 2.4)) - 0.055; Clinear[i] = Math.Min(Clinear[i], 1); Clinear[i] = Math.Max(Clinear[i], 0); } currentPixel++; *(pStart + currentPixel * 3 + 0) = Convert.ToByte(Clinear[2] * 255); //Blue *(pStart + currentPixel * 3 + 1) = Convert.ToByte(Clinear[1] * 255); //Green *(pStart + currentPixel * 3 + 2) = Convert.ToByte(Clinear[0] * 255); //red iColCurrent += iColUnit; } iRowCurrent -= iRowUnit; } bitmap.AddDirtyRect(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight)); bitmap.Unlock(); } }
/// <summary> /// Measure the mask image based on the mask color. /// </summary> /// <param name="mask">is the mask image to be measured</param> /// <param name="maskColor">is mask color used in mask image</param> /// <param name="top">returns the topmost Y </param> /// <param name="left">returns the leftmost X</param> /// <param name="bottom">returns the bottommost Y</param> /// <param name="right">returns the rightmost X</param> /// <returns>true if successful</returns> public static bool MeasureMaskLength( System.Windows.Media.Imaging.WriteableBitmap mask, System.Windows.Media.Color maskColor, ref uint top, ref uint left, ref uint bottom, ref uint right) { top = 30000; left = 30000; bottom = 0; right = 0; if (mask == null) return false; mask.Lock(); unsafe { uint* pixelsMask = (uint*)mask.BackBuffer; if (pixelsMask == null) return false; uint col = 0; int stride = mask.BackBufferStride >> 2; for (uint row = 0; row < mask.Height; ++row) { for (col = 0; col < mask.Width; ++col) { uint index = (uint)(row * stride + col); byte nAlpha = 0; if (MaskColor.IsEqual(maskColor, MaskColor.Red)) nAlpha = (Byte)((pixelsMask[index] & 0xff0000) >> 16); else if (MaskColor.IsEqual(maskColor, MaskColor.Green)) nAlpha = (Byte)((pixelsMask[index] & 0xff00) >> 8); else if (MaskColor.IsEqual(maskColor, MaskColor.Blue)) nAlpha = (Byte)(pixelsMask[index] & 0xff); if (nAlpha > 0) { if (col < left) left = col; if (row < top) top = row; if (col > right) right = col; if (row > bottom) bottom = row; } } } } mask.Unlock(); return true; }
/// <summary> /// Apply image to mask onto the canvas /// </summary> /// <param name="image">is the image to be used</param> /// <param name="mask">is the mask image to be read</param> /// <param name="canvas">is the destination image to be draw upon</param> /// <param name="maskColor">is mask color used in mask image</param> /// <returns>true if successful</returns> public static bool ApplyImageToMask( System.Windows.Media.Imaging.WriteableBitmap image, System.Windows.Media.Imaging.WriteableBitmap mask, System.Windows.Media.Imaging.WriteableBitmap canvas, System.Windows.Media.Color maskColor, bool NoAlphaAtBoundary) { if (image == null || mask == null || canvas == null) return false; image.Lock(); mask.Lock(); canvas.Lock(); unsafe { uint* pixelsImage = (uint*)image.BackBuffer; uint* pixelsMask = (uint*)mask.BackBuffer; uint* pixelsCanvas = (uint*)canvas.BackBuffer; if (pixelsImage == null || pixelsMask == null || pixelsCanvas == null) return false; uint col = 0; int stride = canvas.BackBufferStride >> 2; for (uint row = 0; row < canvas.Height; ++row) { for (col = 0; col < canvas.Width; ++col) { if (row >= image.Height || col >= image.Width) continue; if (row >= mask.Height || col >= mask.Width) continue; uint index = (uint)(row * stride + col); uint indexMask = (uint)(row * (mask.BackBufferStride >> 2) + col); uint indexImage = (uint)(row * (image.BackBufferStride >> 2) + col); byte maskByte = 0; if (MaskColor.IsEqual(maskColor, MaskColor.Red)) maskByte = (Byte)((pixelsMask[indexMask] & 0xff0000) >> 16); else if (MaskColor.IsEqual(maskColor, MaskColor.Green)) maskByte = (Byte)((pixelsMask[indexMask] & 0xff00) >> 8); else if (MaskColor.IsEqual(maskColor, MaskColor.Blue)) maskByte = (Byte)(pixelsMask[indexMask] & 0xff); if (maskByte > 0) { if (NoAlphaAtBoundary) { pixelsCanvas[index] = AlphablendNoAlphaAtBoundary(pixelsCanvas[index], pixelsImage[indexImage], (Byte)(pixelsMask[indexMask] >> 24), (Byte)(pixelsMask[indexMask] >> 24)); } else { pixelsCanvas[index] = Alphablend(pixelsCanvas[index], pixelsImage[indexImage], (Byte)(pixelsMask[indexMask] >> 24), (Byte)(pixelsMask[indexMask] >> 24)); } } } } } canvas.Unlock(); mask.Unlock(); image.Unlock(); return true; }
/// <summary> /// Performs a CRC check on a file and updates the CRC32 checksum for the /// whole process. /// </summary> /// <param name="file">The System.IO.FileStream to scan.</param> /// <returns>Returns an unsigned 32-bit number representing the CRC value for the file.</returns> public string Update( System.IO.FileStream file ) { unchecked { byte[] buffer = new byte[BUFFER_SIZE]; int readSize = BUFFER_SIZE; // Currently we do a "straight" CRC where only the data // is collected. Previous builds included the file name // as part of the CRC, to prevent getting CRCs with 0 // if the file is empty. However, this was changed and is // no longer done. // Used when file names not included UInt32 crc = HighBitMask; // Include file names into CRC // UInt32 crc = Update( file.Name ); try { file.Lock( 0, file.Length ); int count = file.Read(buffer, 0, readSize); while (count > 0) { for (int i = 0; i < count; i++) crc = ((crc) >> 8) ^ CRCTable[(buffer[i]) ^ ((crc) & 0x000000FF)]; //ProcessBuffer( ref buffer, ref crc ); count = file.Read(buffer, 0, readSize); } file.Unlock( 0, file.Length ); } catch ( System.Exception e ) { throw e; } crc = ~crc; LifetimeCRC ^= crc; //Prefix 0's to CRC's less than 8 chars string temp = crc.ToString("X"); int intemp = ( 8 - temp.Length ); do { if ( temp.Length < 8 ) { temp = "0" + temp; } intemp--; } while (intemp>0); return temp; //return crc; } }