blob: 88de79c65e96bebaafd37af9caafb7b021928626 [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
10#include "SkBitmap.h"
11#include "SkCodec.h"
Leon Scroggins III4c119452018-01-20 10:33:24 -050012#include "SkCommonFlags.h"
msarett7f7ec202016-03-01 12:12:27 -080013#include "SkData.h"
Leon Scroggins III4c119452018-01-20 10:33:24 -050014#include "SkEncodedImageFormat.h"
15#include "SkImageEncoder.h"
16#include "SkOSPath.h"
17#include "SkStream.h"
msarett7f7ec202016-03-01 12:12:27 -080018
19inline bool decode_memory(const void* mem, size_t size, SkBitmap* bm) {
Mike Reedede7bac2017-07-23 15:30:02 -040020 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(SkData::MakeWithoutCopy(mem, size)));
msarett7f7ec202016-03-01 12:12:27 -080021 if (!codec) {
22 return false;
23 }
24
Leon Scroggins571b30f2017-07-11 17:35:31 +000025 bm->allocPixels(codec->getInfo());
msarett7f7ec202016-03-01 12:12:27 -080026 const SkCodec::Result result = codec->getPixels(codec->getInfo(), bm->getPixels(),
Leon Scroggins571b30f2017-07-11 17:35:31 +000027 bm->rowBytes());
msarett7f7ec202016-03-01 12:12:27 -080028 return result == SkCodec::kSuccess || result == SkCodec::kIncompleteInput;
29}
Leon Scroggins III4c119452018-01-20 10:33:24 -050030
31inline void write_bm(const char* name, const SkBitmap& bm) {
32 if (FLAGS_writePath.isEmpty()) {
33 return;
34 }
35
36 SkString filename = SkOSPath::Join(FLAGS_writePath[0], name);
37 filename.appendf(".png");
38 SkFILEWStream file(filename.c_str());
39 if (!SkEncodeImage(&file, bm, SkEncodedImageFormat::kPNG, 100)) {
40 SkDebugf("failed to write '%s'\n", filename.c_str());
41 }
42}
43
Hal Canary03a7f5f2017-02-10 09:06:38 -050044#endif // CodecPriv_DEFINED