blob: 6c5ae27c478a22e13b3b62d77e59f0776ad45b23 [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
scroggo@google.com39edf4c2013-04-25 17:33:51 +000037static bool is_bmp(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
scroggo@google.com39edf4c2013-04-25 17:33:51 +000043 return stream->read(buffer, sizeof(kBmpMagic)) == sizeof(kBmpMagic) &&
44 !memcmp(buffer, kBmpMagic, sizeof(kBmpMagic));
45}
46
47static SkImageDecoder* sk_libbmp_dfactory(SkStream* stream) {
48 if (is_bmp(stream)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 return SkNEW(SkBMPImageDecoder);
50 }
51 return NULL;
52}
53
robertphillips@google.com8570b5c2012-03-20 17:40:58 +000054static SkTRegistry<SkImageDecoder*, SkStream*> gReg(sk_libbmp_dfactory);
reed@android.com00bf85a2009-01-22 13:04:56 +000055
scroggo@google.com39edf4c2013-04-25 17:33:51 +000056static 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
63static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_bmp);
64
reed@android.com8a1c16f2008-12-17 15:59:43 +000065///////////////////////////////////////////////////////////////////////////////
66
67class SkBmpDecoderCallback : public image_codec::BmpDecoderCallback {
68public:
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 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000079
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 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; }
commit-bot@chromium.orgaa537d42013-02-28 19:03:13 +000086 const uint8_t* rgb() const { return fRGB.begin(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +000087
88private:
89 SkTDArray<uint8_t> fRGB;
90 int fWidth;
91 int fHeight;
92 bool fJustBounds;
93};
94
reed@android.com3f1f06a2010-03-03 21:04:12 +000095bool SkBMPImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
scroggo@google.com2fd740f2013-08-21 14:59:03 +000096 // First read the entire stream, so that all of the data can be passed to
97 // the BmpDecoderHelper.
reed@android.com8a1c16f2008-12-17 15:59:43 +000098
scroggo@google.com2fd740f2013-08-21 14:59:03 +000099 // Byte length of all of the data.
100 size_t length;
101 // Allocated space used to hold the data.
102 SkAutoMalloc storage;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000103
scroggo@google.com2fd740f2013-08-21 14:59:03 +0000104 if (stream->hasLength()) {
105 length = stream->getLength();
106 void* dst = storage.reset(length);
107 if (stream->read(dst, length) != length) {
108 return false;
109 }
110 } else {
111 SkDynamicMemoryWStream tempStream;
112 // Arbitrary buffer size.
113 const size_t bufferSize = 256 * 1024; // 256 KB
114 char buffer[bufferSize];
115 length = 0;
116 do {
117 size_t bytesRead = stream->read(buffer, bufferSize);
118 tempStream.write(buffer, bytesRead);
119 length += bytesRead;
120 SkASSERT(tempStream.bytesWritten() == length);
121 } while (!stream->isAtEnd());
122 void* dst = storage.reset(length);
123 tempStream.copyTo(dst);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000125
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 const bool justBounds = SkImageDecoder::kDecodeBounds_Mode == mode;
127 SkBmpDecoderCallback callback(justBounds);
128
129 // Now decode the BMP into callback's rgb() array [r,g,b, r,g,b, ...]
130 {
131 image_codec::BmpDecoderHelper helper;
132 const int max_pixels = 16383*16383; // max width*height
133 if (!helper.DecodeImage((const char*)storage.get(), length,
134 max_pixels, &callback)) {
135 return false;
136 }
137 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000138
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 // we don't need this anymore, so free it now (before we try to allocate
140 // the bitmap's pixels) rather than waiting for its destructor
141 storage.free();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000142
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 int width = callback.width();
144 int height = callback.height();
reed@android.com3f1f06a2010-03-03 21:04:12 +0000145 SkBitmap::Config config = this->getPrefConfig(k32Bit_SrcDepth, false);
146
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 // only accept prefConfig if it makes sense for us
reed@android.com3f1f06a2010-03-03 21:04:12 +0000148 if (SkBitmap::kARGB_4444_Config != config &&
149 SkBitmap::kRGB_565_Config != config) {
150 config = SkBitmap::kARGB_8888_Config;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 }
152
153 SkScaledBitmapSampler sampler(width, height, getSampleSize());
154
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000155 bm->setConfig(config, sampler.scaledWidth(), sampler.scaledHeight());
156 bm->setIsOpaque(true);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157
scroggo@google.combc69ce92013-07-09 15:45:14 +0000158 if (justBounds) {
159 return true;
160 }
161
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 if (!this->allocPixelRef(bm, NULL)) {
163 return false;
164 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000165
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 SkAutoLockPixels alp(*bm);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000167
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, getDitherImage())) {
169 return false;
170 }
171
172 const int srcRowBytes = width * 3;
173 const int dstHeight = sampler.scaledHeight();
174 const uint8_t* srcRow = callback.rgb();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000175
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 srcRow += sampler.srcY0() * srcRowBytes;
177 for (int y = 0; y < dstHeight; y++) {
178 sampler.next(srcRow);
179 srcRow += sampler.srcDY() * srcRowBytes;
180 }
181 return true;
182}