blob: 179162356aec980067ee6548c228d13d967b0cc0 [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"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkImageDecoder.h"
13#include "SkScaledBitmapSampler.h"
14#include "SkStream.h"
halcanary67ec1f82014-06-27 11:36:20 -070015#include "SkStreamPriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include "SkTDArray.h"
17
18class SkBMPImageDecoder : public SkImageDecoder {
19public:
20 SkBMPImageDecoder() {}
rmistry@google.comd6176b02012-08-23 18:14:13 +000021
mtklein36352bf2015-03-25 18:17:31 -070022 Format getFormat() const override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000023 return kBMP_Format;
24 }
25
26protected:
mtklein36352bf2015-03-25 18:17:31 -070027 Result onDecode(SkStream* stream, SkBitmap* bm, Mode mode) override;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000028
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.comb5571b32013-09-25 21:34:24 +000037static bool is_bmp(SkStreamRewindable* 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
scroggo@google.comb5571b32013-09-25 21:34:24 +000047static SkImageDecoder* sk_libbmp_dfactory(SkStreamRewindable* stream) {
scroggo@google.com39edf4c2013-04-25 17:33:51 +000048 if (is_bmp(stream)) {
halcanary385fe4d2015-08-26 13:07:48 -070049 return new SkBMPImageDecoder;
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 }
halcanary96fcdcc2015-08-27 07:41:13 -070051 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +000052}
53
mtklein@google.combd6343b2013-09-04 17:20:18 +000054static SkImageDecoder_DecodeReg gReg(sk_libbmp_dfactory);
reed@android.com00bf85a2009-01-22 13:04:56 +000055
scroggo@google.comb5571b32013-09-25 21:34:24 +000056static SkImageDecoder::Format get_format_bmp(SkStreamRewindable* stream) {
scroggo@google.com39edf4c2013-04-25 17:33:51 +000057 if (is_bmp(stream)) {
58 return SkImageDecoder::kBMP_Format;
59 }
60 return SkImageDecoder::kUnknown_Format;
61}
62
mtklein@google.combd6343b2013-09-04 17:20:18 +000063static SkImageDecoder_FormatReg gFormatReg(get_format_bmp);
scroggo@google.com39edf4c2013-04-25 17:33:51 +000064
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) {
halcanary96fcdcc2015-08-27 07:41:13 -070077 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 }
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
scroggo2a120802014-10-22 12:07:00 -070095SkImageDecoder::Result 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 // Allocated space used to hold the data.
100 SkAutoMalloc storage;
scroggo@google.comdbf9f882013-08-21 15:01:48 +0000101 // Byte length of all of the data.
halcanary67ec1f82014-06-27 11:36:20 -0700102 const size_t length = SkCopyStreamToStorage(&storage, stream);
scroggo@google.comdbf9f882013-08-21 15:01:48 +0000103 if (0 == length) {
scroggo2a120802014-10-22 12:07:00 -0700104 return kFailure;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000106
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 const bool justBounds = SkImageDecoder::kDecodeBounds_Mode == mode;
108 SkBmpDecoderCallback callback(justBounds);
109
110 // Now decode the BMP into callback's rgb() array [r,g,b, r,g,b, ...]
111 {
112 image_codec::BmpDecoderHelper helper;
113 const int max_pixels = 16383*16383; // max width*height
114 if (!helper.DecodeImage((const char*)storage.get(), length,
115 max_pixels, &callback)) {
scroggo2a120802014-10-22 12:07:00 -0700116 return kFailure;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 }
118 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000119
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 // we don't need this anymore, so free it now (before we try to allocate
121 // the bitmap's pixels) rather than waiting for its destructor
122 storage.free();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000123
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 int width = callback.width();
125 int height = callback.height();
reed6c225732014-06-09 19:52:07 -0700126 SkColorType colorType = this->getPrefColorType(k32Bit_SrcDepth, false);
reed@android.com3f1f06a2010-03-03 21:04:12 +0000127
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 // only accept prefConfig if it makes sense for us
reed6c225732014-06-09 19:52:07 -0700129 if (kARGB_4444_SkColorType != colorType && kRGB_565_SkColorType != colorType) {
130 colorType = kN32_SkColorType;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 }
132
133 SkScaledBitmapSampler sampler(width, height, getSampleSize());
134
reed6c225732014-06-09 19:52:07 -0700135 bm->setInfo(SkImageInfo::Make(sampler.scaledWidth(), sampler.scaledHeight(),
136 colorType, kOpaque_SkAlphaType));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137
scroggo@google.combc69ce92013-07-09 15:45:14 +0000138 if (justBounds) {
scroggo2a120802014-10-22 12:07:00 -0700139 return kSuccess;
scroggo@google.combc69ce92013-07-09 15:45:14 +0000140 }
141
halcanary96fcdcc2015-08-27 07:41:13 -0700142 if (!this->allocPixelRef(bm, nullptr)) {
scroggo2a120802014-10-22 12:07:00 -0700143 return kFailure;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000145
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 SkAutoLockPixels alp(*bm);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000147
scroggo@google.com8d239242013-10-01 17:27:15 +0000148 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, *this)) {
scroggo2a120802014-10-22 12:07:00 -0700149 return kFailure;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 }
151
152 const int srcRowBytes = width * 3;
153 const int dstHeight = sampler.scaledHeight();
154 const uint8_t* srcRow = callback.rgb();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000155
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 srcRow += sampler.srcY0() * srcRowBytes;
157 for (int y = 0; y < dstHeight; y++) {
158 sampler.next(srcRow);
159 srcRow += sampler.srcDY() * srcRowBytes;
160 }
scroggo2a120802014-10-22 12:07:00 -0700161 return kSuccess;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162}