bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 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 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 15 | static sk_sp<SkImage> create_image(GrContext* context, int width, int height) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 16 | sk_sp<SkSurface> surface; |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 17 | SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
| 18 | if (context) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 19 | surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info); |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 20 | } else { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 21 | surface = SkSurface::MakeRaster(info); |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 22 | } |
| 23 | if (!surface) { |
| 24 | return nullptr; |
| 25 | } |
| 26 | // Create an RGB image from which we will extract planes |
| 27 | SkPaint paint; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 28 | constexpr SkColor kColors[] = |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 29 | { SK_ColorBLUE, SK_ColorYELLOW, SK_ColorGREEN, SK_ColorWHITE }; |
| 30 | SkScalar r = (width + height) / 4.f; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 31 | paint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(0,0), r, kColors, |
| 32 | nullptr, SK_ARRAY_COUNT(kColors), |
| 33 | SkShader::kMirror_TileMode)); |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 34 | |
| 35 | surface->getCanvas()->drawPaint(paint); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 36 | return surface->makeImageSnapshot(); |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | DEF_SIMPLE_GM(image_to_yuv_planes, canvas, 120, 525) { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 40 | constexpr SkScalar kPad = 5.f; |
| 41 | constexpr int kImageSize = 32; |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 42 | |
| 43 | GrContext *context = canvas->getGrContext(); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 44 | sk_sp<SkImage> rgbImage(create_image(context, kImageSize, kImageSize)); |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 45 | if (!rgbImage) { |
| 46 | return; |
| 47 | } |
| 48 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 49 | canvas->drawImage(rgbImage.get(), kPad, kPad); |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 50 | // Test cases where all three planes are the same size, where just u and v are the same size, |
| 51 | // and where all differ. |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 52 | constexpr SkISize kSizes[][3] = { |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 53 | {{kImageSize, kImageSize}, {kImageSize , kImageSize }, {kImageSize, kImageSize }}, |
| 54 | {{kImageSize, kImageSize}, {kImageSize/2, kImageSize/2}, {kImageSize/2, kImageSize/2}}, |
| 55 | {{kImageSize, kImageSize}, {kImageSize/2, kImageSize/2}, {kImageSize/3, kImageSize/3}} |
| 56 | }; |
| 57 | |
| 58 | // A mix of rowbytes triples to go with the above sizes. |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 59 | constexpr size_t kRowBytes[][3] { |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 60 | {0, 0, 0}, |
| 61 | {kImageSize, kImageSize/2 + 1, kImageSize}, |
| 62 | {kImageSize + 13, kImageSize, kImageSize/3 + 8} |
| 63 | }; |
| 64 | |
| 65 | |
| 66 | SkScalar x = kPad; |
| 67 | for (size_t s = 0; s < SK_ARRAY_COUNT(kSizes); ++s) { |
| 68 | SkScalar y = rgbImage->height() + 2 * kPad; |
| 69 | |
| 70 | const SkISize *sizes = kSizes[s]; |
| 71 | size_t realRowBytes[3]; |
| 72 | for (int i = 0; i < 3; ++i) { |
| 73 | realRowBytes[i] = kRowBytes[s][i] ? kRowBytes[s][i] : kSizes[s][i].fWidth; |
| 74 | } |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 75 | std::unique_ptr<uint8_t[]> yPlane(new uint8_t[realRowBytes[0] * sizes[0].fHeight]); |
| 76 | std::unique_ptr<uint8_t[]> uPlane(new uint8_t[realRowBytes[1] * sizes[1].fHeight]); |
| 77 | std::unique_ptr<uint8_t[]> vPlane(new uint8_t[realRowBytes[2] * sizes[2].fHeight]); |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 78 | |
| 79 | void *planes[3] = {yPlane.get(), uPlane.get(), vPlane.get()}; |
| 80 | |
| 81 | // Convert the RGB image to YUV planes using each YUV color space and draw the YUV planes |
| 82 | // to the canvas. |
| 83 | SkBitmap yuvBmps[3]; |
| 84 | yuvBmps[0].setInfo(SkImageInfo::MakeA8(sizes[0].fWidth, sizes[0].fHeight), kRowBytes[s][0]); |
| 85 | yuvBmps[1].setInfo(SkImageInfo::MakeA8(sizes[1].fWidth, sizes[1].fHeight), kRowBytes[s][1]); |
| 86 | yuvBmps[2].setInfo(SkImageInfo::MakeA8(sizes[2].fWidth, sizes[2].fHeight), kRowBytes[s][2]); |
| 87 | yuvBmps[0].setPixels(yPlane.get()); |
| 88 | yuvBmps[1].setPixels(uPlane.get()); |
| 89 | yuvBmps[2].setPixels(vPlane.get()); |
| 90 | |
| 91 | for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) { |
| 92 | // Clear the planes so we don't accidentally see the old values if there is a bug in |
| 93 | // readYUV8Planes(). |
| 94 | memset(yPlane.get(), 0, realRowBytes[0] * sizes[0].fHeight); |
| 95 | memset(uPlane.get(), 0, realRowBytes[1] * sizes[1].fHeight); |
| 96 | memset(vPlane.get(), 0, realRowBytes[2] * sizes[2].fHeight); |
| 97 | if (rgbImage->readYUV8Planes(sizes, planes, kRowBytes[s], |
| 98 | static_cast<SkYUVColorSpace>(space))) { |
| 99 | yuvBmps[0].notifyPixelsChanged(); |
| 100 | yuvBmps[1].notifyPixelsChanged(); |
| 101 | yuvBmps[2].notifyPixelsChanged(); |
| 102 | for (int i = 0; i < 3; ++i) { |
| 103 | canvas->drawBitmap(yuvBmps[i], x, y); |
| 104 | y += kPad + yuvBmps[i].height(); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | x += rgbImage->width() + kPad; |
| 110 | } |
| 111 | } |