FloatImg4PythonBinding/lib/fimg-pnm.c

197 lines
4.0 KiB
C
Raw Normal View History

2019-03-03 16:22:55 +01:00
/*
* fimg-pnm.c
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
2019-03-03 16:22:55 +01:00
#include "string.h"
#include "../floatimg.h"
extern int verbosity; /* must be declared around main() */
/* ---------------------------------------------------------------- */
/* nouveau juin 2019, pendant la Ravebish */
int fimg_load_from_pnm(char *fname, FloatImg *head, int notused)
{
FILE *fp;
int width, height, maxval;
int foo, line, column;
2019-08-08 17:16:20 +02:00
unsigned char *buffline, *idxrd, dummychar;
float *Rptr, *Gptr, *Bptr;
if (NULL==head) {
fprintf(stderr, "%s : head ptr is %p\n", __func__, head);
return -8;
}
if (NULL==(fp=fopen(fname, "r"))) {
perror(fname);
exit(1);
}
foo = fscanf(fp, "P6 %d %d %d", &width, &height, &maxval);
if (3 != foo) {
fprintf(stderr, "%s : fscanf -> %d\n", __func__, foo);
return -1;
}
if (verbosity) {
fprintf(stderr, "%s is %dx%d , max=%d\n",fname, width, height, maxval);
}
if (NULL==(buffline=calloc(3, width))) {
fprintf(stderr, "%s on %s : memory error\n", __func__, fname);
return -2;
}
foo = fimg_create(head, width, height, 3);
if (foo) {
fprintf(stderr, "%s : create floatimg -> %d\n", __func__, foo);
exit(1);
}
2019-08-08 17:16:20 +02:00
#if DEBUG_LEVEL
fread(&dummychar, 1, 1, fp);
fprintf(stderr, "%s : dummychar %xx\n", __func__, dummychar);
#else
fseek(fp, 1L, SEEK_CUR); /* black magic */
2019-08-08 17:16:20 +02:00
#endif
Rptr = head->R; Gptr = head->G; Bptr = head->B;
for (line=0; line<height; line++) {
foo = fread(buffline, 3, width, fp);
// fprintf(stderr, "line %d read %d\n", line, width);
idxrd = buffline;
for (column=0; column<width; column++)
{
*Rptr++ = (float)*idxrd++;
*Gptr++ = (float)*idxrd++;
*Bptr++ = (float)*idxrd++;
}
}
fclose(fp);
return 0;
}
2019-03-03 16:22:55 +01:00
/* ---------------------------------------------------------------- */
2019-08-26 02:35:17 +02:00
static void dump_gray_values(FILE *fp, FloatImg *picz, float fk)
{
int cnt, sz, value;
int idx;
cnt = 0;
sz = picz->width * picz->height;
for (idx=0; idx<sz; idx++) {
if (fk > 0) value = (int)(picz->R[idx] / fk);
else value = 0;
cnt += fprintf(fp, "%d", value);
if (cnt > 70) {
fputs("\n", fp); cnt = 0;
}
else {
fputs(" ", fp); cnt++;
}
}
fputs("\n", fp);
}
/* ---------------------------------------------------------------- */
static void dump_rgb_values(FILE *fp, FloatImg *picz, float fk)
{
int cnt, sz, idx;
int Rv, Gv, Bv;
2019-03-03 16:22:55 +01:00
2019-08-26 02:35:17 +02:00
cnt = 0;
sz = picz->width * picz->height;
for (idx=0; idx<sz; idx++) {
if (fk > 0) {
Rv = (int)(picz->R[idx] / fk);
Gv = (int)(picz->G[idx] / fk);
Bv = (int)(picz->B[idx] / fk);
}
else {
Rv = Gv = Bv = 0;
}
cnt += fprintf(fp, "%d %d %d", Rv, Gv, Bv);
if (cnt > 60) {
fputs("\n", fp); cnt = 0;
}
else {
fputs(" ", fp); cnt++;
}
}
fputs("\n", fp);
}
/* ---------------------------------------------------------------- */
/*
2019-09-17 11:22:00 +02:00
* bit 0 of flags : use fvalue/count
2019-08-26 02:35:17 +02:00
*/
2019-09-17 11:22:00 +02:00
int fimg_save_as_pnm(FloatImg *head, char *fname, int flags)
2019-03-03 16:22:55 +01:00
{
FILE *fp;
float maximum, fk;
2019-08-26 02:35:17 +02:00
char *code;
2019-03-03 16:22:55 +01:00
#if DEBUG_LEVEL
2019-09-17 11:22:00 +02:00
fprintf(stderr, ">>> %-25s ( %p '%s' 0x%04x )\n", __func__, head,
fname, flags);
2019-03-03 16:22:55 +01:00
#endif
2019-08-26 02:35:17 +02:00
if ( head->type != FIMG_TYPE_RGB && head->type != FIMG_TYPE_GRAY) {
2019-03-03 16:22:55 +01:00
#if DEBUG_LEVEL
fprintf(stderr, "%s : type %d is bad.\n", __func__, head->type);
#endif
return -1;
}
if (NULL==(fp=fopen(fname, "w"))) {
perror(fname);
2019-08-26 02:35:17 +02:00
return -2;
2019-03-03 16:22:55 +01:00
}
2019-08-26 02:35:17 +02:00
switch(head->type) {
case FIMG_TYPE_GRAY: code = "P2"; break;
case FIMG_TYPE_RGB: code = "P3"; break;
}
fprintf(fp, "%s\n%d %d\n", code, head->width, head->height);
2019-03-03 16:22:55 +01:00
2019-09-17 11:22:00 +02:00
if ( flags & 1 ) {
fprintf(stderr, "%s using fval/count %f %d -> %f\n", __func__,
head->fval, head->count,
head->fval * head->count);
fk = (head->fval * head->count) / 65535.0;
}
else {
maximum = fimg_get_maxvalue(head);
fk = maximum / 65535.0;
fprintf(fp, "# maxval %15f\n# divisor %15f\n", maximum, fk);
}
2019-03-03 16:22:55 +01:00
fprintf(fp, "65535\n");
2019-08-26 02:35:17 +02:00
fflush(fp);
switch(head->type) {
case FIMG_TYPE_GRAY:
dump_gray_values(fp, head, fk);
break;
case FIMG_TYPE_RGB:
dump_rgb_values(fp, head, fk);
break;
2019-03-03 16:22:55 +01:00
}
2019-08-26 02:35:17 +02:00
2019-03-03 16:22:55 +01:00
fputs("\n", fp);
fclose(fp);
return 0;
}
/* ---------------------------------------------------------------- */