File: vectorimage.cpp

package info (click to toggle)
aoflagger 3.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,960 kB
  • sloc: cpp: 83,076; python: 10,187; sh: 260; makefile: 178
file content (61 lines) | stat: -rw-r--r-- 2,200 bytes parent folder | download | duplicates (2)
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
#include "vectorimage.h"

std::unique_ptr<VectorImage> ShrinkHorizontally(const ImageInterface& source,
                                                size_t shrinkFactor) {
  if (source.Width() == 0) {
    return std::unique_ptr<VectorImage>(new VectorImage());
  } else {
    const size_t newWidth = (source.Width() + shrinkFactor - 1) / shrinkFactor;
    const size_t height = source.Height();

    std::vector<float> newImage;
    newImage.reserve(newWidth * height);

    for (size_t x = 0; x != newWidth; ++x) {
      size_t binSize = shrinkFactor;
      if (binSize + x * shrinkFactor > source.Width())
        binSize = source.Width() - x * shrinkFactor;

      const float* sourceData = source.Data();
      for (size_t y = 0; y != height; ++y) {
        float sum = 0.0;
        const float* sourceRow = &sourceData[y * source.Stride()];
        for (size_t binX = 0; binX < binSize; ++binX) {
          const size_t curX = x * shrinkFactor + binX;
          sum += sourceRow[curX];
        }
        newImage.emplace_back(sum / float(binSize));
      }
    }
    return std::unique_ptr<VectorImage>(
        new VectorImage(std::move(newImage), newWidth));
  }
}

std::unique_ptr<VectorImage> ShrinkVertically(const ImageInterface& source,
                                              size_t shrinkFactor) {
  const size_t sourceHeight = source.Height();
  const size_t newHeight = (sourceHeight + shrinkFactor - 1) / shrinkFactor;

  std::vector<float> newImage;
  newImage.reserve(source.Width() * newHeight);

  const float* sourceData = source.Data();
  for (size_t y = 0; y != newHeight; ++y) {
    size_t binSize = shrinkFactor;
    if (binSize + y * shrinkFactor > sourceHeight)
      binSize = sourceHeight - y * shrinkFactor;

    for (size_t x = 0; x != source.Width(); ++x) {
      float sum = 0.0;
      for (size_t binY = 0; binY < binSize; ++binY) {
        const size_t curY = y * shrinkFactor + binY;
        const float* sourceRow = &sourceData[curY * source.Stride()];
        sum += sourceRow[x];
      }
      newImage.emplace_back(sum / float(binSize));
    }
  }
  return std::unique_ptr<VectorImage>(
      new VectorImage(std::move(newImage), source.Width()));
}