Home | History | Annotate | Download | only in android
      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 "SkBitmapRegionCodec.h"
     10 #include "SkBitmapRegionDecoder.h"
     11 #include "SkAndroidCodec.h"
     12 #include "SkCodec.h"
     13 #include "SkCodecPriv.h"
     14 #include "SkImageDecoder.h"
     15 
     16 SkBitmapRegionDecoder* SkBitmapRegionDecoder::Create(
     17         SkData* data, Strategy strategy) {
     18     return SkBitmapRegionDecoder::Create(new SkMemoryStream(data),
     19             strategy);
     20 }
     21 
     22 SkBitmapRegionDecoder* SkBitmapRegionDecoder::Create(
     23         SkStreamRewindable* stream, Strategy strategy) {
     24     SkAutoTDelete<SkStreamRewindable> streamDeleter(stream);
     25     switch (strategy) {
     26         case kCanvas_Strategy: {
     27             SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(streamDeleter.detach()));
     28             if (nullptr == codec) {
     29                 SkCodecPrintf("Error: Failed to create decoder.\n");
     30                 return nullptr;
     31             }
     32 
     33             SkEncodedFormat format = codec->getEncodedFormat();
     34             switch (format) {
     35                 case SkEncodedFormat::kJPEG_SkEncodedFormat:
     36                 case SkEncodedFormat::kPNG_SkEncodedFormat:
     37                     break;
     38                 default:
     39                     // FIXME: Support webp using a special case.  Webp does not support
     40                     //        scanline decoding.
     41                     return nullptr;
     42             }
     43 
     44             // If the image is a jpeg or a png, the scanline ordering should always be
     45             // kTopDown or kNone.  It is relevant to check because this implementation
     46             // only supports these two scanline orderings.
     47             SkASSERT(SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder() ||
     48                     SkCodec::kNone_SkScanlineOrder == codec->getScanlineOrder());
     49 
     50             return new SkBitmapRegionCanvas(codec.detach());
     51         }
     52         case kAndroidCodec_Strategy: {
     53             SkAutoTDelete<SkAndroidCodec> codec =
     54                     SkAndroidCodec::NewFromStream(streamDeleter.detach());
     55             if (nullptr == codec) {
     56                 SkCodecPrintf("Error: Failed to create codec.\n");
     57                 return NULL;
     58             }
     59 
     60             SkEncodedFormat format = codec->getEncodedFormat();
     61             switch (format) {
     62                 case SkEncodedFormat::kJPEG_SkEncodedFormat:
     63                 case SkEncodedFormat::kPNG_SkEncodedFormat:
     64                 case SkEncodedFormat::kWEBP_SkEncodedFormat:
     65                     break;
     66                 default:
     67                     return nullptr;
     68             }
     69 
     70             return new SkBitmapRegionCodec(codec.detach());
     71         }
     72         default:
     73             SkASSERT(false);
     74             return nullptr;
     75     }
     76 }
     77