FloatImg4PythonBinding/lib/fimg-pnm.c

322 lines
6.7 KiB
C
Raw Normal View History

2019-03-03 16:22:55 +01:00
/*
* fimg-pnm.c
*
2021-05-20 09:31:28 +02:00
* crude version who make HUGE ascii files !
2019-03-03 16:22:55 +01:00
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
2021-05-20 09:31:28 +02:00
#include <stdint.h>
2021-05-20 09:31:28 +02:00
#include <string.h>
2019-03-03 16:22:55 +01:00
#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
/* ---------------------------------------------------------------- */
2022-02-28 20:00:20 +01:00
/*
* this func write the content of the R channel, the
* only one used by graylevel images
*/
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++) {
2022-02-28 20:00:20 +01:00
if (fk > 0) { /* why ? */
2019-08-26 02:35:17 +02:00
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 ) {
fk = (head->fval * head->count) / 65535.0;
2019-09-29 00:12:31 +02:00
if (verbosity > 1) {
fprintf(stderr, "%s using fval/count %f %d -> %f\n",
__func__,
head->fval, head->count, fk);
}
2019-09-17 13:14:33 +02:00
fprintf(fp, "# fval/count %f %d\n", head->fval, head->count);
2019-09-17 11:22:00 +02:00
}
else {
maximum = fimg_get_maxvalue(head);
fk = maximum / 65535.0;
2019-09-17 13:14:33 +02:00
fprintf(fp, "# maxval %15f\n# divisor %15f\n", maximum, fk);
2019-09-17 11:22:00 +02:00
}
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
2021-03-17 18:32:51 +01:00
fputs("\n", fp); fclose(fp);
2019-03-03 16:22:55 +01:00
return 0;
}
/* ---------------------------------------------------------------- */
2022-02-28 20:00:20 +01:00
/* nouveau 27 fevrier 2022 */
/* WARNING ! UGLY CODE INSIDE */
int fimg_save_plane_as_pgm(FloatImg *psrc, char *fname, char plane)
{
FILE *fp;
float maxval, fk, *ptrplane;
int area, idx, printed;
2022-02-09 23:21:58 +01:00
2022-02-28 20:00:20 +01:00
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %s '%c' )\n", __func__, psrc, fname, plane);
#endif
switch (plane) {
case 'r': case 'R':
ptrplane = psrc->R; break;
case 'g': case 'G':
ptrplane = psrc->G; break;
case 'b': case 'B':
ptrplane = psrc->B; break;
case 'a': case 'A':
ptrplane = psrc->A; break;
default:
fprintf(stderr, "%s: bad plane '%c'\n", __func__, plane);
abort(); break;
}
2022-03-06 04:47:58 +01:00
if (NULL == ptrplane) { /* mmmm... */
fprintf(stderr, "%s: mmmm...\n", __func__);
return -3;
}
2022-02-28 20:00:20 +01:00
if (NULL==(fp=fopen(fname, "w"))) {
perror(fname);
return -2;
}
2022-03-06 04:47:58 +01:00
/* WARNING !
* some software (yes, povray, I look at you) doesn't like
* width and height on two separate lines.
*/
fprintf(fp, "P2\n%d %d\n65535\n\n", psrc->width, psrc->height);
2022-03-06 04:47:58 +01:00
2022-02-28 20:00:20 +01:00
area = psrc->width * psrc->height;
maxval = fimg_get_plane_maxvalue(psrc, plane);
fk = maxval / 65535.0;
2022-03-06 04:47:58 +01:00
if (verbosity) {
fprintf(stderr, "%s: maxval of plane '%c' = %f\n", __func__,
plane, maxval);
}
2022-02-09 23:21:58 +01:00
2022-02-28 20:00:20 +01:00
printed = 0;
for (idx=0; idx<area; idx++) {
printed += fprintf(fp, "%d ", (int)(ptrplane[idx]/fk));
if (printed > 72) {
fputs("\n", fp);
printed = 0;
}
}
fclose(fp);
return 0;
2022-02-28 20:00:20 +01:00
}
/* ---------------------------------------------------------------- */
/* nouveau 10 fevrier 2022 */
2022-02-09 23:21:58 +01:00
int fimg_save_as_pgm(FloatImg *src, char *fname, int flags)
{
FILE *fp;
float maximum, fk;
int area, idx, printed;
float accu;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %s %d )\n", __func__, src, fname, flags);
#endif
2022-03-06 04:47:58 +01:00
if (flags) {
fprintf(stderr, "%s: flags must be 0\n", __func__);
}
2022-02-09 23:21:58 +01:00
if ( src->type != FIMG_TYPE_RGB ) {
#if DEBUG_LEVEL
fprintf(stderr, "%s : type %d is bad.\n", __func__, src->type);
#endif
return -1;
}
if (NULL==(fp=fopen(fname, "w"))) {
perror(fname);
return -2;
}
2022-03-06 04:47:58 +01:00
/* WARNING !
* some software (yes, povray, I look at you) doesn't like
* width and height on two separate lines.
*/
2022-03-10 16:59:37 +01:00
fprintf(fp, "P2\n%d %d\n65535\n\n", src->width, src->height);
2022-02-09 23:21:58 +01:00
area = src->width * src->height;
maximum = fimg_get_maxvalue(src);
fk = maximum / 65535.0;
printed = 0;
for (idx=0; idx<area; idx++) {
accu = (src->R[idx] + src->G[idx] + src->B[idx]) / 3.0;
printed += fprintf(fp, "%d ", (int)(accu/fk));
if (printed > 72) {
fputs("\n", fp);
printed = 0;
}
}
fclose(fp);
return 0;
}