Color_Type_RGB_565.cpp 1.3 KB

123456789101112131415161718192021222324252627282930
  1. // Copyright 2019 Google LLC.
  2. // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
  3. #include "tools/fiddle/examples.h"
  4. // HASH=7e7c46bb4572e21e13529ff364eb0a9c
  5. REG_FIDDLE(Color_Type_RGB_565, 256, 96, false, 0) {
  6. void draw(SkCanvas* canvas) {
  7. canvas->scale(16, 16);
  8. SkBitmap bitmap;
  9. SkImageInfo imageInfo = SkImageInfo::Make(2, 2, kRGB_565_SkColorType, kOpaque_SkAlphaType);
  10. bitmap.allocPixels(imageInfo);
  11. SkCanvas offscreen(bitmap);
  12. offscreen.clear(SK_ColorGREEN);
  13. canvas->drawBitmap(bitmap, 0, 0);
  14. auto pack565 = [](unsigned r, unsigned g, unsigned b) -> uint16_t {
  15. return (b << 0) | (g << 5) | (r << 11);
  16. };
  17. uint16_t red565[] = { pack565(0x1F, 0x00, 0x00), pack565(0x17, 0x00, 0x00),
  18. pack565(0x0F, 0x00, 0x00), pack565(0x07, 0x00, 0x00) };
  19. uint16_t blue565[] = { pack565(0x00, 0x00, 0x1F), pack565(0x00, 0x00, 0x17),
  20. pack565(0x00, 0x00, 0x0F), pack565(0x00, 0x00, 0x07) };
  21. SkPixmap redPixmap(imageInfo, &red565, imageInfo.minRowBytes());
  22. if (bitmap.writePixels(redPixmap, 0, 0)) {
  23. canvas->drawBitmap(bitmap, 2, 2);
  24. }
  25. SkPixmap bluePixmap(imageInfo, &blue565, imageInfo.minRowBytes());
  26. if (bitmap.writePixels(bluePixmap, 0, 0)) {
  27. canvas->drawBitmap(bitmap, 4, 4);
  28. }
  29. }
  30. } // END FIDDLE