SkImageDecoder_wbmp.cpp revision 00bf85a98675c9d0c3150bbeb0a3d7198ad8f21f
1/**
2** Copyright 2006, The Android Open Source Project
3**
4** Licensed under the Apache License, Version 2.0 (the "License");
5** you may not use this file except in compliance with the License.
6** You may obtain a copy of the License at
7**
8**     http://www.apache.org/licenses/LICENSE-2.0
9**
10** Unless required by applicable law or agreed to in writing, software
11** distributed under the License is distributed on an "AS IS" BASIS,
12** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13** See the License for the specific language governing permissions and
14** limitations under the License.
15*/
16
17#include "SkImageDecoder.h"
18#include "SkColor.h"
19#include "SkColorPriv.h"
20#include "SkMath.h"
21#include "SkStream.h"
22#include "SkTemplates.h"
23#include "SkUtils.h"
24
25class SkWBMPImageDecoder : public SkImageDecoder {
26public:
27    virtual Format getFormat() const {
28        return kWBMP_Format;
29    }
30
31protected:
32    virtual bool onDecode(SkStream* stream, SkBitmap* bm,
33                          SkBitmap::Config pref, Mode);
34};
35
36static bool read_byte(SkStream* stream, uint8_t* data)
37{
38    return stream->read(data, 1) == 1;
39}
40
41static bool read_mbf(SkStream* stream, int* value)
42{
43    int n = 0;
44    uint8_t data;
45    do {
46        if (!read_byte(stream, &data)) {
47            return false;
48        }
49        n = (n << 7) | (data & 0x7F);
50    } while (data & 0x80);
51
52    *value = n;
53    return true;
54}
55
56struct wbmp_head {
57    int fWidth;
58    int fHeight;
59
60    bool init(SkStream* stream)
61    {
62        uint8_t data;
63
64        if (!read_byte(stream, &data) || data != 0) { // unknown type
65            return false;
66        }
67        if (!read_byte(stream, &data) || (data & 0x9F)) { // skip fixed header
68            return false;
69        }
70        if (!read_mbf(stream, &fWidth) || (unsigned)fWidth > 0xFFFF) {
71            return false;
72        }
73        if (!read_mbf(stream, &fHeight) || (unsigned)fHeight > 0xFFFF) {
74            return false;
75        }
76        return fWidth != 0 && fHeight != 0;
77    }
78};
79
80static void expand_bits_to_bytes(uint8_t dst[], const uint8_t src[], int bits)
81{
82    int bytes = bits >> 3;
83
84    for (int i = 0; i < bytes; i++) {
85        unsigned mask = *src++;
86        dst[0] = (mask >> 7) & 1;
87        dst[1] = (mask >> 6) & 1;
88        dst[2] = (mask >> 5) & 1;
89        dst[3] = (mask >> 4) & 1;
90        dst[4] = (mask >> 3) & 1;
91        dst[5] = (mask >> 2) & 1;
92        dst[6] = (mask >> 1) & 1;
93        dst[7] = (mask >> 0) & 1;
94        dst += 8;
95    }
96
97    bits &= 7;
98    if (bits > 0) {
99        unsigned mask = *src;
100        do {
101            *dst++ = (mask >> 7) & 1;;
102            mask <<= 1;
103        } while (--bits != 0);
104    }
105}
106
107#define SkAlign8(x)     (((x) + 7) & ~7)
108
109bool SkWBMPImageDecoder::onDecode(SkStream* stream, SkBitmap* decodedBitmap,
110                                  SkBitmap::Config prefConfig, Mode mode)
111{
112    wbmp_head   head;
113
114    if (!head.init(stream)) {
115        return false;
116    }
117
118    int width = head.fWidth;
119    int height = head.fHeight;
120
121    // assign these directly, in case we return kDimensions_Result
122    decodedBitmap->setConfig(SkBitmap::kIndex8_Config, width, height);
123    decodedBitmap->setIsOpaque(true);
124
125    if (SkImageDecoder::kDecodeBounds_Mode == mode)
126        return true;
127
128    const SkPMColor colors[] = { SK_ColorBLACK, SK_ColorWHITE };
129    SkColorTable* ct = SkNEW_ARGS(SkColorTable, (colors, 2));
130    SkAutoUnref   aur(ct);
131
132    if (!this->allocPixelRef(decodedBitmap, ct)) {
133        return false;
134    }
135
136    SkAutoLockPixels alp(*decodedBitmap);
137
138    uint8_t* dst = decodedBitmap->getAddr8(0, 0);
139    // store the 1-bit valuess at the end of our pixels, so we won't stomp
140    // on them before we're read them. Just trying to avoid a temp allocation
141    size_t srcRB = SkAlign8(width) >> 3;
142    size_t srcSize = height * srcRB;
143    uint8_t* src = dst + decodedBitmap->getSize() - srcSize;
144    if (stream->read(src, srcSize) != srcSize) {
145        return false;
146    }
147
148    for (int y = 0; y < height; y++)
149    {
150        expand_bits_to_bytes(dst, src, width);
151        dst += decodedBitmap->rowBytes();
152        src += srcRB;
153    }
154
155    return true;
156}
157
158///////////////////////////////////////////////////////////////////////////////
159
160#include "SkTRegistry.h"
161
162static SkImageDecoder* Factory(SkStream* stream) {
163    wbmp_head   head;
164
165    if (head.init(stream)) {
166        return SkNEW(SkWBMPImageDecoder);
167    }
168    return NULL;
169}
170
171static SkTRegistry<SkImageDecoder*, SkStream*> gReg(Factory);
172
173