gfx/2d/unittest/TestScaling.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "TestScaling.h"
michael@0 7
michael@0 8 #include "ImageScaling.h"
michael@0 9
michael@0 10 using namespace mozilla::gfx;
michael@0 11
michael@0 12 TestScaling::TestScaling()
michael@0 13 {
michael@0 14 REGISTER_TEST(TestScaling, BasicHalfScale);
michael@0 15 REGISTER_TEST(TestScaling, DoubleHalfScale);
michael@0 16 REGISTER_TEST(TestScaling, UnevenHalfScale);
michael@0 17 REGISTER_TEST(TestScaling, OddStrideHalfScale);
michael@0 18 REGISTER_TEST(TestScaling, VerticalHalfScale);
michael@0 19 REGISTER_TEST(TestScaling, HorizontalHalfScale);
michael@0 20 REGISTER_TEST(TestScaling, MixedHalfScale);
michael@0 21 }
michael@0 22
michael@0 23 void
michael@0 24 TestScaling::BasicHalfScale()
michael@0 25 {
michael@0 26 std::vector<uint8_t> data;
michael@0 27 data.resize(500 * 500 * 4);
michael@0 28
michael@0 29 uint32_t *pixels = reinterpret_cast<uint32_t*>(&data.front());
michael@0 30 for (int y = 0; y < 500; y += 2) {
michael@0 31 for (int x = 0; x < 500; x += 2) {
michael@0 32 pixels[y * 500 + x] = 0xff00ff00;
michael@0 33 pixels[y * 500 + x + 1] = 0xff00ffff;
michael@0 34 pixels[(y + 1) * 500 + x] = 0xff000000;
michael@0 35 pixels[(y + 1) * 500 + x + 1] = 0xff0000ff;
michael@0 36 }
michael@0 37 }
michael@0 38 ImageHalfScaler scaler(&data.front(), 500 * 4, IntSize(500, 500));
michael@0 39
michael@0 40 scaler.ScaleForSize(IntSize(220, 240));
michael@0 41
michael@0 42 VERIFY(scaler.GetSize().width == 250);
michael@0 43 VERIFY(scaler.GetSize().height == 250);
michael@0 44
michael@0 45 pixels = (uint32_t*)scaler.GetScaledData();
michael@0 46
michael@0 47 for (int y = 0; y < 250; y++) {
michael@0 48 for (int x = 0; x < 250; x++) {
michael@0 49 VERIFY(pixels[y * (scaler.GetStride() / 4) + x] == 0xff007f7f);
michael@0 50 }
michael@0 51 }
michael@0 52 }
michael@0 53
michael@0 54 void
michael@0 55 TestScaling::DoubleHalfScale()
michael@0 56 {
michael@0 57 std::vector<uint8_t> data;
michael@0 58 data.resize(500 * 500 * 4);
michael@0 59
michael@0 60 uint32_t *pixels = reinterpret_cast<uint32_t*>(&data.front());
michael@0 61 for (int y = 0; y < 500; y += 2) {
michael@0 62 for (int x = 0; x < 500; x += 2) {
michael@0 63 pixels[y * 500 + x] = 0xff00ff00;
michael@0 64 pixels[y * 500 + x + 1] = 0xff00ffff;
michael@0 65 pixels[(y + 1) * 500 + x] = 0xff000000;
michael@0 66 pixels[(y + 1) * 500 + x + 1] = 0xff0000ff;
michael@0 67 }
michael@0 68 }
michael@0 69 ImageHalfScaler scaler(&data.front(), 500 * 4, IntSize(500, 500));
michael@0 70
michael@0 71 scaler.ScaleForSize(IntSize(120, 110));
michael@0 72 VERIFY(scaler.GetSize().width == 125);
michael@0 73 VERIFY(scaler.GetSize().height == 125);
michael@0 74
michael@0 75 pixels = (uint32_t*)scaler.GetScaledData();
michael@0 76
michael@0 77 for (int y = 0; y < 125; y++) {
michael@0 78 for (int x = 0; x < 125; x++) {
michael@0 79 VERIFY(pixels[y * (scaler.GetStride() / 4) + x] == 0xff007f7f);
michael@0 80 }
michael@0 81 }
michael@0 82 }
michael@0 83
michael@0 84 void
michael@0 85 TestScaling::UnevenHalfScale()
michael@0 86 {
michael@0 87 std::vector<uint8_t> data;
michael@0 88 // Use a 16-byte aligned stride still, we test none-aligned strides
michael@0 89 // separately.
michael@0 90 data.resize(499 * 500 * 4);
michael@0 91
michael@0 92 uint32_t *pixels = reinterpret_cast<uint32_t*>(&data.front());
michael@0 93 for (int y = 0; y < 500; y += 2) {
michael@0 94 for (int x = 0; x < 500; x += 2) {
michael@0 95 pixels[y * 500 + x] = 0xff00ff00;
michael@0 96 if (x < 498) {
michael@0 97 pixels[y * 500 + x + 1] = 0xff00ffff;
michael@0 98 }
michael@0 99 if (y < 498) {
michael@0 100 pixels[(y + 1) * 500 + x] = 0xff000000;
michael@0 101 if (x < 498) {
michael@0 102 pixels[(y + 1) * 500 + x + 1] = 0xff0000ff;
michael@0 103 }
michael@0 104 }
michael@0 105 }
michael@0 106 }
michael@0 107 ImageHalfScaler scaler(&data.front(), 500 * 4, IntSize(499, 499));
michael@0 108
michael@0 109 scaler.ScaleForSize(IntSize(220, 220));
michael@0 110 VERIFY(scaler.GetSize().width == 249);
michael@0 111 VERIFY(scaler.GetSize().height == 249);
michael@0 112
michael@0 113 pixels = (uint32_t*)scaler.GetScaledData();
michael@0 114
michael@0 115 for (int y = 0; y < 249; y++) {
michael@0 116 for (int x = 0; x < 249; x++) {
michael@0 117 VERIFY(pixels[y * (scaler.GetStride() / 4) + x] == 0xff007f7f);
michael@0 118 }
michael@0 119 }
michael@0 120 }
michael@0 121
michael@0 122 void
michael@0 123 TestScaling::OddStrideHalfScale()
michael@0 124 {
michael@0 125 std::vector<uint8_t> data;
michael@0 126 // Use a 4-byte aligned stride to test if that doesn't cause any issues.
michael@0 127 data.resize(499 * 499 * 4);
michael@0 128
michael@0 129 uint32_t *pixels = reinterpret_cast<uint32_t*>(&data.front());
michael@0 130 for (int y = 0; y < 500; y += 2) {
michael@0 131 for (int x = 0; x < 500; x += 2) {
michael@0 132 pixels[y * 499 + x] = 0xff00ff00;
michael@0 133 if (x < 498) {
michael@0 134 pixels[y * 499 + x + 1] = 0xff00ffff;
michael@0 135 }
michael@0 136 if (y < 498) {
michael@0 137 pixels[(y + 1) * 499 + x] = 0xff000000;
michael@0 138 if (x < 498) {
michael@0 139 pixels[(y + 1) * 499 + x + 1] = 0xff0000ff;
michael@0 140 }
michael@0 141 }
michael@0 142 }
michael@0 143 }
michael@0 144 ImageHalfScaler scaler(&data.front(), 499 * 4, IntSize(499, 499));
michael@0 145
michael@0 146 scaler.ScaleForSize(IntSize(220, 220));
michael@0 147 VERIFY(scaler.GetSize().width == 249);
michael@0 148 VERIFY(scaler.GetSize().height == 249);
michael@0 149
michael@0 150 pixels = (uint32_t*)scaler.GetScaledData();
michael@0 151
michael@0 152 for (int y = 0; y < 249; y++) {
michael@0 153 for (int x = 0; x < 249; x++) {
michael@0 154 VERIFY(pixels[y * (scaler.GetStride() / 4) + x] == 0xff007f7f);
michael@0 155 }
michael@0 156 }
michael@0 157 }
michael@0 158 void
michael@0 159 TestScaling::VerticalHalfScale()
michael@0 160 {
michael@0 161 std::vector<uint8_t> data;
michael@0 162 data.resize(500 * 500 * 4);
michael@0 163
michael@0 164 uint32_t *pixels = reinterpret_cast<uint32_t*>(&data.front());
michael@0 165 for (int y = 0; y < 500; y += 2) {
michael@0 166 for (int x = 0; x < 500; x += 2) {
michael@0 167 pixels[y * 500 + x] = 0xff00ff00;
michael@0 168 pixels[y * 500 + x + 1] = 0xff00ffff;
michael@0 169 pixels[(y + 1) * 500 + x] = 0xff000000;
michael@0 170 pixels[(y + 1) * 500 + x + 1] = 0xff0000ff;
michael@0 171 }
michael@0 172 }
michael@0 173 ImageHalfScaler scaler(&data.front(), 500 * 4, IntSize(500, 500));
michael@0 174
michael@0 175 scaler.ScaleForSize(IntSize(400, 240));
michael@0 176 VERIFY(scaler.GetSize().width == 500);
michael@0 177 VERIFY(scaler.GetSize().height == 250);
michael@0 178
michael@0 179 pixels = (uint32_t*)scaler.GetScaledData();
michael@0 180
michael@0 181 for (int y = 0; y < 250; y++) {
michael@0 182 for (int x = 0; x < 500; x += 2) {
michael@0 183 VERIFY(pixels[y * (scaler.GetStride() / 4) + x] == 0xff007f00);
michael@0 184 VERIFY(pixels[y * (scaler.GetStride() / 4) + x + 1] == 0xff007fff);
michael@0 185 }
michael@0 186 }
michael@0 187 }
michael@0 188
michael@0 189 void
michael@0 190 TestScaling::HorizontalHalfScale()
michael@0 191 {
michael@0 192 std::vector<uint8_t> data;
michael@0 193 data.resize(520 * 500 * 4);
michael@0 194
michael@0 195 uint32_t *pixels = reinterpret_cast<uint32_t*>(&data.front());
michael@0 196 for (int y = 0; y < 500; y ++) {
michael@0 197 for (int x = 0; x < 520; x += 8) {
michael@0 198 pixels[y * 520 + x] = 0xff00ff00;
michael@0 199 pixels[y * 520 + x + 1] = 0xff00ffff;
michael@0 200 pixels[y * 520 + x + 2] = 0xff000000;
michael@0 201 pixels[y * 520 + x + 3] = 0xff0000ff;
michael@0 202 pixels[y * 520 + x + 4] = 0xffff00ff;
michael@0 203 pixels[y * 520 + x + 5] = 0xff0000ff;
michael@0 204 pixels[y * 520 + x + 6] = 0xffffffff;
michael@0 205 pixels[y * 520 + x + 7] = 0xff0000ff;
michael@0 206 }
michael@0 207 }
michael@0 208 ImageHalfScaler scaler(&data.front(), 520 * 4, IntSize(520, 500));
michael@0 209
michael@0 210 scaler.ScaleForSize(IntSize(240, 400));
michael@0 211 VERIFY(scaler.GetSize().width == 260);
michael@0 212 VERIFY(scaler.GetSize().height == 500);
michael@0 213
michael@0 214 pixels = (uint32_t*)scaler.GetScaledData();
michael@0 215
michael@0 216 for (int y = 0; y < 500; y++) {
michael@0 217 for (int x = 0; x < 260; x += 4) {
michael@0 218 VERIFY(pixels[y * (scaler.GetStride() / 4) + x] == 0xff00ff7f);
michael@0 219 VERIFY(pixels[y * (scaler.GetStride() / 4) + x + 1] == 0xff00007f);
michael@0 220 VERIFY(pixels[y * (scaler.GetStride() / 4) + x + 2] == 0xff7f00ff);
michael@0 221 VERIFY(pixels[y * (scaler.GetStride() / 4) + x + 3] == 0xff7f7fff);
michael@0 222 }
michael@0 223 }
michael@0 224 }
michael@0 225
michael@0 226 void
michael@0 227 TestScaling::MixedHalfScale()
michael@0 228 {
michael@0 229 std::vector<uint8_t> data;
michael@0 230 data.resize(500 * 500 * 4);
michael@0 231
michael@0 232 uint32_t *pixels = reinterpret_cast<uint32_t*>(&data.front());
michael@0 233 for (int y = 0; y < 500; y += 2) {
michael@0 234 for (int x = 0; x < 500; x += 2) {
michael@0 235 pixels[y * 500 + x] = 0xff00ff00;
michael@0 236 pixels[y * 500 + x + 1] = 0xff00ffff;
michael@0 237 pixels[(y + 1) * 500 + x] = 0xff000000;
michael@0 238 pixels[(y + 1) * 500 + x + 1] = 0xff0000ff;
michael@0 239 }
michael@0 240 }
michael@0 241 ImageHalfScaler scaler(&data.front(), 500 * 4, IntSize(500, 500));
michael@0 242
michael@0 243 scaler.ScaleForSize(IntSize(120, 240));
michael@0 244 VERIFY(scaler.GetSize().width == 125);
michael@0 245 VERIFY(scaler.GetSize().height == 250);
michael@0 246 scaler.ScaleForSize(IntSize(240, 120));
michael@0 247 VERIFY(scaler.GetSize().width == 250);
michael@0 248 VERIFY(scaler.GetSize().height == 125);
michael@0 249 }

mercurial