-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCUtility.java
120 lines (94 loc) · 4.05 KB
/
SCUtility.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
*****************************************************************************
* Compilation: javac SCUtility.java
* Execution: none
* Dependencies: SeamCarver.java
*
* Some utility functions for testing SeamCarver.java.
*
*****************************************************************************
*/
import edu.princeton.cs.algs4.Picture;
import edu.princeton.cs.algs4.StdRandom;
import java.awt.Color;
public class SCUtility {
// create random width-by-height array of tiles
public static Picture randomPicture(int width, int height) {
Picture picture = new Picture(width, height);
for (int col = 0; col < width; col++) {
for (int row = 0; row < height; row++) {
int r = StdRandom.uniform(255);
int g = StdRandom.uniform(255);
int b = StdRandom.uniform(255);
Color color = new Color(r, g, b);
picture.set(col, row, color);
}
}
return picture;
}
public static double[][] toEnergyMatrix(SeamCarver sc) {
double[][] returnDouble = new double[sc.width()][sc.height()];
for (int col = 0; col < sc.width(); col++)
for (int row = 0; row < sc.height(); row++)
returnDouble[col][row] = sc.energy(col, row);
return returnDouble;
}
// displays grayvalues as energy (converts to picture, calls show)
public static void showEnergy(SeamCarver sc) {
doubleToPicture(toEnergyMatrix(sc)).show();
}
public static Picture toEnergyPicture(SeamCarver sc) {
double[][] energyMatrix = toEnergyMatrix(sc);
return doubleToPicture(energyMatrix);
}
// converts a double matrix of values into a normalized picture
// values are normalized by the maximum grayscale value (ignoring border pixels)
public static Picture doubleToPicture(double[][] grayValues) {
// each 1D array in the matrix represents a single column, so number
// of 1D arrays is the width, and length of each array is the height
int width = grayValues.length;
int height = grayValues[0].length;
Picture picture = new Picture(width, height);
// maximum grayscale value (ignoring border pixels)
double maxVal = 0;
for (int col = 1; col < width - 1; col++) {
for (int row = 1; row < height - 1; row++) {
if (grayValues[col][row] > maxVal)
maxVal = grayValues[col][row];
}
}
if (maxVal == 0)
return picture; // return black picture
for (int col = 0; col < width; col++) {
for (int row = 0; row < height; row++) {
float normalizedGrayValue = (float) grayValues[col][row] / (float) maxVal;
if (normalizedGrayValue >= 1.0f) normalizedGrayValue = 1.0f;
picture.set(col, row, new Color(normalizedGrayValue, normalizedGrayValue,
normalizedGrayValue));
}
}
return picture;
}
// This method is useful for debugging seams. It overlays red
// pixels over the calculate seam. Due to the lack of a copy
// constructor, it also alters the original picture.
public static Picture seamOverlay(Picture picture, boolean horizontal, int[] seamIndices) {
Picture overlaid = new Picture(picture.width(), picture.height());
int width = picture.width();
int height = picture.height();
for (int col = 0; col < width; col++)
for (int row = 0; row < height; row++)
overlaid.set(col, row, picture.get(col, row));
// if horizontal seam, then set one pixel in every column
if (horizontal) {
for (int col = 0; col < width; col++)
overlaid.set(col, seamIndices[col], Color.RED);
}
// if vertical, put one pixel in every row
else {
for (int row = 0; row < height; row++)
overlaid.set(seamIndices[row], row, Color.RED);
}
return overlaid;
}
}