Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2016 Google Inc. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license that can be | |
| 5 * found in the LICENSE file. | |
| 6 */ | |
| 7 | |
| 8 | |
| 9 #include <SkSurface.h> | |
| 10 #include "gm.h" | |
| 11 #include "SkBitmap.h" | |
| 12 #include "SkGradientShader.h" | |
| 13 #include "SkImage.h" | |
| 14 | |
| 15 static SkImage* create_image(GrContext* context, int width, int height) { | |
| 16 SkAutoTUnref<SkSurface> surface; | |
| 17 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); | |
| 18 if (context) { | |
| 19 surface.reset(SkSurface::NewRenderTarget(context, SkSurface::kYes_Budge ted, info, 0)); | |
| 20 } else { | |
| 21 surface.reset(SkSurface::NewRaster(info)); | |
| 22 } | |
| 23 if (!surface) { | |
| 24 return nullptr; | |
| 25 } | |
| 26 // Create an RGB image from which we will extract planes | |
| 27 SkPaint paint; | |
| 28 static const SkColor kColors[] = | |
| 29 { SK_ColorBLUE, SK_ColorYELLOW, SK_ColorGREEN, SK_ColorWHITE }; | |
| 30 SkScalar r = (width + height) / 4.f; | |
| 31 paint.setShader(SkGradientShader::CreateRadial(SkPoint::Make(0,0), r, kColor s, | |
| 32 nullptr, SK_ARRAY_COUNT(kColo rs), | |
| 33 SkShader::kMirror_TileMode))- >unref(); | |
| 34 | |
| 35 surface->getCanvas()->drawPaint(paint); | |
| 36 return surface->newImageSnapshot(); | |
| 37 } | |
| 38 | |
| 39 DEF_SIMPLE_GM(image_to_yuv_planes, canvas, 120, 525) { | |
| 40 static const SkScalar kPad = 5.f; | |
| 41 static const int kImageSize = 32; | |
| 42 | |
| 43 GrContext *context = canvas->getGrContext(); | |
| 44 SkAutoTUnref<SkImage> rgbImage(create_image(context, kImageSize, kImageSize) ); | |
| 45 if (!rgbImage) { | |
| 46 return; | |
| 47 } | |
| 48 | |
| 49 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
| |
| 50 static const SkISize kSizes[][3] = { | |
| 51 {{kImageSize, kImageSize}, {kImageSize , kImageSize }, {kImageSize, kImageSize }}, | |
| 52 {{kImageSize, kImageSize}, {kImageSize/2, kImageSize/2}, {kImageSize/2, kImageSize/2}}, | |
| 53 {{kImageSize, kImageSize}, {kImageSize/2, kImageSize/2}, {kImageSize/3, kImageSize/3}} | |
| 54 }; | |
| 55 | |
| 56 SkScalar x = kPad; | |
| 57 for (size_t s = 0; s < SK_ARRAY_COUNT(kSizes); ++s) { | |
| 58 SkScalar y = rgbImage->height() + 2 * kPad; | |
| 59 | |
| 60 const SkISize *sizes = kSizes[s]; | |
| 61 SkAutoTDeleteArray<uint8_t> yPlane(new uint8_t[sizes[0].fWidth * sizes[0 ].fHeight]); | |
| 62 SkAutoTDeleteArray<uint8_t> uPlane(new uint8_t[sizes[1].fWidth * sizes[1 ].fHeight]); | |
| 63 SkAutoTDeleteArray<uint8_t> vPlane(new uint8_t[sizes[2].fWidth * sizes[2 ].fHeight]); | |
| 64 | |
| 65 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.
| |
| 66 size_t rowBytes[3] = {0, 0, 0}; | |
| 67 | |
| 68 // Convert the RGB image to YUV planes using each YUV color space and dr aw the YUV planes | |
| 69 // to the canvas. | |
| 70 SkBitmap yuvBmps[3]; | |
| 71 yuvBmps[0].setInfo(SkImageInfo::MakeA8(sizes[0].fWidth, sizes[0].fHeight )); | |
| 72 yuvBmps[1].setInfo(SkImageInfo::MakeA8(sizes[1].fWidth, sizes[1].fHeight )); | |
| 73 yuvBmps[2].setInfo(SkImageInfo::MakeA8(sizes[2].fWidth, sizes[2].fHeight )); | |
| 74 yuvBmps[0].setPixels(yPlane.get()); | |
| 75 yuvBmps[1].setPixels(uPlane.get()); | |
| 76 yuvBmps[2].setPixels(vPlane.get()); | |
| 77 | |
| 78 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpa ce; ++space) { | |
| 79 // Clear the planes so we don't accidentally see the old values if t here is a bug in | |
| 80 // readYUV8Planes(). | |
| 81 memset(yPlane.get(), 0, sizes[0].fWidth * sizes[0].fHeight); | |
| 82 memset(uPlane.get(), 0, sizes[1].fWidth * sizes[1].fHeight); | |
| 83 memset(vPlane.get(), 0, sizes[2].fWidth * sizes[2].fHeight); | |
| 84 if (rgbImage->readYUV8Planes(sizes, planes, rowBytes, | |
| 85 static_cast<SkYUVColorSpace>(space))) { | |
| 86 yuvBmps[0].notifyPixelsChanged(); | |
| 87 yuvBmps[1].notifyPixelsChanged(); | |
| 88 yuvBmps[2].notifyPixelsChanged(); | |
| 89 for (int i = 0; i < 3; ++i) { | |
| 90 canvas->drawBitmap(yuvBmps[i], x, y); | |
| 91 y += kPad + yuvBmps[i].height(); | |
| 92 } | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 x += rgbImage->width() + kPad; | |
| 97 } | |
| 98 } | |
| OLD | NEW |