gfx/skia/trunk/src/images/SkImageDecoder_libbmp.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
michael@0 10 #include "bmpdecoderhelper.h"
michael@0 11 #include "SkColorPriv.h"
michael@0 12 #include "SkImageDecoder.h"
michael@0 13 #include "SkScaledBitmapSampler.h"
michael@0 14 #include "SkStream.h"
michael@0 15 #include "SkStreamHelpers.h"
michael@0 16 #include "SkTDArray.h"
michael@0 17
michael@0 18 class SkBMPImageDecoder : public SkImageDecoder {
michael@0 19 public:
michael@0 20 SkBMPImageDecoder() {}
michael@0 21
michael@0 22 virtual Format getFormat() const SK_OVERRIDE {
michael@0 23 return kBMP_Format;
michael@0 24 }
michael@0 25
michael@0 26 protected:
michael@0 27 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode mode) SK_OVERRIDE;
michael@0 28
michael@0 29 private:
michael@0 30 typedef SkImageDecoder INHERITED;
michael@0 31 };
michael@0 32
michael@0 33 ///////////////////////////////////////////////////////////////////////////////
michael@0 34 DEFINE_DECODER_CREATOR(BMPImageDecoder);
michael@0 35 ///////////////////////////////////////////////////////////////////////////////
michael@0 36
michael@0 37 static bool is_bmp(SkStreamRewindable* stream) {
michael@0 38 static const char kBmpMagic[] = { 'B', 'M' };
michael@0 39
michael@0 40
michael@0 41 char buffer[sizeof(kBmpMagic)];
michael@0 42
michael@0 43 return stream->read(buffer, sizeof(kBmpMagic)) == sizeof(kBmpMagic) &&
michael@0 44 !memcmp(buffer, kBmpMagic, sizeof(kBmpMagic));
michael@0 45 }
michael@0 46
michael@0 47 static SkImageDecoder* sk_libbmp_dfactory(SkStreamRewindable* stream) {
michael@0 48 if (is_bmp(stream)) {
michael@0 49 return SkNEW(SkBMPImageDecoder);
michael@0 50 }
michael@0 51 return NULL;
michael@0 52 }
michael@0 53
michael@0 54 static SkImageDecoder_DecodeReg gReg(sk_libbmp_dfactory);
michael@0 55
michael@0 56 static SkImageDecoder::Format get_format_bmp(SkStreamRewindable* stream) {
michael@0 57 if (is_bmp(stream)) {
michael@0 58 return SkImageDecoder::kBMP_Format;
michael@0 59 }
michael@0 60 return SkImageDecoder::kUnknown_Format;
michael@0 61 }
michael@0 62
michael@0 63 static SkImageDecoder_FormatReg gFormatReg(get_format_bmp);
michael@0 64
michael@0 65 ///////////////////////////////////////////////////////////////////////////////
michael@0 66
michael@0 67 class SkBmpDecoderCallback : public image_codec::BmpDecoderCallback {
michael@0 68 public:
michael@0 69 // we don't copy the bitmap, just remember the pointer
michael@0 70 SkBmpDecoderCallback(bool justBounds) : fJustBounds(justBounds) {}
michael@0 71
michael@0 72 // override from BmpDecoderCallback
michael@0 73 virtual uint8* SetSize(int width, int height) {
michael@0 74 fWidth = width;
michael@0 75 fHeight = height;
michael@0 76 if (fJustBounds) {
michael@0 77 return NULL;
michael@0 78 }
michael@0 79
michael@0 80 fRGB.setCount(width * height * 3); // 3 == r, g, b
michael@0 81 return fRGB.begin();
michael@0 82 }
michael@0 83
michael@0 84 int width() const { return fWidth; }
michael@0 85 int height() const { return fHeight; }
michael@0 86 const uint8_t* rgb() const { return fRGB.begin(); }
michael@0 87
michael@0 88 private:
michael@0 89 SkTDArray<uint8_t> fRGB;
michael@0 90 int fWidth;
michael@0 91 int fHeight;
michael@0 92 bool fJustBounds;
michael@0 93 };
michael@0 94
michael@0 95 bool SkBMPImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
michael@0 96 // First read the entire stream, so that all of the data can be passed to
michael@0 97 // the BmpDecoderHelper.
michael@0 98
michael@0 99 // Allocated space used to hold the data.
michael@0 100 SkAutoMalloc storage;
michael@0 101 // Byte length of all of the data.
michael@0 102 const size_t length = CopyStreamToStorage(&storage, stream);
michael@0 103 if (0 == length) {
michael@0 104 return 0;
michael@0 105 }
michael@0 106
michael@0 107 const bool justBounds = SkImageDecoder::kDecodeBounds_Mode == mode;
michael@0 108 SkBmpDecoderCallback callback(justBounds);
michael@0 109
michael@0 110 // Now decode the BMP into callback's rgb() array [r,g,b, r,g,b, ...]
michael@0 111 {
michael@0 112 image_codec::BmpDecoderHelper helper;
michael@0 113 const int max_pixels = 16383*16383; // max width*height
michael@0 114 if (!helper.DecodeImage((const char*)storage.get(), length,
michael@0 115 max_pixels, &callback)) {
michael@0 116 return false;
michael@0 117 }
michael@0 118 }
michael@0 119
michael@0 120 // we don't need this anymore, so free it now (before we try to allocate
michael@0 121 // the bitmap's pixels) rather than waiting for its destructor
michael@0 122 storage.free();
michael@0 123
michael@0 124 int width = callback.width();
michael@0 125 int height = callback.height();
michael@0 126 SkBitmap::Config config = this->getPrefConfig(k32Bit_SrcDepth, false);
michael@0 127
michael@0 128 // only accept prefConfig if it makes sense for us
michael@0 129 if (SkBitmap::kARGB_4444_Config != config &&
michael@0 130 SkBitmap::kRGB_565_Config != config) {
michael@0 131 config = SkBitmap::kARGB_8888_Config;
michael@0 132 }
michael@0 133
michael@0 134 SkScaledBitmapSampler sampler(width, height, getSampleSize());
michael@0 135
michael@0 136 bm->setConfig(config, sampler.scaledWidth(), sampler.scaledHeight(), 0,
michael@0 137 kOpaque_SkAlphaType);
michael@0 138
michael@0 139 if (justBounds) {
michael@0 140 return true;
michael@0 141 }
michael@0 142
michael@0 143 if (!this->allocPixelRef(bm, NULL)) {
michael@0 144 return false;
michael@0 145 }
michael@0 146
michael@0 147 SkAutoLockPixels alp(*bm);
michael@0 148
michael@0 149 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, *this)) {
michael@0 150 return false;
michael@0 151 }
michael@0 152
michael@0 153 const int srcRowBytes = width * 3;
michael@0 154 const int dstHeight = sampler.scaledHeight();
michael@0 155 const uint8_t* srcRow = callback.rgb();
michael@0 156
michael@0 157 srcRow += sampler.srcY0() * srcRowBytes;
michael@0 158 for (int y = 0; y < dstHeight; y++) {
michael@0 159 sampler.next(srcRow);
michael@0 160 srcRow += sampler.srcDY() * srcRowBytes;
michael@0 161 }
michael@0 162 return true;
michael@0 163 }

mercurial