skia2/docs/examples/SkImage_to_PPM_binary.cpp
Hal Canary 6c8422c671 add more docs/examples from named fiddles.
ignore offscreen, srgb, and animated fiddles for now.

Change-Id: I923131b684865698e6cda138b004930e11f504d5
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/263713
Commit-Queue: Hal Canary <halcanary@google.com>
Reviewed-by: Ben Wagner <bungeman@google.com>
2020-01-14 19:22:26 +00:00

53 lines
1.7 KiB
C++

// Copyright 2020 Google LLC.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
#include "tools/fiddle/examples.h"
REG_FIDDLE(SkImage_to_PPM_binary, 256, 256, true, 4) {
void print_data(const SkData* data, const char* name) {
if (data) {
SkDebugf("\nxxd -r -p > %s << EOF", name);
size_t s = data->size();
const uint8_t* d = data->bytes();
for (size_t i = 0; i < s; ++i) {
if (i % 40 == 0) {
SkDebugf("\n");
}
SkDebugf("%02x", d[i]);
}
SkDebugf("\nEOF\n\n");
}
}
sk_sp<SkData> Encode_PPM_B(const SkPixmap& src) {
if (src.width() <= 0 || src.height() <= 0 || !src.addr() ||
src.colorType() == kUnknown_SkColorType) {
return nullptr;
}
SkString s = SkStringPrintf("P6\n%d %d\n255\n", src.width(), src.height());
auto result = SkData::MakeUninitialized(s.size() + 3 * src.width() * src.height());
uint8_t* ptr = static_cast<uint8_t*>(result->writable_data());
memcpy(ptr, s.c_str(), s.size());
ptr += s.size();
for (int y = 0; y < src.height(); ++y) {
for (int x = 0; x < src.height(); ++x) {
SkColor c = src.getColor(x, y);
*ptr++ = SkColorGetR(c);
*ptr++ = SkColorGetG(c);
*ptr++ = SkColorGetB(c);
}
}
return result;
}
SkBitmap ToBitmap(SkImage * img) {
SkBitmap bitmap;
(void)img->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
return bitmap;
}
void draw(SkCanvas*) {
SkBitmap bitmap = ToBitmap(image.get());
sk_sp<SkData> data = Encode_PPM_B(bitmap.pixmap());
print_data(data.get(), "foo.ppm");
}
} // END FIDDLE