xref: /third_party/skia/tests/CodecPriv.h (revision cb93a386)
1/*
2 * Copyright 2016 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#ifndef CodecPriv_DEFINED
8#define CodecPriv_DEFINED
9
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/SkStream.h"
16#include "src/utils/SkOSPath.h"
17#include "tools/flags/CommandLineFlags.h"
18
19static DEFINE_string(codecWritePath, "",
20                     "Dump image decodes from codec unit tests here.");
21
22inline bool decode_memory(const void* mem, size_t size, SkBitmap* bm) {
23    std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(SkData::MakeWithoutCopy(mem, size)));
24    if (!codec) {
25        return false;
26    }
27
28    bm->allocPixels(codec->getInfo());
29    const SkCodec::Result result = codec->getPixels(codec->getInfo(), bm->getPixels(),
30            bm->rowBytes());
31    return result == SkCodec::kSuccess || result == SkCodec::kIncompleteInput;
32}
33
34inline void write_bm(const char* name, const SkBitmap& bm) {
35    if (FLAGS_codecWritePath.isEmpty()) {
36        return;
37    }
38
39    SkString filename = SkOSPath::Join(FLAGS_codecWritePath[0], name);
40    filename.appendf(".png");
41    SkFILEWStream file(filename.c_str());
42    if (!SkEncodeImage(&file, bm, SkEncodedImageFormat::kPNG, 100)) {
43        SkDebugf("failed to write '%s'\n", filename.c_str());
44    }
45}
46
47#endif  // CodecPriv_DEFINED
48