SkImageDecoder_libbmp.cpp revision 2a2cc2057347553f07d933b021876ba5502e55cd
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 { 23 return kBMP_Format; 24 } 25 26protected: 27 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode mode); 28}; 29 30/////////////////////////////////////////////////////////////////////////////// 31DEFINE_DECODER_CREATOR(BMPImageDecoder); 32/////////////////////////////////////////////////////////////////////////////// 33 34static SkImageDecoder* sk_libbmp_dfactory(SkStream* stream) { 35 static const char kBmpMagic[] = { 'B', 'M' }; 36 37 size_t len = stream->getLength(); 38 char buffer[sizeof(kBmpMagic)]; 39 40 if (len > sizeof(kBmpMagic) && 41 stream->read(buffer, sizeof(kBmpMagic)) == sizeof(kBmpMagic) && 42 !memcmp(buffer, kBmpMagic, sizeof(kBmpMagic))) { 43 return SkNEW(SkBMPImageDecoder); 44 } 45 return NULL; 46} 47 48static SkTRegistry<SkImageDecoder*, SkStream*> gReg(sk_libbmp_dfactory); 49 50/////////////////////////////////////////////////////////////////////////////// 51 52class SkBmpDecoderCallback : public image_codec::BmpDecoderCallback { 53public: 54 // we don't copy the bitmap, just remember the pointer 55 SkBmpDecoderCallback(bool justBounds) : fJustBounds(justBounds) {} 56 57 // override from BmpDecoderCallback 58 virtual uint8* SetSize(int width, int height) { 59 fWidth = width; 60 fHeight = height; 61 if (fJustBounds) { 62 return NULL; 63 } 64 65 fRGB.setCount(width * height * 3); // 3 == r, g, b 66 return fRGB.begin(); 67 } 68 69 int width() const { return fWidth; } 70 int height() const { return fHeight; } 71 uint8_t* rgb() const { return fRGB.begin(); } 72 73private: 74 SkTDArray<uint8_t> fRGB; 75 int fWidth; 76 int fHeight; 77 bool fJustBounds; 78}; 79 80bool SkBMPImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { 81 82 size_t length = stream->getLength(); 83 SkAutoMalloc storage(length); 84 85 if (stream->read(storage.get(), length) != length) { 86 return false; 87 } 88 89 const bool justBounds = SkImageDecoder::kDecodeBounds_Mode == mode; 90 SkBmpDecoderCallback callback(justBounds); 91 92 // Now decode the BMP into callback's rgb() array [r,g,b, r,g,b, ...] 93 { 94 image_codec::BmpDecoderHelper helper; 95 const int max_pixels = 16383*16383; // max width*height 96 if (!helper.DecodeImage((const char*)storage.get(), length, 97 max_pixels, &callback)) { 98 return false; 99 } 100 } 101 102 // we don't need this anymore, so free it now (before we try to allocate 103 // the bitmap's pixels) rather than waiting for its destructor 104 storage.free(); 105 106 int width = callback.width(); 107 int height = callback.height(); 108 SkBitmap::Config config = this->getPrefConfig(k32Bit_SrcDepth, false); 109 110 // only accept prefConfig if it makes sense for us 111 if (SkBitmap::kARGB_4444_Config != config && 112 SkBitmap::kRGB_565_Config != config) { 113 config = SkBitmap::kARGB_8888_Config; 114 } 115 116 SkScaledBitmapSampler sampler(width, height, getSampleSize()); 117 118 bm->setConfig(config, sampler.scaledWidth(), sampler.scaledHeight()); 119 bm->setIsOpaque(true); 120 if (justBounds) { 121 return true; 122 } 123 124 if (!this->allocPixelRef(bm, NULL)) { 125 return false; 126 } 127 128 SkAutoLockPixels alp(*bm); 129 130 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, getDitherImage())) { 131 return false; 132 } 133 134 const int srcRowBytes = width * 3; 135 const int dstHeight = sampler.scaledHeight(); 136 const uint8_t* srcRow = callback.rgb(); 137 138 srcRow += sampler.srcY0() * srcRowBytes; 139 for (int y = 0; y < dstHeight; y++) { 140 sampler.next(srcRow); 141 srcRow += sampler.srcDY() * srcRowBytes; 142 } 143 return true; 144} 145