xref: /aosp_15_r20/external/skia/tests/EncodedInfoTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "include/codec/SkCodec.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkColorSpace.h"
11 #include "include/core/SkColorType.h"
12 #include "include/core/SkDataTable.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkStream.h"
15 #include "include/encode/SkPngEncoder.h"
16 #include "tests/Test.h"
17 #include "tools/Resources.h"
18 #include "tools/ToolUtils.h"
19 
20 #include <memory>
21 
DEF_TEST(AlphaEncodedInfo,r)22 DEF_TEST(AlphaEncodedInfo, r) {
23     auto codec = SkCodec::MakeFromStream(GetResourceAsStream("images/grayscale.jpg"));
24     REPORTER_ASSERT(r, codec->getInfo().colorType() == kGray_8_SkColorType);
25 
26     SkBitmap bm;
27     bm.allocPixels(codec->getInfo().makeColorType(kAlpha_8_SkColorType).makeColorSpace(nullptr));
28     auto result = codec->getPixels(codec->getInfo(), bm.getPixels(), bm.rowBytes());
29     REPORTER_ASSERT(r, result == SkCodec::kSuccess);
30 
31     SkDynamicMemoryWStream stream;
32     REPORTER_ASSERT(r, SkPngEncoder::Encode(&stream, bm.pixmap(), {}));
33     REPORTER_ASSERT(r, stream.bytesWritten() > 0);
34 
35     codec = SkCodec::MakeFromData(stream.detachAsData());
36     REPORTER_ASSERT(r, codec);
37     // TODO: Make SkEncodedInfo public and compare to its version of kAlpha_8.
38     REPORTER_ASSERT(r, codec->getInfo().colorType() == kAlpha_8_SkColorType);
39 
40     SkBitmap bm2;
41     bm2.allocPixels(codec->getInfo().makeColorSpace(nullptr));
42     result = codec->getPixels(bm2.pixmap());
43     REPORTER_ASSERT(r, result == SkCodec::kSuccess);
44 
45     REPORTER_ASSERT(r, ToolUtils::equal_pixels(bm.pixmap(), bm2.pixmap()));
46 }
47