aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/SkBitmapRegionDecoderInterface.cpp
blob: ec6327e565c99158261248c857a84ca1fbdaa821 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkBitmapRegionCanvas.h"
#include "SkBitmapRegionCodec.h"
#include "SkBitmapRegionDecoderInterface.h"
#include "SkBitmapRegionSampler.h"
#include "SkAndroidCodec.h"
#include "SkCodec.h"
#include "SkCodecPriv.h"
#include "SkImageDecoder.h"

SkBitmapRegionDecoderInterface* SkBitmapRegionDecoderInterface::CreateBitmapRegionDecoder(
        SkData* data, Strategy strategy) {
    switch (strategy) {
        case kOriginal_Strategy: {
            SkAutoTDelete<SkStreamRewindable> stream(new SkMemoryStream(data));
            SkImageDecoder* decoder = SkImageDecoder::Factory(stream);
            int width, height;
            if (nullptr == decoder) {
                SkCodecPrintf("Error: Could not create image decoder.\n");
                return nullptr;
            }
            if (!decoder->buildTileIndex(stream.detach(), &width, &height)) {
                SkCodecPrintf("Error: Could not build tile index.\n");
                delete decoder;
                return nullptr;
            }
            return new SkBitmapRegionSampler(decoder, width, height);
        }
        case kCanvas_Strategy: {
            SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
            if (nullptr == codec) {
                SkCodecPrintf("Error: Failed to create decoder.\n");
                return nullptr;
            }
            switch (codec->getScanlineOrder()) {
                case SkCodec::kTopDown_SkScanlineOrder:
                case SkCodec::kNone_SkScanlineOrder:
                    break;
                default:
                    SkCodecPrintf("Error: Scanline ordering not supported.\n");
                    return nullptr;
            }
            return new SkBitmapRegionCanvas(codec.detach());
        }
        case kAndroidCodec_Strategy: {
            SkAutoTDelete<SkAndroidCodec> codec = SkAndroidCodec::NewFromData(data);
            if (NULL == codec) {
                SkCodecPrintf("Error: Failed to create codec.\n");
                return NULL;
            }
            return new SkBitmapRegionCodec(codec.detach());
        }
        default:
            SkASSERT(false);
            return nullptr;
    }
}