msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 1 | /* |
| 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" |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 11 | #include "SkCodec.h" |
msarett | 04965c6 | 2015-10-12 10:24:38 -0700 | [diff] [blame] | 12 | #include "SkCodecPriv.h" |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 13 | #include "SkImageDecoder.h" |
| 14 | |
| 15 | SkBitmapRegionDecoderInterface* SkBitmapRegionDecoderInterface::CreateBitmapRegionDecoder( |
| 16 | SkStreamRewindable* stream, Strategy strategy) { |
scroggo | e361781 | 2015-10-09 12:15:57 -0700 | [diff] [blame] | 17 | SkAutoTDelete<SkStreamRewindable> streamDeleter(stream); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 18 | switch (strategy) { |
| 19 | case kOriginal_Strategy: { |
| 20 | SkImageDecoder* decoder = SkImageDecoder::Factory(stream); |
| 21 | int width, height; |
| 22 | if (nullptr == decoder) { |
msarett | 04965c6 | 2015-10-12 10:24:38 -0700 | [diff] [blame] | 23 | SkCodecPrintf("Error: Could not create image decoder.\n"); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 24 | return nullptr; |
| 25 | } |
scroggo | e361781 | 2015-10-09 12:15:57 -0700 | [diff] [blame] | 26 | if (!decoder->buildTileIndex(streamDeleter.detach(), &width, &height)) { |
msarett | 04965c6 | 2015-10-12 10:24:38 -0700 | [diff] [blame] | 27 | SkCodecPrintf("Error: Could not build tile index.\n"); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 28 | delete decoder; |
| 29 | return nullptr; |
| 30 | } |
| 31 | return new SkBitmapRegionSampler(decoder, width, height); |
| 32 | } |
| 33 | case kCanvas_Strategy: { |
scroggo | e361781 | 2015-10-09 12:15:57 -0700 | [diff] [blame] | 34 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(streamDeleter.detach())); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 35 | if (nullptr == codec) { |
msarett | 04965c6 | 2015-10-12 10:24:38 -0700 | [diff] [blame] | 36 | SkCodecPrintf("Error: Failed to create decoder.\n"); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 37 | return nullptr; |
| 38 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 39 | switch (codec->getScanlineOrder()) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 40 | case SkCodec::kTopDown_SkScanlineOrder: |
| 41 | case SkCodec::kNone_SkScanlineOrder: |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 42 | break; |
| 43 | default: |
msarett | 04965c6 | 2015-10-12 10:24:38 -0700 | [diff] [blame] | 44 | SkCodecPrintf("Error: Scanline ordering not supported.\n"); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 45 | return nullptr; |
| 46 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 47 | return new SkBitmapRegionCanvas(codec.detach()); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 48 | } |
| 49 | default: |
| 50 | SkASSERT(false); |
| 51 | return nullptr; |
| 52 | } |
| 53 | } |