aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/SkBitmapRegionDecoderInterface.cpp
blob: 5c769d676e4377da9ee1f34e1aeb06d76c48c9f0 (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
/*
 * 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 "SkBitmapRegionDecoderInterface.h"
#include "SkBitmapRegionSampler.h"
#include "SkCodec.h"
#include "SkImageDecoder.h"

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