blob: 488eddce1edb203065ce063db82e449c169db99f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2007 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
rmistry@google.comd6176b02012-08-23 18:14:13 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "bmpdecoderhelper.h"
11#include "SkImageDecoder.h"
12#include "SkScaledBitmapSampler.h"
13#include "SkStream.h"
14#include "SkColorPriv.h"
15#include "SkTDArray.h"
reed@android.com00bf85a2009-01-22 13:04:56 +000016#include "SkTRegistry.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017
18class SkBMPImageDecoder : public SkImageDecoder {
19public:
20 SkBMPImageDecoder() {}
rmistry@google.comd6176b02012-08-23 18:14:13 +000021
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000022 virtual Format getFormat() const SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +000023 return kBMP_Format;
24 }
25
26protected:
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000027 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode mode) SK_OVERRIDE;
28
29private:
30 typedef SkImageDecoder INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000031};
32
robertphillips@google.comec51cb82012-03-23 18:13:47 +000033///////////////////////////////////////////////////////////////////////////////
34DEFINE_DECODER_CREATOR(BMPImageDecoder);
35///////////////////////////////////////////////////////////////////////////////
36
caryclark@google.com2a2cc202012-06-06 12:04:36 +000037static SkImageDecoder* sk_libbmp_dfactory(SkStream* stream) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 static const char kBmpMagic[] = { 'B', 'M' };
rmistry@google.comd6176b02012-08-23 18:14:13 +000039
djsollen@google.com5dd45022013-03-21 13:30:54 +000040
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 char buffer[sizeof(kBmpMagic)];
rmistry@google.comd6176b02012-08-23 18:14:13 +000042
djsollen@google.com5dd45022013-03-21 13:30:54 +000043 if (stream->read(buffer, sizeof(kBmpMagic)) == sizeof(kBmpMagic) &&
44 !memcmp(buffer, kBmpMagic, sizeof(kBmpMagic))) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 return SkNEW(SkBMPImageDecoder);
46 }
47 return NULL;
48}
49
robertphillips@google.com8570b5c2012-03-20 17:40:58 +000050static SkTRegistry<SkImageDecoder*, SkStream*> gReg(sk_libbmp_dfactory);
reed@android.com00bf85a2009-01-22 13:04:56 +000051
reed@android.com8a1c16f2008-12-17 15:59:43 +000052///////////////////////////////////////////////////////////////////////////////
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 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000066
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 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; }
commit-bot@chromium.orgaa537d42013-02-28 19:03:13 +000073 const uint8_t* rgb() const { return fRGB.begin(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +000074
75private:
76 SkTDArray<uint8_t> fRGB;
77 int fWidth;
78 int fHeight;
79 bool fJustBounds;
80};
81
reed@android.com3f1f06a2010-03-03 21:04:12 +000082bool SkBMPImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000083
84 size_t length = stream->getLength();
85 SkAutoMalloc storage(length);
rmistry@google.comd6176b02012-08-23 18:14:13 +000086
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 if (stream->read(storage.get(), length) != length) {
88 return false;
89 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000090
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 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 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000103
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 // 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();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000107
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 int width = callback.width();
109 int height = callback.height();
reed@android.com3f1f06a2010-03-03 21:04:12 +0000110 SkBitmap::Config config = this->getPrefConfig(k32Bit_SrcDepth, false);
111
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112 // only accept prefConfig if it makes sense for us
reed@android.com3f1f06a2010-03-03 21:04:12 +0000113 if (SkBitmap::kARGB_4444_Config != config &&
114 SkBitmap::kRGB_565_Config != config) {
115 config = SkBitmap::kARGB_8888_Config;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 }
117
118 SkScaledBitmapSampler sampler(width, height, getSampleSize());
119
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 if (justBounds) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000121 bm->setConfig(config, sampler.scaledWidth(), sampler.scaledHeight());
122 bm->setIsOpaque(true);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 return true;
124 }
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000125 // 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);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132
133 if (!this->allocPixelRef(bm, NULL)) {
134 return false;
135 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000136
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 SkAutoLockPixels alp(*bm);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000138
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 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();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000146
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 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}