Monday, 20 November 2017

Fast image processing in C#

if (bitmap.PixelFormat != PixelFormat.Format24bppRgb && bitmap.PixelFormat != PixelFormat.Format32bppArgb) { // <== A1
    return;
}
int ww = bitmap.Width  / 8;
int hh = bitmap.Height / 8;
using (FastBitmap fbitmap = new FastBitmap(bitmap, ww, hh,
                                           bitmap.Width - 2 * ww, bitmap.Height - 2 * hh)) { //  <== A2
    unsafe {                                                                                 //  <== A3
        byte* row = (byte*)fbitmap.Scan0, bb = row;                                          //  <== A4
        for (    int yy = 0; yy  < fbitmap.Height; yy++, bb  = (row += fbitmap.Stride)) {    //  <== A5
            for (int xx = 0; xx  < fbitmap.Width ; xx++, bb += fbitmap.PixelSize) {          //  <== A6
                // *(bb + 0) is B (Blue ) component of the pixel
                // *(bb + 1) is G (Green) component of the pixel
                // *(bb + 2) is R (Red  ) component of the pixel
                // *(bb + 3) is A (Alpha) component of the pixel ( for 32bpp )
                byte gray = (byte)((1140 * *(bb + 0) +
                                    5870 * *(bb + 1) +
                                    2989 * *(bb + 2)) / 10000);                              //  <== A7
                *(bb + 0) = *(bb + 1) = *(bb + 2) = gray;
            }
        }
    }
}   

if (bitmap.PixelFormat != PixelFormat.Format24bppRgb && bitmap.PixelFormat != PixelFormat.Format32bppArgb) { // <== A1
    return;
}
int w0 = bitmap.Width  / 8;
int h0 = bitmap.Height / 8;
int x1 = w0;
int y1 = h0;
int xn = x1 + bitmap.Width  - 2 * w0;
int yn = y1 + bitmap.Height - 2 * h0;

Color gray, cc;
for (    int yy = y1; yy < yn; yy++) {            // <== A1
    for (int xx = x1; xx < xn; xx++) {
        cc = bitmap.GetPixel(xx, yy);             // <== A2
        byte gg = (byte)((cc.B * 1140 +
                          cc.G * 5870 +
                          cc.R * 2989) / 10000);  // <== A3
        gray = Color.FromArgb( gg,gg,gg);
        bitmap.SetPixel(xx, yy, gray);            // <== A4
    }
}   

Mat bitmap = imread(argv[1], CV_LOAD_IMAGE_COLOR);  // <== A1
...
int ww = bitmap.cols - 2 * bitmap.cols / 8;         // <== A2
int hh = bitmap.rows - 2 * bitmap.rows / 8;
int x1 = bitmap.cols / 8;
int y1 = bitmap.rows / 8;

int pixelSize = bitmap.channels();                  // <== A3
int stride = pixelSize * bitmap.cols;
uchar* scan0 = bitmap.ptr<uchar>(0) + (y1  * stride) + x1 * pixelSize;

uchar* row = scan0, *bb = row;                      // <== A4
for (    int yy = 0; yy < hh; yy++, bb = (row += stride)) {
    for (int xx = 0; xx < ww; xx++, bb += pixelSize     ) {
        // *(bb + 0) is B (Blue ) component of the pixel
        // *(bb + 1) is G (Green) component of the pixel
        // *(bb + 2) is R (Red  ) component of the pixel
        // *(bb + 3) is A (Alpha) component of the pixel ( for 32bpp )
        uchar gray = ((1140 * *(bb + 0) +
                       5870 * *(bb + 1) +
                       2989 * *(bb + 2)) / 10000);
        *(bb + 0) = *(bb + 1) = *(bb + 2) = gray;
    }
}
...
imwrite(argv[2], bitmap, compression_params);       // <== A5   
</uchar>

No comments:

Post a Comment