copy_to_4444.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 "gm/gm.h"
  8. #include "include/core/SkBitmap.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkImageInfo.h"
  12. #include "include/core/SkPixmap.h"
  13. #include "include/core/SkScalar.h"
  14. #include "include/core/SkSize.h"
  15. #include "include/core/SkString.h"
  16. #include "include/core/SkTypes.h"
  17. #include "tools/Resources.h"
  18. #include "tools/ToolUtils.h"
  19. namespace {
  20. /**
  21. * Test copying an image from 8888 to 4444.
  22. */
  23. class CopyTo4444GM : public skiagm::GM {
  24. SkString onShortName() override { return SkString("copyTo4444"); }
  25. SkISize onISize() override { return {360, 180}; }
  26. DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
  27. SkBitmap bm, bm4444;
  28. if (!GetResourceAsBitmap("images/dog.jpg", &bm)) {
  29. *errorMsg = "Could not decode the file. Did you forget to set the resourcePath?";
  30. return DrawResult::kFail;
  31. }
  32. canvas->drawBitmap(bm, 0, 0);
  33. // This should dither or we will see artifacts in the background of the image.
  34. SkAssertResult(ToolUtils::copy_to(&bm4444, kARGB_4444_SkColorType, bm));
  35. canvas->drawBitmap(bm4444, SkIntToScalar(bm.width()), 0);
  36. return DrawResult::kOk;
  37. }
  38. };
  39. } // namespace
  40. DEF_GM( return new CopyTo4444GM; )
  41. //////////////////////////////////////////////////////////////////////////////
  42. DEF_SIMPLE_GM(format4444, canvas, 64, 64) {
  43. canvas->scale(16, 16);
  44. SkBitmap bitmap;
  45. SkImageInfo imageInfo = SkImageInfo::Make(1, 1, kARGB_4444_SkColorType, kPremul_SkAlphaType);
  46. bitmap.allocPixels(imageInfo);
  47. SkCanvas offscreen(bitmap);
  48. offscreen.clear(SK_ColorRED);
  49. canvas->drawBitmap(bitmap, 0, 0);
  50. offscreen.clear(SK_ColorBLUE);
  51. canvas->drawBitmap(bitmap, 1, 1);
  52. auto pack4444 = [](unsigned a, unsigned r, unsigned g, unsigned b) -> uint16_t {
  53. return (a << 0) | (b << 4) | (g << 8) | (r << 12);
  54. };
  55. uint16_t red4444 = pack4444(0xF, 0xF, 0x0, 0x0);
  56. uint16_t blue4444 = pack4444(0xF, 0x0, 0x0, 0x0F);
  57. SkPixmap redPixmap(imageInfo, &red4444, 2);
  58. if (bitmap.writePixels(redPixmap, 0, 0)) {
  59. canvas->drawBitmap(bitmap, 2, 2);
  60. }
  61. SkPixmap bluePixmap(imageInfo, &blue4444, 2);
  62. if (bitmap.writePixels(bluePixmap, 0, 0)) {
  63. canvas->drawBitmap(bitmap, 3, 3);
  64. }
  65. }