blob: 835ed9aa56426cbda09b49b19e814a91e7f21514 [file] [log] [blame]
msaretta5783ae2015-09-08 15:35:32 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkBitmapRegionCanvas.h"
9#include "SkBitmapRegionDecoderInterface.h"
10#include "SkBitmapRegionSampler.h"
scroggo46c57472015-09-30 08:57:13 -070011#include "SkCodec.h"
msarett04965c62015-10-12 10:24:38 -070012#include "SkCodecPriv.h"
msaretta5783ae2015-09-08 15:35:32 -070013#include "SkImageDecoder.h"
14
15SkBitmapRegionDecoderInterface* SkBitmapRegionDecoderInterface::CreateBitmapRegionDecoder(
16 SkStreamRewindable* stream, Strategy strategy) {
scroggoe3617812015-10-09 12:15:57 -070017 SkAutoTDelete<SkStreamRewindable> streamDeleter(stream);
msaretta5783ae2015-09-08 15:35:32 -070018 switch (strategy) {
19 case kOriginal_Strategy: {
20 SkImageDecoder* decoder = SkImageDecoder::Factory(stream);
21 int width, height;
22 if (nullptr == decoder) {
msarett04965c62015-10-12 10:24:38 -070023 SkCodecPrintf("Error: Could not create image decoder.\n");
msaretta5783ae2015-09-08 15:35:32 -070024 return nullptr;
25 }
scroggoe3617812015-10-09 12:15:57 -070026 if (!decoder->buildTileIndex(streamDeleter.detach(), &width, &height)) {
msarett04965c62015-10-12 10:24:38 -070027 SkCodecPrintf("Error: Could not build tile index.\n");
msaretta5783ae2015-09-08 15:35:32 -070028 delete decoder;
29 return nullptr;
30 }
31 return new SkBitmapRegionSampler(decoder, width, height);
32 }
33 case kCanvas_Strategy: {
scroggoe3617812015-10-09 12:15:57 -070034 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(streamDeleter.detach()));
msarette6dd0042015-10-09 11:07:34 -070035 if (nullptr == codec) {
msarett04965c62015-10-12 10:24:38 -070036 SkCodecPrintf("Error: Failed to create decoder.\n");
msaretta5783ae2015-09-08 15:35:32 -070037 return nullptr;
38 }
msarette6dd0042015-10-09 11:07:34 -070039 switch (codec->getScanlineOrder()) {
scroggo46c57472015-09-30 08:57:13 -070040 case SkCodec::kTopDown_SkScanlineOrder:
41 case SkCodec::kNone_SkScanlineOrder:
msaretta5783ae2015-09-08 15:35:32 -070042 break;
43 default:
msarett04965c62015-10-12 10:24:38 -070044 SkCodecPrintf("Error: Scanline ordering not supported.\n");
msaretta5783ae2015-09-08 15:35:32 -070045 return nullptr;
46 }
msarette6dd0042015-10-09 11:07:34 -070047 return new SkBitmapRegionCanvas(codec.detach());
msaretta5783ae2015-09-08 15:35:32 -070048 }
49 default:
50 SkASSERT(false);
51 return nullptr;
52 }
53}