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