blob: 96835eba3f1dd978849fdf8a20878618dc5d4762 [file] [log] [blame]
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "screen-capture-utils/png.h"
#include <setjmp.h>
#include <vector>
#include <base/logging.h>
#include <base/files/scoped_file.h>
#include <png.h>
namespace screenshot {
void SaveAsPng(const char* path,
void* data,
uint32_t width,
uint32_t height,
uint32_t stride) {
png_struct* png =
png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
CHECK(png) << "png_create_write_struct failed";
png_info* info = png_create_info_struct(png);
CHECK(info) << "png_create_info_struct failed";
CHECK_EQ(setjmp(png_jmpbuf(png)), 0) << "PNG encode failed";
base::ScopedFILE fp(fopen(path, "we"));
PCHECK(fp) << "Failed to open " << path << " for writing";
png_init_io(png, fp.get());
png_set_IHDR(png, info, width, height, 8, PNG_COLOR_TYPE_RGB,
png_write_info(png, info);
png_set_filler(png, 0, PNG_FILLER_AFTER);
std::vector<png_byte*> rows(height);
for (uint32_t i = 0; i < height; ++i)
rows[i] = static_cast<png_byte*>(data) + stride * i;
png_write_end(png, nullptr);
png_destroy_write_struct(&png, &info);
} // namespace screenshot