| 13 | import edu.princeton.cs.algs4.StdRandom; |
| 14 | |
| 15 | public class SCUtility { |
| 16 | |
| 17 | |
| 18 | // create random width-by-height array of tiles |
| 19 | public static Picture randomPicture(int width, int height) { |
| 20 | Picture picture = new Picture(width, height); |
| 21 | for (int col = 0; col < width; col++) { |
| 22 | for (int row = 0; row < height; row++) { |
| 23 | int r = StdRandom.uniform(255); |
| 24 | int g = StdRandom.uniform(255); |
| 25 | int b = StdRandom.uniform(255); |
| 26 | Color color = new Color(r, g, b); |
| 27 | picture.set(col, row, color); |
| 28 | } |
| 29 | } |
| 30 | return picture; |
| 31 | } |
| 32 | |
| 33 | |
| 34 | public static double[][] toEnergyMatrix(SeamCarver sc) { |
| 35 | double[][] returnDouble = new double[sc.width()][sc.height()]; |
| 36 | for (int col = 0; col < sc.width(); col++) |
| 37 | for (int row = 0; row < sc.height(); row++) |
| 38 | returnDouble[col][row] = sc.energy(col, row); |
| 39 | |
| 40 | return returnDouble; |
| 41 | } |
| 42 | |
| 43 | // displays grayvalues as energy (converts to picture, calls show) |
| 44 | public static void showEnergy(SeamCarver sc) { |
| 45 | doubleToPicture(toEnergyMatrix(sc)).show(); |
| 46 | } |
| 47 | |
| 48 | public static Picture toEnergyPicture(SeamCarver sc) { |
| 49 | double[][] energyMatrix = toEnergyMatrix(sc); |
| 50 | return doubleToPicture(energyMatrix); |
| 51 | } |
| 52 | |
| 53 | // converts a double matrix of values into a normalized picture |
| 54 | // values are normalized by the maximum grayscale value (ignoring border pixels) |
| 55 | public static Picture doubleToPicture(double[][] grayValues) { |
| 56 | |
| 57 | // each 1D array in the matrix represents a single column, so number |
| 58 | // of 1D arrays is the width, and length of each array is the height |
| 59 | int width = grayValues.length; |
| 60 | int height = grayValues[0].length; |
| 61 | |
| 62 | Picture picture = new Picture(width, height); |
| 63 | |
| 64 | // maximum grayscale value (ignoring border pixels) |
| 65 | double maxVal = 0; |
| 66 | for (int col = 1; col < width-1; col++) { |
| 67 | for (int row = 1; row < height-1; row++) { |
| 68 | if (grayValues[col][row] > maxVal) |
| 69 | maxVal = grayValues[col][row]; |
| 70 | } |
| 71 | } |
| 72 |
nothing calls this directly
no outgoing calls
no test coverage detected