export planes as 16 bits PGM, firt try

This commit is contained in:
tth 2022-02-28 20:00:20 +01:00
parent 6ffc08188d
commit c17132e969
5 changed files with 116 additions and 5 deletions

1
.gitignore vendored
View File

@ -8,6 +8,7 @@ lib/*.gif
*.a *.a
gmon.out gmon.out
cscope.out
*.swp *.swp

View File

@ -4,7 +4,7 @@
* http://la.buvette.org/photos/cumul * http://la.buvette.org/photos/cumul
*/ */
#define FIMG_VERSION 169 #define FIMG_VERSION 170
/* /*
* in memory descriptor * in memory descriptor
@ -154,6 +154,7 @@ int fimg_export_picture(FloatImg *pic, char *fname, int flags);
int fimg_save_as_pnm(FloatImg *head, char *fname, int flags); int fimg_save_as_pnm(FloatImg *head, char *fname, int flags);
int fimg_save_as_pgm(FloatImg *head, char *fname, int flags); int fimg_save_as_pgm(FloatImg *head, char *fname, int flags);
int fimg_load_from_pnm(char *fname, FloatImg *head, int notused); int fimg_load_from_pnm(char *fname, FloatImg *head, int notused);
int fimg_save_plane_as_pgm(FloatImg *psrc, char *fname, char plane);
double fimg_timer_set(int whot); double fimg_timer_set(int whot);
double fimg_timer_get(int whot); double fimg_timer_get(int whot);
@ -222,6 +223,7 @@ int fimg_save_as_exr(FloatImg *src, char *outname, int flags);
/* mathematics operations */ /* mathematics operations */
float fimg_get_plane_maxvalue(FloatImg *psrc, char plane);
float fimg_get_maxvalue(FloatImg *head); float fimg_get_maxvalue(FloatImg *head);
int fimg_get_minmax_rgb(FloatImg *head, float mmvals[6]); int fimg_get_minmax_rgb(FloatImg *head, float mmvals[6]);
int fimg_meanvalues(FloatImg *head, float means[4]); int fimg_meanvalues(FloatImg *head, float means[4]);

View File

@ -16,6 +16,41 @@
extern int verbosity; /* must be declared around main() */ extern int verbosity; /* must be declared around main() */
/* ---------------------------------------------------------------- */
/* nouveau 27 fevrier 2022 */
float fimg_get_plane_maxvalue(FloatImg *psrc, char plane)
{
float *ptrplane;
float maxval;
int area, foo;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p '%c' )\n", __func__, psrc, 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: plane error\n", __func__);
abort(); break;
}
area = psrc->width * psrc->height;
maxval = 0.0;
for (foo=0; foo<area; foo++) {
if (ptrplane[foo] > maxval) maxval = ptrplane[foo];
}
return maxval;
}
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
float fimg_get_maxvalue(FloatImg *head) float fimg_get_maxvalue(FloatImg *head)
{ {

View File

@ -83,6 +83,10 @@ return 0;
} }
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
/*
* this func write the content of the R channel, the
* only one used by graylevel images
*/
static void dump_gray_values(FILE *fp, FloatImg *picz, float fk) static void dump_gray_values(FILE *fp, FloatImg *picz, float fk)
{ {
int cnt, sz, value; int cnt, sz, value;
@ -112,7 +116,7 @@ int Rv, Gv, Bv;
cnt = 0; cnt = 0;
sz = picz->width * picz->height; sz = picz->width * picz->height;
for (idx=0; idx<sz; idx++) { for (idx=0; idx<sz; idx++) {
if (fk > 0) { if (fk > 0) { /* why ? */
Rv = (int)(picz->R[idx] / fk); Rv = (int)(picz->R[idx] / fk);
Gv = (int)(picz->G[idx] / fk); Gv = (int)(picz->G[idx] / fk);
Bv = (int)(picz->B[idx] / fk); Bv = (int)(picz->B[idx] / fk);
@ -196,10 +200,60 @@ fputs("\n", fp); fclose(fp);
return 0; return 0;
} }
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
/* 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;
#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;
}
if (NULL==(fp=fopen(fname, "w"))) {
perror(fname);
return -2;
}
fprintf(fp, "P2\n%d\n%d\n65535\n\n", psrc->width, psrc->height);
area = psrc->width * psrc->height;
maxval = fimg_get_plane_maxvalue(psrc, plane);
fk = maxval / 65535.0;
fprintf(stderr, "%s: maxval of %c = %f\n", __func__, plane, maxval);
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 -1;
}
/* ---------------------------------------------------------------- */
/* nouveau 10 fevrier 2022 */ /* nouveau 10 fevrier 2022 */
int fimg_save_as_pgm(FloatImg *src, char *fname, int flags) int fimg_save_as_pgm(FloatImg *src, char *fname, int flags)
{ {
FILE *fp; FILE *fp;

21
lib/t.c
View File

@ -14,6 +14,25 @@
int verbosity; int verbosity;
/* ---------------------------------------------------------------- */
int essai_save_plane(int wot)
{
int foo;
FloatImg image;
fprintf(stderr, "-------- %s ( %d ) --------\n", __func__, wot);
foo = fimg_create_from_dump("grabbed.fimg", &image);
if (foo) {
fprintf(stderr, "err loading picz\n");
return -6;
}
foo = fimg_save_plane_as_pgm(&image, "red.pgm", 'r');
foo = fimg_save_plane_as_pgm(&image, "green.pgm", 'g');
foo = fimg_save_plane_as_pgm(&image, "blue.pgm", 'b');
return 0;
}
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
int essai_timer(int uuuh) int essai_timer(int uuuh)
{ {
@ -269,7 +288,7 @@ if (verbosity) {
fimg_print_sizeof(); fimg_print_sizeof();
} }
foo = essai_2gray(NULL, "quux"); foo = essai_save_plane(0);
fprintf(stderr, "retour essai -> %d\n", foo); fprintf(stderr, "retour essai -> %d\n", foo);
// foo = essai_clone_et_copy(0); // foo = essai_clone_et_copy(0);