EncodedInfoTest.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * Copyright 2018 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 "tests/Test.h"
  8. #include "tools/Resources.h"
  9. #include "tools/ToolUtils.h"
  10. #include "include/codec/SkCodec.h"
  11. #include "include/core/SkBitmap.h"
  12. #include "include/core/SkData.h"
  13. #include "include/core/SkEncodedImageFormat.h"
  14. #include "include/core/SkImageEncoder.h"
  15. #include "include/core/SkImageInfo.h"
  16. DEF_TEST(AlphaEncodedInfo, r) {
  17. auto codec = SkCodec::MakeFromStream(GetResourceAsStream("images/grayscale.jpg"));
  18. REPORTER_ASSERT(r, codec->getInfo().colorType() == kGray_8_SkColorType);
  19. SkBitmap bm;
  20. bm.allocPixels(codec->getInfo().makeColorType(kAlpha_8_SkColorType).makeColorSpace(nullptr));
  21. auto result = codec->getPixels(codec->getInfo(), bm.getPixels(), bm.rowBytes());
  22. REPORTER_ASSERT(r, result == SkCodec::kSuccess);
  23. auto data = SkEncodeBitmap(bm, SkEncodedImageFormat::kPNG, 100);
  24. REPORTER_ASSERT(r, data);
  25. codec = SkCodec::MakeFromData(std::move(data));
  26. REPORTER_ASSERT(r, codec);
  27. // TODO: Make SkEncodedInfo public and compare to its version of kAlpha_8.
  28. REPORTER_ASSERT(r, codec->getInfo().colorType() == kAlpha_8_SkColorType);
  29. SkBitmap bm2;
  30. bm2.allocPixels(codec->getInfo().makeColorSpace(nullptr));
  31. result = codec->getPixels(bm2.pixmap());
  32. REPORTER_ASSERT(r, result == SkCodec::kSuccess);
  33. REPORTER_ASSERT(r, ToolUtils::equal_pixels(bm.pixmap(), bm2.pixmap()));
  34. }