gfx/skia/trunk/src/images/SkImageDecoder.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/images/SkImageDecoder.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,307 @@
     1.4 +/*
     1.5 + * Copyright 2006 The Android Open Source Project
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +
    1.12 +#include "SkImageDecoder.h"
    1.13 +#include "SkBitmap.h"
    1.14 +#include "SkImagePriv.h"
    1.15 +#include "SkPixelRef.h"
    1.16 +#include "SkStream.h"
    1.17 +#include "SkTemplates.h"
    1.18 +#include "SkCanvas.h"
    1.19 +
    1.20 +static SkBitmap::Config gDeviceConfig = SkBitmap::kNo_Config;
    1.21 +
    1.22 +SkBitmap::Config SkImageDecoder::GetDeviceConfig()
    1.23 +{
    1.24 +    return gDeviceConfig;
    1.25 +}
    1.26 +
    1.27 +void SkImageDecoder::SetDeviceConfig(SkBitmap::Config config)
    1.28 +{
    1.29 +    gDeviceConfig = config;
    1.30 +}
    1.31 +
    1.32 +///////////////////////////////////////////////////////////////////////////////
    1.33 +
    1.34 +SkImageDecoder::SkImageDecoder()
    1.35 +    : fPeeker(NULL)
    1.36 +    , fChooser(NULL)
    1.37 +    , fAllocator(NULL)
    1.38 +    , fSampleSize(1)
    1.39 +    , fDefaultPref(SkBitmap::kNo_Config)
    1.40 +    , fDitherImage(true)
    1.41 +    , fUsePrefTable(false)
    1.42 +    , fSkipWritingZeroes(false)
    1.43 +    , fPreferQualityOverSpeed(false)
    1.44 +    , fRequireUnpremultipliedColors(false) {
    1.45 +}
    1.46 +
    1.47 +SkImageDecoder::~SkImageDecoder() {
    1.48 +    SkSafeUnref(fPeeker);
    1.49 +    SkSafeUnref(fChooser);
    1.50 +    SkSafeUnref(fAllocator);
    1.51 +}
    1.52 +
    1.53 +void SkImageDecoder::copyFieldsToOther(SkImageDecoder* other) {
    1.54 +    if (NULL == other) {
    1.55 +        return;
    1.56 +    }
    1.57 +    other->setPeeker(fPeeker);
    1.58 +    other->setChooser(fChooser);
    1.59 +    other->setAllocator(fAllocator);
    1.60 +    other->setSampleSize(fSampleSize);
    1.61 +    if (fUsePrefTable) {
    1.62 +        other->setPrefConfigTable(fPrefTable);
    1.63 +    } else {
    1.64 +        other->fDefaultPref = fDefaultPref;
    1.65 +    }
    1.66 +    other->setDitherImage(fDitherImage);
    1.67 +    other->setSkipWritingZeroes(fSkipWritingZeroes);
    1.68 +    other->setPreferQualityOverSpeed(fPreferQualityOverSpeed);
    1.69 +    other->setRequireUnpremultipliedColors(fRequireUnpremultipliedColors);
    1.70 +}
    1.71 +
    1.72 +SkImageDecoder::Format SkImageDecoder::getFormat() const {
    1.73 +    return kUnknown_Format;
    1.74 +}
    1.75 +
    1.76 +const char* SkImageDecoder::getFormatName() const {
    1.77 +    return GetFormatName(this->getFormat());
    1.78 +}
    1.79 +
    1.80 +const char* SkImageDecoder::GetFormatName(Format format) {
    1.81 +    switch (format) {
    1.82 +        case kUnknown_Format:
    1.83 +            return "Unknown Format";
    1.84 +        case kBMP_Format:
    1.85 +            return "BMP";
    1.86 +        case kGIF_Format:
    1.87 +            return "GIF";
    1.88 +        case kICO_Format:
    1.89 +            return "ICO";
    1.90 +        case kJPEG_Format:
    1.91 +            return "JPEG";
    1.92 +        case kPNG_Format:
    1.93 +            return "PNG";
    1.94 +        case kWBMP_Format:
    1.95 +            return "WBMP";
    1.96 +        case kWEBP_Format:
    1.97 +            return "WEBP";
    1.98 +        default:
    1.99 +            SkDEBUGFAIL("Invalid format type!");
   1.100 +    }
   1.101 +    return "Unknown Format";
   1.102 +}
   1.103 +
   1.104 +SkImageDecoder::Peeker* SkImageDecoder::setPeeker(Peeker* peeker) {
   1.105 +    SkRefCnt_SafeAssign(fPeeker, peeker);
   1.106 +    return peeker;
   1.107 +}
   1.108 +
   1.109 +SkImageDecoder::Chooser* SkImageDecoder::setChooser(Chooser* chooser) {
   1.110 +    SkRefCnt_SafeAssign(fChooser, chooser);
   1.111 +    return chooser;
   1.112 +}
   1.113 +
   1.114 +SkBitmap::Allocator* SkImageDecoder::setAllocator(SkBitmap::Allocator* alloc) {
   1.115 +    SkRefCnt_SafeAssign(fAllocator, alloc);
   1.116 +    return alloc;
   1.117 +}
   1.118 +
   1.119 +void SkImageDecoder::setSampleSize(int size) {
   1.120 +    if (size < 1) {
   1.121 +        size = 1;
   1.122 +    }
   1.123 +    fSampleSize = size;
   1.124 +}
   1.125 +
   1.126 +bool SkImageDecoder::chooseFromOneChoice(SkBitmap::Config config, int width,
   1.127 +                                         int height) const {
   1.128 +    Chooser* chooser = fChooser;
   1.129 +
   1.130 +    if (NULL == chooser) {    // no chooser, we just say YES to decoding :)
   1.131 +        return true;
   1.132 +    }
   1.133 +    chooser->begin(1);
   1.134 +    chooser->inspect(0, config, width, height);
   1.135 +    return chooser->choose() == 0;
   1.136 +}
   1.137 +
   1.138 +bool SkImageDecoder::allocPixelRef(SkBitmap* bitmap,
   1.139 +                                   SkColorTable* ctable) const {
   1.140 +    return bitmap->allocPixels(fAllocator, ctable);
   1.141 +}
   1.142 +
   1.143 +///////////////////////////////////////////////////////////////////////////////
   1.144 +
   1.145 +void SkImageDecoder::setPrefConfigTable(const PrefConfigTable& prefTable) {
   1.146 +    fUsePrefTable = true;
   1.147 +    fPrefTable = prefTable;
   1.148 +}
   1.149 +
   1.150 +SkBitmap::Config SkImageDecoder::getPrefConfig(SrcDepth srcDepth,
   1.151 +                                               bool srcHasAlpha) const {
   1.152 +    SkBitmap::Config config = SkBitmap::kNo_Config;
   1.153 +
   1.154 +    if (fUsePrefTable) {
   1.155 +        switch (srcDepth) {
   1.156 +            case kIndex_SrcDepth:
   1.157 +                config = srcHasAlpha ? fPrefTable.fPrefFor_8Index_YesAlpha_src
   1.158 +                                     : fPrefTable.fPrefFor_8Index_NoAlpha_src;
   1.159 +                break;
   1.160 +            case k8BitGray_SrcDepth:
   1.161 +                config = fPrefTable.fPrefFor_8Gray_src;
   1.162 +                break;
   1.163 +            case k32Bit_SrcDepth:
   1.164 +                config = srcHasAlpha ? fPrefTable.fPrefFor_8bpc_YesAlpha_src
   1.165 +                                     : fPrefTable.fPrefFor_8bpc_NoAlpha_src;
   1.166 +                break;
   1.167 +        }
   1.168 +    } else {
   1.169 +        config = fDefaultPref;
   1.170 +    }
   1.171 +
   1.172 +    if (SkBitmap::kNo_Config == config) {
   1.173 +        config = SkImageDecoder::GetDeviceConfig();
   1.174 +    }
   1.175 +    return config;
   1.176 +}
   1.177 +
   1.178 +bool SkImageDecoder::decode(SkStream* stream, SkBitmap* bm,
   1.179 +                            SkBitmap::Config pref, Mode mode) {
   1.180 +    // we reset this to false before calling onDecode
   1.181 +    fShouldCancelDecode = false;
   1.182 +    // assign this, for use by getPrefConfig(), in case fUsePrefTable is false
   1.183 +    fDefaultPref = pref;
   1.184 +
   1.185 +    // pass a temporary bitmap, so that if we return false, we are assured of
   1.186 +    // leaving the caller's bitmap untouched.
   1.187 +    SkBitmap    tmp;
   1.188 +    if (!this->onDecode(stream, &tmp, mode)) {
   1.189 +        return false;
   1.190 +    }
   1.191 +    bm->swap(tmp);
   1.192 +    return true;
   1.193 +}
   1.194 +
   1.195 +bool SkImageDecoder::decodeSubset(SkBitmap* bm, const SkIRect& rect,
   1.196 +                                  SkBitmap::Config pref) {
   1.197 +    // we reset this to false before calling onDecodeSubset
   1.198 +    fShouldCancelDecode = false;
   1.199 +    // assign this, for use by getPrefConfig(), in case fUsePrefTable is false
   1.200 +    fDefaultPref = pref;
   1.201 +
   1.202 +    return this->onDecodeSubset(bm, rect);
   1.203 +}
   1.204 +
   1.205 +bool SkImageDecoder::buildTileIndex(SkStreamRewindable* stream,
   1.206 +                                    int *width, int *height) {
   1.207 +    // we reset this to false before calling onBuildTileIndex
   1.208 +    fShouldCancelDecode = false;
   1.209 +
   1.210 +    return this->onBuildTileIndex(stream, width, height);
   1.211 +}
   1.212 +
   1.213 +bool SkImageDecoder::cropBitmap(SkBitmap *dst, SkBitmap *src, int sampleSize,
   1.214 +                                int dstX, int dstY, int width, int height,
   1.215 +                                int srcX, int srcY) {
   1.216 +    int w = width / sampleSize;
   1.217 +    int h = height / sampleSize;
   1.218 +    if (src->config() == SkBitmap::kIndex8_Config) {
   1.219 +        // kIndex8 does not allow drawing via an SkCanvas, as is done below.
   1.220 +        // Instead, use extractSubset. Note that this shares the SkPixelRef and
   1.221 +        // SkColorTable.
   1.222 +        // FIXME: Since src is discarded in practice, this holds on to more
   1.223 +        // pixels than is strictly necessary. Switch to a copy if memory
   1.224 +        // savings are more important than speed here. This also means
   1.225 +        // that the pixels in dst can not be reused (though there is no
   1.226 +        // allocation, which was already done on src).
   1.227 +        int x = (dstX - srcX) / sampleSize;
   1.228 +        int y = (dstY - srcY) / sampleSize;
   1.229 +        SkIRect subset = SkIRect::MakeXYWH(x, y, w, h);
   1.230 +        return src->extractSubset(dst, subset);
   1.231 +    }
   1.232 +    // if the destination has no pixels then we must allocate them.
   1.233 +    if (dst->isNull()) {
   1.234 +        dst->setConfig(src->config(), w, h, 0, src->alphaType());
   1.235 +
   1.236 +        if (!this->allocPixelRef(dst, NULL)) {
   1.237 +            SkDEBUGF(("failed to allocate pixels needed to crop the bitmap"));
   1.238 +            return false;
   1.239 +        }
   1.240 +    }
   1.241 +    // check to see if the destination is large enough to decode the desired
   1.242 +    // region. If this assert fails we will just draw as much of the source
   1.243 +    // into the destination that we can.
   1.244 +    if (dst->width() < w || dst->height() < h) {
   1.245 +        SkDEBUGF(("SkImageDecoder::cropBitmap does not have a large enough bitmap.\n"));
   1.246 +    }
   1.247 +
   1.248 +    // Set the Src_Mode for the paint to prevent transparency issue in the
   1.249 +    // dest in the event that the dest was being re-used.
   1.250 +    SkPaint paint;
   1.251 +    paint.setXfermodeMode(SkXfermode::kSrc_Mode);
   1.252 +
   1.253 +    SkCanvas canvas(*dst);
   1.254 +    canvas.drawSprite(*src, (srcX - dstX) / sampleSize,
   1.255 +                            (srcY - dstY) / sampleSize,
   1.256 +                            &paint);
   1.257 +    return true;
   1.258 +}
   1.259 +
   1.260 +///////////////////////////////////////////////////////////////////////////////
   1.261 +
   1.262 +bool SkImageDecoder::DecodeFile(const char file[], SkBitmap* bm,
   1.263 +                            SkBitmap::Config pref,  Mode mode, Format* format) {
   1.264 +    SkASSERT(file);
   1.265 +    SkASSERT(bm);
   1.266 +
   1.267 +    SkAutoTUnref<SkStreamRewindable> stream(SkStream::NewFromFile(file));
   1.268 +    if (stream.get()) {
   1.269 +        if (SkImageDecoder::DecodeStream(stream, bm, pref, mode, format)) {
   1.270 +            bm->pixelRef()->setURI(file);
   1.271 +            return true;
   1.272 +        }
   1.273 +    }
   1.274 +    return false;
   1.275 +}
   1.276 +
   1.277 +bool SkImageDecoder::DecodeMemory(const void* buffer, size_t size, SkBitmap* bm,
   1.278 +                          SkBitmap::Config pref, Mode mode, Format* format) {
   1.279 +    if (0 == size) {
   1.280 +        return false;
   1.281 +    }
   1.282 +    SkASSERT(buffer);
   1.283 +
   1.284 +    SkMemoryStream  stream(buffer, size);
   1.285 +    return SkImageDecoder::DecodeStream(&stream, bm, pref, mode, format);
   1.286 +}
   1.287 +
   1.288 +bool SkImageDecoder::DecodeStream(SkStreamRewindable* stream, SkBitmap* bm,
   1.289 +                                  SkBitmap::Config pref, Mode mode,
   1.290 +                                  Format* format) {
   1.291 +    SkASSERT(stream);
   1.292 +    SkASSERT(bm);
   1.293 +
   1.294 +    bool success = false;
   1.295 +    SkImageDecoder* codec = SkImageDecoder::Factory(stream);
   1.296 +
   1.297 +    if (NULL != codec) {
   1.298 +        success = codec->decode(stream, bm, pref, mode);
   1.299 +        if (success && format) {
   1.300 +            *format = codec->getFormat();
   1.301 +            if (kUnknown_Format == *format) {
   1.302 +                if (stream->rewind()) {
   1.303 +                    *format = GetStreamFormat(stream);
   1.304 +                }
   1.305 +            }
   1.306 +        }
   1.307 +        delete codec;
   1.308 +    }
   1.309 +    return success;
   1.310 +}

mercurial