17
17
#include " operror.hpp"
18
18
19
19
#define STB_IMAGE_IMPLEMENTATION
20
- // #define STBI_ONLY_PNM
20
+ #define STBI_ONLY_PNM
21
+ #define STBI_ONLY_PNG
22
+
21
23
#include " stb/stb_image.h"
22
24
23
25
#define STB_IMAGE_WRITE_IMPLEMENTATION
@@ -50,12 +52,26 @@ namespace opqr::pic
50
52
};
51
53
private:
52
54
std::vector<std::vector<bool >> data;
53
-
55
+
54
56
public:
55
57
Pic (std::vector<std::vector<bool >> data_)
56
58
: data(std::move(data_)) {}
57
-
59
+
58
60
void paint (Format fmt, const std::string &path, size_t width, size_t height) const
61
+ {
62
+ std::ofstream fs (path, std::ios::binary | std::ios::out);
63
+ paint (fmt, fs, width, height);
64
+ fs.close ();
65
+ }
66
+
67
+ void paint (Format fmt, const std::string &path, size_t enlarge) const
68
+ {
69
+ std::ofstream fs (path, std::ios::binary | std::ios::out);
70
+ paint (fmt, fs, enlarge);
71
+ fs.close ();
72
+ }
73
+
74
+ void paint (Format fmt, std::ostream &os, size_t width, size_t height) const
59
75
{
60
76
if (fmt == Format::ANSI256)
61
77
{
@@ -75,25 +91,10 @@ namespace opqr::pic
75
91
pic.data = out;
76
92
pic.width = static_cast <int >(width);
77
93
pic.height = static_cast <int >(height);
78
- write_pic (fmt, path , pic);
94
+ write_pic (fmt, os , pic);
79
95
stbi_image_free (out);
80
96
}
81
97
82
- void paint (Format fmt, const std::string &path, size_t enlarge = 1 ) const
83
- {
84
- if (enlarge == 0 )
85
- {
86
- throw error::Error (OPQR_ERROR_LOCATION, __func__, " enlarge must >= 1." );
87
- }
88
- if (fmt == Format::ANSI256)
89
- {
90
- throw error::Error (OPQR_ERROR_LOCATION, __func__, " Unsupported format(ANSI256) when writing to path" );
91
- }
92
- auto pic = load_pic (enlarge);
93
- write_pic (fmt, path, pic);
94
- stbi_image_free (pic.data );
95
- }
96
-
97
98
void paint (Format fmt, std::ostream &os, size_t enlarge = 1 ) const
98
99
{
99
100
if (enlarge == 0 )
@@ -126,28 +127,37 @@ namespace opqr::pic
126
127
}
127
128
break ;
128
129
default :
129
- throw error::Error (OPQR_ERROR_LOCATION, __func__, " Unsupported format when writing to a stream" );
130
+ {
131
+ auto pic = load_pic (enlarge);
132
+ write_pic (fmt, os, pic);
133
+ stbi_image_free (pic.data );
134
+ }
130
135
break ;
131
136
}
132
137
}
133
138
134
139
private:
135
- void write_pic (Format fmt, const std::string &path , StbData data) const
140
+ void write_pic (Format fmt, std::ostream &os , StbData data) const
136
141
{
137
142
int ret = 0 ;
143
+ auto func = [](void *context, void *data, int size)
144
+ {
145
+ auto osptr = reinterpret_cast <std::ostream *>(context);
146
+ osptr->write (reinterpret_cast <char *>(data), size);
147
+ };
138
148
switch (fmt)
139
149
{
140
150
case Format::JPG:
141
- ret = stbi_write_jpg (path. c_str () , data.width , data.height , data.channels , data.data , 100 );
151
+ ret = stbi_write_jpg_to_func (func, &os , data.width , data.height , data.channels , data.data , 100 );
142
152
break ;
143
153
case Format::PNG:
144
- ret = stbi_write_png (path. c_str () , data.width , data.height , data.channels , data.data , 0 );
154
+ ret = stbi_write_png_to_func (func, &os , data.width , data.height , data.channels , data.data , 0 );
145
155
break ;
146
156
case Format::TGA:
147
- ret = stbi_write_tga (path. c_str () , data.width , data.height , data.channels , data.data );
157
+ ret = stbi_write_tga_to_func (func, &os , data.width , data.height , data.channels , data.data );
148
158
break ;
149
159
case Format::BMP:
150
- ret = stbi_write_bmp (path. c_str () , data.width , data.height , data.channels , data.data );
160
+ ret = stbi_write_bmp_to_func (func, &os , data.width , data.height , data.channels , data.data );
151
161
break ;
152
162
}
153
163
if (ret == 0 )
0 commit comments