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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
#include "stdafx.h"
#include "ImageSet.h"
#include "Exception.h"
namespace storm {
ImageSet::ImageSet() : images(null) {}
ImageSet::ImageSet(Image *image) : images(null) {
push(image);
}
ImageSet::ImageSet(const ImageSet &o) : images(null) {
if (o.images) {
images = runtime::allocArray<Image *>(engine(), &pointerArrayType, o.images->filled);
for (size_t i = 0; i < o.images->filled; i++)
images->v[i] = o.images->v[i];
images->filled = o.images->filled;
}
}
void ImageSet::deepCopy(CloneEnv *env) {
if (empty())
return;
for (size_t i = 0; i < images->filled; i++) {
images->v[i] = clone(images->v[i], env);
}
}
Image *ImageSet::at(Nat i) const {
if (i >= count())
throw new (this) ArrayError(i, count());
return images->v[i];
}
static Bool lessEq(Image *a, Image *b) {
return a->width() != b->width()
? a->width() <= b->width()
: a->height() <= b->height();
}
void ImageSet::push(Image *image) {
if (!images || images->filled >= images->count)
grow();
size_t pos = images->filled;
for (; pos > 0; pos--) {
Image *here = images->v[pos - 1];
if (lessEq(here, image))
break;
images->v[pos] = here;
}
images->v[pos] = image;
images->filled++;
}
void ImageSet::grow() {
if (images) {
GcArray<Image *> *n = runtime::allocArray<Image *>(engine(), &pointerArrayType, images->count * 2);
for (size_t i = 0; i < images->count; i++)
n->v[i] = images->v[i];
n->filled = images->filled;
images = n;
} else {
images = runtime::allocArray<Image *>(engine(), &pointerArrayType, 8);
}
}
static bool compareImage(geometry::Size size, Image *image) {
return size.w != Float(image->width())
? size.w < Float(image->width())
: size.h < Float(image->height());
}
Image *ImageSet::closest(geometry::Size size) {
if (empty())
throw new (this) ArrayError(1, 0);
Image **begin = images->v;
Image **end = images->v + images->filled;
Image **found = std::upper_bound(begin, end, size, compareImage);
// 'found' is the first element "larger than" size.
if (found == begin) {
// No previous element to look at, pick the smallest.
return *begin;
}
// If 'found' is the end element, then there are no elements larger:
if (found == end) {
return end[-1];
}
// Two options, pick the closest one.
Image *a = found[-1];
Image *b = found[0];
if (a->width() == b->width()) {
if (abs(Float(a->height()) - size.h) < abs(Float(b->height()) - size.h))
return a;
else
return b;
} else {
if (abs(Float(a->width()) - size.w) < abs(Float(b->width()) - size.w))
return a;
else
return b;
}
}
Image *ImageSet::closest(Nat width, Nat height) {
return closest(geometry::Size(Float(width), Float(height)));
}
Image *ImageSet::atLeast(geometry::Size size) {
if (empty())
throw new (this) ArrayError(1, 0);
Image **begin = images->v;
Image **end = images->v + images->filled;
Image **found = std::upper_bound(begin, end, size, compareImage);
// 'found' is the first element "larger than" size.
if (found == begin) {
// No previous element to look at, pick the smallest.
return *begin;
}
// If 'found' is the end element, then there are no elements larger:
if (found == end) {
return end[-1];
}
// Two options, pick "a" if it is exactly the requested size.
Image *a = found[-1];
Image *b = found[0];
if (Float(a->width()) == size.w && Float(a->height()) == size.h)
return a;
else
return b;
}
Image *ImageSet::atLeast(Nat width, Nat height) {
return atLeast(geometry::Size(Float(width), Float(height)));
}
void ImageSet::toS(StrBuf *to) const {
*to << S("(dimensions: ");
for (Nat i = 0; i < count(); i++) {
if (i > 0)
*to << S(", ");
Image *x = at(i);
*to << x->width() << S("x") << x->height();
}
*to << S(")");
}
}
|