blob: b36f0eb353a5764aa503b116a06d7a759994e96d [file] [log] [blame]
msarett7f7ec202016-03-01 12:12:27 -08001/*
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 */
Hal Canary03a7f5f2017-02-10 09:06:38 -05007#ifndef CodecPriv_DEFINED
8#define CodecPriv_DEFINED
msarett7f7ec202016-03-01 12:12:27 -08009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#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"
msarett7f7ec202016-03-01 12:12:27 -080018
Mike Kleinc6142d82019-03-25 10:54:59 -050019static DEFINE_string(codecWritePath, "",
20 "Dump image decodes from codec unit tests here.");
21
msarett7f7ec202016-03-01 12:12:27 -080022inline bool decode_memory(const void* mem, size_t size, SkBitmap* bm) {
Mike Reedede7bac2017-07-23 15:30:02 -040023 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(SkData::MakeWithoutCopy(mem, size)));
msarett7f7ec202016-03-01 12:12:27 -080024 if (!codec) {
25 return false;
26 }
27
Leon Scroggins571b30f2017-07-11 17:35:31 +000028 bm->allocPixels(codec->getInfo());
msarett7f7ec202016-03-01 12:12:27 -080029 const SkCodec::Result result = codec->getPixels(codec->getInfo(), bm->getPixels(),
Leon Scroggins571b30f2017-07-11 17:35:31 +000030 bm->rowBytes());
msarett7f7ec202016-03-01 12:12:27 -080031 return result == SkCodec::kSuccess || result == SkCodec::kIncompleteInput;
32}
Leon Scroggins III4c119452018-01-20 10:33:24 -050033
34inline void write_bm(const char* name, const SkBitmap& bm) {
Mike Kleinc6142d82019-03-25 10:54:59 -050035 if (FLAGS_codecWritePath.isEmpty()) {
Leon Scroggins III4c119452018-01-20 10:33:24 -050036 return;
37 }
38
Mike Kleinc6142d82019-03-25 10:54:59 -050039 SkString filename = SkOSPath::Join(FLAGS_codecWritePath[0], name);
Leon Scroggins III4c119452018-01-20 10:33:24 -050040 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
Hal Canary03a7f5f2017-02-10 09:06:38 -050047#endif // CodecPriv_DEFINED