Credit Andrew - fix vorbis audio which was scratchy and ensure aging plugin does...
[goodguy/cinelerra.git] / cinelerra-5.1 / db / utils / pbm2key.C
1 #include<stdio.h>
2 #include<stdarg.h>
3 #include<time.h>
4 #include<math.h>
5
6 #include "tdb.h"
7 #include "s.C"
8
9 #define SWIDTH 80
10 #define SHEIGHT 45
11
12 double
13 mean(uint8_t *dat, int n)
14 {
15   int s = 0;
16   for( int i=0; i<n; ++i, ++dat ) s += *dat;
17   return (double)s / n;
18 }
19
20 double
21 variance(uint8_t *dat, double m, int n)
22 {
23   double ss = 0, s = 0;
24   for( int i=0; i<n; ++i, ++dat ) {
25     double dx = *dat-m;
26     s += dx;  ss += dx*dx;
27   }
28   return (ss - s*s / n) / (n-1);
29 }
30
31 double
32 std_dev(uint8_t *dat, double m, int n)
33 {
34   return sqrt(variance(dat,m,n));
35 }
36
37 double 
38 center(uint8_t *dat, int n, int stride)
39 {
40   int s = 0, ss = 0;
41   for( int i=0; i<n; dat+=stride ) { s += *dat;  ss += ++i * *dat; }
42   return s > 0 ? (double)ss / s : n / 2.;
43 }
44
45 void
46 centroid(uint8_t *dat, int w, int h, double *xx, double *yy)
47 {
48         double x = 0, y = 0;
49         uint8_t *dp = dat;
50         for( int i=h; --i>=0; dp+=w ) x += center(dp, w, 1);
51         for( int i=w; --i>=0; ) y += center(dat+i, h, w);
52         *xx = x / h;  *yy = y / w;
53 }
54
55
56 int main(int ac, char **av)
57 {
58   int ret;  setbuf(stdout,0);
59   theDb db;
60   db.open(av[1]);
61   //db.access(av[1], 34543, 0);
62   if( !db.opened() || db.error() ) exit(1);
63
64   FILE *fp = fopen(av[2],"r");
65   for( int i=3; --i>=0; ) {
66     for( int ch=fgetc(fp); ch>=0 && ch!='\n'; ch=fgetc(fp) );
67   }
68   
69   int w = SWIDTH, h = SHEIGHT, sfrm_sz = w * h;
70   uint8_t dat[sfrm_sz];
71   fread(dat,1,sfrm_sz,fp);
72
73   double mn = mean(dat,sfrm_sz);
74   double sd = std_dev(dat,mn,sfrm_sz);
75   double cx, cy;  centroid(dat, w, h, &cx, &cy);
76   double moment = cx + cy;
77   if( (ret = Video_frameLoc::ikey_Frame_weight(db.video_frame, mn).Locate()) != 0 ) {
78     printf(" not found, ret = %d\n",ret);
79     return 1;
80   }
81   printf(" id %d, mean %f-%f=%f, std_dev %f-%f=%f, "
82       " cx %f-%f=%f, cy %f-%f=%f, moment %f-%f=%f\n", db.video_frame.id(),
83     mn, db.video_frame.Frame_mean(), mn-db.video_frame.Frame_mean(),
84     sd, db.video_frame.Frame_std_dev(), sd-db.video_frame.Frame_std_dev(), 
85     cx, db.video_frame.Frame_cx(), cx-db.video_frame.Frame_cx(), 
86     cy, db.video_frame.Frame_cy(), cy-db.video_frame.Frame_cy(), 
87     moment, db.video_frame.Frame_moment(), moment-db.video_frame.Frame_moment());
88   if( (ret = Video_frameLoc::ikey_Frame_center(db.video_frame, moment).Locate()) != 0 ) {
89     printf(" not found, ret = %d\n",ret);
90     return 1;
91   }
92   printf(" id %d, mean %f-%f=%f, std_dev %f-%f=%f, "
93       " cx %f-%f=%f, cy %f-%f=%f, moment %f-%f=%f\n", db.video_frame.id(),
94     mn, db.video_frame.Frame_mean(), mn-db.video_frame.Frame_mean(),
95     sd, db.video_frame.Frame_std_dev(), sd-db.video_frame.Frame_std_dev(), 
96     cx, db.video_frame.Frame_cx(), cx-db.video_frame.Frame_cx(), 
97     cy, db.video_frame.Frame_cy(), cy-db.video_frame.Frame_cy(), 
98     moment, db.video_frame.Frame_moment(), moment-db.video_frame.Frame_moment());
99   db.close();
100   return 0;
101 }
102