Credit Andrew - fix vorbis audio which was scratchy and ensure aging plugin does...
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / test4.C
1 /*
2  * CINELERRA
3  * Copyright (C) 2015-2020 William Morrow
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published
7  * by the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20
21
22 //c++ -g -I../guicast test4.C ../guicast/x86_64/libguicast.a \
23 // -DHAVE_GL -DHAVE_XFT -I/usr/include/freetype2 -lGL -lX11 -lXext \
24 // -lXinerama -lXv -lpng  -lfontconfig -lfreetype -lXft -pthread
25
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33
34 #include "bcwindowbase.h"
35 #include "bcwindow.h"
36 #include "bcsignals.h"
37 #include "bccolors.h"
38 #include "fonts.h"
39 #include "thread.h"
40 #include "vframe.h"
41
42 class TestWindowGUI : public BC_Window
43 {
44 public:
45
46         TestWindowGUI(int x, int y, int w, int h);
47         ~TestWindowGUI();
48 };
49
50
51 class TestWindow : public Thread
52 {
53         TestWindowGUI *gui;
54 public:
55         TestWindow(int x, int y, int w, int h)
56          : Thread(1,0,0) {
57                 gui = new TestWindowGUI(x,y,w,h);
58                 start();
59         }
60         void draw(VFrame *vframe) {
61                 gui->draw_vframe(vframe,
62                         0,0,gui->get_w(),gui->get_h(),
63                         0,0,vframe->get_w(),vframe->get_h(),
64                         0);
65                 gui->flash();
66         }
67         void show_text(int tx, int ty, const char *fmt, ...);
68         void close_window() { gui->close(0); }
69         ~TestWindow() { delete gui; }
70         void run() { gui->run_window(); }
71 };
72
73 TestWindowGUI::
74 TestWindowGUI(int x, int y, int w, int h)
75  : BC_Window("test", x,y, w,h, 100,100)
76 {
77         set_bg_color(BLACK);
78         clear_box(0,0,get_w(),get_h());
79         flash();
80         set_font(MEDIUMFONT);
81 }
82
83 TestWindowGUI::
84 ~TestWindowGUI()
85 {
86 }
87
88 void TestWindow::show_text(int tx, int ty, const char *fmt, ...)
89 {
90         char text[1024];
91         va_list ap;
92         va_start(ap, fmt);
93         vsprintf(text, fmt, ap);
94         va_end(ap);
95         gui->set_color(0xc080f0);
96         gui->draw_text(tx,ty, text);
97         gui->flash();
98 }
99
100 const char *cmdl[] = {
101  "transparency", "compressed", "rgb8", "rgb565", "bgr565", "bgr888", "bgr8888", "yuv420p",
102  "yuv422p", "rgb888", "rgba8888", "rgb161616", "rgba16161616", "yuv888", "yuva8888", "yuv161616",
103  "yuva16161616", "yuv411p", "uvy422", "yuv422", "argb8888", "abgr8888", "a8", "a16",
104  "yuv101010", "vyu888", "uyva8888", "yuv444p", "yuv410p", "rgb_float", "rgba_float", "a_float",
105  "rgb_floatp", "rgba_floatp", "yuv420pi", "ayuv16161616", "grey8", "grey16", "gbrp",
106 };
107
108 void write_pgm(uint8_t *tp, int w, int h, const char *fmt, ...)
109 {
110   va_list ap;    va_start(ap, fmt);
111   char fn[256];  vsnprintf(fn, sizeof(fn), fmt, ap);
112   va_end(ap);
113   FILE *fp = !strcmp(fn,"-") ? stdout : fopen(fn,"w");
114   if( fp ) {
115     fprintf(fp,"P5\n%d %d\n255\n",w,h);
116     fwrite(tp,w,h,fp);
117     fclose(fp);
118   }
119 }
120
121 void write_ppm(uint8_t *tp, int w, int h, const char *fmt, ...)
122 {
123   va_list ap;    va_start(ap, fmt);
124   char fn[256];  vsnprintf(fn, sizeof(fn), fmt, ap);
125   va_end(ap);
126   FILE *fp = !strcmp(fn,"-") ? stdout : fopen(fn,"w");
127   if( fp ) {
128     fprintf(fp,"P6\n%d %d\n255\n",w,h);
129     fwrite(tp,3*w,h,fp);
130     if( fp != stdout ) fclose(fp);
131   }
132 }
133
134 int64_t tm = 0, tn = 0;
135
136 static int diff_vframe(VFrame &afrm, VFrame &bfrm)
137 {
138   int n = 0, m = 0;
139   int w = afrm.get_w(), h = afrm.get_h();
140   uint8_t **arows = afrm.get_rows();
141   uint8_t **brows = bfrm.get_rows();
142
143   for( int y=0; y<h; ++y ) {
144     uint8_t *ap = arows[y], *bp = brows[y];
145     for( int x=0; x<w; ++x ) {
146       for( int i=0; i<3; ++i ) {
147         int d = *ap++ - *bp++;
148         m += d;
149         if( d < 0 ) d = -d;
150         n += d;
151       }
152     }
153   }
154   int sz = h*w*3;
155   printf(" %d %d %f", m, n, (double)n/sz);
156   tm += m;  tn += n;
157   return n;
158 }
159
160 int main(int ac, char **av)
161 {
162         BC_Signals signals;
163         signals.initialize();
164         int fd = open("test.png",O_RDONLY);
165         if( fd < 0 ) exit(1);
166         struct stat st;  fstat(fd,&st);
167         unsigned char *dat = new unsigned char[st.st_size];
168         read(fd, dat, st.st_size);
169         VFramePng ifrm(dat, st.st_size);
170         delete [] dat;
171         close(fd);
172         int w = ifrm.get_w(), h = ifrm.get_h();
173         TestWindow test_window(100, 100, w, h);
174         for( int fr_cmdl=1; fr_cmdl<=38; ++fr_cmdl ) {
175                 if( fr_cmdl == BC_TRANSPARENCY || fr_cmdl == BC_COMPRESSED ) continue;
176                 if( fr_cmdl == BC_A8 || fr_cmdl == BC_A16 ) continue;
177                 if( fr_cmdl == BC_A_FLOAT || fr_cmdl == 8 ) continue;
178                 VFrame afrm(w, h, fr_cmdl, -1);
179                 afrm.transfer_from(&ifrm, 0);
180                 for( int to_cmdl=1; to_cmdl<=38; ++to_cmdl ) {
181                         if( to_cmdl == BC_TRANSPARENCY || to_cmdl == BC_COMPRESSED ) continue;
182                         if( to_cmdl == BC_A8 || to_cmdl == BC_A16 ) continue;
183                         if( to_cmdl == BC_A_FLOAT || to_cmdl == 8 ) continue;
184                         printf("xfer_%s_to_%s ", cmdl[fr_cmdl],cmdl[to_cmdl]);
185                         VFrame bfrm(w, h, to_cmdl, -1);
186                         bfrm.transfer_from(&afrm, 0);
187                         test_window.draw(&bfrm);
188                         VFrame cfrm(w, h, BC_RGB888, -1);
189                         cfrm.transfer_from(&bfrm, 0);
190                         test_window.show_text(50,50, "xfer_%s_to_%s",cmdl[fr_cmdl],cmdl[to_cmdl]);
191                         write_ppm(cfrm.get_data(), w,h, "/tmp/test/xfer_%s_to_%s.pgm",
192                                 cmdl[fr_cmdl],cmdl[to_cmdl]);
193                         diff_vframe(ifrm, cfrm);
194 //                      usleep(100000);
195                         printf("\n");
196                 }
197         }
198         test_window.close_window();
199         test_window.join();
200         return 0;
201 }
202