Compare commits

...

4 Commits

Author SHA1 Message Date
tth
1c47b50954 simplification 2019-08-27 16:21:40 +02:00
tth
e10f26df53 add a comment 2019-08-27 09:19:25 +02:00
tth
fe0f90af64 better support of gray level 2019-08-26 02:35:17 +02:00
tth
20d122890b simplified test prog 2019-08-26 01:47:05 +02:00
5 changed files with 112 additions and 71 deletions

25
essai.c
View File

@ -13,29 +13,6 @@
int verbosity;
/* --------------------------------------------------------------------- */
int add(FloatImg *a, FloatImg *b)
{
int x, y;
int offset;
double tb;
fimg_timer_set(0);
for (x=0; x<a->width; x++) {
for (y=0; y<a->height; y++) {
offset = x + (y * a->width);
a->R[offset] += b->R[offset];
a->G[offset] += b->G[offset];
a->B[offset] += b->B[offset];
}
}
tb = fimg_timer_get(0);
fprintf(stderr, "%s = %f seconds\n", __func__, tb);
return 0;
}
/* --------------------------------------------------------------------- */
void fait_un_dessin(char *fname)
{
@ -107,7 +84,7 @@ fimg_create(&fimgB, W, H, 3);
fimg_timer_set(0);
fimg_drand48(&fimgB, 100.0);
fimg_drand48(&fimgA, 100.0);
add(&fimgA, &fimgB);
fimg_add(&fimgA, &fimgB, &fimgA);
tb = fimg_timer_get(0);
fprintf(stderr, "%s = %f seconds\n", __func__, tb);
foo = fimg_save_as_pnm(&fimgA, "drand48.pnm", 0);

View File

@ -13,7 +13,7 @@ extern int verbosity; /* must be declared around main() */
/* --------------------------------------------------------------------- */
/*
* floating img MUST be allocated.
* floating imgs MUST be allocated before calling this func.
*/
int fimg_mk_gray_from(FloatImg *src, FloatImg*dst, int k)
{
@ -40,14 +40,13 @@ if (FIMG_TYPE_GRAY != dst->type) {
nbb = src->width * src->height;
for (foo=0; foo<nbb; foo++) {
dst->R[foo] = ( (src->R[foo] * kr) +
(src->G[foo] * kg) +
(src->B[foo] * kb) ) /
kdiv;
}
return -1;
return 0;
}
/* --------------------------------------------------------------------- */

View File

@ -20,7 +20,7 @@ float fimg_get_maxvalue(FloatImg *head)
float maxval;
int foo;
if (head->type != FIMG_TYPE_RGB) {
if (head->type != FIMG_TYPE_RGB && head->type != FIMG_TYPE_GRAY) {
fprintf(stderr, "%s : type %d invalide\n",
__func__, head->type);
return nanf("wtf ?");
@ -28,10 +28,17 @@ if (head->type != FIMG_TYPE_RGB) {
maxval = 0.0; /* no negative values allowed */
for (foo=0; foo<(head->width*head->height); foo++) {
if (head->R[foo] > maxval) maxval = head->R[foo];
if (head->G[foo] > maxval) maxval = head->G[foo];
if (head->B[foo] > maxval) maxval = head->B[foo];
switch (head->type) {
case FIMG_TYPE_RGB:
for (foo=0; foo<(head->width*head->height); foo++) {
if (head->R[foo] > maxval) maxval = head->R[foo];
if (head->G[foo] > maxval) maxval = head->G[foo];
if (head->B[foo] > maxval) maxval = head->B[foo];
}
case FIMG_TYPE_GRAY:
for (foo=0; foo<(head->width*head->height); foo++) {
if (head->R[foo] > maxval) maxval = head->R[foo];
}
}
return maxval;
@ -59,6 +66,9 @@ for (idx=0; idx<4; idx++) means[idx] /= (float)surface;
return 0;
}
/* ---------------------------------------------------------------- */
/*
* more elaborate functions are in fimg-2gray.c
*/
int fimg_to_gray(FloatImg *head)
{
float add;

View File

@ -82,19 +82,69 @@ return 0;
}
/* ---------------------------------------------------------------- */
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;
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);
}
/* ---------------------------------------------------------------- */
/*
*
*/
int fimg_save_as_pnm(FloatImg *head, char *fname, int notused)
{
FILE *fp;
float maximum, fk;
int idx, sz, Rv, Gv, Bv, foo;
char *code;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %-25s ( %p '%s' %d )\n", __func__, head,
fname, notused);
#endif
if (head->type != FIMG_TYPE_RGB) {
if ( head->type != FIMG_TYPE_RGB && head->type != FIMG_TYPE_GRAY) {
#if DEBUG_LEVEL
fprintf(stderr, "%s : type %d is bad.\n", __func__, head->type);
#endif
@ -103,36 +153,32 @@ if (head->type != FIMG_TYPE_RGB) {
if (NULL==(fp=fopen(fname, "w"))) {
perror(fname);
return -1;
return -2;
}
fprintf(fp, "P3\n%d %d\n", head->width, head->height);
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);
maximum = fimg_get_maxvalue(head);
fprintf(fp, "# maxval %15f\n", maximum);
fk = maximum / 65536.0;
fprintf(fp, "# divisor %15f\n", fk);
fprintf(fp, "65535\n");
sz = head->width*head->height;
fflush(fp);
foo = 0;
for (idx=0; idx<sz; idx++) {
if (fk > 0) {
Rv = (int)(head->R[idx] / fk);
Gv = (int)(head->G[idx] / fk);
Bv = (int)(head->B[idx] / fk);
}
else {
Rv = Gv = Bv = 0;
}
foo += fprintf(fp, "%d %d %d", Rv, Gv, Bv);
if (foo > 60) {
fputs("\n", fp); foo = 0;
}
else {
fputs(" ", fp); foo++;
}
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;
}
fputs("\n", fp);
fclose(fp);

43
lib/t.c
View File

@ -35,20 +35,39 @@ return -1;
int essai_2gray(FloatImg *picz, char *outname)
{
int foo;
FloatImg gray;
fprintf(stderr, ">>> %s ( %p '%s' )\n", __func__, picz, outname);
foo = fimg_create(&gray, picz->width, picz->height, FIMG_TYPE_GRAY);
if (foo) {
fprintf(stderr, "%s : err %d on fimg create\n", __func__, foo);
exit(1);
}
foo = fimg_mk_gray_from(picz, &gray, 0);
if (foo) {
fprintf(stderr, "%s : err %d on fimg mk_gray_from\n", __func__, foo);
exit(1);
}
return -1;
foo = fimg_save_as_pnm(&gray, outname, 0);
if (foo) {
fprintf(stderr, "%s : err %d on save_as_pnm\n", __func__, foo);
exit(1);
}
fimg_destroy(&gray);
return 0;
}
/* ---------------------------------------------------------------- */
#define W 4000
#define H 3000
#define W 2048
#define H 2048
int main(int argc, char *argv[])
{
int foo;
FloatImg dessin, noise, result;
int datas[3];
verbosity = 1;
@ -56,24 +75,14 @@ fimg_print_version(1);
foo = fimg_create(&dessin, W, H, 3);
petit_dessin(&dessin);
fimg_save_as_pnm(&dessin, "dessin.pnm", 0);
foo = fimg_create(&noise, W, H, 3);
fimg_drand48(&noise, 1.0);
fimg_save_as_pnm(&noise, "noise.pnm", 0);
fimg_drand48(&noise, 0.1);
foo = fimg_create(&result, W, H, 3);
foo = fimg_add(&dessin, &noise, &result);
fimg_save_as_pnm(&result, "r_add.pnm", 0);
foo = fimg_mul(&dessin, &noise, &result);
essai_2gray(&result, "gray.pnm");
foo = fimg_sub(&dessin, &noise, &result);
fimg_save_as_pnm(&result, "r_sub.pnm", 0);
foo = fimg_mul(&dessin, &noise, &result);
fimg_save_as_pnm(&result, "r_mul.pnm", 0);
fimg_destroy(&dessin), fimg_destroy(&noise), fimg_destroy(&result);
return 0;
}