2015-09-08 22:35:32 +00:00
|
|
|
/*
|
|
|
|
* 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 "SkCanvas.h"
|
2015-10-12 17:24:38 +00:00
|
|
|
#include "SkCodecPriv.h"
|
|
|
|
#include "SkCodecTools.h"
|
2015-09-08 22:35:32 +00:00
|
|
|
|
Merge SkCodec with SkScanlineDecoder
Benefits:
- This mimics other decoding APIs (including the ones SkCodec relies
on, e.g. a png_struct, which can be used to decode an entire image or
one line at a time).
- It allows a client to ask us to do what we can do efficiently - i.e.
start from encoded data and either decode the whole thing or scanlines.
- It removes the duplicate methods which appeared in both SkCodec and
SkScanlineDecoder (some of which, e.g. in SkJpegScanlineDecoder, just
call fCodec->sameMethod()).
- It simplifies moving more checks into the base class (e.g. the
examples in skbug.com/4284).
BUG=skia:4175
BUG=skia:4284
=====================================================================
SkScanlineDecoder.h/.cpp:
Removed.
SkCodec.h/.cpp:
Add methods, enums, and variables which were previously in
SkScanlineDecoder.
Default fCurrScanline to -1, as a sentinel that start has not been
called.
General changes:
Convert SkScanlineDecoders to SkCodecs.
General changes in SkCodec subclasses:
Merge SkScanlineDecoder implementation into SkCodec. Most (all?) owned
an SkCodec, so they now call this-> instead of fCodec->.
SkBmpCodec.h/.cpp:
Replace the unused rowOrder method with an override for
onGetScanlineOrder.
Make getDstRow const, since it is called by onGetY, which is const.
SkCodec_libpng.h/.cpp:
Make SkPngCodec an abstract class, with two subclasses which handle
scanline decoding separately (they share code for decoding the entire
image). Reimplement onReallyHasAlpha so that it can return the most
recent result (e.g. after a scanline decode which only decoded part
of the image) or a better answer (e.g. if the whole image is known to
be opaque).
Compute fNumberPasses early, so we know which subclass to instantiate.
Make SkPngInterlaceScanlineDecoder use the base class' fCurrScanline
rather than a separate variable.
CodexTest.cpp:
Add tests for the state changes in SkCodec (need to call start before
decoding scanlines; calling getPixels means that start will need to
be called again before decoding more scanlines).
Add a test which decodes in stripes, currently only used for an
interlaced PNG.
TODO: Add tests for onReallyHasAlpha.
Review URL: https://codereview.chromium.org/1365313002
2015-09-30 15:57:13 +00:00
|
|
|
SkBitmapRegionCanvas::SkBitmapRegionCanvas(SkCodec* decoder)
|
2015-09-08 22:35:32 +00:00
|
|
|
: INHERITED(decoder->getInfo().width(), decoder->getInfo().height())
|
|
|
|
, fDecoder(decoder)
|
|
|
|
{}
|
|
|
|
|
2015-10-27 19:50:25 +00:00
|
|
|
bool SkBitmapRegionCanvas::decodeRegion(SkBitmap* bitmap, SkBitmap::Allocator* allocator,
|
|
|
|
const SkIRect& desiredSubset, int sampleSize, SkColorType dstColorType,
|
|
|
|
bool requireUnpremul) {
|
2015-09-08 22:35:32 +00:00
|
|
|
// Reject color types not supported by this method
|
|
|
|
if (kIndex_8_SkColorType == dstColorType || kGray_8_SkColorType == dstColorType) {
|
2015-10-12 17:24:38 +00:00
|
|
|
SkCodecPrintf("Error: Color type not supported.\n");
|
2015-10-27 19:50:25 +00:00
|
|
|
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;
|
2015-09-08 22:35:32 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 19:50:25 +00:00
|
|
|
// FIXME: Can we add checks and support kIndex8 or unpremultiplied alpha in special cases?
|
|
|
|
|
2015-10-22 14:29:19 +00:00
|
|
|
// Fix the input sampleSize if necessary.
|
|
|
|
if (sampleSize < 1) {
|
|
|
|
sampleSize = 1;
|
|
|
|
}
|
2015-09-08 22:35:32 +00:00
|
|
|
|
|
|
|
// The size of the output bitmap is determined by the size of the
|
2015-10-22 14:29:19 +00:00
|
|
|
// 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.
|
2015-09-08 22:35:32 +00:00
|
|
|
int outX;
|
|
|
|
int outY;
|
2015-10-27 19:50:25 +00:00
|
|
|
SkIRect subset = desiredSubset;
|
2015-10-22 14:29:19 +00:00
|
|
|
SubsetType type = adjust_subset_rect(fDecoder->getInfo().dimensions(), &subset, &outX, &outY);
|
|
|
|
if (SubsetType::kOutside_SubsetType == type) {
|
2015-10-27 19:50:25 +00:00
|
|
|
return false;
|
2015-09-08 22:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the image info for the decode
|
|
|
|
SkImageInfo decodeInfo = SkImageInfo::Make(this->width(), this->height(),
|
|
|
|
dstColorType, dstAlphaType);
|
2015-10-22 14:29:19 +00:00
|
|
|
|
2015-09-08 22:35:32 +00:00
|
|
|
// Start the scanline decoder
|
Merge SkCodec with SkScanlineDecoder
Benefits:
- This mimics other decoding APIs (including the ones SkCodec relies
on, e.g. a png_struct, which can be used to decode an entire image or
one line at a time).
- It allows a client to ask us to do what we can do efficiently - i.e.
start from encoded data and either decode the whole thing or scanlines.
- It removes the duplicate methods which appeared in both SkCodec and
SkScanlineDecoder (some of which, e.g. in SkJpegScanlineDecoder, just
call fCodec->sameMethod()).
- It simplifies moving more checks into the base class (e.g. the
examples in skbug.com/4284).
BUG=skia:4175
BUG=skia:4284
=====================================================================
SkScanlineDecoder.h/.cpp:
Removed.
SkCodec.h/.cpp:
Add methods, enums, and variables which were previously in
SkScanlineDecoder.
Default fCurrScanline to -1, as a sentinel that start has not been
called.
General changes:
Convert SkScanlineDecoders to SkCodecs.
General changes in SkCodec subclasses:
Merge SkScanlineDecoder implementation into SkCodec. Most (all?) owned
an SkCodec, so they now call this-> instead of fCodec->.
SkBmpCodec.h/.cpp:
Replace the unused rowOrder method with an override for
onGetScanlineOrder.
Make getDstRow const, since it is called by onGetY, which is const.
SkCodec_libpng.h/.cpp:
Make SkPngCodec an abstract class, with two subclasses which handle
scanline decoding separately (they share code for decoding the entire
image). Reimplement onReallyHasAlpha so that it can return the most
recent result (e.g. after a scanline decode which only decoded part
of the image) or a better answer (e.g. if the whole image is known to
be opaque).
Compute fNumberPasses early, so we know which subclass to instantiate.
Make SkPngInterlaceScanlineDecoder use the base class' fCurrScanline
rather than a separate variable.
CodexTest.cpp:
Add tests for the state changes in SkCodec (need to call start before
decoding scanlines; calling getPixels means that start will need to
be called again before decoding more scanlines).
Add a test which decodes in stripes, currently only used for an
interlaced PNG.
TODO: Add tests for onReallyHasAlpha.
Review URL: https://codereview.chromium.org/1365313002
2015-09-30 15:57:13 +00:00
|
|
|
SkCodec::Result r = fDecoder->startScanlineDecode(decodeInfo);
|
2015-09-08 22:35:32 +00:00
|
|
|
if (SkCodec::kSuccess != r) {
|
2015-10-12 17:24:38 +00:00
|
|
|
SkCodecPrintf("Error: Could not start scanline decoder.\n");
|
2015-10-27 19:50:25 +00:00
|
|
|
return false;
|
2015-09-08 22:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate a bitmap for the unscaled decode
|
|
|
|
SkBitmap tmp;
|
2015-10-22 14:29:19 +00:00
|
|
|
SkImageInfo tmpInfo = decodeInfo.makeWH(this->width(), subset.height());
|
2015-09-08 22:35:32 +00:00
|
|
|
if (!tmp.tryAllocPixels(tmpInfo)) {
|
2015-10-12 17:24:38 +00:00
|
|
|
SkCodecPrintf("Error: Could not allocate pixels.\n");
|
2015-10-27 19:50:25 +00:00
|
|
|
return false;
|
2015-09-08 22:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Skip the unneeded rows
|
2015-10-22 14:29:19 +00:00
|
|
|
if (!fDecoder->skipScanlines(subset.y())) {
|
2015-10-12 17:24:38 +00:00
|
|
|
SkCodecPrintf("Error: Failed to skip scanlines.\n");
|
2015-10-27 19:50:25 +00:00
|
|
|
return false;
|
2015-09-08 22:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decode the necessary rows
|
2015-10-22 14:29:19 +00:00
|
|
|
fDecoder->getScanlines(tmp.getAddr(0, 0), subset.height(), tmp.rowBytes());
|
2015-09-08 22:35:32 +00:00
|
|
|
|
|
|
|
// Calculate the size of the output
|
2015-10-27 19:50:25 +00:00
|
|
|
const int outWidth = get_scaled_dimension(desiredSubset.width(), sampleSize);
|
|
|
|
const int outHeight = get_scaled_dimension(desiredSubset.height(), sampleSize);
|
2015-09-08 22:35:32 +00:00
|
|
|
|
|
|
|
// Initialize the destination bitmap
|
|
|
|
SkImageInfo dstInfo = decodeInfo.makeWH(outWidth, outHeight);
|
2015-10-27 19:50:25 +00:00
|
|
|
bitmap->setInfo(dstInfo, dstInfo.minRowBytes());
|
|
|
|
if (!bitmap->tryAllocPixels(allocator, nullptr)) {
|
2015-10-12 17:24:38 +00:00
|
|
|
SkCodecPrintf("Error: Could not allocate pixels.\n");
|
2015-10-27 19:50:25 +00:00
|
|
|
return false;
|
2015-09-08 22:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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?
|
|
|
|
// TODO (msarett): This could be skipped if memory is zero initialized.
|
|
|
|
// This would matter if this code is moved to Android and
|
|
|
|
// uses Android bitmaps.
|
2015-10-22 14:29:19 +00:00
|
|
|
if (SubsetType::kPartiallyInside_SubsetType == type) {
|
2015-09-08 22:35:32 +00:00
|
|
|
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?
|
2015-10-22 14:29:19 +00:00
|
|
|
SkRect src = SkRect::MakeXYWH((SkScalar) subset.x(), (SkScalar) 0,
|
|
|
|
(SkScalar) subset.width(), (SkScalar) subset.height());
|
2015-09-08 22:35:32 +00:00
|
|
|
SkRect dst = SkRect::MakeXYWH((SkScalar) (outX / sampleSize), (SkScalar) (outY / sampleSize),
|
2015-10-22 14:29:19 +00:00
|
|
|
(SkScalar) get_scaled_dimension(subset.width(), sampleSize),
|
|
|
|
(SkScalar) get_scaled_dimension(subset.height(), sampleSize));
|
2015-09-08 22:35:32 +00:00
|
|
|
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);
|
|
|
|
|
2015-10-27 19:50:25 +00:00
|
|
|
return true;
|
2015-09-08 22:35:32 +00:00
|
|
|
}
|
2015-10-12 17:24:38 +00:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|