blob: 1dcea8ad64c38bc7b8cbc06ec9a3ad6e7b5fabc6 [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
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 size_t len = stream->getLength();
41 char buffer[sizeof(kBmpMagic)];
rmistry@google.comd6176b02012-08-23 18:14:13 +000042
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 if (len > sizeof(kBmpMagic) &&
44 stream->read(buffer, sizeof(kBmpMagic)) == sizeof(kBmpMagic) &&
45 !memcmp(buffer, kBmpMagic, sizeof(kBmpMagic))) {
46 return SkNEW(SkBMPImageDecoder);
47 }
48 return NULL;
49}
50
robertphillips@google.com8570b5c2012-03-20 17:40:58 +000051static SkTRegistry<SkImageDecoder*, SkStream*> gReg(sk_libbmp_dfactory);
reed@android.com00bf85a2009-01-22 13:04:56 +000052
reed@android.com8a1c16f2008-12-17 15:59:43 +000053///////////////////////////////////////////////////////////////////////////////
54
55class SkBmpDecoderCallback : public image_codec::BmpDecoderCallback {
56public:
57 // we don't copy the bitmap, just remember the pointer
58 SkBmpDecoderCallback(bool justBounds) : fJustBounds(justBounds) {}
59
60 // override from BmpDecoderCallback
61 virtual uint8* SetSize(int width, int height) {
62 fWidth = width;
63 fHeight = height;
64 if (fJustBounds) {
65 return NULL;
66 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000067
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 fRGB.setCount(width * height * 3); // 3 == r, g, b
69 return fRGB.begin();
70 }
71
72 int width() const { return fWidth; }
73 int height() const { return fHeight; }
commit-bot@chromium.orgaa537d42013-02-28 19:03:13 +000074 const uint8_t* rgb() const { return fRGB.begin(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +000075
76private:
77 SkTDArray<uint8_t> fRGB;
78 int fWidth;
79 int fHeight;
80 bool fJustBounds;
81};
82
reed@android.com3f1f06a2010-03-03 21:04:12 +000083bool SkBMPImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000084
85 size_t length = stream->getLength();
86 SkAutoMalloc storage(length);
rmistry@google.comd6176b02012-08-23 18:14:13 +000087
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 if (stream->read(storage.get(), length) != length) {
89 return false;
90 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000091
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 const bool justBounds = SkImageDecoder::kDecodeBounds_Mode == mode;
93 SkBmpDecoderCallback callback(justBounds);
94
95 // Now decode the BMP into callback's rgb() array [r,g,b, r,g,b, ...]
96 {
97 image_codec::BmpDecoderHelper helper;
98 const int max_pixels = 16383*16383; // max width*height
99 if (!helper.DecodeImage((const char*)storage.get(), length,
100 max_pixels, &callback)) {
101 return false;
102 }
103 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000104
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 // we don't need this anymore, so free it now (before we try to allocate
106 // the bitmap's pixels) rather than waiting for its destructor
107 storage.free();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000108
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 int width = callback.width();
110 int height = callback.height();
reed@android.com3f1f06a2010-03-03 21:04:12 +0000111 SkBitmap::Config config = this->getPrefConfig(k32Bit_SrcDepth, false);
112
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 // only accept prefConfig if it makes sense for us
reed@android.com3f1f06a2010-03-03 21:04:12 +0000114 if (SkBitmap::kARGB_4444_Config != config &&
115 SkBitmap::kRGB_565_Config != config) {
116 config = SkBitmap::kARGB_8888_Config;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 }
118
119 SkScaledBitmapSampler sampler(width, height, getSampleSize());
120
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 if (justBounds) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000122 bm->setConfig(config, sampler.scaledWidth(), sampler.scaledHeight());
123 bm->setIsOpaque(true);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 return true;
125 }
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000126 // No Bitmap reuse supported for this format
127 if (!bm->isNull()) {
128 return false;
129 }
130
131 bm->setConfig(config, sampler.scaledWidth(), sampler.scaledHeight());
132 bm->setIsOpaque(true);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133
134 if (!this->allocPixelRef(bm, NULL)) {
135 return false;
136 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000137
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 SkAutoLockPixels alp(*bm);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000139
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, getDitherImage())) {
141 return false;
142 }
143
144 const int srcRowBytes = width * 3;
145 const int dstHeight = sampler.scaledHeight();
146 const uint8_t* srcRow = callback.rgb();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000147
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 srcRow += sampler.srcY0() * srcRowBytes;
149 for (int y = 0; y < dstHeight; y++) {
150 sampler.next(srcRow);
151 srcRow += sampler.srcDY() * srcRowBytes;
152 }
153 return true;
154}