summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/util/png.h
blob: d802c1a5a82b260b9c91b67e7739340ed853e1fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/***************************************************************************

    png.h

    PNG file management.

***************************************************************************/

#ifndef MAME_LIB_UTIL_PNG_H
#define MAME_LIB_UTIL_PNG_H

#pragma once

#include "bitmap.h"
#include "utilfwd.h"

#include <cstdint>
#include <list>
#include <memory>
#include <string>
#include <string_view>
#include <system_error>
#include <utility>


namespace util {

/***************************************************************************
    CONSTANTS
***************************************************************************/

/* Error types */
enum class png_error : int
{
	UNKNOWN_FILTER = 1,
	BAD_SIGNATURE,
	DECOMPRESS_ERROR,
	FILE_TRUNCATED,
	FILE_CORRUPT,
	UNKNOWN_CHUNK,
	COMPRESS_ERROR,
	UNSUPPORTED_FORMAT
};

std::error_category const &png_category() noexcept;
inline std::error_condition make_error_condition(png_error err) noexcept { return std::error_condition(int(err), png_category()); }



/***************************************************************************
    TYPE DEFINITIONS
***************************************************************************/

class png_info
{
public:
	using png_text = std::pair<std::string, std::string>;

	~png_info() { free_data(); }

	std::error_condition read_file(read_stream &fp);
	std::error_condition copy_to_bitmap(bitmap_argb32 &bitmap, bool &hasalpha);
	std::error_condition expand_buffer_8bit();

	std::error_condition add_text(std::string_view keyword, std::string_view text);

	void free_data();
	void reset() { free_data(); operator=(png_info()); }

	static std::error_condition verify_header(read_stream &fp);

	std::unique_ptr<std::uint8_t []>    image;
	std::uint32_t                       width, height;
	std::uint32_t                       xres = 0, yres = 0;
	rectangle                           screen;
	double                              xscale = 0, yscale = 0;
	double                              source_gamma = 0;
	std::uint32_t                       resolution_unit = 0;
	std::uint8_t                        bit_depth = 0;
	std::uint8_t                        color_type = 0;
	std::uint8_t                        compression_method = 0;
	std::uint8_t                        filter_method = 0;
	std::uint8_t                        interlace_method = 0;

	std::unique_ptr<std::uint8_t []>    palette = 0;
	std::uint32_t                       num_palette = 0;

	std::unique_ptr<std::uint8_t []>    trans = 0;
	std::uint32_t                       num_trans = 0;

	std::list<png_text>                 textlist;

private:
	png_info &operator=(png_info &&) = default;
};



/***************************************************************************
    FUNCTION PROTOTYPES
***************************************************************************/

std::error_condition png_read_bitmap(read_stream &fp, bitmap_argb32 &bitmap);

std::error_condition png_write_bitmap(random_write &fp, png_info *info, bitmap_t const &bitmap, int palette_length, const rgb_t *palette);

std::error_condition mng_capture_start(random_write &fp, bitmap_t const &bitmap, unsigned rate);
std::error_condition mng_capture_frame(random_write &fp, png_info &info, bitmap_t const &bitmap, int palette_length, rgb_t const *palette);
std::error_condition mng_capture_stop(random_write &fp);

} // namespace util


namespace std {

template <> struct is_error_condition_enum<util::png_error> : public std::true_type { };

} // namespace std

#endif // MAME_LIB_UTIL_PNG_H