3 * Copyright 2007 The Android Open Source Project
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #include "bmpdecoderhelper.h"
11 #include "SkColorPriv.h"
12 #include "SkImageDecoder.h"
13 #include "SkScaledBitmapSampler.h"
15 #include "SkStreamPriv.h"
16 #include "SkTDArray.h"
18 class SkBMPImageDecoder : public SkImageDecoder {
20 SkBMPImageDecoder() {}
22 Format getFormat() const SK_OVERRIDE {
27 Result onDecode(SkStream* stream, SkBitmap* bm, Mode mode) SK_OVERRIDE;
30 typedef SkImageDecoder INHERITED;
33 ///////////////////////////////////////////////////////////////////////////////
34 DEFINE_DECODER_CREATOR(BMPImageDecoder);
35 ///////////////////////////////////////////////////////////////////////////////
37 static bool is_bmp(SkStreamRewindable* stream) {
38 static const char kBmpMagic[] = { 'B', 'M' };
41 char buffer[sizeof(kBmpMagic)];
43 return stream->read(buffer, sizeof(kBmpMagic)) == sizeof(kBmpMagic) &&
44 !memcmp(buffer, kBmpMagic, sizeof(kBmpMagic));
47 static SkImageDecoder* sk_libbmp_dfactory(SkStreamRewindable* stream) {
49 return SkNEW(SkBMPImageDecoder);
54 static SkImageDecoder_DecodeReg gReg(sk_libbmp_dfactory);
56 static SkImageDecoder::Format get_format_bmp(SkStreamRewindable* stream) {
58 return SkImageDecoder::kBMP_Format;
60 return SkImageDecoder::kUnknown_Format;
63 static SkImageDecoder_FormatReg gFormatReg(get_format_bmp);
65 ///////////////////////////////////////////////////////////////////////////////
67 class SkBmpDecoderCallback : public image_codec::BmpDecoderCallback {
69 // we don't copy the bitmap, just remember the pointer
70 SkBmpDecoderCallback(bool justBounds) : fJustBounds(justBounds) {}
72 // override from BmpDecoderCallback
73 virtual uint8* SetSize(int width, int height) {
80 fRGB.setCount(width * height * 3); // 3 == r, g, b
84 int width() const { return fWidth; }
85 int height() const { return fHeight; }
86 const uint8_t* rgb() const { return fRGB.begin(); }
89 SkTDArray<uint8_t> fRGB;
95 SkImageDecoder::Result SkBMPImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
96 // First read the entire stream, so that all of the data can be passed to
97 // the BmpDecoderHelper.
99 // Allocated space used to hold the data.
100 SkAutoMalloc storage;
101 // Byte length of all of the data.
102 const size_t length = SkCopyStreamToStorage(&storage, stream);
107 const bool justBounds = SkImageDecoder::kDecodeBounds_Mode == mode;
108 SkBmpDecoderCallback callback(justBounds);
110 // Now decode the BMP into callback's rgb() array [r,g,b, r,g,b, ...]
112 image_codec::BmpDecoderHelper helper;
113 const int max_pixels = 16383*16383; // max width*height
114 if (!helper.DecodeImage((const char*)storage.get(), length,
115 max_pixels, &callback)) {
120 // we don't need this anymore, so free it now (before we try to allocate
121 // the bitmap's pixels) rather than waiting for its destructor
124 int width = callback.width();
125 int height = callback.height();
126 SkColorType colorType = this->getPrefColorType(k32Bit_SrcDepth, false);
128 // only accept prefConfig if it makes sense for us
129 if (kARGB_4444_SkColorType != colorType && kRGB_565_SkColorType != colorType) {
130 colorType = kN32_SkColorType;
133 SkScaledBitmapSampler sampler(width, height, getSampleSize());
135 bm->setInfo(SkImageInfo::Make(sampler.scaledWidth(), sampler.scaledHeight(),
136 colorType, kOpaque_SkAlphaType));
142 if (!this->allocPixelRef(bm, NULL)) {
146 SkAutoLockPixels alp(*bm);
148 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, *this)) {
152 const int srcRowBytes = width * 3;
153 const int dstHeight = sampler.scaledHeight();
154 const uint8_t* srcRow = callback.rgb();
156 srcRow += sampler.srcY0() * srcRowBytes;
157 for (int y = 0; y < dstHeight; y++) {
158 sampler.next(srcRow);
159 srcRow += sampler.srcDY() * srcRowBytes;