00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 #include <inttypes.h>
00026 
00027 #include "utils.c"
00028 
00029 #define FIXP (1 << 16)
00030 #define MY_PI 205887 // (M_PI * FIX)
00031 
00032 static int64_t int_pow(int64_t a, int p)
00033 {
00034     int64_t v = FIXP;
00035 
00036     for (; p; p--) {
00037         v *= a;
00038         v /= FIXP;
00039     }
00040 
00041     return v;
00042 }
00043 
00044 static int64_t int_sin(int64_t a)
00045 {
00046     if (a < 0)
00047         a = MY_PI - a;  
00048     a %= 2 * MY_PI;     
00049 
00050     if (a >= MY_PI * 3 / 2)
00051         a -= 2 * MY_PI; 
00052     if (a >= MY_PI / 2)
00053         a = MY_PI - a;  
00054 
00055     return a - int_pow(a, 3) / 6 + int_pow(a, 5) / 120 - int_pow(a, 7) / 5040;
00056 }
00057 
00058 static unsigned char tab_r[256 * 256];
00059 static unsigned char tab_g[256 * 256];
00060 static unsigned char tab_b[256 * 256];
00061 
00062 static int h_cos[360];
00063 static int h_sin[360];
00064 
00065 static int ipol(uint8_t *src, int x, int y)
00066 {
00067     int int_x  = x >> 16;
00068     int int_y  = y >> 16;
00069     int frac_x = x & 0xFFFF;
00070     int frac_y = y & 0xFFFF;
00071     int s00    = src[( int_x      & 255) + 256 * ( int_y      & 255)];
00072     int s01    = src[((int_x + 1) & 255) + 256 * ( int_y      & 255)];
00073     int s10    = src[( int_x      & 255) + 256 * ((int_y + 1) & 255)];
00074     int s11    = src[((int_x + 1) & 255) + 256 * ((int_y + 1) & 255)];
00075     int s0     = (((1 << 16) - frac_x) * s00 + frac_x * s01) >> 8;
00076     int s1     = (((1 << 16) - frac_x) * s10 + frac_x * s11) >> 8;
00077 
00078     return (((1 << 16) - frac_y) * s0 + frac_y * s1) >> 24;
00079 }
00080 
00081 static void gen_image(int num, int w, int h)
00082 {
00083     const int c = h_cos[num % 360];
00084     const int s = h_sin[num % 360];
00085 
00086     const int xi = -(w / 2) * c;
00087     const int yi =  (w / 2) * s;
00088 
00089     const int xj = -(h / 2) * s;
00090     const int yj = -(h / 2) * c;
00091     int i, j;
00092 
00093     int x, y;
00094     int xprime = xj;
00095     int yprime = yj;
00096 
00097     for (j = 0; j < h; j++) {
00098         x       = xprime + xi + FIXP * w / 2;
00099         xprime += s;
00100 
00101         y       = yprime + yi + FIXP * h / 2;
00102         yprime += c;
00103 
00104         for (i = 0; i < w; i++) {
00105             x += c;
00106             y -= s;
00107             put_pixel(i, j,
00108                       ipol(tab_r, x, y),
00109                       ipol(tab_g, x, y),
00110                       ipol(tab_b, x, y));
00111         }
00112     }
00113 }
00114 
00115 #define W 256
00116 #define H 256
00117 
00118 static int init_demo(const char *filename)
00119 {
00120     int i, j;
00121     int h;
00122     int radian;
00123     char line[3 * W];
00124 
00125     FILE *input_file;
00126 
00127     input_file = fopen(filename, "rb");
00128     if (!input_file) {
00129         perror(filename);
00130         return 1;
00131     }
00132 
00133     if (fread(line, 1, 15, input_file) != 15)
00134         return 1;
00135     for (i = 0; i < H; i++) {
00136         if (fread(line, 1, 3 * W, input_file) != 3 * W)
00137             return 1;
00138         for (j = 0; j < W; j++) {
00139             tab_r[W * i + j] = line[3 * j    ];
00140             tab_g[W * i + j] = line[3 * j + 1];
00141             tab_b[W * i + j] = line[3 * j + 2];
00142         }
00143     }
00144     fclose(input_file);
00145 
00146     
00147     for (i = 0; i < 360; i++) {
00148         radian   = 2 * i * MY_PI / 360;
00149         h        = 2 * FIXP + int_sin(radian);
00150         h_cos[i] = h * int_sin(radian + MY_PI / 2) / 2 / FIXP;
00151         h_sin[i] = h * int_sin(radian)             / 2 / FIXP;
00152     }
00153 
00154     return 0;
00155 }
00156 
00157 int main(int argc, char **argv)
00158 {
00159     int w, h, i;
00160     char buf[1024];
00161     int isdir = 0;
00162 
00163     if (argc != 3) {
00164         printf("usage: %s image.pnm file|dir\n"
00165                "generate a test video stream\n", argv[0]);
00166         return 1;
00167     }
00168 
00169     if (!freopen(argv[2], "wb", stdout))
00170         isdir = 1;
00171 
00172     w = DEFAULT_WIDTH;
00173     h = DEFAULT_HEIGHT;
00174 
00175     rgb_tab = malloc(w * h * 3);
00176     wrap    = w * 3;
00177     width   = w;
00178     height  = h;
00179 
00180     if (init_demo(argv[1]))
00181         return 1;
00182 
00183     for (i = 0; i < DEFAULT_NB_PICT; i++) {
00184         gen_image(i, w, h);
00185         if (isdir) {
00186             snprintf(buf, sizeof(buf), "%s%02d.pgm", argv[2], i);
00187             pgmyuv_save(buf, w, h, rgb_tab);
00188         } else {
00189             pgmyuv_save(NULL, w, h, rgb_tab);
00190         }
00191     }
00192 
00193     free(rgb_tab);
00194     return 0;
00195 }