int energy(Pixel** image, int x, int y, int width, int height) { int energy = 0, energyX = 0, energyY = 0; int rightPX = 0, rightPY = 0, leftPX = 0, leftPY = 0; int upPX = 0, upPY = 0, downPX = 0, downPY = 0; if (x + 1 == width){ rightPX = 0; rightPY = y; leftPX = x - 1; leftPY = y; } else if (x == 0){ leftPX = width - 1; leftPY = y; rightPX = x + 1; rightPY = y; } else{ leftPX = x - 1; leftPY = y; rightPX = x + 1; rightPY = y; } int redX = (image[rightPX][rightPY].r - image[leftPX][leftPY].r) * (image[rightPX][rightPY].r - image[leftPX][leftPY].r); int greenX = (image[rightPX][rightPY].g - image[leftPX][leftPY].g) * (image[rightPX][rightPY].g - image[leftPX][leftPY].g); int blueX = (image[rightPX][rightPY].b - image[leftPX][leftPY].b) * (image[rightPX][rightPY].b - image[leftPX][leftPY].b); energyX = redX + greenX + blueX; if (y + 1 == height){ upPX = x; upPY = y - 1; downPX = x; downPY = 0; } else if (y == 0){ upPX = x; upPY = height - 1; downPX = x; downPY = y + 1; } else{ upPX = x; upPY = y - 1; downPX = x; downPY = y + 1; } int redY = (image[downPX][downPY].r - image[upPX][upPY].r) * (image[downPX][downPY].r - image[upPX][upPY].r); int greenY = (image[downPX][downPY].g - image[upPX][upPY].g) * (image[downPX][downPY].g - image[upPX][upPY].g); int blueY = (image[downPX][downPY].b - image[upPX][upPY].b) * (image[downPX][downPY].b - image[upPX][upPY].b); energyY = redY + greenY + blueY; energy = energyX + energyY; return energy; }