Skip to content

Commit 2352b8c

Browse files
committed
pam: sync with new changes
support for pitch (writer) - not by GPUJPEG used (yet?)
1 parent 9b5d9c5 commit 2352b8c

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

src/utils/image_delegate.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ int pampnm_save_delegate(const char *filename, const struct gpujpeg_image_parame
235235
return -1;
236236
}
237237

238-
bool ret = pam_write(filename, param_image->width, param_image->height, depth, 255, (const unsigned char *) data, pnm);
238+
const bool ret = pam_write(filename, param_image->width, param_image->width, param_image->height, depth, 255,
239+
(const unsigned char*)data, pnm);
239240
return ret ? 0 : -1;
240241
}
241242

src/utils/pam.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
#include <ctype.h>
3939
#include <errno.h>
40+
#include <stddef.h>
4041
#include <stdio.h>
4142
#include <stdlib.h>
4243
#include <string.h>
@@ -227,7 +228,10 @@ bool pam_read(const char *filename, struct pam_metadata *info, unsigned char **d
227228
return true;
228229
}
229230

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+
{
231235
errno = 0;
232236
#ifdef _WIN32
233237
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
267271
"ENDHDR\n",
268272
width, height, ch_count, maxval, tuple_type);
269273
}
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;
271276
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+
}
273287
if (bytes_written != len) {
274288
fprintf(stderr, "Unable to write PAM/PNM data - length %zd, written %zd: %s\n",
275289
len, bytes_written, strerror(errno));

src/utils/pam.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ struct pam_metadata {
5959
};
6060

6161
bool pam_read(const char *filename, struct pam_metadata *info, unsigned char **data, void *(*allocator)(size_t));
62-
bool pam_write(const char *filename, unsigned int width, unsigned int height,
63-
int ch_count, int maxval, const unsigned char *data, bool pnm);
62+
bool pam_write(const char *filename, unsigned int width, unsigned int pitch,
63+
unsigned int height, int ch_count, int maxval,
64+
const unsigned char *data, bool pnm);
6465

6566
#ifdef __cplusplus
6667
} // extern "C"

0 commit comments

Comments
 (0)