blob: 16535ed2826a086358870232a78d797b29f0f49b [file] [log] [blame]
Leon Scroggins III36f7e322018-08-27 11:55:46 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tests/Test.h"
9#include "tools/Resources.h"
10#include "tools/ToolUtils.h"
Leon Scroggins III36f7e322018-08-27 11:55:46 -040011
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#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"
Leon Scroggins III36f7e322018-08-27 11:55:46 -040018
19DEF_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
Mike Kleinea3f0142019-03-20 11:12:10 -050041 REPORTER_ASSERT(r, ToolUtils::equal_pixels(bm.pixmap(), bm2.pixmap()));
Leon Scroggins III36f7e322018-08-27 11:55:46 -040042}