summaryrefslogtreecommitdiffstats
path: root/Algorithms/Part-II/2-SeamCarving/PrintSeams.java
blob: 2611d4838eefbd1a4028cb52fe1bdc2dc14c67b6 (plain)
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
/*************************************************************************
 *  Compilation:  javac PrintSeams.java
 *  Execution:    java PrintSeams input.png
 *  Dependencies: SeamCarver.java SCUtility.java Picture.java StdDraw.java
 *
 *
 *  Read image from file specified as command line argument. Print energy
 *  of each image, as well as both seams, and the total energy of each seam.
 *
 *************************************************************************/

public class PrintSeams {

    private static void printHorizontalSeam(SeamCarver sc)
    {
        double totalSeamEnergy = 0;

        int[] horizontalSeam = sc.findHorizontalSeam();
        for (int j = 0; j < sc.height(); j++)
        {
            for (int i = 0; i < sc.width(); i++)
            {
                char lMarker = ' ';
                char rMarker = ' ';
                if (j == horizontalSeam[i])
                {
                    lMarker = '[';
                    rMarker = ']';
                    totalSeamEnergy += sc.energy(i, j);
                }

                System.out.printf("%c%6.0f%c ", lMarker, sc.energy(i, j), rMarker);
            }
            System.out.println();
        }

        System.out.printf("\nTotal energy: %.0f\n\n", totalSeamEnergy);
    }


    private static void printVerticalSeam(SeamCarver sc)
    {
        double totalSeamEnergy = 0;

        int[] verticalSeam = sc.findVerticalSeam();
        for (int j = 0; j < sc.height(); j++)
        {
            for (int i = 0; i < sc.width(); i++)
            {
                char lMarker = ' ';
                char rMarker = ' ';
                if (i == verticalSeam[j])
                {
                    lMarker = '[';
                    rMarker = ']';
                    totalSeamEnergy += sc.energy(i, j);
                }

                System.out.printf("%c%6.0f%c ", lMarker, sc.energy(i, j), rMarker);
            }

            System.out.println();
        }

        System.out.printf("\nTotal energy: %.0f\n\n", totalSeamEnergy);
    }

    public static void main(String[] args)
    {
        Picture inputImg = new Picture(args[0]);
        System.out.printf("image is %d columns by %d rows\n",
                          inputImg.width(), inputImg.height());
        //inputImg.show();
        SeamCarver sc = new SeamCarver(inputImg);

        System.out.printf("Displaying horizontal seam calculated.\n");
        printHorizontalSeam(sc);

        System.out.printf("Displaying vertical seam calculated.\n");
        printVerticalSeam(sc);


    }

}