blob: 3aa834a6b567182e30646d89519b12f14133c21e [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
reed@android.com8a1c16f2008-12-17 15:59:43 +000022 virtual Format getFormat() const {
23 return kBMP_Format;
24 }
25
26protected:
reed@android.com3f1f06a2010-03-03 21:04:12 +000027 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000028};
29
robertphillips@google.comec51cb82012-03-23 18:13:47 +000030///////////////////////////////////////////////////////////////////////////////
31DEFINE_DECODER_CREATOR(BMPImageDecoder);
32///////////////////////////////////////////////////////////////////////////////
33
caryclark@google.com2a2cc202012-06-06 12:04:36 +000034static SkImageDecoder* sk_libbmp_dfactory(SkStream* stream) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 static const char kBmpMagic[] = { 'B', 'M' };
rmistry@google.comd6176b02012-08-23 18:14:13 +000036
reed@android.com8a1c16f2008-12-17 15:59:43 +000037 size_t len = stream->getLength();
38 char buffer[sizeof(kBmpMagic)];
rmistry@google.comd6176b02012-08-23 18:14:13 +000039
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 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
robertphillips@google.com8570b5c2012-03-20 17:40:58 +000048static SkTRegistry<SkImageDecoder*, SkStream*> gReg(sk_libbmp_dfactory);
reed@android.com00bf85a2009-01-22 13:04:56 +000049
reed@android.com8a1c16f2008-12-17 15:59:43 +000050///////////////////////////////////////////////////////////////////////////////
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 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000064
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 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; }
commit-bot@chromium.orgaa537d42013-02-28 19:03:13 +000071 const uint8_t* rgb() const { return fRGB.begin(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +000072
73private:
74 SkTDArray<uint8_t> fRGB;
75 int fWidth;
76 int fHeight;
77 bool fJustBounds;
78};
79
reed@android.com3f1f06a2010-03-03 21:04:12 +000080bool SkBMPImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000081
82 size_t length = stream->getLength();
83 SkAutoMalloc storage(length);
rmistry@google.comd6176b02012-08-23 18:14:13 +000084
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 if (stream->read(storage.get(), length) != length) {
86 return false;
87 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000088
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 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 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000101
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 // 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();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000105
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 int width = callback.width();
107 int height = callback.height();
reed@android.com3f1f06a2010-03-03 21:04:12 +0000108 SkBitmap::Config config = this->getPrefConfig(k32Bit_SrcDepth, false);
109
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 // only accept prefConfig if it makes sense for us
reed@android.com3f1f06a2010-03-03 21:04:12 +0000111 if (SkBitmap::kARGB_4444_Config != config &&
112 SkBitmap::kRGB_565_Config != config) {
113 config = SkBitmap::kARGB_8888_Config;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 }
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 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000127
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 SkAutoLockPixels alp(*bm);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000129
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 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();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000137
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 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}