blob: b9359bea7a1e169f8b770a0971c38337a0856bec [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"
scroggo@google.comdbf9f882013-08-21 15:01:48 +000011#include "SkColorPriv.h"
scroggoa9132752016-01-19 07:53:39 -080012#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkImageDecoder.h"
14#include "SkScaledBitmapSampler.h"
15#include "SkStream.h"
halcanary67ec1f82014-06-27 11:36:20 -070016#include "SkStreamPriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "SkTDArray.h"
18
19class SkBMPImageDecoder : public SkImageDecoder {
20public:
21 SkBMPImageDecoder() {}
rmistry@google.comd6176b02012-08-23 18:14:13 +000022
mtklein36352bf2015-03-25 18:17:31 -070023 Format getFormat() const override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000024 return kBMP_Format;
25 }
26
27protected:
mtklein36352bf2015-03-25 18:17:31 -070028 Result onDecode(SkStream* stream, SkBitmap* bm, Mode mode) override;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000029
30private:
31 typedef SkImageDecoder INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000032};
33
robertphillips@google.comec51cb82012-03-23 18:13:47 +000034///////////////////////////////////////////////////////////////////////////////
35DEFINE_DECODER_CREATOR(BMPImageDecoder);
36///////////////////////////////////////////////////////////////////////////////
37
scroggo@google.comb5571b32013-09-25 21:34:24 +000038static bool is_bmp(SkStreamRewindable* stream) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 static const char kBmpMagic[] = { 'B', 'M' };
rmistry@google.comd6176b02012-08-23 18:14:13 +000040
djsollen@google.com5dd45022013-03-21 13:30:54 +000041
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 char buffer[sizeof(kBmpMagic)];
rmistry@google.comd6176b02012-08-23 18:14:13 +000043
scroggo@google.com39edf4c2013-04-25 17:33:51 +000044 return stream->read(buffer, sizeof(kBmpMagic)) == sizeof(kBmpMagic) &&
45 !memcmp(buffer, kBmpMagic, sizeof(kBmpMagic));
46}
47
scroggo@google.comb5571b32013-09-25 21:34:24 +000048static SkImageDecoder* sk_libbmp_dfactory(SkStreamRewindable* stream) {
scroggo@google.com39edf4c2013-04-25 17:33:51 +000049 if (is_bmp(stream)) {
halcanary385fe4d2015-08-26 13:07:48 -070050 return new SkBMPImageDecoder;
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 }
halcanary96fcdcc2015-08-27 07:41:13 -070052 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +000053}
54
mtklein@google.combd6343b2013-09-04 17:20:18 +000055static SkImageDecoder_DecodeReg gReg(sk_libbmp_dfactory);
reed@android.com00bf85a2009-01-22 13:04:56 +000056
scroggo@google.comb5571b32013-09-25 21:34:24 +000057static SkImageDecoder::Format get_format_bmp(SkStreamRewindable* stream) {
scroggo@google.com39edf4c2013-04-25 17:33:51 +000058 if (is_bmp(stream)) {
59 return SkImageDecoder::kBMP_Format;
60 }
61 return SkImageDecoder::kUnknown_Format;
62}
63
mtklein@google.combd6343b2013-09-04 17:20:18 +000064static SkImageDecoder_FormatReg gFormatReg(get_format_bmp);
scroggo@google.com39edf4c2013-04-25 17:33:51 +000065
reed@android.com8a1c16f2008-12-17 15:59:43 +000066///////////////////////////////////////////////////////////////////////////////
67
68class SkBmpDecoderCallback : public image_codec::BmpDecoderCallback {
69public:
70 // we don't copy the bitmap, just remember the pointer
71 SkBmpDecoderCallback(bool justBounds) : fJustBounds(justBounds) {}
72
73 // override from BmpDecoderCallback
74 virtual uint8* SetSize(int width, int height) {
75 fWidth = width;
76 fHeight = height;
77 if (fJustBounds) {
halcanary96fcdcc2015-08-27 07:41:13 -070078 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000080
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 fRGB.setCount(width * height * 3); // 3 == r, g, b
82 return fRGB.begin();
83 }
84
85 int width() const { return fWidth; }
86 int height() const { return fHeight; }
commit-bot@chromium.orgaa537d42013-02-28 19:03:13 +000087 const uint8_t* rgb() const { return fRGB.begin(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +000088
89private:
90 SkTDArray<uint8_t> fRGB;
91 int fWidth;
92 int fHeight;
93 bool fJustBounds;
94};
95
scroggo2a120802014-10-22 12:07:00 -070096SkImageDecoder::Result SkBMPImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
scroggo@google.com2fd740f2013-08-21 14:59:03 +000097 // First read the entire stream, so that all of the data can be passed to
98 // the BmpDecoderHelper.
reed@android.com8a1c16f2008-12-17 15:59:43 +000099
reedfde05112016-03-11 13:02:28 -0800100 auto data = SkCopyStreamToData(stream);
scroggoa9132752016-01-19 07:53:39 -0800101 if (!data) {
102 return kFailure;
103 }
104
scroggo@google.comdbf9f882013-08-21 15:01:48 +0000105 // Byte length of all of the data.
scroggoa9132752016-01-19 07:53:39 -0800106 const size_t length = data->size();
scroggo@google.comdbf9f882013-08-21 15:01:48 +0000107 if (0 == length) {
scroggo2a120802014-10-22 12:07:00 -0700108 return kFailure;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000110
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 const bool justBounds = SkImageDecoder::kDecodeBounds_Mode == mode;
112 SkBmpDecoderCallback callback(justBounds);
113
114 // Now decode the BMP into callback's rgb() array [r,g,b, r,g,b, ...]
115 {
116 image_codec::BmpDecoderHelper helper;
117 const int max_pixels = 16383*16383; // max width*height
scroggoa9132752016-01-19 07:53:39 -0800118 if (!helper.DecodeImage((const char*) data->data(), length,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 max_pixels, &callback)) {
scroggo2a120802014-10-22 12:07:00 -0700120 return kFailure;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 }
122 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000123
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 // we don't need this anymore, so free it now (before we try to allocate
125 // the bitmap's pixels) rather than waiting for its destructor
scroggoa9132752016-01-19 07:53:39 -0800126 data.reset(nullptr);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000127
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 int width = callback.width();
129 int height = callback.height();
reed6c225732014-06-09 19:52:07 -0700130 SkColorType colorType = this->getPrefColorType(k32Bit_SrcDepth, false);
reed@android.com3f1f06a2010-03-03 21:04:12 +0000131
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 // only accept prefConfig if it makes sense for us
reed6c225732014-06-09 19:52:07 -0700133 if (kARGB_4444_SkColorType != colorType && kRGB_565_SkColorType != colorType) {
134 colorType = kN32_SkColorType;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 }
136
137 SkScaledBitmapSampler sampler(width, height, getSampleSize());
138
reed6c225732014-06-09 19:52:07 -0700139 bm->setInfo(SkImageInfo::Make(sampler.scaledWidth(), sampler.scaledHeight(),
140 colorType, kOpaque_SkAlphaType));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141
scroggo@google.combc69ce92013-07-09 15:45:14 +0000142 if (justBounds) {
scroggo2a120802014-10-22 12:07:00 -0700143 return kSuccess;
scroggo@google.combc69ce92013-07-09 15:45:14 +0000144 }
145
halcanary96fcdcc2015-08-27 07:41:13 -0700146 if (!this->allocPixelRef(bm, nullptr)) {
scroggo2a120802014-10-22 12:07:00 -0700147 return kFailure;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000149
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 SkAutoLockPixels alp(*bm);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000151
scroggo@google.com8d239242013-10-01 17:27:15 +0000152 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, *this)) {
scroggo2a120802014-10-22 12:07:00 -0700153 return kFailure;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 }
155
156 const int srcRowBytes = width * 3;
157 const int dstHeight = sampler.scaledHeight();
158 const uint8_t* srcRow = callback.rgb();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000159
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 srcRow += sampler.srcY0() * srcRowBytes;
161 for (int y = 0; y < dstHeight; y++) {
162 sampler.next(srcRow);
163 srcRow += sampler.srcDY() * srcRowBytes;
164 }
scroggo2a120802014-10-22 12:07:00 -0700165 return kSuccess;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166}