SkImageDecoder_libbmp.cpp revision 5dd45021c37e24cd2c8e91a0f1a1d28a77ad613c
1 2/* 3 * Copyright 2007 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10#include "bmpdecoderhelper.h" 11#include "SkImageDecoder.h" 12#include "SkScaledBitmapSampler.h" 13#include "SkStream.h" 14#include "SkColorPriv.h" 15#include "SkTDArray.h" 16#include "SkTRegistry.h" 17 18class SkBMPImageDecoder : public SkImageDecoder { 19public: 20 SkBMPImageDecoder() {} 21 22 virtual Format getFormat() const SK_OVERRIDE { 23 return kBMP_Format; 24 } 25 26protected: 27 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode mode) SK_OVERRIDE; 28 29private: 30 typedef SkImageDecoder INHERITED; 31}; 32 33/////////////////////////////////////////////////////////////////////////////// 34DEFINE_DECODER_CREATOR(BMPImageDecoder); 35/////////////////////////////////////////////////////////////////////////////// 36 37static SkImageDecoder* sk_libbmp_dfactory(SkStream* stream) { 38 static const char kBmpMagic[] = { 'B', 'M' }; 39 40 41 char buffer[sizeof(kBmpMagic)]; 42 43 if (stream->read(buffer, sizeof(kBmpMagic)) == sizeof(kBmpMagic) && 44 !memcmp(buffer, kBmpMagic, sizeof(kBmpMagic))) { 45 return SkNEW(SkBMPImageDecoder); 46 } 47 return NULL; 48} 49 50static SkTRegistry<SkImageDecoder*, SkStream*> gReg(sk_libbmp_dfactory); 51 52/////////////////////////////////////////////////////////////////////////////// 53 54class SkBmpDecoderCallback : public image_codec::BmpDecoderCallback { 55public: 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 const uint8_t* rgb() const { return fRGB.begin(); } 74 75private: 76 SkTDArray<uint8_t> fRGB; 77 int fWidth; 78 int fHeight; 79 bool fJustBounds; 80}; 81 82bool SkBMPImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { 83 84 size_t length = stream->getLength(); 85 SkAutoMalloc storage(length); 86 87 if (stream->read(storage.get(), length) != length) { 88 return false; 89 } 90 91 const bool justBounds = SkImageDecoder::kDecodeBounds_Mode == mode; 92 SkBmpDecoderCallback callback(justBounds); 93 94 // Now decode the BMP into callback's rgb() array [r,g,b, r,g,b, ...] 95 { 96 image_codec::BmpDecoderHelper helper; 97 const int max_pixels = 16383*16383; // max width*height 98 if (!helper.DecodeImage((const char*)storage.get(), length, 99 max_pixels, &callback)) { 100 return false; 101 } 102 } 103 104 // we don't need this anymore, so free it now (before we try to allocate 105 // the bitmap's pixels) rather than waiting for its destructor 106 storage.free(); 107 108 int width = callback.width(); 109 int height = callback.height(); 110 SkBitmap::Config config = this->getPrefConfig(k32Bit_SrcDepth, false); 111 112 // only accept prefConfig if it makes sense for us 113 if (SkBitmap::kARGB_4444_Config != config && 114 SkBitmap::kRGB_565_Config != config) { 115 config = SkBitmap::kARGB_8888_Config; 116 } 117 118 SkScaledBitmapSampler sampler(width, height, getSampleSize()); 119 120 if (justBounds) { 121 bm->setConfig(config, sampler.scaledWidth(), sampler.scaledHeight()); 122 bm->setIsOpaque(true); 123 return true; 124 } 125 // No Bitmap reuse supported for this format 126 if (!bm->isNull()) { 127 return false; 128 } 129 130 bm->setConfig(config, sampler.scaledWidth(), sampler.scaledHeight()); 131 bm->setIsOpaque(true); 132 133 if (!this->allocPixelRef(bm, NULL)) { 134 return false; 135 } 136 137 SkAutoLockPixels alp(*bm); 138 139 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, getDitherImage())) { 140 return false; 141 } 142 143 const int srcRowBytes = width * 3; 144 const int dstHeight = sampler.scaledHeight(); 145 const uint8_t* srcRow = callback.rgb(); 146 147 srcRow += sampler.srcY0() * srcRowBytes; 148 for (int y = 0; y < dstHeight; y++) { 149 sampler.next(srcRow); 150 srcRow += sampler.srcDY() * srcRowBytes; 151 } 152 return true; 153} 154