#define min(a,b) ((a)<(b)?(a):(b)) #include #include #include #include #include int greyscale(gdImagePtr img, int c) { int r = gdImageRed(img, c); int g = gdImageGreen(img, c); int b = gdImageBlue(img, c); return (int)((r+g+b)/3.+0.5); } void gradient(int *imageGradient, gdImagePtr img, int sx, int sy) { // static int imageGradient[sx][sy]; int up, down, right, left, center; int i, j; // 0,0 right = greyscale(img, gdImageGetPixel(img, 1, 0)); down = greyscale(img, gdImageGetPixel(img, 0, 1)); center = greyscale(img, gdImageGetPixel(img, 0, 0)); imageGradient[0 * sy + 0] = (int)(1./sqrt(3.)*sqrt((right-center)*(right-center)+(center-down)*(center-down))+0.5); // sx-1,0 left = greyscale(img, gdImageGetPixel(img, sx-2, 0)); down = greyscale(img, gdImageGetPixel(img, sx-1, 1)); center = greyscale(img, gdImageGetPixel(img, sx-1, 0)); imageGradient[(sx-1) * sy + 0] = (int)(1./sqrt(3.)*sqrt((center-left)*(center-left)+(center-down)*(center-down))+0.5); // 0,sy-1 right = greyscale(img, gdImageGetPixel(img, 1, sy-1)); up = greyscale(img, gdImageGetPixel(img, 0, sy-2)); center = greyscale(img, gdImageGetPixel(img, 0, sy-1)); imageGradient[0 * sy + (sy-1)] = (int)(1./sqrt(3.)*sqrt((right-center)*(right-center)+(up-center)*(up-center))+0.5); // sx-1,sy-1 left = greyscale(img, gdImageGetPixel(img, sx-2, sy-1)); up = greyscale(img, gdImageGetPixel(img, sx-1, sy-2)); center = greyscale(img, gdImageGetPixel(img, sx-1, sy-1)); imageGradient[(sx-1) * sy + (sy-1)] = (int)(1./sqrt(3.)*sqrt((center-left)*(center-left)+(up-center)*(up-center))+0.5); // up line for (i=1;i=0;--i) { weight[i][0] = min(weight[i+1][0],weight[i+1][1]) + imageGradient[i * sy + 0]; weight[i][sy-1] = min(weight[i+1][sy-1], weight[i+1][sy-2]) + imageGradient[i * sy + (sy-1)]; for (j=1;j=0.5)?(bestj[i-1]+1):(bestj[i-1]-1); else if (weight[i][bestj[i-1]-1] <= weight[i][bestj[i-1]] && weight[i][bestj[i-1]-1] <= weight[i][bestj[i-1]+1]) bestj[i] = bestj[i-1]-1; else bestj[i] = bestj[i-1]+1; } imgout = gdImageCreateTrueColor(sx, sy-1); int jj; // int newImageGradient[sx-1][sy-1]; for(i=0;i