This is the code that I came up with, but it's slow and I think there's a better way to do it... I need to basically load a bitmap and return it rotated.
static bool LoadImage(const std::string& file, SkBitmap* bitmap)
{
sk_sp<SkData> data = SkData::MakeFromFileName(file.c_str());
if (!data) return false;
std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(std::move(data));
if (!codec) return false;
SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
if (!bitmap->tryAllocPixels(info)) return false;
if (codec->getPixels(info, bitmap->getPixels(), bitmap->rowBytes()) == SkCodec::kSuccess)
{
//checks if rotation is needed
if (codec->getOrigin() != kTopLeft_SkEncodedOrigin)
{
SkMatrix transform = SkEncodedOriginToMatrix(codec->getOrigin(), bitmap->width(), bitmap->height());
SkPath bounds; //this is a dummy path to hold the rotated image bounds
bounds.addRect(0.0f, 0.0f, static_cast<SkScalar>(bitmap->width()), static_cast<SkScalar>(bitmap->height()));
bounds.transform(transform);
SkBitmap rotated;
if (!rotated.tryAllocPixels(info.makeWH(static_cast<int>(bounds.getBounds().width()), static_cast<int>(bounds.getBounds().height()))))
return false;
SkCanvas canvas(rotated);
canvas.concat(transform);
canvas.drawBitmap(*bitmap, 0.0f, 0.0f);
canvas.flush();
*bitmap = rotated;
}
bitmap->setImmutable();
return true;
}
return false;
}