Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(638)

Unified Diff: gm/imagetoyuvplanes.cpp

Issue 1513393002: Add ability to extract YUV planes from SkImage (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: rename Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | gm/yuvtorgbeffect.cpp » ('j') | src/gpu/GrTextureToYUVPlanes.cpp » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: gm/imagetoyuvplanes.cpp
diff --git a/gm/imagetoyuvplanes.cpp b/gm/imagetoyuvplanes.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..f0cf788d159cff8a6e07dd3787c7b3a0668e3fab
--- /dev/null
+++ b/gm/imagetoyuvplanes.cpp
@@ -0,0 +1,98 @@
+/*
+ * 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 <SkSurface.h>
+#include "gm.h"
+#include "SkBitmap.h"
+#include "SkGradientShader.h"
+#include "SkImage.h"
+
+static SkImage* create_image(GrContext* context, int width, int height) {
+ SkAutoTUnref<SkSurface> surface;
+ SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
+ if (context) {
+ surface.reset(SkSurface::NewRenderTarget(context, SkSurface::kYes_Budgeted, info, 0));
+ } else {
+ surface.reset(SkSurface::NewRaster(info));
+ }
+ if (!surface) {
+ return nullptr;
+ }
+ // Create an RGB image from which we will extract planes
+ SkPaint paint;
+ static const SkColor kColors[] =
+ { SK_ColorBLUE, SK_ColorYELLOW, SK_ColorGREEN, SK_ColorWHITE };
+ SkScalar r = (width + height) / 4.f;
+ paint.setShader(SkGradientShader::CreateRadial(SkPoint::Make(0,0), r, kColors,
+ nullptr, SK_ARRAY_COUNT(kColors),
+ SkShader::kMirror_TileMode))->unref();
+
+ surface->getCanvas()->drawPaint(paint);
+ return surface->newImageSnapshot();
+}
+
+DEF_SIMPLE_GM(image_to_yuv_planes, canvas, 120, 525) {
+ static const SkScalar kPad = 5.f;
+ static const int kImageSize = 32;
+
+ GrContext *context = canvas->getGrContext();
+ SkAutoTUnref<SkImage> rgbImage(create_image(context, kImageSize, kImageSize));
+ if (!rgbImage) {
+ return;
+ }
+
+ canvas->drawImage(rgbImage, kPad, kPad);
robertphillips 2016/01/29 20:33:13 // Test out 444, 422 and 42(1.333) YUV formats ?
bsalomon 2016/02/01 20:27:45 I wasn't necessarily thinking about particular YUV
+ static const SkISize kSizes[][3] = {
+ {{kImageSize, kImageSize}, {kImageSize , kImageSize }, {kImageSize, kImageSize }},
+ {{kImageSize, kImageSize}, {kImageSize/2, kImageSize/2}, {kImageSize/2, kImageSize/2}},
+ {{kImageSize, kImageSize}, {kImageSize/2, kImageSize/2}, {kImageSize/3, kImageSize/3}}
+ };
+
+ SkScalar x = kPad;
+ for (size_t s = 0; s < SK_ARRAY_COUNT(kSizes); ++s) {
+ SkScalar y = rgbImage->height() + 2 * kPad;
+
+ const SkISize *sizes = kSizes[s];
+ SkAutoTDeleteArray<uint8_t> yPlane(new uint8_t[sizes[0].fWidth * sizes[0].fHeight]);
+ SkAutoTDeleteArray<uint8_t> uPlane(new uint8_t[sizes[1].fWidth * sizes[1].fHeight]);
+ SkAutoTDeleteArray<uint8_t> vPlane(new uint8_t[sizes[2].fWidth * sizes[2].fHeight]);
+
+ void *planes[3] = {yPlane.get(), uPlane.get(), vPlane.get()};
robertphillips 2016/01/29 20:33:13 Should we exercise the rowBytes ?
bsalomon 2016/02/01 20:27:45 Done.
+ size_t rowBytes[3] = {0, 0, 0};
+
+ // Convert the RGB image to YUV planes using each YUV color space and draw the YUV planes
+ // to the canvas.
+ SkBitmap yuvBmps[3];
+ yuvBmps[0].setInfo(SkImageInfo::MakeA8(sizes[0].fWidth, sizes[0].fHeight));
+ yuvBmps[1].setInfo(SkImageInfo::MakeA8(sizes[1].fWidth, sizes[1].fHeight));
+ yuvBmps[2].setInfo(SkImageInfo::MakeA8(sizes[2].fWidth, sizes[2].fHeight));
+ yuvBmps[0].setPixels(yPlane.get());
+ yuvBmps[1].setPixels(uPlane.get());
+ yuvBmps[2].setPixels(vPlane.get());
+
+ for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
+ // Clear the planes so we don't accidentally see the old values if there is a bug in
+ // readYUV8Planes().
+ memset(yPlane.get(), 0, sizes[0].fWidth * sizes[0].fHeight);
+ memset(uPlane.get(), 0, sizes[1].fWidth * sizes[1].fHeight);
+ memset(vPlane.get(), 0, sizes[2].fWidth * sizes[2].fHeight);
+ if (rgbImage->readYUV8Planes(sizes, planes, rowBytes,
+ static_cast<SkYUVColorSpace>(space))) {
+ yuvBmps[0].notifyPixelsChanged();
+ yuvBmps[1].notifyPixelsChanged();
+ yuvBmps[2].notifyPixelsChanged();
+ for (int i = 0; i < 3; ++i) {
+ canvas->drawBitmap(yuvBmps[i], x, y);
+ y += kPad + yuvBmps[i].height();
+ }
+ }
+ }
+
+ x += rgbImage->width() + kPad;
+ }
+}
« no previous file with comments | « no previous file | gm/yuvtorgbeffect.cpp » ('j') | src/gpu/GrTextureToYUVPlanes.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698