summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/pnm.c67
-rw-r--r--src/pnm.h13
-rw-r--r--src/pnmlcms.c58
3 files changed, 102 insertions, 36 deletions
diff --git a/src/pnm.c b/src/pnm.c
new file mode 100644
index 0000000..b2fa3c8
--- /dev/null
+++ b/src/pnm.c
@@ -0,0 +1,67 @@
+#include <string.h>
+
+#include "pnm.h"
+
+int
+readPnmHeader(FILE *fp, struct pnm *h) {
+ char line[1024];
+ char *lineptr;
+
+ memset(h, 0, sizeof(struct pnm));
+
+ lineptr = fgets(line, sizeof(line), fp);
+ if (!lineptr) {
+ fprintf(stderr, "premature end of pnm file\n");
+ return 1;
+ }
+
+ lineptr ++;
+
+ h->format = atoi(lineptr);
+ while (isdigit(*lineptr)) lineptr ++;
+
+ if (h->format == 7) lineptr = (char *)"";
+
+ while (lineptr != NULL && h->width == 0) {
+ if (*lineptr == '\0' || *lineptr == '#') {
+ lineptr = fgets(line, sizeof(line), fp);
+ } else if (isdigit(*lineptr)) {
+ h->width = strtol(lineptr, &lineptr, 10);
+ } else lineptr ++;
+ }
+
+ while (lineptr != NULL && h->height == 0) {
+ if (*lineptr == '\0' || *lineptr == '#') {
+ lineptr = fgets(line, sizeof(line), fp);
+ } else if (isdigit(*lineptr)) {
+ h->height = strtol(lineptr, &lineptr, 10);
+ } else lineptr ++;
+ }
+
+
+ if (h->format != 1 && h->format != 4) {
+ h->maxval = 0;
+
+ while (lineptr != NULL && h->maxval == 0) {
+ if (*lineptr == '\0' || *lineptr == '#') {
+ lineptr = fgets(line, sizeof(line), fp);
+ } else if (isdigit(*lineptr)) {
+ h->maxval = strtol(lineptr, &lineptr, 10);
+ } else lineptr ++;
+ }
+ } else {
+ h->maxval = 1;
+ }
+
+ return 0;
+}
+
+int
+writePnmHeader(FILE *fp, const struct pnm *h) {
+ fprintf(fp, "P%d\n", h->format);
+ fprintf(fp, "%d %d\n", h->width, h->height);
+ fprintf(fp, "%d\n", h->maxval);
+
+ return 0;
+}
+
diff --git a/src/pnm.h b/src/pnm.h
new file mode 100644
index 0000000..d2e9d01
--- /dev/null
+++ b/src/pnm.h
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+struct pnm {
+ int format;
+ int width;
+ int height;
+ int maxval;
+};
+
+
+int readPnmHeader(FILE *fp, struct pnm *h);
+
+int writePnmHeader(FILE *fp, const struct pnm *h);
diff --git a/src/pnmlcms.c b/src/pnmlcms.c
index 41b3047..bbb785a 100644
--- a/src/pnmlcms.c
+++ b/src/pnmlcms.c
@@ -1,8 +1,10 @@
-#include <pam.h>
#include <lcms.h>
+#include "pnm.h"
+
static int
-pam_transform(struct pam *inpam, struct pam *outpam, cmsHTRANSFORM *hTransform);
+pam_transform(FILE *in_fp, FILE *out_fp,
+ const struct pnm *in_pnm, cmsHTRANSFORM *hTransform);
int
main(int argc, char **argv) {
@@ -11,7 +13,7 @@ main(int argc, char **argv) {
double gamma = 0.0;
cmsHTRANSFORM hTransform;
cmsHPROFILE profiles[3];
- struct pam inpam, outpam;
+ struct pnm in_pnm;
while ((c = getopt(argc, argv, "i:o:g:")) != EOF) {
switch (c) {
@@ -57,20 +59,19 @@ main(int argc, char **argv) {
hTransform = cmsCreateMultiprofileTransform(
profiles,
nProf,
- TYPE_RGB_16,
- TYPE_RGB_16,
+ TYPE_RGB_16_SE,
+ TYPE_RGB_16_SE,
INTENT_PERCEPTUAL,
0);
- pm_init(argv[0], 0);
-
- pnm_readpaminit(stdin, &inpam, PAM_STRUCT_SIZE(tuple_type));
- outpam = inpam; outpam.file = stdout;
+ if (readPnmHeader(stdin, &in_pnm) != 0) {
+ exit(1);
+ }
- pnm_writepaminit(&outpam);
+ writePnmHeader(stdout, &in_pnm);
- pam_transform(&inpam, &outpam, &hTransform);
+ pam_transform(stdin, stdout, &in_pnm, &hTransform);
cmsDeleteTransform(hTransform);
@@ -82,38 +83,23 @@ main(int argc, char **argv) {
}
static int
-pam_transform(struct pam *inpam, struct pam *outpam, cmsHTRANSFORM *hTransform)
+pam_transform(FILE *in_fp, FILE *out_fp,
+ const struct pnm *in_pnm, cmsHTRANSFORM *hTransform)
{
- tuple *inrow, *outrow;
int row, col;
+ char *in_buf = malloc(in_pnm->width * 2 * 3);
+ char *out_buf = malloc(in_pnm->width * 2 * 3);
+ for (row = 0; row < in_pnm->height; row++) {
+ fread(in_buf, in_pnm->width, 2 * 3, in_fp);
- inrow = pnm_allocpamrow(inpam);
- outrow = pnm_allocpamrow(outpam);
-
- for (row = 0; row < inpam->height; row++) {
- pnm_readpamrow(inpam, inrow);
- for (col = 0; col < inpam->width; col++) {
- int i;
- unsigned short inpix[3], outpix[3];
-
- for (i = 0; i < 3; i++) {
- inpix[i] = (unsigned short) inrow[col][i];
- }
-
- cmsDoTransform(*hTransform, inpix, outpix, 1);
-
- for (i = 0; i < 3; i++) {
- outrow[col][i] = (sample) outpix[i];
- }
- }
-
+ cmsDoTransform(*hTransform, in_buf, out_buf, in_pnm->width);
- pnm_writepamrow(outpam, outrow);
+ fwrite(out_buf, in_pnm->width, 2 * 3, out_fp);
}
- pnm_freepamrow(inrow);
- pnm_freepamrow(outrow);
+ free(in_buf);
+ free(out_buf);
return 0;
}