Quick Gaussian Blur [cropping]

Source: Internet
Author: User

The upgraded version of the Gaussian Blur function, with the cropping area.

The check_rect () function is used to process the cropped area rectangle. If you do not want to crop the image, you only need to set left, top, right, and bottom to 0. In addition, the storage format of the bitmap is up and down. If you want to crop the image normally, you only need to set bottom to-bottom.


Bool check_rect (INT width, int height, Int & left, Int & Top, Int & right, Int & bottom) {If (! Left &&! Top &&! Right &&! Bottom) {// left = 0; Top = 0; Right = width; bottom = height; return true;} If (! (Right-left) |! (Bottom-top) {// 0 area rectangle return false;} If (bottom <= 0) {// reverse up/down int H =-bottom-top; bottom = height-top; Top = bottom-H;} If (right <= 0) {// reverse the Left and Right int W =-right-left; Right = width-left; left = right-W;} left = max (left, 0); Top = max (top, 0); Right = min (right, width); bottom = min (bottom, height); Return true;} // rapid Gaussian Blur function, by sdragonx 2014-08-01int gauss_blur (byte_t * image, // bitmap data int linebytes, // bitmap row bytes, BMP Data is 4-byte aligned in windows. Otherwise, int width, // bitmap width int height, // bitmap height int cbyte, // Number of Color Channels int left, // If the filter clipping area is 0, int top, int right, int bottom, float Sigma is not clipped. // Gaussian coefficient) {int x = 0, y = 0, n = 0; int channel = 0; int srcline = 0, dstline = 0; int channelsize = 0; int bufsize = 0; float * W1 = NULL, * W2 = NULL, * imgbuf = NULL; int time = 0; # If defined (_ inc_windows) time = gettickcount (); # Elif defined (_ clock_t) time = clock (); # endifif (! Check_rect (width, height, left, top, right, bottom) {return-1 ;}// crop the width and height of int rc_width = right-left; int rc_height = bottom-top; channelsize = rc_width * rc_height; bufsize = rc_width> rc_height? Rc_width + 4: rc_height + 4; W1 = (float *) malloc (bufsize * sizeof (float); If (! W1) {return-1;} W2 = (float *) malloc (bufsize * sizeof (float); If (! W2) {free (W1); Return-1;} imgbuf = (float *) malloc (channelsize * sizeof (float); If (! Imgbuf) {free (W1); free (W2); Return-1 ;}// ------------------ calculate Gaussian Kernel --------------------------------------- // float q = 0; float q2 = 0, q3 = 0; float b0 = 0, b1 = 0, b2 = 0, B3 = 0; float B = 0; If (Sigma> = 2.5f) {q = 0.98711f * Sigma-0.96330f;} else if (Sigma> = 0.5f) & (Sigma <2.5f) {q = 3.97156f-4.14554f * (float) SQRT (1.0f-0.26891f * sigma);} else {q = 0.1147705018520355224609375f;} q2 = Q * q; Q3 = Q * Q2; b0 = (1.57825f + (2.44413f * q) + (1.4281f * q2) + (0.422205f * Q3); b1 = (2.44413f * q) + (2.85619f * q2) + (1.26661f * Q3); b2 = (-(1.4281f * q2) + (1.26661f * Q3 ))); b3 = (0.422205f * Q3); B = 1.0f-(b1 + B2 + B3)/B0); B1/= b0; B2/= b0; b3/= b0; // ---------------- calculate Gaussian Kernel end ----------------------------------------- // process multiple channels of the image for (Channel = 0; Channel <cbyte; ++ channel) {// obtain all the pixel values of a channel and pre-process srcline = top * linebytes + L EFT * cbyte + channel; dstline = 0; For (y = 0; y <rc_height; ++ y, srcline + = linebytes, dstline + = rc_width) {for (x = 0, n = 0; x <rc_width; ++ X, N + = cbyte) {(imgbuf + dstline) [x] = float (image + srcline) [N]) ;}}for (INT x = 0; x <rc_width; ++ X) {// horizontal processing W1 [0] = (imgbuf + x) [0]; W1 [1] = (imgbuf + x) [0]; w1 [2] = (imgbuf + x) [0]; for (y = 0, n = 0; y <rc_height; ++ y, N + = rc_width) {W1 [Y + 3] = B * (imgbuf + x) [N] + (b1 * W1 [Y + 2] + B2 * W1 [Y + 1] + B3 * W1 [Y + 0]);} W2 [rc_height + 0] = W1 [rc_height + 2]; W2 [rc_height + 1] = W1 [rc_height + 1]; w2 [rc_height + 2] = W1 [rc_height + 0]; for (INT y = rc_height-1, n = y * rc_width; y> = 0; -- y, N-= rc_width) {(imgbuf + x) [N] = W2 [y] = B * W1 [Y + 3] + (b1 * W2 [Y + 1] + B2 * W2 [Y + 2] + B3 * W2 [Y + 3]) ;}} // horizontal processing srcline = 0; dstline = top * linebytes + left * cbyte + channel; For (y = 0; y <rc_height; ++ y, srcline + = rc_width, dstline + = linebytes) {// vertical processing // obtain the data of the current row W1 [0] = (imgbuf + srcline) [0]; W1 [1] = (imgbuf + srcline) [0]; W1 [2] = (imgbuf + srcline) [0]; // For (x = 0; x <rc_width; ++ X) {W1 [x + 3] = B * (imgbuf + srcline) [x] + (b1 * W1 [x + 2] + B2 * W1 [x + 1] + B3 * W1 [x + 0]);} w2 [rc_width + 0] = W1 [rc_width + 2]; W2 [rc_width + 1] = W1 [rc_width + 1]; w2 [rc_width + 2] = W1 [rc_width + 0]; // reverse processing for (x = rc_width-1, n = x * cbyte; x> = 0; -- X, n-= cbyte) {// (imgbuf + srcline) [x] = W2 [x] = B * W1 [x + 3] + (b1 * W2 [x + 1] + B2 * W2 [x + 2] + B3 * W2 [x + 3]); (image + dstline) [N] = W2 [x] = B * W1 [x + 3] + (b1 * W2 [x + 1] + B2 * W2 [x + 2] + B3 * W2 [x + 3]) ;}} // vertically processed/* // The channel for storing the processed data (INT y = 0; y <rc_height; ++ y) {int dstline = (Y + top) * linebytes + left * cbyte; int srcline = y * rc_width; For (INT x = 0, n = channel; x <rc_width; ++ X, N + = cbyte) {(image + dstline) [N] = (imgbuf + srcline) [x]; // byte_edge (imgbuf + srcline) [x]);} // storage cycle // */} // channel loop F REE (W1); W1 = NULL; free (W2); W2 = NULL; free (imgbuf); imgbuf = NULL; # If defined (_ inc_windows) return gettickcount () -time; # Elif defined (_ clock_t) return clock ()-time; # elsereturn 0; # endif} int gauss_blur (byte_t * image, // bitmap data int linebytes, // The number of bytes of the bitmap row. The BMP data is 4 bytes aligned in windows. Otherwise, int width, // bitmap width int height, // bitmap height int cbyte, // Number of Color Channels float Sigma // Gaussian coefficient will be involved when processing non-quadratic power images) {return gauss_blur (image, linebytes, width, height, cbyte, 0, 0, 0, SIGMA );}

Here is the tailoring:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.