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

Subversion Repositories or1k

[/] [or1k/] [tags/] [MW_0_8_9PRE7/] [mw/] [src/] [demos/] [nanox/] [demo6.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 673 markom
/*
2
 * Demonstrates loading a binary PPM file and displaying it in a window
3
 * as a Pixmap.
4
 */
5
 
6
/* Comment this definition out if you don't want to use server side pixmaps */
7
/* (it will be slower but will work on device drivers without bitblt) */
8
#define USE_PIXMAPS
9
 
10
#include <stdio.h>
11
#include <stdlib.h>
12
#include <string.h>
13
#include <errno.h>
14
#include <nano-X.h>
15
 
16
GR_WINDOW_ID window;    /* ID for output window */
17
#ifdef USE_PIXMAPS
18
GR_WINDOW_ID pmap;      /* ID for pixmap */
19
#endif
20
GR_GC_ID gc;            /* Graphics context */
21
int width, height;      /* Size of image */
22
unsigned char *data;    /* Local copy of image data */
23
 
24
void do_exposure(GR_EVENT_EXPOSURE *event)
25
{
26
        /* The window has been exposed so redraw it */
27
#ifdef USE_PIXMAPS
28
        GrCopyArea(window, gc, 0, 0, width, height, pmap, 0, 0, MWROP_SRCCOPY);
29
#else
30
        GrArea(window, gc, 0, 0, width, height, data, MWPF_RGB);
31
#endif
32
}
33
 
34
void errorhandler(GR_EVENT *ep)
35
{
36
        printf("Error (%s) code %d id %d", ep->error.name,
37
                ep->error.code, ep->error.id);
38
        exit(1);
39
}
40
 
41
int main(int argc, char *argv[])
42
{
43
        unsigned char line[256];
44
        GR_EVENT event;
45
        FILE *infile;
46
        int i, o;
47
        unsigned char *p;
48
 
49
        if(argc != 2) {
50
                printf("Usage: demo6 <filename.ppm>\n");
51
                exit(1);
52
        }
53
 
54
        if(!(infile = fopen(argv[1], "r"))) {
55
                printf("Couldn't open \"%s\" for reading: %s\n", argv[1],
56
                                                strerror(errno));
57
                exit(2);
58
        }
59
 
60
        /* Read magic number (P6 = colour, binary encoded PPM file) */
61
        if(!fgets(line, 256, infile)) goto truncated;
62
        if(line[0] != 'P' || line[1] != '6') {
63
                printf("Unsupported PPM type or not a PPM file.\n");
64
                printf("Please supply a valid P6 format file (colour, with "
65
                        "binary encoding).\n");
66
        }
67
 
68
        /* Strip comments */
69
        do {
70
                if(!fgets(line, 256, infile)) goto truncated;
71
        } while(line[0] == '#');
72
 
73
        /* Read width and height */
74
        sscanf(line, "%i %i", &width, &height);
75
 
76
        /* Read the maximum colour value */
77
        if(!fgets(line, 256, infile)) goto truncated;
78
        sscanf(line, "%i", &i);
79
        if(i != 255) {
80
                printf("Truecolour mode only is supported\n");
81
                exit(4);
82
        }
83
 
84
        /* Calculate how many bytes of image data there is */
85
        i = width * height * 3;
86
        /* Calculate how many bytes of data there will be after unpacking */
87
        o = width * height * 4;
88
 
89
        /* Allocate the space to store the data whilst it's being loaded */
90
        if(!(data = malloc(o))) {
91
                printf("Not enough memory to load image\n");
92
                exit(5);
93
        }
94
 
95
        /* Read the data in and unpack it to RGBX format */
96
        /* The lower byte isn't used so we don't set it to anything */
97
        p = data;
98
        while(o) {
99
                if(fread(p, 1, 3, infile) != 3) goto truncated;
100
                p += 4;
101
                o -= 4;
102
        }
103
 
104
        /* We don't need the input file anymore so close it */
105
        fclose(infile);
106
 
107
        /* Register the error handler */
108
        GrSetErrorHandler(errorhandler);
109
 
110
        if(GrOpen() < 0) {
111
                printf("Couldn't connect to Nano-X server\n");
112
                exit(6);
113
        }
114
 
115
#ifdef USE_PIXMAPS
116
        /* Create the pixmap to store the picture in */
117
        pmap = GrNewPixmap(width, height, NULL);
118
#endif
119
 
120
        /* Create a graphics context */
121
        gc = GrNewGC();
122
 
123
#ifdef USE_PIXMAPS
124
        /* Copy the image data into the pixmap */
125
        GrArea(pmap, gc, 0, 0, width, height, data, MWPF_RGB);
126
        /* We can free the image data now because it's stored in the pixmap */
127
        free(data);
128
#endif
129
 
130
        /* Create a window to output the image to */
131
        window = GrNewWindow(GR_ROOT_WINDOW_ID, 0, 0, width, height, 0, 0, 0);
132
 
133
        /* Select expose events so we can redraw the image when necessary */
134
        GrSelectEvents(window, GR_EVENT_MASK_EXPOSURE |
135
                                GR_EVENT_MASK_CLOSE_REQ);
136
 
137
        /* Make the window visible */
138
        GrMapWindow(window);
139
 
140
#ifdef USE_PIXMAPS
141
        /* Paint the pixmap onto it */
142
        GrCopyArea(window, gc, 0, 0, width, height, pmap, 0, 0,
143
                                                        MWROP_SRCCOPY);
144
#else
145
        GrArea(window, gc, 0, 0, width, height, data, MWPF_RGB);
146
#endif
147
 
148
        while(1) {
149
                GrGetNextEvent(&event);
150
                switch(event.type) {
151
                        case GR_EVENT_TYPE_EXPOSURE:
152
                                do_exposure(&event.exposure);
153
                                break;
154
                        case GR_EVENT_TYPE_CLOSE_REQ:
155
                                GrClose();
156
                                exit(0);
157
                }
158
        }
159
 
160
        return 0;
161
 
162
truncated:
163
        printf("Error: File appears to be truncated\n");
164
        exit(3);
165
}

powered by: WebSVN 2.1.0

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