SampleUnpremul.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright 2013 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/SkColorPriv.h"
  9. #include "include/core/SkStream.h"
  10. #include "include/core/SkString.h"
  11. #include "include/core/SkTypes.h"
  12. #include "include/effects/SkBlurDrawLooper.h"
  13. #include "samplecode/DecodeFile.h"
  14. #include "samplecode/Sample.h"
  15. #include "src/core/SkBlurMask.h"
  16. #include "src/core/SkOSFile.h"
  17. #include "src/utils/SkOSPath.h"
  18. #include "src/utils/SkUTF.h"
  19. #include "tools/Resources.h"
  20. #include "tools/ToolUtils.h"
  21. #ifdef SK_SUPPORT_LEGACY_DRAWLOOPER
  22. /**
  23. * Interprets c as an unpremultiplied color, and returns the
  24. * premultiplied equivalent.
  25. */
  26. static SkPMColor premultiply_unpmcolor(SkPMColor c) {
  27. U8CPU a = SkGetPackedA32(c);
  28. U8CPU r = SkGetPackedR32(c);
  29. U8CPU g = SkGetPackedG32(c);
  30. U8CPU b = SkGetPackedB32(c);
  31. return SkPreMultiplyARGB(a, r, g, b);
  32. }
  33. class UnpremulView : public Sample {
  34. public:
  35. UnpremulView(SkString res)
  36. : fResPath(res)
  37. , fPremul(true)
  38. , fDecodeSucceeded(false) {
  39. this->nextImage();
  40. }
  41. protected:
  42. SkString name() override { return SkString("unpremul"); }
  43. bool onChar(SkUnichar uni) override {
  44. char utf8[SkUTF::kMaxBytesInUTF8Sequence];
  45. size_t size = SkUTF::ToUTF8(uni, utf8);
  46. // Only consider events for single char keys
  47. if (1 == size) {
  48. switch (utf8[0]) {
  49. case fNextImageChar:
  50. this->nextImage();
  51. return true;
  52. case fTogglePremulChar:
  53. this->togglePremul();
  54. return true;
  55. default:
  56. break;
  57. }
  58. }
  59. return false;
  60. }
  61. void onDrawBackground(SkCanvas* canvas) override {
  62. ToolUtils::draw_checkerboard(canvas, 0xFFCCCCCC, 0xFFFFFFFF, 12);
  63. }
  64. void onDrawContent(SkCanvas* canvas) override {
  65. SkPaint paint;
  66. paint.setAntiAlias(true);
  67. SkFont font;
  68. font.setSize(24);
  69. auto looper(
  70. SkBlurDrawLooper::Make(SK_ColorBLUE, SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(2)),
  71. 0, 0));
  72. paint.setLooper(looper);
  73. SkScalar height = font.getMetrics(nullptr);
  74. if (!fDecodeSucceeded) {
  75. SkString failure;
  76. if (fResPath.size() == 0) {
  77. failure.printf("resource path is required!");
  78. } else {
  79. failure.printf("Failed to decode %s", fCurrFile.c_str());
  80. }
  81. canvas->drawString(failure, 0, height, font, paint);
  82. return;
  83. }
  84. // Name, size of the file, and whether or not it is premultiplied.
  85. SkString header(SkOSPath::Basename(fCurrFile.c_str()));
  86. header.appendf(" [%dx%d] %s", fBitmap.width(), fBitmap.height(),
  87. (fPremul ? "premultiplied" : "unpremultiplied"));
  88. canvas->drawString(header, 0, height, font, paint);
  89. canvas->translate(0, height);
  90. // Help messages
  91. header.printf("Press '%c' to move to the next image.'", fNextImageChar);
  92. canvas->drawString(header, 0, height, font, paint);
  93. canvas->translate(0, height);
  94. header.printf("Press '%c' to toggle premultiplied decode.", fTogglePremulChar);
  95. canvas->drawString(header, 0, height, font, paint);
  96. // Now draw the image itself.
  97. canvas->translate(height * 2, height * 2);
  98. if (!fPremul) {
  99. // A premultiplied bitmap cannot currently be drawn.
  100. // Copy it to a bitmap which can be drawn, converting
  101. // to premultiplied:
  102. SkBitmap bm;
  103. bm.allocN32Pixels(fBitmap.width(), fBitmap.height());
  104. for (int i = 0; i < fBitmap.width(); ++i) {
  105. for (int j = 0; j < fBitmap.height(); ++j) {
  106. *bm.getAddr32(i, j) = premultiply_unpmcolor(*fBitmap.getAddr32(i, j));
  107. }
  108. }
  109. canvas->drawBitmap(bm, 0, 0);
  110. } else {
  111. canvas->drawBitmap(fBitmap, 0, 0);
  112. }
  113. }
  114. private:
  115. const SkString fResPath;
  116. SkString fCurrFile;
  117. bool fPremul;
  118. bool fDecodeSucceeded;
  119. SkBitmap fBitmap;
  120. SkOSFile::Iter fFileIter;
  121. static const char fNextImageChar = 'j';
  122. static const char fTogglePremulChar = 'h';
  123. void nextImage() {
  124. if (fResPath.size() == 0) {
  125. return;
  126. }
  127. SkString basename;
  128. if (!fFileIter.next(&basename)) {
  129. fFileIter.reset(fResPath.c_str());
  130. if (!fFileIter.next(&basename)) {
  131. // Perhaps this should draw some error message?
  132. return;
  133. }
  134. }
  135. fCurrFile = SkOSPath::Join(fResPath.c_str(), basename.c_str());
  136. this->decodeCurrFile();
  137. }
  138. void decodeCurrFile() {
  139. if (fCurrFile.size() == 0) {
  140. fDecodeSucceeded = false;
  141. return;
  142. }
  143. fDecodeSucceeded = decode_file(fCurrFile.c_str(), &fBitmap, kN32_SkColorType, !fPremul);
  144. }
  145. void togglePremul() {
  146. fPremul = !fPremul;
  147. this->decodeCurrFile();
  148. }
  149. typedef Sample INHERITED;
  150. };
  151. //////////////////////////////////////////////////////////////////////////////
  152. DEF_SAMPLE( return new UnpremulView(GetResourcePath("images")); )
  153. #endif