99#include < fstream>
1010#include < iostream>
1111#include < sstream>
12- #include < string>
13- #include < vector>
1412
1513#define STB_IMAGE_WRITE_IMPLEMENTATION
1614#include " stb_image_write.h"
@@ -101,11 +99,6 @@ int main(const int argc, char **argv) {
10199 hex_length = 8 ;
102100 }
103101
104- uint8_t channels = 3 ;
105- if (hex_length % 4 == 0 ) {
106- channels = 4 ;
107- }
108-
109102 if (output_file.find (" .png" ) != std::string::npos) {
110103 output_file = output_file.substr (0 , output_file.find (" .png" ));
111104 }
@@ -118,103 +111,15 @@ int main(const int argc, char **argv) {
118111
119112 if (hex_input.is_open ()) {
120113 std::string line;
121- uint16_t max_len = 0 ;
122114
123115 std::vector<std::string> lines;
124116 while (getline (hex_input, line)) {
125117 lines.push_back (line);
126- if (line.length () > max_len) {
127- max_len = line.length ();
128- }
129118 }
130119
131120 hex_input.close ();
132121
133- bool ignore_linebreaks = true ;
134- double pixels_per_line = double (max_len) / hex_length;
135- if (width == 0 ) {
136- if (height == 0 ) {
137- width = ceil (pixels_per_line);
138- ignore_linebreaks = false ;
139- height = lines.size ();
140- } else {
141- width = ceil (double (lines.size ()) / height * pixels_per_line);
142- }
143- }
144-
145- if (height == 0 ) {
146- height = ceil (lines.size () / (width / pixels_per_line));
147- }
148-
149- printf (" Output image size: %d x %d pixels.\n " , width, height);
150-
151- uint8_t *pixel_colors = new uint8_t [width * height * channels];
152-
153- for (uint32_t i = 0 ; i < width * height * channels; i++) {
154- pixel_colors[i] = 0 ;
155- }
156-
157- std::string temp;
158- std::string color;
159- std::istringstream stream;
160- uint32_t array_pos = 0 ;
161- uint32_t parsed;
162- for (size_t y = 0 ; y < lines.size (); y++) {
163- line = lines[y];
164-
165- if (ignore_linebreaks && temp.length () > 0 ) {
166- line = temp + line;
167- temp = " " ;
168- }
169-
170- while (line.length () > 0 ) {
171- for (uint16_t x = 0 ; x < width; x++) {
172- if (line.length () < uint32_t ((x + 1 ) * hex_length)) {
173- temp = line.substr (x * hex_length, line.length () - x * hex_length);
174- line = " " ;
175- break ;
176- }
177-
178- if (ignore_linebreaks) {
179- if (y != 0 ) {
180- array_pos += channels;
181- }
182- } else {
183- array_pos = y * width * channels + x * channels;
184- }
185-
186- color = line.substr (x * hex_length, hex_length);
187- if (hex_length <= 4 ) {
188- color.resize (hex_length * 2 );
189- for (int8_t i = hex_length - 1 ; i >= 0 ; i--) {
190- color[i * 2 + 1 ] = color[i * 2 ] = color[i];
191- }
192- }
193-
194- stream.str (color);
195- stream >> std::hex >> parsed;
196-
197- if (stream.rdbuf ()->in_avail () > 0 ) {
198- cerr << line.substr (x * hex_length, hex_length) << " can't be parsed as hex input!" << endl;
199- } else {
200- pixel_colors[array_pos] = (parsed >> 16 ) & 0xFF ;
201- pixel_colors[array_pos + 1 ] = (parsed >> 8 ) & 0xFF ;
202- pixel_colors[array_pos + 2 ] = parsed & 0xFF ;
203- if (channels == 4 ) {
204- pixel_colors[array_pos + 3 ] = (parsed >> 24 ) & 0xFF ;
205- }
206- }
207-
208- stream.clear ();
209- }
210-
211- if (line.length () > width * hex_length) {
212- line = line.substr (width * hex_length, line.length () - width * hex_length);
213- } else {
214- line = " " ;
215- }
216- }
217- }
122+ uint8_t *pixel_colors = convert (lines, hex_length, width, height);
218123
219124 if (FILE *file = fopen (std::string (output_file).append (" .png" ).c_str (), " r" )) {
220125 fclose (file);
@@ -243,6 +148,8 @@ int main(const int argc, char **argv) {
243148 output_file.append (" .png" );
244149 }
245150
151+ uint8_t channels = hex_length % 4 == 0 ? 4 : 3 ;
152+
246153 stbi_write_png (output_file.c_str (), width, height, channels, pixel_colors, width * channels);
247154 cout << " Image saved as \" " << output_file << " \" ." << endl;
248155 delete[] pixel_colors;
@@ -254,6 +161,107 @@ int main(const int argc, char **argv) {
254161 }
255162}
256163
164+ uint8_t * convert (const std::vector<std::string> lines, const uint8_t hex_len, uint16_t &width, uint16_t &height) {
165+ size_t max_len = 0 ;
166+ for (std::string line : lines) {
167+ max_len = std::max (max_len, line.length ());
168+ }
169+
170+ uint8_t channels = hex_len % 4 == 0 ? 4 : 3 ;
171+
172+ bool ignore_linebreaks = true ;
173+ double pixels_per_line = double (max_len) / hex_len;
174+ if (width == 0 ) {
175+ if (height == 0 ) {
176+ width = ceil (pixels_per_line);
177+ ignore_linebreaks = false ;
178+ height = lines.size ();
179+ } else {
180+ width = ceil (double (lines.size ()) / height * pixels_per_line);
181+ }
182+ }
183+
184+ if (height == 0 ) {
185+ height = ceil (lines.size () / (width / pixels_per_line));
186+ }
187+
188+ printf (" Output image size: %d x %d pixels.\n " , width, height);
189+
190+ uint8_t *pixel_colors = new uint8_t [width * height * channels];
191+
192+ for (uint32_t i = 0 ; i < width * height * channels; i++) {
193+ pixel_colors[i] = 0 ;
194+ }
195+
196+ std::string line;
197+ std::string temp;
198+ std::string color;
199+ std::istringstream stream;
200+ uint32_t array_pos = 0 ;
201+ uint32_t parsed;
202+ for (size_t y = 0 ; y < lines.size (); y++) {
203+ line = lines[y];
204+
205+ if (ignore_linebreaks && temp.length () > 0 ) {
206+ line = temp + line;
207+ temp = " " ;
208+ }
209+
210+ while (line.length () > 0 ) {
211+ for (uint16_t x = 0 ; x < width; x++) {
212+ if (line.length () < uint32_t ((x + 1 ) * hex_len)) {
213+ temp = line.substr (x * hex_len,
214+ line.length () - x * hex_len);
215+ line = " " ;
216+ break ;
217+ }
218+
219+ if (ignore_linebreaks) {
220+ if (y != 0 ) {
221+ array_pos += channels;
222+ }
223+ } else {
224+ array_pos = y * width * channels + x * channels;
225+ }
226+
227+ color = line.substr (x * hex_len, hex_len);
228+ if (hex_len <= 4 ) {
229+ color.resize (hex_len * 2 );
230+ for (int8_t i = hex_len - 1 ; i >= 0 ; i--) {
231+ color[i * 2 + 1 ] = color[i * 2 ] = color[i];
232+ }
233+ }
234+
235+ stream.str (color);
236+ stream >> std::hex >> parsed;
237+
238+ if (stream.rdbuf ()->in_avail () > 0 ) {
239+ cerr << line.substr (x * hex_len, hex_len)
240+ << " can't be parsed as hex input!" << endl;
241+ } else {
242+ pixel_colors[array_pos] = (parsed >> 16 ) & 0xFF ;
243+ pixel_colors[array_pos + 1 ] = (parsed >> 8 ) & 0xFF ;
244+ pixel_colors[array_pos + 2 ] = parsed & 0xFF ;
245+ if (channels == 4 ) {
246+ pixel_colors[array_pos + 3 ] = (parsed >> 24 ) & 0xFF ;
247+ }
248+ }
249+
250+ stream.clear ();
251+ }
252+
253+ if (line.length () > width * hex_len) {
254+ line = line.substr (width * hex_len,
255+ line.length () - width * hex_len);
256+ } else {
257+ line = " " ;
258+ }
259+ }
260+ }
261+
262+ return pixel_colors;
263+ }
264+
257265void printHelp () {
258266 cout << " Hex to image Help" << endl;
259267 cout << " =================" << endl;
0 commit comments