OpenCores
URL https://opencores.org/ocsvn/warp/warp/trunk

Subversion Repositories warp

[/] [warp/] [test/] [vpi_images.c] - Blame information for rev 7

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 7 lekernel
/*
2
 * Milkymist VJ SoC
3
 * Copyright (C) 2007, 2008, 2009 Sebastien Bourdeauducq
4
 *
5
 * This program is free and excepted software; you can use it, redistribute it
6
 * and/or modify it under the terms of the Exception General Public License as
7
 * published by the Exception License Foundation; either version 2 of the
8
 * License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful, but WITHOUT
11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12
 * FOR A PARTICULAR PURPOSE. See the Exception General Public License for more
13
 * details.
14
 *
15
 * You should have received a copy of the Exception General Public License along
16
 * with this project; if not, write to the Exception License Foundation.
17
 */
18
 
19
#include <vpi_user.h>
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <gd.h>
23
 
24
static gdImagePtr src;
25
static gdImagePtr dst;
26
 
27
/*
28
 * Open both input and output images.
29
 *
30
 * Use from Verilog:
31
 * call $image_open at the beginning of the testbench.
32
 */
33
static int open_calltf(char *user_data)
34
{
35
        FILE *fd;
36
 
37
        fd = fopen("lena.jpg", "rb");
38
        if(fd == NULL) {
39
                perror("Unable to open input picture");
40
                exit(1);
41
        }
42
        src = gdImageCreateFromJpeg(fd);
43
        if(src == NULL) {
44
                fprintf(stderr, "Unable to read input picture\n");
45
                exit(1);
46
        }
47
        fclose(fd);
48
 
49
        dst = gdImageCreateTrueColor(src->sx, src->sy);
50
        if(src == NULL) {
51
                fprintf(stderr, "Unable to create output picture\n");
52
                exit(1);
53
        }
54
        return 0;
55
}
56
 
57
/*
58
 * Get a RGB565 pixel from the source image.
59
 *
60
 * Use from Verilog:
61
 * $image_get(x, y, color);
62
 * color will be modified to reflect the
63
 * pixel's color ; x and y are unmodified.
64
 */
65
static int get_calltf(char *user_data)
66
{
67
        vpiHandle sys;
68
        vpiHandle argv;
69
        vpiHandle item;
70
        s_vpi_value value;
71
        s_vpi_vecval vec;
72
        unsigned int x, y;
73
        unsigned int c;
74
        unsigned int red, green, blue;
75
        unsigned int r;
76
 
77
        sys = vpi_handle(vpiSysTfCall, 0);
78
        argv = vpi_iterate(vpiArgument, sys);
79
 
80
        /* get x */
81
        item = vpi_scan(argv);
82
        value.format = vpiIntVal;
83
        vpi_get_value(item, &value);
84
        x = value.value.integer;
85
 
86
        /* get y */
87
        item = vpi_scan(argv);
88
        value.format = vpiIntVal;
89
        vpi_get_value(item, &value);
90
        y = value.value.integer;
91
 
92
        /* do the job */
93
        c = gdImageGetTrueColorPixel(src, x, y);
94
        red = gdTrueColorGetRed(c);
95
        green = gdTrueColorGetGreen(c);
96
        blue = gdTrueColorGetBlue(c);
97
        r = ((red & 0xf8) << 8) | ((green & 0xfc) << 3) | ((blue & 0xf8) >> 3);
98
 
99
        /* write to the destination */
100
        item = vpi_scan(argv);
101
        value.format = vpiVectorVal;
102
        vec.aval = r;
103
        vec.bval = 0;
104
        value.value.vector = &vec;
105
        vpi_put_value(item, &value, 0, vpiNoDelay);
106
 
107
        vpi_free_object(argv);
108
        return 0;
109
}
110
 
111
/*
112
 * Set a RGB565 pixel in the destination image.
113
 *
114
 * Use from Verilog:
115
 * $image_get(x, y, color);
116
 * Parameters are not modified.
117
 */
118
static int set_calltf(char *user_data)
119
{
120
        vpiHandle sys;
121
        vpiHandle argv;
122
        vpiHandle item;
123
        s_vpi_value value;
124
        unsigned int x, y;
125
        unsigned int c;
126
        unsigned int red, green, blue;
127
 
128
        sys = vpi_handle(vpiSysTfCall, 0);
129
        argv = vpi_iterate(vpiArgument, sys);
130
 
131
        /* get x */
132
        item = vpi_scan(argv);
133
        value.format = vpiIntVal;
134
        vpi_get_value(item, &value);
135
        x = value.value.integer;
136
 
137
        /* get y */
138
        item = vpi_scan(argv);
139
        value.format = vpiIntVal;
140
        vpi_get_value(item, &value);
141
        y = value.value.integer;
142
 
143
        /* get color */
144
        item = vpi_scan(argv);
145
        value.format = vpiIntVal;
146
        vpi_get_value(item, &value);
147
        c = value.value.integer;
148
 
149
        vpi_free_object(argv);
150
 
151
        /* do the job */
152
        /* extract raw 5, 6 and 5-bit components */
153
        red = (c & 0xf800) >> 11;
154
        green = (c & 0x07e0) >> 5;
155
        blue = c & 0x001f;
156
 
157
        /* shift right and complete with MSBs */
158
        red = (red << 3) | ((red & 0x1c) >> 2);
159
        green = (green << 2) | ((green & 0x30) >> 4);
160
        blue = (blue << 3) | ((blue & 0x1c) >> 2);
161
 
162
        //printf("Set Pixel (%d,%d), %02x%02x%02x\n", x, y, red, green, blue);
163
 
164
        gdImageSetPixel(dst, x, y,
165
                gdImageColorAllocate(dst, red, green, blue));
166
 
167
        return 0;
168
}
169
 
170
/*
171
 * Close both input and output images.
172
 *
173
 * Use from Verilog:
174
 * call $image_close at the end of the testbench.
175
 * Don't forget it, or the destination file will
176
 * not be written !
177
 */
178
static int close_calltf(char *user_data)
179
{
180
        FILE *fd;
181
 
182
        gdImageDestroy(src);
183
 
184
        fd = fopen("out.png", "wb");
185
        gdImagePng(dst, fd);
186
        fclose(fd);
187
        gdImageDestroy(dst);
188
        return 0;
189
}
190
 
191
void vpi_register()
192
{
193
        s_vpi_systf_data tf_data;
194
 
195
        tf_data.type      = vpiSysTask;
196
        tf_data.tfname    = "$image_open";
197
        tf_data.calltf    = open_calltf;
198
        tf_data.compiletf = 0;
199
        tf_data.sizetf    = 0;
200
        tf_data.user_data = 0;
201
        vpi_register_systf(&tf_data);
202
 
203
        tf_data.type      = vpiSysTask;
204
        tf_data.tfname    = "$image_get";
205
        tf_data.calltf    = get_calltf;
206
        tf_data.compiletf = 0;
207
        tf_data.sizetf    = 0;
208
        tf_data.user_data = 0;
209
        vpi_register_systf(&tf_data);
210
 
211
        tf_data.type      = vpiSysTask;
212
        tf_data.tfname    = "$image_set";
213
        tf_data.calltf    = set_calltf;
214
        tf_data.compiletf = 0;
215
        tf_data.sizetf    = 0;
216
        tf_data.user_data = 0;
217
        vpi_register_systf(&tf_data);
218
 
219
        tf_data.type      = vpiSysTask;
220
        tf_data.tfname    = "$image_close";
221
        tf_data.calltf    = close_calltf;
222
        tf_data.compiletf = 0;
223
        tf_data.sizetf    = 0;
224
        tf_data.user_data = 0;
225
        vpi_register_systf(&tf_data);
226
 
227
        printf("PLI Image I/O functions registered\n");
228
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.