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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [services/] [gfx/] [mw/] [v2_0/] [src/] [engine/] [devopen.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
/*
2
 * Copyright (c) 1999, 2000, 2001 Greg Haerr <greg@censoft.com>
3
 * Portions Copyright (c) 1991 David I. Bell
4
 * Permission is granted to use, distribute, or modify this source,
5
 * provided that this copyright notice remains intact.
6
 *
7
 * Device-independent mid level screen device init routines
8
 *
9
 * These routines implement the smallest Microwindows engine level
10
 * interface to the screen driver.  By setting the NOFONTSORCLIPPING
11
 * config option, only these routines will be included, which can
12
 * be used to generate a low-level interface to the screen drivers
13
 * without dragging in any other GdXXX routines.
14
 */
15
#include <stdio.h>
16
#include <stdlib.h>
17
#include "device.h"
18
#include "swap.h"
19
 
20
#if MSDOS | ELKS
21
#define NOSTDPAL8
22
#endif
23
 
24
/*
25
 * The following define can change depending on the window manager
26
 * usage of colors and layout of the 8bpp palette devpal8.c.
27
 * Color entries below this value won't be overwritten by user
28
 * programs or bitmap display conversion tables.
29
 */
30
#define FIRSTUSERPALENTRY       24  /* first writable pal entry over 16 color*/
31
 
32
       MWPIXELVAL gr_foreground;      /* current foreground color */
33
       MWPIXELVAL gr_background;      /* current background color */
34
       MWBOOL   gr_usebg;           /* TRUE if background drawn in pixmaps */
35
       int      gr_mode = MWMODE_COPY;      /* drawing mode */
36
/*static*/ MWPALENTRY   gr_palette[256];    /* current palette*/
37
/*static*/ int  gr_firstuserpalentry;/* first user-changable palette entry*/
38
/*static*/ int  gr_nextpalentry;    /* next available palette entry*/
39
static int      gr_pixtype;         /* screen pixel format*/
40
static long     gr_ncolors;         /* screen # colors*/
41
 
42
/*
43
 * Open low level graphics driver
44
 */
45
PSD
46
GdOpenScreen(void)
47
{
48
        PSD                     psd;
49
        MWPALENTRY *            stdpal;
50
        MWSCREENINFO            sinfo;
51
 
52
        psd = scrdev.Open(&scrdev);
53
        if (!psd)
54
                return NULL;
55
        GdGetScreenInfo(psd, &sinfo);
56
        gr_pixtype = sinfo.pixtype;
57
        gr_ncolors = sinfo.ncolors;
58
 
59
        /* assume no user changable palette entries*/
60
        gr_firstuserpalentry = (int)psd->ncolors;
61
 
62
        /* set palette according to system colors and devpalX.c*/
63
        switch((int)psd->ncolors) {
64
 
65
#if !defined(NOSTDPAL1) /* don't require stdpal1 if not needed */
66
        case 2:         /* 1bpp*/
67
        {
68
                extern MWPALENTRY       mwstdpal1[2];
69
                stdpal = mwstdpal1;
70
        }
71
        break;
72
#endif
73
 
74
#if !defined(NOSTDPAL2) /* don't require stdpal2 if not needed */
75
        case 4:         /* 2bpp*/
76
        {
77
                extern MWPALENTRY       mwstdpal2[4];
78
                stdpal = mwstdpal2;
79
        }
80
        break;
81
#endif
82
 
83
#if !defined(NOSTDPAL4)
84
        /* don't require stdpal4 if not needed */
85
        case 8:         /* 3bpp - not fully supported*/
86
        case 16:        /* 4bpp*/
87
        {
88
                extern MWPALENTRY       mwstdpal4[16];
89
                stdpal = mwstdpal4;
90
        }
91
        break;
92
#endif
93
 
94
#if !defined(NOSTDPAL8) /* don't require large stdpal8 if not needed */
95
        case 256:       /* 8bpp*/
96
        {
97
                extern MWPALENTRY       mwstdpal8[256];
98
#if xxxALPHABLEND
99
                /* don't change uniform palette if alpha blending*/
100
                gr_firstuserpalentry = 256;
101
#else
102
                /* start after last system-reserved color*/
103
                gr_firstuserpalentry = FIRSTUSERPALENTRY;
104
#endif
105
                stdpal = mwstdpal8;
106
        }
107
        break;
108
#endif  /* !defined(NOSTDPAL8)*/
109
 
110
        default:        /* truecolor*/
111
                /* no palette*/
112
                gr_firstuserpalentry = 0;
113
                stdpal = NULL;
114
        }
115
 
116
        /* reset next user palette entry, write hardware palette*/
117
        GdResetPalette();
118
        GdSetPalette(psd, 0, (int)psd->ncolors, stdpal);
119
#if xxxALPHABLEND
120
        /* one-time create alpha lookup table for 8bpp systems (takes ~1 sec)*/
121
        if(psd->ncolors == 256)
122
                init_alpha_lookup();
123
#endif
124
 
125
#if !NOFONTSORCLIPPING
126
        /* init local vars*/
127
        GdSetMode(MWMODE_COPY);
128
        GdSetForeground(GdFindColor(MWRGB(255, 255, 255)));     /* WHITE*/
129
        GdSetBackground(GdFindColor(MWRGB(0, 0, 0)));              /* BLACK*/
130
        GdSetUseBackground(TRUE);
131
        GdSetFont(GdCreateFont(psd, MWFONT_SYSTEM_VAR, 0, NULL));
132
#if DYNAMICREGIONS
133
        GdSetClipRegion(psd,
134
                GdAllocRectRegion(0, 0, psd->xvirtres, psd->yvirtres));
135
#else
136
        GdSetClipRects(psd, 0, NULL);
137
#endif /* DYNAMICREGIONS*/
138
#endif /* NOFONTSORCLIPPING*/
139
 
140
        /* fill black (actually fill to first palette entry or truecolor 0*/
141
        psd->FillRect(psd, 0, 0, psd->xvirtres-1, psd->yvirtres-1, 0);
142
        return psd;
143
}
144
 
145
/*
146
 * Close low level graphics driver
147
 */
148
void
149
GdCloseScreen(PSD psd)
150
{
151
        psd->Close(psd);
152
}
153
 
154
/* Set dynamic screen portrait mode, return new mode*/
155
int
156
GdSetPortraitMode(PSD psd, int portraitmode)
157
{
158
        /* set portrait mode if supported*/
159
        if (psd->SetPortrait)
160
                psd->SetPortrait(psd, portraitmode);
161
        return psd->portrait;
162
}
163
 
164
/*
165
 * Return about the screen.
166
 */
167
void
168
GdGetScreenInfo(PSD psd, PMWSCREENINFO psi)
169
{
170
        psd->GetScreenInfo(psd, psi);
171
        GdGetButtonInfo(&psi->buttons);
172
        GdGetModifierInfo(&psi->modifiers, NULL);
173
        GdGetCursorPos(&psi->xpos, &psi->ypos);
174
}
175
 
176
/* reset palette to empty except for system colors*/
177
void
178
GdResetPalette(void)
179
{
180
        /* note: when palette entries are changed, all
181
         * windows may need to be redrawn
182
         */
183
        gr_nextpalentry = gr_firstuserpalentry;
184
}
185
 
186
/* set the system palette section to the passed palette entries*/
187
void
188
GdSetPalette(PSD psd, int first, int count, MWPALENTRY *palette)
189
{
190
        int     i;
191
 
192
        /* no palette management needed if running truecolor*/
193
        if(psd->pixtype != MWPF_PALETTE)
194
                return;
195
 
196
        /* bounds check against # of device color entries*/
197
        if(first + count > (int)psd->ncolors)
198
                count = (int)psd->ncolors - first;
199
        if(count >= 0 && first < (int)psd->ncolors) {
200
                psd->SetPalette(psd, first, count, palette);
201
 
202
                /* copy palette for GdFind*Color*/
203
                for(i=0; i<count; ++i)
204
                        gr_palette[i+first] = palette[i];
205
        }
206
}
207
 
208
/* get system palette entries, return count*/
209
int
210
GdGetPalette(PSD psd, int first, int count, MWPALENTRY *palette)
211
{
212
        int     i;
213
 
214
        /* no palette if running truecolor*/
215
        if(psd->pixtype != MWPF_PALETTE)
216
                return 0;
217
 
218
        /* bounds check against # of device color entries*/
219
        if(first + count > (int)psd->ncolors)
220
                if( (count = (int)psd->ncolors - first) <= 0)
221
                        return 0;
222
 
223
        for(i=0; i<count; ++i)
224
                *palette++ = gr_palette[i+first];
225
 
226
        return count;
227
}
228
 
229
/*
230
 * Convert a palette-independent value to a hardware color
231
 */
232
MWPIXELVAL
233
GdFindColor(MWCOLORVAL c)
234
{
235
        /*
236
         * Handle truecolor displays.  Note that the MWF_PALINDEX
237
         * bit is ignored when running truecolor drivers.
238
         */
239
        switch(gr_pixtype) {
240
        case MWPF_TRUECOLOR0888:
241
        case MWPF_TRUECOLOR888:
242
                /* create 24 bit 8/8/8 pixel (0x00RRGGBB) from RGB colorval*/
243
                /*RGB2PIXEL888(REDVALUE(c), GREENVALUE(c), BLUEVALUE(c))*/
244
                return COLOR2PIXEL888(c);
245
 
246
        case MWPF_TRUECOLOR565:
247
                /* create 16 bit 5/6/5 format pixel from RGB colorval*/
248
                /*RGB2PIXEL565(REDVALUE(c), GREENVALUE(c), BLUEVALUE(c))*/
249
                return COLOR2PIXEL565(c);
250
 
251
        case MWPF_TRUECOLOR555:
252
                /* create 16 bit 5/5/5 format pixel from RGB colorval*/
253
                /*RGB2PIXEL555(REDVALUE(c), GREENVALUE(c), BLUEVALUE(c))*/
254
                return COLOR2PIXEL555(c);
255
 
256
        case MWPF_TRUECOLOR332:
257
                /* create 8 bit 3/3/2 format pixel from RGB colorval*/
258
                /*RGB2PIXEL332(REDVALUE(c), GREENVALUE(c), BLUEVALUE(c))*/
259
                return COLOR2PIXEL332(c);
260
        }
261
 
262
        /* case MWPF_PALETTE: must be running 1, 2, 4 or 8 bit palette*/
263
 
264
        /*
265
         * Check if color is a palette index.  Note that the index
266
         * isn't error checked against the system palette, for speed.
267
         */
268
        if(c & MWF_PALINDEX)
269
                return (c & 0xff);
270
 
271
        /* search palette for closest match*/
272
        return GdFindNearestColor(gr_palette, (int)gr_ncolors, c);
273
}
274
 
275
/*
276
 * Search a palette to find the nearest color requested.
277
 * Uses a weighted squares comparison.
278
 */
279
MWPIXELVAL
280
GdFindNearestColor(MWPALENTRY *pal, int size, MWCOLORVAL cr)
281
{
282
        MWPALENTRY *    rgb;
283
        int             r, g, b;
284
        int             R, G, B;
285
        long            diff = 0x7fffffffL;
286
        long            sq;
287
        int             best = 0;
288
 
289
        r = REDVALUE(cr);
290
        g = GREENVALUE(cr);
291
        b = BLUEVALUE(cr);
292
        for(rgb=pal; diff && rgb < &pal[size]; ++rgb) {
293
                R = rgb->r - r;
294
                G = rgb->g - g;
295
                B = rgb->b - b;
296
#if 1
297
                /* speedy linear distance method*/
298
                sq = abs(R) + abs(G) + abs(B);
299
#else
300
                /* slower distance-cubed with luminance adjustment*/
301
                /* gray is .30R + .59G + .11B*/
302
                /* = (R*77 + G*151 + B*28)/256*/
303
                sq = (long)R*R*30*30 + (long)G*G*59*59 + (long)B*B*11*11;
304
#endif
305
 
306
                if(sq < diff) {
307
                        best = rgb - pal;
308
                        if((diff = sq) == 0)
309
                                return best;
310
                }
311
        }
312
        return best;
313
}
314
 
315
#if !VXWORKS
316
#include <unistd.h>
317
#include <fcntl.h>
318
/*
319
 * Create .bmp file from framebuffer data
320
 *
321
 * 1, 4, 8, 16, 24 and 32 bpp supported
322
 */
323
#define BI_RGB          0L
324
#define BI_RLE8         1L
325
#define BI_RLE4         2L
326
#define BI_BITFIELDS    3L
327
 
328
typedef unsigned char   BYTE;
329
typedef unsigned short  WORD;
330
typedef unsigned long   DWORD;
331
typedef long            LONG;
332
 
333
#pragma pack(1)
334
/* windows style bmp*/
335
typedef struct {
336
        /* BITMAPFILEHEADER*/
337
        BYTE    bfType[2];
338
        DWORD   bfSize;
339
        WORD    bfReserved1;
340
        WORD    bfReserved2;
341
        DWORD   bfOffBits;
342
        /* BITMAPINFOHEADER*/
343
        DWORD   BiSize;
344
        LONG    BiWidth;
345
        LONG    BiHeight;
346
        WORD    BiPlanes;
347
        WORD    BiBitCount;
348
        DWORD   BiCompression;
349
        DWORD   BiSizeImage;
350
        LONG    BiXpelsPerMeter;
351
        LONG    BiYpelsPerMeter;
352
        DWORD   BiClrUsed;
353
        DWORD   BiClrImportant;
354
} BMPHEAD;
355
#pragma pack()
356
 
357
/* r/g/b masks for non-palette bitmaps*/
358
#define RMASK332        0xe0
359
#define GMASK332        0x1c
360
#define BMASK332        0x03
361
#define RMASK555        0x7c00
362
#define GMASK555        0x03e0
363
#define BMASK555        0x001f
364
#define RMASK565        0xf800
365
#define GMASK565        0x07e0
366
#define BMASK565        0x001f
367
#define RMASK888        0xff0000
368
#define GMASK888        0x00ff00
369
#define BMASK888        0x0000ff
370
 
371
#if defined(HAVE_FILEIO)
372
static void
373
putsw(unsigned long dw, FILE *ofp)
374
{
375
        /* little-endian storage of shortword*/
376
        putc((unsigned char)dw, ofp);
377
        dw >>= 8;
378
        putc((unsigned char)dw, ofp);
379
}
380
 
381
static void
382
putdw(unsigned long dw, FILE *ofp)
383
{
384
        /* little-endian storage of longword*/
385
        putc((unsigned char)dw, ofp);
386
        dw >>= 8;
387
        putc((unsigned char)dw, ofp);
388
        dw >>= 8;
389
        putc((unsigned char)dw, ofp);
390
        dw >>= 8;
391
        putc((unsigned char)dw, ofp);
392
}
393
#endif /* HAVE_FILEIO*/
394
 
395
/* create .bmp file from framebuffer data*/
396
int
397
GdCaptureScreen(char *path)
398
{
399
#if defined(HAVE_FILEIO)
400
        int     ifd, i, j;
401
        FILE *  ofp;
402
        int     cx, cy, extra, bpp, bytespp, ncolors, sizecolortable;
403
        unsigned long rmask, gmask, bmask;
404
        unsigned char *cptr;
405
        unsigned short *sptr;
406
        unsigned long *lptr;
407
        BMPHEAD bmp;
408
        unsigned char buf[2048*4];
409
 
410
        ofp = fopen(path, "wb");
411
        if (!ofp)
412
                return 1;
413
        ifd = open("/dev/fb0", 0);
414
 
415
        cx = scrdev.xvirtres;
416
        cy = scrdev.yvirtres;
417
        bpp = scrdev.bpp;
418
        bytespp = (bpp+7)/8;
419
 
420
        /* dword right padded*/
421
        extra = (cx*bytespp) & 3;
422
        if (extra)
423
                extra = 4 - extra;
424
        ncolors = (bpp <= 8)? (1<<bpp): 0;
425
        /* color table is either palette or 3 longword r/g/b masks*/
426
        sizecolortable = ncolors? ncolors*4: 3*4;
427
        if (bpp == 24)
428
                sizecolortable = 0;      /* special case 24bpp has no table*/
429
 
430
        /* fill out bmp header*/
431
        memset(&bmp, 0, sizeof(bmp));
432
        bmp.bfType[0] = 'B';
433
        bmp.bfType[1] = 'M';
434
        bmp.bfSize = dwswap(sizeof(bmp) + sizecolortable + (long)(cx+extra)*cy*bytespp);
435
        bmp.bfOffBits = dwswap(sizeof(bmp) + sizecolortable);
436
        bmp.BiSize = dwswap(40);
437
        bmp.BiWidth = dwswap(cx);
438
        bmp.BiHeight = dwswap(cy);
439
        bmp.BiPlanes = wswap(1);
440
        bmp.BiBitCount = wswap(bpp);
441
        bmp.BiCompression = dwswap((bpp==16 || bpp==32)? BI_BITFIELDS: BI_RGB);
442
        bmp.BiSizeImage = dwswap((long)(cx+extra)*cy*bytespp);
443
        bmp.BiClrUsed = dwswap((bpp <= 8)? ncolors: 0);
444
        /*bmp.BiClrImportant = 0;*/
445
 
446
        /* write header*/
447
        fwrite(&bmp, sizeof(bmp), 1, ofp);
448
 
449
        /* write colortable*/
450
        if (sizecolortable) {
451
                if(bpp <= 8) {
452
                        /* write palette*/
453
                        for(i=0; i<ncolors; i++) {
454
                                putc(gr_palette[i].b, ofp);
455
                                putc(gr_palette[i].g, ofp);
456
                                putc(gr_palette[i].r, ofp);
457
                                putc(0, ofp);
458
                        }
459
                } else {
460
                        /* write 3 r/g/b masks*/
461
                        switch (gr_pixtype) {
462
                        case MWPF_TRUECOLOR0888:
463
                        default:
464
                                rmask = RMASK888;
465
                                gmask = GMASK888;
466
                                bmask = BMASK888;
467
                                break;
468
                        case MWPF_TRUECOLOR565:
469
                                rmask = RMASK565;
470
                                gmask = GMASK565;
471
                                bmask = BMASK565;
472
                                break;
473
                        case MWPF_TRUECOLOR555:
474
                                rmask = RMASK555;
475
                                gmask = GMASK555;
476
                                bmask = BMASK555;
477
                                break;
478
                        case MWPF_TRUECOLOR332:
479
                                rmask = RMASK332;
480
                                gmask = GMASK332;
481
                                bmask = BMASK332;
482
                                break;
483
                        }
484
                        putdw(rmask, ofp);
485
                        putdw(gmask, ofp);
486
                        putdw(bmask, ofp);
487
                }
488
        }
489
 
490
        /* write image data, upside down ;)*/
491
        for(i=cy-1; i>=0; --i) {
492
                long base = sizeof(bmp) + sizecolortable + (long)i*cx*bytespp;
493
                fseek(ofp, base, SEEK_SET);
494
                read(ifd, buf, cx*bytespp);
495
                switch (bpp) {
496
                case 32:
497
                        lptr = (unsigned long *)buf;
498
                        for(j=0; j<cx; ++j)
499
                                putdw(*lptr++, ofp);
500
                        break;
501
                case 24:
502
                        cptr = (unsigned char *)buf;
503
                        for(j=0; j<cx; ++j) {
504
                                putc(*cptr++, ofp);
505
                                putc(*cptr++, ofp);
506
                                putc(*cptr++, ofp);
507
                        }
508
                        break;
509
                case 16:
510
                        sptr = (unsigned short *)buf;
511
                        for(j=0; j<cx; ++j)
512
                                putsw(*sptr++, ofp);
513
                        break;
514
                default:
515
                        cptr = (unsigned char *)buf;
516
                        for(j=0; j<cx; ++j)
517
                                putc(*cptr++, ofp);
518
                        break;
519
                }
520
                for(j=0; j<extra; ++j)
521
                        putc(0, ofp);            /* DWORD pad each line*/
522
        }
523
 
524
        fclose(ofp);
525
        close(ifd);
526
#endif /* HAVE_FILEIO*/
527
        return 0;
528
}
529
#endif /* !VXWORKS*/

powered by: WebSVN 2.1.0

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