|
37 | 37 |
|
38 | 38 | #include <ctype.h> |
39 | 39 | #include <errno.h> |
| 40 | +#include <stddef.h> |
40 | 41 | #include <stdio.h> |
41 | 42 | #include <stdlib.h> |
42 | 43 | #include <string.h> |
@@ -227,7 +228,10 @@ bool pam_read(const char *filename, struct pam_metadata *info, unsigned char **d |
227 | 228 | return true; |
228 | 229 | } |
229 | 230 |
|
230 | | -bool pam_write(const char *filename, unsigned int width, unsigned int height, int ch_count, int maxval, const unsigned char *data, bool pnm) { |
| 231 | +bool pam_write(const char *filename, unsigned int width, unsigned int pitch, |
| 232 | + unsigned int height, int ch_count, int maxval, |
| 233 | + const unsigned char *data, bool pnm) |
| 234 | +{ |
231 | 235 | errno = 0; |
232 | 236 | #ifdef _WIN32 |
233 | 237 | FILE *file = _wfopen(mbs_to_wstr(filename), L"wb"); |
@@ -267,9 +271,19 @@ bool pam_write(const char *filename, unsigned int width, unsigned int height, in |
267 | 271 | "ENDHDR\n", |
268 | 272 | width, height, ch_count, maxval, tuple_type); |
269 | 273 | } |
270 | | - size_t len = (size_t) width * height * ch_count * (maxval <= 255 ? 1 : 2); |
| 274 | + const size_t linesize = (size_t) height * ch_count * (maxval <= 255 ? 1 : 2); |
| 275 | + const size_t len = width * linesize; |
271 | 276 | errno = 0; |
272 | | - size_t bytes_written = fwrite((const char *) data, 1, len, file); |
| 277 | + size_t bytes_written = 0; |
| 278 | + if (width == pitch) { |
| 279 | + bytes_written = fwrite((const char *) data, 1, len, file); |
| 280 | + } else { |
| 281 | + for (unsigned y = 0; y < height; ++y) { |
| 282 | + bytes_written += fwrite((const char *)data + |
| 283 | + ((size_t)y * pitch), |
| 284 | + 1, linesize, file); |
| 285 | + } |
| 286 | + } |
273 | 287 | if (bytes_written != len) { |
274 | 288 | fprintf(stderr, "Unable to write PAM/PNM data - length %zd, written %zd: %s\n", |
275 | 289 | len, bytes_written, strerror(errno)); |
|
0 commit comments