bitmapimage.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #include "gm/gm.h"
  8. #include "include/codec/SkCodec.h"
  9. #include "include/core/SkBitmap.h"
  10. #include "include/core/SkCanvas.h"
  11. #include "include/core/SkImage.h"
  12. #include "include/core/SkImageInfo.h"
  13. #include "include/core/SkRefCnt.h"
  14. #include "include/core/SkScalar.h"
  15. #include "include/core/SkSize.h"
  16. #include "include/core/SkStream.h"
  17. #include "include/core/SkString.h"
  18. #include "tools/Resources.h"
  19. #include <memory>
  20. namespace skiagm {
  21. class BitmapImageGM : public GM {
  22. public:
  23. BitmapImageGM() {}
  24. protected:
  25. SkString onShortName() override {
  26. return SkString("bitmap-image-srgb-legacy");
  27. }
  28. SkISize onISize() override {
  29. return SkISize::Make(2*kSize, 2*kSize);
  30. }
  31. DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
  32. // Create image.
  33. const char* path = "images/mandrill_512_q075.jpg";
  34. sk_sp<SkImage> image = GetResourceAsImage(path);
  35. if (!image) {
  36. *errorMsg = "Couldn't load images/mandrill_512_q075.jpg. "
  37. "Did you forget to set the resource path?";
  38. return DrawResult::kFail;
  39. }
  40. // Create matching bitmap.
  41. std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(GetResourceAsStream(path)));
  42. SkBitmap bitmap;
  43. bitmap.allocPixels(codec->getInfo());
  44. codec->getPixels(codec->getInfo(), bitmap.getPixels(), bitmap.rowBytes());
  45. // The GM will be displayed in a 2x2 grid.
  46. // The top two squares show an sRGB image, then bitmap, drawn to a legacy canvas.
  47. SkImageInfo linearInfo = SkImageInfo::MakeN32(2*kSize, kSize, kOpaque_SkAlphaType);
  48. SkBitmap legacyBMCanvas;
  49. legacyBMCanvas.allocPixels(linearInfo);
  50. SkCanvas legacyCanvas(legacyBMCanvas);
  51. legacyCanvas.drawImage(image, 0.0f, 0.0f, nullptr);
  52. legacyCanvas.translate(SkScalar(kSize), 0.0f);
  53. legacyCanvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr);
  54. canvas->drawBitmap(legacyBMCanvas, 0.0f, 0.0f, nullptr);
  55. canvas->translate(0.0f, SkScalar(kSize));
  56. // The bottom two squares show an sRGB image, then bitmap, drawn to a srgb canvas.
  57. SkImageInfo srgbInfo = SkImageInfo::MakeS32(2*kSize, kSize, kOpaque_SkAlphaType);
  58. SkBitmap srgbBMCanvas;
  59. srgbBMCanvas.allocPixels(srgbInfo);
  60. SkCanvas srgbCanvas(srgbBMCanvas);
  61. srgbCanvas.drawImage(image, 0.0f, 0.0f, nullptr);
  62. srgbCanvas.translate(SkScalar(kSize), 0.0f);
  63. srgbCanvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr);
  64. canvas->drawBitmap(srgbBMCanvas, 0.0f, 0.0f, nullptr);
  65. return DrawResult::kOk;
  66. }
  67. private:
  68. static constexpr int kSize = 512;
  69. typedef GM INHERITED;
  70. };
  71. //////////////////////////////////////////////////////////////////////////////
  72. DEF_GM( return new BitmapImageGM; )
  73. }