1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <chrono>
17 #include <ctime>
18 #include <iomanip>
19 #include <sstream>
20
21 #include "common/rs_common_tools.h"
22 #include "fstream"
23 #include "pixel_map.h"
24 #include "platform/common/rs_log.h"
25
26 namespace OHOS {
27 namespace Rosen {
28 namespace CommonTools {
GetLocalTime()29 std::string GetLocalTime()
30 {
31 // time string : "year-month-day hour_minute_second.millisecond"
32 auto now = std::chrono::system_clock::now();
33 auto ms = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()) % 1000;
34 std::time_t t = std::chrono::system_clock::to_time_t(now);
35 std::tm* tm = std::localtime(&t);
36
37 std::stringstream ss;
38 int millSecondWidth = 3; // millsecond width
39 ss << std::put_time(tm, "%Y-%m-%d %H_%M_%S.") << std::setfill('0') << std::setw(millSecondWidth) << ms.count();
40 return ss.str();
41 }
42
SavePixelmapToFile(const std::shared_ptr<Media::PixelMap> & pixelMap,const std::string & dst)43 void SavePixelmapToFile(const std::shared_ptr<Media::PixelMap>& pixelMap, const std::string& dst)
44 {
45 int32_t w = pixelMap->GetWidth();
46 int32_t h = pixelMap->GetHeight();
47 int32_t totalSize = pixelMap->GetByteCount();
48 std::string localTime = GetLocalTime();
49 int32_t rowStirde = pixelMap->GetRowStride();
50 std::string fileName = dst + localTime + "_w" + std::to_string(w) + "_h" + std::to_string(h) +
51 "_stride" + std::to_string(rowStirde) + ".dat";
52 std::ofstream outfile(fileName, std::fstream::out);
53 if (!outfile.is_open()) {
54 RS_LOGE("SavePixelmapToFile write error, path=%{public}s", fileName.c_str());
55 return;
56 }
57 outfile.write(reinterpret_cast<const char*>(pixelMap->GetPixels()), totalSize);
58 outfile.close();
59 RS_LOGI("SavePixelmapToFile write success, path=%{public}s", fileName.c_str());
60 }
61 }
62 } // namespace Rosen
63 } // namespace OHOS