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