SampleFilter2.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright 2011 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 "include/core/SkCanvas.h"
  8. #include "include/core/SkFont.h"
  9. #include "include/core/SkString.h"
  10. #include "include/utils/SkTextUtils.h"
  11. #include "samplecode/DecodeFile.h"
  12. #include "samplecode/Sample.h"
  13. #include "tools/Resources.h"
  14. #include <vector>
  15. static const char* gNames[] = {
  16. "images/mandrill_512_q075.jpg",
  17. "images/dog.jpg",
  18. };
  19. struct Filter2View : public Sample {
  20. std::vector<SkBitmap> fBitmaps;
  21. void onOnceBeforeDraw() override {
  22. SkASSERT(fBitmaps.empty());
  23. fBitmaps.reserve(SK_ARRAY_COUNT(gNames) * 2);
  24. for (const char* name : gNames) {
  25. SkBitmap bitmap;
  26. (void)decode_file(GetResourceAsData(name), &bitmap);
  27. fBitmaps.push_back(std::move(bitmap));
  28. }
  29. for (const char* name : gNames) {
  30. SkBitmap bitmap;
  31. (void)decode_file(GetResourceAsData(name), &bitmap, kRGB_565_SkColorType);
  32. fBitmaps.push_back(std::move(bitmap));
  33. }
  34. this->setBGColor(SK_ColorGRAY);
  35. }
  36. SkString name() override { return SkString("Filter/Dither"); }
  37. void onDrawContent(SkCanvas* canvas) override {
  38. canvas->translate(SkIntToScalar(10), SkIntToScalar(50));
  39. const SkScalar W = SkIntToScalar(fBitmaps[0].width() + 1);
  40. const SkScalar H = SkIntToScalar(fBitmaps[0].height() + 1);
  41. SkPaint paint;
  42. const SkScalar scale = 0.897917f;
  43. canvas->scale(SK_Scalar1, scale);
  44. for (int k = 0; k < 2; k++) {
  45. paint.setFilterQuality(k == 1 ? kLow_SkFilterQuality : kNone_SkFilterQuality);
  46. for (int j = 0; j < 2; j++) {
  47. paint.setDither(j == 1);
  48. for (int i = 0; i < (int)fBitmaps.size(); i++) {
  49. SkScalar x = (k * (int)fBitmaps.size() + j) * W;
  50. SkScalar y = i * H;
  51. x = SkScalarRoundToScalar(x);
  52. y = SkScalarRoundToScalar(y);
  53. canvas->drawBitmap(fBitmaps[i], x, y, &paint);
  54. SkFont font;
  55. font.setSize(SkIntToScalar(18));
  56. if (i == 0) {
  57. SkString s("dither=");
  58. s.appendS32(paint.isDither());
  59. s.append(" filter=");
  60. s.appendS32(paint.getFilterQuality() != kNone_SkFilterQuality);
  61. SkTextUtils::DrawString(canvas, s.c_str(), x + W/2, y - font.getSize(), font, SkPaint(),
  62. SkTextUtils::kCenter_Align);
  63. }
  64. if (k+j == 2) {
  65. SkString s;
  66. s.append(" depth=");
  67. s.appendS32(fBitmaps[i].colorType() == kRGB_565_SkColorType ? 16 : 32);
  68. SkTextUtils::DrawString(canvas, s.c_str(), x + W + SkIntToScalar(4), y + H/2, font, SkPaint());
  69. }
  70. }
  71. }
  72. }
  73. }
  74. };
  75. DEF_SAMPLE( return new Filter2View(); )