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 "SkAndroidCodec.h"
      9 #include "SkBitmapRegionCodec.h"
     10 #include "SkBitmapRegionDecoderPriv.h"
     11 #include "SkCodecPriv.h"
     12 #include "SkPixelRef.h"
     13 
     14 SkBitmapRegionCodec::SkBitmapRegionCodec(SkAndroidCodec* codec)
     15     : INHERITED(codec->getInfo().width(), codec->getInfo().height())
     16     , fCodec(codec)
     17 {}
     18 
     19 bool SkBitmapRegionCodec::decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocator,
     20         const SkIRect& desiredSubset, int sampleSize, SkColorType prefColorType,
     21         bool requireUnpremul, sk_sp<SkColorSpace> prefColorSpace) {
     22 
     23     // Fix the input sampleSize if necessary.
     24     if (sampleSize < 1) {
     25         sampleSize = 1;
     26     }
     27 
     28     // The size of the output bitmap is determined by the size of the
     29     // requested subset, not by the size of the intersection of the subset
     30     // and the image dimensions.
     31     // If inputX is negative, we will need to place decoded pixels into the
     32     // output bitmap starting at a left offset.  Call this outX.
     33     // If outX is non-zero, subsetX must be zero.
     34     // If inputY is negative, we will need to place decoded pixels into the
     35     // output bitmap starting at a top offset.  Call this outY.
     36     // If outY is non-zero, subsetY must be zero.
     37     int outX;
     38     int outY;
     39     SkIRect subset = desiredSubset;
     40     SubsetType type = adjust_subset_rect(fCodec->getInfo().dimensions(), &subset, &outX, &outY);
     41     if (SubsetType::kOutside_SubsetType == type) {
     42         return false;
     43     }
     44 
     45     // Ask the codec for a scaled subset
     46     if (!fCodec->getSupportedSubset(&subset)) {
     47         SkCodecPrintf("Error: Could not get subset.\n");
     48         return false;
     49     }
     50     SkISize scaledSize = fCodec->getSampledSubsetDimensions(sampleSize, subset);
     51 
     52     // Create the image info for the decode
     53     SkColorType dstColorType = fCodec->computeOutputColorType(prefColorType);
     54     SkAlphaType dstAlphaType = fCodec->computeOutputAlphaType(requireUnpremul);
     55     sk_sp<SkColorSpace> dstColorSpace = fCodec->computeOutputColorSpace(dstColorType,
     56                                                                         prefColorSpace);
     57     SkImageInfo decodeInfo = SkImageInfo::Make(scaledSize.width(), scaledSize.height(),
     58                                                dstColorType, dstAlphaType, dstColorSpace);
     59 
     60     // Construct a color table for the decode if necessary
     61     sk_sp<SkColorTable> colorTable(nullptr);
     62     int maxColors = 256;
     63     SkPMColor colors[256];
     64     if (kIndex_8_SkColorType == dstColorType) {
     65         colorTable.reset(new SkColorTable(colors, maxColors));
     66     }
     67 
     68     // Initialize the destination bitmap
     69     int scaledOutX = 0;
     70     int scaledOutY = 0;
     71     int scaledOutWidth = scaledSize.width();
     72     int scaledOutHeight = scaledSize.height();
     73     if (SubsetType::kPartiallyInside_SubsetType == type) {
     74         scaledOutX = outX / sampleSize;
     75         scaledOutY = outY / sampleSize;
     76         // We need to be safe here because getSupportedSubset() may have modified the subset.
     77         const int extraX = SkTMax(0, desiredSubset.width() - outX - subset.width());
     78         const int extraY = SkTMax(0, desiredSubset.height() - outY - subset.height());
     79         const int scaledExtraX = extraX / sampleSize;
     80         const int scaledExtraY = extraY / sampleSize;
     81         scaledOutWidth += scaledOutX + scaledExtraX;
     82         scaledOutHeight += scaledOutY + scaledExtraY;
     83     }
     84     SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight);
     85     if (kGray_8_SkColorType == dstColorType) {
     86         // The legacy implementations of BitmapFactory and BitmapRegionDecoder
     87         // used kAlpha8 for grayscale images (before kGray8 existed).  While
     88         // the codec recognizes kGray8, we need to decode into a kAlpha8
     89         // bitmap in order to avoid a behavior change.
     90         outInfo = outInfo.makeColorType(kAlpha_8_SkColorType).makeAlphaType(kPremul_SkAlphaType);
     91     }
     92     bitmap->setInfo(outInfo);
     93     if (!bitmap->tryAllocPixels(allocator, colorTable.get())) {
     94         SkCodecPrintf("Error: Could not allocate pixels.\n");
     95         return false;
     96     }
     97 
     98     // Zero the bitmap if the region is not completely within the image.
     99     // TODO (msarett): Can we make this faster by implementing it to only
    100     //                 zero parts of the image that we won't overwrite with
    101     //                 pixels?
    102     SkCodec::ZeroInitialized zeroInit = allocator ? allocator->zeroInit() :
    103             SkCodec::kNo_ZeroInitialized;
    104     if (SubsetType::kPartiallyInside_SubsetType == type &&
    105             SkCodec::kNo_ZeroInitialized == zeroInit) {
    106         void* pixels = bitmap->getPixels();
    107         size_t bytes = outInfo.getSafeSize(bitmap->rowBytes());
    108         memset(pixels, 0, bytes);
    109     }
    110 
    111     // Decode into the destination bitmap
    112     SkAndroidCodec::AndroidOptions options;
    113     options.fSampleSize = sampleSize;
    114     options.fSubset = &subset;
    115     options.fColorPtr = colors;
    116     options.fColorCount = &maxColors;
    117     options.fZeroInitialized = zeroInit;
    118     void* dst = bitmap->getAddr(scaledOutX, scaledOutY);
    119 
    120     SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, bitmap->rowBytes(),
    121             &options);
    122     if (SkCodec::kSuccess != result && SkCodec::kIncompleteInput != result) {
    123         SkCodecPrintf("Error: Could not get pixels.\n");
    124         return false;
    125     }
    126 
    127     // Intialize the color table
    128     if (kIndex_8_SkColorType == dstColorType) {
    129         colorTable->dangerous_overwriteColors(colors, maxColors);
    130     }
    131 
    132     return true;
    133 }
    134 
    135 bool SkBitmapRegionCodec::conversionSupported(SkColorType colorType) {
    136     SkImageInfo dstInfo = fCodec->getInfo().makeColorType(colorType);
    137     return conversion_possible(dstInfo, fCodec->getInfo());
    138 }
    139