| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2007, The Android Open Source Project | |
| 3 * | |
| 4 * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 * you may not use this file except in compliance with the License. | |
| 6 * You may obtain a copy of the License at | |
| 7 * | |
| 8 * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 * | |
| 10 * Unless required by applicable law or agreed to in writing, software | |
| 11 * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 * See the License for the specific language governing permissions and | |
| 14 * limitations under the License. | |
| 15 */ | |
| 16 | |
| 17 #include "bmpdecoderhelper.h" | |
| 18 #include "SkImageDecoder.h" | |
| 19 #include "SkScaledBitmapSampler.h" | |
| 20 #include "SkStream.h" | |
| 21 #include "SkColorPriv.h" | |
| 22 #include "SkTDArray.h" | |
| 23 | |
| 24 class SkBMPImageDecoder : public SkImageDecoder { | |
| 25 public: | |
| 26 SkBMPImageDecoder() {} | |
| 27 | |
| 28 virtual Format getFormat() const { | |
| 29 return kBMP_Format; | |
| 30 } | |
| 31 | |
| 32 protected: | |
| 33 virtual bool onDecode(SkStream* stream, SkBitmap* bm, | |
| 34 SkBitmap::Config pref, Mode mode); | |
| 35 }; | |
| 36 | |
| 37 SkImageDecoder* SkImageDecoder_BMP_Factory(SkStream*); | |
| 38 SkImageDecoder* SkImageDecoder_BMP_Factory(SkStream* stream) { | |
| 39 static const char kBmpMagic[] = { 'B', 'M' }; | |
| 40 | |
| 41 size_t len = stream->getLength(); | |
| 42 char buffer[sizeof(kBmpMagic)]; | |
| 43 | |
| 44 if (len > sizeof(kBmpMagic) && | |
| 45 stream->read(buffer, sizeof(kBmpMagic)) == sizeof(kBmpMagic) && | |
| 46 !memcmp(buffer, kBmpMagic, sizeof(kBmpMagic))) { | |
| 47 return SkNEW(SkBMPImageDecoder); | |
| 48 } | |
| 49 return NULL; | |
| 50 } | |
| 51 | |
| 52 /////////////////////////////////////////////////////////////////////////////// | |
| 53 | |
| 54 class SkBmpDecoderCallback : public image_codec::BmpDecoderCallback { | |
| 55 public: | |
| 56 // we don't copy the bitmap, just remember the pointer | |
| 57 SkBmpDecoderCallback(bool justBounds) : fJustBounds(justBounds) {} | |
| 58 | |
| 59 // override from BmpDecoderCallback | |
| 60 virtual uint8* SetSize(int width, int height) { | |
| 61 fWidth = width; | |
| 62 fHeight = height; | |
| 63 if (fJustBounds) { | |
| 64 return NULL; | |
| 65 } | |
| 66 | |
| 67 fRGB.setCount(width * height * 3); // 3 == r, g, b | |
| 68 return fRGB.begin(); | |
| 69 } | |
| 70 | |
| 71 int width() const { return fWidth; } | |
| 72 int height() const { return fHeight; } | |
| 73 uint8_t* rgb() const { return fRGB.begin(); } | |
| 74 | |
| 75 private: | |
| 76 SkTDArray<uint8_t> fRGB; | |
| 77 int fWidth; | |
| 78 int fHeight; | |
| 79 bool fJustBounds; | |
| 80 }; | |
| 81 | |
| 82 bool SkBMPImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, | |
| 83 SkBitmap::Config prefConfig, Mode mode) { | |
| 84 | |
| 85 size_t length = stream->getLength(); | |
| 86 SkAutoMalloc storage(length); | |
| 87 | |
| 88 if (stream->read(storage.get(), length) != length) { | |
| 89 return false; | |
| 90 } | |
| 91 | |
| 92 const bool justBounds = SkImageDecoder::kDecodeBounds_Mode == mode; | |
| 93 SkBmpDecoderCallback callback(justBounds); | |
| 94 | |
| 95 // Now decode the BMP into callback's rgb() array [r,g,b, r,g,b, ...] | |
| 96 { | |
| 97 image_codec::BmpDecoderHelper helper; | |
| 98 const int max_pixels = 16383*16383; // max width*height | |
| 99 if (!helper.DecodeImage((const char*)storage.get(), length, | |
| 100 max_pixels, &callback)) { | |
| 101 return false; | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 // we don't need this anymore, so free it now (before we try to allocate | |
| 106 // the bitmap's pixels) rather than waiting for its destructor | |
| 107 storage.free(); | |
| 108 | |
| 109 int width = callback.width(); | |
| 110 int height = callback.height(); | |
| 111 SkBitmap::Config config = SkBitmap::kARGB_8888_Config; | |
| 112 | |
| 113 // only accept prefConfig if it makes sense for us | |
| 114 if (SkBitmap::kARGB_4444_Config == prefConfig || | |
| 115 SkBitmap::kRGB_565_Config == config) { | |
| 116 config = prefConfig; | |
| 117 } | |
| 118 | |
| 119 SkScaledBitmapSampler sampler(width, height, getSampleSize()); | |
| 120 | |
| 121 bm->setConfig(config, sampler.scaledWidth(), sampler.scaledHeight()); | |
| 122 bm->setIsOpaque(true); | |
| 123 if (justBounds) { | |
| 124 return true; | |
| 125 } | |
| 126 | |
| 127 if (!this->allocPixelRef(bm, NULL)) { | |
| 128 return false; | |
| 129 } | |
| 130 | |
| 131 SkAutoLockPixels alp(*bm); | |
| 132 | |
| 133 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, getDitherImage())) { | |
| 134 return false; | |
| 135 } | |
| 136 | |
| 137 const int srcRowBytes = width * 3; | |
| 138 const int dstHeight = sampler.scaledHeight(); | |
| 139 const uint8_t* srcRow = callback.rgb(); | |
| 140 | |
| 141 srcRow += sampler.srcY0() * srcRowBytes; | |
| 142 for (int y = 0; y < dstHeight; y++) { | |
| 143 sampler.next(srcRow); | |
| 144 srcRow += sampler.srcDY() * srcRowBytes; | |
| 145 } | |
| 146 return true; | |
| 147 } | |
| OLD | NEW |