• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 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 #include "gm.h"
8 #include "SkCanvas.h"
9 #include "SkColorPriv.h"
10 #include "SkData.h"
11 #include "SkImage.h"
12 #include "SkImageEncoder.h"
13 
14 namespace skiagm {
15 
16 class ColorCubeGM : public GM {
17 public:
ColorCubeGM()18     ColorCubeGM() {}
19 
20 protected:
onShortName()21     SkString onShortName() override {
22         return SkString("jpg-color-cube");
23     }
24 
onISize()25     SkISize onISize() override {
26         return SkISize::Make(512, 512);
27     }
28 
onOnceBeforeDraw()29     void onOnceBeforeDraw() override {
30         SkBitmap bmp;
31         bmp.allocN32Pixels(512, 512, true);
32         int bX = 0, bY = 0;
33         for (int b = 0; b < 64; ++b) {
34             for (int r = 0; r < 64; ++r) {
35                 for (int g = 0; g < 64; ++g) {
36                     *bmp.getAddr32(bX + r, bY + g) = SkPackARGB32(255,
37                                                                   SkTPin(r * 4, 0, 255),
38                                                                   SkTPin(g * 4, 0, 255),
39                                                                   SkTPin(b * 4, 0, 255));
40                 }
41             }
42             bX += 64;
43             if (bX >= 512) {
44                 bX = 0;
45                 bY += 64;
46             }
47         }
48         auto jpegData = SkEncodeBitmap(bmp, SkEncodedImageFormat::kJPEG, 100);
49         fImage = SkImage::MakeFromEncoded(std::move(jpegData));
50     }
51 
onDraw(SkCanvas * canvas)52     void onDraw(SkCanvas* canvas) override {
53         canvas->drawImage(fImage, 0, 0);
54     }
55 
56 private:
57     sk_sp<SkImage> fImage;
58 
59     typedef GM INHERITED;
60 };
61 
62 DEF_GM( return new ColorCubeGM; )
63 }
64