summaryrefslogtreecommitdiffstats
path: root/perftests/panorama/benchmark.cpp
blob: 2a6440f4beb93d7c3c8868644fd6cf33b96dd63c (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
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
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "mosaic/Mosaic.h"
#include "mosaic/ImageUtils.h"

#define MAX_FRAMES 200
#define KERNEL_ITERATIONS 10

const int blendingType = Blend::BLEND_TYPE_HORZ;
const int stripType = Blend::STRIP_TYPE_WIDE;

ImageType yvuFrames[MAX_FRAMES];

int loadImages(const char* basename, int &width, int &height)
{
    char filename[512];
    struct stat filestat;
    int i;

    for (i = 0; i < MAX_FRAMES; i++) {
        sprintf(filename, "%s_%03d.ppm", basename, i + 1);
        if (stat(filename, &filestat) != 0) break;
        ImageType rgbFrame = ImageUtils::readBinaryPPM(filename, width, height);
        yvuFrames[i] = ImageUtils::allocateImage(width, height,
                                ImageUtils::IMAGE_TYPE_NUM_CHANNELS);
        ImageUtils::rgb2yvu(yvuFrames[i], rgbFrame, width, height);
        ImageUtils::freeImage(rgbFrame);
    }
    return i;
}

int main(int argc, char **argv)
{
    struct timespec t1, t2, t3;

    int width, height;
    float totalElapsedTime = 0;

    const char *basename;
    const char *filename;

    if (argc != 3) {
        printf("Usage: %s input_dir output_filename\n", argv[0]);
        return 0;
    } else {
        basename = argv[1];
        filename = argv[2];
    }

    // Load the images outside the computational kernel
    int totalFrames = loadImages(basename, width, height);

    if (totalFrames == 0) {
        printf("Image files not found. Make sure %s exists.\n",
               basename);
        return 1;
    }

    printf("%d frames loaded\n", totalFrames);


    // Interesting stuff is here
    for (int iteration = 0; iteration < KERNEL_ITERATIONS; iteration++)  {
        Mosaic mosaic;

        mosaic.initialize(blendingType, stripType, width, height, -1, false, 0);

        clock_gettime(CLOCK_MONOTONIC, &t1);
        for (int i = 0; i < totalFrames; i++) {
            mosaic.addFrame(yvuFrames[i]);
        }
        clock_gettime(CLOCK_MONOTONIC, &t2);

        float progress = 0.0;
        bool cancelComputation = false;

        mosaic.createMosaic(progress, cancelComputation);

        int mosaicWidth, mosaicHeight;
        ImageType resultYVU = mosaic.getMosaic(mosaicWidth, mosaicHeight);

        ImageType imageRGB = ImageUtils::allocateImage(
            mosaicWidth, mosaicHeight, ImageUtils::IMAGE_TYPE_NUM_CHANNELS);

        clock_gettime(CLOCK_MONOTONIC, &t3);

        float elapsedTime =
            (t3.tv_sec - t1.tv_sec) + (t3.tv_nsec - t1.tv_nsec)/1e9;
        float addImageTime =
            (t2.tv_sec - t1.tv_sec) + (t2.tv_nsec - t1.tv_nsec)/1e9;
        float stitchImageTime =
            (t3.tv_sec - t2.tv_sec) + (t3.tv_nsec - t2.tv_nsec)/1e9;

        totalElapsedTime += elapsedTime;

        printf("Iteration %d: %dx%d moasic created: "
               "%.2f seconds (%.2f + %.2f)\n",
               iteration, mosaicWidth, mosaicHeight,
               elapsedTime, addImageTime, stitchImageTime);

        // Write the output only once for correctness check
        if (iteration == 0) {
            ImageUtils::yvu2rgb(imageRGB, resultYVU, mosaicWidth,
                                mosaicHeight);
            ImageUtils::writeBinaryPPM(imageRGB, filename, mosaicWidth,
                                       mosaicHeight);
        }
    }
    printf("Total elapsed time: %.2f seconds\n", totalElapsedTime);

    return 0;
}