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