gfx/skia/trunk/src/images/bmpdecoderhelper.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1
michael@0 2 /*
michael@0 3 * Copyright 2007 The Android Open Source Project
michael@0 4 *
michael@0 5 * Use of this source code is governed by a BSD-style license that can be
michael@0 6 * found in the LICENSE file.
michael@0 7 */
michael@0 8
michael@0 9 // Author: cevans@google.com (Chris Evans)
michael@0 10
michael@0 11 #include "bmpdecoderhelper.h"
michael@0 12
michael@0 13 namespace image_codec {
michael@0 14
michael@0 15 static const int kBmpHeaderSize = 14;
michael@0 16 static const int kBmpInfoSize = 40;
michael@0 17 static const int kBmpOS2InfoSize = 12;
michael@0 18 static const int kMaxDim = SHRT_MAX / 2;
michael@0 19
michael@0 20 bool BmpDecoderHelper::DecodeImage(const char* p,
michael@0 21 size_t len,
michael@0 22 int max_pixels,
michael@0 23 BmpDecoderCallback* callback) {
michael@0 24 data_ = reinterpret_cast<const uint8*>(p);
michael@0 25 pos_ = 0;
michael@0 26 len_ = len;
michael@0 27 inverted_ = true;
michael@0 28 // Parse the header structure.
michael@0 29 if (len < kBmpHeaderSize + 4) {
michael@0 30 return false;
michael@0 31 }
michael@0 32 GetShort(); // Signature.
michael@0 33 GetInt(); // Size.
michael@0 34 GetInt(); // Reserved.
michael@0 35 int offset = GetInt();
michael@0 36 // Parse the info structure.
michael@0 37 int infoSize = GetInt();
michael@0 38 if (infoSize != kBmpOS2InfoSize && infoSize < kBmpInfoSize) {
michael@0 39 return false;
michael@0 40 }
michael@0 41 int cols = 0;
michael@0 42 int comp = 0;
michael@0 43 int colLen = 4;
michael@0 44 if (infoSize >= kBmpInfoSize) {
michael@0 45 if (len < kBmpHeaderSize + kBmpInfoSize) {
michael@0 46 return false;
michael@0 47 }
michael@0 48 width_ = GetInt();
michael@0 49 height_ = GetInt();
michael@0 50 GetShort(); // Planes.
michael@0 51 bpp_ = GetShort();
michael@0 52 comp = GetInt();
michael@0 53 GetInt(); // Size.
michael@0 54 GetInt(); // XPPM.
michael@0 55 GetInt(); // YPPM.
michael@0 56 cols = GetInt();
michael@0 57 GetInt(); // Important colours.
michael@0 58 } else {
michael@0 59 if (len < kBmpHeaderSize + kBmpOS2InfoSize) {
michael@0 60 return false;
michael@0 61 }
michael@0 62 colLen = 3;
michael@0 63 width_ = GetShort();
michael@0 64 height_ = GetShort();
michael@0 65 GetShort(); // Planes.
michael@0 66 bpp_ = GetShort();
michael@0 67 }
michael@0 68 if (height_ < 0) {
michael@0 69 height_ = -height_;
michael@0 70 inverted_ = false;
michael@0 71 }
michael@0 72 if (width_ <= 0 || width_ > kMaxDim || height_ <= 0 || height_ > kMaxDim) {
michael@0 73 return false;
michael@0 74 }
michael@0 75 if (width_ * height_ > max_pixels) {
michael@0 76 return false;
michael@0 77 }
michael@0 78 if (cols < 0 || cols > 256) {
michael@0 79 return false;
michael@0 80 }
michael@0 81 // Allocate then read in the colour map.
michael@0 82 if (cols == 0 && bpp_ <= 8) {
michael@0 83 cols = 1 << bpp_;
michael@0 84 }
michael@0 85 if (bpp_ <= 8 || cols > 0) {
michael@0 86 uint8* colBuf = new uint8[256 * 3];
michael@0 87 memset(colBuf, '\0', 256 * 3);
michael@0 88 colTab_.reset(colBuf);
michael@0 89 }
michael@0 90 if (cols > 0) {
michael@0 91 if (pos_ + (cols * colLen) > len_) {
michael@0 92 return false;
michael@0 93 }
michael@0 94 for (int i = 0; i < cols; ++i) {
michael@0 95 int base = i * 3;
michael@0 96 colTab_[base + 2] = GetByte();
michael@0 97 colTab_[base + 1] = GetByte();
michael@0 98 colTab_[base] = GetByte();
michael@0 99 if (colLen == 4) {
michael@0 100 GetByte();
michael@0 101 }
michael@0 102 }
michael@0 103 }
michael@0 104 // Read in the compression data if necessary.
michael@0 105 redBits_ = 0x7c00;
michael@0 106 greenBits_ = 0x03e0;
michael@0 107 blueBits_ = 0x001f;
michael@0 108 bool rle = false;
michael@0 109 if (comp == 1 || comp == 2) {
michael@0 110 rle = true;
michael@0 111 } else if (comp == 3) {
michael@0 112 if (pos_ + 12 > len_) {
michael@0 113 return false;
michael@0 114 }
michael@0 115 redBits_ = GetInt() & 0xffff;
michael@0 116 greenBits_ = GetInt() & 0xffff;
michael@0 117 blueBits_ = GetInt() & 0xffff;
michael@0 118 }
michael@0 119 redShiftRight_ = CalcShiftRight(redBits_);
michael@0 120 greenShiftRight_ = CalcShiftRight(greenBits_);
michael@0 121 blueShiftRight_ = CalcShiftRight(blueBits_);
michael@0 122 redShiftLeft_ = CalcShiftLeft(redBits_);
michael@0 123 greenShiftLeft_ = CalcShiftLeft(greenBits_);
michael@0 124 blueShiftLeft_ = CalcShiftLeft(blueBits_);
michael@0 125 rowPad_ = 0;
michael@0 126 pixelPad_ = 0;
michael@0 127 int rowLen;
michael@0 128 if (bpp_ == 32) {
michael@0 129 rowLen = width_ * 4;
michael@0 130 pixelPad_ = 1;
michael@0 131 } else if (bpp_ == 24) {
michael@0 132 rowLen = width_ * 3;
michael@0 133 } else if (bpp_ == 16) {
michael@0 134 rowLen = width_ * 2;
michael@0 135 } else if (bpp_ == 8) {
michael@0 136 rowLen = width_;
michael@0 137 } else if (bpp_ == 4) {
michael@0 138 rowLen = width_ / 2;
michael@0 139 if (width_ & 1) {
michael@0 140 rowLen++;
michael@0 141 }
michael@0 142 } else if (bpp_ == 1) {
michael@0 143 rowLen = width_ / 8;
michael@0 144 if (width_ & 7) {
michael@0 145 rowLen++;
michael@0 146 }
michael@0 147 } else {
michael@0 148 return false;
michael@0 149 }
michael@0 150 // Round the rowLen up to a multiple of 4.
michael@0 151 if (rowLen % 4 != 0) {
michael@0 152 rowPad_ = 4 - (rowLen % 4);
michael@0 153 rowLen += rowPad_;
michael@0 154 }
michael@0 155
michael@0 156 if (offset > 0 && (size_t)offset > pos_ && (size_t)offset < len_) {
michael@0 157 pos_ = offset;
michael@0 158 }
michael@0 159 // Deliberately off-by-one; a load of BMPs seem to have their last byte
michael@0 160 // missing.
michael@0 161 if (!rle && (pos_ + (rowLen * height_) > len_ + 1)) {
michael@0 162 return false;
michael@0 163 }
michael@0 164
michael@0 165 output_ = callback->SetSize(width_, height_);
michael@0 166 if (NULL == output_) {
michael@0 167 return true; // meaning we succeeded, but they want us to stop now
michael@0 168 }
michael@0 169
michael@0 170 if (rle && (bpp_ == 4 || bpp_ == 8)) {
michael@0 171 DoRLEDecode();
michael@0 172 } else {
michael@0 173 DoStandardDecode();
michael@0 174 }
michael@0 175 return true;
michael@0 176 }
michael@0 177
michael@0 178 void BmpDecoderHelper::DoRLEDecode() {
michael@0 179 static const uint8 RLE_ESCAPE = 0;
michael@0 180 static const uint8 RLE_EOL = 0;
michael@0 181 static const uint8 RLE_EOF = 1;
michael@0 182 static const uint8 RLE_DELTA = 2;
michael@0 183 int x = 0;
michael@0 184 int y = height_ - 1;
michael@0 185 while (pos_ + 1 < len_) {
michael@0 186 uint8 cmd = GetByte();
michael@0 187 if (cmd != RLE_ESCAPE) {
michael@0 188 uint8 pixels = GetByte();
michael@0 189 int num = 0;
michael@0 190 uint8 col = pixels;
michael@0 191 while (cmd-- && x < width_) {
michael@0 192 if (bpp_ == 4) {
michael@0 193 if (num & 1) {
michael@0 194 col = pixels & 0xf;
michael@0 195 } else {
michael@0 196 col = pixels >> 4;
michael@0 197 }
michael@0 198 }
michael@0 199 PutPixel(x++, y, col);
michael@0 200 num++;
michael@0 201 }
michael@0 202 } else {
michael@0 203 cmd = GetByte();
michael@0 204 if (cmd == RLE_EOF) {
michael@0 205 return;
michael@0 206 } else if (cmd == RLE_EOL) {
michael@0 207 x = 0;
michael@0 208 y--;
michael@0 209 if (y < 0) {
michael@0 210 return;
michael@0 211 }
michael@0 212 } else if (cmd == RLE_DELTA) {
michael@0 213 if (pos_ + 1 < len_) {
michael@0 214 uint8 dx = GetByte();
michael@0 215 uint8 dy = GetByte();
michael@0 216 x += dx;
michael@0 217 if (x > width_) {
michael@0 218 x = width_;
michael@0 219 }
michael@0 220 y -= dy;
michael@0 221 if (y < 0) {
michael@0 222 return;
michael@0 223 }
michael@0 224 }
michael@0 225 } else {
michael@0 226 int num = 0;
michael@0 227 int bytesRead = 0;
michael@0 228 uint8 val = 0;
michael@0 229 while (cmd-- && pos_ < len_) {
michael@0 230 if (bpp_ == 8 || !(num & 1)) {
michael@0 231 val = GetByte();
michael@0 232 bytesRead++;
michael@0 233 }
michael@0 234 uint8 col = val;
michael@0 235 if (bpp_ == 4) {
michael@0 236 if (num & 1) {
michael@0 237 col = col & 0xf;
michael@0 238 } else {
michael@0 239 col >>= 4;
michael@0 240 }
michael@0 241 }
michael@0 242 if (x < width_) {
michael@0 243 PutPixel(x++, y, col);
michael@0 244 }
michael@0 245 num++;
michael@0 246 }
michael@0 247 // All pixel runs must be an even number of bytes - skip a byte if we
michael@0 248 // read an odd number.
michael@0 249 if ((bytesRead & 1) && pos_ < len_) {
michael@0 250 GetByte();
michael@0 251 }
michael@0 252 }
michael@0 253 }
michael@0 254 }
michael@0 255 }
michael@0 256
michael@0 257 void BmpDecoderHelper::PutPixel(int x, int y, uint8 col) {
michael@0 258 CHECK(x >= 0 && x < width_);
michael@0 259 CHECK(y >= 0 && y < height_);
michael@0 260 if (!inverted_) {
michael@0 261 y = height_ - (y + 1);
michael@0 262 }
michael@0 263
michael@0 264 int base = ((y * width_) + x) * 3;
michael@0 265 int colBase = col * 3;
michael@0 266 output_[base] = colTab_[colBase];
michael@0 267 output_[base + 1] = colTab_[colBase + 1];
michael@0 268 output_[base + 2] = colTab_[colBase + 2];
michael@0 269 }
michael@0 270
michael@0 271 void BmpDecoderHelper::DoStandardDecode() {
michael@0 272 int row = 0;
michael@0 273 uint8 currVal = 0;
michael@0 274 for (int h = height_ - 1; h >= 0; h--, row++) {
michael@0 275 int realH = h;
michael@0 276 if (!inverted_) {
michael@0 277 realH = height_ - (h + 1);
michael@0 278 }
michael@0 279 uint8* line = output_ + (3 * width_ * realH);
michael@0 280 for (int w = 0; w < width_; w++) {
michael@0 281 if (bpp_ >= 24) {
michael@0 282 line[2] = GetByte();
michael@0 283 line[1] = GetByte();
michael@0 284 line[0] = GetByte();
michael@0 285 } else if (bpp_ == 16) {
michael@0 286 uint32 val = GetShort();
michael@0 287 line[0] = ((val & redBits_) >> redShiftRight_) << redShiftLeft_;
michael@0 288 line[1] = ((val & greenBits_) >> greenShiftRight_) << greenShiftLeft_;
michael@0 289 line[2] = ((val & blueBits_) >> blueShiftRight_) << blueShiftLeft_;
michael@0 290 } else if (bpp_ <= 8) {
michael@0 291 uint8 col;
michael@0 292 if (bpp_ == 8) {
michael@0 293 col = GetByte();
michael@0 294 } else if (bpp_ == 4) {
michael@0 295 if ((w % 2) == 0) {
michael@0 296 currVal = GetByte();
michael@0 297 col = currVal >> 4;
michael@0 298 } else {
michael@0 299 col = currVal & 0xf;
michael@0 300 }
michael@0 301 } else {
michael@0 302 if ((w % 8) == 0) {
michael@0 303 currVal = GetByte();
michael@0 304 }
michael@0 305 int bit = w & 7;
michael@0 306 col = ((currVal >> (7 - bit)) & 1);
michael@0 307 }
michael@0 308 int base = col * 3;
michael@0 309 line[0] = colTab_[base];
michael@0 310 line[1] = colTab_[base + 1];
michael@0 311 line[2] = colTab_[base + 2];
michael@0 312 }
michael@0 313 line += 3;
michael@0 314 for (int i = 0; i < pixelPad_; ++i) {
michael@0 315 GetByte();
michael@0 316 }
michael@0 317 }
michael@0 318 for (int i = 0; i < rowPad_; ++i) {
michael@0 319 GetByte();
michael@0 320 }
michael@0 321 }
michael@0 322 }
michael@0 323
michael@0 324 int BmpDecoderHelper::GetInt() {
michael@0 325 uint8 b1 = GetByte();
michael@0 326 uint8 b2 = GetByte();
michael@0 327 uint8 b3 = GetByte();
michael@0 328 uint8 b4 = GetByte();
michael@0 329 return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24);
michael@0 330 }
michael@0 331
michael@0 332 int BmpDecoderHelper::GetShort() {
michael@0 333 uint8 b1 = GetByte();
michael@0 334 uint8 b2 = GetByte();
michael@0 335 return b1 | (b2 << 8);
michael@0 336 }
michael@0 337
michael@0 338 uint8 BmpDecoderHelper::GetByte() {
michael@0 339 CHECK(pos_ <= len_);
michael@0 340 // We deliberately allow this off-by-one access to cater for BMPs with their
michael@0 341 // last byte missing.
michael@0 342 if (pos_ == len_) {
michael@0 343 return 0;
michael@0 344 }
michael@0 345 return data_[pos_++];
michael@0 346 }
michael@0 347
michael@0 348 int BmpDecoderHelper::CalcShiftRight(uint32 mask) {
michael@0 349 int ret = 0;
michael@0 350 while (mask != 0 && !(mask & 1)) {
michael@0 351 mask >>= 1;
michael@0 352 ret++;
michael@0 353 }
michael@0 354 return ret;
michael@0 355 }
michael@0 356
michael@0 357 int BmpDecoderHelper::CalcShiftLeft(uint32 mask) {
michael@0 358 int ret = 0;
michael@0 359 while (mask != 0 && !(mask & 1)) {
michael@0 360 mask >>= 1;
michael@0 361 }
michael@0 362 while (mask != 0 && !(mask & 0x80)) {
michael@0 363 mask <<= 1;
michael@0 364 ret++;
michael@0 365 }
michael@0 366 return ret;
michael@0 367 }
michael@0 368
michael@0 369 } // namespace image_codec

mercurial