add mask color radio btn sel, fix del all mask btn, fix mask dflt kfrm draw name...
[goodguy/cinelerra.git] / cinelerra-5.1 / db / utils / histplot.C
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4
5 void hist_eq(unsigned char *data, int len, int n)
6 {
7   while( --n >= 0 ) {
8     int hist[256];
9     for( int i=0; i<256; ++i ) hist[i] = 0;
10     unsigned char *bp = data;
11     for( int i=len; --i>=0; ++bp ) ++hist[*bp];
12     for( int t=0, i=0; i<256; ++i ) { t += hist[i];  hist[i] = t; }
13
14     int mn = 0;    while( mn<255 && !hist[mn] ) ++mn;
15     int mx = 256;  while( mx>mn && hist[mx-1]==len ) --mx;
16     double r = (double)(mx - mn) / (256-1);
17     int map[256];  map[0] = 0;  map[255] = 255;
18     double dv = (len-1) / 256., v = dv;
19     for( int i=1; i<255; ++i, v+=dv ) {
20       int j = i * r + mn;
21       map[i] = i * (1-(v - hist[j])/len);
22     }
23 //for( int i=0; i<256; ++i ) fprintf(stderr,"%d\n",map[i]);
24     bp = data;
25     for( int i=len; --i>=0; ++bp ) *bp = map[*bp];
26   }
27 }
28
29 int main(int ac, char **av)
30 {
31   FILE *ifp = !strcmp(av[1],"-") ? stdin : fopen(av[1],"r");
32   char line[120];
33   fgets(line,sizeof(line),ifp);
34   fgets(line,sizeof(line),ifp);
35   int w, h;  if( sscanf(line,"%d %d\n",&w,&h) != 2 ) exit(1);
36   fgets(line,sizeof(line),ifp);
37   int len = w*h;
38   unsigned char data[len], *bp = data;
39   for( int ch, i=len; --i>=0 && (ch=getc(ifp)) >= 0; ++bp ) *bp = ch;
40
41   int hist[256]; for( int i=0; i<256; ++i ) hist[i] = 0;
42   bp = data;     for( int i=len; --i>=0; ++bp ) ++hist[*bp];
43   for( int t=0, i=0; i<256; ++i ) { t += hist[i];  hist[i] = t; }
44   FILE *ofp = popen("gnuplot -e \"set terminal png; plot \\\"-\\\" with lines\" | xv - &","w");
45   for( int i=0; i<256; ++i ) fprintf(ofp,"%d\n",hist[i]);
46   fclose(ofp);
47   if( ifp != stdin ) fclose(ifp);
48   return 0;
49 }
50