msarett | 7f7ec20 | 2016-03-01 12:12:27 -0800 | [diff] [blame] | 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 | */ |
Hal Canary | 03a7f5f | 2017-02-10 09:06:38 -0500 | [diff] [blame] | 7 | #ifndef CodecPriv_DEFINED |
| 8 | #define CodecPriv_DEFINED |
msarett | 7f7ec20 | 2016-03-01 12:12:27 -0800 | [diff] [blame] | 9 | |
| 10 | #include "SkBitmap.h" |
| 11 | #include "SkCodec.h" |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 12 | #include "SkCommonFlags.h" |
msarett | 7f7ec20 | 2016-03-01 12:12:27 -0800 | [diff] [blame] | 13 | #include "SkData.h" |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 14 | #include "SkEncodedImageFormat.h" |
| 15 | #include "SkImageEncoder.h" |
| 16 | #include "SkOSPath.h" |
| 17 | #include "SkStream.h" |
msarett | 7f7ec20 | 2016-03-01 12:12:27 -0800 | [diff] [blame] | 18 | |
| 19 | inline bool decode_memory(const void* mem, size_t size, SkBitmap* bm) { |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 20 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(SkData::MakeWithoutCopy(mem, size))); |
msarett | 7f7ec20 | 2016-03-01 12:12:27 -0800 | [diff] [blame] | 21 | if (!codec) { |
| 22 | return false; |
| 23 | } |
| 24 | |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 25 | bm->allocPixels(codec->getInfo()); |
msarett | 7f7ec20 | 2016-03-01 12:12:27 -0800 | [diff] [blame] | 26 | const SkCodec::Result result = codec->getPixels(codec->getInfo(), bm->getPixels(), |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 27 | bm->rowBytes()); |
msarett | 7f7ec20 | 2016-03-01 12:12:27 -0800 | [diff] [blame] | 28 | return result == SkCodec::kSuccess || result == SkCodec::kIncompleteInput; |
| 29 | } |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 30 | |
| 31 | inline 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 Canary | 03a7f5f | 2017-02-10 09:06:38 -0500 | [diff] [blame] | 44 | #endif // CodecPriv_DEFINED |