public void Sampling(int x, int y) { switch (N) { default: throw new NotSupportedException(); case 1: { int rowHead = _sW * y; middle = _samplingSrc._buffer[rowHead + x]; } break; case 5: { if (x > 0 && x < _sW - 1 && y > 0 && y < _sH - 1) { FloatRGB[] pixelBuffer = _samplingSrc._buffer; int rowHead = _sW * y; int upperRowHead = rowHead - _sW; int lowerRowHead = rowHead + _sW; middle = pixelBuffer[rowHead + x]; left = pixelBuffer[rowHead + x - 1]; right = pixelBuffer[rowHead + x + 1]; top = pixelBuffer[upperRowHead + x]; bottom = pixelBuffer[lowerRowHead + x]; } } break; case 9: { if (x > 0 && x < _sW - 1 && y > 0 && y < _sH - 1) { FloatRGB[] pixelBuffer = _samplingSrc._buffer; int rowHead = _sW * y; int upperRowHead = rowHead - _sW; int lowerRowHead = rowHead + _sW; middle = pixelBuffer[rowHead + x]; left = pixelBuffer[rowHead + x - 1]; right = pixelBuffer[rowHead + x + 1]; top = pixelBuffer[upperRowHead + x]; left_top = pixelBuffer[upperRowHead + x - 1]; right_top = pixelBuffer[upperRowHead + x + 1]; bottom = pixelBuffer[lowerRowHead + x]; left_bottom = pixelBuffer[lowerRowHead + x - 1]; right_bottom = pixelBuffer[lowerRowHead + x + 1]; } } break; } }
static int fixpix(int color, double procent, FloatRGB colpro) { int colorR = (color & 0xFF0000) >> 16; int colorG = (color & 0x00FF00) >> 8; int colorB = color & 0x0000FF; colorR = colorR + (int)((255 - colorR) * procent * colpro.r); colorG = colorG + (int)((255 - colorG) * procent * colpro.g); colorB = colorB + (int)((255 - colorB) * procent * colpro.b); return(Image.rgb(colorR, colorG, colorB)); }
static FloatRGBBmp CreatePixel3Bitmap(Bitmap bmp) { var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); int[] buffer = new int[bmp.Width * bmp.Height]; System.Runtime.InteropServices.Marshal.Copy(bmpdata.Scan0, buffer, 0, bmp.Width * bmp.Height); bmp.UnlockBits(bmpdata); FloatRGBBmp pix3fbmp = new FloatRGBBmp(bmp.Width, bmp.Height); FloatRGB[] buffer3f = pix3fbmp._buffer; for (int i = 0; i < buffer.Length; ++i) { int pixel = buffer[i]; //rgb float r = (pixel & 0xff) / 255f; float g = ((pixel >> 8) & 0xff) / 255f; float b = ((pixel >> 16) & 0xff) / 255f; buffer3f[i] = new FloatRGB(r, g, b); } return(pix3fbmp); }
void GenerateMsdfOutput3(string msdfImg) { //generate msdf output //from msdf fragment shader //#ifdef GL_OES_standard_derivatives // #extension GL_OES_standard_derivatives : enable // #endif // precision mediump float; // varying vec2 v_texCoord; // uniform sampler2D s_texture; //msdf texture // uniform vec4 u_color; // float median(float r, float g, float b) { // return max(min(r, g), min(max(r, g), b)); // } // void main() { // vec4 sample = texture2D(s_texture, v_texCoord); // float sigDist = median(sample[0], sample[1], sample[2]) - 0.5; // float opacity = clamp(sigDist/fwidth(sigDist) + 0.5, 0.0, 1.0); // gl_FragColor= vec4(u_color[0],u_color[1],u_color[2],opacity * u_color[3]); // } DisposeExistingPictureBoxImage(pictureBox1); Bitmap msdf_bmp = new Bitmap(msdfImg); this.pictureBox1.Image = msdf_bmp; FloatRGBBmp pixel3fBmp = CreatePixel3Bitmap(msdf_bmp); int px_index = 0; int px_height = msdf_bmp.Height; int px_width = msdf_bmp.Width; int[] output = new int[px_width * px_height]; bool onlySignDist = chkOnlySignDist.Checked; Pixel3fSampling9 sm = new Pixel3fSampling9(); sm.SetSamplingSource(pixel3fBmp); if ((int)cmdSamplingSize.SelectedItem == 9) { sm.N = 9; } else { sm.N = 5; } for (int y = 1; y < px_height - 1; ++y) { int x = 1; px_index = x + (px_width * y); for (; x < px_width - 1; ++x) { //each pixel sm.Sampling(x, y); FloatRGB rgb = sm.WeightAvg(); float r = rgb.r; float g = rgb.g; float b = rgb.b; float sigDist = median(r, g, b) - 0.5f; float toClamp = sigDist; sm.Sampling(x + 1, y); FloatRGB next_right = sm.WeightAvg(); float d_r1 = next_right.r - r; float d_g1 = next_right.g - g; float d_b1 = next_right.b - b; //get bottom px sm.Sampling(x, y + 1); FloatRGB bottom = sm.WeightAvg(); float d_ry = bottom.r - r; float d_gy = bottom.g - g; float d_by = bottom.b - b; //for test only //fake gles fwidth float fwidth = Math.Abs(((d_r1 + d_g1 + d_b1) / 3f)) + Math.Abs(((d_ry + d_gy + d_by) / 3f)); toClamp = onlySignDist ? sigDist : sigDist / (fwidth); //float opacity = clamp(sigDist/fwidth(sigDist) + 0.5, 0.0, 1.0); float opacity = (float)Math.Max(0, Math.Min(toClamp + 0.5, 1)); byte o_r = 0; byte o_g = 0; byte o_b = 0; byte o_a = (byte)(255 * opacity); output[px_index] = (o_a << 24) | (o_b << 16) | (o_g << 8) | o_r; px_index++; } } Bitmap output2 = new Bitmap(msdf_bmp.Width, msdf_bmp.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); var bmpdata2 = output2.LockBits(new System.Drawing.Rectangle(0, 0, msdf_bmp.Width, msdf_bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, output2.PixelFormat); System.Runtime.InteropServices.Marshal.Copy(output, 0, bmpdata2.Scan0, output.Length); output2.UnlockBits(bmpdata2); DisposeExistingPictureBoxImage(pictureBox2); pictureBox2.Image = output2; }
void GenerateMsdfOutput2() { //generate msdf output //from msdf fragment shader //#ifdef GL_OES_standard_derivatives // #extension GL_OES_standard_derivatives : enable // #endif // precision mediump float; // varying vec2 v_texCoord; // uniform sampler2D s_texture; //msdf texture // uniform vec4 u_color; // float median(float r, float g, float b) { // return max(min(r, g), min(max(r, g), b)); // } // void main() { // vec4 sample = texture2D(s_texture, v_texCoord); // float sigDist = median(sample[0], sample[1], sample[2]) - 0.5; // float opacity = clamp(sigDist/fwidth(sigDist) + 0.5, 0.0, 1.0); // gl_FragColor= vec4(u_color[0],u_color[1],u_color[2],opacity * u_color[3]); // } string msdfImg = listBox1.SelectedItem as string; Bitmap bmp = new Bitmap(msdfImg); this.pictureBox1.Image = bmp; FloatRGBBmp pixel3fBmp = CreatePixel3Bitmap(bmp); int px_index = 0; int px_height = bmp.Height; int px_width = bmp.Width; int line_head = 0; int nextline_head = 0; FloatRGB[] pixel3fBuffer = pixel3fBmp._buffer; int[] output = new int[px_width * px_height]; bool onlySignDist = chkOnlySignDist.Checked; for (int y = 0; y < px_height - 1; ++y) { line_head = y * px_width; nextline_head = (y + 1) * px_width; px_index = y * px_width; int i = 0; for (int x = 0; x < px_width - 1; ++x) { //each pixel FloatRGB rgb = pixel3fBuffer[line_head + i]; float r = rgb.r; float g = rgb.g; float b = rgb.b; float sigDist = median(r, g, b) - 0.5f; float toClamp = sigDist; //get right px FloatRGB next_right = pixel3fBuffer[line_head + i + 1]; float d_r1 = next_right.r - r; float d_g1 = next_right.g - g; float d_b1 = next_right.b - b; //get bottom px FloatRGB bottom = pixel3fBuffer[nextline_head + i]; float d_ry = bottom.r - r; float d_gy = bottom.g - g; float d_by = bottom.b - b; //for test only //fake gles fwidth float fwidth = Math.Abs(((d_r1 + d_g1 + d_b1) / 3f)) + Math.Abs(((d_ry + d_gy + d_by) / 3f)); toClamp = onlySignDist ? sigDist : sigDist / (fwidth); //float opacity = clamp(sigDist/fwidth(sigDist) + 0.5, 0.0, 1.0); float opacity = (float)Math.Max(0, Math.Min(toClamp + 0.5, 1)); byte o_r = 0; byte o_g = 0; byte o_b = 0; byte o_a = (byte)(255 * opacity); output[px_index] = (o_a << 24) | (o_b << 16) | (o_g << 8) | o_r; px_index++; i++; } } Bitmap output2 = new Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); var bmpdata2 = output2.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, output2.PixelFormat); System.Runtime.InteropServices.Marshal.Copy(output, 0, bmpdata2.Scan0, output.Length); output2.UnlockBits(bmpdata2); pictureBox2.Image = output2; }
static int fixpix(int color, double procent, FloatRGB colpro) { int colorR = (color & 0xFF0000) >> 16; int colorG = (color & 0x00FF00) >> 8; int colorB = color & 0x0000FF; colorR = colorR + (int)((255 - colorR) * procent * colpro.r); colorG = colorG + (int)((255 - colorG) * procent * colpro.g); colorB = colorB + (int)((255 - colorB) * procent * colpro.b); return Image.rgb(colorR, colorG, colorB); }