skia2/gm/codec_scaled.cpp
Leon Scroggins III 9be9a42c92 Do not support index8 for generateScaledPixels
Since the in/out parameter is a const SkPixmap without the proper
color table, there is no way to tell the client about it without
modifying the const SkPixmap. Rather than cheating, just return
false.

Change-Id: I63fdf57febc59e1ee9af13aa6eb9b253d19bcb17
Reviewed-on: https://skia-review.googlesource.com/6414
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Leon Scroggins <scroggo@google.com>
2016-12-22 15:30:50 +00:00

113 lines
3.4 KiB
C++

/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm.h"
#include "SkCanvas.h"
#include "SkCodec.h"
#include "SkCodecImageGenerator.h"
#include "SkColor.h"
#include "SkCommandLineFlags.h"
#include "SkImageGenerator.h"
#include "SkString.h"
#include "Resources.h"
DEFINE_string(codec_scaled, "brickwork-texture.jpg", "Image in resources/ to draw scaled.");
class CodecScaledGM : public skiagm::GM {
private:
// FIXME: Once generateScaledPixels is plumbed to SkImage, store an SkImage
// and call SkImage::scalePixels.
std::unique_ptr<SkImageGenerator> fGenerator;
public:
CodecScaledGM()
{}
private:
SkString onShortName() override {
return SkString("codec_scaled");
}
SkISize onISize() override {
if (this->initCodec()) {
SkISize dim = fGenerator->getInfo().dimensions();
// Wide enough to show 8 versions, corresponding to the options JPEG supports.
dim.fWidth *= 8;
// Tall enough to display 2 versions - one using computed dimensions, and one
// with scaling.
dim.fHeight *= 2;
return dim;
}
return SkISize::Make(640, 480);
}
void onDrawBackground(SkCanvas* canvas) override {
canvas->clear(SK_ColorWHITE);
}
bool initCodec() {
if (fGenerator) {
return true;
}
if (FLAGS_codec_scaled.isEmpty()) {
SkDebugf("Nothing specified for --codec_scaled!");
return false;
}
SkString path = GetResourcePath(FLAGS_codec_scaled[0]);
sk_sp<SkData> data(SkData::MakeFromFileName(path.c_str()));
if (!data) {
return false;
}
fGenerator.reset(SkCodecImageGenerator::NewFromEncodedCodec(data));
if (!fGenerator) {
SkDebugf("Could create codec from %s", FLAGS_codec_scaled[0]);
return false;
}
return true;
}
void onDraw(SkCanvas* canvas) override {
if (!this->initCodec()) {
return;
}
SkAutoCanvasRestore acr(canvas, true);
for (float scale : { 1.0f, .875f, .750f, .625f, .5f, .375f, .25f, .125f }) {
// generateScaledPixels does not support index8
const auto info = fGenerator->getInfo().makeColorType(kN32_SkColorType);
auto scaledInfo = info;
SkImageGenerator::SupportedSizes sizes;
if (fGenerator->computeScaledDimensions(scale, &sizes)) {
scaledInfo = info.makeWH(sizes.fSizes[0].fWidth, sizes.fSizes[0].fHeight);
}
SkBitmap bm;
bm.allocPixels(scaledInfo);
SkPixmap pixmap(scaledInfo, bm.getPixels(), bm.rowBytes());
if (fGenerator->generateScaledPixels(pixmap)) {
canvas->drawBitmap(bm, 0, 0);
}
bm.allocPixels(info);
if (fGenerator->getPixels(info, bm.getPixels(), bm.rowBytes())) {
SkAutoCanvasRestore acr2(canvas, true);
canvas->translate(0, SkIntToScalar(info.height()));
canvas->scale(SkFloatToScalar(scale), SkFloatToScalar(scale));
canvas->drawBitmap(bm, 0, 0);
}
canvas->translate(SkIntToScalar(info.width()), 0);
}
}
};
DEF_GM(return new CodecScaledGM);