SkImageDecoder_wbmp.cpp revision 8a1c16ff38322f0210116fa7293eb8817c7e477e
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
80SkImageDecoder* SkImageDecoder_WBMP_Factory(SkStream* stream)
81{
82    wbmp_head   head;
83
84    if (head.init(stream)) {
85        return SkNEW(SkWBMPImageDecoder);
86    }
87    return NULL;
88}
89
90static void expand_bits_to_bytes(uint8_t dst[], const uint8_t src[], int bits)
91{
92    int bytes = bits >> 3;
93
94    for (int i = 0; i < bytes; i++) {
95        unsigned mask = *src++;
96        dst[0] = (mask >> 7) & 1;
97        dst[1] = (mask >> 6) & 1;
98        dst[2] = (mask >> 5) & 1;
99        dst[3] = (mask >> 4) & 1;
100        dst[4] = (mask >> 3) & 1;
101        dst[5] = (mask >> 2) & 1;
102        dst[6] = (mask >> 1) & 1;
103        dst[7] = (mask >> 0) & 1;
104        dst += 8;
105    }
106
107    bits &= 7;
108    if (bits > 0) {
109        unsigned mask = *src;
110        do {
111            *dst++ = (mask >> 7) & 1;;
112            mask <<= 1;
113        } while (--bits != 0);
114    }
115}
116
117#define SkAlign8(x)     (((x) + 7) & ~7)
118
119bool SkWBMPImageDecoder::onDecode(SkStream* stream, SkBitmap* decodedBitmap,
120                                  SkBitmap::Config prefConfig, Mode mode)
121{
122    wbmp_head   head;
123
124    if (!head.init(stream)) {
125        return false;
126    }
127
128    int width = head.fWidth;
129    int height = head.fHeight;
130
131    // assign these directly, in case we return kDimensions_Result
132    decodedBitmap->setConfig(SkBitmap::kIndex8_Config, width, height);
133    decodedBitmap->setIsOpaque(true);
134
135    if (SkImageDecoder::kDecodeBounds_Mode == mode)
136        return true;
137
138    const SkPMColor colors[] = { SK_ColorBLACK, SK_ColorWHITE };
139    SkColorTable* ct = SkNEW_ARGS(SkColorTable, (colors, 2));
140    SkAutoUnref   aur(ct);
141
142    if (!this->allocPixelRef(decodedBitmap, ct)) {
143        return false;
144    }
145
146    SkAutoLockPixels alp(*decodedBitmap);
147
148    uint8_t* dst = decodedBitmap->getAddr8(0, 0);
149    // store the 1-bit valuess at the end of our pixels, so we won't stomp
150    // on them before we're read them. Just trying to avoid a temp allocation
151    size_t srcRB = SkAlign8(width) >> 3;
152    size_t srcSize = height * srcRB;
153    uint8_t* src = dst + decodedBitmap->getSize() - srcSize;
154    if (stream->read(src, srcSize) != srcSize) {
155        return false;
156    }
157
158    for (int y = 0; y < height; y++)
159    {
160        expand_bits_to_bytes(dst, src, width);
161        dst += decodedBitmap->rowBytes();
162        src += srcRB;
163    }
164
165    return true;
166}
167
168