aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/android
diff options
context:
space:
mode:
authorGravatar msarett <msarett@google.com>2015-11-13 06:11:09 -0800
committerGravatar Commit bot <commit-bot@chromium.org>2015-11-13 06:11:09 -0800
commit506e19a4c8395178e8da81576b3c37616593e560 (patch)
tree0877d660a91ef6322059269e7020988459e91ded /tools/android
parent9c8605144a0f15e3e69a4e1dcd5d3e63f339380e (diff)
Move SkBitmapRegionDecoder to include/android and src/android
This will expose the BitmapRegionDecoder API as a public include and move the implementation to src. This makes this code more naturally exposed in Android and easier to test in DM and nanobench. BUG=skia: Review URL: https://codereview.chromium.org/1438873002
Diffstat (limited to 'tools/android')
-rw-r--r--tools/android/SkBRDAllocator.h29
-rw-r--r--tools/android/SkBitmapRegionCanvas.cpp140
-rw-r--r--tools/android/SkBitmapRegionCanvas.h41
-rw-r--r--tools/android/SkBitmapRegionCodec.cpp147
-rw-r--r--tools/android/SkBitmapRegionCodec.h37
-rw-r--r--tools/android/SkBitmapRegionDecoder.cpp62
-rw-r--r--tools/android/SkBitmapRegionDecoder.h90
-rw-r--r--tools/android/SkBitmapRegionDecoderPriv.h63
8 files changed, 0 insertions, 609 deletions
diff --git a/tools/android/SkBRDAllocator.h b/tools/android/SkBRDAllocator.h
deleted file mode 100644
index f003af6f6f..0000000000
--- a/tools/android/SkBRDAllocator.h
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Copyright 2015 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#ifndef SkBRDAllocator_DEFINED
-#define SkBRDAllocator_DEFINED
-
-#include "SkBitmap.h"
-#include "SkCodec.h"
-
-/**
- * Abstract subclass of SkBitmap's allocator.
- * Allows the allocator to indicate if the memory it allocates
- * is zero initialized.
- */
-class SkBRDAllocator : public SkBitmap::Allocator {
-public:
-
- /**
- * Indicates if the memory allocated by this allocator is
- * zero initialized.
- */
- virtual SkCodec::ZeroInitialized zeroInit() const = 0;
-};
-
-#endif
diff --git a/tools/android/SkBitmapRegionCanvas.cpp b/tools/android/SkBitmapRegionCanvas.cpp
deleted file mode 100644
index bac5dc1ffc..0000000000
--- a/tools/android/SkBitmapRegionCanvas.cpp
+++ /dev/null
@@ -1,140 +0,0 @@
-/*
- * Copyright 2015 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include "SkBitmapRegionCanvas.h"
-#include "SkBitmapRegionDecoderPriv.h"
-#include "SkCanvas.h"
-#include "SkCodecPriv.h"
-
-SkBitmapRegionCanvas::SkBitmapRegionCanvas(SkCodec* decoder)
- : INHERITED(decoder->getInfo().width(), decoder->getInfo().height())
- , fDecoder(decoder)
-{}
-
-bool SkBitmapRegionCanvas::decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocator,
- const SkIRect& desiredSubset, int sampleSize, SkColorType dstColorType,
- bool requireUnpremul) {
- // Reject color types not supported by this method
- if (kIndex_8_SkColorType == dstColorType || kGray_8_SkColorType == dstColorType) {
- SkCodecPrintf("Error: Color type not supported.\n");
- return false;
- }
-
- // Reject requests for unpremultiplied alpha
- if (requireUnpremul) {
- SkCodecPrintf("Error: Alpha type not supported.\n");
- return false;
- }
- SkAlphaType dstAlphaType = fDecoder->getInfo().alphaType();
- if (kUnpremul_SkAlphaType == dstAlphaType) {
- dstAlphaType = kPremul_SkAlphaType;
- }
-
- // FIXME: Can we add checks and support kIndex8 or unpremultiplied alpha in special cases?
-
- // Fix the input sampleSize if necessary.
- if (sampleSize < 1) {
- sampleSize = 1;
- }
-
- // The size of the output bitmap is determined by the size of the
- // requested subset, not by the size of the intersection of the subset
- // and the image dimensions.
- // If inputX is negative, we will need to place decoded pixels into the
- // output bitmap starting at a left offset. Call this outX.
- // If outX is non-zero, subsetX must be zero.
- // If inputY is negative, we will need to place decoded pixels into the
- // output bitmap starting at a top offset. Call this outY.
- // If outY is non-zero, subsetY must be zero.
- int outX;
- int outY;
- SkIRect subset = desiredSubset;
- SubsetType type = adjust_subset_rect(fDecoder->getInfo().dimensions(), &subset, &outX, &outY);
- if (SubsetType::kOutside_SubsetType == type) {
- return false;
- }
-
- // Create the image info for the decode
- SkImageInfo decodeInfo = SkImageInfo::Make(this->width(), this->height(),
- dstColorType, dstAlphaType);
-
- // Start the scanline decoder
- SkCodec::Result r = fDecoder->startScanlineDecode(decodeInfo);
- if (SkCodec::kSuccess != r) {
- SkCodecPrintf("Error: Could not start scanline decoder.\n");
- return false;
- }
-
- // Allocate a bitmap for the unscaled decode
- SkBitmap tmp;
- SkImageInfo tmpInfo = decodeInfo.makeWH(this->width(), subset.height());
- if (!tmp.tryAllocPixels(tmpInfo)) {
- SkCodecPrintf("Error: Could not allocate pixels.\n");
- return false;
- }
-
- // Skip the unneeded rows
- if (!fDecoder->skipScanlines(subset.y())) {
- SkCodecPrintf("Error: Failed to skip scanlines.\n");
- return false;
- }
-
- // Decode the necessary rows
- fDecoder->getScanlines(tmp.getAddr(0, 0), subset.height(), tmp.rowBytes());
-
- // Calculate the size of the output
- const int outWidth = get_scaled_dimension(desiredSubset.width(), sampleSize);
- const int outHeight = get_scaled_dimension(desiredSubset.height(), sampleSize);
-
- // Initialize the destination bitmap
- SkImageInfo dstInfo = decodeInfo.makeWH(outWidth, outHeight);
- bitmap->setInfo(dstInfo, dstInfo.minRowBytes());
- if (!bitmap->tryAllocPixels(allocator, nullptr)) {
- SkCodecPrintf("Error: Could not allocate pixels.\n");
- return false;
- }
-
- // Zero the bitmap if the region is not completely within the image.
- // TODO (msarett): Can we make this faster by implementing it to only
- // zero parts of the image that we won't overwrite with
- // pixels?
- if (SubsetType::kPartiallyInside_SubsetType == type) {
- SkCodec::ZeroInitialized zeroInit = allocator ? allocator->zeroInit() :
- SkCodec::kNo_ZeroInitialized;
- if (SkCodec::kNo_ZeroInitialized == zeroInit) {
- bitmap->eraseColor(0);
- }
- }
-
- // Use a canvas to crop and scale to the destination bitmap
- SkCanvas canvas(*bitmap);
- // TODO (msarett): Maybe we can take advantage of the fact that SkRect uses floats?
- SkRect src = SkRect::MakeXYWH((SkScalar) subset.x(), (SkScalar) 0,
- (SkScalar) subset.width(), (SkScalar) subset.height());
- SkRect dst = SkRect::MakeXYWH((SkScalar) (outX / sampleSize), (SkScalar) (outY / sampleSize),
- (SkScalar) get_scaled_dimension(subset.width(), sampleSize),
- (SkScalar) get_scaled_dimension(subset.height(), sampleSize));
- SkPaint paint;
- // Overwrite the dst with the src pixels
- paint.setXfermodeMode(SkXfermode::kSrc_Mode);
- // TODO (msarett): Test multiple filter qualities. kNone is the default.
- canvas.drawBitmapRect(tmp, src, dst, &paint);
-
- return true;
-}
-
-bool SkBitmapRegionCanvas::conversionSupported(SkColorType colorType) {
- // SkCanvas does not draw to these color types.
- if (kIndex_8_SkColorType == colorType || kGray_8_SkColorType == colorType) {
- return false;
- }
-
- // FIXME: Call virtual function when it lands.
- SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fDecoder->getInfo().alphaType(),
- fDecoder->getInfo().profileType());
- return conversion_possible(info, fDecoder->getInfo());
-}
diff --git a/tools/android/SkBitmapRegionCanvas.h b/tools/android/SkBitmapRegionCanvas.h
deleted file mode 100644
index 2edbf1ff3b..0000000000
--- a/tools/android/SkBitmapRegionCanvas.h
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright 2015 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include "SkBitmap.h"
-#include "SkBitmapRegionDecoder.h"
-#include "SkCodec.h"
-
-/*
- * This class implements SkBitmapRegionDecoder using an SkCodec and
- * an SkCanvas. It uses the scanline decoder to subset the height. It then
- * will subset the width and scale by drawing to an SkCanvas.
- */
-// FIXME (msarett): This implementation does not support WEBP, because WEBP
-// does not have a scanline decoder.
-class SkBitmapRegionCanvas : public SkBitmapRegionDecoder {
-public:
-
- /*
- * Takes ownership of pointer to decoder
- */
- SkBitmapRegionCanvas(SkCodec* decoder);
-
- bool decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocator,
- const SkIRect& desiredSubset, int sampleSize,
- SkColorType colorType, bool requireUnpremul) override;
-
- bool conversionSupported(SkColorType colorType) override;
-
- SkEncodedFormat getEncodedFormat() override { return fDecoder->getEncodedFormat(); }
-
-private:
-
- SkAutoTDelete<SkCodec> fDecoder;
-
- typedef SkBitmapRegionDecoder INHERITED;
-
-};
diff --git a/tools/android/SkBitmapRegionCodec.cpp b/tools/android/SkBitmapRegionCodec.cpp
deleted file mode 100644
index 415b60c5ab..0000000000
--- a/tools/android/SkBitmapRegionCodec.cpp
+++ /dev/null
@@ -1,147 +0,0 @@
-/*
- * Copyright 2015 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include "SkAndroidCodec.h"
-#include "SkBitmapRegionCodec.h"
-#include "SkBitmapRegionDecoderPriv.h"
-#include "SkCodecPriv.h"
-#include "SkPixelRef.h"
-
-SkBitmapRegionCodec::SkBitmapRegionCodec(SkAndroidCodec* codec)
- : INHERITED(codec->getInfo().width(), codec->getInfo().height())
- , fCodec(codec)
-{}
-
-bool SkBitmapRegionCodec::decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocator,
- const SkIRect& desiredSubset, int sampleSize, SkColorType dstColorType,
- bool requireUnpremul) {
-
- // Fix the input sampleSize if necessary.
- if (sampleSize < 1) {
- sampleSize = 1;
- }
-
- // The size of the output bitmap is determined by the size of the
- // requested subset, not by the size of the intersection of the subset
- // and the image dimensions.
- // If inputX is negative, we will need to place decoded pixels into the
- // output bitmap starting at a left offset. Call this outX.
- // If outX is non-zero, subsetX must be zero.
- // If inputY is negative, we will need to place decoded pixels into the
- // output bitmap starting at a top offset. Call this outY.
- // If outY is non-zero, subsetY must be zero.
- int outX;
- int outY;
- SkIRect subset = desiredSubset;
- SubsetType type = adjust_subset_rect(fCodec->getInfo().dimensions(), &subset, &outX, &outY);
- if (SubsetType::kOutside_SubsetType == type) {
- return false;
- }
-
- // Ask the codec for a scaled subset
- if (!fCodec->getSupportedSubset(&subset)) {
- SkCodecPrintf("Error: Could not get subset.\n");
- return false;
- }
- SkISize scaledSize = fCodec->getSampledSubsetDimensions(sampleSize, subset);
-
- // Create the image info for the decode
- SkAlphaType dstAlphaType = fCodec->getInfo().alphaType();
- if (kOpaque_SkAlphaType != dstAlphaType) {
- dstAlphaType = requireUnpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
- }
- SkImageInfo decodeInfo = SkImageInfo::Make(scaledSize.width(), scaledSize.height(),
- dstColorType, dstAlphaType);
-
- // Construct a color table for the decode if necessary
- SkAutoTUnref<SkColorTable> colorTable(nullptr);
- SkPMColor* colorPtr = nullptr;
- int* colorCountPtr = nullptr;
- int maxColors = 256;
- SkPMColor colors[256];
- if (kIndex_8_SkColorType == dstColorType) {
- // TODO (msarett): This performs a copy that is unnecessary since
- // we have not yet initialized the color table.
- // And then we need to use a const cast to get
- // a pointer to the color table that we can
- // modify during the decode. We could alternatively
- // perform the decode before creating the bitmap and
- // the color table. We still would need to copy the
- // colors into the color table after the decode.
- colorTable.reset(new SkColorTable(colors, maxColors));
- colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
- colorCountPtr = &maxColors;
- }
-
- // Initialize the destination bitmap
- int scaledOutX = 0;
- int scaledOutY = 0;
- int scaledOutWidth = scaledSize.width();
- int scaledOutHeight = scaledSize.height();
- if (SubsetType::kPartiallyInside_SubsetType == type) {
- scaledOutX = outX / sampleSize;
- scaledOutY = outY / sampleSize;
- // We need to be safe here because getSupportedSubset() may have modified the subset.
- const int extraX = SkTMax(0, desiredSubset.width() - outX - subset.width());
- const int extraY = SkTMax(0, desiredSubset.height() - outY - subset.height());
- const int scaledExtraX = extraX / sampleSize;
- const int scaledExtraY = extraY / sampleSize;
- scaledOutWidth += scaledOutX + scaledExtraX;
- scaledOutHeight += scaledOutY + scaledExtraY;
- }
- SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight);
- bitmap->setInfo(outInfo);
- if (!bitmap->tryAllocPixels(allocator, colorTable.get())) {
- SkCodecPrintf("Error: Could not allocate pixels.\n");
- return false;
- }
-
- // Zero the bitmap if the region is not completely within the image.
- // TODO (msarett): Can we make this faster by implementing it to only
- // zero parts of the image that we won't overwrite with
- // pixels?
- SkCodec::ZeroInitialized zeroInit = allocator ? allocator->zeroInit() :
- SkCodec::kNo_ZeroInitialized;
- if (SubsetType::kPartiallyInside_SubsetType == type &&
- SkCodec::kNo_ZeroInitialized == zeroInit) {
- void* pixels = bitmap->getPixels();
- size_t bytes = outInfo.getSafeSize(bitmap->rowBytes());
- memset(pixels, 0, bytes);
- }
-
- // Decode into the destination bitmap
- SkAndroidCodec::AndroidOptions options;
- options.fSampleSize = sampleSize;
- options.fSubset = &subset;
- options.fColorPtr = colorPtr;
- options.fColorCount = colorCountPtr;
- options.fZeroInitialized = zeroInit;
- void* dst = bitmap->getAddr(scaledOutX, scaledOutY);
-
- // FIXME: skbug.com/4538
- // It is important that we use the rowBytes on the pixelRef. They may not be
- // set properly on the bitmap.
- SkPixelRef* pr = SkRef(bitmap->pixelRef());
- size_t rowBytes = pr->rowBytes();
- bitmap->setInfo(outInfo, rowBytes);
- bitmap->setPixelRef(pr)->unref();
- bitmap->lockPixels();
- SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, rowBytes, &options);
- if (SkCodec::kSuccess != result && SkCodec::kIncompleteInput != result) {
- SkCodecPrintf("Error: Could not get pixels.\n");
- return false;
- }
-
- return true;
-}
-
-bool SkBitmapRegionCodec::conversionSupported(SkColorType colorType) {
- // FIXME: Call virtual function when it lands.
- SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fCodec->getInfo().alphaType(),
- fCodec->getInfo().profileType());
- return conversion_possible(info, fCodec->getInfo());
-}
diff --git a/tools/android/SkBitmapRegionCodec.h b/tools/android/SkBitmapRegionCodec.h
deleted file mode 100644
index 79774173b0..0000000000
--- a/tools/android/SkBitmapRegionCodec.h
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * Copyright 2015 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include "SkBitmap.h"
-#include "SkBitmapRegionDecoder.h"
-#include "SkAndroidCodec.h"
-
-/*
- * This class implements SkBitmapRegionDecoder using an SkAndroidCodec.
- */
-class SkBitmapRegionCodec : public SkBitmapRegionDecoder {
-public:
-
- /*
- * Takes ownership of pointer to codec
- */
- SkBitmapRegionCodec(SkAndroidCodec* codec);
-
- bool decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocator,
- const SkIRect& desiredSubset, int sampleSize,
- SkColorType colorType, bool requireUnpremul) override;
-
- bool conversionSupported(SkColorType colorType) override;
-
- SkEncodedFormat getEncodedFormat() override { return fCodec->getEncodedFormat(); }
-
-private:
-
- SkAutoTDelete<SkAndroidCodec> fCodec;
-
- typedef SkBitmapRegionDecoder INHERITED;
-
-};
diff --git a/tools/android/SkBitmapRegionDecoder.cpp b/tools/android/SkBitmapRegionDecoder.cpp
deleted file mode 100644
index 7dc05059e1..0000000000
--- a/tools/android/SkBitmapRegionDecoder.cpp
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * Copyright 2015 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include "SkBitmapRegionCanvas.h"
-#include "SkBitmapRegionCodec.h"
-#include "SkBitmapRegionDecoder.h"
-#include "SkAndroidCodec.h"
-#include "SkCodec.h"
-#include "SkCodecPriv.h"
-#include "SkImageDecoder.h"
-
-SkBitmapRegionDecoder* SkBitmapRegionDecoder::Create(
- SkData* data, Strategy strategy) {
- return SkBitmapRegionDecoder::Create(new SkMemoryStream(data),
- strategy);
-}
-
-SkBitmapRegionDecoder* SkBitmapRegionDecoder::Create(
- SkStreamRewindable* stream, Strategy strategy) {
- SkAutoTDelete<SkStreamRewindable> streamDeleter(stream);
- switch (strategy) {
- case kCanvas_Strategy: {
- SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(streamDeleter.detach()));
- if (nullptr == codec) {
- SkCodecPrintf("Error: Failed to create decoder.\n");
- return nullptr;
- }
-
- if (SkEncodedFormat::kWEBP_SkEncodedFormat == codec->getEncodedFormat()) {
- // FIXME: Support webp using a special case. Webp does not support
- // scanline decoding.
- return nullptr;
- }
-
- switch (codec->getScanlineOrder()) {
- case SkCodec::kTopDown_SkScanlineOrder:
- case SkCodec::kNone_SkScanlineOrder:
- break;
- default:
- SkCodecPrintf("Error: Scanline ordering not supported.\n");
- return nullptr;
- }
- return new SkBitmapRegionCanvas(codec.detach());
- }
- case kAndroidCodec_Strategy: {
- SkAutoTDelete<SkAndroidCodec> codec =
- SkAndroidCodec::NewFromStream(streamDeleter.detach());
- if (NULL == codec) {
- SkCodecPrintf("Error: Failed to create codec.\n");
- return NULL;
- }
- return new SkBitmapRegionCodec(codec.detach());
- }
- default:
- SkASSERT(false);
- return nullptr;
- }
-}
diff --git a/tools/android/SkBitmapRegionDecoder.h b/tools/android/SkBitmapRegionDecoder.h
deleted file mode 100644
index 575ad9dc01..0000000000
--- a/tools/android/SkBitmapRegionDecoder.h
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * Copyright 2015 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#ifndef SkBitmapRegionDecoder_DEFINED
-#define SkBitmapRegionDecoder_DEFINED
-
-#include "SkBitmap.h"
-#include "SkBRDAllocator.h"
-#include "SkEncodedFormat.h"
-#include "SkStream.h"
-
-/*
- * This class aims to provide an interface to test multiple implementations of
- * SkBitmapRegionDecoder.
- */
-class SkBitmapRegionDecoder {
-public:
-
- enum Strategy {
- kCanvas_Strategy, // Draw to the canvas, uses SkCodec
- kAndroidCodec_Strategy, // Uses SkAndroidCodec for scaling and subsetting
- };
-
- /*
- * @param data Refs the data while this object exists, unrefs on destruction
- * @param strategy Strategy used for scaling and subsetting
- * @return Tries to create an SkBitmapRegionDecoder, returns NULL on failure
- */
- static SkBitmapRegionDecoder* Create(
- SkData* data, Strategy strategy);
-
- /*
- * @param stream Takes ownership of the stream
- * @param strategy Strategy used for scaling and subsetting
- * @return Tries to create an SkBitmapRegionDecoder, returns NULL on failure
- */
- static SkBitmapRegionDecoder* Create(
- SkStreamRewindable* stream, Strategy strategy);
-
- /*
- * Decode a scaled region of the encoded image stream
- *
- * @param bitmap Container for decoded pixels. It is assumed that the pixels
- * are initially unallocated and will be allocated by this function.
- * @param allocator Allocator for the pixels. If this is NULL, the default
- * allocator (HeapAllocator) will be used.
- * @param desiredSubset Subset of the original image to decode.
- * @param sampleSize An integer downscaling factor for the decode.
- * @param colorType Preferred output colorType.
- * New implementations should return NULL if they do not support
- * decoding to this color type.
- * The old kOriginal_Strategy will decode to a default color type
- * if this color type is unsupported.
- * @param requireUnpremul If the image is not opaque, we will use this to determine the
- * alpha type to use.
- *
- */
- virtual bool decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocator,
- const SkIRect& desiredSubset, int sampleSize,
- SkColorType colorType, bool requireUnpremul) = 0;
- /*
- * @param Requested destination color type
- * @return true if we support the requested color type and false otherwise
- */
- virtual bool conversionSupported(SkColorType colorType) = 0;
-
- virtual SkEncodedFormat getEncodedFormat() = 0;
-
- int width() const { return fWidth; }
- int height() const { return fHeight; }
-
- virtual ~SkBitmapRegionDecoder() {}
-
-protected:
-
- SkBitmapRegionDecoder(int width, int height)
- : fWidth(width)
- , fHeight(height)
- {}
-
-private:
- const int fWidth;
- const int fHeight;
-};
-
-#endif
diff --git a/tools/android/SkBitmapRegionDecoderPriv.h b/tools/android/SkBitmapRegionDecoderPriv.h
deleted file mode 100644
index d7423b05fd..0000000000
--- a/tools/android/SkBitmapRegionDecoderPriv.h
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * Copyright 2015 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#ifndef SkBitmapRegionDecoderPriv_DEFINED
-#define SkBitmapRegionDecoderPriv_DEFINED
-
-inline float get_scale_from_sample_size(uint32_t sampleSize) {
- return 1.0f / (float) sampleSize;
-}
-
-enum SubsetType {
- kFullyInside_SubsetType,
- kPartiallyInside_SubsetType,
- kOutside_SubsetType,
-};
-
-/*
- * Corrects image subset offsets and dimensions in order to perform a valid decode.
- * Also indicates if the image subset should be placed at an offset within the
- * output bitmap.
- *
- * Values of output variables are undefined if the SubsetType is kInvalid.
- *
- * @param imageDims Original image dimensions.
- * @param subset As input, the subset that the client requested.
- * As output, the image subset that we will decode.
- * @param outX The left offset of the image subset within the output bitmap.
- * @param outY The top offset of the image subset within the output bitmap.
- *
- * @return An indication of how the subset is contained in the image.
- * If the return value is kInvalid, values of output variables are undefined.
- */
-inline SubsetType adjust_subset_rect(const SkISize& imageDims, SkIRect* subset, int* outX,
- int* outY) {
- // These must be at least zero, we can't start decoding the image at a negative coordinate.
- int left = SkTMax(0, subset->fLeft);
- int top = SkTMax(0, subset->fTop);
-
- // If input offsets are less than zero, we decode to an offset location in the output bitmap.
- *outX = left - subset->fLeft;
- *outY = top - subset->fTop;
-
- // Make sure we don't decode pixels past the edge of the image or past the edge of the subset.
- int width = SkTMin(imageDims.width() - left, subset->width() - *outX);
- int height = SkTMin(imageDims.height() - top, subset->height() - *outY);
- if (width <= 0 || height <= 0) {
- return SubsetType::kOutside_SubsetType;
- }
-
- subset->setXYWH(left, top, width, height);
- if ((*outX != 0) || (*outY != 0) || (width != subset->width()) ||
- (height != subset->height())) {
- return SubsetType::kPartiallyInside_SubsetType;
- }
-
- return SubsetType::kFullyInside_SubsetType;
-}
-
-#endif // SkBitmapRegionDecoderPriv_DEFINED