summaryrefslogtreecommitdiffstats
path: root/test/test-server.c
blob: 91a3db538ad8f5ee56342163b4444c5633b7297c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "kiro-server.h"
#include "kiro-trb.h"
#include <gmodule.h>
#include <gio/gio.h>
#include <string.h>
#include <math.h>



static const char g_digits[10][20] = {
    /* 0 */
    {
        0x00, 0xff, 0xff, 0x00,
        0xff, 0x00, 0x00, 0xff,
        0xff, 0x00, 0x00, 0xff,
        0xff, 0x00, 0x00, 0xff,
        0x00, 0xff, 0xff, 0x00
    },
    /* 1 */
    {
        0x00, 0x00, 0xff, 0x00,
        0x00, 0xff, 0xff, 0x00,
        0x00, 0x00, 0xff, 0x00,
        0x00, 0x00, 0xff, 0x00,
        0x00, 0x00, 0xff, 0x00
    },
    /* 2 */
    {
        0x00, 0xff, 0xff, 0x00,
        0xff, 0x00, 0x00, 0xff,
        0x00, 0x00, 0xff, 0x00,
        0x00, 0xff, 0x00, 0x00,
        0xff, 0xff, 0xff, 0xff
    },
    /* 3 */
    {
        0x00, 0xff, 0xff, 0x00,
        0xff, 0x00, 0x00, 0xff,
        0x00, 0x00, 0xff, 0x00,
        0xff, 0x00, 0x00, 0xff,
        0x00, 0xff, 0xff, 0x00
    },
    /* 4 */
    {
        0xff, 0x00, 0x00, 0x00,
        0xff, 0x00, 0x00, 0xff,
        0xff, 0xff, 0xff, 0xff,
        0x00, 0x00, 0x00, 0xff,
        0x00, 0x00, 0x00, 0xff
    },
    /* 5 */
    {
        0xff, 0xff, 0xff, 0xff,
        0xff, 0x00, 0x00, 0x00,
        0x00, 0xff, 0xff, 0x00,
        0x00, 0x00, 0x00, 0xff,
        0xff, 0xff, 0xff, 0x00
    },
    /* 6 */
    {
        0x00, 0xff, 0xff, 0xff,
        0xff, 0x00, 0x00, 0x00,
        0xff, 0xff, 0xff, 0x00,
        0xff, 0x00, 0x00, 0xff,
        0x00, 0xff, 0xff, 0x00
    },
    /* 7 */
    {
        0xff, 0xff, 0xff, 0xff,
        0x00, 0x00, 0x00, 0xff,
        0x00, 0x00, 0xff, 0x00,
        0x00, 0xff, 0x00, 0x00,
        0xff, 0x00, 0x00, 0x00
    },
    /* 8 */
    {
        0x00, 0xff, 0xff, 0x00,
        0xff, 0x00, 0x00, 0xff,
        0x00, 0xff, 0xff, 0x00,
        0xff, 0x00, 0x00, 0xff,
        0x00, 0xff, 0xff, 0x00
    },
    /* 9 */
    {
        0x00, 0xff, 0xff, 0x00,
        0xff, 0x00, 0x00, 0xff,
        0x00, 0xff, 0xff, 0xff,
        0x00, 0x00, 0x00, 0xff,
        0xff, 0xff, 0xff, 0x00
    }
};

static const guint DIGIT_WIDTH = 4;
static const guint DIGIT_HEIGHT = 5;

static void
print_number (gchar *buffer, guint number, guint x, guint y, guint width)
{
    for (guint i = 0; i < DIGIT_WIDTH; i++) {
        for (guint j = 0; j < DIGIT_HEIGHT; j++) {
            char val = (char) g_digits[number][j * DIGIT_WIDTH + i];

            if (val != 0x00) {
                //This should make the frame counter appear in a bright yellow
                val = 0xBE;
            }

            buffer[ (y + j)*width + (x + i)] = (guint8) val;
        }
    }
}

static void
print_current_frame (gchar *buffer, guint number, guint width, guint height, GRand *rand)
{
    guint divisor = 10000000;
    int x = 1;

    while (divisor > 0) {
        print_number (buffer, number / divisor, x, 1, width);
        number = number % divisor;
        divisor = divisor / 10;
        x += DIGIT_WIDTH + 1;
    }

    //Grayscale pattern is the same for every row. Just calculate one single
    //Scanline, so we can reuse it and dont have to do the whole calculation
    //for every row again.
    char default_line[width];

    for (guint p = 0; p < width; p++) {
        default_line[p] = (char) ((p * 256) / (width));
    }

    //Use memcpy to quickly fill every row with the precalculated grayscale
    //pattern
    for (guint y = 16; y < height; y++) {
        guint index = y * width;
        memcpy (buffer + index, &default_line[0], width);
    }

    //This block will fill a square at the center of the image with normal
    //distributed random data
    const double mean = 128.0;
    const double std = 32.0;

    for (guint y = (height / 3); y < ((height * 2) / 3); y++) {
        guint row_start = y * width;

        for (guint i = (width / 3); i < ((width * 2) / 3); i++) {
            int index = row_start + i;
            double u1 = g_rand_double (rand);
            double u2 = g_rand_double (rand);
            double r = sqrt (-2 * log (u1)) * cos (2 * G_PI * u2);
            buffer[index] = (guint8) (r * std + mean);
        }
    }
}


int 
main (void)
{
    KiroServer *server = kiro_server_new ();
    KiroTrb *rb = kiro_trb_new ();
    kiro_trb_reshape (rb, 512 * 512, 15);
    GRand *rand = g_rand_new();

    if (0 > kiro_server_start (server, NULL, "60010", kiro_trb_get_raw_buffer (rb), kiro_trb_get_raw_size (rb))) {
        g_critical ("Failed to start server properly");
        goto done;
    }

    guint frame = 0;
    gchar *buffer = NULL;

    while (1) {
        buffer = kiro_trb_dma_push (rb);
        print_current_frame (buffer, frame, 512, 512, rand);
        frame++;
    }

done:
    g_rand_free (rand);
    kiro_trb_free (rb);
    kiro_server_free (server);
    return 0;
}