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